17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen#ifndef DNS_LOOKUP_H
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen#define DNS_LOOKUP_H
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen
884cc4cc720145bb3aa506543a96f7acad221370Timo Sirainen#define DNS_CLIENT_SOCKET_NAME "dns-client"
884cc4cc720145bb3aa506543a96f7acad221370Timo Sirainen
7d102c66eb1755e1894cf56e3594cd744e855238Timo Sirainenstruct dns_lookup;
7d102c66eb1755e1894cf56e3594cd744e855238Timo Sirainen
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainenstruct dns_lookup_settings {
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen const char *dns_client_socket_path;
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen unsigned int timeout_msecs;
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen /* the idle_timeout_msecs works only with the dns_client_* API.
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen 0 = disconnect immediately */
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen unsigned int idle_timeout_msecs;
8836c374dc344a48d4f6cefdf224089f3a0c40c6Stephan Bosch
8836c374dc344a48d4f6cefdf224089f3a0c40c6Stephan Bosch /* ioloop to run the lookup on (defaults to current_ioloop) */
8836c374dc344a48d4f6cefdf224089f3a0c40c6Stephan Bosch struct ioloop *ioloop;
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen};
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainenstruct dns_lookup_result {
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen /* all is ok if ret=0, otherwise it contains net_gethosterror()
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen compatible error code. error string is always set if ret != 0. */
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen int ret;
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen const char *error;
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen /* how many milliseconds the lookup took. */
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen unsigned int msecs;
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen /* for IP lookup: */
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen unsigned int ips_count;
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen const struct ip_addr *ips;
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen /* for PTR lookup: */
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen const char *name;
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen};
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainentypedef void dns_lookup_callback_t(const struct dns_lookup_result *result,
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen void *context);
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen
7d102c66eb1755e1894cf56e3594cd744e855238Timo Sirainen/* Do asynchronous DNS lookup via dns-client UNIX socket. Returns 0 if lookup
7d102c66eb1755e1894cf56e3594cd744e855238Timo Sirainen started, -1 if there was an error communicating with the UNIX socket.
7d102c66eb1755e1894cf56e3594cd744e855238Timo Sirainen When failing with -1, the callback is called before returning from the
7d102c66eb1755e1894cf56e3594cd744e855238Timo Sirainen function. */
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainenint dns_lookup(const char *host, const struct dns_lookup_settings *set,
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen dns_lookup_callback_t *callback, void *context,
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen struct dns_lookup **lookup_r) ATTR_NULL(4);
7d102c66eb1755e1894cf56e3594cd744e855238Timo Sirainen#define dns_lookup(host, set, callback, context, lookup_r) \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen dns_lookup(host + \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen CALLBACK_TYPECHECK(callback, void (*)( \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen const struct dns_lookup_result *, typeof(context))), \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen set, (dns_lookup_callback_t *)callback, context, lookup_r)
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainenint dns_lookup_ptr(const struct ip_addr *ip,
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen const struct dns_lookup_settings *set,
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen dns_lookup_callback_t *callback, void *context,
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen struct dns_lookup **lookup_r) ATTR_NULL(4);
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen#define dns_lookup_ptr(host, set, callback, context, lookup_r) \
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen dns_lookup_ptr(host + \
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen CALLBACK_TYPECHECK(callback, void (*)( \
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen const struct dns_lookup_result *, typeof(context))), \
d29c3ac4da9857ffcce57eec726d042c292e2becTimo Sirainen set, (dns_lookup_callback_t *)callback, context, lookup_r)
7d102c66eb1755e1894cf56e3594cd744e855238Timo Sirainen/* Abort the DNS lookup without calling the callback. */
7d102c66eb1755e1894cf56e3594cd744e855238Timo Sirainenvoid dns_lookup_abort(struct dns_lookup **lookup);
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen
96e3a90451b495d8bedbe5dd731539269cb8c08dStephan Boschvoid dns_lookup_switch_ioloop(struct dns_lookup *lookup);
96e3a90451b495d8bedbe5dd731539269cb8c08dStephan Bosch
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen/* Alternative API for clients that need to do multiple DNS lookups. */
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainenstruct dns_client *dns_client_init(const struct dns_lookup_settings *set);
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainenvoid dns_client_deinit(struct dns_client **client);
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen/* Connect immediately to the dns-lookup socket. */
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainenint dns_client_connect(struct dns_client *client, const char **error_r);
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainenint dns_client_lookup(struct dns_client *client, const char *host,
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen dns_lookup_callback_t *callback, void *context,
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen struct dns_lookup **lookup_r) ATTR_NULL(4);
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen#define dns_client_lookup(client, host, callback, context, lookup_r) \
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen dns_client_lookup(client, host + \
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen CALLBACK_TYPECHECK(callback, void (*)( \
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen const struct dns_lookup_result *, typeof(context))), \
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen (dns_lookup_callback_t *)callback, context, lookup_r)
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainenint dns_client_lookup_ptr(struct dns_client *client, const struct ip_addr *ip,
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen dns_lookup_callback_t *callback, void *context,
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen struct dns_lookup **lookup_r) ATTR_NULL(4);
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen#define dns_client_lookup_ptr(client, host, callback, context, lookup_r) \
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen dns_client_lookup_ptr(client, host + \
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen CALLBACK_TYPECHECK(callback, void (*)( \
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen const struct dns_lookup_result *, typeof(context))), \
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen (dns_lookup_callback_t *)callback, context, lookup_r)
ba54c712141b9764a2e06ed8dfb35bc3154b53c7Timo Sirainen
994a2b017b5b7da97914baa1ef711b124c8d15a7Timo Sirainenvoid dns_client_switch_ioloop(struct dns_client *client);
994a2b017b5b7da97914baa1ef711b124c8d15a7Timo Sirainen
17fc2a887a5683b2e1bbd6bd9fdf0cdb97b509fbTimo Sirainen#endif