6 /* ttyw.c - the writer */
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
16 #include <arpa/inet.h>
23 ttyw (command, host, line, user)
35 struct sockaddr_in tty_socket,
38 if (command == NULL) {
43 if ((sp = getservbyname ("ttyserver", "tcp")) == NULL) {
48 (void) gethostname (host = buffer, sizeof buffer);
49 if ((hp = gethostbyname (host))==NULL) {
54 if (line && strncmp (line, "/dev/", strlen ("/dev/")) == 0)
55 line += strlen ("/dev/");
57 privd = *command >= 'A' && *command <= 'Z';/* crude */
61 for (times = 1; times <= 16; times *= 2) {
62 if ((sd = getport (0, privd)) == NOTOK)
65 bzero ((char *) tsock, sizeof *tsock);
66 tsock -> sin_family = hp -> h_addrtype;
67 tsock -> sin_port = sp -> s_port;
68 bcopy (hp -> h_addr, (char *) &tsock -> sin_addr, hp -> h_length);
70 if (connect (sd, (struct sockaddr *) tsock, sizeof *tsock) == NOTOK) {
72 if (errno == ECONNREFUSED || errno == EINTR) {
79 ttym (sd, command, line, user, NULL);
80 if (ttyv (sd) == NOTOK || ttyv (sd) == NOTOK) {
82 errno = EPERM; /* what else??? */
94 static int getport (options, privd)
100 struct sockaddr_in unx_socket,
101 *usock = &unx_socket;
103 if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == NOTOK)
106 if (options & SO_DEBUG)
107 (void) setsockopt (sd, SOL_SOCKET, SO_DEBUG, NULL, 0);
108 (void) setsockopt (sd, SOL_SOCKET, SO_KEEPALIVE, NULL, 0);
113 usock -> sin_family = AF_INET;
114 usock -> sin_addr.s_addr = INADDR_ANY;
116 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
117 usock -> sin_port = htons (port);
119 switch (bind (sd, (struct sockaddr *) usock, sizeof *usock)) {
121 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL)