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