Hi,
I have a problem that is similar to the problem described in 
http://www.freebsd.org/cgi/query-pr.cgi?pr=kern/22868
My program, that opens a tcp socket and binds to INADDR_ANY:0, 
getsockname() returns either the correct answer
or, for some time, a wrong answer, see below
(this is of course not my program, just some code that does the same)
--- snipp ---
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
{
     int sock, len;
     struct sockaddr_in addr, foo;
     if((sock=socket(AF_INET, SOCK_STREAM, 0))<0)
     {
         exit(0);
     }
     memset(&addr, 0, sizeof(struct sockaddr_in));
     addr.sin_family = AF_INET;
     addr.sin_addr.s_addr = INADDR_ANY;
     addr.sin_port = htons(0);
     if(bind(sock, (struct sockaddr *) &addr, sizeof(struct 
sockaddr_in))<0)
     {
         perror("bind");
         exit(0);
     }
     if(listen(sock, 5)<0)
     {
         perror("listen");
         exit(0);
     }
     getsockname(sock, (struct sockaddr *) &foo, &len);
     fprintf(stderr, "listening on %s:%d\n", inet_ntoa(foo.sin_addr), 
ntohs(foo.sin_port));
     return 0;
}
--- snap ---
$ ./a.out
listening on 86.186.4.40:49087
$ ./a.out
listening on 0.0.0.0:3810
regards,
Chris
PS: It is even stranger than I thought, I compiled this program: gcc -o 
freebsd freebsd.c and ran it serveral times:
  $ ./freebsd
listening on 86.186.4.40:49087
  $ mv freebsd a.out
  $ ./a.out
listening on 0.0.0.0:4364
  $ mv a.out freebsd
  $ ./freebsd
listening on 86.186.4.40:49087
  $ mv freebsd a.out
  $ ./a.out
listening on 0.0.0.0:4604
> (this is of course not my program, just some code that does the same) > > --- snipp --- > #include <stdio.h> > #include <sys/types.h> > #include <sys/socket.h> > #include <string.h> > #include <stdlib.h> > #include <netinet/in.h> > #include <arpa/inet.h> > > int main() > { > int sock, len;Okay, you haven't set the value of 'len'.> struct sockaddr_in addr, foo; > > if((sock=socket(AF_INET, SOCK_STREAM, 0))<0) > { > exit(0); > } > > memset(&addr, 0, sizeof(struct sockaddr_in)); > addr.sin_family = AF_INET; > addr.sin_addr.s_addr = INADDR_ANY; > addr.sin_port = htons(0); > > if(bind(sock, (struct sockaddr *) &addr, sizeof(struct > sockaddr_in))<0) > { > perror("bind"); > exit(0); > } > > if(listen(sock, 5)<0) > { > perror("listen"); > exit(0); > } > > getsockname(sock, (struct sockaddr *) &foo, &len);Hmm, you still haven't set the value of 'len'. And you ignore the return value of 'getsockname'.> fprintf(stderr, "listening on %s:%d\n", inet_ntoa(foo.sin_addr), > ntohs(foo.sin_port)); > > return 0; > }> $ ./a.out > listening on 86.186.4.40:49087 > $ ./a.out > listening on 0.0.0.0:3810Fix those two bugs and see if you still have a problem. DS
Christian Klein wrote:> > Am Mittwoch, 07.01.04, um 23:02 Uhr (Europe/Berlin) schrieb Lev Walkin: > >> len = sizeof(*foo); > > > Oh, thanks a lot! > I should have read the manual more careful! > I thought then len parameter would be filled by getsockname().It is. But its original value is being used too. -- Lev Walkin vlm@netli.com