root/src/udns/ex-rdns.c

Revision 5a9b91bc0a14543e0995eba973fbeacc05aaee8b, 3.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

svn merge -r 327:330 https://labs.omniti.com/reconnoiter/branches/dev/udns .

closes #38

  • Property mode set to 100644
Line 
1 /* $Id: ex-rdns.c,v 1.8 2007/01/07 22:46:47 mjt Exp $
2    parallel rDNS resolver example - read IP addresses from stdin,
3    write domain names to stdout
4  
5    Copyright (C) 2005  Michael Tokarev <mjt@corpit.ru>
6    This file is part of UDNS library, an async DNS stub resolver.
7
8    This library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public
10    License as published by the Free Software Foundation; either
11    version 2.1 of the License, or (at your option) any later version.
12
13    This library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Lesser General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public
19    License along with this library, in file named COPYING.LGPL; if not,
20    write to the Free Software Foundation, Inc., 59 Temple Place,
21    Suite 330, Boston, MA  02111-1307  USA
22
23  */
24
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <sys/poll.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <time.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "udns.h"
35
36 static int curq;
37
38 static const char *n2ip(const unsigned char *c) {
39   static char b[sizeof("255.255.255.255")];
40   sprintf(b, "%u.%u.%u.%u", c[0], c[1], c[2], c[3]);
41   return b;
42 }
43 static void dnscb(struct dns_ctx *ctx, struct dns_rr_ptr *rr, void *data) {
44   const char *ip = n2ip((unsigned char *)&data);
45   int i;
46   --curq;
47   if (rr) {
48     printf("%s", ip);
49     for(i = 0; i < rr->dnsptr_nrr; ++i)
50       printf(" %s", rr->dnsptr_ptr[i]);
51     putchar('\n');
52     free(rr);
53   }
54   else
55     fprintf(stderr, "%s: %s\n", ip, dns_strerror(dns_status(ctx)));
56 }
57
58 int main(int argc, char **argv) {
59   int c, t;
60   time_t now;
61   int maxq = 10;
62   struct pollfd pfd;
63   char linebuf[1024];
64   char *eol;
65   int eof;
66
67   if (dns_init(NULL, 1) < 0) {
68     fprintf(stderr, "unable to initialize dns library\n");
69     return 1;
70   }
71   while((c = getopt(argc, argv, "m:r")) != EOF) switch(c) {
72   case 'm': maxq = atoi(optarg); break;
73   case 'r':
74      dns_set_opt(0, DNS_OPT_FLAGS,
75                  dns_set_opt(0, DNS_OPT_FLAGS, -1) | DNS_NORD);
76      break;
77   default: return 1;
78   }
79   if (argc != optind) return 1;
80
81   pfd.fd = dns_sock(0);
82   pfd.events = POLLIN;
83   now = time(NULL);
84   c = optind;
85   eof = 0;
86   while(curq || !eof) {
87     if (!eof && curq < maxq) {
88       union { struct in_addr a; void *p; } pa;
89       if (!fgets(linebuf, sizeof(linebuf), stdin)) {
90         eof = 1;
91         continue;
92       }
93       eol = strchr(linebuf, '\n');
94       if (eol) *eol = '\0';
95       if (!linebuf[0]) continue;
96       if (dns_pton(AF_INET, linebuf, &pa.a) <= 0)
97         fprintf(stderr, "%s: invalid address\n", linebuf);
98       else if (dns_submit_a4ptr(0, &pa.a, dnscb, pa.p) == 0)
99         fprintf(stderr, "%s: unable to submit query: %s\n",
100                 linebuf, dns_strerror(dns_status(0)));
101       else
102         ++curq;
103       continue;
104     }
105     if (curq) {
106       t = dns_timeouts(0, -1, now);
107       t = poll(&pfd, 1, c * 1000);
108       now = time(NULL);
109       if (t) dns_ioevent(0, now);
110     }
111   }
112   return 0;
113 }
Note: See TracBrowser for help on using the browser.