home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ldapsdk.zip / libraries / libldap / os-local.c < prev    next >
C/C++ Source or Header  |  2000-10-31  |  5KB  |  230 lines

  1. /* $OpenLDAP: pkg/ldap/libraries/libldap/os-local.c,v 1.5.2.7 2000/10/30 17:36:25 kurt Exp $ */
  2. /*
  3.  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
  4.  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  5.  */
  6. /*  Portions
  7.  *  Copyright (c) 1995 Regents of the University of Michigan.
  8.  *  All rights reserved.
  9.  *  Copyright (c) 1999 PADL Software Pty Ltd.
  10.  *  os-ip.c -- platform-specific domain socket code
  11.  */
  12.  
  13.  
  14. #include "portable.h"
  15.  
  16. #ifdef LDAP_PF_LOCAL
  17.  
  18. #include <stdio.h>
  19.  
  20. #include <ac/stdlib.h>
  21.  
  22. #include <ac/errno.h>
  23. #include <ac/socket.h>
  24. #include <ac/string.h>
  25. #include <ac/time.h>
  26. #include <ac/unistd.h>
  27.  
  28. /* XXX non-portable */
  29. #include <sys/stat.h>
  30.  
  31. #ifdef HAVE_IO_H
  32. #include <io.h>
  33. #endif /* HAVE_IO_H */
  34.  
  35. #include "ldap-int.h"
  36. #include "ldap_defaults.h"
  37.  
  38. /* int ldap_int_tblsize = 0; */
  39.  
  40. #define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
  41. do { \
  42.     ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
  43. } while(0)
  44.  
  45. static void
  46. ldap_pvt_set_errno(int err)
  47. {
  48.     errno = err;
  49. }
  50.  
  51. static int
  52. ldap_pvt_ndelay_on(LDAP *ld, int fd)
  53. {
  54.     oslocal_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
  55.     return ber_pvt_socket_set_nonblock( fd, 1 );
  56. }
  57.    
  58. static int
  59. ldap_pvt_ndelay_off(LDAP *ld, int fd)
  60. {
  61.     oslocal_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
  62.     return ber_pvt_socket_set_nonblock( fd, 0 );
  63. }
  64.  
  65. static ber_socket_t
  66. ldap_pvt_socket(LDAP *ld)
  67. {
  68.     ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
  69.     oslocal_debug(ld, "ldap_new_socket: %d\n",s,0,0);
  70.     return ( s );
  71. }
  72.  
  73. static int
  74. ldap_pvt_close_socket(LDAP *ld, int s)
  75. {
  76.     oslocal_debug(ld, "ldap_close_socket: %d\n",s,0,0);
  77.     return tcp_close(s);
  78. }
  79.  
  80. #undef TRACE
  81. #define TRACE do { \
  82.     oslocal_debug(ld, \
  83.         "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
  84.         s, \
  85.         errno, \
  86.         STRERROR(errno) ); \
  87. } while( 0 )
  88.  
  89. /*
  90.  * check the socket for errors after select returned.
  91.  */
  92. static int
  93. ldap_pvt_is_socket_ready(LDAP *ld, int s)
  94. {
  95.     oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
  96.  
  97. #if defined( notyet ) /* && defined( SO_ERROR ) */
  98. {
  99.     int so_errno;
  100.     int dummy = sizeof(so_errno);
  101.     if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
  102.         == AC_SOCKET_ERROR )
  103.     {
  104.         return -1;
  105.     }
  106.     if ( so_errno ) {
  107.         ldap_pvt_set_errno(so_errno);
  108.         TRACE;
  109.         return -1;
  110.     }
  111.     return 0;
  112. }
  113. #else
  114. {
  115.     /* error slippery */
  116.     struct sockaddr_un sa;
  117.     char ch;
  118.     int dummy = sizeof(sa);
  119.     if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
  120.         == AC_SOCKET_ERROR )
  121.     {
  122.         /* XXX: needs to be replace with ber_stream_read() */
  123.         read(s, &ch, 1);
  124.         TRACE;
  125.         return -1;
  126.     }
  127.     return 0;
  128. }
  129. #endif
  130.     return -1;
  131. }
  132. #undef TRACE
  133.  
  134. static int
  135. ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
  136. {
  137.     struct timeval    tv, *opt_tv=NULL;
  138.     fd_set        wfds, *z=NULL;
  139.  
  140.     if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
  141.         tv.tv_usec = opt_tv->tv_usec;
  142.         tv.tv_sec = opt_tv->tv_sec;
  143.     }
  144.  
  145.     oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
  146.             s, opt_tv ? tv.tv_sec : -1L, async);
  147.  
  148.     if ( ldap_pvt_ndelay_on(ld, s) == -1 )
  149.         return ( -1 );
  150.  
  151.     if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
  152.         != AC_SOCKET_ERROR )
  153.     {
  154.         if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
  155.             return ( -1 );
  156.         }
  157.         return ( 0 );
  158.     }
  159.  
  160.     if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
  161.         return ( -1 );
  162.     }
  163.     
  164. #ifdef notyet
  165.     if ( async ) return ( -2 );
  166. #endif
  167.  
  168.     FD_ZERO(&wfds);
  169.     FD_SET(s, &wfds );
  170.  
  171.     if ( select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL)
  172.         == AC_SOCKET_ERROR )
  173.     {
  174.         return ( -1 );
  175.     }
  176.  
  177.     if ( FD_ISSET(s, &wfds) ) {
  178.         if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
  179.             return ( -1 );
  180.         if ( ldap_pvt_ndelay_off(ld, s) == -1 )
  181.             return ( -1 );
  182.         return ( 0 );
  183.     }
  184.     oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
  185.     ldap_pvt_set_errno( ETIMEDOUT );
  186.     return ( -1 );
  187. }
  188.  
  189. int
  190. ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
  191. {
  192.     struct sockaddr_un    server;
  193.     ber_socket_t        s;
  194.     int            rc;
  195.  
  196.     oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
  197.  
  198.     s = ldap_pvt_socket( ld );
  199.     if ( s == AC_SOCKET_INVALID ) {
  200.         return -1;
  201.     }
  202.  
  203.     if ( path == NULL || path[0] == '\0' ) {
  204.         path = LDAPI_SOCK;
  205.     } else {
  206.         if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
  207.             ldap_pvt_set_errno( ENAMETOOLONG );
  208.             return -1;
  209.         }
  210.     }
  211.  
  212.     oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
  213.  
  214.     memset( &server, '\0', sizeof(server) );
  215.     server.sun_family = AF_LOCAL;
  216.     strcpy( server.sun_path, path );
  217.  
  218.     rc = ldap_pvt_connect(ld, s, &server, async);
  219.  
  220.     if (rc == 0) {
  221.         ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
  222.     } else {
  223.         ldap_pvt_close_socket(ld, s);
  224.     }
  225.     return rc;
  226. }
  227. #else
  228. static int dummy;
  229. #endif /* LDAP_PF_LOCAL */
  230.