Annotation of embedaddon/libnet/sample/udp1.c, revision 1.1.1.3

1.1       misho       1: /*
                      2:  *  $Id: udp1.c,v 1.6 2004/03/01 20:26:12 mike Exp $
                      3:  *
                      4:  *  libnet 1.1
                      5:  *  Build a UDP 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: #include "./libnet_test.h"
                     34: 
                     35: int
                     36: main(int argc, char *argv[])
                     37: {
                     38:     int c, i, j, build_ip;
1.1.1.2   misho      39:     char *cp;
1.1       misho      40:     libnet_t *l;
                     41:     libnet_ptag_t ip, ipo;
                     42:     libnet_ptag_t udp;
                     43:     char *payload;
                     44:     u_short payload_s;
                     45:     struct libnet_stats ls;
                     46:     u_long src_ip, dst_ip;
                     47:     u_short src_prt, dst_prt;
                     48:     u_char opt[20];
                     49:     char errbuf[LIBNET_ERRBUF_SIZE];
                     50: 
                     51:     printf("libnet 1.1 packet shaping: UDP + IP options[raw]\n"); 
                     52: 
                     53:     /*
                     54:      *  Initialize the library.  Root priviledges are required.
                     55:      */
                     56:     l = libnet_init(
                     57:             LIBNET_RAW4,                            /* injection type */
                     58:             NULL,                                   /* network interface */
                     59:             errbuf);                                /* errbuf */
                     60: 
                     61:     if (l == NULL)
                     62:     {
                     63:         fprintf(stderr, "libnet_init() failed: %s\n", errbuf);
                     64:         exit(EXIT_FAILURE);
                     65:     }
                     66: 
                     67:     src_ip  = 0;
                     68:     dst_ip  = 0;
                     69:     src_prt = 0;
                     70:     dst_prt = 0;
                     71:     payload = NULL;
                     72:     payload_s = 0;
                     73:     ip = ipo = udp = 0;
                     74:     while ((c = getopt(argc, argv, "d:s:p:")) != EOF)
                     75:     {
                     76:         switch (c)
                     77:         {
                     78:             /*
                     79:              *  We expect the input to be of the form `ip.ip.ip.ip.port`.  We
                     80:              *  point cp to the last dot of the IP address/port string and
                     81:              *  then seperate them with a NULL byte.  The optarg now points to
                     82:              *  just the IP address, and cp points to the port.
                     83:              */
                     84:             case 'd':
                     85:                 if (!(cp = strrchr(optarg, '.')))
                     86:                 {
                     87:                     usage(argv[0]);
                     88:                 }
                     89:                 *cp++ = 0;
                     90:                 dst_prt = (u_short)atoi(cp);
                     91:                 if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
                     92:                 {
                     93:                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);                    exit(EXIT_FAILURE);
                     94:                 }
                     95:                 break;
                     96:             case 's':
                     97:                 if (!(cp = strrchr(optarg, '.')))
                     98:                 {
                     99:                     usage(argv[0]);
                    100:                 }
                    101:                 *cp++ = 0;
                    102:                 src_prt = (u_short)atoi(cp);
                    103:                 if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
                    104:                 {
                    105:                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
                    106:                     exit(EXIT_FAILURE);
                    107:                 }
                    108:                 break;
                    109:             case 'p':
                    110:                 payload = optarg;
                    111:                 payload_s = strlen(payload);
                    112:                 break;
                    113:             default:
                    114:                 exit(EXIT_FAILURE);
                    115:         }
                    116:     }
                    117: 
                    118:     if (!src_ip || !src_prt || !dst_ip || !dst_prt)
                    119:     {
                    120:         usage(argv[0]);
                    121:         exit(EXIT_FAILURE);
                    122:     }
                    123: 
                    124:     for (build_ip = 0, i = 0; i < 10; i++)
                    125:     {
                    126:         udp = libnet_build_udp(
                    127:             src_prt,                                /* source port */
                    128:             dst_prt + i,                            /* destination port */
                    129:             LIBNET_UDP_H + payload_s,               /* packet length */
                    130:             0,                                      /* checksum */
1.1.1.2   misho     131:             (uint8_t*)payload,                     /* payload */
1.1       misho     132:             payload_s,                              /* payload size */
                    133:             l,                                      /* libnet handle */
                    134:             udp);                                   /* libnet id */
                    135:         if (udp == -1)
                    136:         {
                    137:             fprintf(stderr, "Can't build UDP header: %s\n", libnet_geterror(l));
                    138:             goto bad;
                    139:         }
                    140: 
                    141:         if (1)
                    142:         {
                    143:             build_ip = 0;
                    144:             /* this is not a legal options string */
                    145:             for (j = 0; j < 20; j++)
                    146:             {
                    147:                 opt[j] = libnet_get_prand(LIBNET_PR2);
                    148:             }
                    149:             ipo = libnet_build_ipv4_options(
                    150:                 opt,
                    151:                 20,
                    152:                 l,
                    153:                 ipo);
                    154:             if (ipo == -1)
                    155:             {
                    156:                 fprintf(stderr, "Can't build IP options: %s\n", libnet_geterror(l));
                    157:                 goto bad;
                    158:             }
                    159: 
                    160:             ip = libnet_build_ipv4(
                    161:                 LIBNET_IPV4_H + 20 + payload_s + LIBNET_UDP_H, /* length */
                    162:                 0,                                          /* TOS */
                    163:                 242,                                        /* IP ID */
                    164:                 0,                                          /* IP Frag */
                    165:                 64,                                         /* TTL */
                    166:                 IPPROTO_UDP,                                /* protocol */
                    167:                 0,                                          /* checksum */
                    168:                 src_ip,
                    169:                 dst_ip,
                    170:                 NULL,                                       /* payload */
                    171:                 0,                                          /* payload size */
                    172:                 l,                                          /* libnet handle */
                    173:                ip);                                         /* libnet id */
                    174:             if (ip == -1)
                    175:             {
                    176:                 fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
                    177:                 goto bad;
                    178:             }
                    179:         }
                    180: 
                    181:         /*
                    182:          *  Write it to the wire.
                    183:          */
                    184:         fprintf(stderr, "%d byte packet, ready to go\n",
                    185:             libnet_getpacket_size(l));
                    186:         c = libnet_write(l);
                    187:         if (c == -1)
                    188:         {
                    189:             fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
                    190:             goto bad;
                    191:         }
                    192:         else
                    193:         {
                    194:             fprintf(stderr, "Wrote %d byte UDP packet; check the wire.\n", c);
                    195:         }
                    196:     }
                    197:     libnet_stats(l, &ls);
                    198:     fprintf(stderr, "Packets sent:  %lld\n"
                    199:                     "Packet errors: %lld\n"
                    200:                     "Bytes written: %lld\n",
1.1.1.3 ! misho     201:                     (long long)ls.packets_sent, (long long)ls.packet_errors,
        !           202:                    (long long)ls.bytes_written);
1.1       misho     203:     libnet_destroy(l);
                    204:     return (EXIT_SUCCESS);
                    205: bad:
                    206:     libnet_destroy(l);
                    207:     return (EXIT_FAILURE);
                    208: }
                    209: 
                    210: void
                    211: usage(char *name)
                    212: {
                    213:     fprintf(stderr,
                    214:         "usage: %s -s source_ip.source_port -d destination_ip.destination_port"
                    215:         " [-p payload]\n",
                    216:         name);
                    217: }

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