Skip to content

Commit

Permalink
test/common/child: remove spawn_server_addr and lookup_hostname f…
Browse files Browse the repository at this point in the history
…rom test

harness

They are never used.
  • Loading branch information
a3a3el authored and notroj committed Oct 3, 2023
1 parent 7093008 commit b5448cb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 107 deletions.
109 changes: 14 additions & 95 deletions test/common/child.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ struct server_addr {
char name[NI_MAXHOST];
};

struct server_addr lh, hn;
struct server_addr lh;

const char *want_header = NULL;
got_header_fn got_header = NULL;
char *local_hostname = NULL;

/* If we have pipe(), then use a pipe between the parent and child to
* know when the child is ready to accept incoming connections.
Expand Down Expand Up @@ -187,87 +186,23 @@ get_lh_inet_addr(void)
return ne_iaddr_make(type, raw);
}

int lookup_hostname(void)
static int do_listen(int port)
{
char buf[NI_MAXHOST];
#ifdef USE_GETADDRINFO
struct addrinfo hints = {}, *results, *rp;
int rv;
#else
struct hostent *ent;
#endif

local_hostname = NULL;
ONV(gethostname(buf, sizeof buf) < 0,
("gethostname failed: %s", strerror(errno)));

#ifdef USE_GETADDRINFO

#ifdef USE_GAI_ADDRCONFIG
hints.ai_flags = AI_ADDRCONFIG;
#endif
hints.ai_flags |= AI_CANONNAME;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

rv = getaddrinfo(buf, NULL, &hints, &results);
ONV(rv != 0, ("getaddrinfo failed: %s", gai_strerror(rv)));

for (rp = results; rp != NULL; rp = rp->ai_next) {
if (rp == results)
snprintf(hn.name, sizeof hn.name, "%s", rp->ai_canonname);

if (rp->ai_family != AF_INET &&
rp->ai_family != AF_INET6)
continue;

hn.family = rp->ai_family;
memcpy(&hn.sockaddr, rp->ai_addr, rp->ai_addrlen);
break;
}

freeaddrinfo(results);

#else

ent = gethostbyname(buf);
#ifdef HAVE_HSTRERROR
ONV(ent == NULL,
("could not resolve `%s': %s", buf, hstrerror(h_errno)));
#else
ONV(ent == NULL, ("could not resolve `%s'", buf));
#endif

hn.family = AF_INET;
strcpy(hn.name, ent->h_name);
hn.sockaddr.in.sin_family = hn.family;
memcpy(&hn.sockaddr.in.sin_addr, ent->h_addr,
sizeof hn.sockaddr.in.sin_addr);

#endif

local_hostname = hn.name;

return OK;
}

static int do_listen(struct server_addr *addr, int port)
{
int ls = socket(addr->family, SOCK_STREAM, 0);
int ls = socket(lh.family, SOCK_STREAM, 0);
struct sockaddr *saddr;
socklen_t saddrlen;
int val = 1;

setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (void *)&val, sizeof(int));

if (addr->family == AF_INET) {
addr->sockaddr.in.sin_port = htons(port);
saddr = (struct sockaddr *) &addr->sockaddr.in;
saddrlen = sizeof addr->sockaddr.in;
if (lh.family == AF_INET) {
lh.sockaddr.in.sin_port = htons(port);
saddr = (struct sockaddr *) &lh.sockaddr.in;
saddrlen = sizeof lh.sockaddr.in;
} else {
addr->sockaddr.in6.sin6_port = htons(port);
saddr = (struct sockaddr *) &addr->sockaddr.in6;
saddrlen = sizeof addr->sockaddr.in6;
lh.sockaddr.in6.sin6_port = htons(port);
saddr = (struct sockaddr *) &lh.sockaddr.in6;
saddrlen = sizeof lh.sockaddr.in6;
}

if (bind(ls, saddr, saddrlen)) {
Expand Down Expand Up @@ -328,15 +263,15 @@ static int close_socket(ne_socket *sock)
}

/* This runs as the child process. */
static int server_child(int readyfd, struct server_addr *addr, int port,
static int server_child(int readyfd, int port,
server_fn callback, void *userdata)
{
ne_socket *s = ne_sock_create();
int ret, listener;

in_child();

listener = do_listen(addr, port);
listener = do_listen(port);
if (listener < 0)
return FAIL;

Expand All @@ -355,24 +290,8 @@ static int server_child(int readyfd, struct server_addr *addr, int port,
}

int spawn_server(int port, server_fn fn, void *ud)
{
return spawn_server_addr(1, port, fn, ud);
}

int spawn_server_addr(int bind_local, int port, server_fn fn, void *ud)
{
int fds[2];
struct server_addr *addr;

if (bind_local) {
addr = &lh;
} else {
if (hn.family == AF_UNSPEC) {
hn.family = AF_INET;
hn.sockaddr.in.sin_family = hn.family;
}
addr = &hn;
}

#ifdef USE_PIPE
if (pipe(fds)) {
Expand All @@ -392,7 +311,7 @@ int spawn_server_addr(int bind_local, int port, server_fn fn, void *ud)
/* this is the child. */
int ret;

ret = server_child(fds[1], addr, port, fn, ud);
ret = server_child(fds[1], port, fn, ud);

#ifdef USE_PIPE
close(fds[0]);
Expand Down Expand Up @@ -451,7 +370,7 @@ int new_spawn_server2(int count, server_fn fn, void *userdata,
if (lh.family == AF_UNSPEC)
lookup_localhost();

ls = do_listen(&lh, 0);
ls = do_listen(0);
ONN("could not bind/listen fd for server", ls < 0);

ONV(getsockname(ls, (struct sockaddr *) &sa, &salen) != 0,
Expand Down
12 changes: 0 additions & 12 deletions test/common/child.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@
* named test. */
int lookup_localhost(void);

/* Test which looks up real local hostname. */
int lookup_hostname(void);

/* set to local hostname if lookup_hostname succeeds. */
extern char *local_hostname;

int get_lh_family(void);
const char *get_lh_addr(void);
ne_inet_addr *get_lh_inet_addr(void);
Expand All @@ -57,12 +51,6 @@ typedef int (*server_fn)(ne_socket *sock, void *userdata);
*/
int spawn_server(int port, server_fn fn, void *userdata);

/* Like spawn_server; if bind_local is non-zero, binds server to
* localhost, otherwise, binds server to real local hostname. (must
* have called lookup_localhost or lookup_hostname as appropriate
* beforehand). */
int spawn_server_addr(int bind_local, int port, server_fn fn, void *userdata);

/* Forks a server child process running 'fn(userdata)' on an
* unspecified port. Sets test suite error on failure; on success,
* sets *port to bound port number. */
Expand Down

0 comments on commit b5448cb

Please sign in to comment.