File: hs_resolv.h

package info (click to toggle)
haskell-resolv 0.2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,140 kB
  • sloc: haskell: 1,083; ansic: 133; makefile: 3
file content (185 lines) | stat: -rw-r--r-- 3,572 bytes parent folder | download
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#if !defined(HS_RESOLV_H)
#define HS_RESOLV_H

#include "hs_resolv_config.h"

#include <sys/types.h>

#if defined(HAVE_NETINET_IN_H)
# include <netinet/in.h>
#endif

#if defined(HAVE_DECL_H_ERRNO)
# include <netdb.h>
#endif

#if defined(HAVE_ARPA_NAMESER_H)
# include <arpa/nameser.h>
#endif

#include <resolv.h>

#include <assert.h>

/* This is usually provided via <arpa/nameser_compat.h> */
#if !defined(QUERY)
# define QUERY ns_o_query
#endif

#if !defined(USE_RES_NQUERY)
# error USE_RES_NQUERY not defined
#endif

#if USE_RES_NQUERY && (SIZEOF_STRUCT___RES_STATE <= 0)
# error broken invariant
#endif

/* Macro to calculate error code returned by hs_get_h_errno */
#define __HS_GET_H_ERRNO(h_errno)    \
  switch(h_errno)                    \
  {                                  \
    case HOST_NOT_FOUND: return 1;   \
    case NO_DATA:        return 2;   \
    case NO_RECOVERY:    return 3;   \
    case TRY_AGAIN:      return 4;   \
    case 0:              return 0;   \
    default:             return -1;  \
  }

#if USE_RES_NQUERY

inline static int
res_opt_set_use_dnssec(struct __res_state *s)
{
  assert(s);

  if (!(s->options & RES_INIT)) {
    int rc = res_ninit(s);
    if (rc) return rc;
  }

  s->options |= RES_USE_DNSSEC | RES_USE_EDNS0;

  return 0;
}

inline static int
hs_res_mkquery(struct __res_state *s, const char *dname, int class, int type, unsigned char *req, int reqlen0)
{
  assert(s);

  int reqlen = res_nmkquery(s, QUERY, dname, class, type, NULL, 0, NULL, req, reqlen0);

  assert(reqlen <= reqlen0);

  return reqlen;
}

inline static int
hs_res_send(struct __res_state *s, const unsigned char *msg, int msglen, unsigned char *answer, int anslen)
{
  assert(s);

  return res_nsend(s, msg, msglen, answer, anslen);
}

inline static int
hs_res_query(struct __res_state *s, const char *dname, int class, int type, unsigned char *answer, int anslen)
{
  assert(s);

  return res_nquery(s, dname, class, type, answer, anslen);
}

/* res_nclose() finalizes resources allocated by res_ninit() and subsequent
 * calls to res_nquery() */

inline static void
hs_res_close(struct __res_state *s)
{
  assert(s);

  res_nclose(s);
}

inline static int
hs_get_h_errno(struct __res_state *s)
{
#if defined(HAVE_DECL_H_ERRNO)
#if defined(HAVE_STRUCT___RES_STATE_RES_H_ERRNO)
  assert(s);
  __HS_GET_H_ERRNO(s->res_h_errno)
#else
  __HS_GET_H_ERRNO(h_errno)
#endif
#else
  return -1;
#endif
}

#else  /* USE_RES_NQUERY */

/* use non-reentrant API */

inline static int
res_opt_set_use_dnssec(void *s)
{
  assert(!s);

  if (!(_res.options & RES_INIT)) {
    int rc = res_init();
    if (rc) return rc;
  }

  _res.options |= RES_USE_DNSSEC | RES_USE_EDNS0;

  return 0;
}


inline static int
hs_res_mkquery(void *s, const char *dname, int class, int type, unsigned char *req, int reqlen0)
{
  assert(!s);

  int reqlen = res_mkquery(QUERY, dname, class, type, NULL, 0, NULL, req, reqlen0);

  assert(reqlen <= reqlen0);

  return reqlen;
}

inline static int
hs_res_send(void *s, const unsigned char *msg, int msglen, unsigned char *answer, int anslen)
{
  assert(!s);

  return res_send(msg, msglen, answer, anslen);
}

inline static int
hs_res_query(void *s, const char *dname, int class, int type, unsigned char *answer, int anslen)
{
  assert(!s);

  return res_query(dname, class, type, answer, anslen);
}

inline static void
hs_res_close(void *s)
{
}

inline static int
hs_get_h_errno(void *s)
{
#if defined(HAVE_DECL_H_ERRNO)
  __HS_GET_H_ERRNO(h_errno)
#else
  return -1;
#endif
}

#endif /* USE_RES_NQUERY */

#endif /* HS_RESOLV_H */