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