2 * Copyright (c) 1990 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
15 #include <quipu/commonarg.h>
16 #include <quipu/ds_error.h>
17 #include <sys/errno.h>
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
24 #if ISODEPACKAGE == IC
25 #include <ll/isoaddrs.h>
31 #if !defined(_AIX) && !defined(__osf__) && !defined(linux)
32 #include <sys/filio.h>
37 #define FIOGETOWN FIOGSAIOOWN
42 struct conn *conn_dup( struct conn *cn )
45 struct PSAPaddr *psap_cpy();
47 if ( (new = (struct conn *) malloc( sizeof(struct conn) )) == NULL )
53 new->c_paddr = psap_cpy( cn->c_paddr );
54 new->c_dn = strdup( cn->c_dn );
55 if ( new->c_credlen > 0 ) {
56 new->c_cred = (char *) malloc( cn->c_credlen );
57 SAFEMEMCPY( new->c_cred, cn->c_cred, cn->c_credlen );
61 new->c_credlen = cn->c_credlen;
70 extern char *dsa_address;
71 struct PSAPaddr *addr, *psap_cpy();
73 if ( (conns = (struct conn *) malloc( sizeof(struct conn) )) == NULL ) {
74 Debug( LDAP_DEBUG_ANY, "conn_init: malloc failed\n", 0, 0, 0 );
83 if ( dsa_address == NULL || (addr = str2paddr( dsa_address ))
85 conns->c_paddr = NULLPA;
86 Debug( LDAP_DEBUG_ANY, "conn_init: bad DSA address (%s)\n",
87 dsa_address ? dsa_address : "NULL", 0, 0 );
89 conns->c_paddr = psap_cpy( addr );
92 conns->c_refcnt = 1; /* this conn is never deleted */
99 conn_free( struct conn *conn )
102 extern int referral_connection_timeout;
104 Debug( LDAP_DEBUG_TRACE, "conn_free (%s): refcnt is %d\n",
105 paddr2str( conn->c_paddr, NULLNA ), conn->c_refcnt, 0 );
107 if ( --conn->c_refcnt > 0 )
110 gettimeofday( &tv, (struct timezone *)NULL );
111 if ( conn->c_time != 0L && (tv.tv_sec - conn->c_time)
112 < referral_connection_timeout ) {
113 Debug( LDAP_DEBUG_TRACE, "conn_free: referral conn ttl is %d\n",
114 referral_connection_timeout - (tv.tv_sec - conn->c_time),
123 free( (char *) conn->c_paddr );
126 if ( conn->c_credlen > 0 )
127 free( conn->c_cred );
132 conn_del( struct conn *conn )
134 struct conn *tmp, *prev;
136 Debug( LDAP_DEBUG_TRACE, "conn_del (%s)\n",
137 paddr2str( conn->c_paddr, NULLNA ), 0, 0 );
140 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
147 Debug( LDAP_DEBUG_ANY, "conn_del: cannot find conn\n", 0, 0,
152 if ( prev == NULL ) {
153 conns = conns->c_next; /* delete head of list */
155 prev->c_next = tmp->c_next;
160 conn_setfds( fd_set *fds )
164 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
165 if ( tmp->c_ad != -1 )
166 FD_SET( tmp->c_ad, fds );
175 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
176 if ( isclosed( tmp->c_ad ) ) {
177 Debug( LDAP_DEBUG_ANY, "conn_badfds: fd %d is bad\n",
184 struct conn *conn_getfd( fd_set *fds )
188 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
189 if ( tmp->c_ad != -1 )
190 if ( FD_ISSET( tmp->c_ad, fds ) )
198 conn_add( struct conn *new )
203 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
206 str = paddr2str( new->c_paddr, NULLNA );
207 Debug( LDAP_DEBUG_CONNS, "conn_add: (%s)\n", str, 0, 0 );
211 gettimeofday( &tv, (struct timezone *)NULL );
212 new->c_time = tv.tv_sec;
218 static psap_cmp( struct PSAPaddr *a, struct PSAPaddr *b )
220 return( bcmp( (char *) a, (char *) b, sizeof(struct PSAPaddr) ) );
223 struct conn *conn_find( struct conn *c )
228 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
231 str = paddr2str( c->c_paddr, NULLNA );
232 Debug( LDAP_DEBUG_CONNS, "conn_find: (%s)\n", str, 0, 0 );
235 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
237 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
240 str = paddr2str( tmp->c_paddr, NULLNA );
241 Debug( LDAP_DEBUG_CONNS, "conn_find: compare to (%s)\n",
245 if ( psap_cmp( tmp->c_paddr, c->c_paddr ) == 0
246 && strcmp( tmp->c_dn, c->c_dn ) == 0
247 && tmp->c_credlen == c->c_credlen
248 && bcmp( tmp->c_cred, c->c_cred, c->c_credlen ) == 0 ) {
249 Debug( LDAP_DEBUG_CONNS, "conn_find: found\n", 0,
255 Debug( LDAP_DEBUG_CONNS, "conn_find: not found\n", 0, 0, 0 );
264 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
265 if ( tmp->c_ad != -1 )
266 dap_unbind( tmp->c_ad );
276 if ( ioctl( ad, FIOGETOWN, &o ) < 0 )
277 return( errno == EBADF ? 1 : 0 );