gweb: Bind socket to the right interface
authorTomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
Fri, 25 Nov 2011 11:03:03 +0000 (13:03 +0200)
committerSamuel Ortiz <sameo@linux.intel.com>
Mon, 28 Nov 2011 14:55:21 +0000 (15:55 +0100)
gweb/gweb.c

index 5c3305e..1461346 100644 (file)
@@ -32,6 +32,7 @@
 #include <sys/socket.h>
 #include <arpa/inet.h>
 #include <netdb.h>
+#include <net/if.h>
 
 #include "giognutls.h"
 #include "gresolv.h"
@@ -910,6 +911,22 @@ static int connect_session_transport(struct web_session *session)
        if (sk < 0)
                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 (session->flags & SESSION_FLAG_USE_TLS) {
                debug(session->web, "using TLS encryption");
                session->transport_channel = g_io_channel_gnutls_new(sk);