3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
10 * ldap_getdxbyname - retrieve DX records from the DNS (from TXT records for now)
15 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
18 #include <ac/stdlib.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
27 static char ** decode_answer LDAP_P(( unsigned char *answer, ber_len_t len ));
29 #define MAX_TO_SORT 32
33 * ldap_getdxbyname - lookup DNS DX records for domain and return an ordered
37 ldap_getdxbyname( const char *domain )
39 unsigned char buf[ PACKETSZ ];
43 Debug( LDAP_DEBUG_TRACE, "ldap_getdxbyname( %s )\n", domain, 0, 0 );
45 memset( buf, 0, sizeof( buf ));
48 ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
51 if (( rc = res_search( domain, C_IN, T_TXT, buf, sizeof( buf ))) < 0
52 || ( dxs = decode_answer( buf, rc )) == NULL ) {
54 * punt: return list conisting of the original domain name only
56 if (( dxs = (char **)LDAP_MALLOC( 2 * sizeof( char * ))) == NULL ||
57 ( dxs[ 0 ] = LDAP_STRDUP( domain )) == NULL ) {
68 ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
76 decode_answer( unsigned char *answer, ber_len_t len )
79 char buf[ 256 ], **dxs;
80 unsigned char *eom, *p;
81 int ancount, err, rc, type, class, dx_count, rr_len;
82 int dx_pref[ MAX_TO_SORT ];
86 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
90 #endif /* LDAP_DEBUG */
93 hp = (HEADER *)answer;
96 if ( ntohs( hp->qdcount ) != 1 ) {
97 h_errno = NO_RECOVERY;
101 ancount = ntohs( hp->ancount );
108 * skip over the query
110 p = answer + HFIXEDSZ;
111 if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
112 h_errno = NO_RECOVERY;
115 p += ( rc + QFIXEDSZ );
118 * pull out the answers we are interested in
121 while ( ancount > 0 && err == 0 && p < eom ) {
122 if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
126 p += rc; /* skip over name */
127 type = _getshort( p );
129 class = _getshort( p );
131 p += INT32SZ; /* skip over TTL */
132 rr_len = _getshort( p );
134 if ( class == C_IN && type == T_TXT ) {
139 while ( q < (char *)p + rr_len && err == 0 ) {
140 if ( *q >= 3 && strncasecmp( q + 1, "dx:", 3 ) == 0 ) {
143 while ( isspace( (unsigned char) *r )) {
148 while ( isdigit( (unsigned char) *r )) {
150 pref += ( *r - '0' );
154 if ( dx_count < MAX_TO_SORT - 1 ) {
155 dx_pref[ dx_count ] = pref;
157 while ( isspace( (unsigned char) *r )) {
161 dxs = (char **)LDAP_REALLOC( dxs,
162 ( dx_count + 2 ) * sizeof( char * ));
163 if ( dxs == NULL || ( dxs[ dx_count ] =
164 (char *)LDAP_CALLOC( 1, txt_len + 1 )) == NULL ) {
168 memcpy( dxs[ dx_count ], r, txt_len );
169 dxs[ ++dx_count ] = NULL;
171 q += ( *q + 1 ); /* move past last TXT record */
178 if ( dx_count == 0 ) {
182 * sort records based on associated preference value
184 int i, j, sort_count, tmp_pref;
187 sort_count = ( dx_count < MAX_TO_SORT ) ? dx_count : MAX_TO_SORT;
188 for ( i = 0; i < sort_count; ++i ) {
189 for ( j = i + 1; j < sort_count; ++j ) {
190 if ( dx_pref[ i ] > dx_pref[ j ] ) {
191 tmp_pref = dx_pref[ i ];
192 dx_pref[ i ] = dx_pref[ j ];
193 dx_pref[ j ] = tmp_pref;
207 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */