File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / sample / isl.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 22:14:23 2012 UTC (12 years, 4 months ago) by misho
Branches: libnet, MAIN
CVS tags: v1_1_6p5, v1_1_6p4, v1_1_6p0, v1_1_6, v1_1_2_1, HEAD
libnet

    1: /*
    2:  *  $Id: isl.c,v 1.1.1.1 2012/02/21 22:14:23 misho Exp $
    3:  *
    4:  *  libnet 1.1
    5:  *  Build an ISL packet
    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: #include "./libnet_test.h"
   37: 
   38: int
   39: main(int argc, char *argv[])
   40: {
   41:     int c, len;
   42:     libnet_t *l;
   43:     libnet_ptag_t t;
   44:     u_char *dst;
   45:     u_long ip;
   46:     u_char dhost[5] = {0x01, 0x00, 0x0c, 0x00, 0x00};
   47:     u_char snap[6]  = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
   48:     char *device = NULL;
   49:     char errbuf[LIBNET_ERRBUF_SIZE];
   50: 
   51:     printf("libnet 1.1 packet shaping: [ISL]\n");
   52: 
   53:     device = NULL;
   54:     dst = NULL;
   55:     while ((c = getopt(argc, argv, "i:d:")) != EOF)
   56:     {
   57:         switch (c)
   58:         {
   59:             case 'd':
   60:                 dst = libnet_hex_aton(optarg, &len);
   61:                 break;
   62:             case 'i':
   63:                 device = optarg;
   64:                 break;
   65:         }
   66:     }
   67: 
   68:     if (dst == NULL)
   69:     {
   70:         fprintf(stderr, "usage %s -d dst [-i interface]\n",
   71:                 argv[0]);
   72:         exit(EXIT_FAILURE);
   73:     }
   74: 
   75:     /*
   76:      *  Initialize the library.  Root priviledges are required.
   77:      */
   78:     l = libnet_init(
   79:             LIBNET_LINK,                            /* injection type */
   80:             device,                                 /* network interface */
   81:             errbuf);                                /* errbuf */
   82: 
   83:     if (l == NULL)
   84:     {
   85:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
   86:         exit(EXIT_FAILURE);
   87:     }
   88: 
   89:     ip = libnet_get_ipaddr4(l);
   90: 
   91:     t = libnet_build_arp(
   92:             ARPHRD_ETHER,                           /* hardware addr */
   93:             ETHERTYPE_IP,                           /* protocol addr */
   94:             6,                                      /* hardware addr size */
   95:             4,                                      /* protocol addr size */
   96:             ARPOP_REPLY,                            /* operation type */
   97:             enet_src,                               /* sender hardware addr */
   98:             (u_char *)&ip,                          /* sender protocol addr */
   99:             enet_dst,                               /* target hardware addr */
  100:             (u_char *)&ip,                          /* target protocol addr */
  101:             NULL,                                   /* payload */
  102:             0,                                      /* payload size */
  103:             l,                                      /* libnet handle */
  104:             0);                                     /* libnet id */
  105:     if (t == -1)
  106:     {
  107:         fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(l));
  108:         goto bad;
  109:     }
  110: 
  111:     t = libnet_autobuild_ethernet(
  112:             dst,                                    /* ethernet destination */
  113:             ETHERTYPE_ARP,                          /* protocol type */
  114:             l);                                     /* libnet handle */
  115:     if (t == -1)
  116:     {
  117:         fprintf(stderr, "Can't build ethernet header: %s\n",
  118:                 libnet_geterror(l));
  119:         goto bad;
  120:     }
  121: 
  122:     t = libnet_build_isl(
  123:             dhost,
  124:             0x0,
  125:             0x0,
  126:             enet_src,
  127:             10 + 42,
  128:             snap,
  129:             10,
  130:             0x8000,
  131:             0,
  132:             NULL,                                   /* payload */
  133:             0,                                      /* payload size */
  134:             l,                                      /* libnet handle */
  135:             0);                                     /* libnet id */
  136:     if (t == -1)
  137:     {
  138:         fprintf(stderr, "Can't build ISL header: %s\n", 
  139:                 libnet_geterror(l));
  140:         goto bad;
  141:     }
  142: 
  143: 
  144:     /*
  145:      *  Write it to the wire.
  146:      */
  147:     c = libnet_write(l);
  148: 
  149:     if (c == -1)
  150:     {
  151:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
  152:         goto bad;
  153:     }
  154:     else
  155:     {
  156:         fprintf(stderr, "Wrote %d byte ISL packet; check the wire.\n", c);
  157:     }
  158:     free(dst);
  159:     libnet_destroy(l);
  160:     return (EXIT_SUCCESS);
  161: bad:
  162:     free(dst);
  163:     libnet_destroy(l);
  164:     return (EXIT_FAILURE);
  165: }
  166: 
  167: /* EOF */

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