1 #ifndef DNS_LOOKUP_H 2 #define DNS_LOOKUP_H 3 4 #define DNS_CLIENT_SOCKET_NAME "dns-client" 5 6 struct dns_lookup; 7 8 struct dns_lookup_settings { 9 const char *dns_client_socket_path; 10 unsigned int timeout_msecs; 11 /* the idle_timeout_msecs works only with the dns_client_* API. 12 0 = disconnect immediately */ 13 unsigned int idle_timeout_msecs; 14 15 /* ioloop to run the lookup on (defaults to current_ioloop) */ 16 struct ioloop *ioloop; 17 struct event *event_parent; 18 }; 19 20 struct dns_lookup_result { 21 /* all is ok if ret=0, otherwise it contains net_gethosterror() 22 compatible error code. error string is always set if ret != 0. */ 23 int ret; 24 const char *error; 25 26 /* how many milliseconds the lookup took. */ 27 unsigned int msecs; 28 29 /* for IP lookup: */ 30 unsigned int ips_count; 31 const struct ip_addr *ips; 32 /* for PTR lookup: */ 33 const char *name; 34 }; 35 36 typedef void dns_lookup_callback_t(const struct dns_lookup_result *result, 37 void *context); 38 39 /* Do asynchronous DNS lookup via dns-client UNIX socket. Returns 0 if lookup 40 started, -1 if there was an error communicating with the UNIX socket. 41 When failing with -1, the callback is called before returning from the 42 function. */ 43 int dns_lookup(const char *host, const struct dns_lookup_settings *set, 44 dns_lookup_callback_t *callback, void *context, 45 struct dns_lookup **lookup_r) ATTR_NULL(4); 46 #define dns_lookup(host, set, callback, context, lookup_r) \ 47 dns_lookup(host - \ 48 CALLBACK_TYPECHECK(callback, void (*)( \ 49 const struct dns_lookup_result *, typeof(context))), \ 50 set, (dns_lookup_callback_t *)callback, context, lookup_r) 51 int dns_lookup_ptr(const struct ip_addr *ip, 52 const struct dns_lookup_settings *set, 53 dns_lookup_callback_t *callback, void *context, 54 struct dns_lookup **lookup_r) ATTR_NULL(4); 55 #define dns_lookup_ptr(host, set, callback, context, lookup_r) \ 56 dns_lookup_ptr(host - \ 57 CALLBACK_TYPECHECK(callback, void (*)( \ 58 const struct dns_lookup_result *, typeof(context))), \ 59 set, (dns_lookup_callback_t *)callback, context, lookup_r) 60 /* Abort the DNS lookup without calling the callback. */ 61 void dns_lookup_abort(struct dns_lookup **lookup); 62 63 void dns_lookup_switch_ioloop(struct dns_lookup *lookup); 64 65 /* Alternative API for clients that need to do multiple DNS lookups. */ 66 struct dns_client *dns_client_init(const struct dns_lookup_settings *set); 67 void dns_client_deinit(struct dns_client **client); 68 69 /* Connect immediately to the dns-lookup socket. */ 70 int dns_client_connect(struct dns_client *client, const char **error_r); 71 int dns_client_lookup(struct dns_client *client, const char *host, 72 dns_lookup_callback_t *callback, void *context, 73 struct dns_lookup **lookup_r) ATTR_NULL(4); 74 #define dns_client_lookup(client, host, callback, context, lookup_r) \ 75 dns_client_lookup(client, host - \ 76 CALLBACK_TYPECHECK(callback, void (*)( \ 77 const struct dns_lookup_result *, typeof(context))), \ 78 (dns_lookup_callback_t *)callback, context, lookup_r) 79 int dns_client_lookup_ptr(struct dns_client *client, const struct ip_addr *ip, 80 dns_lookup_callback_t *callback, void *context, 81 struct dns_lookup **lookup_r) ATTR_NULL(4); 82 #define dns_client_lookup_ptr(client, host, callback, context, lookup_r) \ 83 dns_client_lookup_ptr(client, host - \ 84 CALLBACK_TYPECHECK(callback, void (*)( \ 85 const struct dns_lookup_result *, typeof(context))), \ 86 (dns_lookup_callback_t *)callback, context, lookup_r) 87 88 void dns_client_switch_ioloop(struct dns_client *client); 89 90 #endif 91