- add a few basic tests for pidof(8)
[oweals/busybox.git] / libbb / xconnect.c
index 10ba9477c173d6e35819cb67e2febb48b89133a2..1e5799e516c53ef84a5ced00878ff7d57542fe4c 100644 (file)
@@ -2,7 +2,7 @@
 /*
  * Utility routines.
  *
- * Connect to host at port using address resolusion from getaddrinfo
+ * Connect to host at port using address resolution from getaddrinfo
  *
  */
 
@@ -23,7 +23,7 @@
  * If "port" is a name it is looked up in /etc/services, if it isnt found return
  * default_port
  */
-unsigned short bb_lookup_port(const char *port, unsigned short default_port)
+unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port)
 {
        unsigned short port_nr = htons(default_port);
        if (port) {
@@ -37,11 +37,11 @@ unsigned short bb_lookup_port(const char *port, unsigned short default_port)
                errno = 0;
                port_long = strtol(port, &endptr, 10);
                if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) {
-                       struct servent *tserv = getservbyname(port, "tcp");
+                       struct servent *tserv = getservbyname(port, protocol);
                        if (tserv) {
-                       port_nr = tserv->s_port;
-               }
-       } else {
+                               port_nr = tserv->s_port;
+                       }
+               } else {
                        port_nr = htons(port_long);
                }
                errno = old_errno;
@@ -64,7 +64,8 @@ int xconnect(struct sockaddr_in *s_addr)
        int s = socket(AF_INET, SOCK_STREAM, 0);
        if (connect(s, (struct sockaddr_in *)s_addr, sizeof(struct sockaddr_in)) < 0)
        {
-               bb_perror_msg_and_die("Unable to connect to remote host (%s)", 
+               if (ENABLE_FEATURE_CLEAN_UP) close(s);
+               bb_perror_msg_and_die("Unable to connect to remote host (%s)",
                                inet_ntoa(s_addr->sin_addr));
        }
        return s;