"Fossies" - the Fresh Open Source Software Archive 
Member "udns-0.4/ex-rdns.c" (5 Jul 2011, 3206 Bytes) of package /linux/misc/dns/udns-0.4.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "ex-rdns.c" see the
Fossies "Dox" file reference documentation.
1 /* ex-rdns.c
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;
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 c = dns_timeouts(0, -1, now);
107 c = poll(&pfd, 1, c < 0 ? -1 : c * 1000);
108 now = time(NULL);
109 if (c)
110 dns_ioevent(0, now);
111 }
112 }
113 return 0;
114 }