2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-09-28 10:24:32 quinn
10 * Revision 1.4 1995/09/27 15:02:45 quinn
11 * Modified function heads & prototypes.
13 * Revision 1.3 1995/06/16 10:30:38 quinn
16 * Revision 1.2 1995/06/15 12:30:07 quinn
17 * Added @ as hostname alias for INADDR ANY.
19 * Revision 1.1 1995/06/14 09:58:20 quinn
20 * Renamed yazlib to comstack.
22 * Revision 1.15 1995/05/29 08:12:33 quinn
23 * Updates to aynch. operations.
25 * Revision 1.14 1995/05/16 09:37:31 quinn
28 * Revision 1.13 1995/05/16 08:51:19 quinn
29 * License, documentation, and memory fixes
31 * Revision 1.12 1995/05/02 08:53:24 quinn
32 * Trying in vain to fix comm with ISODE
34 * Revision 1.11 1995/04/21 16:32:08 quinn
35 * *** empty log message ***
37 * Revision 1.10 1995/03/27 08:36:14 quinn
38 * Some work on nonblocking operation in xmosi.c and rfct.c.
39 * Added protocol parameter to cs_create()
41 * Revision 1.9 1995/03/20 09:47:23 quinn
42 * Added server-side support to xmosi.c
43 * Fixed possible problems in rfct
46 * Revision 1.8 1995/03/16 13:29:30 quinn
47 * Beginning to add server-side functions
49 * Revision 1.7 1995/03/14 10:28:47 quinn
50 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
52 * Revision 1.6 1995/03/09 15:22:43 quinn
53 * Fixed two bugs in get/rcv
55 * Revision 1.5 1995/03/07 16:29:47 quinn
58 * Revision 1.4 1995/03/07 10:26:56 quinn
59 * Initialized type field in the comstacks.
61 * Revision 1.3 1995/03/06 16:48:03 quinn
64 * Revision 1.2 1995/03/06 10:54:41 quinn
65 * Server-side functions (t_bind/t_listen/t_accept) seem to work ok, now.
66 * Nonblocking mode needs work (and testing!)
67 * Added makensap to replace function in mosiutil.c.
69 * Revision 1.1 1995/03/01 08:40:33 quinn
70 * First working version of rfct. Addressing needs work.
75 * Glue layer for Peter Furniss' xtimosi package.
83 #include <sys/types.h>
84 #include <sys/socket.h>
86 #include <netinet/in.h>
87 #include <arpa/inet.h>
94 int mosi_connect(COMSTACK h, void *address);
95 int mosi_get(COMSTACK h, char **buf, int *bufsize);
96 int mosi_put(COMSTACK h, char *buf, int size);
97 int mosi_more(COMSTACK h) { return 0; } /* not correct */
98 int mosi_close(COMSTACK h);
99 int mosi_rcvconnect(COMSTACK h);
100 int mosi_bind(COMSTACK h, void *address, int mode);
101 int mosi_listen(COMSTACK h, char *addrp, int *addrlen);
102 COMSTACK mosi_accept(COMSTACK h);
104 typedef struct mosi_state
106 struct t_info info; /* data returned by t_open */
108 int hasread; /* how many bytes read of current PDU */
109 int haswrit; /* how many bytes have we written */
112 static char *oidtostr(int *o)
114 static char buf[512];
119 sprintf(buf + strlen(buf), "%d", *o);
126 static int addopt(struct netbuf *optbuf, unsigned long level, unsigned long
127 name, enum oid_proto proto, enum oid_class class, enum oid_value value)
136 if (!(oid = oid_getoidbyent(&ent)))
139 if (addoidoption(optbuf, level, name, str) < 0)
144 COMSTACK MDF mosi_type(int blocking, int protocol)
150 if (!(r = malloc(sizeof(*r))))
152 if (!(state = r->private = malloc(sizeof(*state))))
158 r->protocol = protocol;
161 r->blocking = blocking;
162 r->f_connect = mosi_connect;
165 r->f_close = mosi_close;
166 r->f_more = mosi_more;
167 r->f_rcvconnect = mosi_rcvconnect;
168 r->f_bind = mosi_bind;
169 r->f_listen = mosi_listen;
170 r->f_accept = mosi_accept;
174 if ((r->iofile = u_open(CO_MOSI_NAME, flags, &state->info)) < 0)
177 r->timeout = COMSTACK_DEFAULT_TIMEOUT;
182 int hex2oct(char *hex, char *oct)
187 while (sscanf(hex, "%2x", &val) == 1)
191 *((unsigned char*) oct++) = (unsigned char) val;
199 * addressing specific to our hack of OSI transport. A sockaddr_in wrapped
200 * up in a t_mosiaddr in a netbuf (on a stick).
202 struct MDF netbuf MDF *mosi_strtoaddr(const char *str)
204 struct netbuf *ret = malloc(sizeof(struct netbuf));
205 struct sockaddr_in *add = malloc(sizeof(struct sockaddr_in));
206 struct t_mosiaddr *mosiaddr = malloc(sizeof(struct t_mosiaddr));
208 char *p, *b, buf[512], *nsap;
209 short int port = 102;
210 unsigned long tmpadd;
213 assert(ret && add && mosiaddr);
215 mosiaddr->osi_ap_inv_id = NO_INVOKEID;
216 mosiaddr->osi_ae_inv_id = NO_INVOKEID;
218 mosiaddr->osi_apt_len = 0;
219 mosiaddr->osi_aeq_len = 0;
220 p = (char*)MOSI_PADDR(mosiaddr);
221 *(p++) = 0; /* No presentation selector */
223 *(p++) = 0; /* no session selector */
225 /* do we have a transport selector? */
227 if ((nsap = strchr(buf, '/')))
230 if ((*p = hex2oct(buf, p + 1)) < 0)
243 add->sin_family = AF_INET;
245 if ((b = strchr(buf, ':')))
250 add->sin_port = htons(port);
251 if (!strcmp("@", buf))
252 add->sin_addr.s_addr = INADDR_ANY;
253 else if ((hp = gethostbyname(buf)))
254 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list, sizeof(struct in_addr));
255 else if ((tmpadd = inet_addr(buf)) != 0)
256 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
259 *(p++) = (char) sizeof(*add);
261 memcpy(p, add, sizeof(*add));
269 mosiaddr->osi_paddr_len = ll;
270 ret->buf = (char*)mosiaddr;
271 ret->len = ret->maxlen = 100 /* sizeof(*mosiaddr) */ ;
276 int mosi_connect(COMSTACK h, void *address)
278 struct netbuf *addr = address, *local;
279 struct t_call *snd, *rcv;
282 if (!(snd = (struct t_call *) u_alloc(h->iofile, T_CALL, T_ALL)))
284 if (!(rcv = (struct t_call *) u_alloc(h->iofile, T_CALL, T_ALL)))
288 if (addopt(&snd->opt, ISO_APCO, AP_CNTX_NAME, h->protocol, CLASS_APPCTX,
291 if (addopt(&snd->opt, ISO_APCO, AP_ABS_SYN, h->protocol, CLASS_ABSYN,
295 * We don't specify record formats yet.
297 * Xtimosi adds the oid for BER as transfer syntax automatically.
302 if (h->state == CS_UNBND)
304 local = mosi_strtoaddr(""); /* not good in long run */
305 memcpy(&bnd.addr, local, sizeof(*local));
306 if (u_bind(h->iofile, &bnd, 0) < 0)
310 memcpy(&snd->addr, addr, sizeof(*addr));
311 if (u_connect(h->iofile, snd, rcv) < 0)
313 if (t_errno == TNODATA)
320 int mosi_rcvconnect(COMSTACK h)
322 if (u_rcvconnect(h->iofile, 0) < 0)
324 if (t_errno == TNODATA)
331 int mosi_bind(COMSTACK h, void *address, int mode)
337 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
342 if (mode == CS_SERVER)
346 memcpy(&bnd.addr, address, sizeof(struct netbuf));
347 if ((res = u_bind(h->iofile, &bnd, 0)) < 0)
353 int mosi_listen(COMSTACK h, char *addp, int *addrlen)
356 mosi_state *st = h->private;
358 if (!(st->call = (struct t_call*) t_alloc(h->iofile, T_CALL_STR,
361 if ((res = u_listen(h->iofile, st->call)) < 0)
363 if (t_errno == TNODATA)
371 COMSTACK mosi_accept(COMSTACK h)
375 struct mosi_state *st = h->private, *ns;
378 if (h->state != CS_INCON)
380 h->cerrno = CSOUTSTATE;
383 if (!(new = malloc(sizeof(*new))))
386 if (!(new->private = ns = malloc(sizeof(*ns))))
391 if ((new->iofile = u_open_r(CO_MOSI_NAME, flags, &st->info, st->call)) < 0)
393 if (!(local = mosi_strtoaddr("")))
395 if (mosi_bind(new, local, CS_CLIENT) < 0) /* CS_CLIENT: qlen == 0 */
397 memcpy(&st->call->addr, local, sizeof(st->call->addr));
398 if (u_accept(h->iofile, new->iofile, st->call) < 0)
406 #define CS_MOSI_BUFCHUNK 4096
408 int mosi_get(COMSTACK h, char **buf, int *bufsize)
411 mosi_state *ct = h->private;
418 if (!(*buf = malloc(*bufsize = CS_MOSI_BUFCHUNK)))
421 else if (*bufsize - ct->hasread < CS_MOSI_BUFCHUNK)
422 if (!(*buf = realloc(*buf, *bufsize *= 2)))
425 if ((res = u_rcv(h->iofile, *buf + ct->hasread, CS_MOSI_BUFCHUNK,
428 if (t_errno == TNODATA)
434 while (flags & T_MORE);
436 /* all done. Reset hasread */
442 int mosi_put(COMSTACK h, char *buf, int size)
444 mosi_state *ct = h->private;
445 int res = u_snd(h->iofile, buf + ct->haswrit, size - ct->haswrit, 0);
447 if (res == size - ct->haswrit)
454 if (t_errno == TFLOW)
462 int mosi_close(COMSTACK h)