Update from Paul Ionescu to set the reported length of the tvbuff for
[obnox/wireshark/wip.git] / packet-lpd.c
1 /* packet-lpd.c
2  * Routines for LPR and LPRng packet disassembly
3  * Gilbert Ramirez <gram@xiexie.org>
4  *
5  * $Id: packet-lpd.c,v 1.25 2001/01/06 00:02:41 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <string.h>
36
37 #include <glib.h>
38 #include "packet.h"
39
40 #define TCP_PORT_PRINTER                515
41
42 static int proto_lpd = -1;
43 static int hf_lpd_response = -1;
44 static int hf_lpd_request = -1;
45
46 static gint ett_lpd = -1;
47
48 enum lpr_type { request, response, unknown };
49
50 static gint find_printer_string(tvbuff_t *tvb, int offset);
51
52 static void
53 dissect_lpd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
54 {
55         proto_tree      *lpd_tree;
56         proto_item      *ti;
57         enum lpr_type   lpr_packet_type;
58         guint8          code;
59         gint            printer_len;
60
61         /* This information comes from the LPRng HOWTO, which also describes
62                 RFC 1179. http://www.astart.com/lprng/LPRng-HOWTO.html */
63         char            *lpd_client_code[] = {
64                 "Unknown command",
65                 "LPC: start print / jobcmd: abort",
66                 "LPR: transfer a printer job / jobcmd: receive control file",
67                 "LPQ: print short form of queue status / jobcmd: receive data file",
68                 "LPQ: print long form of queue status",
69                 "LPRM: remove jobs",
70                 "LPRng lpc: do control operation",
71                 "LPRng lpr: transfer a block format print job",
72                 "LPRng lpc: secure command transfer",
73                 "LPRng lpq: verbose status information"
74         };
75         char            *lpd_server_code[] = {
76                 "Success: accepted, proceed",
77                 "Queue not accepting jobs",
78                 "Queue temporarily full, retry later",
79                 "Bad job format, do not retry"
80         };
81
82         CHECK_DISPLAY_AS_DATA(proto_lpd, tvb, pinfo, tree);
83
84         pinfo->current_proto = "LPD";
85
86         if (check_col(pinfo->fd, COL_PROTOCOL))
87                 col_set_str(pinfo->fd, COL_PROTOCOL, "LPD");
88         if (check_col(pinfo->fd, COL_INFO))
89                 col_clear(pinfo->fd, COL_INFO);
90
91         /* rfc1179 states that all responses are 1 byte long */
92         code = tvb_get_guint8(tvb, 0);
93         if (tvb_reported_length(tvb) == 1) {
94                 lpr_packet_type = response;
95         }
96         else if (code <= 9) {
97                 lpr_packet_type = request;
98         }
99         else {
100                 lpr_packet_type = unknown;
101         }
102
103         if (check_col(pinfo->fd, COL_INFO)) {
104                 if (lpr_packet_type == request) {
105                         col_add_str(pinfo->fd, COL_INFO, lpd_client_code[code]);
106                 }
107                 else if (lpr_packet_type == response) {
108                         col_set_str(pinfo->fd, COL_INFO, "LPD response");
109                 }
110                 else {
111                         col_set_str(pinfo->fd, COL_INFO, "LPD continuation");
112                 }
113         }
114
115         if (tree) {
116                 ti = proto_tree_add_item(tree, proto_lpd, tvb, 0, 
117                                          tvb_length(tvb), FALSE);
118                 lpd_tree = proto_item_add_subtree(ti, ett_lpd);
119
120                 if (lpr_packet_type == response) {
121                   proto_tree_add_boolean_hidden(lpd_tree, hf_lpd_response,
122                                                 tvb, 0, 0, TRUE);
123                 } else {
124                   proto_tree_add_boolean_hidden(lpd_tree, hf_lpd_request,
125                                                 tvb, 0, 0, TRUE);
126                 }
127
128                 if (lpr_packet_type == request) {
129                         printer_len = find_printer_string(tvb, 1);
130
131                         if (code <= 9 && printer_len != -1) {
132                                 proto_tree_add_text(lpd_tree, tvb, 0, 1,
133                                         lpd_client_code[code]);
134                                 proto_tree_add_text(lpd_tree, tvb, 1, printer_len,
135                                          "Printer/options: %s",
136                                          tvb_format_text(tvb, 1, printer_len));
137                         }
138                         else {
139                                 dissect_data(tvb, 0, pinfo, tree);
140                         }
141                 }
142                 else if (lpr_packet_type == response) {
143                         if (code <= 3) {
144                                 proto_tree_add_text(lpd_tree, tvb, 0, 1,
145                                         "Response: %s", lpd_server_code[code]);
146                         }
147                         else {
148                                 dissect_data(tvb, 0, pinfo, tree);
149                         }
150                 }
151                 else {
152                         dissect_data(tvb, 0, pinfo, tree);
153                 }
154         }
155 }
156
157
158 static gint
159 find_printer_string(tvbuff_t *tvb, int offset)
160 {
161         int     i;
162
163         /* try to find end of string, either '\n' or '\0' */
164         i = tvb_find_guint8(tvb, offset, -1, '\0');
165         if (i == -1)
166                 i = tvb_find_guint8(tvb, offset, -1, '\n');
167         if (i == -1)
168                 return -1;
169         return i - offset;      /* length of string */
170 }
171
172
173 void
174 proto_register_lpd(void)
175 {
176   static hf_register_info hf[] = {
177     { &hf_lpd_response,
178       { "Response",           "lpd.response",           
179         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
180         "TRUE if LPD response" }},
181
182     { &hf_lpd_request,
183       { "Request",            "lpd.request",
184         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
185         "TRUE if LPD request" }}
186   };
187   static gint *ett[] = {
188     &ett_lpd,
189   };
190
191   proto_lpd = proto_register_protocol("Line Printer Daemon Protocol", "LPD", "lpd");
192   proto_register_field_array(proto_lpd, hf, array_length(hf));
193   proto_register_subtree_array(ett, array_length(ett));
194 }
195
196 void
197 proto_reg_handoff_lpd(void)
198 {
199   dissector_add("tcp.port", TCP_PORT_PRINTER, &dissect_lpd);
200 }