"Fossies" - the Fresh Open Source Software Archive 
Member "srg-1.3.6/include/resolver.hh" (5 Aug 2009, 3080 Bytes) of package /linux/privat/old/srg-1.3.6.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.
1 #ifndef RESOLVER_HH
2 #define RESOLVER_HH
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <map>
6 #include <netdb.h>
7 #include <netinet/in.h>
8 #include <sys/socket.h>
9 #include <arpa/inet.h>
10
11 class Resolver {
12 private:
13 bool verbose;
14 struct ltstr
15 {
16 bool operator()(const char* s1, const char* s2) const
17 {
18 return strcmp(s1, s2) < 0;
19 }
20 };
21 struct ltip
22 {
23 bool operator()(const in_addr i1,
24 const in_addr i2) const
25 {
26 return i1.s_addr<i2.s_addr;
27 }
28 };
29
30 /* Caches */
31 typedef std::map<const in_addr, char *, ltip> reverse_cache_t;
32 typedef std::map<const char *, in_addr, ltstr> forward_cache_t;
33 reverse_cache_t reverse_cache;
34 forward_cache_t forward_cache;
35
36
37 public:
38 Resolver(bool be_verbose) {
39 verbose = be_verbose;
40 }
41 ~Resolver() {
42 reverse_cache.clear();
43 forward_cache.clear();
44 }
45
46 /* Returns a pointer to a statically allocated string
47 * containing the DNS name for the requested address, or
48 * NULL if no name is available. */
49 char *get_name(const in_addr addr) {
50
51 /* Use cached name if available */
52 reverse_cache_t::iterator name;
53 name = reverse_cache.find(addr);
54 if (name != reverse_cache.end()) {
55 if (verbose)
56 fprintf(stderr, "Using cached name "
57 "for %s\n",
58 inet_ntoa(addr));
59 return reverse_cache[addr];
60 }
61
62 /* Otherwise look it up */
63 struct hostent *h;
64 if ((h=gethostbyaddr((char *)&addr, sizeof(in_addr),
65 AF_INET))==NULL) {
66 if (verbose)
67 fprintf(stderr, "Unable to resolve "
68 "address: %s\n",
69 inet_ntoa(addr));
70 return NULL;
71 } else {
72 /* Put it into our cache */
73 reverse_cache[addr] = strdup(h->h_name);
74 if (verbose)
75 fprintf(stderr, "Cached hostname "
76 "(%s) for %s\n", h->h_name,
77 inet_ntoa(addr));
78 }
79
80 return reverse_cache[addr];
81
82 }
83
84 /* Retrieves the IP Address for the specified hostname and
85 * returns it via the inp parameter. Returns 0 if an ip
86 * address was successfully returned, returns non zero
87 * otherwise */
88 int get_ip(const char *name, in_addr *inp) {
89
90 /* Used cached ip if available */
91 forward_cache_t::iterator tmp;
92 tmp = forward_cache.find(name);
93 if (tmp != forward_cache.end()) {
94 if (verbose)
95 fprintf(stderr, "Using cached ip (%s) "
96 "address for %s\n",
97 inet_ntoa(forward_cache[name]),
98 name);
99 memcpy(inp, &(*tmp).second, sizeof(in_addr));
100 return 0;
101 }
102
103 char *tname = strdup(name);
104
105 /* Otherwise look it up */
106 struct hostent *h;
107 if ((h=gethostbyname(tname))==NULL) {
108 if (verbose)
109 fprintf(stderr, "Unable to resolve "
110 "name: %s\n", tname);
111 inp = NULL;
112 free(tname);
113 return 1;
114 } else {
115 /* Put it into our cache */
116 in_addr t_addr;
117 memcpy(&t_addr, h->h_addr, sizeof(in_addr));
118 forward_cache[tname] = t_addr;
119 if (verbose)
120 fprintf(stderr, "Cached ip address "
121 "(%s) for %s\n",
122 inet_ntoa(forward_cache[tname]),
123 tname);
124 memcpy(inp, &t_addr, sizeof(in_addr));
125 }
126
127 return 0;
128 }
129
130 };
131
132 #endif