source: libpacketdump/eth_2054.c @ a82a9e8

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivendag_formatrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since a82a9e8 was ee6e802, checked in by Shane Alcock <salcock@…>, 5 years ago

Updated copyright blurb on all source files

In some cases, this meant adding copyright blurbs to files that
had never had them before.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 *
3 * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand.
4 * All rights reserved.
5 *
6 * This file is part of libtrace.
7 *
8 * This code has been developed by the University of Waikato WAND
9 * research group. For further information please see http://www.wand.net.nz/
10 *
11 * libtrace is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * libtrace is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 *
25 */
26/* ARP */
27#include <stdio.h>
28#include <inttypes.h>
29#include <dlfcn.h>
30#include "libpacketdump.h"
31#include <sys/socket.h>
32#ifndef WIN32
33        #include <netinet/in_systm.h>
34#endif
35#include <netinet/in.h>
36#include <netinet/ip.h>
37#include <net/if_arp.h>
38#include <arpa/inet.h>
39#include <string.h>
40
41/*
42 * Converts an ARP hardware address to a printable string.
43 * Takes an ARP header structure and a pointer to the start
44 * of the hardware address in the structure that we should
45 * attempt to decode.
46 */
47static char *format_hrd(const struct arphdr *arp, const char *hrd) {
48        static char buffer[1024] = {0,};
49        int i;
50
51        if (!hrd) {
52                strncpy(buffer, "(Truncated)", sizeof(buffer));
53                return buffer;
54        }
55
56        switch(ntohs(arp->ar_hrd)) {
57                case ARPHRD_ETHER:
58                        trace_ether_ntoa((const unsigned char *)hrd, buffer);
59                        break;
60                default:
61                        for (i=0;i<arp->ar_hln;i++) {
62                                snprintf(buffer,sizeof(buffer),"%s %02x",
63                                                buffer,(unsigned char)hrd[i]);
64                        }
65                        break;
66        }
67       
68        return buffer;
69}
70
71/*
72 * Converts an ARP protocol address to a printable string.
73 * Takes an ARP header structure and a pointer to the start
74 * of the protocol address in the structure that we should
75 * attempt to decode.
76 */
77static char *format_pro(const struct arphdr *arp, const char *pro) {
78        static char buffer[1024] = {0,};
79        int i;
80       
81        if (!pro) {
82                strncpy(buffer, "(Truncated)", sizeof(buffer));
83                return buffer;
84        }
85
86        switch(ntohs(arp->ar_pro)) {
87                case 0x0800:
88                        snprintf(buffer,sizeof(buffer),"%s",
89                                        inet_ntoa(*(struct in_addr*)pro));
90                        break;
91                default:
92                        snprintf(buffer, sizeof(buffer), "%s", " (");
93                        for (i=0;i<arp->ar_pln;i++) {
94                                snprintf(buffer,sizeof(buffer),"%s %02x",
95                                                buffer,(unsigned char)pro[i]);
96                        }
97                        strncat(buffer,")",sizeof(buffer) - strlen(buffer) - 1);
98                        break;
99        }
100        return buffer;
101       
102}
103       
104DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len)
105{
106        struct arphdr *arp = (struct arphdr*)packet;
107        const char *source_hrd = NULL;
108        const char *source_pro = NULL;
109        const char *dest_hrd = NULL;
110        const char *dest_pro = NULL;
111
112        if (len < sizeof(struct arphdr)) {
113                printf(" ARP: (Truncated)\n");
114                return;
115        }
116
117        if (len >= sizeof(struct arphdr) + arp->ar_hln) 
118                source_hrd = packet + sizeof(struct arphdr);
119        if (len >= sizeof(struct arphdr) + arp->ar_hln + arp->ar_pln)
120                source_pro = source_hrd + arp->ar_hln;
121        if (len >= sizeof(struct arphdr) + arp->ar_hln * 2 + arp->ar_pln)
122                dest_hrd = source_pro + arp->ar_pln;
123        if (len >= sizeof(struct arphdr) + arp->ar_hln * 2 + arp->ar_pln * 2)
124                dest_pro = dest_hrd + arp->ar_hln;
125
126        switch(ntohs(arp->ar_op)) {
127                case ARPOP_REQUEST:
128                        printf(" ARP: who-has %s", format_pro(arp, dest_pro));
129                        printf(" tell %s (%s)\n", format_pro(arp, source_pro),
130                                        format_hrd(arp, source_hrd));
131                        break;
132                case ARPOP_REPLY:
133                        printf(" ARP: reply %s", format_pro(arp, source_pro));
134                        printf(" is-at %s\n", format_hrd(arp, source_hrd));
135                        break;
136                default:
137                        printf(" ARP: Unknown opcode (%i) from %s to %s\n",
138                                        ntohs(arp->ar_op),
139                                        format_pro(arp, source_pro),
140                                        format_pro(arp, dest_pro));
141
142                        break;
143        }
144        return;
145}
Note: See TracBrowser for help on using the repository browser.