Visit Our UNIX and Linux User Community


Bind() with SO_BINDTODEVICE returns errno 125


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Bind() with SO_BINDTODEVICE returns errno 125
# 1  
Old 05-30-2018
Bind() with SO_BINDTODEVICE returns errno 125

I am setting the socket option SO_BINDTODEVICE for eth0 to be able to route the packets only through that interface. However, bind() fails with "Port already in use" error with this option when the server is restarted despite having the socket option SO_REUSEADDR.

Here is my code snippet:

Code:
int listenFd;
    int temp;
    int buf_size = 0;
    struct sockaddr_in sAddr;
    int enable = 1;
    struct ifreq ifr;

    /* setup listening socket */
    listenFd = socket(AF_INET, SOCK_STREAM, 0);
    if (setsockopt(listenFd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
    {
       close (listenFd);
       return -1;
    }

    memset(&ifr, 0, sizeof(ifr));
    snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "eth0");
    ioctl(listenFd, SIOCGIFINDEX, &ifr);
    if (setsockopt(listenFd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, IFNAMSIZ) < 0)
    {
      close (listenFd);
      return -1;
    }

    /* Initialize the server address and bind to the required port */
    memset(&sAddr, 0, sizeof (struct sockaddr_in));
    sAddr.sin_family = AF_INET;
    sAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    sAddr.sin_port = 8080;

    /* bind to the socket, */
    temp = bind(listenFd, (struct sockaddr*) &sAddr, sizeof (sAddr));
    if (temp < 0)
    {
      close (listenFd);
      return -1;
    }

    /* Set socket options to max buffer size */
    buf_size = REST_MAX_HTTP_BUFFER_LENGTH;
    temp = setsockopt(listenFd, SOL_SOCKET, SO_RCVBUF, &buf_size, sizeof(buf_size));
    if (temp < 0)
    {
      close (listenFd);
      return -1;
    }

Is anything missing in the code above? I have tried the other way of getting IP address of the interface (eth0) and assigning it to sAddr.sin_addr.s_addr but that did not work.

Hope to get some details. Thanks in advance!!




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 05-30-2018 at 10:51 AM.. Reason: Added CODE tags.

Previous Thread | Next Thread
Test Your Knowledge in Computers #38
Difficulty: Easy
The standard port number for SMTP applications is 25.
True or False?

5 More Discussions You Might Find Interesting

1. AIX

Errno.h symbols

Hi, I need to look at a recent copy of /usr/include/errno.h from AIX 7.2 to check some symbols. In particular, I'm curious if it defines EOWNERDEAD and ENOTRECOVERABLE. Can someone who has access to 7.2 please check for me? Thanks! (1 Reply)
Discussion started by: topcat
1 Replies

2. Web Development

Getting access of my project kept in one 121.12.12.125 node

sir.. i am working in a WAN...... i am getting the login screen for an application by typing the address of the location and the file name "http:/121.12.12.111/xyz.html" from the node 121.12.12.125. now i had my web applicatioon at 121.12.12.125 for which i have taken the source code of the... (0 Replies)
Discussion started by: KANNI786
0 Replies

3. Programming

errno

Hey, Can I assume that for certain function calls, errno can never be set to a certain value. More specifically, can I assume that for if the stat function call fails, the errno can never be or "No space left on device." I am assuming that a read function cannot fail because of no space... (5 Replies)
Discussion started by: the_learner
5 Replies

4. Shell Programming and Scripting

#? 0,1,2,,,,,,,125 ?

The return value of the #? returns status of the executed command, i.e If it is 0 then SUCCESS, If it is non-zero value the it means FAILURE. My doubt is how does the return status defined? 1,2,3,,,,,125. Exp: if it returns 125,, how does it relates to failure? (1 Reply)
Discussion started by: praveen_b744
1 Replies

5. Programming

errno pb

Hello, I need to make a lib with pthread, when I run my make file all is good. But when I run my test program, I test errno in the begining and is already set to 251. Is it normal ??? What can I modify in my Makefile to have errno set to 0 ??? Thanks $make gcc -D_REENTRANT -shared -fpic... (3 Replies)
Discussion started by: dts
3 Replies

Featured Tech Videos