]> git.sur5r.net Git - openldap/commitdiff
ITS#4707 added new ldap_init_fd() API
authorHoward Chu <hyc@openldap.org>
Mon, 12 Feb 2007 04:20:24 +0000 (04:20 +0000)
committerHoward Chu <hyc@openldap.org>
Mon, 12 Feb 2007 04:20:24 +0000 (04:20 +0000)
include/ldap_pvt.h
libraries/libldap/open.c

index dc6096d46071f855fa113b513fc5a01389c47cde..bc9c90662c36afd6a98ffe3ad56c28df9bea9194 100644 (file)
@@ -27,6 +27,7 @@ LDAP_BEGIN_DECL
 #define LDAP_PROTO_TCP 1 /* ldap://  */
 #define LDAP_PROTO_UDP 2 /* reserved */
 #define LDAP_PROTO_IPC 3 /* ldapi:// */
+#define LDAP_PROTO_EXT 4 /* user-defined socket/sockbuf */
 
 LDAP_F ( int )
 ldap_pvt_url_scheme2proto LDAP_P((
@@ -236,6 +237,8 @@ ldap_get_message_ber LDAP_P((
 /* open */
 LDAP_F (int) ldap_open_internal_connection LDAP_P((
        struct ldap **ldp, ber_socket_t *fdp ));
+LDAP_F (int) ldap_init_fd LDAP_P((
+       ber_socket_t fd, int proto, LDAP_CONST char *url, struct ldap **ldp ));
 
 /* search.c */
 LDAP_F( int ) ldap_pvt_put_filter LDAP_P((
index 5bf762a4e65d61f3a7ed50e745c37efc63716b82..9939a87df581b288da467b392099e75ed0e6c53a 100644 (file)
@@ -239,6 +239,95 @@ ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
        return LDAP_SUCCESS;
 }
 
+int
+ldap_init_fd(
+       ber_socket_t fd,
+       int proto,
+       LDAP_CONST char *url,
+       LDAP **ldp
+)
+{
+       int rc;
+       LDAP *ld;
+       LDAPConn *conn;
+
+       *ldp = NULL;
+       rc = ldap_create( &ld );
+       if( rc != LDAP_SUCCESS )
+               return( rc );
+
+       if (url != NULL) {
+               rc = ldap_set_option(ld, LDAP_OPT_URI, url);
+               if ( rc != LDAP_SUCCESS ) {
+                       ldap_ld_free(ld, 1, NULL, NULL);
+                       return rc;
+               }
+       }
+
+       /* Attach the passed socket as the LDAP's connection */
+       conn = ldap_new_connection( ld, NULL, 1, 0, NULL);
+       if( conn == NULL ) {
+               ldap_unbind_ext( ld, NULL, NULL );
+               return( LDAP_NO_MEMORY );
+       }
+       ber_sockbuf_ctrl( conn->lconn_sb, LBER_SB_OPT_SET_FD, &fd );
+       ld->ld_defconn = conn;
+       ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
+
+       switch( proto ) {
+       case LDAP_PROTO_TCP:
+#ifdef LDAP_DEBUG
+               ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
+                       LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
+#endif
+               ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
+                       LBER_SBIOD_LEVEL_PROVIDER, NULL );
+               break;
+
+#ifdef LDAP_CONNECTIONLESS
+       case LDAP_PROTO_UDP:
+#ifdef LDAP_DEBUG
+               ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
+                       LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
+#endif
+               ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
+                       LBER_SBIOD_LEVEL_PROVIDER, NULL );
+               ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
+                       LBER_SBIOD_LEVEL_PROVIDER, NULL );
+               break;
+#endif /* LDAP_CONNECTIONLESS */
+
+       case LDAP_PROTO_IPC:
+#ifdef LDAP_DEBUG
+               ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
+                       LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
+#endif
+               ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
+                       LBER_SBIOD_LEVEL_PROVIDER, NULL );
+               break;
+
+       case LDAP_PROTO_EXT:
+               /* caller must supply sockbuf handlers */
+               break;
+
+       default:
+               ldap_unbind_ext( ld, NULL, NULL );
+               return LDAP_PARAM_ERROR;
+       }
+
+#ifdef LDAP_DEBUG
+       ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
+               INT_MAX, (void *)"ldap_" );
+#endif
+
+       /* Add the connection to the *LDAP's select pool */
+       ldap_mark_select_read( ld, conn->lconn_sb );
+       ldap_mark_select_write( ld, conn->lconn_sb );
+       
+       *ldp = ld;
+       return LDAP_SUCCESS;
+}
+
 int
 ldap_int_open_connection(
        LDAP *ld,