merge off 4.1b with 4.1c; and random conversion to netdb library
authorSam Leffler <sam@ucbvax.Berkeley.EDU>
Mon, 15 Nov 1982 05:55:41 +0000 (21:55 -0800)
committerSam Leffler <sam@ucbvax.Berkeley.EDU>
Mon, 15 Nov 1982 05:55:41 +0000 (21:55 -0800)
SCCS-vsn: lib/libc/net/rcmd.c 4.3
SCCS-vsn: lib/libcompat/4.3/rexec.c 4.4

usr/src/lib/libc/net/rcmd.c
usr/src/lib/libcompat/4.3/rexec.c

index 99bd3b4..51a3352 100644 (file)
@@ -1,13 +1,15 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)rcmd.c     4.2 82/10/07";
+static char sccsid[] = "@(#)rcmd.c     4.3 %G%";
 #endif
 
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #endif
 
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <net/in.h>
-#include <errno.h>
+
+#include <netinet/in.h>
+
 #include <netdb.h>
 #include <netdb.h>
+#include <errno.h>
 
 extern errno;
 char   *index(), *sprintf();
 
 extern errno;
 char   *index(), *sprintf();
@@ -19,10 +21,11 @@ rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
        char *locuser, *remuser, *cmd;
        int *fd2p;
 {
        char *locuser, *remuser, *cmd;
        int *fd2p;
 {
-       int s, addr, timo = 1;
+       int s, timo = 1;
        struct sockaddr_in sin, sin2, from;
        char c;
        short port;
        struct sockaddr_in sin, sin2, from;
        char c;
        short port;
+       int lport = IPPORT_RESERVED - 1;
        struct hostent *hp;
 
        hp = gethostbyname(*ahost);
        struct hostent *hp;
 
        hp = gethostbyname(*ahost);
@@ -32,56 +35,68 @@ rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
        }
        *ahost = hp->h_name;
 retry:
        }
        *ahost = hp->h_name;
 retry:
-       s = rresvport(rcmdoptions|SO_KEEPALIVE);
+       s = rresvport(rcmdoptions|SO_KEEPALIVE, &lport);
        if (s < 0)
                return (-1);
        sin.sin_family = hp->h_addrtype;
        if (s < 0)
                return (-1);
        sin.sin_family = hp->h_addrtype;
-       bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
-       sin.sin_port = htons(rport);
-       if (connect(s, &sin) < 0) {
+       bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
+       sin.sin_port = htons((u_short)rport);
+       if (connect(s, (caddr_t)&sin, sizeof (sin), 0) < 0) {
+               if (errno == EADDRINUSE) {
+                       close(s);
+                       lport--;
+                       goto retry;
+               }
                if (errno == ECONNREFUSED && timo <= 16) {
                        (void) close(s);
                        sleep(timo);
                        timo *= 2;
                        goto retry;
                }
                if (errno == ECONNREFUSED && timo <= 16) {
                        (void) close(s);
                        sleep(timo);
                        timo *= 2;
                        goto retry;
                }
-               perror(*ahost);
+               perror(hp->h_name);
                return (-1);
        }
                return (-1);
        }
