gnunet-dns-monitor.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011 GNUnet e.V.
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @file src/dns/gnunet-dns-monitor.c
  19. * @brief Tool to monitor DNS queries
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_dns_service.h"
  25. #include "gnunet_dnsparser_lib.h"
  26. /**
  27. * Handle to transport service.
  28. */
  29. static struct GNUNET_DNS_Handle *handle;
  30. /**
  31. * Option -i.
  32. */
  33. static int inbound_only;
  34. /**
  35. * Option -o.
  36. */
  37. static int outbound_only;
  38. /**
  39. * Global return value (0 success).
  40. */
  41. static int ret;
  42. /**
  43. * Selected level of verbosity.
  44. */
  45. static unsigned int verbosity;
  46. /**
  47. * Convert numeric DNS record type to a string.
  48. *
  49. * @param type type to convert
  50. * @return type as string, only valid until the next call to this function
  51. */
  52. static const char *
  53. get_type (uint16_t type)
  54. {
  55. static char buf[6];
  56. switch (type)
  57. {
  58. case GNUNET_DNSPARSER_TYPE_A: return "A";
  59. case GNUNET_DNSPARSER_TYPE_NS: return "NS";
  60. case GNUNET_DNSPARSER_TYPE_CNAME: return "CNAME";
  61. case GNUNET_DNSPARSER_TYPE_SOA: return "SOA";
  62. case GNUNET_DNSPARSER_TYPE_PTR: return "PTR";
  63. case GNUNET_DNSPARSER_TYPE_MX: return "MX";
  64. case GNUNET_DNSPARSER_TYPE_TXT: return "TXT";
  65. case GNUNET_DNSPARSER_TYPE_AAAA: return "AAAA";
  66. case GNUNET_DNSPARSER_TYPE_SRV: return "SRV";
  67. }
  68. GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) type);
  69. return buf;
  70. }
  71. /**
  72. * Convert numeric DNS record class to a string.
  73. *
  74. * @param class class to convert
  75. * @return class as string, only valid until the next call to this function
  76. */
  77. static const char *
  78. get_class (uint16_t class)
  79. {
  80. static char buf[6];
  81. switch (class)
  82. {
  83. case GNUNET_TUN_DNS_CLASS_INTERNET: return "IN";
  84. case GNUNET_TUN_DNS_CLASS_CHAOS: return "CHAOS";
  85. case GNUNET_TUN_DNS_CLASS_HESIOD: return "HESIOD";
  86. }
  87. GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) class);
  88. return buf;
  89. }
  90. /**
  91. * Output the given DNS query to stdout.
  92. *
  93. * @param query query to display.
  94. */
  95. static void
  96. display_query (const struct GNUNET_DNSPARSER_Query *query)
  97. {
  98. fprintf (stdout,
  99. "\t\t%s %s: %s\n",
  100. get_class (query->dns_traffic_class),
  101. get_type (query->type),
  102. query->name);
  103. }
  104. /**
  105. * Output the given DNS record to stdout.
  106. *
  107. * @param record record to display.
  108. */
  109. static void
  110. display_record (const struct GNUNET_DNSPARSER_Record *record)
  111. {
  112. const char *format;
  113. char buf[INET6_ADDRSTRLEN];
  114. char *tmp;
  115. tmp = NULL;
  116. switch (record->type)
  117. {
  118. case GNUNET_DNSPARSER_TYPE_A:
  119. if (record->data.raw.data_len != sizeof (struct in_addr))
  120. format = "<invalid>";
  121. else
  122. format = inet_ntop (AF_INET, record->data.raw.data, buf, sizeof (buf));
  123. break;
  124. case GNUNET_DNSPARSER_TYPE_AAAA:
  125. if (record->data.raw.data_len != sizeof (struct in6_addr))
  126. format = "<invalid>";
  127. else
  128. format = inet_ntop (AF_INET6, record->data.raw.data, buf, sizeof (buf));
  129. break;
  130. case GNUNET_DNSPARSER_TYPE_NS:
  131. case GNUNET_DNSPARSER_TYPE_CNAME:
  132. case GNUNET_DNSPARSER_TYPE_PTR:
  133. format = record->data.hostname;
  134. break;
  135. case GNUNET_DNSPARSER_TYPE_SOA:
  136. if (NULL == record->data.soa)
  137. format = "<invalid>";
  138. else
  139. {
  140. GNUNET_asprintf (&tmp,
  141. "origin: %s, mail: %s, serial = %u, refresh = %u s, retry = %u s, expire = %u s, minimum = %u s",
  142. record->data.soa->mname,
  143. record->data.soa->rname,
  144. (unsigned int) record->data.soa->serial,
  145. (unsigned int) record->data.soa->refresh,
  146. (unsigned int) record->data.soa->retry,
  147. (unsigned int) record->data.soa->expire,
  148. (unsigned int) record->data.soa->minimum_ttl);
  149. format = tmp;
  150. }
  151. break;
  152. case GNUNET_DNSPARSER_TYPE_MX:
  153. if (record->data.mx == NULL)
  154. format = "<invalid>";
  155. else
  156. {
  157. GNUNET_asprintf (&tmp,
  158. "%u: %s",
  159. record->data.mx->preference,
  160. record->data.mx->mxhost);
  161. format = tmp;
  162. }
  163. break;
  164. case GNUNET_DNSPARSER_TYPE_SRV:
  165. if (NULL == record->data.srv)
  166. format = "<invalid>";
  167. else
  168. {
  169. GNUNET_asprintf (&tmp,
  170. "priority %u, weight = %s, port = %u, target = %s",
  171. (unsigned int) record->data.srv->priority,
  172. (unsigned int) record->data.srv->weight,
  173. (unsigned int) record->data.srv->port,
  174. record->data.srv->target);
  175. format = tmp;
  176. }
  177. break;
  178. case GNUNET_DNSPARSER_TYPE_TXT:
  179. GNUNET_asprintf (&tmp,
  180. "%.*s",
  181. (unsigned int) record->data.raw.data_len,
  182. record->data.raw.data);
  183. format = tmp;
  184. break;
  185. default:
  186. format = "<payload>";
  187. break;
  188. }
  189. fprintf (stdout,
  190. "\t\t%s %s: %s = %s (%u s)\n",
  191. get_class (record->dns_traffic_class),
  192. get_type (record->type),
  193. record->name,
  194. format,
  195. (unsigned int) (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL));
  196. GNUNET_free_non_null (tmp);
  197. }
  198. /**
  199. * Signature of a function that is called whenever the DNS service
  200. * encounters a DNS request and needs to do something with it. The
  201. * function has then the chance to generate or modify the response by
  202. * calling one of the three "GNUNET_DNS_request_*" continuations.
  203. *
  204. * When a request is intercepted, this function is called first to
  205. * give the client a chance to do the complete address resolution;
  206. * "rdata" will be NULL for this first call for a DNS request, unless
  207. * some other client has already filled in a response.
  208. *
  209. * If multiple clients exist, all of them are called before the global
  210. * DNS. The global DNS is only called if all of the clients'
  211. * functions call GNUNET_DNS_request_forward. Functions that call
  212. * GNUNET_DNS_request_forward will be called again before a final
  213. * response is returned to the application. If any of the clients'
  214. * functions call GNUNET_DNS_request_drop, the response is dropped.
  215. *
  216. * @param cls closure
  217. * @param rh request handle to user for reply
  218. * @param request_length number of bytes in request
  219. * @param request udp payload of the DNS request
  220. */
  221. static void
  222. display_request (void *cls,
  223. struct GNUNET_DNS_RequestHandle *rh,
  224. size_t request_length,
  225. const char *request)
  226. {
  227. static const char *return_codes[] =
  228. {
  229. "No error", "Format error", "Server failure", "Name error",
  230. "Not implemented", "Refused", "YXDomain", "YXRRset",
  231. "NXRRset", "NOT AUTH", "NOT ZONE", "<invalid>",
  232. "<invalid>", "<invalid>", "<invalid>", "<invalid>"
  233. };
  234. static const char *op_codes[] =
  235. {
  236. "Query", "Inverse query", "Status", "<invalid>",
  237. "<invalid>", "<invalid>", "<invalid>", "<invalid>",
  238. "<invalid>", "<invalid>", "<invalid>", "<invalid>",
  239. "<invalid>", "<invalid>", "<invalid>", "<invalid>"
  240. };
  241. struct GNUNET_DNSPARSER_Packet *p;
  242. unsigned int i;
  243. p = GNUNET_DNSPARSER_parse (request, request_length);
  244. if (NULL == p)
  245. {
  246. fprintf (stderr, "Received malformed DNS packet!\n");
  247. // FIXME: drop instead?
  248. GNUNET_DNS_request_forward (rh);
  249. return;
  250. }
  251. fprintf (stdout,
  252. "%s with ID: %5u Flags: %s%s%s%s%s%s, Return Code: %s, Opcode: %s\n",
  253. p->flags.query_or_response ? "Response" : "Query",
  254. p->id,
  255. p->flags.recursion_desired ? "RD " : "",
  256. p->flags.message_truncated ? "MT " : "",
  257. p->flags.authoritative_answer ? "AA " : "",
  258. p->flags.checking_disabled ? "CD " : "",
  259. p->flags.authenticated_data ? "AD " : "",
  260. p->flags.recursion_available ? "RA " : "",
  261. return_codes[p->flags.return_code & 15],
  262. op_codes[p->flags.opcode & 15]);
  263. if (p->num_queries > 0)
  264. fprintf (stdout,
  265. "\tQueries:\n");
  266. for (i=0;i<p->num_queries;i++)
  267. display_query (&p->queries[i]);
  268. if (p->num_answers > 0)
  269. fprintf (stdout,
  270. "\tAnswers:\n");
  271. for (i=0;i<p->num_answers;i++)
  272. display_record (&p->answers[i]);
  273. fprintf (stdout, "\n");
  274. GNUNET_DNSPARSER_free_packet (p);
  275. GNUNET_DNS_request_forward (rh);
  276. }
  277. /**
  278. * Shutdown.
  279. */
  280. static void
  281. do_disconnect (void *cls)
  282. {
  283. if (NULL != handle)
  284. {
  285. GNUNET_DNS_disconnect (handle);
  286. handle = NULL;
  287. }
  288. }
  289. /**
  290. * Main function that will be run by the scheduler.
  291. *
  292. * @param cls closure
  293. * @param args remaining command-line arguments
  294. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  295. * @param cfg configuration
  296. */
  297. static void
  298. run (void *cls, char *const *args, const char *cfgfile,
  299. const struct GNUNET_CONFIGURATION_Handle *cfg)
  300. {
  301. enum GNUNET_DNS_Flags flags;
  302. flags = GNUNET_DNS_FLAG_REQUEST_MONITOR | GNUNET_DNS_FLAG_RESPONSE_MONITOR;
  303. if (inbound_only | outbound_only)
  304. flags = 0;
  305. if (inbound_only)
  306. flags |= GNUNET_DNS_FLAG_REQUEST_MONITOR;
  307. if (outbound_only)
  308. flags |= GNUNET_DNS_FLAG_RESPONSE_MONITOR;
  309. handle =
  310. GNUNET_DNS_connect (cfg,
  311. flags,
  312. &display_request,
  313. NULL);
  314. GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL);
  315. }
  316. int
  317. main (int argc, char *const *argv)
  318. {
  319. struct GNUNET_GETOPT_CommandLineOption options[] = {
  320. GNUNET_GETOPT_option_flag ('i',
  321. "inbound-only",
  322. gettext_noop ("only monitor DNS queries"),
  323. &inbound_only),
  324. GNUNET_GETOPT_option_flag ('o',
  325. "outbound-only",
  326. gettext_noop ("only monitor DNS queries"),
  327. &outbound_only),
  328. GNUNET_GETOPT_option_verbose (&verbosity),
  329. GNUNET_GETOPT_OPTION_END
  330. };
  331. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  332. return 2;
  333. ret = (GNUNET_OK ==
  334. GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-monitor",
  335. gettext_noop
  336. ("Monitor DNS queries."), options,
  337. &run, NULL)) ? ret : 1;
  338. GNUNET_free ((void*) argv);
  339. return ret;
  340. }
  341. /* end of gnunet-dns-monitor.c */