diff --git a/lib_acl/include/net/acl_dns.h b/lib_acl/include/net/acl_dns.h index 7c2ae0c67..7733a394a 100644 --- a/lib_acl/include/net/acl_dns.h +++ b/lib_acl/include/net/acl_dns.h @@ -39,7 +39,7 @@ extern "C" { typedef struct ACL_DNS_ADDR { char ip[64]; /* DNS 服务器地址 */ unsigned short port; /* DNS 服务器端口 */ - struct ACL_SOCKADDR addr; /* DNS 地址 */ + ACL_SOCKADDR addr; /* DNS 地址 */ int addr_len; /* addr 大小 */ int mask_length; /* DNS 服务器所在网络的掩码长度(> 0 && < 32) */ struct in_addr in; /* addr 的网段地址 */ diff --git a/lib_acl/include/net/acl_netdb.h b/lib_acl/include/net/acl_netdb.h index 456538dfb..b793fdba9 100644 --- a/lib_acl/include/net/acl_netdb.h +++ b/lib_acl/include/net/acl_netdb.h @@ -16,7 +16,7 @@ typedef struct ACL_HOSTNAME ACL_HOST_INFO; typedef struct ACL_HOSTNAME { char ip[64]; /**< the ip addr of the HOST */ - struct ACL_SOCKADDR saddr; /**< ip addr in ACL_SOCKADDR */ + ACL_SOCKADDR saddr; /**< ip addr in ACL_SOCKADDR */ unsigned int ttl; /**< the HOST's ip timeout(second) */ int hport; unsigned int nrefer; /**< refer number to this HOST */ @@ -58,9 +58,9 @@ ACL_API const ACL_HOSTNAME *acl_netdb_index(const ACL_DNS_DB *h_dns_db, int i); * 从结果集中取得某个下标位置的主机IP地址 * @param h_dns_db {const ACL_DNS_DB*} DNS结果集 * @param i {int} 下标位置 - * @return {const struct ACL_SOCKADDR*} IP地址结构, NULL表示失败 + * @return {const ACL_SOCKADDR*} IP地址结构, NULL表示失败 */ -ACL_API const struct ACL_SOCKADDR *acl_netdb_index_saddr(ACL_DNS_DB *h_dns_db, int i); +ACL_API const ACL_SOCKADDR *acl_netdb_index_saddr(ACL_DNS_DB *h_dns_db, int i); /** * 将结果集中的对应某个下标的主机地址引用增加 diff --git a/lib_acl/include/net/acl_sane_socket.h b/lib_acl/include/net/acl_sane_socket.h index 05fe42359..a0b6fbd17 100644 --- a/lib_acl/include/net/acl_sane_socket.h +++ b/lib_acl/include/net/acl_sane_socket.h @@ -14,18 +14,16 @@ extern "C" { #include #endif -typedef struct ACL_SOCKADDR { - union { - struct sockaddr_storage ss; +typedef union { + struct sockaddr_storage ss; #ifdef AF_INET6 - struct sockaddr_in6 in6; + struct sockaddr_in6 in6; #endif - struct sockaddr_in in; + struct sockaddr_in in; #ifdef ACL_UNIX - struct sockaddr_un un; + struct sockaddr_un un; #endif - struct sockaddr sa; - } sa; + struct sockaddr sa; } ACL_SOCKADDR; /** diff --git a/lib_acl/samples/dns/main.cpp b/lib_acl/samples/dns/main.cpp index e29483098..fc90593f3 100644 --- a/lib_acl/samples/dns/main.cpp +++ b/lib_acl/samples/dns/main.cpp @@ -30,7 +30,7 @@ static int dns_lookup(const char *domain, const char *dns_ip, char buf[32]; info = (ACL_HOST_INFO*) iter.data; - in.s_addr = info->saddr.sa.in.sin_addr.s_addr; + in.s_addr = info->saddr.in.sin_addr.s_addr; acl_mask_addr((unsigned char*) &in.s_addr, sizeof(in.s_addr), 24); acl_inet_ntoa(in, buf, sizeof(buf)); diff --git a/lib_acl/samples/dns/valgrind.sh b/lib_acl/samples/dns/valgrind.sh old mode 100644 new mode 100755 index 0b0c42954..92e84f6cf --- a/lib_acl/samples/dns/valgrind.sh +++ b/lib_acl/samples/dns/valgrind.sh @@ -1,3 +1,2 @@ #!/bin/sh - -valgrind --tool=memcheck --leak-check=yes -v ./dns -h 8.8.8.8 -p 53 -d www.sina.com +valgrind --tool=memcheck --leak-check=yes --show-reachable=yes -v ./dns -h 8.8.8.8 -p 53 -d www.baidu.com diff --git a/lib_acl/samples/resolve/main.cpp b/lib_acl/samples/resolve/main.cpp index b0d64e58e..c59b3b1a3 100644 --- a/lib_acl/samples/resolve/main.cpp +++ b/lib_acl/samples/resolve/main.cpp @@ -1,23 +1,117 @@ #include "lib_acl.h" #include #include +//#include +//extern int h_errno; + +#if !defined(MACOSX) && !defined(ACL_SUNOS5) && !defined(MINGW) +static ACL_DNS_DB *resolve_name(const char *name) +{ + struct hostent *h_addrp = NULL; + struct hostent h_buf; + int err = 0, n; + char buf[4096]; + char **pptr; + ACL_DNS_DB *res; + ACL_HOSTNAME *h_host; + + printf("%s: gethostbyname name: %s\n", __FUNCTION__, name); + + n = gethostbyname_r(name, &h_buf, buf, sizeof(buf), &h_addrp, &err); + if (n) { + printf("%s: gethostbyname %s error\n", __FUNCTION__, name); + return NULL; + } + + res = acl_netdb_new(name); + for (pptr = h_addrp->h_addr_list; *pptr != NULL; pptr++) { + h_host = (ACL_HOSTNAME*) acl_mycalloc(1, sizeof(ACL_HOSTNAME)); + + memset(&h_host->saddr, 0, sizeof(h_host->saddr)); + if ((int) sizeof(h_host->saddr.in.sin_addr) > h_addrp->h_length) + n = h_addrp->h_length; + else + n = (int) sizeof(h_host->saddr.in.sin_addr); + + memcpy(&h_host->saddr.in.sin_addr, *pptr, n); + acl_inet_ntoa(h_host->saddr.in.sin_addr, + h_host->ip, sizeof(h_host->ip)); + + (void) acl_array_append(res->h_db, h_host); + res->size++; + } + + return res; +} +#endif + +static ACL_DNS_DB *resolve_addr(const char *name) +{ + ACL_DNS_DB *res; + struct hostent *h_addrp = NULL; + struct in_addr addr; + ACL_HOSTNAME *h_host; + char **pptr; + int n; + + memset(&addr, 0, sizeof(addr)); + addr.s_addr = inet_addr(name); + + printf("%s: gethostbyaddr addr: %s\n", __FUNCTION__, name); + + h_addrp = gethostbyaddr((char*) &addr, sizeof(addr), AF_INET); + if (!h_addrp) { + printf("%s: gethostbyaddr error=%s\n", + __FUNCTION__, hstrerror(h_errno)); + return NULL; + } + + printf("h_name: %s\n", h_addrp->h_name); + + res = acl_netdb_new(name); + + for (pptr = h_addrp->h_addr_list; *pptr != NULL; pptr++) { + h_host = (ACL_HOSTNAME*) acl_mycalloc(1, sizeof(ACL_HOSTNAME)); + + memset(&h_host->saddr, 0, sizeof(h_host->saddr)); + if ((int) sizeof(h_host->saddr.in.sin_addr) > h_addrp->h_length) + n = h_addrp->h_length; + else + n = (int) sizeof(h_host->saddr.in.sin_addr); + + memcpy(&h_host->saddr.in.sin_addr, *pptr, n); + acl_inet_ntoa(h_host->saddr.in.sin_addr, + h_host->ip, sizeof(h_host->ip)); + + (void) acl_array_append(res->h_db, h_host); + + res->size++; + } + + return res; +} + +static void show(ACL_DNS_DB *res) +{ + ACL_ITER iter; + + if (!res) + return; + + acl_foreach(iter, res) { + const ACL_HOST_INFO *info; + + info = (const ACL_HOST_INFO*) iter.data; + printf("\tip=%s; port=%d\n", info->ip, info->hport); + } +} static void test(const char *name, int use_acl) { ACL_DNS_DB *res = NULL; - ACL_HOSTNAME *h_host; -#if !defined(MACOSX) && !defined(ACL_SUNOS5) && !defined(MINGW) - struct hostent h_buf; - int errnum = 0; - char buf[4096]; -#endif - struct hostent *h_addrp = NULL; - char **pptr; - int n; - ACL_ITER iter; - struct in_addr addr; if (use_acl) { + printf("acl_gethostbyname name: %s\n", name); res = acl_gethostbyname(name, NULL); if (res == NULL) { printf("acl_gethostbyname %s error\n", name); @@ -26,94 +120,43 @@ static void test(const char *name, int use_acl) } #if !defined(MACOSX) && !defined(ACL_SUNOS5) && !defined(MINGW) else { - res = acl_netdb_new(name); - n = gethostbyname_r(name, &h_buf, buf, sizeof(buf), &h_addrp, &errnum); - if (n) { - printf("gethostbyname %s error\n", name); - acl_netdb_free(res); - return; - } - - for (pptr = h_addrp->h_addr_list; *pptr != NULL; pptr++) { - h_host = (ACL_HOSTNAME*) acl_mycalloc(1, sizeof(ACL_HOSTNAME)); - - memset(&h_host->saddr, 0, sizeof(h_host->saddr)); - n = (int) sizeof(h_host->saddr.sa.in.sin_addr) > h_addrp->h_length - ? h_addrp->h_length : (int) sizeof(h_host->saddr.sa.in.sin_addr); - memcpy(&h_host->saddr.sa.in.sin_addr, *pptr, n); - acl_inet_ntoa(h_host->saddr.sa.in.sin_addr, h_host->ip, sizeof(h_host->ip)); - - (void) acl_array_append(res->h_db, h_host); - - res->size++; - } + res = resolve_name(name); } #endif - printf("gethostbyname name: %s\n", name); - if (res != NULL) { - acl_foreach(iter, res) { - const ACL_HOST_INFO *info; - - info = (const ACL_HOST_INFO*) iter.data; - printf("\tip=%s; port=%d\n", info->ip, info->hport); - } + if (res) { + show(res); acl_netdb_free(res); } - res = acl_netdb_new(name); - memset(&addr, 0, sizeof(addr)); - addr.s_addr = inet_addr(name); + printf("-------------------------------------------------------\r\n"); - printf("gethostbyaddr addr: %s\n", name); - - h_addrp = gethostbyaddr((char*) &addr, sizeof(addr), AF_INET); - if (h_addrp) { - printf("h_name: %s\n", h_addrp->h_name); - for (pptr = h_addrp->h_addr_list; *pptr != NULL; pptr++) { - h_host = (ACL_HOSTNAME*) acl_mycalloc(1, sizeof(ACL_HOSTNAME)); - - memset(&h_host->saddr, 0, sizeof(h_host->saddr)); - n = (int) sizeof(h_host->saddr.sa.in.sin_addr) > h_addrp->h_length - ? h_addrp->h_length : (int) sizeof(h_host->saddr.sa.in.sin_addr); - memcpy(&h_host->saddr.sa.in.sin_addr, *pptr, n); - acl_inet_ntoa(h_host->saddr.sa.in.sin_addr, h_host->ip, sizeof(h_host->ip)); - - (void) acl_array_append(res->h_db, h_host); - - res->size++; - } - } else { - printf("error: %s\n", acl_last_serror()); + res = resolve_addr(name); + if (res) { + show(res); + acl_netdb_free(res); } - - acl_foreach(iter, res) { - const ACL_HOST_INFO *info; - - info = (const ACL_HOST_INFO*) iter.data; - printf("\tip=%s; port=%d\n", info->ip, info->hport); - } - acl_netdb_free(res); } static void usage(const char *procname) { - printf("usage: %s -h[help] -a addr -r[use_acl_resolve]\n", procname); + printf("usage: %s -h[help] -n name -r[use_acl_resolve]\n", procname); } int main(int argc, char *argv[]) { int ch, use_acl = 0; - char addr[256]; + char name[256]; - addr[0] = 0; - while ((ch = getopt(argc, argv, "ha:r")) > 0) { + name[0] = 0; + + while ((ch = getopt(argc, argv, "hn:r")) > 0) { switch (ch) { case 'h': usage(argv[0]); return (0); - case 'a': - snprintf(addr, sizeof(addr), "%s", optarg); + case 'n': + snprintf(name, sizeof(name), "%s", optarg); break; case 'r': use_acl = 1; @@ -123,8 +166,24 @@ int main(int argc, char *argv[]) } } - if (addr[0]) - test(addr, use_acl); + acl_msg_stdout_enable(1); + + const char *addr = "192.168.1.1"; + printf("check ipv4 %s %d\r\n", addr, acl_valid_ipv4_hostaddr(addr, 1)); + printf("check ipv6 %s %d\r\n", addr, acl_valid_ipv6_hostaddr(addr, 1)); + addr = "192.168.1.1:80"; + printf("check ipv4 %s %d\r\n", addr, acl_valid_ipv4_hostaddr(addr, 1)); + addr = "192.168.1.1#80"; + printf("check ipv4 %s %d\r\n", addr, acl_valid_ipv4_hostaddr(addr, 1)); + + addr = "fe80::ca1f:66ff:fef6:c496"; + printf("check ipv6 %s %d\r\n", addr, acl_valid_ipv6_hostaddr(addr, 1)); + + addr = "fe80::ca1f:66ff:fef6:c496#80"; + printf("check ipv6 %s %d\r\n", addr, acl_valid_ipv6_hostaddr(addr, 1)); + + if (name[0]) + test(name, use_acl); else usage(argv[0]); return (0); diff --git a/lib_acl/samples/resolve/valgrind.sh b/lib_acl/samples/resolve/valgrind.sh new file mode 100755 index 000000000..a40e41322 --- /dev/null +++ b/lib_acl/samples/resolve/valgrind.sh @@ -0,0 +1,2 @@ +#!/bin/sh +valgrind --tool=memcheck --leak-check=yes --show-reachable=yes -v ./resolve -n www.baidu.com diff --git a/lib_acl/src/master/template/acl_udp_server.c b/lib_acl/src/master/template/acl_udp_server.c index c5a4f7d54..8e8490dfb 100644 --- a/lib_acl/src/master/template/acl_udp_server.c +++ b/lib_acl/src/master/template/acl_udp_server.c @@ -738,38 +738,32 @@ static UDP_SERVER *servers_open(int event_mode, int nthreads, int sock_count) static UDP_SERVER *servers_create(const char *service, int nthreads) { - UDP_SERVER *servers = NULL; - - if (strcasecmp(acl_var_udp_event_mode, "poll") == 0) + if (strcasecmp(acl_var_udp_event_mode, "poll") == 0) { __event_mode = ACL_EVENT_POLL; - else if (strcasecmp(acl_var_udp_event_mode, "kernel") == 0) + } else if (strcasecmp(acl_var_udp_event_mode, "kernel") == 0) { __event_mode = ACL_EVENT_KERNEL; - else + } else { __event_mode = ACL_EVENT_SELECT; + } __main_event = acl_event_new(__event_mode, 0, acl_var_udp_delay_sec, acl_var_udp_delay_usec); -#ifdef ACL_UNIX - if (__daemon_mode) { + if (!__daemon_mode) { + return servers_binding(service, __event_mode, nthreads); + } + +#ifdef ACL_UNIX # ifdef SO_REUSEPORT - servers = servers_binding(service, __event_mode, nthreads); + return servers_binding(service, __event_mode, nthreads); # else - /* __socket_count from command argv */ - servers = servers_open(__event_mode, nthreads, __socket_count); + /* __socket_count from command argv */ + return servers_open(__event_mode, nthreads, __socket_count); # endif - } else - servers = servers_binding(service, __event_mode, nthreads); - -#else /* !ACL_UNIX */ - if (__daemon_mode) { - acl_msg_fatal("%s(%d): not support daemon mode!", - __FILE__, __LINE__); - } else - servers = servers_binding(service, __event_mode, nthreads); -#endif /* ACL_UNIX */ - - return servers; +#else + acl_msg_fatal("%s(%d): not support daemon mode!", __FILE__, __LINE__); + return NULL; /* just avoid compiling warning */ +#endif } static void *thread_main(void *ctx) diff --git a/lib_acl/src/net/acl_host_port.c b/lib_acl/src/net/acl_host_port.c index 810a30ab1..71fa80477 100644 --- a/lib_acl/src/net/acl_host_port.c +++ b/lib_acl/src/net/acl_host_port.c @@ -54,8 +54,8 @@ const char *acl_host_port(char *buf, char **host, char *def_host, *host = *buf ? buf : def_host; *port = *cp ? cp : def_service; } else { - *host = def_host ? def_host : (*buf ? buf : 0); - *port = def_service ? def_service : (*buf ? buf : 0); + *host = *buf ? buf : (def_host ? def_host : NULL); + *port = def_service ? def_service : NULL; } if (*host == 0) @@ -141,8 +141,8 @@ struct addrinfo *acl_host_addrinfo(const char *addr, int type) hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG; #endif if ((err = getaddrinfo(host, port, &hints, &res0))) { - acl_msg_error("%s(%d): getaddrinfo error %s, peer=%s", - __FILE__, __LINE__, gai_strerror(err), host); + acl_msg_error("%s(%d): getaddrinfo error %s, host=%s, addr=%s", + __FILE__, __LINE__, gai_strerror(err), host, addr); acl_myfree(buf); return NULL; } diff --git a/lib_acl/src/net/acl_ifconf.c b/lib_acl/src/net/acl_ifconf.c index 55a23bbf1..331dd0ade 100644 --- a/lib_acl/src/net/acl_ifconf.c +++ b/lib_acl/src/net/acl_ifconf.c @@ -118,17 +118,18 @@ ACL_IFCONF *acl_get_ifaddrs() ACL_IFADDR *ifa = (ACL_IFADDR *) &ifconf->addrs[j]; size_t size; - if (saddr->sa.sa.sa_family == AF_INET) { - if (inet_ntop(AF_INET, &saddr->sa.in.sin_addr, + if (saddr->sa.sa_family == AF_INET) { + if (inet_ntop(AF_INET, &saddr->in.sin_addr, ifa->ip, sizeof(ifa->ip)) == NULL) { continue; } size = sizeof(struct sockaddr_in); #ifdef AF_INET6 - } else if (saddr->sa.sa.sa_family == AF_INET6) { - if (inet_ntop(AF_INET6, &saddr->sa.in6.sin6_addr, + } else if (saddr->sa.sa_family == AF_INET6) { + if (inet_ntop(AF_INET6, &saddr->in6.sin6_addr, ifa->ip, sizeof(ifa->ip)) == NULL) { + continue; } size = sizeof(struct sockaddr_in6); @@ -171,7 +172,7 @@ ACL_IFCONF *acl_get_ifaddrs() typedef HRESULT STDAPICALLTYPE PGAINFO(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen); -ACL_IFCONF *acl_get_ifaddrs() +ACL_IFCONF *acl_get_ifaddrs(void) { const char *myname = "acl_get_ifaddrs"; IP_ADAPTER_INFO info_temp, *infos, *info; @@ -226,7 +227,7 @@ ACL_IFCONF *acl_get_ifaddrs() ifconf->addrs[j].desc = acl_mystrdup(info->Description); snprintf(ifconf->addrs[j].ip, sizeof(ifconf->addrs[j].ip), "%s", info->IpAddressList.IpAddress.String); - ifconf->addrs[j].saddr.sa.in.sin_addr.s_addr + ifconf->addrs[j].saddr.in.sin_addr.s_addr = inet_addr(ifconf->addrs[j].ip); j++; if (j == ifconf->length) { @@ -300,7 +301,7 @@ ACL_IFCONF *acl_get_ifaddrs() ifconf->addrs[j].desc = acl_mystrdup(info->Description); snprintf(ifconf->addrs[j].ip, sizeof(ifconf->addrs[j].ip), "%s", info->IpAddressList.IpAddress.String); - ifconf->addrs[j].saddr.sa.in.sin_addr.s_addr + ifconf->addrs[j].saddr.in.sin_addr.s_addr = inet_addr(ifconf->addrs[j].ip); j++; if (j == ifconf->length) { diff --git a/lib_acl/src/net/acl_sane_socket.c b/lib_acl/src/net/acl_sane_socket.c index c291e871b..298eaaf22 100644 --- a/lib_acl/src/net/acl_sane_socket.c +++ b/lib_acl/src/net/acl_sane_socket.c @@ -25,7 +25,7 @@ int acl_getpeername(ACL_SOCKET fd, char *buf, size_t size) { - struct ACL_SOCKADDR addr; + ACL_SOCKADDR addr; struct sockaddr *sa = (struct sockaddr*) &addr; socklen_t len = sizeof(addr); char ip[LEN]; @@ -47,20 +47,20 @@ int acl_getpeername(ACL_SOCKET fd, char *buf, size_t size) if (getsockname(fd, sa, &len) == -1) return -1; - snprintf(buf, size, "%s", addr.sa.un.sun_path); + snprintf(buf, size, "%s", addr.un.sun_path); return 0; } else #endif if (sa->sa_family == AF_INET) { - if (!inet_ntop(sa->sa_family, &addr.sa.in.sin_addr, ip, LEN)) + if (!inet_ntop(sa->sa_family, &addr.in.sin_addr, ip, LEN)) return -1; - port = ntohs(addr.sa.in.sin_port); + port = ntohs(addr.in.sin_port); } #ifdef AF_INET6 else if (sa->sa_family == AF_INET6) { - if (!inet_ntop(sa->sa_family, &addr.sa.in6.sin6_addr, ip, LEN)) + if (!inet_ntop(sa->sa_family, &addr.in6.sin6_addr, ip, LEN)) return -1; - port = ntohs(addr.sa.in6.sin6_port); + port = ntohs(addr.in6.sin6_port); } else return -1; #else @@ -73,7 +73,7 @@ int acl_getpeername(ACL_SOCKET fd, char *buf, size_t size) int acl_getsockname(ACL_SOCKET fd, char *buf, size_t size) { - struct ACL_SOCKADDR addr; + ACL_SOCKADDR addr; struct sockaddr *sa = (struct sockaddr*) &addr; socklen_t len = sizeof(addr); char ip[LEN]; @@ -89,20 +89,20 @@ int acl_getsockname(ACL_SOCKET fd, char *buf, size_t size) #ifndef ACL_WINDOWS if (sa->sa_family == AF_UNIX) { - snprintf(buf, size, "%s", addr.sa.un.sun_path); + snprintf(buf, size, "%s", addr.un.sun_path); return 0; } else #endif if (sa->sa_family == AF_INET) { - if (!inet_ntop(sa->sa_family, &addr.sa.in.sin_addr, ip, LEN)) + if (!inet_ntop(sa->sa_family, &addr.in.sin_addr, ip, LEN)) return -1; - port = ntohs(addr.sa.in.sin_port); + port = ntohs(addr.in.sin_port); } #ifdef AF_INET6 else if (sa->sa_family == AF_INET6) { - if (!inet_ntop(sa->sa_family, &addr.sa.in6.sin6_addr, ip, LEN)) + if (!inet_ntop(sa->sa_family, &addr.in6.sin6_addr, ip, LEN)) return -1; - port = ntohs(addr.sa.in6.sin6_port); + port = ntohs(addr.in6.sin6_port); } else return -1; #else @@ -116,7 +116,7 @@ int acl_getsockname(ACL_SOCKET fd, char *buf, size_t size) int acl_getsocktype(ACL_SOCKET fd) { - struct ACL_SOCKADDR addr; + ACL_SOCKADDR addr; struct sockaddr *sa = (struct sockaddr*) &addr; socklen_t len = sizeof(addr); diff --git a/lib_acl/src/net/acl_valid_hostname.c b/lib_acl/src/net/acl_valid_hostname.c index 319c54463..66e7df427 100644 --- a/lib_acl/src/net/acl_valid_hostname.c +++ b/lib_acl/src/net/acl_valid_hostname.c @@ -106,42 +106,41 @@ int acl_valid_hostaddr(const char *addr, int gripe) { const char *myname = "acl_valid_hostaddr"; - /* - * Trivial cases first. - */ + /* Trivial cases first. */ if (*addr == 0) { if (gripe) acl_msg_warn("%s: empty address", myname); return 0; } - /* - * Protocol-dependent processing next. - */ - if (strchr(addr, ':') != 0) - return acl_valid_ipv6_hostaddr(addr, gripe); - else - return acl_valid_ipv4_hostaddr(addr, gripe); + /* Protocol-dependent processing next. */ + return acl_valid_ipv4_hostaddr(addr, gripe) + || acl_valid_ipv6_hostaddr(addr, gripe); } /* acl_valid_ipv4_hostaddr - test dotted quad string for correctness */ -int acl_valid_ipv4_hostaddr(const char *addr, int gripe) +int acl_valid_ipv4_hostaddr(const char *addr_in, int gripe) { - char *myname = "acl_valid_ipv4_hostaddr"; + char *myname = "acl_valid_ipv4_hostaddr"; + int in_byte = 0, byte_count = 0, byte_val = 0, ch; + char addr[128], *ptr; const char *cp; - int in_byte = 0; - int byte_count = 0; - int byte_val = 0; - int ch; #define BYTES_NEEDED 4 + ACL_SAFE_STRNCPY(addr, addr_in, sizeof(addr)); + if ((ptr = strrchr(addr, ':')) != NULL) { + *ptr = 0; + } else if ((ptr = strrchr(addr, '#')) != NULL) { + *ptr = 0; + } + /* * Scary code to avoid sscanf() overflow nasties. * - * This routine is called by valid_ipv6_hostaddr(). It must not call that - * routine, to avoid deadly recursion. + * This routine is called by valid_ipv6_hostaddr(). It must not call + * that routine, to avoid deadly recursion. */ for (cp = addr; (ch = *(unsigned const char *) cp) != 0; cp++) { if (ACL_ISDIGIT(ch)) { @@ -176,8 +175,8 @@ int acl_valid_ipv4_hostaddr(const char *addr, int gripe) in_byte = 0; } else { if (gripe) - acl_msg_warn("%s: invalid character %d(decimal): %.100s", - myname, ch, addr); + acl_msg_warn("%s: invalid character %d" + "(decimal): %.100s", myname, ch, addr); return 0; } } @@ -194,13 +193,18 @@ int acl_valid_ipv4_hostaddr(const char *addr, int gripe) /* acl_valid_ipv6_hostaddr - validate IPv6 address syntax */ -int acl_valid_ipv6_hostaddr(const char *addr, int gripe) +int acl_valid_ipv6_hostaddr(const char *addr_in, int gripe) { const char *myname = "acl_valid_ipv6_hostaddr"; - int null_field = 0; - int field = 0; - const unsigned char *cp = (const unsigned char *) addr; - int len = 0; + int null_field = 0, field = 0, len = 0; + char addr[128], *ptr; + const unsigned char *cp; + + ACL_SAFE_STRNCPY(addr, addr_in, sizeof(addr)); + if ((ptr = strrchr(addr, '#')) != NULL) { + *ptr = 0; + } + cp = (const unsigned char *) addr; /* * FIX 200501 The IPv6 patch validated syntax with getaddrinfo(), but I @@ -212,8 +216,8 @@ int acl_valid_ipv6_hostaddr(const char *addr, int gripe) * We require eight-field hex addresses of the form 0:1:2:3:4:5:6:7, * 0:1:2:3:4:5:6a.6b.7c.7d, or some :: compressed version of the same. * - * Note: the character position is advanced inside the loop. I have added - * comments to show why we can't get stuck. + * Note: the character position is advanced inside the loop. I have + * added comments to show why we can't get stuck. */ for (;;) { switch (*cp) { diff --git a/lib_acl/src/net/connect/acl_inet_connect.c b/lib_acl/src/net/connect/acl_inet_connect.c index 8cbf6a7d2..38165da3d 100644 --- a/lib_acl/src/net/connect/acl_inet_connect.c +++ b/lib_acl/src/net/connect/acl_inet_connect.c @@ -71,8 +71,7 @@ static ACL_SOCKET inet_connect_one(const struct addrinfo *peer, acl_tcp_set_rcvbuf(sock, ACL_SOCKET_RBUF_SIZE); acl_tcp_set_sndbuf(sock, ACL_SOCKET_WBUF_SIZE); - if (local0 != NULL && bind_local(sock, peer->ai_family, local0) < 0) - { + if (local0 != NULL && bind_local(sock, peer->ai_family, local0) < 0) { acl_msg_error("%s(%d): bind local error %s, fd=%d", myname, __LINE__, acl_last_serror(), sock); acl_socket_close(sock); @@ -112,8 +111,7 @@ static ACL_SOCKET inet_connect_one(const struct addrinfo *peer, acl_non_blocking(sock, blocking); #ifdef ACL_WINDOWS if (acl_sane_connect(sock, peer->ai_addr, - (socklen_t) peer->ai_addrlen) < 0) - { + (socklen_t) peer->ai_addrlen) < 0) { #else if (acl_sane_connect(sock, peer->ai_addr, peer->ai_addrlen) < 0) { #endif @@ -123,8 +121,7 @@ static ACL_SOCKET inet_connect_one(const struct addrinfo *peer, errnum = acl_last_error(); len = sizeof(err); if (getsockopt(sock, SOL_SOCKET, SO_ERROR, - (char *) &err, &len) < 0) - { + (char *) &err, &len) < 0) { #ifdef SUNOS5 /* * Solaris 2.4's socket emulation doesn't allow you diff --git a/lib_acl/src/net/dns/acl_dns.c b/lib_acl/src/net/dns/acl_dns.c index b6b181667..613b2641a 100644 --- a/lib_acl/src/net/dns/acl_dns.c +++ b/lib_acl/src/net/dns/acl_dns.c @@ -125,20 +125,20 @@ static ACL_DNS_DB *build_dns_db(const rfc1035_message *res, int count, #if defined(ACL_UNIX) /* 这样直接赋值要比用 memcpy 快些 */ # ifdef MINGW - saddr->sa.in.sin_addr.s_addr = + saddr->in.sin_addr.s_addr = *((unsigned int*) res->answer[i].rdata); # else - saddr->sa.in.sin_addr.s_addr = + saddr->in.sin_addr.s_addr = *((in_addr_t*) res->answer[i].rdata); # endif #elif defined(ACL_WINDOWS) - saddr->sa.in.sin_addr.s_addr = + saddr->in.sin_addr.s_addr = *((unsigned int*) res->answer[i].rdata); #endif /* 目前该模块仅支持 IPV4 */ - saddr->sa.sa.sa_family = AF_INET; + saddr->sa.sa_family = AF_INET; - if (inet_ntop(AF_INET, &saddr->sa.in.sin_addr, + if (inet_ntop(AF_INET, &saddr->in.sin_addr, phost->ip, sizeof(phost->ip))) { continue; @@ -211,14 +211,14 @@ static int dns_lookup_callback(ACL_ASTREAM *astream acl_unused, void *ctx, acl_msg_fatal("%s(%d): addr null for %d", myname, __LINE__, i); - if (dns->addr_from.addr.sa.in.sin_addr.s_addr - != addr->addr.sa.in.sin_addr.s_addr) { + if (dns->addr_from.addr.in.sin_addr.s_addr + != addr->addr.in.sin_addr.s_addr) { char from[64], to[64]; - inet_ntop(AF_INET, &dns->addr_from.addr.sa.in.sin_addr, + inet_ntop(AF_INET, &dns->addr_from.addr.in.sin_addr, from, sizeof(from)); - inet_ntop(AF_INET, &addr->addr.sa.in.sin_addr, + inet_ntop(AF_INET, &addr->addr.in.sin_addr, to, sizeof(to)); acl_msg_warn("%s(%d): from(%s) != to(%s)", myname, __LINE__, from, to); @@ -241,15 +241,15 @@ static int dns_lookup_callback(ACL_ASTREAM *astream acl_unused, void *ctx, acl_msg_fatal("%s(%d): addr null for %d", myname, __LINE__, i); - in.s_addr = dns->addr_from.addr.sa.in.sin_addr.s_addr; + in.s_addr = dns->addr_from.addr.in.sin_addr.s_addr; acl_mask_addr((unsigned char*) &in.s_addr, sizeof(in.s_addr), addr->mask_length); if (in.s_addr != addr->in.s_addr) { char from[64], to[64]; - inet_ntop(AF_INET, &dns->addr_from.addr.sa.in.sin_addr, + inet_ntop(AF_INET, &dns->addr_from.addr.in.sin_addr, from, sizeof(from)); - inet_ntop(AF_INET, &addr->addr.sa.in.sin_addr, + inet_ntop(AF_INET, &addr->addr.in.sin_addr, to, sizeof(to)); acl_msg_warn("%s(%d): from(%s) != to(%s)", myname, __LINE__, from, to); @@ -527,14 +527,14 @@ void acl_dns_add_dns(ACL_DNS *dns, const char *dns_ip, addr->port = dns_port; memset(&addr->addr, 0, sizeof(addr->addr)); - addr->addr.sa.in.sin_port = htons(dns_port); - addr->addr.sa.in.sin_addr.s_addr = inet_addr(dns_ip); - addr->addr.sa.sa.sa_family = AF_INET; - addr->addr_len = sizeof(struct sockaddr_in); + addr->addr.in.sin_port = htons(dns_port); + addr->addr.in.sin_addr.s_addr = inet_addr(dns_ip); + addr->addr.sa.sa_family = AF_INET; + addr->addr_len = sizeof(struct sockaddr_in); - addr->in.s_addr = addr->addr.sa.in.sin_addr.s_addr; - acl_mask_addr((unsigned char*) &addr->addr.sa.in.sin_addr.s_addr, - sizeof(addr->addr.sa.in.sin_addr.s_addr), mask_length); + addr->in.s_addr = addr->addr.in.sin_addr.s_addr; + acl_mask_addr((unsigned char*) &addr->addr.in.sin_addr.s_addr, + sizeof(addr->addr.in.sin_addr.s_addr), mask_length); /* 将该DNS地址添加进数组中 */ @@ -560,8 +560,8 @@ void acl_dns_add_host(ACL_DNS *dns, const char *domain, const char *ip_list) ACL_HOSTNAME *phost = acl_mycalloc(1, sizeof(ACL_HOSTNAME)); ACL_SAFE_STRNCPY(phost->ip, ip, sizeof(phost->ip)); - phost->saddr.sa.sa.sa_family = AF_INET; - phost->saddr.sa.in.sin_addr.s_addr = inet_addr(ip); + phost->saddr.sa.sa_family = AF_INET; + phost->saddr.in.sin_addr.s_addr = inet_addr(ip); (void) acl_array_append(dns_db->h_db, phost); } diff --git a/lib_acl/src/net/dns/acl_netdb.c b/lib_acl/src/net/dns/acl_netdb.c index 9b94df35f..46a91cdd5 100644 --- a/lib_acl/src/net/dns/acl_netdb.c +++ b/lib_acl/src/net/dns/acl_netdb.c @@ -53,7 +53,7 @@ const ACL_HOSTNAME *acl_netdb_index(const ACL_DNS_DB *db, int n) return h_hostname; } -const struct ACL_SOCKADDR *acl_netdb_index_saddr(ACL_DNS_DB *db, int n) +const ACL_SOCKADDR *acl_netdb_index_saddr(ACL_DNS_DB *db, int n) { ACL_HOSTNAME *h_hostname; @@ -211,13 +211,13 @@ void acl_netdb_addip(ACL_DNS_DB *db, const char *ip) static int ip2addr(const char *ip, unsigned short port, ACL_SOCKADDR *saddr) { - memset(saddr, 0, sizeof(struct ACL_SOCKADDR)); + memset(saddr, 0, sizeof(ACL_SOCKADDR)); if (acl_valid_ipv4_hostaddr(ip, 0)) { - saddr->sa.sa.sa_family = AF_INET; - saddr->sa.in.sin_port = htons(port); + saddr->sa.sa_family = AF_INET; + saddr->in.sin_port = htons(port); - if (inet_pton(AF_INET, ip, &saddr->sa.in.sin_addr) == 1) { + if (inet_pton(AF_INET, ip, &saddr->in.sin_addr) == 1) { return 1; } acl_msg_error("%s(%d): invalid ip=%s", @@ -225,9 +225,9 @@ static int ip2addr(const char *ip, unsigned short port, ACL_SOCKADDR *saddr) } #ifdef AF_INET6 else if (acl_valid_ipv6_hostaddr(ip, 0)) { - saddr->sa.sa.sa_family = AF_INET6; - saddr->sa.in6.sin6_port = htons(port); - if (inet_pton(AF_INET6, ip, &saddr->sa.in6.sin6_addr) == 1) { + saddr->sa.sa_family = AF_INET6; + saddr->in6.sin6_port = htons(port); + if (inet_pton(AF_INET6, ip, &saddr->in6.sin6_addr) == 1) { return 1; } acl_msg_error("%s(%d): invalid ip=%s", @@ -313,11 +313,6 @@ ACL_DNS_DB *acl_gethostbyname(const char *name, int *h_error) if (db) return db; - res0 = acl_host_addrinfo(name, SOCK_DGRAM); - if (res0 == NULL) { - return NULL; - } - db = acl_netdb_new(name); if (ip2addr(name, 0, &saddr)) { @@ -330,30 +325,34 @@ ACL_DNS_DB *acl_gethostbyname(const char *name, int *h_error) return db; } + res0 = acl_host_addrinfo(name, SOCK_DGRAM); + if (res0 == NULL) + return NULL; + for (res = res0; res != NULL; res = res->ai_next) { - struct ACL_SOCKADDR *sa = (struct ACL_SOCKADDR *) res->ai_addr; + ACL_SOCKADDR *sa = (ACL_SOCKADDR *) res->ai_addr; ACL_HOSTNAME *h_host; char ip[64]; memset(&saddr, 0, sizeof(saddr)); - saddr.sa.sa.sa_family = res->ai_family; + saddr.sa.sa_family = res->ai_family; if (res->ai_family == AF_INET) { - if (inet_ntop(res->ai_family, &sa->sa.in.sin_addr, + if (inet_ntop(res->ai_family, &sa->in.sin_addr, ip, sizeof(ip)) == NULL) { continue; } - memcpy(&saddr.sa.in.sin_addr, &sa->sa.in.sin_addr, - sizeof(saddr.sa.in.sin_addr)); + memcpy(&saddr.in.sin_addr, &sa->in.sin_addr, + sizeof(saddr.in.sin_addr)); #ifdef AF_INET6 } else if (res->ai_family == AF_INET6) { - if (inet_ntop(res->ai_family, &sa->sa.in6.sin6_addr, + if (inet_ntop(res->ai_family, &sa->in6.sin6_addr, ip, sizeof(ip)) == NULL) { continue; } - memcpy(&saddr.sa.in6.sin6_addr, &sa->sa.in6.sin6_addr, - sizeof(saddr.sa.in6.sin6_addr)); + memcpy(&saddr.in6.sin6_addr, &sa->in6.sin6_addr, + sizeof(saddr.in6.sin6_addr)); #endif } else { continue; @@ -370,9 +369,8 @@ ACL_DNS_DB *acl_gethostbyname(const char *name, int *h_error) freeaddrinfo(res0); - if (acl_netdb_size(db) > 0) { + if (acl_netdb_size(db) > 0) return db; - } acl_netdb_free(db); return NULL; diff --git a/lib_acl/src/net/dns/acl_res.c b/lib_acl/src/net/dns/acl_res.c index 71c331a71..65279221a 100644 --- a/lib_acl/src/net/dns/acl_res.c +++ b/lib_acl/src/net/dns/acl_res.c @@ -216,9 +216,9 @@ ACL_DNS_DB *acl_res_lookup(ACL_RES *res, const char *domain) if (answers->answer[i].type == RFC1035_TYPE_A) { phost = acl_mycalloc(1, sizeof(ACL_HOSTNAME)); - memcpy(&phost->saddr.sa.in.sin_addr, + memcpy(&phost->saddr.in.sin_addr, answers->answer[i].rdata, 4); - inet_ntop(AF_INET, &phost->saddr.sa.in.sin_addr, + inet_ntop(AF_INET, &phost->saddr.in.sin_addr, phost->ip, sizeof(phost->ip)); phost->ttl = answers->answer[i].ttl; diff --git a/lib_acl/src/net/listen/acl_inet_listen.c b/lib_acl/src/net/listen/acl_inet_listen.c index 2525b4b36..e3a50d994 100644 --- a/lib_acl/src/net/listen/acl_inet_listen.c +++ b/lib_acl/src/net/listen/acl_inet_listen.c @@ -105,7 +105,7 @@ ACL_SOCKET acl_inet_accept_ex(ACL_SOCKET listen_fd, char *ipbuf, size_t size) /* when client_addr not null and protocol is AF_INET, acl_sane_accept * will set nodelay on the accepted socket, 2008.9.4, zsx */ - fd = acl_sane_accept(listen_fd, (struct sockaddr *)&sa, &len); + fd = acl_sane_accept(listen_fd, (struct sockaddr*) &sa, &len); if (fd == ACL_SOCKET_INVALID) return fd; diff --git a/lib_acl/src/net/listen/acl_sane_accept.c b/lib_acl/src/net/listen/acl_sane_accept.c index 525f6241c..1bc4ba84e 100644 --- a/lib_acl/src/net/listen/acl_sane_accept.c +++ b/lib_acl/src/net/listen/acl_sane_accept.c @@ -104,7 +104,7 @@ ACL_SOCKET acl_sane_accept(ACL_SOCKET sock, struct sockaddr * sa, socklen_t *len else if (sa && sa->sa_family == AF_INET) #endif { - int on = 1; + int on = 1; /* default set client to nodelay --- add by zsx, 2008.9.4 */ acl_tcp_nodelay(fd, on); @@ -120,19 +120,7 @@ ACL_SOCKET acl_sane_accept(ACL_SOCKET sock, struct sockaddr * sa, socklen_t *len ACL_SOCKET acl_accept(ACL_SOCKET sock, char *buf, size_t size, int* sock_type) { - struct { - union { - struct sockaddr_storage ss; -#ifdef AF_INET6 - struct sockaddr_in6 in6; -#endif - struct sockaddr_in in; -#ifndef ACL_WINDOWS - struct sockaddr_un un; -#endif - struct sockaddr sa; - } sa; - } addr; + ACL_SOCKADDR addr; socklen_t len = sizeof(addr); struct sockaddr *sa = (struct sockaddr*) &addr; ACL_SOCKET fd; @@ -154,47 +142,56 @@ ACL_SOCKET acl_accept(ACL_SOCKET sock, char *buf, size_t size, int* sock_type) if (sa->sa_family == AF_INET) { #ifdef ACL_WINDOWS - if (!inet_ntop(sa->sa_family, &addr.sa.in.sin_addr, buf, size)) + if (!inet_ntop(sa->sa_family, &addr.in.sin_addr, buf, size)) #else - if (!inet_ntop(sa->sa_family, &addr.sa.in.sin_addr, - buf, (socklen_t)size)) + if (!inet_ntop(sa->sa_family, &addr.in.sin_addr, + buf, (socklen_t) size)) #endif + { + acl_msg_error("%s(%d): inet_ntop error=%s", + __FUNCTION__, __LINE__, acl_last_serror()); return fd; + } n = strlen(buf); if (n >= size) return fd; snprintf(buf + n, size - n, ":%u", - (unsigned short) ntohs(addr.sa.in.sin_port)); + (unsigned short) ntohs(addr.in.sin_port)); buf[size - 1] = 0; return fd; } #ifdef AF_INET6 else if (sa->sa_family == AF_INET6) { #ifdef ACL_WINDOWS - if (!inet_ntop(sa->sa_family, &addr.sa.in6.sin6_addr, buf, size)) + if (!inet_ntop(sa->sa_family, &addr.in6.sin6_addr, buf, size)) #else - if (!inet_ntop(sa->sa_family, &addr.sa.in6.sin6_addr, - buf, (socklen_t)size)) + if (!inet_ntop(sa->sa_family, &addr.in6.sin6_addr, + buf, (socklen_t) size)) #endif + { + acl_msg_error("%s(%d): inet_ntop error=%s", + __FUNCTION__, __LINE__, acl_last_serror()); return fd; + } n = strlen(buf); if (n >= size) return fd; snprintf(buf + n, size - n, ":%u", - (unsigned short) ntohs(addr.sa.in6.sin6_port)); + (unsigned short) ntohs(addr.in6.sin6_port)); buf[size - 1] = 0; return fd; } #endif -#ifndef ACL_WINDOWS +#ifdef ACL_UNIX else if (sa->sa_family == AF_UNIX) { if (acl_getsockname(fd, buf, size) < 0) buf[0] = 0; return fd; } #endif - else + else { return fd; + } } diff --git a/lib_acl/src/stdlib/acl_vstream.c b/lib_acl/src/stdlib/acl_vstream.c index 1201d24f2..030edc938 100644 --- a/lib_acl/src/stdlib/acl_vstream.c +++ b/lib_acl/src/stdlib/acl_vstream.c @@ -2997,13 +2997,11 @@ static struct sockaddr *set_sock_addr(const char *addr, size_t *sa_size) snprintf(buf, sizeof(buf), "%s", (addr)); - ptr = strchr(buf, ':'); - if (ptr == NULL) - port = -1; - else { + if ((ptr = strrchr(buf, '#')) || (ptr = strrchr(buf, ':'))) { *ptr++ = 0; port = atoi(ptr); - } + } else + port = -1; if (acl_is_ipv4(buf)) { struct sockaddr_in *in; @@ -3014,8 +3012,7 @@ static struct sockaddr *set_sock_addr(const char *addr, size_t *sa_size) in = (struct sockaddr_in *) acl_mycalloc(1, sizeof(struct sockaddr_in)); in->sin_family = AF_INET; - in->sin_port = htons(port); - in->sin_addr.s_addr = inet_addr(buf); + in->sin_port = htons(port); (void) inet_pton(AF_INET, buf, &in->sin_addr); *sa_size = sizeof(struct sockaddr_in); return (struct sockaddr *) in; @@ -3030,7 +3027,7 @@ static struct sockaddr *set_sock_addr(const char *addr, size_t *sa_size) in = (struct sockaddr_in6 *) acl_mycalloc(1, sizeof(struct sockaddr_in6)); in->sin6_family = AF_INET6; - in->sin6_port = htons(port); + in->sin6_port = htons(port); (void) inet_pton(AF_INET6, buf, &in->sin6_addr); *sa_size = sizeof(struct sockaddr_in6); return (struct sockaddr *) in; @@ -3087,7 +3084,7 @@ void acl_vstream_set_local(ACL_VSTREAM *fp, const char *addr) if (fp->sa_local != NULL) acl_myfree(fp->sa_local); - fp->sa_local = set_sock_addr(addr, &fp->sa_local_size); + fp->sa_local = set_sock_addr(addr, &fp->sa_local_size); fp->sa_local_len = fp->sa_local_size; } diff --git a/lib_acl/src/stdlib/string/acl_split_at.c b/lib_acl/src/stdlib/string/acl_split_at.c index 66874da6c..4283ce032 100644 --- a/lib_acl/src/stdlib/string/acl_split_at.c +++ b/lib_acl/src/stdlib/string/acl_split_at.c @@ -19,22 +19,22 @@ /* acl_split_at - break string at first delimiter, return remainder */ -char *acl_split_at(char *string, int delimiter) +char *acl_split_at(char *s, int delimiter) { char *cp; - if ((cp = strchr(string, delimiter)) != 0) + if ((cp = strchr(s, delimiter)) != 0) *cp++ = 0; - return (cp); + return cp; } /* acl_split_at_right - break string at last delimiter, return remainder */ -char *acl_split_at_right(char *string, int delimiter) +char *acl_split_at_right(char *s, int delimiter) { char *cp; - if ((cp = strrchr(string, delimiter)) != 0) + if ((cp = strrchr(s, delimiter)) != 0) *cp++ = 0; - return (cp); + return cp; }