Use of getaddrinfo in libbalsa/pop3.c



Hi all,

Just cast an eye over this code and the following strikes me.

The whole point of getaddrinfo() is that it is protocol independent.  The
structure returned by getaddrinfo() contains both the address *and* its size.
Having code that tests the protocol type to set the size of the address needed
for connect() negates the whole point of having a protocol independent API
and is error prone. 

This patch fixes that problem mostly by removing code.  In addition it does
not constrain the use of POP3 to ip 4 or 6.  It is unlikely to be the case
POP3 services will run on other underlying transports but it is harmless
and simpler.

--- pop3.c.orig Sun Aug 26 13:27:14 2001
+++ pop3.c      Sun Aug 26 13:27:59 2001
@@ -200,18 +200,7 @@
       *s = socket (cur->ai_family, cur->ai_socktype, cur->ai_protocol);
       fcntl(*s, F_SETFD,FD_CLOEXEC);
       if(*s<0) continue;
-
-      if (cur->ai_addr->sa_family == AF_INET)
-         sa_size = sizeof (struct sockaddr_in);
-      else if (cur->ai_addr->sa_family == AF_INET6)
-         sa_size = sizeof (struct sockaddr_in6);
-      else {
-         libbalsa_information(LIBBALSA_INFORMATION_ERROR,
-                              _("Unknown POP socket family: %d"), 
-                              cur->ai_addr->sa_family);
-         return POP_CONNECT_FAILED;
-      }
-      if((rc = connect(*s, cur->ai_addr, sa_size)) == 0) break;
+      if((rc = connect(*s, cur->ai_addr, cur->ai_addrlen)) == 0) break;
       close(*s);   
   }
   freeaddrinfo (res);


A further observation.  Getaddrinfo accepts a string for its service name
argument.  Why not pass a string through from the preferences rather than
sprintf()ing the port number into a buffer?  One very good reason for doing
this is that a DNS SRV RR aware or a SLP aware getaddrinfo cannot use port
numbers to locate services, it would need a name i.e. pop3.  As it is the
standard getaddrinfo uses /etc/services to look up the name.  Much easier
to configure "some.host.org" and "pop3" than trying to remember that pop3 is
on port 110.

Brian Stafford




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]