2 * Routines for packet disassembly
4 * $Id: packet.c,v 1.2 1998/09/16 03:22:13 gerald Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
45 extern GtkWidget *byte_view;
46 extern GdkFont *m_r_font, *m_b_font;
47 extern capture_file cf;
50 ether_to_str(guint8 *ad) {
51 static gchar str[3][18];
54 if (cur == &str[0][0]) {
56 } else if (cur == &str[1][0]) {
61 sprintf(cur, "%02x:%02x:%02x:%02x:%02x:%02x", ad[0], ad[1], ad[2],
67 ip_to_str(guint8 *ad) {
68 static gchar str[3][16];
71 if (cur == &str[0][0]) {
73 } else if (cur == &str[1][0]) {
78 sprintf(cur, "%d.%d.%d.%d", ad[0], ad[1], ad[2], ad[3]);
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;
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;
95 k = i + BYTE_VIEW_WIDTH;
97 /* Print the hex bit */
100 line[cur++] = hexchars[(pd[i] & 0xf0) >> 4];
101 line[cur++] = hexchars[pd[i] & 0x0f];
103 line[cur++] = ' '; line[cur++] = ' ';
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);
116 gtk_text_insert(bv, cur_font, NULL, NULL, line, cur);
119 /* Print the ASCII bit */
120 cur_font = (i >= bstart && i < (bstart + blen)) ? m_b_font : m_r_font;
123 line[cur++] = (isgraph(pd[i])) ? pd[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);
138 gtk_text_insert(bv, cur_font, NULL, NULL, line, -1);
143 add_item_to_tree(GtkWidget *tree, gint start, gint len,
144 gchar *format, ...) {
147 gchar label_str[256];
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);
165 add_subtree(GtkWidget *ti, GtkWidget *subtree, gint idx) {
166 static gint tree_type[NUM_TREE_TYPES];
168 gtk_tree_item_set_subtree(GTK_TREE_ITEM(ti), subtree);
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]);
178 expand_tree(GtkWidget *w, gpointer data) {
179 gint *val = (gint *) data;
184 collapse_tree(GtkWidget *w, gpointer data) {
185 gint *val = (gint *) data;
189 /* decodes the protocol start and length thare are encoded into
190 the t_info field in add_item_to_tree. */
192 decode_start_len(GtkTreeItem *ti, gint *pstart, gint *plen)
197 t_info = (guint32) gtk_object_get_user_data(GTK_OBJECT(ti));
198 *pstart = t_info >> 16;
199 *plen = t_info & 0xffff;
203 /* this routine checks the frame type from the cf structure */
205 dissect_packet(const u_char *pd, frame_data *fd, GtkTree *tree) {
209 dissect_eth(pd, fd, tree);
212 dissect_tr(pd, fd, tree);
215 dissect_raw(pd, fd, tree);