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>
24 #include <quipu/dap.h> /* get dap_unbind() */
25 #if ISODEPACKAGE == IC
26 #include <ll/isoaddrs.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
38 #ifdef HAVE_SYS_FILIO_H
39 #include <sys/filio.h>
43 #define FIOGETOWN FIOGSAIOOWN
49 conn_dup( struct conn *cn )
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 struct PSAPaddr *addr;
77 if ( (conns = (struct conn *) malloc( sizeof(struct conn) )) == NULL ) {
78 Debug( LDAP_DEBUG_ANY, "conn_init: malloc failed\n", 0, 0, 0 );
87 if ( dsa_address == NULL || (addr = str2paddr( dsa_address ))
89 conns->c_paddr = NULLPA;
90 Debug( LDAP_DEBUG_ANY, "conn_init: bad DSA address (%s)\n",
91 dsa_address ? dsa_address : "NULL", 0, 0 );
93 conns->c_paddr = psap_cpy( addr );
96 conns->c_refcnt = 1; /* this conn is never deleted */
103 conn_free( struct conn *conn )
107 Debug( LDAP_DEBUG_TRACE, "conn_free (%s): refcnt is %d\n",
108 paddr2str( conn->c_paddr, NULLNA ), conn->c_refcnt, 0 );
110 if ( --conn->c_refcnt > 0 )
113 gettimeofday( &tv, (struct timezone *)NULL );
114 if ( conn->c_time != 0L && (tv.tv_sec - conn->c_time)
115 < referral_connection_timeout ) {
116 Debug( LDAP_DEBUG_TRACE, "conn_free: referral conn ttl is %d\n",
117 referral_connection_timeout - (tv.tv_sec - conn->c_time),
126 free( (char *) conn->c_paddr );
129 if ( conn->c_credlen > 0 )
130 free( conn->c_cred );
135 conn_del( struct conn *conn )
137 struct conn *tmp, *prev;
139 Debug( LDAP_DEBUG_TRACE, "conn_del (%s)\n",
140 paddr2str( conn->c_paddr, NULLNA ), 0, 0 );
143 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
150 Debug( LDAP_DEBUG_ANY, "conn_del: cannot find conn\n", 0, 0,
155 if ( prev == NULL ) {
156 conns = conns->c_next; /* delete head of list */
158 prev->c_next = tmp->c_next;
163 conn_setfds( fd_set *fds )
167 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
168 if ( tmp->c_ad != -1 )
169 FD_SET( tmp->c_ad, fds );
178 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
179 if ( isclosed( tmp->c_ad ) ) {
180 Debug( LDAP_DEBUG_ANY, "conn_badfds: fd %d is bad\n",
188 conn_getfd( fd_set *fds )
192 for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
193 if ( tmp->c_ad != -1 )
194 if ( FD_ISSET( tmp->c_ad, fds ) )
202 conn_add( struct conn *new )
207 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
210 str = paddr2str( new->c_paddr, NULLNA );
211 Debug( LDAP_DEBUG_CONNS, "conn_add: (%s)\n", str, 0, 0 );
215 gettimeofday( &tv, (struct timezone *)NULL );
216 new->c_time = tv.tv_sec;
223 psap_cmp( struct PSAPaddr *a, struct PSAPaddr *b )
225 return( bcmp( (char *) a, (char *) b, sizeof(struct PSAPaddr) ) );
229 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 );
281 if ( ioctl( ad, FIOGETOWN, &o ) < 0 )
282 return( errno == EBADF ? 1 : 0 );