+       lport--;
        if (fd2p == 0) {
                write(s, "", 1);
                port = 0;
        } else {
                char num[8];
        if (fd2p == 0) {
                write(s, "", 1);
                port = 0;
        } else {
                char num[8];
-               int s2 = rresvport(rcmdoptions|SO_ACCEPTCONN);
+               int s2 = rresvport(rcmdoptions|SO_ACCEPTCONN, &lport), s3;
 
                if (s2 < 0) {
                        (void) close(s);
                        return (-1);
                }
 
                if (s2 < 0) {
                        (void) close(s);
                        return (-1);
                }
+               listen(s2, 1);
                socketaddr(s2, &sin2);
                port = htons((u_short)sin2.sin_port);
                (void) sprintf(num, "%d", port);
                (void) write(s, num, strlen(num)+1);
                socketaddr(s2, &sin2);
                port = htons((u_short)sin2.sin_port);
                (void) sprintf(num, "%d", port);
                (void) write(s, num, strlen(num)+1);
-               if (accept(s2, &from) < 0) {
+               { int len = sizeof (from);
+                 s3 = accept(s2, &from, &len, 0);
+                 close(s2);
+                 if (s3 < 0) {
                        perror("accept");
                        perror("accept");
+                       port = 0;
                        goto bad;
                        goto bad;
+                 }
                }
                }
-               from.sin_port = ntohs(from.sin_port);
+               *fd2p = s3;
+               from.sin_port = ntohs((u_short)from.sin_port);
                if (from.sin_family != AF_INET ||
                    from.sin_port >= IPPORT_RESERVED) {
                        fprintf(stderr,
                            "socket: protocol failure in circuit setup.\n");
                if (from.sin_family != AF_INET ||
                    from.sin_port >= IPPORT_RESERVED) {
                        fprintf(stderr,
                            "socket: protocol failure in circuit setup.\n");
-                       goto bad;
+                       goto bad2;
                }
                }
-               *fd2p = s2;
        }
        (void) write(s, locuser, strlen(locuser)+1);
        (void) write(s, remuser, strlen(remuser)+1);
        (void) write(s, cmd, strlen(cmd)+1);
        if (read(s, &c, 1) != 1) {
                perror(*ahost);
        }
        (void) write(s, locuser, strlen(locuser)+1);
        (void) write(s, remuser, strlen(remuser)+1);
        (void) write(s, cmd, strlen(cmd)+1);
        if (read(s, &c, 1) != 1) {
                perror(*ahost);
-               goto bad;
+               goto bad2;
        }
        if (c != 0) {
                while (read(s, &c, 1) == 1) {
        }
        if (c != 0) {
                while (read(s, &c, 1) == 1) {
@@ -89,36 +104,38 @@ retry:
                        if (c == '\n')
                                break;
                }
                        if (c == '\n')
                                break;
                }
-               goto bad;
+               goto bad2;
        }
        return (s);
        }
        return (s);
-bad:
+bad2:
        if (port)
                (void) close(*fd2p);
        if (port)
                (void) close(*fd2p);
+bad:
        (void) close(s);
        return (-1);
 }
 
        (void) close(s);
        return (-1);
 }
 
-rresvport(options)
-       int options;
+rresvport(options, alport)
+       int options, *alport;
 {
        struct sockaddr_in sin;
 {
        struct sockaddr_in sin;
-       short lport = IPPORT_RESERVED - 1;
        int s;
 
        int s;
 
+       sin.sin_family = AF_INET;
+       sin.sin_addr.s_addr = 0;
+       s = socket(0, SOCK_STREAM, 0, 0);
+       if (s < 0)
+               return (-1);
        for (;;) {
        for (;;) {
-               sin.sin_family = AF_INET;
-               sin.sin_port = htons(lport);
-               sin.sin_addr.s_addr = 0;
-               s = socket(SOCK_STREAM, 0, &sin, options);
-               if (s >= 0)
+               sin.sin_port = htons((u_short)*alport);
+               if (bind(s, (caddr_t)&sin, sizeof (sin), 0) >= 0)
                        return (s);
                if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
                        perror("socket");
                        return (-1);
                }
                        return (s);
                if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
                        perror("socket");
                        return (-1);
                }
-               lport--;
-               if (lport == IPPORT_RESERVED/2) {
+               (*alport)--;
+               if (*alport == IPPORT_RESERVED/2) {
                        fprintf(stderr, "socket: All ports in use\n");
                        return (-1);
                }
                        fprintf(stderr, "socket: All ports in use\n");
                        return (-1);
                }
@@ -158,3 +175,9 @@ ok:
        (void) fclose(hostf);
        return (0);
 }
        (void) fclose(hostf);
        return (0);
 }
+
+socketaddr(x, y)
+{
+
+       syscall(103,x,y);
+}
index 122d5d8..0dd603f 100644 (file)
@@ -1,11 +1,14 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)rexec.c    4.3 82/04/01";
+static char sccsid[] = "@(#)rexec.c    4.4 82/11/14";
 #endif
 
 #endif
 
-#include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <net/in.h>
+
+#include <netinet/in.h>
+
+#include <stdio.h>
+#include <netdb.h>
 #include <errno.h>
 
 extern errno;
 #include <errno.h>
 
 extern errno;
