2 * Routines for Common Unix Printing System (CUPS) Browsing Protocol
3 * packet disassembly for the Ethereal network traffic analyzer.
5 * Charles Levert <charles@comm.polymtl.ca>
6 * Copyright 2001 Charles Levert
8 * $Id: packet-cups.c,v 1.12 2002/08/28 21:00:08 jmayer Exp $
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include <epan/packet.h>
35 #include <epan/strutil.h>
37 /**********************************************************************/
39 /* From cups/cups.h, GNU GPL, Copyright 1997-2001 by Easy Software Products. */
40 typedef guint32 cups_ptype_t; /**** Printer Type/Capability Bits ****/
41 enum /* Not a typedef'd enum so we can OR */
43 CUPS_PRINTER_LOCAL = 0x0000, /* Local printer or class */
44 CUPS_PRINTER_CLASS = 0x0001, /* Printer class */
45 CUPS_PRINTER_REMOTE = 0x0002, /* Remote printer or class */
46 CUPS_PRINTER_BW = 0x0004, /* Can do B&W printing */
47 CUPS_PRINTER_COLOR = 0x0008, /* Can do color printing */
48 CUPS_PRINTER_DUPLEX = 0x0010, /* Can do duplexing */
49 CUPS_PRINTER_STAPLE = 0x0020, /* Can staple output */
50 CUPS_PRINTER_COPIES = 0x0040, /* Can do copies */
51 CUPS_PRINTER_COLLATE = 0x0080, /* Can collage copies */
52 CUPS_PRINTER_PUNCH = 0x0100, /* Can punch output */
53 CUPS_PRINTER_COVER = 0x0200, /* Can cover output */
54 CUPS_PRINTER_BIND = 0x0400, /* Can bind output */
55 CUPS_PRINTER_SORT = 0x0800, /* Can sort output */
56 CUPS_PRINTER_SMALL = 0x1000, /* Can do Letter/Legal/A4 */
57 CUPS_PRINTER_MEDIUM = 0x2000, /* Can do Tabloid/B/C/A3/A2 */
58 CUPS_PRINTER_LARGE = 0x4000, /* Can do D/E/A1/A0 */
59 CUPS_PRINTER_VARIABLE = 0x8000, /* Can do variable sizes */
60 CUPS_PRINTER_IMPLICIT = 0x10000, /* Implicit class */
61 CUPS_PRINTER_DEFAULT = 0x20000, /* Default printer on network */
62 CUPS_PRINTER_OPTIONS = 0xfffc /* ~(CLASS | REMOTE | IMPLICIT) */
64 /* End insert from cups/cups.h */
70 } cups_ptype_bit_info;
72 static const cups_ptype_bit_info cups_ptype_bits[] = {
73 { CUPS_PRINTER_DEFAULT,
74 "Default printer on network", "Not default printer" },
75 { CUPS_PRINTER_IMPLICIT,
76 "Implicit class", "Explicit class" },
77 { CUPS_PRINTER_VARIABLE,
78 "Can print variable sizes", "Cannot print variable sizes" },
80 "Can print up to 36x48 inches", "Cannot print up to 36x48 inches" },
81 { CUPS_PRINTER_MEDIUM,
82 "Can print up to 18x24 inches", "Cannot print up to 18x24 inches" },
84 "Can print up to 9x14 inches", "Cannot print up to 9x14 inches" },
86 "Can sort", "Cannot sort" },
88 "Can bind", "Cannot bind" },
90 "Can cover", "Cannot cover" },
92 "Can punch holes", "Cannot punch holes" },
93 { CUPS_PRINTER_COLLATE,
94 "Can do fast collating", "Cannot do fast collating" },
95 { CUPS_PRINTER_COPIES,
96 "Can do fast copies", "Cannot do fast copies" },
97 { CUPS_PRINTER_STAPLE,
98 "Can staple", "Cannot staple" },
99 { CUPS_PRINTER_DUPLEX,
100 "Can duplex", "Cannot duplex" },
101 { CUPS_PRINTER_COLOR,
102 "Can print color", "Cannot print color" },
104 "Can print black", "Cannot print black" },
105 { CUPS_PRINTER_REMOTE,
106 "Remote", "Local (illegal)" },
107 { CUPS_PRINTER_CLASS,
108 "Printer class", "Single printer" }
111 #define N_CUPS_PTYPE_BITS (sizeof cups_ptype_bits / sizeof cups_ptype_bits[0])
113 typedef enum _cups_state {
119 static const value_string cups_state_values[] = {
120 { CUPS_IDLE, "idle" },
121 { CUPS_PROCESSING, "processing" },
122 { CUPS_STOPPED, "stopped" },
126 static int proto_cups = -1;
127 static int hf_cups_ptype = -1;
128 static int hf_cups_state = -1;
130 static gint ett_cups = -1;
131 static gint ett_cups_ptype = -1;
133 /* This protocol is heavily related to IPP, but it is CUPS-specific
135 #define UDP_PORT_CUPS 631
136 #define PROTO_TAG_CUPS "CUPS"
138 static guint get_hex_uint(tvbuff_t *tvb, gint offset,
140 static gboolean skip_space(tvbuff_t *tvb, gint offset,
142 static const guint8* get_quoted_string(tvbuff_t *tvb, gint offset,
143 gint *next_offset, guint *len);
144 static const guint8* get_unquoted_string(tvbuff_t *tvb, gint offset,
145 gint *next_offset, guint *len);
147 /**********************************************************************/
150 dissect_cups(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
152 proto_tree *cups_tree = 0;
153 proto_tree *ptype_subtree = 0;
163 if (check_col(pinfo->cinfo, COL_PROTOCOL))
164 col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_TAG_CUPS);
165 if (check_col(pinfo->cinfo, COL_INFO))
166 col_clear(pinfo->cinfo, COL_INFO);
169 ti = proto_tree_add_item(tree, proto_cups, tvb, offset, -1,
171 cups_tree = proto_item_add_subtree(ti, ett_cups);
174 /* Format (1450 bytes max.): */
175 /* type state uri ["location" ["info" ["make-and-model"]]]\n */
177 ptype = get_hex_uint(tvb, offset, &next_offset);
178 len = next_offset - offset;
181 ti = proto_tree_add_uint(cups_tree, hf_cups_ptype,
182 tvb, offset, len, ptype);
183 ptype_subtree = proto_item_add_subtree(ti,
185 for (u = 0; u < N_CUPS_PTYPE_BITS; u++) {
186 proto_tree_add_text(ptype_subtree, tvb,
188 decode_boolean_bitfield(ptype,
189 cups_ptype_bits[u].bit, sizeof (ptype)*8,
190 cups_ptype_bits[u].on_string,
191 cups_ptype_bits[u].off_string));
195 offset = next_offset;
197 if (!skip_space(tvb, offset, &next_offset))
198 return; /* end of packet */
199 offset = next_offset;
201 state = get_hex_uint(tvb, offset, &next_offset);
202 len = next_offset - offset;
205 proto_tree_add_uint(cups_tree, hf_cups_state,
206 tvb, offset, len, state);
208 offset = next_offset;
210 if (!skip_space(tvb, offset, &next_offset))
211 return; /* end of packet */
212 offset = next_offset;
214 str = get_unquoted_string(tvb, offset, &next_offset, &len);
216 return; /* separator/terminator not found */
218 proto_tree_add_text(cups_tree, tvb, offset, len,
221 if (check_col(pinfo->cinfo, COL_INFO))
222 col_add_fstr(pinfo->cinfo, COL_INFO,
225 val_to_str(state, cups_state_values, "0x%x"));
226 offset = next_offset;
231 if (!skip_space(tvb, offset, &next_offset))
232 return; /* end of packet */
233 offset = next_offset;
235 str = get_quoted_string(tvb, offset, &next_offset, &len);
237 return; /* separator/terminator not found */
238 proto_tree_add_text(cups_tree, tvb, offset+1, len,
239 "Location: \"%.*s\"",
241 offset = next_offset;
243 if (!skip_space(tvb, offset, &next_offset))
244 return; /* end of packet */
245 offset = next_offset;
247 str = get_quoted_string(tvb, offset, &next_offset, &len);
249 return; /* separator/terminator not found */
250 proto_tree_add_text(cups_tree, tvb, offset+1, len,
251 "Information: \"%.*s\"",
253 offset = next_offset;
255 if (!skip_space(tvb, offset, &next_offset))
256 return; /* end of packet */
257 offset = next_offset;
259 str = get_quoted_string(tvb, offset, &next_offset, &len);
261 return; /* separator/terminator not found */
262 proto_tree_add_text(cups_tree, tvb, offset+1, len,
263 "Make and model: \"%.*s\"",
265 offset = next_offset;
271 get_hex_uint(tvbuff_t *tvb, gint offset, gint *next_offset)
276 while (isxdigit(c = tvb_get_guint8(tvb, offset))) {
284 c = 0; /* This should not happen. */
291 *next_offset = offset;
297 skip_space(tvbuff_t *tvb, gint offset, gint *next_offset)
301 while ((c = tvb_get_guint8(tvb, offset)) == ' ')
303 if (c == '\r' || c == '\n')
304 return FALSE; /* end of packet */
306 *next_offset = offset;
312 get_quoted_string(tvbuff_t *tvb, gint offset, gint *next_offset, guint *len)
315 const guint8* s = NULL;
319 c = tvb_get_guint8(tvb, offset);
321 o = tvb_find_guint8(tvb, offset+1, -1, '"');
325 s = tvb_get_ptr(tvb, offset, l);
330 *next_offset = offset;
337 get_unquoted_string(tvbuff_t *tvb, gint offset, gint *next_offset, guint *len)
339 const guint8* s = NULL;
343 o = tvb_pbrk_guint8(tvb, offset, -1, " \t\r\n");
346 s = tvb_get_ptr(tvb, offset, l);
350 *next_offset = offset;
356 /**********************************************************************/
359 proto_register_cups(void)
361 static hf_register_info hf[] = {
362 /* This one could be split in separate fields. */
364 { "Type", "cups.ptype", FT_UINT32, BASE_HEX,
365 NULL, 0x0, "", HFILL }},
368 { "State", "cups.state", FT_UINT8, BASE_HEX,
369 VALS(cups_state_values), 0x0, "", HFILL }}
372 static gint *ett[] = {
377 proto_cups = proto_register_protocol(
378 "Common Unix Printing System (CUPS) Browsing Protocol",
380 proto_register_field_array(proto_cups, hf, array_length(hf));
381 proto_register_subtree_array(ett, array_length(ett));
385 proto_reg_handoff_cups(void)
387 dissector_handle_t cups_handle;
389 cups_handle = create_dissector_handle(dissect_cups, proto_cups);
390 dissector_add("udp.port", UDP_PORT_CUPS, cups_handle);