Make the "value_string" array in "dissect_arp()" static, so it can be
[obnox/wireshark/wip.git] / packet-arp.c
1 /* packet-arp.c
2  * Routines for ARP packet disassembly
3  *
4  * $Id: packet-arp.c,v 1.6 1998/10/13 05:20:53 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program 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 General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <gtk/gtk.h>
31 #include <pcap.h>
32
33 #include <stdio.h>
34
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
37 #endif
38
39 #ifdef HAVE_NETINET_IN_H
40 # include <netinet/in.h>
41 #endif
42
43 #include "ethereal.h"
44 #include "packet.h"
45 #include "etypes.h"
46
47 void
48 dissect_arp(const u_char *pd, int offset, frame_data *fd, GtkTree *tree) {
49   e_ether_arp  ea;
50   GtkWidget   *arp_tree, *ti;
51   gchar       *op_str;
52   static value_string op_vals[] = { {ARPOP_REQUEST,  "ARP request" },
53                                     {ARPOP_REPLY,    "ARP reply"   },
54                                     {ARPOP_RREQUEST, "RARP request"},
55                                     {ARPOP_RREPLY,   "RARP reply"  } };
56 #define N_OP_VALS       (sizeof op_vals / sizeof op_vals[0])
57
58   /* To do: Check for {cap len,pkt len} < struct len */
59   ea.ar_hrd = pntohs(&pd[offset]);
60   ea.ar_pro = pntohs(&pd[offset + 2]);
61   ea.ar_hln = (guint8) pd[offset + 4];
62   ea.ar_pln = (guint8) pd[offset + 5];
63   ea.ar_op  = pntohs(&pd[offset + 6]);
64   memcpy(&ea.arp_sha, &pd[offset +  8], 6);
65   memcpy(&ea.arp_spa, &pd[offset + 14], 4);
66   memcpy(&ea.arp_tha, &pd[offset + 18], 6);
67   memcpy(&ea.arp_tpa, &pd[offset + 24], 4);
68   
69   if (fd->win_info[COL_NUM]) { strcpy(fd->win_info[COL_PROTOCOL], "ARP"); }
70   
71   if (tree) {
72     if ((op_str = match_strval(ea.ar_op, op_vals, N_OP_VALS)))
73       ti = add_item_to_tree(GTK_WIDGET(tree), offset, 28, op_str);
74     else
75       ti = add_item_to_tree(GTK_WIDGET(tree), offset, 28,
76         "Unknown ARP (opcode 0x%04x)", ea.ar_op);
77     arp_tree = gtk_tree_new();
78     add_subtree(ti, arp_tree, ETT_ARP);
79     add_item_to_tree(arp_tree, offset,      2,
80       "Hardware type: 0x%04x", ea.ar_hrd);
81     add_item_to_tree(arp_tree, offset +  2, 2,
82       "Protocol type: 0x%04x", ea.ar_pro);
83     add_item_to_tree(arp_tree, offset +  4, 1,
84       "Hardware size: 0x%02x", ea.ar_hln);
85     add_item_to_tree(arp_tree, offset +  5, 1,
86       "Protocol size: 0x%02x", ea.ar_pln);
87     add_item_to_tree(arp_tree, offset +  6, 2,
88       "Opcode: 0x%04x (%s)", ea.ar_op, op_str ? op_str : "Unknown");
89     add_item_to_tree(arp_tree, offset +  8, 6,
90       "Sender ether: %s", ether_to_str((guint8 *) ea.arp_sha));
91     add_item_to_tree(arp_tree, offset + 14, 4,
92       "Sender IP: %s", ip_to_str((guint8 *) ea.arp_spa));
93     add_item_to_tree(arp_tree, offset + 18, 6,
94       "Target ether: %s", ether_to_str((guint8 *) ea.arp_tha));
95     add_item_to_tree(arp_tree, offset + 24, 4,
96       "Target IP: %s", ip_to_str((guint8 *) ea.arp_tpa));
97   }
98
99   if (ea.ar_pro != ETHERTYPE_IP && fd->win_info[COL_NUM]) {
100     sprintf(fd->win_info[COL_INFO], "h/w %d (%d) prot %d (%d) op 0x%04x",
101       ea.ar_hrd, ea.ar_hln, ea.ar_pro, ea.ar_pln, ea.ar_op);
102     return;
103   }
104   switch (ea.ar_op) {
105     case ARPOP_REQUEST:
106       if (fd->win_info[COL_NUM]) {
107         sprintf(fd->win_info[COL_INFO], "Who has %s?  Tell %s",
108           ip_to_str((guint8 *) ea.arp_tpa), ip_to_str((guint8 *) ea.arp_spa));
109       }
110       break;
111     case ARPOP_REPLY:
112       if (fd->win_info[COL_NUM]) {
113         sprintf(fd->win_info[COL_INFO], "%s is at %s",
114           ip_to_str((guint8 *) ea.arp_spa),
115           ether_to_str((guint8 *) ea.arp_sha));
116       }
117       break;
118     case ARPOP_RREQUEST:
119       if (fd->win_info[COL_NUM]) {
120         strcpy(fd->win_info[COL_PROTOCOL], "RARP");
121         sprintf(fd->win_info[COL_INFO], "Who is %s?  Tell %s",
122           ether_to_str((guint8 *) ea.arp_tha), 
123           ether_to_str((guint8 *) ea.arp_sha));
124       }
125       break;
126     case ARPOP_RREPLY:
127       if (fd->win_info[COL_NUM]) {
128         strcpy(fd->win_info[COL_PROTOCOL], "RARP");
129         sprintf(fd->win_info[COL_INFO], "%s is at %s",
130           ether_to_str((guint8 *) ea.arp_sha),
131           ip_to_str((guint8 *) ea.arp_spa));
132       }
133       break;
134   }
135 }