]> git.sur5r.net Git - openldap/blob - libraries/msdos/winsock/openwsa.c
Initial revision
[openldap] / libraries / msdos / winsock / openwsa.c
1 /*
2  *  Copyright (c) 1993 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  open-wsa.c -- libldap ldap_open routine that assumes Winsock API
6  */
7
8 #ifndef lint 
9 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
10 #endif
11
12 #include "lber.h"
13 #include "ldap.h"
14 #include <stdio.h>
15 #include <string.h>
16 #include "msdos.h"
17 #ifdef WSHELPER
18 #include "wshelper.h"
19 #endif
20
21 #ifndef INADDR_LOOPBACK
22 #define INADDR_LOOPBACK ((u_long) 0x7f000001)
23 #endif
24
25 #ifdef LDAP_DEBUG
26 int     ldap_debug;
27 #endif
28
29 /*
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.
32  *
33  * Example:
34  *      LDAP    *ld;
35  *      ld = ldap_open( hostname, port );
36  */
37
38 LDAP *ldap_open( host, port )
39 char    *host;
40 int     port;
41 {
42         SOCKET                  s;
43         struct sockaddr_in      sock;
44         struct hostent FAR      *hp;
45         LDAP                    *ld;
46         unsigned long           address;
47         int                     i, connected;
48         char                    *p, hostname[64];
49         WSADATA                 wsadata;
50
51         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
52
53         if ( WSAStartup( 0x0101, &wsadata ) != 0 ) {
54             return( NULL );
55         }
56
57         hp = NULL;
58
59         if ( host != NULL ) {
60             if (( address = inet_addr( host )) == INADDR_NONE ) {
61                 if (( hp = gethostbyname( host )) == NULL ) {
62                     WSACleanup();
63                     return( NULL );
64                 }
65             }
66         } else {
67             address = htonl( INADDR_LOOPBACK );
68         }
69
70         if ( port == 0 )
71                 port = LDAP_PORT;
72
73         if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET ) {
74                 WSACleanup();
75                 return( NULL );
76         }
77
78         connected = 0;
79         for ( i = 0; i == 0 || ( hp != NULL && hp->h_addr_list[ i ] != 0L );
80                 ++i ) {
81             if ( hp != NULL ) {
82                 SAFEMEMCPY( &sock.sin_addr.s_addr, hp->h_addr_list[ i ],
83                     sizeof( sock.sin_addr.s_addr ));
84             } else {
85                 sock.sin_addr.s_addr = address;
86             }
87             sock.sin_family = AF_INET;
88             sock.sin_port = htons( port );
89
90             if ( connect( s, (struct sockaddr *) &sock, sizeof(sock) ) != SOCKET_ERROR ) {
91                 connected = 1;
92                 break;
93             }
94         }
95
96         if ( !connected ) {
97             closesocket( s );
98             WSACleanup();
99             return( NULL );
100         }
101
102         /*
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.
106          */
107
108         hostname[0] = '\0';
109 #ifdef WSHELPER
110         if ( (hp = rgethostbyaddr( (char *)&sock.sin_addr.s_addr,
111 #else
112         if ( (hp = gethostbyaddr( (char *)&sock.sin_addr.s_addr,
113 #endif
114              sizeof(sock.sin_addr.s_addr), AF_INET )) != NULL ) {
115             if ( hp->h_name != NULL ) {
116                 if ( (p = strchr( hp->h_name, '.' )) != NULL ) {
117                     *p = '\0';
118                 }
119                 strcpy( hostname, hp->h_name );
120             }
121         }
122
123         if ( (ld = (LDAP *) calloc( sizeof(LDAP), 1 )) == NULL ) {
124                 closesocket( s );
125                 WSACleanup();
126                 return( NULL );
127         }
128         ld->ld_sb.sb_sd = s;
129         ld->ld_host = strdup( hostname );
130         ld->ld_version = LDAP_VERSION;
131
132         return( ld );
133 }