-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmt.c
102 lines (92 loc) · 1.71 KB
/
fmt.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
#include <stdio.h>
#include <net/ethernet.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "ethdump.h"
void
displayip4addr(uint32_t ip)
{
int i;
for (i = 3; i >= 0; i--) {
printf("%d", (ip >> 8 * i) & 0x000000ff);
if (i != 0)
printf(".");
}
}
void
displayip(struct iphdr *iph)
{
struct protoent *proto;
proto = getprotobynumber(iph->protocol);
printf("\t%s\t", proto->p_name);
displayip4addr(ntohl(iph->saddr));
printf("\t");
displayip4addr(ntohl(iph->daddr));
}
void
displaymac(uint8_t addr[ETH_ALEN])
{
int i;
for (i = 0; i < ETH_ALEN; i++) {
printf("%x", addr[i]);
if (i < ETH_ALEN-1)
printf(":");
}
}
void
displaytype(struct packet *p)
{
int t = p->eh->ether_type;
switch (ntohs(t)) {
case ETHERTYPE_PUP:
printf("Xerox PUP");
break;
case ETHERTYPE_SPRITE:
printf("Sprite");
break;
case ETHERTYPE_IP:
printf("IP");
break;
case ETHERTYPE_ARP:
printf("Address resolution");
break;
case ETHERTYPE_REVARP:
printf("Reverse ARP");
break;
case ETHERTYPE_AT:
printf("AppleTalk protocol");
break;
case ETHERTYPE_AARP:
printf("AppleTalk ARP");
break;
case ETHERTYPE_VLAN:
printf("IEEE 802.1Q VLAN tagging");
break;
case ETHERTYPE_IPX:
printf("IPX");
break;
case ETHERTYPE_IPV6:
printf("IP protocol version 6");
break;
case ETHERTYPE_LOOPBACK:
printf("used to test interfaces");
break;
}
printf("\t%x\t", ntohs(t));
if (ntohs(t) == ETHERTYPE_IP) {
displayip(p->iph);
}
}
// Display the packet according to a specification.
void
displaypacket(struct packet *p)
{
displaymac(p->eh->ether_shost);
printf("\t");
displaymac(p->eh->ether_dhost);
printf("\t%d\t", p->len);
displaytype(p);
printf("\n");
}