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