(kcmd_connect): Use getaddrinfo instead of gethostbyname, but only
get AF_INET addresses for now. Add new argument, pointer to where
to store local socket address used; call getsockname to fill it
- in.
+ in. Always call getport with the address family from the current
+ address. Retry connection to the same remote address if the error
+ returned was EADDRINUSE; that applies to the local address.
(setup_socket): New function.
(getport): Use setup_socket. When selecting an address family to
use, fall through in the last case instead of calling getport
recursively.
(kcmd, k4cmd): Don't call getsockname, let kcmd_connect do it.
- Delete unused variables.
+ Delete unused variables. Don't set *fd2p before calling
+ setup_secondary_channel.
(setup_secondary_channel): No cleanup is needed, just return error
- indications rather than branching to end of function.
+ indications rather than branching to end of function. If fd2p is
+ non-null, initialize the pointed-to value to -1.
* rcp.M: Document -f and -t options as internal use only.
memset(&aihints, 0, sizeof(aihints));
aihints.ai_socktype = SOCK_STREAM;
aihints.ai_flags = AI_CANONNAME;
- aihints.ai_family = AF_INET;
+ aihints.ai_family = *addrfamilyp;
aierr = getaddrinfo(hname, rport_buf, &aihints, &ap);
if (aierr) {
const char *msg;
for (ap2 = ap; ap; ap = ap->ai_next) {
char hostbuf[NI_MAXHOST];
int oerrno;
-
- s = getport(lportp, addrfamilyp);
- if (s < 0) {
- if (errno == EAGAIN)
- fprintf(stderr, "socket: All ports in use\n");
- else
- perror("kcmd: socket");
- return -1;
- }
- if (connect(s, ap->ai_addr, ap->ai_addrlen) >= 0)
- break;
- (void) close(s);
- if (errno == EADDRINUSE) {
+ int af = ap->ai_family;
+
+ for (;;) {
+ s = getport(lportp, &af);
+ if (s < 0) {
+ if (errno == EAGAIN)
+ fprintf(stderr, "socket: All ports in use\n");
+ else
+ perror("kcmd: socket");
+ return -1;
+ }
+ if (connect(s, ap->ai_addr, ap->ai_addrlen) >= 0)
+ goto connected;
+ (void) close(s);
+ if (errno != EADDRINUSE)
+ break;
if (lportp)
(*lportp)--;
- continue;
}
aierr = getnameinfo(ap->ai_addr, ap->ai_addrlen,
if (ap->ai_next)
fprintf(stderr, "Trying next address...\n");
}
- if (ap == 0)
- return -1;
+ return -1;
+connected:
sin_len = sizeof(struct sockaddr_in);
if (getsockname(s, (struct sockaddr *)laddrp, &sin_len) < 0) {
perror("getsockname");
size_t slen;
int s2 = getport(lportp, addrfamilyp), s3;
+ *fd2p = -1;
if (s2 < 0)
return -1;
listen(s2, 1);
return(-1);
}
}
- if (fd2p)
- *fd2p = -1;
status = setup_secondary_channel(s, fd2p, &lport, &addrfamily, &from,
anyport);
if (status)
realm = krb_realmofhost(host_save);
}
lport--;
- if (fd2p)
- *fd2p = -1;
status = setup_secondary_channel(s, fd2p, &lport, &addrfamily, &from,
anyport);
if (status)