Back to index

lightning-sunbird  0.9+nobinonly
nsprio.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * A version of asearch.c that uses NSPR (Netscape Portable Runtime) I/O.
00040  *
00041  * Search the directory for all people whose surname (last name) is
00042  * "Jensen".  Since the "sn" attribute is a caseignorestring (cis), case
00043  * is not significant when searching.
00044  *
00045  * Instead of calling the synchronous ldap_search_s() routine, we call
00046  * the asynchronous routine ldap_search() and poll for results using
00047  * ldap_result().
00048  *
00049  */
00050 
00051 #include "examples.h"
00052 #include <nspr.h>
00053 #include <ldappr.h>
00054 
00055 static void do_other_work();
00056 
00057 unsigned long global_counter = 0;
00058 
00059 int
00060 main( int argc, char **argv )
00061 {
00062     LDAP             *ld;
00063     LDAPMessage             *result, *e;
00064     BerElement              *ber;
00065     char             *host, *a, *dn;
00066     char             **vals;
00067     int                     i;
00068     int                     rc;
00069     int                     finished;
00070     int                     msgid;
00071     int                     num_entries = 0;
00072     struct timeval   zerotime;
00073 
00074     if ( argc > 1 ) {
00075        host = argv[1];
00076     } else {
00077        host = MY_HOST;
00078     }
00079 
00080     zerotime.tv_sec = zerotime.tv_usec = 0L;
00081 
00082     if ( prldap_install_routines( NULL, 1 /* shared */ ) != LDAP_SUCCESS ) {
00083        ldap_perror( NULL, "prldap_install_routines" );
00084        return( 1 );
00085     }
00086 
00087     /* get a handle to an LDAP connection */
00088     if ( (ld = ldap_init( host, MY_PORT )) == NULL ) {
00089        perror( host );
00090        return( 1 );
00091     }
00092 
00093     /* authenticate to the directory as nobody */
00094     if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
00095        ldap_perror( ld, "ldap_simple_bind_s" );
00096        return( 1 );
00097     }
00098     /* search for all entries with surname of Jensen */
00099     if (( msgid = ldap_search( ld, MY_SEARCHBASE, LDAP_SCOPE_SUBTREE,
00100            MY_FILTER, NULL, 0 )) < 0 ) {
00101        ldap_perror( ld, "ldap_search" );
00102        return( 1 );
00103     }
00104 
00105     /* Loop, polling for results until finished */
00106     finished = 0;
00107     while ( !finished ) {
00108        /*
00109         * Poll for results.   We call ldap_result with the "all" argument
00110         * set to LDAP_MSG_ONE.  This causes ldap_result() to return exactly one
00111         * entry if at least one entry is available.  This allows us to
00112         * display the entries as they are received.
00113         */
00114        result = NULL;
00115        rc = ldap_result( ld, msgid, LDAP_MSG_ONE, &zerotime, &result );
00116        switch ( rc ) {
00117        case -1:
00118            /* some error occurred */
00119            ldap_perror( ld, "ldap_result" );
00120            return( 1 );
00121        case 0:
00122            /* Timeout was exceeded.  No entries are ready for retrieval. */
00123            if ( result != NULL ) {
00124               ldap_msgfree( result );
00125            }
00126            break;
00127        default:
00128            /*
00129             * Either an entry is ready for retrieval, or all entries have
00130             * been retrieved.
00131             */
00132            if (( e = ldap_first_entry( ld, result )) == NULL ) {
00133               /* All done */
00134               finished = 1;
00135               if ( result != NULL ) {
00136                   ldap_msgfree( result );
00137               }
00138               continue;
00139            }
00140            /* for each entry print out name + all attrs and values */
00141            num_entries++;
00142            if (( dn = ldap_get_dn( ld, e )) != NULL ) {
00143               printf( "dn: %s\n", dn );
00144               ldap_memfree( dn );
00145            }
00146            for ( a = ldap_first_attribute( ld, e, &ber );
00147                   a != NULL; a = ldap_next_attribute( ld, e, ber ) ) {
00148               if (( vals = ldap_get_values( ld, e, a )) != NULL ) {
00149                   for ( i = 0; vals[ i ] != NULL; i++ ) {
00150                      printf( "%s: %s\n", a, vals[ i ] );
00151                   }
00152                   ldap_value_free( vals );
00153               }
00154               ldap_memfree( a );
00155            }
00156            if ( ber != NULL ) {
00157               ber_free( ber, 0 );
00158            }
00159            printf( "\n" );
00160            ldap_msgfree( result );
00161        }
00162        /* Do other work here while you are waiting... */
00163        do_other_work();
00164     }
00165 
00166     /* All done.  Print a summary. */
00167     printf( "%d entries retrieved.  I counted to %ld "
00168            "while I was waiting.\n", num_entries,
00169            global_counter );
00170     ldap_unbind( ld );
00171     return( 0 );
00172 }
00173 
00174 
00175 /*
00176  * Perform other work while polling for results.  This doesn't do anything
00177  * useful, but it could.
00178  */
00179 static void
00180 do_other_work()
00181 {
00182     global_counter++;
00183 }