Fawkes API  Fawkes Development Version
qa_resolver.cpp
1 
2 /***************************************************************************
3  * qa_resolver.cpp - Fawkes QA for resolver
4  *
5  * Created: Thu May 10 19:10:03 2007
6  * Copyright 2006-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 /// @cond QA
25 
26 #include <netcomm/utils/resolver.h>
27 #include <utils/system/signal.h>
28 #include <utils/system/argparser.h>
29 #ifdef HAVE_AVAHI
30 #include <netcomm/dns-sd/avahi_thread.h>
31 #endif
32 
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <cstdio>
36 #include <cstdlib>
37 #include <cstring>
38 
39 using namespace fawkes;
40 
41 class ResolverQAMain : public SignalHandler
42 {
43  public:
44  ResolverQAMain(ArgumentParser *argp)
45  {
46  this->argp = argp;
47  quit = false;
48 
49  r = NULL;
50 
51 #ifdef HAVE_AVAHI
52  at = NULL;
53  if ( argp->has_arg("a") ) {
54  printf("Instantiating Avahi thread\n");
55  at = new AvahiThread();
56  at->start();
57  at->wait_initialized();
58  usleep(0);
59  }
60  r = new NetworkNameResolver(at);
61 #else
62  if ( argp->has_arg("a") ) {
63  printf("Avahi not available at compile time\n");
64  }
65  r = new NetworkNameResolver();
66 #endif
67 
68  }
69 
70  ~ResolverQAMain()
71  {
72 #ifdef HAVE_AVAHI
73  if ( at != NULL ) {
74  at->cancel();
75  at->join();
76  delete at;
77  }
78 #endif
79  delete r;
80  }
81 
82  void run()
83  {
84  struct sockaddr_in *s = NULL;
85  socklen_t slen;
86 
87  char *test = (char *)malloc(strlen("127.0.0.1") + 1);
88  strcpy(test, "127.0.0.1");
89  r->resolve_name(test, (struct sockaddr **)&s, &slen);
90  free(test);
91 
92  while ( ! quit && ! r->resolve_name("127.0.0.1", (struct sockaddr **)&s, &slen) ) {
93  usleep(0);
94  }
95  if ( quit ) {
96  return;
97  }
98  printf("Successfully resolved to 0x%x\n", s->sin_addr.s_addr);
99 
100  if ( ! r->resolve_name("127.0.0.1", (struct sockaddr **)&s, &slen) ) {
101  printf("A second try to resolve failed after first success, cache broken\n");
102  } else {
103  printf("Successfully resolved to 0x%x again\n", s->sin_addr.s_addr);
104  }
105 
106  std::string name;
107  if ( ! r->resolve_address((struct sockaddr *)s, slen, name) ) {
108  // printf("Resolving address failed\n");
109  } else {
110  printf("Successfully resolved address to '%s'\n", name.c_str());
111  }
112 
113  const char *atmp;
114  if ( (atmp = argp->arg("h")) != NULL ) {
115  printf("Trying to resolve %s\n", atmp);
116  while ( ! quit && ! r->resolve_name(atmp, (struct sockaddr **)&s, &slen) ) {
117  usleep(0);
118  }
119  if ( quit ) {
120  return;
121  }
122  char addrp[INET_ADDRSTRLEN];
123  inet_ntop(AF_INET, &(s->sin_addr), addrp, sizeof(addrp));
124  printf("Successfully resolved to 0x%x (%s)\n", s->sin_addr.s_addr, addrp);
125 
126  struct sockaddr_in so;
127  std::string tmp;
128  slen = sizeof(so);
129  so.sin_addr.s_addr = s->sin_addr.s_addr;
130  r->resolve_address((struct sockaddr *)&so, slen, tmp);
131  printf("Waiting one second to allow resolver thread to suceed\n");
132  sleep(1);
133  if ( r->resolve_address((struct sockaddr *)&so, slen, tmp) ) {
134  printf("Successfully resolved 0x%x to %s\n", so.sin_addr.s_addr, tmp.c_str());
135  }
136  }
137 
138  if ( (atmp = argp->arg("i")) != NULL ) {
139  printf("Resolving %s, press Ctrl-C to end...\n", atmp);
140  struct in_addr ia;
141  if (inet_pton(AF_INET, atmp, &ia) == 1) {
142  while ( ! quit) {
143  struct sockaddr_in so;
144  so.sin_addr.s_addr = ia.s_addr;
145  std::string name;
146  if ( r->resolve_address((struct sockaddr *)&so, sizeof(so), name) ) {
147  //printf("Successfully resolved to %s\n", name);
148  //if (name != "zadeat-1.local") {
149  // printf("Unexpected results: %s\n", name.c_str());
150  //}
151  printf("Result: %s\n", name.c_str());
152  }
153  usleep(50000);
154  }
155  } else {
156  printf("Address could not be converted to binary form. Skipping.\n");
157  }
158  }
159  }
160 
161  virtual void handle_signal(int signum)
162  {
163  quit = true;
164  }
165 
166  private:
167  ArgumentParser *argp;
169  bool quit;
170 #ifdef HAVE_AVAHI
171  AvahiThread *at;
172 #endif
173 };
174 
175 int
176 main(int argc, char **argv)
177 {
178  ArgumentParser argp(argc, argv, "ah:i:");
179 
180  ResolverQAMain m(&argp);
182  SignalManager::ignore(SIGPIPE);
183 
184  m.run();
185 
187 
188  return 0;
189 }
190 
191 
192 /// @endcond
static void finalize()
Finalize (and free) the SignalManager instance, this does NOT implicitly delete the signal handlers...
Definition: signal.cpp:98
const char * arg(const char *argn)
Get argument value.
Definition: argparser.cpp:182
Fawkes library namespace.
Interface for signal handling.
Definition: signal.h:35
Parse command line arguments.
Definition: argparser.h:66
static void ignore(int signum)
Ignore a signal.
Definition: signal.cpp:182
static SignalHandler * register_handler(int signum, SignalHandler *handler)
Register a SignalHandler for a signal.
Definition: signal.cpp:116
Avahi main thread.
Definition: avahi_thread.h:54
Network name and address resolver.
Definition: resolver.h:48
bool has_arg(const char *argn)
Check if argument has been supplied.
Definition: argparser.cpp:169