Allow servers to bind to any address
This commit is contained in:
parent
d8939635ca
commit
bc4ac0eed7
|
@ -32,20 +32,24 @@ static struct addrinfo * _init_address(char const * name, int domain, int flags)
|
||||||
fprintf(stderr, "DEBUG: %s(\"%s\", %d, %d)\n", __func__, name, domain,
|
fprintf(stderr, "DEBUG: %s(\"%s\", %d, %d)\n", __func__, name, domain,
|
||||||
flags);
|
flags);
|
||||||
#endif
|
#endif
|
||||||
|
/* check the arguments */
|
||||||
|
if(name == NULL || strlen(name) == 0)
|
||||||
|
{
|
||||||
|
error_set_code(1, "%s", "Empty names are not allowed");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
/* guess the port separator */
|
/* guess the port separator */
|
||||||
if((p = strchr(name, ':')) != NULL && strchr(++p, ':') != NULL)
|
if((p = strchr(name, ':')) != NULL && strchr(++p, ':') != NULL)
|
||||||
sep = '.';
|
sep = '.';
|
||||||
/* obtain the name */
|
/* obtain the name */
|
||||||
if(strlen(name) == 0)
|
if((p = strdup(name)) == NULL)
|
||||||
hostname = NULL;
|
|
||||||
else if((hostname = strdup(name)) == NULL)
|
|
||||||
{
|
{
|
||||||
error_set_code(1, "%s", strerror(errno));
|
error_set_code(1, "%s", strerror(errno));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
hostname = p;
|
||||||
/* obtain the port number */
|
/* obtain the port number */
|
||||||
if(hostname == NULL || (servname = strrchr(hostname, sep)) == NULL
|
if((servname = strrchr(hostname, sep)) == NULL || servname[1] == '\0')
|
||||||
|| servname[1] == '\0')
|
|
||||||
{
|
{
|
||||||
l = 0;
|
l = 0;
|
||||||
servname = NULL;
|
servname = NULL;
|
||||||
|
@ -53,6 +57,8 @@ static struct addrinfo * _init_address(char const * name, int domain, int flags)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
*(servname++) = '\0';
|
*(servname++) = '\0';
|
||||||
|
if(hostname[0] == '\0')
|
||||||
|
hostname = NULL;
|
||||||
l = strtol(servname, &q, 10);
|
l = strtol(servname, &q, 10);
|
||||||
if(servname[0] == '\0' || *q != '\0')
|
if(servname[0] == '\0' || *q != '\0')
|
||||||
l = -error_set_code(1, "%s", strerror(EINVAL));
|
l = -error_set_code(1, "%s", strerror(EINVAL));
|
||||||
|
@ -62,9 +68,13 @@ static struct addrinfo * _init_address(char const * name, int domain, int flags)
|
||||||
hints.ai_family = domain;
|
hints.ai_family = domain;
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
hints.ai_flags = flags;
|
hints.ai_flags = flags;
|
||||||
|
#ifdef DEBUG
|
||||||
|
fprintf(stderr, "DEBUG: %s() %ld \"%s\" \"%s\" %u\n", __func__, l,
|
||||||
|
hostname, servname, flags);
|
||||||
|
#endif
|
||||||
if(l >= 0)
|
if(l >= 0)
|
||||||
res = getaddrinfo(hostname, servname, &hints, &ai);
|
res = getaddrinfo(hostname, servname, &hints, &ai);
|
||||||
free(hostname);
|
free(p);
|
||||||
/* check for errors */
|
/* check for errors */
|
||||||
if(res != 0)
|
if(res != 0)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user