Re: Perl socket communications problem

Top Page
Attachments:
Message as email
+ (text/plain)
+ (text/html)
Delete this message
Reply to this message
Author: Joseph Sinclair
Date:  
To: plug-discuss
Subject: Re: Perl socket communications problem
This is an ideal question for the PLUG-Devel list, I'll answer here for
convenience, however.

Ben,
    I may be missing something here, as I am not a Perl expert, but 
usually, when the accept function is called for a TCP socket, the 
accepted socket continues to communicate on the same port (in your 
example, on port 4567).  There can only be one connection listener on a 
given port, but there can be multiple active "conversation" sockets (up 
to 65535, represented in your code by SOMAXCONN) on that same port.  TCP 
handles the multiplexing of the individual sockets using a unique socket 
identifier sent in the packet structure, and the TCP stack in most 
systems (including Linux and the BSD's) maintains the server state 
required to route the data to the various threads handling those sockets 
as data streams.  Unless Perl is doing something I'm completely unaware 
of, the only port you need to worry about in your firewall would be 4567 
using the code listed below (minus the client port part).


That said, It looks like, in the code snippet below, you're trying to
initiate an additional socket back to the client using a client port
value, is this deliberate? If so, then you just need to do a quick
check of the client port, and send an error on the initial socket
indicating an acceptable port range. The client software would then
need to handle that error by selecting a port within the acceptable range.

==Joseph++

Ben Weatherall wrote:

> This is probably somewhat off-topic, but I thought some of the members
> of this group would be able to assist me. If nothing else, point me to
> the correct lists for help!
>
> I am working on a daemon in perl that accepts internet style socket
> connections on a specific port. So far, no problems. It then does an
> 'accept' so that it can free up the listening socket for further
> connection requests. Again, no problem... except that I need to be
> able to tell it what range of ports to "map" the subsequent
> communications to. If I listen to port 4567, when I do the 'accept' it
> will transfer further communications to 1039, or maybe 3276, or 59236,
> or... You get the idea. Due to firewall issues, I need to limit the
> ports 'accept' will use to something like 4568 - 4599. Any ideas?
>
> See the code snippet below:
>
> my ($name, $aliases, $proto);
> ($name, $aliases, $proto) = getprotobyname('tcp');
> my $mypaddr = pack($sockaddr, $AF_INET, $port, "\0\0\0\0");
> select(SOCK); $| = 1;select(STDOUT);
> socket(SERVER, $AF_INET, $SOCK_STREAM, $proto)      || die "socket: 
> $!\n";
> bind(SERVER, $mypaddr)                              || die "bind: $!\n";
> listen(SERVER, SOMAXCONN)                           || die "listen: 
> $!\n";
> while($client_address = accept(SOCK, SERVER)) {
>     my ($client_port, $client_packed_ip) = sockaddr_in($client_address);
>     # Communications to client proceeds using client_port
> }

>
>
> Thanks in advance,
> -Ben
>
>