summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2010-07-26 15:21:56 -0700
committerMarcel Holtmann <marcel@holtmann.org>2010-07-26 15:21:56 -0700
commit2674bd2f37efa116e3dca66779a87a853b2d2526 (patch)
tree9abd782367ae5c3d81e83477bc58a35e09bbd6e6 /tools
parentd4da51d7c74e3a647a573dffdba7f84435bfaf28 (diff)
downloadconnman-2674bd2f37efa116e3dca66779a87a853b2d2526.tar.gz
connman-2674bd2f37efa116e3dca66779a87a853b2d2526.tar.bz2
connman-2674bd2f37efa116e3dca66779a87a853b2d2526.zip
Make resolver test tool use internal resolver library
Diffstat (limited to 'tools')
-rw-r--r--tools/resolv-test.c115
1 files changed, 34 insertions, 81 deletions
diff --git a/tools/resolv-test.c b/tools/resolv-test.c
index db5b3fe8..2a5c2787 100644
--- a/tools/resolv-test.c
+++ b/tools/resolv-test.c
@@ -23,112 +23,65 @@
#include <config.h>
#endif
-#include <unistd.h>
+#include <stdio.h>
#include <string.h>
-#include <resolv.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
-#include <arpa/nameser.h>
+#include <signal.h>
-static int do_connect(const char *server)
+#include <gresolv/gresolv.h>
+
+static GMainLoop *main_loop = NULL;
+
+static void resolv_debug(const char *str, void *data)
{
- struct sockaddr_in sin;
- int sk;
-
- sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- //sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sk < 0)
- return -1;
-
- memset(&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- sin.sin_port = htons(53);
- sin.sin_addr.s_addr = inet_addr(server);
-
- if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
- close(sk);
- return -1;
- }
+ g_print("%s: %s\n", (const char *) data, str);
+}
- return sk;
+static void sig_term(int sig)
+{
+ g_main_loop_quit(main_loop);
}
int main(int argc, char *argv[])
{
- ns_msg msg;
- ns_rr rr;
- int rcode;
- const char *nameserver;
- unsigned char buf[4096];
- int i, sk, err, len, off = 0;
+ struct sigaction sa;
+ GResolv *resolv;
+ int index = 0;
if (argc < 2) {
printf("missing argument\n");
return 1;
}
- if (argc > 2)
- nameserver = argv[2];
- else
- nameserver = "127.0.0.1";
-
- sk = do_connect(nameserver);
- if (sk < 0) {
- printf("Can't connect\n");
+ resolv = g_resolv_new(index);
+ if (resolv == NULL) {
+ printf("failed to create resolver\n");
return 1;
}
- len = res_mkquery(ns_o_query, argv[1], ns_c_in, ns_t_a,
- NULL, 0, NULL, buf + off, sizeof(buf) - off);
- printf("query len: %d\n", len);
-
- if (off > 0) {
- buf[0] = len >> 8;
- buf[1] = len & 0xff;
- }
-
- //for (i = 0; i < len + off; i++)
- // printf("%02x ", buf[i]);
- //printf("\n");
-
- err = send(sk, buf, len + off, 0);
- printf("send result: %d\n", err);
-
- len = recv(sk, buf, sizeof(buf), 0);
- printf("answer len: %d\n", len);
-
- //for (i = 0; i < len + off; i++)
- // printf("%02x ", buf[i]);
- //printf("\n");
+ g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
- close(sk);
+ main_loop = g_main_loop_new(NULL, FALSE);
- ns_initparse(buf + off, len - off, &msg);
+ if (argc > 2) {
+ int i;
- rcode = ns_msg_getflag(msg, ns_f_rcode);
+ for (i = 2; i < argc; i++)
+ g_resolv_add_nameserver(resolv, argv[i], 53, 0);
+ } else
+ g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
- printf("msg id: 0x%04x\n", ns_msg_id(msg));
- printf("msg rcode: %d\n", rcode);
- printf("msg count: %d\n", ns_msg_count(msg, ns_s_an));
+ g_resolv_lookup_hostname(resolv, argv[1]);
- for (i = 0; i < ns_msg_count(msg, ns_s_an); i++) {
- char result[100];
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = sig_term;
+ sigaction(SIGINT, &sa, NULL);
+ sigaction(SIGTERM, &sa, NULL);
- ns_parserr(&msg, ns_s_an, i, &rr);
+ g_main_loop_run(main_loop);
- if (ns_rr_class(rr) != ns_c_in)
- continue;
+ g_resolv_unref(resolv);
- if (ns_rr_type(rr) != ns_t_a)
- continue;
-
- if (ns_rr_rdlen(rr) != NS_INADDRSZ)
- continue;
-
- inet_ntop(AF_INET, ns_rr_rdata(rr), result, sizeof(result));
-
- printf("result: %s\n", result);
- }
+ g_main_loop_unref(main_loop);
return 0;
}