* OSPF alignment fixes (Gerald)
[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.4 1998/10/10 03:32:10 gerald 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   guint16      ar_hrd, ar_pro, ar_op;
51   GtkWidget   *arp_tree, *ti;
52   gchar       *op_str;
53   value_string op_vals[] = { ARPOP_REQUEST,  "ARP request",
54                              ARPOP_REPLY,    "ARP reply",
55                              ARPOP_RREQUEST, "RARP request",
56                              ARPOP_RREPLY,   "RARP reply" };
57
58   /* To do: Check for {cap len,pkt len} < struct len */
59   ea = (e_ether_arp *) &pd[offset];
60   ar_hrd = ntohs(ea->ar_hrd);
61   ar_pro = ntohs(ea->ar_pro);
62   /* To do: Check for bounds on ar_op */
63   ar_op  = ntohs(ea->ar_op);
64   
65   if (fd->win_info[COL_NUM]) { strcpy(fd->win_info[COL_PROTOCOL], "ARP"); }
66   
67   if (tree) {
68     if (op_str = match_strval(ar_op, op_vals, 4))
69       ti = add_item_to_tree(GTK_WIDGET(tree), offset, 28, op_str);
70     else
71       ti = add_item_to_tree(GTK_WIDGET(tree), offset, 28,
72         "Unkown ARP (opcode 0x%04x)", ar_op);
73     arp_tree = gtk_tree_new();
74     add_subtree(ti, arp_tree, ETT_ARP);
75     add_item_to_tree(arp_tree, offset,      2,
76       "Hardware type: 0x%04x", ar_hrd);
77     add_item_to_tree(arp_tree, offset +  2, 2,
78       "Protocol type: 0x%04x", ar_pro);
79     add_item_to_tree(arp_tree, offset +  4, 1,
80       "Hardware size: 0x%02x", ea->ar_hln);
81     add_item_to_tree(arp_tree, offset +  5, 1,
82       "Protocol size: 0x%02x", ea->ar_pln);
83     add_item_to_tree(arp_tree, offset +  6, 2,
84       "Opcode: 0x%04x (%s)", ar_op, op_str ? op_str : "Unknown");
85     add_item_to_tree(arp_tree, offset +  8, 6,
86       "Sender ether: %s", ether_to_str((guint8 *) ea->arp_sha));
87     add_item_to_tree(arp_tree, offset + 14, 4,
88       "Sender IP: %s", ip_to_str((guint8 *) ea->arp_spa));
89     add_item_to_tree(arp_tree, offset + 18, 6,
90       "Target ether: %s", ether_to_str((guint8 *) ea->arp_tha));
91     add_item_to_tree(arp_tree, offset + 24, 4,
92       "Target IP: %s", ip_to_str((guint8 *) ea->arp_tpa));
93   }
94
95   if (ar_pro != ETHERTYPE_IP && fd->win_info[COL_NUM]) {
96     sprintf(fd->win_info[COL_INFO], "h/w %d (%d) prot %d (%d) op 0x%04x",
97       ar_hrd, ea->ar_hln, ar_pro, ea->ar_pln, ar_op);
98     return;
99   }
100   switch (ar_op) {
101     case ARPOP_REQUEST:
102       if (fd->win_info[COL_NUM]) {
103         sprintf(fd->win_info[COL_INFO], "Who has %s?  Tell %s",
104           ip_to_str((guint8 *) ea->arp_tpa), ip_to_str((guint8 *) ea->arp_spa));
105       }
106       break;
107     case ARPOP_REPLY:
108       if (fd->win_info[COL_NUM]) {
109         sprintf(fd->win_info[COL_INFO], "%s is at %s",
110           ip_to_str((guint8 *) ea->arp_spa),
111           ether_to_str((guint8 *) ea->arp_sha));
112       }
113       break;
114     case ARPOP_RREQUEST:
115       if (fd->win_info[COL_NUM]) {
116         strcpy(fd->win_info[COL_PROTOCOL], "RARP");
117         sprintf(fd->win_info[COL_INFO], "Who is %s?  Tell %s",
118           ether_to_str((guint8 *) ea->arp_tha), 
119           ether_to_str((guint8 *) ea->arp_sha));
120       }
121       break;
122     case ARPOP_RREPLY:
123       if (fd->win_info[COL_NUM]) {
124         strcpy(fd->win_info[COL_PROTOCOL], "RARP");
125         sprintf(fd->win_info[COL_INFO], "%s is at %s",
126           ether_to_str((guint8 *) ea->arp_sha),
127           ip_to_str((guint8 *) ea->arp_spa));
128       }
129       break;
130   }
131 }