Re: [Libbt-devel] the port [was: header files]
Brought to you by:
ksmathers
From: Peter S. <stu...@cd...> - 2005-02-12 14:15:52
|
On Fri, Feb 11, 2005 at 01:38:11PM -0800, Elliott Mitchell wrote: > >From: Peter Stuge <stu...@cd...> > > > > No, listenport is not required, if we're going to do the networking > > in libbt I strongly suggest that we only use a single port per > > application, and by default one in the dynamic port range as defined > > by IANA, 49152 through 65536. Start from the bottom and try > > allocating upwards. > > It is standard practice for BT clients to start at 6881 and go up as > high as 6889. This is semi-silly, but it does make it easier to > implement MitM caches if so desired. It's completely silly, since some of those ports are reserved by IANA and some are unassigned. In either case it is really inappropriate to use them. > Failing that, taking whichever port the OS gives to you is standard > practice. This typically starts at 1024 and goes upwards as various > programs use temporary ports. For outgoing connections, yes. For incoming connections, no, not in Linux at least. I just tried; stuge@carepad4 ~/a $ cat a.c #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <netinet/in.h> int main() { int s,i; struct sockaddr_in sa; struct sockaddr *sap=(struct sockaddr *)&sa; s=socket(PF_INET,SOCK_STREAM,0); listen(s,5); getsockname(s,sap,&i); printf("got ip %s port %d\n",inet_ntoa(sa.sin_addr),ntohs(sa.sin_port)); close(s); return 0; } --8<-- ip(7) When listen(2) or connect(2) are called on a unbound socket the socket is automatically bound to a random free port with the local address set to INADDR_ANY. -->8-- stuge@carepad4 ~/a $ ./a got ip 0.0.0.0 port 57839 If I run it over and over again it steers clear of ports used by netfilter: stuge@carepad4 ~/a $ while :;do ./a;done [..] got ip 0.0.0.0 port 60999 got ip 0.0.0.0 port 61000 got ip 0.0.0.0 port 32768 got ip 0.0.0.0 port 32769 On Fri, Feb 11, 2005 at 01:47:34PM -0800, Tyler MacDonald wrote: > Elliott Mitchell <eh...@m5...> wrote: > > It is standard practice for BT clients to start at 6881 and go up as > > high as 6889. This is semi-silly, but it does make it easier to > > implement MitM caches if so desired. > > I vote for *NOT* using those ports by default. Bram Cohen has > said that it was a bad idea to begin with, It was. > That said, I think it would be nice to give the application > the chance to decide what port it wants to listen on, and maybe even > pass it's own socket fd to the library and say "here, use this"... > but if it doesnt take that chance, proceed with allocating a random > port either by not specifying one to bind, or by picking one in the > IANA range that's available. This is just a few lines of code we're discussing, but I still think it's important to get right. If user specifies port, try to use that. If it fails, give up and complain to user. If there's no user setting, try listen() and check that we got a port in the dynamic range. Linux can fail here, as shown above. If not, close the socket and make a new one. (How will this affect the IP stack? This isn't playing really nice with it, I can imagine that Windows would get upset by this kind of thing.) If there's still no good socket to accept() on, try bind()ing each port in the dynamic range. If that fails, give up and complain to user. On Fri, Feb 11, 2005 at 05:45:48PM -0500, Nathan Ford wrote: > I think it would be best for the user to be able to set the port > (and use this one port for all torrent transfers) so that users > behind firewalls can forward the port. Fully agreed. I need that. User setting will always have precedence. > If not a single port than atleast a range that can also be > forwarded. There is no reason to use more than one port. //Peter |