Subject: Bind to local iface: bug or feautre.
To: None <tech-net@NetBSD.ORG>
From: Jesus M. Gonzalez <jgb@gsyc.inf.uc3m.es>
List: tech-net
Date: 11/25/1996 14:05:31
Hi!
NetBSD returns an error when binding an UDP address where
the host address matchs a local iface (instead of the more used
INADDR_ANY). I'm not familar with the networking code,
and I'm not sure if this is a bug or a feature... Could somebody
tell me, so that I send-pr it if it is a bug?
Jesus.
PS: Next, the code which causes EADDRNOTAVAIL. I think it should just
work. Was tested on NetBSD-1.1/i386 and NetBSD-1.2/i386.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
main()
{
int sock, length;
struct sockaddr_in name;
char buf[1024];
/* Create socket from which to read. */
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("opening datagram socket");
exit(1);
}
/* Create name with wildcards. */
name.sin_family = AF_INET;
name.sin_addr.s_addr = inet_addr("163.117.137.172"); /* Here your addr */
name.sin_port = 0;
if (bind(sock, (struct sockaddr *) &name, sizeof(name))) {
perror("binding datagram socket");
exit(1);
}
/* Find assigned port value and print it out. */
length = sizeof(name);
if (getsockname(sock, (struct sockaddr *) &name, &length)) {
perror("getting socket name");
exit(1);
}
close(sock);
}
--
Jesus M. Gonzalez Barahona | addr.: c/ Butarque, 15
Grupo de Sistemas y Comunicaciones | 28911 Leganes, Spain
Departamento de Informatica | tel: +34 1 624 94 58
Universidad Carlos III de Madrid | fax: +34 1 624 94 30
e-mail: jgb@gsyc.inf.uc3m.es | www: http://ordago.gsyc.inf.uc3m.es/~jgb