4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-05-31 08:29:35 quinn
10 * Revision 1.4 1995/05/18 13:02:07 quinn
13 * Revision 1.3 1995/05/16 09:37:18 quinn
16 * Revision 1.2 1995/05/02 08:53:09 quinn
17 * Trying in vain to fix comm with ISODE
19 * Revision 1.1 1995/03/30 14:03:17 quinn
20 * Added RFC1006 as separate library
22 * Revision 1.15 1995/03/30 10:54:43 quinn
23 * Fiddling with packet sizes, trying to help ISODE.. :(
25 * Revision 1.14 1995/03/27 08:36:07 quinn
26 * Some work on nonblocking operation in xmosi.c and rfct.c.
27 * Added protocol parameter to cs_create()
29 * Revision 1.13 1995/03/20 11:27:16 quinn
30 * Fixed bug in the _t_rcv stuff
32 * Revision 1.12 1995/03/20 09:54:07 quinn
35 * Revision 1.11 1995/03/20 09:47:16 quinn
36 * Added server-side support to xmosi.c
37 * Fixed possible problems in rfct
40 * Revision 1.10 1995/03/17 19:37:26 quinn
41 * *** empty log message ***
43 * Revision 1.9 1995/03/17 19:28:32 quinn
44 * Working on fixing our mystery-bug.
46 * Revision 1.8 1995/03/14 10:28:38 quinn
47 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
49 * Revision 1.7 1995/03/09 18:42:32 quinn
50 * Fixed another bug in t_rcv
52 * Revision 1.6 1995/03/09 15:22:42 quinn
53 * Fixed two bugs in get/rcv
55 * Revision 1.5 1995/03/07 16:29:46 quinn
58 * Revision 1.4 1995/03/06 16:48:03 quinn
61 * Revision 1.3 1995/03/06 10:54:32 quinn
62 * Server-side functions (t_bind/t_listen/t_accept) seem to work ok, now.
63 * Nonblocking mode needs work (and testing!)
64 * Added makensap to replace function in mosiutil.c.
66 * Revision 1.2 1995/03/03 09:40:36 quinn
67 * Added most of the remaining functionality.
68 * Still need exstensive testing of server-side functions and nonblocking
71 * Revision 1.1 1995/03/01 08:40:33 quinn
72 * First working version of rfct. Addressing needs work.
77 * Simple implementation of XTI/TP0/RFC1006/Sockets.
78 * Note: This is neither complete nor robust. It just has to tick us over
79 * until mr. Furniss finishes his own implementation.
81 * TODO: Asynchronous mode needs a lot of little adjustments to various
82 * return values and error codes, etc.
84 * Check if addressing info is returned correctly by all calls.
89 #include <sys/types.h>
90 #include <sys/param.h>
91 #include <sys/socket.h>
93 #include <netinet/in.h>
94 #include <arpa/inet.h>
106 #include <linux/limits.h>
108 #include <dmalloc.h> /* project memory debugging - delete if you don't have it */
110 #ifdef TRACE_TRANSPORT
116 #define TSEL_MAXLEN 20 /* is there a standard for this? */
117 #define RFC_DEFAULT_PORT 4500
118 #define MAX_QLEN 5 /* outstanding connect indications */
120 static int t_look_wait(int fd, int wait);
121 int t_rcvconnect(int fd, struct t_call *call);
126 static struct rfct_control
128 int state; /* state of transport endpoint */
129 int event; /* current event */
130 int tsize; /* max TPDU size */
131 int curcode; /* code of current TPDU */
132 int pending; /* # bytes of user data waiting on line */
133 int eot_flag; /* current incoming TPDU had EOT set */
134 int hlen; /* TPDU header suffix remaining on line */
135 int eot; /* current incoming DATA TPDU is last in TSDU */
136 int togo; /* # bytes waiting to go on current outgoing DATA */
137 int qlen; /* set by t_bind */
138 int listen; /* we are listening */
139 int tmpfd; /* holding space for client between listen and accept */
140 int flags; /* set by t_open */
141 char rref[2]; /* remote reference */
142 char ltsel[TSEL_MAXLEN]; /* local transport selector */
144 int oci[MAX_QLEN]; /* outstanding connect indications */
148 * In the best tradition of ThinOSI, we combine the RFC1006 and the common
149 * part of the TPDU header into one. Given the complexity of the transport
150 * layer, maybe it would have been nice to have it in a separate module
151 * for reuse - but such is hindsight.
152 * Using a bit-field like this may be dangerous. I would expect it to work
153 * fine on any 32-bit or 64-bit machine, with any decent compiler. A DOS
154 * compiler might make a mess of it, I suppose, but I wouldn't even expect
161 #define RFC_VERSION 3
163 unsigned len:16; /* length of entire package, including header */
164 /* TPDU common fields */
165 unsigned char hlen; /* length of TPDU-header minus length-octet */
166 unsigned char code; /* TPDU code (and an unused 4-bit field) */
167 char suffix[100]; /* unstructured TPDU elements, for convenience */
171 * The TPDU-codes used by this package.
173 #define TPDU_CODE_CREQ 0xe0
174 #define TPDU_CODE_CCON 0xd0
175 #define TPDU_CODE_DATA 0xf0
176 #define TPDU_CODE_DREQ 0x80
179 * Parameters that we care about.
181 #define TPDU_PARM_TSIZE 0xc0 /* max TPDU size */
182 #define TPDU_PARM_CLGID 0xc1 /* Calling TSAP-ID */
183 #define TPDU_PARM_CLDID 0xc2 /* Called TSAP-ID */
185 struct tpdu_connect_header /* length of fixed suffix: 5 octets */
187 char dst_ref[2]; /* 3-4 - not used by TP0 */
188 char src_ref[2]; /* 5-6 - not used by TP0 */
189 char class; /* 7 - always 0 */
192 struct tpdu_disconnect_header /* length of fixed suffix: 5 octets */
199 struct tpdu_data_header /* length of fixed suffix: 1 octet */
201 unsigned char nr; /* we only use bit 7 (1 << 7), to mark EOT */
202 #define DATA_EOT (1<<7)
205 static int rfc_running = 0; /* Have we been initialized? */
207 static void init_rfc(void)
211 for (i = 0; i < NOFILE; i++)
216 int t_open(char *name, int oflag, struct t_info *info)
218 struct rfct_control *cnt;
219 struct protoent *proto;
222 TRC(fprintf(stderr, "T_OPEN\n"));
227 if (!(proto = getprotobyname("tcp")))
229 if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
231 #ifdef NONBLOCKING_OSI
232 if ((oflag & O_NONBLOCK) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
238 if (!(cnt = control[s] = malloc(sizeof(struct rfct_control))))
240 TRC(perror("malloc()"));
247 cnt->state = T_UNBND;
254 for (i = 0; i < MAX_QLEN; i++)
258 * RFC1006 sets a higher than standard (TP) default max TPDU size, but the
259 * Isode seems to like to negotiate it down. We'll keep it here to be
260 * safe. Note that there's no harm in jumping it up. If it's higher
261 * than 2048, t_connect won't try to negotiate.
267 info->addr = TSEL_MAXLEN + sizeof(struct sockaddr_in) + 1;
268 info->options = 1024;
269 info->tsdu = -1; /* is this right? */
273 info->servtype = T_COTS_ORD; /* lets hope our user doesn't
274 try something funny. */
279 int t_connect(int fd, struct t_call *sndcall, struct t_call *rcvcall)
281 struct rfct_control *cnt = control[fd];
282 struct sockaddr_in addr;
284 struct iovec vec[3]; /* RFC1006 header + T-header + parms */
285 struct rfc_header rfc;
286 struct tpdu_connect_header tpdu;
287 unsigned char pbuf[2 + TSEL_MAXLEN + 3]; /* CR parameters */
290 TRC(fprintf(stderr, "T_CONNECT\n"));
291 if (!cnt || cnt->state != T_IDLE)
293 TRC(fprintf(stderr, "TOUTSTATE\n"));
297 /* take the address apart */
298 p = sndcall->addr.buf;
299 if (*p) /* transport selector */
301 TRC(fprintf(stderr, "Tsel length is %d.\n", *p));
302 pbuf[0] = TPDU_PARM_CLDID;
304 memcpy(pbuf + 2, p + 1, *p);
307 p += *p + 1; /* skip tsel */
308 if (*p != sizeof(addr))
310 TRC(fprintf(stderr, "Expected sockaddr here.\n"));
315 memcpy(&addr, p, sizeof(addr));
316 if (connect(fd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
323 * If the default set by t_open() is higher than 2048, we don't try
324 * to negotiate, hoping that the opponent goes by the high default
325 * set by RFC1006. Otherwise, we just go for 2048 (max according to
326 * transport standard). The rest of this module doesn't really care
327 * about the size (although it's respected, of course), since we do
328 * no internal buffering.
330 if (cnt->tsize <= 2048)
332 pbuf[plen++] = TPDU_PARM_TSIZE;
334 pbuf[plen++] = 0x0b; /* request max PDU size (2048 octets) */
337 rfc.version = RFC_VERSION;
339 rfc.len = htons(4 + 7 + plen);
341 rfc.code = TPDU_CODE_CREQ;
343 memset(tpdu.dst_ref, 0, 2);
344 memset(tpdu.src_ref, 0, 2);
347 vec[0].iov_base = (caddr_t) &rfc;
349 vec[1].iov_base = (caddr_t) &tpdu;
351 vec[2].iov_base = (caddr_t) pbuf;
352 vec[2].iov_len = plen;
355 * we don't expect flow-control problems on the first outgoing packet,
356 * though I suppose it'd be possible on some weird types of link.
358 if (writev(fd, vec, 3) < 4 + 7 + plen)
360 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
364 cnt->state = T_OUTCON;
366 return t_rcvconnect(fd, rcvcall);
370 * This needs work for asynchronous mode.
372 static int read_n(int fd, char *buf, int toget)
374 struct rfct_control *cnt = control[fd];
379 if ((res = read(fd, buf, toget - got)) < 0)
385 TRC(fprintf(stderr, "Error on read.\n"));
390 if (!res) /* peer closed network connection */
393 cnt->event = T_DISCONNECT; /* is this correct ? ## */
394 cnt->hlen = cnt->pending = 0;
395 cnt->state = T_IDLE; /* this can't be correct ## */
400 while ((got += res) < toget);
404 int t_rcvconnect(int fd, struct t_call *call)
406 struct rfct_control *cnt = control[fd];
407 struct tpdu_connect_header chead;
408 char buf[100], *p, *addrp;
409 struct sockaddr_in peer;
410 int len = sizeof(struct sockaddr_in);
412 TRC(fprintf(stderr, "T_RCVCONNECT\n"));
413 if (!cnt || cnt->state != T_OUTCON)
415 TRC(fprintf(stderr, "TOUTSTATE\n"));
420 if (t_look_wait(fd, 1) <= 0)
422 if (cnt->event != T_CONNECT)
427 /* read the rest of the CC TPDU */
428 if (read_n(fd, buf, cnt->hlen) <= 0)
430 memcpy(&chead, buf, 5);
431 if (chead.class != 0)
433 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
438 *(addrp = call->addr.buf) = 0;
440 for (p = buf + 5; cnt->hlen > 0;)
442 switch ((unsigned char)*p)
444 case TPDU_PARM_TSIZE:
445 cnt->tsize = 1 << *(p + 2);
447 case TPDU_PARM_CLDID:
450 if (*(p + 1) > TSEL_MAXLEN)
452 TRC(fprintf(stderr, "Called TSEL too long.\n"));
453 t_errno = TSYSERR; /* Wrong.. ## */
456 *addrp = *(p + 1); /* length */
457 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
458 addrp += *(p + 1); /* move past TSEL */
461 case TPDU_PARM_CLGID: break; /* ignoring this for now */
463 TRC(fprintf(stderr, "Inoring CR parameter: %d\n",
465 /* we silently ignore anything else */
468 cnt->hlen -= (unsigned char) *p + 2;
469 p += (unsigned char) *p + 1;
471 addrp++; /* skip to end of addr + 1 */
474 if (getpeername(fd, (struct sockaddr*) &peer, &len) < 0)
476 TRC(perror("getpeername()"));
480 *(addrp++) = sizeof(struct sockaddr_in);
481 memcpy(addrp, &peer, sizeof(struct sockaddr_in));
482 addrp += sizeof(struct sockaddr_in);
483 call->addr.len = addrp - call->addr.buf + 1;
485 cnt->state = T_DATAXFER;
490 int t_snd(int fd, char *buf, unsigned nbytes, int flags)
492 struct rfct_control *cnt = control[fd];
493 struct rfc_header rfc;
494 struct iovec vec[2]; /* RFC1006 header + T-header + (user data) */
495 int writ = 0, res, towrite, eot;
498 TRC(fprintf(stderr, "T_SND [%d bytes, flags %d]\n", nbytes, flags));
499 if (!cnt || cnt->state != T_DATAXFER)
501 TRC(fprintf(stderr, "Trying to write in the wrong state on fd %d.\n",
511 do /* write the TSDU (segment) in chunks depending on the TPDU max size */
513 if (cnt->togo > 0) /* we have a partial TPDU on the wire */
515 TRC(fprintf(stderr, " writing continuation block (%d)\n",
517 if ((res = write(fd, buf, cnt->togo)) < 0)
529 TRC(fprintf(stderr, " wrote %d, total %d\n", res, writ));
531 else /* prepare and send (possibly partial) header */
533 towrite = nbytes - writ;
534 if (towrite + 3 + 4 > cnt->tsize)
535 towrite = cnt->tsize - (3 + 4); /* space for DATA header */
536 rfc.version = RFC_VERSION;
538 rfc.len = htons(towrite + 4 + 3); /* RFC1006 length */
540 rfc.code = TPDU_CODE_DATA;
541 if (flags & T_MORE || towrite + writ < nbytes)
545 rfc.suffix[0] = eot << 7; /* DATA EOT marker */
547 head_offset = 7 + cnt->togo;
550 vec[0].iov_base = (caddr_t) (char*)&rfc + head_offset;
551 vec[0].iov_len = 7 - head_offset;
552 vec[1].iov_base = (caddr_t) buf + writ;
553 vec[1].iov_len = towrite;
554 TRC(fprintf(stderr, " sending beg of block (%d+%d)\n",
555 7 - head_offset, towrite));
556 if ((res = writev(fd, vec, 2)) < 0)
558 TRC(fprintf(stderr, " write returned -1\n"));
559 /* thwarted by flow control */
574 /* somewhat thwarted */
575 else if (res < towrite + 7 - head_offset)
578 * Write came up short. We assume that this is a flow-
579 * control thing, and return immediately. Maybe it'd
580 * be better to take another loop, and generate an
581 * actual EAGAIN from write?
583 TRC(fprintf(stderr, " write returned %d\n", res));
584 if (res < 7 - head_offset) /* we didn't send a full header */
586 cnt->togo = -(7 - head_offset - res);
590 else if ((res -= 7 - head_offset) < towrite) /* not all data */
592 cnt->togo = towrite - res;
593 return nbytes - (writ + res);
596 else /* whew... nonblocking I/O is hard work */
599 writ += res - (7 - head_offset);
603 while (writ < nbytes);
604 TRC(fprintf(stderr, " finishing with %d written\n", nbytes));
608 int _t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
610 struct rfct_control *cnt = control[fd];
611 struct tpdu_data_header dhead;
616 TRC(fprintf(stderr, "T_RCV [nbytes=%d, flags=0x%x]\n", nbytes, *flags));
617 if (!cnt || cnt->state != T_DATAXFER)
623 do /* loop until we have a full TSDU or an error */
626 if (t_look_wait(fd, 0) <= 0)
628 if (cnt->event != T_DATA)
633 if (cnt->hlen) /* beginning of block */
635 TRC(fprintf(stderr, " Beginning of TPDU\n"));
638 TRC(fprintf(stderr, "We can't handle parameters to DATA\n"));
642 toget = cnt->pending;
643 if (toget > nbytes - got)
644 toget = nbytes - got;
645 TRC(fprintf(stderr, " toget=%d\n", toget));
646 vec[0].iov_base = (caddr_t) &dhead;
648 vec[1].iov_base = (caddr_t) buf + got;
649 vec[1].iov_len = toget;
650 if ((res = readv(fd, vec, 2)) < 0)
652 TRC(perror("readv()"));
655 if (got) /* we got some data in previous cycle */
657 t_errno = TNODATA; /* no data */
663 TRC(fprintf(stderr, " readv() returned %d\n", res));
667 cnt->event = T_DISCONNECT;
671 cnt->eot_flag = (dhead.nr & DATA_EOT) >> 7;
674 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
677 else /* continuation */
679 TRC(fprintf(stderr, " Reading middle of TPDU\n"));
680 toget = cnt->pending;
681 if (toget > nbytes - got)
682 toget = nbytes - got;
683 TRC(fprintf(stderr, " toget=%d\n", toget));
684 if ((res = read(fd, buf + got, toget)) < 0)
686 TRC(perror("read()"));
689 if (got) /* we got some data in previous cycle */
691 t_errno = TNODATA; /* no data */
697 TRC(fprintf(stderr, " read() returned %d\n", res));
701 cnt->event = T_DISCONNECT;
706 TRC(fprintf(stderr, " Got total of %d octets, %d pending\n",
709 TRC(fprintf(stderr, " bottom of loop: pending=%d, got=%d\n",
712 while (cnt->pending && got < nbytes);
713 *flags = cnt->pending || !cnt->eot_flag ? T_MORE : 0;
714 TRC(fprintf(stderr, " flags=0x%x\n", *flags));
717 TRC(fprintf(stderr, " Return value: %d\n", got));
718 memset(buf + got, 0, 10);
722 int t_rcv(int fd, char *buf, unsigned nbytes, int *flags)
729 if ((res = _t_rcv(fd, buf, nbytes, flags)) <= 0)
735 while (*flags & T_MORE && nbytes > 0);
741 struct rfct_control *cnt = control[fd];
743 TRC(fprintf(stderr, "T_CLOSE\n"));
744 if (!cnt || cnt->state == T_UNINIT)
746 TRC(fprintf(stderr, "Trying to close a bad fd.\n"));
755 * This isn't right, obviously.
757 int t_error(char *errmsg)
759 TRC(fprintf(stderr, "T_ERROR\n"));
760 fprintf(stderr, "t_error(t_errno=%d):", t_errno);
766 * Put a select in here!!
768 static int t_look_wait(int fd, int wait)
770 struct rfct_control *cnt = control[fd];
771 struct rfc_header head;
774 TRC(fprintf(stderr, "T_LOOK\n"));
775 if (!cnt || cnt->state == T_UNINIT)
782 if (cnt->state == T_IDLE && cnt->tmpfd < 0)
783 return T_LISTEN; /* the only possible type of event */
784 if ((res = read_n(fd, (char*) &head, 6)) < 0)
788 TRC(fprintf(stderr, "Network disconnect\n"));
789 return cnt->event = T_DISCONNECT;
791 TRC(fprintf(stderr, "t_look got %d bytes\n", res));
792 if (head.version != RFC_VERSION)
794 TRC(fprintf(stderr, "Got bad RFC1006 version in t_look: %d.\n",
796 t_errno = TSYSERR; /* should signal protocol error, somehow ## */
799 cnt->curcode = head.code;
800 cnt->hlen = head.hlen - 1; /* length of header suffix */
801 cnt->pending = ntohs(head.len) - 6 - cnt->hlen;
802 TRC(fprintf(stderr, "t_look: len=%d, code=0x%2.2x, hlen=%d.\n",
803 cnt->pending + 6, cnt->curcode, cnt->hlen));
804 switch (cnt->curcode)
806 case TPDU_CODE_CREQ: cnt->event = T_LISTEN; break;
807 case TPDU_CODE_CCON: cnt->event = T_CONNECT; break;
808 case TPDU_CODE_DATA: cnt->event = T_DATA; break;
809 case TPDU_CODE_DREQ: cnt->event = T_DISCONNECT; break;
811 TRC(fprintf(stderr, "t_look: Bad package: 0x%2.2x.\n", cnt->curcode));
812 t_errno = TSYSERR; /* protocol error */
820 return t_look_wait(fd, 0);
824 * If the user doesn't provide a NSAP, and qlen > 0, we'll do a default
825 * server bind. If qlen==0, we won't bind at all - connect will do that
828 int t_bind(int fd, struct t_bind *req, struct t_bind *ret)
830 struct rfct_control *cnt = control[fd];
831 struct sockaddr_in addr;
835 TRC(fprintf(stderr, "T_BIND\n"));
836 if (!cnt || cnt->state != T_UNBND)
838 TRC(fprintf(stderr, "Bad state\n"));
845 cnt->qlen = req->qlen < MAX_QLEN ? req->qlen : MAX_QLEN;
849 if (*p > TSEL_MAXLEN)
851 TRC(fprintf(stderr, "Tsel too large.\n"));
857 memcpy(cnt->ltsel, p + 1, cnt->ltsel_len);
858 p += cnt->ltsel_len + 1;
859 if (*p < sizeof(addr))
861 TRC(fprintf(stderr, "W: No NSAP provided for local bind\n"));
865 memcpy(&addr, p + 1, sizeof(addr));
872 if (!got_addr) /* user didn't give an address - local bind */
874 addr.sin_family = AF_INET;
875 addr.sin_addr.s_addr = INADDR_ANY;
877 addr.sin_port = htons(RFC_DEFAULT_PORT);
878 else /* we'll leave binding to connect - just set dummy */
879 addr.sin_port = 0; /* dummy for ret */
881 if (cnt->qlen && bind(fd, (struct sockaddr *) &addr,
884 TRC(perror("bind()"));
885 t_errno = TSYSERR; /* this should be refined */
890 if (listen(fd, cnt->qlen) < 0)
896 TRC(fprintf(stderr, " listen OK\n"));
899 /* All right! Now let's give something back, if our user wants it */
902 ret->qlen = cnt->qlen;
903 if (ret->addr.maxlen < (ret->addr.len = cnt->ltsel_len + 2 +
906 /* No space - but we're still bound */
912 *(p++) = cnt->ltsel_len;
914 memcpy(p, cnt->ltsel, cnt->ltsel_len);
916 *(p++) = sizeof(addr);
917 memcpy(p, &addr, sizeof(addr));
923 * need to consult RFC1006 on these. I think they just map to close()...
925 int t_snddis(int fd, struct t_call *call)
927 TRC(fprintf(stderr, "T_SNDDIS\n"));
931 int t_rcvdis(int fd, struct t_discon *discon)
933 struct rfct_control *cnt = control[fd];
934 struct tpdu_disconnect_header chead;
935 char udata[64], buf[256], *p;
937 TRC(fprintf(stderr, "T_RCVDIS\n"));
940 TRC(fprintf(stderr, "TOUTSTATE\n"));
945 if (t_look_wait(fd, 1) <= 0)
947 if (cnt->event != T_DISCONNECT)
952 /* read the rest of the DR TPDU */
953 if (read_n(fd, buf, cnt->hlen) <= 0)
955 memcpy(&chead, buf, 5);
957 for (p = buf + 5; cnt->hlen > 0;)
959 switch ((unsigned char)*p)
962 TRC(fprintf(stderr, "Inoring RD parameter: %d\n",
964 /* we silently ignore anything else */
967 cnt->hlen -= (unsigned char) *p + 2;
968 p += (unsigned char) *p + 1;
972 if (read_n(fd, udata, cnt->pending) < cnt->pending)
974 TRC(fprintf(stderr, "Unable to read user data\n"));
979 cnt->state = T_IDLE; /* should we close transport? */
983 discon->sequence = -1; /* TOFIX */
984 discon->reason = chead.reason;
985 TRC(fprintf(stderr, "Diconnect reason %d\n", chead.reason));
986 if (cnt->pending > discon->udata.maxlen)
993 memcpy(discon->udata.buf, udata, cnt->pending);
994 udata[cnt->pending] = '\0';
995 TRC(fprintf(stderr, "Discon udata: '%s'\n", udata));
997 discon->udata.len = cnt->pending;
1003 * fix memory management, you bad Sebastian!
1005 int t_free(char *ptr, int struct_type)
1007 TRC(fprintf(stderr, "T_FREE\n"));
1012 char *t_alloc(int fd, int struct_type, int fields)
1014 char *r = malloc(1024);
1017 TRC(fprintf(stderr, "T_ALLOC\n"));
1023 * this is required for t_listen... if a system doesn't support dup2(), we're
1024 * in trouble: We might not be able to do nonblocking listen. Time will tell.
1026 static int switch_fds(int fd1, int fd2)
1029 struct rfct_control *tmpc;
1031 TRC(fprintf(stderr, "Switching fds %d <--> %d\n", fd1, fd2));
1032 if ((tmp = dup(fd1)) < 0 ||
1033 dup2(fd2, fd1) < 0 ||
1034 dup2(tmp, fd2) < 0 ||
1037 tmpc = control[fd1];
1038 control[fd1] = control[fd2];
1039 control[fd2] = tmpc;
1043 static int rcvconreq(int fd, struct t_call *call)
1045 struct rfct_control *cnt = control[fd];
1046 struct rfct_control *new = control[cnt->tmpfd];
1047 struct tpdu_connect_header chead;
1049 char *p, *addrp = 0;
1050 struct sockaddr_in addr;
1051 int len = sizeof(struct sockaddr_in);
1054 TRC(fprintf(stderr, "RCVCONRES\n"));
1060 for (qslot = 0; qslot < cnt->qlen; qslot++)
1061 if (cnt->oci[qslot] < 0)
1063 if (qslot == cnt->qlen) /* no free slots - shouldn't happen here */
1068 /* read the rest of the CREQ TPDU */
1069 if (read_n(cnt->tmpfd, buf, new->hlen) <= 0)
1071 memcpy(&chead, buf, 5);
1072 if (chead.class != 0)
1074 TRC(fprintf(stderr, "Expected TP0, got %d\n", (int)chead.class));
1078 memcpy(new->rref, chead.src_ref, 2); /* we'll echo this back at her */
1080 if (call && call->addr.maxlen)
1081 *(addrp = call->addr.buf) = 0;
1082 for (p = buf + 5; new->hlen > 0;)
1084 switch ((unsigned char)*p)
1086 case TPDU_PARM_TSIZE:
1087 new->tsize = 1 << *(p + 2); /* we go with their max */
1089 case TPDU_PARM_CLDID: break; /* ignore */
1090 case TPDU_PARM_CLGID:
1093 if (*(p + 1) > TSEL_MAXLEN)
1095 TRC(fprintf(stderr, "Called TSEL too long.\n"));
1096 t_errno = TSYSERR; /* Wrong.. ## */
1099 *addrp = *(p + 1); /* length */
1100 memcpy(addrp + 1, p + 2, *(p + 1)); /* remote TSEL */
1101 addrp += *(p + 1); /* move past TSEL */
1104 /* we silently ignore preferred TPDU size and others */
1107 new->hlen -= (unsigned char) *p + 2;
1108 p += (unsigned char) *p + 1;
1113 if (getpeername(cnt->tmpfd, (struct sockaddr*) &addr, &len) < 0)
1115 TRC(perror("getpeername()"));
1119 *(addrp++) = sizeof(struct sockaddr_in);
1120 memcpy(addrp, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
1124 cnt->oci[qslot] = cnt->tmpfd; /* move the new semi-connection to oci */
1125 call->sequence = qslot;
1127 cnt->state = T_INCON;
1132 * This construction is tricky in async mode: listen calls accept, which
1133 * generates a new fd for the association. Call is supposed to return
1134 * immediately if there is no CR on the line (which is highly likely),
1135 * and t-user will then try to use select() on the wrong socket, waiting for
1136 * the transport level package.
1138 * Compared to the upper-level u_* routines, we have one major handicap
1139 * and one major benefit: We *have* to handle two different endpoints
1140 * after t_listen (which includes sockets accept); and we have access
1143 * If the endpoint is configured for nonblocking I/O, and listen is not
1144 * able to immediately acquire the CR, the two fds are switched, so that
1145 * subsequent selects take place on the data socket, rather than the
1148 * At any rate, the data socket is saved, so that it can later be given
1149 * to the user in t_accept().
1151 int t_listen(int fd, struct t_call *call)
1153 struct rfct_control *cnt = control[fd], *new;
1154 struct sockaddr_in addr;
1155 int addrlen = sizeof(struct sockaddr_in);
1156 int tmpfd_is_new = 0; /* we've just accept()ed a connection */
1159 TRC(fprintf(stderr, "T_LISTEN\n"));
1160 if (!cnt || cnt->state != T_IDLE)
1162 TRC(fprintf(stderr, "T_listen expects state==T_IDLE (wrong?)\n"));
1163 t_errno = TOUTSTATE;
1171 for (i = 0; i < cnt->qlen; i++)
1172 if (cnt->oci[i] < 0)
1174 if (i == cnt->qlen) /* no slots in queue */
1176 TRC(fprintf(stderr, "No more space in queue\n"));
1177 t_errno = TBADF; /* what would be more correct? */
1182 TRC(fprintf(stderr, "Accept..."));
1183 if ((cnt->tmpfd = accept(fd, (struct sockaddr*) &addr, &addrlen)) < 0)
1185 if (errno == EWOULDBLOCK)
1188 TRC(fprintf(stderr, "Accept returned WOULDBLOCK\n"));
1192 TRC(fprintf(stderr, "accept failed\n"));
1197 #ifdef NONBLOCKING_OSI
1198 if ((cnt->flags & O_NONBLOCK) && fcntl(cnt->tmpfd, F_SETFL,
1206 TRC(fprintf(stderr, "Accept OK\n"));
1207 if (!(new = control[cnt->tmpfd] = malloc(sizeof(*new))))
1209 TRC(perror("malloc()"));
1215 new->state = T_IDLE;
1218 new->qlen = cnt->qlen;
1219 new->flags = cnt->flags;
1220 new->tmpfd = cnt->tmpfd;
1222 for (i = 0; i < MAX_QLEN; i++)
1225 /* we got a network connection. Now try to read transport CREQ TPDU */
1226 if ((event = t_look_wait(tmpfd_is_new ? cnt->tmpfd : fd, 1)) < 0)
1228 if (t_errno == TNODATA)
1233 * We give the user something to select on for the incoming
1234 * CR. Switch the new association with the listener socket.
1236 TRC(fprintf(stderr, "Switching FDs\n"));
1237 if (switch_fds(cnt->tmpfd, fd) < 0)
1247 t_close(cnt->tmpfd);
1250 return -1; /* t_look & t_read hopefully set up the right errcodes */
1254 /* We got something! */
1255 if (event != T_LISTEN)
1257 TRC(fprintf(stderr, "Expected T_LISTEN\n"));
1262 * switch back the fds, if necessary */
1263 if (!tmpfd_is_new && switch_fds(fd, cnt->tmpfd) < 0)
1268 if (rcvconreq(fd, call) < 0)
1270 t_close(cnt->tmpfd);
1278 * There's no clean mapping of this onto the socket interface. If someone
1279 * wants it, we could fake it by first closing the socket, and then
1280 * opening a new one and doing a dup2. That should be functionally
1283 int t_unbind(int fd)
1285 fprintf(stderr, "T_UNBIND [not supported by transport implementation]\n");
1286 t_errno = TNOTSUPPORT;
1290 int t_accept(int fd, int resfd, struct t_call *call)
1292 struct rfct_control *listener = control[fd]; /* listener handle */
1293 struct rfct_control *new; /* new, semi-complete association */
1294 struct rfct_control *res; /* resulting association handle */
1295 struct iovec vec[3]; /* RFC1006 header + T-header + parm */
1296 struct rfc_header rfc;
1297 struct tpdu_connect_header tpdu;
1298 unsigned char parm[3 + TSEL_MAXLEN + 2];
1301 TRC(fprintf(stderr, "T_ACCEPT\n"));
1302 if (!listener || listener->state != T_INCON)
1304 TRC(fprintf(stderr, "TOUTSTATE\n"));
1305 t_errno = TOUTSTATE;
1308 /* Get the semi-connection */
1309 if (call->sequence >= listener->qlen || listener->oci[call->sequence] < 0)
1311 TRC(fprintf(stderr, "TBADSEQ\n"));
1315 new = control[(newfd = listener->oci[call->sequence])];
1316 listener->oci[call->sequence] = -1;
1317 res = control[resfd];
1323 if (res != listener) /* move the new connection */
1325 TRC(fprintf(stderr, " Moving to new fd (%d)\n", resfd));
1326 if (res->state != T_IDLE || res->qlen)
1328 TRC(fprintf(stderr, "Trying to move new assc. to bad fd.\n"));
1332 dup2(newfd, resfd); /* closes resfd */
1334 control[resfd] = new;
1335 /* transfer local bindings from res */
1337 memcpy(control[resfd]->ltsel, res->ltsel, res->ltsel_len);
1338 control[resfd]->ltsel_len = res->ltsel_len;
1340 res = control[resfd];
1341 listener->event = 0;
1342 listener->state = T_IDLE;
1344 else /* lose our listener */
1346 TRC(fprintf(stderr, " Moving to listener fd\n"));
1347 for (i = 0; i < listener->qlen; i++)
1348 if (listener->oci[i] >= 0)
1350 TRC(fprintf(stderr, "Still conn indications on listener\n"));
1357 if (listener->ltsel_len)
1358 memcpy(control[resfd]->ltsel, listener->ltsel, listener->ltsel_len);
1359 control[resfd]->ltsel_len = listener->ltsel_len;
1361 res = control[resfd];
1363 rfc.version = RFC_VERSION;
1365 rfc.code = TPDU_CODE_CCON;
1367 memcpy(tpdu.src_ref, "AA", 2);
1368 memcpy(tpdu.dst_ref, res->rref, 2); /* echo back at 'em */
1371 /* grant them their TPDU size */
1372 parm[0] = TPDU_PARM_TSIZE;
1374 for (i = 7; i <= 11 && (1 << i) < res->tsize; i++) ; /* encode TPDU size */
1376 /* give our TSEL. ## Must we echo theirs, if given? check spec */
1377 /* I thought it was ok to give an empty TSEL. Does it have semantic sig? */
1378 parm[3] = TPDU_PARM_CLDID;
1379 parm[4] = res->ltsel_len;
1381 memcpy(parm + 5, res->ltsel, res->ltsel_len);
1383 rfc.len = htons(4 + 7 + 3 + 2 + res->ltsel_len);
1384 rfc.hlen = 6 + 3 + 2 + res->ltsel_len;
1385 vec[0].iov_base = (caddr_t) &rfc;
1387 vec[1].iov_base = (caddr_t) &tpdu;
1389 vec[2].iov_base = (caddr_t) parm;
1390 vec[2].iov_len = 3 + 2 + res->ltsel_len;
1391 if (writev(resfd, vec, 3) < 4 + 7 + 3 + (2 + res->ltsel_len))
1393 TRC(fprintf(stderr, "writev came up short. Aborting connect\n"));
1397 res->state = T_DATAXFER;
1402 int t_getstate(int fd)
1404 TRC(fprintf(stderr, "T_GETSTATE\n"));
1405 return control[fd] ? control[fd]->state : T_UNINIT;