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