]>
git.saurik.com Git - apple/network_cmds.git/blob - tftp.tproj/tftp.c
8e6e3f863e80e3eee99e06bcb91de911a35469aa
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1983, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
62 * TFTP User Program -- Protocol Machines
64 #include <sys/types.h>
65 #include <sys/socket.h>
68 #include <netinet/in.h>
70 #include <arpa/tftp.h>
83 extern struct sockaddr_in peeraddr
; /* filled in by main */
84 extern int f
; /* the opened socket */
88 extern int maxtimeout
;
90 extern jmp_buf toplevel
; /* filled in by main */
92 #define PKTSIZE SEGSIZE+4
97 static void nak
__P((int));
98 static int makerequest
__P((int, const char *, struct tftphdr
*, const char *));
99 static void printstats
__P((const char *, unsigned long));
100 static void startclock
__P((void));
101 static void stopclock
__P((void));
102 static void timer
__P((int));
103 static void tpacket
__P((const char *, struct tftphdr
*, int));
106 * Send the requested file.
109 tftp_sendfile(fd
, name
, mode
)
114 register struct tftphdr
*ap
; /* data and ack packets */
115 struct tftphdr
*r_init(), *dp
;
117 volatile int block
, size
, convert
;
118 volatile unsigned long amount
;
119 struct sockaddr_in from
;
123 startclock(); /* start stat's clock */
124 dp
= r_init(); /* reset fillbuf/read-ahead code */
125 ap
= (struct tftphdr
*)ackbuf
;
126 file
= fdopen(fd
, "r");
127 convert
= !strcmp(mode
, "netascii");
131 signal(SIGALRM
, timer
);
134 size
= makerequest(WRQ
, name
, dp
, mode
) - 4;
136 /* size = read(fd, dp->th_data, SEGSIZE); */
137 size
= readit(file
, &dp
, convert
);
142 dp
->th_opcode
= htons((u_short
)DATA
);
143 dp
->th_block
= htons((u_short
)block
);
146 (void) setjmp(timeoutbuf
);
149 tpacket("sent", dp
, size
+ 4);
150 n
= sendto(f
, dp
, size
+ 4, 0,
151 (struct sockaddr
*)&peeraddr
, sizeof(peeraddr
));
153 perror("tftp: sendto");
156 read_ahead(file
, convert
);
160 fromlen
= sizeof(from
);
161 n
= recvfrom(f
, ackbuf
, sizeof(ackbuf
), 0,
162 (struct sockaddr
*)&from
, &fromlen
);
166 perror("tftp: recvfrom");
169 peeraddr
.sin_port
= from
.sin_port
; /* added */
171 tpacket("received", ap
, n
);
172 /* should verify packet came from server */
173 ap
->th_opcode
= ntohs(ap
->th_opcode
);
174 ap
->th_block
= ntohs(ap
->th_block
);
175 if (ap
->th_opcode
== ERROR
) {
176 printf("Error code %d: %s\n", ap
->th_code
,
180 if (ap
->th_opcode
== ACK
) {
183 if (ap
->th_block
== block
) {
186 /* On an error, try to synchronize
191 printf("discarded %d packets\n",
194 if (ap
->th_block
== (block
-1)) {
202 } while (size
== SEGSIZE
|| block
== 1);
207 printstats("Sent", amount
);
214 recvfile(fd
, name
, mode
)
219 register struct tftphdr
*ap
;
220 struct tftphdr
*dp
, *w_init();
222 volatile int block
, size
, firsttrip
;
223 volatile unsigned long amount
;
224 struct sockaddr_in from
;
227 volatile int convert
; /* true if converting crlf -> lf */
231 ap
= (struct tftphdr
*)ackbuf
;
232 file
= fdopen(fd
, "w");
233 convert
= !strcmp(mode
, "netascii");
238 signal(SIGALRM
, timer
);
241 size
= makerequest(RRQ
, name
, ap
, mode
);
244 ap
->th_opcode
= htons((u_short
)ACK
);
245 ap
->th_block
= htons((u_short
)(block
));
250 (void) setjmp(timeoutbuf
);
253 tpacket("sent", ap
, size
);
254 if (sendto(f
, ackbuf
, size
, 0, (struct sockaddr
*)&peeraddr
,
255 sizeof(peeraddr
)) != size
) {
257 perror("tftp: sendto");
260 write_behind(file
, convert
);
264 fromlen
= sizeof(from
);
265 n
= recvfrom(f
, dp
, PKTSIZE
, 0,
266 (struct sockaddr
*)&from
, &fromlen
);
270 perror("tftp: recvfrom");
273 peeraddr
.sin_port
= from
.sin_port
; /* added */
275 tpacket("received", dp
, n
);
276 /* should verify client address */
277 dp
->th_opcode
= ntohs(dp
->th_opcode
);
278 dp
->th_block
= ntohs(dp
->th_block
);
279 if (dp
->th_opcode
== ERROR
) {
280 printf("Error code %d: %s\n", dp
->th_code
,
284 if (dp
->th_opcode
== DATA
) {
287 if (dp
->th_block
== block
) {
288 break; /* have next packet */
290 /* On an error, try to synchronize
295 printf("discarded %d packets\n", j
);
297 if (dp
->th_block
== (block
-1)) {
298 goto send_ack
; /* resend ack */
302 /* size = write(fd, dp->th_data, n - 4); */
303 size
= writeit(file
, &dp
, n
- 4, convert
);
309 } while (size
== SEGSIZE
);
310 abort
: /* ok to ack, since user */
311 ap
->th_opcode
= htons((u_short
)ACK
); /* has seen err msg */
312 ap
->th_block
= htons((u_short
)block
);
313 (void) sendto(f
, ackbuf
, 4, 0, (struct sockaddr
*)&peeraddr
,
315 write_behind(file
, convert
); /* flush last buffer */
319 printstats("Received", amount
);
323 makerequest(request
, name
, tp
, mode
)
331 tp
->th_opcode
= htons((u_short
)request
);
339 return (cp
- (char *)tp
);
346 { EUNDEF
, "Undefined error code" },
347 { ENOTFOUND
, "File not found" },
348 { EACCESS
, "Access violation" },
349 { ENOSPACE
, "Disk full or allocation exceeded" },
350 { EBADOP
, "Illegal TFTP operation" },
351 { EBADID
, "Unknown transfer ID" },
352 { EEXISTS
, "File already exists" },
353 { ENOUSER
, "No such user" },
358 * Send a nak packet (error message).
359 * Error code passed in is one of the
360 * standard TFTP codes, or a UNIX errno
367 register struct errmsg
*pe
;
368 register struct tftphdr
*tp
;
372 tp
= (struct tftphdr
*)ackbuf
;
373 tp
->th_opcode
= htons((u_short
)ERROR
);
374 tp
->th_code
= htons((u_short
)error
);
375 for (pe
= errmsgs
; pe
->e_code
>= 0; pe
++)
376 if (pe
->e_code
== error
)
378 if (pe
->e_code
< 0) {
379 pe
->e_msg
= strerror(error
- 100);
380 tp
->th_code
= EUNDEF
;
382 strcpy(tp
->th_msg
, pe
->e_msg
);
383 length
= strlen(pe
->e_msg
) + 4;
385 tpacket("sent", tp
, length
);
386 if (sendto(f
, ackbuf
, length
, 0, (struct sockaddr
*)&peeraddr
,
387 sizeof(peeraddr
)) != length
)
397 static char *opcodes
[] =
398 { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" };
399 register char *cp
, *file
;
400 u_short op
= ntohs(tp
->th_opcode
);
403 if (op
< RRQ
|| op
> ERROR
)
404 printf("%s opcode=%x ", s
, op
);
406 printf("%s %s ", s
, opcodes
[op
]);
412 file
= cp
= tp
->th_stuff
;
413 cp
= index(cp
, '\0');
414 printf("<file=%s, mode=%s>\n", file
, cp
+ 1);
418 printf("<block=%d, %d bytes>\n", ntohs(tp
->th_block
), n
- 4);
422 printf("<block=%d>\n", ntohs(tp
->th_block
));
426 printf("<code=%d, msg=%s>\n", ntohs(tp
->th_code
), tp
->th_msg
);
431 struct timeval tstart
;
432 struct timeval tstop
;
438 (void)gettimeofday(&tstart
, NULL
);
445 (void)gettimeofday(&tstop
, NULL
);
449 printstats(direction
, amount
)
450 const char *direction
;
451 unsigned long amount
;
454 /* compute delta in 1/10's second units */
455 delta
= ((tstop
.tv_sec
*10.)+(tstop
.tv_usec
/100000)) -
456 ((tstart
.tv_sec
*10.)+(tstart
.tv_usec
/100000));
457 delta
= delta
/10.; /* back to seconds */
458 printf("%s %d bytes in %.1f seconds", direction
, amount
, delta
);
460 printf(" [%.0f bits/sec]", (amount
*8.)/delta
);
470 if (timeout
>= maxtimeout
) {
471 printf("Transfer timed out.\n");
472 longjmp(toplevel
, -1);
474 longjmp(timeoutbuf
, 1);