File: resolve.c

package info (click to toggle)
ruli 0.36-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,148 kB
  • sloc: ansic: 12,599; sh: 416; makefile: 350; perl: 196; java: 112; php: 55; pascal: 33; lisp: 31
file content (331 lines) | stat: -rw-r--r-- 8,144 bytes parent folder | download | duplicates (6)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*-GNU-GPL-BEGIN-*
RULI - Resolver User Layer Interface - Querying DNS SRV records
Copyright (C) 2003 Everton da Silva Marques

RULI is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

RULI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RULI; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*-GNU-GPL-END-*/

/*
  This test program can redo one query N times.
  One query is resent only after the previous instance is finished.

  $Id: resolve.c,v 1.10 2004/06/16 17:28:45 evertonm Exp $
 */


#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <ruli.h>

#include "trivial_conf_handler.h"


const char *prog_name;

int redo;


static void create_oop_source(oop_source_sys **source_sys, oop_source **source)
{
  /* Create the system event source */
  *source_sys = oop_sys_new();
  if (!*source_sys) {
    fprintf(stderr, 
	    "%s: can't create system event source: oop_sys_new() failed\n", 
	    prog_name);
    exit(1);
  }

  /* Get the system event registration interface */
  *source = oop_sys_source(*source_sys);
  if (!*source) {
    fprintf(stderr, 
	    "%s: can't get registration interface: oop_sys_source() failed\n",
	    prog_name);
    exit(1);
  }
}

static void *perform_redo(ruli_res_t *res_ctx, ruli_res_query_t *qry)
{
  assert(redo >= 0);

  ruli_res_query_delete(qry);

  if (--redo >= 0)
    ruli_res_query_submit(res_ctx, qry);
  
  return OOP_CONTINUE;
}

static void *on_answer(ruli_res_query_t *res_qry, void *arg)
{
  ruli_res_t        *res_ctx = res_qry->resolver;
  ruli_msg_header_t msg_hdr;

  /* Just to check we received the same arg we passed */
  assert(res_qry == arg);

  if (res_qry->answer_code == RULI_CODE_TIMEOUT) {
      printf("%s: query timed out\n", prog_name);
      
      return perform_redo(res_ctx, res_qry);
  }

  if (res_qry->answer_code) {
    printf("%s: query failed with code=%d\n", 
	   prog_name, res_qry->answer_code);

    return perform_redo(res_ctx, res_qry);
  }

  msg_hdr = res_qry->answer_header;

  printf("%s: query succeded: id=%d rcode=%d qd=%d an=%d ns=%d ar=%d "
	 "answer_buf_size=%d answer_msg_len=%d\n", 
	 prog_name, msg_hdr.id, msg_hdr.rcode, msg_hdr.qdcount, 
	 msg_hdr.ancount, msg_hdr.nscount, msg_hdr.arcount,
	 res_qry->answer_buf_size, res_qry->answer_msg_len);

  return perform_redo(res_ctx, res_qry);
}

static void submit_query(ruli_res_t *res_ctx, ruli_res_query_t *res_qry,
			 const char *domain, int domain_len,
			 char *dname_buf, int dname_buf_len,
			 const char *qclass, const char *type)
{
  int  qc, qt;
  int  result;
  char *i;

  qc = ruli_get_qclass_code(qclass);
  if (!qc) {
    fprintf(stderr, "%s: can't find query class: %s\n", prog_name, qclass);
    exit(1);
  }

  qt = ruli_get_qtype_code(type);
  if (!qt) {
    fprintf(stderr, "%s: can't find query type: %s\n", prog_name, type);
    exit(1);
  }

  i = ruli_dname_encode(dname_buf, dname_buf_len, domain, domain_len);
  if (!i) {
    fprintf(stderr, "%s: can't encode domain: %s\n", prog_name, domain);
    exit(1);
  }

  res_qry->q_on_answer      = on_answer;
  res_qry->q_on_answer_arg  = res_qry;    /* bogus arg */
  res_qry->q_domain         = dname_buf;
  res_qry->q_domain_len     = i - dname_buf;
  res_qry->q_class          = qc;
  res_qry->q_type           = qt;
  res_qry->q_options        = RULI_RES_OPT_VOID;

  result = ruli_res_query_submit(res_ctx, res_qry);
  if (result) {
    fprintf(stderr, 
	    "%s: ruli_res_query_submit() failed: %s [%d]\n", 
	    prog_name, ruli_res_errstr(result), result);
    exit(1);
  }
}

