|
|
1.1 root 1: /*
2: * proxy - connect to the external Internet through a proxy machine.
3: */
4:
5: #include <stdio.h>
6: #include <sys/types.h>
7: #include <sys/param.h>
8: #include <string.h>
9: #include <sysexits.h>
10:
11: #include "proxy.h"
12:
13: char ipcname[100];
14:
15: extern char *ipcpath();
16: extern int ipcdebug;
17:
18: int
19: proxy_connect(dest, param, prhost)
20: char *dest, *param, *prhost;
21: {
22: int s;
23: char *f[5], *reply, buf[PATHLEN];
24: int i, ipcflag;
25:
26: if ((s = ipcopen(prhost, param)) < 0)
27: return s;
28:
29: if (strcmp(param, "create") == 0) /* stub - not V10 parameter! */
30: ipcflag = IPC_CREAT;
31: else
32: ipcflag = IPC_OPEN;
33:
34: strcpy(buf, dest);
35: setfields("!");
36: if (getfields(buf, f, 3) < 3) {
37: strcpy(buf, "tcp!");
38: strcat(buf, dest);
39: }
40:
41: if ((send_ipcinfo(s, buf, ipcflag) != 0) ||
42: (read_ipcreply(s, &reply) != 0)) {
43: ipcsyserr("proxy_connect");
44: close(s);
45: return -1;
46: }
47:
48: /* *reply should hold "/cs/tcp!<ip address>!port#", which is
49: address of the connection on proxy machine. Save the conport. */
50: strncpy(ipcname, reply, sizeof(ipcname));
51: setfields("!");
52: if (getfields(reply, f, 3) != 3) {
53: ipcseterror(EX_PROTOCOL, "gateway protocol error 2",
54: "proxy_connect");
55: close(s);
56: return -1;
57: }
58: if (ipcdebug)
59: fprintf(stderr, "reply = %s\n", ipcname);
60: return s;
61: }
62:
63: int
64: send_ipcinfo(fd, name, flag)
65: int fd;
66: char *name;
67: int flag;
68: {
69: char b[INFOLEN], *user, *getlogin();
70: char hostname[256];
71: int namelen = sizeof(hostname);
72: int n;
73:
74: if (name==NULL)
75: name = "";
76: user = getlogin();
77: if (user==NULL)
78: user = "";
79: gethostname(hostname, namelen); /* stub - this needs simulation in Sys V */
80: sprintf(b, "\n%s\n\n%s\n%s\n%d\n-1\n-1\n", name, hostname, user, flag);
81: n = strlen(b);
82: if (ipcdebug)
83: fprintf(stderr, "send_ipcinfo (%d): |%s||%s|%s|%d|-1|-1|\n",
84: n, name, hostname, user, flag);
85: if (write(fd, b, n) != n) {
86: ipcseterror(EX_PROTOCOL, "send_ipcinfo",
87: "sending to gateway machine");
88: return -1;
89: }
90: return 0;
91: }
92:
93: int
94: send_ipcreply(fd, code, answer)
95: int fd;
96: int code;
97: char *answer;
98: {
99: char b[PATHLEN];
100: int n;
101:
102: if (answer==NULL)
103: answer = "";
104: sprintf(b, "%d\n%s\n", code, answer);
105: n = strlen(b);
106: if (ipcdebug)
107: fprintf(stderr, "send_ipcreply (%d): %d|%s|\n",
108: n, code, answer);
109: if (write(fd, b, n) != n) {
110: ipcseterror(EX_PROTOCOL, "send_ipcreply",
111: "sending to gateway machine");
112: return -1;
113: }
114: return 0;
115: }
116:
117: int
118: read_ipcinfo(fd)
119: int fd;
120: {
121: static char b[INFOLEN];
122: int n;
123:
124: n = read(fd, b, sizeof(b));
125: if (n <= 0) {
126: ipcseterror(EX_PROTOCOL, "read_ipcinfo",
127: "can't read from proxy gateway");
128: return -1;
129: }
130: if (ipcdebug) {
131: char buf[PATHLEN], *cp;
132: strncpy(buf, b, n);
133: buf[n] = '\0';
134: for (cp = buf; *cp; cp++)
135: if (*cp == '\n')
136: *cp = '|';
137: fprintf(stderr, "read_ipcinfo (%d): %s\n", n, buf);
138: }
139: /* could crack the fields if we wanted to */
140: return 0;
141: }
142:
143: int
144: read_ipcreply(fd, answer)
145: int fd;
146: char **answer;
147: {
148: static char b[PATHLEN];
149: char *f[2], *ptr;
150: int c, n;
151:
152: *answer = "";
153: n = read(fd, b, sizeof(b));
154: if (n <= 0) {
155: ipcseterror(EX_PROTOCOL, "read_ipcreply",
156: "can't read from proxy gateway");
157: return -1;
158: }
159: b[n] = '\0';
160: if (ipcdebug) {
161: char buf[PATHLEN], *cp;
162: strncpy(buf, b, n);
163: buf[n] = '\0';
164: for (cp = buf; *cp; cp++)
165: if (*cp == '\n')
166: *cp = '|';
167: fprintf(stderr, "read_ipcreply (%d): %s\n", n, buf);
168: }
169: setfields("\n");
170: if (getfields(b, f, 2) != 2) {
171: ipcseterror(EX_PROTOCOL, "gateway protocol botch",
172: "can't read from proxy gateway");
173: return -1;
174: }
175: if ((ptr=strchr(f[1], '\n'))!=NULL)
176: *ptr = '\0';
177: *answer = f[1];
178: c = atoi(f[0]);
179: if (c != 0) {
180: ipcseterror(c, f[1], "gateway error");
181: return -1;
182: }
183: return 0;
184: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.