sensitivity of packet range options fine tuning:
[obnox/wireshark/wip.git] / packet-cups.c
1 /* packet-cups.c
2 * Routines for Common Unix Printing System (CUPS) Browsing Protocol
3 * packet disassembly for the Ethereal network traffic analyzer.
4 *
5 * Charles Levert <charles@comm.polymtl.ca>
6 * Copyright 2001 Charles Levert
7 *
8 * $Id: packet-cups.c,v 1.12 2002/08/28 21:00:08 jmayer Exp $
9 *
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.
14 *
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.
19 *
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.
23 *
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <string.h>
31 #include <ctype.h>
32
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include <epan/strutil.h>
36
37 /**********************************************************************/
38
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 */
42 {
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) */
63 };
64 /* End insert from cups/cups.h */
65
66 typedef struct {
67         guint32 bit;
68         char    *on_string;
69         char    *off_string;
70 } cups_ptype_bit_info;
71
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" },
79         { CUPS_PRINTER_LARGE,
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" },
83         { CUPS_PRINTER_SMALL,
84           "Can print up to 9x14 inches", "Cannot print up to 9x14 inches" },
85         { CUPS_PRINTER_SORT,
86           "Can sort", "Cannot sort" },
87         { CUPS_PRINTER_BIND,
88           "Can bind", "Cannot bind" },
89         { CUPS_PRINTER_COVER,
90           "Can cover", "Cannot cover" },
91         { CUPS_PRINTER_PUNCH,
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" },
103         { CUPS_PRINTER_BW,
104           "Can print black", "Cannot print black" },
105         { CUPS_PRINTER_REMOTE,
106           "Remote", "Local (illegal)" },
107         { CUPS_PRINTER_CLASS,
108           "Printer class", "Single printer" }
109 };
110
111 #define N_CUPS_PTYPE_BITS       (sizeof cups_ptype_bits / sizeof cups_ptype_bits[0])
112
113 typedef enum _cups_state {
114         CUPS_IDLE = 3,
115         CUPS_PROCESSING,
116         CUPS_STOPPED
117 } cups_state_t;
118
119 static const value_string cups_state_values[] = {
120         { CUPS_IDLE,            "idle" },
121         { CUPS_PROCESSING,      "processing" },
122         { CUPS_STOPPED,         "stopped" },
123         { 0,                    NULL }
124 };
125
126 static int proto_cups = -1;
127 static int hf_cups_ptype = -1;
128 static int hf_cups_state = -1;
129
130 static gint ett_cups = -1;
131 static gint ett_cups_ptype = -1;
132
133 /* This protocol is heavily related to IPP, but it is CUPS-specific
134    and non-standard. */
135 #define UDP_PORT_CUPS   631
136 #define PROTO_TAG_CUPS  "CUPS"
137
138 static guint get_hex_uint(tvbuff_t *tvb, gint offset,
139     gint *next_offset);
140 static gboolean skip_space(tvbuff_t *tvb, gint offset,
141     gint *next_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);
146
147 /**********************************************************************/
148
149 static void
150 dissect_cups(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
151 {
152         proto_tree      *cups_tree = 0;
153         proto_tree      *ptype_subtree = 0;
154         proto_item      *ti = 0;
155         gint            offset = 0;
156         gint            next_offset;
157         guint           len;
158         unsigned int    u;
159         const guint8    *str;
160         cups_ptype_t    ptype;
161         unsigned int    state;
162
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);
167
168         if (tree) {
169                 ti = proto_tree_add_item(tree, proto_cups, tvb, offset, -1,
170                     FALSE);
171                 cups_tree = proto_item_add_subtree(ti, ett_cups);
172         }
173
174         /* Format (1450 bytes max.):  */
175         /* type state uri ["location" ["info" ["make-and-model"]]]\n */
176
177         ptype = get_hex_uint(tvb, offset, &next_offset);
178         len = next_offset - offset;
179         if (len != 0) {
180                 if (cups_tree) {
181                         ti = proto_tree_add_uint(cups_tree, hf_cups_ptype,
182                             tvb, offset, len, ptype);
183                         ptype_subtree = proto_item_add_subtree(ti,
184                             ett_cups_ptype);
185                         for (u = 0; u < N_CUPS_PTYPE_BITS; u++) {
186                                 proto_tree_add_text(ptype_subtree, tvb,
187                                     offset, len, "%s",
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));
192                         }
193                 }
194         }
195         offset = next_offset;
196
197         if (!skip_space(tvb, offset, &next_offset))
198                 return; /* end of packet */
199         offset = next_offset;
200
201         state = get_hex_uint(tvb, offset, &next_offset);
202         len = next_offset - offset;
203         if (len != 0) {
204                 if (cups_tree)
205                         proto_tree_add_uint(cups_tree, hf_cups_state,
206                             tvb, offset, len, state);
207         }
208         offset = next_offset;
209
210         if (!skip_space(tvb, offset, &next_offset))
211                 return; /* end of packet */
212         offset = next_offset;
213
214         str = get_unquoted_string(tvb, offset, &next_offset, &len);
215         if (str == NULL)
216                 return; /* separator/terminator not found */
217         if (cups_tree)
218                 proto_tree_add_text(cups_tree, tvb, offset, len,
219                     "URI: %.*s",
220                     (guint16) len, str);
221         if (check_col(pinfo->cinfo, COL_INFO))
222                 col_add_fstr(pinfo->cinfo, COL_INFO,
223                     "%.*s (%s)",
224                     (guint16) len, str,
225                     val_to_str(state, cups_state_values, "0x%x"));
226         offset = next_offset;
227
228         if (!cups_tree)
229                 return;
230
231         if (!skip_space(tvb, offset, &next_offset))
232                 return; /* end of packet */
233         offset = next_offset;
234
235         str = get_quoted_string(tvb, offset, &next_offset, &len);
236         if (str == NULL)
237                 return; /* separator/terminator not found */
238         proto_tree_add_text(cups_tree, tvb, offset+1, len,
239             "Location: \"%.*s\"",
240             (guint16) len, str);
241         offset = next_offset;
242
243         if (!skip_space(tvb, offset, &next_offset))
244                 return; /* end of packet */
245         offset = next_offset;
246
247         str = get_quoted_string(tvb, offset, &next_offset, &len);
248         if (str == NULL)
249                 return; /* separator/terminator not found */
250         proto_tree_add_text(cups_tree, tvb, offset+1, len,
251             "Information: \"%.*s\"",
252             (guint16) len, str);
253         offset = next_offset;
254
255         if (!skip_space(tvb, offset, &next_offset))
256                 return; /* end of packet */
257         offset = next_offset;
258
259         str = get_quoted_string(tvb, offset, &next_offset, &len);
260         if (str == NULL)
261                 return; /* separator/terminator not found */
262         proto_tree_add_text(cups_tree, tvb, offset+1, len,
263             "Make and model: \"%.*s\"",
264             (guint16) len, str);
265         offset = next_offset;
266
267         return;
268 }
269
270 static guint
271 get_hex_uint(tvbuff_t *tvb, gint offset, gint *next_offset)
272 {
273         int c;
274         guint u = 0;
275
276         while (isxdigit(c = tvb_get_guint8(tvb, offset))) {
277                 if (isdigit(c))
278                         c -= '0';
279                 else if (isupper(c))
280                         c -= 'A' - 10;
281                 else if (islower(c))
282                         c -= 'a' - 10;
283                 else
284                         c = 0; /* This should not happen. */
285
286                 u = 16*u + c;
287
288                 offset++;
289         }
290
291         *next_offset = offset;
292
293         return u;
294 }
295
296 static gboolean
297 skip_space(tvbuff_t *tvb, gint offset, gint *next_offset)
298 {
299         int c;
300
301         while ((c = tvb_get_guint8(tvb, offset)) == ' ')
302                 offset++;
303         if (c == '\r' || c == '\n')
304                 return FALSE;   /* end of packet */
305
306         *next_offset = offset;
307
308         return TRUE;
309 }
310
311 static const guint8*
312 get_quoted_string(tvbuff_t *tvb, gint offset, gint *next_offset, guint *len)
313 {
314         int c;
315         const guint8* s = NULL;
316         guint l = 0;
317         gint o;
318
319         c = tvb_get_guint8(tvb, offset);
320         if (c == '"') {
321                 o = tvb_find_guint8(tvb, offset+1, -1, '"');
322                 if (o != -1) {
323                         offset++;
324                         l = o - offset;
325                         s = tvb_get_ptr(tvb, offset, l);
326                         offset = o + 1;
327                 }
328         }
329
330         *next_offset = offset;
331         *len = l;
332
333         return s;
334 }
335
336 static const guint8*
337 get_unquoted_string(tvbuff_t *tvb, gint offset, gint *next_offset, guint *len)
338 {
339         const guint8* s = NULL;
340         guint l = 0;
341         gint o;
342
343         o = tvb_pbrk_guint8(tvb, offset, -1, " \t\r\n");
344         if (o != -1) {
345                 l = o - offset;
346                 s = tvb_get_ptr(tvb, offset, l);
347                 offset = o;
348         }
349
350         *next_offset = offset;
351         *len = l;
352
353         return s;
354 }
355
356 /**********************************************************************/
357
358 void
359 proto_register_cups(void)
360 {
361         static hf_register_info hf[] = {
362                 /* This one could be split in separate fields. */
363                 { &hf_cups_ptype,
364                         { "Type",       "cups.ptype", FT_UINT32, BASE_HEX,
365                           NULL, 0x0, "", HFILL }},
366
367                 { &hf_cups_state,
368                         { "State",      "cups.state", FT_UINT8, BASE_HEX,
369                           VALS(cups_state_values), 0x0, "", HFILL }}
370         };
371
372         static gint *ett[] = {
373                 &ett_cups,
374                 &ett_cups_ptype
375         };
376
377         proto_cups = proto_register_protocol(
378             "Common Unix Printing System (CUPS) Browsing Protocol",
379             "CUPS", "cups");
380         proto_register_field_array(proto_cups, hf, array_length(hf));
381         proto_register_subtree_array(ett, array_length(ett));
382 }
383
384 void
385 proto_reg_handoff_cups(void)
386 {
387         dissector_handle_t cups_handle;
388
389         cups_handle = create_dissector_handle(dissect_cups, proto_cups);
390         dissector_add("udp.port", UDP_PORT_CUPS, cups_handle);
391 }