soup-message-io: keep cancellable alive for duration of io_run_until
[platform/upstream/libsoup.git] / tests / dns.c
1 #include <config.h>
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include "libsoup/soup-address.h"
7
8 static GMainLoop *loop;
9 static int nlookups = 0;
10
11 static void
12 resolve_callback (SoupAddress *addr, guint status, gpointer data)
13 {
14         if (status == SOUP_STATUS_OK) {
15                 printf ("Name:    %s\n", soup_address_get_name (addr));
16                 printf ("Address: %s\n", soup_address_get_physical (addr));
17         } else {
18                 printf ("Name:    %s\n", soup_address_get_name (addr));
19                 printf ("Error:   %s\n", soup_status_get_phrase (status));
20         }
21         printf ("\n");
22
23         g_object_unref (addr);
24
25         nlookups--;
26         if (nlookups == 0)
27                 g_main_loop_quit (loop);
28 }
29
30 static void
31 usage (void)
32 {
33         fprintf (stderr, "Usage: dns hostname ...\n");
34         exit (1);
35 }
36
37 int
38 main (int argc, char **argv)
39 {
40         SoupAddress *addr;
41         int i;
42
43         if (argc < 2)
44                 usage ();
45
46         g_type_init ();
47
48         for (i = 1; i < argc; i++) {
49                 addr = soup_address_new (argv[i], 0);
50                 if (!addr) {
51                         fprintf (stderr, "Could not parse address %s\n", argv[1]);
52                         exit (1);
53                 }
54
55                 soup_address_resolve_async (addr, NULL, NULL,
56                                             resolve_callback, NULL);
57                 nlookups++;
58         }
59
60         loop = g_main_loop_new (NULL, TRUE);
61         g_main_loop_run (loop);
62         g_main_loop_unref (loop);
63
64         return 0;
65 }