"Fossies" - the Fresh Open Source Software Archive

Member "isic-0.07/udpsic.c" (16 Dec 2006, 10804 Bytes) of package /linux/privat/old/isic-0.07.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "udpsic.c" see the Fossies "Dox" file reference documentation.

    1 #include "isic.h"
    2 
    3 /* This is tuned for ethernet sized frames (1500 bytes)
    4  * For user over a modem or frame (or other) you will have to change the
    5  * 'rand() & 0x4ff' line below.  The 0x4ff needs to be less than the size of
    6  * the frame size minus the length of the ip header (20 bytes IIRC) minus the
    7  * length of the TCP header.
    8  */
    9 
   10 
   11 /* Variables shared between main and the signal handler so we can display
   12  * output if ctrl-c'd
   13  */
   14 u_int seed = 0;
   15 u_long acx = 0;
   16 struct timeval starttime;
   17 u_long datapushed = 0;          /* How many bytes we pushed */
   18 
   19 
   20 int
   21 main(int argc, char **argv)
   22 {
   23     int c;
   24     u_char *buf = NULL;
   25     u_short *payload = NULL;
   26     u_int payload_s = 0;
   27     int packet_len = 0;
   28 
   29     struct ip *ip_hdr = NULL;
   30     struct udphdr *udp = NULL;
   31     u_short *ip_opts = NULL;
   32 
   33     /* libnet variables */
   34     char errbuf[LIBNET_ERRBUF_SIZE];
   35     libnet_t *l;
   36     char *device = NULL;
   37 
   38     /* Packet Variables */
   39     u_int32_t src_ip = 0, dst_ip = 0;
   40     u_short src_prt = 0, dst_prt = 0;
   41     u_char tos, ttl, ver;
   42     u_int id, frag_off;
   43     u_int ipopt_len;
   44 
   45 
   46     /* Functionality Variables */
   47     int src_ip_rand = 0, dst_ip_rand = 0;
   48     struct timeval tv, tv2;
   49     float sec;
   50     unsigned int cx = 0;
   51     u_long max_pushed = 10240;      /* 10MB/sec */
   52     u_long num_to_send = 0xffffffff;    /* Send 4billion packets */
   53     u_long skip = 0;            /* Skip how many packets */
   54     int printout = 0;           /* Debugging */
   55     int dst_port_rand = 0;
   56     int src_port_rand = 0;
   57     char *tmp_port = NULL;
   58     u_int repeat = 1;
   59 
   60     /* Defaults */
   61     float FragPct   =   10;
   62     float BadIPVer  =   10;
   63     float IPOpts    =   10;
   64     float UDPCksm   =   10;
   65 
   66 
   67 
   68     /* Not crypto strong randomness but we don't really care.  And this  *
   69      * gives us a way to determine the seed while the program is running *
   70      * if we need to repeat the results                  */
   71     seed = getpid();
   72     
   73     /* Initialize libnet context, Root priviledges are required.*/ 
   74     l = libnet_init(
   75             LIBNET_RAW4_ADV,                        /* injection type */
   76             device,                                 /* network interface */
   77             errbuf);                                /* error buffer */
   78 
   79     if (l == NULL) {
   80       fprintf(stderr, "libnet_init() failed: %s", errbuf);
   81       exit( -1 );
   82     }
   83     
   84 
   85     while((c = getopt(argc, argv, "hd:s:r:m:k:Dp:V:F:I:U:vx:")) != EOF) {
   86       switch (c) {
   87        case 'h':
   88         usage(argv[0]);
   89         exit(0);
   90         break;
   91        case 'd':
   92         dst_port_rand = 1;
   93         if ( (tmp_port = index(optarg, ',')) != NULL ) {
   94             *tmp_port++ = '\0';
   95             dst_port_rand = 0;
   96             dst_prt = htons((u_int) atol(tmp_port));
   97         }
   98         if ( strcmp(optarg, "rand") == 0 ) {
   99             printf("Using random dest IP's\n");
  100             dst_ip = 1; /* Just to pass sanity checks */
  101             dst_ip_rand = 1;
  102             break;
  103         }
  104         if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == (u_int32_t)-1) {
  105             fprintf(stderr, "Bad dest IP\n");
  106             exit( -1 );
  107         }
  108         break;
  109        case 's':
  110         src_port_rand = 1;
  111         if ( (tmp_port = index(optarg, ',')) != NULL ) {
  112             *tmp_port++ = '\0';
  113             src_port_rand = 0;
  114             src_prt = htons((u_int) atol(tmp_port));
  115         }
  116         if ( strcmp(optarg, "rand") == 0 ) {
  117             printf("Using random source IP's\n");
  118             src_ip = 1; /* Just to pass sanity checks */
  119             src_ip_rand = 1;
  120             break;
  121         }
  122         if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == (u_int32_t)-1) {
  123             fprintf(stderr, "Bad source IP\n");
  124             exit( -1 );
  125         }
  126         break;
  127        case 'r':
  128         seed = atoi(optarg);
  129         break;
  130        case 'm':
  131         max_pushed = atol(optarg);
  132         break;
  133        case 'k':
  134         skip = atol(optarg);
  135         printf("Will not transmit first %li packets.\n", skip);
  136         break;
  137        case 'D':
  138         printout++;
  139         break;
  140        case 'p':
  141         num_to_send = atoi(optarg);
  142         break;
  143        case 'V':
  144         BadIPVer = atof(optarg);
  145         break;
  146        case 'F':
  147         FragPct = atof(optarg);
  148         break;
  149        case 'I':
  150         IPOpts = atof(optarg);
  151         break;
  152        case 'U':
  153         UDPCksm = atof(optarg);
  154         break;
  155        case 'x':
  156         repeat = atoi(optarg);
  157         break;
  158        case 'v':
  159         printf("Version %s\n", VERSION);
  160         exit(0);
  161        }
  162     }
  163 
  164     if ( !src_ip || !dst_ip ) {
  165         usage(argv[0]);
  166         exit(EXIT_FAILURE);
  167     }
  168 
  169     printf("Compiled against Libnet %s\n", LIBNET_VERSION);
  170     printf("Installing Signal Handlers.\n");
  171     if ( signal(SIGTERM, &sighandler) == SIG_ERR )
  172         printf("Failed to install signal handler for SIGTERM\n");
  173     if ( signal(SIGINT, &sighandler) == SIG_ERR )
  174         printf("Failed to install signal handler for SIGINT\n");
  175     if ( signal(SIGQUIT, &sighandler) == SIG_ERR )
  176         printf("Failed to install signal handler for SIGQUIT\n");
  177 
  178     printf("Seeding with %i\n", seed);
  179     srand(seed);
  180     max_pushed *= 1024;
  181 
  182     if ( src_port_rand )
  183         printf("Using random source ports.\n");
  184     if ( dst_port_rand )
  185         printf("Using random destination ports.\n");
  186 
  187     if ( (buf = malloc(IP_MAXPACKET)) == NULL ) {
  188         perror("malloc: ");
  189         exit( -1 );
  190     }
  191 
  192 
  193     if ( max_pushed >= 10000000 )
  194         printf("No Maximum traffic limiter\n");
  195     else printf("Maximum traffic rate = %.2f k/s\n", max_pushed/1024.0 );
  196 
  197     printf("Bad IP Version\t= %.0f%%\t\t", BadIPVer);
  198     printf("IP Opts Pcnt\t= %.0f%%\n", IPOpts);
  199 
  200     printf("Frag'd Pcnt\t= %.0f%%\t\t", FragPct);
  201     printf("Bad UDP Cksm\t= %.0f%%\n", UDPCksm);
  202     printf("\n");
  203 
  204 
  205     /* Drop them down to floats so we can multiply and not overflow */
  206     BadIPVer    /= 100;
  207     FragPct     /= 100;
  208     IPOpts      /= 100;
  209     UDPCksm     /= 100;
  210 
  211 
  212     /*************
  213      * Main Loop *
  214      *************/
  215     gettimeofday(&tv, NULL);
  216     gettimeofday(&starttime, NULL);
  217 
  218     for(acx = 0; acx < num_to_send; acx++) {
  219         packet_len = IP_H + UDP_H;
  220     
  221         tos = RAND8;
  222         id  = acx & 0xffff;
  223         ttl = RAND8;
  224 
  225         if ( rand() <= (RAND_MAX * FragPct) )
  226             frag_off = RAND16;
  227         else    frag_off = 0;
  228 
  229         /* We're not going to pad IP Options */
  230         if ( rand() <= (RAND_MAX * IPOpts) ) {
  231             ipopt_len = 10 * (rand() / (float) RAND_MAX);
  232             ipopt_len = ipopt_len << 1;
  233             ip_opts = (u_short *) buf;
  234             packet_len += ipopt_len << 1;
  235 
  236             for ( cx = 0; cx < ipopt_len; cx++ )
  237                 ip_opts[cx] = RAND16;
  238             udp = (struct udphdr *) (buf + IP_H + (ipopt_len << 1));
  239             ipopt_len = ipopt_len >> 1;
  240         } else {
  241             ipopt_len = 0;
  242             udp = (struct udphdr *) (buf + IP_H);
  243         }
  244 
  245 
  246         if ( src_ip_rand == 1 )
  247             src_ip = RAND32;
  248         if ( dst_ip_rand == 1 )
  249             dst_ip = RAND32;
  250 
  251         if ( rand() <= (RAND_MAX * BadIPVer ) )
  252             ver = rand() & 0xf;
  253         else    ver = 4;
  254 
  255         payload_s = rand() & 0x4ff;            /* length of 1279 */
  256         packet_len += payload_s;
  257 
  258 
  259         /*
  260         *  Build the IP header
  261         */
  262         ip_hdr = (struct ip *) buf;
  263         ip_hdr->ip_v    = ver;                 /* version 4 */
  264         ip_hdr->ip_hl   = 5 + ipopt_len;       /* 20 byte header */
  265         ip_hdr->ip_tos  = tos;                 /* IP tos */
  266         ip_hdr->ip_len  = htons(packet_len);   /* total length */
  267         ip_hdr->ip_id   = htons(id);           /* IP ID */
  268         ip_hdr->ip_off  = htons(frag_off);     /* fragmentation flags */
  269         ip_hdr->ip_ttl  = ttl;                 /* time to live */
  270         ip_hdr->ip_p    = IPPROTO_UDP;         /* transport protocol */
  271         ip_hdr->ip_sum  = 0;                   /* do this later */
  272         ip_hdr->ip_src.s_addr = src_ip;
  273         ip_hdr->ip_dst.s_addr = dst_ip;
  274         
  275         if ( src_port_rand == 1 )
  276             udp->uh_sport = RAND16;
  277         else    udp->uh_sport = src_prt;
  278         if ( dst_port_rand == 1 )
  279             udp->uh_dport = RAND16;
  280         else    udp->uh_dport = dst_prt;
  281 
  282 
  283         udp->uh_ulen    = htons(payload_s + UDP_H);
  284         udp->uh_sum = 0;
  285 
  286         payload = (u_short *)((u_char *) udp + UDP_H);
  287         for(cx = 0; cx <= (payload_s >> 1); cx+=1)
  288             payload[cx] = RAND16;
  289 
  290         if ( printout ) {
  291             printf("%s,%i ->",
  292                 inet_ntoa(*((struct in_addr*) &src_ip )),
  293                 htons(udp->uh_sport) );
  294             printf(" %s,%i tos[%i] id[%i] ver[%i] frag[%i]\n",
  295                 inet_ntoa(*((struct in_addr*) &dst_ip )), 
  296                 htons(udp->uh_dport), tos, id, ver, frag_off);
  297         }
  298                 
  299         
  300         if ( rand() <= (RAND_MAX * UDPCksm) )
  301             udp->uh_sum = RAND16;
  302         else    libnet_do_checksum(l, (u_int8_t *)buf, IPPROTO_UDP, UDP_H + payload_s);
  303 
  304          
  305         if ( skip <= acx ) {
  306             for ( cx = 0; cx < repeat; cx++ ) {
  307                 c = libnet_write_raw_ipv4(l, buf, packet_len);
  308                 if (c != -1)
  309                   datapushed+=c;
  310             }
  311             if (c < (packet_len) ) {
  312                 perror("Failed to send packet");
  313         /*      printf("%s ->", 
  314          *           inet_ntoa(*((struct in_addr*) &src_ip )));
  315          *
  316          *      printf(" %s tos[%i] id[%i] ver[%i] "
  317          *           "frag[%i]\n",
  318          *           inet_ntoa(*((struct in_addr*) &dst_ip )),
  319          *          tos, id, ver, frag_off);
  320          */
  321             }
  322         } 
  323 
  324         if ( !(acx % 1000) ) {
  325             if ( acx == 0 )
  326                 continue;
  327             gettimeofday(&tv2, NULL);
  328             sec = (tv2.tv_sec - tv.tv_sec)
  329                   - (tv.tv_usec - tv2.tv_usec) / 1000000.0;
  330             printf(" %li @ %.1f pkts/sec and %.1f k/s\n", acx,
  331                 1000/sec, (datapushed / 1024.0) / sec);
  332             datapushed=0;
  333             gettimeofday(&tv, NULL);
  334         }
  335 
  336 
  337         /* 
  338          * Flood protection is only needed for low traffic limit.
  339          * Since gettimeofday() is a costly function call, doing it
  340          * per packet send stops us from reaching maximum line speed
  341          * for outgoing traffic.
  342          */ 
  343         if ( max_pushed < 10000000 ) {
  344             gettimeofday(&tv2, NULL);
  345             sec = (tv2.tv_sec - tv.tv_sec)
  346                     - (tv.tv_usec - tv2.tv_usec) / 1000000.0;
  347             if ( (datapushed / sec) >= max_pushed )
  348                 usleep(10); /* 10 should give up our timeslice */
  349         }
  350     }
  351 
  352 
  353     gettimeofday(&tv, NULL);
  354     printf("\nWrote %li packets in %.2fs @ %.2f pkts/s\n", acx,
  355         (tv.tv_sec-starttime.tv_sec)
  356         + (tv.tv_usec-starttime.tv_usec) / 1000000.0,
  357         acx / ((tv.tv_sec-starttime.tv_sec)
  358                        + (tv.tv_usec-starttime.tv_usec)/1000000.0) );
  359 
  360     libnet_destroy(l);
  361     free(buf);
  362     return ( 0 );
  363 }
  364 
  365 
  366 void usage(char *name)
  367 {
  368    fprintf(stderr,
  369     "usage: %s [-v] [-D] -s <sourceip>[,port] -d <destination ip>[,port]\n"
  370     "          [-r seed] [-m <max kB/s to generate>]\n"
  371     "          [-p <pkts to generate>] [-k <skip packets>] [-x <repeat times>]\n\n"
  372     "       Percentage Opts: [-F frags] [-V <Bad IP Version>] [-I <IP Options>]\n"
  373     "                        [-U <Bad UDP Checksum>]\n\n"
  374     "       [-D] causes packet info to be printed out -- DEBUGGING\n\n"
  375     "       ex: -s 10.10.10.10,23   -d 10.10.10.100 -I 100\n"
  376     "           will give a 100%% chance of IP Options ^^^\n"
  377     "       ex: -s 10.10.10.10,23   -d 10.10.10.100 -p 100 -r 103334\n"
  378     "       ex: -s rand -d rand,1234 -r 23342\n"
  379     "              ^^^^ causes random source addr\n"
  380     "       ex: -s rand -d rand -k 10000 -p 10001 -r 666\n"
  381     "           Will only send the 10001 packet with random seed 666\n"
  382     "           this is especially useful if you suspect that packet is\n"
  383     "           causing a problem with the target stack.\n\n",
  384     ((char *) rindex(name, '/')) == ((char *) NULL)
  385         ? (char *) name
  386         : (char *) rindex(name, '/') + 1);
  387 }
  388 
  389 void sighandler(int sig)
  390 {
  391     struct timeval tv;
  392     gettimeofday(&tv, NULL);
  393 
  394     printf("\n");
  395     printf("Caught signal %i\n", sig);
  396 
  397     printf("Used random seed %i\n", seed);
  398     printf("Wrote %li packets in %.2fs @ %.2f pkts/s\n", acx,
  399         (tv.tv_sec - starttime.tv_sec)
  400           + (tv.tv_usec - starttime.tv_usec)/1000000.0,
  401         acx / (( tv.tv_sec - starttime.tv_sec)
  402           + (tv.tv_usec - starttime.tv_usec)/1000000.0)
  403         );
  404 
  405     fflush(stdout);
  406     exit(0);
  407 }