0

I am trying to bind a socket to a particular network interface on my computer. I have two network interfaces named interf0 and interf1. I want bind socket to a particular interface say interf0. My OS is vxWorks 6.2.

I am trying following code:

    struct sockaddr_in fromAddr;
    struct sockaddr_in sin;
    int fromLen;
    struct ip_mreq ipMreq;
    int sockDesc;
    STATUS temp;

    if ((sockDesc = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        printf (" cannot open recv socket\n");
        return ERROR;
    }
    bzero ((char *)&sin, sizeof (sin));
    bzero ((char *) &fromAddr, sizeof(fromAddr));
    fromLen = sizeof(fromAddr);

#if 1
    if ((temp = setsockopt(sockDesc, SOL_SOCKET, SO_BINDTODEVICE, "interf0", 7)) < 0)
    {
        perror("Server-setsockopt() error for SO_BINDTODEVICE");
        printf("%s\n", strerror(errno));
        close(sockDesc);
        return ERROR;
    }
#endif

    sin.sin_len = (u_char) sizeof(sin);
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = INADDR_ANY;
    //sin.sin_addr.s_addr = inet_addr(ifAddr);
    /* UDP port number to match for the received packets */
    sin.sin_port = htons (mcastPort);
    /* bind a port number to the socket */
    if (bind(sockDesc, (struct sockaddr *)&sin, sizeof(sin)) != 0)
    {
        perror("bind");
        if (sockDesc != ERROR)
        {
            close (sockDesc);
        }
        return ERROR;
    }

Here, it gives an error saying SO_BINDTODEVICE is not defined.

Is there any other way using which I can bind to a particular interface in vxWorks.

Other ref:
bind socket to network interface

Thank you.

Community
  • 1
  • 1
Jay
  • 1,210
  • 9
  • 28
  • 48
  • ifAddr in above code is IP address of interface in dot notation. However, the same did not work alone. (i.e. with setsockopt commented) – Jay May 09 '17 at 05:59
  • Have you tried using SO_OUTIF in vxWorks? We have used this to setup outbound interface in vxWorks 5.5. – Vikash Jain May 12 '17 at 18:49

1 Answers1

0

It seems that SO_BINDTODEVICE is not part of POSIX, it's a Linux extension. So VxWorks won't necessarily implement interface binding the same way, if it does it at all. A quick look in the VxWorks manuals looks unpromising.

If you have VxWorks you likely also have access to WindRiver's support, assuming you've kept up with the support fees. If so, ask them too, that's what it's there for.

bazza
  • 7,580
  • 15
  • 22