Skip to content

Commit

Permalink
Added support for resolving the wildcard listening address
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Mar 17, 2018
1 parent 5dc2f54 commit ba8d470
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/b6b_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,25 @@ static struct addrinfo *b6b_socket_resolve(struct b6b_interp *interp,
struct b6b_obj *service,
const int socktype)
{
struct b6b_socket_gai_data data = {.hints = {0}, .service = NULL};
struct b6b_socket_gai_data data = {
.hints = {
.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED,
.ai_family = AF_UNSPEC
},
.host = NULL,
.service = NULL
};
const char *s;
int out;

/* both strings must be copied since they may be freed during
* b6b_offload() */
data.host = b6b_strndup(host->s, host->slen);
if (b6b_unlikely(!data.host))
return NULL;
if (host->slen) {
data.host = b6b_strndup(host->s, host->slen);
if (b6b_unlikely(!data.host))
return NULL;
} else
data.hints.ai_flags |= AI_PASSIVE;

if (service) {
data.service = b6b_strndup(service->s, service->slen);
Expand All @@ -304,7 +314,6 @@ static struct addrinfo *b6b_socket_resolve(struct b6b_interp *interp,
}

data.hints.ai_socktype = socktype;
data.hints.ai_family = AF_UNSPEC;
out = b6b_offload(interp, b6b_socket_do_getaddrinfo, &data);

free(data.service);
Expand Down Expand Up @@ -428,7 +437,7 @@ static enum b6b_res b6b_socket_proc_inet_client(struct b6b_interp *interp,
struct addrinfo *res;
const struct b6b_strm_ops *ops = &b6b_tcp_client_ops;

if (!b6b_proc_get_args(interp, args, "osss", NULL, &t, &h, &s))
if (!b6b_proc_get_args(interp, args, "osss", NULL, &t, &h, &s) || !h->slen)
return B6B_ERR;

if (strcmp(t->s, "udp") == 0) {
Expand Down
9 changes: 9 additions & 0 deletions tests/b6b_test_inet_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,14 @@ int main()
39) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$inet.server tcp 127.0.0.1 2924}",
33) == B6B_OK);
assert(b6b_call_copy(&interp,
"{$inet.client tcp {} 2924}",
26) == B6B_ERR);
b6b_interp_destroy(&interp);

return EXIT_SUCCESS;
}
6 changes: 6 additions & 0 deletions tests/b6b_test_inet_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ int main()
33) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$inet.server tcp {} 2924}",
26) == B6B_OK);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$inet.server tcp 127.0.0.1 2924}",
Expand Down

0 comments on commit ba8d470

Please sign in to comment.