|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1988, 1990 Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution is only permitted until one year after the first shipment ! 6: * of 4.4BSD by the Regents. Otherwise, redistribution and use in source and ! 7: * binary forms are permitted provided that: (1) source distributions retain ! 8: * this entire copyright notice and comment, and (2) distributions including ! 9: * binaries display the following acknowledgement: This product includes ! 10: * software developed by the University of California, Berkeley and its ! 11: * contributors'' in the documentation or other materials provided with the ! 12: * distribution and in all advertising materials mentioning features or use ! 13: * of this software. Neither the name of the University nor the names of ! 14: * its contributors may be used to endorse or promote products derived from ! 15: * this software without specific prior written permission. ! 16: * THIS SOFTWARE IS PROVIDED AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED ! 17: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF ! 18: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 19: */ ! 20: #ifndef lint ! 21: static char sccsid[] = "@(#)tisink.c 7.3 (Berkeley) 6/29/90"; ! 22: #endif /* not lint */ ! 23: ! 24: /* ! 25: * This is a test program to be a sink for TP4 connections. ! 26: */ ! 27: #include <sys/param.h> ! 28: #include <sys/uio.h> ! 29: #include <sys/socket.h> ! 30: #include <sys/ioctl.h> ! 31: #include <net/route.h> ! 32: #include <net/if.h> ! 33: #define TCPT_NTIMERS 4 ! 34: #include <netiso/iso.h> ! 35: #include <netiso/tp_param.h> ! 36: #include <netiso/tp_user.h> ! 37: ! 38: #include <stdio.h> ! 39: #include <errno.h> ! 40: #include <ctype.h> ! 41: #include <netdb.h> ! 42: ! 43: ! 44: #define dbprintf if(verbose)printf ! 45: #define try(a,b,c) {x = (a b); dbprintf("%s%s returns %d\n",c,"a",x);\ ! 46: if(x<0) {perror("a"); myexit(0);}} ! 47: ! 48: ! 49: struct ifreq ifr; ! 50: short port = 3000; ! 51: struct sockaddr_iso faddr, laddr = { sizeof(laddr), AF_ISO }; ! 52: struct sockaddr_iso *siso = &laddr; ! 53: ! 54: long size, count = 10, forkp, confp, echop, mynamep, verbose = 1, playtag = 0; ! 55: long records, intercept = 0; ! 56: ! 57: char buf[2048]; ! 58: char your_it[] = "You're it!"; ! 59: ! 60: char *Servername; ! 61: ! 62: main(argc, argv) ! 63: int argc; ! 64: char *argv[]; ! 65: { ! 66: register char **av = argv; ! 67: register char *cp; ! 68: struct iso_addr iso_addr(); ! 69: ! 70: while(--argc > 0) { ! 71: av++; ! 72: if(strcmp(*av,"Servername")==0) { ! 73: av++; ! 74: Servername = *av; ! 75: argc--; ! 76: } else if (strcmp(*av,"host")==0) { ! 77: av++; ! 78: laddr.siso_addr = iso_addr(*av); ! 79: argc--; ! 80: } else if (strcmp(*av,"count")==0) { ! 81: av++; ! 82: sscanf(*av,"%ld",&count); ! 83: argc--; ! 84: } else if (strcmp(*av,"port")==0) { ! 85: av++; ! 86: sscanf(*av,"%hd",&port); ! 87: argc--; ! 88: } else if (strcmp(*av,"size")==0) { ! 89: av++; ! 90: sscanf(*av,"%ld",&size); ! 91: argc--; ! 92: } else if (strcmp(*av, "intercept")==0) { ! 93: intercept++; ! 94: } ! 95: } ! 96: if (Servername) { ! 97: int tlen = laddr.siso_tlen = strlen(Servername); ! 98: int len = TSEL(siso) + tlen - (caddr_t) &siso; ! 99: if (len > sizeof(*siso)) { ! 100: siso = (struct sockaddr_iso *)malloc(len); ! 101: *siso = laddr; ! 102: siso->siso_len = len; ! 103: } ! 104: bcopy(Servername, TSEL(siso), tlen); ! 105: } else { ! 106: port = htons(port); ! 107: laddr.siso_tlen = sizeof(port); ! 108: bcopy((char *)&port, TSEL(siso), sizeof(port)); ! 109: } ! 110: tisink(); ! 111: } ! 112: #define BIG 2048 ! 113: #define MIDLIN 512 ! 114: char readbuf[BIG]; ! 115: struct iovec iov[1] = { ! 116: readbuf, ! 117: sizeof readbuf, ! 118: }; ! 119: char name[MIDLIN]; ! 120: union { ! 121: struct { ! 122: struct cmsghdr cmhdr; ! 123: char cmdata[128 - sizeof(struct cmsghdr)]; ! 124: } cm; ! 125: char data[128]; ! 126: } cbuf; ! 127: #define control cbuf.data ! 128: struct msghdr msghdr = { ! 129: name, sizeof(name), ! 130: iov, sizeof(iov)/sizeof(iov[1]), ! 131: control, sizeof control, ! 132: 0 /* flags */ ! 133: }; ! 134: ! 135: tisink() ! 136: { ! 137: int x, s, pid, on = 1, loop = 0, n; ! 138: extern int errno; ! 139: ! 140: try(socket, (AF_ISO, SOCK_SEQPACKET, 0),""); ! 141: ! 142: s = x; ! 143: ! 144: try(bind, (s, (struct sockaddr *) siso, siso->siso_len), ""); ! 145: ! 146: /*try(setsockopt, (s, SOL_SOCKET, SO_DEBUG, &on, sizeof (on)), ""); */ ! 147: ! 148: try(listen, (s, 5), ""); ! 149: if (intercept) { ! 150: try(setsockopt, ! 151: (s, SOL_TRANSPORT, TPOPT_INTERCEPT, &on, sizeof(on)), ""); ! 152: } ! 153: for(;;) { ! 154: int child, ns; ! 155: int addrlen = sizeof(faddr); ! 156: char childname[50]; ! 157: ! 158: try (accept, (s, &faddr, &addrlen), ""); ! 159: ns = x; ! 160: dumpit("connection from:", &faddr, sizeof faddr); ! 161: if (mynamep || intercept) { ! 162: addrlen = sizeof(faddr); ! 163: try (getsockname, (ns, &faddr, &addrlen), ""); ! 164: dumpit("connected as:", &faddr, addrlen); ! 165: } ! 166: loop++; ! 167: if (loop > 3) myexit(0); ! 168: if (forkp) { ! 169: try(fork, (), ""); ! 170: } else ! 171: x = 0; ! 172: if (x == 0) { ! 173: long n, count = 0, cn, flags; ! 174: records = 0; ! 175: if (confp) { ! 176: msghdr.msg_iovlen = 0; ! 177: msghdr.msg_namelen = 0; ! 178: msghdr.msg_controllen = ! 179: cbuf.cm.cmhdr.cmsg_len = sizeof (cbuf.cm.cmhdr); ! 180: cbuf.cm.cmhdr.cmsg_level = SOL_TRANSPORT; ! 181: cbuf.cm.cmhdr.cmsg_type = TPOPT_CFRM_DATA; ! 182: n = sendmsg(ns, &msghdr, 0); ! 183: if (n <= 0) { ! 184: printf("confirm: errno is %d\n", errno); ! 185: fflush(stdout); ! 186: perror("Confirm error"); ! 187: } else { ! 188: dbprintf("confim ok\n"); ! 189: } ! 190: sleep(10); ! 191: } ! 192: for (;;) { ! 193: msghdr.msg_iovlen = 1; ! 194: msghdr.msg_controllen = sizeof(control); ! 195: iov->iov_len = sizeof(readbuf); ! 196: n = recvmsg(ns, &msghdr, 0); ! 197: flags = msghdr.msg_flags; ! 198: count++; ! 199: dbprintf("recvmsg from child %d got %d ctl %d flags %x\n", ! 200: getpid(), n, (cn = msghdr.msg_controllen), ! 201: flags); ! 202: fflush(stdout); ! 203: if (cn && verbose) ! 204: dumpit("control data:\n", control, cn); ! 205: if (n < 0) { ! 206: fprintf(stderr, "errno is %d\n", errno); ! 207: perror("recvmsg"); ! 208: /*sleep (10);*/ ! 209: break; ! 210: } else { ! 211: if (verbose) ! 212: dumpit("data:\n", readbuf, n); ! 213: } ! 214: if (echop) { ! 215: n = answerback(flags, n, ns); ! 216: } ! 217: if (flags & MSG_EOR) ! 218: records++; ! 219: if (playtag && n == sizeof(your_it) && (flags & MSG_EOR) ! 220: && bcmp(readbuf, your_it, n) == 0) { ! 221: printf("Answering back!!!!\n"); ! 222: answerback(flags, n, ns); ! 223: answerback(flags, n, ns); ! 224: } ! 225: errno = 0; ! 226: } ! 227: } ! 228: myexit(0); ! 229: } ! 230: } ! 231: answerback(flags, n, ns) ! 232: { ! 233: msghdr.msg_controllen = 0; ! 234: msghdr.msg_iovlen = 1; ! 235: iov->iov_len = n; ! 236: n = sendmsg(ns, &msghdr, flags); ! 237: dbprintf("echoed %d\n", n); ! 238: return n; ! 239: } ! 240: ! 241: dumpit(what, where, n) ! 242: char *what; unsigned short *where; int n; ! 243: { ! 244: unsigned short *s = where; ! 245: unsigned short *z = where + (n+1)/2; ! 246: int count = 0; ! 247: printf(what); ! 248: while(s < z) { ! 249: count++; ! 250: printf("%x ",*s++); ! 251: if ((count & 15) == 0) ! 252: putchar('\n'); ! 253: } ! 254: if (count & 15) ! 255: putchar('\n'); ! 256: fflush(stdout); ! 257: } ! 258: myexit(n) ! 259: { ! 260: fflush(stderr); ! 261: printf("got %d records\n", records); ! 262: fflush(stdout); ! 263: exit(n); ! 264: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.