summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-xyplex.c
blob: bdf4ac72f5862ae026b5b659fcc0013257a6fe1b (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
/* packet-xyplex.c
 * Routines for xyplex packet dissection
 *
 * Copyright 2002 Randy McEoin <rmceoin@pe.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Copied from packet-tftp.c
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/conversation.h>

void proto_register_xyplex(void);
void proto_reg_handoff_xyplex(void);

static int proto_xyplex;
static int hf_xyplex_type;
static int hf_xyplex_pad;
static int hf_xyplex_server_port;
static int hf_xyplex_return_port;
static int hf_xyplex_reserved;
static int hf_xyplex_reply;
static int hf_xyplex_data;

static int ett_xyplex;

static dissector_handle_t xyplex_handle;

#define UDP_PORT_XYPLEX    173

#define XYPLEX_REG_OK           0x00
#define XYPLEX_REG_QUEFULL      0x05

static const value_string xyplex_reg_vals[] = {
  { XYPLEX_REG_OK,      "OK" },
  { XYPLEX_REG_QUEFULL, "Queue Full" },
  { 0,          NULL }
};

static int
dissect_xyplex(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
  proto_tree     *xyplex_tree;
  proto_item     *ti;
  conversation_t *conversation;
  int             offset = 0;

  uint8_t prototype;
  uint8_t padding;
  uint16_t server_port;
  uint16_t return_port;
  uint16_t reserved;
  uint16_t reply;

  col_set_str(pinfo->cinfo, COL_PROTOCOL, "XYPLEX");

  ti = proto_tree_add_item(tree, proto_xyplex, tvb, offset, -1, ENC_NA);
  xyplex_tree = proto_item_add_subtree(ti, ett_xyplex);

  if (pinfo->destport == UDP_PORT_XYPLEX) {
    /* This is a registration request from a Unix server
     * to the Xyplex server.  The server_port indicates
     * which Xyplex serial port is desired.  The
     * return_port tells the Xyplex server what TCP port
     * to open to the Unix server.
     */
    prototype = tvb_get_uint8(tvb, offset);
    padding = tvb_get_uint8(tvb, offset+1);
    server_port = tvb_get_ntohs(tvb, offset+2);
    return_port = tvb_get_ntohs(tvb, offset+4);
    reserved = tvb_get_ntohs(tvb, offset+6);
    col_add_fstr(pinfo->cinfo, COL_INFO,
                 "Registration Request: %d Return: %d",
                 server_port, return_port);

    if (tree) {
      proto_tree_add_uint(xyplex_tree, hf_xyplex_type, tvb,
                          offset, 1, prototype);
      proto_tree_add_uint(xyplex_tree, hf_xyplex_pad, tvb,
                          offset+1, 1, padding);
      proto_tree_add_uint(xyplex_tree, hf_xyplex_server_port, tvb,
                          offset+2, 2, server_port);
      proto_tree_add_uint(xyplex_tree, hf_xyplex_return_port, tvb,
                          offset+4, 2, return_port);
      proto_tree_add_uint(xyplex_tree, hf_xyplex_reserved, tvb,
                          offset+6, 2, reserved);
    }
    offset += 8;

    /* Look for all future TCP conversations between the
     * requesting server and the Xyplex host using the
     * return_port.
     */
    conversation = find_conversation(pinfo->num, &pinfo->src, &pinfo->dst,
                                     CONVERSATION_TCP, return_port, 0, NO_PORT_B);
    if (conversation == NULL) {
      conversation = conversation_new(pinfo->num, &pinfo->src, &pinfo->dst,
                                      CONVERSATION_TCP, return_port, 0, NO_PORT2);
      conversation_set_dissector(conversation, xyplex_handle);
    }
    return offset;
  }

  if (pinfo->srcport == UDP_PORT_XYPLEX) {
    prototype = tvb_get_uint8(tvb, offset);
    padding = tvb_get_uint8(tvb, offset+1);
    reply = tvb_get_ntohs(tvb, offset+2);
    col_add_fstr(pinfo->cinfo, COL_INFO, "Registration Reply: %s",
                 val_to_str(reply, xyplex_reg_vals, "Unknown (0x%02x)"));

    if (tree) {
      proto_tree_add_uint(xyplex_tree, hf_xyplex_type, tvb,
                          offset, 1, prototype);
      proto_tree_add_uint(xyplex_tree, hf_xyplex_pad, tvb,
                          offset+1, 1, padding);
      proto_tree_add_uint(xyplex_tree, hf_xyplex_reply, tvb,
                          offset+2, 2, reply);
    }
    offset += 4;
    return offset;
  }

  /*
   * This must be the TCP data stream.  This will just be
   * the raw data being transferred from the remote server
   * and the Xyplex serial port.
   */
  col_add_fstr(pinfo->cinfo, COL_INFO, "%d > %d Data",
               pinfo->srcport, pinfo->destport);

  proto_tree_add_item(xyplex_tree, hf_xyplex_data, tvb, offset, -1, ENC_NA);

  return tvb_reported_length_remaining(tvb, offset);
}


void
proto_register_xyplex(void)
{
  static hf_register_info hf[] = {
    { &hf_xyplex_type,
      { "Type",       "xyplex.type",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        "Protocol type", HFILL }},

    { &hf_xyplex_pad,
      { "Pad",        "xyplex.pad",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        "Padding", HFILL }},

    { &hf_xyplex_server_port,
      { "Server Port",        "xyplex.server_port",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }},

    { &hf_xyplex_return_port,
      { "Return Port",   "xyplex.return_port",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }},

    { &hf_xyplex_reserved,
      { "Reserved field",  "xyplex.reserved",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL }},

    { &hf_xyplex_reply,
      { "Registration Reply",  "xyplex.reply",
        FT_UINT16, BASE_DEC, VALS(xyplex_reg_vals), 0x0,
        NULL, HFILL }},

    { &hf_xyplex_data,
      { "Data",  "xyplex.data",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }},

  };
  static int *ett[] = {
    &ett_xyplex,
  };

  proto_xyplex = proto_register_protocol("Xyplex", "XYPLEX", "xyplex");
  proto_register_field_array(proto_xyplex, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));
  xyplex_handle = register_dissector("xyplex", dissect_xyplex, proto_xyplex);
}

void
proto_reg_handoff_xyplex(void)
{
  dissector_add_uint_with_preference("udp.port", UDP_PORT_XYPLEX, xyplex_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:
 */