packet-git.c says it was copied from packet-pop, but that doesn't mean
[metze/wireshark/wip.git] / epan / dissectors / packet-git.c
1 /* packet-git.c
2  * Routines for git packet dissection
3  * Copyright 2010, Jelmer Vernooij <jelmer@samba.org>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * Copied from packet-pop.c
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28 #include <stdio.h>
29
30 #include <epan/packet.h>
31 #include <epan/prefs.h>
32 #include "packet-tcp.h"
33
34 void proto_register_git(void);
35 void proto_reg_handoff_git(void);
36
37 static dissector_handle_t git_handle;
38
39 static int proto_git = -1;
40
41 static gint ett_git = -1;
42
43 static gint hf_git_packet_len = -1;
44 static gint hf_git_packet_data = -1;
45 static gint hf_git_packet_terminator = -1;
46
47 #define TCP_PORT_GIT    9418
48
49 /* desegmentation of Git over TCP */
50 static gboolean git_desegment = TRUE;
51
52 static gboolean get_packet_length(tvbuff_t *tvb, int offset,
53                                   guint16 *length)
54 {
55   guint8 *lenstr;
56
57   lenstr = tvb_get_string_enc(wmem_packet_scope(), tvb, offset, 4, ENC_ASCII);
58
59   return (sscanf(lenstr, "%hx", length) == 1);
60 }
61
62 static guint
63 get_git_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
64 {
65   guint16 plen;
66
67   if (!get_packet_length(tvb, offset, &plen))
68     return 0; /* No idea what this is */
69
70   if (plen == 0) {
71     /* Terminator packet */
72     return 4;
73   }
74
75   return plen;
76 }
77
78 static int
79 dissect_git_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
80 {
81   proto_tree             *git_tree;
82   proto_item             *ti;
83   int offset = 0;
84   guint16 plen;
85
86   col_set_str(pinfo->cinfo, COL_PROTOCOL, "GIT");
87
88   col_set_str(pinfo->cinfo, COL_INFO, "Git Smart Protocol");
89
90   ti = proto_tree_add_item(tree, proto_git, tvb, offset, -1, ENC_NA);
91   git_tree = proto_item_add_subtree(ti, ett_git);
92
93   if (!get_packet_length(tvb, 0, &plen))
94     return 0;
95
96   if (plen == 0) {
97     proto_tree_add_uint(git_tree, hf_git_packet_terminator, tvb, offset,
98                         4, plen);
99     return 4;
100   }
101
102   if (git_tree)
103   {
104     proto_tree_add_uint(git_tree, hf_git_packet_len, tvb, offset,
105                         4, plen);
106
107     proto_tree_add_item(git_tree, hf_git_packet_data, tvb, offset+4,
108                         plen-4, ENC_NA);
109   }
110
111   return tvb_length(tvb);
112 }
113
114 static int
115 dissect_git(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
116 {
117   tcp_dissect_pdus(tvb, pinfo, tree, git_desegment, 4, get_git_pdu_len,
118                    dissect_git_pdu, data);
119   return tvb_length(tvb);
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
143   proto_git = proto_register_protocol("Git Smart Protocol", "GIT", "git");
144   proto_register_field_array(proto_git, hf, array_length(hf));
145   proto_register_subtree_array(ett, array_length(ett));
146
147   git_handle = new_register_dissector("git", dissect_git, proto_git);
148
149   git_module = prefs_register_protocol(proto_git, NULL);
150
151   prefs_register_bool_preference(git_module, "desegment",
152                                  "Reassemble GIT messages spanning multiple TCP segments",
153                                  "Whether the GIT dissector should reassemble messages spanning multiple TCP segments."
154                                  " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
155                                  &git_desegment);
156 }
157
158 void
159 proto_reg_handoff_git(void)
160 {
161   dissector_add_uint("tcp.port", TCP_PORT_GIT, git_handle);
162 }
163
164 /*
165  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
166  *
167  * Local Variables:
168  * c-basic-offset: 2
169  * tab-width: 8
170  * indent-tabs-mode: nil
171  * End:
172  *
173  * ex: set shiftwidth=2 tabstop=8 expandtab:
174  * :indentSize=2:tabSize=8:noTabs=true:
175  */