42sf2gaf(
const unsigned int getaddr_flags,
const unsigned int sockflags)
92 buf_printf(&hname,
"%s.%s", rnd_str, hostname);
103 int resolve_retry_seconds,
struct signal_info *sig_info,
int ai_family,
104 struct addrinfo **res)
106 struct addrinfo hints;
111 const char *print_hostname;
112 const char *print_servname;
116 ASSERT(hostname || servname);
121 print_servname = servname;
140 hints.ai_flags = AI_NUMERICHOST;
144 hints.ai_flags |= AI_PASSIVE;
149 hints.ai_socktype = SOCK_DGRAM;
153 hints.ai_socktype = SOCK_STREAM;
160 hints.ai_family = ai_family;
163 status = getaddrinfo(hostname, servname, &hints, res);
167 const int fail_wait_interval = 5;
169 int resolve_retries =
170 (flags &
GETADDR_TRY_ONCE) ? 1 : ((resolve_retry_seconds + 4) / fail_wait_interval);
176 hints.ai_family = ai_family;
185 print_hostname = hostname;
189 print_hostname =
"undefined";
192 fmt =
"RESOLVE: Cannot resolve host address: %s:%s%s (%s)";
195 fmt =
"RESOLVE: Cannot resolve host address: %s:%s%s (%s)"
196 "(I would have retried this name query if you had "
197 "specified the --resolv-retry option.)";
202 msg(msglevel,
"RESOLVE: Cannot parse IP address: %s:%s (%s)", print_hostname,
203 print_servname, gai_strerror(
status));
207#ifdef ENABLE_MANAGEMENT
228 hints.ai_flags &= ~AI_NUMERICHOST;
230 hints.ai_family, hints.ai_socktype);
231 status = getaddrinfo(hostname, servname, &hints, res);
241 msg(level,
"RESOLVE: Ignored SIGUSR1 signal received during "
242 "DNS resolution attempt");
269 if (resolve_retries > 0)
277 if (--resolve_retries <= 0)
299 msg(
M_WARN,
"WARNING: ignoring --remote-random-hostname because the "
300 "hostname is an IP address");
316 msg(level,
"RESOLVE: signal received during DNS resolution attempt");
325 unsigned int *netbits,
int resolve_retry_seconds,
struct signal_info *sig_info,
328 char *endp, *sep, *var_host = NULL;
329 struct addrinfo *ai = NULL;
350 max_bits =
sizeof(
struct in6_addr) * 8;
354 msg(
M_WARN,
"Unsupported AF family passed to getaddrinfo for %s (%d)", hostname, af);
363 var_host = strdup(hostname);
371 sep = strchr(var_host,
'/');
374 bits = strtoul(sep + 1, &endp, 10);
375 if ((*endp !=
'\0') || (bits > max_bits))
377 msg(msglevel,
"IP prefix '%s': invalid '/bits' spec (%s)", hostname, sep + 1);
385 if ((ret == 0) && network)
387 struct in6_addr *ip6;
390 if (af != ai->ai_family)
392 msg(msglevel,
"Can't parse %s as IPv%d address", var_host, (af == AF_INET) ? 4 : 6);
401 *ip4 = ((
struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr;
411 *ip6 = ((
struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
416 msg(
M_WARN,
"Unsupported AF family for %s (%d)", var_host, af);
423 *netbits = (
unsigned int)bits;
442getaddr(
unsigned int flags,
const char *hostname,
int resolve_retry_seconds,
bool *succeeded,
472 bool succeeded =
false;
480 ret =
getaddr(flags, ip_string, 0, &succeeded, NULL);
481 if (!succeeded && error)
489get_ipv6_addr(
const char *hostname,
struct in6_addr *network,
unsigned int *netbits,
507 int ai_family,
unsigned int resolve_flags,
struct addrinfo **ai)
515 for (ph = dns_cache; ph; ph = ph->
next)
530 const unsigned int flags)
580 for (
int i = 0; i < l->
len; ++i)
584 unsigned int flags = preresolve_flags;
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
char * format_hex_ex(const uint8_t *data, size_t size, size_t maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
Format a binary buffer as a hex string.
void gc_addspecial(void *addr, void(*free_function)(void *), struct gc_arena *a)
Register an address with a custom free function in a garbage collection arena.
bool streqnull(const char *a, const char *b)
compare two strings which might be NULL
Buffer management functions and garbage collection.
#define BSTR(buf)
Return the buffer content pointer cast to char *.
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Allocate and zero-initialise a garbage-collected object of the given type.
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
static void gc_freeaddrinfo_callback(void *addr)
Callback to free a struct addrinfo, suitable for use with gc_addspecial().
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
void prng_bytes(uint8_t *output, int len)
Data Channel Cryptography Module.
static SERVICE_STATUS status
void management_set_state(struct management *man, const int state, const char *detail, const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6, const struct openvpn_sockaddr *local, const struct openvpn_sockaddr *remote)
void management_sleep(const int n)
A sleep function that services the management layer for n seconds rather than doing nothing.
#define OPENVPN_STATE_RESOLVE
int openvpn_getaddrinfo(unsigned int flags, const char *hostname, const char *servname, int resolve_retry_seconds, struct signal_info *sig_info, int ai_family, struct addrinfo **res)
static int do_preresolve_host(struct context *c, const char *hostname, const char *servname, const int af, const unsigned int flags)
in_addr_t get_ip_addr(const char *ip_string, msglvl_t msglevel, bool *error)
static const char * hostname_randomize(const char *hostname, struct gc_arena *gc)
static int get_addr_generic(sa_family_t af, unsigned int flags, const char *hostname, void *network, unsigned int *netbits, int resolve_retry_seconds, struct signal_info *sig_info, msglvl_t msglevel)
bool get_ipv6_addr(const char *hostname, struct in6_addr *network, unsigned int *netbits, msglvl_t msglevel)
Translate an IPv6 addr or hostname from string form to in6_addr.
int get_cached_dns_entry(struct cached_dns_entry *dns_cache, const char *hostname, const char *servname, int ai_family, unsigned int resolve_flags, struct addrinfo **ai)
get_cached_dns_entry return 0 on success and -1 otherwise.
void do_preresolve(struct context *c)
static const char * getaddrinfo_addr_family_name(int af)
Small helper function for openvpn_getaddrinfo to print the address family when resolving fails.
unsigned int sf2gaf(const unsigned int getaddr_flags, const unsigned int sockflags)
Convert sockflags/getaddr_flags into getaddr_flags.
in_addr_t getaddr(unsigned int flags, const char *hostname, int resolve_retry_seconds, bool *succeeded, struct signal_info *sig_info)
Translate an IPv4 addr or hostname from string form to in_addr_t.
#define GETADDR_CACHE_MASK
#define GETADDR_MSG_VIRT_OUT
#define GETADDR_UPDATE_MANAGEMENT_STATE
#define GETADDR_HOST_ORDER
#define GETADDR_RANDOMIZE
#define GETADDR_FATAL_ON_SIGNAL
#define GETADDR_MENTION_RESOLVE_RETRY
#define GETADDR_WARN_ON_SIGNAL
void throw_signal_soft(const int signum, const char *signal_text)
Throw a soft global signal.
int signal_reset(struct signal_info *si, int signum)
Clear the signal if its current value equals signum.
static void get_signal(volatile int *sig)
Copy the global signal_received (if non-zero) to the passed-in argument sig.
#define SF_HOST_RANDOMIZE
static bool proto_is_dgram(int proto)
Return if the protocol is datagram (UDP).
Wrapper structure for dynamically allocated memory.
struct cached_dns_entry * next
struct local_list * local_list
const char * socks_proxy_port
struct http_proxy_options * http_proxy_options
const char * socks_proxy_server
struct connection_entry ** array
struct cached_dns_entry * dns_cache
Contains all state information for one tunnel.
struct options options
Options loaded from command line or configuration file.
struct gc_arena gc
Garbage collection arena for allocations done in the scope of this context structure.
struct context_1 c1
Level 1 context.
Garbage collection arena used to keep track of dynamically allocated memory.
struct local_entry ** array
int resolve_retry_seconds
struct connection_list * connection_list
const char * ip_remote_hint
volatile int signal_received
unsigned short sa_family_t