bjh         99/06/04 09:15:59

  Added:       apr/network_io/os2 Makefile.in networkio.h poll.c sendrecv.c
                        sockets.c sockopt.c
  Log:
  OS/2 network_io library. Same as unix except for ap_poll. As OS/2 doesn't have
  a poll function it's implemented using select().
  
  Revision  Changes    Path
  1.1                  apache-apr/apr/network_io/os2/Makefile.in
  
  Index: Makefile.in
  ===================================================================
  #CFLAGS=$(OPTIM) $(CFLAGS1) $(EXTRA_CFLAGS)
  #LIBS=$(EXTRA_LIBS) $(LIBS1)
  #INCLUDES=$(INCLUDES1) $(INCLUDES0) $(EXTRA_INCLUDES)
  #LDFLAGS=$(LDFLAGS1) $(EXTRA_LDFLAGS)
  
  [EMAIL PROTECTED]@
  [EMAIL PROTECTED]@
  [EMAIL PROTECTED]@ @CFLAGS@ @OPTIM@
  [EMAIL PROTECTED]@
  [EMAIL PROTECTED]@ $(LDLIBS)
  INCDIR=../../include
  INCDIR1=../../../include
  INCLUDES=-I$(INCDIR) -I$(INCDIR1) -I.
  
  LIB=../network.a
  
  OBJS=poll.o \
        sendrecv.o \
        sockets.o \
        sockopt.o \
  
  .c.o:
        $(CC) $(CFLAGS) -c $(INCLUDES) $<
  
  all: $(LIB)
  
  clean:
        $(RM) -f *.o *.a *.so
  
  distclean: clean
        -$(RM) -f Makefile
  
  $(OBJS): Makefile
  
  $(LIB): $(OBJS)
        $(RM) -f $@
        $(AR) cr $@ $(OBJS)
        $(RANLIB) $@
  
  #
  # We really don't expect end users to use this rule.  It works only with
  # gcc, and rebuilds Makefile.tmpl.  You have to re-run Configure after
  # using it.
  #
  depend:
        cp Makefile.in Makefile.in.bak \
            && sed -ne '1,/^# DO NOT REMOVE/p' Makefile.in > Makefile.new \
            && gcc -MM $(INCLUDES) $(CFLAGS) *.c >> Makefile.new \
            && sed -e '1,$$s: $(INCDIR)/: $$(INCDIR)/:g' \
                   -e '1,$$s: $(OSDIR)/: $$(OSDIR)/:g' Makefile.new \
                > Makefile.in \
            && rm Makefile.new
  
  # DO NOT REMOVE
  poll.o: poll.c ../../../include/apr_network_io.h \
   ../../../include/apr_general.h ../../../include/apr_errno.h \
   networkio.h
  sendrecv.o: sendrecv.c ../../../include/apr_errno.h \
   ../../../include/apr_general.h ../../../include/apr_network_io.h \
   networkio.h
  sockets.o: sockets.c ../../../include/apr_network_io.h \
   ../../../include/apr_general.h ../../../include/apr_errno.h \
   networkio.h $(INCDIR)/apr_lib.h $(INCDIR)/apr_config.h \
   $(INCDIR)/hsregex.h
  sockopt.o: sockopt.c ../../../include/apr_network_io.h \
   ../../../include/apr_general.h ../../../include/apr_errno.h \
   networkio.h
  
  
  
  1.1                  apache-apr/apr/network_io/os2/networkio.h
  
  Index: networkio.h
  ===================================================================
  /* ====================================================================
   * 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/>.
   *
   */
  
  #ifndef NETWORK_IO_H
  #define NETWORK_IO_H
  
  #include "apr_network_io.h"
  #include "apr_general.h"
  
  struct socket_t {
      ap_context_t *cntxt;
      int socketdes;
      char *remote_hostname;
      struct sockaddr_in *addr;
      int addr_len;
  };
  
  struct pollfd_t {
      ap_context_t *cntxt;
      struct socket_t *sock;
      ap_int16_t events;
      ap_int16_t revents;
  };
  
  ap_int16_t get_event(ap_int16_t);
  
  #endif  /* ! NETWORK_IO_H */
  
  
  
  
  1.1                  apache-apr/apr/network_io/os2/poll.c
  
  Index: poll.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 "networkio.h"
  #include "apr_network_io.h"
  #include "apr_general.h"
  
  /*  OS/2 R4 doesn't have a poll function, implement using select */
   
  ap_status_t ap_setup_poll(ap_context_t *cont, ap_int32_t num, struct pollfd_t 
**new)
  {
      (*new) = (struct pollfd_t *)ap_palloc(cont, sizeof(struct pollfd_t) * 
num);
  
      if ((*new) == NULL) {
          return APR_ENOMEM;
      }
  
      (*new)->cntxt = cont;
      return APR_SUCCESS;
  }
  
  
  
  ap_status_t ap_add_poll_socket(struct pollfd_t *aprset,
                                 struct socket_t *sock, ap_int16_t event, 
                                 ap_int32_t pos) 
  { 
      aprset[pos].sock = sock; 
      aprset[pos].events = event;
      return APR_SUCCESS; 
  } 
  
  
  
  ap_status_t ap_poll(struct pollfd_t *pollfdset, ap_int32_t *nsds, ap_int32_t 
timeout)
  {
      int i;
      int rv = 0, maxfd = 0;
      time_t starttime;
      char test = 'T';
      struct timeval tv;
      fd_set readfds, writefds, exceptfds;
  
      FD_ZERO(&readfds);
      FD_ZERO(&writefds);
      FD_ZERO(&exceptfds);
      
      for (i = 0; i < *nsds; i++) {
          if (pollfdset[i].sock->socketdes > maxfd)
              maxfd = pollfdset[i].sock->socketdes;
  
          if (pollfdset[i].events & APR_POLLIN)
              FD_SET(pollfdset[i].sock->socketdes, &readfds);
  
          if (pollfdset[i].events & APR_POLLOUT)
              FD_SET(pollfdset[i].sock->socketdes, &writefds);
  
          if (pollfdset[i].events & APR_POLLPRI)
              FD_SET(pollfdset[i].sock->socketdes, &exceptfds);
      }
  
      tv.tv_sec = timeout;
      tv.tv_usec = 0;
      time(&starttime);
  
      do {
          rv = select(maxfd + 1, &readfds, &writefds, &exceptfds, timeout >= 0 
? &tv : NULL);
  
          if (rv < 0 && errno == EINTR && timeout >= 0 ) {
              time_t elapsed = time(NULL) - starttime;
  
              if (timeout <= elapsed)
                  break;
  
              tv.tv_sec = timeout - elapsed;
          }
      } while ( rv < 0 && errno == EINTR );
  
      if (rv >= 0) {
          for (i = 0; i < *nsds; i++) {
              pollfdset[i].revents =
                  (FD_ISSET(pollfdset[i].sock->socketdes, &readfds) ? 
APR_POLLIN : 0) +
                  (FD_ISSET(pollfdset[i].sock->socketdes, &writefds) ? 
APR_POLLOUT : 0) +
                  (FD_ISSET(pollfdset[i].sock->socketdes, &exceptfds) ? 
APR_POLLPRI : 0);
          }
      }
      
      (*nsds) = rv;
      return rv < 0 ? errno : APR_SUCCESS;
  }
  
  
  
  ap_status_t ap_get_revents(struct pollfd_t *aprset, ap_int32_t pos, 
ap_int16_t *event)
  {
      (*event) = aprset[pos].revents;
      return APR_SUCCESS;
  }
  
  
  
  
  1.1                  apache-apr/apr/network_io/os2/sendrecv.c
  
  Index: sendrecv.c
  ===================================================================
  /* ====================================================================
   * Copyright (c) 1996-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 and was originally based
   * on public domain software written at the National Center for
   * Supercomputing Applications, University of Illinois, Urbana-Champaign.
   * For more information on the Apache Group and the Apache HTTP server
   * project, please see <http://www.apache.org/>.
   *
   */
  
  #include "networkio.h"
  #include "apr_errno.h"
  #include "apr_general.h"
  #include "apr_network_io.h"
  #include <sys/time.h>
  
  ap_status_t ap_send(struct socket_t *sock, const char *buf, ap_ssize_t *len, 
time_t sec)
  {
      ssize_t rv;
      
      do {
          rv = write(sock->socketdes, buf, (*len));
      } while (rv == -1 && errno == EINTR);
  
      if (rv == -1 && errno == EAGAIN && sec > 0) {
          struct timeval tv;
          fd_set fdset;
          int srv;
  
          do {
              FD_ZERO(&fdset);
              FD_SET(sock->socketdes, &fdset);
              tv.tv_sec  = sec;
              tv.tv_usec = 0;
  
              srv = select(FD_SETSIZE, NULL, &fdset, NULL, &tv);
          } while (srv == -1 && errno == EINTR);
  
          if (srv < 1) {
              (*len) = -1;
              return errno;
          }
          else {
              do {
                  rv = write(sock->socketdes, buf, (*len));
              } while (rv == -1 && errno == EINTR);
          }
      }
      (*len) = rv;
      return APR_SUCCESS;
  }
  
  ap_status_t ap_recv(struct socket_t *sock, char *buf, ap_ssize_t *len, time_t 
sec)
  {
      ssize_t rv;
      
      do {
          rv = read(sock->socketdes, buf, (*len));
      } while (rv == -1 && errno == EINTR);
  
      if (rv == -1 && errno == EAGAIN && sec > 0) {
          struct timeval tv;
          fd_set fdset;
          int srv;
  
          do {
              FD_ZERO(&fdset);
              FD_SET(sock->socketdes, &fdset);
              tv.tv_sec  = sec;
              tv.tv_usec = 0;
  
              srv = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
          } while (srv == -1 && errno == EINTR);
  
          if (srv < 1) {
              (*len) = -1;
              return errno;
          }
          else {
              do {
                  rv = read(sock->socketdes, buf, (*len));
              } while (rv == -1 && errno == EINTR);
          }
      }
      (*len) = rv;
      return APR_SUCCESS;
  }
  
  
  
  
  1.1                  apache-apr/apr/network_io/os2/sockets.c
  
  Index: sockets.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 "networkio.h"
  #include "apr_network_io.h"
  #include "apr_general.h"
  #include "apr_lib.h"
  #include <errno.h>
  #include <string.h>
  #include <sys/socket.h>
  #include <netinet/tcp.h>
  #include <netinet/in.h>
  #include <netdb.h>
  
  ap_status_t socket_cleanup(void *sock)
  {
      struct socket_t *thesocket = sock;
      if (close(thesocket->socketdes) == 0) {
          thesocket->socketdes = -1;
          return APR_SUCCESS;
      }
      else {
          return errno;
      }
  }
  
  ap_status_t ap_create_tcp_socket(ap_context_t *cont, struct socket_t **new)
  {
      (*new) = (struct socket_t *)ap_palloc(cont, sizeof(struct socket_t));
  
      if ((*new) == NULL) {
          return APR_ENOMEM;
      }
      (*new)->cntxt = cont; 
      (*new)->addr = (struct sockaddr_in *)ap_palloc((*new)->cntxt,
                         sizeof(struct sockaddr_in));
  
      if ((*new)->addr == NULL) {
          return APR_ENOMEM;
      }
   
      (*new)->socketdes = socket(AF_INET ,SOCK_STREAM, IPPROTO_TCP);
      (*new)->remote_hostname = NULL;
  
      (*new)->addr->sin_family = AF_INET;
  
      (*new)->addr_len = sizeof(*(*new)->addr);
      
      if ((*new)->socketdes < 0) {
          return errno;
      }
      ap_register_cleanup((*new)->cntxt, (void *)(*new), 
                          socket_cleanup, NULL);
      return APR_SUCCESS;
  } 
  
  ap_status_t ap_shutdown(struct socket_t *thesocket, ap_shutdown_how_e how)
  {
      if (shutdown(thesocket->socketdes, how) == 0) {
          return APR_SUCCESS;
      }
      else {
          return errno;
      }
  }
  
  ap_status_t ap_close_socket(struct socket_t *thesocket)
  {
      ap_kill_cleanup(thesocket->cntxt, thesocket, socket_cleanup);
      return socket_cleanup(thesocket);
  }
  
  ap_status_t ap_setport(struct socket_t *sock, ap_uint32_t port)
  {
      sock->addr->sin_port = htons((short)port);
      return APR_SUCCESS;
  }
  
  ap_status_t ap_bind(struct socket_t *sock)
  {
      sock->addr->sin_addr.s_addr = INADDR_ANY;
      if (bind(sock->socketdes, (struct sockaddr *)sock->addr, sock->addr_len) 
== -1)
          return errno;
      else
          return APR_SUCCESS;
  }
  
  ap_status_t ap_listen(struct socket_t *sock, ap_int32_t backlog)
  {
      if (listen(sock->socketdes, backlog) == -1)
          return errno;
      else
          return APR_SUCCESS;
  }
  
  ap_status_t ap_accept(const struct socket_t *sock, struct socket_t **new)
  {
      struct hostent *hptr;
      
      (*new) = (struct socket_t *)ap_palloc(sock->cntxt, 
                              sizeof(struct socket_t));
  
      (*new)->cntxt = sock->cntxt;
      (*new)->addr = (struct sockaddr_in *)ap_palloc((*new)->cntxt, 
                   sizeof(struct sockaddr_in));
      (*new)->addr_len = sizeof(struct sockaddr_in);
  
      (*new)->socketdes = accept(sock->socketdes, (struct sockaddr 
*)(*new)->addr,
                          &(*new)->addr_len);
  
      if ((*new)->socketdes < 0) {
          return errno;
      }
      
      hptr = gethostbyaddr((char *)&(*new)->addr->sin_addr, 
                           sizeof(struct in_addr), AF_INET);
      if (hptr != NULL) {
          (*new)->remote_hostname = strdup(hptr->h_name);
      }
      
      ap_register_cleanup((*new)->cntxt, (void *)(*new), 
                          socket_cleanup, NULL);
      return APR_SUCCESS;
  }
  
  ap_status_t ap_connect(struct socket_t *sock, char *hostname)
  {
      struct hostent *hp;
  
      hp = gethostbyname(hostname);
  
      if ((sock->socketdes < 0) || (!sock->addr)) {
          return APR_ENOTSOCK;
      }
      if (!hp)  {
          if (h_errno == TRY_AGAIN) {
              return EAGAIN;
          }
          return h_errno;
      }
      
      memcpy((char *)&sock->addr->sin_addr, hp->h_addr_list[0], hp->h_length);
  
      sock->addr->sin_family = AF_INET;
     
      sock->addr_len = sizeof(*sock->addr);
  
      if ((connect(sock->socketdes, (const struct sockaddr *)sock->addr, 
sock->addr_len) < 0) &&
          (errno != EINPROGRESS)) {
          return errno;
      }
      else {
          sock->remote_hostname = strdup(hostname);
          return APR_SUCCESS;
      }
  }
  
  
  
  
  1.1                  apache-apr/apr/network_io/os2/sockopt.c
  
  Index: sockopt.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 "networkio.h"
  #include "apr_network_io.h"
  #include "apr_general.h"
  #include "apr_lib.h"
  #include <errno.h>
  #include <string.h>
  #include <sys/socket.h>
  #include <netinet/tcp.h>
  #include <netinet/in.h>
  #include <unistd.h>
  #include <fcntl.h>
  
  int soblock(int sd)
  {
      int fd_flags;
  
      fd_flags = fcntl(sd, F_GETFL, 0);
  #if defined(O_NONBLOCK)
      fd_flags &= ~O_NONBLOCK;
  #elif defined(O_NDELAY)
      fd_flags &= ~O_NDELAY;
  #elif defined(FNDELAY)
      fd_flags &= ~O_FNDELAY;
  #else
      /* XXXX: this breaks things, but an alternative isn't obvious...*/
      return -1;
  #endif
      if (fcntl(sd, F_SETFL, fd_flags) == -1) {
          return errno;
      }
      return APR_SUCCESS;
  }
  
  ap_status_t sononblock(int sd)
  {
      int fd_flags;
  
      fd_flags = fcntl(sd, F_GETFL, 0);
  #if defined(O_NONBLOCK)
      fd_flags |= O_NONBLOCK;
  #elif defined(O_NDELAY)
      fd_flags |= O_NDELAY;
  #eli f defined(FNDELAY)
      fd_flags |= O_FNDELAY;
  #else
      /* XXXX: this breaks things, but an alternative isn't obvious...*/
      return -1;
  #endif
      if (fcntl(sd, F_SETFL, fd_flags) == -1) {
          return errno;
      }
      return APR_SUCCESS;
  }
  
  
  ap_status_t ap_setsocketopt(struct socket_t *sock, ap_int32_t opt, ap_int32_t 
on)
  {
      int one;
      struct linger li;
      ap_status_t stat;
  
      if (on)
          one = 1;
      else
          one = 0;
  
      if (opt & APR_SO_KEEPALIVE) {
          if (setsockopt(sock->socketdes, SOL_SOCKET, SO_KEEPALIVE, (void 
*)&one, sizeof(int)) == -1) {
              return errno;
          }
      }
      if (opt & APR_SO_DEBUG) {
          if (setsockopt(sock->socketdes, SOL_SOCKET, SO_DEBUG, (void *)&one, 
sizeof(int)) == -1) {
              return errno;
          }
      }
      if (opt & APR_SO_REUSEADDR) {
          if (setsockopt(sock->socketdes, SOL_SOCKET, SO_REUSEADDR, (void 
*)&one, sizeof(int)) == -1) {
              return errno;
          }
      }
      if (opt & APR_SO_NONBLOCK) {
          if (on) {
              if ((stat = soblock(sock->socketdes)) != APR_SUCCESS) 
                  return stat;
          }
          else {
              if ((stat = sononblock(sock->socketdes)) != APR_SUCCESS)
                  return stat;
          }
      }
      if (opt & APR_SO_LINGER) {
          li.l_onoff = on;
          li.l_linger = MAX_SECS_TO_LINGER;
          if (setsockopt(sock->socketdes, SOL_SOCKET, SO_LINGER, (char *) &li, 
sizeof(struct linger)) == -1) {
              return errno;
          }
      }
      return APR_SUCCESS;
  }         
  
  ap_status_t ap_gethostname(ap_context_t *cont, char *buf, int len)
  {
      if (gethostname(buf, len) == -1)
          return errno;
      else
          return APR_SUCCESS;
  }
  
  ap_status_t ap_get_remote_hostname(struct socket_t *sock, char **name)
  {
      (*name) = ap_pstrdup(sock->cntxt, sock->remote_hostname);
      if (*name) {
          return APR_SUCCESS;
      }
      return APR_ENOMEM;
  }
  
  
  
  
  

Reply via email to