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.