2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1997-09-17 12:10:30 adam
10 * Revision 1.15 1997/05/14 06:53:34 adam
13 * Revision 1.14 1996/07/26 12:34:07 quinn
16 * Revision 1.13 1996/07/06 19:58:30 quinn
17 * System headerfiles gathered in yconfig
19 * Revision 1.12 1996/05/22 08:34:44 adam
20 * Added ifdef USE_XTIMOSI; so that 'make depend' works.
22 * Revision 1.11 1996/02/23 10:00:41 quinn
25 * Revision 1.10 1996/02/10 12:23:13 quinn
26 * Enablie inetd operations fro TCP/IP stack
28 * Revision 1.9 1996/01/02 08:57:28 quinn
29 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
31 * Revision 1.8 1995/11/01 13:54:29 quinn
34 * Revision 1.7 1995/10/30 12:41:17 quinn
35 * Added hostname lookup for server.
37 * Revision 1.6 1995/09/29 17:12:00 quinn
40 * Revision 1.5 1995/09/28 10:24:32 quinn
43 * Revision 1.4 1995/09/27 15:02:45 quinn
44 * Modified function heads & prototypes.
46 * Revision 1.3 1995/06/16 10:30:38 quinn
49 * Revision 1.2 1995/06/15 12:30:07 quinn
50 * Added @ as hostname alias for INADDR ANY.
52 * Revision 1.1 1995/06/14 09:58:20 quinn
53 * Renamed yazlib to comstack.
55 * Revision 1.15 1995/05/29 08:12:33 quinn
56 * Updates to aynch. operations.
58 * Revision 1.14 1995/05/16 09:37:31 quinn
61 * Revision 1.13 1995/05/16 08:51:19 quinn
62 * License, documentation, and memory fixes
64 * Revision 1.12 1995/05/02 08:53:24 quinn
65 * Trying in vain to fix comm with ISODE
67 * Revision 1.11 1995/04/21 16:32:08 quinn
68 * *** empty log message ***
70 * Revision 1.10 1995/03/27 08:36:14 quinn
71 * Some work on nonblocking operation in xmosi.c and rfct.c.
72 * Added protocol parameter to cs_create()
74 * Revision 1.9 1995/03/20 09:47:23 quinn
75 * Added server-side support to xmosi.c
76 * Fixed possible problems in rfct
79 * Revision 1.8 1995/03/16 13:29:30 quinn
80 * Beginning to add server-side functions
82 * Revision 1.7 1995/03/14 10:28:47 quinn
83 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
85 * Revision 1.6 1995/03/09 15:22:43 quinn
86 * Fixed two bugs in get/rcv
88 * Revision 1.5 1995/03/07 16:29:47 quinn
91 * Revision 1.4 1995/03/07 10:26:56 quinn
92 * Initialized type field in the comstacks.
94 * Revision 1.3 1995/03/06 16:48:03 quinn
97 * Revision 1.2 1995/03/06 10:54:41 quinn
98 * Server-side functions (t_bind/t_listen/t_accept) seem to work ok, now.
99 * Nonblocking mode needs work (and testing!)
100 * Added makensap to replace function in mosiutil.c.
102 * Revision 1.1 1995/03/01 08:40:33 quinn
103 * First working version of rfct. Addressing needs work.
109 * Glue layer for Peter Furniss' xtimosi package.
120 #include <comstack.h>
125 int mosi_connect(COMSTACK h, void *address);
126 int mosi_get(COMSTACK h, char **buf, int *bufsize);
127 int mosi_put(COMSTACK h, char *buf, int size);
128 int mosi_more(COMSTACK h) { return 0; } /* not correct */
129 int mosi_close(COMSTACK h);
130 int mosi_rcvconnect(COMSTACK h);
131 int mosi_bind(COMSTACK h, void *address, int mode);
132 int mosi_listen(COMSTACK h, char *addrp, int *addrlen);
133 COMSTACK mosi_accept(COMSTACK h);
134 char *mosi_addrstr(COMSTACK h);
135 void *mosi_straddr(COMSTACK h, const char *str);
137 typedef struct mosi_state
139 struct t_info info; /* data returned by t_open */
141 int hasread; /* how many bytes read of current PDU */
142 int haswrit; /* how many bytes have we written */
143 struct netbuf netbuf;
146 static char *oidtostr(int *o)
148 static char buf[512];
153 sprintf(buf + strlen(buf), "%d", *o);
160 static int addopt(struct netbuf *optbuf, unsigned long level, unsigned long
161 name, enum oid_proto proto, enum oid_class class, enum oid_value value)
170 if (!(oid = oid_getoidbyent(&ent)))
173 if (addoidoption(optbuf, level, name, str) < 0)
178 COMSTACK mosi_type(int s, int blocking, int protocol)
187 if (!(r = xmalloc(sizeof(*r))))
189 if (!(state = r->cprivate = xmalloc(sizeof(*state))))
195 r->protocol = protocol;
198 r->blocking = blocking;
199 r->f_connect = mosi_connect;
202 r->f_close = mosi_close;
203 r->f_more = mosi_more;
204 r->f_rcvconnect = mosi_rcvconnect;
205 r->f_bind = mosi_bind;
206 r->f_listen = mosi_listen;
207 r->f_accept = mosi_accept;
208 r->f_addrstr = mosi_addrstr;
209 r->r_straddr = mosi_straddr;
213 if ((r->iofile = u_open(CO_MOSI_NAME, flags, &state->info)) < 0)
216 r->timeout = COMSTACK_DEFAULT_TIMEOUT;
221 int hex2oct(char *hex, char *oct)
226 while (sscanf(hex, "%2x", &val) == 1)
230 *((unsigned char*) oct++) = (unsigned char) val;
238 * addressing specific to our hack of OSI transport. A sockaddr_in wrapped
239 * up in a t_mosiaddr in a netbuf (on a stick).
242 int MDF *mosi_strtoaddr_ex(const char *str, struct netbuf *ret)
244 struct sockaddr_in *add = xmalloc(sizeof(struct sockaddr_in));
245 struct t_mosiaddr *mosiaddr = xmalloc(sizeof(struct t_mosiaddr));
247 char *p, *b, buf[512], *nsap;
248 short int port = 102;
249 unsigned long tmpadd;
252 assert(ret && add && mosiaddr);
254 mosiaddr->osi_ap_inv_id = NO_INVOKEID;
255 mosiaddr->osi_ae_inv_id = NO_INVOKEID;
257 mosiaddr->osi_apt_len = 0;
258 mosiaddr->osi_aeq_len = 0;
259 p = (char*)MOSI_PADDR(mosiaddr);
260 *(p++) = 0; /* No presentation selector */
262 *(p++) = 0; /* no session selector */
264 /* do we have a transport selector? */
266 if ((nsap = strchr(buf, '/')))
269 if ((*p = hex2oct(buf, p + 1)) < 0)
282 add->sin_family = AF_INET;
284 if ((b = strchr(buf, ':')))
289 add->sin_port = htons(port);
290 if (!strcmp("@", buf))
291 add->sin_addr.s_addr = INADDR_ANY;
292 else if ((hp = gethostbyname(buf)))
293 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list, sizeof(struct in_addr));
294 else if ((tmpadd = inet_addr(buf)) != 0)
295 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
298 *(p++) = (char) sizeof(*add);
300 memcpy(p, add, sizeof(*add));
308 mosiaddr->osi_paddr_len = ll;
309 ret->buf = (char*)mosiaddr;
310 ret->len = ret->maxlen = 100 /* sizeof(*mosiaddr) */ ;
315 struct netbuf MDF *mosi_strtoaddr(const char *str)
317 struct netbuf *ret = xmalloc(sizeof(struct netbuf));
319 if (!mosi_strtoaddr_ex (str, ret))
327 struct netbuf MDF *mosi_straddr(COMSTACK h, const char *str)
329 mosi_state *st = h->cprivate;
330 struct netbuf *ret = &st->netbuf;
332 if (!mosi_strtoaddr_ex (str, ret))
340 int mosi_connect(COMSTACK h, void *address)
342 struct netbuf *addr = address, *local;
343 struct t_call *snd, *rcv;
346 if (!(snd = (struct t_call *) u_alloc(h->iofile, T_CALL, T_ALL)))
348 if (!(rcv = (struct t_call *) u_alloc(h->iofile, T_CALL, T_ALL)))
352 if (addopt(&snd->opt, ISO_APCO, AP_CNTX_NAME, h->protocol, CLASS_APPCTX,
355 if (addopt(&snd->opt, ISO_APCO, AP_ABS_SYN, h->protocol, CLASS_ABSYN,
359 * We don't specify record formats yet.
361 * Xtimosi adds the oid for BER as transfer syntax automatically.
366 if (h->state == CS_UNBND)
368 local = mosi_strtoaddr(""); /* not good in long run */
369 memcpy(&bnd.addr, local, sizeof(*local));
370 if (u_bind(h->iofile, &bnd, 0) < 0)
374 memcpy(&snd->addr, addr, sizeof(*addr));
375 if (u_connect(h->iofile, snd, rcv) < 0)
377 if (t_errno == TNODATA)
384 int mosi_rcvconnect(COMSTACK h)
386 if (u_rcvconnect(h->iofile, 0) < 0)
388 if (t_errno == TNODATA)
395 int mosi_bind(COMSTACK h, void *address, int mode)
401 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
406 if (mode == CS_SERVER)
410 memcpy(&bnd.addr, address, sizeof(struct netbuf));
411 if ((res = u_bind(h->iofile, &bnd, 0)) < 0)
417 int mosi_listen(COMSTACK h, char *addp, int *addrlen)
420 mosi_state *st = h->cprivate;
422 if (!(st->call = (struct t_call*) t_alloc(h->iofile, T_CALL_STR,
425 if ((res = u_listen(h->iofile, st->call)) < 0)
427 if (t_errno == TNODATA)
435 COMSTACK mosi_accept(COMSTACK h)
439 struct mosi_state *st = h->cprivate, *ns;
442 if (h->state != CS_INCON)
444 h->cerrno = CSOUTSTATE;
447 if (!(new = xmalloc(sizeof(*new))))
450 if (!(new->cprivate = ns = xmalloc(sizeof(*ns))))
455 if ((new->iofile = u_open_r(CO_MOSI_NAME, flags, &st->info, st->call)) < 0)
457 if (!(local = mosi_strtoaddr("")))
459 if (mosi_bind(new, local, CS_CLIENT) < 0) /* CS_CLIENT: qlen == 0 */
461 memcpy(&st->call->addr, local, sizeof(st->call->addr));
462 if (u_accept(h->iofile, new->iofile, st->call) < 0)
470 #define CS_MOSI_BUFCHUNK 4096
472 int mosi_get(COMSTACK h, char **buf, int *bufsize)
475 mosi_state *ct = h->cprivate;
482 if (!(*buf = xmalloc(*bufsize = CS_MOSI_BUFCHUNK)))
485 else if (*bufsize - ct->hasread < CS_MOSI_BUFCHUNK)
486 if (!(*buf =xrealloc(*buf, *bufsize *= 2)))
489 if ((res = u_rcv(h->iofile, *buf + ct->hasread, CS_MOSI_BUFCHUNK,
492 if (t_errno == TNODATA)
498 while (flags & T_MORE);
500 /* all done. Reset hasread */
506 int mosi_put(COMSTACK h, char *buf, int size)
508 mosi_state *ct = h->cprivate;
509 int res = u_snd(h->iofile, buf + ct->haswrit, size - ct->haswrit, 0);
511 if (res == size - ct->haswrit)
518 if (t_errno == TFLOW)
526 int mosi_close(COMSTACK h)
535 char *mosi_addrstr(COMSTACK h)
537 return "osi:[UNIMPLEMENTED]";