4f9dedf389d4e8400f046d8f3a981e099bab2fcc
[obnox/wireshark/wip.git] / epan / dissectors / packet-git.c
1 /* packet-git.c
2  * Routines for git packet dissection
3  * RFC 1939
4  * Copyright 2010, Jelmer Vernooij <jelmer@samba.org>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * Copied from packet-pop.c
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #include <glib.h>
37 #include <epan/packet.h>
38 #include <epan/strutil.h>
39 #include <epan/prefs.h>
40 #include "packet-tcp.h"
41
42 static int proto_git = -1;
43
44 static gint ett_git = -1;
45
46 static gint hf_git_packet_len = -1;
47 static gint hf_git_packet_data = -1;
48 static gint hf_git_packet_terminator = -1;
49
50 #define TCP_PORT_GIT                    9418
51
52 /* desegmentation of Git over TCP */
53 static gboolean git_desegment = TRUE;
54
55 static gboolean tvb_get_packet_length(tvbuff_t *tvb, int offset,
56                                                                           guint16 *length)
57 {
58         guint8 *lenstr;
59
60         lenstr = tvb_get_ephemeral_string(tvb, offset, 4);
61
62         return (sscanf(lenstr, "%hx", length) == 1);
63 }
64
65 static guint
66 get_git_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
67 {
68         guint16 plen;
69
70         if (!tvb_get_packet_length(tvb, offset, &plen))
71                 return 0; /* No idea what this is */
72
73         if (plen == 0) {
74                 /* Terminator packet */
75                 return 4;
76         } else {
77                 return plen;
78         }
79 }
80
81 static void
82 dissect_git_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
83 {
84   proto_tree             *git_tree;
85   proto_item             *ti;
86   int offset = 0;
87   guint16 plen;
88
89   col_set_str(pinfo->cinfo, COL_PROTOCOL, "GIT");
90
91   col_set_str(pinfo->cinfo, COL_INFO, "Git Smart Protocol");
92
93   ti = proto_tree_add_item(tree, proto_git, tvb, offset, -1, FALSE);
94   git_tree = proto_item_add_subtree(ti, ett_git);
95
96   if (!tvb_get_packet_length(tvb, 0, &plen))
97           return;
98
99   if (plen == 0) {
100           proto_tree_add_uint(git_tree, hf_git_packet_terminator, tvb, offset,
101                                                                 4, plen);
102           return;
103   }
104
105   if (git_tree)
106   {
107           proto_tree_add_uint(git_tree, hf_git_packet_len, tvb, offset,
108                                                                 4, plen);
109
110           proto_tree_add_item(git_tree, hf_git_packet_data, tvb, offset+4, 
111                                                                 plen-4, ENC_NA);
112         }
113 }
114
115 static void
116 dissect_git(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
117 {
118         tcp_dissect_pdus(tvb, pinfo, tree, git_desegment, 4, get_git_pdu_len,
119                          dissect_git_pdu);
120 }
121
122 void
123 proto_register_git(void)
124 {
125   static hf_register_info hf[] = {
126         { &hf_git_packet_len,
127                 { "Packet length", "git.length", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
128         },
129         { &hf_git_packet_data,
130                 { "Packet data", "git.data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL },
131         },
132         { &hf_git_packet_terminator,
133                 { "Terminator packet", "git.terminator", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
134         },
135   };
136
137   static gint *ett[] = {
138     &ett_git,
139   };
140
141   module_t *git_module;
142   proto_git = proto_register_protocol("Git Smart Protocol", "GIT", "git");
143   register_dissector("git", dissect_git, proto_git);
144   proto_register_field_array(proto_git, hf, array_length(hf));
145   proto_register_subtree_array(ett, array_length(ett));
146
147   git_module = prefs_register_protocol(proto_git, NULL);
148
149   prefs_register_bool_preference(git_module, "desegment",
150                                  "Reassemble GIT messages spanning multiple TCP segments",
151                                  "Whether the GIT dissector should reassemble messages spanning multiple TCP segments."
152                                  " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
153                                  &git_desegment);
154 }
155
156 void
157 proto_reg_handoff_git(void)
158 {
159   dissector_handle_t git_handle;
160
161   git_handle = find_dissector("git");
162   dissector_add_uint("tcp.port", TCP_PORT_GIT, git_handle);
163 }