Annotation of embedaddon/libnet/sample/icmp_echo_cq.c, revision 1.1.1.2

1.1       misho       1: /*
                      2:  *  $Id: icmp_echo_cq.c,v 1.3 2004/01/03 20:31:01 mike Exp $
                      3:  *
                      4:  *  libnet 1.1
                      5:  *  Build ICMP_ECHO packets using the context queue interface.
                      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 "./libnet_test.h"
                     34: 
                     35: void usage(char *);
                     36: 
                     37: 
                     38: int
                     39: main(int argc, char **argv)
                     40: {
                     41:     libnet_t *l = NULL;
                     42:     u_long src_ip = 0, dst_ip = 0;
                     43:     u_long count = 10;
                     44:     int i, c;
                     45:     libnet_ptag_t t;
                     46:     char *payload = NULL;
                     47:     u_short payload_s = 0;
                     48:   
                     49:     char *device = NULL;
                     50:     char *pDst = NULL, *pSrc = NULL;
                     51:     char errbuf[LIBNET_ERRBUF_SIZE];
                     52:     char label[LIBNET_LABEL_SIZE];
                     53: 
                     54:     printf("libnet 1.1 packet shaping: ICMP[RAW using context queue]\n");
                     55: 
                     56:     while((c = getopt(argc, argv, "d:s:i:c:p:")) != EOF)
                     57:     {
                     58:         switch (c)
                     59:         {
                     60:         case 'd':
                     61:             pDst = optarg;
                     62:             break;
                     63:         case 's':
                     64:             pSrc = optarg;
                     65:             break;
                     66:         case 'i':
                     67:             device = optarg;
                     68:             break;
                     69:         case 'c':
                     70:             count = strtoul(optarg, 0, 10);
                     71:             break;
                     72:         case 'p':
                     73:             payload = optarg;
                     74:             payload_s = strlen(payload);
                     75:             break;
                     76:         }
                     77:     }
                     78: 
                     79:     if (!pSrc || !pDst)
                     80:     {
                     81:         usage(argv[0]);
                     82:         exit(EXIT_FAILURE);
                     83:     }
                     84: 
                     85:     /*
                     86:      *  Fill the context queue with "count" packets, each with their own
                     87:      *  context.
                     88:      */
                     89:     for (i = 0; i < count; i++)
                     90:     {
                     91:         l = libnet_init(
                     92:                 LIBNET_RAW4,                  /* injection type */
                     93:                 device,                       /* network interface */
                     94:                 errbuf);                      /* errbuf */
                     95: 
                     96:         if (l == NULL)
                     97:         {
                     98:             /* we should run through the queue and free any stragglers */
                     99:             fprintf(stderr, "libnet_init() failed: %s", errbuf);
                    100:             exit(EXIT_FAILURE);
                    101:         }
                    102:         /*
                    103:          *  Since we need a libnet context for address resolution it is
                    104:          *  necessary to put this inside the loop.
                    105:          */
                    106:         if (!dst_ip && (dst_ip = libnet_name2addr4(l, pDst,
                    107:                 LIBNET_RESOLVE)) == -1)
                    108:         {
                    109:             fprintf(stderr, "Bad destination IP address: %s\n", pDst);
                    110:             exit(1);
                    111:         }
                    112:         if (!src_ip && (src_ip = libnet_name2addr4(l, pSrc,
                    113:                 LIBNET_RESOLVE)) == -1)
                    114:         {
                    115:             fprintf(stderr, "Bad source IP address: %s\n", pSrc);
                    116:             exit(1);
                    117:         }
                    118: 
                    119:         t = libnet_build_icmpv4_echo(
                    120:             ICMP_ECHO,                            /* type */
                    121:             0,                                    /* code */
                    122:             0,                                    /* checksum */
                    123:             0x42,                                 /* id */
                    124:             0x42,                                 /* sequence number */
                    125:             NULL,                                 /* payload */
                    126:             0,                                    /* payload size */
                    127:             l,                                    /* libnet handle */
                    128:             0);
                    129:         if (t == -1)
                    130:         {
                    131:             fprintf(stderr, "Can't build ICMP header: %s\n",
                    132:                     libnet_geterror(l));
                    133:             goto bad;
                    134:         }
                    135: 
                    136:         t = libnet_build_ipv4(
                    137:             LIBNET_IPV4_H + LIBNET_ICMPV4_ECHO_H + payload_s, /* length */
                    138:             0,                                    /* TOS */
                    139:             0x42,                                 /* IP ID */
                    140:             0,                                    /* IP Frag */
                    141:             64,                                   /* TTL */
                    142:             IPPROTO_ICMP,                         /* protocol */
                    143:             0,                                    /* checksum */
                    144:             src_ip,                               /* source IP */
                    145:             dst_ip,                               /* destination IP */
1.1.1.2 ! misho     146:             (uint8_t *)payload,                   /* payload */
1.1       misho     147:             payload_s,                            /* payload size */
                    148:             l,                                    /* libnet handle */
                    149:             0);
                    150:         if (t == -1)
                    151:         {
                    152:             fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
                    153:             goto bad;
                    154:         }
                    155: 
                    156:         /* and finally, put it in the context queue */
                    157:         snprintf(label, sizeof(label)-1, "echo %d", i);
                    158:         if (libnet_cq_add(l, label) == -1)
                    159:         {
                    160:             fprintf(stderr, "add error: %s\n", libnet_geterror(l));
                    161:             goto bad;
                    162:         }
                    163:     }
                    164: 
                    165:     for_each_context_in_cq(l)
                    166:     {
                    167:         c = libnet_write(l);
                    168:         if (c == -1)
                    169:         {
                    170:             fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
                    171:             goto bad;
                    172:         }
                    173:         else
                    174:         {
                    175:             fprintf(stderr, "Wrote %d byte ICMP packet from context \"%s\"; "
                    176:                     "check the wire.\n", c, libnet_cq_getlabel(l));
                    177:         }
                    178:   }
                    179: 
                    180:     libnet_cq_destroy();
                    181:     return (EXIT_SUCCESS);
                    182: bad:
                    183:     libnet_cq_destroy();
                    184:     libnet_destroy(l);
                    185:     return (EXIT_FAILURE);
                    186: }
                    187: 
                    188: void
                    189: usage(char *name)
                    190: {
                    191:     fprintf(stderr, "usage: %s -s source_ip -d destination_ip"
                    192:                     " [-i iface] [-c count = 10]\n ", name);
                    193: }
                    194: 

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