3 * Copyright (c) 1990 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
19 #include <ac/socket.h>
20 #include <ac/string.h>
23 #include <quipu/commonarg.h>
24 #include <quipu/ds_error.h>
25 #include <quipu/dap.h> /* get dap_unbind() */
26 #if ISODEPACKAGE == IC
27 #include <ll/isoaddrs.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
39 #ifdef HAVE_SYS_FILIO_H
40 #include <sys/filio.h>
44 #define FIOGETOWN FIOGSAIOOWN
50 conn_dup( struct conn *cn )
53 if ( (new = (struct conn *) malloc( sizeof(struct conn) )) == NULL )
59 new->c_paddr = psap_cpy( cn->c_paddr );
60 new->c_dn = strdup( cn->c_dn );
61 if ( new->c_credlen > 0 ) {
62 new->c_cred = (char *) malloc( cn->c_credlen );
63 SAFEMEMCPY( new->c_cred, cn->c_cred, cn->c_credlen );
67 new->c_credlen = cn->c_credlen;
76 struct PSAPaddr *addr;
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 )
108 Debug( LDAP_DEBUG_TRACE, "conn_free (%s): refcnt is %d\n",
109 paddr2str( conn->c_paddr, NULLNA ), conn->c_refcnt, 0 );
111 if ( --conn->c_refcnt > 0 )
114 gettimeofday( &tv, (struct timezone *)NULL );
115 if ( conn->c_time != 0L && (tv.tv_sec - conn->c_time)
116 < referral_connection_timeout ) {
117 Debug( LDAP_DEBUG_TRACE, "conn_free: referral conn ttl is %d\n",
118 referral_connection_timeout - (tv.tv_sec - conn->c_time),
127 free( (char *) conn->c_paddr );
130 if ( conn->c_credlen > 0 )
131 free( conn->c_cred );
136 conn_del( struct conn *conn )
138 struct conn *tmp, *prev;
140 Debug( LDAP_DEBUG_TRACE, "conn_del (%s)\n",
141 paddr2str( conn->c_paddr, NULLNA ), 0, 0 );
144 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
151 Debug( LDAP_DEBUG_ANY, "conn_del: cannot find conn\n", 0, 0,
156 if ( prev == NULL ) {
157 conns = conns->c_next; /* delete head of list */
159 prev->c_next = tmp->c_next;
164 conn_setfds( fd_set *fds )
168 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
169 if ( tmp->c_ad != -1 )
170 FD_SET( tmp->c_ad, fds );
179 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
180 if ( isclosed( tmp->c_ad ) ) {
181 Debug( LDAP_DEBUG_ANY, "conn_badfds: fd %d is bad\n",
189 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) ) );
230 conn_find( struct conn *c )
235 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
238 str = paddr2str( c->c_paddr, NULLNA );
239 Debug( LDAP_DEBUG_CONNS, "conn_find: (%s)\n", str, 0, 0 );
242 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
244 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
247 str = paddr2str( tmp->c_paddr, NULLNA );
248 Debug( LDAP_DEBUG_CONNS, "conn_find: compare to (%s)\n",
252 if ( psap_cmp( tmp->c_paddr, c->c_paddr ) == 0
253 && strcmp( tmp->c_dn, c->c_dn ) == 0
254 && tmp->c_credlen == c->c_credlen
255 && bcmp( tmp->c_cred, c->c_cred, c->c_credlen ) == 0 ) {
256 Debug( LDAP_DEBUG_CONNS, "conn_find: found\n", 0,
262 Debug( LDAP_DEBUG_CONNS, "conn_find: not found\n", 0, 0, 0 );
271 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
272 if ( tmp->c_ad != -1 )
273 dap_unbind( tmp->c_ad );
282 if ( ioctl( ad, FIOGETOWN, &o ) < 0 )
283 return( errno == EBADF ? 1 : 0 );