Coverage Report

Created: 2026-01-02 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wireshark/epan/dissectors/packet-usb-printer.c
Line
Count
Source
1
/* packet-usb-printer.c
2
 *
3
 * Copyright 2020, Martin Kaiser <martin@kaiser.cx>
4
 *
5
 * Wireshark - Network traffic analyzer
6
 * By Gerald Combs <gerald@wireshark.org>
7
 * Copyright 1998 Gerald Combs
8
 *
9
 * SPDX-License-Identifier: GPL-2.0-or-later
10
 */
11
12
/*
13
 * References:
14
 *
15
 * USB printer class specification
16
 * https://www.usb.org/sites/default/files/usbprint11a021811.pdf
17
 *
18
 * IEEE 1284 (parallel peripheral interface for personal computers)
19
 * http://kazus.ru/nuke/modules/Downloads/pub/148/0/IEEE%201284-2000.pdf
20
 */
21
22
#include "config.h"
23
#include <epan/packet.h>
24
#include <epan/proto.h>
25
#include "packet-usb.h"
26
27
static dissector_handle_t usb_printer_ctl_handle;
28
29
static int proto_usb_printer;
30
31
static int hf_usb_printer_req;
32
static int hf_usb_printer_cfg_idx;
33
static int hf_usb_printer_intf;
34
static int hf_usb_printer_alt_set;
35
static int hf_usb_printer_max_len;
36
static int hf_usb_printer_dev_id_len;
37
static int hf_usb_printer_dev_id;
38
39
static int ett_usb_printer;
40
41
void proto_register_usb_printer(void);
42
void proto_reg_handoff_usb_printer(void);
43
44
0
#define REQ_GET_DEV_ID    0
45
#define REQ_GET_PORT_STAT 1
46
#define REQ_GET_SOFT_RST  2
47
48
static const value_string usb_printer_req[] = {
49
    { REQ_GET_DEV_ID,    "GET_DEVICE_ID" },
50
    { REQ_GET_PORT_STAT, "GET_PORT_STATUS" },
51
    { REQ_GET_SOFT_RST,  "SOFT_RESET" },
52
    { 0, NULL }
53
};
54
55
static int dissect_usb_printer_ctl(
56
        tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
57
0
{
58
0
    bool is_request = (pinfo->srcport == NO_ENDPOINT);
59
0
    urb_info_t *urb = (urb_info_t *)data;
60
0
    usb_trans_info_t *usb_trans_info;
61
0
    int offset = 0;
62
0
    uint8_t bReq;
63
0
    uint32_t dev_id_len;
64
65
0
    if (!urb)
66
0
        return 0;
67
68
0
    usb_trans_info = urb->usb_trans_info;
69
0
    if (!usb_trans_info)
70
0
        return 0;
71
72
0
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "USBPRINTER");
73
0
    col_set_str(pinfo->cinfo, COL_INFO,
74
0
            val_to_str_const(usb_trans_info->setup.request,
75
0
                usb_printer_req, "Invalid"));
76
77
0
    if (is_request) {
78
0
        col_append_str(pinfo->cinfo, COL_INFO, " request");
79
80
0
        bReq = tvb_get_uint8(tvb, offset);
81
0
        proto_tree_add_item(tree, hf_usb_printer_req,
82
0
                tvb, offset, 1, ENC_LITTLE_ENDIAN);
83
0
        offset++;
84
85
0
        if (bReq == REQ_GET_DEV_ID) {
86
            /* Generally, fields in USB messages are little endian. */
87
0
            proto_tree_add_item(tree, hf_usb_printer_cfg_idx,
88
0
                    tvb, offset, 2, ENC_LITTLE_ENDIAN);
89
0
            offset += 2;
90
0
            proto_tree_add_item(tree, hf_usb_printer_intf,
91
0
                    tvb, offset, 2, ENC_LITTLE_ENDIAN);
92
0
            proto_tree_add_item(tree, hf_usb_printer_alt_set,
93
0
                    tvb, offset, 2, ENC_LITTLE_ENDIAN);
94
0
            offset += 2;
95
0
            proto_tree_add_item(tree, hf_usb_printer_max_len,
96
0
                    tvb, offset, 2, ENC_LITTLE_ENDIAN);
97
0
            offset += 2;
98
0
        }
99
0
    }
100
0
    else {
101
0
        col_append_str(pinfo->cinfo, COL_INFO, " response");
102
103
0
        if (usb_trans_info->setup.request == REQ_GET_DEV_ID) {
104
            /*
105
             * A printer's Device ID is defined in IEEE 1284, section 7.6.
106
             * It starts with a 16-bit length field in big endian encoding.
107
             * The length field includes the two bytes for itself. Therefore,
108
             * we can't use an FT_UINT_STRING for the entire Device ID.
109
             * The actual Device ID string consists of ASCII characters.
110
             */
111
0
            proto_tree_add_item_ret_uint(tree, hf_usb_printer_dev_id_len,
112
0
                    tvb, offset, 2, ENC_BIG_ENDIAN, &dev_id_len);
113
0
            offset += 2;
114
0
            if (dev_id_len > 2) {
115
0
                proto_tree_add_item(tree, hf_usb_printer_dev_id,
116
0
                        tvb, offset, dev_id_len-2, ENC_ASCII);
117
0
                offset += dev_id_len-2;
118
0
            }
119
            /* XXX - expert info for invalid dev_id_len */
120
0
        }
121
0
    }
122
123
0
    return offset;
124
0
}
125
126
void proto_register_usb_printer(void)
127
14
{
128
14
    static hf_register_info hf[] = {
129
14
        { &hf_usb_printer_req,
130
14
            { "bRequest", "usbprinter.bRequest", FT_UINT8, BASE_HEX,
131
14
                VALS(usb_printer_req), 0x0, NULL, HFILL }
132
14
        },
133
14
        { &hf_usb_printer_cfg_idx,
134
14
            { "Config index", "usbprinter.config_index", FT_UINT16, BASE_HEX,
135
14
                NULL, 0x0, NULL, HFILL }
136
14
        },
137
14
        { &hf_usb_printer_intf,
138
14
            { "Interface", "usbprinter.interface", FT_UINT16, BASE_HEX,
139
14
                NULL, 0xFF00, NULL, HFILL }
140
14
        },
141
14
        { &hf_usb_printer_alt_set,
142
14
            { "Alternate setting", "usbprinter.alt_set", FT_UINT16, BASE_HEX,
143
14
                NULL, 0x00FF, NULL, HFILL }
144
14
        },
145
14
        { &hf_usb_printer_max_len,
146
14
            { "Maximum length", "usbprinter.max_len", FT_UINT16, BASE_HEX,
147
14
                NULL, 0, NULL, HFILL }
148
14
        },
149
14
        { &hf_usb_printer_dev_id_len,
150
14
            { "Device ID length", "usbprinter.device_id_len", FT_UINT16,
151
14
                BASE_HEX, NULL, 0, NULL, HFILL }
152
14
        },
153
14
        { &hf_usb_printer_dev_id,
154
14
            { "Device ID", "usbprinter.device_id", FT_STRING, BASE_NONE,
155
14
                NULL, 0, NULL, HFILL }
156
14
        }
157
14
    };
158
159
14
    static int *ett[] = {
160
14
        &ett_usb_printer
161
14
    };
162
163
14
    proto_usb_printer = proto_register_protocol("USB Printer", "USBPRINTER", "usbprinter");
164
14
    proto_register_field_array(proto_usb_printer, hf, array_length(hf));
165
14
    proto_register_subtree_array(ett, array_length(ett));
166
14
    usb_printer_ctl_handle = register_dissector("usbprinter",  dissect_usb_printer_ctl, proto_usb_printer);
167
14
}
168
169
void
170
proto_reg_handoff_usb_printer(void)
171
14
{
172
14
    dissector_add_uint("usb.control", IF_CLASS_PRINTER, usb_printer_ctl_handle);
173
14
}
174
175
/*
176
* Editor modelines - https://www.wireshark.org/tools/modelines.html
177
*
178
* Local variables:
179
* c-basic-offset: 4
180
* tab-width: 8
181
* indent-tabs-mode: nil
182
* End:
183
*
184
* ex: set shiftwidth=4 tabstop=8 expandtab:
185
* :indentSize=4:tabSize=8:noTabs=true:
186
*/