Cannot connect() setting SO_BINDTODEVICE to another interface (2.4.17 / libc6 2.2.4)

From: Tuxisuau (tuxisuau@7a69ezine.org)
Date: Sun Dec 30 2001 - 07:09:09 EST


Hi.

I've been trying to code some client program that allow me to specify
the interface i want to connect through. I've tried to do something
like:

setsockopt(ifd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)+1);

Ok, it worked perfecly for raw sockets in some experiment i did in the
past... only sending.
But when i try to do that with a TCP socket, the syn is sent and the syn
& ack received in the correct interface. But then connection does not
continue.
The connection completes perfecly if the interface i use is the default route.
Then I tried to bind the source adress before, etc without good results.

I'm using libc6 2.2.4-7 Debian Unstable package, and kernel 2.4.17.

Here's a example:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int main()
{
        int ifd;
        char *interface;
        struct sockaddr_in server,client;

        interface = (char *)malloc(4);
        strncpy(interface, "ppp0\0",5);

        server.sin_family = AF_INET;
        server.sin_port = htons(21);
        server.sin_addr.s_addr = inet_addr("10.0.0.1"); /* ftp server address */
        bzero(&(server.sin_zero),8);

        client.sin_port = htons(0);
        client.sin_addr.s_addr = inet_addr("172.16.0.1"); /* my ppp0 address */

        if((ifd = socket(AF_INET, SOCK_STREAM, 0))==-1)
        {
                perror("socket()");
                exit(-1);
        }
        if((setsockopt(ifd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)+1))==-1)
        {
                perror("setsockopt() SO_BINDTODEVICE");
                exit(-1);
        }

        if((bind(ifd, (struct sockaddr *)&client,sizeof(struct sockaddr)))==-1) /* it's really required? */
        {
                perror("bind()");
                exit(-1);
        }

        if((connect(ifd, (struct sockaddr *)&server,sizeof(struct sockaddr)))==-1)
        {
                perror("connect()");
                exit(-1);
        }

        return 0;
}

-- 
tuxisuau. tuxisuau@7a69ezine.org
http://tuxisuau.7a69ezine.org

"How I need a drink, alcoholic of course, after the heavy chapters involving quantics mechanics" George Polya


- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Mon Dec 31 2001 - 21:00:22 EST