Tvbuffify the STP dissector, have it register itself and have the LLC
[metze/wireshark/wip.git] / packet-sip.c
1 /* packet-sip.c
2  * Routines for the Session Initiation Protocol (SIP) dissection.
3  * RFC 2543
4  * 
5  * TODO: Pay attention to Content-Type: It might not always be SDP.
6  *       Add hf_* fields for filtering support.
7  *
8  * Copyright 2000, Heikki Vatiainen <hessu@cs.tut.fi>
9  *
10  * $Id: packet-sip.c,v 1.6 2000/11/16 07:35:38 guy Exp $
11  *
12  * Ethereal - Network traffic analyzer
13  * By Gerald Combs <gerald@zing.org>
14  * Copyright 1998 Gerald Combs
15  *
16  * Copied from packet-cops.c
17  * 
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License
20  * as published by the Free Software Foundation; either version 2
21  * of the License, or (at your option) any later version.
22  * 
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  * 
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
31  */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include <glib.h>
42 #include "packet.h"
43
44 #define TCP_PORT_SIP 5060
45 #define UDP_PORT_SIP 5060
46
47 /* Initialize the protocol and registered fields */
48 static gint proto_sip = -1;
49 static gint hf_msg_hdr = -1;
50
51 /* Initialize the subtree pointers */
52 static gint ett_sip = -1;
53 static gint ett_sip_hdr = -1;
54
55 static const char *sip_methods[] = {
56         "<Invalid method>",      /* Pad so that the real methods start at index 1 */
57         "INVITE",
58         "ACK",
59         "OPTIONS",
60         "BYE",
61         "CANCEL",
62         "REGISTER"
63 };
64
65 static int sip_is_request(tvbuff_t *tvb, guint32 offset);
66 static gint sip_get_msg_offset(tvbuff_t *tvb, guint32 offset);
67  
68 static dissector_handle_t sdp_handle;
69
70 /* Code to actually dissect the packets */
71 static void dissect_sip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
72 {
73         guint32 offset;
74         gint eol, next_offset, msg_offset;
75         tvbuff_t *next_tvb;
76         gboolean is_request;
77
78         CHECK_DISPLAY_AS_DATA(proto_sip, tvb, pinfo, tree);
79
80         pinfo->current_proto = "SIP";
81         if (check_col(pinfo->fd, COL_PROTOCOL)) 
82                 col_add_str(pinfo->fd, COL_PROTOCOL, "SIP");
83     
84         offset = 0;
85         is_request = sip_is_request(tvb, 0);
86         eol = tvb_find_line_end(tvb, 0, -1, &next_offset);
87
88         if (check_col(pinfo->fd, COL_INFO))
89                 col_add_fstr(pinfo->fd, COL_INFO, "%s: %s",
90                              is_request ? "Request" : "Status",
91                              is_request ? 
92                              tvb_format_text(tvb, 0, eol - strlen(" SIP/2.0")) :
93                              tvb_format_text(tvb, strlen("SIP/2.0 "), eol - strlen("SIP/2.0 ")));
94
95         if (tree) {
96                 proto_item *ti, *th;
97                 proto_tree *sip_tree, *hdr_tree;
98
99                 ti = proto_tree_add_item(tree, proto_sip, tvb, 0, tvb_length(tvb), FALSE);
100                 sip_tree = proto_item_add_subtree(ti, ett_sip);
101
102                 proto_tree_add_text(sip_tree, tvb, 0, next_offset, "%s-Line: %s",
103                                     is_request ? "Request" : "Status",
104                                     tvb_format_text(tvb, 0, eol));
105
106                 offset = next_offset;
107                 msg_offset = sip_get_msg_offset(tvb, offset);
108                 if (msg_offset < 0) goto bad;
109                 th = proto_tree_add_item(sip_tree, hf_msg_hdr, tvb, offset, msg_offset - offset, FALSE);
110                 hdr_tree = proto_item_add_subtree(th, ett_sip_hdr);
111
112                 /* - 2 since we have a CRLF separating the message-body */
113                 while (msg_offset - 2 > offset) {
114                         eol = tvb_find_line_end(tvb, offset, -1, &next_offset);
115                         proto_tree_add_text(hdr_tree, tvb, offset, next_offset - offset, "%s",
116                                             tvb_format_text(tvb, offset, eol));
117                         offset = next_offset;
118                 }
119                 offset += 2;  /* Skip the CRLF mentioned above */
120        }
121
122         if (tvb_length_remaining(tvb, offset) > 0) {
123                 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
124                 call_dissector(sdp_handle, next_tvb, pinfo, tree);
125         }
126
127         return;
128
129   bad:
130         next_tvb = tvb_new_subset(tvb, offset, -1, -1);
131         dissect_data(next_tvb, 0, pinfo, tree);
132
133         return;
134 }
135
136 /* Returns the offset to the start of the optional message-body, or
137  * -1 for an error.
138  */
139 static gint sip_get_msg_offset(tvbuff_t *tvb, guint32 offset)
140 {
141         gint eol;
142
143         while ((eol = tvb_find_guint8(tvb, offset, tvb_length_remaining(tvb, offset), '\r')) > 0) {
144                         if (tvb_get_guint8(tvb, eol + 1) == '\n' && 
145                             tvb_get_guint8(tvb, eol + 2) == '\r' && 
146                             tvb_get_guint8(tvb, eol + 3) == '\n')
147                                 return eol + 4;
148                         offset = eol + 2;
149         }
150
151         return -1;
152 }
153                 
154 static int sip_is_request(tvbuff_t *tvb, guint32 offset)
155 {
156         int i;
157
158         for (i = 1; i < array_length(sip_methods); i++) {
159                 if (tvb_strneql(tvb, offset, sip_methods[i], strlen(sip_methods[i])) == 0)
160                         return i;
161         }
162
163         return 0;
164 }
165
166 /* Register the protocol with Ethereal */
167 void proto_register_sip(void)
168 {                 
169
170         /* Setup list of header fields */
171         static hf_register_info hf[] = {
172
173                 { &hf_msg_hdr,
174                         { "Message Header",           "sip.msg_hdr",
175                         FT_NONE, 0, NULL, 0,
176                         "Message Header in SIP message" }
177                 },
178         };
179
180         /* Setup protocol subtree array */
181         static gint *ett[] = {
182                 &ett_sip,
183                 &ett_sip_hdr,
184         };
185
186         /* Register the protocol name and description */
187         proto_sip = proto_register_protocol("Session Initiation Protocol", "sip");
188
189         /* Required function calls to register the header fields and subtrees used */
190         proto_register_field_array(proto_sip, hf, array_length(hf));
191         proto_register_subtree_array(ett, array_length(ett));
192 };
193
194 void
195 proto_reg_handoff_sip(void)
196 {
197         dissector_add("tcp.port", TCP_PORT_SIP, dissect_sip);
198         dissector_add("udp.port", UDP_PORT_SIP, dissect_sip);
199
200         /*
201          * Get a handle for the SDP dissector.
202          */
203         sdp_handle = find_dissector("sdp");
204 }