Slightly better invalid packet handling.
[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.13 2001/01/30 02:38:33 gerald 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 #define SIP2_HDR "SIP/2.0 "
71 #define SIP2_HDR_LEN (strlen (SIP2_HDR))
72
73 /* Code to actually dissect the packets */
74 static void dissect_sip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
75 {
76         guint32 offset;
77         gint eol, next_offset, msg_offset;
78         tvbuff_t *next_tvb;
79         gboolean is_request;
80
81         /*
82          * Note that "tvb_strneql()" doesn't throw exceptions, so
83          * "sip_is_request()" won't throw an exception.
84          *
85          * Note that "tvb_find_line_end()" will return a value that
86          * is not longer than what's in the buffer, so the
87          * "tvb_get_ptr()" call s below won't throw exceptions.
88          */
89         offset = 0;
90         eol = tvb_find_line_end(tvb, 0, -1, &next_offset);
91         is_request = sip_is_request(tvb, 0);
92         /* XXX - Is this case-sensitive?  RFC 2543 didn't explicitly say. */
93         if (tvb_strneql(tvb, 0, SIP2_HDR, SIP2_HDR_LEN) != 0 && ! is_request)
94                 goto bad;
95           
96         if (check_col(pinfo->fd, COL_PROTOCOL)) 
97                 col_set_str(pinfo->fd, COL_PROTOCOL, "SIP");
98     
99
100         if (check_col(pinfo->fd, COL_INFO))
101                 col_add_fstr(pinfo->fd, COL_INFO, "%s: %s",
102                              is_request ? "Request" : "Status",
103                              is_request ? 
104                              tvb_format_text(tvb, 0, eol - SIP2_HDR_LEN) :
105                              tvb_format_text(tvb, SIP2_HDR_LEN, eol - SIP2_HDR_LEN));
106
107         msg_offset = sip_get_msg_offset(tvb, offset);
108         if (msg_offset < 0) goto bad;
109
110         if (tree) {
111                 proto_item *ti, *th;
112                 proto_tree *sip_tree, *hdr_tree;
113
114                 ti = proto_tree_add_item(tree, proto_sip, tvb, 0, tvb_length(tvb), FALSE);
115                 sip_tree = proto_item_add_subtree(ti, ett_sip);
116
117                 proto_tree_add_text(sip_tree, tvb, 0, next_offset, "%s-Line: %s",
118                                     is_request ? "Request" : "Status",
119                                     tvb_format_text(tvb, 0, eol));
120
121                 offset = next_offset;
122                 th = proto_tree_add_item(sip_tree, hf_msg_hdr, tvb, offset, msg_offset - offset, FALSE);
123                 hdr_tree = proto_item_add_subtree(th, ett_sip_hdr);
124
125                 /* - 2 since we have a CRLF separating the message-body */
126                 while (msg_offset - 2 > offset) {
127                         eol = tvb_find_line_end(tvb, offset, -1, &next_offset);
128                         proto_tree_add_text(hdr_tree, tvb, offset, next_offset - offset, "%s",
129                                             tvb_format_text(tvb, offset, eol));
130                         offset = next_offset;
131                 }
132                 offset += 2;  /* Skip the CRLF mentioned above */
133        }
134
135         if (tvb_length_remaining(tvb, msg_offset) > 0) {
136                 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
137                 call_dissector(sdp_handle, next_tvb, pinfo, tree);
138         }
139
140         return;
141
142   bad:
143         next_tvb = tvb_new_subset(tvb, offset, -1, -1);
144         dissect_data(next_tvb, 0, pinfo, tree);
145
146         return;
147 }
148
149 /* Returns the offset to the start of the optional message-body, or
150  * -1 for an error.
151  */
152 static gint sip_get_msg_offset(tvbuff_t *tvb, guint32 offset)
153 {
154         gint eol;
155
156         while ((eol = tvb_find_guint8(tvb, offset, tvb_length_remaining(tvb, offset), '\r')) > 0) {
157                         if (tvb_get_guint8(tvb, eol + 1) == '\n' && 
158                             tvb_get_guint8(tvb, eol + 2) == '\r' && 
159                             tvb_get_guint8(tvb, eol + 3) == '\n')
160                                 return eol + 4;
161                         offset = eol + 2;
162         }
163
164         return -1;
165 }
166                 
167 static int sip_is_request(tvbuff_t *tvb, guint32 offset)
168 {
169         int i;
170
171         for (i = 1; i < array_length(sip_methods); i++) {
172                 if (tvb_strneql(tvb, offset, sip_methods[i], strlen(sip_methods[i])) == 0)
173                         return i;
174         }
175
176         return 0;
177 }
178
179 /* Register the protocol with Ethereal */
180 void proto_register_sip(void)
181 {                 
182
183         /* Setup list of header fields */
184         static hf_register_info hf[] = {
185
186                 { &hf_msg_hdr,
187                         { "Message Header",           "sip.msg_hdr",
188                         FT_NONE, 0, NULL, 0,
189                         "Message Header in SIP message" }
190                 },
191         };
192
193         /* Setup protocol subtree array */
194         static gint *ett[] = {
195                 &ett_sip,
196                 &ett_sip_hdr,
197         };
198
199         /* Register the protocol name and description */
200         proto_sip = proto_register_protocol("Session Initiation Protocol",
201             "SIP", "sip");
202
203         /* Required function calls to register the header fields and subtrees used */
204         proto_register_field_array(proto_sip, hf, array_length(hf));
205         proto_register_subtree_array(ett, array_length(ett));
206 };
207
208 void
209 proto_reg_handoff_sip(void)
210 {
211         dissector_add("tcp.port", TCP_PORT_SIP, dissect_sip, proto_sip);
212         dissector_add("udp.port", UDP_PORT_SIP, dissect_sip, proto_sip);
213
214         /*
215          * Get a handle for the SDP dissector.
216          */
217         sdp_handle = find_dissector("sdp");
218 }