Include <time.h> to declare "gmtime()".
[obnox/wireshark/wip.git] / packet-lpd.c
1 /* packet-lpd.c
2  * Routines for LPR and LPRng packet disassembly
3  * Gilbert Ramirez <gram@verdict.uthscsa.edu>
4  *
5  * $Id: packet-lpd.c,v 1.14 1999/12/15 23:47:30 gram Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@unicom.net>
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 static int proto_lpd = -1;
41 static int hf_lpd_response = -1;
42 static int hf_lpd_request = -1;
43
44 static gint ett_lpd = -1;
45
46 enum lpr_type { request, response, unknown };
47
48 static char* find_printer_string(const u_char *pd, int offset, int frame_length);
49
50 void
51 dissect_lpd(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
52 {
53         proto_tree      *lpd_tree;
54         proto_item      *ti;
55         enum lpr_type   lpr_packet_type;
56         char            *printer;
57
58         /* This information comes from the LPRng HOWTO, which also describes
59                 RFC 1179. http://www.astart.com/lprng/LPRng-HOWTO.html */
60         char            *lpd_client_code[] = {
61                 "Unknown command",
62                 "LPC: start print",
63                 "LPR: transfer a printer job",
64                 "LPQ: print short form of queue status",
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         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
80         if (pd[offset+1] == '\n') {
81                 lpr_packet_type = response;
82         }
83         else if (pd[offset] <= 9) {
84                 lpr_packet_type = request;
85         }
86         else {
87                 lpr_packet_type = unknown;
88         }
89
90         if (check_col(fd, COL_PROTOCOL))
91                 col_add_str(fd, COL_PROTOCOL, "LPD");
92         if (check_col(fd, COL_INFO)) {
93                 if (lpr_packet_type == request) {
94                         col_add_str(fd, COL_INFO, lpd_client_code[pd[offset]]);
95                 }
96                 else if (lpr_packet_type == response) {
97                         col_add_str(fd, COL_INFO, "LPD response");
98                 }
99                 else {
100                         col_add_str(fd, COL_INFO, "LPD continuation");
101                 }
102         }
103
104         if (tree) {
105                 ti = proto_tree_add_item(tree, proto_lpd, offset, 
106                                          END_OF_FRAME, NULL);
107                 lpd_tree = proto_item_add_subtree(ti, ett_lpd);
108
109                 if (lpr_packet_type == response) {
110                   proto_tree_add_item_hidden(lpd_tree, hf_lpd_response, 0, 0, TRUE);
111                 } else {
112                   proto_tree_add_item_hidden(lpd_tree, hf_lpd_request, 0, 0, TRUE);
113                 }
114
115                 if (lpr_packet_type == request) {
116                         printer = find_printer_string(pd, offset+1, END_OF_FRAME);
117
118                         if (pd[offset] <= 9 && printer) {
119                                 proto_tree_add_text(lpd_tree, offset,           1,
120                                         lpd_client_code[pd[offset]]);
121                                 proto_tree_add_text(lpd_tree, offset+1,
122                                         strlen(printer), "Printer/options: %s", printer);
123                         }
124                         else {
125                                 dissect_data(pd, offset, fd, tree);
126                         }
127
128                         if (printer) 
129                                 g_free(printer);
130                 }
131                 else if (lpr_packet_type == response) {
132                         int response = pd[offset];
133
134                         if (response <= 3) {
135                                 proto_tree_add_text(lpd_tree, offset, 2, "Response: %s",
136                                         lpd_server_code[response]);
137                         }
138                         else {
139                                 dissect_data(pd, offset, fd, tree);
140                         }
141                 }
142                 else {
143                                 dissect_data(pd, offset, fd, tree);
144                 }
145         }
146 }
147
148
149 static char*
150 find_printer_string(const u_char *pd, int offset, int frame_length)
151 {
152         int     i, final_offset;
153         char    c;
154         char    *string;
155         int     bytes;
156
157         final_offset = offset + frame_length;
158
159         /* try to find end of string, either 0x0a or 0x00 */
160         for (i = offset; i < final_offset; i++) {
161                 c = pd[i];
162                 if (c == '\x00' || c == '\x0a') {
163                         bytes = i - offset;
164                         string = g_malloc(bytes+1);
165                         memcpy(string, &pd[offset], bytes);
166                         string[bytes] = 0;
167                         return string;
168                 }
169         }
170
171         return NULL;
172 }
173
174
175 void
176 proto_register_lpd(void)
177 {
178   static hf_register_info hf[] = {
179     { &hf_lpd_response,
180       { "Response",           "lpd.response",           
181         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
182         "TRUE if LPD response" }},
183
184     { &hf_lpd_request,
185       { "Request",            "lpd.request",
186         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
187         "TRUE if LPD request" }}
188   };
189   static gint *ett[] = {
190     &ett_lpd,
191   };
192
193   proto_lpd = proto_register_protocol("Line Printer Daemon Protocol", "lpd");
194   proto_register_field_array(proto_lpd, hf, array_length(hf));
195   proto_register_subtree_array(ett, array_length(ett));
196 }
197