Annotation of embedaddon/libnet/src/libnet_init.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  *  $Id: libnet_init.c,v 1.17 2004/03/16 18:40:59 mike Exp $
        !             3:  *
        !             4:  *  libnet
        !             5:  *  libnet_init.c - Initilization 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: libnet_t *
        !            43: libnet_init(int injection_type, char *device, char *err_buf)
        !            44: {
        !            45:     libnet_t *l = NULL;
        !            46: 
        !            47: #if !defined(__WIN32__)
        !            48:     if (getuid() && geteuid())
        !            49:     {
        !            50:         snprintf(err_buf, LIBNET_ERRBUF_SIZE,
        !            51:                 "%s(): UID or EUID of 0 required\n", __func__);
        !            52:         goto bad;
        !            53:     }
        !            54: #else
        !            55:     WSADATA wsaData;
        !            56: 
        !            57:     if ((WSAStartup(0x0202, &wsaData)) != 0)
        !            58:     {
        !            59:         snprintf(err_buf, LIBNET_ERRBUF_SIZE, 
        !            60:                 "%s(): unable to initialize winsock 2\n", __func__);
        !            61:         goto bad;
        !            62:     }
        !            63: #endif
        !            64: 
        !            65:     l = (libnet_t *)malloc(sizeof (libnet_t));
        !            66:     if (l == NULL)
        !            67:     {
        !            68:         snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s(): malloc(): %s\n", __func__,
        !            69:                 strerror(errno));
        !            70:         goto bad;
        !            71:     }
        !            72:        
        !            73:     memset(l, 0, sizeof (*l));
        !            74: 
        !            75:     l->injection_type   = injection_type;
        !            76:     l->ptag_state       = LIBNET_PTAG_INITIALIZER;
        !            77:     l->device           = (device ? strdup(device) : NULL);
        !            78: 
        !            79:     strncpy(l->label, LIBNET_LABEL_DEFAULT, LIBNET_LABEL_SIZE);
        !            80:     l->label[sizeof(l->label)] = '\0';
        !            81: 
        !            82:     switch (l->injection_type)
        !            83:     {
        !            84:         case LIBNET_LINK:
        !            85:         case LIBNET_LINK_ADV:
        !            86:             if (libnet_select_device(l) == -1)
        !            87:             {
        !            88:                 snprintf(err_buf, LIBNET_ERRBUF_SIZE, l->err_buf);
        !            89:                goto bad;
        !            90:             }
        !            91:             if (libnet_open_link(l) == -1)
        !            92:             {
        !            93:                 snprintf(err_buf, LIBNET_ERRBUF_SIZE, l->err_buf);
        !            94:                 goto bad;
        !            95:             }
        !            96:             break;
        !            97:         case LIBNET_RAW4:
        !            98:         case LIBNET_RAW4_ADV:
        !            99:             if (libnet_open_raw4(l) == -1)
        !           100:             {
        !           101:                 snprintf(err_buf, LIBNET_ERRBUF_SIZE, l->err_buf);
        !           102:                 goto bad;
        !           103:             }
        !           104:             break;
        !           105:         case LIBNET_RAW6:
        !           106:         case LIBNET_RAW6_ADV:
        !           107:             if (libnet_open_raw6(l) == -1)
        !           108:             {
        !           109:                 snprintf(err_buf, LIBNET_ERRBUF_SIZE, l->err_buf);
        !           110:                 goto bad;
        !           111:             }
        !           112:             break;
        !           113:         default:
        !           114:             snprintf(err_buf, LIBNET_ERRBUF_SIZE,
        !           115:                     "%s(): unsupported injection type\n", __func__);
        !           116:             goto bad;
        !           117:             break;
        !           118:     }
        !           119: 
        !           120:     return (l);
        !           121: 
        !           122: bad:
        !           123:     if (l)
        !           124:     {
        !           125:         libnet_destroy(l);
        !           126:     }
        !           127:     return (NULL);
        !           128: }
        !           129: 
        !           130: void
        !           131: libnet_destroy(libnet_t *l)
        !           132: {
        !           133:     if (l)
        !           134:     {
        !           135:         close(l->fd);
        !           136:         if (l->device)
        !           137:         {
        !           138:             free(l->device);
        !           139:         }
        !           140:         libnet_clear_packet(l);
        !           141:         free(l);
        !           142:     }
        !           143: }
        !           144: 
        !           145: void
        !           146: libnet_clear_packet(libnet_t *l)
        !           147: {
        !           148:     libnet_pblock_t *p;
        !           149:     libnet_pblock_t *next;
        !           150: 
        !           151:     if (l)
        !           152:     {
        !           153:         p = l->protocol_blocks;
        !           154:         if (p)
        !           155:         {
        !           156:             for (; p; p = next)
        !           157:             {
        !           158:                 next = p->next;
        !           159:                 if (p->buf)
        !           160:                 {
        !           161:                     free(p->buf);
        !           162:                 }
        !           163:                 free(p);
        !           164:             }
        !           165:         }
        !           166:         l->protocol_blocks = NULL;
        !           167:         l->total_size = 0;
        !           168:     }
        !           169: }
        !           170: 
        !           171: void
        !           172: libnet_stats(libnet_t *l, struct libnet_stats *ls)
        !           173: {
        !           174:     if (l == NULL)
        !           175:     { 
        !           176:         return;
        !           177:     }
        !           178: 
        !           179:     ls->packets_sent  = l->stats.packets_sent;
        !           180:     ls->packet_errors = l->stats.packet_errors;
        !           181:     ls->bytes_written = l->stats.bytes_written;
        !           182: }
        !           183: 
        !           184: int
        !           185: libnet_getfd(libnet_t *l)
        !           186: {
        !           187:     if (l == NULL)
        !           188:     { 
        !           189:         return (-1);
        !           190:     } 
        !           191: 
        !           192:     return (l->fd);
        !           193: }
        !           194: 
        !           195: int8_t *
        !           196: libnet_getdevice(libnet_t *l)
        !           197: {
        !           198:     if (l == NULL)
        !           199:     { 
        !           200:         return (NULL);
        !           201:     }
        !           202: 
        !           203:     return (l->device);
        !           204: }
        !           205: 
        !           206: u_int8_t *
        !           207: libnet_getpbuf(libnet_t *l, libnet_ptag_t ptag)
        !           208: {
        !           209:     libnet_pblock_t *p;
        !           210: 
        !           211:     if (l == NULL)
        !           212:     { 
        !           213:         return (NULL);
        !           214:     }
        !           215: 
        !           216:     p = libnet_pblock_find(l, ptag);
        !           217:     if (p == NULL)
        !           218:     {
        !           219:         /* err msg set in libnet_pblock_find() */
        !           220:         return (NULL);
        !           221:     }
        !           222:     else
        !           223:     {
        !           224:         return (p->buf);
        !           225:     }
        !           226: }
        !           227: 
        !           228: u_int32_t
        !           229: libnet_getpbuf_size(libnet_t *l, libnet_ptag_t ptag)
        !           230: {
        !           231:     libnet_pblock_t *p;
        !           232: 
        !           233:     if (l == NULL)
        !           234:     { 
        !           235:         return (0);
        !           236:     } 
        !           237: 
        !           238:     p = libnet_pblock_find(l, ptag);
        !           239:     if (p == NULL)
        !           240:     {
        !           241:         /* err msg set in libnet_pblock_find() */
        !           242:         return (0);
        !           243:     }
        !           244:     else
        !           245:     {
        !           246:         return (p->b_len);
        !           247:     }
        !           248: }
        !           249: 
        !           250: u_int32_t
        !           251: libnet_getpacket_size(libnet_t *l)
        !           252: {
        !           253:     libnet_pblock_t *p;
        !           254:     u_int32_t n;
        !           255: 
        !           256:     if (l == NULL)
        !           257:     { 
        !           258:         return (0);
        !           259:     } 
        !           260: 
        !           261:     n = 0;
        !           262:     p = l->protocol_blocks;
        !           263:     if (p)
        !           264:     {
        !           265:         for (; p; p = p->next)
        !           266:         {
        !           267:             n += p->b_len;
        !           268:         }
        !           269:     }
        !           270:     return (n);
        !           271: }
        !           272: 
        !           273: /* EOF */

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