summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-git.c
blob: a9644ac1e6b7a76847fa8bacc796b2fd4ba5a6f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* packet-git.c
 * Routines for git packet dissection
 * Copyright 2010, Jelmer Vernooij <jelmer@samba.org>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Copied from packet-pop.c
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * Current testing suite 'case_dissect_git' can be found at
 * test/suite_dissection.py
 */

#include "config.h"

#include <stdio.h>    /* for sscanf() */

#include <epan/packet.h>
#include <epan/expert.h>
#include <epan/prefs.h>
#include "packet-tcp.h"

void proto_register_git(void);
void proto_reg_handoff_git(void);

static dissector_handle_t git_handle;

static int proto_git = -1;
static expert_field ei_git_bad_pkt_len = EI_INIT;
static expert_field ei_git_malformed = EI_INIT;

static gint ett_git = -1;

static gint hf_git_protocol_version = -1;
static gint hf_git_packet_type = -1;
static gint hf_git_packet_len = -1;
static gint hf_git_packet_data = -1;
static gint hf_git_sideband_control_code = -1;
static gint hf_git_upload_pack_adv = -1;
static gint hf_git_upload_pack_req = -1;
static gint hf_git_upload_pack_res = -1;

#define PNAME  "Git Smart Protocol"
#define PSNAME "Git"
#define PFNAME "git"

#define TCP_PORT_GIT    9418

static const value_string packet_type_vals[] = {
  { 0, "Flush" },
  { 1, "Delimiter" },
  { 2, "Response end" },
  { 0, NULL }
};

static const value_string version_vals[] = {
  { '1', "Git protocol version 1" },
  { '2', "Git protocol version 2" },
  { 0, NULL }
};

#define SIDEBAND_PACKFILE_DATA 0x01
#define SIDEBAND_PROGRESS_INFO 0x02
#define SIDEBAND_ERROR_INFO 0x03
static const value_string sideband_vals[] = {
  { SIDEBAND_PACKFILE_DATA, "Git packfile data" },
  { SIDEBAND_PROGRESS_INFO, "Git progress data" },
  { SIDEBAND_ERROR_INFO, "Git error data" },
  { 0, NULL }
};

/* desegmentation of Git over TCP */
static gboolean git_desegment = TRUE;

static gboolean get_packet_length(tvbuff_t *tvb, int offset,
                                  guint16 *length)
{
  guint8 *lenstr;

  lenstr = tvb_get_string_enc(wmem_packet_scope(), tvb, offset, 4, ENC_ASCII);

  return (sscanf(lenstr, "%hx", length) == 1);
}

static guint
get_git_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset, void *data _U_)
{
  guint16 plen;

  if (!get_packet_length(tvb, offset, &plen))
    return 0; /* No idea what this is */

  return plen < 4
      ? 4   // Special packet (e.g., flush-pkt)
      : plen;
}

