1: #ifdef  TTYD
   2: /* ttyw.c - the writer */
   3: 
   4: #include <errno.h>
   5: #include <stdio.h>
   6: #include <strings.h>
   7: #include <sys/types.h>
   8: #include <sys/socket.h>
   9: #include <netinet/in.h>
  10: #include <netdb.h>
  11: #include <arpa/inet.h>
  12: #include "ttyd.h"
  13: #include "ttym.c"
  14: 
  15: /*  */
  16: 
  17: ttyw (command, host, line, user)
  18: char   *command,
  19:        *host,
  20:        *line,
  21:        *user;
  22: {
  23:     int     privd,
  24:             sd;
  25:     unsigned    times;
  26:     char        buffer[BUFSIZ];
  27:     struct  hostent *hp;
  28:     struct  servent *sp;
  29:     struct sockaddr_in  tty_socket,
  30:                        *tsock = &tty_socket;
  31: 
  32:     if (command == NULL) {
  33:     errno = EINVAL;
  34:     return NOTOK;
  35:     }
  36: 
  37:     if ((sp = getservbyname ("ttyserver", "tcp")) == NULL) {
  38:     errno = ENETDOWN;
  39:     return NOTOK;
  40:     }
  41:     if (host == NULL)
  42:     (void) gethostname (host = buffer, sizeof buffer);
  43:     if ((hp = gethostbyname (host))==NULL) {
  44:     errno = ENETDOWN;
  45:     return NOTOK;
  46:     }
  47: 
  48:     if (line && strncmp (line, "/dev/", strlen ("/dev/")) == 0)
  49:     line += strlen ("/dev/");
  50: 
  51:     privd = *command >= 'A' && *command <= 'Z';/* crude */
  52: 
  53: /*  */
  54: 
  55:     for (times = 1; times <= 16; times *= 2) {
  56:     if ((sd = getport (0, privd)) == NOTOK)
  57:         return NOTOK;
  58: 
  59:     bzero ((char *) tsock, sizeof *tsock);
  60:     tsock -> sin_family = hp -> h_addrtype;
  61:     tsock -> sin_port = sp -> s_port;
  62:     bcopy (hp -> h_addr, (char *) &tsock -> sin_addr, hp -> h_length);
  63: 
  64:     if (connect (sd, (struct sockaddr *) tsock, sizeof *tsock) == NOTOK) {
  65:         (void) close (sd);
  66:         if (errno == ECONNREFUSED || errno == EINTR) {
  67:         sleep (times);
  68:         continue;
  69:         }
  70:         break;
  71:     }
  72: 
  73:     ttym (sd, command, line, user, NULL);
  74:     if (ttyv (sd) == NOTOK || ttyv (sd) == NOTOK) {
  75:         (void) close (sd);
  76:         errno = EPERM;  /* what else??? */
  77:         return NOTOK;
  78:     }
  79:     else
  80:         return sd;
  81:     }
  82: 
  83:     return NOTOK;
  84: }
  85: 
  86: /*  */
  87: 
  88: static int  getport (options, privd)
  89: unsigned    options;
  90: int     privd;
  91: {
  92:     int     sd,
  93:             port;
  94:     struct sockaddr_in  unx_socket,
  95:                        *usock = &unx_socket;
  96: 
  97:     if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == NOTOK)
  98:     return sd;
  99: 
 100:     if (options & SO_DEBUG)
 101:     (void) setsockopt (sd, SOL_SOCKET, SO_DEBUG, NULL, 0);
 102:     (void) setsockopt (sd, SOL_SOCKET, SO_KEEPALIVE, NULL, 0);
 103: 
 104:     if (!privd)
 105:     return sd;
 106: 
 107:     usock -> sin_family = AF_INET;
 108:     usock -> sin_addr.s_addr = INADDR_ANY;
 109: 
 110:     for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
 111:     usock -> sin_port = htons (port);
 112: 
 113:     switch (bind (sd, (struct sockaddr *) usock, sizeof *usock)) {
 114:         case NOTOK:
 115:         if (errno != EADDRINUSE && errno != EADDRNOTAVAIL)
 116:             return NOTOK;
 117:         continue;
 118: 
 119:         default:
 120:         return sd;
 121:     }
 122:     }
 123: 
 124:     return NOTOK;
 125: }
 126: #endif	TTYD

Defined functions

getport defined in line 88; used 1 times
  • in line 56
ttyw defined in line 17; used 1 times
Last modified: 1985-12-20
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1068
Valid CSS Valid XHTML 1.0 Strict