4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 2005-06-25 15:46:03 adam
8 * Expanded tabs in all source files. Added vim/emacs local variables
11 * Revision 1.9 1996/02/23 10:01:00 quinn
14 * Revision 1.8 1995/11/01 13:54:52 quinn
17 * Revision 1.7 1995/06/16 10:46:48 quinn
18 * *** empty log message ***
20 * Revision 1.6 1995/06/15 07:45:11 quinn
23 * Revision 1.5 1995/05/31 08:29:35 quinn
24 * Nothing significant.
26 * Revision 1.4 1995/05/18 13:02:07 quinn
29 * Revision 1.3 1995/05/16 09:37:18 quinn
32 * Revision 1.2 1995/05/02 08:53:09 quinn
33 * Trying in vain to fix comm with ISODE
35 * Revision 1.1 1995/03/30 14:03:17 quinn
36 * Added RFC1006 as separate library
38 * Revision 1.15 1995/03/30 10:54:43 quinn
39 * Fiddling with packet sizes, trying to help ISODE.. :(
41 * Revision 1.14 1995/03/27 08:36:07 quinn
42 * Some work on nonblocking operation in xmosi.c and rfct.c.
43 * Added protocol parameter to cs_create()
45 * Revision 1.13 1995/03/20 11:27:16 quinn
46 * Fixed bug in the _t_rcv stuff
48 * Revision 1.12 1995/03/20 09:54:07 quinn
51 * Revision 1.11 1995/03/20 09:47:16 quinn
52 * Added server-side support to xmosi.c
53 * Fixed possible problems in rfct
56 * Revision 1.10 1995/03/17 19:37:26 quinn
57 * *** empty log message ***
59 * Revision 1.9 1995/03/17 19:28:32 quinn
60 * Working on fixing our mystery-bug.
62 * Revision 1.8 1995/03/14 10:28:38 quinn
63 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
65 * Revision 1.7 1995/03/09 18:42:32 quinn
66 * Fixed another bug in t_rcv
68 * Revision 1.6 1995/03/09 15:22:42 quinn
69 * Fixed two bugs in get/rcv
71 * Revision 1.5 1995/03/07 16:29:46 quinn
74 * Revision 1.4 1995/03/06 16:48:03 quinn
77 * Revision 1.3 1995/03/06 10:54:32 quinn
78 * Server-side functions (t_bind/t_listen/t_accept) seem to work ok, now.
79 * Nonblocking mode needs work (and testing!)
80 * Added makensap to replace function in mosiutil.c.
82 * Revision 1.2 1995/03/03 09:40:36 quinn
83 * Added most of the remaining functionality.
84 * Still need exstensive testing of server-side functions and nonblocking
87 * Revision 1.1 1995/03/01 08:40:33 quinn
88 * First working version of rfct. Addressing needs work.
93 * Simple implementation of XTI/TP0/RFC1006/Sockets.
94 * Note: There is still some work to do in here, but basically it works.
96 * TODO: Asynchronous mode needs a lot of little adjustments to various
97 * return values and error codes, etc. Formally, we should probably hold
98 * this up against the protocol state tables, and see if it's correct.
100 * Check if addressing info is returned correctly by all calls.
105 #include <sys/types.h>
106 #include <sys/param.h>
107 #include <sys/socket.h>
109 #include <netinet/in.h>
110 #include <arpa/inet.h>
122 #include <linux/limits.h>
125 #ifdef TRACE_TRANSPORT
131 #define TSEL_MAXLEN 20 /* is there a standard for this? */
132 #define RFC_DEFAULT_PORT 4500
133 #define MAX_QLEN 5 /* outstanding connect indications */
135 static int t_look_wait(int fd, int wait);
136 int t_rcvconnect(int fd, struct t_call *call);
141 static struct rfct_control
143 int state; /* state of transport endpoint */
144 int event; /* current event */
145 int tsize; /* max TPDU size */
146 int curcode; /* code of current TPDU */
147 int pending; /* # bytes of user data waiting on line */
148 int eot_flag; /* current incoming TPDU had EOT set */
149 int hlen; /* TPDU header suffix remaining on line */
150 int eot; /* current incoming DATA TPDU is last in TSDU */
151 int togo; /* # bytes waiting to go on current outgoing DATA */
152 int qlen; /* set by t_bind */
153 int listen; /* we are listening */
154 int tmpfd; /* holding space for client between listen and accept */
155 int flags; /* set by t_open */
156 char rref[2]; /* remote reference */
157 char ltsel[TSEL_MAXLEN]; /* local transport selector */
159 int oci[MAX_QLEN]; /* outstanding connect indications */
163 * In the best tradition of ThinOSI, we combine the RFC1006 and the common
164 * part of the TPDU header into one. Given the complexity of the transport
165 * layer, maybe it would have been nice to have it in a separate module
166 * for reuse - but such is hindsight.
167 * Using a bit-field like this may be dangerous. I would expect it to work
168 * fine on any 32-bit or 64-bit machine, with any decent compiler. A DOS
169 * compiler might make a mess of it, I suppose, but I wouldn't even expect
176 #define RFC_VERSION 3
178 unsigned len:16; /* length of entire package, including header */
179 /* TPDU common fields */
180 unsigned char hlen; /* length of TPDU-header minus length-octet */
181 unsigned char code; /* TPDU code (and an unused 4-bit field) */
182 char suffix[100]; /* unstructured TPDU elements, for convenience */
186 * The TPDU-codes used by this package.
188 #define TPDU_CODE_CREQ 0xe0
189 #define TPDU_CODE_CCON 0xd0
190 #define TPDU_CODE_DATA 0xf0
191 #define TPDU_CODE_DREQ 0x80
194 * Parameters that we care about.
196 #define TPDU_PARM_TSIZE 0xc0 /* max TPDU size */
197 #define TPDU_PARM_CLGID 0xc1 /* Calling TSAP-ID */
198 #define TPDU_PARM_CLDID 0xc2 /* Called TSAP-ID */
200 struct tpdu_connect_header /* length of fixed suffix: 5 octets */
202 char dst_ref[2]; /* 3-4 - not used by TP0 */
203 char src_ref[2]; /* 5-6 - not used by TP0 */
204 char class; /* 7 - always 0 */
207 struct tpdu_disconnect_header /* length of fixed suffix: 5 octets */
214 struct tpdu_data_header /* length of fixed suffix: 1 octet */
216 unsigned char nr; /* we only use bit 7 (1 << 7), to mark EOT */
217 #define DATA_EOT (1<<7)
220 static int rfc_running = 0; /* Have we been initialized? */
222 static void init_rfc(void)
226 for (i = 0; i < NOFILE; i++)
231 int t_open(char *name, int oflag, struct t_info *info)
233 struct rfct_control *cnt;
234 struct protoent *proto;
237 TRC(fprintf(stderr, "T_OPEN\n"));
242 if (!(proto = getprotobyname("tcp")))
244 if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
246 #ifdef NONBLOCKING_OSI
247 if ((oflag & O_NONBLOCK) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
253 if (!(cnt = control[s] = malloc(sizeof(struct rfct_control))))
255 TRC(perror("malloc()"));
262 cnt->state = T_UNBND;
269 for (i = 0; i < MAX_QLEN; i++)
273 * RFC1006 sets a higher than standard (TP) default max TPDU size, but the
274 * Isode seems to like to negotiate it down. We'll keep it here to be
275 * safe. Note that there's no harm in jumping it up. If it's higher
276 * than 2048, t_connect won't try to negotiate.
282 info->addr = TSEL_MAXLEN + sizeof(struct sockaddr_in) + 1;
283 info->options = 1024;
284 info->tsdu = -1; /* is this right? */
288 info->servtype = T_COTS_ORD; /* lets hope our user doesn't
289 try something funny. */
294 int t_connect(int fd, struct t_call *sndcall, struct t_call *rcvcall)
296 struct rfct_control *cnt = control[fd];
297 struct sockaddr_in addr;
299 struct iovec vec[3]; /* RFC1006 header + T-header + parms */
300 struct rfc_header rfc;
301 struct tpdu_connect_header tpdu;
302 unsigned char pbuf[2 + TSEL_MAXLEN + 3]; /* CR parameters */
305 TRC(fprintf(stderr, "T_CONNECT\n"));
306 if (!cnt || cnt->state != T_IDLE)
308 TRC(fprintf(stderr, "TOUTSTATE\n"));
312 /* take the address apart */
313 p = sndcall->addr.buf;
314 if (*p) /* transport selector */
316 TRC(fprintf(stderr, "Tsel length is %d.\n", *p));
317 pbuf[0] = TPDU_PARM_CLDID;
319 memcpy(pbuf + 2, p + 1, *p);
322 p += *p + 1; /* skip tsel */
323 if (*p != sizeof(addr))
325 TRC(fprintf(stderr, "Expected sockaddr here.\n"));
330 memcpy(&addr, p, sizeof(addr));
331 if (connect(fd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
338 * If the default set by t_open() is higher than 2048, we don't try
339 * to negotiate, hoping that the opponent goes by the high default
340 * set by RFC1006. Otherwise, we just go for 2048 (max according to
341 * transport standard). The rest of this module doesn't really care
342 * about the size (although it's respected, of course), since we do
343 * no internal buffering.
345 if (cnt->tsize <= 2048)
347 pbuf[plen++] = TPDU_PARM_TSIZE;
349 pbuf[plen++] = 0x0b; /* request max PDU size (2048 octets) */
352 rfc.version = RFC_VERSION;
354 rfc.len = htons(4 + 7 + plen);
356 rfc.code = TPDU_CODE_CREQ;
358 memset(tpdu.dst_ref, 0, 2);
359 memset(tpdu.src_ref, 0, 2);
362 vec[0].iov_base = (caddr_t) &rfc;
364 vec[1].iov_base = (caddr_t) &tpdu;
366 vec[2].iov_base = (caddr_t) pbuf;
367 vec[2].iov_len = plen;
370 * we don't expect flow-control problems on the first outgoing packet,
371 * though I suppose it'd be possible on some weird types of link.
373 if (writev(fd, vec, 3) < 4 + 7 + plen)
375 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
379 cnt->state = T_OUTCON;
381 return t_rcvconnect(fd, rcvcall);
385 * This needs work for asynchronous mode.
387 static int read_n(int fd, char *buf, int toget)
389 struct rfct_control *cnt = control[fd];
394 if ((res = read(fd, buf, toget - got)) < 0)
400 TRC(fprintf(stderr, "Error on read.\n"));
405 if (!res) /* peer closed network connection */
408 cnt->event = T_DISCONNECT; /* is this correct ? ## */
409 cnt->hlen = cnt->pending = 0;
410 cnt->state = T_IDLE; /* this can't be correct ## */
415 while ((got += res) < toget);
419 int t_rcvconnect(int fd, struct t_call *call)
421 struct rfct_control *cnt = control[fd];
422 struct tpdu_connect_header chead;
423 char buf[100], *p, *addrp;
424 struct sockaddr_in peer;
425 int len = sizeof(struct sockaddr_in);
427 TRC(fprintf(stderr, "T_RCVCONNECT\n"));
428 if (!cnt || cnt->state != T_OUTCON)
430 TRC(fprintf(stderr, "TOUTSTATE\n"));
435 if (t_look_wait(fd, 1) <= 0)
437 if (cnt->event != T_CONNECT)
442 /* read the rest of the CC TPDU */
443 if (read_n(fd, buf, cnt->hlen) <= 0)
445 memcpy(&chead, buf, 5);
446 if (chead.class != 0)
448 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
453 *(addrp = call->addr.buf) = 0;
455 for (p = buf + 5; cnt->hlen > 0;)
457 switch ((unsigned char)*p)
459 case TPDU_PARM_TSIZE:
460 cnt->tsize = 1 << *(p + 2);
462 case TPDU_PARM_CLDID:
465 if (*(p + 1) > TSEL_MAXLEN)
467 TRC(fprintf(stderr, "Called TSEL too long.\n"));
468 t_errno = TSYSERR; /* Wrong.. ## */
471 *addrp = *(p + 1); /* length */
472 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
473 addrp += *(p + 1); /* move past TSEL */
476 case TPDU_PARM_CLGID: break; /* ignoring this for now */
478 TRC(fprintf(stderr, "Inoring CR parameter: %d\n",
480 /* we silently ignore anything else */
483 cnt->hlen -= (unsigned char) *p + 2;
484 p += (unsigned char) *p + 1;
486 addrp++; /* skip to end of addr + 1 */
489 if (getpeername(fd, (struct sockaddr*) &peer, &len) < 0)
491 TRC(perror("getpeername()"));
495 *(addrp++) = sizeof(struct sockaddr_in);
496 memcpy(addrp, &peer, sizeof(struct sockaddr_in));
497 addrp += sizeof(struct sockaddr_in);
498 call->addr.len = addrp - call->addr.buf + 1;
500 cnt->state = T_DATAXFER;
505 int t_snd(int fd, char *buf, unsigned nbytes, int flags)
507 struct rfct_control *cnt = control[fd];
508 struct rfc_header rfc;
509 struct iovec vec[2]; /* RFC1006 header + T-header + (user data) */
510 int writ = 0, res, towrite, eot;
513 TRC(fprintf(stderr, "T_SND [%d bytes, flags %d]\n", nbytes, flags));
514 if (!cnt || cnt->state != T_DATAXFER)
516 TRC(fprintf(stderr, "Trying to write in the wrong state on fd %d.\n",
526 do /* write the TSDU (segment) in chunks depending on the TPDU max size */
528 if (cnt->togo > 0) /* we have a partial TPDU on the wire */
530 TRC(fprintf(stderr, " writing continuation block (%d)\n",
532 if ((res = write(fd, buf, cnt->togo)) < 0)
544 TRC(fprintf(stderr, " wrote %d, total %d\n", res, writ));
546 else /* prepare and send (possibly partial) header */
548 towrite = nbytes - writ;
549 if (towrite + 3 + 4 > cnt->tsize)
550 towrite = cnt->tsize - (3 + 4); /* space for DATA header */
551 rfc.version = RFC_VERSION;
553 rfc.len = htons(towrite + 4 + 3); /* RFC1006 length */
555 rfc.code = TPDU_CODE_DATA;
556 if (flags & T_MORE || towrite + writ < nbytes)
560 rfc.suffix[0] = eot << 7; /* DATA EOT marker */
562 head_offset = 7 + cnt->togo;
565 vec[0].iov_base = (caddr_t) (char*)&rfc + head_offset;
566 vec[0].iov_len = 7 - head_offset;
567 vec[1].iov_base = (caddr_t) buf + writ;
568 vec[1].iov_len = towrite;
569 TRC(fprintf(stderr, " sending beg of block (%d+%d)\n",
570 7 - head_offset, towrite));
571 if ((res = writev(fd, vec, 2)) < 0)
573 TRC(fprintf(stderr, " write returned -1\n"));
574 /* thwarted by flow control */
589 /* somewhat thwarted */
590 else if (res < towrite + 7 - head_offset)
593 * Write came up short. We assume that this is a flow-
594 * control thing, and return immediately. Maybe it'd
595 * be better to take another loop, and generate an
596 * actual EAGAIN from write?
598 TRC(fprintf(stderr, " write returned %d\n", res));
599 if (res < 7 - head_offset) /* we didn't send a full header */
601 cnt->togo = -(7 - head_offset - res);
605 else if ((res -= 7 - head_offset) < towrite) /* not all data */
607 cnt->togo = towrite - res;
608 return nbytes - (writ + res);
611 else /* whew... nonblocking I/O is hard work */
614 writ += res - (7 - head_offset);
618 while (writ < nbytes);
619 TRC(fprintf(stderr, " finishing with %d written\n", nbytes));
623 int _t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
625 struct rfct_control *cnt = control[fd];
626 struct tpdu_data_header dhead;
631 TRC(fprintf(stderr, "T_RCV [nbytes=%d, flags=0x%x]\n", nbytes, *flags));
632 if (!cnt || cnt->state != T_DATAXFER)
638 do /* loop until we have a full TSDU or an error */
641 if (t_look_wait(fd, 0) <= 0)
643 if (cnt->event != T_DATA)
648 if (cnt->hlen) /* beginning of block */
650 TRC(fprintf(stderr, " Beginning of TPDU\n"));
653 TRC(fprintf(stderr, "We can't handle parameters to DATA\n"));
657 toget = cnt->pending;
658 if (toget > nbytes - got)
659 toget = nbytes - got;
660 TRC(fprintf(stderr, " toget=%d\n", toget));
661 vec[0].iov_base = (caddr_t) &dhead;
663 vec[1].iov_base = (caddr_t) buf + got;
664 vec[1].iov_len = toget;
665 if ((res = readv(fd, vec, 2)) < 0)
667 TRC(perror("readv()"));
670 if (got) /* we got some data in previous cycle */
672 t_errno = TNODATA; /* no data */
678 TRC(fprintf(stderr, " readv() returned %d\n", res));
682 cnt->event = T_DISCONNECT;
686 cnt->eot_flag = (dhead.nr & DATA_EOT) >> 7;
689 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
692 else /* continuation */
694 TRC(fprintf(stderr, " Reading middle of TPDU\n"));
695 toget = cnt->pending;
696 if (toget > nbytes - got)
697 toget = nbytes - got;
698 TRC(fprintf(stderr, " toget=%d\n", toget));
699 if ((res = read(fd, buf + got, toget)) < 0)
701 TRC(perror("read()"));
704 if (got) /* we got some data in previous cycle */
706 t_errno = TNODATA; /* no data */
712 TRC(fprintf(stderr, " read() returned %d\n", res));
716 cnt->event = T_DISCONNECT;
721 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
724 TRC(fprintf(stderr, " bottom of loop: pending=%d, got=%d\n",
727 while (cnt->pending && got < nbytes);
728 *flags = cnt->pending || !cnt->eot_flag ? T_MORE : 0;
729 TRC(fprintf(stderr, " flags=0x%x\n", *flags));
732 TRC(fprintf(stderr, " Return value: %d\n", got));
733 memset(buf + got, 0, 10);
739 int t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
746 if ((res = _t_rcv(fd, buf, nbytes, flags)) <= 0)
752 while (*flags & T_MORE && nbytes > 0);
760 struct rfct_control *cnt = control[fd];
762 TRC(fprintf(stderr, "T_CLOSE\n"));
763 if (!cnt || cnt->state == T_UNINIT)
765 TRC(fprintf(stderr, "Trying to close a bad fd.\n"));
774 * This isn't right, obviously.
776 int t_error(char *errmsg)
778 TRC(fprintf(stderr, "T_ERROR\n"));
779 fprintf(stderr, "t_error(t_errno=%d):", t_errno);
785 * Put a select in here!!
787 static int t_look_wait(int fd, int wait)
789 struct rfct_control *cnt = control[fd];
790 struct rfc_header head;
793 TRC(fprintf(stderr, "T_LOOK\n"));
794 if (!cnt || cnt->state == T_UNINIT)
801 if (cnt->state == T_IDLE && cnt->tmpfd < 0)
802 return T_LISTEN; /* the only possible type of event */
803 if ((res = read_n(fd, (char*) &head, 6)) < 0)
807 TRC(fprintf(stderr, "Network disconnect\n"));
808 return cnt->event = T_DISCONNECT;
810 TRC(fprintf(stderr, "t_look got %d bytes\n", res));
811 if (head.version != RFC_VERSION)
813 TRC(fprintf(stderr, "Got bad RFC1006 version in t_look: %d.\n",
815 t_errno = TSYSERR; /* should signal protocol error, somehow ## */
818 cnt->curcode = head.code;
819 cnt->hlen = head.hlen - 1; /* length of header suffix */
820 cnt->pending = ntohs(head.len) - 6 - cnt->hlen;
821 TRC(fprintf(stderr, "t_look: len=%d, code=0x%2.2x, hlen=%d.\n",
822 cnt->pending + 6, cnt->curcode, cnt->hlen));
823 switch (cnt->curcode)
825 case TPDU_CODE_CREQ: cnt->event = T_LISTEN; break;
826 case TPDU_CODE_CCON: cnt->event = T_CONNECT; break;
827 case TPDU_CODE_DATA: cnt->event = T_DATA; break;
828 case TPDU_CODE_DREQ: cnt->event = T_DISCONNECT; break;
830 TRC(fprintf(stderr, "t_look: Bad package: 0x%2.2x.\n", cnt->curcode));
831 t_errno = TSYSERR; /* protocol error */
839 return t_look_wait(fd, 0);
843 * If the user doesn't provide a NSAP, and qlen > 0, we'll do a default
844 * server bind. If qlen==0, we won't bind at all - connect will do that
847 int t_bind(int fd, struct t_bind *req, struct t_bind *ret)
849 struct rfct_control *cnt = control[fd];
850 struct sockaddr_in addr;
854 TRC(fprintf(stderr, "T_BIND\n"));
855 if (!cnt || cnt->state != T_UNBND)
857 TRC(fprintf(stderr, "Bad state\n"));
864 cnt->qlen = req->qlen < MAX_QLEN ? req->qlen : MAX_QLEN;
868 if (*p > TSEL_MAXLEN)
870 TRC(fprintf(stderr, "Tsel too large.\n"));
876 memcpy(cnt->ltsel, p + 1, cnt->ltsel_len);
877 p += cnt->ltsel_len + 1;
878 if (*p < sizeof(addr))
880 TRC(fprintf(stderr, "W: No NSAP provided for local bind\n"));
884 memcpy(&addr, p + 1, sizeof(addr));
891 if (!got_addr) /* user didn't give an address - local bind */
893 addr.sin_family = AF_INET;
894 addr.sin_addr.s_addr = INADDR_ANY;
896 addr.sin_port = htons(RFC_DEFAULT_PORT);
897 else /* we'll leave binding to connect - just set dummy */
898 addr.sin_port = 0; /* dummy for ret */
900 if (cnt->qlen && bind(fd, (struct sockaddr *) &addr,
903 TRC(perror("bind()"));
904 t_errno = TSYSERR; /* this should be refined */
909 if (listen(fd, cnt->qlen) < 0)
915 TRC(fprintf(stderr, " listen OK\n"));
918 /* All right! Now let's give something back, if our user wants it */
921 ret->qlen = cnt->qlen;
922 if (ret->addr.maxlen < (ret->addr.len = cnt->ltsel_len + 2 +
925 /* No space - but we're still bound */
931 *(p++) = cnt->ltsel_len;
933 memcpy(p, cnt->ltsel, cnt->ltsel_len);
935 *(p++) = sizeof(addr);
936 memcpy(p, &addr, sizeof(addr));
942 * need to consult RFC1006 on these. I think they just map to close()...
944 int t_snddis(int fd, struct t_call *call)
946 TRC(fprintf(stderr, "T_SNDDIS\n"));
950 int t_rcvdis(int fd, struct t_discon *discon)
952 struct rfct_control *cnt = control[fd];
953 struct tpdu_disconnect_header chead;
954 char udata[64], buf[256], *p;
956 TRC(fprintf(stderr, "T_RCVDIS\n"));
959 TRC(fprintf(stderr, "TOUTSTATE\n"));
964 if (t_look_wait(fd, 1) <= 0)
966 if (cnt->event != T_DISCONNECT)
971 /* read the rest of the DR TPDU */
972 if (read_n(fd, buf, cnt->hlen) <= 0)
974 memcpy(&chead, buf, 5);
976 for (p = buf + 5; cnt->hlen > 0;)
978 switch ((unsigned char)*p)
981 TRC(fprintf(stderr, "Inoring RD parameter: %d\n",
983 /* we silently ignore anything else */
986 cnt->hlen -= (unsigned char) *p + 2;
987 p += (unsigned char) *p + 1;
991 if (read_n(fd, udata, cnt->pending) < cnt->pending)
993 TRC(fprintf(stderr, "Unable to read user data\n"));
998 cnt->state = T_IDLE; /* should we close transport? */
1002 discon->sequence = -1; /* TOFIX */
1003 discon->reason = chead.reason;
1004 TRC(fprintf(stderr, "Diconnect reason %d\n", chead.reason));
1005 if (cnt->pending > discon->udata.maxlen)
1007 t_errno = TBUFOVFLW;
1012 memcpy(discon->udata.buf, udata, cnt->pending);
1013 udata[cnt->pending] = '\0';
1014 TRC(fprintf(stderr, "Discon udata: '%s'\n", udata));
1016 discon->udata.len = cnt->pending;
1022 * fix memory management, you bad Sebastian!
1024 int t_free(char *ptr, int struct_type)
1026 TRC(fprintf(stderr, "T_FREE\n"));
1031 char *t_alloc(int fd, int struct_type, int fields)
1033 char *r = malloc(1024);
1036 TRC(fprintf(stderr, "T_ALLOC\n"));
1042 * this is required for t_listen... if a system doesn't support dup2(), we're
1043 * in trouble: We might not be able to do nonblocking listen. Time will tell.
1045 static int switch_fds(int fd1, int fd2)
1048 struct rfct_control *tmpc;
1050 TRC(fprintf(stderr, "Switching fds %d <--> %d\n", fd1, fd2));
1051 if ((tmp = dup(fd1)) < 0 ||
1052 dup2(fd2, fd1) < 0 ||
1053 dup2(tmp, fd2) < 0 ||
1056 tmpc = control[fd1];
1057 control[fd1] = control[fd2];
1058 control[fd2] = tmpc;
1062 static int rcvconreq(int fd, struct t_call *call)
1064 struct rfct_control *cnt = control[fd];
1065 struct rfct_control *new = control[cnt->tmpfd];
1066 struct tpdu_connect_header chead;
1068 char *p, *addrp = 0;
1069 struct sockaddr_in addr;
1070 int len = sizeof(struct sockaddr_in);
1073 TRC(fprintf(stderr, "RCVCONRES\n"));
1079 for (qslot = 0; qslot < cnt->qlen; qslot++)
1080 if (cnt->oci[qslot] < 0)
1082 if (qslot == cnt->qlen) /* no free slots - shouldn't happen here */
1087 /* read the rest of the CREQ TPDU */
1088 if (read_n(cnt->tmpfd, buf, new->hlen) <= 0)
1090 memcpy(&chead, buf, 5);
1091 if (chead.class != 0)
1093 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
1097 memcpy(new->rref, chead.src_ref, 2); /* we'll echo this back at her */
1099 if (call && call->addr.maxlen)
1100 *(addrp = call->addr.buf) = 0;
1101 for (p = buf + 5; new->hlen > 0;)
1103 switch ((unsigned char)*p)
1105 case TPDU_PARM_TSIZE:
1106 new->tsize = 1 << *(p + 2); /* we go with their max */
1108 case TPDU_PARM_CLDID: break; /* ignore */
1109 case TPDU_PARM_CLGID:
1112 if (*(p + 1) > TSEL_MAXLEN)
1114 TRC(fprintf(stderr, "Called TSEL too long.\n"));
1115 t_errno = TSYSERR; /* Wrong.. ## */
1118 *addrp = *(p + 1); /* length */
1119 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
1120 addrp += *(p + 1); /* move past TSEL */
1123 /* we silently ignore preferred TPDU size and others */
1126 new->hlen -= (unsigned char) *p + 2;
1127 p += (unsigned char) *p + 1;
1132 if (getpeername(cnt->tmpfd, (struct sockaddr*) &addr, &len) < 0)
1134 TRC(perror("getpeername()"));
1138 *(addrp++) = sizeof(struct sockaddr_in);
1139 memcpy(addrp, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
1143 cnt->oci[qslot] = cnt->tmpfd; /* move the new semi-connection to oci */
1144 call->sequence = qslot;
1146 cnt->state = T_INCON;
1151 * This construction is tricky in async mode: listen calls accept, which
1152 * generates a new fd for the association. Call is supposed to return
1153 * immediately if there is no CR on the line (which is highly likely),
1154 * and t-user will then try to use select() on the wrong socket, waiting for
1155 * the transport level package.
1157 * Compared to the upper-level u_* routines, we have one major handicap
1158 * and one major benefit: We *have* to handle two different endpoints
1159 * after t_listen (which includes sockets accept); and we have access
1162 * If the endpoint is configured for nonblocking I/O, and listen is not
1163 * able to immediately acquire the CR, the two fds are switched, so that
1164 * subsequent selects take place on the data socket, rather than the
1167 * At any rate, the data socket is saved, so that it can later be given
1168 * to the user in t_accept().
1170 int t_listen(int fd, struct t_call *call)
1172 struct rfct_control *cnt = control[fd], *new;
1173 struct sockaddr_in addr;
1174 int addrlen = sizeof(struct sockaddr_in);
1175 int tmpfd_is_new = 0; /* we've just accept()ed a connection */
1178 TRC(fprintf(stderr, "T_LISTEN\n"));
1179 if (!cnt || cnt->state != T_IDLE)
1181 TRC(fprintf(stderr, "T_listen expects state==T_IDLE (wrong?)\n"));
1182 t_errno = TOUTSTATE;
1190 for (i = 0; i < cnt->qlen; i++)
1191 if (cnt->oci[i] < 0)
1193 if (i == cnt->qlen) /* no slots in queue */
1195 TRC(fprintf(stderr, "No more space in queue\n"));
1196 t_errno = TBADF; /* what would be more correct? */
1201 TRC(fprintf(stderr, "Accept..."));
1202 if ((cnt->tmpfd = accept(fd, (struct sockaddr*) &addr, &addrlen)) < 0)
1204 if (errno == EWOULDBLOCK)
1207 TRC(fprintf(stderr, "Accept returned WOULDBLOCK\n"));
1211 TRC(fprintf(stderr, "accept failed\n"));
1216 #ifdef NONBLOCKING_OSI
1217 if ((cnt->flags & O_NONBLOCK) && fcntl(cnt->tmpfd, F_SETFL,
1225 TRC(fprintf(stderr, "Accept OK\n"));
1226 if (!(new = control[cnt->tmpfd] = malloc(sizeof(*new))))
1228 TRC(perror("malloc()"));
1234 new->state = T_IDLE;
1237 new->qlen = cnt->qlen;
1238 new->flags = cnt->flags;
1239 new->tmpfd = cnt->tmpfd;
1241 for (i = 0; i < MAX_QLEN; i++)
1244 /* we got a network connection. Now try to read transport CREQ TPDU */
1245 if ((event = t_look_wait(tmpfd_is_new ? cnt->tmpfd : fd, 1)) < 0)
1247 if (t_errno == TNODATA)
1252 * We give the user something to select on for the incoming
1253 * CR. Switch the new association with the listener socket.
1255 TRC(fprintf(stderr, "Switching FDs\n"));
1256 if (switch_fds(cnt->tmpfd, fd) < 0)
1266 t_close(cnt->tmpfd);
1269 return -1; /* t_look & t_read hopefully set up the right errcodes */
1273 /* We got something! */
1274 if (event != T_LISTEN)
1276 TRC(fprintf(stderr, "Expected T_LISTEN\n"));
1281 * switch back the fds, if necessary */
1282 if (!tmpfd_is_new && switch_fds(fd, cnt->tmpfd) < 0)
1287 if (rcvconreq(fd, call) < 0)
1289 t_close(cnt->tmpfd);
1297 * There's no clean mapping of this onto the socket interface. If someone
1298 * wants it, we could fake it by first closing the socket, and then
1299 * opening a new one and doing a dup2. That should be functionally
1302 int t_unbind(int fd)
1305 "T_UNBIND [not supported by transport implementation]\n"));
1306 t_errno = TNOTSUPPORT;
1310 int t_accept(int fd, int resfd, struct t_call *call)
1312 struct rfct_control *listener = control[fd]; /* listener handle */
1313 struct rfct_control *new; /* new, semi-complete association */
1314 struct rfct_control *res; /* resulting association handle */
1315 struct iovec vec[3]; /* RFC1006 header + T-header + parm */
1316 struct rfc_header rfc;
1317 struct tpdu_connect_header tpdu;
1318 unsigned char parm[3 + TSEL_MAXLEN + 2];
1321 TRC(fprintf(stderr, "T_ACCEPT\n"));
1322 if (!listener || listener->state != T_INCON)
1324 TRC(fprintf(stderr, "TOUTSTATE\n"));
1325 t_errno = TOUTSTATE;
1328 /* Get the semi-connection */
1329 if (call->sequence >= listener->qlen || listener->oci[call->sequence] < 0)
1331 TRC(fprintf(stderr, "TBADSEQ\n"));
1335 new = control[(newfd = listener->oci[call->sequence])];
1336 listener->oci[call->sequence] = -1;
1337 res = control[resfd];
1343 if (res != listener) /* move the new connection */
1345 TRC(fprintf(stderr, " Moving to new fd (%d)\n", resfd));
1346 if (res->state != T_IDLE || res->qlen)
1348 TRC(fprintf(stderr, "Trying to move new assc. to bad fd.\n"));
1352 dup2(newfd, resfd); /* closes resfd */
1354 control[resfd] = new;
1355 /* transfer local bindings from res */
1357 memcpy(control[resfd]->ltsel, res->ltsel, res->ltsel_len);
1358 control[resfd]->ltsel_len = res->ltsel_len;
1360 res = control[resfd];
1361 listener->event = 0;
1362 listener->state = T_IDLE;
1364 else /* lose our listener */
1366 TRC(fprintf(stderr, " Moving to listener fd\n"));
1367 for (i = 0; i < listener->qlen; i++)
1368 if (listener->oci[i] >= 0)
1370 TRC(fprintf(stderr, "Still conn indications on listener\n"));
1377 if (listener->ltsel_len)
1378 memcpy(control[resfd]->ltsel, listener->ltsel, listener->ltsel_len);
1379 control[resfd]->ltsel_len = listener->ltsel_len;
1381 res = control[resfd];
1383 rfc.version = RFC_VERSION;
1385 rfc.code = TPDU_CODE_CCON;
1387 memcpy(tpdu.src_ref, "AA", 2);
1388 memcpy(tpdu.dst_ref, res->rref, 2); /* echo back at 'em */
1391 /* grant them their TPDU size */
1392 parm[0] = TPDU_PARM_TSIZE;
1394 for (i = 7; i <= 11 && (1 << i) < res->tsize; i++) ; /* encode TPDU size */
1396 /* give our TSEL. ## Must we echo theirs, if given? check spec */
1397 /* I thought it was ok to give an empty TSEL. Does it have semantic sig? */
1398 parm[3] = TPDU_PARM_CLDID;
1399 parm[4] = res->ltsel_len;
1401 memcpy(parm + 5, res->ltsel, res->ltsel_len);
1403 rfc.len = htons(4 + 7 + 3 + 2 + res->ltsel_len);
1404 rfc.hlen = 6 + 3 + 2 + res->ltsel_len;
1405 vec[0].iov_base = (caddr_t) &rfc;
1407 vec[1].iov_base = (caddr_t) &tpdu;
1409 vec[2].iov_base = (caddr_t) parm;
1410 vec[2].iov_len = 3 + 2 + res->ltsel_len;
1411 if (writev(resfd, vec, 3) < 4 + 7 + 3 + (2 + res->ltsel_len))
1413 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
1417 res->state = T_DATAXFER;
1422 int t_getstate(int fd)
1424 TRC(fprintf(stderr, "T_GETSTATE\n"));
1425 return control[fd] ? control[fd]->state : T_UNINIT;
1430 * indent-tabs-mode: nil
1432 * vim: shiftwidth=4 tabstop=8 expandtab