static void perform_query(const char *domain, const char *qclass, 
			  const char *type, int retry, int timeout,
			  ruli_list_t *server_list)
{
  oop_source_sys      *source_sys; /* System event source */
  oop_source          *source;     /* Event registration interface */
  ruli_res_t          res_ctx;
  int                 result;
  ruli_conf_handler_t handler;

  int                 domain_len    = strlen(domain);
  int                 dname_buf_len = ruli_dname_encode_size(domain, domain_len);
  char                dname_buf[dname_buf_len];

  ruli_res_query_t res_qry;
  /* ruli_res_query_t res_qry2; */

  /*
   * Create event source
   */
  create_oop_source(&source_sys, &source);

  /*
   * Initialize resolver
   */

  handler.opaque          = server_list;
  handler.search_loader   = load_search_list;
  handler.search_unloader = unload_search_list;
  handler.ns_loader       = load_ns_list;
  handler.ns_unloader     = unload_ns_list;
  
  res_ctx.res_conf_handler = &handler;
  res_ctx.res_source       = source;
  res_ctx.res_retry        = retry;
  res_ctx.res_timeout      = timeout;

  result = ruli_res_new(&res_ctx);
  if (result) {
    fprintf(stderr, 
	    "%s: can't create ruli resolver: %s [%d]\n", 
	    prog_name, ruli_res_errstr(result), result);
    exit(1);
  }

  /*
   * Submit query
   */
  submit_query(&res_ctx, &res_qry, domain, domain_len,
	       dname_buf, dname_buf_len,
	       qclass, type);

  /*
   * Replay
   */
  /*
  submit_query(&res_ctx, &res_qry2, domain, domain_len,
	       dname_buf, dname_buf_len,
	       qclass, type);
  */

  /*
   * Run event loop
   */

  {
    void *oop_result = oop_sys_run(source_sys);

    if (oop_result == OOP_ERROR)
      printf("%s: oop system source returned error\n", prog_name);
    else if (oop_result == OOP_CONTINUE)
      printf("%s: oop system source had no event registered\n", prog_name);
    else if (oop_result == OOP_HALT)
      printf("%s: some sink requested oop system halt\n", prog_name);
    else
      printf("%s: unexpected oop system source result (!)\n", prog_name);
  }

  /*
   * Destroy resolver
   */
  ruli_res_delete(&res_ctx);

  /*
   * Destroy event source
   */
  oop_sys_delete(source_sys);
}

static void parse_servers(ruli_list_t *server_list, int serverc, 
			  const char **serverv)
{
  int i;
  assert(serverc >= 1);

  for (i = 0; i < serverc; ++i) {
    ruli_addr_t *addr;

    addr = ruli_addr_parse_new(serverv[i]);
    if (!addr) {
      fprintf(stderr, "%s: can't create address: %s\n", prog_name, serverv[i]);
      exit(1);
    }

    printf("%s: saving server: ", prog_name);
    ruli_addr_print(stdout, addr);
    printf("\n");

    {
      int result = ruli_list_push(server_list, addr);
      assert(!result);
    }
    
  }
}

int main(int argc, const char **argv) 
{
  const char  *domain;
  const char  *qclass;
  const char  *type;
  int         retry;
  int         timeout;
  int         serverc;
  const char  **serverv;
  ruli_list_t server_list; /* list of ruli_addr_t* */

  prog_name = argv[0];

  if (argc < 8) {
    fprintf(stderr, 
	    "usage: %s <REDO> <domain> <class> <type> <retry> <timeout> <server1> [ ... <serverN> ]\n", 
	    prog_name);
    exit(1);
  }

  redo    = atoi(argv[1]);
  domain  = argv[2];
  qclass   = argv[3];
  type    = argv[4];
  retry   = atoi(argv[5]);
  timeout = atoi(argv[6]);
  serverc = argc - 7;
  serverv = argv + 7;

  if (redo < 0) {
    fprintf(stderr, "%s: bad REDO: %d\n", prog_name, redo);
    exit(1);
  }

  if (retry < 0) {
    fprintf(stderr, "%s: bad retry: %d\n", prog_name, retry);
    exit(1);
  }

  if (timeout < 0) {
    fprintf(stderr, "%s: bad timeout: %d\n", prog_name, timeout);
    exit(1);
  }

  {
    int result = ruli_list_new(&server_list);
    assert(!result);
  }

  parse_servers(&server_list, serverc, serverv);

  perform_query(domain, qclass, type, retry, timeout, &server_list);

  ruli_list_dispose_trivial(&server_list);

  fprintf(stderr, "%s: done\n", prog_name);

  exit(0);
}