Skip to content

Commit

Permalink
optimize to get dns server address
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwii committed Oct 31, 2021
1 parent e71112c commit b6ed50c
Showing 1 changed file with 165 additions and 149 deletions.
314 changes: 165 additions & 149 deletions src/ascore.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct as_loop_s
fd_set write_set;
fd_set except_set;
#endif
char dns_server_inited;
struct sockaddr_storage dns_server;
};

Expand Down Expand Up @@ -177,6 +178,160 @@ static int __get_socket_error(int fd, char *serrno)
return getsockopt(fd, SOL_SOCKET, SO_ERROR, serrno, &len);
}

static int __dns_server(struct sockaddr *addr)
{
#if defined _WIN32 || defined __CYGWIN__
IP_ADAPTER_ADDRESSES *ad_address = NULL;
IP_ADAPTER_ADDRESSES *cur_address = NULL;
ULONG buffer_len = 16 * 1024 * 1024;
IP_ADAPTER_DNS_SERVER_ADDRESS *dns_server = NULL;
ad_address = (IP_ADAPTER_ADDRESSES *) malloc(buffer_len);
if(ad_address == NULL)
{
LOG_ERR(MSG_NOT_ENOUGH_MEMORY);
abort();
}
if(GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, ad_address, &buffer_len) != 0)
{
LOG_WARN(MSG_DNS_NAMESERVER_NOT_FOUND);
return 1;
}
for(cur_address = ad_address; cur_address != NULL; cur_address = cur_address->Next)
{
for(dns_server = cur_address->FirstDnsServerAddress; dns_server != NULL; dns_server = dns_server->Next)
{
if(dns_server->Address.lpSockaddr->sa_family == AF_INET6)
{
memcpy(addr, dns_server->Address.lpSockaddr, sizeof(struct sockaddr_in6));
((struct sockaddr_in6 *) addr)->sin6_port = htons(53);
}
else if(dns_server->Address.lpSockaddr->sa_family == AF_INET)
{
memcpy(addr, dns_server->Address.lpSockaddr, sizeof(struct sockaddr_in));
((struct sockaddr_in *) addr)->sin_port = htons(53);
}
else
{
continue;
}
return 0;
}
}
LOG_WARN(MSG_DNS_NAMESERVER_NOT_FOUND);
return 1;
#else
int c;
char is_comment = 0;
char key[80];
char value[80];
int ikey = 0;
int ivalue = 0;
int type = 0;
struct sockaddr_in6 *in_addr6 = (struct sockaddr_in6 *) addr;
struct sockaddr_in *in_addr = (struct sockaddr_in *) addr;
FILE *resolv_file = fopen(_PATH_RESCONF, "r");
if(resolv_file == NULL)
{
LOG_WARN(MSG_OPEN_FILE, _PATH_RESCONF);
return 1;
}
while((c = fgetc(resolv_file)) != EOF)
{
switch (c)
{
case '\n':
key[ikey++] = '\0';
value[ivalue++] = '\0';
if(strcmp(key, "nameserver") == 0)
{
int rtn = inet_pton(AF_INET6, value, &in_addr6->sin6_addr);
if(rtn == 1)
{
in_addr6->sin6_family = AF_INET6;
in_addr6->sin6_port = htons(53);
fclose(resolv_file);
return 0;
}
rtn = inet_pton(AF_INET, value, &in_addr->sin_addr);
if(rtn == 1)
{
in_addr->sin_family = AF_INET;
in_addr->sin_port = htons(53);
fclose(resolv_file);
return 0;
}
}
ikey = 0;
ivalue = 0;
type = 0;
is_comment = 0;
break;
case '\r':
case ' ':
case '\t':
case '\f':
case '\v':
if(type == 1)
{
type = 2;
}
if(type == 3)
{
type = 4;
}
break;
case '#':
is_comment = 1;
break;
default:
if(is_comment)
continue;
if(type == 0 || type == 1)
{
if(ikey < 80 - 1)
key[ikey++] = c;
type = 1;
}
if(type == 2 || type == 3)
{
if(ivalue < 80 - 1)
value[ivalue++] = c;
type = 3;
}
break;
}
}
fclose(resolv_file);
if(type >= 3)
{
key[ikey++] = '\0';
value[ivalue++] = '\0';
if(strcmp(key, "nameserver") == 0)
{
int rtn = inet_pton(AF_INET6, value, &in_addr6->sin6_addr);
if(rtn == 1)
{
in_addr6->sin6_family = AF_INET6;
in_addr6->sin6_port = htons(53);
fclose(resolv_file);
return 0;
}
rtn = inet_pton(AF_INET, value, &in_addr->sin_addr);
if(rtn == 1)
{
in_addr->sin_family = AF_INET;
in_addr->sin_port = htons(53);
fclose(resolv_file);
return 0;
}
}
}
LOG_WARN(MSG_DNS_NAMESERVER_NOT_FOUND);
return 1;
#endif

}

