Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / RCS / ttyw.c,v
1 head     1.2;
2 branch   ;
3 access   ;
4 symbols  ;
5 locks    ; strict;
6 comment  @ * @;
7
8
9 1.2
10 date     91.01.25.14.49.56;  author mh;  state Exp;
11 branches ;
12 next     1.1;
13
14 1.1
15 date     91.01.25.14.49.13;  author mh;  state Exp;
16 branches ;
17 next     ;
18
19
20 desc
21 @@
22
23
24 1.2
25 log
26 @don't include arpa/inet.h on hpux
27 @
28 text
29 @#ifndef        BSD42
30 #undef  TTYD
31 #endif
32
33 #ifdef  TTYD
34 /* ttyw.c - the writer */
35
36 #include <errno.h>
37 #include <stdio.h>
38 #include <strings.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <netdb.h>
43 #ifndef hpux
44 #include <arpa/inet.h>
45 #endif
46 #include "ttyd.h"
47 #include "ttym.c"
48
49 /* \f */
50
51 ttyw (command, host, line, user)
52 char   *command,
53        *host,
54        *line,
55        *user;
56 {
57     int     privd,
58             sd;
59     unsigned    times;
60     char        buffer[BUFSIZ];
61     struct      hostent *hp;
62     struct      servent *sp;
63     struct sockaddr_in  tty_socket,
64                        *tsock = &tty_socket;
65
66     if (command == NULL) {
67         errno = EINVAL;
68         return NOTOK;
69     }
70
71     if ((sp = getservbyname ("ttyserver", "tcp")) == NULL) {
72         errno = ENETDOWN;
73         return NOTOK;
74     }
75     if (host == NULL)
76         (void) gethostname (host = buffer, sizeof buffer);
77     if ((hp = gethostbyname (host))==NULL) {
78         errno = ENETDOWN;
79         return NOTOK;
80     }
81
82     if (line && strncmp (line, "/dev/", strlen ("/dev/")) == 0)
83         line += strlen ("/dev/");
84
85     privd = *command >= 'A' && *command <= 'Z';/* crude */
86
87 /* \f */
88
89     for (times = 1; times <= 16; times *= 2) {
90         if ((sd = getport (0, privd)) == NOTOK)
91             return NOTOK;
92
93         bzero ((char *) tsock, sizeof *tsock);
94         tsock -> sin_family = hp -> h_addrtype;
95         tsock -> sin_port = sp -> s_port;
96         bcopy (hp -> h_addr, (char *) &tsock -> sin_addr, hp -> h_length);
97
98         if (connect (sd, (struct sockaddr *) tsock, sizeof *tsock) == NOTOK) {
99             (void) close (sd);
100             if (errno == ECONNREFUSED || errno == EINTR) {
101                 sleep (times);
102                 continue;
103             }
104             break;
105         }
106
107         ttym (sd, command, line, user, NULL);
108         if (ttyv (sd) == NOTOK || ttyv (sd) == NOTOK) {
109             (void) close (sd);
110             errno = EPERM;      /* what else??? */
111             return NOTOK;
112         }
113         else
114             return sd;
115     }
116
117     return NOTOK;
118 }
119
120 /* \f */
121
122 static int  getport (options, privd)
123 unsigned    options;
124 int     privd;
125 {
126     int     sd,
127             port;
128     struct sockaddr_in  unx_socket,
129                        *usock = &unx_socket;
130
131     if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == NOTOK)
132         return sd;
133
134     if (options & SO_DEBUG)
135         (void) setsockopt (sd, SOL_SOCKET, SO_DEBUG, NULL, 0);
136     (void) setsockopt (sd, SOL_SOCKET, SO_KEEPALIVE, NULL, 0);
137
138     if (!privd)
139         return sd;
140
141     usock -> sin_family = AF_INET;
142     usock -> sin_addr.s_addr = INADDR_ANY;
143
144     for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
145         usock -> sin_port = htons (port);
146
147         switch (bind (sd, (struct sockaddr *) usock, sizeof *usock)) {
148             case NOTOK: 
149                 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL)
150                     return NOTOK;
151                 continue;
152
153             default: 
154                 return sd;
155         }
156     }
157
158     return NOTOK;
159 }
160 #endif  TTYD
161 @
162
163
164 1.1
165 log
166 @Initial revision
167 @
168 text
169 @d15 1
170 d17 1
171 @