"Fossies" - the Fresh Open Source Software Archive

Member "isic-0.07/isic.c" (16 Dec 2006, 9168 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 "isic.c" see the Fossies "Dox" file reference documentation.

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