1 /* $NetBSD: ftp.c,v 1.132 2005/05/14 15:26:43 lukem Exp $ */
4 * Copyright (c) 1996-2005 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>
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/ip.h>
106 #include <arpa/inet.h>
107 #include <arpa/ftp.h>
108 #include <arpa/telnet.h>
124 volatile sig_atomic_t abrtflag
;
125 volatile sig_atomic_t timeoutflag
;
130 char pasv
[BUFSIZ
]; /* passive port for proxy data connection */
132 static int empty(FILE *, FILE *, int);
136 struct sockaddr_in su_sin
;
138 struct sockaddr_in6 su_sin6
;
141 #if !HAVE_SOCKADDR_SA_LEN
146 #if !HAVE_SOCKADDR_SA_LEN
147 # define su_len si_len
149 # define su_len si_su.su_sin.sin_len
151 #define su_family si_su.su_sin.sin_family
152 #define su_port si_su.su_sin.sin_port
154 struct sockinet myctladdr
, hisctladdr
, data_addr
;
157 hookup(char *host
, char *port
)
159 int s
= -1, error
, portnum
;
160 struct addrinfo hints
, *res
, *res0
;
161 char hbuf
[MAXHOSTNAMELEN
];
162 static char hostnamebuf
[MAXHOSTNAMELEN
];
163 char *cause
= "unknown";
167 memset((char *)&hisctladdr
, 0, sizeof (hisctladdr
));
168 memset((char *)&myctladdr
, 0, sizeof (myctladdr
));
169 memset(&hints
, 0, sizeof(hints
));
170 portnum
= parseport(port
, FTP_PORT
);
171 hints
.ai_flags
= AI_CANONNAME
;
172 hints
.ai_family
= family
;
173 hints
.ai_socktype
= SOCK_STREAM
;
174 hints
.ai_protocol
= 0;
175 error
= getaddrinfo(host
, NULL
, &hints
, &res0
);
177 warnx("%s", 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
) {
191 * make sure that ai_addr is NOT an IPv4 mapped address.
192 * IPv4 mapped address complicates too many things in FTP
193 * protocol handling, as FTP protocol is defined differently
194 * between IPv4 and IPv6.
196 * This may not be the best way to handle this situation,
197 * since the semantics of IPv4 mapped address is defined in
198 * the kernel. There are configurations where we should use
199 * IPv4 mapped address as native IPv6 address, not as
200 * "an IPv6 address that embeds IPv4 address" (namely, SIIT).
202 * More complete solution would be to have an additional
203 * getsockopt to grab "real" peername/sockname. "real"
204 * peername/sockname will be AF_INET if IPv4 mapped address
205 * is used to embed IPv4 address, and will be AF_INET6 if
206 * we use it as native. What a mess!
209 #if 0 /*old behavior*/
210 if (res
!= res0
) /* not on the first address */
212 if (res0
->ai_next
) /* if we have multiple possibilities */
215 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
,
216 hbuf
, sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
))
217 strlcpy(hbuf
, "?", sizeof(hbuf
));
218 fprintf(ttyout
, "Trying %s...\n", hbuf
);
220 ((struct sockaddr_in
*)res
->ai_addr
)->sin_port
= htons(portnum
);
221 s
= socket(res
->ai_family
, SOCK_STREAM
, res
->ai_protocol
);
226 error
= xconnect(s
, res
->ai_addr
, res
->ai_addrlen
);
228 /* this "if" clause is to prevent print warning twice */
230 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
,
231 hbuf
, sizeof(hbuf
), NULL
, 0,
233 strlcpy(hbuf
, "?", sizeof(hbuf
));
234 warn("connect to address %s", hbuf
);
242 /* finally we got one */
251 memcpy(&hisctladdr
.si_su
, res
->ai_addr
, res
->ai_addrlen
);
252 hisctladdr
.su_len
= res
->ai_addrlen
;
256 len
= hisctladdr
.su_len
;
257 if (getsockname(s
, (struct sockaddr
*)&myctladdr
.si_su
, &len
) == -1) {
262 myctladdr
.su_len
= len
;
264 #ifdef IPTOS_LOWDELAY
265 if (hisctladdr
.su_family
== AF_INET
) {
266 int tos
= IPTOS_LOWDELAY
;
267 if (setsockopt(s
, IPPROTO_IP
, IP_TOS
,
268 (void *)&tos
, sizeof(tos
)) == -1) {
270 warn("setsockopt %s (ignored)",
275 cin
= fdopen(s
, "r");
276 cout
= fdopen(s
, "w");
277 if (cin
== NULL
|| cout
== NULL
) {
278 warnx("fdopen failed.");
287 fprintf(ttyout
, "Connected to %s.\n", hostname
);
288 if (getreply(0) > 2) { /* read startup message from server */
297 if (setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
,
298 (void *)&on
, sizeof(on
)) == -1) {
300 warn("setsockopt %s (ignored)", "SO_OOBINLINE");
310 cmdabort(int notused
)
317 write(fileno(ttyout
), "\n", 1);
320 siglongjmp(ptabort
, 1);
325 cmdtimeout(int notused
)
331 write(fileno(ttyout
), "\n", 1);
334 siglongjmp(ptabort
, 1);
340 command(const char *fmt
, ...)
347 fputs("---> ", ttyout
);
349 if (strncmp("PASS ", fmt
, 5) == 0)
350 fputs("PASS XXXX", ttyout
);
351 else if (strncmp("ACCT ", fmt
, 5) == 0)
352 fputs("ACCT XXXX", ttyout
);
354 vfprintf(ttyout
, fmt
, ap
);
359 warnx("No control connection for command.");
366 oldsigint
= xsignal(SIGINT
, cmdabort
);
369 vfprintf(cout
, fmt
, ap
);
374 r
= getreply(!strcmp(fmt
, "QUIT"));
375 if (abrtflag
&& oldsigint
!= SIG_IGN
)
376 (*oldsigint
)(SIGINT
);
377 (void)xsignal(SIGINT
, oldsigint
);
382 getreply(int expecteof
)
384 char current_line
[BUFSIZ
]; /* last line of previous reply */
387 int originalcode
= 0, continuation
= 0;
388 sigfunc oldsigint
, oldsigalrm
;
390 char *cp
, *pt
= pasv
;
395 oldsigint
= xsignal(SIGINT
, cmdabort
);
396 oldsigalrm
= xsignal(SIGALRM
, cmdtimeout
);
398 for (line
= 0 ;; line
++) {
401 while (alarmtimer(60),((c
= getc(cin
)) != '\n')) {
402 if (c
== IAC
) { /* handle telnet commands */
403 switch (c
= getc(cin
)) {
407 fprintf(cout
, "%c%c%c", IAC
, DONT
, c
);
413 fprintf(cout
, "%c%c%c", IAC
, WONT
, c
);
424 * these will get trashed by pswitch()
427 int reply_timeoutflag
= timeoutflag
;
428 int reply_abrtflag
= abrtflag
;
431 if (expecteof
&& feof(cin
)) {
432 (void)xsignal(SIGINT
, oldsigint
);
433 (void)xsignal(SIGALRM
, oldsigalrm
);
440 if (reply_timeoutflag
)
442 "421 Service not available, remote server timed out. Connection closed\n",
444 else if (reply_abrtflag
)
446 "421 Service not available, user interrupt. Connection closed.\n",
450 "421 Service not available, remote server has closed connection.\n",
452 (void)fflush(ttyout
);
455 (void)xsignal(SIGINT
, oldsigint
);
456 (void)xsignal(SIGALRM
, oldsigalrm
);
459 if (c
!= '\r' && (verbose
> 0 ||
460 ((verbose
> -1 && n
== '5' && dig
> 4) &&
461 (((!n
&& c
< '5') || (n
&& n
< '5'))
462 || !retry_connect
)))) {
464 (dig
== 1 || (dig
== 5 && verbose
== 0)))
465 fprintf(ttyout
, "%s:", hostname
);
466 (void)putc(c
, ttyout
);
468 if (dig
< 4 && isdigit(c
))
469 code
= code
* 10 + (c
- '0');
470 if (!pflag
&& (code
== 227 || code
== 228))
472 else if (!pflag
&& code
== 229)
474 if (dig
> 4 && pflag
== 1 && isdigit(c
))
477 if (c
!= '\r' && c
!= ')') {
478 if (pt
< &pasv
[sizeof(pasv
) - 1])
485 if (pflag
== 100 && c
== '(')
487 if (dig
== 4 && c
== '-') {
494 if (cp
< ¤t_line
[sizeof(current_line
) - 1])
497 if (verbose
> 0 || ((verbose
> -1 && n
== '5') &&
498 (n
< '5' || !retry_connect
))) {
499 (void)putc(c
, ttyout
);
500 (void)fflush (ttyout
);
506 (void)strlcpy(reply_string
, current_line
,
507 sizeof(reply_string
));
508 if (line
> 0 && code
== 0 && reply_callback
!= NULL
)
509 (*reply_callback
)(current_line
);
510 if (continuation
&& code
!= originalcode
) {
511 if (originalcode
== 0)
518 (void)xsignal(SIGINT
, oldsigint
);
519 (void)xsignal(SIGALRM
, oldsigalrm
);
520 if (code
== 421 || originalcode
== 421)
522 if (abrtflag
&& oldsigint
!= cmdabort
&& oldsigint
!= SIG_IGN
)
523 (*oldsigint
)(SIGINT
);
524 if (timeoutflag
&& oldsigalrm
!= cmdtimeout
&&
525 oldsigalrm
!= SIG_IGN
)
526 (*oldsigalrm
)(SIGINT
);
532 empty(FILE *cin
, FILE *din
, int sec
)
535 struct pollfd pfd
[2];
539 pfd
[nfd
].fd
= fileno(cin
);
540 pfd
[nfd
++].events
= POLLIN
;
544 pfd
[nfd
].fd
= fileno(din
);
545 pfd
[nfd
++].events
= POLLIN
;
548 if ((nr
= xpoll(pfd
, nfd
, sec
* 1000)) <= 0)
554 nr
|= (pfd
[nfd
++].revents
& POLLIN
) ? 1 : 0;
556 nr
|= (pfd
[nfd
++].revents
& POLLIN
) ? 2 : 0;
560 sigjmp_buf xferabort
;
563 abortxfer(int notused
)
572 switch (direction
[0]) {
574 strlcpy(msgbuf
, "\nreceive", sizeof(msgbuf
));
577 strlcpy(msgbuf
, "\nsend", sizeof(msgbuf
));
580 errx(1, "abortxfer called with unknown direction `%s'",
583 len
= strlcat(msgbuf
, " aborted. Waiting for remote to finish abort.\n",
585 write(fileno(ttyout
), msgbuf
, len
);
586 siglongjmp(xferabort
, 1);
590 sendrequest(const char *cmd
, const char *local
, const char *remote
,
596 int (*closefunc
)(FILE *);
597 sigfunc oldintr
, oldintp
;
598 volatile off_t hashbytes
;
600 static size_t bufsize
;
604 #ifdef __GNUC__ /* to shut up gcc warnings */
618 oprogress
= progress
;
619 if (verbose
&& printnames
) {
620 if (local
&& *local
!= '-')
621 fprintf(ttyout
, "local: %s ", local
);
623 fprintf(ttyout
, "remote: %s\n", remote
);
626 proxtrans(cmd
, local
, remote
);
635 if (sigsetjmp(xferabort
, 1)) {
641 (void)xsignal(SIGQUIT
, psummary
);
642 oldintr
= xsignal(SIGINT
, abortxfer
);
643 if (strcmp(local
, "-") == 0) {
646 } else if (*local
== '|') {
647 oldintp
= xsignal(SIGPIPE
, SIG_IGN
);
648 fin
= popen(local
+ 1, "r");
650 warn("%s", local
+ 1);
657 fin
= fopen(local
, "r");
659 warn("local: %s", local
);
664 if (fstat(fileno(fin
), &st
) < 0 || !S_ISREG(st
.st_mode
)) {
665 fprintf(ttyout
, "%s: not a plain file.\n", local
);
669 filesize
= st
.st_size
;
675 if (sigsetjmp(xferabort
, 1))
679 (strcmp(cmd
, "STOR") == 0 || strcmp(cmd
, "APPE") == 0)) {
685 rc
= fseeko(fin
, restart_point
, SEEK_SET
);
689 rc
= lseek(fileno(fin
), restart_point
, SEEK_SET
);
693 warn("local: %s", local
);
696 if (command("REST " LLF
, (LLT
)restart_point
) != CONTINUE
)
701 if (command("%s %s", cmd
, remote
) != PRELIM
)
704 if (command("%s", cmd
) != PRELIM
)
708 dout
= dataconn(lmode
);
712 if (sndbuf_size
> bufsize
) {
715 bufsize
= sndbuf_size
;
716 buf
= xmalloc(bufsize
);
720 oldintp
= xsignal(SIGPIPE
, SIG_IGN
);
726 if (rate_put
) { /* rate limited */
728 struct timeval then
, now
, td
;
731 (void)gettimeofday(&then
, NULL
);
735 if ((c
= read(fileno(fin
), buf
,
736 MIN(bufsize
, bufrem
))) <= 0)
740 for (bufp
= buf
; c
> 0;
742 if ((d
= write(fileno(dout
),
748 (!progress
|| filesize
< 0) ) {
749 while (bytes
>= hashbytes
) {
750 (void)putc('#', ttyout
);
753 (void)fflush(ttyout
);
757 (void)gettimeofday(&now
, NULL
);
758 timersub(&now
, &then
, &td
);
761 usleep(1000000 - td
.tv_usec
);
764 } else { /* simpler/faster; no rate limit */
767 if ((c
= read(fileno(fin
), buf
, bufsize
)) <= 0)
770 for (bufp
= buf
; c
> 0; c
-= d
, bufp
+= d
)
771 if ((d
= write(fileno(dout
), bufp
, c
))
776 if (hash
&& (!progress
|| filesize
< 0) ) {
777 while (bytes
>= hashbytes
) {
778 (void)putc('#', ttyout
);
781 (void)fflush(ttyout
);
786 if (hash
&& (!progress
|| filesize
< 0) && bytes
> 0) {
788 (void)putc('#', ttyout
);
789 (void)putc('\n', ttyout
);
792 warn("local: %s", local
);
801 while ((c
= getc(fin
)) != EOF
) {
803 while (hash
&& (!progress
|| filesize
< 0) &&
804 (bytes
>= hashbytes
)) {
805 (void)putc('#', ttyout
);
806 (void)fflush(ttyout
);
811 (void)putc('\r', dout
);
816 #if 0 /* this violates RFC */
818 (void)putc('\0', dout
);
823 if (hash
&& (!progress
|| filesize
< 0)) {
824 if (bytes
< hashbytes
)
825 (void)putc('#', ttyout
);
826 (void)putc('\n', ttyout
);
829 warn("local: %s", local
);
839 if (closefunc
!= NULL
) {
851 (void)xsignal(SIGINT
, oldintr
);
872 (void)xsignal(SIGINT
, oldintr
);
874 (void)xsignal(SIGPIPE
, oldintp
);
879 if (closefunc
!= NULL
&& fin
!= NULL
)
883 progress
= oprogress
;
889 recvrequest(const char *cmd
, const char *local
, const char *remote
,
890 const char *lmode
, int printnames
, int ignorespecial
)
893 int (*closefunc
)(FILE *);
894 sigfunc oldintr
, oldintp
;
896 volatile int is_retr
, tcrflag
, bare_lfs
;
897 static size_t bufsize
;
899 volatile off_t hashbytes
;
902 struct timeval tval
[2];
906 #ifdef __GNUC__ /* to shut up gcc warnings */
918 direction
= "received";
922 oprogress
= progress
;
923 opreserve
= preserve
;
924 is_retr
= (strcmp(cmd
, "RETR") == 0);
925 if (is_retr
&& verbose
&& printnames
) {
926 if (local
&& (ignorespecial
|| *local
!= '-'))
927 fprintf(ttyout
, "local: %s ", local
);
929 fprintf(ttyout
, "remote: %s\n", remote
);
931 if (proxy
&& is_retr
) {
932 proxtrans(cmd
, local
, remote
);
938 tcrflag
= !crflag
&& is_retr
;
939 if (sigsetjmp(xferabort
, 1)) {
945 (void)xsignal(SIGQUIT
, psummary
);
946 oldintr
= xsignal(SIGINT
, abortxfer
);
947 if (ignorespecial
|| (strcmp(local
, "-") && *local
!= '|')) {
948 if (access(local
, W_OK
) < 0) {
949 char *dir
= strrchr(local
, '/');
951 if (errno
!= ENOENT
&& errno
!= EACCES
) {
952 warn("local: %s", local
);
958 d
= access(dir
== local
? "/" :
959 dir
? local
: ".", W_OK
);
963 warn("local: %s", local
);
967 if (!runique
&& errno
== EACCES
&&
968 chmod(local
, (S_IRUSR
|S_IWUSR
)) < 0) {
969 warn("local: %s", local
);
973 if (runique
&& errno
== EACCES
&&
974 (local
= gunique(local
)) == NULL
) {
979 else if (runique
&& (local
= gunique(local
)) == NULL
) {
985 if (curtype
!= TYPE_A
)
986 changetype(TYPE_A
, 0);
990 filesize
= remotesize(remote
, 0);
991 if (code
== 421 || code
== -1)
998 if (sigsetjmp(xferabort
, 1))
1000 if (is_retr
&& restart_point
&&
1001 command("REST " LLF
, (LLT
) restart_point
) != CONTINUE
)
1003 if (! EMPTYSTRING(remote
)) {
1004 if (command("%s %s", cmd
, remote
) != PRELIM
)
1007 if (command("%s", cmd
) != PRELIM
)
1010 din
= dataconn("r");
1013 if (!ignorespecial
&& strcmp(local
, "-") == 0) {
1017 } else if (!ignorespecial
&& *local
== '|') {
1018 oldintp
= xsignal(SIGPIPE
, SIG_IGN
);
1019 fout
= popen(local
+ 1, "w");
1021 warn("%s", local
+1);
1028 fout
= fopen(local
, lmode
);
1030 warn("local: %s", local
);
1036 if (fstat(fileno(fout
), &st
) != -1 && !S_ISREG(st
.st_mode
)) {
1040 if (rcvbuf_size
> bufsize
) {
1043 bufsize
= rcvbuf_size
;
1044 buf
= xmalloc(bufsize
);
1053 if (is_retr
&& restart_point
&&
1054 lseek(fileno(fout
), restart_point
, SEEK_SET
) < 0) {
1055 warn("local: %s", local
);
1058 if (rate_get
) { /* rate limiting */
1060 struct timeval then
, now
, td
;
1063 (void)gettimeofday(&then
, NULL
);
1065 for (bufrem
= rate_get
; bufrem
> 0; ) {
1066 if ((c
= read(fileno(din
), buf
,
1067 MIN(bufsize
, bufrem
))) <= 0)
1071 if ((d
= write(fileno(fout
), buf
, c
))
1075 (!progress
|| filesize
< 0)) {
1076 while (bytes
>= hashbytes
) {
1077 (void)putc('#', ttyout
);
1080 (void)fflush(ttyout
);
1083 /* sleep until time is up */
1085 (void)gettimeofday(&now
, NULL
);
1086 timersub(&now
, &then
, &td
);
1089 usleep(1000000 - td
.tv_usec
);
1092 } else { /* faster code (no limiting) */
1095 if ((c
= read(fileno(din
), buf
, bufsize
)) <= 0)
1098 if ((d
= write(fileno(fout
), buf
, c
)) != c
)
1100 if (hash
&& (!progress
|| filesize
< 0)) {
1101 while (bytes
>= hashbytes
) {
1102 (void)putc('#', ttyout
);
1105 (void)fflush(ttyout
);
1110 if (hash
&& (!progress
|| filesize
< 0) && bytes
> 0) {
1112 (void)putc('#', ttyout
);
1113 (void)putc('\n', ttyout
);
1122 warn("local: %s", local
);
1124 warnx("%s: short write", local
);
1129 if (is_retr
&& restart_point
) {
1133 if (fseeko(fout
, (off_t
)0, SEEK_SET
) < 0)
1135 for (i
= 0; i
++ < restart_point
;) {
1136 if ((ch
= getc(fout
)) == EOF
)
1141 if (fseeko(fout
, (off_t
)0, SEEK_CUR
) < 0) {
1143 warn("local: %s", local
);
1147 while ((c
= getc(din
)) != EOF
) {
1151 while (hash
&& (!progress
|| filesize
< 0) &&
1152 (bytes
>= hashbytes
)) {
1153 (void)putc('#', ttyout
);
1154 (void)fflush(ttyout
);
1158 if ((c
= getc(din
)) != '\n' || tcrflag
) {
1161 (void)putc('\r', fout
);
1170 (void)putc(c
, fout
);
1175 if (hash
&& (!progress
|| filesize
< 0)) {
1176 if (bytes
< hashbytes
)
1177 (void)putc('#', ttyout
);
1178 (void)putc('\n', ttyout
);
1186 warn("local: %s", local
);
1191 if (closefunc
!= NULL
) {
1200 "WARNING! %d bare linefeeds received in ASCII mode.\n",
1202 fputs("File may not have transferred correctly.\n", ttyout
);
1204 if (bytes
>= 0 && is_retr
) {
1207 if (preserve
&& (closefunc
== fclose
)) {
1208 mtime
= remotemodtime(remote
, 0);
1210 (void)gettimeofday(&tval
[0], NULL
);
1211 tval
[1].tv_sec
= mtime
;
1212 tval
[1].tv_usec
= 0;
1213 if (utimes(local
, tval
) == -1) {
1215 "Can't change modification time on %s to %s",
1216 local
, asctime(localtime(&mtime
)));
1225 * abort using RFC 959 recommended IP,SYNC sequence
1227 if (! sigsetjmp(xferabort
, 1)) {
1228 /* this is the first call */
1229 (void)xsignal(SIGINT
, abort_squared
);
1242 (void)xsignal(SIGINT
, oldintr
);
1244 (void)xsignal(SIGPIPE
, oldintp
);
1249 if (closefunc
!= NULL
&& fout
!= NULL
)
1253 progress
= oprogress
;
1254 preserve
= opreserve
;
1259 * Need to start a listen on the data channel before we send the command,
1260 * otherwise the server's connect may fail.
1266 int result
, tmpno
= 0;
1269 u_int addr
[16], port
[2];
1272 char *pasvcmd
= NULL
;
1275 if (myctladdr
.su_family
== AF_INET6
&& debug
&&
1276 (IN6_IS_ADDR_LINKLOCAL(&myctladdr
.si_su
.su_sin6
.sin6_addr
) ||
1277 IN6_IS_ADDR_SITELOCAL(&myctladdr
.si_su
.su_sin6
.sin6_addr
))) {
1278 warnx("use of scoped address can be troublesome");
1283 data_addr
= myctladdr
;
1284 data
= socket(data_addr
.su_family
, SOCK_STREAM
, 0);
1289 if ((options
& SO_DEBUG
) &&
1290 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
,
1291 (void *)&on
, sizeof(on
)) == -1) {
1293 warn("setsockopt %s (ignored)", "SO_DEBUG");
1295 result
= COMPLETE
+ 1;
1296 switch (data_addr
.su_family
) {
1298 if (epsv4
&& !epsv4bad
) {
1300 result
= command("EPSV");
1304 * this code is to be friendly with broken
1307 if (code
/ 10 == 22 && code
!= 229) {
1309 "wrong server: return code must be 229\n",
1311 result
= COMPLETE
+ 1;
1313 if (result
!= COMPLETE
) {
1317 "disabling epsv4 for this connection\n",
1321 if (result
!= COMPLETE
) {
1323 result
= command("PASV");
1331 result
= command("EPSV");
1334 /* this code is to be friendly with broken BSDI ftpd */
1335 if (code
/ 10 == 22 && code
!= 229) {
1337 "wrong server: return code must be 229\n",
1339 result
= COMPLETE
+ 1;
1341 if (result
!= COMPLETE
) {
1343 result
= command("LPSV");
1350 result
= COMPLETE
+ 1;
1353 if (result
!= COMPLETE
) {
1354 if (activefallback
) {
1363 fputs("Passive mode refused.\n", ttyout
);
1367 #define pack2(var, off) \
1368 (((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))
1369 #define pack4(var, off) \
1370 (((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \
1371 ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))
1372 #define UC(b) (((int)b)&0xff)
1375 * What we've got at this point is a string of comma separated
1376 * one-byte unsigned integer values, separated by commas.
1378 if (strcmp(pasvcmd
, "PASV") == 0) {
1379 if (data_addr
.su_family
!= AF_INET
) {
1381 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout
);
1385 if (code
/ 10 == 22 && code
!= 227) {
1386 fputs("wrong server: return code must be 227\n",
1391 error
= sscanf(pasv
, "%u,%u,%u,%u,%u,%u",
1392 &addr
[0], &addr
[1], &addr
[2], &addr
[3],
1393 &port
[0], &port
[1]);
1396 "Passive mode address scan failure. Shouldn't happen!\n", ttyout
);
1401 memset(&data_addr
, 0, sizeof(data_addr
));
1402 data_addr
.su_family
= AF_INET
;
1403 data_addr
.su_len
= sizeof(struct sockaddr_in
);
1404 data_addr
.si_su
.su_sin
.sin_addr
.s_addr
=
1405 htonl(pack4(addr
, 0));
1406 data_addr
.su_port
= htons(pack2(port
, 0));
1407 } else if (strcmp(pasvcmd
, "LPSV") == 0) {
1408 if (code
/ 10 == 22 && code
!= 228) {
1409 fputs("wrong server: return code must be 228\n",
1414 switch (data_addr
.su_family
) {
1416 error
= sscanf(pasv
,
1417 "%u,%u,%u,%u,%u,%u,%u,%u,%u",
1419 &addr
[0], &addr
[1], &addr
[2], &addr
[3],
1420 &pal
, &port
[0], &port
[1]);
1423 "Passive mode address scan failure. Shouldn't happen!\n", ttyout
);
1427 if (af
!= 4 || hal
!= 4 || pal
!= 2) {
1429 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout
);
1435 memset(&data_addr
, 0, sizeof(data_addr
));
1436 data_addr
.su_family
= AF_INET
;
1437 data_addr
.su_len
= sizeof(struct sockaddr_in
);
1438 data_addr
.si_su
.su_sin
.sin_addr
.s_addr
=
1439 htonl(pack4(addr
, 0));
1440 data_addr
.su_port
= htons(pack2(port
, 0));
1444 error
= sscanf(pasv
,
1445 "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
1447 &addr
[0], &addr
[1], &addr
[2], &addr
[3],
1448 &addr
[4], &addr
[5], &addr
[6], &addr
[7],
1449 &addr
[8], &addr
[9], &addr
[10],
1450 &addr
[11], &addr
[12], &addr
[13],
1451 &addr
[14], &addr
[15],
1452 &pal
, &port
[0], &port
[1]);
1455 "Passive mode address scan failure. Shouldn't happen!\n", ttyout
);
1459 if (af
!= 6 || hal
!= 16 || pal
!= 2) {
1461 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout
);
1467 memset(&data_addr
, 0, sizeof(data_addr
));
1468 data_addr
.su_family
= AF_INET6
;
1469 data_addr
.su_len
= sizeof(struct sockaddr_in6
);
1472 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
1473 data_addr
.si_su
.su_sin6
.sin6_addr
.s6_addr
[i
] =
1477 data_addr
.su_port
= htons(pack2(port
, 0));
1483 } else if (strcmp(pasvcmd
, "EPSV") == 0) {
1487 if (code
/ 10 == 22 && code
!= 229) {
1488 fputs("wrong server: return code must be 229\n",
1493 if (sscanf(pasv
, "%c%c%c%d%c", &delim
[0],
1494 &delim
[1], &delim
[2], &port
[1],
1496 fputs("parse error!\n", ttyout
);
1500 if (delim
[0] != delim
[1] || delim
[0] != delim
[2]
1501 || delim
[0] != delim
[3]) {
1502 fputs("parse error!\n", ttyout
);
1506 data_addr
= hisctladdr
;
1507 data_addr
.su_port
= htons(port
[1]);
1511 while (xconnect(data
, (struct sockaddr
*)&data_addr
.si_su
,
1512 data_addr
.su_len
) < 0) {
1513 if (activefallback
) {
1522 warn("connect for data channel");
1525 #ifdef IPTOS_THROUGHPUT
1526 if (data_addr
.su_family
== AF_INET
) {
1527 on
= IPTOS_THROUGHPUT
;
1528 if (setsockopt(data
, IPPROTO_IP
, IP_TOS
,
1529 (void *)&on
, sizeof(on
)) == -1) {
1531 warn("setsockopt %s (ignored)",
1532 "IPTOS_THROUGHPUT");
1540 data_addr
= myctladdr
;
1542 data_addr
.su_port
= 0; /* let system pick one */
1545 data
= socket(data_addr
.su_family
, SOCK_STREAM
, 0);
1553 if (setsockopt(data
, SOL_SOCKET
, SO_REUSEADDR
,
1554 (void *)&on
, sizeof(on
)) == -1) {
1555 warn("setsockopt %s", "SO_REUSEADDR");
1558 if (bind(data
, (struct sockaddr
*)&data_addr
.si_su
,
1559 data_addr
.su_len
) < 0) {
1563 if ((options
& SO_DEBUG
) &&
1564 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
,
1565 (void *)&on
, sizeof(on
)) == -1) {
1567 warn("setsockopt %s (ignored)", "SO_DEBUG");
1569 len
= sizeof(data_addr
.si_su
);
1570 memset((char *)&data_addr
, 0, sizeof (data_addr
));
1571 if (getsockname(data
, (struct sockaddr
*)&data_addr
.si_su
, &len
) == -1) {
1572 warn("getsockname");
1575 data_addr
.su_len
= len
;
1576 if (xlisten(data
, 1) < 0)
1580 char hname
[NI_MAXHOST
], sname
[NI_MAXSERV
];
1582 struct sockinet tmp
;
1584 switch (data_addr
.su_family
) {
1586 if (!epsv4
|| epsv4bad
) {
1587 result
= COMPLETE
+ 1;
1594 af
= (data_addr
.su_family
== AF_INET
) ? 1 : 2;
1597 if (tmp
.su_family
== AF_INET6
)
1598 tmp
.si_su
.su_sin6
.sin6_scope_id
= 0;
1600 if (getnameinfo((struct sockaddr
*)&tmp
.si_su
,
1601 tmp
.su_len
, hname
, sizeof(hname
), sname
,
1602 sizeof(sname
), NI_NUMERICHOST
| NI_NUMERICSERV
)) {
1605 result
= command("EPRT |%d|%s|%s|", af
, hname
,
1609 if (result
!= COMPLETE
) {
1613 "disabling epsv4 for this connection\n",
1619 result
= COMPLETE
+ 1;
1622 if (result
== COMPLETE
)
1625 switch (data_addr
.su_family
) {
1627 a
= (char *)&data_addr
.si_su
.su_sin
.sin_addr
;
1628 p
= (char *)&data_addr
.su_port
;
1629 result
= command("PORT %d,%d,%d,%d,%d,%d",
1630 UC(a
[0]), UC(a
[1]), UC(a
[2]), UC(a
[3]),
1631 UC(p
[0]), UC(p
[1]));
1635 a
= (char *)&data_addr
.si_su
.su_sin6
.sin6_addr
;
1636 p
= (char *)&data_addr
.su_port
;
1638 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1640 UC(a
[0]),UC(a
[1]),UC(a
[2]),UC(a
[3]),
1641 UC(a
[4]),UC(a
[5]),UC(a
[6]),UC(a
[7]),
1642 UC(a
[8]),UC(a
[9]),UC(a
[10]),UC(a
[11]),
1643 UC(a
[12]),UC(a
[13]),UC(a
[14]),UC(a
[15]),
1644 2, UC(p
[0]), UC(p
[1]));
1648 result
= COMPLETE
+ 1; /* xxx */
1654 if (result
== ERROR
&& sendport
== -1) {
1659 return (result
!= COMPLETE
);
1663 #ifdef IPTOS_THROUGHPUT
1664 if (data_addr
.su_family
== AF_INET
) {
1665 on
= IPTOS_THROUGHPUT
;
1666 if (setsockopt(data
, IPPROTO_IP
, IP_TOS
,
1667 (void *)&on
, sizeof(on
)) == -1)
1669 warn("setsockopt %s (ignored)",
1670 "IPTOS_THROUGHPUT");
1683 dataconn(const char *lmode
)
1685 struct sockinet from
;
1686 int s
, flags
, rv
, timeout
;
1687 struct timeval endtime
, now
, td
;
1688 struct pollfd pfd
[1];
1691 if (passivemode
) /* passive data connection */
1692 return (fdopen(data
, lmode
));
1694 /* active mode data connection */
1696 if ((flags
= fcntl(data
, F_GETFL
, 0)) == -1)
1697 goto dataconn_failed
; /* get current socket flags */
1698 if (fcntl(data
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
1699 goto dataconn_failed
; /* set non-blocking connect */
1701 /* NOTE: we now must restore socket flags on successful exit */
1703 /* limit time waiting on listening socket */
1705 pfd
[0].events
= POLLIN
;
1706 (void)gettimeofday(&endtime
, NULL
); /* determine end time */
1707 endtime
.tv_sec
+= (quit_time
> 0) ? quit_time
: 60;
1708 /* without -q, default to 60s */
1710 (void)gettimeofday(&now
, NULL
);
1711 timersub(&endtime
, &now
, &td
);
1712 timeout
= td
.tv_sec
* 1000 + td
.tv_usec
/1000;
1715 rv
= xpoll(pfd
, 1, timeout
);
1716 } while (rv
== -1 && errno
== EINTR
); /* loop until poll ! EINTR */
1718 warn("poll waiting before accept");
1719 goto dataconn_failed
;
1722 warn("poll timeout waiting before accept");
1723 goto dataconn_failed
;
1726 /* (non-blocking) accept the connection */
1727 fromlen
= myctladdr
.su_len
;
1729 s
= accept(data
, (struct sockaddr
*) &from
.si_su
, &fromlen
);
1730 } while (s
== -1 && errno
== EINTR
); /* loop until accept ! EINTR */
1733 goto dataconn_failed
;
1738 if (fcntl(data
, F_SETFL
, flags
) == -1) /* restore socket flags */
1739 goto dataconn_failed
;
1741 #ifdef IPTOS_THROUGHPUT
1742 if (from
.su_family
== AF_INET
) {
1743 int tos
= IPTOS_THROUGHPUT
;
1744 if (setsockopt(s
, IPPROTO_IP
, IP_TOS
,
1745 (void *)&tos
, sizeof(tos
)) == -1) {
1747 warn("setsockopt %s (ignored)",
1748 "IPTOS_THROUGHPUT");
1752 return (fdopen(data
, lmode
));
1761 psabort(int notused
)
1775 static struct comvars
{
1777 char name
[MAXHOSTNAMELEN
];
1778 struct sockinet mctl
;
1779 struct sockinet hctl
;
1792 char mi
[MAXPATHLEN
];
1793 char mo
[MAXPATHLEN
];
1794 } proxstruct
, tmpstruct
;
1795 struct comvars
*ip
, *op
;
1798 oldintr
= xsignal(SIGINT
, psabort
);
1812 ip
->connect
= connected
;
1813 connected
= op
->connect
;
1815 (void)strlcpy(ip
->name
, hostname
, sizeof(ip
->name
));
1818 hostname
= op
->name
;
1819 ip
->hctl
= hisctladdr
;
1820 hisctladdr
= op
->hctl
;
1821 ip
->mctl
= myctladdr
;
1822 myctladdr
= op
->mctl
;
1829 ip
->curtpe
= curtype
;
1830 curtype
= op
->curtpe
;
1833 ip
->sunqe
= sunique
;
1834 sunique
= op
->sunqe
;
1835 ip
->runqe
= runique
;
1836 runique
= op
->runqe
;
1841 (void)strlcpy(ip
->nti
, ntin
, sizeof(ip
->nti
));
1842 (void)strlcpy(ntin
, op
->nti
, sizeof(ntin
));
1843 (void)strlcpy(ip
->nto
, ntout
, sizeof(ip
->nto
));
1844 (void)strlcpy(ntout
, op
->nto
, sizeof(ntout
));
1845 ip
->mapflg
= mapflag
;
1846 mapflag
= op
->mapflg
;
1847 (void)strlcpy(ip
->mi
, mapin
, sizeof(ip
->mi
));
1848 (void)strlcpy(mapin
, op
->mi
, sizeof(mapin
));
1849 (void)strlcpy(ip
->mo
, mapout
, sizeof(ip
->mo
));
1850 (void)strlcpy(mapout
, op
->mo
, sizeof(mapout
));
1851 (void)xsignal(SIGINT
, oldintr
);
1859 abortpt(int notused
)
1865 write(fileno(ttyout
), "\n", 1);
1869 siglongjmp(ptabort
, 1);
1873 proxtrans(const char *cmd
, const char *local
, const char *remote
)
1876 int prox_type
, nfnd
;
1877 volatile int secndflag
;
1880 #ifdef __GNUC__ /* to shut up gcc warnings */
1887 if (strcmp(cmd
, "RETR"))
1890 cmd2
= runique
? "STOU" : "STOR";
1891 if ((prox_type
= type
) == 0) {
1892 if (unix_server
&& unix_proxy
)
1897 if (curtype
!= prox_type
)
1898 changetype(prox_type
, 1);
1899 if (command("PASV") != COMPLETE
) {
1900 fputs("proxy server does not support third party transfers.\n",
1906 fputs("No primary connection.\n", ttyout
);
1911 if (curtype
!= prox_type
)
1912 changetype(prox_type
, 1);
1913 if (command("PORT %s", pasv
) != COMPLETE
) {
1917 if (sigsetjmp(ptabort
, 1))
1919 oldintr
= xsignal(SIGINT
, abortpt
);
1920 if ((restart_point
&&
1921 (command("REST " LLF
, (LLT
) restart_point
) != CONTINUE
))
1922 || (command("%s %s", cmd
, remote
) != PRELIM
)) {
1923 (void)xsignal(SIGINT
, oldintr
);
1930 if ((restart_point
&&
1931 (command("REST " LLF
, (LLT
) restart_point
) != CONTINUE
))
1932 || (command("%s %s", cmd2
, local
) != PRELIM
))
1938 (void)xsignal(SIGINT
, oldintr
);
1941 fprintf(ttyout
, "local: %s remote: %s\n", local
, remote
);
1944 if (sigsetjmp(xferabort
, 1)) {
1945 (void)xsignal(SIGINT
, oldintr
);
1948 (void)xsignal(SIGINT
, abort_squared
);
1950 if (strcmp(cmd
, "RETR") && !proxy
)
1952 else if (!strcmp(cmd
, "RETR") && proxy
)
1954 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1955 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1963 (void)xsignal(SIGINT
, oldintr
);
1969 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1970 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1977 (void)xsignal(SIGINT
, oldintr
);
1985 if ((nfnd
= empty(cin
, NULL
, 10)) <= 0) {
2000 (void)xsignal(SIGINT
, oldintr
);
2004 reset(int argc
, char *argv
[])
2008 if (argc
== 0 && argv
!= NULL
) {
2009 fprintf(ttyout
, "usage: %s\n", argv
[0]);
2014 if ((nfnd
= empty(cin
, NULL
, 0)) < 0) {
2024 gunique(const char *local
)
2026 static char new[MAXPATHLEN
];
2027 char *cp
= strrchr(local
, '/');
2028 int d
, count
=0, len
;
2033 d
= access(cp
== local
? "/" : cp
? local
: ".", W_OK
);
2037 warn("local: %s", local
);
2040 len
= strlcpy(new, local
, sizeof(new));
2044 if (++count
== 100) {
2045 fputs("runique: can't find unique file name.\n",
2055 if ((d
= access(new, F_OK
)) < 0)
2059 else if (*(cp
- 2) == '.')
2062 *(cp
- 2) = *(cp
- 2) + 1;
2071 * aborts abort_remote(). lostpeer() is called because if the user is
2072 * too impatient to wait or there's another problem then ftp really
2073 * needs to get back to a known state.
2076 abort_squared(int dummy
)
2083 len
= strlcpy(msgbuf
, "\nremote abort aborted; closing connection.\n",
2085 write(fileno(ttyout
), msgbuf
, len
);
2087 siglongjmp(xferabort
, 1);
2091 abort_remote(FILE *din
)
2097 warnx("Lost control connection for abort.");
2104 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
2105 * after urgent byte rather than before as is protocol now
2110 if (send(fileno(cout
), buf
, 3, MSG_OOB
) != 3)
2112 fprintf(cout
, "%cABOR\r\n", DM
);
2114 if ((nfnd
= empty(cin
, din
, 10)) <= 0) {
2121 if (din
&& (nfnd
& 2)) {
2122 while (read(fileno(din
), buf
, BUFSIZ
) > 0)
2125 if (getreply(0) == ERROR
&& code
== 552) {
2126 /* 552 needed for nic style abort */
2133 ai_unmapped(struct addrinfo
*ai
)
2136 struct sockaddr_in6
*sin6
;
2137 struct sockaddr_in sin
;
2140 if (ai
->ai_family
!= AF_INET6
)
2142 if (ai
->ai_addrlen
!= sizeof(struct sockaddr_in6
) ||
2143 sizeof(sin
) > ai
->ai_addrlen
)
2145 sin6
= (struct sockaddr_in6
*)ai
->ai_addr
;
2146 if (!IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
2149 memset(&sin
, 0, sizeof(sin
));
2150 sin
.sin_family
= AF_INET
;
2151 len
= sizeof(struct sockaddr_in
);
2152 memcpy(&sin
.sin_addr
, &sin6
->sin6_addr
.s6_addr
[12],
2153 sizeof(sin
.sin_addr
));
2154 sin
.sin_port
= sin6
->sin6_port
;
2156 ai
->ai_family
= AF_INET
;
2157 #if HAVE_SOCKADDR_SA_LEN
2160 memcpy(ai
->ai_addr
, &sin
, len
);
2161 ai
->ai_addrlen
= len
;