-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket_print.c
78 lines (60 loc) · 2.04 KB
/
packet_print.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "packet_struct.h"
#include "packet_print.h"
void print_mac_addr(const u_char * bytes) {
int i = 0;
for(i =0; i < ETH_ADDR_SIZE;i++) {
fprintf(stdout,"%02X:",bytes[i]);
}
}
void print_pkt_eth(const pkt_eth * eth) {
int i = 0;
fprintf(stdout,"Ethernet Layer \n");
fprintf(stdout,"\tSource:\t%s",ether_ntoa(ð->src));
//fprintf(stdout,"%s",to_addr(eth->src,ETH_ADDR_SIZE));
fprintf(stdout,"\n\tDest:\t%s",ether_ntoa(ð->dest));
if(ntohs(eth->type) == ETHERTYPE_IP)
fprintf(stdout,"\n\tType:\t IPv4");
else if(ntohs(eth->type) == ETHERTYPE_ARP)
fprintf(stdout,"\n\tType:\t ARP");
printf("\n");
}
void print_pkt_arp(const pkt_arp * arp) {
int op = 0;
int i = 0;
printf("ARP Layer \n");
printf("\tHardware type:\t%02X\n",ntohs(arp->htype));
printf("\tProtocol type:\t%04X\n",ntohs(arp->ptype));
op = ntohs(arp->opcode);
printf("\tOperation code:\t");
op == 1 ? printf("request\n") : printf("reply\n");
printf("\tHardware sender:\t%s",ether_ntoa(&arp->hard_addr_send));
printf("\n\tSoftware sender:\t");
printf("%s",inet_ntoa(arp->proto_addr_send));
printf("\n\tHardware destination:\t%s",ether_ntoa(&arp->hard_addr_dest));
printf("\n\tSoftware destination:\t");
printf("%s",inet_ntoa(arp->proto_addr_dest));
printf("\n");
}
void print_pkt_ip(const pkt_ip * ip) {
int i = 0;
printf("IPv4 Layer \n");
printf("\tProtocol code:\t%d\n",ip->proto);
printf("\tIP source:\t");
printf("%s",inet_ntoa(ip->addr_src));
printf("\n\tIP Destination:\t");
printf("%s",inet_ntoa(ip->addr_dest));
printf("\n");
}
void print_packet(const Packet * p) {
struct pkt_eth * eth = (struct pkt_eth *) p;
struct pkt_arp * arp = (struct pkt_arp *) (p + ETH_SIZE);
print_pkt_eth(eth);
if (ntohs(eth->type) == ETHERTYPE_ARP) {
print_pkt_arp(arp);
}
}