gweb: Make debug for write simpler
[framework/connectivity/connman.git] / gweb / gweb.c
index f3c8a7e..1183cc6 100644 (file)
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
+#include <netdb.h>
 
 #include "giognutls.h"
 #include "gresolv.h"
@@ -63,6 +64,7 @@ struct web_session {
        char *host;
        uint16_t port;
        unsigned long flags;
+       struct addrinfo *addr;
 
        char *content_type;
 
@@ -168,6 +170,9 @@ static void free_session(struct web_session *session)
 
        g_free(session->host);
        g_free(session->address);
+       if (session->addr != NULL)
+               freeaddrinfo(session->addr);
+
        g_free(session);
 }
 
@@ -424,13 +429,11 @@ static gboolean process_send_buffer(struct web_session *session)
                return FALSE;
        }
 
-       debug(session->web, "bytes to write %zu", count);
-
        status = g_io_channel_write_chars(session->transport_channel,
                                        buf->str, count, &bytes_written, NULL);
 
-       debug(session->web, "status %u bytes written %zu",
-                                       status, bytes_written);
+       debug(session->web, "status %u bytes to write %zu bytes written %zu",
+                                       status, count, bytes_written);
 
        if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN)
                return FALSE;
@@ -881,10 +884,9 @@ static gboolean received_data(GIOChannel *channel, GIOCondition cond,
 static int connect_session_transport(struct web_session *session)
 {
        GIOFlags flags;
-       struct sockaddr_in sin;
        int sk;
 
-       sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+       sk = socket(session->addr->ai_family, SOCK_STREAM, IPPROTO_TCP);
        if (sk < 0)
                return -EIO;
 
@@ -910,12 +912,8 @@ static int connect_session_transport(struct web_session *session)
 
        g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
 
-       memset(&sin, 0, sizeof(sin));
-       sin.sin_family = AF_INET;
-       sin.sin_port = htons(session->port);
-       sin.sin_addr.s_addr = inet_addr(session->address);
-
-       if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
+       if (connect(sk, session->addr->ai_addr,
+                       session->addr->ai_addrlen) < 0) {
                if (errno != EINPROGRESS) {
                        close(sk);
                        return -EIO;
@@ -1048,6 +1046,9 @@ static void resolv_result(GResolvResultStatus status,
                                        char **results, gpointer user_data)
 {
        struct web_session *session = user_data;
+       struct addrinfo hints;
+       char *port;
+       int ret;
 
        if (results == NULL || results[0] == NULL) {
                call_result_func(session, 404);
@@ -1056,7 +1057,18 @@ static void resolv_result(GResolvResultStatus status,
 
        debug(session->web, "address %s", results[0]);
 
-       if (inet_aton(results[0], NULL) == 0) {
+       memset(&hints, 0, sizeof(struct addrinfo));
+       hints.ai_flags = AI_NUMERICHOST;
+
+       if (session->addr != NULL) {
+               freeaddrinfo(session->addr);
+               session->addr = NULL;
+       }
+
+       port = g_strdup_printf("%u", session->port);
+       ret = getaddrinfo(results[0], port, &hints, &session->addr);
+       g_free(port);
+       if (ret != 0 || session->addr == NULL) {
                call_result_func(session, 400);
                return;
        }
@@ -1134,9 +1146,30 @@ static guint do_request(GWeb *web, const char *url,
                        return 0;
                }
        } else {
+               struct addrinfo hints;
+               char *port;
+               int ret;
+
                if (session->address == NULL)
                        session->address = g_strdup(session->host);
 
+               memset(&hints, 0, sizeof(struct addrinfo));
+               hints.ai_flags = AI_NUMERICHOST;
+
+               if (session->addr != NULL) {
+                       freeaddrinfo(session->addr);
+                       session->addr = NULL;
+               }
+
+               port = g_strdup_printf("%u", session->port);
+               ret = getaddrinfo(session->address, port, &hints,
+                                                       &session->addr);
+               g_free(port);
+               if (ret != 0 || session->addr == NULL) {
+                       free_session(session);
+                       return 0;
+               }
+
                if (create_transport(session) < 0) {
                        free_session(session);
                        return 0;