2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1996-01-02 08:57:28 quinn
8 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
10 * Revision 1.8 1995/11/01 13:54:29 quinn
13 * Revision 1.7 1995/10/30 12:41:17 quinn
14 * Added hostname lookup for server.
16 * Revision 1.6 1995/09/29 17:12:00 quinn
19 * Revision 1.5 1995/09/28 10:24:32 quinn
22 * Revision 1.4 1995/09/27 15:02:45 quinn
23 * Modified function heads & prototypes.
25 * Revision 1.3 1995/06/16 10:30:38 quinn
28 * Revision 1.2 1995/06/15 12:30:07 quinn
29 * Added @ as hostname alias for INADDR ANY.
31 * Revision 1.1 1995/06/14 09:58:20 quinn
32 * Renamed yazlib to comstack.
34 * Revision 1.15 1995/05/29 08:12:33 quinn
35 * Updates to aynch. operations.
37 * Revision 1.14 1995/05/16 09:37:31 quinn
40 * Revision 1.13 1995/05/16 08:51:19 quinn
41 * License, documentation, and memory fixes
43 * Revision 1.12 1995/05/02 08:53:24 quinn
44 * Trying in vain to fix comm with ISODE
46 * Revision 1.11 1995/04/21 16:32:08 quinn
47 * *** empty log message ***
49 * Revision 1.10 1995/03/27 08:36:14 quinn
50 * Some work on nonblocking operation in xmosi.c and rfct.c.
51 * Added protocol parameter to cs_create()
53 * Revision 1.9 1995/03/20 09:47:23 quinn
54 * Added server-side support to xmosi.c
55 * Fixed possible problems in rfct
58 * Revision 1.8 1995/03/16 13:29:30 quinn
59 * Beginning to add server-side functions
61 * Revision 1.7 1995/03/14 10:28:47 quinn
62 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
64 * Revision 1.6 1995/03/09 15:22:43 quinn
65 * Fixed two bugs in get/rcv
67 * Revision 1.5 1995/03/07 16:29:47 quinn
70 * Revision 1.4 1995/03/07 10:26:56 quinn
71 * Initialized type field in the comstacks.
73 * Revision 1.3 1995/03/06 16:48:03 quinn
76 * Revision 1.2 1995/03/06 10:54:41 quinn
77 * Server-side functions (t_bind/t_listen/t_accept) seem to work ok, now.
78 * Nonblocking mode needs work (and testing!)
79 * Added makensap to replace function in mosiutil.c.
81 * Revision 1.1 1995/03/01 08:40:33 quinn
82 * First working version of rfct. Addressing needs work.
87 * Glue layer for Peter Furniss' xtimosi package.
95 #include <sys/types.h>
96 #include <sys/socket.h>
98 #include <netinet/in.h>
99 #include <arpa/inet.h>
101 #include <comstack.h>
106 int mosi_connect(COMSTACK h, void *address);
107 int mosi_get(COMSTACK h, char **buf, int *bufsize);
108 int mosi_put(COMSTACK h, char *buf, int size);
109 int mosi_more(COMSTACK h) { return 0; } /* not correct */
110 int mosi_close(COMSTACK h);
111 int mosi_rcvconnect(COMSTACK h);
112 int mosi_bind(COMSTACK h, void *address, int mode);
113 int mosi_listen(COMSTACK h, char *addrp, int *addrlen);
114 COMSTACK mosi_accept(COMSTACK h);
115 char *mosi_addrstr(COMSTACK h);
117 typedef struct mosi_state
119 struct t_info info; /* data returned by t_open */
121 int hasread; /* how many bytes read of current PDU */
122 int haswrit; /* how many bytes have we written */
125 static char *oidtostr(int *o)
127 static char buf[512];
132 sprintf(buf + strlen(buf), "%d", *o);
139 static int addopt(struct netbuf *optbuf, unsigned long level, unsigned long
140 name, enum oid_proto proto, enum oid_class class, enum oid_value value)
149 if (!(oid = oid_getoidbyent(&ent)))
152 if (addoidoption(optbuf, level, name, str) < 0)
157 COMSTACK mosi_type(int blocking, int protocol)
163 if (!(r = xmalloc(sizeof(*r))))
165 if (!(state = r->private = xmalloc(sizeof(*state))))
171 r->protocol = protocol;
174 r->blocking = blocking;
175 r->f_connect = mosi_connect;
178 r->f_close = mosi_close;
179 r->f_more = mosi_more;
180 r->f_rcvconnect = mosi_rcvconnect;
181 r->f_bind = mosi_bind;
182 r->f_listen = mosi_listen;
183 r->f_accept = mosi_accept;
184 r->f_addrstr = mosi_addrstr;
188 if ((r->iofile = u_open(CO_MOSI_NAME, flags, &state->info)) < 0)
191 r->timeout = COMSTACK_DEFAULT_TIMEOUT;
196 int hex2oct(char *hex, char *oct)
201 while (sscanf(hex, "%2x", &val) == 1)
205 *((unsigned char*) oct++) = (unsigned char) val;
213 * addressing specific to our hack of OSI transport. A sockaddr_in wrapped
214 * up in a t_mosiaddr in a netbuf (on a stick).
216 struct netbuf MDF *mosi_strtoaddr(const char *str)
218 struct netbuf *ret = xmalloc(sizeof(struct netbuf));
219 struct sockaddr_in *add = xmalloc(sizeof(struct sockaddr_in));
220 struct t_mosiaddr *mosiaddr = xmalloc(sizeof(struct t_mosiaddr));
222 char *p, *b, buf[512], *nsap;
223 short int port = 102;
224 unsigned long tmpadd;
227 assert(ret && add && mosiaddr);
229 mosiaddr->osi_ap_inv_id = NO_INVOKEID;
230 mosiaddr->osi_ae_inv_id = NO_INVOKEID;
232 mosiaddr->osi_apt_len = 0;
233 mosiaddr->osi_aeq_len = 0;
234 p = (char*)MOSI_PADDR(mosiaddr);
235 *(p++) = 0; /* No presentation selector */
237 *(p++) = 0; /* no session selector */
239 /* do we have a transport selector? */
241 if ((nsap = strchr(buf, '/')))
244 if ((*p = hex2oct(buf, p + 1)) < 0)
257 add->sin_family = AF_INET;
259 if ((b = strchr(buf, ':')))
264 add->sin_port = htons(port);
265 if (!strcmp("@", buf))
266 add->sin_addr.s_addr = INADDR_ANY;
267 else if ((hp = gethostbyname(buf)))
268 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list, sizeof(struct in_addr));
269 else if ((tmpadd = inet_addr(buf)) != 0)
270 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
273 *(p++) = (char) sizeof(*add);
275 memcpy(p, add, sizeof(*add));
283 mosiaddr->osi_paddr_len = ll;
284 ret->buf = (char*)mosiaddr;
285 ret->len = ret->maxlen = 100 /* sizeof(*mosiaddr) */ ;
290 int mosi_connect(COMSTACK h, void *address)
292 struct netbuf *addr = address, *local;
293 struct t_call *snd, *rcv;
296 if (!(snd = (struct t_call *) u_alloc(h->iofile, T_CALL, T_ALL)))
298 if (!(rcv = (struct t_call *) u_alloc(h->iofile, T_CALL, T_ALL)))
302 if (addopt(&snd->opt, ISO_APCO, AP_CNTX_NAME, h->protocol, CLASS_APPCTX,
305 if (addopt(&snd->opt, ISO_APCO, AP_ABS_SYN, h->protocol, CLASS_ABSYN,
309 * We don't specify record formats yet.
311 * Xtimosi adds the oid for BER as transfer syntax automatically.
316 if (h->state == CS_UNBND)
318 local = mosi_strtoaddr(""); /* not good in long run */
319 memcpy(&bnd.addr, local, sizeof(*local));
320 if (u_bind(h->iofile, &bnd, 0) < 0)
324 memcpy(&snd->addr, addr, sizeof(*addr));
325 if (u_connect(h->iofile, snd, rcv) < 0)
327 if (t_errno == TNODATA)
334 int mosi_rcvconnect(COMSTACK h)
336 if (u_rcvconnect(h->iofile, 0) < 0)
338 if (t_errno == TNODATA)
345 int mosi_bind(COMSTACK h, void *address, int mode)
351 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
356 if (mode == CS_SERVER)
360 memcpy(&bnd.addr, address, sizeof(struct netbuf));
361 if ((res = u_bind(h->iofile, &bnd, 0)) < 0)
367 int mosi_listen(COMSTACK h, char *addp, int *addrlen)
370 mosi_state *st = h->private;
372 if (!(st->call = (struct t_call*) t_alloc(h->iofile, T_CALL_STR,
375 if ((res = u_listen(h->iofile, st->call)) < 0)
377 if (t_errno == TNODATA)
385 COMSTACK mosi_accept(COMSTACK h)
389 struct mosi_state *st = h->private, *ns;
392 if (h->state != CS_INCON)
394 h->cerrno = CSOUTSTATE;
397 if (!(new = xmalloc(sizeof(*new))))
400 if (!(new->private = ns = xmalloc(sizeof(*ns))))
405 if ((new->iofile = u_open_r(CO_MOSI_NAME, flags, &st->info, st->call)) < 0)
407 if (!(local = mosi_strtoaddr("")))
409 if (mosi_bind(new, local, CS_CLIENT) < 0) /* CS_CLIENT: qlen == 0 */
411 memcpy(&st->call->addr, local, sizeof(st->call->addr));
412 if (u_accept(h->iofile, new->iofile, st->call) < 0)
420 #define CS_MOSI_BUFCHUNK 4096
422 int mosi_get(COMSTACK h, char **buf, int *bufsize)
425 mosi_state *ct = h->private;
432 if (!(*buf = xmalloc(*bufsize = CS_MOSI_BUFCHUNK)))
435 else if (*bufsize - ct->hasread < CS_MOSI_BUFCHUNK)
436 if (!(*buf =xrealloc(*buf, *bufsize *= 2)))
439 if ((res = u_rcv(h->iofile, *buf + ct->hasread, CS_MOSI_BUFCHUNK,
442 if (t_errno == TNODATA)
448 while (flags & T_MORE);
450 /* all done. Reset hasread */
456 int mosi_put(COMSTACK h, char *buf, int size)
458 mosi_state *ct = h->private;
459 int res = u_snd(h->iofile, buf + ct->haswrit, size - ct->haswrit, 0);
461 if (res == size - ct->haswrit)
468 if (t_errno == TFLOW)
476 int mosi_close(COMSTACK h)
485 char *mosi_addrstr(COMSTACK h)
487 return "osi:[UNIMPLEMENTED";