InfoDirectory使用手引書
目次 索引 前ページ次ページ

第3部 SDK編> 第5章 アプリケーション開発環境(LDAP C API)> 5.5 サンプルプログラム> 5.5.3 サンプルプログラムの紹介

5.5.3.1 非同期型のエントリ検索

 非同期型のエントリ検索において、検索結果を1個ずつ受信する場合のサンプルプログラムを以下に示します。

[asearch.c]

  /*
  * Copyright (c) 2001. Fujitsu Limited. All rights reserved. 
  *
  * Search the directory for all entry.
  * 
  */ 
  
  #include    <sys/types.h>  
  #include    <stdio.h>  
  #include    <stdlib.h>  
  #include    <string.h>  
  #if defined(unix)
  #include    <time.h>  
  #include    <unistd.h>
  #elif defined(_WIN32)
  #include    <winsock.h>
  #endif
  #include    "examples.h"  
  
  unsigned long    global_counter = 0;  
  
  char   *host   = "localhost";  
  int    port    = LDAP_PORT;  
  char   *s_base = "o=Fujitsu,c=JP";  
  int    scope   = LDAP_SCOPE_SUBTREE;  
  char   *filter = "(objectclass=*)";  
  
  
  /*-------------------------------------------------------------*/  
  /*  
  * Perform other work while polling for results. This doesn't do anything  
  * useful, but it could.  
  */  
  static void do_other_work()
  {
      extern unsigned  long global_counter;  
      global_counter++;  
  }  
  
  
  /*-------------------------------------------------------------*/  
  /*  
  * main program  
  */  
  int main(  
      int    argc,  
      char   *argv[]  
  )
  {  
      LDAP            *ld;  
      LDAPMessage     *result;  
      LDAPMessage     *entry;  
      int             msgid;  
      int             rtn;  
      int             optdata;  
      struct timeval  zerotime;  
      int             finished;  
      int             errcode;  
      char            *errmsg;  
      char            *mached;  
      int             num_entries = 0;  
  
      extern char     *host;  
      extern int      port;  
      extern char     *s_base;  
      extern int      scope;  
      extern char     *filter;  
  /*----------------------------------------------------------*/  
  
      printf( "%s is start\n", argv[0] );  
  
      printf( "%s: ldap_init( \"%s\", %d )\n", argv[0], host, port );  
      ld = ldap_init( host, port );  
      if ( ld == NULL ) {  
          perror( "ldap_init" );  
          printf( "%s is abnormal end\n", argv[0] );  
          exit( 1 );  
      }  
  
      /* set option ( LDAP_DEREF_ALWAYS ) */
      optdata = LDAP_DEREF_ALWAYS;
      printf( "%s: ldap_set_option( LDAP_OPT_DEREF, %d )\n", argv[0], optdata);
      rtn = ldap_set_option(ld, LDAP_OPT_DEREF, (void *)(&optdata));
      if (rtn != 0 ) {
          do_error_msg( "ldap_set_option", rtn, NULL, NULL );
          do_unbind( ld, argv );
          printf( "%s is abnormal end\n", argv[0] );
          exit( 1 ); 
      }  
  
      /* set option ( LDAP_VERSION3 ) */
      optdata = LDAP_VERSION3; 
      printf( "%s: ldap_set_option( LDAP_OPT_PROTOCOL_VERSION, %d )\n",argv[0],optdata );
      rtn = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, (void *)(&optdata));
      if ( rtn != 0 ) {
          do_error_msg( "ldap_set_option", rtn, NULL, NULL );
          do_unbind( ld, argv );
          printf( "%s is abnormal end\n", argv[0] );
          exit( 1 );
      }  
  
      /* simple authentication */
      printf( "%s: ldap_simple_bind_s( anonymous )\n", argv[0] );
      rtn = ldap_simple_bind_s( ld, NULL, NULL );
      if ( rtn != LDAP_SUCCESS ) {
          do_error_msg( "ldap_simple_bind_s", rtn, NULL, NULL ); 
          do_unbind( ld, argv );  
          printf( "%s is abnormal end\n", argv[0] );  
          exit( 1 );  
      }  
  
      /* entry search */
      printf( "%s: ldap_search( \"%s\", %d, \"%s\" )\n", argv[0], s_base,scope,filter );
      msgid = ldap_search( ld, s_base, scope, filter, NULL, 0 );
      if ( msgid < 0 ) {
          do_get_ldaperror( ld, &errcode, &errmsg );
          do_error_msg( "ldap_search", errcode, NULL, errmsg );
          do_unbind( ld, argv );
          printf( "%s is abnormal end\n", argv[0] );
          exit( 1 );
      }
  
      /* Loop, polling for results until finished */
      finished = 0;
      while ( finished == 0 ) {
          printf( "%s: ldap_result( )\n", argv[0] );
          result = NULL;
          zerotime.tv_sec = zerotime.tv_usec = 0L;
          rtn = ldap_result( ld, msgid, LDAP_MSG_ONE, &zerotime, &result);
          switch ( rtn ) {
              case -1:
                  /* some error occurred */
                  if ( result != NULL ) {
                      errcode = NULL;
                      mached = NULL;
                      errmsg = NULL;
                      ldap_parse_result( ld, result, &errcode, &mached,&errmsg,0, 0, 0 );
  
                      do_error_msg( "ldap_result", errcode, mached,errmsg );
                  }
                  printf( "%s: Entry search error.\n", argv[0] );
                  do_unbind( ld, argv );
                  printf( "%s is abnormal end\n", argv[0] );
                  exit( 1 );
              case 0:
                  /* Timeout was exceeded.
                     No entries are ready for retrieval. */
                  if ( result != NULL ) {
                      ldap_msgfree( result );
                  }
  #if defined(unix)
                  sleep(1);
  #elif defined(_WIN32)
                  Sleep(1*1000);
  #endif
                  break;
              default:
                  /*
                   * Either an entry is ready for retrieval, or all entries
                   * have been retrieved.
                   */
                  entry = ldap_first_entry( ld, result );
                  if ( entry == NULL ) {
                      /* All done */
                      finished = 1;
                      if ( result != NULL ) {
                          ldap_msgfree( result );
                      }
                      continue;
                  }
                  num_entries += print_entry( ld, result, 1 );
          }
          /* Do other work here while you are waiting... */
          do_other_work();
      }
  
      /* All done. Print a summary. */
      printf( "%s: %d entries retrieved. I counted to %ld while I waswaiting.\n",argv[0], num_entries, global_counter);
  
      do_unbind( ld, argv );
      printf( "%s is normal end\n", argv[0] );
      exit( 0 );
  }

目次 索引 前ページ次ページ

All Rights Reserved, Copyright(C) 富士通株式会社 2005