2 * Copyright (c) 1992 Regents of the University of Michigan.
9 static char copyright[] = "@(#) Copyright (c) 1992 Regents of the University of Michigan.\nAll rights reserved.\n";
19 #include <sys/tk_types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
29 #ifndef INADDR_LOOPBACK
30 #define INADDR_LOOPBACK ((u_long) 0x7f000001)
38 u_long *lookup_addrs( char *host );
39 extern long inet_addr( char *addr );
40 extern struct machinfo *lookup( char *host );
44 * ldap_open - initialize and connect to an ldap server. A magic cookie to
45 * be used for future communication is returned on success, NULL on failure.
49 * ld = ldap_open( hostname, port );
53 LDAP *ldap_open( host, port )
58 unsigned long address;
59 struct sockaddr_in sock;
62 char *p, hostname[BUFSIZ];
64 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
67 fprintf(stderr, "No hostname!\n");
70 if ( (hp = gethostbyname( host )) == NULL ) {
71 perror( "gethostbyname" );
74 SAFEMEMCPY( (char *) &address, (char *) hp->h_addr, hp->h_length );
75 strcpy( hostname, hp->h_name );
76 if ( (p = strchr( hostname, '.' )) != NULL )
81 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
82 tk_perror( "socket" );
86 memset( (char *)&sock, 0, sizeof(sock));
87 SAFEMEMCPY( &sock.sin_addr, hp->h_addr, hp->h_length );
88 sock.sin_family = hp->h_addrtype;
89 sock.sin_port = htons( (u_short) port);
91 if (connect( s, (struct sockaddr *)&sock, sizeof(sock) ) < 0 ) {
92 tk_perror( "connect" );
96 if ( (ld = (LDAP *) calloc( sizeof(LDAP), 1 )) == NULL ) {
101 ld->ld_host = strdup( hostname );
102 ld->ld_version = LDAP_VERSION;
108 LDAP *ldap_open( host, port )
113 unsigned long tmpaddr[2], *addrs;
115 char *p, hostname[BUFSIZ];
117 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
122 if ( host != NULL ) {
123 strcpy( hostname, host );
124 if ( (tmpaddr[0] = inet_addr( host )) == -1 ) {
125 if (( addrs = lookup_addrs( host )) == NULL ) {
131 tmpaddr[0] = INADDR_LOOPBACK;
132 strcpy( hostname, "localhost" );
134 if ( (p = strchr( hostname, '.' )) != NULL )
139 for ( i = 0; addrs[ i ] != 0; ++i ) {
140 if ( (s = ncsaopen( addrs[ i ], port )) >= 0 ) {
145 if ( addrs[ i ] == 0 ) {
150 if ( (ld = (LDAP *) calloc( sizeof(LDAP), 1 )) == NULL ) {
156 ld->ld_host = strdup( hostname );
157 ld->ld_version = LDAP_VERSION;
170 u_long *addrs, along;
172 if (( mr = lookup( host )) == NULL ) {
179 while ( numaddrs < 4 ) { /* maximum of 4 addresses */
180 SAFEMEMCPY( (char *)&along, (char *)ipp, sizeof( u_long ));
187 #else /* NCSADOMAINFIX */
189 #endif /* NCSADOMAINFIX */
191 if (( addrs = (u_long *)malloc(( numaddrs + 1 ) * sizeof( u_long )))
195 addrs[ numaddrs ] = 0;
197 for ( i = 0, ipp = mr->hostip; i < numaddrs; ++i, ipp += 4 ) {
198 SAFEMEMCPY( (char *)&addrs[ i ], (char *)ipp, sizeof( u_long ));
205 * Stand alone inet_addr derived from BSD 4.3 Networking Release 2 by MCS
207 * Copyright (c) 1992 Regents of the University of Michigan.
208 * All rights reserved.
214 * Copyright (c) 1983, 1990 Regents of the University of California.
215 * All rights reserved.
217 * Redistribution and use in source and binary forms, with or without
218 * modification, are permitted provided that the following conditions
220 * 1. Redistributions of source code must retain the above copyright
221 * notice, this list of conditions and the following disclaimer.
222 * 2. Redistributions in binary form must reproduce the above copyright
223 * notice, this list of conditions and the following disclaimer in the
224 * documentation and/or other materials provided with the distribution.
225 * 3. All advertising materials mentioning features or use of this software
226 * must display the following acknowledgement:
227 * This product includes software developed by the University of
228 * California, Berkeley and its contributors.
229 * 4. Neither the name of the University nor the names of its contributors
230 * may be used to endorse or promote products derived from this software
231 * without specific prior written permission.
233 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
237 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
238 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
240 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
242 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
246 #if defined(LIBC_SCCS) && !defined(lint)
247 static char sccsid[] = "@(#)inet_addr.c 5.10 (Berkeley) 2/24/91";
248 #endif /* LIBC_SCCS and not lint */
253 #define INADDR_NONE 0xffffffff /* -1 return */
255 int inet_aton( char *cp, u_long *addr);
258 #include <netinet/in.h>
259 #include <arpa/inet.h>
265 #define isascii(c) ((unsigned)(c)<=0177) /* for broken machines */
269 * Ascii internet address interpretation routine.
270 * The value returned is in network order.
274 register const char *cp;
282 if (inet_aton(cp, &val))
288 return (INADDR_NONE);
292 * Check whether "cp" is a valid ascii representation
293 * of an Internet address and convert to a binary address.
294 * Returns 1 if the address is valid, 0 if not.
295 * This replaces inet_addr, the return value from which
296 * cannot distinguish between failure and a local broadcast address.
304 struct in_addr *addr;
307 register u_long val, base, n;
309 u_long parts[4], *pp = parts;
313 * Collect number up to ``.''.
314 * Values are specified as for C:
315 * 0x=hex, 0=octal, other=decimal.
319 if (*++cp == 'x' || *cp == 'X')
324 while ((c = *cp) != '\0') {
325 if (isascii(c) && isdigit(c)) {
326 val = (val * base) + (c - '0');
330 if (base == 16 && isascii(c) && isxdigit(c)) {
332 (c + 10 - (islower(c) ? 'a' : 'A'));
342 * a.b.c (with c treated as 16-bits)
343 * a.b (with b treated as 24 bits)
345 if (pp >= parts + 3 || val > 0xff)
352 * Check for trailing characters.
354 if (*cp && (!isascii(*cp) || !isspace(*cp)))
357 * Concoct the address according to
358 * the number of parts specified.
363 case 1: /* a -- 32 bits */
366 case 2: /* a.b -- 8.24 bits */
369 val |= parts[0] << 24;
372 case 3: /* a.b.c -- 8.8.16 bits */
375 val |= (parts[0] << 24) | (parts[1] << 16);
378 case 4: /* a.b.c.d -- 8.8.8.8 bits */
381 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
388 addr->s_addr = htonl(val);