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