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