loudmouth/lm-asyncns-resolver.c
changeset 518 cdd6a0c5b439
parent 516 4dd3aa6b83e5
child 547 692c7753f64e
--- a/loudmouth/lm-asyncns-resolver.c	Wed Oct 08 12:44:10 2008 +0200
+++ b/loudmouth/lm-asyncns-resolver.c	Wed Oct 08 13:11:02 2008 +0200
@@ -42,10 +42,10 @@
 
 typedef struct LmAsyncnsResolverPriv LmAsyncnsResolverPriv;
 struct LmAsyncnsResolverPriv {
-	GSource		*watch_resolv;
-	asyncns_query_t *resolv_query;
-	asyncns_t	*asyncns_ctx;
-	GIOChannel	*resolv_channel;
+    GSource     *watch_resolv;
+    asyncns_query_t *resolv_query;
+    asyncns_t   *asyncns_ctx;
+    GIOChannel  *resolv_channel;
 };
 
 static void     asyncns_resolver_finalize      (GObject     *object);
@@ -57,84 +57,84 @@
 static void
 lm_asyncns_resolver_class_init (LmAsyncnsResolverClass *class)
 {
-	GObjectClass    *object_class = G_OBJECT_CLASS (class);
-        LmResolverClass *resolver_class = LM_RESOLVER_CLASS (class);
+    GObjectClass    *object_class = G_OBJECT_CLASS (class);
+    LmResolverClass *resolver_class = LM_RESOLVER_CLASS (class);
 
-	object_class->finalize = asyncns_resolver_finalize;
+    object_class->finalize = asyncns_resolver_finalize;
 
-        resolver_class->lookup = asyncns_resolver_lookup;
-        resolver_class->cancel = asyncns_resolver_cancel;
+    resolver_class->lookup = asyncns_resolver_lookup;
+    resolver_class->cancel = asyncns_resolver_cancel;
 
-	g_type_class_add_private (object_class, sizeof (LmAsyncnsResolverPriv));
+    g_type_class_add_private (object_class, sizeof (LmAsyncnsResolverPriv));
 }
 
 static void
 lm_asyncns_resolver_init (LmAsyncnsResolver *asyncns_resolver)
 {
-	LmAsyncnsResolverPriv *priv;
+    LmAsyncnsResolverPriv *priv;
 
-	priv = GET_PRIV (asyncns_resolver);
+    priv = GET_PRIV (asyncns_resolver);
 }
 
 static void
 asyncns_resolver_finalize (GObject *object)
 {
-	LmAsyncnsResolverPriv *priv;
+    LmAsyncnsResolverPriv *priv;
 
-	priv = GET_PRIV (object);
+    priv = GET_PRIV (object);
 
-	(G_OBJECT_CLASS (lm_asyncns_resolver_parent_class)->finalize) (object);
+    (G_OBJECT_CLASS (lm_asyncns_resolver_parent_class)->finalize) (object);
 }
 
 static void
 asyncns_resolver_cleanup (LmResolver *resolver)
 {
-        LmAsyncnsResolverPriv *priv = GET_PRIV (resolver);
+    LmAsyncnsResolverPriv *priv = GET_PRIV (resolver);
 
-        if (priv->resolv_channel != NULL) {
-		g_io_channel_unref (priv->resolv_channel);
-                priv->resolv_channel = NULL;
-	}
+    if (priv->resolv_channel != NULL) {
+        g_io_channel_unref (priv->resolv_channel);
+        priv->resolv_channel = NULL;
+    }
  
-        if (priv->watch_resolv) {
-		g_source_destroy (priv->watch_resolv);
-                priv->watch_resolv = NULL;
-	}
+    if (priv->watch_resolv) {
+        g_source_destroy (priv->watch_resolv);
+        priv->watch_resolv = NULL;
+    }
 
-	if (priv->asyncns_ctx) {
-                asyncns_free (priv->asyncns_ctx);
-                priv->asyncns_ctx = NULL;
-	}
+    if (priv->asyncns_ctx) {
+        asyncns_free (priv->asyncns_ctx);
+        priv->asyncns_ctx = NULL;
+    }
 
-        priv->resolv_query = NULL;
+    priv->resolv_query = NULL;
 }
 
 static void
 asyncns_resolver_done (LmResolver *resolver)
 {
-        LmAsyncnsResolverPriv *priv = GET_PRIV (resolver);
-        struct addrinfo	      *ans;
-	int 		       err;
+    LmAsyncnsResolverPriv *priv = GET_PRIV (resolver);
+    struct addrinfo       *ans;
+    int                err;
 
-        err = asyncns_getaddrinfo_done (priv->asyncns_ctx, priv->resolv_query, &ans);
-        priv->resolv_query = NULL;
-        /* Signal that we are done */
+    err = asyncns_getaddrinfo_done (priv->asyncns_ctx, priv->resolv_query, &ans);
+    priv->resolv_query = NULL;
+    /* Signal that we are done */
 
-        g_object_ref (resolver);
+    g_object_ref (resolver);
 
-        if (err) {
-                _lm_resolver_set_result (resolver,
-                                         LM_RESOLVER_RESULT_FAILED, 
-                                         NULL);
-        } else {
-                _lm_resolver_set_result (resolver,
-                                         LM_RESOLVER_RESULT_OK,
-                                         ans);
-        }
+    if (err) {
+        _lm_resolver_set_result (resolver,
+                                 LM_RESOLVER_RESULT_FAILED, 
+                                 NULL);
+    } else {
+        _lm_resolver_set_result (resolver,
+                                 LM_RESOLVER_RESULT_OK,
+                                 ans);
+    }
 
-        asyncns_resolver_cleanup (resolver);
+    asyncns_resolver_cleanup (resolver);
 
-        g_object_unref (resolver);
+    g_object_unref (resolver);
 }
 
 typedef gboolean  (* LmAsyncnsResolverCallback) (LmResolver *resolver);
