summaryrefslogtreecommitdiff
path: root/src/include/s6-dns/s6dns-engine.h
blob: bcb1a46dd8ab6fff5ea45ad03727aae88941bef7 (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
/* ISC license. */

#ifndef S6DNS_ENGINE_H
#define S6DNS_ENGINE_H

#include <stdint.h>
#include <errno.h>
#include <skalibs/tai.h>
#include <skalibs/stralloc.h>
#include <s6-dns/s6dns-constants.h>
#include <s6-dns/s6dns-ip46.h>


 /* The dt structure: all the DNS q/r state information, transport-agnostic */

typedef struct s6dns_engine_s s6dns_engine_t, *s6dns_engine_t_ref ;


 /* Debug function hooks */

typedef int s6dns_debughook_func_t (s6dns_engine_t const *, void *) ;
typedef s6dns_debughook_func_t *s6dns_debughook_func_t_ref ;

typedef struct s6dns_debughook_s s6dns_debughook_t, *s6dns_debughook_t_ref ;
struct s6dns_debughook_s
{
  s6dns_debughook_func_t_ref post_recv ;
  s6dns_debughook_func_t_ref pre_send ;
  s6dns_debughook_func_t_ref post_send ;
  void *external ;
} ;
#define S6DNS_DEBUGHOOK_ZERO { 0, 0, 0, 0 }
extern s6dns_debughook_t const s6dns_debughook_zero ;
      

 /*
   s6dns-engine: the asynchronous DNS resolution primitives.
 */

struct s6dns_engine_s
{
  stralloc sa ; /* 2 bytes (qlen) + qlen bytes (query) + answers */
  tain_t deadline ;
  tain_t localdeadline ;
  unsigned int querylen ;
  int fd ;
  uint32_t protostate ;
  s6dns_ip46list_t servers ;
  s6dns_debughook_t const *debughook ;
  unsigned int curserver ;
  int status ;
  unsigned int flagstrict : 1 ;
  unsigned int flagtcp : 1 ;
  unsigned int flagconnecting : 1 ;
  unsigned int flagreading : 1 ;
  unsigned int flagwriting : 1 ;
} ;

#define S6DNS_ENGINE_ZERO { \
  .sa = STRALLOC_ZERO, \
  .deadline = TAIN_ZERO, \
  .localdeadline = TAIN_ZERO, \
  .querylen = 0, \
  .fd = -1, \
  .protostate = 0, \
  .servers = S6DNS_IP46LIST_ZERO, \
  .debughook = 0, \
  .curserver = 0, \
  .status = ECONNABORTED, \
  .flagstrict = 0, \
  .flagtcp = 0, \
  .flagconnecting = 0, \
  .flagreading = 0, \
  .flagwriting = 0 \
}

extern s6dns_engine_t const s6dns_engine_zero ;
extern s6dns_engine_t s6dns_engine_here ;

extern void s6dns_engine_recycle (s6dns_engine_t *) ;
extern void s6dns_engine_free (s6dns_engine_t *) ;
extern void s6dns_engine_freen (s6dns_engine_t *, unsigned int) ;

#define s6dns_engine_init(dt, servers, options, q, qlen, qtype, deadline, stamp) s6dns_engine_init_r(dt, servers, options, q, qlen, qtype, &s6dns_debughook_zero, deadline, stamp)
#define s6dns_engine_init_g(dt, servers, options, q, qlen, qtype, deadline) s6dns_engine_init(dt, servers, options, q, qlen, qtype, (deadline), &STAMP)
extern int s6dns_engine_init_r (s6dns_engine_t *, s6dns_ip46list_t const *, uint32_t, char const *, unsigned int, uint16_t, s6dns_debughook_t const *, tain_t const *, tain_t const *) ;
#define s6dns_engine_init_r_g(dt, servers, options, q, qlen, qtype, dbh, deadline) s6dns_engine_init_r(dt, servers, options, q, qlen, qtype, dbh, (deadline), &STAMP)


 /* Call before iopause() */

extern void s6dns_engine_nextdeadline (s6dns_engine_t const *, tain_t *) ;
#define s6dns_engine_isreadable(dt) ((dt)->flagreading)
#define s6dns_engine_iswritable(dt) ((dt)->flagwriting)


 /* Call after iopause(): _timeout if iopause returns 0, _event otherwise */

extern int s6dns_engine_timeout (s6dns_engine_t *, tain_t const *) ;
#define s6dns_engine_timeout_g(dt) s6dns_engine_timeout((dt), &STAMP)
extern int s6dns_engine_event (s6dns_engine_t *, tain_t const *) ;
#define s6dns_engine_event_g(dt) s6dns_engine_event((dt), &STAMP)

#define s6dns_engine_packet(dt) ((dt)->sa.s + (dt)->querylen)
#define s6dns_engine_packetlen(dt) ((unsigned int)((dt)->sa.len - (dt)->querylen))

#endif