1 /* $NetBSD: ftp.c,v 1.152 2007/07/22 05:02:50 lukem Exp $ */
4 * Copyright (c) 1996-2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
40 * Copyright (c) 1985, 1989, 1993, 1994
41 * The Regents of the University of California. All rights reserved.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * Copyright (C) 1997 and 1998 WIDE Project.
70 * All rights reserved.
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
75 * 1. Redistributions of source code must retain the above copyright
76 * notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright
78 * notice, this list of conditions and the following disclaimer in the
79 * documentation and/or other materials provided with the distribution.
80 * 3. Neither the name of the project nor the names of its contributors
81 * may be used to endorse or promote products derived from this software
82 * without specific prior written permission.
84 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
85 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
88 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
97 #include <sys/cdefs.h>
100 static char sccsid
[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94";
102 __RCSID("$NetBSD: ftp.c,v 1.152 2007/07/22 05:02:50 lukem Exp $");
104 #endif /* not lint */
106 #include <sys/types.h>
107 #include <sys/stat.h>
108 #include <sys/socket.h>
109 #include <sys/time.h>
111 #include <netinet/in.h>
112 #include <netinet/in_systm.h>
113 #include <netinet/ip.h>
114 #include <arpa/inet.h>
115 #include <arpa/ftp.h>
116 #include <arpa/telnet.h>
132 volatile sig_atomic_t abrtflag
;
133 volatile sig_atomic_t timeoutflag
;
138 char pasv
[BUFSIZ
]; /* passive port for proxy data connection */
140 static int empty(FILE *, FILE *, int);
144 struct sockaddr_in su_sin
;
146 struct sockaddr_in6 su_sin6
;
149 #if !defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
154 #if !defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
155 # define su_len si_len
157 # define su_len si_su.su_sin.sin_len
159 #define su_family si_su.su_sin.sin_family
160 #define su_port si_su.su_sin.sin_port
162 struct sockinet myctladdr
, hisctladdr
, data_addr
;
165 hookup(char *host
, char *port
)
167 int s
= -1, error
, portnum
;
168 struct addrinfo hints
, *res
, *res0
;
169 char hbuf
[MAXHOSTNAMELEN
];
170 static char hostnamebuf
[MAXHOSTNAMELEN
];
174 memset((char *)&hisctladdr
, 0, sizeof (hisctladdr
));
175 memset((char *)&myctladdr
, 0, sizeof (myctladdr
));
176 memset(&hints
, 0, sizeof(hints
));
177 portnum
= parseport(port
, FTP_PORT
);
178 hints
.ai_flags
= AI_CANONNAME
;
179 hints
.ai_family
= family
;
180 hints
.ai_socktype
= SOCK_STREAM
;
181 hints
.ai_protocol
= 0;
182 error
= getaddrinfo(host
, NULL
, &hints
, &res0
);
184 warnx("Can't lookup `%s': %s", host
,
185 (error
== EAI_SYSTEM
) ? strerror(errno
)
186 : gai_strerror(error
));
191 if (res0
->ai_canonname
)
192 (void)strlcpy(hostnamebuf
, res0
->ai_canonname
,
193 sizeof(hostnamebuf
));
195 (void)strlcpy(hostnamebuf
, host
, sizeof(hostnamebuf
));
196 hostname
= hostnamebuf
;
198 for (res
= res0
; res
; res
= res
->ai_next
) {
200 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
,
201 hbuf
, sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
))
202 strlcpy(hbuf
, "?", sizeof(hbuf
));
203 if (verbose
&& res0
->ai_next
) {
204 /* if we have multiple possibilities */
205 fprintf(ttyout
, "Trying %s...\n", hbuf
);
207 ((struct sockaddr_in
*)res
->ai_addr
)->sin_port
= htons(portnum
);
208 s
= socket(res
->ai_family
, SOCK_STREAM
, res
->ai_protocol
);
210 warn("Can't create socket for connection to `%s'",
214 if (ftp_connect(s
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
220 /* finally we got one */
224 warnx("Can't connect to `%s'", host
);
229 memcpy(&hisctladdr
.si_su
, res
->ai_addr
, res
->ai_addrlen
);
230 hisctladdr
.su_len
= res
->ai_addrlen
;
234 len
= hisctladdr
.su_len
;
235 if (getsockname(s
, (struct sockaddr
*)&myctladdr
.si_su
, &len
) == -1) {
236 warn("Can't determine my address of connection to `%s'", host
);
240 myctladdr
.su_len
= len
;
242 #ifdef IPTOS_LOWDELAY
243 if (hisctladdr
.su_family
== AF_INET
) {
244 int tos
= IPTOS_LOWDELAY
;
245 if (setsockopt(s
, IPPROTO_IP
, IP_TOS
,
246 (void *)&tos
, sizeof(tos
)) == -1) {
247 DWARN("setsockopt %s (ignored)",
252 cin
= fdopen(s
, "r");
253 cout
= fdopen(s
, "w");
254 if (cin
== NULL
|| cout
== NULL
) {
255 warnx("Can't fdopen socket");
264 fprintf(ttyout
, "Connected to %s.\n", hostname
);
265 if (getreply(0) > 2) { /* read startup message from server */
274 if (setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
,
275 (void *)&on
, sizeof(on
)) == -1) {
276 DWARN("setsockopt %s (ignored)", "SO_OOBINLINE");
286 cmdabort(int notused
)
293 write(fileno(ttyout
), "\n", 1);
296 siglongjmp(ptabort
, 1);
301 cmdtimeout(int notused
)
307 write(fileno(ttyout
), "\n", 1);
310 siglongjmp(ptabort
, 1);
316 command(const char *fmt
, ...)
324 fputs("---> ", ttyout
);
326 if (strncmp("PASS ", fmt
, 5) == 0)
327 fputs("PASS XXXX", ttyout
);
328 else if (strncmp("ACCT ", fmt
, 5) == 0)
329 fputs("ACCT XXXX", ttyout
);
331 vfprintf(ttyout
, fmt
, ap
);
337 warnx("No control connection for command");
344 oldsigint
= xsignal(SIGINT
, cmdabort
);
347 vfprintf(cout
, fmt
, ap
);
352 r
= getreply(!strcmp(fmt
, "QUIT"));
353 if (abrtflag
&& oldsigint
!= SIG_IGN
)
354 (*oldsigint
)(SIGINT
);
355 (void)xsignal(SIGINT
, oldsigint
);
359 static const char *m421
[] = {
360 "remote server timed out. Connection closed",
361 "user interrupt. Connection closed",
362 "remote server has closed connection",
366 getreply(int expecteof
)
368 char current_line
[BUFSIZ
]; /* last line of previous reply */
371 int originalcode
= 0, continuation
= 0;
372 sigfunc oldsigint
, oldsigalrm
;
374 char *cp
, *pt
= pasv
;
379 oldsigint
= xsignal(SIGINT
, cmdabort
);
380 oldsigalrm
= xsignal(SIGALRM
, cmdtimeout
);
382 for (line
= 0 ;; line
++) {
385 while (alarmtimer(quit_time
? quit_time
: 60),
386 ((c
= getc(cin
)) != '\n')) {
387 if (c
== IAC
) { /* handle telnet commands */
388 switch (c
= getc(cin
)) {
392 fprintf(cout
, "%c%c%c", IAC
, DONT
, c
);
398 fprintf(cout
, "%c%c%c", IAC
, WONT
, c
);
409 * these will get trashed by pswitch()
412 int reply_timeoutflag
= timeoutflag
;
413 int reply_abrtflag
= abrtflag
;
416 if (expecteof
&& feof(cin
)) {
417 (void)xsignal(SIGINT
, oldsigint
);
418 (void)xsignal(SIGALRM
, oldsigalrm
);
426 if (reply_timeoutflag
)
428 else if (reply_abrtflag
)
432 (void)fprintf(ttyout
,
433 "421 Service not available, %s.\n", m421
[midx
]);
434 (void)fflush(ttyout
);
437 (void)xsignal(SIGINT
, oldsigint
);
438 (void)xsignal(SIGALRM
, oldsigalrm
);
441 if (c
!= '\r' && (verbose
> 0 ||
442 ((verbose
> -1 && n
== '5' && dig
> 4) &&
443 (((!n
&& c
< '5') || (n
&& n
< '5'))
444 || !retry_connect
)))) {
446 (dig
== 1 || (dig
== 5 && verbose
== 0)))
447 fprintf(ttyout
, "%s:", hostname
);
448 (void)putc(c
, ttyout
);
450 if (dig
< 4 && isdigit(c
))
451 code
= code
* 10 + (c
- '0');
452 if (!pflag
&& (code
== 227 || code
== 228))
454 else if (!pflag
&& code
== 229)
456 if (dig
> 4 && pflag
== 1 && isdigit(c
))
459 if (c
!= '\r' && c
!= ')') {
460 if (pt
< &pasv
[sizeof(pasv
) - 1])
467 if (pflag
== 100 && c
== '(')
469 if (dig
== 4 && c
== '-') {
476 if (cp
< ¤t_line
[sizeof(current_line
) - 1])
479 if (verbose
> 0 || ((verbose
> -1 && n
== '5') &&
480 (n
< '5' || !retry_connect
))) {
481 (void)putc(c
, ttyout
);
482 (void)fflush(ttyout
);
488 (void)strlcpy(reply_string
, current_line
,
489 sizeof(reply_string
));
490 if (line
> 0 && code
== 0 && reply_callback
!= NULL
)
491 (*reply_callback
)(current_line
);
492 if (continuation
&& code
!= originalcode
) {
493 if (originalcode
== 0)
500 (void)xsignal(SIGINT
, oldsigint
);
501 (void)xsignal(SIGALRM
, oldsigalrm
);
502 if (code
== 421 || originalcode
== 421)
504 if (abrtflag
&& oldsigint
!= cmdabort
&& oldsigint
!= SIG_IGN
)
505 (*oldsigint
)(SIGINT
);
506 if (timeoutflag
&& oldsigalrm
!= cmdtimeout
&&
507 oldsigalrm
!= SIG_IGN
)
508 (*oldsigalrm
)(SIGINT
);
514 empty(FILE *cin
, FILE *din
, int sec
)
517 struct pollfd pfd
[2];
521 pfd
[nfd
].fd
= fileno(cin
);
522 pfd
[nfd
++].events
= POLLIN
;
526 pfd
[nfd
].fd
= fileno(din
);
527 pfd
[nfd
++].events
= POLLIN
;
530 if ((nr
= ftp_poll(pfd
, nfd
, sec
* 1000)) <= 0)
536 nr
|= (pfd
[nfd
++].revents
& POLLIN
) ? 1 : 0;
538 nr
|= (pfd
[nfd
++].revents
& POLLIN
) ? 2 : 0;
542 sigjmp_buf xferabort
;
545 abortxfer(int notused
)
554 switch (direction
[0]) {
556 strlcpy(msgbuf
, "\nreceive", sizeof(msgbuf
));
559 strlcpy(msgbuf
, "\nsend", sizeof(msgbuf
));
562 errx(1, "abortxfer: unknown direction `%s'", direction
);
564 len
= strlcat(msgbuf
, " aborted. Waiting for remote to finish abort.\n",
566 write(fileno(ttyout
), msgbuf
, len
);
567 siglongjmp(xferabort
, 1);
571 * Read data from infd & write to outfd, using buf/bufsize as the temporary
572 * buffer, dealing with short writes.
573 * If rate_limit != 0, rate-limit the transfer.
574 * If hash_interval != 0, fputc('c', ttyout) every hash_interval bytes.
575 * Updates global variables: bytes.
576 * Returns 0 if ok, 1 if there was a read error, 2 if there was a write error.
577 * In the case of error, errno contains the appropriate error code.
580 copy_bytes(int infd
, int outfd
, char *buf
, size_t bufsize
,
581 int rate_limit
, int hash_interval
)
583 volatile off_t hashc
;
586 struct timeval tvthen
, tvnow
, tvdiff
;
587 off_t bufrem
, bufchunk
;
590 hashc
= hash_interval
;
592 bufchunk
= rate_limit
;
598 (void)gettimeofday(&tvthen
, NULL
);
602 /* copy bufchunk at a time */
605 inc
= read(infd
, buf
, MIN(bufsize
, bufrem
));
612 outc
= write(outfd
, bufp
, inc
);
619 while (bytes
>= hashc
) {
620 (void)putc('#', ttyout
);
621 hashc
+= hash_interval
;
623 (void)fflush(ttyout
);
626 if (rate_limit
) { /* rate limited; wait if necessary */
628 (void)gettimeofday(&tvnow
, NULL
);
629 timersub(&tvnow
, &tvthen
, &tvdiff
);
630 if (tvdiff
.tv_sec
> 0)
632 usleep(1000000 - tvdiff
.tv_usec
);
639 if (hash_interval
&& bytes
> 0) {
640 if (bytes
< hash_interval
)
641 (void)putc('#', ttyout
);
642 (void)putc('\n', ttyout
);
643 (void)fflush(ttyout
);
655 sendrequest(const char *cmd
, const char *local
, const char *remote
,
662 int (*volatile closefunc
)(FILE *);
663 sigfunc
volatile oldintr
;
664 sigfunc
volatile oldintp
;
665 off_t
volatile hashbytes
;
667 char *volatile lmode
;
668 static size_t bufsize
;
677 oprogress
= progress
;
678 if (verbose
&& printnames
) {
680 fprintf(ttyout
, "local: %s ", local
);
682 fprintf(ttyout
, "remote: %s\n", remote
);
685 proxtrans(cmd
, local
, remote
);
694 if (sigsetjmp(xferabort
, 1)) {
700 (void)xsignal(SIGQUIT
, psummary
);
701 oldintr
= xsignal(SIGINT
, abortxfer
);
702 if (strcmp(local
, "-") == 0) {
705 } else if (*local
== '|') {
706 oldintp
= xsignal(SIGPIPE
, SIG_IGN
);
707 fin
= popen(local
+ 1, "r");
709 warn("Can't execute `%s'", local
+ 1);
716 fin
= fopen(local
, "r");
718 warn("Can't open `%s'", local
);
723 if (fstat(fileno(fin
), &st
) < 0 || !S_ISREG(st
.st_mode
)) {
724 fprintf(ttyout
, "%s: not a plain file.\n", local
);
728 filesize
= st
.st_size
;
734 if (sigsetjmp(xferabort
, 1))
738 (strcmp(cmd
, "STOR") == 0 || strcmp(cmd
, "APPE") == 0)) {
744 rc
= fseeko(fin
, restart_point
, SEEK_SET
);
748 rc
= lseek(fileno(fin
), restart_point
, SEEK_SET
);
752 warn("Can't seek to restart `%s'", local
);
755 if (command("REST " LLF
, (LLT
)restart_point
) != CONTINUE
)
760 if (command("%s %s", cmd
, remote
) != PRELIM
)
763 if (command("%s", cmd
) != PRELIM
)
767 dout
= dataconn(lmode
);
771 if (sndbuf_size
> bufsize
) {
774 bufsize
= sndbuf_size
;
775 buf
= ftp_malloc(bufsize
);
779 oldintp
= xsignal(SIGPIPE
, SIG_IGN
);
780 hash_interval
= (hash
&& (!progress
|| filesize
< 0)) ? mark
: 0;
786 c
= copy_bytes(fileno(fin
), fileno(dout
), buf
, bufsize
,
787 rate_put
, hash_interval
);
789 warn("Reading `%s'", local
);
792 warn("Writing to network");
798 while ((c
= getc(fin
)) != EOF
) {
800 while (hash_interval
&& bytes
>= hashbytes
) {
801 (void)putc('#', ttyout
);
802 (void)fflush(ttyout
);
807 (void)putc('\r', dout
);
812 #if 0 /* this violates RFC0959 */
814 (void)putc('\0', dout
);
820 if (bytes
< hashbytes
)
821 (void)putc('#', ttyout
);
822 (void)putc('\n', ttyout
);
825 warn("Reading `%s'", local
);
828 warn("Writing to network");
835 if (closefunc
!= NULL
) {
847 (void)xsignal(SIGINT
, oldintr
);
868 (void)xsignal(SIGINT
, oldintr
);
870 (void)xsignal(SIGPIPE
, oldintp
);
875 if (closefunc
!= NULL
&& fin
!= NULL
)
879 progress
= oprogress
;
885 recvrequest(const char *cmd
, const char *volatile local
, const char *remote
,
886 const char *lmode
, int printnames
, int ignorespecial
)
890 int (*volatile closefunc
)(FILE *);
891 sigfunc
volatile oldintr
;
892 sigfunc
volatile oldintp
;
894 int volatile is_retr
;
895 int volatile tcrflag
;
896 int volatile bare_lfs
;
897 static size_t bufsize
;
899 off_t
volatile hashbytes
;
903 struct timeval tval
[2];
910 direction
= "received";
914 oprogress
= progress
;
915 opreserve
= preserve
;
916 is_retr
= (strcmp(cmd
, "RETR") == 0);
917 if (is_retr
&& verbose
&& printnames
) {
918 if (ignorespecial
|| *local
!= '-')
919 fprintf(ttyout
, "local: %s ", local
);
921 fprintf(ttyout
, "remote: %s\n", remote
);
923 if (proxy
&& is_retr
) {
924 proxtrans(cmd
, local
, remote
);
930 tcrflag
= !crflag
&& is_retr
;
931 if (sigsetjmp(xferabort
, 1)) {
937 (void)xsignal(SIGQUIT
, psummary
);
938 oldintr
= xsignal(SIGINT
, abortxfer
);
939 if (ignorespecial
|| (strcmp(local
, "-") && *local
!= '|')) {
940 if (access(local
, W_OK
) < 0) {
941 char *dir
= strrchr(local
, '/');
943 if (errno
!= ENOENT
&& errno
!= EACCES
) {
944 warn("Can't access `%s'", local
);
950 d
= access(dir
== local
? "/" :
951 dir
? local
: ".", W_OK
);
955 warn("Can't access `%s'", local
);
959 if (!runique
&& errno
== EACCES
&&
960 chmod(local
, (S_IRUSR
|S_IWUSR
)) < 0) {
961 warn("Can't chmod `%s'", local
);
965 if (runique
&& errno
== EACCES
&&
966 (local
= gunique(local
)) == NULL
) {
971 else if (runique
&& (local
= gunique(local
)) == NULL
) {
977 if (curtype
!= TYPE_A
)
978 changetype(TYPE_A
, 0);
982 filesize
= remotesize(remote
, 0);
983 if (code
== 421 || code
== -1)
990 if (sigsetjmp(xferabort
, 1))
992 if (is_retr
&& restart_point
&&
993 command("REST " LLF
, (LLT
) restart_point
) != CONTINUE
)
995 if (! EMPTYSTRING(remote
)) {
996 if (command("%s %s", cmd
, remote
) != PRELIM
)
999 if (command("%s", cmd
) != PRELIM
)
1002 din
= dataconn("r");
1005 if (!ignorespecial
&& strcmp(local
, "-") == 0) {
1009 } else if (!ignorespecial
&& *local
== '|') {
1010 oldintp
= xsignal(SIGPIPE
, SIG_IGN
);
1011 fout
= popen(local
+ 1, "w");
1013 warn("Can't execute `%s'", local
+1);
1020 fout
= fopen(local
, lmode
);
1022 warn("Can't open `%s'", local
);
1028 if (fstat(fileno(fout
), &st
) != -1 && !S_ISREG(st
.st_mode
)) {
1032 if (rcvbuf_size
> bufsize
) {
1035 bufsize
= rcvbuf_size
;
1036 buf
= ftp_malloc(bufsize
);
1040 hash_interval
= (hash
&& (!progress
|| filesize
< 0)) ? mark
: 0;
1046 if (is_retr
&& restart_point
&&
1047 lseek(fileno(fout
), restart_point
, SEEK_SET
) < 0) {
1048 warn("Can't seek to restart `%s'", local
);
1051 c
= copy_bytes(fileno(din
), fileno(fout
), buf
, bufsize
,
1052 rate_get
, hash_interval
);
1055 warn("Reading from network");
1057 } else if (c
== 2) {
1058 warn("Writing `%s'", local
);
1063 if (is_retr
&& restart_point
) {
1067 if (fseeko(fout
, (off_t
)0, SEEK_SET
) < 0)
1069 for (i
= 0; i
++ < restart_point
;) {
1070 if ((ch
= getc(fout
)) == EOF
)
1075 if (fseeko(fout
, (off_t
)0, SEEK_CUR
) < 0) {
1077 warn("Can't seek to restart `%s'", local
);
1081 while ((c
= getc(din
)) != EOF
) {
1085 while (hash_interval
&& bytes
>= hashbytes
) {
1086 (void)putc('#', ttyout
);
1087 (void)fflush(ttyout
);
1091 if ((c
= getc(din
)) != '\n' || tcrflag
) {
1094 (void)putc('\r', fout
);
1103 (void)putc(c
, fout
);
1108 if (hash_interval
) {
1109 if (bytes
< hashbytes
)
1110 (void)putc('#', ttyout
);
1111 (void)putc('\n', ttyout
);
1115 warn("Reading from network");
1119 warn("Writing `%s'", local
);
1124 if (closefunc
!= NULL
) {
1133 "WARNING! %d bare linefeeds received in ASCII mode.\n",
1135 fputs("File may not have transferred correctly.\n", ttyout
);
1137 if (bytes
>= 0 && is_retr
) {
1140 if (preserve
&& (closefunc
== fclose
)) {
1141 mtime
= remotemodtime(remote
, 0);
1143 (void)gettimeofday(&tval
[0], NULL
);
1144 tval
[1].tv_sec
= mtime
;
1145 tval
[1].tv_usec
= 0;
1146 if (utimes(local
, tval
) == -1) {
1148 "Can't change modification time on %s to %s",
1150 rfc2822time(localtime(&mtime
)));
1159 * abort using RFC0959 recommended IP,SYNC sequence
1161 if (! sigsetjmp(xferabort
, 1)) {
1162 /* this is the first call */
1163 (void)xsignal(SIGINT
, abort_squared
);
1176 (void)xsignal(SIGINT
, oldintr
);
1178 (void)xsignal(SIGPIPE
, oldintp
);
1183 if (closefunc
!= NULL
&& fout
!= NULL
)
1187 progress
= oprogress
;
1188 preserve
= opreserve
;
1193 * Need to start a listen on the data channel before we send the command,
1194 * otherwise the server's connect may fail.
1200 int result
, tmpno
= 0;
1203 unsigned int addr
[16], port
[2];
1204 unsigned int af
, hal
, pal
;
1206 char *pasvcmd
= NULL
;
1211 if (myctladdr
.su_family
== AF_INET6
&& ftp_debug
&&
1212 (IN6_IS_ADDR_LINKLOCAL(&myctladdr
.si_su
.su_sin6
.sin6_addr
) ||
1213 IN6_IS_ADDR_SITELOCAL(&myctladdr
.si_su
.su_sin6
.sin6_addr
))) {
1214 warnx("Use of scoped addresses can be troublesome");
1221 data_addr
= myctladdr
;
1222 data
= socket(data_addr
.su_family
, SOCK_STREAM
, 0);
1224 warn("Can't create socket for data connection");
1227 if ((options
& SO_DEBUG
) &&
1228 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
,
1229 (void *)&on
, sizeof(on
)) == -1) {
1230 DWARN("setsockopt %s (ignored)", "SO_DEBUG");
1232 result
= COMPLETE
+ 1;
1233 switch (data_addr
.su_family
) {
1235 if (epsv4
&& !epsv4bad
) {
1240 result
= command("EPSV");
1243 (result
== COMPLETE
|| !connected
))
1244 fprintf(ttyout
, "%s\n", reply_string
);
1248 * this code is to be friendly with broken
1251 if (code
/ 10 == 22 && code
!= 229) {
1253 "wrong server: return code must be 229\n",
1255 result
= COMPLETE
+ 1;
1257 if (result
!= COMPLETE
) {
1259 DPRINTF("disabling epsv4 for this "
1263 if (result
!= COMPLETE
) {
1265 result
= command("PASV");
1276 result
= command("EPSV");
1279 (result
== COMPLETE
|| !connected
))
1280 fprintf(ttyout
, "%s\n", reply_string
);
1283 /* this code is to be friendly with broken BSDI ftpd */
1284 if (code
/ 10 == 22 && code
!= 229) {
1286 "wrong server: return code must be 229\n",
1288 result
= COMPLETE
+ 1;
1290 if (result
!= COMPLETE
) {
1292 result
= command("LPSV");
1299 result
= COMPLETE
+ 1;
1302 if (result
!= COMPLETE
) {
1303 if (activefallback
) {
1312 fputs("Passive mode refused.\n", ttyout
);
1316 #define pack2(var, off) \
1317 (((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))
1318 #define pack4(var, off) \
1319 (((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \
1320 ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))
1321 #define UC(b) (((int)b)&0xff)
1324 * What we've got at this point is a string of comma separated
1325 * one-byte unsigned integer values, separated by commas.
1327 if (strcmp(pasvcmd
, "PASV") == 0) {
1328 if (data_addr
.su_family
!= AF_INET
) {
1330 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout
);
1334 if (code
/ 10 == 22 && code
!= 227) {
1335 fputs("wrong server: return code must be 227\n",
1340 error
= sscanf(pasv
, "%u,%u,%u,%u,%u,%u",
1341 &addr
[0], &addr
[1], &addr
[2], &addr
[3],
1342 &port
[0], &port
[1]);
1345 "Passive mode address scan failure. Shouldn't happen!\n", ttyout
);
1350 memset(&data_addr
, 0, sizeof(data_addr
));
1351 data_addr
.su_family
= AF_INET
;
1352 data_addr
.su_len
= sizeof(struct sockaddr_in
);
1353 data_addr
.si_su
.su_sin
.sin_addr
.s_addr
=
1354 htonl(pack4(addr
, 0));
1355 data_addr
.su_port
= htons(pack2(port
, 0));
1356 } else if (strcmp(pasvcmd
, "LPSV") == 0) {
1357 if (code
/ 10 == 22 && code
!= 228) {
1358 fputs("wrong server: return code must be 228\n",
1363 switch (data_addr
.su_family
) {
1365 error
= sscanf(pasv
,
1366 "%u,%u,%u,%u,%u,%u,%u,%u,%u",
1368 &addr
[0], &addr
[1], &addr
[2], &addr
[3],
1369 &pal
, &port
[0], &port
[1]);
1372 "Passive mode address scan failure. Shouldn't happen!\n", ttyout
);
1376 if (af
!= 4 || hal
!= 4 || pal
!= 2) {
1378 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout
);
1384 memset(&data_addr
, 0, sizeof(data_addr
));
1385 data_addr
.su_family
= AF_INET
;
1386 data_addr
.su_len
= sizeof(struct sockaddr_in
);
1387 data_addr
.si_su
.su_sin
.sin_addr
.s_addr
=
1388 htonl(pack4(addr
, 0));
1389 data_addr
.su_port
= htons(pack2(port
, 0));
1393 error
= sscanf(pasv
,
1394 "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
1396 &addr
[0], &addr
[1], &addr
[2], &addr
[3],
1397 &addr
[4], &addr
[5], &addr
[6], &addr
[7],
1398 &addr
[8], &addr
[9], &addr
[10],
1399 &addr
[11], &addr
[12], &addr
[13],
1400 &addr
[14], &addr
[15],
1401 &pal
, &port
[0], &port
[1]);
1404 "Passive mode address scan failure. Shouldn't happen!\n", ttyout
);
1408 if (af
!= 6 || hal
!= 16 || pal
!= 2) {
1410 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout
);
1416 memset(&data_addr
, 0, sizeof(data_addr
));
1417 data_addr
.su_family
= AF_INET6
;
1418 data_addr
.su_len
= sizeof(struct sockaddr_in6
);
1421 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
1422 data_addr
.si_su
.su_sin6
.sin6_addr
.s6_addr
[i
] =
1426 data_addr
.su_port
= htons(pack2(port
, 0));
1432 } else if (strcmp(pasvcmd
, "EPSV") == 0) {
1436 if (code
/ 10 == 22 && code
!= 229) {
1437 fputs("wrong server: return code must be 229\n",
1442 if (sscanf(pasv
, "%c%c%c%d%c", &delim
[0],
1443 &delim
[1], &delim
[2], &port
[1],
1445 fputs("parse error!\n", ttyout
);
1449 if (delim
[0] != delim
[1] || delim
[0] != delim
[2]
1450 || delim
[0] != delim
[3]) {
1451 fputs("parse error!\n", ttyout
);
1455 data_addr
= hisctladdr
;
1456 data_addr
.su_port
= htons(port
[1]);
1460 if (ftp_connect(data
, (struct sockaddr
*)&data_addr
.si_su
,
1461 data_addr
.su_len
) < 0) {
1462 if (activefallback
) {
1473 #ifdef IPTOS_THROUGHPUT
1474 if (data_addr
.su_family
== AF_INET
) {
1475 on
= IPTOS_THROUGHPUT
;
1476 if (setsockopt(data
, IPPROTO_IP
, IP_TOS
,
1477 (void *)&on
, sizeof(on
)) == -1) {
1478 DWARN("setsockopt %s (ignored)",
1479 "IPTOS_THROUGHPUT");
1487 data_addr
= myctladdr
;
1489 data_addr
.su_port
= 0; /* let system pick one */
1492 data
= socket(data_addr
.su_family
, SOCK_STREAM
, 0);
1494 warn("Can't create socket for data connection");
1500 if (setsockopt(data
, SOL_SOCKET
, SO_REUSEADDR
,
1501 (void *)&on
, sizeof(on
)) == -1) {
1502 warn("Can't set SO_REUSEADDR on data connection");
1505 if (bind(data
, (struct sockaddr
*)&data_addr
.si_su
,
1506 data_addr
.su_len
) < 0) {
1507 warn("Can't bind for data connection");
1510 if ((options
& SO_DEBUG
) &&
1511 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
,
1512 (void *)&on
, sizeof(on
)) == -1) {
1513 DWARN("setsockopt %s (ignored)", "SO_DEBUG");
1515 len
= sizeof(data_addr
.si_su
);
1516 memset((char *)&data_addr
, 0, sizeof (data_addr
));
1517 if (getsockname(data
, (struct sockaddr
*)&data_addr
.si_su
, &len
) == -1) {
1518 warn("Can't determine my address of data connection");
1521 data_addr
.su_len
= len
;
1522 if (ftp_listen(data
, 1) < 0)
1523 warn("Can't listen to data connection");
1526 char hname
[NI_MAXHOST
], sname
[NI_MAXSERV
];
1528 struct sockinet tmp
;
1530 switch (data_addr
.su_family
) {
1532 if (!epsv4
|| epsv4bad
) {
1533 result
= COMPLETE
+ 1;
1540 af
= (data_addr
.su_family
== AF_INET
) ? 1 : 2;
1543 if (tmp
.su_family
== AF_INET6
)
1544 tmp
.si_su
.su_sin6
.sin6_scope_id
= 0;
1546 if (getnameinfo((struct sockaddr
*)&tmp
.si_su
,
1547 tmp
.su_len
, hname
, sizeof(hname
), sname
,
1548 sizeof(sname
), NI_NUMERICHOST
| NI_NUMERICSERV
)) {
1554 result
= command("EPRT |%d|%s|%s|", af
, hname
,
1558 (result
== COMPLETE
|| !connected
))
1559 fprintf(ttyout
, "%s\n", reply_string
);
1562 if (result
!= COMPLETE
) {
1564 DPRINTF("disabling epsv4 for this "
1570 result
= COMPLETE
+ 1;
1573 if (result
== COMPLETE
)
1576 switch (data_addr
.su_family
) {
1578 a
= (char *)&data_addr
.si_su
.su_sin
.sin_addr
;
1579 p
= (char *)&data_addr
.su_port
;
1580 result
= command("PORT %d,%d,%d,%d,%d,%d",
1581 UC(a
[0]), UC(a
[1]), UC(a
[2]), UC(a
[3]),
1582 UC(p
[0]), UC(p
[1]));
1586 a
= (char *)&data_addr
.si_su
.su_sin6
.sin6_addr
;
1587 p
= (char *)&data_addr
.su_port
;
1589 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1591 UC(a
[0]),UC(a
[1]),UC(a
[2]),UC(a
[3]),
1592 UC(a
[4]),UC(a
[5]),UC(a
[6]),UC(a
[7]),
1593 UC(a
[8]),UC(a
[9]),UC(a
[10]),UC(a
[11]),
1594 UC(a
[12]),UC(a
[13]),UC(a
[14]),UC(a
[15]),
1595 2, UC(p
[0]), UC(p
[1]));
1599 result
= COMPLETE
+ 1; /* xxx */
1605 if (result
== ERROR
&& sendport
== -1) {
1610 return (result
!= COMPLETE
);
1614 #ifdef IPTOS_THROUGHPUT
1615 if (data_addr
.su_family
== AF_INET
) {
1616 on
= IPTOS_THROUGHPUT
;
1617 if (setsockopt(data
, IPPROTO_IP
, IP_TOS
,
1618 (void *)&on
, sizeof(on
)) == -1)
1619 DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT");
1632 dataconn(const char *lmode
)
1634 struct sockinet from
;
1635 int s
, flags
, rv
, timeout
;
1636 struct timeval endtime
, now
, td
;
1637 struct pollfd pfd
[1];
1640 if (passivemode
) /* passive data connection */
1641 return (fdopen(data
, lmode
));
1643 /* active mode data connection */
1645 if ((flags
= fcntl(data
, F_GETFL
, 0)) == -1)
1646 goto dataconn_failed
; /* get current socket flags */
1647 if (fcntl(data
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
1648 goto dataconn_failed
; /* set non-blocking connect */
1650 /* NOTE: we now must restore socket flags on successful exit */
1652 /* limit time waiting on listening socket */
1654 pfd
[0].events
= POLLIN
;
1655 (void)gettimeofday(&endtime
, NULL
); /* determine end time */
1656 endtime
.tv_sec
+= (quit_time
> 0) ? quit_time
: 60;
1657 /* without -q, default to 60s */
1659 (void)gettimeofday(&now
, NULL
);
1660 timersub(&endtime
, &now
, &td
);
1661 timeout
= td
.tv_sec
* 1000 + td
.tv_usec
/1000;
1664 rv
= ftp_poll(pfd
, 1, timeout
);
1665 } while (rv
== -1 && errno
== EINTR
); /* loop until poll ! EINTR */
1667 warn("Can't poll waiting before accept");
1668 goto dataconn_failed
;
1671 warnx("Poll timeout waiting before accept");
1672 goto dataconn_failed
;
1675 /* (non-blocking) accept the connection */
1676 fromlen
= myctladdr
.su_len
;
1678 s
= accept(data
, (struct sockaddr
*) &from
.si_su
, &fromlen
);
1679 } while (s
== -1 && errno
== EINTR
); /* loop until accept ! EINTR */
1681 warn("Can't accept data connection");
1682 goto dataconn_failed
;
1687 if (fcntl(data
, F_SETFL
, flags
) == -1) /* restore socket flags */
1688 goto dataconn_failed
;
1690 #ifdef IPTOS_THROUGHPUT
1691 if (from
.su_family
== AF_INET
) {
1692 int tos
= IPTOS_THROUGHPUT
;
1693 if (setsockopt(s
, IPPROTO_IP
, IP_TOS
,
1694 (void *)&tos
, sizeof(tos
)) == -1) {
1695 DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT");
1699 return (fdopen(data
, lmode
));
1708 psabort(int notused
)
1722 static struct comvars
{
1724 char name
[MAXHOSTNAMELEN
];
1725 struct sockinet mctl
;
1726 struct sockinet hctl
;
1739 char mi
[MAXPATHLEN
];
1740 char mo
[MAXPATHLEN
];
1741 } proxstruct
, tmpstruct
;
1742 struct comvars
*ip
, *op
;
1745 oldintr
= xsignal(SIGINT
, psabort
);
1759 ip
->connect
= connected
;
1760 connected
= op
->connect
;
1762 (void)strlcpy(ip
->name
, hostname
, sizeof(ip
->name
));
1765 hostname
= op
->name
;
1766 ip
->hctl
= hisctladdr
;
1767 hisctladdr
= op
->hctl
;
1768 ip
->mctl
= myctladdr
;
1769 myctladdr
= op
->mctl
;
1776 ip
->curtpe
= curtype
;
1777 curtype
= op
->curtpe
;
1780 ip
->sunqe
= sunique
;
1781 sunique
= op
->sunqe
;
1782 ip
->runqe
= runique
;
1783 runique
= op
->runqe
;
1788 (void)strlcpy(ip
->nti
, ntin
, sizeof(ip
->nti
));
1789 (void)strlcpy(ntin
, op
->nti
, sizeof(ntin
));
1790 (void)strlcpy(ip
->nto
, ntout
, sizeof(ip
->nto
));
1791 (void)strlcpy(ntout
, op
->nto
, sizeof(ntout
));
1792 ip
->mapflg
= mapflag
;
1793 mapflag
= op
->mapflg
;
1794 (void)strlcpy(ip
->mi
, mapin
, sizeof(ip
->mi
));
1795 (void)strlcpy(mapin
, op
->mi
, sizeof(mapin
));
1796 (void)strlcpy(ip
->mo
, mapout
, sizeof(ip
->mo
));
1797 (void)strlcpy(mapout
, op
->mo
, sizeof(mapout
));
1798 (void)xsignal(SIGINT
, oldintr
);
1806 abortpt(int notused
)
1812 write(fileno(ttyout
), "\n", 1);
1816 siglongjmp(ptabort
, 1);
1820 proxtrans(const char *cmd
, const char *local
, const char *remote
)
1822 sigfunc
volatile oldintr
;
1823 int prox_type
, nfnd
;
1824 int volatile secndflag
;
1825 char *volatile cmd2
;
1829 if (strcmp(cmd
, "RETR"))
1832 cmd2
= runique
? "STOU" : "STOR";
1833 if ((prox_type
= type
) == 0) {
1834 if (unix_server
&& unix_proxy
)
1839 if (curtype
!= prox_type
)
1840 changetype(prox_type
, 1);
1841 if (command("PASV") != COMPLETE
) {
1842 fputs("proxy server does not support third party transfers.\n",
1848 fputs("No primary connection.\n", ttyout
);
1853 if (curtype
!= prox_type
)
1854 changetype(prox_type
, 1);
1855 if (command("PORT %s", pasv
) != COMPLETE
) {
1859 if (sigsetjmp(ptabort
, 1))
1861 oldintr
= xsignal(SIGINT
, abortpt
);
1862 if ((restart_point
&&
1863 (command("REST " LLF
, (LLT
) restart_point
) != CONTINUE
))
1864 || (command("%s %s", cmd
, remote
) != PRELIM
)) {
1865 (void)xsignal(SIGINT
, oldintr
);
1872 if ((restart_point
&&
1873 (command("REST " LLF
, (LLT
) restart_point
) != CONTINUE
))
1874 || (command("%s %s", cmd2
, local
) != PRELIM
))
1880 (void)xsignal(SIGINT
, oldintr
);
1883 fprintf(ttyout
, "local: %s remote: %s\n", local
, remote
);
1886 if (sigsetjmp(xferabort
, 1)) {
1887 (void)xsignal(SIGINT
, oldintr
);
1890 (void)xsignal(SIGINT
, abort_squared
);
1892 if (strcmp(cmd
, "RETR") && !proxy
)
1894 else if (!strcmp(cmd
, "RETR") && proxy
)
1896 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1897 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1905 (void)xsignal(SIGINT
, oldintr
);
1911 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1912 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1919 (void)xsignal(SIGINT
, oldintr
);
1927 if ((nfnd
= empty(cin
, NULL
, 10)) <= 0) {
1929 warn("Error aborting proxy command");
1942 (void)xsignal(SIGINT
, oldintr
);
1946 reset(int argc
, char *argv
[])
1950 if (argc
== 0 && argv
!= NULL
) {
1951 UPRINTF("usage: %s\n", argv
[0]);
1956 if ((nfnd
= empty(cin
, NULL
, 0)) < 0) {
1957 warn("Error resetting connection");
1966 gunique(const char *local
)
1968 static char new[MAXPATHLEN
];
1969 char *cp
= strrchr(local
, '/');
1970 int d
, count
=0, len
;
1975 d
= access(cp
== local
? "/" : cp
? local
: ".", W_OK
);
1979 warn("Can't access `%s'", local
);
1982 len
= strlcpy(new, local
, sizeof(new));
1986 if (++count
== 100) {
1987 fputs("runique: can't find unique file name.\n",
1997 if ((d
= access(new, F_OK
)) < 0)
2001 else if (*(cp
- 2) == '.')
2004 *(cp
- 2) = *(cp
- 2) + 1;
2013 * aborts abort_remote(). lostpeer() is called because if the user is
2014 * too impatient to wait or there's another problem then ftp really
2015 * needs to get back to a known state.
2018 abort_squared(int dummy
)
2025 len
= strlcpy(msgbuf
, "\nremote abort aborted; closing connection.\n",
2027 write(fileno(ttyout
), msgbuf
, len
);
2029 siglongjmp(xferabort
, 1);
2033 abort_remote(FILE *din
)
2039 warnx("Lost control connection for abort");
2046 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
2047 * after urgent byte rather than before as is protocol now
2052 if (send(fileno(cout
), buf
, 3, MSG_OOB
) != 3)
2053 warn("Can't send abort message");
2054 fprintf(cout
, "%cABOR\r\n", DM
);
2056 if ((nfnd
= empty(cin
, din
, 10)) <= 0) {
2058 warn("Can't send abort message");
2063 if (din
&& (nfnd
& 2)) {
2064 while (read(fileno(din
), buf
, BUFSIZ
) > 0)
2067 if (getreply(0) == ERROR
&& code
== 552) {
2068 /* 552 needed for nic style abort */
2075 * Ensure that ai->ai_addr is NOT an IPv4 mapped address.
2076 * IPv4 mapped address complicates too many things in FTP
2077 * protocol handling, as FTP protocol is defined differently
2078 * between IPv4 and IPv6.
2080 * This may not be the best way to handle this situation,
2081 * since the semantics of IPv4 mapped address is defined in
2082 * the kernel. There are configurations where we should use
2083 * IPv4 mapped address as native IPv6 address, not as
2084 * "an IPv6 address that embeds IPv4 address" (namely, SIIT).
2086 * More complete solution would be to have an additional
2087 * getsockopt to grab "real" peername/sockname. "real"
2088 * peername/sockname will be AF_INET if IPv4 mapped address
2089 * is used to embed IPv4 address, and will be AF_INET6 if
2090 * we use it as native. What a mess!
2093 ai_unmapped(struct addrinfo
*ai
)
2096 struct sockaddr_in6
*sin6
;
2097 struct sockaddr_in sin
;
2100 if (ai
->ai_family
!= AF_INET6
)
2102 if (ai
->ai_addrlen
!= sizeof(struct sockaddr_in6
) ||
2103 sizeof(sin
) > ai
->ai_addrlen
)
2105 sin6
= (struct sockaddr_in6
*)ai
->ai_addr
;
2106 if (!IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
2109 memset(&sin
, 0, sizeof(sin
));
2110 sin
.sin_family
= AF_INET
;
2111 len
= sizeof(struct sockaddr_in
);
2112 memcpy(&sin
.sin_addr
, &sin6
->sin6_addr
.s6_addr
[12],
2113 sizeof(sin
.sin_addr
));
2114 sin
.sin_port
= sin6
->sin6_port
;
2116 ai
->ai_family
= AF_INET
;
2117 #if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
2120 memcpy(ai
->ai_addr
, &sin
, len
);
2121 ai
->ai_addrlen
= len
;
2129 fputs("Usage error\n", ttyout
);