-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathresolve.c
75 lines (64 loc) · 1.78 KB
/
resolve.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "resolve.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error_handling.h"
#ifdef WIN32
#include <ws2tcpip.h>
#else // !WIN32
#include <arpa/inet.h> // inet_pton (parsing ipv4 and ipv6 notation)
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#endif
int resolveName(struct sockaddr* addr, socklen_t* addr_len, const char* hostname, int family)
{
int result;
struct addrinfo* res = NULL;
// zero addr
memset(addr, 0, *addr_len);
// try to parse ipv4
if(family == AF_UNSPEC || family == AF_INET) {
result = inet_pton(AF_INET, hostname, &((struct sockaddr_in*) addr)->sin_addr);
if(result == 1)
{
addr->sa_family = AF_INET;
*addr_len = sizeof(struct sockaddr_in);
return 1;
}
}
// try to parse ipv6
if(family == AF_UNSPEC || family == AF_INET6) {
result = inet_pton(AF_INET6, hostname, &((struct sockaddr_in6*) addr)->sin6_addr);
if(result == 1)
{
addr->sa_family = AF_INET6;
*addr_len = sizeof(struct sockaddr_in6);
return 1;
}
}
// try to resolve DNS
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = family; // AF_UNSPEC, AF_INET, AF_INET6
hints.ai_socktype = 0; // 0 = any, SOCK_STREAM, SOCK_DGRAM
hints.ai_protocol = 0; // 0 = any
hints.ai_flags = 0; // no flags
result = getaddrinfo(hostname, NULL, &hints, &res);
if( result != 0)
{
ERRM( "Error: cannot resolve hostname %s: %s\n", hostname, gai_strerror(result) );
exit( -1 );
}
if(res->ai_addrlen > *addr_len)
{
// error - this should not happen
ERRM( "Error addr is to short. required length: %d, available length: %d", res->ai_addrlen, *addr_len );
freeaddrinfo(res);
exit( -1 );
}
memcpy(addr, res->ai_addr, res->ai_addrlen);
*addr_len = res->ai_addrlen;
freeaddrinfo(res);
return 1;
}