Use "%u", not "%d", to print unsigned integral quantities.
[obnox/wireshark/wip.git] / packet-raw.c
1 /* packet-raw.c
2  * Routines for raw packet disassembly
3  *
4  * $Id: packet-raw.c,v 1.18 2000/08/13 08:53:51 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  *
9  * This file created and by Mike Hall <mlh@io.com>
10  * Copyright 1998
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <glib.h>
36 #include "packet.h"
37 #include "packet-raw.h"
38 #include "packet-ip.h"
39 #include "packet-ppp.h"
40
41 static gint ett_raw = -1;
42
43 static const char zeroes[10];
44
45 void
46 capture_raw(const u_char *pd, packet_counts *ld)
47 {
48   /* So far, the only time we get raw connection types are with Linux and
49    * Irix PPP connections.  We can't tell what type of data is coming down
50    * the line, so our safest bet is IP. - GCC
51    */
52    
53   /* Currently, the Linux 2.1.xxx PPP driver passes back some of the header
54    * sometimes.  This check should be removed when 2.2 is out.
55    */
56   if (BYTES_ARE_IN_FRAME(0,2) && pd[0] == 0xff && pd[1] == 0x03) {
57     capture_ppp(pd, 0, ld);
58   }
59   /* The Linux ISDN driver sends a fake MAC address before the PPP header
60    * on its ippp interfaces... */
61   else if (BYTES_ARE_IN_FRAME(0,8) && pd[6] == 0xff && pd[7] == 0x03) {
62     capture_ppp(pd, 6, ld);
63   }
64   /* ...except when it just puts out one byte before the PPP header... */
65   else if (BYTES_ARE_IN_FRAME(0,3) && pd[1] == 0xff && pd[2] == 0x03) {
66     capture_ppp(pd, 1, ld);
67   }
68   /* ...and if the connection is currently down, it sends 10 bytes of zeroes
69    * instead of a fake MAC address and PPP header. */
70   else if (BYTES_ARE_IN_FRAME(0,10) && memcmp(pd, zeroes, 10) == 0) {
71     capture_ip(pd, 10, ld);
72   }
73   else {
74     capture_ip(pd, 0, ld);
75   }
76 }
77
78 void
79 dissect_raw(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
80 {
81   proto_tree    *fh_tree;
82   proto_item    *ti;
83   tvbuff_t      *next_tvb;
84   const guint8  *next_pd;
85   int           next_offset;
86
87   /* load the top pane info. This should be overwritten by
88      the next protocol in the stack */
89   if(check_col(pinfo->fd, COL_RES_DL_SRC))
90     col_add_str(pinfo->fd, COL_RES_DL_SRC, "N/A" );
91   if(check_col(pinfo->fd, COL_RES_DL_DST))
92     col_add_str(pinfo->fd, COL_RES_DL_DST, "N/A" );
93   if(check_col(pinfo->fd, COL_PROTOCOL))
94     col_add_str(pinfo->fd, COL_PROTOCOL, "N/A" );
95   if(check_col(pinfo->fd, COL_INFO))
96     col_add_str(pinfo->fd, COL_INFO, "Raw packet data" );
97
98   /* populate a tree in the second pane with the status of the link
99      layer (ie none) */
100   if (tree) {
101     ti = proto_tree_add_text(tree, tvb, 0, 0, "Raw packet data" );
102     fh_tree = proto_item_add_subtree(ti, ett_raw);
103     proto_tree_add_text(fh_tree, tvb, 0, 0, "No link information available");
104   }
105
106   /* So far, the only time we get raw connection types are with Linux and
107    * Irix PPP connections.  We can't tell what type of data is coming down
108    * the line, so our safest bet is IP. - GCC
109    */
110    
111   /* Currently, the Linux 2.1.xxx PPP driver passes back some of the header
112    * sometimes.  This check should be removed when 2.2 is out.
113    */
114   if (tvb_get_ntohs(tvb, 0) == 0xff03) {
115         dissect_ppp(tvb, pinfo, tree);
116         return;
117   }
118   /* The Linux ISDN driver sends a fake MAC address before the PPP header
119    * on its ippp interfaces... */
120   else if (tvb_get_ntohs(tvb, 6) == 0xff03) {
121         next_tvb = tvb_new_subset(tvb, 6, -1, -1);
122         dissect_ppp(next_tvb, pinfo, tree);
123         return;
124   }
125   /* ...except when it just puts out one byte before the PPP header... */
126   else if (tvb_get_ntohs(tvb, 1) == 0xff03) {
127         next_tvb = tvb_new_subset(tvb, 1, -1, -1);
128         dissect_ppp(next_tvb, pinfo, tree);
129         return;
130   }
131   /* ...and if the connection is currently down, it sends 10 bytes of zeroes
132    * instead of a fake MAC address and PPP header. */
133   else if (memcmp(tvb_get_ptr(tvb, 0, 10), zeroes, 10) == 0) {
134         tvb_compat(tvb, &next_pd, &next_offset);
135         dissect_ip(next_pd, next_offset + 10, pinfo->fd, tree);
136         return;
137   }
138   else {
139         tvb_compat(tvb, &next_pd, &next_offset);
140         dissect_ip(next_pd, next_offset, pinfo->fd, tree);
141         return;
142   }
143   g_assert_not_reached();
144 }
145
146 void
147 proto_register_raw(void)
148 {
149   static gint *ett[] = {
150     &ett_raw,
151   };
152
153   proto_register_subtree_array(ett, array_length(ett));
154 }