@@ -144,213 +144,213 @@
                         GIOCondition  condition,
                         LmResolver   *resolver)
 {
-        LmAsyncnsResolverPriv     *priv = GET_PRIV (resolver);
-        LmAsyncnsResolverCallback  func;
+    LmAsyncnsResolverPriv     *priv = GET_PRIV (resolver);
+    LmAsyncnsResolverCallback  func;
 
-        asyncns_wait (priv->asyncns_ctx, FALSE);
+    asyncns_wait (priv->asyncns_ctx, FALSE);
 
-        if (!asyncns_isdone (priv->asyncns_ctx, priv->resolv_query)) {
-                return TRUE;
-        }
+    if (!asyncns_isdone (priv->asyncns_ctx, priv->resolv_query)) {
+        return TRUE;
+    }
 
-        func = (LmAsyncnsResolverCallback) asyncns_getuserdata (priv->asyncns_ctx,
-                                                                priv->resolv_query);
-        return func (resolver);
+    func = (LmAsyncnsResolverCallback) asyncns_getuserdata (priv->asyncns_ctx,
+                                                            priv->resolv_query);
+    return func (resolver);
 }
 
 static gboolean
 asyncns_resolver_prep (LmResolver *resolver, GError **error)
 {
-        LmAsyncnsResolverPriv *priv = GET_PRIV (resolver);
-        GMainContext          *context;
+    LmAsyncnsResolverPriv *priv = GET_PRIV (resolver);
+    GMainContext          *context;
 
-        if (priv->asyncns_ctx) {
-                return TRUE;
-	}
+    if (priv->asyncns_ctx) {
+        return TRUE;
+    }
 
-        priv->asyncns_ctx = asyncns_new (1);
-	if (priv->asyncns_ctx == NULL) {
-                g_set_error (error,
-                             LM_ERROR,                 
-                             LM_ERROR_CONNECTION_FAILED,   
-                             "can't initialise libasyncns");
-		return FALSE;
-	}
+    priv->asyncns_ctx = asyncns_new (1);
+    if (priv->asyncns_ctx == NULL) {
+        g_set_error (error,
+                     LM_ERROR,                 
+                     LM_ERROR_CONNECTION_FAILED,   
+                     "can't initialise libasyncns");
+        return FALSE;
+    }
 
-        priv->resolv_channel =
-                g_io_channel_unix_new (asyncns_fd (priv->asyncns_ctx));
+    priv->resolv_channel =
+        g_io_channel_unix_new (asyncns_fd (priv->asyncns_ctx));
 
-        g_object_get (resolver, "context", &context, NULL);
+    g_object_get (resolver, "context", &context, NULL);
         
-        g_print ("Setting up io watch\n");
+    g_print ("Setting up io watch\n");
 
-        priv->watch_resolv = 
-                lm_misc_add_io_watch (context,
-                                      priv->resolv_channel,
-				      G_IO_IN,
-                                      (GIOFunc) asyncns_resolver_io_cb,
-                                      resolver);
+    priv->watch_resolv = 
+        lm_misc_add_io_watch (context,
+                              priv->resolv_channel,
+                              G_IO_IN,
+                              (GIOFunc) asyncns_resolver_io_cb,
+                              resolver);
 
-	return TRUE;
+    return TRUE;
 }
 
 static void
 asyncns_resolver_lookup_host (LmResolver *resolver)
 {
-        LmAsyncnsResolverPriv *priv = GET_PRIV (resolver);
-        gchar               *host;
-        struct addrinfo      req;
+    LmAsyncnsResolverPriv *priv = GET_PRIV (resolver);
+    gchar               *host;
+    struct addrinfo      req;
 
-        g_object_get (resolver, "host", &host, NULL);
+    g_object_get (resolver, "host", &host, NULL);
 
-	memset (&req, 0, sizeof(req));
-	req.ai_family   = AF_UNSPEC;
-	req.ai_socktype = SOCK_STREAM;
-	req.ai_protocol = IPPROTO_TCP;
+    memset (&req, 0, sizeof(req));
+    req.ai_family   = AF_UNSPEC;
+    req.ai_socktype = SOCK_STREAM;
+    req.ai_protocol = IPPROTO_TCP;
 
-	if (!asyncns_resolver_prep (resolver, NULL)) {
-                g_warning ("Signal error\n");
-		return;
-        }
+    if (!asyncns_resolver_prep (resolver, NULL)) {
+        g_warning ("Signal error\n");
+        return;
+    }
 
-        priv->resolv_query =
-	  	asyncns_getaddrinfo (priv->asyncns_ctx,
-                                     host,
-				     NULL,
-				     &req);
+    priv->resolv_query =
+        asyncns_getaddrinfo (priv->asyncns_ctx,
+                             host,
+                             NULL,
+                             &req);
 
-	asyncns_setuserdata (priv->asyncns_ctx,
-                             priv->resolv_query,
-			     (gpointer) asyncns_resolver_done);
+    asyncns_setuserdata (priv->asyncns_ctx,
+                         priv->resolv_query,
+                         (gpointer) asyncns_resolver_done);
 }
 
 static void
 asyncns_resolver_srv_done (LmResolver *resolver)
 {
-        LmAsyncnsResolverPriv *priv = GET_PRIV (resolver);
-        unsigned char         *srv_ans;
-	int 		       srv_len;
-        gboolean               result = FALSE;
+    LmAsyncnsResolverPriv *priv = GET_PRIV (resolver);
+    unsigned char         *srv_ans;
+    int                srv_len;
+    gboolean               result = FALSE;
 
-        g_print ("srv_done callback\n");
+    g_print ("srv_done callback\n");
 
-        srv_len = asyncns_res_done (priv->asyncns_ctx, 
-                                    priv->resolv_query, &srv_ans);
+    srv_len = asyncns_res_done (priv->asyncns_ctx, 
+                                priv->resolv_query, &srv_ans);
 
-        priv->resolv_query = NULL;
+    priv->resolv_query = NULL;
 
-        if (srv_len <= 0) {
-                /* FIXME: Report error */
-                g_warning ("Failed to read srv request results");
-        } else {
-                gchar *new_server;
-                guint  new_port;
+    if (srv_len <= 0) {
+        /* FIXME: Report error */
+        g_warning ("Failed to read srv request results");
+    } else {
+        gchar *new_server;
+        guint  new_port;
 
-                g_print ("trying to parse srv response\n");
+        g_print ("trying to parse srv response\n");
 
-                result = _lm_resolver_parse_srv_response (srv_ans, srv_len,
-                                                          &new_server,
-                                                          &new_port);
-                if (result == TRUE) {
-                        g_print ("worked, new host/post is %s/%d\n",
-                                 new_server, new_port);
+        result = _lm_resolver_parse_srv_response (srv_ans, srv_len,
+                                                  &new_server,
+                                                  &new_port);
+        if (result == TRUE) {
+            g_print ("worked, new host/post is %s/%d\n",
+                     new_server, new_port);
 
-                        g_object_set (resolver,
-                                      "host", new_server,
-                                      "port", new_port,
-                                      NULL);
-                }
-
-                g_free (new_server);
-                /* TODO: Check whether srv_ans needs freeing */
+            g_object_set (resolver,
+                          "host", new_server,
+                          "port", new_port,
+                          NULL);
         }
 
-        asyncns_resolver_cleanup (resolver);
+        g_free (new_server);
+        /* TODO: Check whether srv_ans needs freeing */
+    }
 
-        if (result == TRUE) {
-                asyncns_resolver_lookup_host (resolver);
-        }
+    asyncns_resolver_cleanup (resolver);
+
+    if (result == TRUE) {
+        asyncns_resolver_lookup_host (resolver);
+    }
 }
 
 static void
 asyncns_resolver_lookup_service (LmResolver *resolver)
 {
-        LmAsyncnsResolverPriv *priv = GET_PRIV (resolver);
-        gchar                 *domain;
-        gchar                 *service;
-        gchar                 *protocol;
-        gchar                 *srv;
+    LmAsyncnsResolverPriv *priv = GET_PRIV (resolver);
+    gchar                 *domain;
+    gchar                 *service;
+    gchar                 *protocol;
+    gchar                 *srv;
 
 
-        g_object_get (resolver,
-                      "domain", &domain,
-                      "service", &service,
-                      "protocol", &protocol,
-                      NULL);
+    g_object_get (resolver,
+                  "domain", &domain,
+                  "service", &service,
+                  "protocol", &protocol,
+                  NULL);
         
-        srv = _lm_resolver_create_srv_string (domain, service, protocol);
+    srv = _lm_resolver_create_srv_string (domain, service, protocol);
         
-        g_print ("Looking up service: %s %s %s\n[%s]", domain, service, protocol, srv);
+    g_print ("Looking up service: %s %s %s\n[%s]", domain, service, protocol, srv);
 
-        if (!asyncns_resolver_prep (resolver, /* Use GError? */ NULL)) {
-                g_warning ("Failed to initiate the asyncns library");
-                /* FIXME: Signal error */
-                return;
-        }
+    if (!asyncns_resolver_prep (resolver, /* Use GError? */ NULL)) {
+        g_warning ("Failed to initiate the asyncns library");
+        /* FIXME: Signal error */
+        return;
+    }
 
-        priv->resolv_query =
-                asyncns_res_query (priv->asyncns_ctx, srv, C_IN, T_SRV);
+    priv->resolv_query =
+        asyncns_res_query (priv->asyncns_ctx, srv, C_IN, T_SRV);
         
-        asyncns_setuserdata (priv->asyncns_ctx, 
-                             priv->resolv_query, 
-                             (gpointer) asyncns_resolver_srv_done);
+    asyncns_setuserdata (priv->asyncns_ctx, 
+                         priv->resolv_query, 
+                         (gpointer) asyncns_resolver_srv_done);
 
-        g_free (srv);
-        g_free (domain);
-        g_free (service);
-        g_free (protocol);
+    g_free (srv);
+    g_free (domain);
+    g_free (service);
+    g_free (protocol);
 }
 
 static void
 asyncns_resolver_lookup (LmResolver *resolver)
 {
-        gint type;
+    gint type;
 
-        /* Start the DNS querying */
+    /* Start the DNS querying */
 
-        /* Decide if we are going to lookup a srv or host */
-        g_object_get (resolver, "type", &type, NULL);
+    /* Decide if we are going to lookup a srv or host */
+    g_object_get (resolver, "type", &type, NULL);
 
-        switch (type) {
-        case LM_RESOLVER_HOST:
-                asyncns_resolver_lookup_host (resolver);
-                break;
-        case LM_RESOLVER_SRV:
-                asyncns_resolver_lookup_service (resolver);
-                break;
-        };
+    switch (type) {
+    case LM_RESOLVER_HOST:
+        asyncns_resolver_lookup_host (resolver);
+        break;
+    case LM_RESOLVER_SRV:
+        asyncns_resolver_lookup_service (resolver);
+        break;
+    };
 
-        /* End of DNS querying */
+    /* End of DNS querying */
 } 
 
 static void
 asyncns_resolver_cancel (LmResolver *resolver)
 {
-        LmAsyncnsResolverPriv *priv;
+    LmAsyncnsResolverPriv *priv;
 
-        g_return_if_fail (LM_IS_ASYNCNS_RESOLVER (resolver));
+    g_return_if_fail (LM_IS_ASYNCNS_RESOLVER (resolver));
 
-        priv = GET_PRIV (resolver);
+    priv = GET_PRIV (resolver);
 
-        if (priv->asyncns_ctx) {
-                if (priv->resolv_query) {
-                        asyncns_cancel (priv->asyncns_ctx, priv->resolv_query);
-                        priv->resolv_query = NULL;
-                }
+    if (priv->asyncns_ctx) {
+        if (priv->resolv_query) {
+            asyncns_cancel (priv->asyncns_ctx, priv->resolv_query);
+            priv->resolv_query = NULL;
+        }
 
-                _lm_resolver_set_result (resolver,
-                                         LM_RESOLVER_RESULT_CANCELLED,
-                                         NULL);
-        }
+        _lm_resolver_set_result (resolver,
+                                 LM_RESOLVER_RESULT_CANCELLED,
+                                 NULL);
+    }
 }