File: adns.c

package info (click to toggle)
liboop 1.0.1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,108 kB
  • sloc: sh: 11,098; ansic: 2,533; makefile: 133
file content (178 lines) | stat: -rw-r--r-- 3,931 bytes parent folder | download | duplicates (8)
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
/* adns.c, liboop, copyright 1999 Dan Egnor

   This is free software; you can redistribute it and/or modify it under the
   terms of the GNU Lesser General Public License, version 2.1 or later.
   See the file COPYING for details. */

#ifdef HAVE_ADNS

#include "oop.h"
#include "adns.h"
#include "oop-adns.h"

#include <assert.h>

#ifdef HAVE_STRING_H
#include <string.h>   /* Needed on NetBSD1.1/SPARC due to bzero/FD_ZERO. */
#endif

#ifdef HAVE_STRINGS_H
#include <strings.h>  /* Needed on AIX 4.2 due to bzero/FD_ZERO. */
#endif

struct oop_adapter_adns {
	oop_source *source;
	oop_adapter_select *select;
	adns_state state;
	int count;
};

struct oop_adns_query {
	oop_adapter_adns *a;
	adns_query query;
	oop_adns_call *call;
	void *data;
};

static oop_call_select on_select;
static oop_call_time on_process;
static void set_select(oop_adapter_adns *);

oop_adapter_adns *oop_adns_new(
	oop_source *source,
	adns_initflags flags,FILE *diag) 
{
	oop_adapter_adns *a = oop_malloc(sizeof(*a));
	if (NULL == a) return NULL;
	a->select = NULL;
	a->state = NULL;

	if (adns_init(&a->state,flags | adns_if_noautosys,diag)
	|| (NULL == (a->select = oop_select_new(source,on_select,a)))) {
		if (NULL != a->state) adns_finish(a->state);
		if (NULL != a->select) oop_select_delete(a->select);
		oop_free(a);
		return NULL;
	}

	a->source = source;
	a->count = 0;
	return a;
}

void oop_adns_delete(oop_adapter_adns *a) {
	assert(0 == a->count && 
	       "deleting oop_adapter_adns with outstanding queries");
	a->source->cancel_time(a->source,OOP_TIME_NOW,on_process,a);
	oop_select_delete(a->select);
	adns_finish(a->state);
	oop_free(a);
}

oop_adns_query *oop_adns_submit(
	oop_adapter_adns *a,int *errcode,
	const char *owner,adns_rrtype type,adns_queryflags flags,
	oop_adns_call *call,void *data)
{
	oop_adns_query *q = oop_malloc(sizeof(*q));
	int err;
	if (NULL == q) return NULL;

	err = adns_submit(a->state,owner,type,flags,q,&q->query);
	if (errcode) *errcode = err;
	if (err) {
		oop_free(q);
		return NULL;
	}

	q->a = a;
	q->call = call;
	q->data = data;
	++q->a->count;
	set_select(a);
	return q;
}

oop_adns_query *oop_adns_submit_reverse(
	oop_adapter_adns *a,int *errcode,
	const struct sockaddr *addr,adns_rrtype type,adns_queryflags flags,
	oop_adns_call *call,void *data)
{
	oop_adns_query *q = oop_malloc(sizeof(*q));
	int err;
	if (NULL == q) return NULL;

	err = adns_submit_reverse(a->state,addr,type,flags,q,&q->query);
	if (errcode) *errcode = err;
	if (err) {
		oop_free(q);
		return NULL;
	}

	q->a = a;
	q->call = call;
	q->data = data;
	++q->a->count;
	set_select(a);
	return q;
}

void oop_adns_cancel(oop_adns_query *q) {
	adns_cancel(q->query);
	--q->a->count;
	set_select(q->a);
	oop_free(q);
}

static void set_select(oop_adapter_adns *a) {
	fd_set rfd,wfd,xfd;
	struct timeval buf,*out = NULL,now;
	int maxfd = 0;
	FD_ZERO(&rfd);
	FD_ZERO(&wfd);
	FD_ZERO(&xfd);
	gettimeofday(&now,NULL);
	adns_beforeselect(a->state,&maxfd,&rfd,&wfd,&xfd,&out,&buf,&now);
	oop_select_set(a->select,maxfd,&rfd,&wfd,&xfd,out);
}

static void *on_process(oop_source *source,struct timeval when,void *data) {
	oop_adapter_adns *a = (oop_adapter_adns *) data;
	adns_answer *r;
	adns_query query;
	oop_adns_query *q = NULL;
	void *adns_data;

	query = NULL;
	if (0 == adns_check(a->state,&query,&r,&adns_data)) {
		q = (oop_adns_query *) adns_data;
		assert(query == q->query);
	}

	set_select(a);

	if (NULL != q) {
		oop_adns_call *call = q->call;
		void *data = q->data;
		assert(a == q->a);
		--q->a->count;
		oop_free(q);
		source->on_time(source,when,on_process,a);
		return call(a,r,data);
	}

	return OOP_CONTINUE;
}

static void *on_select(
	oop_adapter_select *select,
	int num,fd_set *rfd,fd_set *wfd,fd_set *xfd,
	struct timeval now,void *data)
{
	oop_adapter_adns *a = (oop_adapter_adns *) data;

	adns_afterselect(a->state,num,rfd,wfd,xfd,&now);
	return on_process(a->source,OOP_TIME_NOW,a);
}

#endif