Rename the routines that handle dissector tables with unsigned integer
[obnox/wireshark/wip.git] / epan / dissectors / packet-lpd.c
1 /* packet-lpd.c
2  * Routines for LPR and LPRng packet disassembly
3  * Gilbert Ramirez <gram@alumni.rice.edu>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
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.
15  *
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.
20  *
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.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32
33 #define TCP_PORT_PRINTER                515
34
35 static int proto_lpd = -1;
36 static int hf_lpd_response = -1;
37 static int hf_lpd_request = -1;
38
39 static gint ett_lpd = -1;
40
41 enum lpr_type { request, response, unknown };
42
43 static gint find_printer_string(tvbuff_t *tvb, int offset);
44
45 static dissector_handle_t data_handle;
46
47 static void
48 dissect_lpd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
49 {
50         proto_tree      *lpd_tree;
51         proto_item      *ti, *hidden_item;
52         enum lpr_type   lpr_packet_type;
53         guint8          code;
54         gint            printer_len;
55
56         /* This information comes from the LPRng HOWTO, which also describes
57                 RFC 1179. http://www.astart.com/lprng/LPRng-HOWTO.html */
58         static const value_string lpd_client_code[] = {
59                 { 1, "LPC: start print / jobcmd: abort" },
60                 { 2, "LPR: transfer a printer job / jobcmd: receive control file" },
61                 { 3, "LPQ: print short form of queue status / jobcmd: receive data file" },
62                 { 4, "LPQ: print long form of queue status" },
63                 { 5, "LPRM: remove jobs" },
64                 { 6, "LPRng lpc: do control operation" },
65                 { 7, "LPRng lpr: transfer a block format print job" },
66                 { 8, "LPRng lpc: secure command transfer" },
67                 { 9, "LPRng lpq: verbose status information" },
68                 { 0, NULL }
69         };
70         static const value_string lpd_server_code[] = {
71                 { 0, "Success: accepted, proceed" },
72                 { 1, "Queue not accepting jobs" },
73                 { 2, "Queue temporarily full, retry later" },
74                 { 3, "Bad job format, do not retry" },
75                 { 0, NULL }
76         };
77
78         col_set_str(pinfo->cinfo, COL_PROTOCOL, "LPD");
79         col_clear(pinfo->cinfo, COL_INFO);
80
81         /* rfc1179 states that all responses are 1 byte long */
82         code = tvb_get_guint8(tvb, 0);
83         if (tvb_reported_length(tvb) == 1) {
84                 lpr_packet_type = response;
85         }
86         else if (code <= 9) {
87                 lpr_packet_type = request;
88         }
89         else {
90                 lpr_packet_type = unknown;
91         }
92
93         if (check_col(pinfo->cinfo, COL_INFO)) {
94                 if (lpr_packet_type == request && code !=0) {
95                         col_add_str(pinfo->cinfo, COL_INFO, val_to_str(code, lpd_client_code, "Unknown client code: %u"));
96                 }
97                 else if (lpr_packet_type == response) {
98                         col_set_str(pinfo->cinfo, COL_INFO, "LPD response");
99                 }
100                 else {
101                         col_set_str(pinfo->cinfo, COL_INFO, "LPD continuation");
102                 }
103         }
104
105         if (tree) {
106                 ti = proto_tree_add_item(tree, proto_lpd, tvb, 0, -1, FALSE);
107                 lpd_tree = proto_item_add_subtree(ti, ett_lpd);
108
109                 if (lpr_packet_type == response) {
110                   hidden_item = proto_tree_add_boolean(lpd_tree, hf_lpd_response,
111                                                 tvb, 0, 0, TRUE);
112                 } else {
113                   hidden_item = proto_tree_add_boolean(lpd_tree, hf_lpd_request,
114                                                 tvb, 0, 0, TRUE);
115                 }
116                 PROTO_ITEM_SET_HIDDEN(hidden_item);
117
118                 if (lpr_packet_type == request) {
119                         printer_len = find_printer_string(tvb, 1);
120
121                         if (code <= 9 && printer_len != -1) {
122                                 proto_tree_add_text(lpd_tree, tvb, 0, 1, "%s",
123                                         val_to_str(code, lpd_client_code, "Unknown client code: %u"));
124                                 proto_tree_add_text(lpd_tree, tvb, 1, printer_len,
125                                          "Printer/options: %s",
126                                          tvb_format_text(tvb, 1, printer_len));
127                         }
128                         else {
129                                 call_dissector(data_handle,tvb, pinfo, lpd_tree);
130                         }
131                 }
132                 else if (lpr_packet_type == response) {
133                         if (code <= 3) {
134                                 proto_tree_add_text(lpd_tree, tvb, 0, 1,
135                                         "Response: %s", val_to_str(code, lpd_server_code, "Unknown server code: %u"));
136                         }
137                         else {
138                                 call_dissector(data_handle,tvb, pinfo, lpd_tree);
139                         }
140                 }
141                 else {
142                         call_dissector(data_handle,tvb, pinfo, lpd_tree);
143                 }
144         }
145 }
146
147
148 static gint
149 find_printer_string(tvbuff_t *tvb, int offset)
150 {
151         int     i;
152
153         /* try to find end of string, either '\n' or '\0' */
154         i = tvb_find_guint8(tvb, offset, -1, '\0');
155         if (i == -1)
156                 i = tvb_find_guint8(tvb, offset, -1, '\n');
157         if (i == -1)
158                 return -1;
159         return i - offset;      /* length of string */
160 }
161
162
163 void
164 proto_register_lpd(void)
165 {
166   static hf_register_info hf[] = {
167     { &hf_lpd_response,
168       { "Response",           "lpd.response",
169         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
170         "TRUE if LPD response", HFILL }},
171
172     { &hf_lpd_request,
173       { "Request",            "lpd.request",
174         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
175         "TRUE if LPD request", HFILL }}
176   };
177   static gint *ett[] = {
178     &ett_lpd,
179   };
180
181   proto_lpd = proto_register_protocol("Line Printer Daemon Protocol", "LPD", "lpd");
182   proto_register_field_array(proto_lpd, hf, array_length(hf));
183   proto_register_subtree_array(ett, array_length(ett));
184 }
185
186 void
187 proto_reg_handoff_lpd(void)
188 {
189   dissector_handle_t lpd_handle;
190
191   lpd_handle = create_dissector_handle(dissect_lpd, proto_lpd);
192   dissector_add_uint("tcp.port", TCP_PORT_PRINTER, lpd_handle);
193   data_handle = find_dissector("data");
194 }