I removed the ncp code from packet-ipx.c and created packet-ncp.c. Now that
[obnox/wireshark/wip.git] / packet.c
1 /* packet.c
2  * Routines for packet disassembly
3  *
4  * $Id: packet.c,v 1.2 1998/09/16 03:22:13 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
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <ctype.h>
35
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
38 #endif
39
40 #include "packet.h"
41 #include "ethereal.h"
42 #include "etypes.h"
43 #include "file.h"
44
45 extern GtkWidget    *byte_view;
46 extern GdkFont      *m_r_font, *m_b_font;
47 extern capture_file  cf;
48
49 gchar *
50 ether_to_str(guint8 *ad) {
51   static gchar  str[3][18];
52   static gchar *cur;
53
54   if (cur == &str[0][0]) {
55     cur = &str[1][0];
56   } else if (cur == &str[1][0]) {  
57     cur = &str[2][0];
58   } else {  
59     cur = &str[0][0];
60   }
61   sprintf(cur, "%02x:%02x:%02x:%02x:%02x:%02x", ad[0], ad[1], ad[2],
62     ad[3], ad[4], ad[5]);
63   return cur;
64 }
65
66 gchar *
67 ip_to_str(guint8 *ad) {
68   static gchar  str[3][16];
69   static gchar *cur;
70
71   if (cur == &str[0][0]) {
72     cur = &str[1][0];
73   } else if (cur == &str[1][0]) {  
74     cur = &str[2][0];
75   } else {  
76     cur = &str[0][0];
77   }
78   sprintf(cur, "%d.%d.%d.%d", ad[0], ad[1], ad[2], ad[3]);
79   return cur;
80 }
81
82 void
83 packet_hex_print(GtkText *bv, guchar *pd, gint len, gint bstart, gint blen) {
84   gint     i = 0, j, k, cur;
85   gchar    line[128], hexchars[] = "0123456789abcdef";
86   GdkFont *cur_font, *new_font;
87   
88   while (i < len) {
89     /* Print the line number */
90     sprintf(line, "%04x  ", i);
91     gtk_text_insert(bv, m_r_font, NULL, NULL, line, -1);
92     /* Do we start in bold? */
93     cur_font = (i >= bstart && i < (bstart + blen)) ? m_b_font : m_r_font;
94     j   = i;
95     k   = i + BYTE_VIEW_WIDTH;
96     cur = 0;
97     /* Print the hex bit */
98     while (i < k) {
99       if (i < len) {
100         line[cur++] = hexchars[(pd[i] & 0xf0) >> 4];
101         line[cur++] = hexchars[pd[i] & 0x0f];
102       } else {
103         line[cur++] = ' '; line[cur++] = ' ';
104       }
105       line[cur++] = ' ';
106       i++;
107       /* Did we cross a bold/plain boundary? */
108       new_font = (i >= bstart && i < (bstart + blen)) ? m_b_font : m_r_font;
109       if (cur_font != new_font) {
110         gtk_text_insert(bv, cur_font, NULL, NULL, line, cur);
111         cur_font = new_font;
112         cur = 0;
113       }
114     }
115     line[cur++] = ' ';
116     gtk_text_insert(bv, cur_font, NULL, NULL, line, cur);
117     cur = 0;
118     i = j;
119     /* Print the ASCII bit */
120     cur_font = (i >= bstart && i < (bstart + blen)) ? m_b_font : m_r_font;
121     while (i < k) {
122       if (i < len) {
123         line[cur++] = (isgraph(pd[i])) ? pd[i] : '.';
124       } else {
125         line[cur++] = ' ';
126       }
127       i++;
128       /* Did we cross a bold/plain boundary? */
129       new_font = (i >= bstart && i < (bstart + blen)) ? m_b_font : m_r_font;
130       if (cur_font != new_font) {
131         gtk_text_insert(bv, cur_font, NULL, NULL, line, cur);
132         cur_font = new_font;
133         cur = 0;
134       }
135     }
136     line[cur++] = '\n';
137     line[cur]   = '\0';
138     gtk_text_insert(bv, cur_font, NULL, NULL, line, -1);
139   }
140 }
141
142 GtkWidget *
143 add_item_to_tree(GtkWidget *tree, gint start, gint len,
144   gchar *format, ...) {
145   GtkWidget *ti;
146   va_list    ap;
147   gchar      label_str[256];
148   guint32    t_info;
149   
150   /* This limits us to a max packet size of 65535 bytes. */
151   /* Are there any systems out there with < 32-bit pointers? */
152   /* To do: use gtk_object_set_data instead, now that I know it exists. */
153   t_info = ((start & 0xffff) << 16) | (len & 0xffff);
154   va_start(ap, format);
155   vsnprintf(label_str, 256, format, ap);
156   ti = gtk_tree_item_new_with_label(label_str);
157   gtk_object_set_user_data(GTK_OBJECT(ti), (gpointer) t_info);
158   gtk_tree_append(GTK_TREE(tree), ti);
159   gtk_widget_show(ti);
160
161   return ti;
162 }
163
164 void
165 add_subtree(GtkWidget *ti, GtkWidget *subtree, gint idx) {
166   static gint tree_type[NUM_TREE_TYPES];
167
168   gtk_tree_item_set_subtree(GTK_TREE_ITEM(ti), subtree);
169   if (tree_type[idx])
170     gtk_tree_item_expand(GTK_TREE_ITEM(ti));
171   gtk_signal_connect(GTK_OBJECT(ti), "expand", (GtkSignalFunc) expand_tree,
172     (gpointer) &tree_type[idx]);
173   gtk_signal_connect(GTK_OBJECT(ti), "collapse", (GtkSignalFunc) collapse_tree,
174     (gpointer) &tree_type[idx]);
175 }
176
177 void
178 expand_tree(GtkWidget *w, gpointer data) {
179   gint *val = (gint *) data;
180   *val = 1;
181 }
182
183 void
184 collapse_tree(GtkWidget *w, gpointer data) {
185   gint *val = (gint *) data;
186   *val = 0;
187 }
188
189 /* decodes the protocol start and length thare are encoded into
190         the t_info field in add_item_to_tree. */
191 void
192 decode_start_len(GtkTreeItem *ti, gint *pstart, gint *plen)
193 {
194         guint32         t_info;
195         int                     start, len;
196
197         t_info = (guint32) gtk_object_get_user_data(GTK_OBJECT(ti));
198         *pstart = t_info >> 16;
199         *plen = t_info & 0xffff;
200 }
201
202
203 /* this routine checks the frame type from the cf structure */
204 void
205 dissect_packet(const u_char *pd, frame_data *fd, GtkTree *tree) {
206
207         switch (cf.lnk_t) {
208                 case DLT_EN10MB :
209                         dissect_eth(pd, fd, tree);
210                         break;
211                 case DLT_IEEE802 :
212                         dissect_tr(pd, fd, tree);
213                         break;
214                 case DLT_RAW :
215                 dissect_raw(pd, fd, tree);
216                         break;
217         }
218 }