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