1 /* $NetBSD: ftp.c,v 1.158 2009/04/12 10:18:52 lukem Exp $ */
4 * Copyright (c) 1996-2009 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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1985, 1989, 1993, 1994
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * Copyright (C) 1997 and 1998 WIDE Project.
63 * All rights reserved.
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. Neither the name of the project nor the names of its contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
77 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90 #include <sys/cdefs.h>
93 static char sccsid
[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94";
95 __RCSID("$NetBSD: ftp.c,v 1.158 2009/04/12 10:18:52 lukem Exp $");
99 #include <sys/types.h>
100 #include <sys/stat.h>
101 #include <sys/socket.h>
102 #include <sys/time.h>
104 #include <netinet/in.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/ip.h>
107 #include <arpa/inet.h>
108 #include <arpa/ftp.h>
109 #include <arpa/telnet.h>
125 volatile sig_atomic_t abrtflag
;
126 volatile sig_atomic_t timeoutflag
;
131 char pasv
[BUFSIZ
]; /* passive port for proxy data connection */
133 static int empty(FILE *, FILE *, int);
137 struct sockaddr_in su_sin
;
139 struct sockaddr_in6 su_sin6
;
142 #if !defined(HAVE_STRUCT_SOCKADDR_IN_SIN_LEN)
147 #if !defined(HAVE_STRUCT_SOCKADDR_IN_SIN_LEN)
148 # define su_len si_len
150 # define su_len si_su.su_sin.sin_len
152 #define su_family si_su.su_sin.sin_family
153 #define su_port si_su.su_sin.sin_port
155 struct sockinet myctladdr
, hisctladdr
, data_addr
;
158 hookup(const char *host
, const char *port
)
161 struct addrinfo hints
, *res
, *res0
;
162 static char hostnamebuf
[MAXHOSTNAMELEN
];
166 memset((char *)&hisctladdr
, 0, sizeof (hisctladdr
));
167 memset((char *)&myctladdr
, 0, sizeof (myctladdr
));
168 memset(&hints
, 0, sizeof(hints
));
169 hints
.ai_flags
= AI_CANONNAME
;
170 hints
.ai_family
= family
;
171 hints
.ai_socktype
= SOCK_STREAM
;
172 hints
.ai_protocol
= 0;
173 error
= getaddrinfo(host
, port
, &hints
, &res0
);
175 warnx("Can't lookup `%s:%s': %s", host
, port
,
176 (error
== EAI_SYSTEM
) ? strerror(errno
)
177 : gai_strerror(error
));
182 if (res0
->ai_canonname
)
183 (void)strlcpy(hostnamebuf
, res0
->ai_canonname
,
184 sizeof(hostnamebuf
));
186 (void)strlcpy(hostnamebuf
, host
, sizeof(hostnamebuf
));
187 hostname
= hostnamebuf
;
189 for (res
= res0
; res
; res
= res
->ai_next
) {
190 char hname
[NI_MAXHOST
], sname
[NI_MAXSERV
];
193 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
,
194 hname
, sizeof(hname
), sname
, sizeof(sname
),
195 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
196 strlcpy(hname
, "?", sizeof(hname
));
197 strlcpy(sname
, "?", sizeof(sname
));
199 if (verbose
&& res0
->ai_next
) {
200 /* if we have multiple possibilities */
201 fprintf(ttyout
, "Trying %s:%s ...\n", hname
, sname
);
203 s
= socket(res
->ai_family
, SOCK_STREAM
, res
->ai_protocol
);
205 warn("Can't create socket for connection to `%s:%s'",
209 if (ftp_connect(s
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
215 /* finally we got one */
219 warnx("Can't connect to `%s:%s'", host
, port
);
224 memcpy(&hisctladdr
.si_su
, res
->ai_addr
, res
->ai_addrlen
);
225 hisctladdr
.su_len
= res
->ai_addrlen
;
229 len
= hisctladdr
.su_len
;
230 if (getsockname(s
, (struct sockaddr
*)&myctladdr
.si_su
, &len
) == -1) {
231 warn("Can't determine my address of connection to `%s:%s'",
236 myctladdr
.su_len
= len
;
238 #ifdef IPTOS_LOWDELAY
239 if (hisctladdr
.su_family
== AF_INET
) {
240 int tos
= IPTOS_LOWDELAY
;
241 if (setsockopt(s
, IPPROTO_IP
, IP_TOS
,
242 (void *)&tos
, sizeof(tos
)) == -1) {
243 DWARN("setsockopt %s (ignored)",
248 cin
= fdopen(s
, "r");
249 cout
= fdopen(s
, "w");
250 if (cin
== NULL
|| cout
== NULL
) {
251 warnx("Can't fdopen socket");
260 fprintf(ttyout
, "Connected to %s.\n", hostname
);
261 if (getreply(0) > 2) { /* read startup message from server */
270 if (setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
,
271 (void *)&on
, sizeof(on
)) == -1) {
272 DWARN("setsockopt %s (ignored)", "SO_OOBINLINE");
282 cmdabort(int notused
)
289 write(fileno(ttyout
), "\n", 1);
292 siglongjmp(ptabort
, 1);
297 cmdtimeout(int notused
)
303 write(fileno(ttyout
), "\n", 1);
306 siglongjmp(ptabort
, 1);
312 command(const char *fmt
, ...)
320 fputs("---> ", ttyout
);
322 if (strncmp("PASS ", fmt
, 5) == 0)
323 fputs("PASS XXXX", ttyout
);
324 else if (strncmp("ACCT ", fmt
, 5) == 0)
325 fputs("ACCT XXXX", ttyout
);
327 vfprintf(ttyout
, fmt
, ap
);
333 warnx("No control connection for command");
340 oldsigint
= xsignal(SIGINT
, cmdabort
);
343 vfprintf(cout
, fmt
, ap
);
348 r
= getreply(!strcmp(fmt
, "QUIT"));
349 if (abrtflag
&& oldsigint
!= SIG_IGN
)
350 (*oldsigint
)(SIGINT
);
351 (void)xsignal(SIGINT
, oldsigint
);
355 static const char *m421
[] = {
356 "remote server timed out. Connection closed",
357 "user interrupt. Connection closed",
358 "remote server has closed connection",
362 getreply(int expecteof
)
364 char current_line
[BUFSIZ
]; /* last line of previous reply */
367 int originalcode
= 0, continuation
= 0;
368 sigfunc oldsigint
, oldsigalrm
;
370 char *cp
, *pt
= pasv
;
375 oldsigint
= xsignal(SIGINT
, cmdabort
);
376 oldsigalrm
= xsignal(SIGALRM
, cmdtimeout
);
378 for (lineno
= 0 ;; lineno
++) {
381 while (alarmtimer(quit_time
? quit_time
: 60),
382 ((c
= getc(cin
)) != '\n')) {
383 if (c
== IAC
) { /* handle telnet commands */
384 switch (c
= getc(cin
)) {
388 fprintf(cout
, "%c%c%c", IAC
, DONT
, c
);
394 fprintf(cout
, "%c%c%c", IAC
, WONT
, c
);
405 * these will get trashed by pswitch()
408 int reply_timeoutflag
= timeoutflag
;
409 int reply_abrtflag
= abrtflag
;
412 if (expecteof
&& feof(cin
)) {
413 (void)xsignal(SIGINT
, oldsigint
);
414 (void)xsignal(SIGALRM
, oldsigalrm
);
422 if (reply_timeoutflag
)
424 else if (reply_abrtflag
)
428 (void)fprintf(ttyout
,
429 "421 Service not available, %s.\n", m421
[midx
]);
430 (void)fflush(ttyout
);
433 (void)xsignal(SIGINT
, oldsigint
);
434 (void)xsignal(SIGALRM
, oldsigalrm
);
437 if (c
!= '\r' && (verbose
> 0 ||
438 ((verbose
> -1 && n
== '5' && dig
> 4) &&
439 (((!n
&& c
< '5') || (n
&& n
< '5'))
440 || !retry_connect
)))) {
442 (dig
== 1 || (dig
== 5 && verbose
== 0)))
443 fprintf(ttyout
, "%s:", hostname
);
444 (void)putc(c
, ttyout
);
446 if (dig
< 4 && isdigit(c
))
447 code
= code
* 10 + (c
- '0');
448 if (!pflag
&& (code
== 227 || code
== 228))
450 else if (!pflag
&& code
== 229)
452 if (dig
> 4 && pflag
== 1 && isdigit(c
))
455 if (c
!= '\r' && c
!= ')') {
456 if (pt
< &pasv
[sizeof(pasv
) - 1])
463 if (pflag
== 100 && c
== '(')
465 if (dig
== 4 && c
== '-') {
472 if (cp
< ¤t_line
[sizeof(current_line
) - 1])
475 if (verbose
> 0 || ((verbose
> -1 && n
== '5') &&
476 (n
< '5' || !retry_connect
))) {
477 (void)putc(c
, ttyout
);
478 (void)fflush(ttyout
);
484 (void)strlcpy(reply_string
, current_line
,
485 sizeof(reply_string
));
486 if (lineno
> 0 && code
== 0 && reply_callback
!= NULL
)
487 (*reply_callback
)(current_line
);
488 if (continuation
&& code
!= originalcode
) {
489 if (originalcode
== 0)
496 (void)xsignal(SIGINT
, oldsigint
);
497 (void)xsignal(SIGALRM
, oldsigalrm
);
498 if (code
== 421 || originalcode
== 421)
500 if (abrtflag
&& oldsigint
!= cmdabort
&& oldsigint
!= SIG_IGN
)
501 (*oldsigint
)(SIGINT
);
502 if (timeoutflag
&& oldsigalrm
!= cmdtimeout
&&
503 oldsigalrm
!= SIG_IGN
)
504 (*oldsigalrm
)(SIGINT
);
510 empty(FILE *ecin
, FILE *din
, int sec
)
513 struct pollfd pfd
[2];
517 pfd
[nfd
].fd
= fileno(ecin
);
518 pfd
[nfd
++].events
= POLLIN
;
522 pfd
[nfd
].fd
= fileno(din
);
523 pfd
[nfd
++].events
= POLLIN
;
526 if ((nr
= ftp_poll(pfd
, nfd
, sec
* 1000)) <= 0)
532 nr
|= (pfd
[nfd
++].revents
& POLLIN
) ? 1 : 0;
534 nr
|= (pfd
[nfd
++].revents
& POLLIN
) ? 2 : 0;
538 sigjmp_buf xferabort
;
541 abortxfer(int notused
)
550 switch (direction
[0]) {
552 strlcpy(msgbuf
, "\nreceive", sizeof(msgbuf
));
555 strlcpy(msgbuf
, "\nsend", sizeof(msgbuf
));
558 errx(1, "abortxfer: unknown direction `%s'", direction
);
560 len
= strlcat(msgbuf
, " aborted. Waiting for remote to finish abort.\n",
562 write(fileno(ttyout
), msgbuf
, len
);
563 siglongjmp(xferabort
, 1);
567 * Read data from infd & write to outfd, using buf/bufsize as the temporary
568 * buffer, dealing with short writes.
569 * If rate_limit != 0, rate-limit the transfer.
570 * If hash_interval != 0, fputc('c', ttyout) every hash_interval bytes.
571 * Updates global variables: bytes.
572 * Returns 0 if ok, 1 if there was a read error, 2 if there was a write error.
573 * In the case of error, errno contains the appropriate error code.
576 copy_bytes(int infd
, int outfd
, char *buf
, size_t bufsize
,
577 int rate_limit
, int hash_interval
)
579 volatile off_t hashc
;
582 struct timeval tvthen
, tvnow
, tvdiff
;
583 off_t bufrem
, bufchunk
;
586 hashc
= hash_interval
;
588 bufchunk
= rate_limit
;
594 (void)gettimeofday(&tvthen
, NULL
);
598 /* copy bufchunk at a time */
601 inc
= read(infd
, buf
, MIN((off_t
)bufsize
, bufrem
));
608 outc
= write(outfd
, bufp
, inc
);
615 while (bytes
>= hashc
) {
616 (void)putc('#', ttyout
);
617 hashc
+= hash_interval
;
619 (void)fflush(ttyout
);
622 if (rate_limit
) { /* rate limited; wait if necessary */
624 (void)gettimeofday(&tvnow
, NULL
);
625 timersub(&tvnow
, &tvthen
, &tvdiff
);
626 if (tvdiff
.tv_sec
> 0)
628 usleep(1000000 - tvdiff
.tv_usec
);
635 if (hash_interval
&& bytes
> 0) {
636 if (bytes
< hash_interval
)
637 (void)putc('#', ttyout
);
638 (void)putc('\n', ttyout
);
639 (void)fflush(ttyout
);
651 sendrequest(const char *cmd
, const char *local
, const char *remote
,
658 int (*volatile closefunc
)(FILE *);
659 sigfunc
volatile oldintr
;
660 sigfunc
volatile oldintp
;
661 off_t
volatile hashbytes
;
664 static size_t bufsize
;
673 oprogress
= progress
;
674 if (verbose
&& printnames
) {
676 fprintf(ttyout
, "local: %s ", local
);
678 fprintf(ttyout
, "remote: %s\n", remote
);
681 proxtrans(cmd
, local
, remote
);
690 if (sigsetjmp(xferabort
, 1)) {
696 (void)xsignal(SIGQUIT
, psummary
);
697 oldintr
= xsignal(SIGINT
, abortxfer
);
698 if (strcmp(local
, "-") == 0) {
701 } else if (*local
== '|') {
702 oldintp
= xsignal(SIGPIPE
, SIG_IGN
);
703 fin
= popen(local
+ 1, "r");
705 warn("Can't execute `%s'", local
+ 1);
712 fin
= fopen(local
, "r");
714 warn("Can't open `%s'", local
);
719 if (fstat(fileno(fin
), &st
) < 0 || !S_ISREG(st
.st_mode
)) {
720 fprintf(ttyout
, "%s: not a plain file.\n", local
);
724 filesize
= st
.st_size
;
730 if (sigsetjmp(xferabort
, 1))
734 (strcmp(cmd
, "STOR") == 0 || strcmp(cmd
, "APPE") == 0)) {
740 rc
= fseeko(fin
, restart_point
, SEEK_SET
);
744 rc
= lseek(fileno(fin
), restart_point
, SEEK_SET
);
748 warn("Can't seek to restart `%s'", local
);
751 if (command("REST " LLF
, (LLT
)restart_point
) != CONTINUE
)
756 if (command("%s %s", cmd
, remote
) != PRELIM
)
759 if (command("%s", cmd
) != PRELIM
)
763 dout
= dataconn(lmode
);
767 if ((size_t)sndbuf_size
> bufsize
) {
770 bufsize
= sndbuf_size
;
771 buf
= ftp_malloc(bufsize
);
775 oldintp
= xsignal(SIGPIPE
, SIG_IGN
);
776 hash_interval
= (hash
&& (!progress
|| filesize
< 0)) ? mark
: 0;
782 c
= copy_bytes(fileno(fin
), fileno(dout
), buf
, bufsize
,
783 rate_put
, hash_interval
);
785 warn("Reading `%s'", local
);
788 warn("Writing to network");
794 while ((c
= getc(fin
)) != EOF
) {
796 while (hash_interval
&& bytes
>= hashbytes
) {
797 (void)putc('#', ttyout
);
798 (void)fflush(ttyout
);
803 (void)putc('\r', dout
);
808 #if 0 /* this violates RFC0959 */
810 (void)putc('\0', dout
);
816 if (bytes
< hashbytes
)
817 (void)putc('#', ttyout
);
818 (void)putc('\n', ttyout
);
821 warn("Reading `%s'", local
);
824 warn("Writing to network");
831 if (closefunc
!= NULL
) {
843 (void)xsignal(SIGINT
, oldintr
);
864 (void)xsignal(SIGINT
, oldintr
);
866 (void)xsignal(SIGPIPE
, oldintp
);
871 if (closefunc
!= NULL
&& fin
!= NULL
)
875 progress
= oprogress
;
881 recvrequest(const char *cmd
, const char *volatile local
, const char *remote
,
882 const char *lmode
, int printnames
, int ignorespecial
)
886 int (*volatile closefunc
)(FILE *);
887 sigfunc
volatile oldintr
;
888 sigfunc
volatile oldintp
;
890 int volatile is_retr
;
891 int volatile tcrflag
;
892 int volatile bare_lfs
;
893 static size_t bufsize
;
895 off_t
volatile hashbytes
;
899 struct timeval tval
[2];
906 direction
= "received";
910 oprogress
= progress
;
911 opreserve
= preserve
;
912 is_retr
= (strcmp(cmd
, "RETR") == 0);
913 if (is_retr
&& verbose
&& printnames
) {
914 if (ignorespecial
|| *local
!= '-')
915 fprintf(ttyout
, "local: %s ", local
);
917 fprintf(ttyout
, "remote: %s\n", remote
);
919 if (proxy
&& is_retr
) {
920 proxtrans(cmd
, local
, remote
);
926 tcrflag
= !crflag
&& is_retr
;
927 if (sigsetjmp(xferabort
, 1)) {
933 (void)xsignal(SIGQUIT
, psummary
);
934 oldintr
= xsignal(SIGINT
, abortxfer
);
935 if (ignorespecial
|| (strcmp(local
, "-") && *local
!= '|')) {
936 if (access(local
, W_OK
) < 0) {
937 char *dir
= strrchr(local
, '/');
939 if (errno
!= ENOENT
&& errno
!= EACCES
) {
940 warn("Can't access `%s'", local
);
946 d
= access(dir
== local
? "/" :
947 dir
? local
: ".", W_OK
);
951 warn("Can't access `%s'", local
);
955 if (!runique
&& errno
== EACCES
&&
956 chmod(local
, (S_IRUSR
|S_IWUSR
)) < 0) {
957 warn("Can't chmod `%s'", local
);
961 if (runique
&& errno
== EACCES
&&
962 (local
= gunique(local
)) == NULL
) {
967 else if (runique
&& (local
= gunique(local
)) == NULL
) {
973 if (curtype
!= TYPE_A
)
974 changetype(TYPE_A
, 0);
978 filesize
= remotesize(remote
, 0);
979 if (code
== 421 || code
== -1)
986 if (sigsetjmp(xferabort
, 1))
988 if (is_retr
&& restart_point
&&
989 command("REST " LLF
, (LLT
) restart_point
) != CONTINUE
)
991 if (! EMPTYSTRING(remote
)) {
992 if (command("%s %s", cmd
, remote
) != PRELIM
)
995 if (command("%s", cmd
) != PRELIM
)
1001 if (!ignorespecial
&& strcmp(local
, "-") == 0) {
1005 } else if (!ignorespecial
&& *local
== '|') {
1006 oldintp
= xsignal(SIGPIPE
, SIG_IGN
);
1007 fout
= popen(local
+ 1, "w");
1009 warn("Can't execute `%s'", local
+1);
1016 fout
= fopen(local
, lmode
);
1018 warn("Can't open `%s'", local
);
1024 if (fstat(fileno(fout
), &st
) != -1 && !S_ISREG(st
.st_mode
)) {
1028 if ((size_t)rcvbuf_size
> bufsize
) {
1031 bufsize
= rcvbuf_size
;
1032 buf
= ftp_malloc(bufsize
);
1036 hash_interval
= (hash
&& (!progress
|| filesize
< 0)) ? mark
: 0;
1042 if (is_retr
&& restart_point
&&
1043 lseek(fileno(fout
), restart_point
, SEEK_SET
) < 0) {
1044 warn("Can't seek to restart `%s'", local
);
1047 c
= copy_bytes(fileno(din
), fileno(fout
), buf
, bufsize
,
1048 rate_get
, hash_interval
);
1051 warn("Reading from network");
1053 } else if (c
== 2) {
1054 warn("Writing `%s'", local
);
1059 if (is_retr
&& restart_point
) {
1063 if (fseeko(fout
, (off_t
)0, SEEK_SET
) < 0)
1065 for (i
= 0; i
++ < restart_point
;) {
1066 if ((ch
= getc(fout
)) == EOF
)
1071 if (fseeko(fout
, (off_t
)0, SEEK_CUR
) < 0) {
1073 warn("Can't seek to restart `%s'", local
);
1077 while ((c
= getc(din
)) != EOF
) {
1081 while (hash_interval
&& bytes
>= hashbytes
) {
1082 (void)putc('#', ttyout
);
1083 (void)fflush(ttyout
);
1087 if ((c
= getc(din
)) != '\n' || tcrflag
) {
1090 (void)putc('\r', fout
);
1099 (void)putc(c
, fout
);
1104 if (hash_interval
) {
1105 if (bytes
< hashbytes
)
1106 (void)putc('#', ttyout
);
1107 (void)putc('\n', ttyout
);
1111 warn("Reading from network");
1115 warn("Writing `%s'", local
);
1120 if (closefunc
!= NULL
) {
1129 "WARNING! %d bare linefeeds received in ASCII mode.\n",
1131 fputs("File may not have transferred correctly.\n", ttyout
);
1133 if (bytes
>= 0 && is_retr
) {
1136 if (preserve
&& (closefunc
== fclose
)) {
1137 mtime
= remotemodtime(remote
, 0);
1139 (void)gettimeofday(&tval
[0], NULL
);
1140 tval
[1].tv_sec
= mtime
;
1141 tval
[1].tv_usec
= 0;
1142 if (utimes(local
, tval
) == -1) {
1144 "Can't change modification time on %s to %s",
1146 rfc2822time(localtime(&mtime
)));
1155 * abort using RFC0959 recommended IP,SYNC sequence
1157 if (! sigsetjmp(xferabort
, 1)) {
1158 /* this is the first call */
1159 (void)xsignal(SIGINT
, abort_squared
);
1172 (void)xsignal(SIGINT
, oldintr
);
1174 (void)xsignal(SIGPIPE
, oldintp
);
1179 if (closefunc
!= NULL
&& fout
!= NULL
)
1183 progress
= oprogress
;
1184 preserve
= opreserve
;
1189 * Need to start a listen on the data channel before we send the command,
1190 * otherwise the server's connect may fail.
1196 int result
, tmpno
= 0;
1199 unsigned int addr
[16], port
[2];
1200 unsigned int af
, hal
, pal
;
1202 const char *pasvcmd
= NULL
;
1207 if (myctladdr
.su_family
== AF_INET6
&& ftp_debug
&&
1208 (IN6_IS_ADDR_LINKLOCAL(&myctladdr
.si_su
.su_sin6
.sin6_addr
) ||
1209 IN6_IS_ADDR_SITELOCAL(&myctladdr
.si_su
.su_sin6
.sin6_addr
))) {
1210 warnx("Use of scoped addresses can be troublesome");
1217 data_addr
= myctladdr
;
1218 data
= socket(data_addr
.su_family
, SOCK_STREAM
, 0);
1220 warn("Can't create socket for data connection");
1223 if ((options
& SO_DEBUG
) &&
1224 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
,
1225 (void *)&on
, sizeof(on
)) == -1) {
1226 DWARN("setsockopt %s (ignored)", "SO_DEBUG");
1228 result
= COMPLETE
+ 1;
1229 switch (data_addr
.su_family
) {
1231 if (epsv4
&& !epsv4bad
) {
1236 result
= command("EPSV");
1239 (result
== COMPLETE
|| !connected
))
1240 fprintf(ttyout
, "%s\n", reply_string
);
1244 * this code is to be friendly with broken
1247 if (code
/ 10 == 22 && code
!= 229) {
1249 "wrong server: return code must be 229\n",
1251 result
= COMPLETE
+ 1;
1253 if (result
!= COMPLETE
) {
1255 DPRINTF("disabling epsv4 for this "
1259 if (result
!= COMPLETE
) {
1261 result
= command("PASV");
1268 if (epsv6
&& !epsv6bad
) {
1273 result
= command("EPSV");
1276 (result
== COMPLETE
|| !connected
))
1277 fprintf(ttyout
, "%s\n", reply_string
);
1281 * this code is to be friendly with
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 DPRINTF("disabling epsv6 for this "
1296 if (result
!= COMPLETE
) {
1298 result
= command("LPSV");
1305 result
= COMPLETE
+ 1;
1308 if (result
!= COMPLETE
) {
1309 if (activefallback
) {
1318 fputs("Passive mode refused.\n", ttyout
);
1322 #define pack2(var, off) \
1323 (((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))
1324 #define pack4(var, off) \
1325 (((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \
1326 ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))
1327 #define UC(b) (((int)b)&0xff)
1330 * What we've got at this point is a string of comma separated
1331 * one-byte unsigned integer values, separated by commas.
1333 if (strcmp(pasvcmd
, "PASV") == 0) {
1334 if (data_addr
.su_family
!= AF_INET
) {
1336 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout
);
1340 if (code
/ 10 == 22 && code
!= 227) {
1341 fputs("wrong server: return code must be 227\n",
1346 error
= sscanf(pasv
, "%u,%u,%u,%u,%u,%u",
1347 &addr
[0], &addr
[1], &addr
[2], &addr
[3],
1348 &port
[0], &port
[1]);
1351 "Passive mode address scan failure. Shouldn't happen!\n", ttyout
);
1356 memset(&data_addr
, 0, sizeof(data_addr
));
1357 data_addr
.su_family
= AF_INET
;
1358 data_addr
.su_len
= sizeof(struct sockaddr_in
);
1359 data_addr
.si_su
.su_sin
.sin_addr
.s_addr
=
1360 htonl(pack4(addr
, 0));
1361 data_addr
.su_port
= htons(pack2(port
, 0));
1362 } else if (strcmp(pasvcmd
, "LPSV") == 0) {
1363 if (code
/ 10 == 22 && code
!= 228) {
1364 fputs("wrong server: return code must be 228\n",
1369 switch (data_addr
.su_family
) {
1371 error
= sscanf(pasv
,
1372 "%u,%u,%u,%u,%u,%u,%u,%u,%u",
1374 &addr
[0], &addr
[1], &addr
[2], &addr
[3],
1375 &pal
, &port
[0], &port
[1]);
1378 "Passive mode address scan failure. Shouldn't happen!\n", ttyout
);
1382 if (af
!= 4 || hal
!= 4 || pal
!= 2) {
1384 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout
);
1390 memset(&data_addr
, 0, sizeof(data_addr
));
1391 data_addr
.su_family
= AF_INET
;
1392 data_addr
.su_len
= sizeof(struct sockaddr_in
);
1393 data_addr
.si_su
.su_sin
.sin_addr
.s_addr
=
1394 htonl(pack4(addr
, 0));
1395 data_addr
.su_port
= htons(pack2(port
, 0));
1399 error
= sscanf(pasv
,
1400 "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
1402 &addr
[0], &addr
[1], &addr
[2], &addr
[3],
1403 &addr
[4], &addr
[5], &addr
[6], &addr
[7],
1404 &addr
[8], &addr
[9], &addr
[10],
1405 &addr
[11], &addr
[12], &addr
[13],
1406 &addr
[14], &addr
[15],
1407 &pal
, &port
[0], &port
[1]);
1410 "Passive mode address scan failure. Shouldn't happen!\n", ttyout
);
1414 if (af
!= 6 || hal
!= 16 || pal
!= 2) {
1416 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout
);
1422 memset(&data_addr
, 0, sizeof(data_addr
));
1423 data_addr
.su_family
= AF_INET6
;
1424 data_addr
.su_len
= sizeof(struct sockaddr_in6
);
1427 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
1428 data_addr
.si_su
.su_sin6
.sin6_addr
.s6_addr
[i
] =
1432 data_addr
.su_port
= htons(pack2(port
, 0));
1438 } else if (strcmp(pasvcmd
, "EPSV") == 0) {
1442 if (code
/ 10 == 22 && code
!= 229) {
1443 fputs("wrong server: return code must be 229\n",
1448 if (sscanf(pasv
, "%c%c%c%d%c", &delim
[0],
1449 &delim
[1], &delim
[2], &port
[1],
1451 fputs("parse error!\n", ttyout
);
1455 if (delim
[0] != delim
[1] || delim
[0] != delim
[2]
1456 || delim
[0] != delim
[3]) {
1457 fputs("parse error!\n", ttyout
);
1461 data_addr
= hisctladdr
;
1462 data_addr
.su_port
= htons(port
[1]);
1466 if (ftp_connect(data
, (struct sockaddr
*)&data_addr
.si_su
,
1467 data_addr
.su_len
) < 0) {
1468 if (activefallback
) {
1479 #ifdef IPTOS_THROUGHPUT
1480 if (data_addr
.su_family
== AF_INET
) {
1481 on
= IPTOS_THROUGHPUT
;
1482 if (setsockopt(data
, IPPROTO_IP
, IP_TOS
,
1483 (void *)&on
, sizeof(on
)) == -1) {
1484 DWARN("setsockopt %s (ignored)",
1485 "IPTOS_THROUGHPUT");
1493 data_addr
= myctladdr
;
1495 data_addr
.su_port
= 0; /* let system pick one */
1498 data
= socket(data_addr
.su_family
, SOCK_STREAM
, 0);
1500 warn("Can't create socket for data connection");
1506 if (setsockopt(data
, SOL_SOCKET
, SO_REUSEADDR
,
1507 (void *)&on
, sizeof(on
)) == -1) {
1508 warn("Can't set SO_REUSEADDR on data connection");
1511 if (bind(data
, (struct sockaddr
*)&data_addr
.si_su
,
1512 data_addr
.su_len
) < 0) {
1513 warn("Can't bind for data connection");
1516 if ((options
& SO_DEBUG
) &&
1517 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
,
1518 (void *)&on
, sizeof(on
)) == -1) {
1519 DWARN("setsockopt %s (ignored)", "SO_DEBUG");
1521 len
= sizeof(data_addr
.si_su
);
1522 memset((char *)&data_addr
, 0, sizeof (data_addr
));
1523 if (getsockname(data
, (struct sockaddr
*)&data_addr
.si_su
, &len
) == -1) {
1524 warn("Can't determine my address of data connection");
1527 data_addr
.su_len
= len
;
1528 if (ftp_listen(data
, 1) < 0)
1529 warn("Can't listen to data connection");
1532 char hname
[NI_MAXHOST
], sname
[NI_MAXSERV
];
1533 struct sockinet tmp
;
1535 switch (data_addr
.su_family
) {
1537 if (!epsv4
|| epsv4bad
) {
1538 result
= COMPLETE
+ 1;
1544 if (!epsv6
|| epsv6bad
) {
1545 result
= COMPLETE
+ 1;
1549 af
= (data_addr
.su_family
== AF_INET
) ? 1 : 2;
1552 if (tmp
.su_family
== AF_INET6
)
1553 tmp
.si_su
.su_sin6
.sin6_scope_id
= 0;
1555 if (getnameinfo((struct sockaddr
*)&tmp
.si_su
,
1556 tmp
.su_len
, hname
, sizeof(hname
), sname
,
1557 sizeof(sname
), NI_NUMERICHOST
| NI_NUMERICSERV
)) {
1563 result
= command("EPRT |%u|%s|%s|", af
, hname
,
1567 (result
== COMPLETE
|| !connected
))
1568 fprintf(ttyout
, "%s\n", reply_string
);
1571 if (result
!= COMPLETE
) {
1573 DPRINTF("disabling epsv4 for this "
1579 result
= COMPLETE
+ 1;
1582 if (result
== COMPLETE
)
1585 switch (data_addr
.su_family
) {
1587 a
= (char *)&data_addr
.si_su
.su_sin
.sin_addr
;
1588 p
= (char *)&data_addr
.su_port
;
1589 result
= command("PORT %d,%d,%d,%d,%d,%d",
1590 UC(a
[0]), UC(a
[1]), UC(a
[2]), UC(a
[3]),
1591 UC(p
[0]), UC(p
[1]));
1595 a
= (char *)&data_addr
.si_su
.su_sin6
.sin6_addr
;
1596 p
= (char *)&data_addr
.su_port
;
1598 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1600 UC(a
[0]),UC(a
[1]),UC(a
[2]),UC(a
[3]),
1601 UC(a
[4]),UC(a
[5]),UC(a
[6]),UC(a
[7]),
1602 UC(a
[8]),UC(a
[9]),UC(a
[10]),UC(a
[11]),
1603 UC(a
[12]),UC(a
[13]),UC(a
[14]),UC(a
[15]),
1604 2, UC(p
[0]), UC(p
[1]));
1608 result
= COMPLETE
+ 1; /* xxx */
1614 if (result
== ERROR
&& sendport
== -1) {
1619 return (result
!= COMPLETE
);
1623 #ifdef IPTOS_THROUGHPUT
1624 if (data_addr
.su_family
== AF_INET
) {
1625 on
= IPTOS_THROUGHPUT
;
1626 if (setsockopt(data
, IPPROTO_IP
, IP_TOS
,
1627 (void *)&on
, sizeof(on
)) == -1) {
1628 DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT");
1642 dataconn(const char *lmode
)
1644 struct sockinet from
;
1645 int s
, flags
, rv
, timeout
;
1646 struct timeval endtime
, now
, td
;
1647 struct pollfd pfd
[1];
1650 if (passivemode
) /* passive data connection */
1651 return (fdopen(data
, lmode
));
1653 /* active mode data connection */
1655 if ((flags
= fcntl(data
, F_GETFL
, 0)) == -1)
1656 goto dataconn_failed
; /* get current socket flags */
1657 if (fcntl(data
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
1658 goto dataconn_failed
; /* set non-blocking connect */
1660 /* NOTE: we now must restore socket flags on successful exit */
1662 /* limit time waiting on listening socket */
1664 pfd
[0].events
= POLLIN
;
1665 (void)gettimeofday(&endtime
, NULL
); /* determine end time */
1666 endtime
.tv_sec
+= (quit_time
> 0) ? quit_time
: 60;
1667 /* without -q, default to 60s */
1669 (void)gettimeofday(&now
, NULL
);
1670 timersub(&endtime
, &now
, &td
);
1671 timeout
= td
.tv_sec
* 1000 + td
.tv_usec
/1000;
1674 rv
= ftp_poll(pfd
, 1, timeout
);
1675 } while (rv
== -1 && errno
== EINTR
); /* loop until poll ! EINTR */
1677 warn("Can't poll waiting before accept");
1678 goto dataconn_failed
;
1681 warnx("Poll timeout waiting before accept");
1682 goto dataconn_failed
;
1685 /* (non-blocking) accept the connection */
1686 fromlen
= myctladdr
.su_len
;
1688 s
= accept(data
, (struct sockaddr
*) &from
.si_su
, &fromlen
);
1689 } while (s
== -1 && errno
== EINTR
); /* loop until accept ! EINTR */
1691 warn("Can't accept data connection");
1692 goto dataconn_failed
;
1697 if (fcntl(data
, F_SETFL
, flags
) == -1) /* restore socket flags */
1698 goto dataconn_failed
;
1700 #ifdef IPTOS_THROUGHPUT
1701 if (from
.su_family
== AF_INET
) {
1702 int tos
= IPTOS_THROUGHPUT
;
1703 if (setsockopt(s
, IPPROTO_IP
, IP_TOS
,
1704 (void *)&tos
, sizeof(tos
)) == -1) {
1705 DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT");
1709 return (fdopen(data
, lmode
));
1718 psabort(int notused
)
1732 static struct comvars
{
1734 char name
[MAXHOSTNAMELEN
];
1735 struct sockinet mctl
;
1736 struct sockinet hctl
;
1749 char mi
[MAXPATHLEN
];
1750 char mo
[MAXPATHLEN
];
1751 } proxstruct
, tmpstruct
;
1752 struct comvars
*ip
, *op
;
1755 oldintr
= xsignal(SIGINT
, psabort
);
1769 ip
->connect
= connected
;
1770 connected
= op
->connect
;
1772 (void)strlcpy(ip
->name
, hostname
, sizeof(ip
->name
));
1775 hostname
= op
->name
;
1776 ip
->hctl
= hisctladdr
;
1777 hisctladdr
= op
->hctl
;
1778 ip
->mctl
= myctladdr
;
1779 myctladdr
= op
->mctl
;
1786 ip
->curtpe
= curtype
;
1787 curtype
= op
->curtpe
;
1790 ip
->sunqe
= sunique
;
1791 sunique
= op
->sunqe
;
1792 ip
->runqe
= runique
;
1793 runique
= op
->runqe
;
1798 (void)strlcpy(ip
->nti
, ntin
, sizeof(ip
->nti
));
1799 (void)strlcpy(ntin
, op
->nti
, sizeof(ntin
));
1800 (void)strlcpy(ip
->nto
, ntout
, sizeof(ip
->nto
));
1801 (void)strlcpy(ntout
, op
->nto
, sizeof(ntout
));
1802 ip
->mapflg
= mapflag
;
1803 mapflag
= op
->mapflg
;
1804 (void)strlcpy(ip
->mi
, mapin
, sizeof(ip
->mi
));
1805 (void)strlcpy(mapin
, op
->mi
, sizeof(mapin
));
1806 (void)strlcpy(ip
->mo
, mapout
, sizeof(ip
->mo
));
1807 (void)strlcpy(mapout
, op
->mo
, sizeof(mapout
));
1808 (void)xsignal(SIGINT
, oldintr
);
1816 abortpt(int notused
)
1822 write(fileno(ttyout
), "\n", 1);
1826 siglongjmp(ptabort
, 1);
1830 proxtrans(const char *cmd
, const char *local
, const char *remote
)
1832 sigfunc
volatile oldintr
;
1833 int prox_type
, nfnd
;
1834 int volatile secndflag
;
1835 const char *volatile cmd2
;
1839 if (strcmp(cmd
, "RETR"))
1842 cmd2
= runique
? "STOU" : "STOR";
1843 if ((prox_type
= type
) == 0) {
1844 if (unix_server
&& unix_proxy
)
1849 if (curtype
!= prox_type
)
1850 changetype(prox_type
, 1);
1851 if (command("PASV") != COMPLETE
) {
1852 fputs("proxy server does not support third party transfers.\n",
1858 fputs("No primary connection.\n", ttyout
);
1863 if (curtype
!= prox_type
)
1864 changetype(prox_type
, 1);
1865 if (command("PORT %s", pasv
) != COMPLETE
) {
1869 if (sigsetjmp(ptabort
, 1))
1871 oldintr
= xsignal(SIGINT
, abortpt
);
1872 if ((restart_point
&&
1873 (command("REST " LLF
, (LLT
) restart_point
) != CONTINUE
))
1874 || (command("%s %s", cmd
, remote
) != PRELIM
)) {
1875 (void)xsignal(SIGINT
, oldintr
);
1882 if ((restart_point
&&
1883 (command("REST " LLF
, (LLT
) restart_point
) != CONTINUE
))
1884 || (command("%s %s", cmd2
, local
) != PRELIM
))
1890 (void)xsignal(SIGINT
, oldintr
);
1893 fprintf(ttyout
, "local: %s remote: %s\n", local
, remote
);
1896 if (sigsetjmp(xferabort
, 1)) {
1897 (void)xsignal(SIGINT
, oldintr
);
1900 (void)xsignal(SIGINT
, abort_squared
);
1902 if (strcmp(cmd
, "RETR") && !proxy
)
1904 else if (!strcmp(cmd
, "RETR") && proxy
)
1906 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1907 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1915 (void)xsignal(SIGINT
, oldintr
);
1921 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1922 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1929 (void)xsignal(SIGINT
, oldintr
);
1937 if ((nfnd
= empty(cin
, NULL
, 10)) <= 0) {
1939 warn("Error aborting proxy command");
1952 (void)xsignal(SIGINT
, oldintr
);
1956 reset(int argc
, char *argv
[])
1960 if (argc
== 0 && argv
!= NULL
) {
1961 UPRINTF("usage: %s\n", argv
[0]);
1966 if ((nfnd
= empty(cin
, NULL
, 0)) < 0) {
1967 warn("Error resetting connection");
1976 gunique(const char *local
)
1978 static char new[MAXPATHLEN
];
1979 char *cp
= strrchr(local
, '/');
1980 int d
, count
=0, len
;
1985 d
= access(cp
== local
? "/" : cp
? local
: ".", W_OK
);
1989 warn("Can't access `%s'", local
);
1992 len
= strlcpy(new, local
, sizeof(new));
1996 if (++count
== 100) {
1997 fputs("runique: can't find unique file name.\n",
2007 if ((d
= access(new, F_OK
)) < 0)
2011 else if (*(cp
- 2) == '.')
2014 *(cp
- 2) = *(cp
- 2) + 1;
2023 * aborts abort_remote(). lostpeer() is called because if the user is
2024 * too impatient to wait or there's another problem then ftp really
2025 * needs to get back to a known state.
2028 abort_squared(int dummy
)
2035 len
= strlcpy(msgbuf
, "\nremote abort aborted; closing connection.\n",
2037 write(fileno(ttyout
), msgbuf
, len
);
2039 siglongjmp(xferabort
, 1);
2043 abort_remote(FILE *din
)
2049 warnx("Lost control connection for abort");
2056 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
2057 * after urgent byte rather than before as is protocol now
2062 if (send(fileno(cout
), buf
, 3, MSG_OOB
) != 3)
2063 warn("Can't send abort message");
2064 fprintf(cout
, "%cABOR\r\n", DM
);
2066 if ((nfnd
= empty(cin
, din
, 10)) <= 0) {
2068 warn("Can't send abort message");
2073 if (din
&& (nfnd
& 2)) {
2074 while (read(fileno(din
), buf
, BUFSIZ
) > 0)
2077 if (getreply(0) == ERROR
&& code
== 552) {
2078 /* 552 needed for nic style abort */
2085 * Ensure that ai->ai_addr is NOT an IPv4 mapped address.
2086 * IPv4 mapped address complicates too many things in FTP
2087 * protocol handling, as FTP protocol is defined differently
2088 * between IPv4 and IPv6.
2090 * This may not be the best way to handle this situation,
2091 * since the semantics of IPv4 mapped address is defined in
2092 * the kernel. There are configurations where we should use
2093 * IPv4 mapped address as native IPv6 address, not as
2094 * "an IPv6 address that embeds IPv4 address" (namely, SIIT).
2096 * More complete solution would be to have an additional
2097 * getsockopt to grab "real" peername/sockname. "real"
2098 * peername/sockname will be AF_INET if IPv4 mapped address
2099 * is used to embed IPv4 address, and will be AF_INET6 if
2100 * we use it as native. What a mess!
2103 ai_unmapped(struct addrinfo
*ai
)
2106 struct sockaddr_in6
*sin6
;
2107 struct sockaddr_in sin
;
2110 if (ai
->ai_family
!= AF_INET6
)
2112 if (ai
->ai_addrlen
!= sizeof(struct sockaddr_in6
) ||
2113 sizeof(sin
) > ai
->ai_addrlen
)
2115 sin6
= (struct sockaddr_in6
*)ai
->ai_addr
;
2116 if (!IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
2119 memset(&sin
, 0, sizeof(sin
));
2120 sin
.sin_family
= AF_INET
;
2121 len
= sizeof(struct sockaddr_in
);
2122 memcpy(&sin
.sin_addr
, &sin6
->sin6_addr
.s6_addr
[12],
2123 sizeof(sin
.sin_addr
));
2124 sin
.sin_port
= sin6
->sin6_port
;
2126 ai
->ai_family
= AF_INET
;
2127 #if defined(HAVE_STRUCT_SOCKADDR_IN_SIN_LEN)
2130 memcpy(ai
->ai_addr
, &sin
, len
);
2131 ai
->ai_addrlen
= len
;
2139 fputs("Usage error\n", ttyout
);