4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1995-11-01 13:54:52 quinn
10 * Revision 1.7 1995/06/16 10:46:48 quinn
11 * *** empty log message ***
13 * Revision 1.6 1995/06/15 07:45:11 quinn
16 * Revision 1.5 1995/05/31 08:29:35 quinn
17 * Nothing significant.
19 * Revision 1.4 1995/05/18 13:02:07 quinn
22 * Revision 1.3 1995/05/16 09:37:18 quinn
25 * Revision 1.2 1995/05/02 08:53:09 quinn
26 * Trying in vain to fix comm with ISODE
28 * Revision 1.1 1995/03/30 14:03:17 quinn
29 * Added RFC1006 as separate library
31 * Revision 1.15 1995/03/30 10:54:43 quinn
32 * Fiddling with packet sizes, trying to help ISODE.. :(
34 * Revision 1.14 1995/03/27 08:36:07 quinn
35 * Some work on nonblocking operation in xmosi.c and rfct.c.
36 * Added protocol parameter to cs_create()
38 * Revision 1.13 1995/03/20 11:27:16 quinn
39 * Fixed bug in the _t_rcv stuff
41 * Revision 1.12 1995/03/20 09:54:07 quinn
44 * Revision 1.11 1995/03/20 09:47:16 quinn
45 * Added server-side support to xmosi.c
46 * Fixed possible problems in rfct
49 * Revision 1.10 1995/03/17 19:37:26 quinn
50 * *** empty log message ***
52 * Revision 1.9 1995/03/17 19:28:32 quinn
53 * Working on fixing our mystery-bug.
55 * Revision 1.8 1995/03/14 10:28:38 quinn
56 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
58 * Revision 1.7 1995/03/09 18:42:32 quinn
59 * Fixed another bug in t_rcv
61 * Revision 1.6 1995/03/09 15:22:42 quinn
62 * Fixed two bugs in get/rcv
64 * Revision 1.5 1995/03/07 16:29:46 quinn
67 * Revision 1.4 1995/03/06 16:48:03 quinn
70 * Revision 1.3 1995/03/06 10:54:32 quinn
71 * Server-side functions (t_bind/t_listen/t_accept) seem to work ok, now.
72 * Nonblocking mode needs work (and testing!)
73 * Added makensap to replace function in mosiutil.c.
75 * Revision 1.2 1995/03/03 09:40:36 quinn
76 * Added most of the remaining functionality.
77 * Still need exstensive testing of server-side functions and nonblocking
80 * Revision 1.1 1995/03/01 08:40:33 quinn
81 * First working version of rfct. Addressing needs work.
86 * Simple implementation of XTI/TP0/RFC1006/Sockets.
87 * Note: There is still some work to do in here, but basically it works.
89 * TODO: Asynchronous mode needs a lot of little adjustments to various
90 * return values and error codes, etc. Formally, we should probably hold
91 * this up against the protocol state tables, and see if it's correct.
93 * Check if addressing info is returned correctly by all calls.
98 #include <sys/types.h>
99 #include <sys/param.h>
100 #include <sys/socket.h>
102 #include <netinet/in.h>
103 #include <arpa/inet.h>
115 #include <linux/limits.h>
118 #ifdef TRACE_TRANSPORT
124 #define TSEL_MAXLEN 20 /* is there a standard for this? */
125 #define RFC_DEFAULT_PORT 4500
126 #define MAX_QLEN 5 /* outstanding connect indications */
128 static int t_look_wait(int fd, int wait);
129 int t_rcvconnect(int fd, struct t_call *call);
134 static struct rfct_control
136 int state; /* state of transport endpoint */
137 int event; /* current event */
138 int tsize; /* max TPDU size */
139 int curcode; /* code of current TPDU */
140 int pending; /* # bytes of user data waiting on line */
141 int eot_flag; /* current incoming TPDU had EOT set */
142 int hlen; /* TPDU header suffix remaining on line */
143 int eot; /* current incoming DATA TPDU is last in TSDU */
144 int togo; /* # bytes waiting to go on current outgoing DATA */
145 int qlen; /* set by t_bind */
146 int listen; /* we are listening */
147 int tmpfd; /* holding space for client between listen and accept */
148 int flags; /* set by t_open */
149 char rref[2]; /* remote reference */
150 char ltsel[TSEL_MAXLEN]; /* local transport selector */
152 int oci[MAX_QLEN]; /* outstanding connect indications */
156 * In the best tradition of ThinOSI, we combine the RFC1006 and the common
157 * part of the TPDU header into one. Given the complexity of the transport
158 * layer, maybe it would have been nice to have it in a separate module
159 * for reuse - but such is hindsight.
160 * Using a bit-field like this may be dangerous. I would expect it to work
161 * fine on any 32-bit or 64-bit machine, with any decent compiler. A DOS
162 * compiler might make a mess of it, I suppose, but I wouldn't even expect
169 #define RFC_VERSION 3
171 unsigned len:16; /* length of entire package, including header */
172 /* TPDU common fields */
173 unsigned char hlen; /* length of TPDU-header minus length-octet */
174 unsigned char code; /* TPDU code (and an unused 4-bit field) */
175 char suffix[100]; /* unstructured TPDU elements, for convenience */
179 * The TPDU-codes used by this package.
181 #define TPDU_CODE_CREQ 0xe0
182 #define TPDU_CODE_CCON 0xd0
183 #define TPDU_CODE_DATA 0xf0
184 #define TPDU_CODE_DREQ 0x80
187 * Parameters that we care about.
189 #define TPDU_PARM_TSIZE 0xc0 /* max TPDU size */
190 #define TPDU_PARM_CLGID 0xc1 /* Calling TSAP-ID */
191 #define TPDU_PARM_CLDID 0xc2 /* Called TSAP-ID */
193 struct tpdu_connect_header /* length of fixed suffix: 5 octets */
195 char dst_ref[2]; /* 3-4 - not used by TP0 */
196 char src_ref[2]; /* 5-6 - not used by TP0 */
197 char class; /* 7 - always 0 */
200 struct tpdu_disconnect_header /* length of fixed suffix: 5 octets */
207 struct tpdu_data_header /* length of fixed suffix: 1 octet */
209 unsigned char nr; /* we only use bit 7 (1 << 7), to mark EOT */
210 #define DATA_EOT (1<<7)
213 static int rfc_running = 0; /* Have we been initialized? */
215 static void init_rfc(void)
219 for (i = 0; i < NOFILE; i++)
224 int t_open(char *name, int oflag, struct t_info *info)
226 struct rfct_control *cnt;
227 struct protoent *proto;
230 TRC(fprintf(stderr, "T_OPEN\n"));
235 if (!(proto = getprotobyname("tcp")))
237 if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
239 #ifdef NONBLOCKING_OSI
240 if ((oflag & O_NONBLOCK) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
246 if (!(cnt = control[s] = malloc(sizeof(struct rfct_control))))
248 TRC(perror("malloc()"));
255 cnt->state = T_UNBND;
262 for (i = 0; i < MAX_QLEN; i++)
266 * RFC1006 sets a higher than standard (TP) default max TPDU size, but the
267 * Isode seems to like to negotiate it down. We'll keep it here to be
268 * safe. Note that there's no harm in jumping it up. If it's higher
269 * than 2048, t_connect won't try to negotiate.
275 info->addr = TSEL_MAXLEN + sizeof(struct sockaddr_in) + 1;
276 info->options = 1024;
277 info->tsdu = -1; /* is this right? */
281 info->servtype = T_COTS_ORD; /* lets hope our user doesn't
282 try something funny. */
287 int t_connect(int fd, struct t_call *sndcall, struct t_call *rcvcall)
289 struct rfct_control *cnt = control[fd];
290 struct sockaddr_in addr;
292 struct iovec vec[3]; /* RFC1006 header + T-header + parms */
293 struct rfc_header rfc;
294 struct tpdu_connect_header tpdu;
295 unsigned char pbuf[2 + TSEL_MAXLEN + 3]; /* CR parameters */
298 TRC(fprintf(stderr, "T_CONNECT\n"));
299 if (!cnt || cnt->state != T_IDLE)
301 TRC(fprintf(stderr, "TOUTSTATE\n"));
305 /* take the address apart */
306 p = sndcall->addr.buf;
307 if (*p) /* transport selector */
309 TRC(fprintf(stderr, "Tsel length is %d.\n", *p));
310 pbuf[0] = TPDU_PARM_CLDID;
312 memcpy(pbuf + 2, p + 1, *p);
315 p += *p + 1; /* skip tsel */
316 if (*p != sizeof(addr))
318 TRC(fprintf(stderr, "Expected sockaddr here.\n"));
323 memcpy(&addr, p, sizeof(addr));
324 if (connect(fd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
331 * If the default set by t_open() is higher than 2048, we don't try
332 * to negotiate, hoping that the opponent goes by the high default
333 * set by RFC1006. Otherwise, we just go for 2048 (max according to
334 * transport standard). The rest of this module doesn't really care
335 * about the size (although it's respected, of course), since we do
336 * no internal buffering.
338 if (cnt->tsize <= 2048)
340 pbuf[plen++] = TPDU_PARM_TSIZE;
342 pbuf[plen++] = 0x0b; /* request max PDU size (2048 octets) */
345 rfc.version = RFC_VERSION;
347 rfc.len = htons(4 + 7 + plen);
349 rfc.code = TPDU_CODE_CREQ;
351 memset(tpdu.dst_ref, 0, 2);
352 memset(tpdu.src_ref, 0, 2);
355 vec[0].iov_base = (caddr_t) &rfc;
357 vec[1].iov_base = (caddr_t) &tpdu;
359 vec[2].iov_base = (caddr_t) pbuf;
360 vec[2].iov_len = plen;
363 * we don't expect flow-control problems on the first outgoing packet,
364 * though I suppose it'd be possible on some weird types of link.
366 if (writev(fd, vec, 3) < 4 + 7 + plen)
368 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
372 cnt->state = T_OUTCON;
374 return t_rcvconnect(fd, rcvcall);
378 * This needs work for asynchronous mode.
380 static int read_n(int fd, char *buf, int toget)
382 struct rfct_control *cnt = control[fd];
387 if ((res = read(fd, buf, toget - got)) < 0)
393 TRC(fprintf(stderr, "Error on read.\n"));
398 if (!res) /* peer closed network connection */
401 cnt->event = T_DISCONNECT; /* is this correct ? ## */
402 cnt->hlen = cnt->pending = 0;
403 cnt->state = T_IDLE; /* this can't be correct ## */
408 while ((got += res) < toget);
412 int t_rcvconnect(int fd, struct t_call *call)
414 struct rfct_control *cnt = control[fd];
415 struct tpdu_connect_header chead;
416 char buf[100], *p, *addrp;
417 struct sockaddr_in peer;
418 int len = sizeof(struct sockaddr_in);
420 TRC(fprintf(stderr, "T_RCVCONNECT\n"));
421 if (!cnt || cnt->state != T_OUTCON)
423 TRC(fprintf(stderr, "TOUTSTATE\n"));
428 if (t_look_wait(fd, 1) <= 0)
430 if (cnt->event != T_CONNECT)
435 /* read the rest of the CC TPDU */
436 if (read_n(fd, buf, cnt->hlen) <= 0)
438 memcpy(&chead, buf, 5);
439 if (chead.class != 0)
441 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
446 *(addrp = call->addr.buf) = 0;
448 for (p = buf + 5; cnt->hlen > 0;)
450 switch ((unsigned char)*p)
452 case TPDU_PARM_TSIZE:
453 cnt->tsize = 1 << *(p + 2);
455 case TPDU_PARM_CLDID:
458 if (*(p + 1) > TSEL_MAXLEN)
460 TRC(fprintf(stderr, "Called TSEL too long.\n"));
461 t_errno = TSYSERR; /* Wrong.. ## */
464 *addrp = *(p + 1); /* length */
465 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
466 addrp += *(p + 1); /* move past TSEL */
469 case TPDU_PARM_CLGID: break; /* ignoring this for now */
471 TRC(fprintf(stderr, "Inoring CR parameter: %d\n",
473 /* we silently ignore anything else */
476 cnt->hlen -= (unsigned char) *p + 2;
477 p += (unsigned char) *p + 1;
479 addrp++; /* skip to end of addr + 1 */
482 if (getpeername(fd, (struct sockaddr*) &peer, &len) < 0)
484 TRC(perror("getpeername()"));
488 *(addrp++) = sizeof(struct sockaddr_in);
489 memcpy(addrp, &peer, sizeof(struct sockaddr_in));
490 addrp += sizeof(struct sockaddr_in);
491 call->addr.len = addrp - call->addr.buf + 1;
493 cnt->state = T_DATAXFER;
498 int t_snd(int fd, char *buf, unsigned nbytes, int flags)
500 struct rfct_control *cnt = control[fd];
501 struct rfc_header rfc;
502 struct iovec vec[2]; /* RFC1006 header + T-header + (user data) */
503 int writ = 0, res, towrite, eot;
506 TRC(fprintf(stderr, "T_SND [%d bytes, flags %d]\n", nbytes, flags));
507 if (!cnt || cnt->state != T_DATAXFER)
509 TRC(fprintf(stderr, "Trying to write in the wrong state on fd %d.\n",
519 do /* write the TSDU (segment) in chunks depending on the TPDU max size */
521 if (cnt->togo > 0) /* we have a partial TPDU on the wire */
523 TRC(fprintf(stderr, " writing continuation block (%d)\n",
525 if ((res = write(fd, buf, cnt->togo)) < 0)
537 TRC(fprintf(stderr, " wrote %d, total %d\n", res, writ));
539 else /* prepare and send (possibly partial) header */
541 towrite = nbytes - writ;
542 if (towrite + 3 + 4 > cnt->tsize)
543 towrite = cnt->tsize - (3 + 4); /* space for DATA header */
544 rfc.version = RFC_VERSION;
546 rfc.len = htons(towrite + 4 + 3); /* RFC1006 length */
548 rfc.code = TPDU_CODE_DATA;
549 if (flags & T_MORE || towrite + writ < nbytes)
553 rfc.suffix[0] = eot << 7; /* DATA EOT marker */
555 head_offset = 7 + cnt->togo;
558 vec[0].iov_base = (caddr_t) (char*)&rfc + head_offset;
559 vec[0].iov_len = 7 - head_offset;
560 vec[1].iov_base = (caddr_t) buf + writ;
561 vec[1].iov_len = towrite;
562 TRC(fprintf(stderr, " sending beg of block (%d+%d)\n",
563 7 - head_offset, towrite));
564 if ((res = writev(fd, vec, 2)) < 0)
566 TRC(fprintf(stderr, " write returned -1\n"));
567 /* thwarted by flow control */
582 /* somewhat thwarted */
583 else if (res < towrite + 7 - head_offset)
586 * Write came up short. We assume that this is a flow-
587 * control thing, and return immediately. Maybe it'd
588 * be better to take another loop, and generate an
589 * actual EAGAIN from write?
591 TRC(fprintf(stderr, " write returned %d\n", res));
592 if (res < 7 - head_offset) /* we didn't send a full header */
594 cnt->togo = -(7 - head_offset - res);
598 else if ((res -= 7 - head_offset) < towrite) /* not all data */
600 cnt->togo = towrite - res;
601 return nbytes - (writ + res);
604 else /* whew... nonblocking I/O is hard work */
607 writ += res - (7 - head_offset);
611 while (writ < nbytes);
612 TRC(fprintf(stderr, " finishing with %d written\n", nbytes));
616 int _t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
618 struct rfct_control *cnt = control[fd];
619 struct tpdu_data_header dhead;
624 TRC(fprintf(stderr, "T_RCV [nbytes=%d, flags=0x%x]\n", nbytes, *flags));
625 if (!cnt || cnt->state != T_DATAXFER)
631 do /* loop until we have a full TSDU or an error */
634 if (t_look_wait(fd, 0) <= 0)
636 if (cnt->event != T_DATA)
641 if (cnt->hlen) /* beginning of block */
643 TRC(fprintf(stderr, " Beginning of TPDU\n"));
646 TRC(fprintf(stderr, "We can't handle parameters to DATA\n"));
650 toget = cnt->pending;
651 if (toget > nbytes - got)
652 toget = nbytes - got;
653 TRC(fprintf(stderr, " toget=%d\n", toget));
654 vec[0].iov_base = (caddr_t) &dhead;
656 vec[1].iov_base = (caddr_t) buf + got;
657 vec[1].iov_len = toget;
658 if ((res = readv(fd, vec, 2)) < 0)
660 TRC(perror("readv()"));
663 if (got) /* we got some data in previous cycle */
665 t_errno = TNODATA; /* no data */
671 TRC(fprintf(stderr, " readv() returned %d\n", res));
675 cnt->event = T_DISCONNECT;
679 cnt->eot_flag = (dhead.nr & DATA_EOT) >> 7;
682 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
685 else /* continuation */
687 TRC(fprintf(stderr, " Reading middle of TPDU\n"));
688 toget = cnt->pending;
689 if (toget > nbytes - got)
690 toget = nbytes - got;
691 TRC(fprintf(stderr, " toget=%d\n", toget));
692 if ((res = read(fd, buf + got, toget)) < 0)
694 TRC(perror("read()"));
697 if (got) /* we got some data in previous cycle */
699 t_errno = TNODATA; /* no data */
705 TRC(fprintf(stderr, " read() returned %d\n", res));
709 cnt->event = T_DISCONNECT;
714 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
717 TRC(fprintf(stderr, " bottom of loop: pending=%d, got=%d\n",
720 while (cnt->pending && got < nbytes);
721 *flags = cnt->pending || !cnt->eot_flag ? T_MORE : 0;
722 TRC(fprintf(stderr, " flags=0x%x\n", *flags));
725 TRC(fprintf(stderr, " Return value: %d\n", got));
726 memset(buf + got, 0, 10);
732 int t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
739 if ((res = _t_rcv(fd, buf, nbytes, flags)) <= 0)
745 while (*flags & T_MORE && nbytes > 0);
753 struct rfct_control *cnt = control[fd];
755 TRC(fprintf(stderr, "T_CLOSE\n"));
756 if (!cnt || cnt->state == T_UNINIT)
758 TRC(fprintf(stderr, "Trying to close a bad fd.\n"));
767 * This isn't right, obviously.
769 int t_error(char *errmsg)
771 TRC(fprintf(stderr, "T_ERROR\n"));
772 fprintf(stderr, "t_error(t_errno=%d):", t_errno);
778 * Put a select in here!!
780 static int t_look_wait(int fd, int wait)
782 struct rfct_control *cnt = control[fd];
783 struct rfc_header head;
786 TRC(fprintf(stderr, "T_LOOK\n"));
787 if (!cnt || cnt->state == T_UNINIT)
794 if (cnt->state == T_IDLE && cnt->tmpfd < 0)
795 return T_LISTEN; /* the only possible type of event */
796 if ((res = read_n(fd, (char*) &head, 6)) < 0)
800 TRC(fprintf(stderr, "Network disconnect\n"));
801 return cnt->event = T_DISCONNECT;
803 TRC(fprintf(stderr, "t_look got %d bytes\n", res));
804 if (head.version != RFC_VERSION)
806 TRC(fprintf(stderr, "Got bad RFC1006 version in t_look: %d.\n",
808 t_errno = TSYSERR; /* should signal protocol error, somehow ## */
811 cnt->curcode = head.code;
812 cnt->hlen = head.hlen - 1; /* length of header suffix */
813 cnt->pending = ntohs(head.len) - 6 - cnt->hlen;
814 TRC(fprintf(stderr, "t_look: len=%d, code=0x%2.2x, hlen=%d.\n",
815 cnt->pending + 6, cnt->curcode, cnt->hlen));
816 switch (cnt->curcode)
818 case TPDU_CODE_CREQ: cnt->event = T_LISTEN; break;
819 case TPDU_CODE_CCON: cnt->event = T_CONNECT; break;
820 case TPDU_CODE_DATA: cnt->event = T_DATA; break;
821 case TPDU_CODE_DREQ: cnt->event = T_DISCONNECT; break;
823 TRC(fprintf(stderr, "t_look: Bad package: 0x%2.2x.\n", cnt->curcode));
824 t_errno = TSYSERR; /* protocol error */
832 return t_look_wait(fd, 0);
836 * If the user doesn't provide a NSAP, and qlen > 0, we'll do a default
837 * server bind. If qlen==0, we won't bind at all - connect will do that
840 int t_bind(int fd, struct t_bind *req, struct t_bind *ret)
842 struct rfct_control *cnt = control[fd];
843 struct sockaddr_in addr;
847 TRC(fprintf(stderr, "T_BIND\n"));
848 if (!cnt || cnt->state != T_UNBND)
850 TRC(fprintf(stderr, "Bad state\n"));
857 cnt->qlen = req->qlen < MAX_QLEN ? req->qlen : MAX_QLEN;
861 if (*p > TSEL_MAXLEN)
863 TRC(fprintf(stderr, "Tsel too large.\n"));
869 memcpy(cnt->ltsel, p + 1, cnt->ltsel_len);
870 p += cnt->ltsel_len + 1;
871 if (*p < sizeof(addr))
873 TRC(fprintf(stderr, "W: No NSAP provided for local bind\n"));
877 memcpy(&addr, p + 1, sizeof(addr));
884 if (!got_addr) /* user didn't give an address - local bind */
886 addr.sin_family = AF_INET;
887 addr.sin_addr.s_addr = INADDR_ANY;
889 addr.sin_port = htons(RFC_DEFAULT_PORT);
890 else /* we'll leave binding to connect - just set dummy */
891 addr.sin_port = 0; /* dummy for ret */
893 if (cnt->qlen && bind(fd, (struct sockaddr *) &addr,
896 TRC(perror("bind()"));
897 t_errno = TSYSERR; /* this should be refined */
902 if (listen(fd, cnt->qlen) < 0)
908 TRC(fprintf(stderr, " listen OK\n"));
911 /* All right! Now let's give something back, if our user wants it */
914 ret->qlen = cnt->qlen;
915 if (ret->addr.maxlen < (ret->addr.len = cnt->ltsel_len + 2 +
918 /* No space - but we're still bound */
924 *(p++) = cnt->ltsel_len;
926 memcpy(p, cnt->ltsel, cnt->ltsel_len);
928 *(p++) = sizeof(addr);
929 memcpy(p, &addr, sizeof(addr));
935 * need to consult RFC1006 on these. I think they just map to close()...
937 int t_snddis(int fd, struct t_call *call)
939 TRC(fprintf(stderr, "T_SNDDIS\n"));
943 int t_rcvdis(int fd, struct t_discon *discon)
945 struct rfct_control *cnt = control[fd];
946 struct tpdu_disconnect_header chead;
947 char udata[64], buf[256], *p;
949 TRC(fprintf(stderr, "T_RCVDIS\n"));
952 TRC(fprintf(stderr, "TOUTSTATE\n"));
957 if (t_look_wait(fd, 1) <= 0)
959 if (cnt->event != T_DISCONNECT)
964 /* read the rest of the DR TPDU */
965 if (read_n(fd, buf, cnt->hlen) <= 0)
967 memcpy(&chead, buf, 5);
969 for (p = buf + 5; cnt->hlen > 0;)
971 switch ((unsigned char)*p)
974 TRC(fprintf(stderr, "Inoring RD parameter: %d\n",
976 /* we silently ignore anything else */
979 cnt->hlen -= (unsigned char) *p + 2;
980 p += (unsigned char) *p + 1;
984 if (read_n(fd, udata, cnt->pending) < cnt->pending)
986 TRC(fprintf(stderr, "Unable to read user data\n"));
991 cnt->state = T_IDLE; /* should we close transport? */
995 discon->sequence = -1; /* TOFIX */
996 discon->reason = chead.reason;
997 TRC(fprintf(stderr, "Diconnect reason %d\n", chead.reason));
998 if (cnt->pending > discon->udata.maxlen)
1000 t_errno = TBUFOVFLW;
1005 memcpy(discon->udata.buf, udata, cnt->pending);
1006 udata[cnt->pending] = '\0';
1007 TRC(fprintf(stderr, "Discon udata: '%s'\n", udata));
1009 discon->udata.len = cnt->pending;
1015 * fix memory management, you bad Sebastian!
1017 int t_free(char *ptr, int struct_type)
1019 TRC(fprintf(stderr, "T_FREE\n"));
1024 char *t_alloc(int fd, int struct_type, int fields)
1026 char *r = malloc(1024);
1029 TRC(fprintf(stderr, "T_ALLOC\n"));
1035 * this is required for t_listen... if a system doesn't support dup2(), we're
1036 * in trouble: We might not be able to do nonblocking listen. Time will tell.
1038 static int switch_fds(int fd1, int fd2)
1041 struct rfct_control *tmpc;
1043 TRC(fprintf(stderr, "Switching fds %d <--> %d\n", fd1, fd2));
1044 if ((tmp = dup(fd1)) < 0 ||
1045 dup2(fd2, fd1) < 0 ||
1046 dup2(tmp, fd2) < 0 ||
1049 tmpc = control[fd1];
1050 control[fd1] = control[fd2];
1051 control[fd2] = tmpc;
1055 static int rcvconreq(int fd, struct t_call *call)
1057 struct rfct_control *cnt = control[fd];
1058 struct rfct_control *new = control[cnt->tmpfd];
1059 struct tpdu_connect_header chead;
1061 char *p, *addrp = 0;
1062 struct sockaddr_in addr;
1063 int len = sizeof(struct sockaddr_in);
1066 TRC(fprintf(stderr, "RCVCONRES\n"));
1072 for (qslot = 0; qslot < cnt->qlen; qslot++)
1073 if (cnt->oci[qslot] < 0)
1075 if (qslot == cnt->qlen) /* no free slots - shouldn't happen here */
1080 /* read the rest of the CREQ TPDU */
1081 if (read_n(cnt->tmpfd, buf, new->hlen) <= 0)
1083 memcpy(&chead, buf, 5);
1084 if (chead.class != 0)
1086 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
1090 memcpy(new->rref, chead.src_ref, 2); /* we'll echo this back at her */
1092 if (call && call->addr.maxlen)
1093 *(addrp = call->addr.buf) = 0;
1094 for (p = buf + 5; new->hlen > 0;)
1096 switch ((unsigned char)*p)
1098 case TPDU_PARM_TSIZE:
1099 new->tsize = 1 << *(p + 2); /* we go with their max */
1101 case TPDU_PARM_CLDID: break; /* ignore */
1102 case TPDU_PARM_CLGID:
1105 if (*(p + 1) > TSEL_MAXLEN)
1107 TRC(fprintf(stderr, "Called TSEL too long.\n"));
1108 t_errno = TSYSERR; /* Wrong.. ## */
1111 *addrp = *(p + 1); /* length */
1112 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
1113 addrp += *(p + 1); /* move past TSEL */
1116 /* we silently ignore preferred TPDU size and others */
1119 new->hlen -= (unsigned char) *p + 2;
1120 p += (unsigned char) *p + 1;
1125 if (getpeername(cnt->tmpfd, (struct sockaddr*) &addr, &len) < 0)
1127 TRC(perror("getpeername()"));
1131 *(addrp++) = sizeof(struct sockaddr_in);
1132 memcpy(addrp, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
1136 cnt->oci[qslot] = cnt->tmpfd; /* move the new semi-connection to oci */
1137 call->sequence = qslot;
1139 cnt->state = T_INCON;
1144 * This construction is tricky in async mode: listen calls accept, which
1145 * generates a new fd for the association. Call is supposed to return
1146 * immediately if there is no CR on the line (which is highly likely),
1147 * and t-user will then try to use select() on the wrong socket, waiting for
1148 * the transport level package.
1150 * Compared to the upper-level u_* routines, we have one major handicap
1151 * and one major benefit: We *have* to handle two different endpoints
1152 * after t_listen (which includes sockets accept); and we have access
1155 * If the endpoint is configured for nonblocking I/O, and listen is not
1156 * able to immediately acquire the CR, the two fds are switched, so that
1157 * subsequent selects take place on the data socket, rather than the
1160 * At any rate, the data socket is saved, so that it can later be given
1161 * to the user in t_accept().
1163 int t_listen(int fd, struct t_call *call)
1165 struct rfct_control *cnt = control[fd], *new;
1166 struct sockaddr_in addr;
1167 int addrlen = sizeof(struct sockaddr_in);
1168 int tmpfd_is_new = 0; /* we've just accept()ed a connection */
1171 TRC(fprintf(stderr, "T_LISTEN\n"));
1172 if (!cnt || cnt->state != T_IDLE)
1174 TRC(fprintf(stderr, "T_listen expects state==T_IDLE (wrong?)\n"));
1175 t_errno = TOUTSTATE;
1183 for (i = 0; i < cnt->qlen; i++)
1184 if (cnt->oci[i] < 0)
1186 if (i == cnt->qlen) /* no slots in queue */
1188 TRC(fprintf(stderr, "No more space in queue\n"));
1189 t_errno = TBADF; /* what would be more correct? */
1194 TRC(fprintf(stderr, "Accept..."));
1195 if ((cnt->tmpfd = accept(fd, (struct sockaddr*) &addr, &addrlen)) < 0)
1197 if (errno == EWOULDBLOCK)
1200 TRC(fprintf(stderr, "Accept returned WOULDBLOCK\n"));
1204 TRC(fprintf(stderr, "accept failed\n"));
1209 #ifdef NONBLOCKING_OSI
1210 if ((cnt->flags & O_NONBLOCK) && fcntl(cnt->tmpfd, F_SETFL,
1218 TRC(fprintf(stderr, "Accept OK\n"));
1219 if (!(new = control[cnt->tmpfd] = malloc(sizeof(*new))))
1221 TRC(perror("malloc()"));
1227 new->state = T_IDLE;
1230 new->qlen = cnt->qlen;
1231 new->flags = cnt->flags;
1232 new->tmpfd = cnt->tmpfd;
1234 for (i = 0; i < MAX_QLEN; i++)
1237 /* we got a network connection. Now try to read transport CREQ TPDU */
1238 if ((event = t_look_wait(tmpfd_is_new ? cnt->tmpfd : fd, 1)) < 0)
1240 if (t_errno == TNODATA)
1245 * We give the user something to select on for the incoming
1246 * CR. Switch the new association with the listener socket.
1248 TRC(fprintf(stderr, "Switching FDs\n"));
1249 if (switch_fds(cnt->tmpfd, fd) < 0)
1259 t_close(cnt->tmpfd);
1262 return -1; /* t_look & t_read hopefully set up the right errcodes */
1266 /* We got something! */
1267 if (event != T_LISTEN)
1269 TRC(fprintf(stderr, "Expected T_LISTEN\n"));
1274 * switch back the fds, if necessary */
1275 if (!tmpfd_is_new && switch_fds(fd, cnt->tmpfd) < 0)
1280 if (rcvconreq(fd, call) < 0)
1282 t_close(cnt->tmpfd);
1290 * There's no clean mapping of this onto the socket interface. If someone
1291 * wants it, we could fake it by first closing the socket, and then
1292 * opening a new one and doing a dup2. That should be functionally
1295 int t_unbind(int fd)
1297 fprintf(stderr, "T_UNBIND [not supported by transport implementation]\n");
1298 t_errno = TNOTSUPPORT;
1302 int t_accept(int fd, int resfd, struct t_call *call)
1304 struct rfct_control *listener = control[fd]; /* listener handle */
1305 struct rfct_control *new; /* new, semi-complete association */
1306 struct rfct_control *res; /* resulting association handle */
1307 struct iovec vec[3]; /* RFC1006 header + T-header + parm */
1308 struct rfc_header rfc;
1309 struct tpdu_connect_header tpdu;
1310 unsigned char parm[3 + TSEL_MAXLEN + 2];
1313 TRC(fprintf(stderr, "T_ACCEPT\n"));
1314 if (!listener || listener->state != T_INCON)
1316 TRC(fprintf(stderr, "TOUTSTATE\n"));
1317 t_errno = TOUTSTATE;
1320 /* Get the semi-connection */
1321 if (call->sequence >= listener->qlen || listener->oci[call->sequence] < 0)
1323 TRC(fprintf(stderr, "TBADSEQ\n"));
1327 new = control[(newfd = listener->oci[call->sequence])];
1328 listener->oci[call->sequence] = -1;
1329 res = control[resfd];
1335 if (res != listener) /* move the new connection */
1337 TRC(fprintf(stderr, " Moving to new fd (%d)\n", resfd));
1338 if (res->state != T_IDLE || res->qlen)
1340 TRC(fprintf(stderr, "Trying to move new assc. to bad fd.\n"));
1344 dup2(newfd, resfd); /* closes resfd */
1346 control[resfd] = new;
1347 /* transfer local bindings from res */
1349 memcpy(control[resfd]->ltsel, res->ltsel, res->ltsel_len);
1350 control[resfd]->ltsel_len = res->ltsel_len;
1352 res = control[resfd];
1353 listener->event = 0;
1354 listener->state = T_IDLE;
1356 else /* lose our listener */
1358 TRC(fprintf(stderr, " Moving to listener fd\n"));
1359 for (i = 0; i < listener->qlen; i++)
1360 if (listener->oci[i] >= 0)
1362 TRC(fprintf(stderr, "Still conn indications on listener\n"));
1369 if (listener->ltsel_len)
1370 memcpy(control[resfd]->ltsel, listener->ltsel, listener->ltsel_len);
1371 control[resfd]->ltsel_len = listener->ltsel_len;
1373 res = control[resfd];
1375 rfc.version = RFC_VERSION;
1377 rfc.code = TPDU_CODE_CCON;
1379 memcpy(tpdu.src_ref, "AA", 2);
1380 memcpy(tpdu.dst_ref, res->rref, 2); /* echo back at 'em */
1383 /* grant them their TPDU size */
1384 parm[0] = TPDU_PARM_TSIZE;
1386 for (i = 7; i <= 11 && (1 << i) < res->tsize; i++) ; /* encode TPDU size */
1388 /* give our TSEL. ## Must we echo theirs, if given? check spec */
1389 /* I thought it was ok to give an empty TSEL. Does it have semantic sig? */
1390 parm[3] = TPDU_PARM_CLDID;
1391 parm[4] = res->ltsel_len;
1393 memcpy(parm + 5, res->ltsel, res->ltsel_len);
1395 rfc.len = htons(4 + 7 + 3 + 2 + res->ltsel_len);
1396 rfc.hlen = 6 + 3 + 2 + res->ltsel_len;
1397 vec[0].iov_base = (caddr_t) &rfc;
1399 vec[1].iov_base = (caddr_t) &tpdu;
1401 vec[2].iov_base = (caddr_t) parm;
1402 vec[2].iov_len = 3 + 2 + res->ltsel_len;
1403 if (writev(resfd, vec, 3) < 4 + 7 + 3 + (2 + res->ltsel_len))
1405 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
1409 res->state = T_DATAXFER;
1414 int t_getstate(int fd)
1416 TRC(fprintf(stderr, "T_GETSTATE\n"));
1417 return control[fd] ? control[fd]->state : T_UNINIT;