00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 #include <stdlib.h>
00083 #include <stdio.h>
00084 #include <math.h>
00085 #include <sys/time.h>
00086
00087 #define min(a,b) ((a) <= (b) ? (a) : (b))
00088 #define max(a,b) ((a) >= (b) ? (a) : (b))
00089
00090 int report_ACK_err = 1;
00091
00092 void Usage(char *s)
00093 {
00094 fprintf (stderr,"\nUsage: %s\n", s);
00095 fprintf (stderr," [-w file_name] (name for output file)\n");
00096 fprintf (stderr," [-r file_name] (name for input file)\n");
00097 fprintf (stderr,"If either -w or -r is omitted, stdout(stdin) is used\n");
00098 fprintf (stderr,"\n");
00099 exit(-1);
00100 }
00101
00102 FILE *dumpFP, *outFP;
00103 FILE *logFP;
00104
00105 struct timeval recvTime;
00106 struct timeval lastTime = {0,0};
00107
00108 char ts[20];
00109
00110 char sh[25];
00111
00112 char gt[3];
00113 char lt[3];
00114 char dh[25];
00115
00116 char fl[5];
00117 char p1[50];
00118 char p2[50];
00119 char p3[50];
00120
00121
00122 unsigned long begin_seq, end_seq, seq_bytes, new_ack;
00123 unsigned long current_synseq;
00124
00125 int has_seq, has_ack;
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 unsigned long current_request_end, last_request_end;
00142
00143
00144
00145
00146
00147
00148
00149
00150 unsigned long current_response_end, last_response_end;
00151
00152
00153
00154 int syn_count = 0;
00155 int req_count = 0;
00156 int rsp_count = 0;
00157 int fin_count = 0;
00158 int rst_count = 0;
00159 int trm_count = 0;
00160 int err_count = 0;
00161 int act_req_count = 0;
00162 int act_rsp_count = 0;
00163 int pending_fin_count = 0;
00164 int pending_rst_count = 0;
00165 int pending_ack_count = 0;
00166 int pending_oth_count = 0;
00167 int pending_cmb_count = 0;
00168
00169
00170
00171
00172 int have_pending_acks = 0;
00173 int have_pending_fins = 0;
00174 int have_pending_rsts = 0;
00175 int have_pending_othr = 0;
00176
00177 int have_ACK_error = 0;
00178 int have_value_error = 0;
00179 int have_FINdata_error = 0;
00180
00181 enum states {PENDING, SYN_SENT, FIN_SENT, RESET, IN_REQUEST, IN_RESPONSE};
00182
00183 enum states connection_state = PENDING;
00184 enum states last_state = PENDING;
00185
00186 enum inputs {SYN, FIN, RST, ACK_ONLY, DATA_ACK};
00187 enum inputs input_type;
00188
00189 char current_src[25] = "";
00190 char src_host[25];
00191 char src_port[10];
00192
00193 char current_dst[25] = "";
00194 char dst_host[25];
00195 char dst_port[10];
00196
00197
00198
00199
00200
00201
00202 char start_request_time[20];
00203
00204
00205
00206
00207
00208 char start_response_time[20];
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 char response_end_time[20];
00222 char request_end_time[20];
00223
00224 char FIN_sent_time[20];
00225 char RST_sent_time[20];
00226 char last_connection_time[20];
00227
00228 char input_name[256] = "";
00229 char output_name[256] = "";
00230 char log_name[256] = "";
00231
00232 char new_line[500];
00233
00234 long elapsed;
00235
00236 int new_address = 0;
00237 int rc = 0;
00238
00239 void error_line(char *s);
00240 void error_state(char *s);
00241 int parse_dump_record(void);
00242 void init_connection(void);
00243 void init_active(void);
00244 void check_tuple_reuse(void);
00245 int check_ACK_advance(unsigned long old_ack);
00246 void begin_REQ(void);
00247 void more_REQ(void);
00248 void begin_RSP(void);
00249 void more_RSP(void);
00250 void log_REQ(void);
00251 void log_RSP(void);
00252 void log_SYN(void);
00253 void log_END(char *how);
00254 void log_ACT(char *how);
00255 void log_nosyn(void);
00256 void log_connection(void);
00257 void log_log(void);
00258 long elapsed_ms(char *end, char *start);
00259 void get_host_port(char *adr, char *host, char *port);
00260 int get_sequence(char *p, unsigned long *begin, unsigned long *end,
00261 unsigned long *bytes);
00262
00263 void main (int argc, char* argv[])
00264 {
00265 int i;
00266
00267
00268 i = 1;
00269 while (i < argc) {
00270 if (strcmp (argv[i], "-r") == 0) {
00271
00272 if (++i >= argc) Usage (argv[0]);
00273 strcpy (input_name, argv[i]);
00274 }
00275 else if (strcmp (argv[i], "-w") == 0) {
00276
00277 if (++i >= argc) Usage (argv[0]);
00278 strcpy (output_name, argv[i]);
00279 }
00280 else
00281 Usage (argv[0]);
00282 i++;
00283 }
00284
00285
00286
00287
00288 if (strcmp(output_name, "") == 0)
00289
00290 outFP = stdout;
00291 else
00292 {
00293 if ((outFP = fopen (output_name, "w")) == NULL) {
00294 fprintf (stderr, "error opening %s\n", output_name);
00295 exit (-1);
00296 }
00297 }
00298
00299 if (strcmp(input_name, "") == 0)
00300
00301 dumpFP = stdin;
00302 else
00303 {
00304 if ((dumpFP = fopen (input_name, "r")) == NULL) {
00305 fprintf (stderr, "error opening %s\n", input_name);
00306 exit (-1);
00307 }
00308 }
00309
00310 strcpy(log_name, output_name);
00311 strcat(log_name, ".log");
00312 if ((logFP = fopen (log_name, "w")) == NULL) {
00313 fprintf (stderr, "error opening %s\n", log_name);
00314 exit (-1);
00315 }
00316
00317
00318
00319
00320
00321 while (!feof (dumpFP)) {
00322
00323
00324
00325
00326
00327 fgets (new_line, sizeof(new_line), dumpFP);
00328
00329
00330
00331 sscanf (new_line, "%s %s %s %s %s %s %s %s %s",
00332 &ts, <, &sh, >, &dh, &fl, &p1, &p2, &p3);
00333
00334
00335
00336
00337
00338
00339
00340 if ((strcmp(current_src, sh) != 0) ||
00341 (strcmp(current_dst, dh) != 0))
00342 {
00343 log_connection();
00344
00345
00346
00347
00348 strcpy(current_src, sh);
00349 strcpy(current_dst, dh);
00350
00351 have_pending_acks = 0;
00352 have_pending_fins = 0;
00353 have_pending_rsts = 0;
00354 have_pending_othr = 0;
00355
00356 current_synseq = 0;
00357 connection_state = PENDING;
00358 last_state = PENDING;
00359 new_address = 1;
00360
00361
00362 }
00363
00364
00365
00366
00367
00368
00369
00370 if ((rc = parse_dump_record()) < 0)
00371 continue;
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419 switch (connection_state)
00420
00421
00422
00423
00424 {
00425 case PENDING:
00426 {
00427 switch (input_type)
00428 {
00429 case FIN:
00430 {
00431
00432
00433 have_pending_fins++;
00434 break;
00435 }
00436
00437 case SYN:
00438 {
00439
00440
00441 init_connection();
00442 break;
00443 }
00444
00445 case RST:
00446 {
00447
00448
00449 have_pending_rsts++;
00450 break;
00451 }
00452
00453
00454
00455
00456
00457
00458
00459
00460 case ACK_ONLY:
00461 {
00462
00463
00464 if (new_address == 1)
00465 {
00466 new_address = 0;
00467 have_pending_acks++;
00468 }
00469 else
00470 {
00471
00472
00473 if ((new_ack > 2) &&
00474 (new_ack < 16384))
00475 {
00476 log_ACT("REQ");
00477
00478 last_request_end = 1;
00479 current_request_end = new_ack;
00480
00481 last_response_end = 1;
00482 current_response_end = 1;
00483
00484
00485
00486
00487 strcpy(start_request_time, ts);
00488 strcpy(request_end_time, ts);
00489
00490 last_state = connection_state;
00491 connection_state = IN_REQUEST;
00492
00493 init_active();
00494 }
00495 else
00496 have_pending_acks++;
00497 }
00498 break;
00499 }
00500
00501 case DATA_ACK:
00502 {
00503
00504
00505 if ((seq_bytes > 1) &&
00506 (seq_bytes < 65535))
00507 {
00508 log_ACT("RSP");
00509
00510
00511
00512 last_request_end = 1;
00513 current_request_end = 1;
00514
00515 last_response_end = 0;
00516 current_response_end = seq_bytes;
00517
00518
00519
00520
00521
00522 strcpy(start_response_time, ts);
00523 strcpy(response_end_time, ts);
00524
00525 last_state = connection_state;
00526 connection_state = IN_RESPONSE;
00527
00528 init_active();
00529 }
00530 else
00531 have_pending_othr++;
00532 break;
00533 }
00534 default:
00535 break;
00536 }
00537 break;
00538 }
00539
00540 case SYN_SENT:
00541 {
00542
00543
00544
00545
00546
00547
00548 switch (input_type)
00549 {
00550 case FIN:
00551
00552
00553
00554 {
00555 if ((has_ack == 1) &&
00556 (new_ack > (current_request_end + 1)))
00557
00558
00559 {
00560
00561
00562
00563
00564 begin_REQ();
00565 log_REQ();
00566 }
00567
00568
00569
00570
00571
00572
00573 if ((has_seq == 1) &&
00574 (end_seq > current_response_end))
00575 {
00576 begin_RSP();
00577 log_RSP();
00578 }
00579 last_state = SYN_SENT;
00580 connection_state = FIN_SENT;
00581
00582
00583 if (strcmp(FIN_sent_time, "") == 0)
00584 strcpy(FIN_sent_time, ts);
00585 break;
00586 }
00587
00588 case SYN:
00589 {
00590
00591
00592
00593 check_tuple_reuse();
00594 break;
00595 }
00596
00597 case RST:
00598 {
00599 connection_state = RESET;
00600 last_state = SYN_SENT;
00601
00602
00603
00604 if (strcmp(RST_sent_time, "") == 0)
00605 strcpy(RST_sent_time, ts);
00606 break;
00607 }
00608
00609 case ACK_ONLY:
00610 {
00611
00612
00613
00614
00615
00616 if (new_ack > (current_request_end + 1))
00617 begin_REQ();
00618 break;
00619 }
00620
00621 case DATA_ACK:
00622 {
00623
00624
00625
00626
00627
00628 if (new_ack > (current_request_end + 1))
00629 {
00630
00631
00632 begin_REQ();
00633
00634
00635
00636
00637
00638 if (end_seq > current_response_end)
00639 {
00640
00641
00642
00643 log_REQ();
00644 begin_RSP();
00645 }
00646 }
00647 else
00648
00649
00650
00651
00652 if ((end_seq > last_response_end) &&
00653 (seq_bytes > 0))
00654 begin_RSP();
00655 break;
00656 }
00657 default:
00658 break;
00659 }
00660 break;
00661 }
00662
00663 case FIN_SENT:
00664 {
00665 switch (input_type)
00666 {
00667 case SYN:
00668 {
00669
00670
00671
00672 check_tuple_reuse();
00673 break;
00674 }
00675
00676 case FIN:
00677 break;
00678
00679 case RST:
00680 break;
00681
00682 case ACK_ONLY:
00683
00684
00685
00686
00687
00688
00689 break;
00690
00691 case DATA_ACK:
00692 {
00693
00694
00695
00696
00697
00698
00699
00700 if ((end_seq > (current_response_end + 2)) &&
00701 (have_FINdata_error == 0))
00702 {
00703 error_state("new data in FIN_SENT state");
00704 have_FINdata_error = 1;
00705 }
00706 break;
00707 }
00708 default:
00709 break;
00710 }
00711 break;
00712 }
00713
00714 case RESET:
00715 {
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727 switch (input_type)
00728 {
00729 case RST:
00730 break;
00731
00732 case SYN:
00733 {
00734
00735
00736
00737 check_tuple_reuse();
00738 break;
00739 }
00740
00741 case ACK_ONLY:
00742
00743
00744
00745
00746
00747 break;
00748
00749 case FIN:
00750 {
00751
00752
00753
00754
00755
00756 if (last_state == IN_RESPONSE)
00757 {
00758 if ((has_seq == 1) &&
00759 (end_seq > current_response_end))
00760 more_RSP();
00761 log_RSP();
00762
00763 last_state = RESET;
00764 connection_state = FIN_SENT;
00765
00766
00767
00768 if (strcmp(FIN_sent_time, "") == 0)
00769 strcpy(FIN_sent_time, ts);
00770 }
00771 break;
00772 }
00773
00774 case DATA_ACK:
00775 {
00776
00777
00778
00779
00780
00781
00782
00783 if (last_state == IN_RESPONSE)
00784 {
00785 if (new_ack > (last_request_end + 1))
00786 {
00787 log_RSP();
00788 last_state = RESET;
00789 break;
00790 }
00791
00792
00793
00794
00795 if ((end_seq > current_response_end) &&
00796 (seq_bytes > 0))
00797 more_RSP();
00798 }
00799 break;
00800 }
00801 default:
00802 break;
00803 }
00804 break;
00805 }
00806
00807 case IN_RESPONSE:
00808 {
00809
00810
00811
00812
00813
00814 switch (input_type)
00815 {
00816 case FIN:
00817 {
00818
00819
00820
00821
00822
00823
00824
00825 if (has_ack == 1)
00826 {
00827 if ((rc = check_ACK_advance(current_request_end)) < 0)
00828 break;
00829 }
00830
00831
00832
00833
00834
00835
00836 if ((has_ack == 1) &&
00837 (new_ack > (current_request_end + 1)))
00838 {
00839
00840
00841 log_RSP();
00842
00843
00844
00845
00846 begin_REQ();
00847 log_REQ();
00848
00849
00850
00851
00852
00853 if ((has_seq == 1) &&
00854 (end_seq > last_response_end))
00855 {
00856 begin_RSP();
00857 log_RSP();
00858 }
00859
00860
00861
00862
00863
00864
00865
00866 }
00867 else
00868
00869
00870
00871 {
00872 if ((has_seq == 1) &&
00873 (end_seq > current_response_end))
00874 more_RSP();
00875 log_RSP();
00876 }
00877
00878 last_state = IN_RESPONSE;
00879 connection_state = FIN_SENT;
00880
00881 if (strcmp(FIN_sent_time, "") == 0)
00882 strcpy(FIN_sent_time, ts);
00883 break;
00884 }
00885
00886 case RST:
00887
00888
00889
00890
00891
00892
00893 {
00894
00895
00896 last_state = IN_RESPONSE;
00897 connection_state = RESET;
00898
00899 if (strcmp(RST_sent_time, "") == 0)
00900 strcpy(RST_sent_time, ts);
00901 break;
00902 }
00903
00904 case SYN:
00905 {
00906
00907
00908
00909 check_tuple_reuse();
00910 break;
00911 }
00912
00913 case ACK_ONLY:
00914
00915
00916
00917
00918 {
00919
00920
00921
00922
00923
00924
00925 if (new_ack > (last_request_end + 1))
00926 {
00927
00928
00929 log_RSP();
00930
00931
00932
00933
00934
00935
00936 begin_REQ();
00937 }
00938 break;
00939 }
00940
00941 case DATA_ACK:
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952 {
00953 if ((rc = check_ACK_advance(last_request_end)) < 0)
00954 break;
00955
00956
00957
00958
00959
00960
00961 if (new_ack > (last_request_end + 1))
00962 {
00963
00964
00965 log_RSP();
00966 begin_REQ();
00967
00968
00969
00970
00971
00972 if ((end_seq > current_response_end) &&
00973 (seq_bytes > 0))
00974 {
00975 log_REQ();
00976 begin_RSP();
00977 }
00978 }
00979 else
00980
00981
00982
00983 if (end_seq > current_response_end)
00984 more_RSP();
00985 break;
00986 }
00987 default:
00988 break;
00989 }
00990 break;
00991 }
00992
00993 case IN_REQUEST:
00994 {
00995
00996
00997
00998
00999
01000 switch (input_type)
01001 {
01002 case FIN:
01003
01004
01005
01006
01007
01008
01009
01010
01011 {
01012
01013
01014
01015
01016
01017
01018 if ((has_ack == 1) &&
01019 (new_ack > (current_request_end + 1)))
01020 more_REQ();
01021 else
01022 if (has_ack == 1)
01023 {
01024 if ((rc = check_ACK_advance(current_request_end)) < 0)
01025 break;
01026 }
01027
01028 log_REQ();
01029
01030
01031
01032
01033 if ((has_seq == 1) &&
01034 (end_seq > last_response_end))
01035 {
01036 begin_RSP();
01037 log_RSP();
01038 }
01039
01040 last_state = IN_REQUEST;
01041 connection_state = FIN_SENT;
01042
01043 if (strcmp(FIN_sent_time, "") == 0)
01044 strcpy(FIN_sent_time, ts);
01045 break;
01046 }
01047
01048 case RST:
01049 {
01050
01051
01052
01053
01054 log_REQ();
01055 last_state = IN_REQUEST;
01056 connection_state = RESET;
01057
01058 if (strcmp(RST_sent_time, "") == 0)
01059 strcpy(RST_sent_time, ts);
01060 break;
01061 }
01062
01063 case SYN:
01064 {
01065
01066
01067
01068 check_tuple_reuse();
01069 break;
01070 }
01071
01072 case ACK_ONLY:
01073 {
01074
01075
01076
01077
01078
01079
01080 if (new_ack > (current_request_end + 1))
01081 more_REQ();
01082 break;
01083 }
01084
01085 case DATA_ACK:
01086 {
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097 if (new_ack > (current_request_end + 1))
01098 more_REQ();
01099 else
01100 if ((rc = check_ACK_advance(current_request_end)) < 0)
01101 break;
01102
01103
01104
01105
01106
01107 if ((end_seq > last_response_end) &&
01108 (seq_bytes > 0))
01109 {
01110
01111
01112
01113 log_REQ();
01114 begin_RSP();
01115 }
01116 break;
01117 }
01118 default:
01119 break;
01120 }
01121 break;
01122 }
01123
01124 default:
01125 break;
01126 }
01127
01128
01129 strcpy(last_connection_time, ts);
01130
01131 }
01132 log_log();
01133 close (dumpFP);
01134 close (outFP);
01135 close (logFP);
01136 }
01137
01138
01139
01140
01141
01142 void init_connection(void)
01143 {
01144 log_SYN();
01145 connection_state = SYN_SENT;
01146
01147
01148 if (has_seq == 1)
01149 current_synseq = begin_seq;
01150 else
01151 error_line ("SYN without valid sequence #");
01152
01153
01154
01155
01156
01157 last_request_end = 1;
01158 last_response_end = 1;
01159 current_response_end = 1;
01160 current_request_end = 1;
01161
01162 strcpy(FIN_sent_time, "");
01163 strcpy(RST_sent_time, "");
01164 strcpy(last_connection_time, "");
01165
01166 have_ACK_error = 0;
01167 have_value_error = 0;
01168 have_FINdata_error = 0;
01169 }
01170
01171
01172
01173
01174
01175
01176 void init_active(void)
01177 {
01178 strcpy(FIN_sent_time, "");
01179 strcpy(RST_sent_time, "");
01180 strcpy(last_connection_time, "");
01181
01182 have_ACK_error = 0;
01183 have_value_error = 0;
01184 have_FINdata_error = 0;
01185
01186 }
01187
01188
01189
01190
01191
01192 int check_ACK_advance(unsigned long old_ack)
01193 {
01194
01195 if ((new_ack < old_ack) &&
01196 report_ACK_err)
01197 {
01198 if (have_ACK_error == 0)
01199 {
01200 error_state("ACK error -- backward");
01201 have_ACK_error = 1;
01202 }
01203 return(-1);
01204 }
01205 else
01206 return (0);
01207 }
01208
01209
01210
01211 void check_tuple_reuse(void)
01212 {
01213
01214
01215 if ((has_seq == 1) &&
01216 (current_synseq != begin_seq))
01217 {
01218
01219
01220
01221
01222 if ((connection_state == SYN_SENT) ||
01223 ((connection_state == RESET) && (last_state == SYN_SENT)))
01224 {
01225 log_END("TRM");
01226 init_connection();
01227 return;
01228 }
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242 if (connection_state == FIN_SENT)
01243 elapsed = 60001;
01244 else
01245 elapsed = elapsed_ms(ts, last_connection_time);
01246
01247 if (elapsed < 60000)
01248 error_state("Non-duplicate SYN in connection");
01249 else
01250 {
01251
01252
01253
01254 switch (connection_state)
01255 {
01256 case FIN_SENT:
01257 log_END("FIN");
01258 break;
01259 case RESET:
01260 if (last_state == IN_RESPONSE)
01261 log_RSP();
01262
01263 log_END("RST");
01264 break;
01265 case IN_RESPONSE:
01266 log_RSP();
01267 log_END("TRM");
01268 break;
01269 case IN_REQUEST:
01270 log_REQ();
01271 log_END("TRM");
01272 break;
01273 default:
01274 break;
01275 }
01276 init_connection();
01277 }
01278 }
01279 }
01280
01281
01282
01283 void begin_REQ(void)
01284 {
01285 current_request_end = new_ack;
01286
01287
01288
01289
01290 strcpy(start_request_time, ts);
01291 strcpy(request_end_time, ts);
01292
01293 last_state = connection_state;
01294 connection_state = IN_REQUEST;
01295 }
01296
01297
01298
01299 void more_REQ(void)
01300 {
01301 current_request_end = new_ack;
01302 strcpy(request_end_time, ts);
01303 }
01304
01305
01306
01307 void begin_RSP(void)
01308 {
01309 current_response_end = end_seq;
01310
01311
01312
01313
01314
01315 strcpy(start_response_time, ts);
01316 strcpy(response_end_time, ts);
01317
01318 last_state = connection_state;
01319 connection_state = IN_RESPONSE;
01320 }
01321
01322
01323
01324 void more_RSP(void)
01325 {
01326 current_response_end = end_seq;
01327
01328 strcpy(response_end_time, ts);
01329 }
01330
01331
01332
01333
01334
01335
01336
01337
01338 int parse_dump_record()
01339 {
01340 begin_seq = end_seq = seq_bytes = new_ack = 0;
01341 has_ack = has_seq = 0;
01342
01343
01344
01345
01346 if ((strcmp(fl, "SFRP") == 0) ||
01347 (strcmp(fl, "SFR") == 0) ||
01348 (strcmp(fl, "SFP") == 0) ||
01349 (strcmp(fl, "SF") == 0) ||
01350 (strcmp(fl, "SRP") == 0) ||
01351 (strcmp(fl, "SR") == 0))
01352 {
01353
01354
01355
01356 if (connection_state != PENDING)
01357 error_line ("SYN in combination with F or R");
01358 return(-1);
01359 }
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370 if (strcmp(p1, "ack") == 0)
01371 {
01372 has_seq = 0;
01373 has_ack = 1;
01374 new_ack = strtoul(p2, (char **)NULL, 10);
01375 }
01376 else
01377 {
01378 if (strcmp(p1, "win") == 0)
01379 {
01380 has_ack = 0;
01381 has_seq = 0;
01382 }
01383 else
01384 {
01385
01386
01387
01388
01389
01390 if ((rc = get_sequence(p1, &begin_seq, &end_seq, &seq_bytes)) < 0)
01391 {
01392 error_line ("invalid sequence # field");
01393 return (-1);
01394 }
01395 has_seq = 1;
01396
01397
01398
01399 if (strcmp(p2, "ack") == 0)
01400 {
01401 has_ack = 1;
01402 new_ack = strtoul(p3, (char **)NULL, 10);
01403 }
01404 else
01405 has_ack = 0;
01406 }
01407 }
01408
01409
01410
01411 if ((strcmp(fl, "F") == 0) ||
01412 (strcmp(fl, "FP") == 0) ||
01413 (strcmp(fl, "FR") == 0) ||
01414 (strcmp(fl, "FRP") == 0))
01415 input_type = FIN;
01416 else
01417 {
01418 if ((strcmp(fl, "R") == 0) ||
01419 (strcmp(fl, "RP") == 0))
01420 input_type = RST;
01421 else
01422 {
01423 if ((strcmp(fl, "S") == 0) ||
01424 (strcmp(fl, "SP") == 0))
01425 input_type = SYN;
01426 else
01427 {
01428 if ((has_ack == 1) &&
01429 (has_seq == 0))
01430 input_type = ACK_ONLY;
01431 else
01432 if ((has_seq == 1) &&
01433 (has_ack == 1))
01434 input_type = DATA_ACK;
01435 else
01436 {
01437 error_line("Unexpected Data/ACK combination");
01438 return (-1);
01439 }
01440 }
01441 }
01442 }
01443
01444
01445 if ((connection_state == IN_RESPONSE) ||
01446 (connection_state == IN_REQUEST) ||
01447 (connection_state == SYN_SENT) ||
01448 ((connection_state == RESET) && (last_state == IN_RESPONSE)))
01449 {
01450
01451
01452 if (((input_type == FIN) ||
01453 (input_type == DATA_ACK)) &&
01454 (end_seq > (current_response_end + 65535)))
01455 {
01456 if (have_value_error == 0)
01457 {
01458 error_line ("suspect sequence # value");
01459 have_value_error = 1;
01460 }
01461 return (-1);
01462 }
01463
01464
01465
01466 if (((input_type == FIN) ||
01467 (input_type == ACK_ONLY) ||
01468 (input_type == DATA_ACK)) &&
01469 (new_ack > (current_request_end + 16384)))
01470 {
01471 if (have_value_error == 0)
01472 {
01473 error_line ("suspect ACK value");
01474 have_value_error = 1;
01475 }
01476 return (-1);
01477 }
01478 }
01479 return(0);
01480 }
01481
01482
01483
01484 void log_connection(void)
01485 {
01486
01487
01488
01489 if (connection_state == IN_REQUEST)
01490 log_REQ();
01491 else
01492 {
01493
01494
01495
01496 if ((connection_state == IN_RESPONSE) ||
01497 ((connection_state == RESET) && last_state == IN_RESPONSE))
01498 {
01499 if (current_response_end > (last_response_end + 1))
01500 log_RSP();
01501 }
01502 }
01503
01504
01505
01506
01507
01508 if (connection_state != PENDING)
01509 {
01510 if (connection_state == FIN_SENT)
01511 log_END("FIN");
01512 else
01513 {
01514 if (connection_state == RESET)
01515 log_END("RST");
01516 else
01517 log_END("TRM");
01518 }
01519 }
01520 else
01521 {
01522 if (((have_pending_fins > 0) +
01523 (have_pending_rsts > 0) +
01524 (have_pending_othr > 0) +
01525 (have_pending_acks > 0)) > 1)
01526 pending_cmb_count++;
01527 else
01528 {
01529 pending_fin_count += (have_pending_fins > 0);
01530 pending_rst_count += (have_pending_rsts > 0);
01531 pending_ack_count += (have_pending_acks > 0);
01532 pending_oth_count += (have_pending_othr > 0);
01533 }
01534 }
01535 }
01536
01537
01538 void log_log(void)
01539 {
01540 fprintf(logFP, "Input tcpdump file: %s \n", input_name);
01541 fprintf(logFP, "Output connection file: %s \n", output_name);
01542 fprintf(logFP, " SYNs %8d \n", syn_count);
01543 fprintf(logFP, " REQs %8d \n", req_count);
01544 fprintf(logFP, " ACT-REQs %8d \n", act_req_count);
01545 fprintf(logFP, " RSPs %8d \n", rsp_count);
01546 fprintf(logFP, " ACT-RSPs %8d \n", act_rsp_count);
01547 fprintf(logFP, " FINs %8d \n", fin_count);
01548 fprintf(logFP, " RSTs %8d \n", rst_count);
01549 fprintf(logFP, " TRMs %8d \n", trm_count);
01550 fprintf(logFP, " ERRs %8d \n", err_count);
01551 fprintf(logFP, "Partial Connections:\n");
01552 fprintf(logFP, " FIN only %8d \n", pending_fin_count);
01553 fprintf(logFP, " RST only %8d \n", pending_rst_count);
01554 fprintf(logFP, " ACK only %8d \n", pending_ack_count);
01555 fprintf(logFP, " Combos %8d \n", pending_cmb_count);
01556 fprintf(logFP, " Other %8d \n", pending_oth_count);
01557 }
01558
01559
01560
01561
01562
01563
01564
01565
01566 void log_REQ(void)
01567 {
01568
01569 get_host_port(current_src, src_host, src_port);
01570
01571
01572 get_host_port(current_dst, dst_host, dst_port);
01573
01574
01575
01576
01577
01578
01579 fprintf(outFP, "%s %-15s %5s > %-15s %4s: REQ %12d %s\n",
01580 start_request_time,
01581 dst_host, dst_port, src_host, src_port,
01582 current_request_end - last_request_end,
01583 request_end_time);
01584
01585 last_request_end = current_request_end;
01586 req_count++;
01587 }
01588
01589 void log_RSP(void)
01590 {
01591
01592 get_host_port(current_src, src_host, src_port);
01593
01594
01595 get_host_port(current_dst, dst_host, dst_port);
01596
01597
01598
01599
01600
01601
01602
01603 fprintf(outFP, "%s %-15s %5s > %-15s %4s: RSP %12d %s\n",
01604 response_end_time,
01605 dst_host, dst_port, src_host, src_port,
01606 current_response_end - last_response_end,
01607 start_response_time);
01608 #ifdef FOO
01609 fprintf(outFP, "%s %-15s %5s > %-15s %4s RSP %d %s\n", start_response_time,
01610 src_host, src_port, dst_host, dst_port,
01611 current_response_end - last_response_end,
01612 response_end_time);
01613 fprintf(outFP, "%s %s > %s RSP %d\n", start_response_time, current_src,
01614 current_dst,
01615 current_response_end - last_response_end);
01616 #endif
01617
01618 last_response_end = current_response_end;
01619 rsp_count++;
01620 }
01621
01622 void log_SYN(void)
01623 {
01624
01625 get_host_port(current_src, src_host, src_port);
01626
01627
01628 get_host_port(current_dst, dst_host, dst_port);
01629
01630 fprintf(outFP, "%s %-15s %5s > %-15s %4s: SYN\n", ts,
01631 dst_host, dst_port, src_host, src_port);
01632 syn_count++;
01633 }
01634
01635 void log_END(char *how)
01636 {
01637 char logical_end_time[20];
01638
01639
01640 get_host_port(current_src, src_host, src_port);
01641
01642
01643 get_host_port(current_dst, dst_host, dst_port);
01644
01645 if (strcmp(how, "FIN") == 0)
01646 {
01647 fin_count++;
01648 strcpy(logical_end_time, FIN_sent_time);
01649 }
01650 else
01651 {
01652 if (strcmp(how, "RST") == 0)
01653 {
01654 rst_count++;
01655 strcpy(logical_end_time, RST_sent_time);
01656 }
01657 else
01658 if (strcmp(how, "TRM") == 0)
01659 {
01660 trm_count++;
01661 strcpy(logical_end_time, last_connection_time);
01662 }
01663 }
01664
01665
01666
01667
01668
01669
01670 fprintf(outFP, "%s %-15s %5s > %-15s %4s: %s %s\n",
01671 last_connection_time,
01672 dst_host, dst_port, src_host, src_port,
01673 how, logical_end_time);
01674 }
01675
01676 void log_ACT(char *how)
01677 {
01678
01679 get_host_port(current_src, src_host, src_port);
01680
01681
01682 get_host_port(current_dst, dst_host, dst_port);
01683
01684
01685
01686
01687
01688
01689 fprintf(outFP, "%s %-15s %5s > %-15s %4s: ACT-%s\n", ts,
01690 dst_host, dst_port, src_host, src_port,
01691 how);
01692 if (strcmp(how, "REQ") == 0)
01693 act_req_count++;
01694 else
01695 if (strcmp(how, "RSP") == 0)
01696 act_rsp_count++;
01697 }
01698
01699 void error_line(char * s)
01700 {
01701
01702 get_host_port(sh, src_host, src_port);
01703
01704
01705 get_host_port(dh, dst_host, dst_port);
01706
01707 fprintf(outFP, "%s %-15s %5s > %-15s %4s: ERR: %s\n", ts,
01708 dst_host, dst_port, src_host, src_port, s);
01709 err_count++;
01710 }
01711
01712 void error_state(char * s)
01713 {
01714
01715 get_host_port(sh, src_host, src_port);
01716
01717
01718 get_host_port(dh, dst_host, dst_port);
01719
01720 fprintf(outFP, "%s %-15s %5s > %-15s %4s: ERR: %s\n", ts,
01721 dst_host, dst_port, src_host, src_port, s);
01722 err_count++;
01723 }
01724
01725 void get_host_port(char *adr, char *host, char *port)
01726 {
01727 char *fp;
01728 char *fpx;
01729 char adr_field[50];
01730
01731 strcpy(adr_field, adr);
01732
01733 fp = (char *)rindex(adr_field, '.');
01734 *fp = '\0';
01735 strcpy(host, adr_field);
01736
01737 fp++;
01738 fpx = (char *)index(fp, ':');
01739 if (fpx != NULL)
01740 *fpx = '\0';
01741 strcpy(port, fp);
01742 }
01743
01744 int get_sequence(char *p, unsigned long *begin, unsigned long *end,
01745 unsigned long *bytes)
01746 {
01747 char seq_field[50];
01748 char *cursor = seq_field;
01749 char *fp;
01750
01751 strcpy (seq_field, p);
01752
01753 fp = (char *)strsep(&cursor, ":" );
01754 if ((cursor == (char *)NULL) ||
01755 (fp == (char *)NULL))
01756 return (-1);
01757 else
01758 *begin = strtoul(fp, (char **)NULL, 10);
01759
01760 fp = (char *)strsep(&cursor, "(" );
01761 if ((cursor == (char *)NULL) ||
01762 (fp == (char *)NULL))
01763 return (-1);
01764 else
01765 *end = strtoul(fp, (char **)NULL, 10);
01766
01767 fp = (char *)strsep(&cursor, ")" );
01768 if ((cursor == (char *)NULL) ||
01769 (fp == (char *)NULL))
01770 return (-1);
01771 else
01772 *bytes = strtoul(fp, (char **)NULL, 10);
01773 return(0);
01774 }
01775
01776
01777
01778
01779
01780 static void
01781 tvsub(tdiff, t1, t0)
01782 struct timeval *tdiff, *t1, *t0;
01783 {
01784
01785 tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
01786 tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
01787 if (tdiff->tv_usec < 0)
01788 {
01789 tdiff->tv_sec--;
01790 tdiff->tv_usec += 1000000;
01791 }
01792 }
01793
01794
01795
01796
01797
01798 long elapsed_ms(char *end, char *start)
01799 {
01800 struct timeval delta, end_time, start_time;
01801 long elapsed_time;
01802
01803 char end_tmp[20];
01804 char start_tmp[20];
01805
01806 char *cursor;
01807 char *cp;
01808
01809 strcpy(end_tmp, end);
01810 cursor = end_tmp;
01811 cp = (char *)strsep(&cursor, "." );
01812 end_time.tv_sec = atoi(end_tmp);
01813 end_time.tv_usec = atoi(cursor);
01814
01815 strcpy(start_tmp, start);
01816 cursor = start_tmp;
01817 cp = (char *)strsep(&cursor, "." );
01818 start_time.tv_sec = atoi(start_tmp);
01819 start_time.tv_usec = atoi(cursor);
01820
01821 tvsub(&delta, &end_time, &start_time);
01822
01823 elapsed_time = (delta.tv_sec * 1000) + (delta.tv_usec/1000);
01824 return (elapsed_time);
01825 }
01826