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

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

5.5.3.4 非同期型のエントリ追加

 非同期型でエントリを追加する場合のサンプルプログラムを以下に示します。

 このサンプルプログラムでは、人のエントリを追加しています。

■[add.c]

  /*
   * Copyright (c) 1998. Fujitsu Limited. All rights reserved.
   *
   * Add a new entry to the directory.
   *
   */
  
  #include    <sys/types.h>
  #include    <stdio.h>
  #include    <stdlib.h>
  #include    <string.h>
  #if defined(unix)
  #include    <time.h>
  #elif defined(_WIN32)
  #include    <winsock.h>
  #endif
  #include    "examples.h"
  #define     NMODS    6
  
  unsigned long    global_counter = 0;
  char    *host     = "localhost";
  int     port      = LDAP_PORT;
  char    *bind_dn  = "cn=admin,o=Fujitsu,c=JP";
  char    *passwd   = "admin123";
  char    *add_dn   = "uid=User1,o=Fujitsu,c=JP";
  
  
  char *objectclass_values[] = {
      "top",
      "person",
      "organizationalPerson",
      "inetOrgPerson",
      NULL
  };
  
  char *cn_values[] = {
      "T Fujitsu",
      "Tarou Fujitsu",
      NULL
  };
  
  char *sn_values[] = {
      "Fujitsu",
      NULL
  };
  
  char *givenname_values[] = {
      "Tarou",
      NULL
  };
  
  char *telephonenumber_values[] = {
      "+83-12-345-6789",
      NULL
  };
  
  char *uid_values[] = {
      "User1",
      NULL
  };
  
  
  /*-------------------------------------------------------------*/
  /*
   * Free a mods array.
   */
  static void free_mods(
      LDAPMod    *mods[]
  )
  {
      int i;
      for ( i = 0; i < NMODS; i++ ) {
          if ( mods[ i ] == NULL ) {
              break;
          }
          free( mods[ i ] );
      }
      free( mods );
  }
  
  
  /*-------------------------------------------------------------*/
  /*
   * 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++;
  }
  
  /*-------------------------------------------------------------*/
  int main(
      int   argc,
      char  *argv[]
  )
  {  
      LDAP              *ld;  
      LDAPMessage       *result; 
      int               i, j;  
      int               rtn;  
      int               msgid;  
      int               finished;  
      struct timeval    zerotime;  
      LDAPMod           **mods;  
      int               errcode;  
      char              *errmsg;  
      char              *mached;  
      int               optdata;  
  
      extern char       *host;  
      extern int        port;  
      extern char       *bind_dn;  
      extern char       *passwd;  
      extern char       *add_dn;  
      extern char       *objectclass_values[];  
      extern char       *cn_values[];  
      extern char       *sn_values[];  
      extern char       *givenname_values[];  
      extern char       *telephonenumber_values[];  
      extern char       *uid_values[];  
  /*----------------------------------------------------------*/  
  
      printf( "%s is start\n", argv[0] );  
  
      /* get a handle to an LDAP connection */  
      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_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 authenticate to the directory as the Directory Manager */  
      printf( "%s: ldap_simple_bind_s( \"%s\", \"******\" )\n", argv[0], bind_dn);  
      rtn = ldap_simple_bind_s( ld, bind_dn, passwd );  
      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 );  
      }  
  
      /* Construct the array of values to add */  
      mods = ( LDAPMod ** )calloc(( NMODS + 1 ), sizeof( LDAPMod * ));  
      if ( mods == NULL ) {  
          fprintf( stderr, "%s: Cannot allocate memory for mods array\n", argv[0]);  
          do_unbind( ld, argv );  
          printf( "%s is abnormal end\n", argv[0] );  
          exit( 1 );  
      }  
  
      for ( i = 0; i < NMODS; i++ ) {  
          mods[ i ] = ( LDAPMod * )calloc( 1, sizeof( LDAPMod ));  
          if ( mods[ i ] == NULL ) {  
              fprintf( stderr, "%s: Cannot allocate memory for mods element\n",argv[0] );  
              free_mods( mods );  
              do_unbind( ld, argv );  
              printf( "%s is abnormal end\n", argv[0] );  
              exit( 1 );  
          }  
      }  
  
      mods[ 0 ]->mod_op     = 0;  
      mods[ 0 ]->mod_type   = "objectclass";  
      mods[ 0 ]->mod_values = objectclass_values;  
      mods[ 1 ]->mod_op     = 0;  
      mods[ 1 ]->mod_type   = "cn";  
      mods[ 1 ]->mod_values = cn_values;  
      mods[ 2 ]->mod_op     = 0;  
      mods[ 2 ]->mod_type   = "sn";  
      mods[ 2 ]->mod_values = sn_values;  
      mods[ 3 ]->mod_op     = 0;  
      mods[ 3 ]->mod_type   = "givenname";  
      mods[ 3 ]->mod_values = givenname_values;  
      mods[ 4 ]->mod_op     = 0;  
      mods[ 4 ]->mod_type   = "telephonenumber";  
      mods[ 4 ]->mod_values = telephonenumber_values;  
      mods[ 5 ]->mod_op     = 0;  
      mods[ 5 ]->mod_type   = "uid";  
      mods[ 5 ]->mod_values = uid_values;  
      mods[ 6 ] = NULL;  
  
      /* Initiate the add operation */  
      printf( "%s: ldap_add( \"%s\" )\n", argv[0], add_dn );  
      msgid = ldap_add( ld, add_dn, mods );  
      if ( msgid < 0 ) {  
          do_get_ldaperror( ld, &errcode, &errmsg );  
          do_error_msg( "ldap_add", errcode, NULL, errmsg );  
          do_unbind( ld, argv );  
          free_mods( mods );  
          printf( "%s is abnormal end\n", argv[0] );  
          exit( 1 );  
      }  
  
      /* Poll for the result */  
      finished = 0;  
      while ( !finished ) {  
          printf( "%s: ldap_result( )\n", argv[0] );  
          result = NULL;  
          zerotime.tv_sec = zerotime.tv_usec = 0L;  
          rtn = ldap_result( ld, msgid, LDAP_MSG_ALL, &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 added error.\n", argv[0] );  
                  do_unbind( ld, argv );  
                  free_mods( mods );  
                  printf( "%s is abnormal end\n", argv[0] );  
                  exit( 1 );  
              case 0:  
                  /* Timeout was exceeded. No entries are ready for retrieval */  
  #if defined(unix)
                  sleep(1);
  #elif defined(_WIN32)
                  Sleep(1*1000);
  #endif
                  break;  
              default:  
                  /* Should be finished here */  
                  finished = 1;  
                  errcode = NULL; 
                  mached = NULL;  
                  errmsg = NULL;  
                  rtn = ldap_parse_result( ld, result, &errcode, &mached, &errmsg,0,0, 0 );  
                  if ( errcode == LDAP_SUCCESS ) {  
                      printf( "%s: Entry added successfully." "I counted to %ld whilewaiting.\n", argv[0], global_counter);  
                  } 
                  else {  
                      do_error_msg( "ldap_result", errcode, mached, errmsg );  
                      printf( "%s: Error while adding entry\n", argv[0]);  
                  }  
                  ldap_msgfree( result );  
          }  
          do_other_work();  
      }
      do_unbind( ld, argv );  
      free_mods( mods );  
      printf( "%s is normal end\n", argv[0] );  
      exit( 0 );  
  }

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

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