I'm looking for an LDAP libracy in C or C++ that allows me to specify a list of LDAP hostnames instead of a single hostname. The library should then use the first one it can connect to in case one or more of the servers is/are down. I'm sure it'd be easy to wrap an existing library to create this, but why reinvent the wheel?
I can't say I've ever heard of one. Furthermore, most LDAP-capable software I've used supported failover poorly or not at all. You might be better off trying to implement the failover at the server, by putting it behind a load balancer or similar.
Use multiple A records, each with a different IP.
ldapserver.example.com. IN A 1.2.3.4
ldapserver.example.com. IN A 2.3.4.5
The OpenLDAP client libs will try each host in turn. Failover is (unfortunately) as slow as your TCP connection timeout...
The novell cldap libraries (and java libraries) support a list of space separated hosts when connecting. It'll try each one in turn, as noted in the ldap_init()
page.
The openldap libldap library also supports a space separated list of hosts passed to ldap_open()
or a comma separated list passed to ldap_initialize()
.
The only catch is to make sure to handle the LDAP_SERVER_DOWN
error that gets returned after a connection goes away. I usually write a wrapper function that tries an operation (ie: a search), and tries to reconnect if LDAP_SERVER_DOWN
occurs, and then does the operation again.