home *** CD-ROM | disk | FTP | other *** search
/ Chaos Computer Club 1997 February / cccd_beta_feb_97.iso / contrib / hacktool / sniffer / sniffod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-28  |  21.0 KB  |  1,287 lines

  1. #include <stdio.h>
  2. #include <dos.h>
  3.  
  4. #define MAXETHB 1522
  5. #define MINETHB 59
  6.  
  7. #define CONNTAB 20
  8.  
  9. typedef struct {
  10.  
  11.    unsigned long sip;
  12.    unsigned int sp;
  13.    unsigned long dip;
  14.    unsigned int dp;
  15.    unsigned long expack;
  16.    int state;
  17.    int lru;
  18.  
  19.    } tcp_connection;
  20.  
  21. typedef struct {
  22.  
  23.    unsigned char etha[6];
  24.    unsigned char enam[18];
  25.  
  26.    } ethtrans;
  27.  
  28. typedef struct {
  29.  
  30.    unsigned int port;
  31.    char *name;
  32.  
  33.    } portmap;
  34.  
  35. portmap tcpports[] = {
  36. #include "c:/c/tcpport.h"
  37.  
  38. portmap udpports[] = {
  39. #include "C:/c/udpport.h"
  40.  
  41. portmap protocols[] = {
  42. #include "C:/c/protocol.h"
  43.  
  44. portmap ipprotocols[] = {
  45. #include "c:/c/ipprot.h"
  46.  
  47. int tcpcount, udpcount, procount, ipprocount;
  48. int tcpstart, udpstart, prostart, ipprostart;
  49.  
  50. ethtrans far *ethtab;
  51.  
  52. int ethtabs, ethstart;
  53.  
  54. tcp_connection conn[CONNTAB];
  55.  
  56. void inittab(tab, length, count, start)
  57.  
  58. portmap tab[];
  59. int length;
  60. int *count;
  61. int *start;
  62.  
  63. {
  64.  
  65. *count = length/sizeof(portmap);
  66. *start = 1;
  67.  
  68. while ((*start << 1) <= (*count - 1)) *start = *start << 1;
  69.  
  70. }
  71.  
  72. char *findtab(val, tab, length, start)
  73.  
  74. unsigned int val;
  75. portmap tab[];
  76. int length;
  77. int start;
  78.  
  79. {
  80.  
  81. int i, up, down, l, v;
  82.  
  83. i = start;
  84. l = length;
  85. v = val;
  86.  
  87. up = i >> 1;
  88. down = i | up;
  89.  
  90. loop:
  91.  
  92. if(i >= l) goto godown;
  93.  
  94. if(tab[i].port == v) return tab[i].name;
  95.  
  96. if(tab[i].port < v) {
  97.  
  98.    if(up == 0) goto notfound;
  99.  
  100.    i ^= up;
  101.  
  102.    down >>= 1;
  103.    up >>= 1;
  104.  
  105.    goto loop;
  106.  
  107.    }
  108.  
  109. godown:
  110.  
  111. if(down == 0) goto notfound;
  112.  
  113. i ^= down;
  114.  
  115. down >>= 1;
  116. up >>= 1;
  117.  
  118. goto loop;
  119.  
  120. notfound:
  121.  
  122. return NULL;
  123.  
  124. }
  125.  
  126.  
  127. void initethtab(file)
  128.  
  129. FILE *file;
  130.  
  131. {
  132.  
  133. int ic, iv, linecnt, charcnt;
  134. unsigned int segp;
  135. ethtrans far *nexteth, far *competh;
  136.  
  137. linecnt = 0;
  138. charcnt = 0;
  139.  
  140. loop1:
  141.  
  142. ic = fgetc(file);
  143.  
  144. switch (ic) {
  145.  
  146.    case '\n': linecnt++;
  147.               charcnt = 0;
  148.               goto loop1;
  149.  
  150.    case EOF : if(charcnt > 0) linecnt++;
  151.               break;
  152.  
  153.    default:   charcnt++;
  154.               goto loop1;
  155.  
  156.    }
  157.  
  158. fseek(file, (long) 0, SEEK_SET);
  159.  
  160. if((long) linecnt* (long)24 > (long) 65536) {
  161.    puts("Cannot accomodate that many address translations");
  162.    linecnt = (long) 65536/(long) 24;
  163.    }
  164.  
  165. if(allocmem((linecnt * 24 + 15)/16, &segp) != -1) {
  166.    puts("Error allocating Address Translation Table");
  167.    ethtabs=0;
  168.    return;
  169.    }
  170.  
  171. ethtab = (ethtrans far *) MK_FP(segp, 0);
  172. nexteth = ethtab;
  173. ethtabs = 0;
  174.  
  175. loop2:
  176.  
  177. charcnt = 0;
  178. iv = -1;
  179.  
  180. loop3:
  181.  
  182. ic = fgetc(file);
  183.  
  184. if (ic == EOF) goto sort1;
  185.  
  186. if ((ic >= '0') && (ic <= '9')) {
  187.  
  188.    if(iv < 0) {
  189.  
  190.       iv = ic - '0';
  191.  
  192.       }
  193.  
  194.    else {
  195.  
  196.       iv = (iv << 4) + ic - '0';
  197.  
  198.       goto putadd;
  199.  
  200.       }
  201.  
  202.    }
  203.  
  204. if ((ic >= 'A') && (ic <= 'F')) {
  205.  
  206.    if(iv < 0) {
  207.  
  208.       iv = ic - 'A' + 10;
  209.  
  210.       }
  211.  
  212.    else {
  213.  
  214.       iv = (iv << 4) + ic - 'A' + 10;
  215.  
  216.       goto putadd;
  217.  
  218.       }
  219.  
  220.    }
  221.  
  222. if ((ic >= 'a') && (ic <= 'f')) {
  223.  
  224.    if(iv < 0) {
  225.  
  226.       iv = ic - 'a' + 10;
  227.  
  228.       }
  229.  
  230.    else {
  231.  
  232.       iv = (iv << 4) + ic - 'a' + 10;
  233.  
  234.       goto putadd;
  235.  
  236.       }
  237.  
  238.    }
  239.  
  240. goto loop3;
  241.  
  242. skip2:
  243.  
  244. if( ic == '\n') goto loop2;
  245.  
  246. ic = fgetc(file);
  247.  
  248. if(ic == EOF) goto sort1;
  249.  
  250. goto skip2;
  251.  
  252. putadd:
  253.  
  254. nexteth->etha[charcnt] = iv;
  255. charcnt++;
  256. iv = -1;
  257. if (charcnt < 6) goto loop3;
  258.  
  259. charcnt = 0;
  260.  
  261. ic = fgetc(file);
  262.  
  263. if(ic != ' ') goto skip2;
  264.  
  265. loop4:
  266.  
  267. ic = fgetc(file);
  268.  
  269. if (ic == EOF) goto done;
  270.  
  271. if (ic == '\n') goto fill;
  272.  
  273. if (charcnt < 17) {
  274.  
  275.    nexteth->enam[charcnt] = ic;
  276.    charcnt++;
  277.    goto loop4;
  278.  
  279.    }
  280.  
  281. nexteth->enam[17] = 0;
  282. nexteth++;
  283. ethtabs++;
  284.  
  285. if(ethtabs >= linecnt) goto sort1;
  286.  
  287. skip3:
  288.  
  289. ic = fgetc(file);
  290.  
  291. if(ic == EOF) goto sort1;
  292.  
  293. if(ic == '\n') goto loop2;
  294.  
  295. goto skip3;
  296.  
  297. fill:
  298.  
  299. for(iv = charcnt; iv < 17; iv++) nexteth->enam[iv] = ' ';
  300.  
  301. nexteth->enam[17] = 0;
  302. nexteth++;
  303. ethtabs++;
  304.  
  305. if(ethtabs >= linecnt) goto sort1;
  306.  
  307. goto loop2;
  308.  
  309. done:
  310.  
  311. if(charcnt == 0) goto sort1;
  312.  
  313. for(iv = charcnt; iv < 17; iv++) nexteth->enam[iv] = ' ';
  314.  
  315. nexteth->enam[17] = 0;
  316. nexteth++;
  317. ethtabs++;
  318.  
  319. sort1:
  320.  
  321. if(ethtabs < 2) {
  322.    ethstart = 0;
  323.    return;
  324.    }
  325.  
  326. linecnt = ethtabs - 1;
  327.  
  328. sort2:
  329.  
  330. linecnt--;
  331.  
  332. if(linecnt < 0) goto sortdone;
  333.  
  334. charcnt = 0;
  335. nexteth = ethtab;
  336. competh = ethtab;
  337. competh++;
  338.  
  339. sort3:
  340.  
  341. for(iv = 0; iv < 6; iv++) {
  342.  
  343.    if(nexteth->etha[iv] < competh->etha[iv]) goto checknext;
  344.    if(nexteth->etha[iv] > competh->etha[iv]) goto swap;
  345.  
  346.    }
  347.  
  348. puts("Duplicate Ethernet Address removed");
  349.  
  350. nexteth = ethtab + (ethtabs - 1);
  351.  
  352. for(iv = 0; iv < 6; iv++) competh->etha[iv] = nexteth->etha[iv];
  353.  
  354. for(iv = 0; iv < 17; iv++) competh->enam[iv] = nexteth->enam[iv];
  355.  
  356. ethtabs--;
  357.  
  358. goto sort1;
  359.  
  360. swap:
  361.  
  362. for(iv = 0; iv < 6; iv++) {
  363.  
  364.    ic = competh->etha[iv];
  365.    competh->etha[iv] = nexteth->etha[iv];
  366.    nexteth->etha[iv] = ic;
  367.  
  368.    }
  369.  
  370. for(iv = 0; iv < 17; iv++) {
  371.  
  372.    ic = competh->enam[iv];
  373.    competh->enam[iv] = nexteth->enam[iv];
  374.    nexteth->enam[iv] = ic;
  375.  
  376.    }
  377.  
  378. checknext:
  379.  
  380. if(charcnt >= linecnt) goto sort2;
  381.  
  382. nexteth++;
  383. competh++;
  384. charcnt++;
  385.  
  386. goto sort3;
  387.  
  388. sortdone:
  389.  
  390. ethstart = 1;
  391.  
  392. while(ethtabs > (ethstart << 1)) ethstart <<= 1;
  393.  
  394. }
  395.  
  396. void printethaddr(addr)
  397.  
  398. unsigned char *addr;
  399.  
  400. {
  401.  
  402. unsigned char *work;
  403.  
  404. ethtrans far *competh;
  405.  
  406. int start, comphigh, complow, i;
  407.  
  408. if(ethtabs < 1) goto printhex;
  409.  
  410. start = ethstart;
  411. comphigh = (start >> 1) & 0x7fff;
  412. complow = start | comphigh;
  413.  
  414. testloop:
  415.  
  416. if(start >= ethtabs) goto scanlower;
  417.  
  418. competh = ethtab + start;
  419.  
  420. work = addr;
  421.  
  422. for(i = 0; i < 6; i++) {
  423.  
  424.    if(*work < competh->etha[i]) goto scanlower;
  425.    if(*work > competh->etha[i]) goto scanhigher;
  426.  
  427.    work++;
  428.  
  429.    }
  430.  
  431. printf("%17Fs",&competh->enam[0]);
  432.  
  433. return;
  434.  
  435. scanlower:
  436.  
  437. if(complow == 0) goto printhex;
  438.  
  439. start ^= complow;
  440.  
  441. scannext:
  442.  
  443. complow = (complow >> 1) & 0x7fff;
  444. comphigh = (comphigh >> 1) & 0x7fff;
  445.  
  446. goto testloop;
  447.  
  448. scanhigher:
  449.  
  450. if(comphigh == 0) goto printhex;
  451.  
  452. start ^= comphigh;
  453.  
  454. goto scannext;
  455.  
  456. printhex:
  457.  
  458. work = addr;
  459.  
  460. printf("%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x", *work, *(work+1),
  461.         *(work+2), *(work+3), *(work+4), *(work+5));
  462.  
  463. }
  464.  
  465.  
  466. void printtcpport(port)
  467.  
  468. unsigned int port;
  469.  
  470. {
  471.  
  472. char *res;
  473.  
  474. if((res = findtab(port, tcpports, tcpcount, tcpstart)) == NULL) return;
  475.  
  476. printf(" (%s) ", res);
  477.  
  478. }
  479.  
  480. int findtcpconn(source, sourceport, dest, destport)
  481.  
  482. unsigned char source[4];
  483. unsigned int sourceport;
  484. unsigned char dest[4];
  485. unsigned int destport;
  486.  
  487. {
  488.  
  489. int i, j;
  490.  
  491. unsigned long ips, ipd;
  492.  
  493. ips = (((unsigned long) source[0]) << 24) |
  494.       (((unsigned long) source[1]) << 16) |
  495.       (((unsigned long) source[2]) << 8) |
  496.         (unsigned long) source[3];
  497.  
  498. ipd = (((unsigned long) dest[0]) << 24) |
  499.       (((unsigned long) dest[1]) << 16) |
  500.       (((unsigned long) dest[2]) << 8) |
  501.         (unsigned long) dest[3];
  502.  
  503. for(i = 0; i < CONNTAB; i++) {
  504.  
  505.    if(ips == conn[i].sip && sourceport == conn[i].sp &&
  506.       ipd == conn[i].dip && destport == conn[i].dp) {
  507.  
  508.       for(j = 0; j < CONNTAB; j++) {
  509.  
  510.          if(conn[j].lru < conn[i].lru) conn[j].lru++;
  511.  
  512.          }
  513.  
  514.       conn[i].lru = 0;
  515.  
  516.       return i;
  517.  
  518.       }
  519.  
  520.    }
  521.  
  522. return (-1);
  523.  
  524. }
  525.  
  526.  
  527.  
  528. void checktcpack(source, sourceport, dest, destport, ack)
  529.  
  530. unsigned char source[4];
  531. unsigned int sourceport;
  532. unsigned char dest[4];
  533. unsigned int destport;
  534. unsigned long ack;
  535.  
  536. {
  537.  
  538. int tcpconn;
  539.  
  540. if((tcpconn = findtcpconn(source, sourceport, dest, destport)) >= 0) {
  541.  
  542.    if(conn[tcpconn].expack != ack) {
  543.  
  544.       printf("TCP: **** Acknowledge Mismatch, Expected = %lx ****\n",
  545.              conn[tcpconn].expack);
  546.  
  547.       }
  548.  
  549.    }
  550.  
  551. }
  552.  
  553.  
  554. int maketcpconn(source, sourceport, dest, destport)
  555.  
  556. unsigned char source[4];
  557. unsigned int sourceport;
  558. unsigned char dest[4];
  559. unsigned int destport;
  560.  
  561. {
  562.  
  563. int i, j, maxlru;
  564.  
  565. unsigned long ips, ipd;
  566.  
  567. ips = (((unsigned long) source[0]) << 24) |
  568.       (((unsigned long) source[1]) << 16) |
  569.       (((unsigned long) source[2]) << 8) |
  570.         (unsigned long) source[3];
  571.  
  572. ipd = (((unsigned long) dest[0]) << 24) |
  573.       (((unsigned long) dest[1]) << 16) |
  574.       (((unsigned long) dest[2]) << 8) |
  575.         (unsigned long) dest[3];
  576.  
  577. j = 0;
  578. maxlru = 0;
  579.  
  580. for(i  = 0; i < CONNTAB; i++) {
  581.  
  582.    if(conn[i].lru > maxlru) {
  583.  
  584.       maxlru = conn[i].lru;
  585.       j = i;
  586.  
  587.       }
  588.  
  589.    conn[i].lru++;
  590.  
  591.    }
  592.  
  593. conn[j].sip = ips;
  594. conn[j].sp = sourceport;
  595. conn[j].dip = ipd;
  596. conn[j].dp = destport;
  597. conn[j].expack = 0;
  598. conn[j].lru = 0;
  599.  
  600. return j;
  601.  
  602. }
  603.  
  604.  
  605.  
  606.  
  607. void analyzetcp(data, source, dest, len)
  608.  
  609. unsigned char *data;
  610. unsigned char source[4];
  611. unsigned char dest[4];
  612. unsigned int len;
  613.  
  614. {
  615.  
  616. union {
  617.  
  618. unsigned char form4[4];
  619. unsigned long form1;
  620.  
  621.    } ipform;
  622.  
  623. unsigned int sourceport, destport;
  624. unsigned long sequence, ack;
  625. int tcphl, flagurg, flagack, flagpsh, flagrst, flagsyn, flagfin;
  626. unsigned int window;
  627. int chksum;
  628. unsigned int urgpnt;
  629.  
  630. unsigned char *work;
  631.  
  632. int i, optl;
  633.  
  634. work = data;
  635.  
  636. sourceport = (((unsigned int) *work) << 8) | *(work + 1);
  637. destport = (((unsigned int) *(work + 2)) << 8) | *(work + 3);
  638. sequence = (((unsigned long) *(work + 4)) << 24) |
  639.            (((unsigned long) *(work + 5)) << 16) |
  640.            (((unsigned long) *(work + 6)) << 8) | *(work + 7);
  641. ack = (((unsigned long) *(work + 8)) << 24) |
  642.       (((unsigned long) *(work + 9)) << 16) |
  643.       (((unsigned long) *(work + 10)) << 8) | *(work + 11);
  644. tcphl = *(work + 12) >> 4;
  645. flagurg = (*(work + 13) >> 5) & 1;
  646. flagack = (*(work + 13) >> 4) & 1;
  647. flagpsh = (*(work + 13) >> 3) & 1;
  648. flagrst = (*(work + 13) >> 2) & 1;
  649. flagsyn = (*(work + 13) >> 1) & 1;
  650. flagfin = *(work + 13) & 1;
  651. window = (((unsigned int) *(work + 14)) << 8) | *(work + 15);
  652. chksum = ((((int) *(work + 16)) << 8) & 0xff00) | *(work + 17);
  653. urgpnt = (((unsigned int) *(work + 18)) << 8) | *(work + 19);
  654.  
  655. printf("TCP:   Source Port: %d ", sourceport);
  656.  
  657. printtcpport(sourceport);
  658.  
  659. printf("  Destination Port: %d ", destport);
  660.  
  661. printtcpport(destport);
  662.  
  663. printf("\n");
  664.  
  665. printf("TCP:   Sequence : %lx  Acknowledge : %lx\n", sequence, ack);
  666.  
  667. if(flagack) checktcpack(dest, destport, source, sourceport, ack);
  668.  
  669. if((i = findtcpconn(source, sourceport, dest, destport)) >= 0) {
  670.  
  671.    if(conn[i].expack != sequence) {
  672.  
  673.       printf("TCP: **** Unexpected Sequence, Expected: %lx ****\n",
  674.              conn[i].expack);
  675.  
  676.       }
  677.  
  678.    }
  679.  
  680. else {
  681.  
  682.    i = maketcpconn(source, sourceport, dest, destport);
  683.  
  684.    }
  685.  
  686. conn[i].expack = sequence + (flagsyn | flagfin) + (len - 4*tcphl);
  687.  
  688. printf("TCP:   Header Length: %i  Window: %u ", tcphl, window);
  689.  
  690. if(flagurg || flagack || flagpsh || flagrst || flagsyn || flagfin) {
  691.  
  692.    printf(" Flags: ");
  693.  
  694.    if(flagurg) printf(" Urgent Valid ");
  695.    if(flagack) printf(" ACK ");
  696.    if(flagpsh) printf(" Push ");
  697.    if(flagrst) printf(" Reset ");
  698.    if(flagsyn) printf(" Synch ");
  699.    if(flagfin) printf(" Finish ");
  700.  
  701.    }
  702.  
  703. printf("\n");
  704.  
  705. printf("TCP:   Checksum: %x ", chksum);
  706.  
  707. if(flagurg) printf(" Urgent Data up to Byte %u ", urgpnt);
  708.  
  709. printf("\n");
  710.  
  711. if(tcphl > 5) {
  712.  
  713.    work = data + 5*4;
  714.    optl = (tcphl - 5)*4;
  715.    i = 0;
  716.  
  717.    while (i < optl) {
  718.  
  719.       switch (*work) {
  720.  
  721.          case 0: return;
  722.  
  723.          case 1: work++;
  724.                  i++;
  725.                  break;
  726.  
  727.          case 2: printf("TCP:   Maximum Segment Size Option: ");
  728.                  if(*(work + 1) == 4) {
  729.  
  730.                     printf("%u\n", (((unsigned int) *(work + 2)) << 8) |
  731.                                      (unsigned int) *(work + 3));
  732.  
  733.                     }
  734.  
  735.                  else {
  736.  
  737.                     printf("Illegal Option Length\n");
  738.  
  739.                     }
  740.  
  741.                  i += *(work + 1);
  742.                  work += *(work + 1);
  743.  
  744.                  break;
  745.  
  746.          default: printf("TCP:   Unknown Option: %d\n", *work);
  747.  
  748.                   return;
  749.  
  750.          }
  751.  
  752.       }
  753.  
  754.    }
  755.  
  756. }
  757.  
  758.  
  759. void printipprot(port)
  760.  
  761. unsigned int port;
  762.  
  763. {
  764.  
  765. char *res;
  766.  
  767. if((res = findtab(port, ipprotocols, ipprocount, ipprostart)) == NULL) return;
  768.  
  769. printf(" (%s) ", res);
  770.  
  771. }
  772.  
  773. void printudpport(port)
  774.  
  775. unsigned int port;
  776.  
  777. {
  778.  
  779. char *res;
  780.  
  781. if((res = findtab(port, udpports, udpcount, udpstart)) == NULL) return;
  782.  
  783. printf(" (%s) ", res);
  784.  
  785. }
  786.  
  787.  
  788. void analyzeudp(data, source, dest, len)
  789.  
  790. unsigned char *data;
  791. unsigned char source[4];
  792. unsigned char dest[4];
  793. unsigned int len;
  794.  
  795. {
  796.  
  797. union {
  798.  
  799. unsigned char form4[4];
  800. unsigned long form1;
  801.  
  802.    } ipform;
  803.  
  804. unsigned int sourceport, destport;
  805. int chksum;
  806. unsigned int udplen;
  807.  
  808. unsigned char *work;
  809.  
  810. work = data;
  811.  
  812. sourceport = (((unsigned int) *work) << 8) | *(work + 1);
  813. destport = (((unsigned int) *(work + 2)) << 8) | *(work + 3);
  814. udplen = (((unsigned int) *(work + 4)) << 8) | *(work + 5);
  815. chksum = (((int) *(work + 6)) << 8) | *(work + 7);
  816.  
  817. printf("UDP:   Source Port: %d ", sourceport);
  818.  
  819. printudpport(sourceport);
  820.  
  821. printf("  Destination Port: %d ", destport);
  822.  
  823. printudpport(destport);
  824.  
  825. printf("\n");
  826.  
  827. printf("UDP:   Length: %u    Checksum: %4.4x\n", udplen, chksum);
  828.  
  829. }
  830.  
  831.  
  832.  
  833. void analyzeip(data, etype, len)
  834.  
  835. unsigned char *data;
  836. unsigned int etype;
  837. int len;
  838.  
  839. {
  840.  
  841. unsigned char *work;
  842.  
  843. int vers, ihl, ttl, protocol, checks, offset;
  844. int precedence, delay, throughput, reliability;
  845. int mayfragment, lastfragment;
  846. int optl;
  847.  
  848. int i, k;
  849.  
  850. int source[4], dest[4];
  851.  
  852. unsigned int totlen, ident;
  853.  
  854. work = data;
  855.  
  856. vers = (*work >> 4) & 0xff;
  857. ihl = *work & 0xf;
  858. precedence = (*(work + 1) >> 5) & 7;
  859. delay = (*(work + 1) >> 4) & 1;
  860. throughput = (*(work + 1) >> 3) & 1;
  861. reliability = (*(work + 1) >> 2) & 1;
  862. totlen = (*(work + 2) << 8) | *(work + 3);
  863. ident = (*(work + 4) << 8) | *(work + 5);
  864. mayfragment = (*(work + 6) >> 6) & 1;
  865. lastfragment = (*(work + 6) >> 5) & 1;
  866. offset = ((*(work + 6) & 0x1f) << 8) | *(work + 7);
  867. ttl = *(work + 8);
  868. protocol = *(work + 9);
  869. checks = (*(work + 10) << 8) | *(work + 11);
  870.  
  871. for(i = 0; i < 4; i++) {
  872.  
  873.    source[i] = *(work + 12 + i);
  874.    dest[i] = *(work + 16 + i);
  875.  
  876.    }
  877.  
  878. printf("IP:   From : %d.%d.%d.%d  To : %d.%d.%d.%d  Length : %u\n",
  879.         source[0], source[1], source[2], source[3],
  880.         dest[0], dest[1], dest[2], dest[3], totlen);
  881.  
  882. printf("IP:   Ident: %4.4x  TTL: %d  Protocol: %d ", ident, ttl, protocol);
  883.  
  884. printipprot(protocol);
  885.  
  886. printf("\n");
  887.  
  888. printf("IP:   Precedence: ");
  889.  
  890. switch (precedence) {
  891.  
  892.    case 0: printf("Routine ");
  893.            break;
  894.  
  895.    case 1: printf("Priority ");
  896.            break;
  897.  
  898.    case 2: printf("Immediate ");
  899.            break;
  900.  
  901.    case 3: printf("Flash ");
  902.            break;
  903.  
  904.    case 4: printf("Flash Override ");
  905.            break;
  906.  
  907.    case 5: printf("CRITIC/ECP ");
  908.            break;
  909.  
  910.    case 6: printf("Internetwork Control ");
  911.            break;
  912.  
  913.    case 7: printf("Network Control ");
  914.            break;
  915.  
  916.    }
  917.  
  918. if(delay) printf(" Low Delay ");
  919.  
  920. if(throughput) printf(" High Throughput ");
  921.  
  922. if(reliability) printf(" High Reliability ");
  923.  
  924. printf("\n");
  925.  
  926. printf("IP:   Fragment Offset: %d ", offset);
  927.  
  928. if(mayfragment) printf(" May Fragment ");
  929.  
  930. if(lastfragment) printf(" Last Fragment ");
  931.  
  932. printf("\n");
  933.  
  934. if(ihl > 5) {
  935.  
  936.    work = data + 5 * 4;
  937.    optl = (ihl - 5) * 4;
  938.  
  939.    i = 0;
  940.  
  941.    while (i < optl) {
  942.  
  943.  
  944.      switch ((*work) & 0x7f) {
  945.  
  946.         case 0 : goto optdone;
  947.  
  948.         case 1 : i++;
  949.                  work++;
  950.                  break;
  951.  
  952.         case 2 : printf("IP:   Security Option: ");
  953.                  if (*(work + 1) != 11) {
  954.  
  955.                     printf(" Wrong Length ");
  956.  
  957.                     i += *(work + 1);
  958.                     work += *(work + 1);
  959.  
  960.                     break;
  961.  
  962.                     }
  963.  
  964.                  printf(" %2.2x%2.2x %2.2x%2.2x %c%c %c%c%c\n",
  965.                         *(work+2), *(work+3), *(work+4), *(work+5),
  966.                         *(work+6), *(work+7),
  967.                         *(work+8), *(work+9), *(work+10));
  968.  
  969.                  i +=11;
  970.                  work +=11;
  971.  
  972.                  break;
  973.  
  974.         case 3 : printf("IP:   Loose Source and Record Route Option ");
  975.                  goto routeopt;
  976.  
  977.         case 9 : printf("IP:   Strict Source and Record Route Option ");
  978.                  goto routeopt;
  979.  
  980.         case 7 : printf("IP:   Record Route Option ");
  981.  
  982. routeopt:
  983.                  printf(" Length: %d  Pointer: %d\n",
  984.                         (*(work + 1) - 3)/4, *(work + 2)/4);
  985.  
  986.                  for (k = 0; k < ((*(work + 1) - 3)/4); k++) {
  987.  
  988.                     printf("IP:       Route: %u.%u.%u.%u\n",
  989.                            *(work + 3 + k*4), *(work + 4 + k*4),
  990.                            *(work + 5 + k*4), *(work + 6 + k*4));
  991.  
  992.                     }
  993.  
  994.                  i += *(work + 1);
  995.                  work += *(work + 1);
  996.  
  997.                  break;
  998.  
  999.         case 8 : printf("IP:   Stream ID Option: ");
  1000.  
  1001.                  if(*(work + 1) != 4) {
  1002.  
  1003.                     printf(" Wrong Length\n");
  1004.  
  1005.                     i += *(work + 1);
  1006.                     work += *(work + 1);
  1007.  
  1008.                     break;
  1009.  
  1010.                     }
  1011.  
  1012.                  printf(" %2.2x%2.2x\n",
  1013.                         *(work + 2), *(work + 3));
  1014.  
  1015.                  i += 4;
  1016.                  work += 4;
  1017.  
  1018.                  break;
  1019.  
  1020.         case 68: printf("IP:   Timestamp Option  Length: %d  Pointer %d\n",
  1021.                         (*(work + 1) - 4)/4, (*(work + 2) - 5)/4);
  1022.  
  1023.                  printf("IP:       Overflow: %d ",
  1024.                         (*(work + 3) >> 4) & 0xf);
  1025.  
  1026.                  if(*(work + 3) & 1) printf(" IP Addresses");
  1027.  
  1028.                  if(*(work + 3) & 2) printf(" prespecified");
  1029.  
  1030.                  printf("\n");
  1031.  
  1032.                  if(*(work + 3) & 1) {
  1033.  
  1034.                     for (k = 0; k < (*(work + 1) - 4)/4; k += 2) {
  1035.  
  1036.                        printf("IP:      IP-Address: %u.%u.%u.%u  Time: %l\n",
  1037.                                *(work + 4 + 4*k), *(work + 5 + 4*k),
  1038.                                *(work + 6 + 4*k), *(work + 7 + 4*k),
  1039.                                ((long) *(work + 8 + 4*k) << 24) |
  1040.                                ((long) *(work + 9 + 4*k) << 16) |
  1041.                                ((long) *(work + 10 + 4*k) << 8) |
  1042.                                (long) *(work + 11 + 4*k));
  1043.  
  1044.                        }
  1045.  
  1046.                     }
  1047.  
  1048.                  else {
  1049.  
  1050.                     for (k = 0; k < (*(work + 1) - 4)/4; k++) {
  1051.  
  1052.                        printf("IP:      Time: %l\n",
  1053.                                ((long) *(work + 4 + 4*k) << 24) |
  1054.                                ((long) *(work + 5 + 4*k) << 16) |
  1055.                                ((long) *(work + 6 + 4*k) << 8) |
  1056.                                (long) *(work + 7 + 4*k));
  1057.  
  1058.                        }
  1059.  
  1060.                     }
  1061.  
  1062.                  i += *(work + 1);
  1063.                  work += *(work + 1);
  1064.  
  1065.                  break;
  1066.  
  1067.         default: printf("IP     Unknown Option\n");
  1068.  
  1069.                  goto optdone;
  1070.  
  1071.  
  1072.         }
  1073.  
  1074.       }
  1075.  
  1076.    }
  1077.  
  1078. optdone:
  1079.  
  1080.  
  1081. if(offset == 0) {
  1082.  
  1083.    switch (protocol) {
  1084.  
  1085.       case 6 : analyzetcp(data + ihl*4, &source[0], &dest[0], totlen - 4*ihl);
  1086.                break;
  1087.  
  1088.       case 17: analyzeudp(data + ihl*4, &source[0], &dest[0], totlen - 4*ihl);
  1089.                break;
  1090.  
  1091.       }
  1092.  
  1093.    }
  1094.  
  1095. }
  1096.  
  1097.  
  1098. void analyze(data, etype, len)
  1099.  
  1100. unsigned char *data;
  1101. unsigned int etype;
  1102. int len;
  1103.  
  1104. {
  1105.  
  1106. char *res;
  1107.  
  1108. switch (etype) {
  1109.  
  1110.    case 0x0800 : analyzeip(data, etype, len);
  1111.                  break;
  1112.  
  1113.    default : if((res = findtab(etype, protocols, procount, prostart)) == NULL) {
  1114.  
  1115.                 printf("*** Protocol Type Unknown ***\n");
  1116.  
  1117.                 }
  1118.  
  1119.              else {
  1120.  
  1121.                 printf("** Protocol %s not further analyzed **\n", res);
  1122.  
  1123.                 }
  1124.  
  1125.    }
  1126.  
  1127. }
  1128.  
  1129.  
  1130.  
  1131.  
  1132. FILE *capture;
  1133. FILE *ethname;
  1134.  
  1135. main (argc, argv)
  1136.  
  1137. int argc;
  1138. char *argv[];
  1139.  
  1140. {
  1141.  
  1142. int lenr, hours, testlen, lostflag, minutes, secs;
  1143. unsigned int ticks;
  1144. float seconds;
  1145. int i, j, k, m;
  1146. unsigned char skipch;
  1147. unsigned char ethblock[MAXETHB];
  1148. unsigned int ethtype;
  1149.  
  1150. for (i = 0; i < CONNTAB; i++) conn[i].lru = i + CONNTAB + 1;
  1151.  
  1152. lostflag = 0;
  1153.  
  1154. if (argc > 1) {
  1155.  
  1156.    if((capture = fopen(argv[1], "rb")) == NULL) {
  1157.  
  1158.       puts("Cannot open capture file");
  1159.       exit(1);
  1160.  
  1161.       }
  1162.  
  1163.    }
  1164.  
  1165. if (argc > 2) {
  1166.  
  1167.    if((ethname = fopen(argv[2], "r")) == NULL) {
  1168.  
  1169.       puts("Cannot open Ethernet Address file");
  1170.       exit(1);
  1171.  
  1172.       }
  1173.  
  1174. initethtab(ethname);
  1175.  
  1176.    }
  1177.  
  1178. inittab(protocols, sizeof(protocols), &procount, &prostart);
  1179. inittab(tcpports, sizeof(tcpports), &tcpcount, &tcpstart);
  1180. inittab(udpports, sizeof(udpports), &udpcount, &udpstart);
  1181. inittab(ipprotocols, sizeof(ipprotocols), &ipprocount, &ipprostart);
  1182.  
  1183. readnext:
  1184.  
  1185. if(fread(&lenr, 2, 1, capture) < 1) goto done;
  1186.  
  1187. if(fread(&ticks, 2, 1, capture) < 1) goto done;
  1188.  
  1189. if(fread(&hours, 2, 1, capture) < 1) goto done;
  1190.  
  1191. if(lenr > MAXETHB) {
  1192.  
  1193.    puts("*** Long block ***");
  1194.  
  1195.    if(fread(ðblock[0], 1, MAXETHB, capture) < MAXETHB) goto done;
  1196.  
  1197.    for (i = MAXETHB; i < lenr; i++) {
  1198.  
  1199.       if(fread(&skipch, 1, 1, capture) < 1) goto done;
  1200.  
  1201.       }
  1202.  
  1203.    }
  1204.  
  1205. if(lenr < MINETHB) {
  1206.  
  1207.    puts("*** Short block ***");
  1208.  
  1209.    }
  1210.  
  1211. if(lenr <= MAXETHB) {
  1212.  
  1213.    if(fread(ðblock[0], 1, lenr, capture) < lenr) goto done;
  1214.  
  1215.    }
  1216.  
  1217. if(fread(&testlen, 2, 1, capture) < 1) goto done;
  1218.  
  1219. if(testlen != lenr) {
  1220.  
  1221.    puts("*** Lost position ***");
  1222.  
  1223.    lostflag = 1;
  1224.  
  1225.    }
  1226.  
  1227. seconds = (float) ticks / 65536.0 * 3600.0;
  1228. minutes = seconds / 60.0;
  1229. seconds = seconds - (float) minutes * 60.0;
  1230. secs = seconds / 10.0;
  1231. seconds = seconds - (float) secs * 10.0;
  1232.  
  1233. printf("\n Time %2d:%2.2d:%1.1d%4.2f  Length %4d\n",
  1234.        hours, minutes, secs, seconds, lenr);
  1235.  
  1236. printf(" From ");
  1237.  
  1238. printethaddr(ðblock[6]);
  1239.  
  1240. printf("  To ");
  1241.  
  1242. printethaddr(ðblock[0]);
  1243.  
  1244. printf("  Type/Length 0x%2.2x%2.2x\n", ethblock[12], ethblock[13]);
  1245.  
  1246. ethtype = (ethblock[12] << 8) | ethblock[13];
  1247.  
  1248. if (ethtype > MAXETHB) {
  1249.  
  1250.    analyze(ðblock[14], ethtype, lenr - 14);
  1251.  
  1252.    }
  1253.  
  1254. for (i = 14; i < lenr; i += 16) {
  1255.  
  1256.    printf(" %4i   ", i);
  1257.  
  1258.    m = (i + 16 > lenr) ? lenr : i + 16;
  1259.  
  1260.    for (j = i; j < m; j++) printf("%2.2x ", ethblock[j]);
  1261.  
  1262.    if (m < i + 16) for (j = m; j < i + 16; j++) printf("   ");
  1263.  
  1264.    printf("  ");
  1265.  
  1266.    for (j = i; j < m; j++) {
  1267.  
  1268.       k = ethblock[j];
  1269.  
  1270.       if ((k < (int) ' ') || (k > 0x7e)) k = ' ';
  1271.  
  1272.       printf("%c", k);
  1273.  
  1274.       }
  1275.  
  1276.    printf("\n");
  1277.  
  1278.    }
  1279.  
  1280. if (!lostflag) goto readnext;
  1281.  
  1282. done:
  1283.  
  1284. fclose(capture);
  1285.  
  1286. }
  1287.