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