]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
63fc1373ad542845c681cfd0a9aaa3518153b16e
[openldap] / libraries / libldap / os-ip.c
1 /*
2  *  Copyright (c) 1995 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  os-ip.c -- platform-specific TCP & UDP related code
6  */
7
8 #ifndef lint 
9 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
10 #endif
11
12 #define DISABLE_BRIDGE
13 #include "portable.h"
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <ac/string.h>
18 #include <errno.h>
19
20 #ifdef _WIN32
21 #include <io.h>
22 #include "msdos.h"
23 #else /* _WIN32 */
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <netdb.h>
30 #include <unistd.h>
31 #endif /* _WIN32 */
32 #ifdef _AIX
33 #include <sys/select.h>
34 #endif /* _AIX */
35 #ifdef VMS
36 #include "ucx_select.h"
37 #endif /* VMS */
38
39 #include "portable.h"
40 #include "lber.h"
41 #include "ldap.h"
42
43 #ifdef LDAP_REFERRALS
44 #ifdef USE_SYSCONF
45 #include <unistd.h>
46 #endif /* USE_SYSCONF */
47 #ifdef notyet
48 #ifdef NEED_FILIO
49 #include <sys/filio.h>
50 #else /* NEED_FILIO */
51 #include <sys/ioctl.h>
52 #endif /* NEED_FILIO */
53 #endif /* notyet */
54 #endif /* LDAP_REFERRALS */
55
56 #ifdef MACOS
57 #define tcp_close( s )          tcpclose( s )
58 #else /* MACOS */
59 #ifdef DOS
60 #ifdef PCNFS
61 #define tcp_close( s )          close( s )
62 #endif /* PCNFS */
63 #ifdef NCSA
64 #define tcp_close( s )          netclose( s ); netshut()
65 #endif /* NCSA */
66 #ifdef WINSOCK
67 #define tcp_close( s )          closesocket( s ); WSACleanup();
68 #endif /* WINSOCK */
69 #else /* DOS */
70 #define tcp_close( s )          close( s )
71 #endif /* DOS */
72 #endif /* MACOS */
73
74 #include "ldap-int.h"
75
76 int
77 ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
78         int port, int async )
79 /*
80  * if host == NULL, connect using address
81  * "address" and "port" must be in network byte order
82  * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
83  * async is only used ifdef LDAP_REFERRALS (non-0 means don't wait for connect)
84  * XXX async is not used yet!
85  */
86 {
87         int                     rc, i, s = 0;
88         int                     connected, use_hp;
89         struct sockaddr_in      sin;
90         struct hostent          *hp = NULL;
91 #ifdef notyet
92 #ifdef LDAP_REFERRALS
93         int                     status; /* for ioctl call */
94 #endif /* LDAP_REFERRALS */
95 #endif /* notyet */
96
97         Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
98             ( host == NULL ) ? "(by address)" : host, ntohs( port ), 0 );
99
100         connected = use_hp = 0;
101
102         if ( host != NULL && ( address = inet_addr( host )) == -1 ) {
103                 if ( (hp = gethostbyname( host )) == NULL ) {
104                         errno = EHOSTUNREACH;   /* not exactly right, but... */
105                         return( -1 );
106                 }
107                 use_hp = 1;
108         }
109
110         rc = -1;
111         for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
112                 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
113                         return( -1 );
114                 }
115 #ifdef notyet
116 #ifdef LDAP_REFERRALS
117                 status = 1;
118                 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
119                         Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
120                             s, 0, 0 );
121                 }
122 #endif /* LDAP_REFERRALS */
123 #endif /* notyet */
124                 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
125                 sin.sin_family = AF_INET;
126                 sin.sin_port = port;
127                 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
128                     ( use_hp ? (char *) hp->h_addr_list[ i ] :
129                     (char *) &address ), sizeof( sin.sin_addr.s_addr) );
130
131                 if ( connect( s, (struct sockaddr *)&sin,
132                     sizeof( struct sockaddr_in )) >= 0 ) {
133                         connected = 1;
134                         rc = 0;
135                         break;
136                 } else {
137 #ifdef notyet
138 #ifdef LDAP_REFERRALS
139 #ifdef EAGAIN
140                         if ( errno == EINPROGRESS || errno == EAGAIN ) {
141 #else /* EAGAIN */
142                         if ( errno == EINPROGRESS ) {
143 #endif /* EAGAIN */
144                                 Debug( LDAP_DEBUG_TRACE,
145                                         "connect would block...\n", 0, 0, 0 );
146                                 rc = -2;
147                                 break;
148                         }
149 #endif /* LDAP_REFERRALS */
150 #endif /* notyet */
151
152 #ifdef LDAP_DEBUG               
153                         if ( ldap_debug & LDAP_DEBUG_TRACE ) {
154                                 perror( (char *)inet_ntoa( sin.sin_addr ));
155                         }
156 #endif
157                         close( s );
158                         if ( !use_hp ) {
159                                 break;
160                         }
161                 }
162         }
163
164         sb->sb_sd = s;
165
166         if ( connected ) {
167 #ifdef notyet
168 #ifdef LDAP_REFERRALS
169                 status = 0;
170                 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
171                         Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
172                             s, 0, 0 );
173                 }
174 #endif /* LDAP_REFERRALS */
175 #endif /* notyet */
176
177                 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
178                     s, inet_ntoa( sin.sin_addr ), 0 );
179         }
180
181         return( rc );
182 }
183
184
185 void
186 ldap_close_connection( Sockbuf *sb )
187 {
188     tcp_close( sb->sb_sd );
189 }
190
191
192 #ifdef KERBEROS
193 char *
194 ldap_host_connected_to( Sockbuf *sb )
195 {
196         struct hostent          *hp;
197         char                    *p;
198         int                     len;
199         struct sockaddr_in      sin;
200
201         (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
202         len = sizeof( sin );
203         if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
204                 return( NULL );
205         }
206
207         /*
208          * do a reverse lookup on the addr to get the official hostname.
209          * this is necessary for kerberos to work right, since the official
210          * hostname is used as the kerberos instance.
211          */
212         if (( hp = gethostbyaddr( (char *) &sin.sin_addr,
213             sizeof( sin.sin_addr ), AF_INET )) != NULL ) {
214                 if ( hp->h_name != NULL ) {
215                         return( strdup( hp->h_name ));
216                 }
217         }
218
219         return( NULL );
220 }
221 #endif /* KERBEROS */
222
223
224 #ifdef LDAP_REFERRALS
225 /* for UNIX */
226 struct selectinfo {
227         fd_set  si_readfds;
228         fd_set  si_writefds;
229         fd_set  si_use_readfds;
230         fd_set  si_use_writefds;
231 };
232
233
234 void
235 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
236 {
237         struct selectinfo       *sip;
238
239         sip = (struct selectinfo *)ld->ld_selectinfo;
240
241         if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
242                 FD_SET( sb->sb_sd, &sip->si_writefds );
243         }
244 }
245
246
247 void
248 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
249 {
250         struct selectinfo       *sip;
251
252         sip = (struct selectinfo *)ld->ld_selectinfo;
253
254         if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
255                 FD_SET( sb->sb_sd, &sip->si_readfds );
256         }
257 }
258
259
260 void
261 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
262 {
263         struct selectinfo       *sip;
264
265         sip = (struct selectinfo *)ld->ld_selectinfo;
266
267         FD_CLR( sb->sb_sd, &sip->si_writefds );
268         FD_CLR( sb->sb_sd, &sip->si_readfds );
269 }
270
271
272 int
273 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
274 {
275         struct selectinfo       *sip;
276
277         sip = (struct selectinfo *)ld->ld_selectinfo;
278
279         return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
280 }
281
282
283 int
284 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
285 {
286         struct selectinfo       *sip;
287
288         sip = (struct selectinfo *)ld->ld_selectinfo;
289
290         return( FD_ISSET( sb->sb_sd, &sip->si_use_readfds ));
291 }
292
293
294 void *
295 ldap_new_select_info()
296 {
297         struct selectinfo       *sip;
298
299         if (( sip = (struct selectinfo *)calloc( 1,
300             sizeof( struct selectinfo ))) != NULL ) {
301                 FD_ZERO( &sip->si_readfds );
302                 FD_ZERO( &sip->si_writefds );
303         }
304
305         return( (void *)sip );
306 }
307
308
309 void
310 ldap_free_select_info( void *sip )
311 {
312         free( sip );
313 }
314
315
316 int
317 do_ldap_select( LDAP *ld, struct timeval *timeout )
318 {
319         struct selectinfo       *sip;
320         static int              tblsize;
321
322         Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
323
324         if ( tblsize == 0 ) {
325 #ifdef USE_SYSCONF
326                 tblsize = sysconf( _SC_OPEN_MAX );
327 #else /* !USE_SYSCONF */
328                 tblsize = getdtablesize();
329 #endif /* !USE_SYSCONF */
330
331 #ifdef FD_SETSIZE
332                 if( tblsize > FD_SETSIZE ) {
333                         tblsize = FD_SETSIZE;
334                 }
335 #endif  /* FD_SETSIZE*/
336         }
337
338         sip = (struct selectinfo *)ld->ld_selectinfo;
339         sip->si_use_readfds = sip->si_readfds;
340         sip->si_use_writefds = sip->si_writefds;
341         
342         return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
343             NULL, timeout ));
344 }
345 #endif /* LDAP_REFERRALS */