File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / src / libnet_advanced.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Sep 27 11:11:38 2023 UTC (9 months, 2 weeks ago) by misho
Branches: libnet, MAIN
CVS tags: v1_2p1, HEAD
Version 1.2p1

    1: /*
    2:  *  $Id: libnet_advanced.c,v 1.1.1.3 2023/09/27 11:11:38 misho Exp $
    3:  *
    4:  *  libnet
    5:  *  libnet_advanced.c - Advanced routines
    6:  *
    7:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
    8:  *  All rights reserved.
    9:  *
   10:  * Redistribution and use in source and binary forms, with or without
   11:  * modification, are permitted provided that the following conditions
   12:  * are met:
   13:  * 1. Redistributions of source code must retain the above copyright
   14:  *    notice, this list of conditions and the following disclaimer.
   15:  * 2. Redistributions in binary form must reproduce the above copyright
   16:  *    notice, this list of conditions and the following disclaimer in the
   17:  *    documentation and/or other materials provided with the distribution.
   18:  *
   19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29:  * SUCH DAMAGE.
   30:  *
   31:  */
   32: 
   33: #include "common.h"
   34: 
   35: int
   36: libnet_adv_cull_packet(libnet_t *l, uint8_t **packet, uint32_t *packet_s)
   37: {
   38:     *packet = NULL;
   39:     *packet_s = 0;
   40: 
   41:     if (l->injection_type != LIBNET_LINK_ADV)
   42:     {
   43:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
   44:                 "%s(): advanced link mode not enabled", __func__);
   45:         return (-1);
   46:     }
   47: 
   48:     /* checksums will be written in */
   49:     return (libnet_pblock_coalesce(l, packet, packet_s));
   50: }
   51: 
   52: int
   53: libnet_adv_cull_header(libnet_t *l, libnet_ptag_t ptag, uint8_t **header,
   54:         uint32_t *header_s)
   55: {
   56:     libnet_pblock_t *p;
   57: 
   58:     *header = NULL;
   59:     *header_s = 0;
   60: 
   61:     if (l->injection_type != LIBNET_LINK_ADV)
   62:     {
   63:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
   64:                 "%s(): advanced link mode not enabled", __func__);
   65:         return (-1);
   66:     }
   67: 
   68:     p = libnet_pblock_find(l, ptag);
   69:     if (p == NULL)
   70:     {
   71:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
   72:             "%s(): ptag not found, you sure it exists?", __func__);
   73:         return (-1);
   74:     }
   75:     *header   = p->buf;
   76:     *header_s = p->b_len;
   77: 
   78:     return (1);
   79: }
   80: 
   81: int
   82: libnet_adv_write_link(libnet_t *l, const uint8_t *packet, uint32_t packet_s)
   83: {
   84:     int c;
   85: 
   86:     if (l->injection_type != LIBNET_LINK_ADV)
   87:     {
   88:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
   89:                 "%s(): advanced link mode not enabled", __func__);
   90:         return (-1);
   91:     }
   92:     c = libnet_write_link(l, packet, packet_s);
   93: 
   94:     /* do statistics */
   95:     if (c == packet_s)
   96:     {
   97:         l->stats.packets_sent++;
   98:         l->stats.bytes_written += c;
   99:     }
  100:     else
  101:     {
  102:         l->stats.packet_errors++;
  103:         /*
  104:          *  XXX - we probably should have a way to retrieve the number of
  105:          *  bytes actually written (since we might have written something).
  106:          */
  107:         if (c > 0)
  108:         {
  109:             l->stats.bytes_written += c;
  110:         }
  111:     }
  112:     return (c);
  113: }
  114: 
  115: int
  116: libnet_adv_write_raw_ipv4(libnet_t *l, const uint8_t *packet, uint32_t packet_s)
  117: {
  118:     int c;
  119: 
  120:     if (l->injection_type != LIBNET_RAW4_ADV)
  121:     {
  122:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
  123:                 "%s(): advanced raw4 mode not enabled", __func__);
  124:         return (-1);
  125:     }
  126:     c = libnet_write_raw_ipv4(l, packet, packet_s);
  127: 
  128:     /* do statistics */
  129:     if (c == packet_s)
  130:     {
  131:         l->stats.packets_sent++;
  132:         l->stats.bytes_written += c;
  133:     }
  134:     else
  135:     {
  136:         l->stats.packet_errors++;
  137:         /*
  138:          *  XXX - we probably should have a way to retrieve the number of
  139:          *  bytes actually written (since we might have written something).
  140:          */
  141:         if (c > 0)
  142:         {
  143:             l->stats.bytes_written += c;
  144:         }
  145:     }
  146:     return (c);
  147: }
  148: 
  149: void
  150: libnet_adv_free_packet(libnet_t *l, uint8_t *packet)
  151: {
  152:     /*
  153:      *  Restore original pointer address so free won't complain about a
  154:      *  modified chunk pointer.
  155:      */
  156:     if (l->aligner > 0)
  157:     {
  158:         packet = packet - l->aligner;
  159:     }
  160:     free(packet);
  161: }
  162: 
  163: /**
  164:  * Local Variables:
  165:  *  indent-tabs-mode: nil
  166:  *  c-file-style: "stroustrup"
  167:  * End:
  168:  */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>