From d496fdb287beabfa4267fc82ac18e88d28c4238c Mon Sep 17 00:00:00 2001 From: Ken Raeburn Date: Fri, 8 Mar 2002 22:54:34 +0000 Subject: [PATCH] * kcmd.c (kcmd_connect): 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. (kcmd, k4cmd): Don't set *fd2p before calling setup_secondary_channel. (setup_secondary_channel): If fd2p is non-null, initialize the pointed-to value to -1. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@14255 dc483132-0cff-0310-8789-dd5450dbe970 --- src/appl/bsd/ChangeLog | 10 +++++++--- src/appl/bsd/kcmd.c | 41 ++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/appl/bsd/ChangeLog b/src/appl/bsd/ChangeLog index 8f0cf453a..6b256a061 100644 --- a/src/appl/bsd/ChangeLog +++ b/src/appl/bsd/ChangeLog @@ -4,15 +4,19 @@ (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. diff --git a/src/appl/bsd/kcmd.c b/src/appl/bsd/kcmd.c index ba3bb9f15..f69cf0752 100644 --- a/src/appl/bsd/kcmd.c +++ b/src/appl/bsd/kcmd.c @@ -205,7 +205,7 @@ kcmd_connect (int *sp, int *addrfamilyp, struct sockaddr_in *sockinp, 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; @@ -238,22 +238,24 @@ kcmd_connect (int *sp, int *addrfamilyp, struct sockaddr_in *sockinp, 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, @@ -269,9 +271,9 @@ kcmd_connect (int *sp, int *addrfamilyp, struct sockaddr_in *sockinp, 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"); @@ -297,6 +299,7 @@ setup_secondary_channel (int s, int *fd2p, int *lportp, int *addrfamilyp, size_t slen; int s2 = getport(lportp, addrfamilyp), s3; + *fd2p = -1; if (s2 < 0) return -1; listen(s2, 1); @@ -413,8 +416,6 @@ kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, service, realm, return(-1); } } - if (fd2p) - *fd2p = -1; status = setup_secondary_channel(s, fd2p, &lport, &addrfamily, &from, anyport); if (status) @@ -624,8 +625,6 @@ k4cmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, ticket, service, realm, realm = krb_realmofhost(host_save); } lport--; - if (fd2p) - *fd2p = -1; status = setup_secondary_channel(s, fd2p, &lport, &addrfamily, &from, anyport); if (status) -- 2.26.2