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.
18 #include <ac/socket.h>
19 #include <ac/string.h>
22 #include <quipu/commonarg.h>
23 #include <quipu/ds_error.h>
27 #if ISODEPACKAGE == IC
28 #include <ll/isoaddrs.h>
34 #ifdef HAVE_SYS_IOCTL_H
35 #include <sys/ioctl.h>
37 #ifdef HAVE_SYS_FILIO_H
38 #include <sys/filio.h>
42 #define FIOGETOWN FIOGSAIOOWN
47 struct conn *conn_dup( struct conn *cn )
50 struct PSAPaddr *psap_cpy();
52 if ( (new = (struct conn *) malloc( sizeof(struct conn) )) == NULL )
58 new->c_paddr = psap_cpy( cn->c_paddr );
59 new->c_dn = strdup( cn->c_dn );
60 if ( new->c_credlen > 0 ) {
61 new->c_cred = (char *) malloc( cn->c_credlen );
62 SAFEMEMCPY( new->c_cred, cn->c_cred, cn->c_credlen );
66 new->c_credlen = cn->c_credlen;
75 extern char *dsa_address;
76 struct PSAPaddr *addr, *psap_cpy();
78 if ( (conns = (struct conn *) malloc( sizeof(struct conn) )) == NULL ) {
79 Debug( LDAP_DEBUG_ANY, "conn_init: malloc failed\n", 0, 0, 0 );
88 if ( dsa_address == NULL || (addr = str2paddr( dsa_address ))
90 conns->c_paddr = NULLPA;
91 Debug( LDAP_DEBUG_ANY, "conn_init: bad DSA address (%s)\n",
92 dsa_address ? dsa_address : "NULL", 0, 0 );
94 conns->c_paddr = psap_cpy( addr );
97 conns->c_refcnt = 1; /* this conn is never deleted */
104 conn_free( struct conn *conn )
107 extern int referral_connection_timeout;
109 Debug( LDAP_DEBUG_TRACE, "conn_free (%s): refcnt is %d\n",
110 paddr2str( conn->c_paddr, NULLNA ), conn->c_refcnt, 0 );
112 if ( --conn->c_refcnt > 0 )
115 gettimeofday( &tv, (struct timezone *)NULL );
116 if ( conn->c_time != 0L && (tv.tv_sec - conn->c_time)
117 < referral_connection_timeout ) {
118 Debug( LDAP_DEBUG_TRACE, "conn_free: referral conn ttl is %d\n",
119 referral_connection_timeout - (tv.tv_sec - conn->c_time),
128 free( (char *) conn->c_paddr );
131 if ( conn->c_credlen > 0 )
132 free( conn->c_cred );
137 conn_del( struct conn *conn )
139 struct conn *tmp, *prev;
141 Debug( LDAP_DEBUG_TRACE, "conn_del (%s)\n",
142 paddr2str( conn->c_paddr, NULLNA ), 0, 0 );
145 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
152 Debug( LDAP_DEBUG_ANY, "conn_del: cannot find conn\n", 0, 0,
157 if ( prev == NULL ) {
158 conns = conns->c_next; /* delete head of list */
160 prev->c_next = tmp->c_next;
165 conn_setfds( fd_set *fds )
169 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
170 if ( tmp->c_ad != -1 )
171 FD_SET( tmp->c_ad, fds );
180 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
181 if ( isclosed( tmp->c_ad ) ) {
182 Debug( LDAP_DEBUG_ANY, "conn_badfds: fd %d is bad\n",
189 struct conn *conn_getfd( fd_set *fds )
193 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
194 if ( tmp->c_ad != -1 )
195 if ( FD_ISSET( tmp->c_ad, fds ) )
203 conn_add( struct conn *new )
208 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
211 str = paddr2str( new->c_paddr, NULLNA );
212 Debug( LDAP_DEBUG_CONNS, "conn_add: (%s)\n", str, 0, 0 );
216 gettimeofday( &tv, (struct timezone *)NULL );
217 new->c_time = tv.tv_sec;
224 psap_cmp( struct PSAPaddr *a, struct PSAPaddr *b )
226 return( bcmp( (char *) a, (char *) b, sizeof(struct PSAPaddr) ) );
229 struct conn *conn_find( struct conn *c )
234 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
237 str = paddr2str( c->c_paddr, NULLNA );
238 Debug( LDAP_DEBUG_CONNS, "conn_find: (%s)\n", str, 0, 0 );
241 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
243 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
246 str = paddr2str( tmp->c_paddr, NULLNA );
247 Debug( LDAP_DEBUG_CONNS, "conn_find: compare to (%s)\n",
251 if ( psap_cmp( tmp->c_paddr, c->c_paddr ) == 0
252 && strcmp( tmp->c_dn, c->c_dn ) == 0
253 && tmp->c_credlen == c->c_credlen
254 && bcmp( tmp->c_cred, c->c_cred, c->c_credlen ) == 0 ) {
255 Debug( LDAP_DEBUG_CONNS, "conn_find: found\n", 0,
261 Debug( LDAP_DEBUG_CONNS, "conn_find: not found\n", 0, 0, 0 );
270 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
271 if ( tmp->c_ad != -1 )
272 dap_unbind( tmp->c_ad );
282 if ( ioctl( ad, FIOGETOWN, &o ) < 0 )
283 return( errno == EBADF ? 1 : 0 );