2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1997-05-14 06:53:34 adam
10 * Revision 1.14 1996/07/26 12:34:07 quinn
13 * Revision 1.13 1996/07/06 19:58:30 quinn
14 * System headerfiles gathered in yconfig
16 * Revision 1.12 1996/05/22 08:34:44 adam
17 * Added ifdef USE_XTIMOSI; so that 'make depend' works.
19 * Revision 1.11 1996/02/23 10:00:41 quinn
22 * Revision 1.10 1996/02/10 12:23:13 quinn
23 * Enablie inetd operations fro TCP/IP stack
25 * Revision 1.9 1996/01/02 08:57:28 quinn
26 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
28 * Revision 1.8 1995/11/01 13:54:29 quinn
31 * Revision 1.7 1995/10/30 12:41:17 quinn
32 * Added hostname lookup for server.
34 * Revision 1.6 1995/09/29 17:12:00 quinn
37 * Revision 1.5 1995/09/28 10:24:32 quinn
40 * Revision 1.4 1995/09/27 15:02:45 quinn
41 * Modified function heads & prototypes.
43 * Revision 1.3 1995/06/16 10:30:38 quinn
46 * Revision 1.2 1995/06/15 12:30:07 quinn
47 * Added @ as hostname alias for INADDR ANY.
49 * Revision 1.1 1995/06/14 09:58:20 quinn
50 * Renamed yazlib to comstack.
52 * Revision 1.15 1995/05/29 08:12:33 quinn
53 * Updates to aynch. operations.
55 * Revision 1.14 1995/05/16 09:37:31 quinn
58 * Revision 1.13 1995/05/16 08:51:19 quinn
59 * License, documentation, and memory fixes
61 * Revision 1.12 1995/05/02 08:53:24 quinn
62 * Trying in vain to fix comm with ISODE
64 * Revision 1.11 1995/04/21 16:32:08 quinn
65 * *** empty log message ***
67 * Revision 1.10 1995/03/27 08:36:14 quinn
68 * Some work on nonblocking operation in xmosi.c and rfct.c.
69 * Added protocol parameter to cs_create()
71 * Revision 1.9 1995/03/20 09:47:23 quinn
72 * Added server-side support to xmosi.c
73 * Fixed possible problems in rfct
76 * Revision 1.8 1995/03/16 13:29:30 quinn
77 * Beginning to add server-side functions
79 * Revision 1.7 1995/03/14 10:28:47 quinn
80 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
82 * Revision 1.6 1995/03/09 15:22:43 quinn
83 * Fixed two bugs in get/rcv
85 * Revision 1.5 1995/03/07 16:29:47 quinn
88 * Revision 1.4 1995/03/07 10:26:56 quinn
89 * Initialized type field in the comstacks.
91 * Revision 1.3 1995/03/06 16:48:03 quinn
94 * Revision 1.2 1995/03/06 10:54:41 quinn
95 * Server-side functions (t_bind/t_listen/t_accept) seem to work ok, now.
96 * Nonblocking mode needs work (and testing!)
97 * Added makensap to replace function in mosiutil.c.
99 * Revision 1.1 1995/03/01 08:40:33 quinn
100 * First working version of rfct. Addressing needs work.
106 * Glue layer for Peter Furniss' xtimosi package.
115 #include <sys/types.h>
116 #include <sys/socket.h>
118 #include <netinet/in.h>
119 #include <arpa/inet.h>
123 >>>>>>> /tmp/T4a00144
125 #include <comstack.h>
130 int mosi_connect(COMSTACK h, void *address);
131 int mosi_get(COMSTACK h, char **buf, int *bufsize);
132 int mosi_put(COMSTACK h, char *buf, int size);
133 int mosi_more(COMSTACK h) { return 0; } /* not correct */
134 int mosi_close(COMSTACK h);
135 int mosi_rcvconnect(COMSTACK h);
136 int mosi_bind(COMSTACK h, void *address, int mode);
137 int mosi_listen(COMSTACK h, char *addrp, int *addrlen);
138 COMSTACK mosi_accept(COMSTACK h);
139 char *mosi_addrstr(COMSTACK h);
141 typedef struct mosi_state
143 struct t_info info; /* data returned by t_open */
145 int hasread; /* how many bytes read of current PDU */
146 int haswrit; /* how many bytes have we written */
149 static char *oidtostr(int *o)
151 static char buf[512];
156 sprintf(buf + strlen(buf), "%d", *o);
163 static int addopt(struct netbuf *optbuf, unsigned long level, unsigned long
164 name, enum oid_proto proto, enum oid_class class, enum oid_value value)
173 if (!(oid = oid_getoidbyent(&ent)))
176 if (addoidoption(optbuf, level, name, str) < 0)
181 COMSTACK mosi_type(int s, int blocking, int protocol)
190 if (!(r = xmalloc(sizeof(*r))))
192 if (!(state = r->cprivate = xmalloc(sizeof(*state))))
198 r->protocol = protocol;
201 r->blocking = blocking;
202 r->f_connect = mosi_connect;
205 r->f_close = mosi_close;
206 r->f_more = mosi_more;
207 r->f_rcvconnect = mosi_rcvconnect;
208 r->f_bind = mosi_bind;
209 r->f_listen = mosi_listen;
210 r->f_accept = mosi_accept;
211 r->f_addrstr = mosi_addrstr;
215 if ((r->iofile = u_open(CO_MOSI_NAME, flags, &state->info)) < 0)
218 r->timeout = COMSTACK_DEFAULT_TIMEOUT;
223 int hex2oct(char *hex, char *oct)
228 while (sscanf(hex, "%2x", &val) == 1)
232 *((unsigned char*) oct++) = (unsigned char) val;
240 * addressing specific to our hack of OSI transport. A sockaddr_in wrapped
241 * up in a t_mosiaddr in a netbuf (on a stick).
243 struct netbuf MDF *mosi_strtoaddr(const char *str)
245 struct netbuf *ret = xmalloc(sizeof(struct netbuf));
246 struct sockaddr_in *add = xmalloc(sizeof(struct sockaddr_in));
247 struct t_mosiaddr *mosiaddr = xmalloc(sizeof(struct t_mosiaddr));
249 char *p, *b, buf[512], *nsap;
250 short int port = 102;
251 unsigned long tmpadd;
254 assert(ret && add && mosiaddr);
256 mosiaddr->osi_ap_inv_id = NO_INVOKEID;
257 mosiaddr->osi_ae_inv_id = NO_INVOKEID;
259 mosiaddr->osi_apt_len = 0;
260 mosiaddr->osi_aeq_len = 0;
261 p = (char*)MOSI_PADDR(mosiaddr);
262 *(p++) = 0; /* No presentation selector */
264 *(p++) = 0; /* no session selector */
266 /* do we have a transport selector? */
268 if ((nsap = strchr(buf, '/')))
271 if ((*p = hex2oct(buf, p + 1)) < 0)
284 add->sin_family = AF_INET;
286 if ((b = strchr(buf, ':')))
291 add->sin_port = htons(port);
292 if (!strcmp("@", buf))
293 add->sin_addr.s_addr = INADDR_ANY;
294 else if ((hp = gethostbyname(buf)))
295 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list, sizeof(struct in_addr));
296 else if ((tmpadd = inet_addr(buf)) != 0)
297 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
300 *(p++) = (char) sizeof(*add);
302 memcpy(p, add, sizeof(*add));
310 mosiaddr->osi_paddr_len = ll;
311 ret->buf = (char*)mosiaddr;
312 ret->len = ret->maxlen = 100 /* sizeof(*mosiaddr) */ ;
317 int mosi_connect(COMSTACK h, void *address)
319 struct netbuf *addr = address, *local;
320 struct t_call *snd, *rcv;
323 if (!(snd = (struct t_call *) u_alloc(h->iofile, T_CALL, T_ALL)))
325 if (!(rcv = (struct t_call *) u_alloc(h->iofile, T_CALL, T_ALL)))
329 if (addopt(&snd->opt, ISO_APCO, AP_CNTX_NAME, h->protocol, CLASS_APPCTX,
332 if (addopt(&snd->opt, ISO_APCO, AP_ABS_SYN, h->protocol, CLASS_ABSYN,
336 * We don't specify record formats yet.
338 * Xtimosi adds the oid for BER as transfer syntax automatically.
343 if (h->state == CS_UNBND)
345 local = mosi_strtoaddr(""); /* not good in long run */
346 memcpy(&bnd.addr, local, sizeof(*local));
347 if (u_bind(h->iofile, &bnd, 0) < 0)
351 memcpy(&snd->addr, addr, sizeof(*addr));
352 if (u_connect(h->iofile, snd, rcv) < 0)
354 if (t_errno == TNODATA)
361 int mosi_rcvconnect(COMSTACK h)
363 if (u_rcvconnect(h->iofile, 0) < 0)
365 if (t_errno == TNODATA)
372 int mosi_bind(COMSTACK h, void *address, int mode)
378 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
383 if (mode == CS_SERVER)
387 memcpy(&bnd.addr, address, sizeof(struct netbuf));
388 if ((res = u_bind(h->iofile, &bnd, 0)) < 0)
394 int mosi_listen(COMSTACK h, char *addp, int *addrlen)
397 mosi_state *st = h->cprivate;
399 if (!(st->call = (struct t_call*) t_alloc(h->iofile, T_CALL_STR,
402 if ((res = u_listen(h->iofile, st->call)) < 0)
404 if (t_errno == TNODATA)
412 COMSTACK mosi_accept(COMSTACK h)
416 struct mosi_state *st = h->cprivate, *ns;
419 if (h->state != CS_INCON)
421 h->cerrno = CSOUTSTATE;
424 if (!(new = xmalloc(sizeof(*new))))
427 if (!(new->cprivate = ns = xmalloc(sizeof(*ns))))
432 if ((new->iofile = u_open_r(CO_MOSI_NAME, flags, &st->info, st->call)) < 0)
434 if (!(local = mosi_strtoaddr("")))
436 if (mosi_bind(new, local, CS_CLIENT) < 0) /* CS_CLIENT: qlen == 0 */
438 memcpy(&st->call->addr, local, sizeof(st->call->addr));
439 if (u_accept(h->iofile, new->iofile, st->call) < 0)
447 #define CS_MOSI_BUFCHUNK 4096
449 int mosi_get(COMSTACK h, char **buf, int *bufsize)
452 mosi_state *ct = h->cprivate;
459 if (!(*buf = xmalloc(*bufsize = CS_MOSI_BUFCHUNK)))
462 else if (*bufsize - ct->hasread < CS_MOSI_BUFCHUNK)
463 if (!(*buf =xrealloc(*buf, *bufsize *= 2)))
466 if ((res = u_rcv(h->iofile, *buf + ct->hasread, CS_MOSI_BUFCHUNK,
469 if (t_errno == TNODATA)
475 while (flags & T_MORE);
477 /* all done. Reset hasread */
483 int mosi_put(COMSTACK h, char *buf, int size)
485 mosi_state *ct = h->cprivate;
486 int res = u_snd(h->iofile, buf + ct->haswrit, size - ct->haswrit, 0);
488 if (res == size - ct->haswrit)
495 if (t_errno == TFLOW)
503 int mosi_close(COMSTACK h)
512 char *mosi_addrstr(COMSTACK h)
514 return "osi:[UNIMPLEMENTED]";