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