@@ -19,30 +22,29 @@ rexec(ahost, rport, name, pass, cmd, fd2p)
        char *name, *pass, *cmd;
        int *fd2p;
 {
        char *name, *pass, *cmd;
        int *fd2p;
 {
-       int s, addr, timo = 1;
+       int s, timo = 1, s3;
        struct sockaddr_in sin, sin2, from;
        char c;
        short port;
        struct sockaddr_in sin, sin2, from;
        char c;
        short port;
+       struct hostent *hp;
 
 
-       addr = rhost(ahost);
-       if (addr == -1) {
+       hp = gethostbyname(*ahost);
+       if (hp == 0) {
                fprintf(stderr, "%s: unknown host\n", *ahost);
                return (-1);
        }
                fprintf(stderr, "%s: unknown host\n", *ahost);
                return (-1);
        }
-       ruserpass(*ahost, &name, &pass);
+       *ahost = hp->h_name;
+       ruserpass(hp->h_name, &name, &pass);
 retry:
 retry:
-       sin.sin_family = AF_INET;
-       sin.sin_port = 0;
-       sin.sin_addr.s_addr = 0;
-       s = socket(SOCK_STREAM, 0, &sin, rexecoptions|SO_KEEPALIVE);
-       if (s < 0)
+       s = socket(0, SOCK_STREAM, 0, 0);
+       if (s < 0) {
+               perror("rexec: socket");
                return (-1);
                return (-1);
-       sin.sin_addr.s_addr = addr;
+       }
+       sin.sin_family = hp->h_addrtype;
        sin.sin_port = rport;
        sin.sin_port = rport;
-#if vax
-       sin.sin_port =
-           ((sin.sin_port << 8) & 0xff00) | ((sin.sin_port >> 8) & 0x00ff);
-#endif
+       bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
+       sin.sin_port = htons((u_short)rport);
        if (connect(s, &sin) < 0) {
                if (errno == ECONNREFUSED && timo <= 16) {
                        (void) close(s);
        if (connect(s, &sin) < 0) {
                if (errno == ECONNREFUSED && timo <= 16) {
                        (void) close(s);
@@ -50,7 +52,7 @@ retry:
                        timo *= 2;
                        goto retry;
                }
                        timo *= 2;
                        goto retry;
                }
-               perror(*ahost);
+               perror(hp->h_name);
                return (-1);
        }
        if (fd2p == 0) {
                return (-1);
        }
        if (fd2p == 0) {
@@ -60,27 +62,26 @@ retry:
                char num[8];
                int s2;
                
                char num[8];
                int s2;
                
-               sin.sin_family = AF_INET;
-               sin.sin_port = 0;
-               sin.sin_addr.s_addr = 0;
-               s2 = socket(SOCK_STREAM, 0, &sin, rexecoptions|SO_ACCEPTCONN);
-
+               s2 = socket(0, SOCK_STREAM, 0, 0);
                if (s2 < 0) {
                        (void) close(s);
                        return (-1);
                }
                if (s2 < 0) {
                        (void) close(s);
                        return (-1);
                }
+               listen(s2, 1);
                socketaddr(s2, &sin2);
                socketaddr(s2, &sin2);
-               port = sin2.sin_port;
-#if vax
-               port = ((port << 8) & 0xff00) | ((port >> 8) & 0x00ff);
-#endif
+               port = ntohs((u_short)sin2.sin_port);
                (void) sprintf(num, "%d", port);
                (void) write(s, num, strlen(num)+1);
                (void) sprintf(num, "%d", port);
                (void) write(s, num, strlen(num)+1);
-               if (accept(s2, &from) < 0) {
+               { int len = sizeof (from);
+                 s3 = accept(s2, &from, &len, 0);
+                 close(s2);
+                 if (s3 < 0) {
                        perror("accept");
                        perror("accept");
+                       port = 0;
                        goto bad;
                        goto bad;
+                 }
                }
                }
-               *fd2p = s2;
+               *fd2p = s3;
        }
        (void) write(s, name, strlen(name) + 1);
        /* should public key encypt the password here */
        }
        (void) write(s, name, strlen(name) + 1);
        /* should public key encypt the password here */