|
|
1.1 root 1: #include <stdio.h>
2: #include <signal.h>
3: #include <errno.h>
4: #include <ipc.h>
5: #include "defs.h"
6: #include <sys/inet/tcp_user.h>
7: #include <libc.h>
8: #include <sys/param.h>
9: #include <sys/ioctl.h>
10:
11: #define CSPORT 1
12:
13: /* imported */
14: extern char *in_ntoa();
15: extern int tcp_connect();
16: extern int tcp_accept();
17: extern int tcp_listen();
18: extern char *in_host();
19: extern in_addr in_address();
20: extern int rmesg_ld;
21:
22: /* dial a service on the internet - try first via cs, then direct */
23: int
24: net_dial(ip)
25: ipcinfo *ip;
26: {
27: char *service;
28: struct tcpuser tu;
29: in_addr faddr;
30: int fd;
31:
32: /* parse the name */
33: service = strchr(ip->name, '!');
34: if (service != NULL)
35: *service++ = '\0';
36: else
37: return ABORT(ENOENT, "unassigned destination", NULLINFO);
38: faddr = in_address(ip->name);
39: if (faddr==INADDR_ANY)
40: return ABORT(ENOENT, "unassigned destination", NULLINFO);
41:
42: /* get a channel for the connection */
43: fd = tcp_sock();
44: if (fd < 0)
45: return ABORT(EBUSY, "out of output channels", NULLINFO);
46:
47: /* connect to the remote connection server */
48: tu.laddr = INADDR_ANY;
49: tu.lport = 0;
50: tu.faddr = faddr;
51: tu.fport = CSPORT;
52: tu.param = 0;
53: errstr = "destination not answering";
54: if (tcp_connect(fd, &tu)>=0) {
55: /* send the request */
56: ip->name = service;
57: if (_info_write(fd, ip)==0) {
58: condition(fd, ip);
59: if (_reply_read(fd)==0 && errno==0) {
60: ip->myname = in_ntoa(tu.laddr);
61: return fd;
62: }
63: }
64: }
65: close(fd);
66:
67: /* no connection server, try the port directly */
68: fd = tcp_sock();
69: if (fd < 0)
70: return ABORT(EBUSY, "out of output channels", NULLINFO);
71: tu.laddr = INADDR_ANY;
72: tu.lport = 0;
73: tu.faddr = faddr;
74: tu.fport = fstotcp(service);
75: if (tu.fport<=0) {
76: close(fd);
77: return ABORT(ENOENT, errstr, NULLINFO);
78: }
79: tu.param = 0;
80: if (tcp_connect(fd, &tu)<0) {
81: close(fd);
82: return ABORT(ENOENT, errstr, NULLINFO);
83: }
84: condition(fd, ip);
85: ip->myname = in_ntoa(tu.laddr);
86: return fd;
87: }
88:
89: /* condition the connection as requested */
90: condition(fd, ip)
91: int fd;
92: ipcinfo *ip;
93: {
94: char *fields[10];
95: char param[128];
96: int i;
97: int dohup=0;
98: int domesg=0;
99:
100: strcpy(param, ip->param);
101: setfields(" \t");
102: getmfields(param, fields, 10);
103: for (i=0; i<10 && fields[i]; i++) {
104: if (strcmp(fields[i], "hup")==0)
105: dohup = 1;
106: if (strcmp(fields[i], "delim")==0)
107: domesg = 1;
108: }
109: if (dohup)
110: ioctl(fd, TCPIOHUP, 0);
111: if (domesg)
112: ioctl(fd, FIOPUSHLD, &rmesg_ld);
113: }
114:
115: /* announce onto the internet - only the first one gets there */
116: int
117: net_announce(ip)
118: ipcinfo *ip;
119: {
120: int fd;
121: struct tcpuser tu;
122:
123: USE(ip);
124: fd = tcp_sock();
125: if (fd < 0)
126: return ABORT(EBUSY, "no more output channels", NULLINFO);
127: tu.lport = TCPPORT_ANY;
128: tu.laddr = 0;
129: tu.fport = 0;
130: tu.faddr = 0;
131: tu.param = 0;
132: if (tcp_listen(fd, &tu)<0) {
133: close(fd);
134: return ABORT(EEXIST, "server already exists", NULLINFO);
135: }
136: return fd;
137: }
138:
139: /* listen for a call in */
140: ipcinfo *
141: net_listen(fd)
142: int fd;
143: {
144: static ipcinfo info;
145: static char myname[PATHLEN];
146: static char name[PATHLEN];
147: static char machine[PATHLEN];
148: struct tcpuser tu;
149: extern char *tcptofs();
150:
151: info.flags = 0;
152: for(;;) {
153: tu.param = 0;
154: if ((fd = tcp_accept(fd, &tu))<0)
155: return NULL;
156: if (tu.lport == CSPORT) {
157: if (tu.fport > 1023)
158: info.uid = info.gid = -1;
159: else
160: info.uid = info.gid = 0;
161: info.user = "_unknown_";
162: if (_info_read(fd, &info)<0) {
163: close(fd);
164: return (ipcinfo *)NULL;
165: }
166: info.flags |= IPC_HANDOFF;
167: condition(fd, &info);
168: } else {
169: info.user = "_unknown_";
170: info.param = "";
171: strcpy(name, tcptofs(tu.lport));
172: info.name = name;
173: }
174: strcpy(machine, in_host(tu.faddr));
175: info.machine = machine;
176: strcpy(myname, in_ntoa(tu.laddr));
177: info.myname = myname;
178: info.flags |= IPC_OPEN;
179: info.rfd = fd;
180: info.cfd = -1;
181: return &info;
182: }
183: }
184:
185: /* accept a call */
186: void
187: net_accept(ip)
188: ipcinfo *ip;
189: {
190: USE(ip);
191: }
192:
193: /* reject a call - null if a non-cs call */
194: void
195: net_reject(ip, no, str)
196: ipcinfo *ip;
197: int no;
198: char *str;
199: {
200: if (ip->flags&IPC_HANDOFF && ip->rfd>=0)
201: _reply_write(ip->rfd, no, str);
202: if (ip->rfd>=0) {
203: close(ip->rfd);
204: ip->rfd = -1;
205: }
206: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.