home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sys / tests / netccitt / xi_src.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-07  |  7.0 KB  |  279 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_src.c    7.5 (Berkeley) 5/7/91";
  42. #endif /* not lint */
  43.  
  44. /*
  45.  * This is a test program to be a source for X.25 connections.
  46.  */
  47. #include <sys/types.h>
  48. #include <sys/socket.h>
  49. #include <sys/uio.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"); exit(1);}}
  64.  
  65. fd_set    readfds, writefds, exceptfds;
  66. long size, count = 10;
  67. int verbose = 1, selectp, type = SOCK_STREAM, nobuffs, errno, playtag = 0;
  68. int verify = 0, mqdata;
  69. short portnumber = 3000;
  70. struct sockaddr_x25 to;
  71. char your_it[] = "You're it!";
  72. char *port, *conndata, data_msg[2048];
  73. struct iovec iov[1] = {data_msg};
  74. union {
  75.     struct {
  76.         struct cmsghdr    cmhdr;
  77.         char        cmdata[128 - sizeof (struct cmsghdr)];
  78.     } cm;
  79.     char data[128];
  80. } cm;
  81. struct msghdr msg = { 0, 0, iov, 1, 0, 0, 0};
  82.  
  83. main(argc, argv)
  84. int argc;
  85. char *argv[];
  86. {
  87.     register char **av = argv;
  88.     register char *cp;
  89.     u_long len;
  90.     int handy;
  91.  
  92.     while(--argc > 0) {
  93.         av++;
  94.         if (strcmp(*av,"dest")==0) {
  95.             av++;
  96.             ccitt_addr(*av, &to);
  97.             argc--;
  98.         } else if (strcmp(*av,"count")==0) {
  99.             av++;
  100.             sscanf(*av,"%ld",&count);
  101.             argc--;
  102.         } else if (strcmp(*av,"size")==0) {
  103.             av++;
  104.             sscanf(*av,"%ld",&size);
  105.         }
  106.     }
  107.     xisrc();
  108. }
  109.  
  110. xisrc() {
  111.     int x, s, pid, on = 1, flags = 8, n;
  112.  
  113.     try(socket, (AF_CCITT, type, 0),"");
  114.     s = x;
  115.  
  116.     /*try(setsockopt, (s, SOL_SOCKET, SO_DEBUG, &on, sizeof (on)), "");*/
  117.  
  118.     to.x25_opts.op_flags |= X25_MQBIT;
  119.     try(connect, (s, (struct sockaddr *) &to, to.x25_len), "");
  120.  
  121.     if (selectp) {
  122.         FD_ZERO(&writefds); FD_SET(s, &writefds);
  123.         select(1, &writefds, 0, 0, 0);
  124.     }
  125.     while (count-- > 0) {
  126.         if (size <= 0 && get_record(&flags) == EOF)
  127.             exit(0);
  128.         n = put_record(s, flags);
  129.         if (n < iov->iov_len) {
  130.             if (n==-1 && errno == 55) {
  131.                 nobuffs++;
  132.                 count++;
  133.                 continue;
  134.             }
  135.             fprintf(stderr, "wrote %d < %d, count %d,",
  136.                         n, iov->iov_len, count);
  137.             perror("due to");
  138.         }
  139.     }
  140.     if (playtag) {
  141.         printf("Tag time!\n");
  142.         iov->iov_base = your_it;
  143.         iov->iov_len = sizeof your_it;
  144.         sendmsg(s, &msg, MSG_EOR);
  145.         sendmsg(s, &msg, MSG_EOR);
  146.         iov->iov_base = data_msg;
  147.         iov->iov_len = sizeof data_msg;
  148.         try(recvmsg, (s, &msg, flags), " playtag ");
  149.     }
  150.     if (nobuffs) {
  151.         printf("looped %d times waiting for bufs\n", nobuffs);
  152.     }
  153. }
  154. int localsize;
  155. char dupbuf[4096];
  156.  
  157. put_record(s, flags)
  158. int s, flags;
  159. {
  160.     int fd, buflen;
  161.     char *buf;
  162.     int x, saved_x;
  163.  
  164.     msg.msg_flags = flags;
  165.     if (verbose) {
  166.         unsigned short *zp, *zlim;
  167.         if (msg.msg_controllen) {
  168.             zp = (unsigned short *)&(cm.cm.cmhdr.cmsg_len);
  169.             printf("(CMessage Type is %x) ", cm.cm.cmhdr.cmsg_type);
  170.             printf("CMsg data: ");
  171.             x = msg.msg_controllen;
  172.             zlim = zp + ((x + 1) / 2);
  173.             while (zp < zlim) printf("%x ", *zp++);
  174.             putchar ('\n');
  175.         }
  176.         if (iov->iov_len) {
  177.             printf("sending: %s %s",
  178.             (flags & MSG_OOB ? "(OOB Data)" : ""),
  179.                 (flags & MSG_EOR ? "(Record Mark)" : ""));
  180.             x = localsize;
  181.             zp = (unsigned short *)data_msg;
  182.             zlim = zp + ((x + 1) / 2);
  183.             while (zp < zlim) printf("%x ", *zp++);
  184.             putchar ('\n');
  185.         }
  186.     }
  187.     if (verify) {
  188.         buflen = iov->iov_len;
  189.         bcopy(iov->iov_base, dupbuf, buflen);
  190.     }
  191.     try(sendmsg, (s, &msg, flags), " put_record ");
  192.     saved_x = x;
  193.     while (verify && buflen > 0) {
  194.         iov->iov_len = buflen;
  195.         iov->iov_base = dupbuf;
  196.         try(recvmsg, (s, &msg, flags), " put_record ");
  197.         printf("verify got %d\n", x);
  198.         buflen -= x;
  199.     }
  200.     msg.msg_control = 0;
  201.     return (saved_x);
  202. }
  203. int *datasize = &iov->iov_len;
  204. char *cp, *cplim;
  205.  
  206. get_control_data(type, level)
  207. {
  208.  
  209.     datasize = (int *)&msg.msg_controllen;
  210.     cp = cm.cm.cmdata;
  211.     cplim = cp + sizeof(cm.cm.cmdata);
  212.     cm.cm.cmhdr.cmsg_level = level;
  213.     cm.cm.cmhdr.cmsg_type = type;
  214.     msg.msg_control = cm.data;
  215. }
  216.  
  217.  
  218.  
  219. get_record(flags)
  220. int *flags;
  221. {
  222.     int factor = 1, x = 0;
  223.     char workbuf[10240];
  224.     static repeatcount, repeatsize;
  225.  
  226.     if (repeatcount > 0) {
  227.         repeatcount--;
  228.         return;
  229.     }
  230.     *flags = 0;
  231.     *datasize = 0;
  232.     datasize = &iov->iov_len;
  233.     cp = data_msg + 1;
  234.     cplim  = data_msg + sizeof(data_msg);
  235.  
  236.     *data_msg = 0;
  237.     for(;;) {
  238.         x = scanf("%s", workbuf);
  239.         if (x == EOF)
  240.             break;
  241.         if (strcmp(workbuf, "oob") == 0)
  242.             *flags |= MSG_OOB;
  243.         else if (strcmp(workbuf, "qbit") == 0)
  244.             *data_msg |= 0x80;
  245.         else if (strcmp(workbuf, "mbit") == 0)
  246.             *data_msg |= 0x40;
  247.         else if (strcmp(workbuf, "eom") == 0)
  248.             *flags |= MSG_EOR;
  249.         else if (strcmp(workbuf, "factor") == 0) {
  250.             x = scanf("%d", &factor);
  251.             if (factor <= 0) factor = 1;
  252.             if (x == EOF)
  253.                 break;
  254.         } else if (strcmp(workbuf, "repeat") == 0) {
  255.             x = scanf("%d", &repeatcount);
  256.             if (repeatcount <= 0) repeatcount = 1;
  257.             if (x == EOF)
  258.                 break;
  259.         } else {
  260.             int len = strlen(workbuf);
  261.             localsize = 1;
  262.             while ((factor-- > 0) &&
  263.                    ((cp + len) < cplim)) {
  264.                     strcpy(cp, workbuf);
  265.                     cp += len;
  266.                     localsize += len;
  267.             }
  268.             *datasize = localsize;
  269.             if (datasize != &iov->iov_len) {
  270.                 *datasize += sizeof(cm.cm.cmhdr);
  271.                 repeatsize = cm.cm.cmhdr.cmsg_len = *datasize;
  272.             }
  273.             break;
  274.         }
  275.     }
  276.     errno = 0;
  277.     return (x);
  278. }
  279.