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

  1. /*-
  2.  * Copyright (c) 1988, 1991 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, 1991 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[] = "@(#)xi_sink.c    7.4 (Berkeley) 5/7/91";
  42. #endif /* not lint */
  43.  
  44. /*
  45.  * This is a test program to be a sink for X.25 connections.
  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. #include <netccitt/x25.h>
  54.  
  55. #include <stdio.h>
  56. #include <errno.h>
  57. #include <ctype.h>
  58. #include <netdb.h>
  59.  
  60.  
  61. #define dbprintf if(verbose)printf
  62. #define try(a,b,c) {x = (a b); dbprintf("%s%s returns %d\n",c,"a",x);\
  63.         if(x<0) {perror("a"); myexit(0);}}
  64.  
  65.  
  66. struct  ifreq ifr;
  67. short port = 3000;
  68. struct  sockaddr_x25 faddr, laddr = { sizeof(laddr), AF_CCITT };
  69. struct  sockaddr_x25 *sx25 = &laddr;
  70. char **xenvp;
  71.  
  72. long size, count = 10, forkp, confp, echop, mynamep, verbose = 1, playtag = 0;
  73. long records, intercept = 0, isode_mode;
  74.  
  75. char buf[2048];
  76. char your_it[] = "You're it!";
  77.  
  78. char *Servername;
  79.  
  80. main(argc, argv, envp)
  81. int argc;
  82. char *argv[];
  83. char *envp[];
  84. {
  85.     register char **av = argv;
  86.     register char *cp;
  87.  
  88.     xenvp = envp;
  89.     while(--argc > 0) {
  90.         av++;
  91.         if (strcmp(*av,"host")==0) {
  92.             av++;
  93.             ccitt_addr(*av, sx25);
  94.             argc--;
  95.         } else if (strcmp(*av,"count")==0) {
  96.             av++;
  97.             sscanf(*av,"%ld",&count);
  98.             argc--;
  99.         } else if (strcmp(*av,"size")==0) {
  100.             av++;
  101.             sscanf(*av,"%ld",&size);
  102.             argc--;
  103.         } else if (strcmp(*av, "intercept")==0) {
  104.             intercept++;
  105.         }
  106.     }
  107.     tisink();
  108. }
  109. #define BIG 2048
  110. #define MIDLIN 512
  111. char readbuf[BIG];
  112. struct iovec iov[1] = {
  113.     readbuf,
  114.     sizeof readbuf,
  115. };
  116. char name[MIDLIN];
  117. union {
  118.     struct {
  119.         struct cmsghdr    cmhdr;
  120.         char        cmdata[128 - sizeof(struct cmsghdr)];
  121.     } cm;
  122.     char data[128];
  123. } cbuf;
  124. #define control cbuf.data
  125. struct msghdr msghdr = {
  126.     name, sizeof(name),
  127.     iov, sizeof(iov)/sizeof(iov[1]),
  128.     control, sizeof control,
  129.     0 /* flags */
  130. };
  131.  
  132. tisink()
  133. {
  134.     int x, s, pid, on = 1, loop = 0, n;
  135.     extern int errno;
  136.  
  137.     try(socket, (AF_CCITT, SOCK_STREAM, 0),"");
  138.  
  139.     s = x;
  140.  
  141.     sx25->x25_opts.op_flags |= X25_MQBIT;
  142.     try(bind, (s, (struct sockaddr *) sx25, sx25->x25_len), "");
  143.  
  144.     /*try(setsockopt, (s, SOL_SOCKET, SO_DEBUG, &on, sizeof (on)), ""); */
  145.  
  146.     try(listen, (s, 5), "");
  147.     for(;;) {
  148.         int child, ns;
  149.         int addrlen = sizeof(faddr);
  150.         char childname[50];
  151.  
  152.         try (accept, (s, &faddr, &addrlen), "");
  153.         ns = x;
  154.         dumpit("connection from:", &faddr, sizeof faddr);
  155.         if (mynamep || intercept) {
  156.             addrlen = sizeof(faddr);
  157.             try (getsockname, (ns, &faddr, &addrlen), "");
  158.             dumpit("connected as:", &faddr, addrlen);
  159.         }
  160.         loop++;
  161.         if (loop > 3) myexit(0);
  162.         if (forkp) {
  163.             try(fork, (), "");
  164.         } else
  165.             x = 0;
  166.         if (x == 0)  {
  167.             long n, count = 0, cn, flags;
  168.             records = 0;
  169. #ifdef ISODE_MODE
  170.             if (isode_mode) {
  171.             static char fdbuf[10];
  172.             static char *nargv[4] =
  173.                 {"/usr/sbin/isod.tsap", fdbuf, "", 0};
  174.             sprintf(fdbuf, "Z%d", ns);
  175.             old_isod_main(3, nargv, xenvp);
  176.             } else
  177. #endif
  178.             for (;;) {
  179.             msghdr.msg_iovlen = 1;
  180.             msghdr.msg_controllen = sizeof(control);
  181.             iov->iov_len = sizeof(readbuf);
  182.             n = recvmsg(ns, &msghdr, 0);
  183.             flags = msghdr.msg_flags;
  184.             count++;
  185.             dbprintf("recvmsg from child %d got %d ctl %d flags %x\n",
  186.                     getpid(), n, (cn = msghdr.msg_controllen),
  187.                     flags);
  188.             fflush(stdout);
  189.             if (cn && verbose)
  190.                 dumpit("control data:\n", control, cn);
  191.             if (n < 0) {
  192.                 fprintf(stderr, "errno is %d\n", errno);
  193.                 perror("recvmsg");
  194.                 /*sleep (10);*/
  195.                 break;
  196.             } else {
  197.                 if (verbose)
  198.                     dumpit("data:\n", readbuf, n);
  199.             }
  200.             if (echop) {
  201.                 n = answerback(flags, n, ns);
  202.             }
  203.             if (flags & MSG_EOR)
  204.                 records++;
  205.             if (playtag && n == sizeof(your_it) && (flags & MSG_EOR)
  206.                 && bcmp(readbuf, your_it, n) == 0) {
  207.                 printf("Answering back!!!!\n");
  208.                 answerback(flags, n, ns);
  209.                 answerback(flags, n, ns);
  210.             }
  211.             errno = 0;
  212.             }
  213.         }
  214.         myexit(0);
  215.     }
  216. }
  217. answerback(flags, n, ns)
  218. {
  219.     msghdr.msg_controllen = 0;
  220.     msghdr.msg_iovlen = 1;
  221.     iov->iov_len = n;
  222.     n = sendmsg(ns, &msghdr, flags);
  223.     dbprintf("echoed %d\n", n);
  224.     return n;
  225. }
  226.  
  227. dumpit(what, where, n)
  228. char *what; unsigned short *where; int n;
  229. {
  230.     unsigned short *s = where;
  231.     unsigned short *z = where + (n+1)/2;
  232.     int count = 0;
  233.     printf(what);
  234.     while(s < z) {
  235.         count++;
  236.         printf("%x ",*s++);
  237.         if ((count & 15) == 0)
  238.             putchar('\n');
  239.     }
  240.     if (count & 15)
  241.         putchar('\n');
  242.     fflush(stdout);
  243. }
  244. myexit(n)
  245. {
  246.     fflush(stderr);
  247.     printf("got %d records\n", records);
  248.     fflush(stdout);
  249.     exit(n);
  250. }
  251.