4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 #include <arpa/tftp.h>
36 extern char *dlc_header
;
37 static char *tftperror(unsigned short);
38 static char *show_type(int);
39 static char *tftp_parse_oack(char *, size_t, struct tftp_options
*);
42 interpret_tftp(int flags
, void *data
, int fraglen
)
45 extern int src_port
, dst_port
;
46 int blocksize
= fraglen
- 4;
47 struct tftp_options opts
;
49 struct tftphdr
*tftp
= data
;
54 switch (ntohs(tftp
->th_opcode
)) {
57 add_transient(src_port
, interpret_tftp
);
60 del_transient(src_port
);
63 tt
= is_transient(dst_port
);
65 tt
->blksize
= opts
.blksize
;
68 tt
= is_transient(dst_port
);
70 opts
.blksize
= tt
->blksize
;
77 switch (ntohs(tftp
->th_opcode
)) {
79 name
= (char *)&tftp
->th_stuff
;
80 mode
= name
+ (strlen(name
) + 1);
81 (void) sprintf(get_sum_line(),
82 "TFTP Read \"%s\" (%s)", name
, mode
);
85 name
= (char *)&tftp
->th_stuff
;
86 mode
= name
+ (strlen(name
) + 1);
87 (void) sprintf(get_sum_line(),
88 "TFTP Write \"%s\" (%s)", name
, mode
);
91 (void) sprintf(get_sum_line(),
92 "TFTP Data block %u (%d bytes)%s",
93 ntohs(tftp
->th_block
), blocksize
,
94 blocksize
< opts
.blksize
? " (last block)":"");
97 (void) sprintf(get_sum_line(), "TFTP Ack block %d",
98 ntohs(tftp
->th_block
));
101 (void) sprintf(get_sum_line(), "TFTP Error: %s",
102 tftperror(ntohs(tftp
->th_code
)));
105 (void) sprintf(get_sum_line(), "TFTP OACK: %s",
106 tftp_parse_oack((char *)&tftp
->th_stuff
,
107 fraglen
- sizeof (tftp
->th_opcode
), &opts
));
109 tt
->blksize
= opts
.blksize
;
114 if (flags
& F_DTAIL
) {
115 show_header("TFTP: ", "Trivial File Transfer Protocol",
118 (void) sprintf(get_line((char *)(uintptr_t)tftp
->th_opcode
-
119 dlc_header
, 2), "Opcode = %d (%s)", ntohs(tftp
->th_opcode
),
120 show_type(ntohs(tftp
->th_opcode
)));
122 switch (ntohs(tftp
->th_opcode
)) {
125 name
= (char *)&tftp
->th_stuff
;
126 mode
= name
+ (strlen(name
) + 1);
128 get_line(name
- dlc_header
, strlen(name
) + 1),
129 "File name = \"%s\"", name
);
131 get_line(mode
- dlc_header
, strlen(mode
) + 1),
132 "Transfer mode = %s", mode
);
136 (void) sprintf(get_line(
137 (char *)(uintptr_t)tftp
->th_block
- dlc_header
, 2),
138 "Data block = %d%s", ntohs(tftp
->th_block
),
139 blocksize
< opts
.blksize
? " (last block)" : "");
140 (void) sprintf(get_line(
141 (char *)(uintptr_t)tftp
->th_data
- dlc_header
,
142 blocksize
), "[ %d bytes of data ]", blocksize
);
146 (void) sprintf(get_line(
147 (char *)(uintptr_t)tftp
->th_block
- dlc_header
, 2),
148 "Acknowledge block = %d", ntohs(tftp
->th_block
));
152 (void) sprintf(get_line(
153 (char *)(uintptr_t)tftp
->th_code
- dlc_header
, 2),
154 "Error = %d (%s)", ntohs(tftp
->th_code
),
155 tftperror(ntohs(tftp
->th_code
)));
156 (void) sprintf(get_line(
157 (char *)(uintptr_t)tftp
->th_data
-
158 dlc_header
, strlen(tftp
->th_data
) + 1),
159 "Error string = \"%s\"", tftp
->th_data
);
162 (void) sprintf(get_line(
163 (char *)(uintptr_t)tftp
->th_code
- dlc_header
, 2),
165 tftp_parse_oack((char *)&tftp
->th_stuff
,
166 fraglen
- sizeof (tftp
->th_opcode
), &opts
));
168 tt
->blksize
= opts
.blksize
;
180 case RRQ
: return ("read request");
181 case WRQ
: return ("write request");
182 case DATA
: return ("data packet");
183 case ACK
: return ("acknowledgement");
184 case ERROR
: return ("error");
185 case OACK
: return ("option acknowledgement");
191 tftperror(unsigned short code
)
193 static char buf
[128];
196 case EUNDEF
: return ("not defined");
197 case ENOTFOUND
: return ("file not found");
198 case EACCESS
: return ("access violation");
199 case ENOSPACE
: return ("disk full or allocation exceeded");
200 case EBADOP
: return ("illegal TFTP operation");
201 case EBADID
: return ("unknown transfer ID");
202 case EEXISTS
: return ("file already exists");
203 case ENOUSER
: return ("no such user");
205 (void) sprintf(buf
, "%d", code
);
211 tftp_parse_oack(char *buf
, size_t size
, struct tftp_options
*opts
)
213 static char tftp_options
[128];
216 tftp_options
[0] = '\0';
219 while (size
> 0 && idx
< sizeof (tftp_options
)) {
221 tftp_options
[idx
++] = ' ';
222 tftp_options
[idx
] = '\0';
226 if (idx
+ strnlen(buf
, size
) + 1 > sizeof (tftp_options
))
228 for (i
= 0; i
< size
; i
++) {
229 tftp_options
[idx
] = buf
[i
];
230 if (tftp_options
[idx
] == '\0') {
238 * RFC 2348 requires this case in-sensitive.
240 if (strcasecmp(buf
, "blksize") == 0) {
241 int blksize
= strtol(buf
+ i
, NULL
, 0);
244 opts
->blksize
= blksize
;
248 /* can we store separator? */
249 if (idx
+ 3 > sizeof (tftp_options
))
251 strcat(tftp_options
, ": ");
255 if (idx
+ strnlen(buf
, size
) + 1 > sizeof (tftp_options
))
258 for (i
= 0; i
< size
; i
++) {
259 tftp_options
[idx
] = buf
[i
];
260 if (tftp_options
[idx
] == '\0') {
269 return (tftp_options
);