rbb         99/04/28 07:30:28

  Modified:    apr/network_io/unix sockets.c
               apr/test Makefile
               include  apr_thread_proc.h
  Added:       apr/test client.c server.c testsock.c
  Log:
  First pass at the test case for network I/O.  It is not portable yet, because
  select is annoying.  So I am changing to poll next, but I wanted to get this
  stuff in.
  
  Revision  Changes    Path
  1.11      +12 -8     apache-apr/apr/network_io/unix/sockets.c
  
  Index: sockets.c
  ===================================================================
  RCS file: /home/cvs/apache-apr/apr/network_io/unix/sockets.c,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- sockets.c 1999/04/26 13:07:48     1.10
  +++ sockets.c 1999/04/28 14:30:23     1.11
  @@ -135,21 +135,26 @@
       apr_socket_t *new = (apr_socket_t *)malloc(sizeof(apr_socket_t));
       struct hostent *hptr;
   
  +    new->addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
  +    new->addr_len = sizeof(struct sockaddr_in);
  +
       new->socketdes = accept(sock->socketdes, (struct sockaddr *)new->addr, 
&new->addr_len);
   
  -    hptr = gethostbyaddr((char *)&new->addr->sin_addr, sizeof(struct 
in_addr), AF_INET);
  +    if (new->socketdes < 0) {
  +        free(new->addr);
  +        free(new);
  +        return NULL;
  +    }
  +    
  +hptr = gethostbyaddr((char *)&new->addr->sin_addr, sizeof(struct in_addr), 
AF_INET);
       if (hptr != NULL) {
           new->remote_hostname = strdup(hptr->h_name);
       }
       
  -    free(hptr);    
  -    if (new->socketdes >= 0)
  -        return new;
  -    free(new);
  -    return NULL;
  +    return new;
   }
   
  -apr_status_t apr_connect(apr_socket_t *sock, char *hostname, unsigned short 
port)
  +apr_status_t apr_connect(apr_socket_t *sock, char *hostname)
   {
       struct hostent *hp;
   
  @@ -163,7 +168,6 @@
       
       memcpy((char *)&sock->addr->sin_addr, hp->h_addr_list[0], hp->h_length);
   
  -    sock->addr->sin_port = htons((short)port);
       sock->addr->sin_family = AF_INET;
      
       sock->addr_len = sizeof(*sock->addr);
  
  
  
  1.7       +6 -1      apache-apr/apr/test/Makefile
  
  Index: Makefile
  ===================================================================
  RCS file: /home/cvs/apache-apr/apr/test/Makefile,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Makefile  1999/04/26 15:35:17     1.6
  +++ Makefile  1999/04/28 14:30:25     1.7
  @@ -27,7 +27,7 @@
   LD_SHLIB=ld
   LDFLAGS_SHLIB=-Bshareable
   LDFLAGS_SHLIB_EXPORT=-rdynamic
  -CFLAGS1= -DLINUX=2 -pthread -DUSE_HSREGEX
  +CFLAGS1= -DLINUX=2 -DUSE_HSREGEX
   INCLUDES1=
   LIBS_SHLIB=
   LDFLAGS1=
  @@ -52,6 +52,11 @@
   
   all:
        testfile ab_apr testproc
  +
  +testsock:
  +     $(CC) client.c $(INCLUDES) $(CFLAGS) $(LIBS) -o client $<
  +     $(CC) server.c $(INCLUDES) $(CFLAGS) $(LIBS) -o server $<
  +     $(CC) testsock.c $(INCLUDES) $(CFLAGS) $(LIBS) -o testsock $<
   
   testproc:
        $(CC) testproc.c $(INCLUDES) $(CFLAGS) $(LIBS) -o testproc $<
  
  
  
  1.1                  apache-apr/apr/test/client.c
  
  Index: client.c
  ===================================================================
  /* ====================================================================
   * Copyright (c) 1999 The Apache Group.  All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. All advertising materials mentioning features or use of this
   *    software must display the following acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * 4. The names "Apache Server" and "Apache Group" must not be used to
   *    endorse or promote products derived from this software without
   *    prior written permission. For written permission, please contact
   *    [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 6. Redistributions of any form whatsoever must retain the following
   *    acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
   * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Group.
   * For more information on the Apache Group and the Apache HTTP server
   * project, please see <http://www.apache.org/>.
   *
   */
  
  #include "apr_network_io.h"
  #include "apr_errno.h"
  #include "apr_general.h"
  #include "errno.h"
  #include <sys/types.h>
  #include <sys/time.h>
  #include <stdio.h>
  #include <unistd.h>
  
  #define STRLEN 15
  
  int main(int argc, char *argv[])
  {
      apr_socket_t *sock;
      apr_sd_set_t *sdset;
      apr_int32_t rv;
      struct timeval timeout;
      char datasend[STRLEN] = "Send data test";
      char datarecv[STRLEN];
  
      fprintf(stdout, "\tClient:  Creating new socket.......");
      if ((sock = apr_create_tcp_socket()) == NULL) {
          fprintf(stderr, "Couldn't create socket\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  
      fprintf(stdout, "\tClient:  Setting socket option NONBLOCK.......");
      if (apr_setsocketopt(sock, APR_SO_NONBLOCK, 1) == APR_FAILURE) {
          apr_close_socket(sock);
          fprintf(stderr, "Couldn't set socket option\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  
      fprintf(stdout, "\tClient:  Setting port for socket.......");
      if (apr_setport(sock, 8021) == APR_FAILURE) {
          apr_close_socket(sock);
          fprintf(stderr, "Couldn't set the port correctly\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");           
  
      fprintf(stdout, "\tClient:  Connecting to socket.......");
      if (apr_connect(sock, "localhost", 8021) == APR_FAILURE) {
          apr_close_socket(sock);
          fprintf(stderr, "Could not connect\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  
      fprintf(stdout, "\tClient:  Trying to send data over socket.......");
      if (apr_send(sock, datasend, STRLEN, 5) != STRLEN) {
          apr_close_socket(sock);
          fprintf(stderr, "Problem sending data\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
      
      fprintf(stdout, "\tClient:  Trying to receive data over socket.......");
      if (apr_recv(sock, datarecv, STRLEN, 5) != STRLEN) {
          apr_close_socket(sock);
          fprintf(stderr, "Problem receiving data\n");
          exit(-1);
      }
      if (strcmp(datarecv, "Recv data test")) {
          apr_close_socket(sock);
          fprintf(stderr, "I did not receive the correct data %s\n", datarecv);
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  
      fprintf(stdout, "\tClient:  Shutting down socket.......");
      if (apr_shutdown(sock, APR_SHUTDOWN_WRITE) == APR_FAILURE) {
          apr_close_socket(sock);
          fprintf(stderr, "Could not shutdown socket\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  
      fprintf(stdout, "\tClient:  Closing down socket.......");
      if (apr_close_socket(sock) == APR_FAILURE) {
          fprintf(stderr, "Could not shutdown socket\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  }
  
  
  
  1.1                  apache-apr/apr/test/server.c
  
  Index: server.c
  ===================================================================
  /* ====================================================================
   * Copyright (c) 1999 The Apache Group.  All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. All advertising materials mentioning features or use of this
   *    software must display the following acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * 4. The names "Apache Server" and "Apache Group" must not be used to
   *    endorse or promote products derived from this software without
   *    prior written permission. For written permission, please contact
   *    [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 6. Redistributions of any form whatsoever must retain the following
   *    acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
   * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Group.
   * For more information on the Apache Group and the Apache HTTP server
   * project, please see <http://www.apache.org/>.
   *
   */
  
  #include <stdio.h>
  #include <sys/time.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include "apr_network_io.h"
  #include "apr_errno.h"
  #include "apr_general.h"
  #include "errno.h"
  
  #define STRLEN 15
  
  int main(int argc, char *argv[])
  {
      apr_socket_t *sock;
      apr_socket_t *sock2;
      apr_sd_set_t sdset;
      apr_int32_t rv;
      char datasend[STRLEN];
      char datarecv[STRLEN] = "Recv data test";
  
      fprintf(stdout, "\tServer:  Creating new socket.......");
      if ((sock = apr_create_tcp_socket()) == NULL) {
          fprintf(stderr, "Couldn't create socket\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  
      fprintf(stdout, "\tServer:  Setting socket option NONBLOCK.......");
      if (apr_setsocketopt(sock, APR_SO_NONBLOCK, 1) == APR_FAILURE) {
          apr_close_socket(sock);
          fprintf(stderr, "Couldn't set socket option\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  
      fprintf(stdout, "\tServer:  Setting port for socket.......");
      if (apr_setport(sock, 8021) == APR_FAILURE) {
          apr_close_socket(sock);
          fprintf(stderr, "Couldn't set the port correctly\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  
      fprintf(stdout, "\tServer:  Binding socket to port.......");
      if (apr_bind(sock) == APR_FAILURE) {
          apr_close_socket(sock);
          fprintf(stderr, "Could not bind  %d\n", errno);
          exit(-1);
      }
      fprintf(stdout, "OK\n");
      
      fprintf(stdout, "\tServer:  Listening to socket.......");
      if (apr_listen(sock, 8021) == APR_FAILURE) {
          apr_close_socket(sock);
          fprintf(stderr, "Could not listen\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  
      fprintf(stdout, "\tServer:  Setting up sd sets.......");
      apr_sd_zero(&sdset);
      apr_sd_set(sock, &sdset);
      fprintf(stdout, "Setup done\n");
      
      fprintf(stdout, "\tServer:  Beginning to select on socket.......");
      rv = apr_select(sock->socketdes + 1, &sdset, &sdset, &sdset, NULL); 
      if (rv == -1) {
          apr_close_socket(sock);
          fprintf(stderr, "Select caused an error\n");
          exit(-1);
      }
      else if (rv == 0) {
          apr_close_socket(sock);
          fprintf(stderr, "I should not return until rv == 1\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  
      fprintf(stdout, "\tServer:  Accepting a connection.......");
      if ((sock2 = apr_accept(sock)) == NULL) {
          apr_close_socket(sock);
          fprintf(stderr, "Could not accept connection.\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  
      fprintf(stdout, "\tServer:  Trying to recv data from socket.......");
      if (apr_recv(sock2, datasend, STRLEN, 5) != STRLEN) {
          apr_close_socket(sock);
          apr_close_socket(sock2);
          fprintf(stderr, "Problem recving data\n");
          exit(-1);
      }
      if (strcmp(datasend, "Send data test")) {
          apr_close_socket(sock);
          apr_close_socket(sock2);
          fprintf(stderr, "I did not receive the correct data %s\n", datarecv);
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  
      fprintf(stdout, "\tServer:  Sending data over socket.......");
      if (apr_send(sock2, datarecv, STRLEN, 5) != STRLEN) {
          apr_close_socket(sock);
          apr_close_socket(sock2);
          fprintf(stderr, "Problem sending data\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
      
      fprintf(stdout, "\tServer:  Shutting down accepte socket.......");
      if (apr_shutdown(sock2, APR_SHUTDOWN_READ) == APR_FAILURE) {
          apr_close_socket(sock);
          apr_close_socket(sock2);
          fprintf(stderr, "Problem shutting down\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  
      fprintf(stdout, "\tServer:  closing duplicate socket.......");
      if (apr_close_socket(sock2) == APR_FAILURE) {
          apr_close_socket(sock);
          fprintf(stderr, "Problem closing down\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
      
      fprintf(stdout, "\tServer:  closing original socket.......");
      if (apr_close_socket(sock) == APR_FAILURE) {
          fprintf(stderr, "Problem closing down\n");
          exit(-1);
      }
      fprintf(stdout, "OK\n");
  }
  
  
  
  
  1.1                  apache-apr/apr/test/testsock.c
  
  Index: testsock.c
  ===================================================================
  /* ====================================================================
   * Copyright (c) 1999 The Apache Group.  All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. All advertising materials mentioning features or use of this
   *    software must display the following acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * 4. The names "Apache Server" and "Apache Group" must not be used to
   *    endorse or promote products derived from this software without
   *    prior written permission. For written permission, please contact
   *    [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 6. Redistributions of any form whatsoever must retain the following
   *    acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
   * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Group.
   * For more information on the Apache Group and the Apache HTTP server
   * project, please see <http://www.apache.org/>.
   *
   */
  
  #include <stdio.h>
  #include <signal.h>
  #include "apr_thread_proc.h"
  #include "apr_errno.h"
  #include "apr_general.h"
  #include "errno.h"
  
  #define STRLEN 15
  
  int main(int argc, char *argv[])
  {
      apr_procattr_t *attr1;
      apr_procattr_t *attr2;
      apr_proc_t *proc1;
      apr_proc_t *proc2;
      apr_status_t s1;
      apr_status_t s2;
  
      fprintf(stdout, "This test relies on the process test working.  
Please\n");
      fprintf(stdout, "run that test first, and only run this test when it\n");
      fprintf(stdout, "completes successfully.  Alternitevly, you could run\n");
      fprintf(stdout, "parnetio and childnetio by yourself.\n");
  
      fprintf(stdout, "Creating children to run network tests.......\n");
      attr1 = apr_createprocattr_init();
      attr2 = apr_createprocattr_init();
  
  
      if (attr1 == NULL || attr2 == NULL) {
          fprintf(stderr, "Problem creating proc attrs\n");
          exit(-1);
      }
     
      proc1 = apr_create_process("parnetio", NULL, NULL, attr1);
      proc2 = apr_create_process("childnetio", NULL, NULL, attr2);
  
      if (proc1 == NULL || proc2 == NULL) {
          fprintf(stderr, "Problem spawning new process\n");
          exit(-1);
      }
  
      while ((s1 = apr_wait_proc(proc1, APR_NOWAIT)) == APR_FAILURE &&
             (s2 = apr_wait_proc(proc2, APR_NOWAIT)) == APR_FAILURE)
          continue;
  
      if (s1 == APR_SUCCESS) {
          kill(proc2->pid, SIGTERM);
          apr_wait_proc(proc2, APR_WAIT);
      }
      else {
          kill(proc1->pid, SIGTERM);
          apr_wait_proc(proc1, APR_WAIT);
      }
      fprintf(stdout, "Network test completed.\n");   
  }
  
  
  
  1.4       +1 -1      apache-apr/include/apr_thread_proc.h
  
  Index: apr_thread_proc.h
  ===================================================================
  RCS file: /home/cvs/apache-apr/include/apr_thread_proc.h,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- apr_thread_proc.h 1999/04/26 18:16:48     1.3
  +++ apr_thread_proc.h 1999/04/28 14:30:28     1.4
  @@ -79,7 +79,7 @@
   
   apr_int32_t apr_fork(apr_proc_t *);
   apr_proc_t *apr_create_process(char *, char *const [], char **, 
apr_procattr_t *);
  -apr_wait_proc(apr_proc_t *, apr_wait_how_e);
  +apr_status_t apr_wait_proc(apr_proc_t *, apr_wait_how_e);
   
   #endif  /* ! APR_FILE_IO_H */
   
  
  
  

Reply via email to