If we get an exception when dissecting a packet, append "[Short Frame]"
[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.23 2000/11/19 08:53:59 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 char* find_printer_string(const u_char *pd, int offset, int frame_length);
51
52 static void
53 dissect_lpd(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
54 {
55         proto_tree      *lpd_tree;
56         proto_item      *ti;
57         enum lpr_type   lpr_packet_type;
58         char            *printer;
59
60         /* This information comes from the LPRng HOWTO, which also describes
61                 RFC 1179. http://www.astart.com/lprng/LPRng-HOWTO.html */
62         char            *lpd_client_code[] = {
63                 "Unknown command",
64                 "LPC: start print / jobcmd: abort",
65                 "LPR: transfer a printer job / jobcmd: receive control file",
66                 "LPQ: print short form of queue status / jobcmd: receive data file",
67                 "LPQ: print long form of queue status",
68                 "LPRM: remove jobs",
69                 "LPRng lpc: do control operation",
70                 "LPRng lpr: transfer a block format print job",
71                 "LPRng lpc: secure command transfer",
72                 "LPRng lpq: verbose status information"
73         };
74         char            *lpd_server_code[] = {
75                 "Success: accepted, proceed",
76                 "Queue not accepting jobs",
77                 "Queue temporarily full, retry later",
78                 "Bad job format, do not retry"
79         };
80
81         OLD_CHECK_DISPLAY_AS_DATA(proto_lpd, pd, offset, fd, tree);
82
83         /* rfc1179 states that all responses are 1 byte long */
84         if (END_OF_FRAME == 1) {
85                 lpr_packet_type = response;
86         }
87         else if (pd[offset] <= 9) {
88                 lpr_packet_type = request;
89         }
90         else {
91                 lpr_packet_type = unknown;
92         }
93
94         if (check_col(fd, COL_PROTOCOL))
95                 col_set_str(fd, COL_PROTOCOL, "LPD");
96         if (check_col(fd, COL_INFO)) {
97                 if (lpr_packet_type == request) {
98                         col_add_str(fd, COL_INFO, lpd_client_code[pd[offset]]);
99                 }
100                 else if (lpr_packet_type == response) {
101                         col_set_str(fd, COL_INFO, "LPD response");
102                 }
103                 else {
104                         col_set_str(fd, COL_INFO, "LPD continuation");
105                 }
106         }
107
108         if (tree) {
109                 ti = proto_tree_add_item(tree, proto_lpd, NullTVB, offset, 
110                                          END_OF_FRAME, FALSE);
111                 lpd_tree = proto_item_add_subtree(ti, ett_lpd);
112
113                 if (lpr_packet_type == response) {
114                   proto_tree_add_boolean_hidden(lpd_tree, hf_lpd_response, NullTVB, 0, 0, TRUE);
115                 } else {
116                   proto_tree_add_boolean_hidden(lpd_tree, hf_lpd_request, NullTVB, 0, 0, TRUE);
117                 }
118
119                 if (lpr_packet_type == request) {
120                         printer = find_printer_string(pd, offset+1, END_OF_FRAME);
121
122                         if (pd[offset] <= 9 && printer) {
123                                 proto_tree_add_text(lpd_tree, NullTVB, offset,          1,
124                                         lpd_client_code[pd[offset]]);
125                                 proto_tree_add_text(lpd_tree, NullTVB, offset+1,
126                                         strlen(printer), "Printer/options: %s", printer);
127                         }
128                         else {
129                                 old_dissect_data(pd, offset, fd, tree);
130                         }
131
132                         if (printer) 
133                                 g_free(printer);
134                 }
135                 else if (lpr_packet_type == response) {
136                         int response = pd[offset];
137
138                         if (response <= 3) {
139                                 proto_tree_add_text(lpd_tree, NullTVB, offset, 1, "Response: %s",
140                                         lpd_server_code[response]);
141                         }
142                         else {
143                                 old_dissect_data(pd, offset, fd, tree);
144                         }
145                 }
146                 else {
147                                 old_dissect_data(pd, offset, fd, tree);
148                 }
149         }
150 }
151
152
153 static char*
154 find_printer_string(const u_char *pd, int offset, int frame_length)
155 {
156         int     i, final_offset;
157         char    c;
158         char    *string;
159         int     bytes;
160
161         final_offset = offset + frame_length;
162
163         /* try to find end of string, either 0x0a or 0x00 */
164         for (i = offset; i < final_offset; i++) {
165                 c = pd[i];
166                 if (c == '\x00' || c == '\x0a') {
167                         bytes = i - offset;
168                         string = g_malloc(bytes+1);
169                         memcpy(string, &pd[offset], bytes);
170                         string[bytes] = 0;
171                         return string;
172                 }
173         }
174
175         return NULL;
176 }
177
178
179 void
180 proto_register_lpd(void)
181 {
182   static hf_register_info hf[] = {
183     { &hf_lpd_response,
184       { "Response",           "lpd.response",           
185         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
186         "TRUE if LPD response" }},
187
188     { &hf_lpd_request,
189       { "Request",            "lpd.request",
190         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
191         "TRUE if LPD request" }}
192   };
193   static gint *ett[] = {
194     &ett_lpd,
195   };
196
197   proto_lpd = proto_register_protocol("Line Printer Daemon Protocol", "lpd");
198   proto_register_field_array(proto_lpd, hf, array_length(hf));
199   proto_register_subtree_array(ett, array_length(ett));
200 }
201
202 void
203 proto_reg_handoff_lpd(void)
204 {
205   old_dissector_add("tcp.port", TCP_PORT_PRINTER, &dissect_lpd);
206 }