atnds_test.c

This extended example shows how to convert AFTN address to the corresponding AMHS form, and vice versa. The example also makes use of DSAPI to connect to the directory and manipulate directory data.

To compile this example on Unix machines:

  cc -c -I/opt/isode/include/isode/ds/atnds/ -I/opt/isode/include
        -I/opt/isode/include/h
  

To link this example on Unix machines:

  cc -o atnds_test atnds_test.o -L/opt/isode/lib \
      -latnds -ldua -lisode -libase -lmdldap -lmdlber -lssl -lcrypto
  

00001 /*  Copyright (c) 2004-2007, Isode Limited, London, England.
00002  *  All rights reserved.
00003  *                                                                       
00004  *  Acquisition and use of this software and related materials for any      
00005  *  purpose requires a written licence agreement from Isode Limited,
00006  *  or a written licence from an organisation licenced by Isode Limited
00007  *  to grant such a licence.
00008  *
00009  */
00010 
00011 /*
00012  * atnds_test.c
00013  *
00014  * Test functions of ATNds C library.
00015  *
00016  */
00017 
00018 /* $RCSfile: atnds_test.c,v $ $Revision: 1.15 $ $Date: 2007/05/15 12:38:01 $ $State: Exp $
00019  *
00020  * $Log: atnds_test.c,v $
00021  * Revision 1.15  2007/05/15 12:38:01  dbw
00022  * T070515D - Update/Add copyright
00023  *
00024  * Revision 1.14  2007/05/10 10:43:55  ldv
00025  * T070509V - Remove the invalid variable dnbuf from error messages
00026  *
00027  * Revision 1.13  2007/04/05 12:41:46  brs
00028  * T070405A - Fix some warnings.
00029  *
00030  * Revision 1.12  2006/10/05 13:49:24  tc
00031  * T061005C - Replace use of free() with calls to ATNds_free().
00032  *
00033  * Revision 1.11  2006/03/17 15:02:55  cjr
00034  * T060317B - Fix leak in read_atn_user(), and return 0 if successful.
00035  *
00036  * Revision 1.10  2006/02/23 11:42:05  brs
00037  * T060222A - Include standard headers.
00038  *
00039  * Revision 1.9  2005/11/03 14:21:54  ldv
00040  * T051103D.T1 - Add new feature to display AMHS user DN entries, if they exist
00041  *
00042  * Revision 1.8  2005/09/05 16:18:47  ldv
00043  * T050825L - Use o=Isode-MD-Register. Accept AFTN addresses as program argument
00044  *
00045  * Revision 1.7  2005/07/21 10:50:19  dsm
00046  * T050720C - Modify layout so that doxygen can parse function calls
00047  *            correctly when cross-referencing to examples.
00048  *
00049  * Revision 1.6  2005/07/04 12:23:40  cjr
00050  * T050704A - Change result types to DS_Status.
00051  *
00052  * Revision 1.5  2005/05/25 15:45:34  dbw
00053  * T050525C - Add or update copyright for 2005
00054  *
00055  * Revision 1.4  2005/05/17 11:04:35  ldv
00056  * T050517E - Remove references to CAAS AFTN and XF AFTN
00057  *
00058  * Revision 1.3  2005/05/16 14:52:00  dsm
00059  * T050516P - Remove references to Isode internal header files.
00060  *
00061  * Revision 1.2  2005/05/13 10:49:50  dsm
00062  * T050513C - Test new and updated conversion functionality.
00063  *
00064  * Revision 1.1  2005/04/12 16:09:48  dsm
00065  * T050412V - Initial revision of ATN DS library.
00066  *
00067  *
00068  */
00069 
00070 #include <stdio.h>
00071 #include <string.h>
00072 #include <isode/ds/atnds/atnds.h>
00073 
00084 DS_Session *ds;
00085 
00086 static int read_atn_user (DS_DN *user_dn);
00087 
00088 int main( int argc, char *argv[] )
00089 {
00090 
00091     const char *addr = "Internet=localhost+19999";
00092     const char *registry_dn_str = "o=Isode-MD-Register";
00093     DS_DN *registry_dn;
00094     DS_DN *user_dn;
00095     char *aftn_addr;
00096     const char *caas_aftn_addr = "LFCIZPZX";
00097     const char *xf_aftn_addr = "ESMANUZX";
00098     char *orbuf;
00099     size_t orbuf_len;
00100     DS_Status status;
00101     char *dnbuf;
00102     size_t dnbuf_len;
00103     char aftnbuf[9];
00104 
00105     /* Initialize the DUA library. */
00106     printf( "\nTEST: initializing DUA library %s.\n", addr );
00107     printf( "----------------------------------------------------------------\n" );
00108 
00109     status = DS_Initialize();
00110     if ( status != DS_E_NOERROR ) {
00111         printf( "FAIL: couldn't initialize DUA library.\n" );
00112         return status;
00113     }
00114 
00115     /* Attempt to bind." */
00116     printf( "\nTEST: bind (to %s)\n", addr );
00117     printf( "----------------------------------------------------------------\n" );
00118 
00119     status = DS_BindSync_Anonymous( addr, &ds );
00120     if ( status != DS_E_NOERROR ) {
00121         printf( "FAIL: couldn't bind to directory.\n" );
00122         return status;
00123     }
00124 
00125     printf( "SUCCESS: bound to directory\n" );
00126 
00127 
00128     /* Convert string registry DN to DSAPI DN structure. */
00129     status = DS_String2DN( registry_dn_str, &registry_dn );
00130     if ( status != DS_E_NOERROR ) {
00131 
00132         printf ( "FAIL: DN conversion failed (%s).\n", registry_dn_str );
00133         return DS_E_BADDN;
00134 
00135     }
00136 
00137     /* Attempt to convert an AFTN address using CAAS. */
00138     if ( argc > 1 ) {
00139 
00140         aftn_addr = argv[1];
00141         if ( strlen(aftn_addr) != 8 ) {
00142             printf ( "FAIL: AFTN address is not 8 characters long (%s).\n", aftn_addr );
00143             return DS_E_BADPARAM;
00144         }
00145 
00146         printf( "\nTEST: convert AFTN to X.400 (%s)\n", aftn_addr );
00147         status = ATNds_AFTN2AMHS
00148                 ( ds, registry_dn, aftn_addr, &orbuf, &orbuf_len, &user_dn );
00149 
00150     } else {
00151 
00152         printf( "\nTEST: convert AFTN to X.400 (CAAS) (%s)\n", caas_aftn_addr );
00153         status = ATNds_AFTN2AMHS
00154                 ( ds, registry_dn, caas_aftn_addr, &orbuf, &orbuf_len, &user_dn );
00155 
00156     }
00157     printf( "----------------------------------------------------------------\n" );
00158  
00159     if ( status != DS_E_NOERROR ) {
00160 
00161         switch ( status ) {
00162         
00163         case DS_E_NOTFOUND:
00164 
00165             printf
00166                 ( "FAIL: conversion data not found in the ATN directory.\n" );
00167             
00168             return status;
00169             
00170 
00171         default:
00172 
00173             printf ( "FAIL: conversion failed (%d - no error message available).\n", status );
00174             return status;
00175 
00176         }
00177 
00178     }
00179 
00180     printf( "SUCCESS: conversion succeeded.\n" );
00181     printf( "DETAIL: O/R address is %s\n\n", orbuf );
00182 
00183     if ( user_dn != NULL ) {
00184 
00185         status = DS_DN2String( user_dn, &dnbuf, &dnbuf_len );
00186         if ( status != DS_E_NOERROR ) {
00187             printf( "DETAIL: Failed to display aMHSUser entry DN.\n" );
00188         } else {
00189             printf( "DETAIL: aMHSUser entry DN is %s.\n", dnbuf );
00190             ATNds_free( dnbuf );
00191         }
00192 
00193         DS_DN_Delete( user_dn );
00194 
00195     } else {
00196         printf( "DETAIL: no aMHSUser entry DN could be computed.\n");
00197     }
00198 
00199     /* Perform reverse conversion. */
00200     printf( "\nTEST: performing reverse conversion\n(from %s)\n", orbuf );
00201     printf( "----------------------------------------------------------------\n" );
00202     
00203     status = ATNds_AMHS2AFTN
00204         ( ds, registry_dn, orbuf, aftnbuf, &user_dn );
00205     
00206     ATNds_free( ( void * ) orbuf );
00207 
00208     if ( status != DS_E_NOERROR ) {
00209 
00210         switch ( status ) {
00211         
00212         case DS_E_NOTFOUND:
00213 
00214             printf
00215                 ( "FAIL: conversion data not available.\n" );
00216             
00217             return status;
00218             
00219 
00220         default:
00221 
00222             printf ( "FAIL: conversion failed (no error message available).\n" );
00223             return status;
00224 
00225         }
00226 
00227     }
00228 
00229     printf( "SUCCESS: conversion succeeded.\n" );
00230     printf( "DETAIL: AFTN address is %s\n", aftnbuf );
00231 
00232     if ( user_dn != NULL ) {
00233 
00234         status = DS_DN2String( user_dn, &dnbuf, &dnbuf_len );
00235         if ( status != DS_E_NOERROR ) {
00236             printf( "DETAIL: Failed to display aMHSUser entry DN.\n" );
00237         } else {
00238             printf( "DETAIL: aMHSUser entry DN is %s.\n", dnbuf );
00239             ATNds_free( dnbuf );
00240         }
00241 
00242         read_atn_user( user_dn );
00243         
00244         DS_DN_Delete( user_dn );
00245 
00246     } else {
00247         printf( "DETAIL: no aMHSUser entry DN could be computed.\n");
00248     }
00249 
00250     /* Attempt to convert an AFTN address using XF. */
00251     printf( "\nTEST: convert AFTN to X.400 (XF) (%s)\n", xf_aftn_addr );
00252     printf( "----------------------------------------------------------------\n" );
00253     
00254     status = ATNds_AFTN2AMHS
00255         ( ds, registry_dn, xf_aftn_addr, &orbuf, &orbuf_len, &user_dn );
00256 
00257     if ( status != DS_E_NOERROR ) {
00258 
00259         switch ( status ) {
00260         
00261         case DS_E_NOTFOUND:
00262 
00263             printf
00264                 ( "FAIL: conversion data not found in the ATN directory.\n" );
00265             
00266             return status;
00267             
00268 
00269         default:
00270 
00271             printf ( "FAIL: conversion failed (no error message available).\n" );
00272             return status;
00273 
00274         }
00275 
00276     } 
00277 
00278     printf( "SUCCESS: conversion succeeded.\n" );
00279     printf( "DETAIL: O/R address is %s\n", orbuf );
00280 
00281     if ( user_dn != NULL ) {
00282 
00283         status = DS_DN2String( user_dn, &dnbuf, &dnbuf_len );
00284         if ( status != DS_E_NOERROR ) {
00285             printf( "DETAIL: Failed to display aMHSUser entry DN.\n" );
00286         } else {
00287             printf( "DETAIL: aMHSUser entry DN is %s.\n", dnbuf );
00288             ATNds_free( dnbuf );
00289         }
00290 
00291         read_atn_user( user_dn );
00292         
00293         DS_DN_Delete( user_dn );
00294 
00295     } else {
00296         printf( "DETAIL: no aMHSUser entry DN could be computed.\n");
00297     }
00298 
00299     /* Perform reverse conversion. */
00300     printf( "\nTEST: performing reverse conversion\n(from %s)\n", orbuf );
00301     printf( "----------------------------------------------------------------\n" );
00302     
00303     status = ATNds_AMHS2AFTN
00304         ( ds, registry_dn, orbuf, aftnbuf, &user_dn );
00305     
00306     ATNds_free( ( void * ) orbuf );
00307 
00308     if ( status != DS_E_NOERROR ) {
00309 
00310         switch ( status ) {
00311         
00312         case DS_E_NOTFOUND:
00313 
00314             printf
00315                 ( "FAIL: conversion data not available.\n" );
00316             
00317             return status;
00318             
00319 
00320         default:
00321 
00322             printf ( "FAIL: conversion failed (no error message available).\n" );
00323             return status;
00324 
00325         }
00326 
00327     }
00328 
00329     printf( "SUCCESS: conversion succeeded.\n" );
00330     printf( "DETAIL: AFTN address is %s\n", aftnbuf );
00331 
00332     if ( user_dn != NULL ) {
00333 
00334         status = DS_DN2String( user_dn, &dnbuf, &dnbuf_len );
00335         if ( status != DS_E_NOERROR ) {
00336             printf( "DETAIL: Failed to display aMHSUser entry DN.\n" );
00337         } else {
00338             printf( "DETAIL: aMHSUser entry DN is %s.\n", dnbuf );
00339             ATNds_free( dnbuf );
00340         }
00341 
00342         read_atn_user( user_dn );
00343         
00344         DS_DN_Delete( user_dn );
00345 
00346     } else {
00347         printf( "DETAIL: no aMHSUser entry DN could be computed.\n");
00348     }
00349 
00350     /* Clean up. */
00351     DS_DN_Delete( registry_dn );
00352 
00353     /* Unbind from the DSA */
00354     DS_UnbindSync( &ds );
00355 
00356     /* All tests done. */
00357     return 0;
00358 
00359 }
00360 
00361 
00362 static int
00363 read_atn_user ( DS_DN *user_dn ) {
00364 
00365     DS_Indication      *di;
00366     const DS_EntryList *el;
00367     const DS_Entry     *e;
00368     const DS_AttrList  *al;
00369     const DS_Attr      *a;
00370     DS_ErrorType type_p;
00371     int                 status;
00372 
00373 
00374     status = DS_ReadSync( ds, user_dn , NULL, NULL, &di );
00375     if ( status != DS_E_NOERROR ) {
00376         if ( di != NULL ) {
00377             DS_Indication_GetStatus ( di, &type_p ) ;
00378             DS_Indication_Delete ( di );
00379             if ( type_p == DS_E_NAME ) {
00380                 printf( "No DSA entry for this ATN User entry\n");
00381                 return 0;
00382             }
00383         }
00384         printf( "Failed to read ATN User entry (status=%d)\n", status);
00385         return 1;
00386     }
00387 
00388     status = DS_Indication_GetEntryList( di, &el );
00389     if ( status != DS_E_NOERROR ) {
00390         DS_Indication_Delete ( di );
00391         printf( "Failed to read ATN User entry Indication\n");
00392         return 2;
00393     }
00394 
00395     e = DS_EntryList_GetFirst( el );
00396 
00397     status = DS_Entry_GetAttrList( e, &al );
00398     if ( status != DS_E_NOERROR ) {
00399         DS_Indication_Delete ( di );
00400         printf( "Failed to read attribute list for ATN User entry\n");
00401         return 3;
00402     }
00403 
00404     for ( a = DS_AttrList_GetFirst( al );
00405           a != NULL;
00406           a = DS_AttrList_GetNext( a ) ) {
00407 
00408         const char           *name;
00409         const DS_AttrValList *vl;
00410         const DS_AttrVal     *v;
00411 
00412         DS_Attr_GetTypeName( a, &name );
00413         printf( "%s:\n", name );
00414         /* Don't free name */
00415 
00416         status = DS_Attr_GetValueList( a, &vl );
00417 
00418         for ( v = DS_AttrValList_GetFirst( vl );
00419               v != NULL;
00420               v = DS_AttrValList_GetNext( v ) ) {
00421 
00422             const char *val_str;
00423             size_t      val_len;
00424 
00425             status = DS_AttrVal_GetStringPointer( v, &val_str, &val_len );
00426             if ( status == DS_E_NOERROR )
00427                 printf( "\t%s\n", val_str );
00428             else
00429                 printf( "\t(No string value)\n");
00430             /* Don't free val_str */
00431 
00432         }
00433     }
00434     DS_Indication_Delete ( di );
00435     return 0;
00436 }
Copyright © 2008 Isode privacy   feedback Subscribe to our rss newsfeed