mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-12-01 19:37:45 +08:00
add acl_sane_bind()
This commit is contained in:
parent
b7ecc6b8a9
commit
44ff4a5c91
@ -1,5 +1,8 @@
|
||||
修改历史列表:
|
||||
|
||||
672) 2021.10.12
|
||||
672.1) feature: 增加 acl_sane_bind() 接口,可以适合不同的地址类型。
|
||||
|
||||
671) 2021.3.27
|
||||
671.1) bugfix: 修复了 json 模块的一处问题。
|
||||
|
||||
|
@ -79,10 +79,21 @@ ACL_API ACL_SOCKET acl_inet_accept_ex(ACL_SOCKET listen_fd, char *ipbuf,
|
||||
* @param res {const struct addrinfo*} 域名解析得到的地址信息对象
|
||||
* @param flag {unsigned int} 标志位
|
||||
* @return {ACL_SOCKET} 返回 ACL_SOCKET_INVALID 表示绑定失败
|
||||
*
|
||||
*/
|
||||
ACL_API ACL_SOCKET acl_inet_bind(const struct addrinfo *res, unsigned flag);
|
||||
|
||||
/**
|
||||
* 网络地址绑定函数,适用于 TCP/UDP 套接口
|
||||
* @param addr {const char*} 域名解析得到的地址
|
||||
* @param flag {unsigned int} 标志位
|
||||
* @param socktype {int} 所绑定的 socket 类型:SOCK_STREAM, SOCK_DGRAM
|
||||
* @param family {int*} 如果绑定成功且该地址非空则存放地址类型,类型有:
|
||||
* AF_INET, AF_INET6, AF_UNIX
|
||||
* @return {ACL_SOCKET} 返回 ACL_SOCKET_INVALID 表示绑定失败
|
||||
*/
|
||||
ACL_API ACL_SOCKET acl_sane_bind(const char *addr, unsigned flag,
|
||||
int socktype, int *family);
|
||||
|
||||
#ifdef ACL_UNIX
|
||||
|
||||
/**
|
||||
|
@ -32,10 +32,10 @@
|
||||
|
||||
#endif
|
||||
|
||||
static ACL_SOCKET inet_listen(const char *addr, const struct addrinfo *res,
|
||||
int backlog, unsigned flag)
|
||||
/* acl_inet_listen - create TCP listener */
|
||||
ACL_SOCKET acl_inet_listen(const char *addr, int backlog, unsigned flag)
|
||||
{
|
||||
ACL_SOCKET sock = acl_inet_bind(res, flag);
|
||||
ACL_SOCKET sock = acl_sane_bind(addr, flag, SOCK_STREAM, NULL);
|
||||
|
||||
if (sock == ACL_SOCKET_INVALID) {
|
||||
acl_msg_error("%s(%d), %s: bind %s error %s", __FILE__,
|
||||
@ -48,9 +48,10 @@ static ACL_SOCKET inet_listen(const char *addr, const struct addrinfo *res,
|
||||
int on = 1;
|
||||
int ret = setsockopt(sock, IPPROTO_TCP, TCP_FASTOPEN,
|
||||
(const void *) &on, sizeof(on));
|
||||
if (ret < 0)
|
||||
if (ret < 0) {
|
||||
acl_msg_warn("%s(%d): setsocket(TCP_FASTOPEN): %s",
|
||||
__FUNCTION__, __LINE__, acl_last_serror());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -68,30 +69,6 @@ static ACL_SOCKET inet_listen(const char *addr, const struct addrinfo *res,
|
||||
return sock;
|
||||
}
|
||||
|
||||
/* acl_inet_listen - create TCP listener */
|
||||
|
||||
ACL_SOCKET acl_inet_listen(const char *addr, int backlog, unsigned flag)
|
||||
{
|
||||
struct addrinfo *res0 = acl_host_addrinfo(addr, SOCK_STREAM), *res;
|
||||
ACL_SOCKET sock;
|
||||
|
||||
if (res0 == NULL) {
|
||||
return ACL_SOCKET_INVALID;
|
||||
}
|
||||
|
||||
sock = ACL_SOCKET_INVALID;
|
||||
|
||||
for (res = res0; res != NULL; res = res->ai_next) {
|
||||
sock = inet_listen(addr, res, backlog, flag);
|
||||
if (sock != ACL_SOCKET_INVALID) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
freeaddrinfo(res0);
|
||||
return sock;
|
||||
}
|
||||
|
||||
ACL_SOCKET acl_inet_accept(ACL_SOCKET listen_fd)
|
||||
{
|
||||
return acl_inet_accept_ex(listen_fd, NULL, 0);
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include "stdlib/acl_iostuff.h"
|
||||
#include "net/acl_host_port.h"
|
||||
#include "net/acl_valid_hostname.h"
|
||||
#include "net/acl_sane_inet.h"
|
||||
#include "net/acl_listen.h"
|
||||
#endif
|
||||
|
||||
@ -165,36 +166,40 @@ ACL_SOCKET acl_unix_dgram_bind(const char *addr, unsigned flag)
|
||||
}
|
||||
#endif
|
||||
|
||||
ACL_SOCKET acl_udp_bind(const char *addr, unsigned flag)
|
||||
static ACL_SOCKET bind_addr(const char *addr, unsigned flag,
|
||||
int socktype, int family)
|
||||
{
|
||||
return acl_udp_bind3(addr, flag, NULL);
|
||||
struct addrinfo res;
|
||||
ACL_SOCKADDR in;
|
||||
size_t addrlen;
|
||||
|
||||
addrlen = acl_inet_pton(family, addr, (struct sockaddr*) &in);
|
||||
if (addrlen == 0) {
|
||||
acl_msg_error("%s(%d), %s: acl_inet_pton error, addr=%s",
|
||||
__FILE__, __LINE__, __FUNCTION__, addr);
|
||||
return ACL_SOCKET_INVALID;
|
||||
}
|
||||
|
||||
res.ai_flags = AI_NUMERICHOST;
|
||||
res.ai_family = family;
|
||||
res.ai_socktype = socktype;
|
||||
res.ai_protocol = 0;
|
||||
res.ai_addrlen = addrlen;
|
||||
res.ai_addr = (struct sockaddr*) ∈
|
||||
|
||||
return acl_inet_bind(&res, flag);
|
||||
}
|
||||
|
||||
ACL_SOCKET acl_udp_bind3(const char *addr, unsigned flag, int *family)
|
||||
static ACL_SOCKET find_bind(const char *addr, unsigned flag,
|
||||
int socktype, int *family)
|
||||
{
|
||||
struct addrinfo *res0, *res;
|
||||
ACL_SOCKET fd;
|
||||
|
||||
if (family) {
|
||||
*family = 0;
|
||||
}
|
||||
|
||||
#ifdef ACL_UNIX
|
||||
if (!acl_valid_ipv4_hostaddr(addr, 0)
|
||||
&& !acl_valid_ipv6_hostaddr(addr, 0)) {
|
||||
|
||||
fd = acl_unix_dgram_bind(addr, flag);
|
||||
if (fd >= 0 && family) {
|
||||
*family = AF_UNIX;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
#endif
|
||||
|
||||
res0 = acl_host_addrinfo(addr, SOCK_DGRAM);
|
||||
res0 = acl_host_addrinfo(addr, socktype);
|
||||
if (res0 == NULL) {
|
||||
acl_msg_error("%s(%d): host_addrinfo NULL, addr=%s",
|
||||
__FILE__, __LINE__, addr);
|
||||
acl_msg_error("%s(%d), %s: host_addrinfo NULL, addr=%s",
|
||||
__FILE__, __LINE__, __FUNCTION__, addr);
|
||||
return ACL_SOCKET_INVALID;
|
||||
}
|
||||
|
||||
@ -211,6 +216,27 @@ ACL_SOCKET acl_udp_bind3(const char *addr, unsigned flag, int *family)
|
||||
}
|
||||
|
||||
freeaddrinfo(res0);
|
||||
return fd;
|
||||
}
|
||||
|
||||
ACL_SOCKET acl_sane_bind(const char *addr, unsigned flag,
|
||||
int socktype, int *family)
|
||||
{
|
||||
ACL_SOCKET fd;
|
||||
|
||||
if (acl_valid_ipv4_hostaddr(addr, 0)) {
|
||||
if (family) {
|
||||
*family = AF_INET;
|
||||
}
|
||||
fd = bind_addr(addr, flag, socktype, AF_INET);
|
||||
} else if (acl_valid_ipv6_hostaddr(addr, 0)) {
|
||||
if (family) {
|
||||
*family = AF_INET6;
|
||||
}
|
||||
fd = bind_addr(addr, flag, socktype, AF_INET6);
|
||||
} else {
|
||||
fd = find_bind(addr, flag, socktype, family);
|
||||
}
|
||||
|
||||
if (fd == ACL_SOCKET_INVALID) {
|
||||
acl_msg_error("%s(%d): bind %s error %s",
|
||||
@ -220,6 +246,31 @@ ACL_SOCKET acl_udp_bind3(const char *addr, unsigned flag, int *family)
|
||||
|
||||
acl_non_blocking(fd, flag & ACL_INET_FLAG_NBLOCK ?
|
||||
ACL_NON_BLOCKING : ACL_BLOCKING);
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
ACL_SOCKET acl_udp_bind3(const char *addr, unsigned flag, int *family)
|
||||
{
|
||||
ACL_SOCKET fd;
|
||||
|
||||
if (family) {
|
||||
*family = 0;
|
||||
}
|
||||
|
||||
#ifdef ACL_UNIX
|
||||
if (acl_valid_unix(addr)) {
|
||||
fd = acl_unix_dgram_bind(addr, flag);
|
||||
if (fd >= 0 && family) {
|
||||
*family = AF_UNIX;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
#endif
|
||||
|
||||
return acl_sane_bind(addr, flag, SOCK_DGRAM, family);
|
||||
}
|
||||
|
||||
ACL_SOCKET acl_udp_bind(const char *addr, unsigned flag)
|
||||
{
|
||||
return acl_udp_bind3(addr, flag, NULL);
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
|
||||
116) 2021.10.10-12
|
||||
116.1) bugfix: 修复采用 iocp 引擎时的问题
|
||||
|
||||
115) 2020.1.25
|
||||
115.1) bugfix: hook/epoll.c,在 read_callback/write_callback 中如果 nready
|
||||
超过了 maxevents,则会引起崩溃,原因是用户在调用 epoll_wait 时可能给定的值
|
||||
|
Loading…
Reference in New Issue
Block a user