*
* Web service library with GLib integration
*
- * Copyright (C) 2009-2010 Intel Corporation. All rights reserved.
+ * Copyright (C) 2009-2012 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
#include <netdb.h>
#include <net/if.h>
#include <netinet/tcp.h>
+#include <ifaddrs.h>
#include "giognutls.h"
#include "gresolv.h"
GWebResult result;
GWebResultFunc result_func;
+ GWebRouteFunc route_func;
GWebInputFunc input_func;
int fd;
gsize length;
g_free(web);
}
+gboolean g_web_supports_tls(void)
+{
+ return g_io_channel_supports_tls();
+}
+
void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
{
if (web == NULL)
result == TRUE ? "continue" : "stop");
}
+static inline void call_route_func(struct web_session *session)
+{
+ if (session->route_func != NULL)
+ session->route_func(session->address, session->addr->ai_family,
+ session->web->index, session->user_data);
+}
+
static gboolean process_send_buffer(struct web_session *session)
{
GString *buf;
return TRUE;
}
+static int bind_to_address(int sk, const char *interface, int family)
+{
+ struct ifaddrs *ifaddr_list, *ifaddr;
+ int size, err = -1;
+
+ if (getifaddrs(&ifaddr_list) < 0)
+ return err;
+
+ for (ifaddr = ifaddr_list; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
+ if (g_strcmp0(ifaddr->ifa_name, interface) != 0)
+ continue;
+
+ if (ifaddr->ifa_addr == NULL ||
+ ifaddr->ifa_addr->sa_family != family)
+ continue;
+
+ switch (family) {
+ case AF_INET:
+ size = sizeof(struct sockaddr_in);
+ break;
+ case AF_INET6:
+ size = sizeof(struct sockaddr_in6);
+ break;
+ default:
+ continue;
+ }
+
+ err = bind(sk, (struct sockaddr *) ifaddr->ifa_addr, size);
+ break;
+ }
+
+ freeifaddrs(ifaddr_list);
+ return err;
+}
+
+static inline int bind_socket(int sk, int index, int family)
+{
+ char interface[IF_NAMESIZE];
+ int err;
+
+ if (if_indextoname(index, interface) == NULL)
+ return -1;
+
+ err = setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
+ interface, IF_NAMESIZE);
+ if (err < 0)
+ err = bind_to_address(sk, interface, family);
+
+ return err;
+}
+
static int connect_session_transport(struct web_session *session)
{
GIOFlags flags;
return -EIO;
if (session->web->index > 0) {
- char interface[IF_NAMESIZE];
-
- memset(interface, 0, IF_NAMESIZE);
-
- if (if_indextoname(session->web->index, interface) != NULL) {
- if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
- interface, IF_NAMESIZE) < 0) {
- close(sk);
- return -EIO;
- }
-
- debug(session->web, "Use interface %s", interface);
+ if (bind_socket(sk, session->web->index,
+ session->addr->ai_family) < 0) {
+ debug(session->web, "bind() %s", strerror(errno));
+ close(sk);
+ return -EIO;
}
}
}
if (session->transport_channel == NULL) {
+ debug(session->web, "channel missing");
close(sk);
return -ENOMEM;
}
if (connect(sk, session->addr->ai_addr,
session->addr->ai_addrlen) < 0) {
if (errno != EINPROGRESS) {
+ debug(session->web, "connect() %s", strerror(errno));
close(sk);
return -EIO;
}
}
session->address = g_strdup(results[0]);
+ call_route_func(session);
if (create_transport(session) < 0) {
call_result_func(session, 409);
static guint do_request(GWeb *web, const char *url,
const char *type, GWebInputFunc input,
int fd, gsize length, GWebResultFunc func,
- gpointer user_data)
+ GWebRouteFunc route, gpointer user_data)
{
struct web_session *session;
session->web = web;
session->result_func = func;
+ session->route_func = route;
session->input_func = input;
session->fd = fd;
session->length = length;
return web->next_query_id++;
}
-guint g_web_request_get(GWeb *web, const char *url,
- GWebResultFunc func, gpointer user_data)
+guint g_web_request_get(GWeb *web, const char *url, GWebResultFunc func,
+ GWebRouteFunc route, gpointer user_data)
{
- return do_request(web, url, NULL, NULL, -1, 0, func, user_data);
+ return do_request(web, url, NULL, NULL, -1, 0, func, route, user_data);
}
guint g_web_request_post(GWeb *web, const char *url,
const char *type, GWebInputFunc input,
GWebResultFunc func, gpointer user_data)
{
- return do_request(web, url, type, input, -1, 0, func, user_data);
+ return do_request(web, url, type, input, -1, 0, func, NULL, user_data);
}
guint g_web_request_post_file(GWeb *web, const char *url,
if (fd < 0)
return 0;
- ret = do_request(web, url, type, NULL, fd, st.st_size, func, user_data);
+ ret = do_request(web, url, type, NULL, fd, st.st_size, func, NULL,
+ user_data);
if (ret == 0)
close(fd);