int __dns_priority(dns_resr_t *dns_resr, int ipv6_first)
{
if(dns_resr->type == 0x1c)
Expand Down Expand Up @@ -995,155 +1150,7 @@ as_loop_t *as_loop_init()
abort();
}
#endif
#if defined _WIN32 || defined __CYGWIN__
IP_ADAPTER_ADDRESSES *ad_address = NULL;
IP_ADAPTER_ADDRESSES *cur_address = NULL;
ULONG buffer_len = 16 * 1024 * 1024;
IP_ADAPTER_DNS_SERVER_ADDRESS *dns_server = NULL;
ad_address = (IP_ADAPTER_ADDRESSES *) malloc(buffer_len);
if(ad_address == NULL)
{
LOG_ERR(MSG_NOT_ENOUGH_MEMORY);
abort();
}
if(GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, ad_address, &buffer_len) != 0)
{
LOG_ERR(MSG_DNS_NAMESERVER_NOT_FOUND);
abort();
}
for(cur_address = ad_address; cur_address != NULL; cur_address = cur_address->Next)
{
for(dns_server = cur_address->FirstDnsServerAddress; dns_server != NULL; dns_server = dns_server->Next)
{
if(dns_server->Address.lpSockaddr->sa_family == AF_INET6)
{
memcpy(&loop->dns_server, dns_server->Address.lpSockaddr, sizeof(struct sockaddr_in6));
((struct sockaddr_in6 *) &loop->dns_server)->sin6_port = htons(53);
}
else if(dns_server->Address.lpSockaddr->sa_family == AF_INET)
{
memcpy(&loop->dns_server, dns_server->Address.lpSockaddr, sizeof(struct sockaddr_in));
((struct sockaddr_in *) &loop->dns_server)->sin_port = htons(53);
}
else
{
continue;
}
return loop;
}
}
LOG_ERR(MSG_DNS_NAMESERVER_NOT_FOUND);
abort();
#else
int c;
char is_comment = 0;
char key[80];
char value[80];
int ikey = 0;
int ivalue = 0;
int type = 0;
struct sockaddr_in6 *in_addr6 = (struct sockaddr_in6 *) &loop->dns_server;
struct sockaddr_in *in_addr = (struct sockaddr_in *) &loop->dns_server;
FILE *resolv_file = fopen(_PATH_RESCONF, "r");
if(resolv_file == NULL)
{
LOG_ERR(MSG_OPEN_FILE, _PATH_RESCONF);
abort();
}
while((c = fgetc(resolv_file)) != EOF)
{
switch (c)
{
case '\n':
key[ikey++] = '\0';
value[ivalue++] = '\0';
if(strcmp(key, "nameserver") == 0)
{
int rtn = inet_pton(AF_INET6, value, &in_addr6->sin6_addr);
if(rtn == 1)
{
in_addr6->sin6_family = AF_INET6;
in_addr6->sin6_port = htons(53);
fclose(resolv_file);
return loop;
}
rtn = inet_pton(AF_INET, value, &in_addr->sin_addr);
if(rtn == 1)
{
in_addr->sin_family = AF_INET;
in_addr->sin_port = htons(53);
fclose(resolv_file);
return loop;
}
}
ikey = 0;
ivalue = 0;
type = 0;
is_comment = 0;
break;
case '\r':
case ' ':
case '\t':
case '\f':
case '\v':
if(type == 1)
{
type = 2;
}
if(type == 3)
{
type = 4;
}
break;
case '#':
is_comment = 1;
break;
default:
if(is_comment)
continue;
if(type == 0 || type == 1)
{
if(ikey < 80 - 1)
key[ikey++] = c;
type = 1;
}
if(type == 2 || type == 3)
{
if(ivalue < 80 - 1)
value[ivalue++] = c;
type = 3;
}
break;
}
}
fclose(resolv_file);
if(type >= 3)
{
key[ikey++] = '\0';
value[ivalue++] = '\0';
if(strcmp(key, "nameserver") == 0)
{
int rtn = inet_pton(AF_INET6, value, &in_addr6->sin6_addr);
if(rtn == 1)
{
in_addr6->sin6_family = AF_INET6;
in_addr6->sin6_port = htons(53);
fclose(resolv_file);
return loop;
}
rtn = inet_pton(AF_INET, value, &in_addr->sin_addr);
if(rtn == 1)
{
in_addr->sin_family = AF_INET;
in_addr->sin_port = htons(53);
fclose(resolv_file);
return loop;
}
}
}
LOG_ERR(MSG_DNS_NAMESERVER_NOT_FOUND);
abort();
#endif
loop->dns_server_inited = 0;
return loop;
}

Expand Down Expand Up @@ -1648,6 +1655,15 @@ int as_resolver(as_socket_t *sck, __const__ char *host, int ipv6_first, as_resol
in_addr->sin_family = AF_INET;
return cb(sck, 0, (struct sockaddr *) &addr);
}
if(!sck->loop->dns_server_inited)
{
rtn = __dns_server((struct sockaddr *) &sck->loop->dns_server);
if(rtn != 0)
{
return cb(sck, 2, NULL);
}
sck->loop->dns_server_inited = 1;
}
dns_hdr_flag.flag_rd = 1;
dns_data_t *dns_data = malloc(sizeof(dns_data_t));
if(dns_data == NULL)
Expand Down

0 comments on commit b6ed50c

Please sign in to comment.