Skip to content

Commit dc11fc2

Browse files
committed
Merge branch 'rs/daemon-fixes'
"git daemon" (with NO_IPV6 build configuration) used to incorrectly use the hostname even when gethostbyname() reported that the given hostname is not found. * rs/daemon-fixes: daemon: remove write-only variable maxfd daemon: fix error message after bind() daemon: handle gethostbyname() error
2 parents e2ebb5c + 107efbe commit dc11fc2

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

daemon.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -553,20 +553,21 @@ static void parse_host_arg(char *extra_args, int buflen)
553553
static char addrbuf[HOST_NAME_MAX + 1];
554554

555555
hent = gethostbyname(hostname);
556+
if (hent) {
557+
ap = hent->h_addr_list;
558+
memset(&sa, 0, sizeof sa);
559+
sa.sin_family = hent->h_addrtype;
560+
sa.sin_port = htons(0);
561+
memcpy(&sa.sin_addr, *ap, hent->h_length);
562+
563+
inet_ntop(hent->h_addrtype, &sa.sin_addr,
564+
addrbuf, sizeof(addrbuf));
556565

557-
ap = hent->h_addr_list;
558-
memset(&sa, 0, sizeof sa);
559-
sa.sin_family = hent->h_addrtype;
560-
sa.sin_port = htons(0);
561-
memcpy(&sa.sin_addr, *ap, hent->h_length);
562-
563-
inet_ntop(hent->h_addrtype, &sa.sin_addr,
564-
addrbuf, sizeof(addrbuf));
565-
566-
free(canon_hostname);
567-
canon_hostname = xstrdup(hent->h_name);
568-
free(ip_address);
569-
ip_address = xstrdup(addrbuf);
566+
free(canon_hostname);
567+
canon_hostname = xstrdup(hent->h_name);
568+
free(ip_address);
569+
ip_address = xstrdup(addrbuf);
570+
}
570571
#endif
571572
}
572573
}
@@ -814,7 +815,6 @@ static const char *ip2str(int family, struct sockaddr *sin, socklen_t len)
814815
static int setup_named_sock(char *listen_addr, int listen_port, struct socketlist *socklist)
815816
{
816817
int socknum = 0;
817-
int maxfd = -1;
818818
char pbuf[NI_MAXSERV];
819819
struct addrinfo hints, *ai0, *ai;
820820
int gai;
@@ -882,9 +882,6 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
882882
ALLOC_GROW(socklist->list, socklist->nr + 1, socklist->alloc);
883883
socklist->list[socklist->nr++] = sockfd;
884884
socknum++;
885-
886-
if (maxfd < sockfd)
887-
maxfd = sockfd;
888885
}
889886

890887
freeaddrinfo(ai0);
@@ -923,7 +920,7 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
923920
}
924921

925922
if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
926-
logerror("Could not listen to %s: %s",
923+
logerror("Could not bind to %s: %s",
927924
ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
928925
strerror(errno));
929926
close(sockfd);

0 commit comments

Comments
 (0)