home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / netper21.zip / netserver.c < prev    next >
Text File  |  1997-03-20  |  14KB  |  581 lines

  1.  
  2. /*
  3.  
  4.             Copyright (C) 1993,1994,1995 Hewlett-Packard Company
  5.                          ALL RIGHTS RESERVED.
  6.  
  7.   The enclosed software and documention includes copyrighted works of
  8.   Hewlett-Packard Co. For as long as you comply with the following
  9.   limitations, you are hereby authorized to (i) use, reproduce, and
  10.   modify the software and documentation, and to (ii) distribute the
  11.   software and documentation, including modifications, for
  12.   non-commercial purposes only.
  13.       
  14.   1.  The enclosed software and documentation is made available at no
  15.       charge in order to advance the general development of
  16.       high-performance networking products.
  17.  
  18.   2.  You may not delete any copyright notices contained in the
  19.       software or documentation. All hard copies, and copies in
  20.       source code or object code form, of the software or
  21.       documentation (including modifications) must contain at least
  22.       one of the copyright notices.
  23.  
  24.   3.  The enclosed software and documentation has not been subjected
  25.       to testing and quality control and is not a Hewlett-Packard Co.
  26.       product. At a future time, Hewlett-Packard Co. may or may not
  27.       offer a version of the software and documentation as a product.
  28.   
  29.   4.  THE SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS".
  30.       HEWLETT-PACKARD COMPANY DOES NOT WARRANT THAT THE USE,
  31.       REPRODUCTION, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR
  32.       DOCUMENTATION WILL NOT INFRINGE A THIRD PARTY'S INTELLECTUAL
  33.       PROPERTY RIGHTS. HP DOES NOT WARRANT THAT THE SOFTWARE OR
  34.       DOCUMENTATION IS ERROR FREE. HP DISCLAIMS ALL WARRANTIES,
  35.       EXPRESS AND IMPLIED, WITH REGARD TO THE SOFTWARE AND THE
  36.       DOCUMENTATION. HP SPECIFICALLY DISCLAIMS ALL WARRANTIES OF
  37.       MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  38.   
  39.   5.  HEWLETT-PACKARD COMPANY WILL NOT IN ANY EVENT BE LIABLE FOR ANY
  40.       DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
  41.       (INCLUDING LOST PROFITS) RELATED TO ANY USE, REPRODUCTION,
  42.       MODIFICATION, OR DISTRIBUTION OF THE SOFTWARE OR DOCUMENTATION.
  43.  
  44. */
  45. char    netserver_id[]="\
  46. @(#)netserver.c (c) Copyright 1993, 1994 Hewlett-Packard Co. Version 2.1";
  47.  
  48.  /***********************************************************************/
  49.  /*                                    */
  50.  /*    netserver.c                            */
  51.  /*                                    */
  52.  /*    This is the server side code for the netperf test package. It    */
  53.  /* will operate either stand-alone, or as a child of inetd. In this    */
  54.  /* way, we insure that it can be installed on systems with or without    */
  55.  /* root permissions (editing inetd.conf). Essentially, this code is    */
  56.  /* the analog to the netsh.c code.                    */
  57.  /*                                    */
  58.  /***********************************************************************/
  59.  
  60.  
  61. /************************************************************************/
  62. /*                                    */
  63. /*    Global include files                        */
  64. /*                                    */
  65. /************************************************************************/
  66. #include <sys/types.h>
  67. #include <stdio.h>
  68. #include <errno.h>
  69. #include <signal.h>
  70. #ifndef WIN32
  71. #include <sys/ipc.h>
  72. #endif /* WIN32 */
  73. #include <fcntl.h>
  74. #ifdef WIN32
  75. #include <time.h>
  76. #include <windows.h>
  77. #include <winsock.h>
  78. #else
  79. #include <sys/time.h>
  80. #include <sys/ioctl.h>
  81. #include <sys/socket.h>
  82. #include <sys/stat.h>
  83. #include <netinet/in.h>
  84. #include <netdb.h>
  85. #include <unistd.h>
  86. #ifndef DONT_WAIT
  87. #include <sys/wait.h>
  88. #endif /* DONT_WAIT */
  89. #endif /* WIN32 */
  90. #include <string.h>
  91. #include <stdlib.h>
  92.  
  93. #include "netlib.h"
  94. #include "nettest_bsd.h"
  95.  
  96. #ifdef DO_UNIX
  97. #include "nettest_unix.h"
  98. #endif /* DO_UNIX */
  99.  
  100. #ifdef DO_DLPI
  101. #include "nettest_dlpi.h"
  102. #endif /* DO_DLPI */
  103.  
  104. #ifdef DO_IPV6
  105. #include "nettest_ipv6.h"
  106. #endif /* DO_IPV6 */
  107.  
  108. #include "netsh.h"
  109.  
  110. #ifndef DEBUG_LOG_FILE
  111. #define DEBUG_LOG_FILE "/tmp/netperf.debug"
  112. #endif /* DEBUG_LOG_FILE */
  113.  
  114.  /* some global variables */
  115.  
  116. FILE    *afp;
  117. short    listen_port_num;
  118. extern    char    *optarg;
  119. extern    int    optind, opterr;
  120.  
  121. #define SERVER_ARGS "dn:p:"
  122.  
  123.  /* This routine implements the "main event loop" of the netperf    */
  124.  /* server code. Code above it will have set-up the control connection    */
  125.  /* so it can just merrily go about its business, which is to        */
  126.  /* "schedule" performance tests on the server.                */
  127.  
  128. void 
  129. process_requests()
  130. {
  131.   
  132.   float    temp_rate;
  133.   
  134.   
  135.   while (1) {
  136.     recv_request();
  137.     if (debug)
  138.       dump_request();
  139.     
  140.     switch (netperf_request.content.request_type) {
  141.       
  142.     case DEBUG_ON:
  143.       netperf_response.content.response_type = DEBUG_OK;
  144.       if (!debug)
  145.     debug++;
  146.       dump_request();
  147.       send_response();
  148.       break;
  149.       
  150.     case DEBUG_OFF:
  151.       if (debug)
  152.     debug--;
  153.       netperf_response.content.response_type = DEBUG_OK;
  154.       fclose(where);
  155.       send_response();
  156.       break;
  157.       
  158.     case CPU_CALIBRATE:
  159.       netperf_response.content.response_type = CPU_CALIBRATE;
  160.       temp_rate = calibrate_local_cpu(0.0);
  161.       bcopy((char *)&temp_rate,
  162.         (char *)netperf_response.content.test_specific_data,
  163.         sizeof(temp_rate));
  164.       if (debug) {
  165.     fprintf(where,"netserver: sending CPU information:");
  166.     fprintf(where,"rate is %g\n",temp_rate);
  167.     fflush(where);
  168.       }
  169.  
  170.       /* we need the cpu_start, cpu_stop in the looper case to kill the */
  171.       /* child proceses raj 7/95 */
  172.       cpu_start(1);
  173.       cpu_stop(1,&temp_rate);
  174.  
  175.       send_response();
  176.       break;
  177.       
  178.     case DO_TCP_STREAM:
  179.       recv_tcp_stream();
  180.       break;
  181.       
  182.     case DO_TCP_RR:
  183.       recv_tcp_rr();
  184.       break;
  185.       
  186.     case DO_TCP_CRR:
  187.       recv_tcp_conn_rr();
  188.       break;
  189.       
  190. #ifdef DO_1644
  191.     case DO_TCP_TRR:
  192.       recv_tcp_tran_rr();
  193.       break;
  194. #endif /* DO_1644 */
  195.       
  196. #ifdef DO_NBRR
  197.     case DO_TCP_NBRR:
  198.       recv_tcp_nbrr();
  199.       break;
  200. #endif /* DO_NBRR */
  201.       
  202.     case DO_UDP_STREAM:
  203.       recv_udp_stream();
  204.       break;
  205.       
  206.     case DO_UDP_RR:
  207.       recv_udp_rr();
  208.       break;
  209.       
  210. #ifdef DO_DLPI
  211.  
  212.     case DO_DLPI_CO_RR:
  213.       recv_dlpi_co_rr();
  214.       break;
  215.       
  216.     case DO_DLPI_CL_RR:
  217.       recv_dlpi_cl_rr();
  218.       break;
  219.  
  220.     case DO_DLPI_CO_STREAM:
  221.       recv_dlpi_co_stream();
  222.       break;
  223.  
  224.     case DO_DLPI_CL_STREAM:
  225.       recv_dlpi_cl_stream();
  226.       break;
  227.  
  228. #endif /* DO_DLPI */
  229.  
  230. #ifdef DO_UNIX
  231.  
  232.     case DO_STREAM_STREAM:
  233.       recv_stream_stream();
  234.       break;
  235.       
  236.     case DO_STREAM_RR:
  237.       recv_stream_rr();
  238.       break;
  239.       
  240.     case DO_DG_STREAM:
  241.       recv_dg_stream();
  242.       break;
  243.       
  244.     case DO_DG_RR:
  245.       recv_dg_rr();
  246.       break;
  247.       
  248. #endif /* DO_UNIX */
  249.  
  250. #ifdef DO_FORE
  251.  
  252.     case DO_FORE_STREAM:
  253.       recv_fore_stream();
  254.       break;
  255.       
  256.     case DO_FORE_RR:
  257.       recv_fore_rr();
  258.       break;
  259.       
  260. #endif /* DO_FORE */
  261.  
  262. #ifdef DO_HIPPI
  263.  
  264.     case DO_HIPPI_STREAM:
  265.       recv_hippi_stream();
  266.       break;
  267.       
  268.     case DO_HIPPI_RR:
  269.       recv_hippi_rr();
  270.       break;
  271.       
  272. #endif /* DO_HIPPI */
  273.  
  274. #ifdef DO_XTI
  275.     case DO_XTI_TCP_STREAM:
  276.       recv_xti_tcp_stream();
  277.       break;
  278.       
  279.     case DO_XTI_TCP_RR:
  280.       recv_xti_tcp_rr();
  281.       break;
  282.       
  283.     case DO_XTI_UDP_STREAM:
  284.       recv_xti_udp_stream();
  285.       break;
  286.       
  287.     case DO_XTI_UDP_RR:
  288.       recv_xti_udp_rr();
  289.       break;
  290.  
  291. #endif /* DO_XTI */
  292. #ifdef DO_LWP
  293.     case DO_LWPSTR_STREAM:
  294.       recv_lwpstr_stream();
  295.       break;
  296.       
  297.     case DO_LWPSTR_RR:
  298.       recv_lwpstr_rr();
  299.       break;
  300.       
  301.     case DO_LWPDG_STREAM:
  302.       recv_lwpdg_stream();
  303.       break;
  304.       
  305.     case DO_LWPDG_RR:
  306.       recv_lwpdg_rr();
  307.       break;
  308.  
  309. #endif /* DO_LWP */
  310. #ifdef DO_IPV6
  311.     case DO_TCPIPV6_STREAM:
  312.       recv_tcpipv6_stream();
  313.       break;
  314.       
  315.     case DO_TCPIPV6_RR:
  316.       recv_tcpipv6_rr();
  317.       break;
  318.       
  319.     case DO_TCPIPV6_CRR:
  320.       recv_tcpipv6_conn_rr();
  321.       break;
  322.       
  323.     case DO_UDPIPV6_STREAM:
  324.       recv_udpipv6_stream();
  325.       break;
  326.       
  327.     case DO_UDPIPV6_RR:
  328.       recv_udpipv6_rr();
  329.       break;
  330.  
  331. #endif /* DO_IPV6 */
  332.  
  333.     default:
  334.       fprintf(where,"unknown test number %d\n",
  335.           netperf_request.content.request_type);
  336.       fflush(where);
  337.       netperf_response.content.serv_errno=998;
  338.       send_response();
  339.       break;
  340.       
  341.     }
  342.   }
  343. }
  344.  
  345. /*********************************************************************/
  346. /*                                                    */
  347. /*    set_up_server()                             */
  348. /*                                     */
  349. /* set-up the server listen socket. we only call this routine if the */
  350. /* user has specified a port number on the command line.             */
  351. /*                                     */
  352. /*********************************************************************/
  353. /*KC*/
  354.  
  355. void set_up_server()
  356.   struct sockaddr_in     server;
  357.   struct sockaddr_in     peeraddr;
  358.  
  359.   int server_control;
  360.   int peeraddr_len;
  361.   
  362.   server.sin_port = htons(listen_port_num);
  363.   server.sin_addr.s_addr = INADDR_ANY;
  364.   server.sin_family = AF_INET;
  365.   
  366.   printf("Starting netserver at port %d\n",listen_port_num);
  367.  
  368.   server_control = socket (AF_INET,SOCK_STREAM,0);
  369. #ifdef WIN32
  370.   if (server_control == INVALID_SOCKET)
  371. #else
  372.   if (server_control < 0)
  373. #endif /* WIN32 */
  374.     {
  375.       perror("server_set_up: creating the socket");
  376.       exit(1);
  377.     }
  378.   if (bind (server_control,
  379.         (struct sockaddr *)&server, 
  380.         sizeof(struct sockaddr_in)) == -1)
  381.     {
  382.       perror("server_set_up: binding the socket");
  383.       exit (1);
  384.     }
  385.   if (listen (server_control,5) == -1)
  386.     {
  387.       perror("server_set_up: listening");
  388.       exit(1);
  389.     }
  390.   
  391.   /*
  392.     setpgrp();
  393.     */
  394.  
  395. #ifndef WIN32
  396.   switch (fork())
  397.     {
  398.     case -1:      
  399.       perror("netperf server error");
  400.       exit(1);
  401.       
  402.     case 0:    
  403.       /* stdin/stderr should use fclose */
  404.       fclose(stdin);
  405.       fclose(stderr);
  406. #if defined(__NetBSD__) || defined(__bsdi__) || defined(sun)
  407.     setsid();
  408. #else
  409. #ifndef __EMX__
  410.     setpgrp();
  411. #endif
  412. #endif
  413.  
  414.  /* some OS's have SIGCLD defined as SIGCHLD */
  415. #ifndef SIGCLD
  416. #define SIGCLD SIGCHLD
  417. #endif /* SIGCLD */
  418.  
  419.       signal(SIGCLD, SIG_IGN);
  420.       
  421. #endif /* WIN32 */
  422.  
  423.       for (;;)
  424.     {
  425.       peeraddr_len = sizeof(peeraddr);
  426.       if ((server_sock=accept(server_control,
  427.                   (struct sockaddr *)&peeraddr,
  428.                   &peeraddr_len)) == -1)
  429.         {
  430.           printf("server_control: accept failed\n");
  431.           exit(1);
  432.         }
  433. #ifdef WIN32
  434.     /*
  435.      * Since we cannot fork this process , we cant fire any threads
  436.      * as they all share the same global data . So we better allow
  437.      * one request at at time 
  438.      */
  439.         process_requests() ;
  440.     }
  441. #else
  442.  
  443.       signal(SIGCLD, SIG_IGN);
  444.       
  445.       switch (fork())
  446.         {
  447.         case -1:
  448.           /* something went wrong */
  449.           exit(1);
  450.         case 0:
  451.           /* we are the child process */
  452.           close(server_control);
  453.           process_requests();
  454.           exit(0);
  455.           break;
  456.         default:
  457.           /* we are the parent process */
  458.           close(server_sock);
  459.           /* we should try to "reap" some of our children. on some */
  460.           /* systems they are being left as defunct processes. we */
  461.           /* will call waitpid, looking for any child process, */
  462.           /* with the WNOHANG feature. when waitpid return a zero, */
  463.           /* we have reaped all the children there are to reap at */
  464.           /* the moment, so it is time to move on. raj 12/94 */
  465. #ifndef DONT_WAIT
  466.           while(waitpid(-1, NULL, WNOHANG) > 0) { }
  467. #endif /* DONT_WAIT */
  468.           break;
  469.         }
  470.     } /*for*/
  471.       break; /*case 0*/
  472.       
  473.     default: 
  474.       exit (0);
  475.       
  476.     }
  477. #endif /* WIN32 */  
  478. }
  479.  
  480.  
  481. int
  482. main(argc, argv)
  483. int argc;
  484. char *argv[];
  485. {
  486.  
  487.   int    c;
  488.  
  489.   struct sockaddr name;
  490.   int namelen = sizeof(name);
  491.   
  492.  
  493. #ifdef WIN32
  494.     WSADATA    wsa_data ;
  495.  
  496.     /* Initialise the wsock lib ( version 1.1 ) */
  497.     if ( WSAStartup(0x0101,&wsa_data) == SOCKET_ERROR ){
  498.         printf("WSAStartup() fauled : %d\n",GetLastError()) ;
  499.         return 1 ;
  500.     }
  501. #endif /* WIN32 */
  502.  
  503.   netlib_init();
  504.   
  505.   /* Scan the command line to see if we are supposed to set-up our own */
  506.   /* listen socket instead of relying on inetd. */
  507.   
  508.   while ((c = getopt(argc, argv, SERVER_ARGS)) != EOF) {
  509.     switch (c) {
  510.     case '?':
  511.     case 'h':
  512.       print_netserver_usage();
  513.       exit(1);
  514.     case 'd':
  515.       /* we want to set the debug file name sometime */
  516.       debug++;
  517.       break;
  518.     case 'p':
  519.       /* we want to open a listen socket at a */
  520.       /* specified port number */
  521.       listen_port_num = atoi(optarg);
  522.       break;
  523.     case 'n':
  524.       shell_num_cpus = atoi(optarg);
  525.       if (shell_num_cpus > MAXCPUS) {
  526.     fprintf(stderr,
  527.         "netserver: This version can only support %d CPUs. Please",
  528.         MAXCPUS);
  529.     fprintf(stderr,
  530.         "           increase MAXCPUS in netlib.h and recompile.\n");
  531.     fflush(stderr);
  532.     exit(1);
  533.       }
  534.       break;
  535.  
  536.     }
  537.   }
  538.  
  539. /*  unlink(DEBUG_LOG_FILE); */
  540.   
  541.   if ((where = fopen(DEBUG_LOG_FILE, "w")) == NULL) {
  542.     perror("netserver: debug file");
  543.     exit(1);
  544.   }
  545.   
  546.   chmod(DEBUG_LOG_FILE,0644);
  547.   
  548.   /* if we were given a port number, then we should open a */
  549.   /* socket and hang listens off of it. otherwise, we should go */
  550.   /* straight into processing requests. the do_listen() routine */
  551.   /* will sit in an infinite loop accepting connections and */
  552.   /* forking child processes. the child processes will call */
  553.   /* process_requests */
  554.   
  555.   /* If fd 0 is not a socket then assume we're not being called */
  556.   /* from inetd and start server socket on the default port. */
  557.   /* this enhancement comes from vwelch@ncsa.uiuc.edu (Von Welch) */
  558.  
  559.   if (listen_port_num) {
  560.     /* the user specified a port number on the command line */
  561.     set_up_server();
  562.   }
  563.   else if (getsockname(server_sock, &name, &namelen) == -1) {
  564.     /* we may not be a chile of inetd */
  565. #ifdef WIN32
  566.       if (WSAGetLastError() == WSAENOTSOCK) {
  567. #else 
  568.       if (errno == ENOTSOCK) {
  569. #endif /* WIN32 */
  570.       listen_port_num = TEST_PORT;
  571.       set_up_server();
  572.     }
  573.   }
  574.   else {
  575.     /* we are probably a child of inetd */
  576.     process_requests();
  577.   }
  578.   return(0);
  579. }
  580.