code adjust for pull request #6
parent
0e1fcdafce
commit
5618afabbb
|
|
@ -350,22 +350,28 @@ int sf_socket_create_server(SFListener *listener,
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
// 如果bind_addr未设置
|
if (af == AF_UNSPEC) {
|
||||||
if (strlen(bind_addr) == 0) {
|
if (bind_addr == NULL || *bind_addr == '\0') {
|
||||||
// 如果当前服务不存在IPv4地址,但是存在IPv6地址,则自动绑定IPv6地址
|
// 如果当前服务不存在IPv4地址,但是存在IPv6地址,则自动绑定IPv6地址
|
||||||
if(!checkHostHasIPv4Addr() && checkHostHasIPv6Addr()){
|
if (!checkHostHasIPv4Addr() && checkHostHasIPv6Addr()) {
|
||||||
listener->sock = socketServerIPv6(bind_addr, port, &result);
|
listener->sock = socketServerIPv6(bind_addr,
|
||||||
}else{
|
listener->port, &result);
|
||||||
listener->sock = socketServer(bind_addr, port, &result);
|
} else {
|
||||||
|
listener->sock = socketServer(bind_addr,
|
||||||
|
listener->port, &result);
|
||||||
|
}
|
||||||
|
} else if (is_ipv6_addr(bind_addr)) {
|
||||||
|
listener->sock = socketServerIPv6(bind_addr,
|
||||||
|
listener->port, &result);
|
||||||
|
} else {
|
||||||
|
listener->sock = socketServer(bind_addr,
|
||||||
|
listener->port, &result);
|
||||||
}
|
}
|
||||||
} else if (is_ipv6_addr(bind_addr)) // 通过判断IP地址是IPv4或者IPv6,根据结果进行初始化
|
} else {
|
||||||
{
|
listener->sock = socketServer2(af, bind_addr,
|
||||||
listener->sock = socketServerIPv6(bind_addr, port, &result);
|
listener->port, &result);
|
||||||
}else{
|
|
||||||
listener->sock = socketServer(bind_addr, port, &result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// listener->sock = socketServer2(af, bind_addr, listener->port, &result);
|
|
||||||
if (listener->sock < 0) {
|
if (listener->sock < 0) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -380,7 +386,7 @@ int sf_socket_create_server(SFListener *listener,
|
||||||
int sf_socket_server_ex(SFContext *sf_context)
|
int sf_socket_server_ex(SFContext *sf_context)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
int af = AF_INET;
|
int af = AF_UNSPEC;
|
||||||
bool dual_ports;
|
bool dual_ports;
|
||||||
const char *bind_addr;
|
const char *bind_addr;
|
||||||
SFNetworkHandler *handler;
|
SFNetworkHandler *handler;
|
||||||
|
|
@ -858,13 +864,16 @@ void sf_set_sig_quit_handler(sf_sig_quit_handler quit_handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断当前服务器是否存在IPv4地址
|
// 判断当前服务器是否存在IPv4地址
|
||||||
bool checkHostHasIPv4Addr(){
|
bool checkHostHasIPv4Addr()
|
||||||
|
{
|
||||||
struct ifaddrs *ifaddr, *ifa;
|
struct ifaddrs *ifaddr, *ifa;
|
||||||
|
bool hasIPv4;
|
||||||
|
|
||||||
if (getifaddrs(&ifaddr) == -1) {
|
if (getifaddrs(&ifaddr) == -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasIPv4 = false;
|
hasIPv4 = false;
|
||||||
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
||||||
if (ifa->ifa_addr == NULL) {
|
if (ifa->ifa_addr == NULL) {
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -879,18 +888,20 @@ bool checkHostHasIPv4Addr(){
|
||||||
}
|
}
|
||||||
|
|
||||||
freeifaddrs(ifaddr);
|
freeifaddrs(ifaddr);
|
||||||
|
|
||||||
return hasIPv4;
|
return hasIPv4;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断当前服务器是否存在IPv6地址
|
// 判断当前服务器是否存在IPv6地址
|
||||||
bool checkHostHasIPv6Addr(){
|
bool checkHostHasIPv6Addr()
|
||||||
|
{
|
||||||
struct ifaddrs *ifaddr, *ifa;
|
struct ifaddrs *ifaddr, *ifa;
|
||||||
|
bool hasIPv6;
|
||||||
|
|
||||||
if (getifaddrs(&ifaddr) == -1) {
|
if (getifaddrs(&ifaddr) == -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasIPv6 = false;
|
hasIPv6 = false;
|
||||||
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
||||||
if (ifa->ifa_addr == NULL) {
|
if (ifa->ifa_addr == NULL) {
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -899,12 +910,11 @@ bool checkHostHasIPv6Addr(){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (ifa->ifa_addr->sa_family == AF_INET6) {
|
if (ifa->ifa_addr->sa_family == AF_INET6) {
|
||||||
hasIPv6 = true;
|
hasIPv6 = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
freeifaddrs(ifaddr);
|
freeifaddrs(ifaddr);
|
||||||
|
|
||||||
return hasIPv6;
|
return hasIPv6;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue