forked from freemandealer/shaniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapture.c
671 lines (554 loc) · 21.5 KB
/
shapture.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#include<netinet/in.h>
#include<errno.h>
#include<netdb.h>
#include<stdio.h> //For standard things
#include<stdlib.h> //malloc
#include<string.h> //strlen
#include<netinet/ip_icmp.h> //Provides declarations for icmp header
#include<netinet/udp.h> //Provides declarations for udp header
#include<netinet/tcp.h> //Provides declarations for tcp header
#include<netinet/ip.h> //Provides declarations for ip header
#include<netinet/if_ether.h> //For ETH_P_ALL
#include<net/ethernet.h> //For ether_header
#include<sys/socket.h>
#include<arpa/inet.h>
#include<sys/ioctl.h>
#include<sys/time.h>
#include<sys/types.h>
#include <linux/if.h> // For struct in_addr
#include<unistd.h>
#include "include/dhcp.h"
#include "include/rip.h"
#include "include/ospf.h"
void ProcessIPPacket(unsigned char *, int);
void ProcessPacket(unsigned char *, int);
void print_arp_packet(unsigned char *, int);
void print_ospf_packet(unsigned char *, int);
void print_ip_header(unsigned char *, int);
void print_tcp_packet(unsigned char *, int);
void process_udp_packet(unsigned char *, int);
void print_icmp_packet(unsigned char *, int);
void PrintData(unsigned char *, int);
FILE *logfile;
struct sockaddr_in source, dest;
int tcp = 0, udp = 0, icmp = 0, arp = 0, ospf = 0, others = 0, igmp = 0, total = 0, dhcp =0, rip = 0, i, j;
int main(int argc, char **argv)
{
int saddr_size, data_size;
struct sockaddr saddr;
char *interface = NULL;
if (argc > 1)
interface = argv[1];
unsigned char *buffer = (unsigned char *)malloc(65536); //Its Big!
logfile = fopen("/var/shanilog.txt", "w");
if (logfile == NULL) {
printf("Unable to create log.txt file.");
return 1;
}
printf("Starting ...\n");
int sock_raw = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (interface != NULL) {
printf("listening on %s ...\n", interface);
setsockopt(sock_raw, SOL_SOCKET, SO_BINDTODEVICE, interface,
strlen(interface) + 1);
}
if (sock_raw < 0) {
//Print the error with proper message
perror("Socket Error");
return 1;
}
while (1) {
saddr_size = sizeof saddr;
//Receive a packet
data_size =
recvfrom(sock_raw, buffer, 65536, 0, &saddr,
(socklen_t *) & saddr_size);
if (data_size < 0) {
printf("Recvfrom error , failed to get packets\n");
return 1;
}
//Now process the packet
ProcessPacket(buffer , data_size);
}
close(sock_raw);
printf("Finished");
return 0;
}
void ProcessIPPacket(unsigned char *buffer, int size)
{
struct iphdr *iph = (struct iphdr *)(buffer + sizeof(struct ethhdr));
switch (iph->protocol) //Check the Protocol and do accordingly...
{
case 1: //ICMP Protocol
++icmp;
print_icmp_packet(buffer, size);
break;
case 2: //IGMP Protocol
++igmp;
break;
case 6: //TCP Protocol
++tcp;
print_tcp_packet(buffer, size);
break;
case 17: //UDP Protocol
++udp;
process_udp_packet(buffer, size);
break;
case 89:
++ospf;
print_ospf_packet(buffer, size);
break;
default: //Some Other Protocol
++others;
break;
}
}
void ProcessPacket(unsigned char *buffer, int size)
{
int ether_proto = ((struct ethhdr *)buffer)->h_proto;
++total;
switch (ether_proto) {
case 0x0608:
++arp;
print_arp_packet(buffer, size);
break;
case 0x0008:
ProcessIPPacket(buffer, size);
break;
default: //Some Other Protocol.
++others;
break;
}
printf
("TCP : %d UDP : %d (DHCP : %d RIP : %d) ICMP : %d IGMP : %d ARP : %d OSPF : %d Others : %d Total : %d\r",
tcp, udp, dhcp, rip, icmp, igmp, arp, ospf, others, total);
}
void print_ethernet_header(unsigned char *Buffer, int Size)
{
struct ethhdr *eth = (struct ethhdr *)Buffer;
fprintf(logfile, "\n");
fprintf(logfile, "Ethernet Header\n");
fprintf(logfile,
" |-Destination Address : %.2X-%.2X-%.2X-%.2X-%.2X-%.2X \n",
eth->h_dest[0], eth->h_dest[1], eth->h_dest[2],
eth->h_dest[3], eth->h_dest[4], eth->h_dest[5]);
fprintf(logfile,
" |-Source Address : %.2X-%.2X-%.2X-%.2X-%.2X-%.2X \n",
eth->h_source[0], eth->h_source[1], eth->h_source[2],
eth->h_source[3], eth->h_source[4], eth->h_source[5]);
fprintf(logfile, " |-Protocol : %u \n",
(unsigned short)eth->h_proto);
}
void print_arp_packet(unsigned char *Buffer, int Size)
{
struct ether_arp *arp = (struct ether_arp *)(Buffer + sizeof(struct ethhdr));
struct arphdr *arph = (struct arphdr *)arp;
fprintf(logfile,
"\n\n***********************ARP Packet*************************\n");
print_ethernet_header(Buffer, Size);
fprintf(logfile, "\n");
fprintf(logfile, "ARP Header\n");
fprintf(logfile, " |-ar_hrd : %u\n", ntohs(arph->ar_hrd));
fprintf(logfile, " |-ar_pro : %u\n", ntohs(arph->ar_pro));
fprintf(logfile, " |-Length of Hardware Address : %u\n", ntohs(arph->ar_hln));
fprintf(logfile, " |-Length of Protocol Address : %u\n", ntohs(arph->ar_pln));
fprintf(logfile, " |-Opcode(command) : %u\n", ntohs(arph->ar_op));
fprintf(logfile, "\n");
fprintf(logfile, "ARP Content\n");
fprintf(logfile, " |-Sender Hardware Address : %.2X-%.2X-%.2X-%.2X-%.2X-%.2X \n",
arp->arp_sha[0], arp->arp_sha[1], arp->arp_sha[2], arp->arp_sha[3],
arp->arp_sha[4], arp->arp_sha[5]);
fprintf(logfile, " |-Sender Protocol Address : %u.%u.%u.%u\n",
arp->arp_spa[0], arp->arp_spa[1], arp->arp_spa[2], arp->arp_spa[3]);
fprintf(logfile, " |-Target Hardware Address : %.2X-%.2X-%.2X-%.2X-%.2X-%.2X \n",
arp->arp_tha[0], arp->arp_tha[1], arp->arp_tha[2], arp->arp_tha[3],
arp->arp_tha[4], arp->arp_tha[5]);
fprintf(logfile, " |-Target Protocol Address : %u.%u.%u.%u\n",
arp->arp_tpa[0], arp->arp_tpa[1], arp->arp_tpa[2], arp->arp_tpa[3]);
fprintf(logfile, "\n");
fprintf(logfile,
" DATA Dump ");
fprintf(logfile, "\n");
fprintf(logfile, "ARP Header\n");
PrintData(Buffer + sizeof(struct ethhdr), sizeof(struct arphdr));
fprintf(logfile, "ARP Payload\n");
PrintData(Buffer + sizeof(struct ethhdr) + sizeof(struct arphdr),
Size - sizeof(struct ethhdr) - sizeof(struct arphdr));
fprintf(logfile,
"\n###########################################################");
}
void print_ospf_packet(unsigned char *Buffer, int Size)
{
struct ospf_hdr *ospfhdr = (struct ospf_hdr *)(Buffer + sizeof(struct ethhdr)+
sizeof(struct iphdr));
fprintf(logfile,
"\n\n***********************OSPF Packet*************************\n");
print_ip_header(Buffer, Size);
fprintf(logfile, "\nOSPF Header\n");
fprintf(logfile, " |-Version : %d\n", ospfhdr->version);
switch (ospfhdr->type) {
case 1:
fprintf(logfile, " |-Type : hello(%d)\n", ospfhdr->type);
break;
case 2:
fprintf(logfile, " |-Type : database desciption(%d)\n", ospfhdr->type);
break;
case 3:
fprintf(logfile, " |-Type : lsr(%d)\n", ospfhdr->type);
break;
case 4:
fprintf(logfile, " |-Type : lsu(%d)\n", ospfhdr->type);
break;
default:
printf("unknown OSPF packet type (%d)\n", ospfhdr->type);
}
fprintf(logfile, " |-Length : %d\n", ntohs(ospfhdr->len));
fprintf(logfile, " |-rtr ID : %d\n", ntohl(ospfhdr->rtr_id));
fprintf(logfile, " |-Area ID : %d\n", ntohl(ospfhdr->area_id));
fprintf(logfile, " |-CheckSum : %d\n", ntohs(ospfhdr->chksum));
switch (ntohs(ospfhdr->auth_type)) {
case 0:
fprintf(logfile, " |-AuthType : none(%d)\n", ntohs(ospfhdr->auth_type));
break;
case 1:
fprintf(logfile, " |-AuthType : simple(%d)\n", ntohs(ospfhdr->auth_type));
fprintf(logfile, " |-AuthKey : see data dump\n");
break;
case 2:
fprintf(logfile, " |-AuthType : crypted(%d)\n", ntohs(ospfhdr->auth_type));
fprintf(logfile, " |-AuthKey : see data dump\n");
break;
}
fprintf(logfile, "\n");
// TODO Add detailed type-specific headers and contents
fprintf(logfile,
" DATA Dump ");
fprintf(logfile, "\n");
fprintf(logfile, "OSPF Header\n");
PrintData(Buffer + sizeof(struct ethhdr), sizeof(struct ospf_hdr));
fprintf(logfile, "OSPF Payload\n");
PrintData(Buffer + sizeof(struct ethhdr) + sizeof(struct ospf_hdr),
Size - sizeof(struct ethhdr) - sizeof(struct ospf_hdr));
fprintf(logfile,
"\n###########################################################");
}
void print_ip_header(unsigned char *Buffer, int Size)
{
print_ethernet_header(Buffer, Size);
unsigned short iphdrlen;
struct iphdr *iph = (struct iphdr *)(Buffer + sizeof(struct ethhdr));
iphdrlen = iph->ihl * 4;
memset(&source, 0, sizeof(source));
source.sin_addr.s_addr = iph->saddr;
memset(&dest, 0, sizeof(dest));
dest.sin_addr.s_addr = iph->daddr;
fprintf(logfile, "\n");
fprintf(logfile, "IP Header\n");
fprintf(logfile, " |-IP Version : %d\n",
(unsigned int)iph->version);
fprintf(logfile, " |-IP Header Length : %d DWORDS or %d Bytes\n",
(unsigned int)iph->ihl, ((unsigned int)(iph->ihl)) * 4);
fprintf(logfile, " |-Type Of Service : %d\n",
(unsigned int)iph->tos);
fprintf(logfile,
" |-IP Total Length : %d Bytes(Size of Packet)\n",
ntohs(iph->tot_len));
fprintf(logfile, " |-Identification : %d\n", ntohs(iph->id));
//fprintf(logfile , " |-Reserved ZERO Field : %d\n",(unsigned int)iphdr->ip_reserved_zero);
//fprintf(logfile , " |-Dont Fragment Field : %d\n",(unsigned int)iphdr->ip_dont_fragment);
//fprintf(logfile , " |-More Fragment Field : %d\n",(unsigned int)iphdr->ip_more_fragment);
fprintf(logfile, " |-TTL : %d\n", (unsigned int)iph->ttl);
fprintf(logfile, " |-Protocol : %d\n", (unsigned int)iph->protocol);
fprintf(logfile, " |-Checksum : %d\n", ntohs(iph->check));
fprintf(logfile, " |-Source IP : %s\n",
inet_ntoa(source.sin_addr));
fprintf(logfile, " |-Destination IP : %s\n",
inet_ntoa(dest.sin_addr));
}
void print_tcp_packet(unsigned char *Buffer, int Size)
{
unsigned short iphdrlen;
struct iphdr *iph = (struct iphdr *)(Buffer + sizeof(struct ethhdr));
iphdrlen = iph->ihl * 4;
struct tcphdr *tcph =
(struct tcphdr *)(Buffer + iphdrlen + sizeof(struct ethhdr));
int header_size = sizeof(struct ethhdr) + iphdrlen + tcph->doff * 4;
fprintf(logfile,
"\n\n***********************TCP Packet*************************\n");
print_ip_header(Buffer, Size);
fprintf(logfile, "\n");
fprintf(logfile, "TCP Header\n");
fprintf(logfile, " |-Source Port : %u\n", ntohs(tcph->source));
fprintf(logfile, " |-Destination Port : %u\n", ntohs(tcph->dest));
fprintf(logfile, " |-Sequence Number : %u\n", ntohl(tcph->seq));
fprintf(logfile, " |-Acknowledge Number : %u\n",
ntohl(tcph->ack_seq));
fprintf(logfile, " |-Header Length : %d DWORDS or %d BYTES\n",
(unsigned int)tcph->doff, (unsigned int)tcph->doff * 4);
//fprintf(logfile , " |-CWR Flag : %d\n",(unsigned int)tcph->cwr);
//fprintf(logfile , " |-ECN Flag : %d\n",(unsigned int)tcph->ece);
fprintf(logfile, " |-Urgent Flag : %d\n",
(unsigned int)tcph->urg);
fprintf(logfile, " |-Acknowledgement Flag : %d\n",
(unsigned int)tcph->ack);
fprintf(logfile, " |-Push Flag : %d\n",
(unsigned int)tcph->psh);
fprintf(logfile, " |-Reset Flag : %d\n",
(unsigned int)tcph->rst);
fprintf(logfile, " |-Synchronise Flag : %d\n",
(unsigned int)tcph->syn);
fprintf(logfile, " |-Finish Flag : %d\n",
(unsigned int)tcph->fin);
fprintf(logfile, " |-Window : %d\n", ntohs(tcph->window));
fprintf(logfile, " |-Checksum : %d\n", ntohs(tcph->check));
fprintf(logfile, " |-Urgent Pointer : %d\n", tcph->urg_ptr);
fprintf(logfile, "\n");
fprintf(logfile,
" DATA Dump ");
fprintf(logfile, "\n");
fprintf(logfile, "IP Header\n");
PrintData(Buffer, iphdrlen);
fprintf(logfile, "TCP Header\n");
PrintData(Buffer + iphdrlen, tcph->doff * 4);
fprintf(logfile, "Data Payload\n");
PrintData(Buffer + header_size, Size - header_size);
fprintf(logfile,
"\n###########################################################");
}
int is_dhcp_packet(int src_port, int dst_port, unsigned char *buf, int size)
{
// easy check via ports
if (src_port == 68 && dst_port == 67)
return 1;
else if (src_port == 67 && dst_port == 68)
return 1;
else
return 0;
}
int is_rip_packet(int src_port, int dst_port, unsigned char *buf, int size)
{
if (520 == src_port || 520 == dst_port)
return 1;
else
return 0;
}
void print_udp_packet(unsigned char *Buffer, int Size)
{
unsigned short iphdrlen;
struct iphdr *iph = (struct iphdr *)(Buffer + sizeof(struct ethhdr));
iphdrlen = iph->ihl * 4;
struct udphdr *udph =
(struct udphdr *)(Buffer + iphdrlen + sizeof(struct ethhdr));
int header_size = sizeof(struct ethhdr) + iphdrlen + sizeof udph;
fprintf(logfile,
"\n\n***********************UDP Packet*************************\n");
print_ip_header(Buffer, Size);
fprintf(logfile, "\nUDP Header\n");
fprintf(logfile, " |-Source Port : %d\n", ntohs(udph->source));
fprintf(logfile, " |-Destination Port : %d\n", ntohs(udph->dest));
fprintf(logfile, " |-UDP Length : %d\n", ntohs(udph->len));
fprintf(logfile, " |-UDP Checksum : %d\n", ntohs(udph->check));
fprintf(logfile, "\n");
fprintf(logfile, "IP Header\n");
PrintData(Buffer, iphdrlen);
fprintf(logfile, "UDP Header\n");
PrintData(Buffer + iphdrlen, sizeof udph);
fprintf(logfile, "Data Payload\n");
//Move the pointer ahead and reduce the size of string
PrintData(Buffer + header_size, Size - header_size);
fprintf(logfile,
"\n###########################################################");
}
void print_dhcp_packet(unsigned char *Buffer, int Size)
{
unsigned short iphdrlen;
struct iphdr *iph = (struct iphdr *)(Buffer + sizeof(struct ethhdr));
iphdrlen = iph->ihl * 4;
struct udphdr *udph =
(struct udphdr *)(Buffer + iphdrlen + sizeof(struct ethhdr));
int header_size = sizeof(struct ethhdr) + iphdrlen + sizeof udph;
struct dhcp_packet *dhcp =
(struct dhcp_packet *)(Buffer + header_size);
fprintf(logfile,
"\n\n***********************DHCP Packet*************************\n");
print_ip_header(Buffer, Size);
fprintf(logfile, "\nUDP Header\n");
fprintf(logfile, " |-Source Port : %d\n", ntohs(udph->source));
fprintf(logfile, " |-Destination Port : %d\n", ntohs(udph->dest));
fprintf(logfile, " |-UDP Length : %d\n", ntohs(udph->len));
fprintf(logfile, " |-UDP Checksum : %d\n", ntohs(udph->check));
fprintf(logfile, "\n");
fprintf(logfile, "\nDHCP Content\n");
if (1 == dhcp->op)
fprintf(logfile, " |-Operation : c->s\n");
else if (2 == dhcp->op)
fprintf(logfile, " |-Operation : s->c\n");
else
printf("bad dhcp packet\n");
if (1 == dhcp->htype)
fprintf(logfile, " |-Hardware Addr Type : Ethernet\n");
else
fprintf(logfile, " |-Hardware Addr Type : %d\n", dhcp->htype);
fprintf(logfile, " |-Hardware Addr Len : %d\n", dhcp->hlen);
fprintf(logfile, " |-Hops : %d\n", dhcp->hops);
fprintf(logfile, " |-Transac ID : %d\n", ntohl(dhcp->xid));
fprintf(logfile, " |-Duration : %d\n", ntohs(dhcp->secs));
fprintf(logfile, " |-Flags : 0x%x\n", ntohs(dhcp->flags));
fprintf(logfile, " |-Client IP Used : %s\n", inet_ntoa(dhcp->ciaddr));
fprintf(logfile, " |-Client IP : %s\n", inet_ntoa(dhcp->yiaddr));
fprintf(logfile, " |-Next Server : %s\n", inet_ntoa(dhcp->siaddr));
fprintf(logfile, " |-Relay Agent : %s\n", inet_ntoa(dhcp->giaddr));
if (1 == dhcp->op) // we only know the len of hardware address is 48 in ethernet
fprintf(logfile, " |-Client Hardware Addr : %.2X-%.2X-%.2X-%.2X-%.2X-%.2X \n",
dhcp->chaddr[0], dhcp->chaddr[1], dhcp->chaddr[2], dhcp->chaddr[3],
dhcp->chaddr[4], dhcp->chaddr[5]);
fprintf(logfile, " |-Server Name : %s\n", dhcp->sname);
fprintf(logfile, " |-Boot Filename : %s\n", dhcp->file);
fprintf(logfile, " |-Options : see dump data\n");
fprintf(logfile, "\n");
fprintf(logfile, "IP Header\n");
PrintData(Buffer, iphdrlen);
fprintf(logfile, "UDP Header\n");
PrintData(Buffer + iphdrlen, sizeof udph);
fprintf(logfile, "Data Payload\n");
//Move the pointer ahead and reduce the size of string
PrintData(Buffer + header_size, Size - header_size);
fprintf(logfile,
"\n###########################################################");
}
void print_rip_packet(unsigned char *Buffer, int Size)
{
unsigned short iphdrlen;
struct iphdr *iph = (struct iphdr *)(Buffer + sizeof(struct ethhdr));
iphdrlen = iph->ihl * 4;
struct udphdr *udph =
(struct udphdr *)(Buffer + iphdrlen + sizeof(struct ethhdr));
int header_size = sizeof(struct ethhdr) + iphdrlen + sizeof udph;
struct rip_packet *rip =
(struct rip_packet *)(Buffer + header_size);
fprintf(logfile,
"\n\n***********************RIP Packet*************************\n");
print_ip_header(Buffer, Size);
fprintf(logfile, "\nUDP Header\n");
fprintf(logfile, " |-Source Port : %d\n", ntohs(udph->source));
fprintf(logfile, " |-Destination Port : %d\n", ntohs(udph->dest));
fprintf(logfile, " |-UDP Length : %d\n", ntohs(udph->len));
fprintf(logfile, " |-UDP Checksum : %d\n", ntohs(udph->check));
fprintf(logfile, "\n");
fprintf(logfile, "\nRIP Content\n");
fprintf(logfile, " |-Command : %d\n", rip->cmd);
fprintf(logfile, " |-Version : %d\n", rip->ver);
if (1 == rip->ver) {
fprintf(logfile, " |-Addr Family : %d\n", ntohs(rip->u.v1.addr_family));
fprintf(logfile, " |-Address : %s\n", inet_ntoa(*(struct in_addr *)&(rip->u.v1.addr)));
fprintf(logfile, " |-Metric : %d\n", ntohl(rip->u.v1.metric));
} else if (2 == rip->ver) {
fprintf(logfile, " |-Addr Format : %d\n", ntohs(rip->u.v2.addr_format));
fprintf(logfile, " |-Route Tag : %d\n", ntohs(rip->u.v2.rt_tag));
fprintf(logfile, " |-Address : %s\n", inet_ntoa(*(struct in_addr *)&(rip->u.v2.addr)));
fprintf(logfile, " |-Subnet Mask : %s\n", inet_ntoa(*(struct in_addr *)&(rip->u.v2.subnet_mask)));
fprintf(logfile, " |-Next Hop : %s\n", inet_ntoa(*(struct in_addr *)&(rip->u.v2.nhop)));
fprintf(logfile, " |-Metric : %d\n", ntohl(rip->u.v2.metric));
} else {
printf("unknown version of rip\n");
}
fprintf(logfile, "\n");
fprintf(logfile, "IP Header\n");
PrintData(Buffer, iphdrlen);
fprintf(logfile, "UDP Header\n");
PrintData(Buffer + iphdrlen, sizeof udph);
fprintf(logfile, "Data Payload\n");
//Move the pointer ahead and reduce the size of string
PrintData(Buffer + header_size, Size - header_size);
fprintf(logfile,
"\n###########################################################");
}
void process_udp_packet(unsigned char *Buffer, int Size)
{
unsigned short iphdrlen;
int src_port, dst_port;
struct iphdr *iph = (struct iphdr *)(Buffer + sizeof(struct ethhdr));
iphdrlen = iph->ihl * 4;
struct udphdr *udph =
(struct udphdr *)(Buffer + iphdrlen + sizeof(struct ethhdr));
src_port = ntohs(udph->source);
dst_port = ntohs(udph->dest);
if (is_dhcp_packet(src_port, dst_port, Buffer, Size)) {
dhcp ++;
print_dhcp_packet(Buffer, Size);
} else if (is_rip_packet(src_port, dst_port, Buffer, Size)) {
rip ++;
print_rip_packet(Buffer, Size);
} else
print_udp_packet(Buffer, Size);
}
void print_icmp_packet(unsigned char *Buffer, int Size)
{
unsigned short iphdrlen;
struct iphdr *iph = (struct iphdr *)(Buffer + sizeof(struct ethhdr));
iphdrlen = iph->ihl * 4;
struct icmphdr *icmph =
(struct icmphdr *)(Buffer + iphdrlen + sizeof(struct ethhdr));
int header_size = sizeof(struct ethhdr) + iphdrlen + sizeof icmph;
fprintf(logfile,
"\n\n***********************ICMP Packet*************************\n");
print_ip_header(Buffer, Size);
fprintf(logfile, "\n");
fprintf(logfile, "ICMP Header\n");
fprintf(logfile, " |-Type : %d", (unsigned int)(icmph->type));
if ((unsigned int)(icmph->type) == 11) {
fprintf(logfile, " (TTL Expired)\n");
} else if ((unsigned int)(icmph->type) == ICMP_ECHOREPLY) {
fprintf(logfile, " (ICMP Echo Reply)\n");
}
fprintf(logfile, " |-Code : %d\n", (unsigned int)(icmph->code));
fprintf(logfile, " |-Checksum : %d\n", ntohs(icmph->checksum));
//fprintf(logfile , " |-ID : %d\n",ntohs(icmph->id));
//fprintf(logfile , " |-Sequence : %d\n",ntohs(icmph->sequence));
fprintf(logfile, "\n");
fprintf(logfile, "IP Header\n");
PrintData(Buffer, iphdrlen);
fprintf(logfile, "UDP Header\n");
PrintData(Buffer + iphdrlen, sizeof icmph);
fprintf(logfile, "Data Payload\n");
//Move the pointer ahead and reduce the size of string
PrintData(Buffer + header_size, (Size - header_size));
fprintf(logfile,
"\n###########################################################");
}
void PrintData(unsigned char *data, int Size)
{
int i, j;
for (i = 0; i < Size; i++) {
if (i != 0 && i % 16 == 0) //if one line of hex printing is complete...
{
fprintf(logfile, " ");
for (j = i - 16; j < i; j++) {
if (data[j] >= 32 && data[j] <= 128)
fprintf(logfile, "%c", (unsigned char)data[j]); //if its a number or alphabet
else
fprintf(logfile, "."); //otherwise print a dot
}
fprintf(logfile, "\n");
}
if (i % 16 == 0)
fprintf(logfile, " ");
fprintf(logfile, " %02X", (unsigned int)data[i]);
if (i == Size - 1) //print the last spaces
{
for (j = 0; j < 15 - i % 16; j++) {
fprintf(logfile, " "); //extra spaces
}
fprintf(logfile, " ");
for (j = i - i % 16; j <= i; j++) {
if (data[j] >= 32 && data[j] <= 128) {
fprintf(logfile, "%c",
(unsigned char)data[j]);
} else {
fprintf(logfile, ".");
}
}
fprintf(logfile, "\n");
}
}
}