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.6 2001/09/20 02:26:03 guy Exp $
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
42 /**********************************************************************/
44 /* From cups/cups.h, GNU GPL, Copyright 1997-2001 by Easy Software Products. */
45 typedef guint32 cups_ptype_t; /**** Printer Type/Capability Bits ****/
46 enum /* Not a typedef'd enum so we can OR */
48 CUPS_PRINTER_LOCAL = 0x0000, /* Local printer or class */
49 CUPS_PRINTER_CLASS = 0x0001, /* Printer class */
50 CUPS_PRINTER_REMOTE = 0x0002, /* Remote printer or class */
51 CUPS_PRINTER_BW = 0x0004, /* Can do B&W printing */
52 CUPS_PRINTER_COLOR = 0x0008, /* Can do color printing */
53 CUPS_PRINTER_DUPLEX = 0x0010, /* Can do duplexing */
54 CUPS_PRINTER_STAPLE = 0x0020, /* Can staple output */
55 CUPS_PRINTER_COPIES = 0x0040, /* Can do copies */
56 CUPS_PRINTER_COLLATE = 0x0080, /* Can collage copies */
57 CUPS_PRINTER_PUNCH = 0x0100, /* Can punch output */
58 CUPS_PRINTER_COVER = 0x0200, /* Can cover output */
59 CUPS_PRINTER_BIND = 0x0400, /* Can bind output */
60 CUPS_PRINTER_SORT = 0x0800, /* Can sort output */
61 CUPS_PRINTER_SMALL = 0x1000, /* Can do Letter/Legal/A4 */
62 CUPS_PRINTER_MEDIUM = 0x2000, /* Can do Tabloid/B/C/A3/A2 */
63 CUPS_PRINTER_LARGE = 0x4000, /* Can do D/E/A1/A0 */
64 CUPS_PRINTER_VARIABLE = 0x8000, /* Can do variable sizes */
65 CUPS_PRINTER_IMPLICIT = 0x10000, /* Implicit class */
66 CUPS_PRINTER_DEFAULT = 0x20000, /* Default printer on network */
67 CUPS_PRINTER_OPTIONS = 0xfffc /* ~(CLASS | REMOTE | IMPLICIT) */
69 /* End insert from cups/cups.h */
75 } cups_ptype_bit_info;
77 static const cups_ptype_bit_info cups_ptype_bits[] = {
78 { CUPS_PRINTER_DEFAULT,
79 "Default printer on network", "Not default printer" },
80 { CUPS_PRINTER_IMPLICIT,
81 "Implicit class", "Explicit class" },
82 { CUPS_PRINTER_VARIABLE,
83 "Can print variable sizes", "Cannot print variable sizes" },
85 "Can print up to 36x48 inches", "Cannot print up to 36x48 inches" },
86 { CUPS_PRINTER_MEDIUM,
87 "Can print up to 18x24 inches", "Cannot print up to 18x24 inches" },
89 "Can print up to 9x14 inches", "Cannot print up to 9x14 inches" },
91 "Can sort", "Cannot sort" },
93 "Can bind", "Cannot bind" },
95 "Can cover", "Cannot cover" },
97 "Can punch holes", "Cannot punch holes" },
98 { CUPS_PRINTER_COLLATE,
99 "Can do fast collating", "Cannot do fast collating" },
100 { CUPS_PRINTER_COPIES,
101 "Can do fast copies", "Cannot do fast copies" },
102 { CUPS_PRINTER_STAPLE,
103 "Can staple", "Cannot staple" },
104 { CUPS_PRINTER_DUPLEX,
105 "Can duplex", "Cannot duplex" },
106 { CUPS_PRINTER_COLOR,
107 "Can print color", "Cannot print color" },
109 "Can print black", "Cannot print black" },
110 { CUPS_PRINTER_REMOTE,
111 "Remote", "Local (illegal)" },
112 { CUPS_PRINTER_CLASS,
113 "Printer class", "Single printer" }
116 #define N_CUPS_PTYPE_BITS (sizeof cups_ptype_bits / sizeof cups_ptype_bits[0])
118 typedef enum _cups_state {
124 static const value_string cups_state_values[] = {
125 { CUPS_IDLE, "idle" },
126 { CUPS_PROCESSING, "processing" },
127 { CUPS_STOPPED, "stopped" },
131 static int proto_cups = -1;
132 static int hf_cups_ptype = -1;
133 static int hf_cups_state = -1;
135 static gint ett_cups = -1;
136 static gint ett_cups_ptype = -1;
138 /* This protocol is heavily related to IPP, but it is CUPS-specific
140 #define UDP_PORT_CUPS 631
141 #define PROTO_TAG_CUPS "CUPS"
143 static guint get_hex_uint(tvbuff_t *tvb, gint offset,
145 static gboolean skip_space(tvbuff_t *tvb, gint offset,
147 static const guint8* get_quoted_string(tvbuff_t *tvb, gint offset,
148 gint *next_offset, guint *len);
149 static const guint8* get_unquoted_string(tvbuff_t *tvb, gint offset,
150 gint *next_offset, guint *len);
152 /**********************************************************************/
155 dissect_cups(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
157 proto_tree *cups_tree = 0;
158 proto_tree *ptype_subtree = 0;
168 if (check_col(pinfo->fd, COL_PROTOCOL))
169 col_set_str(pinfo->fd, COL_PROTOCOL, PROTO_TAG_CUPS);
170 if (check_col(pinfo->fd, COL_INFO))
171 col_clear(pinfo->fd, COL_INFO);
174 ti = proto_tree_add_item(tree, proto_cups, tvb, offset,
175 tvb_length_remaining(tvb, offset), FALSE);
176 cups_tree = proto_item_add_subtree(ti, ett_cups);
179 /* Format (1450 bytes max.): */
180 /* type state uri ["location" ["info" ["make-and-model"]]]\n */
182 ptype = get_hex_uint(tvb, offset, &next_offset);
183 len = next_offset - offset;
186 ti = proto_tree_add_uint(cups_tree, hf_cups_ptype,
187 tvb, offset, len, ptype);
188 ptype_subtree = proto_item_add_subtree(ti,
190 for (u = 0; u < N_CUPS_PTYPE_BITS; u++) {
191 proto_tree_add_text(ptype_subtree, tvb,
193 decode_boolean_bitfield(ptype,
194 cups_ptype_bits[u].bit, sizeof (ptype)*8,
195 cups_ptype_bits[u].on_string,
196 cups_ptype_bits[u].off_string));
200 offset = next_offset;
202 if (!skip_space(tvb, offset, &next_offset))
203 return; /* end of packet */
204 offset = next_offset;
206 state = get_hex_uint(tvb, offset, &next_offset);
207 len = next_offset - offset;
210 proto_tree_add_uint(cups_tree, hf_cups_state,
211 tvb, offset, len, state);
213 offset = next_offset;
215 if (!skip_space(tvb, offset, &next_offset))
216 return; /* end of packet */
217 offset = next_offset;
219 str = get_unquoted_string(tvb, offset, &next_offset, &len);
221 return; /* separator/terminator not found */
223 proto_tree_add_text(cups_tree, tvb, offset, len,
226 if (check_col(pinfo->fd, COL_INFO))
227 col_add_fstr(pinfo->fd, COL_INFO,
230 val_to_str(state, cups_state_values, "0x%x"));
231 offset = next_offset;
236 if (!skip_space(tvb, offset, &next_offset))
237 return; /* end of packet */
238 offset = next_offset;
240 str = get_quoted_string(tvb, offset, &next_offset, &len);
242 return; /* separator/terminator not found */
243 proto_tree_add_text(cups_tree, tvb, offset+1, len,
244 "Location: \"%.*s\"",
246 offset = next_offset;
248 if (!skip_space(tvb, offset, &next_offset))
249 return; /* end of packet */
250 offset = next_offset;
252 str = get_quoted_string(tvb, offset, &next_offset, &len);
254 return; /* separator/terminator not found */
255 proto_tree_add_text(cups_tree, tvb, offset+1, len,
256 "Information: \"%.*s\"",
258 offset = next_offset;
260 if (!skip_space(tvb, offset, &next_offset))
261 return; /* end of packet */
262 offset = next_offset;
264 str = get_quoted_string(tvb, offset, &next_offset, &len);
266 return; /* separator/terminator not found */
267 proto_tree_add_text(cups_tree, tvb, offset+1, len,
268 "Make and model: \"%.*s\"",
270 offset = next_offset;
276 get_hex_uint(tvbuff_t *tvb, gint offset, gint *next_offset)
281 while (isxdigit(c = tvb_get_guint8(tvb, offset))) {
289 c = 0; /* This should not happen. */
296 *next_offset = offset;
302 skip_space(tvbuff_t *tvb, gint offset, gint *next_offset)
306 while ((c = tvb_get_guint8(tvb, offset)) == ' ')
308 if (c == '\r' || c == '\n')
309 return FALSE; /* end of packet */
311 *next_offset = offset;
317 get_quoted_string(tvbuff_t *tvb, gint offset, gint *next_offset, guint *len)
320 const guint8* s = NULL;
324 c = tvb_get_guint8(tvb, offset);
326 o = tvb_find_guint8(tvb, offset+1, -1, '"');
330 s = tvb_get_ptr(tvb, offset, l);
335 *next_offset = offset;
342 get_unquoted_string(tvbuff_t *tvb, gint offset, gint *next_offset, guint *len)
344 const guint8* s = NULL;
348 o = tvb_pbrk_guint8(tvb, offset, -1, " \t\r\n");
351 s = tvb_get_ptr(tvb, offset, l);
355 *next_offset = offset;
361 /**********************************************************************/
364 proto_register_cups(void)
366 static hf_register_info hf[] = {
367 /* This one could be split in separate fields. */
369 { "Type", "cups.ptype", FT_UINT32, BASE_HEX,
370 NULL, 0x0, "", HFILL }},
373 { "State", "cups.state", FT_UINT8, BASE_HEX,
374 VALS(cups_state_values), 0x0, "", HFILL }}
377 static gint *ett[] = {
382 proto_cups = proto_register_protocol(
383 "Common Unix Printing System (CUPS) Browsing Protocol",
385 proto_register_field_array(proto_cups, hf, array_length(hf));
386 proto_register_subtree_array(ett, array_length(ett));
390 proto_reg_handoff_cups(void)
392 dissector_add("udp.port", UDP_PORT_CUPS, dissect_cups, proto_cups);