SO_BINDTODEVICE

Hi,

System setup :

Beaglebone Black with Debian from SD card, Ethernet and Wifi. Cross compiling on Windows 7 machine.
Running program in debug mode via Eclipse (Win7) using gdb as debugger

Problem:
I am writing an application that requires ethernet and wifi (via usb) on the beaglebone black. My problem is that I need to point some TCP sockets to the Ethernet port but by default Linux os decides what is best so chooses wifi! So to get around this I use

struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), interface);

ioctl(TsocketId, SIOCGIFINDEX, &ifr);
error = setsockopt(socketId, SOL_SOCKET, SO_BINDTODEVICE, (void*)&ifr, sizeof(ifr));

However setsockopt always returns an error EPERM which is Operation Not Permitted (or similar). I have tried the simpler form of passing ‘eth0’ in, also with the same results.

After much reading on the internet it appears that I need extra rights to run this command.

I am struggling at this point

I have set the file permissions to include chmod u+s — made no difference
I have edited sudoers to giv**e admin users sudo privileges (I believe my debian user is one as now I don’t have to enter a password everytime I type sudo…)

# Members of the admin group may gain root privileges
%admin  ALL=(ALL) NOPASSWD:ALL

This also made no difference.

My application is running under the debian user

Any help much appreciated..

A P

Did you try to run your program as root, with su or sudo?
eg:
~ sudo yourprogram
or
~ su
~ yourprogram

A P, try running the program as root to find out if it works. If it does you may need to run sudo with the -S parameter. man sudo in any case.

Thank you both for you quick replies.

I am Running my program as debug via Eclipse

in the debug configuration there is a set up option for the command to run before the program, I have the line “sudo chmod u+s /home/debian/bin/test/myproj” here.

OK tried it running as Su

i.e root@beaglebone\ ./myproj

the executable runs but I am guessing the problem persists as it picks up the wifi IP address still

So this is telling me that SO_BINDTODEVICE does not work ??? or I am doing something else wrong - but everything that goes to the wifi is correct just the wrong device.

my code :

struct sockaddr_in addr;
char* interface = “eth0”;

if((socketId = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
currentConnection = -1;
//return 1;
}

struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), interface);

ioctl(.socketId, SIOCGIFINDEX, &ifr);
error = setsockoptsocketId, SOL_SOCKET, SO_BINDTODEVICE, (void*)&ifr, sizeof(ifr));

memset(&addr.addr, 0, sizeof(addr.addr));

addr.addr.sin_family = AF_INET;
addr.addr.sin_port = htons(TCP_DATA);

if (ipaddr != NULL)
{
if( inet_pton(AF_INET, ipaddr, &addr.addr.sin_addr) <=0 )

currentConnection = -1;

}

else
{
if(inet_pton(AF_INET, hostIP, &addr.addr.sin_addr)<=0)
currentConnection = -1;

}

if (currentConnection!=-1)
{
/* So that we can re-bind to it without TIME_WAIT problems */
setsockopt(socketId, SOL_SOCKET, SO_REUSEADDR, &addr.addr, sizeof(addr.addr));
setnonblocking(socketId);

status = connect(socketId, (struct sockaddr *)&addr.addr, sizeof(addr.addr));


}