home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sys / tests / netiso / tisink.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-07  |  7.6 KB  |  305 lines

  1. /*-
  2.  * Copyright (c) 1988, 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1988, 1990 The Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)tisink.c    7.6 (Berkeley) 5/7/91";
  42. #endif /* not lint */
  43.  
  44. /*
  45.  * This is a test program to be a sink for ISO packets.
  46.  */
  47. #include <sys/param.h>
  48. #include <sys/uio.h>
  49. #include <sys/socket.h>
  50. #include <sys/ioctl.h>
  51. #include <net/route.h>
  52. #include <net/if.h>
  53. #define  TCPT_NTIMERS 4
  54. #include <netiso/iso.h>
  55. #include <netiso/tp_param.h>
  56. #include <netiso/tp_user.h>
  57.  
  58. #include <stdio.h>
  59. #include <errno.h>
  60. #include <ctype.h>
  61. #include <netdb.h>
  62.  
  63.  
  64. #define dbprintf if(verbose)printf
  65. #define try(a,b,c) {x = (a b); dbprintf("%s%s returns %d\n",c,"a",x);\
  66.         if(x<0) {perror("a"); myexit(0);}}
  67.  
  68.  
  69. struct  ifreq ifr;
  70. short port = 3000;
  71. struct  sockaddr_iso faddr, laddr = { sizeof(laddr), AF_ISO };
  72. struct  sockaddr_iso *siso = &laddr;
  73. char **xenvp;
  74.  
  75. long size, count = 10, forkp, confp, echop, mynamep, verbose = 1, playtag = 0;
  76. long records, intercept = 0, isode_mode = 0, dgramp = 0;
  77.  
  78. char buf[2048];
  79. char your_it[] = "You're it!";
  80.  
  81. char *Servername;
  82.  
  83. main(argc, argv, envp)
  84. int argc;
  85. char *argv[];
  86. char *envp[];
  87. {
  88.     register char **av = argv;
  89.     register char *cp;
  90.     struct iso_addr *iso_addr();
  91.  
  92.     xenvp = envp;
  93.     while(--argc > 0) {
  94.         av++;
  95.         if(strcmp(*av,"Servername")==0) {
  96.             av++;
  97.             Servername = *av;
  98.             argc--;
  99.         } else if (strcmp(*av,"host")==0) {
  100.             av++;
  101.             laddr.siso_addr = *iso_addr(*av);
  102.             argc--;
  103.         } else if (strcmp(*av,"count")==0) {
  104.             av++;
  105.             sscanf(*av,"%ld",&count);
  106.             argc--;
  107.         } else if (strcmp(*av,"port")==0) {
  108.             av++;
  109.             sscanf(*av,"%hd",&port);
  110.             argc--;
  111.         } else if (strcmp(*av,"size")==0) {
  112.             av++;
  113.             sscanf(*av,"%ld",&size);
  114.             argc--;
  115.         } else if (strcmp(*av, "intercept")==0) {
  116.             intercept++;
  117.         }
  118.     }
  119.     if (Servername) {
  120.         int tlen = laddr.siso_tlen = strlen(Servername);
  121.         int len =  TSEL(siso) + tlen - (caddr_t) &siso;
  122.         if (len > sizeof(*siso)) {
  123.             siso = (struct sockaddr_iso *)malloc(len);
  124.             *siso = laddr;
  125.             siso->siso_len = len;
  126.         }
  127.         bcopy(Servername, TSEL(siso), tlen);
  128.     } else {
  129.         port = htons(port);
  130.         laddr.siso_tlen = sizeof(port);
  131.         bcopy((char *)&port, TSEL(siso), sizeof(port));
  132.     }
  133.     tisink();
  134. }
  135. #define BIG 2048
  136. #define MIDLIN 512
  137. char readbuf[BIG];
  138. struct iovec iov[1] = {
  139.     readbuf,
  140.     sizeof readbuf,
  141. };
  142. char name[MIDLIN];
  143. union {
  144.     struct {
  145.         struct cmsghdr    cmhdr;
  146.         char        cmdata[128 - sizeof(struct cmsghdr)];
  147.     } cm;
  148.     char data[128];
  149. } cbuf;
  150. #define control cbuf.data
  151. struct msghdr msghdr = {
  152.     name, sizeof(name),
  153.     iov, sizeof(iov)/sizeof(iov[1]),
  154.     control, sizeof control,
  155.     0 /* flags */
  156. };
  157.  
  158. tisink()
  159. {
  160.     int x, s, pid, on = 1, loop = 0, n, ns;
  161.     extern int errno;
  162.     int socktype = (dgramp ? SOCK_DGRAM : SOCK_SEQPACKET);
  163.     int addrlen = sizeof(faddr);
  164.  
  165.     try(socket, (AF_ISO, socktype, 0),"");
  166.  
  167.     s = x;
  168.  
  169.     try(bind, (s, (struct sockaddr *) siso, siso->siso_len), "");
  170.  
  171.     /*try(setsockopt, (s, SOL_SOCKET, SO_DEBUG, &on, sizeof (on)), ""); */
  172.     if (dgramp) {
  173.         ns  =  s;
  174.         goto dgram1;
  175.     }
  176.  
  177.     try(listen, (s, 5), "");
  178.     if (intercept) {
  179.         try(setsockopt,
  180.         (s, SOL_TRANSPORT, TPOPT_INTERCEPT, &on, sizeof(on)), "");
  181.     }
  182.     for(;;) {
  183.         int child;
  184.         char childname[50];
  185.  
  186.         try (accept, (s, &faddr, &addrlen), "");
  187.         ns = x;
  188.         dumpit("connection from:", &faddr, sizeof faddr);
  189.         if (mynamep || intercept) {
  190.             addrlen = sizeof(faddr);
  191.             try (getsockname, (ns, &faddr, &addrlen), "");
  192.             dumpit("connected as:", &faddr, addrlen);
  193.         }
  194.         loop++;
  195.         if(loop > 3) myexit(0);
  196.         if (forkp) {
  197.             try(fork, (), "");
  198.         } else
  199.             x = 0;
  200.         if (x == 0)  {
  201.             long n, count = 0, cn, flags;
  202.             records = 0;
  203.             if (confp) {
  204.             msghdr.msg_iovlen = 0;
  205.             msghdr.msg_namelen = 0;
  206.             msghdr.msg_controllen = 
  207.                 cbuf.cm.cmhdr.cmsg_len = sizeof (cbuf.cm.cmhdr);
  208.             cbuf.cm.cmhdr.cmsg_level = SOL_TRANSPORT;
  209.             cbuf.cm.cmhdr.cmsg_type = TPOPT_CFRM_DATA;
  210.             n = sendmsg(ns, &msghdr, 0);
  211.             if (n < 0) {
  212.                 printf("confirm: errno is %d\n", errno);
  213.                 fflush(stdout);
  214.                 perror("Confirm error");
  215.             } else {
  216.                 dbprintf("confim ok\n");
  217.             }
  218.             sleep(10);
  219.             }
  220. #ifdef ISODE_MODE
  221.             if (isode_mode) {
  222.             static char fdbuf[10];
  223.             static char *nargv[4] =
  224.                 {"/usr/sbin/isod.tsap", fdbuf, "", 0};
  225.             sprintf(fdbuf, "Z%d", ns);
  226.             old_isod_main(3, nargv, xenvp);
  227.             } else
  228. #endif
  229.             for (;;) {
  230.             dgram1:
  231.             msghdr.msg_iovlen = 1;
  232.             msghdr.msg_controllen = sizeof(control);
  233.             msghdr.msg_namelen = (dgramp ? (sizeof name) : 0);
  234.             iov->iov_len = sizeof(readbuf);
  235.             n = recvmsg(ns, &msghdr, 0);
  236.             flags = msghdr.msg_flags;
  237.             count++;
  238.             dbprintf("recvmsg from child %d got %d ctl %d flags %x\n",
  239.                 getpid(), n, (cn = msghdr.msg_controllen), flags);
  240.             fflush(stdout);
  241.             if (dgramp && msghdr.msg_namelen && verbose)
  242.                 dumpit("from:\n", name, msghdr.msg_namelen);
  243.             if (cn && verbose)
  244.                 dumpit("control data:\n", control, cn);
  245.             if (n < 0) {
  246.                 fprintf(stderr, "errno is %d\n", errno);
  247.                 perror("recvmsg");
  248.                 /*sleep (10);*/
  249.                 break;
  250.             } else {
  251.                 if (verbose)
  252.                     dumpit("data:\n", readbuf, n);
  253.             }
  254.             if (echop) {
  255.                 n = answerback(flags, n, ns);
  256.             }
  257.             if (flags & MSG_EOR)
  258.                 records++;
  259.             if (playtag && n == sizeof(your_it) && (flags & MSG_EOR)
  260.                 && bcmp(readbuf, your_it, n) == 0) {
  261.                 printf("Answering back!!!!\n");
  262.                 answerback(flags, n, ns);
  263.                 answerback(flags, n, ns);
  264.             }
  265.             errno = 0;
  266.             }
  267.         }
  268.         myexit(0);
  269.     }
  270. }
  271. answerback(flags, n, ns)
  272. {
  273.     msghdr.msg_controllen = 0;
  274.     msghdr.msg_iovlen = 1;
  275.     iov->iov_len = n;
  276.     n = sendmsg(ns, &msghdr, flags);
  277.     dbprintf("echoed %d\n", n);
  278.     return n;
  279. }
  280.  
  281. dumpit(what, where, n)
  282. char *what; unsigned short *where; int n;
  283. {
  284.     unsigned short *s = where;
  285.     unsigned short *z = where + (n+1)/2;
  286.     int count = 0;
  287.     printf(what);
  288.     while(s < z) {
  289.         count++;
  290.         printf("%x ",*s++);
  291.         if ((count & 15) == 0)
  292.             putchar('\n');
  293.     }
  294.     if (count & 15)
  295.         putchar('\n');
  296.     fflush(stdout);
  297. }
  298. myexit(n)
  299. {
  300.     fflush(stderr);
  301.     printf("got %d records\n", records);
  302.     fflush(stdout);
  303.     exit(n);
  304. }
  305.