diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-01-27 12:46:51 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-01-27 12:46:51 +0000 |
commit | fae775b8e3ebc6e19b97156d3a3008ee91c3b64b (patch) | |
tree | 12c7d0f18bc3f1f00f356df0f493c71ee006dda3 /src/util/gnunet-resolver.c | |
parent | f4c1249ba12461abe0616963959b9470a41e1de5 (diff) | |
download | gnunet-fae775b8e3ebc6e19b97156d3a3008ee91c3b64b.tar.gz gnunet-fae775b8e3ebc6e19b97156d3a3008ee91c3b64b.zip |
-fixing #2116
Diffstat (limited to 'src/util/gnunet-resolver.c')
-rw-r--r-- | src/util/gnunet-resolver.c | 86 |
1 files changed, 82 insertions, 4 deletions
diff --git a/src/util/gnunet-resolver.c b/src/util/gnunet-resolver.c index 131693dd1..142dd0d2f 100644 --- a/src/util/gnunet-resolver.c +++ b/src/util/gnunet-resolver.c | |||
@@ -30,12 +30,39 @@ | |||
30 | #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1) | 30 | #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1) |
31 | 31 | ||
32 | /** | 32 | /** |
33 | * Flag for reverse lookup. | ||
34 | */ | ||
35 | static int reverse; | ||
36 | |||
37 | |||
38 | /** | ||
39 | * Prints each hostname obtained from DNS. | ||
40 | * | ||
41 | * @param cls closure (unused) | ||
42 | * @param hostname one of the names for the host, NULL | ||
43 | * on the last call to the callback | ||
44 | */ | ||
45 | static void | ||
46 | print_hostname (void *cls, | ||
47 | const char *hostname) | ||
48 | { | ||
49 | if (NULL == hostname) | ||
50 | return; | ||
51 | FPRINTF (stdout, "%s\n", hostname); | ||
52 | } | ||
53 | |||
54 | |||
55 | /** | ||
33 | * Callback function to display address. | 56 | * Callback function to display address. |
57 | * | ||
58 | * @param cls closure (unused) | ||
59 | * @param addr one of the addresses of the host, NULL for the last address | ||
60 | * @param addrlen length of the address | ||
34 | */ | 61 | */ |
35 | static void | 62 | static void |
36 | printer (void *cls, const struct sockaddr *addr, socklen_t addrlen) | 63 | print_sockaddr (void *cls, const struct sockaddr *addr, socklen_t addrlen) |
37 | { | 64 | { |
38 | if (addr == NULL) | 65 | if (NULL == addr) |
39 | return; | 66 | return; |
40 | FPRINTF (stdout, "%s\n", GNUNET_a2s (addr, addrlen)); | 67 | FPRINTF (stdout, "%s\n", GNUNET_a2s (addr, addrlen)); |
41 | } | 68 | } |
@@ -53,9 +80,57 @@ static void | |||
53 | run (void *cls, char *const *args, const char *cfgfile, | 80 | run (void *cls, char *const *args, const char *cfgfile, |
54 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 81 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
55 | { | 82 | { |
83 | const struct sockaddr *sa; | ||
84 | socklen_t salen; | ||
85 | struct sockaddr_in v4; | ||
86 | struct sockaddr_in6 v6; | ||
87 | |||
56 | if (args[0] == NULL) | 88 | if (args[0] == NULL) |
57 | return; | 89 | return; |
58 | GNUNET_RESOLVER_ip_get (args[0], AF_UNSPEC, GET_TIMEOUT, &printer, NULL); | 90 | if (! reverse) |
91 | { | ||
92 | GNUNET_RESOLVER_ip_get (args[0], AF_UNSPEC, GET_TIMEOUT, &print_sockaddr, NULL); | ||
93 | return; | ||
94 | } | ||
95 | |||
96 | sa = NULL; | ||
97 | memset (&v4, 0, sizeof (v4)); | ||
98 | v4.sin_family = AF_INET; | ||
99 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
100 | v4.sin_len = sizeof (v4); | ||
101 | #endif | ||
102 | if (1 == inet_pton (AF_INET, | ||
103 | args[0], | ||
104 | &v4.sin_addr)) | ||
105 | { | ||
106 | sa = (struct sockaddr *) &v4; | ||
107 | salen = sizeof (v4); | ||
108 | } | ||
109 | memset (&v6, 0, sizeof (v6)); | ||
110 | v6.sin6_family = AF_INET6; | ||
111 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
112 | v6.sin6_len = sizeof (v6); | ||
113 | #endif | ||
114 | if (1 == inet_pton (AF_INET6, | ||
115 | args[0], | ||
116 | &v6.sin6_addr)) | ||
117 | { | ||
118 | sa = (struct sockaddr *) &v6; | ||
119 | salen = sizeof (v6); | ||
120 | } | ||
121 | if (NULL == sa) | ||
122 | { | ||
123 | fprintf (stderr, | ||
124 | "`%s' is not a valid IP: %s\n", | ||
125 | args[0], | ||
126 | strerror (errno)); | ||
127 | return; | ||
128 | } | ||
129 | GNUNET_RESOLVER_hostname_get (sa, salen, | ||
130 | GNUNET_YES, | ||
131 | GET_TIMEOUT, | ||
132 | &print_hostname, | ||
133 | NULL); | ||
59 | } | 134 | } |
60 | 135 | ||
61 | /** | 136 | /** |
@@ -69,11 +144,14 @@ int | |||
69 | main (int argc, char *const *argv) | 144 | main (int argc, char *const *argv) |
70 | { | 145 | { |
71 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | 146 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { |
147 | { 'r', "reverse", NULL, | ||
148 | gettext_noop ("perform a reverse lookup"), | ||
149 | 0, &GNUNET_GETOPT_set_one, &reverse }, | ||
72 | GNUNET_GETOPT_OPTION_END | 150 | GNUNET_GETOPT_OPTION_END |
73 | }; | 151 | }; |
74 | return (GNUNET_OK == | 152 | return (GNUNET_OK == |
75 | GNUNET_PROGRAM_run (argc, argv, "gnunet-resolver [hostname]", | 153 | GNUNET_PROGRAM_run (argc, argv, "gnunet-resolver [hostname]", |
76 | gettext_noop ("Test GNUnet DNS resolver code."), | 154 | gettext_noop ("Use build-in GNUnet stub resolver"), |
77 | options, &run, NULL)) ? 0 : 1; | 155 | options, &run, NULL)) ? 0 : 1; |
78 | } | 156 | } |
79 | 157 | ||