Re: More allocating portnumbers....

From: ZoombyWoof (zoombywoofremove_at_thishotmail.com)
Date: 07/04/04


Date: Sun, 04 Jul 2004 21:47:50 +0200

Kasper Dupont wrote:

> ZoombyWoof wrote:
>
>>HI all again. I have now come to the point where my programs compile
>>fine but something weird is happening and I cant find out what.
>>
>>Process A creates a socket, binds it to port 0 and then I call
>>getsockname to retrieve the port allocated.
>
>
> I'm not sure you can actually bind to port 0. IIRC giving
> zero as the port number is interpreted as a request for the
> kernel to chose a free port number.
That is exactly what I want. I want the kernel to get me a free port
number and then retrieve it by a call to getsockname. I will then use
this portnumber and registrate it in a database so remote processes can
get this portnumber and connect to.
>
>
>>int allocPort( int *listensock )
>
>
> Why do you take a pointer as argument. Normally you would
> take no argument and return the filedescriptor (or -1 in
> case of an error).
allocPort is called by another function. Sure I could have had allocPort
return the filedescr, but I choose this method, it should work, and it
does. listensock is later on passed on to a created thread, and
listen/accept is called on it and it works. It's just that it doesnt
listen to the portnumber returned from getsockname, it listens to
another one....
>
>
>>{
>> stat = OpenSocket( listensock );
>
>
> Where is stat and OpenSocket declared? I think stat should
> have been a local variable.
OpenSocket is just e generic routine I use (It takes care of small
differences between win32 and linux). I didnt put out local variable
declarations in my example.
>
>
*snip*

>> CloseSocket( *listensock );
>> return( -1 );
>> }
>>
>> // Bind our adress
>> memset( (char*)&serv_addr, '\0', sizeof(serv_addr) );
>> serv_addr.sin_family = AF_INET;
>> serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
>> serv_addr.sin_port = 0;
>> if( (bind(*listensock, (struct sockaddr*)&serv_addr,
>>sizeof(serv_addr))) < 0 )
>> {
>> perror( "bind" );
>> sprintf( buf, "Error bind local address. errno = %d.\n", errno );
>> fprintf( stderr, buf );
>> CloseSocket( *listensock );
>> return( -1 );
>> }
>>
>> clilen = sizeof(cli_addr);
>> stat = getsockname( *listensock, (struct sockaddr*)&cli_addr, &clilen );
>> if( stat < 0 )
>> {
>> perror( "getsockname" );
>> sprintf( buf, "Error getsockname. errno = %d.\n", errno );
>> fprintf( stderr, buf );
>> CloseSocket( *listensock );
>> return( -1 );
>> }
>>}
>>
>>The port number returned in cli_addr is mostly around 61000 or
>>something, lets say 61228.
>
>
> Strange, my ip_local_port_range is 32768-61000. So why
> does it allocate something just outside that range?
well, thats what I want to know. When this program runs, the portnumber
returned in cli_addr is something else than what the process actually
listens too when I check with netstat.
>
>
>>I then issue a listen and accept call on the listensock and if I do a
>>netstat, I see that my process listens to 31015 or something like that.
>>If I telnet localhost 31015 my process accepts the connection. If I
>>telnet to the port getsockname returned for me, nothing happens.
>>
>>Why does my process listens and accepts on some other portnumber that
>>the socket descriptor was binded to ??
>
>
> I couldn't compile your code and test it. Some parts
> seems to be missing.
It wasnt complete. The important stuff was there just to show how I
called bind, getsockname etc.

/ZW
>



Relevant Pages

  • Re: Exchange Outgoing E-Mail
    ... Default Web Site is listening on Port 3200, ... However, by default, the Default Web Site listens on Port 80, SSL Port 443, ... please refer to the following link to install the Microsoft ... This newsgroup only focuses on SBS technical issues. ...
    (microsoft.public.windows.server.sbs)
  • Re: [opensuse] Still having problems with syslog-ng syntax
    ... It appears that in the case where we want syslog-ng to listen to a port, ... syslog-ng has a command that globally listens to 'anything' ...
    (SuSE)
  • Re: Please help interpret Sygate Personal Firewall traffic log (ndisuio.sys)
    ... the host listens on port 445 wirelessly? ... the host listens on port 445 wired? ... Given a bluetooth card, the host listens on port 445 bluetoothed? ...
    (comp.security.firewalls)
  • Re: Remote Web from home to office LAN
    ... If I change tie listensing port do I change from the default 3389 in the ... I have set several of the office machines up for RWC. ... The router needs to forward the requests properly to the machine you want. ... PC1 listens on port 3389 and the portis forwarded through to it. ...
    (microsoft.public.windowsxp.work_remotely)