In Win32, treat both '/' and '\' as pathname separators.
[obnox/wireshark/wip.git] / packet-ipp.c
1 /* packet-ipp.c
2  * Routines for IPP packet disassembly
3  *
4  * Guy Harris <guy@alum.mit.edu>
5  *
6  * $Id: packet-ipp.c,v 1.4 2000/01/22 06:22:13 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  *
27  *
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37
38 #include <string.h>
39 #include <ctype.h>
40
41 #include <glib.h>
42 #include "packet.h"
43
44 static int proto_ipp = -1;
45
46 static gint ett_ipp = -1;
47 static gint ett_ipp_as = -1;
48 static gint ett_ipp_attr = -1;
49
50 #define PRINT_JOB               0x0002
51 #define PRINT_URI               0x0003
52 #define VALIDATE_JOB            0x0004
53 #define CREATE_JOB              0x0005
54 #define SEND_DOCUMENT           0x0006
55 #define SEND_URI                0x0007
56 #define CANCEL_JOB              0x0008
57 #define GET_JOB_ATTRIBUTES      0x0009
58 #define GET_JOBS                0x000A
59 #define GET_PRINTER_ATTRIBUTES  0x000B
60
61 static const value_string operation_vals[] = {
62     { PRINT_JOB,              "Print-Job" },
63     { PRINT_URI,              "Print-URI" },
64     { VALIDATE_JOB,           "Validate-Job" },
65     { CREATE_JOB,             "Create-Job" },
66     { SEND_DOCUMENT,          "Send-Document" },
67     { SEND_URI,               "Send-URI" },
68     { CANCEL_JOB,             "Cancel-Job" },
69     { GET_JOB_ATTRIBUTES,     "Get-Job-Attributes" },
70     { GET_JOBS,               "Get-Jobs" },
71     { GET_PRINTER_ATTRIBUTES, "Get-Printer-Attributes" },
72     { 0,                      NULL }
73 };
74
75 #define STATUS_SUCCESSFUL       0x0000
76 #define STATUS_INFORMATIONAL    0x0100
77 #define STATUS_REDIRECTION      0x0200
78 #define STATUS_CLIENT_ERROR     0x0400
79 #define STATUS_SERVER_ERROR     0x0500
80
81 #define STATUS_TYPE_MASK        0xFF00
82
83 #define SUCCESSFUL_OK                           0x0000
84 #define SUCCESSFUL_OK_IGN_OR_SUB_ATTR           0x0001
85 #define SUCCESSFUL_OK_CONFLICTING_ATTR          0x0002
86
87 #define CLIENT_ERROR_BAD_REQUEST                0x0400
88 #define CLIENT_ERROR_FORBIDDEN                  0x0401
89 #define CLIENT_ERROR_NOT_AUTHENTICATED          0x0402
90 #define CLIENT_ERROR_NOT_AUTHORIZED             0x0403
91 #define CLIENT_ERROR_NOT_POSSIBLE               0x0404
92 #define CLIENT_ERROR_TIMEOUT                    0x0405
93 #define CLIENT_ERROR_NOT_FOUND                  0x0406
94 #define CLIENT_ERROR_GONE                       0x0407
95 #define CLIENT_ERROR_REQ_ENTITY_TOO_LRG         0x0408
96 #define CLIENT_ERROR_REQ_VALUE_TOO_LONG         0x0409
97 #define CLIENT_ERROR_DOC_FMT_NOT_SUPP           0x040A
98 #define CLIENT_ERROR_ATTR_OR_VAL_NOT_SUPP       0x040B
99 #define CLIENT_ERROR_URI_SCHEME_NOT_SUPP        0x040C
100 #define CLIENT_ERROR_CHARSET_NOT_SUPP           0x040D
101 #define CLIENT_ERROR_CONFLICTING_ATTRS          0x040E
102
103 #define SERVER_ERROR_INTERNAL_ERROR             0x0500
104 #define SERVER_ERROR_OPERATION_NOT_SUPP         0x0501
105 #define SERVER_ERROR_SERVICE_UNAVAIL            0x0502
106 #define SERVER_ERROR_VERSION_NOT_SUPP           0x0503
107 #define SERVER_ERROR_DEVICE_ERROR               0x0504
108 #define SERVER_ERROR_TEMPORARY_ERROR            0x0505
109 #define SERVER_ERROR_NOT_ACCEPTING_JOBS         0x0506
110 #define SERVER_ERROR_BUSY                       0x0507
111 #define SERVER_ERROR_JOB_CANCELED               0x0508
112
113 static const value_string status_vals[] = {
114     { SUCCESSFUL_OK,                     "Successful-OK" },
115     { SUCCESSFUL_OK_IGN_OR_SUB_ATTR,     "Successful-OK-Ignored-Or-Substituted-Attributes" },
116     { SUCCESSFUL_OK_CONFLICTING_ATTR,    "Successful-OK-Conflicting-Attributes" },
117     { CLIENT_ERROR_BAD_REQUEST,          "Client-Error-Bad-Request" },
118     { CLIENT_ERROR_FORBIDDEN,            "Client-Error-Forbidden" },
119     { CLIENT_ERROR_NOT_AUTHENTICATED,    "Client-Error-Not-Authenticated" },
120     { CLIENT_ERROR_NOT_AUTHORIZED,       "Client-Error-Not-Authorized" },
121     { CLIENT_ERROR_NOT_POSSIBLE,         "Client-Error-Not-Possible" },
122     { CLIENT_ERROR_TIMEOUT,              "Client-Error-Timeout" },
123     { CLIENT_ERROR_NOT_FOUND,            "Client-Error-Not-Found" },
124     { CLIENT_ERROR_GONE,                 "Client-Error-Gone" },
125     { CLIENT_ERROR_REQ_ENTITY_TOO_LRG,   "Client-Error-Request-Entity-Too-Large" },
126     { CLIENT_ERROR_REQ_VALUE_TOO_LONG,   "Client-Error-Request-Value-Too-Long" },
127     { CLIENT_ERROR_DOC_FMT_NOT_SUPP,     "Client-Error-Document-Format-Not-Supported" },
128     { CLIENT_ERROR_ATTR_OR_VAL_NOT_SUPP, "Client-Error-Attributes-Or-Values-Not-Supported" },
129     { CLIENT_ERROR_URI_SCHEME_NOT_SUPP,  "Client-Error-URI-Scheme-Not-Supported" },
130     { CLIENT_ERROR_CHARSET_NOT_SUPP,     "Client-Error-Charset-Not-Supported" },
131     { CLIENT_ERROR_CONFLICTING_ATTRS,    "Client-Error-Conflicting-Attributes" },
132     { SERVER_ERROR_INTERNAL_ERROR,       "Server-Error-Internal-Error" },
133     { SERVER_ERROR_OPERATION_NOT_SUPP,   "Server-Error-Operation-Not-Supported" },
134     { SERVER_ERROR_SERVICE_UNAVAIL,      "Server-Error-Service-Unavailable" },
135     { SERVER_ERROR_VERSION_NOT_SUPP,     "Server-Error-Version-Not-Supported" },
136     { SERVER_ERROR_DEVICE_ERROR,         "Server-Error-Device-Error" },
137     { SERVER_ERROR_TEMPORARY_ERROR,      "Server-Error-Temporary-Error" },
138     { SERVER_ERROR_NOT_ACCEPTING_JOBS,   "Server-Error-Not-Accepting-Jobs" },
139     { SERVER_ERROR_BUSY,                 "Server-Error-Busy" },
140     { SERVER_ERROR_JOB_CANCELED,         "Server-Error-Job-Canceled" },
141     { 0,                                 NULL }
142 };
143
144 static int parse_attributes(const u_char *pd, int offset, frame_data *fd,
145     proto_tree *tree);
146 static proto_tree *add_integer_tree(proto_tree *tree, const u_char *pd,
147     int offset, guint name_length, guint value_length);
148 static void add_integer_value(guint tag, gchar *tag_desc, proto_tree *tree,
149     const u_char *pd, int offset, guint name_length, guint value_length);
150 static proto_tree *add_octetstring_tree(proto_tree *tree, const u_char *pd,
151     int offset, guint name_length, guint value_length);
152 static void add_octetstring_value(guint tag, gchar *tag_desc, proto_tree *tree,
153     const u_char *pd, int offset, guint name_length, guint value_length);
154 static proto_tree *add_charstring_tree(proto_tree *tree, const u_char *pd,
155     int offset, guint name_length, guint value_length);
156 static void add_charstring_value(guint tag, gchar *tag_desc, proto_tree *tree,
157     const u_char *pd, int offset, guint name_length, guint value_length);
158 static int add_value_head(guint tag, gchar *tag_desc, proto_tree *tree,
159     const u_char *pd, int offset, guint name_length, guint value_length);
160
161 void dissect_ipp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
162 {
163         proto_tree *ipp_tree;
164         proto_item *ti;
165         gboolean is_request = (pi.destport == 631);
166         guint16 status_code;
167         gchar *status_fmt;
168
169         if (check_col(fd, COL_PROTOCOL))
170                 col_add_str(fd, COL_PROTOCOL, "IPP");
171         if (check_col(fd, COL_INFO)) {
172                 if (is_request)
173                         col_add_str(fd, COL_INFO, "IPP request");
174                 else
175                         col_add_str(fd, COL_INFO, "IPP response");
176         }
177
178         if (tree) {
179                 ti = proto_tree_add_item(tree, proto_ipp, offset, END_OF_FRAME, NULL);
180                 ipp_tree = proto_item_add_subtree(ti, ett_ipp);
181
182                 proto_tree_add_text(ipp_tree, offset, 2, "Version: %u.%u",
183                     pd[offset], pd[offset + 1]);
184                 offset += 2;
185
186                 if (is_request) {
187                         proto_tree_add_text(ipp_tree, offset, 2, "Operation-id: %s",
188                             val_to_str(pntohs(&pd[offset]), operation_vals,
189                                 "Unknown (0x%04x)"));
190                 } else {
191                         status_code = pntohs(&pd[offset]);
192                         switch (status_code & STATUS_TYPE_MASK) {
193
194                         case STATUS_SUCCESSFUL:
195                                 status_fmt = "Successful (0x%04x)";
196                                 break;
197
198                         case STATUS_INFORMATIONAL:
199                                 status_fmt = "Informational (0x%04x)";
200                                 break;
201
202                         case STATUS_REDIRECTION:
203                                 status_fmt = "Redirection (0x%04x)";
204                                 break;
205
206                         case STATUS_CLIENT_ERROR:
207                                 status_fmt = "Client error (0x%04x)";
208                                 break;
209
210                         case STATUS_SERVER_ERROR:
211                                 status_fmt = "Server error (0x%04x)";
212                                 break;
213
214                         default:
215                                 status_fmt = "Unknown (0x%04x)";
216                                 break;
217                         }
218                         proto_tree_add_text(ipp_tree, offset, 2, "Status-code: %s",
219                             val_to_str(status_code, status_vals, status_fmt));
220                 }
221                 offset += 2;
222
223                 proto_tree_add_text(ipp_tree, offset, 4, "Request ID: %u",
224                     pntohl(&pd[offset]));
225                 offset += 4;
226
227                 offset = parse_attributes(pd, offset, fd, ipp_tree);
228
229                 if (IS_DATA_IN_FRAME(offset))
230                         dissect_data(pd, offset, fd, ipp_tree);
231         }
232 }
233
234 #define TAG_TYPE(tag)           ((tag) & 0xF0)
235 #define TAG_TYPE_DELIMITER      0x00
236 #define TAG_TYPE_INTEGER        0x20
237 #define TAG_TYPE_OCTETSTRING    0x30
238 #define TAG_TYPE_CHARSTRING     0x40
239
240 #define TAG_END_OF_ATTRIBUTES   0x03
241
242 #define TAG_INTEGER             0x21
243 #define TAG_BOOLEAN             0x22
244 #define TAG_ENUM                0x23
245
246 #define TAG_OCTETSTRING         0x30
247 #define TAG_DATETIME            0x31
248 #define TAG_RESOLUTION          0x32
249 #define TAG_RANGEOFINTEGER      0x33
250 #define TAG_TEXTWITHLANGUAGE    0x35
251 #define TAG_NAMEWITHLANGUAGE    0x36
252
253 #define TAG_TEXTWITHOUTLANGUAGE 0x41
254 #define TAG_NAMEWITHOUTLANGUAGE 0x42
255 #define TAG_KEYWORD             0x44
256 #define TAG_URI                 0x45
257 #define TAG_URISCHEME           0x46
258 #define TAG_CHARSET             0x47
259 #define TAG_NATURALLANGUAGE     0x48
260 #define TAG_MIMEMEDIATYPE       0x49
261
262 static const value_string tag_vals[] = {
263         /* Delimiter tags */
264         { 0x01,                    "Operation attributes" },
265         { 0x02,                    "Job attributes" },
266         { TAG_END_OF_ATTRIBUTES,   "End of attributes" },
267         { 0x04,                    "Printer attributes" },
268         { 0x05,                    "Unsupported attributes" },
269
270         /* Value tags */
271         { 0x10,                    "Unsupported" },
272         { 0x12,                    "Unknown" },
273         { 0x13,                    "No value" },
274         { TAG_INTEGER,             "Integer" },
275         { TAG_BOOLEAN,             "Boolean" },
276         { TAG_ENUM,                "Enum" },
277         { TAG_OCTETSTRING,         "Octet string" },
278         { TAG_DATETIME,            "Date/Time" },
279         { TAG_RESOLUTION,          "Resolution" },
280         { TAG_RANGEOFINTEGER,      "Range of integer" },
281         { TAG_TEXTWITHLANGUAGE,    "Text with language" },
282         { TAG_NAMEWITHLANGUAGE,    "Name with language" },
283         { TAG_TEXTWITHOUTLANGUAGE, "Text without language" },
284         { TAG_NAMEWITHOUTLANGUAGE, "Name without language" },
285         { TAG_KEYWORD,             "Keyword" },
286         { TAG_URI,                 "URI" },
287         { TAG_URISCHEME,           "URI scheme" },
288         { TAG_CHARSET,             "Character set" },
289         { TAG_NATURALLANGUAGE,     "Natural language" },
290         { TAG_MIMEMEDIATYPE,       "MIME media type" },
291         { 0,                       NULL }
292 };
293
294 static int
295 parse_attributes(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
296 {
297         guint8 tag;
298         gchar *tag_desc;
299         guint16 name_length, value_length;
300         proto_tree *as_tree = tree;
301         proto_item *tas = NULL;
302         int start_offset = offset;
303         proto_tree *attr_tree = tree;
304
305         while (IS_DATA_IN_FRAME(offset)) {
306                 tag = pd[offset];
307                 tag_desc = val_to_str(tag, tag_vals, "Reserved (0x%02x)");
308                 if (TAG_TYPE(tag) == TAG_TYPE_DELIMITER) {
309                         /*
310                          * If we had an attribute sequence we were
311                          * working on, we're done with it; set its
312                          * length to the length of all the stuff
313                          * we've done so far.
314                          */
315                         if (tas != NULL)
316                                 proto_item_set_len(tas, offset - start_offset);
317
318                         /*
319                          * This tag starts a new attribute sequence;
320                          * create a new tree under this tag when we see
321                          * a non-delimiter tag, under which to put
322                          * those attributes.
323                          */
324                         as_tree = NULL;
325                         attr_tree = tree;
326
327                         /*
328                          * Remember the offset at which this attribute
329                          * sequence started, so we can use it to compute
330                          * its length when it's finished.
331                          */
332                         start_offset = offset;
333
334                         /*
335                          * Now create a new item for this tag.
336                          */
337                         tas = proto_tree_add_text(tree, offset, 1,
338                             "%s", tag_desc);
339                         offset++;
340                         if (tag == TAG_END_OF_ATTRIBUTES) {
341                                 /*
342                                  * No more attributes.
343                                  */
344                                 break;
345                         }
346                 } else {
347                         /*
348                          * Value tag - get the name length.
349                          */
350                         if (!BYTES_ARE_IN_FRAME(offset + 1, 2)) {
351                                 /*
352                                  * We ran past the end of the frame.
353                                  * Quit (we need to be able to handle
354                                  * stuff that crosses frames to do more)
355                                  */
356                                 break;
357                         }
358                         name_length = pntohs(&pd[offset + 1]);
359
360                         /*
361                          * OK, get the value length.
362                          */
363                         if (!BYTES_ARE_IN_FRAME(offset + 1 + 2, name_length)) {
364                                 /*
365                                  * We ran past the end of the frame.
366                                  * Quit (we need to be able to handle
367                                  * stuff that crosses frames to do more)
368                                  */
369                                 break;
370                         }
371                         value_length = pntohs(&pd[offset + 1 + 2 + name_length]);
372
373                         /*
374                          * OK, does the value run past the end of the
375                          * frame?
376                          */
377                         if (!BYTES_ARE_IN_FRAME(offset + 1 + 2 + name_length + 2,
378                             value_length)) {
379                                 /*
380                                  * We ran past the end of the frame.
381                                  * Quit (we need to be able to handle
382                                  * stuff that crosses frames to do more)
383                                  */
384                                 break;
385                         }
386                         if (as_tree == NULL) {
387                                 /*
388                                  * OK, there's an attribute to hang
389                                  * under a delimiter tag, but we don't
390                                  * have a tree for that tag yet; create
391                                  * a tree.
392                                  */
393                                 as_tree = proto_item_add_subtree(tas,
394                                     ett_ipp_as);
395                                 attr_tree = as_tree;
396                         }
397
398                         switch (TAG_TYPE(tag)) {
399
400                         case TAG_TYPE_INTEGER:
401                                 if (name_length != 0) {
402                                         /*
403                                          * This is an attribute, not
404                                          * an additional value, so
405                                          * start a tree for it.
406                                          */
407                                         attr_tree = add_integer_tree(as_tree,
408                                             pd, offset, name_length,
409                                             value_length);
410                                 }
411                                 add_integer_value(tag, tag_desc, attr_tree, pd,
412                                     offset, name_length, value_length);
413                                 break;
414
415                         case TAG_TYPE_OCTETSTRING:
416                                 if (name_length != 0) {
417                                         /*
418                                          * This is an attribute, not
419                                          * an additional value, so
420                                          * start a tree for it.
421                                          */
422                                         attr_tree = add_octetstring_tree(as_tree,
423                                             pd, offset, name_length,
424                                             value_length);
425                                 }
426                                 add_octetstring_value(tag, tag_desc,
427                                     attr_tree, pd, offset, name_length,
428                                     value_length);
429                                 break;
430
431                         case TAG_TYPE_CHARSTRING:
432                                 if (name_length != 0) {
433                                         /*
434                                          * This is an attribute, not
435                                          * an additional value, so
436                                          * start a tree for it.
437                                          */
438                                         attr_tree = add_charstring_tree(as_tree,
439                                             pd, offset, name_length,
440                                             value_length);
441                                 }
442                                 add_charstring_value(tag, tag_desc,
443                                     attr_tree, pd, offset, name_length,
444                                     value_length);
445                                 break;
446                         }
447                         offset += 1 + 2 + name_length + 2 + value_length;
448                 }
449         }
450
451         return offset;
452 }
453
454 static proto_tree *
455 add_integer_tree(proto_tree *tree, const u_char *pd, int offset,
456     guint name_length, guint value_length)
457 {
458         proto_item *ti;
459
460         if (value_length != 4) {
461                 ti = proto_tree_add_text(tree, offset,
462                     1 + 2 + name_length + 2 + value_length,
463                     "%.*s: Invalid integer (length is %u, should be 4)",
464                     name_length, &pd[offset + 1 + 2],
465                     value_length);
466         } else {
467                 ti = proto_tree_add_text(tree, offset,
468                     1 + 2 + name_length + 2 + value_length,
469                     "%.*s: %u",
470                     name_length, &pd[offset + 1 + 2],
471                     pntohl(&pd[1 + 2 + name_length + 2]));
472         }
473         return proto_item_add_subtree(ti, ett_ipp_attr);
474 }
475
476 static void
477 add_integer_value(guint tag, gchar *tag_desc, proto_tree *tree,
478     const u_char *pd, int offset, guint name_length, guint value_length)
479 {
480         offset = add_value_head(tag, tag_desc, tree, pd, offset,
481             name_length, value_length);
482         if (value_length == 4) {
483                 proto_tree_add_text(tree, offset, value_length,
484                     "Value: %u", pntohl(&pd[1 + 2 + name_length + 2]));
485         }
486 }
487
488 static proto_tree *
489 add_octetstring_tree(proto_tree *tree, const u_char *pd, int offset,
490     guint name_length, guint value_length)
491 {
492         proto_item *ti;
493
494         ti = proto_tree_add_text(tree, offset,
495             1 + 2 + name_length + 2 + value_length,
496             "%.*s: %s",
497             name_length,
498             &pd[offset + 1 + 2]);
499             bytes_to_str(&pd[offset + 1 + 2 + name_length + 2], value_length);
500         return proto_item_add_subtree(ti, ett_ipp_attr);
501 }
502
503 static void
504 add_octetstring_value(guint tag, gchar *tag_desc, proto_tree *tree,
505     const u_char *pd, int offset, guint name_length, guint value_length)
506 {
507         offset = add_value_head(tag, tag_desc, tree, pd, offset,
508             name_length, value_length);
509         proto_tree_add_text(tree, offset, value_length,
510             "Value: %s", bytes_to_str(&pd[offset], value_length));
511 }
512
513 static proto_tree *
514 add_charstring_tree(proto_tree *tree, const u_char *pd, int offset,
515     guint name_length, guint value_length)
516 {
517         proto_item *ti;
518
519         ti = proto_tree_add_text(tree, offset,
520             1 + 2 + name_length + 2 + value_length,
521             "%.*s: %.*s",
522             name_length, &pd[offset + 1 + 2],
523             value_length, &pd[offset + 1 + 2 + name_length + 2]);
524         return proto_item_add_subtree(ti, ett_ipp_attr);
525 }
526
527 static void
528 add_charstring_value(guint tag, gchar *tag_desc, proto_tree *tree,
529     const u_char *pd, int offset, guint name_length, guint value_length)
530 {
531         offset = add_value_head(tag, tag_desc, tree, pd, offset,
532             name_length, value_length);
533         proto_tree_add_text(tree, offset, value_length,
534             "Value: %.*s", value_length, &pd[offset]);
535 }
536
537 static int
538 add_value_head(guint tag, gchar *tag_desc, proto_tree *tree,
539     const u_char *pd, int offset, guint name_length, guint value_length)
540 {
541         proto_tree_add_text(tree, offset, 1, "Tag: %s", tag_desc);
542         offset += 1;
543         proto_tree_add_text(tree, offset, 2, "Name length: %u",
544             name_length);
545         offset += 2;
546         if (name_length != 0) {
547                 proto_tree_add_text(tree, offset, name_length,
548                     "Name: %.*s", name_length, &pd[offset]);
549         }
550         offset += name_length;
551         proto_tree_add_text(tree, offset, 2, "Value length: %u",
552             value_length);
553         offset += 2;
554         return offset;
555 }
556
557 void
558 proto_register_ipp(void)
559 {
560 /*        static hf_register_info hf[] = {
561                 { &variable,
562                 { "Name",           "ipp.abbreviation", TYPE, VALS_POINTER }},
563         };*/
564         static gint *ett[] = {
565                 &ett_ipp,
566                 &ett_ipp_as,
567                 &ett_ipp_attr,
568         };
569
570         proto_ipp = proto_register_protocol("Internet Printing Protocol", "ipp");
571  /*       proto_register_field_array(proto_ipp, hf, array_length(hf));*/
572         proto_register_subtree_array(ett, array_length(ett));
573 }