Back to index

lightning-sunbird  0.9+nobinonly
ldappr-dns.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  * DNS callback functions for libldap that use the NSPR (Netscape
00040  * Portable Runtime) thread API.
00041  *
00042  */
00043 
00044 #include "ldappr-int.h"
00045 
00046 static LDAPHostEnt *prldap_gethostbyname( const char *name,
00047        LDAPHostEnt *result, char *buffer, int buflen, int *statusp,
00048        void *extradata );
00049 static LDAPHostEnt *prldap_gethostbyaddr( const char *addr, int length,
00050        int type, LDAPHostEnt *result, char *buffer, int buflen,
00051        int *statusp, void *extradata );
00052 static LDAPHostEnt *prldap_convert_hostent( LDAPHostEnt *ldhp,
00053        PRHostEnt *prhp );
00054 
00055 
00056 /*
00057  * Install NSPR DNS functions into ld (if ld is NULL, they are installed
00058  * as the default functions for new LDAP * handles).
00059  *
00060  * Returns 0 if all goes well and -1 if not.
00061  */
00062 int
00063 prldap_install_dns_functions( LDAP *ld )
00064 {
00065     struct ldap_dns_fns                   dnsfns;
00066 
00067     memset( &dnsfns, '\0', sizeof(struct ldap_dns_fns) );
00068     dnsfns.lddnsfn_bufsize = PR_NETDB_BUF_SIZE;
00069     dnsfns.lddnsfn_gethostbyname = prldap_gethostbyname;
00070     dnsfns.lddnsfn_gethostbyaddr = prldap_gethostbyaddr;
00071     if ( ldap_set_option( ld, LDAP_OPT_DNS_FN_PTRS, (void *)&dnsfns ) != 0 ) {
00072        return( -1 );
00073     }
00074 
00075     return( 0 );
00076 }
00077 
00078 
00079 static LDAPHostEnt *
00080 prldap_gethostbyname( const char *name, LDAPHostEnt *result,
00081        char *buffer, int buflen, int *statusp, void *extradata )
00082 {
00083     PRHostEnt prhent;
00084 
00085     if( !statusp || ( *statusp = (int)PR_GetIPNodeByName( name,
00086               PRLDAP_DEFAULT_ADDRESS_FAMILY, PR_AI_DEFAULT,
00087               buffer, buflen, &prhent )) == PR_FAILURE ) {
00088        return( NULL );
00089     }
00090 
00091     return( prldap_convert_hostent( result, &prhent ));
00092 }
00093 
00094 
00095 static LDAPHostEnt *
00096 prldap_gethostbyaddr( const char *addr, int length, int type,
00097        LDAPHostEnt *result, char *buffer, int buflen, int *statusp,
00098        void *extradata )
00099 {
00100     PRHostEnt prhent;
00101     PRNetAddr iaddr;
00102 
00103     if ( NULL == statusp ) {
00104        return( NULL );
00105     }
00106 
00107     memset( &iaddr, 0, sizeof( iaddr ));
00108     if ( PR_StringToNetAddr( addr, &iaddr ) == PR_FAILURE ) {
00109        return( NULL );
00110     }
00111     PRLDAP_SET_PORT( &iaddr, 0 );
00112 
00113     if( PR_FAILURE == (*statusp =
00114                      PR_GetHostByAddr(&iaddr, buffer, buflen, &prhent ))) {
00115        return( NULL );
00116     }
00117 
00118     return( prldap_convert_hostent( result, &prhent ));
00119 }
00120 
00121 
00122 /*
00123  * Function: prldap_convert_hostent()
00124  * Description: copy the fields of a PRHostEnt struct to an LDAPHostEnt
00125  * Returns: the LDAPHostEnt pointer passed in.
00126  */
00127 static LDAPHostEnt *
00128 prldap_convert_hostent( LDAPHostEnt *ldhp, PRHostEnt *prhp )
00129 {
00130        ldhp->ldaphe_name = prhp->h_name;
00131        ldhp->ldaphe_aliases = prhp->h_aliases;
00132        ldhp->ldaphe_addrtype = prhp->h_addrtype;
00133        ldhp->ldaphe_length =  prhp->h_length;
00134        ldhp->ldaphe_addr_list =  prhp->h_addr_list;
00135        return( ldhp );
00136 }