File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / src / libnet_init.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_init.c,v 1.1.1.3 2023/09/27 11:11:38 misho 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: #include "common.h"
   34: 
   35: libnet_t *
   36: libnet_init(int injection_type, const char *device, char *err_buf)
   37: {
   38:     libnet_t *l = NULL;
   39: 
   40: #if defined(__WIN32__)
   41:     WSADATA wsaData;
   42: 
   43:     if ((WSAStartup(0x0202, &wsaData)) != 0)
   44:     {
   45:         snprintf(err_buf, LIBNET_ERRBUF_SIZE, 
   46:                 "%s(): unable to initialize winsock 2", __func__);
   47:         goto bad;
   48:     }
   49: #endif
   50: 
   51:     l = (libnet_t *)malloc(sizeof (libnet_t));
   52:     if (l == NULL)
   53:     {
   54:         snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s(): malloc(): %s", __func__,
   55:                 strerror(errno));
   56:         goto bad;
   57:     }
   58: 	
   59:     memset(l, 0, sizeof (*l));
   60: 
   61:     l->injection_type   = injection_type;
   62:     l->ptag_state       = LIBNET_PTAG_INITIALIZER;
   63:     l->device           = (device ? strdup(device) : NULL);
   64:     l->fd               = -1;
   65: 
   66:     strncpy(l->label, LIBNET_LABEL_DEFAULT, LIBNET_LABEL_SIZE);
   67:     l->label[LIBNET_LABEL_SIZE - 1] = '\0';
   68: 
   69:     switch (l->injection_type)
   70:     {
   71:         case LIBNET_NONE:
   72:             break;
   73:         case LIBNET_LINK:
   74:         case LIBNET_LINK_ADV:
   75:             if (libnet_select_device(l) == -1)
   76:             {
   77:                 snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s", l->err_buf);
   78: 		goto bad;
   79:             }
   80:             if (libnet_open_link(l) == -1)
   81:             {
   82:                 snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s", l->err_buf);
   83:                 goto bad;
   84:             }
   85:             break;
   86:         case LIBNET_RAW4:
   87:         case LIBNET_RAW4_ADV:
   88:             if (libnet_open_raw4(l) == -1)
   89:             {
   90:                 snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s", l->err_buf);
   91:                 goto bad;
   92:             }
   93:             break;
   94:         case LIBNET_RAW6:
   95:         case LIBNET_RAW6_ADV:
   96:             if (libnet_open_raw6(l) == -1)
   97:             {
   98:                 snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s", l->err_buf);
   99:                 goto bad;
  100:             }
  101:             break;
  102:         default:
  103:             snprintf(err_buf, LIBNET_ERRBUF_SIZE,
  104:                     "%s(): unsupported injection type", __func__);
  105:             goto bad;
  106:             break;
  107:     }
  108: 
  109:     return (l);
  110: 
  111: bad:
  112:     if (l)
  113:     {
  114:         libnet_destroy(l);
  115:     }
  116:     return (NULL);
  117: }
  118: 
  119: void
  120: libnet_destroy(libnet_t *l)
  121: {
  122:     if (l)
  123:     {
  124:         if (l->fd != -1)
  125:             close(l->fd);
  126:         free(l->device);
  127:         libnet_clear_packet(l);
  128:         free(l);
  129:     }
  130: }
  131: 
  132: void
  133: libnet_clear_packet(libnet_t *l)
  134: {
  135:     libnet_pblock_t *p;
  136: 
  137:     if (!l)
  138:     {
  139:         return;
  140:     }
  141: 
  142:     while((p = l->protocol_blocks))
  143:     {
  144:         libnet_pblock_delete(l, p);
  145:     }
  146: 
  147:     /* All pblocks are deleted, so start the tag count over from 1. */
  148:     l->ptag_state = 0;
  149: }
  150: 
  151: void
  152: libnet_stats(libnet_t *l, struct libnet_stats *ls)
  153: {
  154:     if (l == NULL)
  155:     { 
  156:         return;
  157:     }
  158: 
  159:     ls->packets_sent  = l->stats.packets_sent;
  160:     ls->packet_errors = l->stats.packet_errors;
  161:     ls->bytes_written = l->stats.bytes_written;
  162: }
  163: 
  164: int
  165: libnet_getfd(libnet_t *l)
  166: {
  167:     if (l == NULL)
  168:     { 
  169:         return (-1);
  170:     } 
  171: 
  172:     return (int)(l->fd);
  173: }
  174: 
  175: const char *
  176: libnet_getdevice(libnet_t *l)
  177: {
  178:     if (l == NULL)
  179:     { 
  180:         return (NULL);
  181:     }
  182: 
  183:     return (l->device);
  184: }
  185: 
  186: uint8_t *
  187: libnet_getpbuf(libnet_t *l, libnet_ptag_t ptag)
  188: {
  189:     libnet_pblock_t *p;
  190: 
  191:     if (l == NULL)
  192:     { 
  193:         return (NULL);
  194:     }
  195: 
  196:     p = libnet_pblock_find(l, ptag);
  197:     if (p == NULL)
  198:     {
  199:         /* err msg set in libnet_pblock_find() */
  200:         return (NULL);
  201:     }
  202:     else
  203:     {
  204:         return (p->buf);
  205:     }
  206: }
  207: 
  208: uint32_t
  209: libnet_getpbuf_size(libnet_t *l, libnet_ptag_t ptag)
  210: {
  211:     libnet_pblock_t *p;
  212: 
  213:     if (l == NULL)
  214:     { 
  215:         return (0);
  216:     } 
  217: 
  218:     p = libnet_pblock_find(l, ptag);
  219:     if (p == NULL)
  220:     {
  221:         /* err msg set in libnet_pblock_find() */
  222:         return (0);
  223:     }
  224:     else
  225:     {
  226:         return (p->b_len);
  227:     }
  228: }
  229: 
  230: uint32_t
  231: libnet_getpacket_size(libnet_t *l)
  232: {
  233:     /* Why doesn't this return l->total_size? */
  234:     libnet_pblock_t *p;
  235:     uint32_t n;
  236: 
  237:     if (l == NULL)
  238:     { 
  239:         return (0);
  240:     } 
  241: 
  242:     n = 0;
  243:     p = l->protocol_blocks;
  244:     if (p)
  245:     {
  246:         for (; p; p = p->next)
  247:         {
  248:             n += p->b_len;
  249:         }
  250:     }
  251:     return (n);
  252: }
  253: 
  254: /**
  255:  * Local Variables:
  256:  *  indent-tabs-mode: nil
  257:  *  c-file-style: "stroustrup"
  258:  * End:
  259:  */

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