Initial revision
[obnox/wireshark/wip.git] / packet.c
1 /* packet.c
2  * Routines for packet disassembly
3  *
4  * Ethereal - Network traffic analyzer
5  * By Gerald Combs <gerald@zing.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * 
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <gtk/gtk.h>
29
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <ctype.h>
33
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
36 #endif
37
38 #include "packet.h"
39 #include "ethereal.h"
40 #include "etypes.h"
41 #include "file.h"
42
43 extern GtkWidget    *byte_view;
44 extern GdkFont      *m_r_font, *m_b_font;
45 extern capture_file  cf;
46
47 gchar *
48 ether_to_str(guint8 *ad) {
49   static gchar  str[3][18];
50   static gchar *cur;
51
52   if (cur == &str[0][0]) {
53     cur = &str[1][0];
54   } else if (cur == &str[1][0]) {  
55     cur = &str[2][0];
56   } else {  
57     cur = &str[0][0];
58   }
59   sprintf(cur, "%02x:%02x:%02x:%02x:%02x:%02x", ad[0], ad[1], ad[2],
60     ad[3], ad[4], ad[5]);
61   return cur;
62 }
63
64 gchar *
65 ip_to_str(guint8 *ad) {
66   static gchar  str[3][16];
67   static gchar *cur;
68
69   if (cur == &str[0][0]) {
70     cur = &str[1][0];
71   } else if (cur == &str[1][0]) {  
72     cur = &str[2][0];
73   } else {  
74     cur = &str[0][0];
75   }
76   sprintf(cur, "%d.%d.%d.%d", ad[0], ad[1], ad[2], ad[3]);
77   return cur;
78 }
79
80 void
81 packet_hex_print(GtkText *bv, guchar *pd, gint len, gint bstart, gint blen) {
82   gint     i = 0, j, k, cur;
83   gchar    line[128], hexchars[] = "0123456789abcdef";
84   GdkFont *cur_font, *new_font;
85   
86   while (i < len) {
87     /* Print the line number */
88     sprintf(line, "%04x  ", i);
89     gtk_text_insert(bv, m_r_font, NULL, NULL, line, -1);
90     /* Do we start in bold? */
91     cur_font = (i >= bstart && i < (bstart + blen)) ? m_b_font : m_r_font;
92     j   = i;
93     k   = i + BYTE_VIEW_WIDTH;
94     cur = 0;
95     /* Print the hex bit */
96     while (i < k) {
97       if (i < len) {
98         line[cur++] = hexchars[(pd[i] & 0xf0) >> 4];
99         line[cur++] = hexchars[pd[i] & 0x0f];
100       } else {
101         line[cur++] = ' '; line[cur++] = ' ';
102       }
103       line[cur++] = ' ';
104       i++;
105       /* Did we cross a bold/plain boundary? */
106       new_font = (i >= bstart && i < (bstart + blen)) ? m_b_font : m_r_font;
107       if (cur_font != new_font) {
108         gtk_text_insert(bv, cur_font, NULL, NULL, line, cur);
109         cur_font = new_font;
110         cur = 0;
111       }
112     }
113     line[cur++] = ' ';
114     gtk_text_insert(bv, cur_font, NULL, NULL, line, cur);
115     cur = 0;
116     i = j;
117     /* Print the ASCII bit */
118     cur_font = (i >= bstart && i < (bstart + blen)) ? m_b_font : m_r_font;
119     while (i < k) {
120       if (i < len) {
121         line[cur++] = (isgraph(pd[i])) ? pd[i] : '.';
122       } else {
123         line[cur++] = ' ';
124       }
125       i++;
126       /* Did we cross a bold/plain boundary? */
127       new_font = (i >= bstart && i < (bstart + blen)) ? m_b_font : m_r_font;
128       if (cur_font != new_font) {
129         gtk_text_insert(bv, cur_font, NULL, NULL, line, cur);
130         cur_font = new_font;
131         cur = 0;
132       }
133     }
134     line[cur++] = '\n';
135     line[cur]   = '\0';
136     gtk_text_insert(bv, cur_font, NULL, NULL, line, -1);
137   }
138 }
139
140 GtkWidget *
141 add_item_to_tree(GtkWidget *tree, gint start, gint len,
142   gchar *format, ...) {
143   GtkWidget *ti;
144   va_list    ap;
145   gchar      label_str[256];
146   guint32    t_info;
147   
148   /* This limits us to a max packet size of 65535 bytes. */
149   /* Are there any systems out there with < 32-bit pointers? */
150   /* To do: use gtk_object_set_data instead, now that I know it exists. */
151   t_info = ((start & 0xffff) << 16) | (len & 0xffff);
152   va_start(ap, format);
153   vsnprintf(label_str, 256, format, ap);
154   ti = gtk_tree_item_new_with_label(label_str);
155   gtk_object_set_user_data(GTK_OBJECT(ti), (gpointer) t_info);
156   gtk_tree_append(GTK_TREE(tree), ti);
157   gtk_widget_show(ti);
158
159   return ti;
160 }
161
162 void
163 add_subtree(GtkWidget *ti, GtkWidget *subtree, gint idx) {
164   static gint tree_type[NUM_TREE_TYPES];
165
166   gtk_tree_item_set_subtree(GTK_TREE_ITEM(ti), subtree);
167   if (tree_type[idx])
168     gtk_tree_item_expand(GTK_TREE_ITEM(ti));
169   gtk_signal_connect(GTK_OBJECT(ti), "expand", (GtkSignalFunc) expand_tree,
170     (gpointer) &tree_type[idx]);
171   gtk_signal_connect(GTK_OBJECT(ti), "collapse", (GtkSignalFunc) collapse_tree,
172     (gpointer) &tree_type[idx]);
173 }
174
175 void
176 expand_tree(GtkWidget *w, gpointer data) {
177   gint *val = (gint *) data;
178   *val = 1;
179 }
180
181 void
182 collapse_tree(GtkWidget *w, gpointer data) {
183   gint *val = (gint *) data;
184   *val = 0;
185 }
186
187 /* decodes the protocol start and length thare are encoded into
188         the t_info field in add_item_to_tree. */
189 void
190 decode_start_len(GtkTreeItem *ti, gint *pstart, gint *plen)
191 {
192         guint32         t_info;
193         int                     start, len;
194
195         t_info = (guint32) gtk_object_get_user_data(GTK_OBJECT(ti));
196         *pstart = t_info >> 16;
197         *plen = t_info & 0xffff;
198 }
199
200
201 /* this routine checks the frame type from the cf structure */
202 void
203 dissect_packet(const u_char *pd, frame_data *fd, GtkTree *tree) {
204
205         switch (cf.lnk_t) {
206                 case DLT_EN10MB :
207                         dissect_eth(pd, fd, tree);
208                         break;
209                 case DLT_IEEE802 :
210                         dissect_tr(pd, fd, tree);
211                         break;
212                 case DLT_RAW :
213                 dissect_raw(pd, fd, tree);
214                         break;
215         }
216 }