2 * Copyright (c) 1993 Regents of the University of Michigan.
5 * open-wsa.c -- libldap ldap_open routine that assumes Winsock API
9 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
21 #ifndef INADDR_LOOPBACK
22 #define INADDR_LOOPBACK ((u_long) 0x7f000001)
30 * ldap_open - initialize and connect to an ldap server. A magic cookie to
31 * be used for future communication is returned on success, NULL on failure.
35 * ld = ldap_open( hostname, port );
38 LDAP *ldap_open( host, port )
43 struct sockaddr_in sock;
44 struct hostent FAR *hp;
46 unsigned long address;
48 char *p, hostname[64];
51 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
53 if ( WSAStartup( 0x0101, &wsadata ) != 0 ) {
60 if (( address = inet_addr( host )) == INADDR_NONE ) {
61 if (( hp = gethostbyname( host )) == NULL ) {
67 address = htonl( INADDR_LOOPBACK );
73 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET ) {
79 for ( i = 0; i == 0 || ( hp != NULL && hp->h_addr_list[ i ] != 0L );
82 SAFEMEMCPY( &sock.sin_addr.s_addr, hp->h_addr_list[ i ],
83 sizeof( sock.sin_addr.s_addr ));
85 sock.sin_addr.s_addr = address;
87 sock.sin_family = AF_INET;
88 sock.sin_port = htons( port );
90 if ( connect( s, (struct sockaddr *) &sock, sizeof(sock) ) != SOCKET_ERROR ) {
103 * do a reverse lookup on the addr to get the official hostname.
104 * this is necessary for kerberos to work right, since the official
105 * hostname is used as the kerberos instance.
110 if ( (hp = rgethostbyaddr( (char *)&sock.sin_addr.s_addr,
112 if ( (hp = gethostbyaddr( (char *)&sock.sin_addr.s_addr,
114 sizeof(sock.sin_addr.s_addr), AF_INET )) != NULL ) {
115 if ( hp->h_name != NULL ) {
116 if ( (p = strchr( hp->h_name, '.' )) != NULL ) {
119 strcpy( hostname, hp->h_name );
123 if ( (ld = (LDAP *) calloc( sizeof(LDAP), 1 )) == NULL ) {
129 ld->ld_host = strdup( hostname );
130 ld->ld_version = LDAP_VERSION;