/* Parse pkt-lines one-by-one
 *
 * @param  tvb       The buffer to dissect.
 * @param  pinfo     Packet info associated to this buffer.
 * @param  git_tree  The git protocol subtree.
 * @param  offset    The offset at which to start the dissection.
 * @return bool      After successful/unsuccessful parsing.
 *
 * This new helper takes the contents of the tvbuffer, updates the
 * offset, and returns to the caller for subsequent processing of the
 * remainder of the data.
*/
static gboolean
dissect_pkt_line(tvbuff_t *tvb, packet_info *pinfo, proto_tree *git_tree,
                 int *offset)
{
  guint16 plen;

  // what type of pkt-line is it?
  if (!get_packet_length(tvb, *offset, &plen))
    return FALSE;
  if (plen < 4) {   // a special packet (e.g., flush-pkt)
    proto_item *ti =
        proto_tree_add_uint(git_tree, hf_git_packet_type, tvb,
                            *offset, 4, plen);
    *offset += 4;

    if (!try_val_to_str(plen, packet_type_vals))
      expert_add_info(pinfo, ti, &ei_git_bad_pkt_len);
    return TRUE;
  }

  proto_tree_add_uint(git_tree, hf_git_packet_len, tvb, *offset, 4, plen);
  *offset += 4;
  plen -= 4;

  /*
   * Parse out the version of the Git Protocol
   *
   * The initial server response contains the version of the Git Protocol in use;
   * 1 or 2. Parsing out this information helps identify the capabilities and
   * information that can be used with the protocol.
  */
  if (plen >= 9 && !tvb_strneql(tvb, *offset, "version ", 8)) {
    proto_tree_add_item(git_tree, hf_git_protocol_version, tvb, *offset + 8,
                        1, ENC_NA);
  }

  /*
   * Parse out the sideband control code.
   *
   * Not all pkt-lines have a sideband control code. With more context from the rest of
   * the request or response, we would be able to tell whether sideband is expected here;
   * lacking that, let's assume for now that all pkt-lines starting with \1, \2, or \3
   * are using sideband.
   */
  int sideband_code = tvb_get_guint8(tvb, *offset);

  if (1 <= sideband_code && sideband_code <= 3) {
    proto_tree_add_uint(git_tree, hf_git_sideband_control_code, tvb, *offset, 1,
                        sideband_code);
    (*offset)++;
    plen--;
  }

  proto_tree_add_item(git_tree, hf_git_packet_data, tvb, *offset, plen, ENC_NA);
  *offset += plen;
  return TRUE;
}

static int
dissect_git_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
  proto_tree             *git_tree;
  proto_item             *ti;
  int offset = 0;

  col_set_str(pinfo->cinfo, COL_PROTOCOL, PSNAME);

  col_set_str(pinfo->cinfo, COL_INFO, PNAME);

  ti = proto_tree_add_item(tree, proto_git, tvb, offset, -1, ENC_NA);
  git_tree = proto_item_add_subtree(ti, ett_git);

  if (!dissect_pkt_line(tvb, pinfo, git_tree, &offset))
    return 0;

  return tvb_captured_length(tvb);
}

/* Parse http packs
 *
 * @param  tvb        The buffer to dissect.
 * @param  pinfo      The Packet Info.
 * @param  tree       The protocol tree.
 * @param  hfindex    The type of http pack.
 * @return tvb_length The amount of captured data in the buffer,
 *                    returns 0 if parsing failed.
 *
 * This new helper takes the contents of the tvbuffer sent by http
 * dissectors, adds the packs to the git subtree and returns the amount
 * of consumed bytes in the tvb buffer.
*/
static int
dissect_http_pkt_lines(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int hfindex)
{
  proto_tree             *git_tree;
  proto_item             *ti;
  int offset = 0;
  int total_len = 0;

  col_set_str(pinfo->cinfo, COL_PROTOCOL, PSNAME);

  col_set_str(pinfo->cinfo, COL_INFO, PNAME);

  ti = proto_tree_add_item(tree, proto_git, tvb, offset, -1, ENC_NA);
  git_tree = proto_item_add_subtree(ti, ett_git);

  proto_tree_add_item(git_tree, hfindex, tvb, offset,
                      tvb_captured_length(tvb), ENC_NA);

  total_len = tvb_reported_length(tvb);
  while (offset < total_len) {
    /* Add expert info if there is trouble parsing part-way through */
    if (!dissect_pkt_line(tvb, pinfo, git_tree, &offset)) {
      proto_tree_add_expert(git_tree, pinfo, &ei_git_malformed, tvb, offset, -1);
      break;
    }
  }

  return tvb_captured_length(tvb);
}

static int
dissect_git_upload_pack_adv(tvbuff_t *tvb, packet_info *pinfo,
                            proto_tree *tree, void *data _U_)
{
  return dissect_http_pkt_lines(tvb, pinfo, tree, hf_git_upload_pack_adv);
}

static int
dissect_git_upload_pack_req(tvbuff_t *tvb, packet_info *pinfo,
                            proto_tree *tree, void *data _U_)
{
  return dissect_http_pkt_lines(tvb, pinfo, tree, hf_git_upload_pack_req);
}

static int
dissect_git_upload_pack_res(tvbuff_t *tvb, packet_info *pinfo,
                            proto_tree *tree, void *data _U_)
{
  return dissect_http_pkt_lines(tvb, pinfo, tree, hf_git_upload_pack_res);
}

