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