Tweak xconnect: socket can be a string (ala "ftp") from /etc/services.
authorRob Landley <rob@landley.net>
Thu, 6 Aug 2015 01:32:49 +0000 (20:32 -0500)
committerRob Landley <rob@landley.net>
Thu, 6 Aug 2015 01:32:49 +0000 (20:32 -0500)
Still need a rethink on how to handle socket/bind/connect sequence.

lib/lib.h
lib/net.c
toys/pending/telnet.c

index 8e6a8e2..1b3f957 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -205,7 +205,7 @@ void tty_sigreset(int i);
 // net.c
 int xsocket(int domain, int type, int protocol);
 void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
-int xconnect(char *host, int port, int family, int socktype, int protocol,
+int xconnect(char *host, char *port, int family, int socktype, int protocol,
   int flags);
 
 // password.c
index fc97a26..7be66b6 100644 (file)
--- a/lib/net.c
+++ b/lib/net.c
@@ -13,10 +13,9 @@ void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len)
   if (-1 == setsockopt(fd, level, opt, val, len)) perror_exit("setsockopt");
 }
 
-int xconnect(char *host, int port, int family, int socktype, int protocol,
+int xconnect(char *host, char *port, int family, int socktype, int protocol,
              int flags)
 {
-  char buf[32];
   struct addrinfo info, *ai;
   int fd;
 
@@ -26,11 +25,10 @@ int xconnect(char *host, int port, int family, int socktype, int protocol,
   info.ai_protocol = protocol;
   info.ai_flags = flags;
 
-  sprintf(buf, "%d", port);
-  fd = getaddrinfo(host, port ? buf : 0, &info, &ai);
+  fd = getaddrinfo(host, port, &info, &ai);
 
   if (fd || !ai)
-    error_exit("Connect '%s:%d': %s", host, port,
+    error_exit("Connect '%s%s%s': %s", host, port ? ":" : "", port ? port : "",
       fd ? gai_strerror(fd) : "not found");
 
   fd = xsocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
index 49dabd0..dc3487a 100644 (file)
@@ -286,14 +286,14 @@ static void write_server(int len)
 
 void telnet_main(void)
 {
+  char *port = "23";
   int set = 1, len;
   struct pollfd pfds[2];
 
-  TT.port = 23; //TELNET_PORT
   TT.win_width = 80; //columns
   TT.win_height = 24; //rows
 
-  if(toys.optc == 2) TT.port = atolx_range(toys.optargs[1], 0, 65535);
+  if (toys.optc == 2) port = toys.optargs[1];
 
   TT.ttype = getenv("TERM");
   if(!TT.ttype) TT.ttype = "";
@@ -306,7 +306,7 @@ void telnet_main(void)
   }
   terminal_size(&TT.win_width, &TT.win_height);
 
-  TT.sfd = xconnect(*toys.optargs, TT.port, 0, SOCK_STREAM, IPPROTO_TCP, 0);
+  TT.sfd = xconnect(*toys.optargs, port, 0, SOCK_STREAM, IPPROTO_TCP, 0);
   setsockopt(TT.sfd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set));
   setsockopt(TT.sfd, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));