|
|
1.1 root 1: /* bridge.c - X.25 abstractions for TCP bridge to X25 */
2:
3: #ifndef lint
4: static char *rcsid = "$Header: /f/osi/compat/RCS/bridge.c,v 7.1 90/07/09 14:31:32 mrose Exp $";
5: #endif
6:
7: /*
8: * $Header: /f/osi/compat/RCS/bridge.c,v 7.1 90/07/09 14:31:32 mrose Exp $
9: *
10: * Contributed by Julian Onions, Nottingham University in the UK
11: *
12: *
13: * $Log: bridge.c,v $
14: * Revision 7.1 90/07/09 14:31:32 mrose
15: * sync
16: *
17: * Revision 7.0 89/11/23 21:22:55 mrose
18: * Release 6.0
19: *
20: */
21:
22: /*
23: * NOTICE
24: *
25: * Acquisition, use, and distribution of this module and related
26: * materials are subject to the restrictions of a license agreement.
27: * Consult the Preface in the User's Manual for the full terms of
28: * this agreement.
29: *
30: */
31:
32:
33: /* LINTLIBRARY */
34:
35: #include <errno.h>
36: #include <stdio.h>
37: #include "general.h"
38: #include "manifest.h"
39: #include "tailor.h"
40: #include "internet.h"
41: #include "tpkt.h"
42:
43: /* TCP/X.25 BRIDGE */
44:
45: #ifdef BRIDGE_X25
46:
47:
48: static int assfd[FD_SETSIZE];
49: static char bridge_inited = 0;
50:
51: /* */
52:
53: /* ARGSUSED */
54:
55: int start_bridge_client (local)
56: struct NSAPaddr *local;
57: {
58: int sd;
59: u_short port;
60: register struct servent *sp;
61:
62: if ((sp = getservbyname ("x25bridge", "tcp")) == NULL)
63: port = x25_bridge_port;
64: else
65: port = sp -> s_port;
66:
67: if ((sd = in_connect (x25_bridge_host, port)) == NOTOK)
68: return NOTOK;
69:
70: if (write_tcp_socket (sd, "\01", 1) != 1) {
71: SLOG (compat_log, LLOG_EXCEPTIONS, "failed", ("initial write"));
72:
73: (void) close_tcp_socket (sd);
74: return NOTOK;
75: }
76:
77: return sd;
78: }
79:
80: /* */
81:
82: static int in_connect (addr, port)
83: char *addr;
84: u_short port;
85: {
86: int sd;
87: struct sockaddr_in in_socket;
88: register struct sockaddr_in *isock = &in_socket;
89: register struct hostent *hp;
90:
91: if ((hp = gethostbystring (addr)) == NULL) {
92: SLOG (addr_log, LLOG_EXCEPTIONS, NULLCP, ("%s: unknown host", addr));
93:
94: return NOTOK;
95: }
96:
97: bzero ((char *) isock, sizeof *isock);
98: isock -> sin_family = hp -> h_addrtype;
99: isock -> sin_port = port;
100: inaddr_copy (hp, isock);
101:
102: if ((sd = start_tcp_client ((struct sockaddr_in *) NULL, 0)) == NOTOK) {
103: SLOG (compat_log, LLOG_EXCEPTIONS, "failed", ("start_tcp_client"));
104:
105: return NOTOK;
106: }
107:
108: if (join_tcp_server (sd, isock) == NOTOK) {
109: SLOG (compat_log, LLOG_EXCEPTIONS, "failed", ("join_tcp_server"));
110:
111: (void) close_tcp_socket (sd);
112: return NOTOK;
113: }
114:
115: return sd;
116: }
117:
118: /* */
119:
120: int join_bridge_server (fd, remote)
121: int fd;
122: register struct NSAPaddr *remote;
123: {
124: if (remote != NULLNA)
125: remote -> na_stack = NA_BRG, remote -> na_community = ts_comm_x25_default;
126: if (bridge_write_nsap_addr (fd, remote, write_tcp_socket) == NOTOK) {
127: SLOG (compat_log, LLOG_EXCEPTIONS, NULLCP, ("write of NSAP failed"));
128:
129: return NOTOK;
130: }
131:
132: return fd;
133: }
134:
135: /* */
136:
137: int start_bridge_server (local, backlog, opt1, opt2)
138: struct NSAPaddr *local;
139: int backlog,
140: opt1,
141: opt2;
142: {
143: int len,
144: new,
145: sd;
146: u_short port;
147: struct servent *sp;
148: struct sockaddr_in in_socket;
149: register struct sockaddr_in *isock = &in_socket;
150:
151: if (bridge_inited == 0) {
152: for (sd = 0; sd < FD_SETSIZE; sd++)
153: assfd[sd] = NOTOK;
154: bridge_inited = 1;
155: }
156: if ((sp = getservbyname ("x25bridge", "tcp")) == NULL)
157: port = x25_bridge_port;
158: else
159: port = sp -> s_port;
160:
161: if ((sd = in_connect (x25_bridge_host, port)) == NOTOK)
162: return NOTOK;
163:
164: if (write_tcp_socket (sd, "\02", 1) != 1) {
165: SLOG (compat_log, LLOG_EXCEPTIONS, "failed", ("initial write"));
166:
167: (void) close_tcp_socket (sd);
168: return NOTOK;
169: }
170:
171: if (local != NULLNA)
172: local -> na_stack = NA_BRG, local -> na_community = ts_comm_x25_default;
173: if (local != NULLNA && local -> na_dtelen == 0)
174: {
175: (void) strcpy (local -> na_dte, x25_bridge_addr);
176: local -> na_dtelen = strlen(x25_bridge_addr);
177: }
178: if (local != NULLNA) {
179: DLOG (compat_log, LLOG_DEBUG,
180: ("addr", "type=%d '%s' len=%d",
181: local -> na_stack, local -> na_dte,local -> na_dtelen));
182: DLOG (compat_log, LLOG_DEBUG,
183: ("addr", "pid='%s'(%d) fac='%s'(%d) cudf='%s'(%d)",
184: local -> na_pid, local -> na_pidlen,
185: local -> na_fac, local -> na_faclen,
186: local -> na_pid, local -> na_pidlen,
187: local -> na_cudf, local -> na_cudflen));
188: }
189:
190: if (bridge_write_nsap_addr (sd, local, write_tcp_socket) == NOTOK) {
191: SLOG (compat_log, LLOG_EXCEPTIONS, NULLCP, ("write of NSAP failed"));
192:
193: (void) close_tcp_socket (sd);
194: return NOTOK;
195: }
196:
197: if ((new = in_listen (backlog, opt1, opt2)) == NOTOK) {
198: (void) close_tcp_socket (sd);
199: return NOTOK;
200: }
201:
202: len = sizeof *isock;
203: if (getsockname (new, (struct sockaddr *) isock, &len) == NOTOK) {
204: SLOG (compat_log, LLOG_EXCEPTIONS, "failed", ("getsockname"));
205:
206: out: ;
207: (void) close_tcp_socket (sd);
208: (void) close_tcp_socket (new);
209: return NOTOK;
210: }
211:
212: isock -> sin_family = htons (isock -> sin_family);
213: if (write_tcp_socket (sd, (char *)isock, sizeof *isock) != sizeof *isock) {
214: SLOG (compat_log, LLOG_EXCEPTIONS, "failed", ("write of sockaddr_in"));
215:
216: goto out;
217: }
218: assfd[new] = sd;
219:
220: return new;
221: }
222:
223: int get_bridge_assfd(fd)
224: int fd;
225: {
226: if (!bridge_inited)
227: return NOTOK;
228: return assfd[fd];
229: }
230:
231: /* */
232:
233: static int in_listen (backlog, opt1, opt2)
234: int backlog,
235: opt1,
236: opt2;
237: {
238: int sd;
239: char *cp;
240: struct sockaddr_in lo_socket;
241: register struct sockaddr_in *lsock = &lo_socket;
242: register struct hostent *hp;
243:
244: if ((hp = gethostbystring (cp = getlocalhost ())) == NULL) {
245: SLOG (addr_log, LLOG_EXCEPTIONS, NULLCP, ("%s: unknown host", cp));
246:
247: return NOTOK;
248: }
249:
250: bzero ((char *) lsock, sizeof *lsock);
251: lsock -> sin_family = hp -> h_addrtype;
252: inaddr_copy (hp, lsock);
253:
254: if ((sd = start_tcp_server (lsock, backlog, opt1, opt2)) == NOTOK) {
255: SLOG (compat_log, LLOG_EXCEPTIONS, "failed", ("start_tcp_server"));
256:
257: return NOTOK;
258: }
259:
260: return sd;
261: }
262:
263: /* */
264:
265: int join_bridge_client (fd, remote)
266: int fd;
267: struct NSAPaddr *remote;
268: {
269: int new;
270: struct sockaddr_in in_socket;
271: struct NSAPaddr sock;
272:
273: if ((new = join_tcp_client (fd, &in_socket)) == NOTOK) {
274: SLOG (compat_log, LLOG_EXCEPTIONS, "failed", ("join_tcp_client"));
275:
276: return NOTOK;
277: }
278:
279: if (bridge_read_nsap_addr (new, &sock, read_tcp_socket) == NOTOK) {
280: SLOG (compat_log, LLOG_EXCEPTIONS, "failed", ("read of NSAP"));
281:
282: (void) close_tcp_socket (new);
283: return NOTOK;
284: }
285: DLOG (compat_log, LLOG_DEBUG,
286: ("addr", "type=%d '%s' len=%d", sock.na_stack, sock.na_dte,
287: sock.na_dtelen));
288: DLOG (compat_log, LLOG_DEBUG,
289: ("addr", "pid='%s'(%d) fac='%s'(%d) cudf='%s'(%d)",
290: sock.na_pid, sock.na_pidlen,
291: sock.na_fac, sock.na_faclen,
292: sock.na_pid, sock.na_pidlen,
293: sock.na_cudf, sock.na_cudflen));
294: sock.na_stack = ntohl (sock.na_stack);
295: *remote = sock;
296: DLOG (compat_log, LLOG_DEBUG,
297: ("addr", "type=%d '%s' len=%d",
298: remote -> na_stack, remote -> na_dte,remote -> na_dtelen));
299: DLOG (compat_log, LLOG_DEBUG,
300: ("addr", "pid='%s'(%d) fac='%s'(%d) cudf='%s'(%d)",
301: remote -> na_pid, remote -> na_pidlen,
302: remote -> na_fac, remote -> na_faclen,
303: remote -> na_pid, remote -> na_pidlen,
304: remote -> na_cudf, remote -> na_cudflen));
305: return new;
306: }
307:
308: int close_bridge_socket (sd)
309: int sd;
310: {
311: if (bridge_inited && assfd[sd] != NOTOK)
312: (void) close_tcp_socket (assfd[sd]);
313: assfd[sd] = NOTOK;
314: return close_tcp_socket (sd);
315: }
316:
317: /* ARGSUSED */
318:
319: int bridgediscrim (na)
320: struct NSAPaddr *na;
321: {
322: #ifndef X25
323: return 1; /* must be bridge */
324: #else
325: int len = strlen (x25_bridge_discrim);
326:
327: if (len == 1 && *x25_bridge_discrim == '-')
328: return 0;
329:
330: return (len == 0 ? 1
331: : strncmp (na -> na_dte, x25_bridge_discrim, len) == 0);
332: #endif
333: }
334: #endif
335:
336: /*
337: * Structure is as follows :-
338: * 0-1 type
339: * 2-17 dte
340: * 18 dte len
341: * 19-22 pid
342: * 23 pid len
343: * 24-39 user data
344: * 40 user data len
345: * 41-46 facilities
346: * 47 facility length
347: */
348:
349: int bridge_write_nsap_addr (fd, nsap, writefnx)
350: int fd;
351: struct NSAPaddr *nsap;
352: IFP writefnx;
353: {
354: u_short na_stack;
355: char buffer[50];
356:
357: if (nsap == NULLNA || (na_stack = nsap -> na_stack) != NA_BRG)
358: return NOTOK;
359: na_stack = htons(na_stack);
360: bcopy ((char *)&na_stack, buffer, sizeof(na_stack));
361: bcopy (nsap -> na_dte, &buffer[2], 16);
362: buffer[18] = nsap -> na_dtelen;
363: bcopy (nsap -> na_pid, &buffer[19], 4);
364: buffer[23] = nsap -> na_pidlen;
365: bcopy (nsap -> na_cudf, &buffer[24], 16);
366: buffer[40] = nsap -> na_cudflen;
367: bcopy (nsap -> na_fac, &buffer[41], 6);
368: buffer[47] = nsap -> na_faclen;
369: if ((*writefnx) (fd, buffer, 48) != 48)
370: return NOTOK;
371: return OK;
372: }
373:
374: int bridge_read_nsap_addr (fd, nsap, readfnx)
375: int fd;
376: struct NSAPaddr *nsap;
377: IFP readfnx;
378: {
379: u_short na_stack;
380: char buffer[50];
381:
382: if (readx (fd, buffer, 48, readfnx) != 48)
383: return NOTOK;
384: bcopy (buffer, (char *)&na_stack, sizeof(na_stack));
385: na_stack = ntohs(na_stack);
386: if (na_stack != NA_BRG)
387: return NOTOK;
388: nsap -> na_stack = na_stack;
389: bcopy (&buffer[2], nsap -> na_dte, 16);
390: nsap -> na_dtelen = buffer[18];
391: bcopy (&buffer[19], nsap -> na_pid, 4);
392: nsap -> na_pidlen = buffer[23];
393: bcopy (&buffer[24], nsap -> na_cudf, 16);
394: nsap -> na_cudflen = buffer[40];
395: bcopy (&buffer[41], nsap -> na_fac, 6);
396: nsap -> na_faclen = buffer[47];
397: return OK;
398: }
399:
400: static int readx (fd, buffer, n, readfnx)
401: int fd;
402: char *buffer;
403: int n;
404: IFP readfnx;
405: {
406: register int i,
407: cc;
408: register char *bp;
409:
410: for (bp = buffer, i = n; i > 0; bp += cc, i -= cc) {
411: switch (cc = (*readfnx) (fd, bp, i)) {
412: case NOTOK:
413: return (i = bp - buffer) ? i : NOTOK;
414:
415: case OK:
416: break;
417:
418: default:
419: continue;
420: }
421: break;
422: }
423:
424: return (bp - buffer);
425: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.