4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1995-03-30 14:03:17 quinn
8 * Added RFC1006 as separate library
10 * Revision 1.15 1995/03/30 10:54:43 quinn
11 * Fiddling with packet sizes, trying to help ISODE.. :(
13 * Revision 1.14 1995/03/27 08:36:07 quinn
14 * Some work on nonblocking operation in xmosi.c and rfct.c.
15 * Added protocol parameter to cs_create()
17 * Revision 1.13 1995/03/20 11:27:16 quinn
18 * Fixed bug in the _t_rcv stuff
20 * Revision 1.12 1995/03/20 09:54:07 quinn
23 * Revision 1.11 1995/03/20 09:47:16 quinn
24 * Added server-side support to xmosi.c
25 * Fixed possible problems in rfct
28 * Revision 1.10 1995/03/17 19:37:26 quinn
29 * *** empty log message ***
31 * Revision 1.9 1995/03/17 19:28:32 quinn
32 * Working on fixing our mystery-bug.
34 * Revision 1.8 1995/03/14 10:28:38 quinn
35 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
37 * Revision 1.7 1995/03/09 18:42:32 quinn
38 * Fixed another bug in t_rcv
40 * Revision 1.6 1995/03/09 15:22:42 quinn
41 * Fixed two bugs in get/rcv
43 * Revision 1.5 1995/03/07 16:29:46 quinn
46 * Revision 1.4 1995/03/06 16:48:03 quinn
49 * Revision 1.3 1995/03/06 10:54:32 quinn
50 * Server-side functions (t_bind/t_listen/t_accept) seem to work ok, now.
51 * Nonblocking mode needs work (and testing!)
52 * Added makensap to replace function in mosiutil.c.
54 * Revision 1.2 1995/03/03 09:40:36 quinn
55 * Added most of the remaining functionality.
56 * Still need exstensive testing of server-side functions and nonblocking
59 * Revision 1.1 1995/03/01 08:40:33 quinn
60 * First working version of rfct. Addressing needs work.
65 * Simple implementation of XTI/TP0/RFC1006/Sockets.
66 * Note: This is neither complete nor robust. It just has to tick us over
67 * until mr. Furniss finishes his own implementation.
69 * TODO: Asynchronous mode needs a lot of little adjustments to various
70 * return values and error codes, etc.
72 * Check if addressing info is returned correctly by all calls.
77 #include <sys/types.h>
78 #include <sys/param.h>
79 #include <sys/socket.h>
81 #include <netinet/in.h>
82 #include <arpa/inet.h>
94 #include <linux/limits.h>
96 #include <dmalloc.h> /* project memory debugging - delete if you don't have it */
98 #ifdef TRACE_TRANSPORT
104 #define TSEL_MAXLEN 20 /* is there a standard for this? */
105 #define RFC_DEFAULT_PORT 4500
106 #define MAX_QLEN 5 /* outstanding connect indications */
108 static int t_look_wait(int fd, int wait);
109 int t_rcvconnect(int fd, struct t_call *call);
114 static struct rfct_control
116 int state; /* state of transport endpoint */
117 int event; /* current event */
118 int tsize; /* max TPDU size */
119 int curcode; /* code of current TPDU */
120 int pending; /* # bytes of user data waiting on line */
121 int eot_flag; /* current incoming TPDU had EOT set */
122 int hlen; /* TPDU header suffix remaining on line */
123 int eot; /* current incoming DATA TPDU is last in TSDU */
124 int togo; /* # bytes waiting to go on current outgoing DATA */
125 int qlen; /* set by t_bind */
126 int listen; /* we are listening */
127 int tmpfd; /* holding space for client between listen and accept */
128 int flags; /* set by t_open */
129 char rref[2]; /* remote reference */
130 char ltsel[TSEL_MAXLEN]; /* local transport selector */
132 int oci[MAX_QLEN]; /* outstanding connect indications */
136 * In the best tradition of ThinOSI, we combine the RFC1006 and the common
137 * part of the TPDU header into one. Given the complexity of the transport
138 * layer, maybe it would have been nice to have it in a separate module
139 * for reuse - but such is hindsight.
140 * Using a bit-field like this may be dangerous. I would expect it to work
141 * fine on any 32-bit or 64-bit machine, with any decent compiler. A DOS
142 * compiler might make a mess of it, I suppose, but I wouldn't even expect
149 #define RFC_VERSION 3
151 unsigned len:16; /* length of entire package, including header */
152 /* TPDU common fields */
153 unsigned char hlen; /* length of TPDU-header minus length-octet */
154 unsigned char code; /* TPDU code (and an unused 4-bit field) */
155 char suffix[100]; /* unstructured TPDU elements, for convenience */
159 * The TPDU-codes used by this package.
161 #define TPDU_CODE_CREQ 0xe0
162 #define TPDU_CODE_CCON 0xd0
163 #define TPDU_CODE_DATA 0xf0
164 #define TPDU_CODE_DREQ 0x80
167 * Parameters that we care about.
169 #define TPDU_PARM_TSIZE 0xc0 /* max TPDU size */
170 #define TPDU_PARM_CLGID 0xc1 /* Calling TSAP-ID */
171 #define TPDU_PARM_CLDID 0xc2 /* Called TSAP-ID */
173 struct tpdu_connect_header /* length of fixed suffix: 5 octets */
175 char dst_ref[2]; /* 3-4 - not used by TP0 */
176 char src_ref[2]; /* 5-6 - not used by TP0 */
177 char class; /* 7 - always 0 */
180 struct tpdu_disconnect_header /* length of fixed suffix: 5 octets */
187 struct tpdu_data_header /* length of fixed suffix: 1 octet */
189 unsigned char nr; /* we only use bit 7 (1 << 7), to mark EOT */
190 #define DATA_EOT (1<<7)
193 static int rfc_running = 0; /* Have we been initialized? */
195 static void init_rfc(void)
199 for (i = 0; i < NOFILE; i++)
204 int t_open(char *name, int oflag, struct t_info *info)
206 struct rfct_control *cnt;
207 struct protoent *proto;
210 TRC(fprintf(stderr, "T_OPEN\n"));
215 if (!(proto = getprotobyname("tcp")))
217 if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
219 #ifdef NONBLOCKING_OSI
220 if ((oflag & O_NONBLOCK) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
226 if (!(cnt = control[s] = malloc(sizeof(struct rfct_control))))
228 TRC(perror("malloc()"));
235 cnt->state = T_UNBND;
242 for (i = 0; i < MAX_QLEN; i++)
246 * RFC1006 sets a higher than standard default max TPDU size, but the
247 * Isode seems to like to negotiate it down. We'll keep it here to be
248 * safe. Not that there's no harm in jumping it up. If it's higher
249 * than 2048, t_connect won't try to negotiate.
255 info->addr = TSEL_MAXLEN + sizeof(struct sockaddr_in) + 1;
256 info->options = 1024;
261 info->servtype = T_COTS_ORD; /* lets hope our user doesn't
262 try something funny. */
267 int t_connect(int fd, struct t_call *sndcall, struct t_call *rcvcall)
269 struct rfct_control *cnt = control[fd];
270 struct sockaddr_in addr;
272 struct iovec vec[3]; /* RFC1006 header + T-header + parms */
273 struct rfc_header rfc;
274 struct tpdu_connect_header tpdu;
275 unsigned char pbuf[2 + TSEL_MAXLEN + 3]; /* CR parameters */
278 TRC(fprintf(stderr, "T_CONNECT\n"));
279 if (!cnt || cnt->state != T_IDLE)
281 TRC(fprintf(stderr, "TOUTSTATE\n"));
285 /* take the address apart */
286 p = sndcall->addr.buf;
287 if (*p) /* transport selector */
289 TRC(fprintf(stderr, "Tsel length is %d.\n", *p));
290 pbuf[0] = TPDU_PARM_CLDID;
292 memcpy(pbuf + 2, p + 1, *p);
295 p += *p + 1; /* skip tsel */
296 if (*p != sizeof(addr))
298 TRC(fprintf(stderr, "Expected sockaddr here.\n"));
303 memcpy(&addr, p, sizeof(addr));
304 if (connect(fd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
311 * If the default set by t_open() is higher than 2048, we don't try
312 * to negotiate, hoping that the opponent goes by the high default
313 * set by RFC1006. Otherwise, we just go for 2048 (max according to
314 * transport standard). The rest of this module doesn't really care
315 * about the size (although it's respected, of course), since we do
316 * no internal buffering.
318 if (cnt->tsize <= 2048)
320 pbuf[plen++] = TPDU_PARM_TSIZE;
322 pbuf[plen++] = 0x0b; /* request max PDU size (2048 octets) */
325 rfc.version = RFC_VERSION;
327 rfc.len = htons(4 + 7 + plen);
329 rfc.code = TPDU_CODE_CREQ;
331 memset(tpdu.dst_ref, 0, 2);
332 memset(tpdu.src_ref, 0, 2);
335 vec[0].iov_base = (caddr_t) &rfc;
337 vec[1].iov_base = (caddr_t) &tpdu;
339 vec[2].iov_base = (caddr_t) pbuf;
340 vec[2].iov_len = plen;
343 * we don't expect flow-control problems on the first outgoing packet,
344 * though I suppose it'd be possible on some weird types of link.
346 if (writev(fd, vec, 3) < 4 + 7 + plen)
348 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
352 cnt->state = T_OUTCON;
354 return t_rcvconnect(fd, rcvcall);
358 * This needs work for asynchronous mode.
360 static int read_n(int fd, char *buf, int toget)
362 struct rfct_control *cnt = control[fd];
367 if ((res = read(fd, buf, toget - got)) < 0)
373 TRC(fprintf(stderr, "Error on read.\n"));
378 if (!res) /* peer closed network connection */
381 cnt->event = T_DISCONNECT; /* is this correct ? ## */
382 cnt->hlen = cnt->pending = 0;
383 cnt->state = T_IDLE; /* this can't be correct ## */
388 while ((got += res) < toget);
392 int t_rcvconnect(int fd, struct t_call *call)
394 struct rfct_control *cnt = control[fd];
395 struct tpdu_connect_header chead;
396 char buf[100], *p, *addrp;
397 struct sockaddr_in peer;
398 int len = sizeof(struct sockaddr_in);
400 TRC(fprintf(stderr, "T_RCVCONNECT\n"));
401 if (!cnt || cnt->state != T_OUTCON)
403 TRC(fprintf(stderr, "TOUTSTATE\n"));
408 if (t_look_wait(fd, 1) <= 0)
410 if (cnt->event != T_CONNECT)
415 /* read the rest of the CC TPDU */
416 if (read_n(fd, buf, cnt->hlen) <= 0)
418 memcpy(&chead, buf, 5);
419 if (chead.class != 0)
421 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
426 *(addrp = call->addr.buf) = 0;
428 for (p = buf + 5; cnt->hlen > 0;)
430 switch ((unsigned char)*p)
432 case TPDU_PARM_TSIZE:
433 cnt->tsize = 1 << *(p + 2);
435 case TPDU_PARM_CLDID:
438 if (*(p + 1) > TSEL_MAXLEN)
440 TRC(fprintf(stderr, "Called TSEL too long.\n"));
441 t_errno = TSYSERR; /* Wrong.. ## */
444 *addrp = *(p + 1); /* length */
445 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
446 addrp += *(p + 1); /* move past TSEL */
449 case TPDU_PARM_CLGID: break; /* ignoring this for now */
451 TRC(fprintf(stderr, "Inoring CR parameter: %d\n",
453 /* we silently ignore anything else */
456 cnt->hlen -= (unsigned char) *p + 2;
457 p += (unsigned char) *p + 1;
459 addrp++; /* skip to end of addr + 1 */
462 if (getpeername(fd, (struct sockaddr*) &peer, &len) < 0)
464 TRC(perror("getpeername()"));
468 *(addrp++) = sizeof(struct sockaddr_in);
469 memcpy(addrp, &peer, sizeof(struct sockaddr_in));
470 addrp += sizeof(struct sockaddr_in);
471 call->addr.len = addrp - call->addr.buf + 1;
473 cnt->state = T_DATAXFER;
478 int t_snd(int fd, char *buf, unsigned nbytes, int flags)
480 struct rfct_control *cnt = control[fd];
481 struct rfc_header rfc;
482 struct iovec vec[2]; /* RFC1006 header + T-header + (user data) */
483 int writ = 0, res, towrite, eot;
486 TRC(fprintf(stderr, "T_SND [%d bytes, flags %d]\n", nbytes, flags));
487 if (!cnt || cnt->state != T_DATAXFER)
489 TRC(fprintf(stderr, "Trying to write in the wrong state on fd %d.\n",
499 do /* write the TSDU (segment) in chunks depending on the TPDU max size */
501 if (cnt->togo > 0) /* we have a partial TPDU on the wire */
503 TRC(fprintf(stderr, " writing continuation block (%d)\n",
505 if ((res = write(fd, buf, cnt->togo)) < 0)
517 TRC(fprintf(stderr, " wrote %d, total %d\n", res, writ));
519 else /* prepare and send (possibly partial) header */
521 towrite = nbytes - writ;
522 if (towrite + 3 + 4 > cnt->tsize)
523 towrite = cnt->tsize - (3 + 4); /* space for DATA header */
524 rfc.version = RFC_VERSION;
526 rfc.len = htons(towrite + 4 + 3); /* RFC1006 length */
528 rfc.code = TPDU_CODE_DATA;
529 if (flags & T_MORE || towrite + writ < nbytes)
533 rfc.suffix[0] = eot << 7; /* DATA EOT marker */
535 head_offset = 7 + cnt->togo;
538 vec[0].iov_base = (caddr_t) (char*)&rfc + head_offset;
539 vec[0].iov_len = 7 - head_offset;
540 vec[1].iov_base = (caddr_t) buf + writ;
541 vec[1].iov_len = towrite;
542 TRC(fprintf(stderr, " sending beg of block (%d+%d)\n",
543 7 - head_offset, towrite));
544 if ((res = writev(fd, vec, 2)) < 0)
546 TRC(fprintf(stderr, " write returned -1\n"));
547 /* thwarted by flow control */
562 /* somewhat thwarted */
563 else if (res < towrite + 7 - head_offset)
566 * Write came up short. We assume that this is a flow-
567 * control thing, and return immediately. Maybe it'd
568 * be better to take another loop, and generate an
569 * actual EAGAIN from write?
571 TRC(fprintf(stderr, " write returned %d\n", res));
572 if (res < 7 - head_offset) /* we didn't send a full header */
574 cnt->togo = -(7 - head_offset - res);
578 else if ((res -= 7 - head_offset) < towrite) /* not all data */
580 cnt->togo = towrite - res;
581 return nbytes - (writ + res);
584 else /* whew... nonblocking I/O is hard work */
587 writ += res - (7 - head_offset);
591 while (writ < nbytes);
592 TRC(fprintf(stderr, " finishing with %d written\n", nbytes));
596 int _t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
598 struct rfct_control *cnt = control[fd];
599 struct tpdu_data_header dhead;
604 TRC(fprintf(stderr, "T_RCV [nbytes=%d, flags=0x%x]\n", nbytes, *flags));
605 if (!cnt || cnt->state != T_DATAXFER)
611 do /* loop until we have a full TSDU or an error */
614 if (t_look_wait(fd, 0) <= 0)
616 if (cnt->event != T_DATA)
621 if (cnt->hlen) /* beginning of block */
623 TRC(fprintf(stderr, " Beginning of TPDU\n"));
626 TRC(fprintf(stderr, "We can't handle parameters to DATA\n"));
630 toget = cnt->pending;
631 if (toget > nbytes - got)
632 toget = nbytes - got;
633 TRC(fprintf(stderr, " toget=%d\n", toget));
634 vec[0].iov_base = (caddr_t) &dhead;
636 vec[1].iov_base = (caddr_t) buf + got;
637 vec[1].iov_len = toget;
638 if ((res = readv(fd, vec, 2)) < 0)
640 TRC(perror("readv()"));
643 if (got) /* we got some data in previous cycle */
645 t_errno = TNODATA; /* no data */
651 TRC(fprintf(stderr, " readv() returned %d\n", res));
655 cnt->event = T_DISCONNECT;
659 cnt->eot_flag = (dhead.nr & DATA_EOT) >> 7;
662 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
665 else /* continuation */
667 TRC(fprintf(stderr, " Reading middle of TPDU\n"));
668 toget = cnt->pending;
669 if (toget > nbytes - got)
670 toget = nbytes - got;
671 TRC(fprintf(stderr, " toget=%d\n", toget));
672 if ((res = read(fd, buf + got, toget)) < 0)
674 TRC(perror("read()"));
677 if (got) /* we got some data in previous cycle */
679 t_errno = TNODATA; /* no data */
685 TRC(fprintf(stderr, " read() returned %d\n", res));
689 cnt->event = T_DISCONNECT;
694 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
697 TRC(fprintf(stderr, " bottom of loop: pending=%d, got=%d\n",
700 while (cnt->pending && got < nbytes);
701 *flags = cnt->pending || !cnt->eot_flag ? T_MORE : 0;
702 TRC(fprintf(stderr, " flags=0x%x\n", *flags));
705 TRC(fprintf(stderr, " Return value: %d\n", got));
706 memset(buf + got, 0, 10);
710 int t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
717 if ((res = _t_rcv(fd, buf, nbytes, flags)) <= 0)
723 while (*flags & T_MORE && nbytes > 0);
729 struct rfct_control *cnt = control[fd];
731 TRC(fprintf(stderr, "T_CLOSE\n"));
732 if (!cnt || cnt->state == T_UNINIT)
734 TRC(fprintf(stderr, "Trying to close a bad fd.\n"));
743 * This isn't right, obviously.
745 int t_error(char *errmsg)
747 TRC(fprintf(stderr, "T_ERROR\n"));
748 fprintf(stderr, "t_error(t_errno=%d):", t_errno);
754 * Put a select in here!!
756 static int t_look_wait(int fd, int wait)
758 struct rfct_control *cnt = control[fd];
759 struct rfc_header head;
762 TRC(fprintf(stderr, "T_LOOK\n"));
763 if (!cnt || cnt->state == T_UNINIT)
770 if (cnt->state == T_IDLE && cnt->tmpfd < 0)
771 return T_LISTEN; /* the only possible type of event */
772 if ((res = read_n(fd, (char*) &head, 6)) < 0)
774 if (head.version != RFC_VERSION)
776 TRC(fprintf(stderr, "Got bad RFC1006 version in t_look: %d.\n",
778 t_errno = TSYSERR; /* should signal protocol error, somehow ## */
781 cnt->curcode = head.code;
782 cnt->hlen = head.hlen - 1; /* length of header suffix */
783 cnt->pending = ntohs(head.len) - 6 - cnt->hlen;
784 TRC(fprintf(stderr, "t_look: len=%d, code=0x%2.2x, hlen=%d.\n",
785 cnt->pending + 6, cnt->curcode, cnt->hlen));
786 switch (cnt->curcode)
788 case TPDU_CODE_CREQ: cnt->event = T_LISTEN; break;
789 case TPDU_CODE_CCON: cnt->event = T_CONNECT; break;
790 case TPDU_CODE_DATA: cnt->event = T_DATA; break;
791 case TPDU_CODE_DREQ: cnt->event = T_DISCONNECT; break;
793 TRC(fprintf(stderr, "t_look: Bad package: 0x%2.2x.\n", cnt->curcode));
794 t_errno = TSYSERR; /* protocol error */
802 return t_look_wait(fd, 0);
806 * If the user doesn't provide a NSAP, and qlen > 0, we'll do a default
807 * server bind. If qlen==0, we won't bind at all - connect will do that
810 int t_bind(int fd, struct t_bind *req, struct t_bind *ret)
812 struct rfct_control *cnt = control[fd];
813 struct sockaddr_in addr;
817 TRC(fprintf(stderr, "T_BIND\n"));
818 if (!cnt || cnt->state != T_UNBND)
820 TRC(fprintf(stderr, "Bad state\n"));
827 cnt->qlen = req->qlen < MAX_QLEN ? req->qlen : MAX_QLEN;
831 if (*p > TSEL_MAXLEN)
833 TRC(fprintf(stderr, "Tsel too large.\n"));
839 memcpy(cnt->ltsel, p + 1, cnt->ltsel_len);
840 p += cnt->ltsel_len + 1;
841 if (*p < sizeof(addr))
843 TRC(fprintf(stderr, "W: No NSAP provided for local bind\n"));
847 memcpy(&addr, p + 1, sizeof(addr));
854 if (!got_addr) /* user didn't give an address - local bind */
856 addr.sin_family = AF_INET;
857 addr.sin_addr.s_addr = INADDR_ANY;
859 addr.sin_port = htons(RFC_DEFAULT_PORT);
860 else /* we'll leave binding to connect - just set dummy */
861 addr.sin_port = 0; /* dummy for ret */
863 if (cnt->qlen && bind(fd, (struct sockaddr *) &addr,
866 TRC(perror("bind()"));
867 t_errno = TSYSERR; /* this should be refined */
872 if (listen(fd, cnt->qlen) < 0)
878 TRC(fprintf(stderr, " listen OK\n"));
881 /* All right! Now let's give something back, if our user wants it */
884 ret->qlen = cnt->qlen;
885 if (ret->addr.maxlen < (ret->addr.len = cnt->ltsel_len + 2 +
888 /* No space - but we're still bound */
894 *(p++) = cnt->ltsel_len;
896 memcpy(p, cnt->ltsel, cnt->ltsel_len);
898 *(p++) = sizeof(addr);
899 memcpy(p, &addr, sizeof(addr));
905 * need to consult RFC1006 on these. I think they just map to close()...
907 int t_snddis(int fd, struct t_call *call)
909 TRC(fprintf(stderr, "T_SNDDIS\n"));
913 int t_rcvdis(int fd, struct t_discon *discon)
915 struct rfct_control *cnt = control[fd];
916 struct tpdu_disconnect_header chead;
917 char udata[64], buf[256], *p;
919 TRC(fprintf(stderr, "T_RCVDIS\n"));
922 TRC(fprintf(stderr, "TOUTSTATE\n"));
927 if (t_look_wait(fd, 1) <= 0)
929 if (cnt->event != T_DISCONNECT)
934 /* read the rest of the DR TPDU */
935 if (read_n(fd, buf, cnt->hlen) <= 0)
937 memcpy(&chead, buf, 5);
939 for (p = buf + 5; cnt->hlen > 0;)
941 switch ((unsigned char)*p)
944 TRC(fprintf(stderr, "Inoring RD parameter: %d\n",
946 /* we silently ignore anything else */
949 cnt->hlen -= (unsigned char) *p + 2;
950 p += (unsigned char) *p + 1;
954 if (read_n(fd, udata, cnt->pending) < cnt->pending)
956 TRC(fprintf(stderr, "Unable to read user data\n"));
961 cnt->state = T_IDLE; /* should we close transport? */
965 discon->sequence = -1; /* TOFIX */
966 discon->reason = chead.reason;
967 TRC(fprintf(stderr, "Diconnect reason %d\n", chead.reason));
968 if (cnt->pending > discon->udata.maxlen)
975 memcpy(discon->udata.buf, udata, cnt->pending);
976 udata[cnt->pending] = '\0';
977 TRC(fprintf(stderr, "Discon udata: '%s'\n", udata));
979 discon->udata.len = cnt->pending;
985 * fix memory management, you bad Sebastian!
987 int t_free(char *ptr, int struct_type)
989 TRC(fprintf(stderr, "T_FREE\n"));
994 char *t_alloc(int fd, int struct_type, int fields)
996 char *r = malloc(1024);
999 TRC(fprintf(stderr, "T_ALLOC\n"));
1005 * this is required for t_listen... if a system doesn't support dup2(), we're
1006 * in trouble: We might not be able to do nonblocking listen. Time will tell.
1008 static int switch_fds(int fd1, int fd2)
1011 struct rfct_control *tmpc;
1013 TRC(fprintf(stderr, "Switching fds %d <--> %d\n", fd1, fd2));
1014 if ((tmp = dup(fd1)) < 0 ||
1015 dup2(fd2, fd1) < 0 ||
1016 dup2(tmp, fd2) < 0 ||
1019 tmpc = control[fd1];
1020 control[fd1] = control[fd2];
1021 control[fd2] = tmpc;
1025 static int rcvconreq(int fd, struct t_call *call)
1027 struct rfct_control *cnt = control[fd];
1028 struct rfct_control *new = control[cnt->tmpfd];
1029 struct tpdu_connect_header chead;
1031 char *p, *addrp = 0;
1032 struct sockaddr_in addr;
1033 int len = sizeof(struct sockaddr_in);
1036 TRC(fprintf(stderr, "RCVCONRES\n"));
1042 for (qslot = 0; qslot < cnt->qlen; qslot++)
1043 if (cnt->oci[qslot] < 0)
1045 if (qslot == cnt->qlen) /* no free slots - shouldn't happen here */
1050 /* read the rest of the CREQ TPDU */
1051 if (read_n(cnt->tmpfd, buf, new->hlen) <= 0)
1053 memcpy(&chead, buf, 5);
1054 if (chead.class != 0)
1056 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
1060 memcpy(new->rref, chead.src_ref, 2); /* we'll echo this back at her */
1062 if (call && call->addr.maxlen)
1063 *(addrp = call->addr.buf) = 0;
1064 for (p = buf + 5; new->hlen > 0;)
1066 switch ((unsigned char)*p)
1068 case TPDU_PARM_TSIZE:
1069 new->tsize = 1 << *(p + 2); /* we go with their max */
1071 case TPDU_PARM_CLDID: break; /* ignore */
1072 case TPDU_PARM_CLGID:
1075 if (*(p + 1) > TSEL_MAXLEN)
1077 TRC(fprintf(stderr, "Called TSEL too long.\n"));
1078 t_errno = TSYSERR; /* Wrong.. ## */
1081 *addrp = *(p + 1); /* length */
1082 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
1083 addrp += *(p + 1); /* move past TSEL */
1086 /* we silently ignore preferred TPDU size and others */
1089 new->hlen -= (unsigned char) *p + 2;
1090 p += (unsigned char) *p + 1;
1095 if (getpeername(cnt->tmpfd, (struct sockaddr*) &addr, &len) < 0)
1097 TRC(perror("getpeername()"));
1101 *(addrp++) = sizeof(struct sockaddr_in);
1102 memcpy(addrp, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
1106 cnt->oci[qslot] = cnt->tmpfd; /* move the new semi-connection to oci */
1107 call->sequence = qslot;
1109 cnt->state = T_INCON;
1114 * This construction is tricky in async mode: listen calls accept, which
1115 * generates a new fd for the association. Call is supposed to return
1116 * immediately if there is no CR on the line (which is highly likely),
1117 * and t-user will then try to use select() on the wrong socket, waiting for
1118 * the transport level package.
1120 * Compared to the upper-level u_* routines, we have one major handicap
1121 * and one major benefit: We *have* to handle two different endpoints
1122 * after t_listen (which includes sockets accept); and we have access
1125 * If the endpoint is configured for nonblocking I/O, and listen is not
1126 * able to immediately acquire the CR, the two fds are switched, so that
1127 * subsequent selects take place on the data socket, rather than the
1130 * At any rate, the data socket is saved, so that it can later be given
1131 * to the user in t_accept().
1133 int t_listen(int fd, struct t_call *call)
1135 struct rfct_control *cnt = control[fd], *new;
1136 struct sockaddr_in addr;
1137 int addrlen = sizeof(struct sockaddr_in);
1138 int tmpfd_is_new = 0; /* we've just accept()ed a connection */
1141 TRC(fprintf(stderr, "T_LISTEN\n"));
1142 if (!cnt || cnt->state != T_IDLE)
1144 TRC(fprintf(stderr, "T_listen expects state==T_IDLE (wrong?)\n"));
1145 t_errno = TOUTSTATE;
1153 for (i = 0; i < cnt->qlen; i++)
1154 if (cnt->oci[i] < 0)
1156 if (i == cnt->qlen) /* no slots in queue */
1158 TRC(fprintf(stderr, "No more space in queue\n"));
1159 t_errno = TBADF; /* what would be more correct? */
1164 TRC(fprintf(stderr, "Accept..."));
1165 if ((cnt->tmpfd = accept(fd, (struct sockaddr*) &addr, &addrlen)) < 0)
1167 if (errno == EWOULDBLOCK)
1170 TRC(fprintf(stderr, "Accept returned WOULDBLOCK\n"));
1174 TRC(fprintf(stderr, "accept failed\n"));
1179 #ifdef NONBLOCKING_OSI
1180 if ((cnt->flags & O_NONBLOCK) && fcntl(cnt->tmpfd, F_SETFL,
1188 TRC(fprintf(stderr, "Accept OK\n"));
1189 if (!(new = control[cnt->tmpfd] = malloc(sizeof(*new))))
1191 TRC(perror("malloc()"));
1197 new->state = T_IDLE;
1200 new->qlen = cnt->qlen;
1201 new->flags = cnt->flags;
1202 new->tmpfd = cnt->tmpfd;
1204 for (i = 0; i < MAX_QLEN; i++)
1207 /* we got a network connection. Now try to read transport CREQ TPDU */
1208 if ((event = t_look_wait(tmpfd_is_new ? cnt->tmpfd : fd, 1)) < 0)
1210 if (t_errno == TNODATA)
1215 * We give the user something to select on for the incoming
1216 * CR. Switch the new association with the listener socket.
1218 TRC(fprintf(stderr, "Switching FDs\n"));
1219 if (switch_fds(cnt->tmpfd, fd) < 0)
1229 t_close(cnt->tmpfd);
1232 return -1; /* t_look & t_read hopefully set up the right errcodes */
1236 /* We got something! */
1237 if (event != T_LISTEN)
1239 TRC(fprintf(stderr, "Expected T_LISTEN\n"));
1244 * switch back the fds, if necessary */
1245 if (!tmpfd_is_new && switch_fds(fd, cnt->tmpfd) < 0)
1250 if (rcvconreq(fd, call) < 0)
1252 t_close(cnt->tmpfd);
1260 * There's no clean mapping of this onto the socket interface. If someone
1261 * wants it, we could fake it by first closing the socket, and then
1262 * opening a new one and doing a dup2. That should be functionally
1265 int t_unbind(int fd)
1267 fprintf(stderr, "T_UNBIND [not supported by transport implementation]\n");
1268 t_errno = TNOTSUPPORT;
1272 int t_accept(int fd, int resfd, struct t_call *call)
1274 struct rfct_control *listener = control[fd]; /* listener handle */
1275 struct rfct_control *new; /* new, semi-complete association */
1276 struct rfct_control *res; /* resulting association handle */
1277 struct iovec vec[3]; /* RFC1006 header + T-header + parm */
1278 struct rfc_header rfc;
1279 struct tpdu_connect_header tpdu;
1280 unsigned char parm[3 + TSEL_MAXLEN + 2];
1283 TRC(fprintf(stderr, "T_ACCEPT\n"));
1284 if (!listener || listener->state != T_INCON)
1286 TRC(fprintf(stderr, "TOUTSTATE\n"));
1287 t_errno = TOUTSTATE;
1290 /* Get the semi-connection */
1291 if (call->sequence >= listener->qlen || listener->oci[call->sequence] < 0)
1293 TRC(fprintf(stderr, "TBADSEQ\n"));
1297 new = control[(newfd = listener->oci[call->sequence])];
1298 listener->oci[call->sequence] = -1;
1299 res = control[resfd];
1305 if (res != listener) /* move the new connection */
1307 TRC(fprintf(stderr, " Moving to new fd (%d)\n", resfd));
1308 if (res->state != T_IDLE || res->qlen)
1310 TRC(fprintf(stderr, "Trying to move new assc. to bad fd.\n"));
1314 dup2(newfd, resfd); /* closes resfd */
1316 control[resfd] = new;
1317 /* transfer local bindings from res */
1319 memcpy(control[resfd]->ltsel, res->ltsel, res->ltsel_len);
1320 control[resfd]->ltsel_len = res->ltsel_len;
1322 res = control[resfd];
1323 listener->event = 0;
1324 listener->state = T_IDLE;
1326 else /* lose our listener */
1328 TRC(fprintf(stderr, " Moving to listener fd\n"));
1329 for (i = 0; i < listener->qlen; i++)
1330 if (listener->oci[i] >= 0)
1332 TRC(fprintf(stderr, "Still conn indications on listener\n"));
1339 if (listener->ltsel_len)
1340 memcpy(control[resfd]->ltsel, listener->ltsel, listener->ltsel_len);
1341 control[resfd]->ltsel_len = listener->ltsel_len;
1343 res = control[resfd];
1345 rfc.version = RFC_VERSION;
1347 rfc.code = TPDU_CODE_CCON;
1349 memcpy(tpdu.src_ref, "AA", 2);
1350 memcpy(tpdu.dst_ref, res->rref, 2); /* echo back at 'em */
1353 /* grant them their TPDU size */
1354 parm[0] = TPDU_PARM_TSIZE;
1356 for (i = 7; i <= 11 && (1 << i) < res->tsize; i++) ; /* encode TPDU size */
1358 /* give our TSEL. ## Must we echo theirs, if given? check spec */
1359 /* I think it was ok to give an empty TSEL. Does it have semantic sig? */
1360 parm[3] = TPDU_PARM_CLDID;
1361 parm[4] = res->ltsel_len;
1363 memcpy(parm + 5, res->ltsel, res->ltsel_len);
1365 rfc.len = htons(4 + 7 + 3 + 2 + res->ltsel_len);
1366 rfc.hlen = 6 + 3 + 2 + res->ltsel_len;
1367 vec[0].iov_base = (caddr_t) &rfc;
1369 vec[1].iov_base = (caddr_t) &tpdu;
1371 vec[2].iov_base = (caddr_t) parm;
1372 vec[2].iov_len = 3 + 2 + res->ltsel_len;
1373 if (writev(resfd, vec, 3) < 4 + 7 + 3 + (2 + res->ltsel_len))
1375 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
1379 res->state = T_DATAXFER;
1384 int t_getstate(int fd)
1386 TRC(fprintf(stderr, "T_GETSTATE\n"));
1387 return control[fd] ? control[fd]->state : T_UNINIT;