4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1995-05-16 09:37:18 quinn
10 * Revision 1.2 1995/05/02 08:53:09 quinn
11 * Trying in vain to fix comm with ISODE
13 * Revision 1.1 1995/03/30 14:03:17 quinn
14 * Added RFC1006 as separate library
16 * Revision 1.15 1995/03/30 10:54:43 quinn
17 * Fiddling with packet sizes, trying to help ISODE.. :(
19 * Revision 1.14 1995/03/27 08:36:07 quinn
20 * Some work on nonblocking operation in xmosi.c and rfct.c.
21 * Added protocol parameter to cs_create()
23 * Revision 1.13 1995/03/20 11:27:16 quinn
24 * Fixed bug in the _t_rcv stuff
26 * Revision 1.12 1995/03/20 09:54:07 quinn
29 * Revision 1.11 1995/03/20 09:47:16 quinn
30 * Added server-side support to xmosi.c
31 * Fixed possible problems in rfct
34 * Revision 1.10 1995/03/17 19:37:26 quinn
35 * *** empty log message ***
37 * Revision 1.9 1995/03/17 19:28:32 quinn
38 * Working on fixing our mystery-bug.
40 * Revision 1.8 1995/03/14 10:28:38 quinn
41 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
43 * Revision 1.7 1995/03/09 18:42:32 quinn
44 * Fixed another bug in t_rcv
46 * Revision 1.6 1995/03/09 15:22:42 quinn
47 * Fixed two bugs in get/rcv
49 * Revision 1.5 1995/03/07 16:29:46 quinn
52 * Revision 1.4 1995/03/06 16:48:03 quinn
55 * Revision 1.3 1995/03/06 10:54:32 quinn
56 * Server-side functions (t_bind/t_listen/t_accept) seem to work ok, now.
57 * Nonblocking mode needs work (and testing!)
58 * Added makensap to replace function in mosiutil.c.
60 * Revision 1.2 1995/03/03 09:40:36 quinn
61 * Added most of the remaining functionality.
62 * Still need exstensive testing of server-side functions and nonblocking
65 * Revision 1.1 1995/03/01 08:40:33 quinn
66 * First working version of rfct. Addressing needs work.
71 * Simple implementation of XTI/TP0/RFC1006/Sockets.
72 * Note: This is neither complete nor robust. It just has to tick us over
73 * until mr. Furniss finishes his own implementation.
75 * TODO: Asynchronous mode needs a lot of little adjustments to various
76 * return values and error codes, etc.
78 * Check if addressing info is returned correctly by all calls.
83 #include <sys/types.h>
84 #include <sys/param.h>
85 #include <sys/socket.h>
87 #include <netinet/in.h>
88 #include <arpa/inet.h>
100 #include <linux/limits.h>
102 #include <dmalloc.h> /* project memory debugging - delete if you don't have it */
104 #ifdef TRACE_TRANSPORT
110 #define TSEL_MAXLEN 20 /* is there a standard for this? */
111 #define RFC_DEFAULT_PORT 4500
112 #define MAX_QLEN 5 /* outstanding connect indications */
114 static int t_look_wait(int fd, int wait);
115 int t_rcvconnect(int fd, struct t_call *call);
120 static struct rfct_control
122 int state; /* state of transport endpoint */
123 int event; /* current event */
124 int tsize; /* max TPDU size */
125 int curcode; /* code of current TPDU */
126 int pending; /* # bytes of user data waiting on line */
127 int eot_flag; /* current incoming TPDU had EOT set */
128 int hlen; /* TPDU header suffix remaining on line */
129 int eot; /* current incoming DATA TPDU is last in TSDU */
130 int togo; /* # bytes waiting to go on current outgoing DATA */
131 int qlen; /* set by t_bind */
132 int listen; /* we are listening */
133 int tmpfd; /* holding space for client between listen and accept */
134 int flags; /* set by t_open */
135 char rref[2]; /* remote reference */
136 char ltsel[TSEL_MAXLEN]; /* local transport selector */
138 int oci[MAX_QLEN]; /* outstanding connect indications */
142 * In the best tradition of ThinOSI, we combine the RFC1006 and the common
143 * part of the TPDU header into one. Given the complexity of the transport
144 * layer, maybe it would have been nice to have it in a separate module
145 * for reuse - but such is hindsight.
146 * Using a bit-field like this may be dangerous. I would expect it to work
147 * fine on any 32-bit or 64-bit machine, with any decent compiler. A DOS
148 * compiler might make a mess of it, I suppose, but I wouldn't even expect
155 #define RFC_VERSION 3
157 unsigned len:16; /* length of entire package, including header */
158 /* TPDU common fields */
159 unsigned char hlen; /* length of TPDU-header minus length-octet */
160 unsigned char code; /* TPDU code (and an unused 4-bit field) */
161 char suffix[100]; /* unstructured TPDU elements, for convenience */
165 * The TPDU-codes used by this package.
167 #define TPDU_CODE_CREQ 0xe0
168 #define TPDU_CODE_CCON 0xd0
169 #define TPDU_CODE_DATA 0xf0
170 #define TPDU_CODE_DREQ 0x80
173 * Parameters that we care about.
175 #define TPDU_PARM_TSIZE 0xc0 /* max TPDU size */
176 #define TPDU_PARM_CLGID 0xc1 /* Calling TSAP-ID */
177 #define TPDU_PARM_CLDID 0xc2 /* Called TSAP-ID */
179 struct tpdu_connect_header /* length of fixed suffix: 5 octets */
181 char dst_ref[2]; /* 3-4 - not used by TP0 */
182 char src_ref[2]; /* 5-6 - not used by TP0 */
183 char class; /* 7 - always 0 */
186 struct tpdu_disconnect_header /* length of fixed suffix: 5 octets */
193 struct tpdu_data_header /* length of fixed suffix: 1 octet */
195 unsigned char nr; /* we only use bit 7 (1 << 7), to mark EOT */
196 #define DATA_EOT (1<<7)
199 static int rfc_running = 0; /* Have we been initialized? */
201 static void init_rfc(void)
205 for (i = 0; i < NOFILE; i++)
210 int t_open(char *name, int oflag, struct t_info *info)
212 struct rfct_control *cnt;
213 struct protoent *proto;
216 TRC(fprintf(stderr, "T_OPEN\n"));
221 if (!(proto = getprotobyname("tcp")))
223 if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
225 #ifdef NONBLOCKING_OSI
226 if ((oflag & O_NONBLOCK) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
232 if (!(cnt = control[s] = malloc(sizeof(struct rfct_control))))
234 TRC(perror("malloc()"));
241 cnt->state = T_UNBND;
248 for (i = 0; i < MAX_QLEN; i++)
252 * RFC1006 sets a higher than standard default max TPDU size, but the
253 * Isode seems to like to negotiate it down. We'll keep it here to be
254 * safe. Note that there's no harm in jumping it up. If it's higher
255 * than 2048, t_connect won't try to negotiate.
261 info->addr = TSEL_MAXLEN + sizeof(struct sockaddr_in) + 1;
262 info->options = 1024;
263 info->tsdu = -1; /* is this right? */
267 info->servtype = T_COTS_ORD; /* lets hope our user doesn't
268 try something funny. */
273 int t_connect(int fd, struct t_call *sndcall, struct t_call *rcvcall)
275 struct rfct_control *cnt = control[fd];
276 struct sockaddr_in addr;
278 struct iovec vec[3]; /* RFC1006 header + T-header + parms */
279 struct rfc_header rfc;
280 struct tpdu_connect_header tpdu;
281 unsigned char pbuf[2 + TSEL_MAXLEN + 3]; /* CR parameters */
284 TRC(fprintf(stderr, "T_CONNECT\n"));
285 if (!cnt || cnt->state != T_IDLE)
287 TRC(fprintf(stderr, "TOUTSTATE\n"));
291 /* take the address apart */
292 p = sndcall->addr.buf;
293 if (*p) /* transport selector */
295 TRC(fprintf(stderr, "Tsel length is %d.\n", *p));
296 pbuf[0] = TPDU_PARM_CLDID;
298 memcpy(pbuf + 2, p + 1, *p);
301 p += *p + 1; /* skip tsel */
302 if (*p != sizeof(addr))
304 TRC(fprintf(stderr, "Expected sockaddr here.\n"));
309 memcpy(&addr, p, sizeof(addr));
310 if (connect(fd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
317 * If the default set by t_open() is higher than 2048, we don't try
318 * to negotiate, hoping that the opponent goes by the high default
319 * set by RFC1006. Otherwise, we just go for 2048 (max according to
320 * transport standard). The rest of this module doesn't really care
321 * about the size (although it's respected, of course), since we do
322 * no internal buffering.
324 if (cnt->tsize <= 2048)
326 pbuf[plen++] = TPDU_PARM_TSIZE;
328 pbuf[plen++] = 0x0b; /* request max PDU size (2048 octets) */
331 rfc.version = RFC_VERSION;
333 rfc.len = htons(4 + 7 + plen);
335 rfc.code = TPDU_CODE_CREQ;
337 memset(tpdu.dst_ref, 0, 2);
338 memset(tpdu.src_ref, 0, 2);
341 vec[0].iov_base = (caddr_t) &rfc;
343 vec[1].iov_base = (caddr_t) &tpdu;
345 vec[2].iov_base = (caddr_t) pbuf;
346 vec[2].iov_len = plen;
349 * we don't expect flow-control problems on the first outgoing packet,
350 * though I suppose it'd be possible on some weird types of link.
352 if (writev(fd, vec, 3) < 4 + 7 + plen)
354 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
358 cnt->state = T_OUTCON;
360 return t_rcvconnect(fd, rcvcall);
364 * This needs work for asynchronous mode.
366 static int read_n(int fd, char *buf, int toget)
368 struct rfct_control *cnt = control[fd];
373 if ((res = read(fd, buf, toget - got)) < 0)
379 TRC(fprintf(stderr, "Error on read.\n"));
384 if (!res) /* peer closed network connection */
387 cnt->event = T_DISCONNECT; /* is this correct ? ## */
388 cnt->hlen = cnt->pending = 0;
389 cnt->state = T_IDLE; /* this can't be correct ## */
394 while ((got += res) < toget);
398 int t_rcvconnect(int fd, struct t_call *call)
400 struct rfct_control *cnt = control[fd];
401 struct tpdu_connect_header chead;
402 char buf[100], *p, *addrp;
403 struct sockaddr_in peer;
404 int len = sizeof(struct sockaddr_in);
406 TRC(fprintf(stderr, "T_RCVCONNECT\n"));
407 if (!cnt || cnt->state != T_OUTCON)
409 TRC(fprintf(stderr, "TOUTSTATE\n"));
414 if (t_look_wait(fd, 1) <= 0)
416 if (cnt->event != T_CONNECT)
421 /* read the rest of the CC TPDU */
422 if (read_n(fd, buf, cnt->hlen) <= 0)
424 memcpy(&chead, buf, 5);
425 if (chead.class != 0)
427 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
432 *(addrp = call->addr.buf) = 0;
434 for (p = buf + 5; cnt->hlen > 0;)
436 switch ((unsigned char)*p)
438 case TPDU_PARM_TSIZE:
439 cnt->tsize = 1 << *(p + 2);
441 case TPDU_PARM_CLDID:
444 if (*(p + 1) > TSEL_MAXLEN)
446 TRC(fprintf(stderr, "Called TSEL too long.\n"));
447 t_errno = TSYSERR; /* Wrong.. ## */
450 *addrp = *(p + 1); /* length */
451 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
452 addrp += *(p + 1); /* move past TSEL */
455 case TPDU_PARM_CLGID: break; /* ignoring this for now */
457 TRC(fprintf(stderr, "Inoring CR parameter: %d\n",
459 /* we silently ignore anything else */
462 cnt->hlen -= (unsigned char) *p + 2;
463 p += (unsigned char) *p + 1;
465 addrp++; /* skip to end of addr + 1 */
468 if (getpeername(fd, (struct sockaddr*) &peer, &len) < 0)
470 TRC(perror("getpeername()"));
474 *(addrp++) = sizeof(struct sockaddr_in);
475 memcpy(addrp, &peer, sizeof(struct sockaddr_in));
476 addrp += sizeof(struct sockaddr_in);
477 call->addr.len = addrp - call->addr.buf + 1;
479 cnt->state = T_DATAXFER;
484 int t_snd(int fd, char *buf, unsigned nbytes, int flags)
486 struct rfct_control *cnt = control[fd];
487 struct rfc_header rfc;
488 struct iovec vec[2]; /* RFC1006 header + T-header + (user data) */
489 int writ = 0, res, towrite, eot;
492 TRC(fprintf(stderr, "T_SND [%d bytes, flags %d]\n", nbytes, flags));
493 if (!cnt || cnt->state != T_DATAXFER)
495 TRC(fprintf(stderr, "Trying to write in the wrong state on fd %d.\n",
505 do /* write the TSDU (segment) in chunks depending on the TPDU max size */
507 if (cnt->togo > 0) /* we have a partial TPDU on the wire */
509 TRC(fprintf(stderr, " writing continuation block (%d)\n",
511 if ((res = write(fd, buf, cnt->togo)) < 0)
523 TRC(fprintf(stderr, " wrote %d, total %d\n", res, writ));
525 else /* prepare and send (possibly partial) header */
527 towrite = nbytes - writ;
528 if (towrite + 3 + 4 > cnt->tsize)
529 towrite = cnt->tsize - (3 + 4); /* space for DATA header */
530 rfc.version = RFC_VERSION;
532 rfc.len = htons(towrite + 4 + 3); /* RFC1006 length */
534 rfc.code = TPDU_CODE_DATA;
535 if (flags & T_MORE || towrite + writ < nbytes)
539 rfc.suffix[0] = eot << 7; /* DATA EOT marker */
541 head_offset = 7 + cnt->togo;
544 vec[0].iov_base = (caddr_t) (char*)&rfc + head_offset;
545 vec[0].iov_len = 7 - head_offset;
546 vec[1].iov_base = (caddr_t) buf + writ;
547 vec[1].iov_len = towrite;
548 TRC(fprintf(stderr, " sending beg of block (%d+%d)\n",
549 7 - head_offset, towrite));
550 if ((res = writev(fd, vec, 2)) < 0)
552 TRC(fprintf(stderr, " write returned -1\n"));
553 /* thwarted by flow control */
568 /* somewhat thwarted */
569 else if (res < towrite + 7 - head_offset)
572 * Write came up short. We assume that this is a flow-
573 * control thing, and return immediately. Maybe it'd
574 * be better to take another loop, and generate an
575 * actual EAGAIN from write?
577 TRC(fprintf(stderr, " write returned %d\n", res));
578 if (res < 7 - head_offset) /* we didn't send a full header */
580 cnt->togo = -(7 - head_offset - res);
584 else if ((res -= 7 - head_offset) < towrite) /* not all data */
586 cnt->togo = towrite - res;
587 return nbytes - (writ + res);
590 else /* whew... nonblocking I/O is hard work */
593 writ += res - (7 - head_offset);
597 while (writ < nbytes);
598 TRC(fprintf(stderr, " finishing with %d written\n", nbytes));
602 int _t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
604 struct rfct_control *cnt = control[fd];
605 struct tpdu_data_header dhead;
610 TRC(fprintf(stderr, "T_RCV [nbytes=%d, flags=0x%x]\n", nbytes, *flags));
611 if (!cnt || cnt->state != T_DATAXFER)
617 do /* loop until we have a full TSDU or an error */
620 if (t_look_wait(fd, 0) <= 0)
622 if (cnt->event != T_DATA)
627 if (cnt->hlen) /* beginning of block */
629 TRC(fprintf(stderr, " Beginning of TPDU\n"));
632 TRC(fprintf(stderr, "We can't handle parameters to DATA\n"));
636 toget = cnt->pending;
637 if (toget > nbytes - got)
638 toget = nbytes - got;
639 TRC(fprintf(stderr, " toget=%d\n", toget));
640 vec[0].iov_base = (caddr_t) &dhead;
642 vec[1].iov_base = (caddr_t) buf + got;
643 vec[1].iov_len = toget;
644 if ((res = readv(fd, vec, 2)) < 0)
646 TRC(perror("readv()"));
649 if (got) /* we got some data in previous cycle */
651 t_errno = TNODATA; /* no data */
657 TRC(fprintf(stderr, " readv() returned %d\n", res));
661 cnt->event = T_DISCONNECT;
665 cnt->eot_flag = (dhead.nr & DATA_EOT) >> 7;
668 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
671 else /* continuation */
673 TRC(fprintf(stderr, " Reading middle of TPDU\n"));
674 toget = cnt->pending;
675 if (toget > nbytes - got)
676 toget = nbytes - got;
677 TRC(fprintf(stderr, " toget=%d\n", toget));
678 if ((res = read(fd, buf + got, toget)) < 0)
680 TRC(perror("read()"));
683 if (got) /* we got some data in previous cycle */
685 t_errno = TNODATA; /* no data */
691 TRC(fprintf(stderr, " read() returned %d\n", res));
695 cnt->event = T_DISCONNECT;
700 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
703 TRC(fprintf(stderr, " bottom of loop: pending=%d, got=%d\n",
706 while (cnt->pending && got < nbytes);
707 *flags = cnt->pending || !cnt->eot_flag ? T_MORE : 0;
708 TRC(fprintf(stderr, " flags=0x%x\n", *flags));
711 TRC(fprintf(stderr, " Return value: %d\n", got));
712 memset(buf + got, 0, 10);
716 int t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
723 if ((res = _t_rcv(fd, buf, nbytes, flags)) <= 0)
729 while (*flags & T_MORE && nbytes > 0);
735 struct rfct_control *cnt = control[fd];
737 TRC(fprintf(stderr, "T_CLOSE\n"));
738 if (!cnt || cnt->state == T_UNINIT)
740 TRC(fprintf(stderr, "Trying to close a bad fd.\n"));
749 * This isn't right, obviously.
751 int t_error(char *errmsg)
753 TRC(fprintf(stderr, "T_ERROR\n"));
754 fprintf(stderr, "t_error(t_errno=%d):", t_errno);
760 * Put a select in here!!
762 static int t_look_wait(int fd, int wait)
764 struct rfct_control *cnt = control[fd];
765 struct rfc_header head;
768 TRC(fprintf(stderr, "T_LOOK\n"));
769 if (!cnt || cnt->state == T_UNINIT)
776 if (cnt->state == T_IDLE && cnt->tmpfd < 0)
777 return T_LISTEN; /* the only possible type of event */
778 if ((res = read_n(fd, (char*) &head, 6)) < 0)
780 if (head.version != RFC_VERSION)
782 TRC(fprintf(stderr, "Got bad RFC1006 version in t_look: %d.\n",
784 t_errno = TSYSERR; /* should signal protocol error, somehow ## */
787 cnt->curcode = head.code;
788 cnt->hlen = head.hlen - 1; /* length of header suffix */
789 cnt->pending = ntohs(head.len) - 6 - cnt->hlen;
790 TRC(fprintf(stderr, "t_look: len=%d, code=0x%2.2x, hlen=%d.\n",
791 cnt->pending + 6, cnt->curcode, cnt->hlen));
792 switch (cnt->curcode)
794 case TPDU_CODE_CREQ: cnt->event = T_LISTEN; break;
795 case TPDU_CODE_CCON: cnt->event = T_CONNECT; break;
796 case TPDU_CODE_DATA: cnt->event = T_DATA; break;
797 case TPDU_CODE_DREQ: cnt->event = T_DISCONNECT; break;
799 TRC(fprintf(stderr, "t_look: Bad package: 0x%2.2x.\n", cnt->curcode));
800 t_errno = TSYSERR; /* protocol error */
808 return t_look_wait(fd, 0);
812 * If the user doesn't provide a NSAP, and qlen > 0, we'll do a default
813 * server bind. If qlen==0, we won't bind at all - connect will do that
816 int t_bind(int fd, struct t_bind *req, struct t_bind *ret)
818 struct rfct_control *cnt = control[fd];
819 struct sockaddr_in addr;
823 TRC(fprintf(stderr, "T_BIND\n"));
824 if (!cnt || cnt->state != T_UNBND)
826 TRC(fprintf(stderr, "Bad state\n"));
833 cnt->qlen = req->qlen < MAX_QLEN ? req->qlen : MAX_QLEN;
837 if (*p > TSEL_MAXLEN)
839 TRC(fprintf(stderr, "Tsel too large.\n"));
845 memcpy(cnt->ltsel, p + 1, cnt->ltsel_len);
846 p += cnt->ltsel_len + 1;
847 if (*p < sizeof(addr))
849 TRC(fprintf(stderr, "W: No NSAP provided for local bind\n"));
853 memcpy(&addr, p + 1, sizeof(addr));
860 if (!got_addr) /* user didn't give an address - local bind */
862 addr.sin_family = AF_INET;
863 addr.sin_addr.s_addr = INADDR_ANY;
865 addr.sin_port = htons(RFC_DEFAULT_PORT);
866 else /* we'll leave binding to connect - just set dummy */
867 addr.sin_port = 0; /* dummy for ret */
869 if (cnt->qlen && bind(fd, (struct sockaddr *) &addr,
872 TRC(perror("bind()"));
873 t_errno = TSYSERR; /* this should be refined */
878 if (listen(fd, cnt->qlen) < 0)
884 TRC(fprintf(stderr, " listen OK\n"));
887 /* All right! Now let's give something back, if our user wants it */
890 ret->qlen = cnt->qlen;
891 if (ret->addr.maxlen < (ret->addr.len = cnt->ltsel_len + 2 +
894 /* No space - but we're still bound */
900 *(p++) = cnt->ltsel_len;
902 memcpy(p, cnt->ltsel, cnt->ltsel_len);
904 *(p++) = sizeof(addr);
905 memcpy(p, &addr, sizeof(addr));
911 * need to consult RFC1006 on these. I think they just map to close()...
913 int t_snddis(int fd, struct t_call *call)
915 TRC(fprintf(stderr, "T_SNDDIS\n"));
919 int t_rcvdis(int fd, struct t_discon *discon)
921 struct rfct_control *cnt = control[fd];
922 struct tpdu_disconnect_header chead;
923 char udata[64], buf[256], *p;
925 TRC(fprintf(stderr, "T_RCVDIS\n"));
928 TRC(fprintf(stderr, "TOUTSTATE\n"));
933 if (t_look_wait(fd, 1) <= 0)
935 if (cnt->event != T_DISCONNECT)
940 /* read the rest of the DR TPDU */
941 if (read_n(fd, buf, cnt->hlen) <= 0)
943 memcpy(&chead, buf, 5);
945 for (p = buf + 5; cnt->hlen > 0;)
947 switch ((unsigned char)*p)
950 TRC(fprintf(stderr, "Inoring RD parameter: %d\n",
952 /* we silently ignore anything else */
955 cnt->hlen -= (unsigned char) *p + 2;
956 p += (unsigned char) *p + 1;
960 if (read_n(fd, udata, cnt->pending) < cnt->pending)
962 TRC(fprintf(stderr, "Unable to read user data\n"));
967 cnt->state = T_IDLE; /* should we close transport? */
971 discon->sequence = -1; /* TOFIX */
972 discon->reason = chead.reason;
973 TRC(fprintf(stderr, "Diconnect reason %d\n", chead.reason));
974 if (cnt->pending > discon->udata.maxlen)
981 memcpy(discon->udata.buf, udata, cnt->pending);
982 udata[cnt->pending] = '\0';
983 TRC(fprintf(stderr, "Discon udata: '%s'\n", udata));
985 discon->udata.len = cnt->pending;
991 * fix memory management, you bad Sebastian!
993 int t_free(char *ptr, int struct_type)
995 TRC(fprintf(stderr, "T_FREE\n"));
1000 char *t_alloc(int fd, int struct_type, int fields)
1002 char *r = malloc(1024);
1005 TRC(fprintf(stderr, "T_ALLOC\n"));
1011 * this is required for t_listen... if a system doesn't support dup2(), we're
1012 * in trouble: We might not be able to do nonblocking listen. Time will tell.
1014 static int switch_fds(int fd1, int fd2)
1017 struct rfct_control *tmpc;
1019 TRC(fprintf(stderr, "Switching fds %d <--> %d\n", fd1, fd2));
1020 if ((tmp = dup(fd1)) < 0 ||
1021 dup2(fd2, fd1) < 0 ||
1022 dup2(tmp, fd2) < 0 ||
1025 tmpc = control[fd1];
1026 control[fd1] = control[fd2];
1027 control[fd2] = tmpc;
1031 static int rcvconreq(int fd, struct t_call *call)
1033 struct rfct_control *cnt = control[fd];
1034 struct rfct_control *new = control[cnt->tmpfd];
1035 struct tpdu_connect_header chead;
1037 char *p, *addrp = 0;
1038 struct sockaddr_in addr;
1039 int len = sizeof(struct sockaddr_in);
1042 TRC(fprintf(stderr, "RCVCONRES\n"));
1048 for (qslot = 0; qslot < cnt->qlen; qslot++)
1049 if (cnt->oci[qslot] < 0)
1051 if (qslot == cnt->qlen) /* no free slots - shouldn't happen here */
1056 /* read the rest of the CREQ TPDU */
1057 if (read_n(cnt->tmpfd, buf, new->hlen) <= 0)
1059 memcpy(&chead, buf, 5);
1060 if (chead.class != 0)
1062 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
1066 memcpy(new->rref, chead.src_ref, 2); /* we'll echo this back at her */
1068 if (call && call->addr.maxlen)
1069 *(addrp = call->addr.buf) = 0;
1070 for (p = buf + 5; new->hlen > 0;)
1072 switch ((unsigned char)*p)
1074 case TPDU_PARM_TSIZE:
1075 new->tsize = 1 << *(p + 2); /* we go with their max */
1077 case TPDU_PARM_CLDID: break; /* ignore */
1078 case TPDU_PARM_CLGID:
1081 if (*(p + 1) > TSEL_MAXLEN)
1083 TRC(fprintf(stderr, "Called TSEL too long.\n"));
1084 t_errno = TSYSERR; /* Wrong.. ## */
1087 *addrp = *(p + 1); /* length */
1088 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
1089 addrp += *(p + 1); /* move past TSEL */
1092 /* we silently ignore preferred TPDU size and others */
1095 new->hlen -= (unsigned char) *p + 2;
1096 p += (unsigned char) *p + 1;
1101 if (getpeername(cnt->tmpfd, (struct sockaddr*) &addr, &len) < 0)
1103 TRC(perror("getpeername()"));
1107 *(addrp++) = sizeof(struct sockaddr_in);
1108 memcpy(addrp, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
1112 cnt->oci[qslot] = cnt->tmpfd; /* move the new semi-connection to oci */
1113 call->sequence = qslot;
1115 cnt->state = T_INCON;
1120 * This construction is tricky in async mode: listen calls accept, which
1121 * generates a new fd for the association. Call is supposed to return
1122 * immediately if there is no CR on the line (which is highly likely),
1123 * and t-user will then try to use select() on the wrong socket, waiting for
1124 * the transport level package.
1126 * Compared to the upper-level u_* routines, we have one major handicap
1127 * and one major benefit: We *have* to handle two different endpoints
1128 * after t_listen (which includes sockets accept); and we have access
1131 * If the endpoint is configured for nonblocking I/O, and listen is not
1132 * able to immediately acquire the CR, the two fds are switched, so that
1133 * subsequent selects take place on the data socket, rather than the
1136 * At any rate, the data socket is saved, so that it can later be given
1137 * to the user in t_accept().
1139 int t_listen(int fd, struct t_call *call)
1141 struct rfct_control *cnt = control[fd], *new;
1142 struct sockaddr_in addr;
1143 int addrlen = sizeof(struct sockaddr_in);
1144 int tmpfd_is_new = 0; /* we've just accept()ed a connection */
1147 TRC(fprintf(stderr, "T_LISTEN\n"));
1148 if (!cnt || cnt->state != T_IDLE)
1150 TRC(fprintf(stderr, "T_listen expects state==T_IDLE (wrong?)\n"));
1151 t_errno = TOUTSTATE;
1159 for (i = 0; i < cnt->qlen; i++)
1160 if (cnt->oci[i] < 0)
1162 if (i == cnt->qlen) /* no slots in queue */
1164 TRC(fprintf(stderr, "No more space in queue\n"));
1165 t_errno = TBADF; /* what would be more correct? */
1170 TRC(fprintf(stderr, "Accept..."));
1171 if ((cnt->tmpfd = accept(fd, (struct sockaddr*) &addr, &addrlen)) < 0)
1173 if (errno == EWOULDBLOCK)
1176 TRC(fprintf(stderr, "Accept returned WOULDBLOCK\n"));
1180 TRC(fprintf(stderr, "accept failed\n"));
1185 #ifdef NONBLOCKING_OSI
1186 if ((cnt->flags & O_NONBLOCK) && fcntl(cnt->tmpfd, F_SETFL,
1194 TRC(fprintf(stderr, "Accept OK\n"));
1195 if (!(new = control[cnt->tmpfd] = malloc(sizeof(*new))))
1197 TRC(perror("malloc()"));
1203 new->state = T_IDLE;
1206 new->qlen = cnt->qlen;
1207 new->flags = cnt->flags;
1208 new->tmpfd = cnt->tmpfd;
1210 for (i = 0; i < MAX_QLEN; i++)
1213 /* we got a network connection. Now try to read transport CREQ TPDU */
1214 if ((event = t_look_wait(tmpfd_is_new ? cnt->tmpfd : fd, 1)) < 0)
1216 if (t_errno == TNODATA)
1221 * We give the user something to select on for the incoming
1222 * CR. Switch the new association with the listener socket.
1224 TRC(fprintf(stderr, "Switching FDs\n"));
1225 if (switch_fds(cnt->tmpfd, fd) < 0)
1235 t_close(cnt->tmpfd);
1238 return -1; /* t_look & t_read hopefully set up the right errcodes */
1242 /* We got something! */
1243 if (event != T_LISTEN)
1245 TRC(fprintf(stderr, "Expected T_LISTEN\n"));
1250 * switch back the fds, if necessary */
1251 if (!tmpfd_is_new && switch_fds(fd, cnt->tmpfd) < 0)
1256 if (rcvconreq(fd, call) < 0)
1258 t_close(cnt->tmpfd);
1266 * There's no clean mapping of this onto the socket interface. If someone
1267 * wants it, we could fake it by first closing the socket, and then
1268 * opening a new one and doing a dup2. That should be functionally
1271 int t_unbind(int fd)
1273 fprintf(stderr, "T_UNBIND [not supported by transport implementation]\n");
1274 t_errno = TNOTSUPPORT;
1278 int t_accept(int fd, int resfd, struct t_call *call)
1280 struct rfct_control *listener = control[fd]; /* listener handle */
1281 struct rfct_control *new; /* new, semi-complete association */
1282 struct rfct_control *res; /* resulting association handle */
1283 struct iovec vec[3]; /* RFC1006 header + T-header + parm */
1284 struct rfc_header rfc;
1285 struct tpdu_connect_header tpdu;
1286 unsigned char parm[3 + TSEL_MAXLEN + 2];
1289 TRC(fprintf(stderr, "T_ACCEPT\n"));
1290 if (!listener || listener->state != T_INCON)
1292 TRC(fprintf(stderr, "TOUTSTATE\n"));
1293 t_errno = TOUTSTATE;
1296 /* Get the semi-connection */
1297 if (call->sequence >= listener->qlen || listener->oci[call->sequence] < 0)
1299 TRC(fprintf(stderr, "TBADSEQ\n"));
1303 new = control[(newfd = listener->oci[call->sequence])];
1304 listener->oci[call->sequence] = -1;
1305 res = control[resfd];
1311 if (res != listener) /* move the new connection */
1313 TRC(fprintf(stderr, " Moving to new fd (%d)\n", resfd));
1314 if (res->state != T_IDLE || res->qlen)
1316 TRC(fprintf(stderr, "Trying to move new assc. to bad fd.\n"));
1320 dup2(newfd, resfd); /* closes resfd */
1322 control[resfd] = new;
1323 /* transfer local bindings from res */
1325 memcpy(control[resfd]->ltsel, res->ltsel, res->ltsel_len);
1326 control[resfd]->ltsel_len = res->ltsel_len;
1328 res = control[resfd];
1329 listener->event = 0;
1330 listener->state = T_IDLE;
1332 else /* lose our listener */
1334 TRC(fprintf(stderr, " Moving to listener fd\n"));
1335 for (i = 0; i < listener->qlen; i++)
1336 if (listener->oci[i] >= 0)
1338 TRC(fprintf(stderr, "Still conn indications on listener\n"));
1345 if (listener->ltsel_len)
1346 memcpy(control[resfd]->ltsel, listener->ltsel, listener->ltsel_len);
1347 control[resfd]->ltsel_len = listener->ltsel_len;
1349 res = control[resfd];
1351 rfc.version = RFC_VERSION;
1353 rfc.code = TPDU_CODE_CCON;
1355 memcpy(tpdu.src_ref, "AA", 2);
1356 memcpy(tpdu.dst_ref, res->rref, 2); /* echo back at 'em */
1359 /* grant them their TPDU size */
1360 parm[0] = TPDU_PARM_TSIZE;
1362 for (i = 7; i <= 11 && (1 << i) < res->tsize; i++) ; /* encode TPDU size */
1364 /* give our TSEL. ## Must we echo theirs, if given? check spec */
1365 /* I thought it was ok to give an empty TSEL. Does it have semantic sig? */
1366 parm[3] = TPDU_PARM_CLDID;
1367 parm[4] = res->ltsel_len;
1369 memcpy(parm + 5, res->ltsel, res->ltsel_len);
1371 rfc.len = htons(4 + 7 + 3 + 2 + res->ltsel_len);
1372 rfc.hlen = 6 + 3 + 2 + res->ltsel_len;
1373 vec[0].iov_base = (caddr_t) &rfc;
1375 vec[1].iov_base = (caddr_t) &tpdu;
1377 vec[2].iov_base = (caddr_t) parm;
1378 vec[2].iov_len = 3 + 2 + res->ltsel_len;
1379 if (writev(resfd, vec, 3) < 4 + 7 + 3 + (2 + res->ltsel_len))
1381 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
1385 res->state = T_DATAXFER;
1390 int t_getstate(int fd)
1392 TRC(fprintf(stderr, "T_GETSTATE\n"));
1393 return control[fd] ? control[fd]->state : T_UNINIT;