1 | /* ARP */ |
---|
2 | #include <stdio.h> |
---|
3 | #include <inttypes.h> |
---|
4 | #include <dlfcn.h> |
---|
5 | #include "libpacketdump.h" |
---|
6 | #include <sys/socket.h> |
---|
7 | #ifndef WIN32 |
---|
8 | #include <netinet/in_systm.h> |
---|
9 | #endif |
---|
10 | #include <netinet/in.h> |
---|
11 | #include <netinet/ip.h> |
---|
12 | #include <net/if_arp.h> |
---|
13 | #include <arpa/inet.h> |
---|
14 | #include <string.h> |
---|
15 | |
---|
16 | /* |
---|
17 | * Converts an ARP hardware address to a printable string. |
---|
18 | * Takes an ARP header structure and a pointer to the start |
---|
19 | * of the hardware address in the structure that we should |
---|
20 | * attempt to decode. |
---|
21 | */ |
---|
22 | static char *format_hrd(const struct arphdr *arp, const char *hrd) { |
---|
23 | static char buffer[1024] = {0,}; |
---|
24 | int i; |
---|
25 | |
---|
26 | if (!hrd) { |
---|
27 | strncpy(buffer, "(Truncated)", sizeof(buffer)); |
---|
28 | return buffer; |
---|
29 | } |
---|
30 | |
---|
31 | switch(ntohs(arp->ar_hrd)) { |
---|
32 | case ARPHRD_ETHER: |
---|
33 | trace_ether_ntoa((const unsigned char *)hrd, buffer); |
---|
34 | break; |
---|
35 | default: |
---|
36 | for (i=0;i<arp->ar_hln;i++) { |
---|
37 | snprintf(buffer,sizeof(buffer),"%s %02x", |
---|
38 | buffer,(unsigned char)hrd[i]); |
---|
39 | } |
---|
40 | break; |
---|
41 | } |
---|
42 | |
---|
43 | return buffer; |
---|
44 | } |
---|
45 | |
---|
46 | /* |
---|
47 | * Converts an ARP protocol address to a printable string. |
---|
48 | * Takes an ARP header structure and a pointer to the start |
---|
49 | * of the protocol address in the structure that we should |
---|
50 | * attempt to decode. |
---|
51 | */ |
---|
52 | static char *format_pro(const struct arphdr *arp, const char *pro) { |
---|
53 | static char buffer[1024] = {0,}; |
---|
54 | int i; |
---|
55 | |
---|
56 | if (!pro) { |
---|
57 | strncpy(buffer, "(Truncated)", sizeof(buffer)); |
---|
58 | return buffer; |
---|
59 | } |
---|
60 | |
---|
61 | switch(ntohs(arp->ar_pro)) { |
---|
62 | case 0x0800: |
---|
63 | snprintf(buffer,sizeof(buffer),"%s", |
---|
64 | inet_ntoa(*(struct in_addr*)pro)); |
---|
65 | break; |
---|
66 | default: |
---|
67 | snprintf(buffer, sizeof(buffer), "%s", " ("); |
---|
68 | for (i=0;i<arp->ar_pln;i++) { |
---|
69 | snprintf(buffer,sizeof(buffer),"%s %02x", |
---|
70 | buffer,(unsigned char)pro[i]); |
---|
71 | } |
---|
72 | strncat(buffer,")",sizeof(buffer) - strlen(buffer) - 1); |
---|
73 | break; |
---|
74 | } |
---|
75 | return buffer; |
---|
76 | |
---|
77 | } |
---|
78 | |
---|
79 | DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len) |
---|
80 | { |
---|
81 | struct arphdr *arp = (struct arphdr*)packet; |
---|
82 | const char *source_hrd = NULL; |
---|
83 | const char *source_pro = NULL; |
---|
84 | const char *dest_hrd = NULL; |
---|
85 | const char *dest_pro = NULL; |
---|
86 | |
---|
87 | if (len < sizeof(struct arphdr)) { |
---|
88 | printf(" ARP: (Truncated)\n"); |
---|
89 | return; |
---|
90 | } |
---|
91 | |
---|
92 | if (len >= sizeof(struct arphdr) + arp->ar_hln) |
---|
93 | source_hrd = packet + sizeof(struct arphdr); |
---|
94 | if (len >= sizeof(struct arphdr) + arp->ar_hln + arp->ar_pln) |
---|
95 | source_pro = source_hrd + arp->ar_hln; |
---|
96 | if (len >= sizeof(struct arphdr) + arp->ar_hln * 2 + arp->ar_pln) |
---|
97 | dest_hrd = source_pro + arp->ar_pln; |
---|
98 | if (len >= sizeof(struct arphdr) + arp->ar_hln * 2 + arp->ar_pln * 2) |
---|
99 | dest_pro = dest_hrd + arp->ar_hln; |
---|
100 | |
---|
101 | switch(ntohs(arp->ar_op)) { |
---|
102 | case ARPOP_REQUEST: |
---|
103 | printf(" ARP: who-has %s", format_pro(arp, dest_pro)); |
---|
104 | printf(" tell %s (%s)\n", format_pro(arp, source_pro), |
---|
105 | format_hrd(arp, source_hrd)); |
---|
106 | break; |
---|
107 | case ARPOP_REPLY: |
---|
108 | printf(" ARP: reply %s", format_pro(arp, source_pro)); |
---|
109 | printf(" is-at %s\n", format_hrd(arp, source_hrd)); |
---|
110 | break; |
---|
111 | default: |
---|
112 | printf(" ARP: Unknown opcode (%i) from %s to %s\n", |
---|
113 | ntohs(arp->ar_op), |
---|
114 | format_pro(arp, source_pro), |
---|
115 | format_pro(arp, dest_pro)); |
---|
116 | |
---|
117 | break; |
---|
118 | } |
---|
119 | return; |
---|
120 | } |
---|