Subject: Re: Bind to local iface: bug or feautre.
To: Jesus M. Gonzalez <jgb@gsyc.inf.uc3m.es>
From: Assar Westerlund <assar@sics.se>
List: tech-net
Date: 11/25/1996 14:39:27
"Jesus M. Gonzalez" <jgb@gsyc.inf.uc3m.es> writes:
> Hi!
Hola
> 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?
It's not a bug, or perhaps it is. The sockaddr_in needs to zeroed out
because a bit-by-bit comparison is done in `in_pcbbind' with
`ifa_ifwithaddr'
/assar
> #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. */
memset(&name, 0, sizeof(name));
> 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);
> }