4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1995-06-15 07:45:11 quinn
10 * Revision 1.5 1995/05/31 08:29:35 quinn
11 * Nothing significant.
13 * Revision 1.4 1995/05/18 13:02:07 quinn
16 * Revision 1.3 1995/05/16 09:37:18 quinn
19 * Revision 1.2 1995/05/02 08:53:09 quinn
20 * Trying in vain to fix comm with ISODE
22 * Revision 1.1 1995/03/30 14:03:17 quinn
23 * Added RFC1006 as separate library
25 * Revision 1.15 1995/03/30 10:54:43 quinn
26 * Fiddling with packet sizes, trying to help ISODE.. :(
28 * Revision 1.14 1995/03/27 08:36:07 quinn
29 * Some work on nonblocking operation in xmosi.c and rfct.c.
30 * Added protocol parameter to cs_create()
32 * Revision 1.13 1995/03/20 11:27:16 quinn
33 * Fixed bug in the _t_rcv stuff
35 * Revision 1.12 1995/03/20 09:54:07 quinn
38 * Revision 1.11 1995/03/20 09:47:16 quinn
39 * Added server-side support to xmosi.c
40 * Fixed possible problems in rfct
43 * Revision 1.10 1995/03/17 19:37:26 quinn
44 * *** empty log message ***
46 * Revision 1.9 1995/03/17 19:28:32 quinn
47 * Working on fixing our mystery-bug.
49 * Revision 1.8 1995/03/14 10:28:38 quinn
50 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
52 * Revision 1.7 1995/03/09 18:42:32 quinn
53 * Fixed another bug in t_rcv
55 * Revision 1.6 1995/03/09 15:22:42 quinn
56 * Fixed two bugs in get/rcv
58 * Revision 1.5 1995/03/07 16:29:46 quinn
61 * Revision 1.4 1995/03/06 16:48:03 quinn
64 * Revision 1.3 1995/03/06 10:54:32 quinn
65 * Server-side functions (t_bind/t_listen/t_accept) seem to work ok, now.
66 * Nonblocking mode needs work (and testing!)
67 * Added makensap to replace function in mosiutil.c.
69 * Revision 1.2 1995/03/03 09:40:36 quinn
70 * Added most of the remaining functionality.
71 * Still need exstensive testing of server-side functions and nonblocking
74 * Revision 1.1 1995/03/01 08:40:33 quinn
75 * First working version of rfct. Addressing needs work.
80 * Simple implementation of XTI/TP0/RFC1006/Sockets.
81 * Note: There is still some work to do in here, but basically it works.
83 * TODO: Asynchronous mode needs a lot of little adjustments to various
84 * return values and error codes, etc. Formally, we should probably hold
85 * this up against the protocol state tables, and see if it's correct.
87 * Check if addressing info is returned correctly by all calls.
92 #include <sys/types.h>
93 #include <sys/param.h>
94 #include <sys/socket.h>
96 #include <netinet/in.h>
97 #include <arpa/inet.h>
109 #include <linux/limits.h>
111 #include <dmalloc.h> /* project memory debugging - delete if you don't have it */
113 #ifdef TRACE_TRANSPORT
119 #define TSEL_MAXLEN 20 /* is there a standard for this? */
120 #define RFC_DEFAULT_PORT 4500
121 #define MAX_QLEN 5 /* outstanding connect indications */
123 static int t_look_wait(int fd, int wait);
124 int t_rcvconnect(int fd, struct t_call *call);
129 static struct rfct_control
131 int state; /* state of transport endpoint */
132 int event; /* current event */
133 int tsize; /* max TPDU size */
134 int curcode; /* code of current TPDU */
135 int pending; /* # bytes of user data waiting on line */
136 int eot_flag; /* current incoming TPDU had EOT set */
137 int hlen; /* TPDU header suffix remaining on line */
138 int eot; /* current incoming DATA TPDU is last in TSDU */
139 int togo; /* # bytes waiting to go on current outgoing DATA */
140 int qlen; /* set by t_bind */
141 int listen; /* we are listening */
142 int tmpfd; /* holding space for client between listen and accept */
143 int flags; /* set by t_open */
144 char rref[2]; /* remote reference */
145 char ltsel[TSEL_MAXLEN]; /* local transport selector */
147 int oci[MAX_QLEN]; /* outstanding connect indications */
151 * In the best tradition of ThinOSI, we combine the RFC1006 and the common
152 * part of the TPDU header into one. Given the complexity of the transport
153 * layer, maybe it would have been nice to have it in a separate module
154 * for reuse - but such is hindsight.
155 * Using a bit-field like this may be dangerous. I would expect it to work
156 * fine on any 32-bit or 64-bit machine, with any decent compiler. A DOS
157 * compiler might make a mess of it, I suppose, but I wouldn't even expect
164 #define RFC_VERSION 3
166 unsigned len:16; /* length of entire package, including header */
167 /* TPDU common fields */
168 unsigned char hlen; /* length of TPDU-header minus length-octet */
169 unsigned char code; /* TPDU code (and an unused 4-bit field) */
170 char suffix[100]; /* unstructured TPDU elements, for convenience */
174 * The TPDU-codes used by this package.
176 #define TPDU_CODE_CREQ 0xe0
177 #define TPDU_CODE_CCON 0xd0
178 #define TPDU_CODE_DATA 0xf0
179 #define TPDU_CODE_DREQ 0x80
182 * Parameters that we care about.
184 #define TPDU_PARM_TSIZE 0xc0 /* max TPDU size */
185 #define TPDU_PARM_CLGID 0xc1 /* Calling TSAP-ID */
186 #define TPDU_PARM_CLDID 0xc2 /* Called TSAP-ID */
188 struct tpdu_connect_header /* length of fixed suffix: 5 octets */
190 char dst_ref[2]; /* 3-4 - not used by TP0 */
191 char src_ref[2]; /* 5-6 - not used by TP0 */
192 char class; /* 7 - always 0 */
195 struct tpdu_disconnect_header /* length of fixed suffix: 5 octets */
202 struct tpdu_data_header /* length of fixed suffix: 1 octet */
204 unsigned char nr; /* we only use bit 7 (1 << 7), to mark EOT */
205 #define DATA_EOT (1<<7)
208 static int rfc_running = 0; /* Have we been initialized? */
210 static void init_rfc(void)
214 for (i = 0; i < NOFILE; i++)
219 int t_open(char *name, int oflag, struct t_info *info)
221 struct rfct_control *cnt;
222 struct protoent *proto;
225 TRC(fprintf(stderr, "T_OPEN\n"));
230 if (!(proto = getprotobyname("tcp")))
232 if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
234 #ifdef NONBLOCKING_OSI
235 if ((oflag & O_NONBLOCK) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
241 if (!(cnt = control[s] = malloc(sizeof(struct rfct_control))))
243 TRC(perror("malloc()"));
250 cnt->state = T_UNBND;
257 for (i = 0; i < MAX_QLEN; i++)
261 * RFC1006 sets a higher than standard (TP) default max TPDU size, but the
262 * Isode seems to like to negotiate it down. We'll keep it here to be
263 * safe. Note that there's no harm in jumping it up. If it's higher
264 * than 2048, t_connect won't try to negotiate.
270 info->addr = TSEL_MAXLEN + sizeof(struct sockaddr_in) + 1;
271 info->options = 1024;
272 info->tsdu = -1; /* is this right? */
276 info->servtype = T_COTS_ORD; /* lets hope our user doesn't
277 try something funny. */
282 int t_connect(int fd, struct t_call *sndcall, struct t_call *rcvcall)
284 struct rfct_control *cnt = control[fd];
285 struct sockaddr_in addr;
287 struct iovec vec[3]; /* RFC1006 header + T-header + parms */
288 struct rfc_header rfc;
289 struct tpdu_connect_header tpdu;
290 unsigned char pbuf[2 + TSEL_MAXLEN + 3]; /* CR parameters */
293 TRC(fprintf(stderr, "T_CONNECT\n"));
294 if (!cnt || cnt->state != T_IDLE)
296 TRC(fprintf(stderr, "TOUTSTATE\n"));
300 /* take the address apart */
301 p = sndcall->addr.buf;
302 if (*p) /* transport selector */
304 TRC(fprintf(stderr, "Tsel length is %d.\n", *p));
305 pbuf[0] = TPDU_PARM_CLDID;
307 memcpy(pbuf + 2, p + 1, *p);
310 p += *p + 1; /* skip tsel */
311 if (*p != sizeof(addr))
313 TRC(fprintf(stderr, "Expected sockaddr here.\n"));
318 memcpy(&addr, p, sizeof(addr));
319 if (connect(fd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
326 * If the default set by t_open() is higher than 2048, we don't try
327 * to negotiate, hoping that the opponent goes by the high default
328 * set by RFC1006. Otherwise, we just go for 2048 (max according to
329 * transport standard). The rest of this module doesn't really care
330 * about the size (although it's respected, of course), since we do
331 * no internal buffering.
333 if (cnt->tsize <= 2048)
335 pbuf[plen++] = TPDU_PARM_TSIZE;
337 pbuf[plen++] = 0x0b; /* request max PDU size (2048 octets) */
340 rfc.version = RFC_VERSION;
342 rfc.len = htons(4 + 7 + plen);
344 rfc.code = TPDU_CODE_CREQ;
346 memset(tpdu.dst_ref, 0, 2);
347 memset(tpdu.src_ref, 0, 2);
350 vec[0].iov_base = (caddr_t) &rfc;
352 vec[1].iov_base = (caddr_t) &tpdu;
354 vec[2].iov_base = (caddr_t) pbuf;
355 vec[2].iov_len = plen;
358 * we don't expect flow-control problems on the first outgoing packet,
359 * though I suppose it'd be possible on some weird types of link.
361 if (writev(fd, vec, 3) < 4 + 7 + plen)
363 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
367 cnt->state = T_OUTCON;
369 return t_rcvconnect(fd, rcvcall);
373 * This needs work for asynchronous mode.
375 static int read_n(int fd, char *buf, int toget)
377 struct rfct_control *cnt = control[fd];
382 if ((res = read(fd, buf, toget - got)) < 0)
388 TRC(fprintf(stderr, "Error on read.\n"));
393 if (!res) /* peer closed network connection */
396 cnt->event = T_DISCONNECT; /* is this correct ? ## */
397 cnt->hlen = cnt->pending = 0;
398 cnt->state = T_IDLE; /* this can't be correct ## */
403 while ((got += res) < toget);
407 int t_rcvconnect(int fd, struct t_call *call)
409 struct rfct_control *cnt = control[fd];
410 struct tpdu_connect_header chead;
411 char buf[100], *p, *addrp;
412 struct sockaddr_in peer;
413 int len = sizeof(struct sockaddr_in);
415 TRC(fprintf(stderr, "T_RCVCONNECT\n"));
416 if (!cnt || cnt->state != T_OUTCON)
418 TRC(fprintf(stderr, "TOUTSTATE\n"));
423 if (t_look_wait(fd, 1) <= 0)
425 if (cnt->event != T_CONNECT)
430 /* read the rest of the CC TPDU */
431 if (read_n(fd, buf, cnt->hlen) <= 0)
433 memcpy(&chead, buf, 5);
434 if (chead.class != 0)
436 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
441 *(addrp = call->addr.buf) = 0;
443 for (p = buf + 5; cnt->hlen > 0;)
445 switch ((unsigned char)*p)
447 case TPDU_PARM_TSIZE:
448 cnt->tsize = 1 << *(p + 2);
450 case TPDU_PARM_CLDID:
453 if (*(p + 1) > TSEL_MAXLEN)
455 TRC(fprintf(stderr, "Called TSEL too long.\n"));
456 t_errno = TSYSERR; /* Wrong.. ## */
459 *addrp = *(p + 1); /* length */
460 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
461 addrp += *(p + 1); /* move past TSEL */
464 case TPDU_PARM_CLGID: break; /* ignoring this for now */
466 TRC(fprintf(stderr, "Inoring CR parameter: %d\n",
468 /* we silently ignore anything else */
471 cnt->hlen -= (unsigned char) *p + 2;
472 p += (unsigned char) *p + 1;
474 addrp++; /* skip to end of addr + 1 */
477 if (getpeername(fd, (struct sockaddr*) &peer, &len) < 0)
479 TRC(perror("getpeername()"));
483 *(addrp++) = sizeof(struct sockaddr_in);
484 memcpy(addrp, &peer, sizeof(struct sockaddr_in));
485 addrp += sizeof(struct sockaddr_in);
486 call->addr.len = addrp - call->addr.buf + 1;
488 cnt->state = T_DATAXFER;
493 int t_snd(int fd, char *buf, unsigned nbytes, int flags)
495 struct rfct_control *cnt = control[fd];
496 struct rfc_header rfc;
497 struct iovec vec[2]; /* RFC1006 header + T-header + (user data) */
498 int writ = 0, res, towrite, eot;
501 TRC(fprintf(stderr, "T_SND [%d bytes, flags %d]\n", nbytes, flags));
502 if (!cnt || cnt->state != T_DATAXFER)
504 TRC(fprintf(stderr, "Trying to write in the wrong state on fd %d.\n",
514 do /* write the TSDU (segment) in chunks depending on the TPDU max size */
516 if (cnt->togo > 0) /* we have a partial TPDU on the wire */
518 TRC(fprintf(stderr, " writing continuation block (%d)\n",
520 if ((res = write(fd, buf, cnt->togo)) < 0)
532 TRC(fprintf(stderr, " wrote %d, total %d\n", res, writ));
534 else /* prepare and send (possibly partial) header */
536 towrite = nbytes - writ;
537 if (towrite + 3 + 4 > cnt->tsize)
538 towrite = cnt->tsize - (3 + 4); /* space for DATA header */
539 rfc.version = RFC_VERSION;
541 rfc.len = htons(towrite + 4 + 3); /* RFC1006 length */
543 rfc.code = TPDU_CODE_DATA;
544 if (flags & T_MORE || towrite + writ < nbytes)
548 rfc.suffix[0] = eot << 7; /* DATA EOT marker */
550 head_offset = 7 + cnt->togo;
553 vec[0].iov_base = (caddr_t) (char*)&rfc + head_offset;
554 vec[0].iov_len = 7 - head_offset;
555 vec[1].iov_base = (caddr_t) buf + writ;
556 vec[1].iov_len = towrite;
557 TRC(fprintf(stderr, " sending beg of block (%d+%d)\n",
558 7 - head_offset, towrite));
559 if ((res = writev(fd, vec, 2)) < 0)
561 TRC(fprintf(stderr, " write returned -1\n"));
562 /* thwarted by flow control */
577 /* somewhat thwarted */
578 else if (res < towrite + 7 - head_offset)
581 * Write came up short. We assume that this is a flow-
582 * control thing, and return immediately. Maybe it'd
583 * be better to take another loop, and generate an
584 * actual EAGAIN from write?
586 TRC(fprintf(stderr, " write returned %d\n", res));
587 if (res < 7 - head_offset) /* we didn't send a full header */
589 cnt->togo = -(7 - head_offset - res);
593 else if ((res -= 7 - head_offset) < towrite) /* not all data */
595 cnt->togo = towrite - res;
596 return nbytes - (writ + res);
599 else /* whew... nonblocking I/O is hard work */
602 writ += res - (7 - head_offset);
606 while (writ < nbytes);
607 TRC(fprintf(stderr, " finishing with %d written\n", nbytes));
611 int t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
613 struct rfct_control *cnt = control[fd];
614 struct tpdu_data_header dhead;
619 TRC(fprintf(stderr, "T_RCV [nbytes=%d, flags=0x%x]\n", nbytes, *flags));
620 if (!cnt || cnt->state != T_DATAXFER)
626 do /* loop until we have a full TSDU or an error */
629 if (t_look_wait(fd, 0) <= 0)
631 if (cnt->event != T_DATA)
636 if (cnt->hlen) /* beginning of block */
638 TRC(fprintf(stderr, " Beginning of TPDU\n"));
641 TRC(fprintf(stderr, "We can't handle parameters to DATA\n"));
645 toget = cnt->pending;
646 if (toget > nbytes - got)
647 toget = nbytes - got;
648 TRC(fprintf(stderr, " toget=%d\n", toget));
649 vec[0].iov_base = (caddr_t) &dhead;
651 vec[1].iov_base = (caddr_t) buf + got;
652 vec[1].iov_len = toget;
653 if ((res = readv(fd, vec, 2)) < 0)
655 TRC(perror("readv()"));
658 if (got) /* we got some data in previous cycle */
660 t_errno = TNODATA; /* no data */
666 TRC(fprintf(stderr, " readv() returned %d\n", res));
670 cnt->event = T_DISCONNECT;
674 cnt->eot_flag = (dhead.nr & DATA_EOT) >> 7;
677 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
680 else /* continuation */
682 TRC(fprintf(stderr, " Reading middle of TPDU\n"));
683 toget = cnt->pending;
684 if (toget > nbytes - got)
685 toget = nbytes - got;
686 TRC(fprintf(stderr, " toget=%d\n", toget));
687 if ((res = read(fd, buf + got, toget)) < 0)
689 TRC(perror("read()"));
692 if (got) /* we got some data in previous cycle */
694 t_errno = TNODATA; /* no data */
700 TRC(fprintf(stderr, " read() returned %d\n", res));
704 cnt->event = T_DISCONNECT;
709 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
712 TRC(fprintf(stderr, " bottom of loop: pending=%d, got=%d\n",
715 while (cnt->pending && got < nbytes);
716 *flags = cnt->pending || !cnt->eot_flag ? T_MORE : 0;
717 TRC(fprintf(stderr, " flags=0x%x\n", *flags));
720 TRC(fprintf(stderr, " Return value: %d\n", got));
721 memset(buf + got, 0, 10);
727 int t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
734 if ((res = _t_rcv(fd, buf, nbytes, flags)) <= 0)
740 while (*flags & T_MORE && nbytes > 0);
748 struct rfct_control *cnt = control[fd];
750 TRC(fprintf(stderr, "T_CLOSE\n"));
751 if (!cnt || cnt->state == T_UNINIT)
753 TRC(fprintf(stderr, "Trying to close a bad fd.\n"));
762 * This isn't right, obviously.
764 int t_error(char *errmsg)
766 TRC(fprintf(stderr, "T_ERROR\n"));
767 fprintf(stderr, "t_error(t_errno=%d):", t_errno);
773 * Put a select in here!!
775 static int t_look_wait(int fd, int wait)
777 struct rfct_control *cnt = control[fd];
778 struct rfc_header head;
781 TRC(fprintf(stderr, "T_LOOK\n"));
782 if (!cnt || cnt->state == T_UNINIT)
789 if (cnt->state == T_IDLE && cnt->tmpfd < 0)
790 return T_LISTEN; /* the only possible type of event */
791 if ((res = read_n(fd, (char*) &head, 6)) < 0)
795 TRC(fprintf(stderr, "Network disconnect\n"));
796 return cnt->event = T_DISCONNECT;
798 TRC(fprintf(stderr, "t_look got %d bytes\n", res));
799 if (head.version != RFC_VERSION)
801 TRC(fprintf(stderr, "Got bad RFC1006 version in t_look: %d.\n",
803 t_errno = TSYSERR; /* should signal protocol error, somehow ## */
806 cnt->curcode = head.code;
807 cnt->hlen = head.hlen - 1; /* length of header suffix */
808 cnt->pending = ntohs(head.len) - 6 - cnt->hlen;
809 TRC(fprintf(stderr, "t_look: len=%d, code=0x%2.2x, hlen=%d.\n",
810 cnt->pending + 6, cnt->curcode, cnt->hlen));
811 switch (cnt->curcode)
813 case TPDU_CODE_CREQ: cnt->event = T_LISTEN; break;
814 case TPDU_CODE_CCON: cnt->event = T_CONNECT; break;
815 case TPDU_CODE_DATA: cnt->event = T_DATA; break;
816 case TPDU_CODE_DREQ: cnt->event = T_DISCONNECT; break;
818 TRC(fprintf(stderr, "t_look: Bad package: 0x%2.2x.\n", cnt->curcode));
819 t_errno = TSYSERR; /* protocol error */
827 return t_look_wait(fd, 0);
831 * If the user doesn't provide a NSAP, and qlen > 0, we'll do a default
832 * server bind. If qlen==0, we won't bind at all - connect will do that
835 int t_bind(int fd, struct t_bind *req, struct t_bind *ret)
837 struct rfct_control *cnt = control[fd];
838 struct sockaddr_in addr;
842 TRC(fprintf(stderr, "T_BIND\n"));
843 if (!cnt || cnt->state != T_UNBND)
845 TRC(fprintf(stderr, "Bad state\n"));
852 cnt->qlen = req->qlen < MAX_QLEN ? req->qlen : MAX_QLEN;
856 if (*p > TSEL_MAXLEN)
858 TRC(fprintf(stderr, "Tsel too large.\n"));
864 memcpy(cnt->ltsel, p + 1, cnt->ltsel_len);
865 p += cnt->ltsel_len + 1;
866 if (*p < sizeof(addr))
868 TRC(fprintf(stderr, "W: No NSAP provided for local bind\n"));
872 memcpy(&addr, p + 1, sizeof(addr));
879 if (!got_addr) /* user didn't give an address - local bind */
881 addr.sin_family = AF_INET;
882 addr.sin_addr.s_addr = INADDR_ANY;
884 addr.sin_port = htons(RFC_DEFAULT_PORT);
885 else /* we'll leave binding to connect - just set dummy */
886 addr.sin_port = 0; /* dummy for ret */
888 if (cnt->qlen && bind(fd, (struct sockaddr *) &addr,
891 TRC(perror("bind()"));
892 t_errno = TSYSERR; /* this should be refined */
897 if (listen(fd, cnt->qlen) < 0)
903 TRC(fprintf(stderr, " listen OK\n"));
906 /* All right! Now let's give something back, if our user wants it */
909 ret->qlen = cnt->qlen;
910 if (ret->addr.maxlen < (ret->addr.len = cnt->ltsel_len + 2 +
913 /* No space - but we're still bound */
919 *(p++) = cnt->ltsel_len;
921 memcpy(p, cnt->ltsel, cnt->ltsel_len);
923 *(p++) = sizeof(addr);
924 memcpy(p, &addr, sizeof(addr));
930 * need to consult RFC1006 on these. I think they just map to close()...
932 int t_snddis(int fd, struct t_call *call)
934 TRC(fprintf(stderr, "T_SNDDIS\n"));
938 int t_rcvdis(int fd, struct t_discon *discon)
940 struct rfct_control *cnt = control[fd];
941 struct tpdu_disconnect_header chead;
942 char udata[64], buf[256], *p;
944 TRC(fprintf(stderr, "T_RCVDIS\n"));
947 TRC(fprintf(stderr, "TOUTSTATE\n"));
952 if (t_look_wait(fd, 1) <= 0)
954 if (cnt->event != T_DISCONNECT)
959 /* read the rest of the DR TPDU */
960 if (read_n(fd, buf, cnt->hlen) <= 0)
962 memcpy(&chead, buf, 5);
964 for (p = buf + 5; cnt->hlen > 0;)
966 switch ((unsigned char)*p)
969 TRC(fprintf(stderr, "Inoring RD parameter: %d\n",
971 /* we silently ignore anything else */
974 cnt->hlen -= (unsigned char) *p + 2;
975 p += (unsigned char) *p + 1;
979 if (read_n(fd, udata, cnt->pending) < cnt->pending)
981 TRC(fprintf(stderr, "Unable to read user data\n"));
986 cnt->state = T_IDLE; /* should we close transport? */
990 discon->sequence = -1; /* TOFIX */
991 discon->reason = chead.reason;
992 TRC(fprintf(stderr, "Diconnect reason %d\n", chead.reason));
993 if (cnt->pending > discon->udata.maxlen)
1000 memcpy(discon->udata.buf, udata, cnt->pending);
1001 udata[cnt->pending] = '\0';
1002 TRC(fprintf(stderr, "Discon udata: '%s'\n", udata));
1004 discon->udata.len = cnt->pending;
1010 * fix memory management, you bad Sebastian!
1012 int t_free(char *ptr, int struct_type)
1014 TRC(fprintf(stderr, "T_FREE\n"));
1019 char *t_alloc(int fd, int struct_type, int fields)
1021 char *r = malloc(1024);
1024 TRC(fprintf(stderr, "T_ALLOC\n"));
1030 * this is required for t_listen... if a system doesn't support dup2(), we're
1031 * in trouble: We might not be able to do nonblocking listen. Time will tell.
1033 static int switch_fds(int fd1, int fd2)
1036 struct rfct_control *tmpc;
1038 TRC(fprintf(stderr, "Switching fds %d <--> %d\n", fd1, fd2));
1039 if ((tmp = dup(fd1)) < 0 ||
1040 dup2(fd2, fd1) < 0 ||
1041 dup2(tmp, fd2) < 0 ||
1044 tmpc = control[fd1];
1045 control[fd1] = control[fd2];
1046 control[fd2] = tmpc;
1050 static int rcvconreq(int fd, struct t_call *call)
1052 struct rfct_control *cnt = control[fd];
1053 struct rfct_control *new = control[cnt->tmpfd];
1054 struct tpdu_connect_header chead;
1056 char *p, *addrp = 0;
1057 struct sockaddr_in addr;
1058 int len = sizeof(struct sockaddr_in);
1061 TRC(fprintf(stderr, "RCVCONRES\n"));
1067 for (qslot = 0; qslot < cnt->qlen; qslot++)
1068 if (cnt->oci[qslot] < 0)
1070 if (qslot == cnt->qlen) /* no free slots - shouldn't happen here */
1075 /* read the rest of the CREQ TPDU */
1076 if (read_n(cnt->tmpfd, buf, new->hlen) <= 0)
1078 memcpy(&chead, buf, 5);
1079 if (chead.class != 0)
1081 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
1085 memcpy(new->rref, chead.src_ref, 2); /* we'll echo this back at her */
1087 if (call && call->addr.maxlen)
1088 *(addrp = call->addr.buf) = 0;
1089 for (p = buf + 5; new->hlen > 0;)
1091 switch ((unsigned char)*p)
1093 case TPDU_PARM_TSIZE:
1094 new->tsize = 1 << *(p + 2); /* we go with their max */
1096 case TPDU_PARM_CLDID: break; /* ignore */
1097 case TPDU_PARM_CLGID:
1100 if (*(p + 1) > TSEL_MAXLEN)
1102 TRC(fprintf(stderr, "Called TSEL too long.\n"));
1103 t_errno = TSYSERR; /* Wrong.. ## */
1106 *addrp = *(p + 1); /* length */
1107 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
1108 addrp += *(p + 1); /* move past TSEL */
1111 /* we silently ignore preferred TPDU size and others */
1114 new->hlen -= (unsigned char) *p + 2;
1115 p += (unsigned char) *p + 1;
1120 if (getpeername(cnt->tmpfd, (struct sockaddr*) &addr, &len) < 0)
1122 TRC(perror("getpeername()"));
1126 *(addrp++) = sizeof(struct sockaddr_in);
1127 memcpy(addrp, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
1131 cnt->oci[qslot] = cnt->tmpfd; /* move the new semi-connection to oci */
1132 call->sequence = qslot;
1134 cnt->state = T_INCON;
1139 * This construction is tricky in async mode: listen calls accept, which
1140 * generates a new fd for the association. Call is supposed to return
1141 * immediately if there is no CR on the line (which is highly likely),
1142 * and t-user will then try to use select() on the wrong socket, waiting for
1143 * the transport level package.
1145 * Compared to the upper-level u_* routines, we have one major handicap
1146 * and one major benefit: We *have* to handle two different endpoints
1147 * after t_listen (which includes sockets accept); and we have access
1150 * If the endpoint is configured for nonblocking I/O, and listen is not
1151 * able to immediately acquire the CR, the two fds are switched, so that
1152 * subsequent selects take place on the data socket, rather than the
1155 * At any rate, the data socket is saved, so that it can later be given
1156 * to the user in t_accept().
1158 int t_listen(int fd, struct t_call *call)
1160 struct rfct_control *cnt = control[fd], *new;
1161 struct sockaddr_in addr;
1162 int addrlen = sizeof(struct sockaddr_in);
1163 int tmpfd_is_new = 0; /* we've just accept()ed a connection */
1166 TRC(fprintf(stderr, "T_LISTEN\n"));
1167 if (!cnt || cnt->state != T_IDLE)
1169 TRC(fprintf(stderr, "T_listen expects state==T_IDLE (wrong?)\n"));
1170 t_errno = TOUTSTATE;
1178 for (i = 0; i < cnt->qlen; i++)
1179 if (cnt->oci[i] < 0)
1181 if (i == cnt->qlen) /* no slots in queue */
1183 TRC(fprintf(stderr, "No more space in queue\n"));
1184 t_errno = TBADF; /* what would be more correct? */
1189 TRC(fprintf(stderr, "Accept..."));
1190 if ((cnt->tmpfd = accept(fd, (struct sockaddr*) &addr, &addrlen)) < 0)
1192 if (errno == EWOULDBLOCK)
1195 TRC(fprintf(stderr, "Accept returned WOULDBLOCK\n"));
1199 TRC(fprintf(stderr, "accept failed\n"));
1204 #ifdef NONBLOCKING_OSI
1205 if ((cnt->flags & O_NONBLOCK) && fcntl(cnt->tmpfd, F_SETFL,
1213 TRC(fprintf(stderr, "Accept OK\n"));
1214 if (!(new = control[cnt->tmpfd] = malloc(sizeof(*new))))
1216 TRC(perror("malloc()"));
1222 new->state = T_IDLE;
1225 new->qlen = cnt->qlen;
1226 new->flags = cnt->flags;
1227 new->tmpfd = cnt->tmpfd;
1229 for (i = 0; i < MAX_QLEN; i++)
1232 /* we got a network connection. Now try to read transport CREQ TPDU */
1233 if ((event = t_look_wait(tmpfd_is_new ? cnt->tmpfd : fd, 1)) < 0)
1235 if (t_errno == TNODATA)
1240 * We give the user something to select on for the incoming
1241 * CR. Switch the new association with the listener socket.
1243 TRC(fprintf(stderr, "Switching FDs\n"));
1244 if (switch_fds(cnt->tmpfd, fd) < 0)
1254 t_close(cnt->tmpfd);
1257 return -1; /* t_look & t_read hopefully set up the right errcodes */
1261 /* We got something! */
1262 if (event != T_LISTEN)
1264 TRC(fprintf(stderr, "Expected T_LISTEN\n"));
1269 * switch back the fds, if necessary */
1270 if (!tmpfd_is_new && switch_fds(fd, cnt->tmpfd) < 0)
1275 if (rcvconreq(fd, call) < 0)
1277 t_close(cnt->tmpfd);
1285 * There's no clean mapping of this onto the socket interface. If someone
1286 * wants it, we could fake it by first closing the socket, and then
1287 * opening a new one and doing a dup2. That should be functionally
1290 int t_unbind(int fd)
1292 fprintf(stderr, "T_UNBIND [not supported by transport implementation]\n");
1293 t_errno = TNOTSUPPORT;
1297 int t_accept(int fd, int resfd, struct t_call *call)
1299 struct rfct_control *listener = control[fd]; /* listener handle */
1300 struct rfct_control *new; /* new, semi-complete association */
1301 struct rfct_control *res; /* resulting association handle */
1302 struct iovec vec[3]; /* RFC1006 header + T-header + parm */
1303 struct rfc_header rfc;
1304 struct tpdu_connect_header tpdu;
1305 unsigned char parm[3 + TSEL_MAXLEN + 2];
1308 TRC(fprintf(stderr, "T_ACCEPT\n"));
1309 if (!listener || listener->state != T_INCON)
1311 TRC(fprintf(stderr, "TOUTSTATE\n"));
1312 t_errno = TOUTSTATE;
1315 /* Get the semi-connection */
1316 if (call->sequence >= listener->qlen || listener->oci[call->sequence] < 0)
1318 TRC(fprintf(stderr, "TBADSEQ\n"));
1322 new = control[(newfd = listener->oci[call->sequence])];
1323 listener->oci[call->sequence] = -1;
1324 res = control[resfd];
1330 if (res != listener) /* move the new connection */
1332 TRC(fprintf(stderr, " Moving to new fd (%d)\n", resfd));
1333 if (res->state != T_IDLE || res->qlen)
1335 TRC(fprintf(stderr, "Trying to move new assc. to bad fd.\n"));
1339 dup2(newfd, resfd); /* closes resfd */
1341 control[resfd] = new;
1342 /* transfer local bindings from res */
1344 memcpy(control[resfd]->ltsel, res->ltsel, res->ltsel_len);
1345 control[resfd]->ltsel_len = res->ltsel_len;
1347 res = control[resfd];
1348 listener->event = 0;
1349 listener->state = T_IDLE;
1351 else /* lose our listener */
1353 TRC(fprintf(stderr, " Moving to listener fd\n"));
1354 for (i = 0; i < listener->qlen; i++)
1355 if (listener->oci[i] >= 0)
1357 TRC(fprintf(stderr, "Still conn indications on listener\n"));
1364 if (listener->ltsel_len)
1365 memcpy(control[resfd]->ltsel, listener->ltsel, listener->ltsel_len);
1366 control[resfd]->ltsel_len = listener->ltsel_len;
1368 res = control[resfd];
1370 rfc.version = RFC_VERSION;
1372 rfc.code = TPDU_CODE_CCON;
1374 memcpy(tpdu.src_ref, "AA", 2);
1375 memcpy(tpdu.dst_ref, res->rref, 2); /* echo back at 'em */
1378 /* grant them their TPDU size */
1379 parm[0] = TPDU_PARM_TSIZE;
1381 for (i = 7; i <= 11 && (1 << i) < res->tsize; i++) ; /* encode TPDU size */
1383 /* give our TSEL. ## Must we echo theirs, if given? check spec */
1384 /* I thought it was ok to give an empty TSEL. Does it have semantic sig? */
1385 parm[3] = TPDU_PARM_CLDID;
1386 parm[4] = res->ltsel_len;
1388 memcpy(parm + 5, res->ltsel, res->ltsel_len);
1390 rfc.len = htons(4 + 7 + 3 + 2 + res->ltsel_len);
1391 rfc.hlen = 6 + 3 + 2 + res->ltsel_len;
1392 vec[0].iov_base = (caddr_t) &rfc;
1394 vec[1].iov_base = (caddr_t) &tpdu;
1396 vec[2].iov_base = (caddr_t) parm;
1397 vec[2].iov_len = 3 + 2 + res->ltsel_len;
1398 if (writev(resfd, vec, 3) < 4 + 7 + 3 + (2 + res->ltsel_len))
1400 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
1404 res->state = T_DATAXFER;
1409 int t_getstate(int fd)
1411 TRC(fprintf(stderr, "T_GETSTATE\n"));
1412 return control[fd] ? control[fd]->state : T_UNINIT;