|
|
1.1 root 1: /* ts2x25.c - TPM: X.25 interface */
2:
3: #ifndef lint
4: static char *rcsid = "$Header: /f/osi/tsap/RCS/ts2x25.c,v 7.3 90/07/09 14:51:23 mrose Exp $";
5: #endif
6:
7: /*
8: * $Header: /f/osi/tsap/RCS/ts2x25.c,v 7.3 90/07/09 14:51:23 mrose Exp $
9: *
10: *
11: * $Log: ts2x25.c,v $
12: * Revision 7.3 90/07/09 14:51:23 mrose
13: * sync
14: *
15: * Revision 7.2 90/03/23 17:31:30 mrose
16: * 8
17: *
18: * Revision 7.1 89/12/07 01:07:39 mrose
19: * queued writes
20: *
21: * Revision 7.0 89/11/23 22:30:41 mrose
22: * Release 6.0
23: *
24: */
25:
26: /*
27: * NOTICE
28: *
29: * Acquisition, use, and distribution of this module and related
30: * materials are subject to the restrictions of a license agreement.
31: * Consult the Preface in the User's Manual for the full terms of
32: * this agreement.
33: *
34: */
35:
36:
37: /* LINTLIBRARY */
38:
39: #include <stdio.h>
40: #include "tpkt.h"
41: #include "tailor.h"
42:
43: #ifdef X25
44: #include "x25.h"
45: #include <sys/ioctl.h>
46:
47: static fd_set inprogress;
48: static struct NSAPaddr **peers = NULL;
49:
50: extern int errno;
51:
52: /* N-CONNECT.REQUEST */
53:
54: int x25open (tb, local, remote, td, async)
55: register struct tsapblk *tb;
56: struct NSAPaddr *local,
57: *remote;
58: struct TSAPdisconnect *td;
59: {
60: register int fd;
61: int onoff;
62:
63: /*
64: * start_x25_client does nothing with its arguments in the CAMTEC
65: * case but there's less #ifdef code this way so...
66: */
67: if ((fd = start_x25_client (local)) == NOTOK)
68: return tsaplose (td, DR_CONGEST, "socket", "unable to start");
69:
70: if (async) {
71: if (ioctl (fd, FIONBIO, (onoff = 1, (char *) &onoff)) < 0) {
72: (void) tsaplose (td, DR_CONGEST, "ioctl", "FIONBIO");
73: (void) close_x25_socket (fd);
74: return NOTOK;
75: }
76: }
77: tb -> tb_fd = fd;
78: (void) XTService (tb);
79:
80: if (join_x25_server (fd, remote) == NOTOK) {
81: if (async)
82: switch (errno) {
83: case EINPROGRESS:
84: if (peers == NULL) {
85: peers = (struct NSAPaddr **)
86: calloc ((unsigned) getdtablesize (),
87: sizeof *peers);
88: if (peers == NULL) {
89: (void) tsaplose (td, DR_CONGEST, NULLCP,
90: "out of memory");
91: (void) close_x25_socket (fd);
92: return (tb -> tb_fd = NOTOK);
93: }
94:
95: FD_ZERO (&inprogress);
96: }
97: if (peers[fd] == NULL
98: && (peers[fd] = (struct NSAPaddr *)
99: malloc (sizeof **peers))
100: == NULL) {
101: (void) tsaplose (td, DR_CONGEST, NULLCP,
102: "out of memory");
103: (void) close_x25_socket (fd);
104: return (tb -> tb_fd = NOTOK);
105: }
106: *(peers[fd]) = *remote; /* struct copy */
107: FD_SET (fd, &inprogress);
108: return OK;
109:
110: case EISCONN:
111: goto done;
112:
113: default:
114: break;
115: }
116:
117: (void) tsaplose (td, DR_REFUSED, "connection", "unable to establish");
118: LLOG (x25_log, LLOG_NOTICE,
119: ("connection to %s failed", na2str (remote)));
120: (void) close_x25_socket (fd);
121: return (tb -> tb_fd = NOTOK);
122: }
123: done: ;
124:
125: if (async)
126: if (ioctl (fd, FIONBIO, (onoff = 0, (char *) &onoff)) < 0) {
127: (void) tsaplose (td, DR_CONGEST, "ioctl", "FIONBIO");
128: (void) close_x25_socket (fd);
129: return NOTOK;
130: }
131:
132: (void) XTService (tb); /* in case pktsize changed... */
133: LLOG (x25_log, LLOG_NOTICE,
134: ("connection %d to %s", fd, na2str (remote)));
135:
136: return DONE;
137: }
138:
139: /* */
140:
141: static int x25retry (tb, td)
142: struct tsapblk *tb;
143: struct TSAPdisconnect *td;
144: {
145: int onoff;
146: int fd = tb -> tb_fd;
147: fd_set mask;
148: struct NSAPaddr *remote = peers[fd];
149:
150: FD_ZERO (&mask);
151: FD_SET (fd, &mask);
152: if (xselect (fd + 1, NULLFD, &mask, NULLFD, 0) < 1)
153: return OK;
154:
155: if (!FD_ISSET (fd, &inprogress))
156: return DONE;
157:
158: if (join_x25_server (fd, remote) == NOTOK) {
159: switch (errno) {
160: case EINPROGRESS:
161: return OK;
162:
163: case EISCONN:
164: goto done;
165:
166: case EINVAL: /* UNIX bug: could be any socket errno, e.g.,
167: ETIMEDOUT */
168: errno = ECONNREFUSED;
169: /* and fall */
170: default:
171: break;
172: }
173:
174: (void) tsaplose (td, DR_REFUSED, "connection", "unable to establish");
175: FD_CLR (fd, &inprogress);
176: (void) close_x25_socket (fd);
177: LLOG (x25_log, LLOG_NOTICE,
178: ("connection to %s failed", na2str (remote)));
179: return (tb -> tb_fd = NOTOK);
180: }
181: done: ;
182:
183: (void) ioctl (fd, FIONBIO, (onoff = 0, (char *) &onoff));
184: FD_CLR (fd, &inprogress);
185:
186: (void) XTService (tb); /* in case pktsize changed... */
187: LLOG (x25_log, LLOG_NOTICE,
188: ("connection %d to %s", fd, na2str (remote)));
189:
190: return DONE;
191: }
192:
193: /* init for read from network */
194:
195: static char nsdu[MAXNSDU];
196: static char *np;
197: static int bl;
198:
199:
200: static int x25init (fd, t)
201: int fd;
202: register struct tsapkt *t;
203: {
204: register int cc;
205:
206: /* XXX: cc should be set to the maximum acceptable NSDU length.
207: Longer NSDUs will be truncated without notification.
208: Should be configurable (or set during N-CONNECT and remembered) */
209:
210: cc = sizeof nsdu;
211:
212: switch (cc = read_x25_socket (fd, nsdu, cc)) {
213: case OK: /* no data ? */
214: case NOTOK:
215: #ifdef SUN_X25
216: if (compat_log -> ll_events & LLOG_EXCEPTIONS)
217: (void) log_cause_and_diag(fd);
218: #endif
219: return DR_NETWORK;
220:
221: default:
222: t -> t_length = cc + sizeof t -> t_pkthdr;
223: break;
224: }
225:
226: if (t -> t_length < TPKT_HDRLEN (t))
227: return DR_LENGTH;
228:
229: t -> t_li = nsdu[0];
230: t -> t_code = nsdu[1];
231:
232: np = nsdu + 2;
233: bl = t -> t_length - TPKT_HDRLEN (t);
234:
235: t -> t_vrsn = TPKT_VRSN; /* Not really needed! */
236:
237: return OK;
238: }
239:
240:
241: /* ARGSUSED */
242:
243: static int read_nsdu_buffer (fd, buffer, cc)
244: int fd;
245: register char *buffer;
246: register int cc;
247: {
248: if (cc > bl)
249: cc = bl;
250:
251: if (cc > 0) {
252: bcopy (np, buffer, cc);
253: np += cc, bl -= cc;
254: }
255:
256: return cc;
257: }
258:
259: /* */
260:
261: /* ARGSUSED */
262:
263: char *x25save (fd, dte1, l1, dte2, l2, td)
264: int fd;
265: char *dte1;
266: int l1;
267: char *dte2;
268: int l2;
269: struct TSAPdisconnect *td;
270: {
271: static char buffer[BUFSIZ];
272:
273: (void) sprintf (buffer, "%c%d %*s %*s",
274: NT_X25, fd, l1, dte1, l2, dte2);
275:
276: return buffer;
277: }
278:
279:
280: int x25restore (tb, buffer, td)
281: register struct tsapblk *tb;
282: char *buffer;
283: struct TSAPdisconnect *td;
284: {
285: int fd;
286: char dte1[NSAP_DTELEN + 1],
287: dte2[NSAP_DTELEN + 1];
288: register struct NSAPaddr *na;
289: register struct tsapADDR *ta;
290:
291: if (sscanf (buffer, "%d %s %s", &fd, dte1, dte2) != 3 || fd < 0)
292: return tsaplose (td, DR_PARAMETER, NULLCP,
293: "bad initialization vector \"%s\"", buffer);
294:
295: ta = &tb -> tb_initiating;
296: ta -> ta_present = 1;
297: na = &ta -> ta_addr;
298: na -> na_stack = NA_X25;
299: na -> na_community = ts_comm_x25_default;
300: bcopy(dte1, na -> na_dte, strlen(dte1));
301: na -> na_dtelen = strlen (na -> na_dte);
302:
303: tb -> tb_fd = fd;
304: (void) XTService (tb);
305:
306: ta = &tb -> tb_responding;
307: ta -> ta_present = 1;
308: na = &ta -> ta_addr;
309: na -> na_stack = NA_X25;
310: na -> na_community = ts_comm_x25_default;
311: bcopy(dte2, na -> na_dte, strlen(dte2));
312: na -> na_dtelen = strlen (na -> na_dte);
313:
314: #ifdef SUN_X25
315: (void) set_x25_facilities (tb -> tb_fd, -1, "Negotiated");
316: #endif
317:
318: return OK;
319: }
320:
321: /* */
322:
323: int XTService (tb)
324: register struct tsapblk *tb;
325: {
326: #ifndef UBC_X25
327: int maxnsdu = MAXNSDU;
328: #else
329: int maxnsdu = X25_PACKETSIZE;
330: #endif
331:
332: tb -> tb_flags |= TB_X25;
333:
334: #ifdef notyet
335: if (recvpktsize > DT_MAGIC && recvpktsize < maxnsdu)
336: maxnsdu = recvpktsize;
337: if (sendpktsize > DT_MAGIC && sendpktsize < maxnsdu)
338: maxnsdu = sendpktsize;
339: #endif
340: tb -> tb_tsdusize = maxnsdu - (tb -> tb_tpduslop = DT_MAGIC);
341:
342: tb -> tb_retryfnx = x25retry;
343:
344: tb -> tb_initfnx = x25init;
345: tb -> tb_readfnx = read_nsdu_buffer;
346: tb -> tb_writefnx = tp0write;
347: tb -> tb_closefnx = close_x25_socket;
348: tb -> tb_selectfnx = select_x25_socket;
349:
350: tp0init (tb);
351: }
352: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.