3 * Copyright (c) 1995 Regents of the University of Michigan.
6 * ldap_getdxbyname - retrieve DX records from the DNS (from TXT records for now)
17 #if !defined(MACOS) && !defined(DOS) && !defined( _WIN32 )
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/nameser.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
31 #if defined( DOS ) || defined( _WIN32 )
37 static char ** decode_answer( unsigned char *answer, int len );
38 #else /* NEEDPROTOS */
39 static char **decode_answer();
40 #endif /* NEEDPROTOS */
43 extern char *h_errlist[];
46 #define MAX_TO_SORT 32
50 * ldap_getdxbyname - lookup DNS DX records for domain and return an ordered
54 ldap_getdxbyname( char *domain )
56 unsigned char buf[ PACKETSZ ];
60 Debug( LDAP_DEBUG_TRACE, "ldap_getdxbyname( %s )\n", domain, 0, 0 );
62 memset( buf, 0, sizeof( buf ));
64 if (( rc = res_search( domain, C_IN, T_TXT, buf, sizeof( buf ))) < 0
65 || ( dxs = decode_answer( buf, rc )) == NULL ) {
67 * punt: return list conisting of the original domain name only
69 if (( dxs = (char **)malloc( 2 * sizeof( char * ))) == NULL ||
70 ( dxs[ 0 ] = strdup( domain )) == NULL ) {
85 decode_answer( unsigned char *answer, int len )
88 char buf[ 256 ], **dxs;
89 unsigned char *eom, *p;
90 int ancount, err, rc, type, class, dx_count, rr_len;
91 int dx_pref[ MAX_TO_SORT ];
95 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
99 #endif /* LDAP_DEBUG */
102 hp = (HEADER *)answer;
105 if ( ntohs( hp->qdcount ) != 1 ) {
106 h_errno = NO_RECOVERY;
110 ancount = ntohs( hp->ancount );
117 * skip over the query
119 p = answer + HFIXEDSZ;
120 if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
121 h_errno = NO_RECOVERY;
124 p += ( rc + QFIXEDSZ );
127 * pull out the answers we are interested in
130 while ( ancount > 0 && err == 0 && p < eom ) {
131 if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
135 p += rc; /* skip over name */
136 type = _getshort( p );
138 class = _getshort( p );
140 p += INT32SZ; /* skip over TTL */
141 rr_len = _getshort( p );
143 if ( class == C_IN && type == T_TXT ) {
144 int i, n, pref, txt_len;
148 while ( q < (char *)p + rr_len && err == 0 ) {
149 if ( *q >= 3 && strncasecmp( q + 1, "dx:", 3 ) == 0 ) {
152 while ( isspace( *r )) {
157 while ( isdigit( *r )) {
159 pref += ( *r - '0' );
163 if ( dx_count < MAX_TO_SORT - 1 ) {
164 dx_pref[ dx_count ] = pref;
166 while ( isspace( *r )) {
170 if ( dx_count == 0 ) {
171 dxs = (char **)malloc( 2 * sizeof( char * ));
173 dxs = (char **)realloc( dxs,
174 ( dx_count + 2 ) * sizeof( char * ));
176 if ( dxs == NULL || ( dxs[ dx_count ] =
177 (char *)calloc( 1, txt_len + 1 )) == NULL ) {
181 memcpy( dxs[ dx_count ], r, txt_len );
182 dxs[ ++dx_count ] = NULL;
184 q += ( *q + 1 ); /* move past last TXT record */
191 if ( dx_count == 0 ) {
195 * sort records based on associated preference value
197 int i, j, sort_count, tmp_pref;
200 sort_count = ( dx_count < MAX_TO_SORT ) ? dx_count : MAX_TO_SORT;
201 for ( i = 0; i < sort_count; ++i ) {
202 for ( j = i + 1; j < sort_count; ++j ) {
203 if ( dx_pref[ i ] > dx_pref[ j ] ) {
204 tmp_pref = dx_pref[ i ];
205 dx_pref[ i ] = dx_pref[ j ];
206 dx_pref[ j ] = tmp_pref;
220 #endif /* LDAP_DNS */