Annotation of embedaddon/libnet/src/libnet_advanced.c, revision 1.1.1.2

1.1       misho       1: /*
1.1.1.2 ! misho       2:  *  $Id: libnet_advanced.c,v 1.8 2004/11/09 07:05:07 mike Exp $
1.1       misho       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: #if (HAVE_CONFIG_H)
                     34: #include "../include/config.h"
                     35: #endif
                     36: #if (!(_WIN32) || (__CYGWIN__)) 
                     37: #include "../include/libnet.h"
                     38: #else
                     39: #include "../include/win32/libnet.h"
                     40: #endif
                     41: 
                     42: int
1.1.1.2 ! misho      43: libnet_adv_cull_packet(libnet_t *l, uint8_t **packet, uint32_t *packet_s)
1.1       misho      44: {
                     45:     *packet = NULL;
                     46:     *packet_s = 0;
                     47: 
                     48:     if (l->injection_type != LIBNET_LINK_ADV)
                     49:     {
                     50:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                     51:                 "%s(): advanced link mode not enabled\n", __func__);
                     52:         return (-1);
                     53:     }
                     54: 
                     55:     /* checksums will be written in */
                     56:     return (libnet_pblock_coalesce(l, packet, packet_s));
                     57: }
                     58: 
                     59: int
1.1.1.2 ! misho      60: libnet_adv_cull_header(libnet_t *l, libnet_ptag_t ptag, uint8_t **header,
        !            61:         uint32_t *header_s)
1.1       misho      62: {
                     63:     libnet_pblock_t *p;
                     64: 
                     65:     *header = NULL;
                     66:     *header_s = 0;
                     67: 
                     68:     if (l->injection_type != LIBNET_LINK_ADV)
                     69:     {
                     70:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                     71:                 "%s(): advanced link mode not enabled\n", __func__);
                     72:         return (-1);
                     73:     }
                     74: 
                     75:     p = libnet_pblock_find(l, ptag);
                     76:     if (p == NULL)
                     77:     {
                     78:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                     79:             "%s(): ptag not found, you sure it exists?\n", __func__);
                     80:         return (-1);
                     81:     }
                     82:     *header   = p->buf;
                     83:     *header_s = p->b_len;
                     84: 
                     85:     return (1);
                     86: }
                     87: 
                     88: int
1.1.1.2 ! misho      89: libnet_adv_write_link(libnet_t *l, const uint8_t *packet, uint32_t packet_s)
1.1       misho      90: {
                     91:     int c;
                     92: 
                     93:     if (l->injection_type != LIBNET_LINK_ADV)
                     94:     {
                     95:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                     96:                 "%s(): advanced link mode not enabled\n", __func__);
                     97:         return (-1);
                     98:     }
                     99:     c = libnet_write_link(l, packet, packet_s);
                    100: 
                    101:     /* do statistics */
                    102:     if (c == packet_s)
                    103:     {
                    104:         l->stats.packets_sent++;
                    105:         l->stats.bytes_written += c;
                    106:     }
                    107:     else
                    108:     {
                    109:         l->stats.packet_errors++;
                    110:         /*
                    111:          *  XXX - we probably should have a way to retrieve the number of
                    112:          *  bytes actually written (since we might have written something).
                    113:          */
                    114:         if (c > 0)
                    115:         {
                    116:             l->stats.bytes_written += c;
                    117:         }
                    118:     }
                    119:     return (c);
                    120: }
                    121: 
1.1.1.2 ! misho     122: int
        !           123: libnet_adv_write_raw_ipv4(libnet_t *l, const uint8_t *packet, uint32_t packet_s)
        !           124: {
        !           125:     int c;
        !           126: 
        !           127:     if (l->injection_type != LIBNET_RAW4_ADV)
        !           128:     {
        !           129:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
        !           130:                 "%s(): advanced raw4 mode not enabled\n", __func__);
        !           131:         return (-1);
        !           132:     }
        !           133:     c = libnet_write_raw_ipv4(l, packet, packet_s);
        !           134: 
        !           135:     /* do statistics */
        !           136:     if (c == packet_s)
        !           137:     {
        !           138:         l->stats.packets_sent++;
        !           139:         l->stats.bytes_written += c;
        !           140:     }
        !           141:     else
        !           142:     {
        !           143:         l->stats.packet_errors++;
        !           144:         /*
        !           145:          *  XXX - we probably should have a way to retrieve the number of
        !           146:          *  bytes actually written (since we might have written something).
        !           147:          */
        !           148:         if (c > 0)
        !           149:         {
        !           150:             l->stats.bytes_written += c;
        !           151:         }
        !           152:     }
        !           153:     return (c);
        !           154: }
        !           155: 
1.1       misho     156: void
1.1.1.2 ! misho     157: libnet_adv_free_packet(libnet_t *l, uint8_t *packet)
1.1       misho     158: {
                    159:     /*
                    160:      *  Restore original pointer address so free won't complain about a
                    161:      *  modified chunk pointer.
                    162:      */
                    163:     if (l->aligner > 0)
                    164:     {
                    165:         packet = packet - l->aligner;
                    166:     }
                    167:     free(packet);
                    168: }
                    169: 
                    170: /* EOF */

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