mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-09 00:00:04 +01:00
listen: ip bind: force ipv4 if given ipv4 numeric iface
This commit is contained in:
parent
0a71e0716c
commit
9cd1f4d78b
3 changed files with 40 additions and 18 deletions
|
@ -260,11 +260,15 @@ lws_socket_bind(struct lws_vhost *vhost, struct lws *wsi,
|
|||
__func__, iface);
|
||||
return m;
|
||||
}
|
||||
serv_addr6.sin6_scope_id = (unsigned int)lws_get_addr_scope(iface);
|
||||
}
|
||||
if (serv_addr6.sin6_family == AF_INET6)
|
||||
serv_addr6.sin6_scope_id = (unsigned int)lws_get_addr_scope(iface);
|
||||
} else
|
||||
serv_addr6.sin6_family = AF_INET6;
|
||||
|
||||
serv_addr6.sin6_family = AF_INET6;
|
||||
serv_addr6.sin6_port = (uint16_t)htons((uint16_t)port);
|
||||
if (serv_addr6.sin6_family == AF_INET6)
|
||||
serv_addr6.sin6_port = (uint16_t)htons((uint16_t)port);
|
||||
else
|
||||
((struct sockaddr_in *)v)->sin_port = (uint16_t)htons((uint16_t)port);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
|
|
|
@ -335,16 +335,9 @@ lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
|
|||
|
||||
freeifaddrs(ifr);
|
||||
|
||||
if (rc) {
|
||||
/* check if bind to IP address */
|
||||
#ifdef LWS_WITH_IPV6
|
||||
if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
|
||||
rc = LWS_ITOSA_USABLE;
|
||||
else
|
||||
#endif
|
||||
if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
|
||||
rc = LWS_ITOSA_USABLE;
|
||||
}
|
||||
if (rc &&
|
||||
!lws_sa46_parse_numeric_address(ifname, (lws_sockaddr46 *)addr))
|
||||
rc = LWS_ITOSA_USABLE;
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
|
|
@ -55,9 +55,12 @@ _lws_vhost_init_server(const struct lws_context_creation_info *info,
|
|||
struct lws_context_per_thread *pt;
|
||||
int n, opt = 1, limit = 1;
|
||||
lws_sockfd_type sockfd;
|
||||
int m = 0, is;
|
||||
#if defined(LWS_WITH_IPV6)
|
||||
int af = 0;
|
||||
#endif
|
||||
struct lws_vhost *vh;
|
||||
struct lws *wsi;
|
||||
int m = 0, is;
|
||||
|
||||
(void)method_names;
|
||||
(void)opt;
|
||||
|
@ -183,14 +186,36 @@ done_list:
|
|||
#ifdef LWS_WITH_UNIX_SOCK
|
||||
if (LWS_UNIX_SOCK_ENABLED(vhost))
|
||||
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
else
|
||||
else {
|
||||
#endif
|
||||
#ifdef LWS_WITH_IPV6
|
||||
if (LWS_IPV6_ENABLED(vhost))
|
||||
|
||||
#if defined(LWS_WITH_IPV6)
|
||||
/*
|
||||
* We have to assess iface if it's around, to choose
|
||||
* ahead of time to create a socket with the right AF
|
||||
*/
|
||||
|
||||
if (vhost->iface) {
|
||||
uint8_t buf[16];
|
||||
int q;
|
||||
|
||||
q = lws_parse_numeric_address(vhost->iface, buf, sizeof(buf));
|
||||
|
||||
if (q == 4)
|
||||
af = AF_INET;
|
||||
if (q == 16)
|
||||
af = AF_INET6;
|
||||
}
|
||||
|
||||
if (LWS_IPV6_ENABLED(vhost) && af != AF_INET)
|
||||
sockfd = socket(AF_INET6, SOCK_STREAM, 0);
|
||||
else
|
||||
#endif
|
||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
#ifdef LWS_WITH_UNIX_SOCK
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (sockfd == LWS_SOCK_INVALID) {
|
||||
|
|
Loading…
Add table
Reference in a new issue