dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libldap5 / sources / ldap / common / getdxbyname.c
blob617ff5a9c5443135a08ccfac03c713364c454399
1 #pragma ident "%Z%%M% %I% %E% SMI"
3 /*
4 * The contents of this file are subject to the Netscape Public
5 * License Version 1.1 (the "License"); you may not use this file
6 * except in compliance with the License. You may obtain a copy of
7 * the License at http://www.mozilla.org/NPL/
9 * Software distributed under the License is distributed on an "AS
10 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11 * implied. See the License for the specific language governing
12 * rights and limitations under the License.
14 * The Original Code is Mozilla Communicator client code, released
15 * March 31, 1998.
17 * The Initial Developer of the Original Code is Netscape
18 * Communications Corporation. Portions created by Netscape are
19 * Copyright (C) 1998-1999 Netscape Communications Corporation. All
20 * Rights Reserved.
22 * Contributor(s):
25 * Copyright (c) 1995 Regents of the University of Michigan.
26 * All rights reserved.
29 * nsldapi_getdxbyname - retrieve DX records from the DNS (from
30 * TXT records for now)
33 #include <stdio.h>
35 #ifdef LDAP_DNS
37 XXX not MT-safe XXX
39 #include <string.h>
40 #include <ctype.h>
42 #ifdef macintosh
43 #include <stdlib.h>
44 #include "macos.h"
45 #endif /* macintosh */
47 #ifdef _WINDOWS
48 #include <windows.h>
49 #endif
51 #if !defined(macintosh) && !defined(DOS) && !defined( _WINDOWS )
52 #include <sys/types.h>
53 #include <netinet/in.h>
54 #include <arpa/nameser.h>
55 #include <sys/time.h>
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <netdb.h>
59 #include <resolv.h>
60 #endif
61 #include "ldap-int.h"
63 #if defined( DOS )
64 #include "msdos.h"
65 #endif /* DOS */
68 #ifdef NEEDPROTOS
69 static char ** decode_answer( unsigned char *answer, int len );
70 #else /* NEEDPROTOS */
71 static char **decode_answer();
72 #endif /* NEEDPROTOS */
74 extern int h_errno;
75 extern char *h_errlist[];
78 #define MAX_TO_SORT 32
82 * nsldapi_getdxbyname - lookup DNS DX records for domain and return an ordered
83 * array.
85 char **
86 nsldapi_getdxbyname( char *domain )
88 unsigned char buf[ PACKETSZ ];
89 char **dxs;
90 int rc;
92 LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_getdxbyname( %s )\n", domain, 0, 0 );
94 memset( buf, 0, sizeof( buf ));
96 /* XXX not MT safe XXX */
97 if (( rc = res_search( domain, C_IN, T_TXT, buf, sizeof( buf ))) < 0
98 || ( dxs = decode_answer( buf, rc )) == NULL ) {
100 * punt: return list conisting of the original domain name only
102 if (( dxs = (char **)NSLDAPI_MALLOC( 2 * sizeof( char * ))) == NULL ||
103 ( dxs[ 0 ] = nsldapi_strdup( domain )) == NULL ) {
104 if ( dxs != NULL ) {
105 NSLDAPI_FREE( dxs );
107 dxs = NULL;
108 } else {
109 dxs[ 1 ] = NULL;
113 return( dxs );
117 static char **
118 decode_answer( unsigned char *answer, int len )
120 HEADER *hp;
121 char buf[ 256 ], **dxs;
122 unsigned char *eom, *p;
123 int ancount, err, rc, type, class, dx_count, rr_len;
124 int dx_pref[ MAX_TO_SORT ];
126 #ifdef LDAP_DEBUG
127 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
128 /* __p_query( answer ); */
130 #endif /* LDAP_DEBUG */
132 dxs = NULL;
133 hp = (HEADER *)answer;
134 eom = answer + len;
136 if ( ntohs( hp->qdcount ) != 1 ) {
137 h_errno = NO_RECOVERY;
138 return( NULL );
141 ancount = ntohs( hp->ancount );
142 if ( ancount < 1 ) {
143 h_errno = NO_DATA;
144 return( NULL );
148 * skip over the query
150 p = answer + HFIXEDSZ;
151 if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
152 h_errno = NO_RECOVERY;
153 return( NULL );
155 p += ( rc + QFIXEDSZ );
158 * pull out the answers we are interested in
160 err = dx_count = 0;
161 while ( ancount > 0 && err == 0 && p < eom ) {
162 if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
163 err = NO_RECOVERY;
164 continue;
166 p += rc; /* skip over name */
167 type = _getshort( p );
168 p += INT16SZ;
169 class = _getshort( p );
170 p += INT16SZ;
171 p += INT32SZ; /* skip over TTL */
172 rr_len = _getshort( p );
173 p += INT16SZ;
174 if ( class == C_IN && type == T_TXT ) {
175 int i, n, pref, txt_len;
176 char *q, *r;
178 q = (char *)p;
179 while ( q < (char *)p + rr_len && err == 0 ) {
180 if ( *q >= 3 && strncasecmp( q + 1, "dx:", 3 ) == 0 ) {
181 txt_len = *q - 3;
182 r = q + 4;
183 while ( isspace( *r )) {
184 ++r;
185 --txt_len;
187 pref = 0;
188 while ( isdigit( *r )) {
189 pref *= 10;
190 pref += ( *r - '0' );
191 ++r;
192 --txt_len;
194 if ( dx_count < MAX_TO_SORT - 1 ) {
195 dx_pref[ dx_count ] = pref;
197 while ( isspace( *r )) {
198 ++r;
199 --txt_len;
201 if ( dx_count == 0 ) {
202 dxs = (char **)NSLDAPI_MALLOC( 2 * sizeof( char * ));
203 } else {
204 dxs = (char **)NSLDAPI_REALLOC( dxs,
205 ( dx_count + 2 ) * sizeof( char * ));
207 if ( dxs == NULL || ( dxs[ dx_count ] =
208 (char *)NSLDAPI_CALLOC( 1, txt_len + 1 ))
209 == NULL ) {
210 err = NO_RECOVERY;
211 continue;
213 SAFEMEMCPY( dxs[ dx_count ], r, txt_len );
214 dxs[ ++dx_count ] = NULL;
216 q += ( *q + 1 ); /* move past last TXT record */
219 p += rr_len;
222 if ( err == 0 ) {
223 if ( dx_count == 0 ) {
224 err = NO_DATA;
225 } else {
227 * sort records based on associated preference value
229 int i, j, sort_count, tmp_pref;
230 char *tmp_dx;
232 sort_count = ( dx_count < MAX_TO_SORT ) ? dx_count : MAX_TO_SORT;
233 for ( i = 0; i < sort_count; ++i ) {
234 for ( j = i + 1; j < sort_count; ++j ) {
235 if ( dx_pref[ i ] > dx_pref[ j ] ) {
236 tmp_pref = dx_pref[ i ];
237 dx_pref[ i ] = dx_pref[ j ];
238 dx_pref[ j ] = tmp_pref;
239 tmp_dx = dxs[ i ];
240 dxs[ i ] = dxs[ j ];
241 dxs[ j ] = tmp_dx;
248 h_errno = err;
249 return( dxs );
252 #endif /* LDAP_DNS */