2 * Routines for the Session Initiation Protocol (SIP) dissection.
5 * TODO: Pay attention to Content-Type: It might not always be SDP.
6 * Add hf_* fields for filtering support.
7 * Add sip msg body dissection based on Content-Type for:
8 * SDP, MIME, and other types
9 * Align SIP methods with recent Internet Drafts or RFC
10 * (SIP INFO, rfc2976 - done)
11 * (SIP SUBSCRIBE-NOTIFY - done)
15 * Copyright 2000, Heikki Vatiainen <hessu@cs.tut.fi>
16 * Copyright 2001, Jean-Francois Mule <jfm@clarent.com>
18 * $Id: packet-sip.c,v 1.23 2002/02/02 02:56:19 guy Exp $
20 * Ethereal - Network traffic analyzer
21 * By Gerald Combs <gerald@ethereal.com>
22 * Copyright 1998 Gerald Combs
24 * Copied from packet-cops.c
26 * This program is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU General Public License
28 * as published by the Free Software Foundation; either version 2
29 * of the License, or (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
50 #include <epan/packet.h>
52 #define TCP_PORT_SIP 5060
53 #define UDP_PORT_SIP 5060
55 /* Initialize the protocol and registered fields */
56 static gint proto_sip = -1;
57 static gint hf_msg_hdr = -1;
59 /* Initialize the subtree pointers */
60 static gint ett_sip = -1;
61 static gint ett_sip_hdr = -1;
63 static const char *sip_methods[] = {
64 "<Invalid method>", /* Pad so that the real methods start at index 1 */
77 static gboolean sip_is_request(tvbuff_t *tvb, guint32 offset);
78 static gint sip_get_msg_offset(tvbuff_t *tvb, guint32 offset);
80 static dissector_handle_t sdp_handle;
81 static dissector_handle_t data_handle;
83 #define SIP2_HDR "SIP/2.0 "
84 #define SIP2_HDR_LEN (strlen (SIP2_HDR))
86 /* Code to actually dissect the packets */
87 static void dissect_sip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
90 gint eol, next_offset, msg_offset;
95 * Note that "tvb_strneql()" doesn't throw exceptions, so
96 * "sip_is_request()" won't throw an exception.
98 * Note that "tvb_find_line_end()" will return a value that
99 * is not longer than what's in the buffer, so the
100 * "tvb_get_ptr()" call s below won't throw exceptions.
103 eol = tvb_find_line_end(tvb, 0, -1, &next_offset);
104 is_request = sip_is_request(tvb, 0);
105 /* XXX - Is this case-sensitive? RFC 2543 didn't explicitly say. */
106 if (tvb_strneql(tvb, 0, SIP2_HDR, SIP2_HDR_LEN) != 0 && ! is_request)
109 if (check_col(pinfo->cinfo, COL_PROTOCOL))
110 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SIP");
113 if (check_col(pinfo->cinfo, COL_INFO))
114 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
115 is_request ? "Request" : "Status",
117 tvb_format_text(tvb, 0, eol - SIP2_HDR_LEN) :
118 tvb_format_text(tvb, SIP2_HDR_LEN, eol - SIP2_HDR_LEN));
120 msg_offset = sip_get_msg_offset(tvb, offset);
121 if (msg_offset < 0) {
123 * XXX - this may just mean that the entire SIP message
124 * didn't fit in this TCP segment.
131 proto_tree *sip_tree, *hdr_tree;
133 ti = proto_tree_add_item(tree, proto_sip, tvb, 0, -1, FALSE);
134 sip_tree = proto_item_add_subtree(ti, ett_sip);
136 proto_tree_add_text(sip_tree, tvb, 0, next_offset, "%s-Line: %s",
137 is_request ? "Request" : "Status",
138 tvb_format_text(tvb, 0, eol));
140 offset = next_offset;
141 th = proto_tree_add_item(sip_tree, hf_msg_hdr, tvb, offset, msg_offset - offset, FALSE);
142 hdr_tree = proto_item_add_subtree(th, ett_sip_hdr);
144 /* - 2 since we have a CRLF separating the message-body */
145 while (msg_offset - 2 > (int) offset) {
146 eol = tvb_find_line_end(tvb, offset, -1, &next_offset);
147 proto_tree_add_text(hdr_tree, tvb, offset, next_offset - offset, "%s",
148 tvb_format_text(tvb, offset, eol));
149 offset = next_offset;
151 offset += 2; /* Skip the CRLF mentioned above */
154 if (tvb_offset_exists(tvb, msg_offset)) {
155 next_tvb = tvb_new_subset(tvb, msg_offset, -1, -1);
156 call_dissector(sdp_handle, next_tvb, pinfo, tree);
162 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
163 call_dissector(data_handle,next_tvb, pinfo, tree);
168 /* Returns the offset to the start of the optional message-body, or
171 static gint sip_get_msg_offset(tvbuff_t *tvb, guint32 offset)
175 while ((eol = tvb_find_guint8(tvb, offset, -1, '\r')) > 0
176 && tvb_bytes_exist(tvb, eol, 4)) {
177 if (tvb_get_guint8(tvb, eol + 1) == '\n' &&
178 tvb_get_guint8(tvb, eol + 2) == '\r' &&
179 tvb_get_guint8(tvb, eol + 3) == '\n')
187 static gboolean sip_is_request(tvbuff_t *tvb, guint32 offset)
191 for (i = 1; i < array_length(sip_methods); i++) {
192 if (tvb_strneql(tvb, offset, sip_methods[i], strlen(sip_methods[i])) == 0)
199 /* Register the protocol with Ethereal */
200 void proto_register_sip(void)
203 /* Setup list of header fields */
204 static hf_register_info hf[] = {
207 { "Message Header", "sip.msg_hdr",
209 "Message Header in SIP message", HFILL }
213 /* Setup protocol subtree array */
214 static gint *ett[] = {
219 /* Register the protocol name and description */
220 proto_sip = proto_register_protocol("Session Initiation Protocol",
223 /* Required function calls to register the header fields and subtrees used */
224 proto_register_field_array(proto_sip, hf, array_length(hf));
225 proto_register_subtree_array(ett, array_length(ett));
229 proto_reg_handoff_sip(void)
231 dissector_handle_t sip_handle;
233 sip_handle = create_dissector_handle(dissect_sip, proto_sip);
234 dissector_add("tcp.port", TCP_PORT_SIP, sip_handle);
235 dissector_add("udp.port", UDP_PORT_SIP, sip_handle);
238 * Get a handle for the SDP dissector.
240 sdp_handle = find_dissector("sdp");
241 data_handle = find_dissector("data");