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 ));
47 if (( rc = res_search( domain, C_IN, T_TXT, buf, sizeof( buf ))) < 0
48 || ( dxs = decode_answer( buf, rc )) == NULL ) {
50 * punt: return list conisting of the original domain name only
52 if (( dxs = (char **)LDAP_MALLOC( 2 * sizeof( char * ))) == NULL ||
53 ( dxs[ 0 ] = LDAP_STRDUP( domain )) == NULL ) {
68 decode_answer( unsigned char *answer, ber_len_t len )
71 char buf[ 256 ], **dxs;
72 unsigned char *eom, *p;
73 int ancount, err, rc, type, class, dx_count, rr_len;
74 int dx_pref[ MAX_TO_SORT ];
78 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
82 #endif /* LDAP_DEBUG */
85 hp = (HEADER *)answer;
88 if ( ntohs( hp->qdcount ) != 1 ) {
89 h_errno = NO_RECOVERY;
93 ancount = ntohs( hp->ancount );
100 * skip over the query
102 p = answer + HFIXEDSZ;
103 if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
104 h_errno = NO_RECOVERY;
107 p += ( rc + QFIXEDSZ );
110 * pull out the answers we are interested in
113 while ( ancount > 0 && err == 0 && p < eom ) {
114 if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
118 p += rc; /* skip over name */
119 type = _getshort( p );
121 class = _getshort( p );
123 p += INT32SZ; /* skip over TTL */
124 rr_len = _getshort( p );
126 if ( class == C_IN && type == T_TXT ) {
131 while ( q < (char *)p + rr_len && err == 0 ) {
132 if ( *q >= 3 && strncasecmp( q + 1, "dx:", 3 ) == 0 ) {
135 while ( isspace( (unsigned char) *r )) {
140 while ( isdigit( (unsigned char) *r )) {
142 pref += ( *r - '0' );
146 if ( dx_count < MAX_TO_SORT - 1 ) {
147 dx_pref[ dx_count ] = pref;
149 while ( isspace( (unsigned char) *r )) {
153 dxs = (char **)LDAP_REALLOC( dxs,
154 ( dx_count + 2 ) * sizeof( char * ));
155 if ( dxs == NULL || ( dxs[ dx_count ] =
156 (char *)LDAP_CALLOC( 1, txt_len + 1 )) == NULL ) {
160 memcpy( dxs[ dx_count ], r, txt_len );
161 dxs[ ++dx_count ] = NULL;
163 q += ( *q + 1 ); /* move past last TXT record */
170 if ( dx_count == 0 ) {
174 * sort records based on associated preference value
176 int i, j, sort_count, tmp_pref;
179 sort_count = ( dx_count < MAX_TO_SORT ) ? dx_count : MAX_TO_SORT;
180 for ( i = 0; i < sort_count; ++i ) {
181 for ( j = i + 1; j < sort_count; ++j ) {
182 if ( dx_pref[ i ] > dx_pref[ j ] ) {
183 tmp_pref = dx_pref[ i ];
184 dx_pref[ i ] = dx_pref[ j ];
185 dx_pref[ j ] = tmp_pref;
199 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */