summaryrefslogtreecommitdiff
path: root/src/rexmpp_dns.h
blob: 664123877cf028ef7bad79afa8fef1ca791c1af5 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
   @file rexmpp_dns.h
   @brief DNS resolution
   @author defanor <defanor@uberspace.net>
   @date 2020
   @copyright MIT license.

*/


#ifndef REXMPP_DNS_H
#define REXMPP_DNS_H

#include <stdint.h>
#include "config.h"

#include "rexmpp.h"

/**
   @brief DNS context.
*/
#if defined(USE_UNBOUND)
#include <unbound.h>
struct rexmpp_dns_ctx {
  struct ub_ctx *ctx;
};
#elif defined(USE_CARES)
#include <ares.h>
struct rexmpp_dns_ctx {
  ares_channel channel;
};
#else
struct rexmpp_dns_ctx {
  int dummy;
};
#endif

typedef struct rexmpp_dns_ctx rexmpp_dns_ctx_t;

struct rexmpp_dns_srv {
  uint16_t priority;
  uint16_t weight;
  uint16_t port;
  char target[256];
};

typedef struct rexmpp_dns_srv rexmpp_dns_srv_t;

/**
   @brief DNS query result.
*/
struct rexmpp_dns_result {
  /** @brief NULL-terminated array of data pointers. They contain
      ::rexmpp_dns_srv for SRV lookups, host addresses for A and AAAA
      ones. */
  void **data;
  /** @brief An array of data structure lengths. */
  int *len;
  /** @brief Whether the result was retrieved securely (that is,
      verified with DNSSEC). */
  int secure;
};

typedef struct rexmpp_dns_result rexmpp_dns_result_t;

/**
   @brief Parses an SRV DNS RR's RDATA.
   @param[in] in SRV record's RDATA.
   @param[in] in_len Length of the input data in octets.
   @param[out] out A structure to fill with data.
   @returns 0 on success, non-zero on parsing failure.
*/
int
rexmpp_parse_srv (char *in, int in_len, struct rexmpp_dns_srv *out);

/**
   @brief Frees a ::rexmpp_dns_result structure and its members.
   @param[in] result A pointer to a ::rexmpp_dns_result structure.
*/
void rexmpp_dns_result_free (rexmpp_dns_result_t *result);

/**
   @brief Initializes a DNS resolver context.
*/
int rexmpp_dns_ctx_init (rexmpp_t *s);

/**
   @brief Cleans up the state that can be discarded between XMPP
   connections, to be called from rexmpp_cleanup.
*/
void rexmpp_dns_ctx_cleanup (rexmpp_t *s);

/**
   @brief Deinitializes a DNS resolver context.
*/
void rexmpp_dns_ctx_deinit (rexmpp_t *s);

/**
   @brief Sets file descriptors to select/poll.
*/
int rexmpp_dns_fds (rexmpp_t *s, fd_set *read_fds, fd_set *write_fds);

/**
   @brief Reports timeouts.
*/
struct timeval * rexmpp_dns_timeout (rexmpp_t *s,
                                     struct timeval *max_tv,
                                     struct timeval *tv);

typedef void (*dns_query_cb_t) (rexmpp_t *s, void *ptr, rexmpp_dns_result_t *result);

/**
   @brief Initiates a query.
*/
int rexmpp_dns_resolve (rexmpp_t *s,
                        const char *query,
                        int rrtype,
                        int rrclass,
                        void* ptr,
                        dns_query_cb_t callback);

/**
   @brief Processes active queries, should be called based on the
   reported timeouts and file descriptors.
*/
int rexmpp_dns_process (rexmpp_t *s, fd_set *read_fds, fd_set *write_fds);


#endif