static int
dissect_git(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
{
  tcp_dissect_pdus(tvb, pinfo, tree, git_desegment, 4, get_git_pdu_len,
                   dissect_git_pdu, data);
  return tvb_captured_length(tvb);
}

void
proto_register_git(void)
{
  static hf_register_info hf[] = {
    { &hf_git_protocol_version,
      { "Git Protocol Version", "git.version", FT_UINT8, BASE_NONE, VALS(version_vals),
      0, NULL, HFILL },
    },
    { &hf_git_packet_type,
      { "Git Packet Type", "git.packet_type", FT_UINT8, BASE_NONE, VALS(packet_type_vals),
      0, NULL, HFILL },
    },
    { &hf_git_packet_len,
      { "Packet length", "git.length", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
    },
    { &hf_git_packet_data,
      { "Packet data", "git.data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL },
    },
    { &hf_git_sideband_control_code,
      { "Sideband control code", "git.sideband_control_code", FT_UINT8,
      BASE_HEX, VALS(sideband_vals), 0, NULL, HFILL },
    },
    { &hf_git_upload_pack_adv,
      { "Upload Pack Advertisement", "git.upload_pack_advertisement",
      FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL },
    },
    { &hf_git_upload_pack_req,
      { "Upload Pack Request", "git.upload_pack_request",
      FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL },
    },
    { &hf_git_upload_pack_res,
      { "Upload Pack Result", "git.upload_pack_result",
      FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL },
    },
  };

  static gint *ett[] = {
    &ett_git,
  };

  static ei_register_info ei[] = {
    { &ei_git_bad_pkt_len,
      { "git.bad_pkt_len", PI_PROTOCOL, PI_ERROR,
        "unrecognized special pkt-len value", EXPFILL }
    },
    { &ei_git_malformed,
      { "git.malformed", PI_MALFORMED, PI_ERROR,
        "malformed packet", EXPFILL }
    },
  };

  module_t *git_module;
  expert_module_t *expert_git;

  proto_git = proto_register_protocol(PNAME, PSNAME, PFNAME);
  proto_register_field_array(proto_git, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));

  expert_git = expert_register_protocol(proto_git);
  expert_register_field_array(expert_git, ei, array_length(ei));

  git_handle = register_dissector(PFNAME, dissect_git, proto_git);

  git_module = prefs_register_protocol(proto_git, NULL);

  prefs_register_bool_preference(git_module, "desegment",
                                 "Reassemble GIT messages spanning multiple TCP segments",
                                 "Whether the GIT dissector should reassemble messages spanning multiple TCP segments."
                                 " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
                                 &git_desegment);
}

void
proto_reg_handoff_git(void)
{
  /*
   * Add the dissectors for GIT over HTTP
   *
   * Reference documentation at
   * https://www.kernel.org/pub/software/scm/git/docs//technical/http-protocol.txt
   */
  dissector_handle_t git_upload_pack_adv_handle;
  dissector_handle_t git_upload_pack_req_handle;
  dissector_handle_t git_upload_pack_res_handle;

  git_upload_pack_adv_handle = create_dissector_handle(dissect_git_upload_pack_adv,
                        proto_git);

  git_upload_pack_req_handle = create_dissector_handle(dissect_git_upload_pack_req,
                        proto_git);

  git_upload_pack_res_handle = create_dissector_handle(dissect_git_upload_pack_res,
                        proto_git);

  dissector_add_string("media_type",
                        "application/x-git-upload-pack-advertisement",
                        git_upload_pack_adv_handle);
  dissector_add_string("media_type",
                        "application/x-git-upload-pack-request",
                        git_upload_pack_req_handle);
  dissector_add_string("media_type",
                        "application/x-git-upload-pack-result",
                        git_upload_pack_res_handle);

  dissector_add_uint_with_preference("tcp.port", TCP_PORT_GIT, git_handle);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local Variables:
 * c-basic-offset: 2
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=2 tabstop=8 expandtab:
 * :indentSize=2:tabSize=8:noTabs=true:
 */