00001
00002
00003
00004
00005 #include "packets.h"
00006 #include "modem.h"
00007 #include "flock.h"
00008 #include "dstring.h"
00009 #include <unistd.h>
00010 #include <time.h>
00011
00012
00013 static char* dater();
00014 static void get_remote_address (struct sockaddr_in *sender, char *ip, int *port);
00015
00016
00017
00018
00019 static int order[256] = {1, 3, 15, 6, 0, 2, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255};
00020
00021
00022
00023
00024
00025 extern int dump_sync_fd;
00026
00027
00028
00029
00030
00031
00032
00033 static int debug = 0;
00034
00035
00036
00037
00038 static char default_log_file[] = "\0";
00039
00040
00041
00042
00043 static char *log_file = default_log_file;
00044
00045
00046
00047
00048
00049 static int my_fake_syslog (const char *file, const char * fmt,...) { return 0; }
00050
00051
00052
00053
00054
00055 static int (*my_syslog) (const char *file, const char * fmt,...) = my_fake_syslog;
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 void packet_logging_service (int dbg,
00070 char *logfile,
00071 int (*logger) (const char *file, const char * fmt,...))
00072 {
00073 if ((logger != NULL) && (logfile != NULL))
00074 { if (logfile[0] != 0) { my_syslog = logger; log_file = logfile; } }
00075
00076 debug = dbg;
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 static char chaddr[DHCP_P_STR_HADDR_SIZE];
00095
00096
00097
00098
00099 static char chaddr_exact[DHCP_P_STR_HADDR_SIZE_EXACT];
00100
00101
00102
00103
00104 static char sname[DHCP_P_STR_SNAME_SIZE];
00105
00106
00107
00108
00109 static char file[DHCP_P_STR_FILE_SIZE];
00110
00111
00112
00113
00114 static char myhostname[256];
00115
00116
00117
00118
00119
00120 void packet_init_dump_service ()
00121 {
00122 memset ((void*)myhostname, 0, 256);
00123 gethostname (myhostname, 255);
00124 }
00125
00126
00127
00128
00129 static char* descriptions[256] =
00130 {
00131 "Pad",
00132 "Subnet Mask",
00133 "Time Offset",
00134 "Router",
00135 "Time Server",
00136 "Name Server",
00137 "Domain Name Server",
00138 "Log Server",
00139 "Cookie Server",
00140 "LPR Server",
00141 "Impress Server",
00142 "Resource Location Server",
00143 "Host Name",
00144 "Boot File Size",
00145 "Merit Dump File",
00146 "Domain Name",
00147 "Swap Server",
00148 "Root Path",
00149 "Extensions Path",
00150 "IP Forwarding Enable/Disable",
00151 "Non-Local Source Routing Enable/Disable",
00152 "Policy Filter",
00153 "Maximum Datagram Reassembly Size",
00154 "Default IP Time-to-live",
00155 "Path MTU Aging Timeout",
00156 "Path MTU Plateau Table",
00157 "Interface MTU",
00158 "All Subnets are Local",
00159 "Broadcast Address",
00160 "Perform Mask Discovery",
00161 "Mask Supplier",
00162 "Perform Router Discovery",
00163 "Router Solicitation Address",
00164 "Static Route",
00165 "Trailer Encapsulation",
00166 "ARP Cache Timeout",
00167 "Ethernet Encapsulation",
00168 "TCP Default TTL",
00169 "TCP Keepalive Interval",
00170 "TCP Keepalive Garbage",
00171 "Network Information Service Domain",
00172 "Network Information Servers",
00173 "Network Time Protocol Servers",
00174 "Vendor Specific Information",
00175 "NetBIOS over TCP/IP Name Server",
00176 "NetBIOS over TCP/IP Datagram Distribution Server",
00177 "NetBIOS over TCP/IP Node Type",
00178 "NetBIOS over TCP/IP Scope",
00179 "X Window System Font Server",
00180 "X Window System Display Manager",
00181 "Requested IP Address",
00182 "IP Address Lease Time",
00183 "Option Overload",
00184 "DHCP Message Type",
00185 "Server Identifier",
00186 "Parameter Request List",
00187 "Message",
00188 "Maximum DHCP Message Size",
00189 "Renewal (T1) Time Value",
00190 "Rebinding (T2) Time Value",
00191 "Vendor class identifier",
00192 "Client-identifier",
00193 "TAG_NDS_IPDOMAIN",
00194 "TAG_NDS_IPINFO",
00195 "Network Information Service+ Domain",
00196 "Network Information Service+ Servers",
00197 "TFTP server name",
00198 "Bootfile name",
00199 "Mobile IP Home Agent",
00200 "Simple Mail Transport Protocol (SMTP) Server",
00201 "Post Office Protocol (POP3) Server",
00202 "Network News Transport Protocol (NNTP) Server",
00203 "Default World Wide Web (WWW) Server",
00204 "Default Finger Server",
00205 "Default Internet Relay Chat (IRC) Server",
00206 "StreetTalk Server",
00207 "StreetTalk Directory Assistance (STDA) Server",
00208 "TAG_USER_CLASS",
00209 "TAG_SLP_DA",
00210 "TAG_SLP_SCOPE",
00211 "TAG_SLP_NAMING_AUTH",
00212 "TAG_CLIENT_FQDN",
00213 "Agent Circuit ID",
00214 "TAG_AGENT_REMOTE",
00215 "TAG_AGENT_MASK",
00216 "TAG_NDS_SERVERS",
00217 "TAG_NDS_TREE_NAME",
00218 "TAG_NDS_CONTEXT",
00219 "TAG_TZ_STRING",
00220 "TAG_FQDN_OPTION",
00221 "TAG_AUTH",
00222 "TAG_VINES_SERVERS",
00223 "TAG_SERVER_RANK",
00224 "TAG_CLIENT_ARCH",
00225 "TAG_CLIENT_NDI",
00226 "TAG_LDAP_URL",
00227 "TAG_6OVER4",
00228 "TAG_CLIENT_GUID",
00229 "TAG_OPEN_GROUP_UAP",
00230 "Unknown option",
00231 "Printer Name",
00232 "TAG_MDHCP_SERVER",
00233 "Unknown option",
00234 "Unknown option",
00235 "Unknown option",
00236 "Unknown option",
00237 "Unknown option",
00238 "Unknown option",
00239 "Unknown option",
00240 "Unknown option",
00241 "TAG_IPX_COMPAT",
00242 "Unknown option",
00243 "TAG_NETINFO_PARENT",
00244 "TAG_NETINFO_PARENT_TAG",
00245 "TAG_URL",
00246 "TAG_FAILOVER",
00247 "TAG_DISABLE_AUTOCONF",
00248 "TAG_NS_SEARCH",
00249 "TAG_IP",
00250 "Unknown option",
00251 "Unknown option",
00252 "Unknown option",
00253 "Unknown option",
00254 "Unknown option",
00255 "Unknown option",
00256 "Unknown option",
00257 "TAG_EXTENDED_REQUEST",
00258 "TAG_EXTENDED_OPTION",
00259 "Unknown option",
00260 "Unknown option",
00261 "Unknown option",
00262 "Unknown option",
00263 "Unknown option",
00264 "Unknown option",
00265 "Unknown option",
00266 "Unknown option",
00267 "Unknown option",
00268 "Unknown option",
00269 "Unknown option",
00270 "Unknown option",
00271 "Unknown option",
00272 "Unknown option",
00273 "Unknown option",
00274 "Unknown option",
00275 "Unknown option",
00276 "Unknown option",
00277 "Unknown option",
00278 "Unknown option",
00279 "Unknown option",
00280 "Unknown option",
00281 "Unknown option",
00282 "Unknown option",
00283 "Unknown option",
00284 "Unknown option",
00285 "Unknown option",
00286 "Unknown option",
00287 "Unknown option",
00288 "Unknown option",
00289 "Unknown option",
00290 "Unknown option",
00291 "Unknown option",
00292 "Unknown option",
00293 "Unknown option",
00294 "Unknown option",
00295 "Unknown option",
00296 "Unknown option",
00297 "Unknown option",
00298 "Unknown option",
00299 "Unknown option",
00300 "Unknown option",
00301 "Unknown option",
00302 "Unknown option",
00303 "Unknown option",
00304 "Unknown option",
00305 "Unknown option",
00306 "Unknown option",
00307 "Unknown option",
00308 "Unknown option",
00309 "Unknown option",
00310 "Unknown option",
00311 "Unknown option",
00312 "Unknown option",
00313 "Unknown option",
00314 "Unknown option",
00315 "Unknown option",
00316 "Unknown option",
00317 "Unknown option",
00318 "Unknown option",
00319 "Unknown option",
00320 "Unknown option",
00321 "Unknown option",
00322 "Unknown option",
00323 "Unknown option",
00324 "Unknown option",
00325 "Unknown option",
00326 "Unknown option",
00327 "Unknown option",
00328 "Unknown option",
00329 "Unknown option",
00330 "Unknown option",
00331 "Unknown option",
00332 "Unknown option",
00333 "Unknown option",
00334 "Unknown option",
00335 "Unknown option",
00336 "Unknown option",
00337 "Unknown option",
00338 "Unknown option",
00339 "Unknown option",
00340 "Unknown option",
00341 "Unknown option",
00342 "Unknown option",
00343 "Unknown option",
00344 "Unknown option",
00345 "Unknown option",
00346 "Unknown option",
00347 "Unknown option",
00348 "Unknown option",
00349 "Unknown option",
00350 "Unknown option",
00351 "Unknown option",
00352 "Unknown option",
00353 "Unknown option",
00354 "Unknown option",
00355 "Unknown option",
00356 "Unknown option",
00357 "Unknown option",
00358 "Unknown option",
00359 "Unknown option",
00360 "Unknown option",
00361 "Unknown option",
00362 "Unknown option",
00363 "Unknown option",
00364 "Unknown option",
00365 "Unknown option",
00366 "Unknown option",
00367 "Unknown option",
00368 "Unknown option",
00369 "Unknown option",
00370 "Unknown option",
00371 "Unknown option",
00372 "Unknown option",
00373 "Unknown option",
00374 "Unknown option",
00375 "Unknown option",
00376 "Unknown option",
00377 "Unknown option",
00378 "Unknown option",
00379 "Unknown option",
00380 "Unknown option",
00381 "Unknown option",
00382 "Unknown option",
00383 "Unknown option",
00384 "Unknown option",
00385 "Unknown option",
00386 "End",
00387 };
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405 char* packets_type_to_char (int type)
00406 {
00407 static char DHCPDISCOVER_S[] = "DHCPDISCOVER";
00408 static char DHCPOFFER_S[] = "DHCPOFFER";
00409 static char DHCPREQUEST_S[] = "DHCPREQUEST";
00410 static char DHCPDECLINE_S[] = "DHCPDECLINE";
00411 static char DHCPACK_S[] = "DHCPACK";
00412 static char DHCPNAK_S[] = "DHCPNAK";
00413 static char DHCPRELEASE_S[] = "DHCPRELEASE";
00414 static char DHCPINFORM_S[] = "DHCPINFORM";
00415 static char DEFAULT_S[] = "Unknown type";
00416
00417 switch (type)
00418 {
00419 case DHCPDISCOVER: return DHCPDISCOVER_S;
00420 case DHCPOFFER: return DHCPOFFER_S;
00421 case DHCPREQUEST: return DHCPREQUEST_S;
00422 case DHCPDECLINE: return DHCPDECLINE_S;
00423 case DHCPACK: return DHCPACK_S;
00424 case DHCPNAK: return DHCPNAK_S;
00425 case DHCPRELEASE: return DHCPRELEASE_S;
00426 case DHCPINFORM: return DHCPINFORM_S;
00427 default: return DEFAULT_S;
00428 }
00429
00430 return DEFAULT_S;
00431 }
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446 void packets_init_option_writer (struct dhcp_packet *p)
00447 {
00448 (p->ctrl).opt_pointer = p->options;
00449
00450 *((p->ctrl).opt_pointer) = 99; (p->ctrl).opt_pointer += 1;
00451 *((p->ctrl).opt_pointer) = 130; (p->ctrl).opt_pointer += 1;
00452 *((p->ctrl).opt_pointer) = 83; (p->ctrl).opt_pointer += 1;
00453 *((p->ctrl).opt_pointer) = 99; (p->ctrl).opt_pointer += 1;
00454
00455
00456
00457 (p->ctrl).taille = 236+4;
00458 }
00459
00460
00461
00462
00463
00464
00465
00466
00467 void packets_init_option_reader (struct dhcp_packet *p)
00468 {
00469 int i;
00470
00471
00472
00473 (p->ctrl).opt_pointer_reader = p->options + 4;
00474
00475 for (i=0; i<256; i++)
00476 {
00477 ((p->ctrl).list[i]).code = 0;
00478 ((p->ctrl).list[i]).len = 0;
00479 memset ((void*)((p->ctrl).list[i]).value, 0, VALUE_MAX_SIZE);
00480 }
00481
00482 memset ((void*)(&((p->ctrl).dslam_data)), 0, sizeof(struct dhcp82));
00483 }
00484
00485
00486
00487
00488
00489
00490 void packets_clear (struct dhcp_packet *p)
00491 {
00492 memset ((void*)p, 0, PACKET_SIZE);
00493 packets_init_option_writer (p);
00494 packets_init_option_reader (p);
00495 }
00496
00497
00498
00499
00500
00501
00502 void packets_options_clear (struct dhcp_packet *p) { memset ((void*)(p->options), 0, OPTION_SIZE); }
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518 void packets_set_op (struct dhcp_packet *p, int op) { p->op = ((unsigned char)op) & 0x000000FF; }
00519
00520
00521
00522
00523
00524
00525
00526 unsigned char packets_get_op (struct dhcp_packet *p) { return p->op; }
00527
00528
00529
00530
00531
00532
00533
00534 void packets_set_htype (struct dhcp_packet *p, int htype) { p->htype = ((unsigned char)htype) & 0x000000FF; }
00535
00536
00537
00538
00539
00540
00541
00542 unsigned char packets_get_htype (struct dhcp_packet *p) { return p->htype; }
00543
00544
00545
00546
00547
00548
00549
00550 void packets_set_hlen (struct dhcp_packet *p, int hlen) { p->hlen = ((unsigned char)hlen) & 0x000000FF; }
00551
00552
00553
00554
00555
00556
00557
00558 unsigned char packets_get_hlen (struct dhcp_packet *p) { return p->hlen; }
00559
00560
00561
00562
00563
00564
00565
00566 void packets_set_hops (struct dhcp_packet *p, int hops) { p->hops = ((unsigned char)hops) & 0x000000FF; }
00567
00568
00569
00570
00571
00572
00573
00574 unsigned char packets_get_hops (struct dhcp_packet *p) { return p->hops; }
00575
00576
00577
00578
00579
00580
00581
00582 void packets_set_xid (struct dhcp_packet *p, unsigned long int xid) { p->xid = htonl(xid); }
00583
00584
00585
00586
00587
00588
00589
00590 unsigned long int packets_get_xid (struct dhcp_packet *p) { return ntohl(p->xid); }
00591
00592
00593
00594
00595
00596
00597
00598 void packets_set_secs (struct dhcp_packet *p, unsigned short int secs) { p->secs = htons(secs); }
00599
00600
00601
00602
00603
00604
00605
00606 unsigned short int packets_get_secs (struct dhcp_packet *p) { return ntohs (p->secs); }
00607
00608
00609
00610
00611
00612
00613
00614 void packets_set_flags (struct dhcp_packet *p, unsigned short int flags) { p->flags = htons(flags); }
00615
00616
00617
00618
00619
00620
00621
00622 unsigned short int packets_get_flags (struct dhcp_packet *p) { return htons(p->flags); }
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632 int packets_set_ciaddr (struct dhcp_packet *p, char *ciaddr)
00633 {
00634 struct in_addr inp;
00635 int rc;
00636
00637 rc = inet_aton (ciaddr, &inp);
00638 if (rc == 0) { return 1; }
00639 p->ciaddr = inp.s_addr;
00640 return 0;
00641 }
00642
00643
00644
00645
00646
00647
00648
00649 void packets_set_bin_ciaddr (struct dhcp_packet *p, unsigned long int ciaddr)
00650 { p->ciaddr = ciaddr; }
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661 char* packets_get_ciaddr (struct dhcp_packet *p)
00662 {
00663 struct in_addr inp;
00664 static char ip[16];
00665
00666 inp.s_addr = p->ciaddr;
00667 memset ((void*)ip, 0, 16);
00668 strncpy (ip, inet_ntoa(inp), 15);
00669 return ip;
00670 }
00671
00672
00673
00674
00675
00676
00677
00678 unsigned long int packets_get_bin_ciaddr (struct dhcp_packet *p)
00679 { return p->ciaddr; }
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689 int packets_set_yiaddr (struct dhcp_packet *p, char *yiaddr)
00690 {
00691 struct in_addr inp;
00692 int rc;
00693
00694 rc = inet_aton (yiaddr, &inp);
00695 if (rc == 0) { return 1; }
00696 p->yiaddr = inp.s_addr;
00697 return 0;
00698 }
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708 char* packets_get_yiaddr (struct dhcp_packet *p)
00709 {
00710 struct in_addr inp;
00711 static char ip[16];
00712
00713 inp.s_addr = p->yiaddr;
00714 memset ((void*)ip, 0, 16);
00715 strncpy (ip, inet_ntoa(inp), 15);
00716 return ip;
00717 }
00718
00719
00720
00721
00722
00723
00724
00725
00726 int packets_set_siaddr (struct dhcp_packet *p, char *siaddr)
00727 {
00728 struct in_addr inp;
00729 int rc;
00730
00731 rc = inet_aton (siaddr, &inp);
00732 if (rc == 0) { return 1; }
00733 p->siaddr = inp.s_addr;
00734 return 0;
00735 }
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745 char* packets_get_siaddr (struct dhcp_packet *p)
00746 {
00747 struct in_addr inp;
00748 static char ip[16];
00749
00750 inp.s_addr = p->siaddr;
00751 memset ((void*)ip, 0, 16);
00752 strncpy (ip, inet_ntoa(inp), 15);
00753 return ip;
00754 }
00755
00756
00757
00758
00759
00760
00761
00762
00763 int packets_set_giaddr (struct dhcp_packet *p, char *giaddr)
00764 {
00765 struct in_addr inp;
00766 int rc;
00767
00768 rc = inet_aton (giaddr, &inp);
00769 if (rc == 0) { return 1; }
00770 p->giaddr = inp.s_addr;
00771 return 0;
00772 }
00773
00774
00775
00776
00777
00778
00779
00780 void packets_set_bin_giaddr (struct dhcp_packet *p, unsigned long int giaddr)
00781 { p->giaddr = giaddr; }
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791 char* packets_get_giaddr (struct dhcp_packet *p)
00792 {
00793 struct in_addr inp;
00794 static char ip[16];
00795
00796 inp.s_addr = p->giaddr;
00797 memset ((void*)ip, 0, 16);
00798 strncpy (ip, inet_ntoa(inp), 15);
00799 return ip;
00800 }
00801
00802
00803
00804
00805
00806
00807
00808 unsigned long int packets_get_bin_giaddr (struct dhcp_packet *p)
00809 { return p->giaddr; }
00810
00811
00812
00813
00814
00815
00816
00817
00818 void packets_set_chaddr (struct dhcp_packet *p, char *chaddr) { memcpy ((void*)p->chaddr, (void*)chaddr, 16); }
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829 char* packets_get_chaddr (struct dhcp_packet *p)
00830 {
00831 int len, i, pp;
00832 char *c;
00833
00834 len = ((int)packets_get_hlen(p)) & 0x000000FF;
00835 c = p->chaddr;
00836 pp = 0;
00837
00838 if (len > 16) { len = 16; }
00839
00840 memset ((void*)chaddr, 0, DHCP_P_STR_HADDR_SIZE);
00841
00842 for (i=0; i<len; i++)
00843 {
00844 chaddr[pp++] = int_to_char (((*c) & 0xF0) >> 4);
00845 chaddr[pp++] = int_to_char ((*c) & 0x0F);
00846 c += 1;
00847 if (i < len-1) { chaddr[pp++] = ':'; }
00848 }
00849
00850 return chaddr;
00851 }
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863 char* packets_get_chaddr_no_separator (struct dhcp_packet *p)
00864 {
00865 int len, i, pp;
00866 char *c;
00867
00868 len = ((int)packets_get_hlen(p)) & 0x000000FF;
00869 c = p->chaddr;
00870 pp = 0;
00871
00872 if (len > 16) { len = 16; }
00873
00874 memset ((void*)chaddr_exact, 0, DHCP_P_STR_HADDR_SIZE_EXACT);
00875
00876 for (i=0; i<len; i++)
00877 {
00878 chaddr_exact[pp++] = int_to_char (((*c) & 0xF0) >> 4);
00879 chaddr_exact[pp++] = int_to_char ((*c) & 0x0F);
00880 c += 1;
00881 }
00882
00883 return chaddr_exact;
00884 }
00885
00886
00887
00888
00889
00890
00891
00892
00893 char* packets_get_bin_chaddr (struct dhcp_packet *p)
00894 { return p->chaddr; }
00895
00896
00897
00898
00899
00900
00901
00902 void packets_set_sname (struct dhcp_packet *p, char *sname) { strncpy (p->sname, sname, DHCP_P_STR_SNAME_SIZE-1); }
00903
00904
00905
00906
00907
00908
00909
00910
00911 char* packets_get_sname (struct dhcp_packet *p)
00912 {
00913 memset ((void*)sname, 0, DHCP_P_STR_SNAME_SIZE);
00914 strncpy (sname, p->sname, DHCP_P_STR_SNAME_SIZE-1);
00915 return sname;
00916 }
00917
00918
00919
00920
00921
00922
00923
00924 void packets_set_file (struct dhcp_packet *p, char *file) { strncpy (p->file, file, DHCP_P_STR_FILE_SIZE-1); }
00925
00926
00927
00928
00929
00930
00931
00932
00933 char* packets_get_file (struct dhcp_packet *p)
00934 {
00935 memset ((void*)file, 0, DHCP_P_STR_FILE_SIZE);
00936 strncpy (file, p->file, DHCP_P_STR_FILE_SIZE-1);
00937 return file;
00938 }
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955 int packets_set_option (struct dhcp_packet *p, unsigned char option, char *value, int len)
00956 {
00957 int i;
00958
00959 if (len > VALUE_MAX_SIZE) { return 1; }
00960
00961 *((p->ctrl).opt_pointer) = option; (p->ctrl).opt_pointer += 1;
00962 *((p->ctrl).opt_pointer) = ((char)len) & 0x000000FF; (p->ctrl).opt_pointer += 1;
00963 (p->ctrl).taille += 2;
00964
00965 for (i=0; i<len; i++)
00966 {
00967 *((p->ctrl).opt_pointer) = *(value + i);
00968 (p->ctrl).opt_pointer += 1;
00969 (p->ctrl).taille += 1;
00970 }
00971
00972 return 0;
00973 }
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988 void packets_close_option (struct dhcp_packet *p)
00989 {
00990 *((p->ctrl).opt_pointer) = 0xFF;
00991 (p->ctrl).taille += 1;
00992 }
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002 int packets_parse_options (struct dhcp_packet *p)
01003 {
01004 int inopt = 0;
01005 int len, i, total;
01006 int dslam_vendor;
01007 unsigned char code;
01008
01009 packets_init_option_reader (p);
01010 dslam_vendor = DSLAM_UNKNOWN;
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022 total = (((p->ctrl).taille > (236 + 4)) ? ((p->ctrl).taille - 236) : OPTION_SIZE) - 4;
01023
01024 if (total > OPTION_SIZE - 4) { return 1; }
01025
01026
01027 if (debug > 0)
01028 {
01029 my_syslog (log_file,
01030 "[DEBUG] [%s:%d] packets_parse_options: Size of the option's part total = %d",
01031 __FILE__, __LINE__, total);
01032 }
01033
01034 while (1)
01035 {
01036 if (debug > 1)
01037 { my_syslog (log_file,
01038 "[DEBUG] [%s:%d] packets_parse_options: total = %d", __FILE__, __LINE__, total); }
01039
01040 if (inopt == 0)
01041 {
01042 if (*((p->ctrl).opt_pointer_reader) == 0x00)
01043 {
01044 (p->ctrl).opt_pointer_reader += 1; if (--total < 1) { return 1; }
01045 continue;
01046 }
01047
01048 if (*((p->ctrl).opt_pointer_reader) == 0xFF) { break; }
01049
01050 code = *((p->ctrl).opt_pointer_reader);
01051 (p->ctrl).opt_pointer_reader += 1; if (--total < 1) { return 1; }
01052 inopt = 1;
01053 continue;
01054 }
01055
01056 len = (int)(*((p->ctrl).opt_pointer_reader)) & 0x000000FF;
01057
01058 ((p->ctrl).list[code]).code = code;
01059 ((p->ctrl).list[code]).len = len;
01060 (p->ctrl).opt_pointer_reader += 1; if (--total < 1) { return 1; }
01061
01062 if (len > VALUE_MAX_SIZE) { return 1; }
01063 for (i=0; i<len; i++)
01064 {
01065 (((p->ctrl).list[code]).value)[i] = *((p->ctrl).opt_pointer_reader);
01066 (p->ctrl).opt_pointer_reader += 1; if (--total < 1) { return 1; }
01067
01068 if (debug > 0)
01069 {
01070 my_syslog (log_file,
01071 "[DEBUG] [%s:%d] packets_parse_options: Size of the option's part total = %d",
01072 __FILE__, __LINE__, total);
01073 }
01074 }
01075
01076 inopt = 0;
01077 }
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088 if (((p->ctrl).list[82]).code != 0)
01089 {
01090 if (((p->ctrl).list[82]).len == 28)
01091 {
01092 struct m_dhcp82 *mask;
01093 unsigned long int vid, nasip;
01094 unsigned short int vlanid, node, vci;
01095
01096 mask = (struct m_dhcp82*)((p->ctrl).list[82]).value;
01097
01098
01099
01100
01101
01102 vid = 0;
01103 vid = (mask->vendor_id[0] << 24) |
01104 (mask->vendor_id[1] << 16) |
01105 (mask->vendor_id[2] << 8) |
01106 mask->vendor_id[3];
01107
01108 vlanid = 0;
01109 vlanid = (mask->vlan_id[0] << 8) |
01110 mask->vlan_id[1];
01111
01112 nasip = 0;
01113 nasip = (mask->nas_ip[0] << 24) |
01114 (mask->nas_ip[1] << 16) |
01115 (mask->nas_ip[2] << 8) |
01116 mask->nas_ip[3];
01117
01118 node = (mask->node_id[0] << 8) |
01119 mask->node_id[1];
01120
01121 vci = (mask->vci[0] << 8) |
01122 mask->vci[1];
01123
01124
01125
01126
01127
01128 if (vid == 0x0000079D)
01129 {
01130 if (debug > 0)
01131 {
01132 my_syslog (log_file,
01133 "[DEBUG] [%s:%d] packets_parse_options: Vendor ID for UTSTARCOM found",
01134 __FILE__, __LINE__);
01135 }
01136
01137
01138
01139
01140
01141 if (mask->sub_option != 2)
01142 {
01143 my_syslog (log_file,
01144 "[WARNING] [%s:%d] Invalid value for option 82 sub-option (%d instead of 2)",
01145 __FILE__, __LINE__, (int)mask->sub_option);
01146 return 1;
01147 }
01148
01149 if (mask->length != 26)
01150 {
01151 my_syslog (log_file,
01152 "[WARNING] [%s:%d] Invalid value for option 82 length (%d instead of 26)",
01153 __FILE__, __LINE__, (int)mask->length);
01154 return 1;
01155 }
01156
01157 (p->ctrl).dslam_data.sub_option = mask->sub_option;
01158 (p->ctrl).dslam_data.length = mask->length;
01159 (p->ctrl).dslam_data.vendor_id = vid;
01160 (p->ctrl).dslam_data.port_type = mask->port_type;
01161 (p->ctrl).dslam_data.version = mask->version;
01162 (p->ctrl).dslam_data.vlan_id = vlanid;
01163
01164 memcpy ((void*)(p->ctrl).dslam_data.nas_mac, mask->nas_mac, 6);
01165 memset ((void*)((p->ctrl).dslam_data.node_id), 0, NODE_BUFFER_SIZE);
01166 snprintf ((p->ctrl).dslam_data.node_id, NODE_BUFFER_SIZE-1, "%u", node);
01167
01168 (p->ctrl).dslam_data.nas_ip = nasip;
01169 (p->ctrl).dslam_data.shelf = mask->shelf + 1;
01170 (p->ctrl).dslam_data.numero_slot = mask->numero_slot + 1;
01171 (p->ctrl).dslam_data.numero_port = mask->numero_port + 1;
01172 (p->ctrl).dslam_data.vpi = mask->vpi;
01173 (p->ctrl).dslam_data.vci = vci;
01174
01175 dslam_vendor = DSLAM_UTSTARCOM;
01176 }
01177 }
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214 if (dslam_vendor == DSLAM_UNKNOWN)
01215
01216 {
01217 while (
01218 (((p->ctrl).list[82]).len >= 20)
01219 &&
01220 (((p->ctrl).list[82]).len < VALUE_MAX_SIZE)
01221 )
01222 {
01223 char *c, *v82, value82[VALUE_MAX_SIZE], node[NODE_BUFFER_SIZE], rack[2], shelf[2], slot[3], port[3];
01224 char vpi[4], vci[4], vlanid[7];
01225 unsigned char subopt, subopt_length;
01226
01227
01228 if (debug > 0)
01229 {
01230 my_syslog (log_file,
01231 "[DEBUG] [%s:%d] packets_parse_options: Is it an ALCATEL DSLAM?",
01232 __FILE__, __LINE__);
01233 }
01234
01235 memset ((void*)value82, 0, VALUE_MAX_SIZE);
01236 memset ((void*)node, 0, NODE_BUFFER_SIZE);
01237 memset ((void*)rack, 0, 2);
01238 memset ((void*)shelf, 0, 2);
01239 memset ((void*)slot, 0, 3);
01240 memset ((void*)port, 0, 3);
01241 memset ((void*)vpi, 0, 4);
01242 memset ((void*)vci, 0, 4);
01243 memset ((void*)vlanid, 0, 7);
01244
01245
01246
01247
01248
01249
01250
01251 memcpy ((void*)value82, (void*)((p->ctrl).list[82]).value, ((p->ctrl).list[82]).len);
01252 v82 = value82;
01253
01254
01255
01256
01257
01258 subopt = value82[0];
01259 subopt_length = value82[1];
01260
01261 if (debug > 0)
01262 {
01263 my_syslog (log_file,
01264 "[DEBUG] [%s:%d] packets_parse_options: OPT82 = [%s]",
01265 __FILE__, __LINE__, value82);
01266
01267 my_syslog (log_file,
01268 "[DEBUG] [%s:%d] packets_parse_options: Sub-Option=%u and Sub-Option length=%u",
01269 __FILE__, __LINE__, subopt, subopt_length);
01270 }
01271
01272
01273
01274
01275
01276
01277
01278 v82 = value82 + 2;
01279 c = strstr (v82, " atm ");
01280 if (c == NULL)
01281 {
01282 my_syslog (log_file,
01283 "[WARNING] [%s:%d] packets_parse_options: Can not find the string ' atm '!",
01284 __FILE__, __LINE__);
01285 return 1;
01286 }
01287
01288 if (c != v82) {
01289 int length;
01290 length = (int)(c - v82);
01291 if (length >= NODE_BUFFER_SIZE) { break; }
01292 memcpy ((void*)node, (void*)v82, length);
01293 v82 = c;
01294 }
01295
01296 v82 += 5;
01297
01298
01299
01300
01301
01302
01303 if ((*v82 < 48) || (*v82 > 57))
01304 {
01305 my_syslog (log_file,
01306 "[WARNING] [%s:%d] packets_parse_options: Rack is not numeric!",
01307 __FILE__, __LINE__);
01308 return 1;
01309 }
01310
01311 if (*(v82+1) != '/')
01312 {
01313 my_syslog (log_file,
01314 "[WARNING] [%s:%d] packets_parse_options: No character '/' after Rack!",
01315 __FILE__, __LINE__);
01316 return 1;
01317 }
01318
01319 rack[0] = *v82;
01320 v82 += 2;
01321
01322
01323
01324
01325
01326
01327 if ((*v82 < 48) || (*v82 > 57))
01328 {
01329 my_syslog (log_file,
01330 "[WARNING] [%s:%d] packets_parse_options: Shelf is not numeric!",
01331 __FILE__, __LINE__);
01332 return 1;
01333 }
01334
01335 if (*(v82+1) != '/')
01336 {
01337 my_syslog (log_file,
01338 "[WARNING] [%s:%d] packets_parse_options: No character '/' after Rack!",
01339 __FILE__, __LINE__);
01340 return 1;
01341 }
01342
01343 shelf[0] = *v82;
01344 v82 += 2;
01345
01346
01347
01348
01349
01350
01351 if ((*v82 < 48) || (*v82 > 57))
01352 {
01353 my_syslog (log_file,
01354 "[WARNING] [%s:%d] packets_parse_options: Slot is not numeric!",
01355 __FILE__, __LINE__);
01356 return 1;
01357 }
01358
01359 if ((*(v82+1) < 48) || (*(v82+1) > 57))
01360 {
01361 my_syslog (log_file,
01362 "[WARNING] [%s:%d] packets_parse_options: Slot is not numeric!",
01363 __FILE__, __LINE__);
01364 return 1;
01365 }
01366
01367 if (*(v82+2) != '/')
01368 {
01369 my_syslog (log_file,
01370 "[WARNING] [%s:%d] packets_parse_options: No character '/' after Slot!",
01371 __FILE__, __LINE__);
01372 return 1;
01373 }
01374
01375 slot[0] = *v82;
01376 slot[1] = *(v82 + 1);
01377 v82 += 3;
01378
01379
01380
01381
01382
01383
01384 if ((*v82 < 48) || (*v82 > 57))
01385 {
01386 my_syslog (log_file,
01387 "[WARNING] [%s:%d] packets_parse_options: Port is not numeric!",
01388 __FILE__, __LINE__);
01389 return 1;
01390 }
01391
01392 if ((*(v82+1) < 48) || (*(v82+1) > 57))
01393 {
01394 my_syslog (log_file,
01395 "[WARNING] [%s:%d] packets_parse_options: Port is not numeric!",
01396 __FILE__, __LINE__);
01397 return 1;
01398 }
01399
01400 if (*(v82+2) != ':')
01401 {
01402 my_syslog (log_file,
01403 "[WARNING] [%s:%d] packets_parse_options: No character ':' after Port!",
01404 __FILE__, __LINE__);
01405 return 1;
01406 }
01407
01408 port[0] = *v82;
01409 port[1] = *(v82+1);
01410 v82 += 3;
01411
01412
01413
01414
01415
01416
01417 c = strstr (v82, ".");
01418 if (c == NULL)
01419 {
01420 my_syslog (log_file,
01421 "[WARNING] [%s:%d] packets_parse_options: No character '.' after VPI!",
01422 __FILE__, __LINE__);
01423 return 1;
01424 }
01425
01426 if (c == v82)
01427 {
01428 my_syslog (log_file,
01429 "[WARNING] [%s:%d] packets_parse_options: Empty VPI!",
01430 __FILE__, __LINE__);
01431 return 1;
01432 }
01433 else {
01434 int length;
01435 length = (int)(c - v82);
01436 if (length > 3)
01437 {
01438 my_syslog (log_file,
01439 "[WARNING] [%s:%d] packets_parse_options: VPI size is %d (too long, max is 3)!",
01440 __FILE__, __LINE__, length);
01441 return 1;
01442 }
01443 memcpy ((void*)vpi, (void*)v82, length);
01444 }
01445 v82 = c + 1;
01446
01447
01448
01449
01450
01451 c = strstr (v82, "\2");
01452 if (c == NULL)
01453 {
01454 my_syslog (log_file,
01455 "[WARNING] [%s:%d] packets_parse_options: No character '.' after VCI!",
01456 __FILE__, __LINE__);
01457 return 1;
01458 }
01459
01460 if (c == v82)
01461 {
01462 my_syslog (log_file,
01463 "[WARNING] [%s:%d] packets_parse_options: Empty VCI!",
01464 __FILE__, __LINE__);
01465 return 1;
01466 }
01467 else {
01468 int length;
01469 length = (int)(c - v82);
01470 if (length > 3)
01471 {
01472 my_syslog (log_file,
01473 "[WARNING] [%s:%d] packets_parse_options: VCI size is %d (too long, max is 3)!",
01474 __FILE__, __LINE__, length);
01475 return 1;
01476 }
01477 memcpy ((void*)vci, (void*)v82, length);
01478 }
01479
01480
01481
01482
01483 v82 = c + 1;
01484
01485
01486
01487
01488
01489 (p->ctrl).dslam_data.sub_option = (unsigned char)subopt;
01490 (p->ctrl).dslam_data.length = (unsigned char)subopt_length;
01491 (p->ctrl).dslam_data.vendor_id = 0;
01492 (p->ctrl).dslam_data.port_type = 0;
01493 (p->ctrl).dslam_data.version = 0;
01494
01495 memset ((void*)(p->ctrl).dslam_data.nas_mac, 0, 6);
01496 memset ((void*)((p->ctrl).dslam_data.node_id), 0, NODE_BUFFER_SIZE);
01497 strncpy ((p->ctrl).dslam_data.node_id, node, NODE_BUFFER_SIZE-1);
01498
01499 (p->ctrl).dslam_data.nas_ip = 0;
01500 (p->ctrl).dslam_data.shelf = (unsigned char)(atoi(shelf));
01501 (p->ctrl).dslam_data.numero_slot = (unsigned char)(atoi(slot));
01502 (p->ctrl).dslam_data.numero_port = (unsigned char)(atoi(port));
01503 (p->ctrl).dslam_data.vpi = (unsigned short int)(atoi(vpi));
01504 (p->ctrl).dslam_data.vci = (unsigned short int)(atoi(vci));
01505
01506
01507
01508
01509
01510
01511
01512 strncpy (vlanid, vpi, 3);
01513 strncat (vlanid, vci, 3);
01514
01515 (p->ctrl).dslam_data.vlan_id = (unsigned int)(atoi(vlanid));
01516
01517 dslam_vendor = DSLAM_ALCATEL;
01518 break;
01519 }
01520 }
01521
01522
01523
01524
01525
01526
01527 (p->ctrl).dslam_data.constructor = dslam_vendor;
01528
01529 if (dslam_vendor == DSLAM_UNKNOWN)
01530 {
01531 my_syslog (log_file,
01532 "[DEBUG] [%s:%d] packets_parse_options: Could not find the DSLAM's vendor!",
01533 __FILE__, __LINE__);
01534 return 1;
01535 }
01536 }
01537
01538 return 0;
01539 }
01540
01541
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551
01552
01553
01554
01555
01556
01557 int packets_get_option (struct dhcp_packet *p, int option, int *len, unsigned char **value)
01558 {
01559 if (((p->ctrl).list[option]).code == 0) { return 1; }
01560
01561 *len = ((p->ctrl).list[option]).len;
01562 *value = ((p->ctrl).list[option]).value;
01563
01564 return 0;
01565 }
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591
01592 int packets_get_message_type (struct dhcp_packet *p)
01593 {
01594 int rc, len;
01595 unsigned char *value;
01596
01597 rc = packets_get_option (p, 53, &len, &value);
01598 if (rc == 1) { return -1; }
01599
01600 return ((int)*value) & 0x000000FF;
01601 }
01602
01603
01604
01605
01606
01607
01608
01609
01610
01611
01612
01613 char* packets_get_subnet_mask (struct dhcp_packet *p)
01614 {
01615 int rc, len;
01616 unsigned char *value;
01617 static char subvalue[OPTIONS_OPTION_VALUE_SIZE];
01618
01619 memset ((void*)subvalue, 0, OPTIONS_OPTION_VALUE_SIZE);
01620
01621 rc = packets_get_option (p, 1, &len, &value);
01622 if (rc == 1) { return NULL; }
01623
01624 memcpy ((void*)subvalue, (void*)value, (len < OPTIONS_OPTION_VALUE_SIZE) ? len : OPTIONS_OPTION_VALUE_SIZE-1);
01625
01626 return subvalue;
01627 }
01628
01629
01630
01631
01632
01633
01634
01635
01636
01637
01638
01639
01640
01641
01642
01643 char* packets_get_node_id (struct dhcp_packet *p)
01644 { return (p->ctrl).dslam_data.node_id; }
01645
01646
01647
01648
01649
01650
01651
01652
01653
01654
01655
01656
01657
01658
01659
01660 unsigned char packets_get_shelf (struct dhcp_packet *p)
01661 { return (p->ctrl).dslam_data.shelf; }
01662
01663
01664
01665
01666
01667
01668
01669
01670
01671
01672
01673
01674
01675
01676
01677 unsigned char packets_get_numero_slot (struct dhcp_packet *p)
01678 { return (p->ctrl).dslam_data.numero_slot; }
01679
01680
01681
01682
01683
01684
01685
01686
01687
01688
01689
01690
01691
01692
01693
01694 unsigned short int packets_get_vci (struct dhcp_packet *p)
01695 { return (p->ctrl).dslam_data.vci; }
01696
01697
01698
01699
01700
01701
01702
01703
01704
01705
01706
01707
01708
01709
01710
01711 unsigned char packets_get_numero_port (struct dhcp_packet *p)
01712 { return (p->ctrl).dslam_data.numero_port; }
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728 unsigned short int packets_get_vlan_id (struct dhcp_packet *p)
01729 { return (p->ctrl).dslam_data.vlan_id; }
01730
01731
01732
01733
01734
01735 #define BUFFER_SIZE 4096
01736
01737
01738
01739
01740
01741
01742
01743
01744
01745
01746
01747
01748
01749 int packet_dump (struct dhcp_packet *p, char *file, char *csv, struct sockaddr_in *from, struct sockaddr_in *to)
01750 {
01751 FILE *out, *csvfd;
01752 int iout, icsvfd;
01753 int i, j, k, len, port_from, port_to, type, modem_manufacturor, size, o;
01754 unsigned char *c, *value;
01755 char addresse_from[16], addresse_to[16], buffer[BUFFER_SIZE], buff82[BUFFER_SIZE], *s, *sopt;
01756 static unsigned int mark = 0;
01757 dstring ds, dsoptions;
01758
01759
01760
01761 if (debug > 0)
01762 { my_syslog (log_file, "[DEBUG] [%s:%d] Calling packet_dump()", __FILE__, __LINE__); }
01763
01764
01765
01766 memset ((void*)buff82, 0, BUFFER_SIZE);
01767
01768 iout = open (file, O_CREAT | O_WRONLY | O_APPEND | O_LARGEFILE, S_IRWXU | S_IRWXG | S_IRWXO);
01769 if (iout == -1)
01770 {
01771 my_syslog (log_file, "[WARNING] [%s:%d] Could not open file '%s' open() failed - %s",
01772 __FILE__, __LINE__,
01773 file,
01774 strerror(errno));
01775 return 1;
01776 }
01777
01778 out = fdopen (iout, "a");
01779 if (out == NULL)
01780 {
01781 my_syslog (log_file, "[WARNING] [%s:%d] Could not \"fdopen()\" file '%s' - %s",
01782 __FILE__, __LINE__,
01783 file,
01784 strerror(errno));
01785 return 1;
01786 }
01787
01788
01789
01790
01791
01792 modem_manufacturor = get_modem_manufacturor(p);
01793
01794
01795
01796
01797
01798 if (packets_parse_options(p) == 1)
01799 {
01800 my_syslog (log_file,
01801 "[WARNING] [%s:%d] Error while parsing packet, drop packet", __FILE__, __LINE__);
01802 fclose (out);
01803 return 1;
01804 }
01805
01806
01807
01808
01809
01810 if (dstring_init (&ds, 2048) == 1)
01811 {
01812 my_syslog (log_file,
01813 "[FATAL] [%s:%d] Can not allocate memory for dynamic string (ds)!",
01814 __FILE__, __LINE__);
01815 fclose (out);
01816 exit (1);
01817 }
01818
01819 if (dstring_init (&dsoptions, 2048) == 1)
01820 {
01821 my_syslog (log_file,
01822 "[FATAL] [%s:%d] Can not allocate memory for dynamic string (dsoptions)!",
01823 __FILE__, __LINE__);
01824 fclose (out);
01825 exit (1);
01826 }
01827
01828
01829
01830
01831
01832 if (from != NULL)
01833 { memset ((void*)addresse_from, 0, 16); get_remote_address (from, addresse_from, &port_from); }
01834
01835 if (to != NULL)
01836 { memset ((void*)addresse_to, 0, 16); get_remote_address (to, addresse_to, &port_to); }
01837
01838
01839
01840
01841
01842 if (dump_sync_fd != -1)
01843 {
01844 if (lock_file (dump_sync_fd) == 1)
01845 {
01846 my_syslog (log_file,
01847 "[ERROR] [%s:%d] Could not lock dump file! Continue processing anyway...",
01848 __FILE__, __LINE__);
01849 }
01850 }
01851
01852
01853
01854
01855
01856 fprintf (out, "%s (%u)\n\n", dater(), mark);
01857
01858 memset ((void*)buffer, 0, BUFFER_SIZE);
01859 snprintf (buffer, BUFFER_SIZE, "%s;%u;%s;", dater(), mark, myhostname);
01860 mark++;
01861 if (dstring_add (&ds, buffer, strlen(buffer)) == 1)
01862 {
01863 my_syslog (log_file,
01864 "[FATAL] [%s:%d] Can not allocate memory!",
01865 __FILE__, __LINE__);
01866 fclose (out);
01867 exit (1);
01868 }
01869
01870
01871
01872
01873
01874 memset ((void*)buffer, 0, BUFFER_SIZE);
01875
01876 if (from != NULL)
01877 {
01878 fprintf (out, "%s (%d) => ", addresse_from, port_from);
01879 snprintf (buffer, BUFFER_SIZE, "IN;%s;%d;", addresse_from, port_from);
01880 }
01881 else
01882 {
01883 fprintf (out, "- => ");
01884 snprintf (buffer, BUFFER_SIZE, "OUT;-;-;");
01885 }
01886
01887 if (dstring_add (&ds, buffer, strlen(buffer)) == 1)
01888 {
01889 my_syslog (log_file,
01890 "[FATAL] [%s:%d] Can not allocate memory!",
01891 __FILE__, __LINE__);
01892 fclose (out);
01893 exit (1);
01894 }
01895
01896
01897
01898
01899
01900 memset ((void*)buffer, 0, BUFFER_SIZE);
01901
01902 if (to != NULL)
01903 {
01904 fprintf (out, "%s (%d)", addresse_to, port_to);
01905 snprintf (buffer, BUFFER_SIZE, "%s;%d;", addresse_to, port_to);
01906 }
01907 else
01908 {
01909 fprintf (out, "-");
01910 snprintf (buffer, BUFFER_SIZE, "-;-;");
01911 }
01912
01913 if (dstring_add (&ds, buffer, strlen(buffer)) == 1)
01914 {
01915 my_syslog (log_file,
01916 "[FATAL] [%s:%d] Can not allocate memory!",
01917 __FILE__, __LINE__);
01918 fclose (out);
01919 exit (1);
01920 }
01921
01922
01923
01924
01925
01926 type = packets_get_message_type(p);
01927 fprintf (out, " %s", packets_type_to_char(type));
01928 fprintf (out, "\n\n");
01929
01930 memset ((void*)buffer, 0, BUFFER_SIZE);
01931 snprintf (buffer, BUFFER_SIZE, "%s;", packets_type_to_char(type));
01932 if (dstring_add (&ds, buffer, strlen(buffer)) == 1)
01933 {
01934 my_syslog (log_file,
01935 "[FATAL] [%s:%d] Can not allocate memory!",
01936 __FILE__, __LINE__);
01937 fclose (out);
01938 exit (1);
01939 }
01940
01941
01942
01943
01944
01945 fprintf (out, "Number of bytes : %u\n", (unsigned int)((p->ctrl).taille));
01946 fprintf (out, "Opcode : %u\n", ((unsigned int)packets_get_op(p)) & 0x000000FF);
01947 fprintf (out, "Hardware type : %u\n", ((unsigned int)packets_get_htype(p)) & 0x000000FF);
01948 fprintf (out, "Hardware address length : %u\n", ((unsigned int)packets_get_hlen(p)) & 0x000000FF);
01949 fprintf (out, "Hop count : %u\n", ((unsigned int)packets_get_hops(p)) & 0x000000FF);
01950 fprintf (out, "Transaction ID : %lu\n", packets_get_xid(p));
01951 fprintf (out, "Ellapsed time : %hu\n", packets_get_secs(p));
01952 fprintf (out, "Flags : %hu\n", packets_get_flags(p));
01953 fprintf (out, "Client IP address : %s\n", packets_get_ciaddr(p));
01954 fprintf (out, "Your IP address : %s\n", packets_get_yiaddr(p));
01955 fprintf (out, "Server IP address : %s\n", packets_get_siaddr(p));
01956 fprintf (out, "Gateway IP address : %s\n", packets_get_giaddr(p));
01957 fprintf (out, "Client hardware address : %s (%s)\n", packets_get_chaddr(p), id_manufacturor_to_string(modem_manufacturor));
01958 fprintf (out, "Server host name : %s\n", packets_get_sname(p));
01959 fprintf (out, "Boot file name : %s\n", packets_get_file(p));
01960 fprintf (out, "\n");
01961
01962 memset ((void*)buffer, 0, BUFFER_SIZE);
01963 snprintf (buffer, BUFFER_SIZE, "%u;%u;%u;%u;%u;%lu;%hu;%hu;%s;%s;%s;%s;%s;%s;%s;%s;",
01964 (unsigned int)((p->ctrl).taille),
01965 ((unsigned int)packets_get_op(p)) & 0x000000FF,
01966 ((unsigned int)packets_get_htype(p)) & 0x000000FF,
01967 ((unsigned int)packets_get_hlen(p)) & 0x000000FF,
01968 ((unsigned int)packets_get_hops(p)) & 0x000000FF,
01969 packets_get_xid(p),
01970 packets_get_secs(p),
01971 packets_get_flags(p),
01972 packets_get_ciaddr(p),
01973 packets_get_yiaddr(p),
01974 packets_get_siaddr(p),
01975 packets_get_giaddr(p),
01976 packets_get_chaddr(p),
01977 packets_get_sname(p),
01978 packets_get_file(p),
01979 id_manufacturor_to_string(modem_manufacturor));
01980
01981 if (dstring_add (&ds, buffer, strlen(buffer)) == 1)
01982 {
01983 my_syslog (log_file,
01984 "[FATAL] [%s:%d] Can not allocate memory!",
01985 __FILE__, __LINE__);
01986 fclose (out);
01987 exit (1);
01988 }
01989
01990
01991
01992
01993
01994 fprintf (out, "Options:\n\n");
01995
01996 for (o=0; o<256; o++)
01997 {
01998
01999
02000
02001
02002
02003 i = order[o];
02004
02005
02006
02007
02008
02009 if ((i==1 || i==3 || i==15 || i==6) && (((p->ctrl).list[i]).code == 0))
02010 {
02011 char aux[]=";";
02012 if (dstring_add (&dsoptions, aux, strlen(aux)) == 1)
02013 {
02014 my_syslog (log_file,
02015 "[FATAL] [%s:%d] Can not allocate memory!",
02016 __FILE__, __LINE__);
02017 fclose (out);
02018 exit (1);
02019 }
02020 continue;
02021 }
02022
02023
02024
02025
02026
02027 if (((p->ctrl).list[i]).code == 0) { continue; }
02028
02029 len = ((p->ctrl).list[i]).len;
02030 value = ((p->ctrl).list[i]).value;
02031
02032 fprintf (out, "\t%3d (%s) ", i, descriptions[i]);
02033
02034
02035
02036 if (i != 82)
02037 {
02038 if (! (i==1 || i==3 || i==15 || i==6))
02039 {
02040 memset ((void*)buffer, 0, BUFFER_SIZE);
02041 snprintf (buffer, BUFFER_SIZE, "%3d|%s|", i, descriptions[i]);
02042 if (dstring_add (&dsoptions, buffer, strlen(buffer)) == 1)
02043 {
02044 my_syslog (log_file,
02045 "[FATAL] [%s:%d] Can not allocate memory!",
02046 __FILE__, __LINE__);
02047 fclose (out);
02048 exit (1);
02049 }
02050 }
02051 }
02052
02053
02054
02055 memset ((void*)buffer, 0, BUFFER_SIZE);
02056 c = value;
02057
02058 for (j=0; j<len; j++)
02059 {
02060 fprintf (out, "%c%c ",
02061 int_to_char (((*c) & 0xF0) >> 4),
02062 int_to_char ((*c) & 0x0F));
02063
02064
02065
02066 if (i != 82)
02067 {
02068 snprintf (buffer, BUFFER_SIZE, "%c%c",
02069 int_to_char (((*c) & 0xF0) >> 4),
02070 int_to_char ((*c) & 0x0F));
02071 if (dstring_add (&dsoptions, buffer, strlen(buffer)) == 1)
02072 {
02073 my_syslog (log_file,
02074 "[FATAL] [%s:%d] Can not allocate memory!",
02075 __FILE__, __LINE__);
02076 fclose (out);
02077 exit (1);
02078 }
02079 }
02080
02081
02082
02083 c++;
02084 }
02085
02086
02087
02088 if (i != 82)
02089 {
02090 if (dstring_add (&dsoptions, ";", 1) == 1)
02091 {
02092 my_syslog (log_file,
02093 "[FATAL] [%s:%d] Can not allocate memory!",
02094 __FILE__, __LINE__);
02095 fclose (out);
02096 exit (1);
02097 }
02098 }
02099
02100
02101
02102 fprintf (out, "\n");
02103
02104
02105
02106
02107
02108 if ((i==82) && (((p->ctrl).list[i]).code != 0))
02109 {
02110 fprintf (out, "\t\tsub option: %u\n", (p->ctrl).dslam_data.sub_option);
02111 fprintf (out, "\t\tlength: %u\n", (p->ctrl).dslam_data.length);
02112 fprintf (out, "\t\tvendor ID: %#lX\n", (p->ctrl).dslam_data.vendor_id);
02113 fprintf (out, "\t\tport type: %u\n", (p->ctrl).dslam_data.port_type);
02114 fprintf (out, "\t\tversion: %u\n", (p->ctrl).dslam_data.version);
02115 fprintf (out, "\t\tvlan ID: %hu\n", (p->ctrl).dslam_data.vlan_id);
02116
02117
02118
02119 snprintf (buff82, BUFFER_SIZE, "82;%#lX;%u;%u;%hu;",
02120 (p->ctrl).dslam_data.vendor_id,
02121 (p->ctrl).dslam_data.port_type,
02122 (p->ctrl).dslam_data.version,
02123 (p->ctrl).dslam_data.vlan_id);
02124
02125
02126
02127 fprintf (out, "\t\tnas mac: ");
02128 for (k=0; k<6; k++)
02129 {
02130 char dd[3];
02131 unsigned char c = (p->ctrl).dslam_data.nas_mac[k];
02132 char c1 = int_to_char ((c & 0xF0) >> 4);
02133 char c2 = int_to_char (c & 0x0F);
02134
02135 fprintf (out, "%c%c", c1, c2);
02136 snprintf (dd, 3, "%c%c", c1, c2);
02137 strcat (buff82, dd);
02138
02139 if (k<5) { fprintf (out, ":"); strcat (buff82, ":"); }
02140 }
02141 strcat (buff82, ";");
02142 fprintf (out, "\n");
02143
02144 fprintf (out, "\t\tnas ip: %s\n", get_doted_ip((p->ctrl).dslam_data.nas_ip));
02145 fprintf (out, "\t\tnode: %s\n", (p->ctrl).dslam_data.node_id);
02146 fprintf (out, "\t\tshelf: %u\n", (p->ctrl).dslam_data.shelf);
02147 fprintf (out, "\t\tslot: %u\n", (p->ctrl).dslam_data.numero_slot);
02148 fprintf (out, "\t\tport: %u\n", (p->ctrl).dslam_data.numero_port);
02149 fprintf (out, "\t\tvpi: %u\n", (p->ctrl).dslam_data.vpi);
02150 fprintf (out, "\t\tvci: %hu\n", (p->ctrl).dslam_data.vci);
02151 fprintf (out, "\t\tvendor: ");
02152
02153
02154
02155 memset ((void*)buffer, 0, BUFFER_SIZE);
02156 snprintf (buffer, BUFFER_SIZE, "%s;%s;%u;%u;%u;%u;%hu;",
02157 get_doted_ip((p->ctrl).dslam_data.nas_ip),
02158 (p->ctrl).dslam_data.node_id,
02159 (p->ctrl).dslam_data.shelf,
02160 (p->ctrl).dslam_data.numero_slot,
02161 (p->ctrl).dslam_data.numero_port,
02162 (p->ctrl).dslam_data.vpi,
02163 (p->ctrl).dslam_data.vci);
02164
02165 strcat (buff82, buffer);
02166
02167
02168
02169 switch ((p->ctrl).dslam_data.constructor)
02170 {
02171 case DSLAM_ALCATEL: { fprintf (out, "ALCATEL\n");
02172 strcat (buff82, "ALCATEL;"); }
02173 break;
02174 case DSLAM_UTSTARCOM: { fprintf (out, "UTSTARCOM\n");
02175 strcat (buff82, "UTSTARCOM;"); }
02176 break;
02177 default: { fprintf (out, "Unknown (%d) - This should not happen!\n", (p->ctrl).dslam_data.constructor);
02178 strcat (buff82, "UNKNOWN;"); }
02179 }
02180 }
02181
02182 }
02183
02184
02185
02186
02187
02188 if (buff82[0] != 0)
02189 {
02190 if (dstring_add (&ds, buff82, strlen(buff82)) == 1)
02191 {
02192 my_syslog (log_file,
02193 "[FATAL] [%s:%d] Can not allocate memory!",
02194 __FILE__, __LINE__);
02195 fclose (out);
02196 exit (1);
02197 }
02198 }
02199 else
02200 {
02201 char aux[]=";;;;;;;;;;;;;;";
02202
02203 if (dstring_add (&ds, aux, strlen(aux)) == 1)
02204 {
02205 my_syslog (log_file,
02206 "[FATAL] [%s:%d] Can not allocate memory!",
02207 __FILE__, __LINE__);
02208 fclose (out);
02209 exit (1);
02210 }
02211 }
02212
02213 fprintf (out, "\n\n\n");
02214
02215
02216
02217
02218
02219 sopt = dstring_get_data (&dsoptions, &size);
02220 if (sopt == NULL)
02221 {
02222 my_syslog (log_file,
02223 "[FATAL] [%s:%d] Can not allocate memory!",
02224 __FILE__, __LINE__);
02225 fclose (out);
02226 exit (1);
02227 }
02228
02229 if (dstring_add (&ds, sopt, strlen(sopt)) == 1)
02230 {
02231 my_syslog (log_file,
02232 "[FATAL] [%s:%d] Can not allocate memory!",
02233 __FILE__, __LINE__);
02234 fclose (out);
02235 exit (1);
02236 }
02237
02238
02239
02240
02241
02242 s = dstring_get_data (&ds, &size);
02243 if (s == NULL)
02244 {
02245 my_syslog (log_file,
02246 "[FATAL] [%s:%d] Can not allocate memory!",
02247 __FILE__, __LINE__);
02248 fclose (out);
02249 exit (1);
02250 }
02251
02252 icsvfd = open (csv, O_CREAT | O_WRONLY | O_APPEND | O_LARGEFILE, S_IRWXU | S_IRWXG | S_IRWXO);
02253 if (icsvfd == -1)
02254 {
02255 my_syslog (log_file, "[WARNING] [%s:%d] Could not open file '%s' open() failed - %s",
02256 __FILE__, __LINE__,
02257 csv,
02258 strerror(errno));
02259 fclose (out);
02260 return 1;
02261 }
02262
02263
02264 csvfd = fdopen (icsvfd, "a");
02265 if (csvfd == NULL)
02266 {
02267 my_syslog (log_file, "[WARNING] [%s:%d] Could not fdopen file '%s' - %s",
02268 __FILE__, __LINE__,
02269 csv,
02270 strerror(errno));
02271 fclose (out);
02272 return 1;
02273 }
02274
02275 fprintf (csvfd, "%s\n", s);
02276 fclose (csvfd);
02277
02278
02279
02280
02281
02282 if (dump_sync_fd != -1)
02283 {
02284 if (unlock_file (dump_sync_fd) == 1)
02285 {
02286 my_syslog (log_file, "[ERROR] [%s:%d] Could not unlock dump file! Continue processing ...",
02287 __FILE__, __LINE__);
02288 }
02289 }
02290
02291 fclose (out);
02292
02293
02294
02295
02296
02297 dstring_free (&ds);
02298 dstring_free (&dsoptions);
02299 free (s);
02300
02301 return 0;
02302 }
02303
02304
02305
02306
02307
02308
02309
02310 int check_packet_size (int size)
02311 {
02312
02313
02314
02315
02316
02317
02318
02319
02320
02321 return (size >= 244);
02322 }
02323
02324
02325
02326
02327
02328
02329
02330
02331
02332 void packet_set_taille (struct dhcp_packet *p, int taille) { (p->ctrl).taille = taille; }
02333
02334
02335
02336
02337
02338
02339
02340
02341
02342
02343 static char* dater()
02344 {
02345 static char strtime[128];
02346 time_t tt;
02347
02348 tt = time(NULL);
02349 strftime (strtime, 128, "%Y-%m-%d %k:%M:%S", localtime(&tt));
02350
02351 return strtime;
02352 }
02353
02354
02355
02356
02357
02358
02359
02360
02361
02362 static void get_remote_address (struct sockaddr_in *sender, char *ip, int *port)
02363 {
02364 int i;
02365 char *a;
02366
02367 a = inet_ntoa(sender->sin_addr);
02368 for (i=0; i<15; i++) { ip[i] = *(a+i); };
02369 ip[15] = 0;
02370 *port = ntohs (sender->sin_port);
02371 }
02372
02373
02374