int udp_client(const char *host, const char *serv, void **vptr, socklen_t *lenp) { int tfd; void *handle; struct netconfig *ncp; struct nd_hostserv hs; struct nd_addrlist *alp; struct netbuf *np; struct t_unitdata *tudptr; handle = Setnetpath(); hs.h_host = (char *) host; hs.h_serv = (char *) serv; while ( (ncp = getnetpath(handle)) != NULL) { if (strcmp(ncp->nc_proto, "udp") != 0) continue; if (netdir_getbyname(ncp, &hs, &alp) != 0) continue; tfd = T_open(ncp->nc_device, O_RDWR, NULL); T_bind(tfd, NULL, NULL); tudptr = T_alloc(tfd, T_UNITDATA, T_ADDR); np = alp->n_addrs; /* use first server address */ tudptr->addr.len = min(tudptr->addr.maxlen, np->len); memcpy(tudptr->addr.buf, np->buf, tudptr->addr.len); endnetpath(handle); netdir_free(alp, ND_ADDRLIST); *vptr = tudptr; /* return pointer to t_unitdata{} */ *lenp = tudptr->addr.maxlen;/* and size of addresses */ return(tfd); } endnetpath(handle); return(-1); }
int udp_server(const char *host, const char *serv, socklen_t *addrlenp) { int tfd; void *handle; struct t_bind tbind; struct t_info tinfo; struct netconfig *ncp; struct nd_hostserv hs; struct nd_addrlist *alp; struct netbuf *np; handle = Setnetconfig(); hs.h_host = (host == NULL) ? HOST_SELF : (char *) host; hs.h_serv = (char *) serv; while ( (ncp = getnetconfig(handle)) != NULL && strcmp(ncp->nc_proto, "udp") != 0) ; if (ncp == NULL) return(-1); if (netdir_getbyname(ncp, &hs, &alp) != 0) return(-2); np = alp->n_addrs; /* use first address */ tfd = T_open(ncp->nc_device, O_RDWR, &tinfo); tbind.addr = *np; /* copy entire netbuf{} */ tbind.qlen = 0; /* not used for connectionless server */ T_bind(tfd, &tbind, NULL); endnetconfig(handle); netdir_free(alp, ND_ADDRLIST); if (addrlenp) *addrlenp = tinfo.addr; /* size of protocol addresses */ return(tfd); }
/* include qlen */ void do_parent(void) { int qlen, j, k, junk, fd[MAXBACKLOG + 1]; struct t_call tcall; Close(cfd); Signal(SIGALRM, parent_alrm); for (qlen = 0; qlen <= 14; qlen++) { printf("qlen = %d: ", qlen); Write(pfd, &qlen, sizeof(int)); /* tell child value */ Read(pfd, &junk, sizeof(int)); /* wait for child */ for (j = 0; j <= MAXBACKLOG; j++) { fd[j] = T_open(XTI_TCP, O_RDWR, NULL); T_bind(fd[j], NULL, NULL); tcall.addr.maxlen = sizeof(serv); tcall.addr.len = sizeof(serv); tcall.addr.buf = &serv; tcall.opt.len = 0; tcall.udata.len = 0; alarm(2); if (t_connect(fd[j], &tcall, NULL) < 0) { if (errno != EINTR) err_xti("t_connect error, j = %d", j); printf("timeout, %d connections completed\n", j-1); for (k = 1; k < j; k++) T_close(fd[k]); break; /* next value of qlen */ } alarm(0); } if (j > MAXBACKLOG) printf("%d connections?\n", MAXBACKLOG); } qlen = -1; /* tell child we're all done */ Write(pfd, &qlen, sizeof(int)); }
int main(int argc, char **argv) { int fd; if (argc != 2) err_quit("usage: checkopts <device>"); fd = T_open(argv[1], O_RDWR, NULL); T_bind(fd, NULL, NULL); req = T_alloc(fd, T_OPTMGMT, T_ALL); ret = T_alloc(fd, T_OPTMGMT, T_ALL); xti_def_uchar_opt(fd, "T_IP_TOS", T_INET_IP, T_IP_TOS); xti_def_uchar_opt(fd, "T_IP_TTL", T_INET_IP, T_IP_TTL); xti_def_uscalar_opt(fd, "T_TCP_MAXSEG", T_INET_TCP, T_TCP_MAXSEG); xti_def_uscalar_opt(fd, "T_TCP_NODELAY", T_INET_TCP, T_TCP_NODELAY); exit(0); }
int main(int argc, char **argv) { int fd; if (argc != 2) err_quit("usage: checkopts <device>"); fd = T_open(argv[1], O_RDWR, NULL); T_bind(fd, NULL, NULL); req = T_alloc(fd, T_OPTMGMT, T_ALL); ret = T_alloc(fd, T_OPTMGMT, T_ALL); xti_set_uchar_opt(fd, "T_IP_TOS", T_INET_IP, T_IP_TOS, SET_TOS(T_ROUTINE, T_LDELAY)); xti_set_uchar_opt(fd, "T_IP_TTL", T_INET_IP, T_IP_TTL, 23); /* xti_def_ulong_opt(fd, "TCP_MAXSEG", INET_TCP, TCP_MAXSEG); */ /* xti_def_ulong_opt(fd, "TCP_NODELAY", INET_TCP, TCP_NODELAY); */ exit(0); }
int main(int argc, char **argv) { int tfd, n, flags; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr; struct t_call tcall; struct t_discon tdiscon; if (argc != 2) err_quit("usage: daytimecli01 <IPaddress>"); tfd = T_open(XTI_TCP, O_RDWR, NULL); T_bind(tfd, NULL, NULL); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(13); /* daytime server */ Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); tcall.addr.maxlen = sizeof(servaddr); tcall.addr.len = sizeof(servaddr); tcall.addr.buf = &servaddr; tcall.opt.len = 0; /* no options with connect */ tcall.udata.len = 0; /* no user data with connect */ if (t_connect(tfd, &tcall, NULL) < 0) { if (t_errno == TLOOK) { if ( (n = T_look(tfd)) == T_DISCONNECT) { tdiscon.udata.maxlen = 0; T_rcvdis(tfd, &tdiscon); errno = tdiscon.reason; err_sys("t_connect error"); } else err_quit("unexpected event after t_connect: %d", n); } else err_xti("t_connect error"); } /* end daytimecli1 */ /* include daytimecli2 */ for ( ; ; ) { if ( (n = t_rcv(tfd, recvline, MAXLINE, &flags)) < 0) { if (t_errno == TLOOK) { if ( (n = T_look(tfd)) == T_ORDREL) { T_rcvrel(tfd); break; } else if (n == T_DISCONNECT) { tdiscon.udata.maxlen = 0; T_rcvdis(tfd, &tdiscon); errno = tdiscon.reason; /* probably ECONNRESET */ err_sys("server terminated prematurely"); } else err_quit("unexpected event after t_rcv: %d", n); } else err_xti("t_rcv error"); } recvline[n] = 0; /* null terminate */ fputs(recvline, stdout); } exit(0); }
int tcp_connect(const char *host, const char *serv) { int tfd, i; void *handle; struct t_call tcall; struct t_discon tdiscon; struct netconfig *ncp; struct nd_hostserv hs; struct nd_addrlist *alp; struct netbuf *np; struct t_opthdr *topt; handle = Setnetpath(); hs.h_host = (char *) host; hs.h_serv = (char *) serv; while ( (ncp = getnetpath(handle)) != NULL) { if (strcmp(ncp->nc_proto, "tcp") != 0) continue; if (netdir_getbyname(ncp, &hs, &alp) != 0) continue; /* try each server address */ for (i = 0, np = alp->n_addrs; i < alp->n_cnt; i++, np++) { tfd = T_open(ncp->nc_device, O_RDWR, NULL); T_bind(tfd, NULL, NULL); tcall.addr.len = np->len; tcall.addr.buf = np->buf; /* pointer copy */ tcall.opt.len = 0; /* no options */ tcall.udata.len = 0; /* no user data with connect */ if (t_connect(tfd, &tcall, NULL) == 0) { endnetpath(handle); /* success, connected to server */ netdir_free(alp, ND_ADDRLIST); req = T_alloc(fd, T_OPTMGMT, T_ALL); ret = T_alloc(fd, T_OPTMGMT, T_ALL); topt = (struct t_opthdr *) req->opt.buf; topt->level = T_INET_TCP; topt->name = T_TCP_MAXSEG; topt->len = sizeof(struct t_opthdr) + sizeof(u_long); req->opt.len = topt->len; topt = OPT_NEXTHDR(req->opt.buf, req->opt.maxlen, topt); topt->level = XTI_GENERIC; topt->name = XTI_SNDBUF; topt->len = sizeof(struct t_opthdr) + sizeof(u_long); req->opt.len += topt->len; return(tfd); } if (t_errno == TLOOK && t_look(tfd) == T_DISCONNECT) { t_rcvdis(tfd, &tdiscon); errno = tdiscon.reason; } t_close(tfd); } netdir_free(alp, ND_ADDRLIST); } endnetpath(handle); return(-1); }