parser.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4. #include <time.h>
  5. //#include <process.h>
  6. #include "parser.h"
  7. #define printit
  8. extern BOOL bFilter;
  9. extern int iline;
  10. extern char * author;
  11. extern char myipname[];
  12. extern int justheader;
  13. extern int gre,sortbysize,fromip,toip;
  14. int nomac=1;
  15. int mostird=0;
  16. char mypbuff[2048];
  17. // accounting variables
  18. #define MAXHASH 0xffff
  19. #define MAXTCPPORT 4096
  20. unsigned long *iph=NULL; //[MAXHASH];
  21. typedef struct {
  22. unsigned short from;
  23. unsigned short to;
  24. unsigned long byte;
  25. unsigned short pkt;
  26. unsigned short sport;
  27. unsigned short dport;
  28. } ta;
  29. ta *acc = NULL;
  30. unsigned long tcppb[MAXTCPPORT];
  31. unsigned long tcppp[MAXTCPPORT];
  32. unsigned long typp[255];
  33. unsigned long typb[255];
  34. unsigned long udpb,tcpb,udpp,tcpp;
  35. time_t elapsed=0;
  36. int iCycle=10;
  37. int iScreen=1;
  38. int iFile=0;
  39. long lNum=0;
  40. char filename[128];
  41. char intlist[128];
  42. int iRun=1;
  43. int iDetail=0;
  44. FILE *f=NULL;
  45. int iProto=0;
  46. int iSum=0;
  47. char execname[255];
  48. char pbuf[8196];
  49. char str[255];
  50. extern char pattern[];
  51. #ifndef LINUX
  52. int iLnxplus=0; // Windows buffer without the MAC frame !
  53. #else
  54. int iLnxplus=14; // Linux plus IP header len =14 !!!
  55. #endif
  56. //
  57. // A list of protocol types in the IP protocol header
  58. //
  59. char *szProto[255] = {"IP", // 0
  60. "ICMP", // 1
  61. "IGMP", // 2
  62. "GGP", // 3
  63. "IP", // 4
  64. "ST", // 5
  65. "TCP", // 6
  66. "UCL", // 7
  67. "EGP", // 8
  68. "IGP", // 9
  69. "BBN-RCC-MON", // 10
  70. "NVP-II", // 11
  71. "PUP", // 12
  72. "ARGUS", // 13
  73. "EMCON", // 14
  74. "XNET", // 15
  75. "CHAOS", // 16
  76. "UDP", // 17
  77. "MUX", // 18
  78. "DCN-MEAS", // 19
  79. "HMP", // 20
  80. "PRM", // 21
  81. "XNS-IDP", // 22
  82. "TRUNK-1", // 23
  83. "TRUNK-2", // 24
  84. "LEAF-1", // 25
  85. "LEAF-2", // 26
  86. "RDP", // 27
  87. "IRTP", // 28
  88. "ISO-TP4", // 29
  89. "NETBLT", // 30
  90. "MFE-NSP", // 31
  91. "MERIT-INP", // 32
  92. "SEP", // 33
  93. "3PC", // 34
  94. "IDPR", // 35
  95. "XTP", // 36
  96. "DDP", // 37
  97. "IDPR-CMTP", // 38
  98. "TP++", // 39
  99. "IL", // 40
  100. "SIP", // 41
  101. "SDRP", // 42
  102. "SIP-SR", // 43
  103. "SIP-FRAG", // 44
  104. "IDRP", // 45
  105. "RSVP", // 46
  106. "GRE", // 47
  107. "MHRP", // 48
  108. "BNA", // 49
  109. "IPSEC-ESP", // 50
  110. "IPSEC-AH", // 51
  111. "I-NLSP", // 52
  112. "SWIPE", // 53
  113. "NHRP", // 54
  114. "?55?", // 55
  115. "?56?", // 56
  116. "SKIO", // 57
  117. "V6ICMP", // 58
  118. "V6NoNXT", // 59
  119. "V6OPT", // 60
  120. "int.host", // 61
  121. "CFTP", // 62
  122. "loc.net", // 63
  123. "SAT-EXPAK", // 64
  124. "KRYPTOLAN", // 65
  125. "RVD", // 66
  126. "IPPC", // 67
  127. "dist.fs", // 68
  128. "SAT-MON", // 69
  129. "VISA", // 70
  130. "IPCV", // 71
  131. "CPNX", // 72
  132. "CPHB", // 73
  133. "WSN", // 74
  134. "PVP", // 75
  135. "BR-SAT-MON", // 76
  136. "SUN-ND", // 77
  137. "WB-MON", // 78
  138. "WB-EXPAK", // 79
  139. "ISO-IP", // 80
  140. "VMTP", // 81
  141. "SECURE-VMTP",// 82
  142. "VINES", // 83
  143. "TTP", // 84
  144. "NSFNET-IGP", // 85
  145. "DGP", // 86
  146. "TCF", // 87
  147. "IGRP", // 88
  148. "OSPF", // 89
  149. "Sprite-RPC", // 90
  150. "LARP", // 91
  151. "MTP", // 92
  152. "AX.25", // 93
  153. "IPIP", // 94
  154. "MICP", // 95
  155. "SCC-SP", // 96
  156. "ETHERIP", // 97
  157. "ENCAP", // 98
  158. "priv.enc", // 99
  159. "GMTP" // 99
  160. };
  161. //
  162. // The types of IGMP messages
  163. //
  164. char *szIgmpType[] = {"",
  165. "Host Membership Query",
  166. "HOst Membership Report",
  167. "",
  168. "",
  169. "",
  170. "Version 2 Membership Report",
  171. "Leave Group",
  172. "",
  173. ""
  174. };
  175. //
  176. // Function: PrintRawBytes
  177. //
  178. // Description:
  179. // This function simply prints out a series of bytes
  180. // as hexadecimal digits.
  181. //
  182. void PrintRawBytes(BYTE *ptr, DWORD len)
  183. {
  184. int i,j;
  185. // if (! iFile) {
  186. *(ptr+len)=0;
  187. if ((*pattern==0) || strstr(ptr,pattern) ) {
  188. fprintf(iFile?f:stdout,"%s",pbuf);
  189. fprintf(iFile?f:stdout," " );
  190. while (len > 0) {
  191. for(i=0; i < 16; i++) {
  192. fprintf(iFile?f:stdout,"%x%x ", HI_WORD(*ptr), LO_WORD(*ptr));
  193. len--;
  194. ptr++;
  195. if (len == 0) {j=i++; while(++j < 16) fprintf(iFile?f:stdout," "); break; }
  196. }
  197. fprintf(iFile?f:stdout," ");
  198. for(j=0; j < i; j++) fprintf(iFile?f:stdout,"%c",isprint(*(ptr-i+j))?*(ptr-i+j):'.');
  199. if (len) fprintf(iFile?f:stdout,"\n ");
  200. }
  201. // } else {
  202. // fwrite(ptr,sizeof(BYTE),len,f);
  203. // }
  204. }
  205. }
  206. static char *ICMPTypeTable[]={
  207. "Echo Reply", "ICMP 1", "ICMP 2", "Dest Unreachable","SrcQuench", "Redirect", "6", "7","Echo Request","9","10",
  208. "Time Exceed", "ParamPrblm", "Timestamp", "Timestamp reply","InfoRqst", "InfoRply"
  209. };
  210. static char *Dstunreach[]={
  211. "net unreach.","host unreach.","protocol unreach.","port unreach.",
  212. "frag needed","source route?","",""
  213. };
  214. int DecodeICMPHeader(WSABUF *wsabuf, DWORD iphdrlen) {
  215. BYTE *hdr = (BYTE *)((BYTE *)wsabuf->buf + iphdrlen + iLnxplus );
  216. unsigned short type,code,chksum,
  217. id,
  218. seq;
  219. unsigned long resptime,r1,r2;
  220. BYTE *hhh;
  221. SOCKADDR_IN addr;
  222. type=*hdr++; code=*hdr++;
  223. sprintf(str," Type:%-12s Code:%3d,",ICMPTypeTable[type],code);
  224. strcat(pbuf,str);
  225. memcpy(&chksum, hdr, 2);
  226. hdr += 2; hhh=hdr;
  227. memcpy(&id, hdr, 2);
  228. hdr += 2;
  229. memcpy(&seq, hdr, 2);
  230. hdr+=2;
  231. // memcpy(&resptime, hdr, 4);
  232. // hdr+=4;
  233. switch (type) {
  234. case 3:
  235. memcpy(&addr.sin_addr.s_addr, hdr+16, 4);
  236. if (code==4 ) sprintf(str,"frag needed-Max MTU:%u at %-15s\n",ntohs(seq), inet_ntoa(addr.sin_addr));
  237. else sprintf(str,"%s at %-15s\n",Dstunreach[code&7],inet_ntoa(addr.sin_addr));
  238. hdr+=iphdrlen;
  239. break;
  240. case 11:
  241. memcpy(&addr.sin_addr.s_addr, hdr+16, 4);
  242. sprintf(str,"%s at %-15s\n",code?"frag reass. exceed":"ttl exceed",inet_ntoa(addr.sin_addr));
  243. hdr+=iphdrlen;
  244. break;
  245. case 12:
  246. memcpy(&addr.sin_addr.s_addr, hdr+16, 4);
  247. sprintf(str," err:%d at %-15s\n",id,inet_ntoa(addr.sin_addr));
  248. hdr+=iphdrlen;
  249. break;
  250. case 4:
  251. memcpy(&addr.sin_addr.s_addr, hdr+16, 4);
  252. sprintf(str," wait for %-15s\n",ntohs(id),inet_ntoa(addr.sin_addr));
  253. hdr+=iphdrlen;
  254. break;
  255. case 5:
  256. memcpy(&addr.sin_addr.s_addr, hhh, 4);
  257. sprintf(str," from gw: %-15s\n",inet_ntoa(addr.sin_addr));
  258. hdr+=iphdrlen;
  259. break;
  260. case 0:
  261. case 8:
  262. sprintf(str," Id:%3u Seq:%3u\n",ntohs(id),ntohs(seq));
  263. break;
  264. case 13:
  265. case 14:
  266. memcpy(&resptime, hdr, 4);
  267. hdr+=4;
  268. memcpy(&r1, hdr, 4);
  269. hdr+=4;
  270. memcpy(&r2, hdr, 4);
  271. hdr+=4;
  272. sprintf(str," Id:%3u Seq:%3d Rec/Tr %ld/%ld ms\n",ntohs(id),ntohs(seq),ntohl(r1)-ntohl(resptime),ntohl(r2)-ntohl(resptime));
  273. break;
  274. case 15:
  275. case 16:
  276. sprintf(str," Id:%3u Seq:%3d\n",ntohs(id),ntohs(seq));
  277. break;
  278. }
  279. strcat(pbuf,str);
  280. return hdr-(BYTE *)(wsabuf->buf + iphdrlen + iLnxplus);
  281. }
  282. //
  283. // Function: DecodeIGMPHeader
  284. //
  285. // Description:
  286. // This function takes a pointer to a buffer containing
  287. // an IGMP packet and prints it out in a readable form.
  288. //
  289. int DecodeIGMPHeader(WSABUF *wsabuf, DWORD iphdrlen) {
  290. BYTE *hdr = (BYTE *)((BYTE *)wsabuf->buf + iphdrlen + iLnxplus);
  291. unsigned short chksum,
  292. version,
  293. type,
  294. maxresptime;
  295. SOCKADDR_IN addr;
  296. version = HI_WORD(*hdr);
  297. type = LO_WORD(*hdr);
  298. hdr++;
  299. maxresptime = *hdr;
  300. hdr++;
  301. memcpy(&chksum, hdr, 2);
  302. chksum = ntohs(chksum);
  303. hdr += 2;
  304. memcpy(&(addr.sin_addr.s_addr), hdr, 4);
  305. sprintf(str," IGMP HEADER:\n");
  306. strcat(pbuf,str);
  307. if ((type == 1) || (type == 2)) version = 1;
  308. else version = 2;
  309. sprintf(str," IGMP Version = %d\n IGMP Type = %s\n",version, szIgmpType[type]);
  310. strcat(pbuf,str);
  311. if (version == 2) {
  312. sprintf(str," Max Resp Time = %d\n", maxresptime);
  313. strcat(pbuf,str);
  314. }
  315. sprintf(str," IGMP Grp Addr = %s\n", inet_ntoa(addr.sin_addr));
  316. strcat(pbuf,str);
  317. return 8;
  318. }
  319. //
  320. // Function: DecodeUDPHeader
  321. //
  322. // Description:
  323. // This function takes a buffer which points to a UDP
  324. // header and prints it out in a readable form.
  325. //
  326. int DecodeUDPHeader(WSABUF *wsabuf, DWORD iphdrlen) {
  327. BYTE *hdr = (BYTE *)((BYTE *)wsabuf->buf + iphdrlen + iLnxplus);
  328. unsigned short shortval,
  329. udp_src_port,
  330. udp_dest_port,
  331. udp_len,
  332. udp_chksum;
  333. memcpy(&shortval, hdr, 2);
  334. udp_src_port = ntohs(shortval);
  335. hdr += 2;
  336. memcpy(&shortval, hdr, 2);
  337. udp_dest_port = ntohs(shortval);
  338. hdr += 2;
  339. memcpy(&shortval, hdr, 2);
  340. udp_len = ntohs(shortval);
  341. hdr += 2;
  342. memcpy(&shortval, hdr, 2);
  343. udp_chksum = ntohs(shortval);
  344. hdr += 2;
  345. sprintf(str," UDP: SPort: %-05d | DPort: %-05d",udp_src_port, udp_dest_port);
  346. strcat(pbuf,str);
  347. sprintf(str," | Len: %-05d | CSum: 0x%08x\n",udp_len, udp_chksum);
  348. strcat(pbuf,str);
  349. return hdr-(BYTE *)(wsabuf->buf + iphdrlen + iLnxplus);
  350. }
  351. //
  352. // Function: DecodeTCPHeader
  353. //
  354. // Description:
  355. // This function takes a buffer pointing to a TCP header
  356. // and prints it out in a readable form.
  357. //
  358. int DecodeTCPHeader(WSABUF *wsabuf, DWORD iphdrlen) {
  359. BYTE *hdr = (BYTE *)((BYTE *)wsabuf->buf + iphdrlen + iLnxplus);
  360. unsigned short shortval;
  361. unsigned long longval;
  362. memcpy(&shortval, hdr, 2);
  363. shortval = ntohs(shortval);
  364. sprintf(str," TCP: SPort: %u", shortval);
  365. strcat(pbuf,str);
  366. hdr += 2;
  367. memcpy(&shortval, hdr, 2);
  368. shortval = ntohs(shortval);
  369. sprintf(str," DPort: %u", shortval);
  370. strcat(pbuf,str);
  371. hdr += 2;
  372. memcpy(&longval, hdr, 4);
  373. longval = ntohl(longval);
  374. sprintf(str," Seq: %lX", longval);
  375. strcat(pbuf,str);
  376. hdr += 4;
  377. memcpy(&longval, hdr, 4);
  378. longval = ntohl(longval);
  379. sprintf(str," ACK: %lX", longval);
  380. strcat(pbuf,str);
  381. hdr += 4;
  382. // printf(" Header Len : %d (bytes %d)\n", HI_WORD(*hdr), (HI_WORD(*hdr) * 4));
  383. memcpy(&shortval, hdr, 2);
  384. shortval = ntohs(shortval) & 0x3F;
  385. sprintf(str," Flags: ");
  386. strcat(pbuf,str);
  387. if (shortval & 0x20) strcat(pbuf,"URG ");
  388. if (shortval & 0x10) strcat(pbuf,"ACK ");
  389. if (shortval & 0x08) strcat(pbuf,"PSH ");
  390. if (shortval & 0x04) strcat(pbuf,"RST ");
  391. if (shortval & 0x02) strcat(pbuf,"SYN ");
  392. if (shortval & 0x01) strcat(pbuf,"FIN ");
  393. strcat(pbuf,"\n");
  394. hdr += 2;
  395. memcpy(&shortval, hdr, 2);
  396. shortval = ntohs(shortval);
  397. // printf(" Window size: %d\n", shortval);
  398. hdr += 2;
  399. memcpy(&shortval, hdr, 2);
  400. shortval = ntohs(shortval);
  401. // printf(" TCP Chksum : %d\n", shortval);
  402. hdr += 2;
  403. memcpy(&shortval, hdr, 2);
  404. shortval = ntohs(shortval);
  405. hdr += 2;
  406. // printf(" Urgent ptr : %d\n", shortval);
  407. return hdr-(BYTE *)(wsabuf->buf + iphdrlen + iLnxplus);
  408. }
  409. int DecodeGREHeader(WSABUF *wsabuf, DWORD iphdrlen,DWORD bytesret,
  410. unsigned int srcip, unsigned short srcport, unsigned long srcnet,unsigned int destip, unsigned short destport, unsigned long destnet,
  411. unsigned short xport,unsigned int xip, unsigned long xnet)
  412. {
  413. BYTE *hdr = (BYTE *)((BYTE *)wsabuf->buf + iphdrlen + iLnxplus);
  414. unsigned short shortval;
  415. unsigned long longval;
  416. int ipe;
  417. BYTE *orihdr;
  418. char *sstr;
  419. SOCKADDR_IN srcaddr;
  420. orihdr=hdr;
  421. memcpy(&shortval, hdr, 2);
  422. shortval = ntohs(shortval);
  423. sprintf(str," GRE Flag: %u Prot:", shortval);
  424. strcat(mypbuff,str);
  425. hdr += 2;
  426. memcpy(&shortval, hdr, 2);
  427. shortval = ntohs(shortval);
  428. ipe=0;
  429. sstr=str;
  430. // sprintf(str," Prot: %u", shortval);
  431. switch ( shortval ) {
  432. case 4: sstr="SNA";
  433. break;
  434. case 0xfe: sstr="OSI";
  435. break;
  436. case 0x200: sstr="PUP";
  437. break;
  438. case 0x600: sstr="XNS";
  439. break;
  440. case 0x800: sstr="IP";
  441. ipe=1;
  442. break;
  443. case 0x804: sstr="Chaos";
  444. break;
  445. case 0x806: sstr="ARP";
  446. break;
  447. case 0x6558: sstr="Tr.bridge";
  448. break;
  449. default: sprintf(str,"%u", shortval);
  450. break;
  451. }
  452. hdr += 2;
  453. strcat(mypbuff,sstr);
  454. if (ipe && gre) {
  455. int plusment,jj,protoment;
  456. plusment=iLnxplus;
  457. protoment=iProto;
  458. if (iProto==47) iProto=0;
  459. iLnxplus+=4;
  460. nomac=0;
  461. iLnxplus=plusment+24;
  462. DecodeIPHeader(wsabuf,srcip,srcport,srcnet,destip,destport,destnet,bytesret,xport,xip,xnet);
  463. nomac=1;
  464. iLnxplus=plusment;
  465. iProto=protoment;
  466. }
  467. return -1;
  468. // return hdr-(BYTE *)(wsabuf->buf + iphdrlen + iLnxplus);
  469. }
  470. int ClearIPAcc() {
  471. unsigned long i;
  472. ta *tai;
  473. for(i=0;i<MAXHASH;i++) *(iph + i)=0;
  474. tai=acc;
  475. for(i=0;i<MAXHASH;i++) { tai->from=tai->to=0; tai++; }
  476. for (i=0;i<MAXTCPPORT; i++) tcppb[i]=tcppp[i]=0;
  477. udpb=udpp=tcpp=tcpb=0;
  478. for (i=0;i<255; i++) typp[i]=0;
  479. for (i=0;i<255; i++) typb[i]=0;
  480. return 0;
  481. };
  482. int InitIPAcc() {
  483. acc=malloc(MAXHASH*sizeof(ta));
  484. iph=malloc(MAXHASH*sizeof(long));
  485. if (!acc || !iph ) return 0;
  486. ClearIPAcc();
  487. time(&elapsed);
  488. return 1;
  489. }
  490. int bytesort(const void *s1, const void *s2) { // sorting tale in byte order
  491. ta *d1;
  492. ta *d2;
  493. d1= (ta *)s1; d2=(ta *)s2;
  494. if (d1->byte > d2->byte) return -1;
  495. if (d1->byte < d2->byte) return 1;
  496. return 0;
  497. }
  498. int countsort(const void *s1, const void *s2) { // sorting tale in packet count order
  499. ta *d1;
  500. ta *d2;
  501. d1= (ta *)s1; d2=(ta *)s2;
  502. if (d1->pkt > d2->pkt) return -1;
  503. if (d1->pkt < d2->pkt) return 1;
  504. return 0;
  505. }
  506. int CloseIPAcc( long ti) {
  507. unsigned long i;
  508. ta *tai;
  509. SOCKADDR_IN srcaddr;
  510. SOCKADDR_IN dstaddr;
  511. float ff;
  512. char str[16];
  513. unsigned long j,k,l;
  514. int lin=0;
  515. int linn;
  516. time(&elapsed);
  517. if (iFile) f=fopen(filename,"w+");
  518. k=0;
  519. if (sortbysize) qsort(acc,MAXHASH,sizeof(ta),bytesort);
  520. else qsort(acc,MAXHASH,sizeof(ta),countsort);
  521. ff=0.0;
  522. for (i=0;i<255;i++) ff+=typb[i];
  523. for (i=0; i<MAXHASH; i++) {
  524. tai=acc + i;
  525. if ((tai->from!=0) && (tai->to!=0)) ++k;
  526. }
  527. if (iScreen) {
  528. #ifndef LINUX
  529. system("cls");
  530. #else
  531. system("clear");
  532. // printf("\033[1~");
  533. #endif
  534. printf("%-16s Speed: %5.2f Kbit/s , %ld IP pairs / %ld secs. %s@%s.hu",myipname,ff/ti/1024*8,k,ti,author,author);
  535. printf("\nProt:"); j=0; ++lin;
  536. while (1) {
  537. l=k=0;
  538. for (i=0;i<100;i++) if ( typb[i]>k) { k=typb[i]; l=i; }
  539. if (k==0) break;
  540. if ((j>0) && ((j%3)==0)) { printf("\n "); ++lin; }
  541. if (k>1024*1024) printf(" %-8.8s:%5.1fk/%-6.1f M",szProto[l],(float)typp[l]/1024,(float)k/(1024*1024));
  542. else if (k>1024) printf(" %-8.8s:%5ld/%-6.1f k",szProto[l],typp[l],(float)k/1024);
  543. else printf(" %-8.8s:%5ld/%-8ld",szProto[l],typp[l],k);
  544. typb[l]=0;
  545. ++j;
  546. }
  547. printf("\nPort:"); j=0; ++lin;
  548. k=0; linn=lin;
  549. while (1) {
  550. l=k=0;
  551. for (i=0;i<MAXTCPPORT;i++) if (tcppb[i]>k) { k=tcppb[i]; l=i; }
  552. if (k==0) break;
  553. if (j && (j%4)==0) {
  554. if (lin >= linn+1) break;
  555. printf("\n ");
  556. ++lin;
  557. }
  558. if (k>1024*1024) printf(" %04d:%4.1fk/%-5.1f M",l,(float)tcppp[l]/1024,(float)k/(1024*1024));
  559. else if (k>1024) printf(" %04d:%4ld/%-5.1f k",l,tcppp[l],(float)k/1024);
  560. else printf(" %04d:%4ld/%-7ld",l,tcppp[l],k);
  561. tcppb[l]=0;
  562. ++j;
  563. }
  564. } else if (f) {
  565. fprintf(f,"%-16s Speed: %5.2f Kbit/s , %ld IP pairs / %ld secs. %s@%s.hu",myipname,ff/ti/1024*8,k,ti,author,author);
  566. fprintf(f,"\nProt:"); j=0;
  567. while (1) {
  568. l=k=0;
  569. for (i=0;i<100;i++) if ( typb[i]>k) { k=typb[i]; l=i; }
  570. if (k==0) break;
  571. if (k>1024*1024) fprintf(f," %-8.8s:%5.1fk/%-6.1f M",szProto[l],(float)typp[l]/1024,(float)k/(1024*1024));
  572. else if (k>1024) fprintf(f," %-8.8s:%5ld/%-6.1f k",szProto[l],typp[l],(float)k/1024);
  573. else fprintf(f," %-8.8s:%5ld/%-8ld",szProto[l],typp[l],k);
  574. typb[l]=0;
  575. ++j;
  576. }
  577. printf("\nPort:"); j=0;
  578. k=0; linn=lin;
  579. while (1) {
  580. l=k=0;
  581. for (i=0;i<MAXTCPPORT;i++) if (tcppb[i]>k) { k=tcppb[i]; l=i; }
  582. if (k==0) break;
  583. if (k>1024*1024) fprintf(f," %04d:%4.1fk/%-5.1f M",l,(float)tcppp[l]/1024,(float)k/(1024*1024));
  584. else if (k>1024) fprintf(f," %04d:%4ld/%-5.1f k",l,tcppp[l],(float)k/1024);
  585. else fprintf(f," %04d:%4ld/%-7ld",l,tcppp[l],k);
  586. tcppb[l]=0;
  587. ++j;
  588. }
  589. }
  590. for (i=0; i<MAXHASH; i++) {
  591. tai=acc + i;
  592. if ((tai->from!=0) && (tai->to!=0)) { ++k;
  593. if (!iSum) {
  594. dstaddr.sin_addr.s_addr = htonl(*(iph+tai->from));
  595. srcaddr.sin_addr.s_addr = htonl(*(iph+(tai->to)));
  596. strcpy(str,inet_ntoa(dstaddr.sin_addr));
  597. if (iScreen && (++lin<iline) ) printf("\n%-15s\t%-15s\t%5d pkt, %10ld byte :%7.2f Kbps",str,inet_ntoa(srcaddr.sin_addr),tai->pkt,tai->byte,((float)tai->byte)/ti/1024*8);
  598. if (f) fprintf(f,"%-15s\t%-15s\t%d\t%ld\n",str,inet_ntoa(srcaddr.sin_addr),tai->pkt,tai->byte);
  599. }
  600. }
  601. }
  602. if (iScreen) printf("\n");
  603. #ifdef LINUX
  604. if (iScreen) fflush(stdout);
  605. #endif
  606. ClearIPAcc();
  607. if (f) {
  608. char cmdline[255];
  609. fclose(f);
  610. // if (*execname) _spawnle(_P_NOWAIT,execname,execname,filename);
  611. // if (*execname) _execl(execname,execname);
  612. if (*execname) {
  613. #ifndef LINUX
  614. sprintf(cmdline,"%s %s",execname,filename);
  615. #else
  616. sprintf(cmdline,"%s %s",execname,filename);
  617. #endif
  618. system(cmdline);
  619. // iRun=0;
  620. }
  621. }
  622. f=NULL;
  623. return 0;
  624. }
  625. unsigned short FindIPHash( unsigned long ip ) {
  626. unsigned short hashval;
  627. unsigned long *ipt;
  628. hashval = (unsigned short)(((ip&0xFFFF0000)>>16) ^ (ip&0x0000FFFF));
  629. ipt=iph + hashval;
  630. while (*ipt != 0 && (*ipt!=ip)) { ipt++; hashval++; }
  631. if (*ipt==0) *ipt=ip;
  632. return hashval;
  633. }
  634. unsigned short SetIPAcc( unsigned long src, unsigned long dst, unsigned long byte, unsigned short typ, unsigned short sport, unsigned short dport) {
  635. unsigned short from,to,hash;
  636. ta *tai;
  637. hash=0;
  638. if (src) {
  639. if (fromip) from=FindIPHash(src); else from=-1;
  640. if (toip) to=FindIPHash(dst); else to=-1;
  641. hash=from^to;
  642. tai=acc + hash;
  643. while ( ((tai->from!=from) && (tai->to!=to)) && ((tai->from!=0) && (tai->to!=0)) ) {tai++; hash++; }
  644. if ((tai->from==0)&&(tai->to==0)) {
  645. tai->byte=byte; tai->from=from; tai->to=to; tai->pkt=1;
  646. } else { tai->byte+=byte; tai->pkt++; }
  647. typp[typ]++;
  648. typb[typ]+=byte;
  649. if ((sport>0) && (sport<MAXTCPPORT)) { tcppp[sport]++; tcppb[sport]+=byte; }
  650. if ((dport>0) && (dport<MAXTCPPORT)) { tcppp[dport]++; tcppb[dport]+=byte; }
  651. }
  652. return hash;
  653. }
  654. //
  655. // Function: DecodeIPHeader
  656. //
  657. // Description:
  658. // This function takes a pointer to an IP header and prints
  659. // it out in a readable form.
  660. //
  661. int DecodeIPHeader(WSABUF *wsabuf, unsigned int srcip, unsigned short srcport, unsigned long srcnet,
  662. unsigned int destip, unsigned short destport, unsigned long destnet, DWORD bytesret,
  663. unsigned short xport,unsigned int xip, unsigned long xnet)
  664. {
  665. BYTE *hdr = (BYTE *)wsabuf->buf,
  666. *nexthdr = NULL,
  667. *ohdr;
  668. unsigned short shortval;
  669. SOCKADDR_IN srcaddr,
  670. destaddr;
  671. unsigned short ip_version,
  672. ip_hdr_len,
  673. ip_tos,
  674. ip_total_len,
  675. ip_id,
  676. ip_flags,
  677. ip_ttl,
  678. ip_frag_offset,
  679. ip_proto,
  680. ip_hdr_chksum,
  681. ip_src_port,
  682. ip_dest_port;
  683. unsigned int ip_src,
  684. ip_dest;
  685. BOOL bPrint = FALSE;
  686. char ip_prtype=0;
  687. int j;
  688. time_t tt;
  689. struct tm *tmm;
  690. ohdr=hdr;
  691. if (iLnxplus) ip_prtype=*(hdr+iLnxplus-1);
  692. if (ip_prtype) return 0;
  693. hdr += iLnxplus;
  694. ip_version = HI_WORD(*hdr);
  695. ip_hdr_len = LO_WORD(*hdr) * 4;
  696. nexthdr = (BYTE *)((BYTE *)hdr + ip_hdr_len);
  697. hdr++;
  698. ip_tos = *hdr;
  699. hdr++;
  700. memcpy(&shortval, hdr, 2);
  701. ip_total_len = ntohs(shortval);
  702. hdr += 2;
  703. memcpy(&shortval, hdr, 2);
  704. ip_id = ntohs(shortval);
  705. hdr += 2;
  706. ip_flags = ((*hdr) >> 5);
  707. memcpy(&shortval, hdr, 2);
  708. ip_frag_offset = ((ntohs(shortval)) & 0x1FFF);
  709. hdr += 2;
  710. ip_ttl = *hdr;
  711. hdr++;
  712. ip_proto = *hdr;
  713. hdr++;
  714. memcpy(&shortval, hdr, 2);
  715. ip_hdr_chksum = ntohs(shortval);
  716. hdr += 2;
  717. memcpy(&srcaddr.sin_addr.s_addr, hdr, 4);
  718. ip_src = ntohl(srcaddr.sin_addr.s_addr);
  719. hdr += 4;
  720. memcpy(&destaddr.sin_addr.s_addr, hdr, 4);
  721. ip_dest = ntohl(destaddr.sin_addr.s_addr);
  722. hdr += 4;
  723. //
  724. // If packet is UDP, TCP, or IGMP read ahead and
  725. // get the port values.
  726. //
  727. ip_src_port=ip_dest_port=0;
  728. if (((ip_proto == 2) ||
  729. (ip_proto == 6) ||
  730. (ip_proto == 17)) ) //&& bFilter)
  731. {
  732. memcpy(&ip_src_port, nexthdr, 2);
  733. ip_src_port = ntohs(ip_src_port);
  734. memcpy(&ip_dest_port, nexthdr+2, 2);
  735. ip_dest_port = ntohs(ip_dest_port);
  736. };
  737. bPrint = 0;
  738. // xaok= (xip!=0) && (((xip&xnet)==(ip_src&xnet))||((xip&xnet)==(ip_dest&xnet)));
  739. // saok= ((srcip==0)||((srcip&srcnet)==(ip_src&srcnet)));
  740. // daok = ((destip==0)||((destip&destnet)==(ip_dest&destnet)));
  741. // xpok=(xport!=0) && ((xport==ip_src_port)||(xport==ip_dest_port));
  742. // spok=((srcport==0)||(srcport == ip_src_port));
  743. // dpok=((destport==0)||(destport == ip_dest_port));
  744. //printf("\nf:%d xa:%d sa:%d da:%d xp:%d sp:%d dp:%d",bFilter,xaok,saok,daok,xpok,spok,dpok);
  745. // if (!bFilter || ( (xaok||(saok&&daok)) && (xpok||(spok&&dpok)))) {
  746. if ((!bFilter) || ((ip_proto==47)&&gre) ||
  747. (
  748. ((iProto==0)||(ip_proto==iProto)) &&
  749. (
  750. ((xip!=0) && (((xip&xnet)==(ip_src&xnet))||((xip&xnet)==(ip_dest&xnet)))
  751. ) || (
  752. ((srcip==0) || ((srcip&srcnet)==(ip_src&srcnet))) && ((destip==0)||((destip&destnet)==(ip_dest&destnet)))
  753. )
  754. )
  755. &&
  756. (
  757. ((xport!=0) && ((xport==ip_src_port)||(xport==ip_dest_port))
  758. ) || (
  759. ((srcport==0)||(srcport == ip_src_port))&&((destport==0)||(destport == ip_dest_port))
  760. )
  761. )
  762. )
  763. ) {
  764. if (! iDetail) {
  765. if ((ip_proto==47)&&gre) {
  766. *mypbuff=0;
  767. DecodeGREHeader(wsabuf, ip_hdr_len, bytesret,
  768. srcip,srcport,srcnet,destip,destport,destnet,xport,xip,xnet);
  769. // SetIPAcc(0,0,0,0,0,0);
  770. return ip_hdr_len;
  771. }
  772. SetIPAcc(ip_src,ip_dest,ip_total_len,ip_proto,ip_src_port,ip_dest_port);
  773. }
  774. else bPrint=TRUE;
  775. // printf("%d %ld %ld %ld %ld",ip_proto,xip,xip&xnet,ip_src&xnet,ip_dest&xnet);
  776. } else {
  777. if (! iDetail) SetIPAcc(0,0,0,0,0,0);
  778. // else bPrint=TRUE;
  779. }
  780. time(&tt);
  781. if ((!iSum && ( tt-elapsed > iCycle)) || !iRun || mostird) {
  782. mostird=0;
  783. if (! iDetail) CloseIPAcc(tt-elapsed-1);
  784. else {
  785. time(&elapsed);
  786. if (f) fclose(f);
  787. if (iFile) f=fopen(filename,"a");
  788. }
  789. }
  790. if (lNum) { if (--lNum <= 0) iRun=0; }
  791. //
  792. *pbuf=0;
  793. if (bPrint) {
  794. tmm=localtime(&tt);
  795. if (! nomac ) {
  796. strcpy(pbuf,mypbuff);
  797. } else {
  798. sprintf(str,"\n%4.4d.%2.2d.%2.2d %2.2d:%2.2d:%2.2d ",
  799. tmm->tm_year+1900,tmm->tm_mon+1,tmm->tm_mday,tmm->tm_hour,tmm->tm_min,tmm->tm_sec);
  800. strcat(pbuf,str);
  801. #ifdef LINUX
  802. sprintf(str,"%x:%x:%x:%x:%x:%x > %x:%x:%x:%x:%x:%x",*ohdr,*(ohdr+1),*(ohdr+2),*(ohdr+3),*(ohdr+4),*(ohdr+5),
  803. *(ohdr+6),*(ohdr+7),*(ohdr+8),*(ohdr+9),*(ohdr+10),*(ohdr+11));
  804. strcat(pbuf,str);
  805. #endif
  806. }
  807. sprintf(str," %d bytes\n%-15s>", ip_total_len, inet_ntoa(srcaddr.sin_addr));
  808. strcat(pbuf,str);
  809. sprintf(str,"%-15s", inet_ntoa(destaddr.sin_addr));
  810. strcat(pbuf,str);
  811. sprintf(str," TTL:%-3d Proto:%-6s F:%d/%d TOS:%X%X\n",
  812. ip_ttl, szProto[ip_proto],ip_flags,ip_frag_offset,HI_WORD(ip_tos), LO_WORD(ip_tos));
  813. strcat(pbuf,str);
  814. if (iFile) strcat(pbuf,".");
  815. strcpy(mypbuff,pbuf);
  816. }
  817. else return ip_hdr_len;
  818. if (justheader) { if (*pbuf) fprintf(iFile?f:stdout,"%s",pbuf); return ip_hdr_len; }
  819. if (iDetail) {
  820. switch (ip_proto) {
  821. case 1: // ICMP
  822. j=DecodeICMPHeader(wsabuf, ip_hdr_len);
  823. break;
  824. case 2: // IGMP
  825. j=DecodeIGMPHeader(wsabuf, ip_hdr_len);
  826. break;
  827. case 6: // TCP
  828. j=DecodeTCPHeader(wsabuf, ip_hdr_len);
  829. break;
  830. case 17: // UDP
  831. j=DecodeUDPHeader(wsabuf, ip_hdr_len);
  832. break;
  833. case 47: // UDP
  834. j=DecodeGREHeader(wsabuf, ip_hdr_len, bytesret,
  835. srcip,srcport,srcnet,destip,destport,destnet,xport,xip,xnet);
  836. break;
  837. default:
  838. j=0; hdr=(BYTE *)wsabuf->buf;
  839. sprintf(str," No decoder installed for protocol\n");
  840. strcat(pbuf,str);
  841. break;
  842. }
  843. if (j>=0) PrintRawBytes(hdr+j,bytesret-j-ip_hdr_len-12); //(hdr-(BYTE *)(wsabuf->buf + iLnxplus)));
  844. }
  845. else if (*pbuf) fprintf(iFile?f:stdout,"%s",pbuf);
  846. return ip_hdr_len;
  847. }