4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1995-05-02 08:53:09 quinn
8 * Trying in vain to fix comm with ISODE
10 * Revision 1.1 1995/03/30 14:03:17 quinn
11 * Added RFC1006 as separate library
13 * Revision 1.15 1995/03/30 10:54:43 quinn
14 * Fiddling with packet sizes, trying to help ISODE.. :(
16 * Revision 1.14 1995/03/27 08:36:07 quinn
17 * Some work on nonblocking operation in xmosi.c and rfct.c.
18 * Added protocol parameter to cs_create()
20 * Revision 1.13 1995/03/20 11:27:16 quinn
21 * Fixed bug in the _t_rcv stuff
23 * Revision 1.12 1995/03/20 09:54:07 quinn
26 * Revision 1.11 1995/03/20 09:47:16 quinn
27 * Added server-side support to xmosi.c
28 * Fixed possible problems in rfct
31 * Revision 1.10 1995/03/17 19:37:26 quinn
32 * *** empty log message ***
34 * Revision 1.9 1995/03/17 19:28:32 quinn
35 * Working on fixing our mystery-bug.
37 * Revision 1.8 1995/03/14 10:28:38 quinn
38 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
40 * Revision 1.7 1995/03/09 18:42:32 quinn
41 * Fixed another bug in t_rcv
43 * Revision 1.6 1995/03/09 15:22:42 quinn
44 * Fixed two bugs in get/rcv
46 * Revision 1.5 1995/03/07 16:29:46 quinn
49 * Revision 1.4 1995/03/06 16:48:03 quinn
52 * Revision 1.3 1995/03/06 10:54:32 quinn
53 * Server-side functions (t_bind/t_listen/t_accept) seem to work ok, now.
54 * Nonblocking mode needs work (and testing!)
55 * Added makensap to replace function in mosiutil.c.
57 * Revision 1.2 1995/03/03 09:40:36 quinn
58 * Added most of the remaining functionality.
59 * Still need exstensive testing of server-side functions and nonblocking
62 * Revision 1.1 1995/03/01 08:40:33 quinn
63 * First working version of rfct. Addressing needs work.
68 * Simple implementation of XTI/TP0/RFC1006/Sockets.
69 * Note: This is neither complete nor robust. It just has to tick us over
70 * until mr. Furniss finishes his own implementation.
72 * TODO: Asynchronous mode needs a lot of little adjustments to various
73 * return values and error codes, etc.
75 * Check if addressing info is returned correctly by all calls.
80 #include <sys/types.h>
81 #include <sys/param.h>
82 #include <sys/socket.h>
84 #include <netinet/in.h>
85 #include <arpa/inet.h>
97 #include <linux/limits.h>
99 #include <dmalloc.h> /* project memory debugging - delete if you don't have it */
101 #ifdef TRACE_TRANSPORT
107 #define TSEL_MAXLEN 20 /* is there a standard for this? */
108 #define RFC_DEFAULT_PORT 4500
109 #define MAX_QLEN 5 /* outstanding connect indications */
111 static int t_look_wait(int fd, int wait);
112 int t_rcvconnect(int fd, struct t_call *call);
117 static struct rfct_control
119 int state; /* state of transport endpoint */
120 int event; /* current event */
121 int tsize; /* max TPDU size */
122 int curcode; /* code of current TPDU */
123 int pending; /* # bytes of user data waiting on line */
124 int eot_flag; /* current incoming TPDU had EOT set */
125 int hlen; /* TPDU header suffix remaining on line */
126 int eot; /* current incoming DATA TPDU is last in TSDU */
127 int togo; /* # bytes waiting to go on current outgoing DATA */
128 int qlen; /* set by t_bind */
129 int listen; /* we are listening */
130 int tmpfd; /* holding space for client between listen and accept */
131 int flags; /* set by t_open */
132 char rref[2]; /* remote reference */
133 char ltsel[TSEL_MAXLEN]; /* local transport selector */
135 int oci[MAX_QLEN]; /* outstanding connect indications */
139 * In the best tradition of ThinOSI, we combine the RFC1006 and the common
140 * part of the TPDU header into one. Given the complexity of the transport
141 * layer, maybe it would have been nice to have it in a separate module
142 * for reuse - but such is hindsight.
143 * Using a bit-field like this may be dangerous. I would expect it to work
144 * fine on any 32-bit or 64-bit machine, with any decent compiler. A DOS
145 * compiler might make a mess of it, I suppose, but I wouldn't even expect
152 #define RFC_VERSION 3
154 unsigned len:16; /* length of entire package, including header */
155 /* TPDU common fields */
156 unsigned char hlen; /* length of TPDU-header minus length-octet */
157 unsigned char code; /* TPDU code (and an unused 4-bit field) */
158 char suffix[100]; /* unstructured TPDU elements, for convenience */
162 * The TPDU-codes used by this package.
164 #define TPDU_CODE_CREQ 0xe0
165 #define TPDU_CODE_CCON 0xd0
166 #define TPDU_CODE_DATA 0xf0
167 #define TPDU_CODE_DREQ 0x80
170 * Parameters that we care about.
172 #define TPDU_PARM_TSIZE 0xc0 /* max TPDU size */
173 #define TPDU_PARM_CLGID 0xc1 /* Calling TSAP-ID */
174 #define TPDU_PARM_CLDID 0xc2 /* Called TSAP-ID */
176 struct tpdu_connect_header /* length of fixed suffix: 5 octets */
178 char dst_ref[2]; /* 3-4 - not used by TP0 */
179 char src_ref[2]; /* 5-6 - not used by TP0 */
180 char class; /* 7 - always 0 */
183 struct tpdu_disconnect_header /* length of fixed suffix: 5 octets */
190 struct tpdu_data_header /* length of fixed suffix: 1 octet */
192 unsigned char nr; /* we only use bit 7 (1 << 7), to mark EOT */
193 #define DATA_EOT (1<<7)
196 static int rfc_running = 0; /* Have we been initialized? */
198 static void init_rfc(void)
202 for (i = 0; i < NOFILE; i++)
207 int t_open(char *name, int oflag, struct t_info *info)
209 struct rfct_control *cnt;
210 struct protoent *proto;
213 TRC(fprintf(stderr, "T_OPEN\n"));
218 if (!(proto = getprotobyname("tcp")))
220 if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
222 #ifdef NONBLOCKING_OSI
223 if ((oflag & O_NONBLOCK) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
229 if (!(cnt = control[s] = malloc(sizeof(struct rfct_control))))
231 TRC(perror("malloc()"));
238 cnt->state = T_UNBND;
245 for (i = 0; i < MAX_QLEN; i++)
249 * RFC1006 sets a higher than standard default max TPDU size, but the
250 * Isode seems to like to negotiate it down. We'll keep it here to be
251 * safe. Note that there's no harm in jumping it up. If it's higher
252 * than 2048, t_connect won't try to negotiate.
258 info->addr = TSEL_MAXLEN + sizeof(struct sockaddr_in) + 1;
259 info->options = 1024;
260 info->tsdu = 2041; /* trying to please the ISODE */
264 info->servtype = T_COTS_ORD; /* lets hope our user doesn't
265 try something funny. */
270 int t_connect(int fd, struct t_call *sndcall, struct t_call *rcvcall)
272 struct rfct_control *cnt = control[fd];
273 struct sockaddr_in addr;
275 struct iovec vec[3]; /* RFC1006 header + T-header + parms */
276 struct rfc_header rfc;
277 struct tpdu_connect_header tpdu;
278 unsigned char pbuf[2 + TSEL_MAXLEN + 3]; /* CR parameters */
281 TRC(fprintf(stderr, "T_CONNECT\n"));
282 if (!cnt || cnt->state != T_IDLE)
284 TRC(fprintf(stderr, "TOUTSTATE\n"));
288 /* take the address apart */
289 p = sndcall->addr.buf;
290 if (*p) /* transport selector */
292 TRC(fprintf(stderr, "Tsel length is %d.\n", *p));
293 pbuf[0] = TPDU_PARM_CLDID;
295 memcpy(pbuf + 2, p + 1, *p);
298 p += *p + 1; /* skip tsel */
299 if (*p != sizeof(addr))
301 TRC(fprintf(stderr, "Expected sockaddr here.\n"));
306 memcpy(&addr, p, sizeof(addr));
307 if (connect(fd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
314 * If the default set by t_open() is higher than 2048, we don't try
315 * to negotiate, hoping that the opponent goes by the high default
316 * set by RFC1006. Otherwise, we just go for 2048 (max according to
317 * transport standard). The rest of this module doesn't really care
318 * about the size (although it's respected, of course), since we do
319 * no internal buffering.
321 if (cnt->tsize <= 2048)
323 pbuf[plen++] = TPDU_PARM_TSIZE;
325 pbuf[plen++] = 0x0b; /* request max PDU size (2048 octets) */
328 rfc.version = RFC_VERSION;
330 rfc.len = htons(4 + 7 + plen);
332 rfc.code = TPDU_CODE_CREQ;
334 memset(tpdu.dst_ref, 0, 2);
335 memset(tpdu.src_ref, 0, 2);
338 vec[0].iov_base = (caddr_t) &rfc;
340 vec[1].iov_base = (caddr_t) &tpdu;
342 vec[2].iov_base = (caddr_t) pbuf;
343 vec[2].iov_len = plen;
346 * we don't expect flow-control problems on the first outgoing packet,
347 * though I suppose it'd be possible on some weird types of link.
349 if (writev(fd, vec, 3) < 4 + 7 + plen)
351 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
355 cnt->state = T_OUTCON;
357 return t_rcvconnect(fd, rcvcall);
361 * This needs work for asynchronous mode.
363 static int read_n(int fd, char *buf, int toget)
365 struct rfct_control *cnt = control[fd];
370 if ((res = read(fd, buf, toget - got)) < 0)
376 TRC(fprintf(stderr, "Error on read.\n"));
381 if (!res) /* peer closed network connection */
384 cnt->event = T_DISCONNECT; /* is this correct ? ## */
385 cnt->hlen = cnt->pending = 0;
386 cnt->state = T_IDLE; /* this can't be correct ## */
391 while ((got += res) < toget);
395 int t_rcvconnect(int fd, struct t_call *call)
397 struct rfct_control *cnt = control[fd];
398 struct tpdu_connect_header chead;
399 char buf[100], *p, *addrp;
400 struct sockaddr_in peer;
401 int len = sizeof(struct sockaddr_in);
403 TRC(fprintf(stderr, "T_RCVCONNECT\n"));
404 if (!cnt || cnt->state != T_OUTCON)
406 TRC(fprintf(stderr, "TOUTSTATE\n"));
411 if (t_look_wait(fd, 1) <= 0)
413 if (cnt->event != T_CONNECT)
418 /* read the rest of the CC TPDU */
419 if (read_n(fd, buf, cnt->hlen) <= 0)
421 memcpy(&chead, buf, 5);
422 if (chead.class != 0)
424 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
429 *(addrp = call->addr.buf) = 0;
431 for (p = buf + 5; cnt->hlen > 0;)
433 switch ((unsigned char)*p)
435 case TPDU_PARM_TSIZE:
436 cnt->tsize = 1 << *(p + 2);
438 case TPDU_PARM_CLDID:
441 if (*(p + 1) > TSEL_MAXLEN)
443 TRC(fprintf(stderr, "Called TSEL too long.\n"));
444 t_errno = TSYSERR; /* Wrong.. ## */
447 *addrp = *(p + 1); /* length */
448 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
449 addrp += *(p + 1); /* move past TSEL */
452 case TPDU_PARM_CLGID: break; /* ignoring this for now */
454 TRC(fprintf(stderr, "Inoring CR parameter: %d\n",
456 /* we silently ignore anything else */
459 cnt->hlen -= (unsigned char) *p + 2;
460 p += (unsigned char) *p + 1;
462 addrp++; /* skip to end of addr + 1 */
465 if (getpeername(fd, (struct sockaddr*) &peer, &len) < 0)
467 TRC(perror("getpeername()"));
471 *(addrp++) = sizeof(struct sockaddr_in);
472 memcpy(addrp, &peer, sizeof(struct sockaddr_in));
473 addrp += sizeof(struct sockaddr_in);
474 call->addr.len = addrp - call->addr.buf + 1;
476 cnt->state = T_DATAXFER;
481 int t_snd(int fd, char *buf, unsigned nbytes, int flags)
483 struct rfct_control *cnt = control[fd];
484 struct rfc_header rfc;
485 struct iovec vec[2]; /* RFC1006 header + T-header + (user data) */
486 int writ = 0, res, towrite, eot;
489 TRC(fprintf(stderr, "T_SND [%d bytes, flags %d]\n", nbytes, flags));
490 if (!cnt || cnt->state != T_DATAXFER)
492 TRC(fprintf(stderr, "Trying to write in the wrong state on fd %d.\n",
502 do /* write the TSDU (segment) in chunks depending on the TPDU max size */
504 if (cnt->togo > 0) /* we have a partial TPDU on the wire */
506 TRC(fprintf(stderr, " writing continuation block (%d)\n",
508 if ((res = write(fd, buf, cnt->togo)) < 0)
520 TRC(fprintf(stderr, " wrote %d, total %d\n", res, writ));
522 else /* prepare and send (possibly partial) header */
524 towrite = nbytes - writ;
525 if (towrite + 3 + 4 > cnt->tsize)
526 towrite = cnt->tsize - (3 + 4); /* space for DATA header */
527 rfc.version = RFC_VERSION;
529 rfc.len = htons(towrite + 4 + 3); /* RFC1006 length */
531 rfc.code = TPDU_CODE_DATA;
532 if (flags & T_MORE || towrite + writ < nbytes)
536 rfc.suffix[0] = eot << 7; /* DATA EOT marker */
538 head_offset = 7 + cnt->togo;
541 vec[0].iov_base = (caddr_t) (char*)&rfc + head_offset;
542 vec[0].iov_len = 7 - head_offset;
543 vec[1].iov_base = (caddr_t) buf + writ;
544 vec[1].iov_len = towrite;
545 TRC(fprintf(stderr, " sending beg of block (%d+%d)\n",
546 7 - head_offset, towrite));
547 if ((res = writev(fd, vec, 2)) < 0)
549 TRC(fprintf(stderr, " write returned -1\n"));
550 /* thwarted by flow control */
565 /* somewhat thwarted */
566 else if (res < towrite + 7 - head_offset)
569 * Write came up short. We assume that this is a flow-
570 * control thing, and return immediately. Maybe it'd
571 * be better to take another loop, and generate an
572 * actual EAGAIN from write?
574 TRC(fprintf(stderr, " write returned %d\n", res));
575 if (res < 7 - head_offset) /* we didn't send a full header */
577 cnt->togo = -(7 - head_offset - res);
581 else if ((res -= 7 - head_offset) < towrite) /* not all data */
583 cnt->togo = towrite - res;
584 return nbytes - (writ + res);
587 else /* whew... nonblocking I/O is hard work */
590 writ += res - (7 - head_offset);
594 while (writ < nbytes);
595 TRC(fprintf(stderr, " finishing with %d written\n", nbytes));
599 int _t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
601 struct rfct_control *cnt = control[fd];
602 struct tpdu_data_header dhead;
607 TRC(fprintf(stderr, "T_RCV [nbytes=%d, flags=0x%x]\n", nbytes, *flags));
608 if (!cnt || cnt->state != T_DATAXFER)
614 do /* loop until we have a full TSDU or an error */
617 if (t_look_wait(fd, 0) <= 0)
619 if (cnt->event != T_DATA)
624 if (cnt->hlen) /* beginning of block */
626 TRC(fprintf(stderr, " Beginning of TPDU\n"));
629 TRC(fprintf(stderr, "We can't handle parameters to DATA\n"));
633 toget = cnt->pending;
634 if (toget > nbytes - got)
635 toget = nbytes - got;
636 TRC(fprintf(stderr, " toget=%d\n", toget));
637 vec[0].iov_base = (caddr_t) &dhead;
639 vec[1].iov_base = (caddr_t) buf + got;
640 vec[1].iov_len = toget;
641 if ((res = readv(fd, vec, 2)) < 0)
643 TRC(perror("readv()"));
646 if (got) /* we got some data in previous cycle */
648 t_errno = TNODATA; /* no data */
654 TRC(fprintf(stderr, " readv() returned %d\n", res));
658 cnt->event = T_DISCONNECT;
662 cnt->eot_flag = (dhead.nr & DATA_EOT) >> 7;
665 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
668 else /* continuation */
670 TRC(fprintf(stderr, " Reading middle of TPDU\n"));
671 toget = cnt->pending;
672 if (toget > nbytes - got)
673 toget = nbytes - got;
674 TRC(fprintf(stderr, " toget=%d\n", toget));
675 if ((res = read(fd, buf + got, toget)) < 0)
677 TRC(perror("read()"));
680 if (got) /* we got some data in previous cycle */
682 t_errno = TNODATA; /* no data */
688 TRC(fprintf(stderr, " read() returned %d\n", res));
692 cnt->event = T_DISCONNECT;
697 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
700 TRC(fprintf(stderr, " bottom of loop: pending=%d, got=%d\n",
703 while (cnt->pending && got < nbytes);
704 *flags = cnt->pending || !cnt->eot_flag ? T_MORE : 0;
705 TRC(fprintf(stderr, " flags=0x%x\n", *flags));
708 TRC(fprintf(stderr, " Return value: %d\n", got));
709 memset(buf + got, 0, 10);
713 int t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
720 if ((res = _t_rcv(fd, buf, nbytes, flags)) <= 0)
726 while (*flags & T_MORE && nbytes > 0);
732 struct rfct_control *cnt = control[fd];
734 TRC(fprintf(stderr, "T_CLOSE\n"));
735 if (!cnt || cnt->state == T_UNINIT)
737 TRC(fprintf(stderr, "Trying to close a bad fd.\n"));
746 * This isn't right, obviously.
748 int t_error(char *errmsg)
750 TRC(fprintf(stderr, "T_ERROR\n"));
751 fprintf(stderr, "t_error(t_errno=%d):", t_errno);
757 * Put a select in here!!
759 static int t_look_wait(int fd, int wait)
761 struct rfct_control *cnt = control[fd];
762 struct rfc_header head;
765 TRC(fprintf(stderr, "T_LOOK\n"));
766 if (!cnt || cnt->state == T_UNINIT)
773 if (cnt->state == T_IDLE && cnt->tmpfd < 0)
774 return T_LISTEN; /* the only possible type of event */
775 if ((res = read_n(fd, (char*) &head, 6)) < 0)
777 if (head.version != RFC_VERSION)
779 TRC(fprintf(stderr, "Got bad RFC1006 version in t_look: %d.\n",
781 t_errno = TSYSERR; /* should signal protocol error, somehow ## */
784 cnt->curcode = head.code;
785 cnt->hlen = head.hlen - 1; /* length of header suffix */
786 cnt->pending = ntohs(head.len) - 6 - cnt->hlen;
787 TRC(fprintf(stderr, "t_look: len=%d, code=0x%2.2x, hlen=%d.\n",
788 cnt->pending + 6, cnt->curcode, cnt->hlen));
789 switch (cnt->curcode)
791 case TPDU_CODE_CREQ: cnt->event = T_LISTEN; break;
792 case TPDU_CODE_CCON: cnt->event = T_CONNECT; break;
793 case TPDU_CODE_DATA: cnt->event = T_DATA; break;
794 case TPDU_CODE_DREQ: cnt->event = T_DISCONNECT; break;
796 TRC(fprintf(stderr, "t_look: Bad package: 0x%2.2x.\n", cnt->curcode));
797 t_errno = TSYSERR; /* protocol error */
805 return t_look_wait(fd, 0);
809 * If the user doesn't provide a NSAP, and qlen > 0, we'll do a default
810 * server bind. If qlen==0, we won't bind at all - connect will do that
813 int t_bind(int fd, struct t_bind *req, struct t_bind *ret)
815 struct rfct_control *cnt = control[fd];
816 struct sockaddr_in addr;
820 TRC(fprintf(stderr, "T_BIND\n"));
821 if (!cnt || cnt->state != T_UNBND)
823 TRC(fprintf(stderr, "Bad state\n"));
830 cnt->qlen = req->qlen < MAX_QLEN ? req->qlen : MAX_QLEN;
834 if (*p > TSEL_MAXLEN)
836 TRC(fprintf(stderr, "Tsel too large.\n"));
842 memcpy(cnt->ltsel, p + 1, cnt->ltsel_len);
843 p += cnt->ltsel_len + 1;
844 if (*p < sizeof(addr))
846 TRC(fprintf(stderr, "W: No NSAP provided for local bind\n"));
850 memcpy(&addr, p + 1, sizeof(addr));
857 if (!got_addr) /* user didn't give an address - local bind */
859 addr.sin_family = AF_INET;
860 addr.sin_addr.s_addr = INADDR_ANY;
862 addr.sin_port = htons(RFC_DEFAULT_PORT);
863 else /* we'll leave binding to connect - just set dummy */
864 addr.sin_port = 0; /* dummy for ret */
866 if (cnt->qlen && bind(fd, (struct sockaddr *) &addr,
869 TRC(perror("bind()"));
870 t_errno = TSYSERR; /* this should be refined */
875 if (listen(fd, cnt->qlen) < 0)
881 TRC(fprintf(stderr, " listen OK\n"));
884 /* All right! Now let's give something back, if our user wants it */
887 ret->qlen = cnt->qlen;
888 if (ret->addr.maxlen < (ret->addr.len = cnt->ltsel_len + 2 +
891 /* No space - but we're still bound */
897 *(p++) = cnt->ltsel_len;
899 memcpy(p, cnt->ltsel, cnt->ltsel_len);
901 *(p++) = sizeof(addr);
902 memcpy(p, &addr, sizeof(addr));
908 * need to consult RFC1006 on these. I think they just map to close()...
910 int t_snddis(int fd, struct t_call *call)
912 TRC(fprintf(stderr, "T_SNDDIS\n"));
916 int t_rcvdis(int fd, struct t_discon *discon)
918 struct rfct_control *cnt = control[fd];
919 struct tpdu_disconnect_header chead;
920 char udata[64], buf[256], *p;
922 TRC(fprintf(stderr, "T_RCVDIS\n"));
925 TRC(fprintf(stderr, "TOUTSTATE\n"));
930 if (t_look_wait(fd, 1) <= 0)
932 if (cnt->event != T_DISCONNECT)
937 /* read the rest of the DR TPDU */
938 if (read_n(fd, buf, cnt->hlen) <= 0)
940 memcpy(&chead, buf, 5);
942 for (p = buf + 5; cnt->hlen > 0;)
944 switch ((unsigned char)*p)
947 TRC(fprintf(stderr, "Inoring RD parameter: %d\n",
949 /* we silently ignore anything else */
952 cnt->hlen -= (unsigned char) *p + 2;
953 p += (unsigned char) *p + 1;
957 if (read_n(fd, udata, cnt->pending) < cnt->pending)
959 TRC(fprintf(stderr, "Unable to read user data\n"));
964 cnt->state = T_IDLE; /* should we close transport? */
968 discon->sequence = -1; /* TOFIX */
969 discon->reason = chead.reason;
970 TRC(fprintf(stderr, "Diconnect reason %d\n", chead.reason));
971 if (cnt->pending > discon->udata.maxlen)
978 memcpy(discon->udata.buf, udata, cnt->pending);
979 udata[cnt->pending] = '\0';
980 TRC(fprintf(stderr, "Discon udata: '%s'\n", udata));
982 discon->udata.len = cnt->pending;
988 * fix memory management, you bad Sebastian!
990 int t_free(char *ptr, int struct_type)
992 TRC(fprintf(stderr, "T_FREE\n"));
997 char *t_alloc(int fd, int struct_type, int fields)
999 char *r = malloc(1024);
1002 TRC(fprintf(stderr, "T_ALLOC\n"));
1008 * this is required for t_listen... if a system doesn't support dup2(), we're
1009 * in trouble: We might not be able to do nonblocking listen. Time will tell.
1011 static int switch_fds(int fd1, int fd2)
1014 struct rfct_control *tmpc;
1016 TRC(fprintf(stderr, "Switching fds %d <--> %d\n", fd1, fd2));
1017 if ((tmp = dup(fd1)) < 0 ||
1018 dup2(fd2, fd1) < 0 ||
1019 dup2(tmp, fd2) < 0 ||
1022 tmpc = control[fd1];
1023 control[fd1] = control[fd2];
1024 control[fd2] = tmpc;
1028 static int rcvconreq(int fd, struct t_call *call)
1030 struct rfct_control *cnt = control[fd];
1031 struct rfct_control *new = control[cnt->tmpfd];
1032 struct tpdu_connect_header chead;
1034 char *p, *addrp = 0;
1035 struct sockaddr_in addr;
1036 int len = sizeof(struct sockaddr_in);
1039 TRC(fprintf(stderr, "RCVCONRES\n"));
1045 for (qslot = 0; qslot < cnt->qlen; qslot++)
1046 if (cnt->oci[qslot] < 0)
1048 if (qslot == cnt->qlen) /* no free slots - shouldn't happen here */
1053 /* read the rest of the CREQ TPDU */
1054 if (read_n(cnt->tmpfd, buf, new->hlen) <= 0)
1056 memcpy(&chead, buf, 5);
1057 if (chead.class != 0)
1059 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
1063 memcpy(new->rref, chead.src_ref, 2); /* we'll echo this back at her */
1065 if (call && call->addr.maxlen)
1066 *(addrp = call->addr.buf) = 0;
1067 for (p = buf + 5; new->hlen > 0;)
1069 switch ((unsigned char)*p)
1071 case TPDU_PARM_TSIZE:
1072 new->tsize = 1 << *(p + 2); /* we go with their max */
1074 case TPDU_PARM_CLDID: break; /* ignore */
1075 case TPDU_PARM_CLGID:
1078 if (*(p + 1) > TSEL_MAXLEN)
1080 TRC(fprintf(stderr, "Called TSEL too long.\n"));
1081 t_errno = TSYSERR; /* Wrong.. ## */
1084 *addrp = *(p + 1); /* length */
1085 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
1086 addrp += *(p + 1); /* move past TSEL */
1089 /* we silently ignore preferred TPDU size and others */
1092 new->hlen -= (unsigned char) *p + 2;
1093 p += (unsigned char) *p + 1;
1098 if (getpeername(cnt->tmpfd, (struct sockaddr*) &addr, &len) < 0)
1100 TRC(perror("getpeername()"));
1104 *(addrp++) = sizeof(struct sockaddr_in);
1105 memcpy(addrp, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
1109 cnt->oci[qslot] = cnt->tmpfd; /* move the new semi-connection to oci */
1110 call->sequence = qslot;
1112 cnt->state = T_INCON;
1117 * This construction is tricky in async mode: listen calls accept, which
1118 * generates a new fd for the association. Call is supposed to return
1119 * immediately if there is no CR on the line (which is highly likely),
1120 * and t-user will then try to use select() on the wrong socket, waiting for
1121 * the transport level package.
1123 * Compared to the upper-level u_* routines, we have one major handicap
1124 * and one major benefit: We *have* to handle two different endpoints
1125 * after t_listen (which includes sockets accept); and we have access
1128 * If the endpoint is configured for nonblocking I/O, and listen is not
1129 * able to immediately acquire the CR, the two fds are switched, so that
1130 * subsequent selects take place on the data socket, rather than the
1133 * At any rate, the data socket is saved, so that it can later be given
1134 * to the user in t_accept().
1136 int t_listen(int fd, struct t_call *call)
1138 struct rfct_control *cnt = control[fd], *new;
1139 struct sockaddr_in addr;
1140 int addrlen = sizeof(struct sockaddr_in);
1141 int tmpfd_is_new = 0; /* we've just accept()ed a connection */
1144 TRC(fprintf(stderr, "T_LISTEN\n"));
1145 if (!cnt || cnt->state != T_IDLE)
1147 TRC(fprintf(stderr, "T_listen expects state==T_IDLE (wrong?)\n"));
1148 t_errno = TOUTSTATE;
1156 for (i = 0; i < cnt->qlen; i++)
1157 if (cnt->oci[i] < 0)
1159 if (i == cnt->qlen) /* no slots in queue */
1161 TRC(fprintf(stderr, "No more space in queue\n"));
1162 t_errno = TBADF; /* what would be more correct? */
1167 TRC(fprintf(stderr, "Accept..."));
1168 if ((cnt->tmpfd = accept(fd, (struct sockaddr*) &addr, &addrlen)) < 0)
1170 if (errno == EWOULDBLOCK)
1173 TRC(fprintf(stderr, "Accept returned WOULDBLOCK\n"));
1177 TRC(fprintf(stderr, "accept failed\n"));
1182 #ifdef NONBLOCKING_OSI
1183 if ((cnt->flags & O_NONBLOCK) && fcntl(cnt->tmpfd, F_SETFL,
1191 TRC(fprintf(stderr, "Accept OK\n"));
1192 if (!(new = control[cnt->tmpfd] = malloc(sizeof(*new))))
1194 TRC(perror("malloc()"));
1200 new->state = T_IDLE;
1203 new->qlen = cnt->qlen;
1204 new->flags = cnt->flags;
1205 new->tmpfd = cnt->tmpfd;
1207 for (i = 0; i < MAX_QLEN; i++)
1210 /* we got a network connection. Now try to read transport CREQ TPDU */
1211 if ((event = t_look_wait(tmpfd_is_new ? cnt->tmpfd : fd, 1)) < 0)
1213 if (t_errno == TNODATA)
1218 * We give the user something to select on for the incoming
1219 * CR. Switch the new association with the listener socket.
1221 TRC(fprintf(stderr, "Switching FDs\n"));
1222 if (switch_fds(cnt->tmpfd, fd) < 0)
1232 t_close(cnt->tmpfd);
1235 return -1; /* t_look & t_read hopefully set up the right errcodes */
1239 /* We got something! */
1240 if (event != T_LISTEN)
1242 TRC(fprintf(stderr, "Expected T_LISTEN\n"));
1247 * switch back the fds, if necessary */
1248 if (!tmpfd_is_new && switch_fds(fd, cnt->tmpfd) < 0)
1253 if (rcvconreq(fd, call) < 0)
1255 t_close(cnt->tmpfd);
1263 * There's no clean mapping of this onto the socket interface. If someone
1264 * wants it, we could fake it by first closing the socket, and then
1265 * opening a new one and doing a dup2. That should be functionally
1268 int t_unbind(int fd)
1270 fprintf(stderr, "T_UNBIND [not supported by transport implementation]\n");
1271 t_errno = TNOTSUPPORT;
1275 int t_accept(int fd, int resfd, struct t_call *call)
1277 struct rfct_control *listener = control[fd]; /* listener handle */
1278 struct rfct_control *new; /* new, semi-complete association */
1279 struct rfct_control *res; /* resulting association handle */
1280 struct iovec vec[3]; /* RFC1006 header + T-header + parm */
1281 struct rfc_header rfc;
1282 struct tpdu_connect_header tpdu;
1283 unsigned char parm[3 + TSEL_MAXLEN + 2];
1286 TRC(fprintf(stderr, "T_ACCEPT\n"));
1287 if (!listener || listener->state != T_INCON)
1289 TRC(fprintf(stderr, "TOUTSTATE\n"));
1290 t_errno = TOUTSTATE;
1293 /* Get the semi-connection */
1294 if (call->sequence >= listener->qlen || listener->oci[call->sequence] < 0)
1296 TRC(fprintf(stderr, "TBADSEQ\n"));
1300 new = control[(newfd = listener->oci[call->sequence])];
1301 listener->oci[call->sequence] = -1;
1302 res = control[resfd];
1308 if (res != listener) /* move the new connection */
1310 TRC(fprintf(stderr, " Moving to new fd (%d)\n", resfd));
1311 if (res->state != T_IDLE || res->qlen)
1313 TRC(fprintf(stderr, "Trying to move new assc. to bad fd.\n"));
1317 dup2(newfd, resfd); /* closes resfd */
1319 control[resfd] = new;
1320 /* transfer local bindings from res */
1322 memcpy(control[resfd]->ltsel, res->ltsel, res->ltsel_len);
1323 control[resfd]->ltsel_len = res->ltsel_len;
1325 res = control[resfd];
1326 listener->event = 0;
1327 listener->state = T_IDLE;
1329 else /* lose our listener */
1331 TRC(fprintf(stderr, " Moving to listener fd\n"));
1332 for (i = 0; i < listener->qlen; i++)
1333 if (listener->oci[i] >= 0)
1335 TRC(fprintf(stderr, "Still conn indications on listener\n"));
1342 if (listener->ltsel_len)
1343 memcpy(control[resfd]->ltsel, listener->ltsel, listener->ltsel_len);
1344 control[resfd]->ltsel_len = listener->ltsel_len;
1346 res = control[resfd];
1348 rfc.version = RFC_VERSION;
1350 rfc.code = TPDU_CODE_CCON;
1352 memcpy(tpdu.src_ref, "AA", 2);
1353 memcpy(tpdu.dst_ref, res->rref, 2); /* echo back at 'em */
1356 /* grant them their TPDU size */
1357 parm[0] = TPDU_PARM_TSIZE;
1359 for (i = 7; i <= 11 && (1 << i) < res->tsize; i++) ; /* encode TPDU size */
1361 /* give our TSEL. ## Must we echo theirs, if given? check spec */
1362 /* I thought it was ok to give an empty TSEL. Does it have semantic sig? */
1363 parm[3] = TPDU_PARM_CLDID;
1364 parm[4] = res->ltsel_len;
1366 memcpy(parm + 5, res->ltsel, res->ltsel_len);
1368 rfc.len = htons(4 + 7 + 3 + 2 + res->ltsel_len);
1369 rfc.hlen = 6 + 3 + 2 + res->ltsel_len;
1370 vec[0].iov_base = (caddr_t) &rfc;
1372 vec[1].iov_base = (caddr_t) &tpdu;
1374 vec[2].iov_base = (caddr_t) parm;
1375 vec[2].iov_len = 3 + 2 + res->ltsel_len;
1376 if (writev(resfd, vec, 3) < 4 + 7 + 3 + (2 + res->ltsel_len))
1378 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
1382 res->state = T_DATAXFER;
1387 int t_getstate(int fd)
1389 TRC(fprintf(stderr, "T_GETSTATE\n"));
1390 return control[fd] ? control[fd]->state : T_UNINIT;