+Listener **slap_listeners = NULL;
+
+#ifdef HAVE_WINSOCK2
+/* in nt_main.c */
+extern ldap_pvt_thread_cond_t started_event;
+
+/* forward reference */
+static void hit_socket(void);
+/* In wsa_err.c */
+char *WSAGetLastErrorString();
+static ldap_pvt_thread_t hit_tid;
+
+#define WAKE_LISTENER(w) \
+do {\
+ if( w ) {\
+ ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );\
+ hit_socket(); \
+ }\
+} while(0)
+#else
+#define WAKE_LISTENER(w) \
+do {\
+ if( w ) {\
+ ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );\
+ }\
+} while(0)
+#endif
+
+#ifndef HAVE_WINSOCK
+static
+#endif
+volatile sig_atomic_t slapd_shutdown = 0;
+
+static ldap_pvt_thread_t listener_tid;
+static volatile sig_atomic_t slapd_listener = 0;
+
+static struct slap_daemon {
+ ldap_pvt_thread_mutex_t sd_mutex;
+
+ int sd_nactives;
+
+#ifndef HAVE_WINSOCK
+ /* In winsock, accept() returns values higher than dtblsize
+ so don't bother with this optimization */
+ int sd_nfds;
+#endif
+
+ fd_set sd_actives;
+ fd_set sd_readers;
+ fd_set sd_writers;
+} slap_daemon;
+
+/*
+ * Add a descriptor to daemon control
+ */
+static void slapd_add(ber_socket_t s) {
+ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
+
+ assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
+ assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
+ assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
+
+#ifndef HAVE_WINSOCK
+ if (s >= slap_daemon.sd_nfds) {
+ slap_daemon.sd_nfds = s + 1;
+ }
+#endif
+
+ FD_SET( s, &slap_daemon.sd_actives );
+ FD_SET( s, &slap_daemon.sd_readers );
+
+ Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
+ (long) s,
+ FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
+ FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
+
+ ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
+}
+
+/*
+ * Remove the descriptor from daemon control
+ */
+void slapd_remove(ber_socket_t s, int wake) {
+ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
+ WAKE_LISTENER(wake);
+
+ Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
+ (long) s,
+ FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
+ FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
+
+ FD_CLR( s, &slap_daemon.sd_actives );
+ FD_CLR( s, &slap_daemon.sd_readers );
+ FD_CLR( s, &slap_daemon.sd_writers );
+
+ ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
+}
+
+void slapd_clr_write(ber_socket_t s, int wake) {
+ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
+ WAKE_LISTENER(wake);
+
+ assert( FD_ISSET( s, &slap_daemon.sd_actives) );
+ FD_CLR( s, &slap_daemon.sd_writers );
+
+ ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
+
+ if( wake ) {
+ ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
+ }
+}
+
+void slapd_set_write(ber_socket_t s, int wake) {
+ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
+ WAKE_LISTENER(wake);
+
+ assert( FD_ISSET( s, &slap_daemon.sd_actives) );
+ FD_SET( (unsigned) s, &slap_daemon.sd_writers );
+
+ ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
+
+ if( wake ) {
+ ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
+ }
+}
+
+void slapd_clr_read(ber_socket_t s, int wake) {
+ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
+ WAKE_LISTENER(wake);
+
+ assert( FD_ISSET( s, &slap_daemon.sd_actives) );
+ FD_CLR( s, &slap_daemon.sd_readers );
+
+ ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
+
+ if( wake ) {
+ ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
+ }
+}
+
+void slapd_set_read(ber_socket_t s, int wake) {
+ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
+ WAKE_LISTENER(wake);
+
+ assert( FD_ISSET( s, &slap_daemon.sd_actives) );
+ FD_SET( s, &slap_daemon.sd_readers );
+
+ ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
+
+ if( wake ) {
+ ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
+ }
+}
+
+static void slapd_close(ber_socket_t s) {
+ Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
+ (long) s, 0, 0 );
+ tcp_close(s);
+}
+
+
+Listener *
+open_listener(
+ const char* url,
+ int port,
+ int tls_port )
+{
+ int tmp, rc;
+ Listener l;
+ Listener *li;
+ LDAPURLDesc *lud;
+ char *s;
+
+ rc = ldap_url_parse( url, &lud );
+
+ if( rc != LDAP_URL_SUCCESS ) {
+ Debug( LDAP_DEBUG_ANY,
+ "daemon: listen URL \"%s\" parse error=%d\n",
+ url, rc, 0 );
+ return NULL;
+ }
+
+#ifndef HAVE_TLS
+ if( lud->lud_ldaps ) {
+ Debug( LDAP_DEBUG_ANY,
+ "daemon: TLS not supported (%s)\n",
+ url, 0, 0 );
+ ldap_free_urldesc( lud );
+ return NULL;
+ }
+
+ if(! lud->lud_port ) {
+ lud->lud_port = port;
+ }
+
+#else
+ l.sl_is_tls = lud->lud_ldaps;
+
+ if(! lud->lud_port ) {
+ lud->lud_port = lud->lud_ldaps ? tls_port : port;
+ }
+#endif
+
+ port = lud->lud_port;
+
+ (void) memset( (void*) &l.sl_addr, '\0', sizeof(l.sl_addr) );
+
+ l.sl_addr.sin_family = AF_INET;
+ l.sl_addr.sin_port = htons( (unsigned short) lud->lud_port );
+
+ if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
+ || strcmp(lud->lud_host, "*") == 0 )
+ {
+ l.sl_addr.sin_addr.s_addr = htonl(INADDR_ANY);
+
+ } else {
+ /* host or address was specified */
+ if( !inet_aton( lud->lud_host, &l.sl_addr.sin_addr ) ) {
+ struct hostent *he = gethostbyname( lud->lud_host );
+ if( he == NULL ) {
+ Debug( LDAP_DEBUG_ANY, "invalid host (%s) in URL: %s",
+ lud->lud_host, url, 0);
+ ldap_free_urldesc( lud );
+ return NULL;
+ }
+
+ memcpy( &l.sl_addr.sin_addr, he->h_addr,
+ sizeof( l.sl_addr.sin_addr ) );
+ }
+ }
+
+ ldap_free_urldesc( lud );
+
+
+ if ( (l.sl_sd = socket( AF_INET, SOCK_STREAM, 0 )) == AC_SOCKET_INVALID ) {
+#ifndef HAVE_WINSOCK
+ int err = errno;
+ Debug( LDAP_DEBUG_ANY,
+ "daemon: socket() failed errno %d (%s)\n", err,
+ err > -1 && err < sys_nerr ? sys_errlist[err] :
+ "unknown", 0 );
+#else
+ Debug( LDAP_DEBUG_ANY,
+ "daemon: socket() failed errno %d (%s)\n",
+ WSAGetLastError(),
+ WSAGetLastErrorString(), 0 );
+#endif
+ return NULL;
+ }
+
+#ifndef HAVE_WINSOCK
+ if ( l.sl_sd >= dtblsize ) {
+ Debug( LDAP_DEBUG_ANY,
+ "daemon: listener descriptor %ld is too great %ld\n",
+ (long) l.sl_sd, (long) dtblsize, 0 );
+ tcp_close( l.sl_sd );
+ return NULL;
+ }
+#endif
+
+#ifdef SO_REUSEADDR
+ tmp = 1;
+ if ( setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
+ (char *) &tmp, sizeof(tmp) ) == -1 )
+ {
+ int err = errno;
+ Debug( LDAP_DEBUG_ANY,
+ "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno %d (%s)\n",
+ (long) l.sl_sd, err,
+ err > -1 && err < sys_nerr
+ ? sys_errlist[err] : "unknown" );
+ }
+#endif
+#ifdef SO_KEEPALIVE
+ tmp = 1;
+ if ( setsockopt( l.sl_sd, SOL_SOCKET, SO_KEEPALIVE,
+ (char *) &tmp, sizeof(tmp) ) == -1 )
+ {
+ int err = errno;
+ Debug( LDAP_DEBUG_ANY,
+ "slapd(%ld): setsockopt(SO_KEEPALIVE) failed errno %d (%s)\n",
+ (long) l.sl_sd, err,
+ err > -1 && err < sys_nerr
+ ? sys_errlist[err] : "unknown" );
+ }
+#endif
+#ifdef TCP_NODELAY
+ tmp = 1;
+ if ( setsockopt( l.sl_sd, IPPROTO_TCP, TCP_NODELAY,
+ (char *)&tmp, sizeof(tmp) ) )
+ {
+ int err = errno;
+ Debug( LDAP_DEBUG_ANY,
+ "slapd(%ld): setsockopt(TCP_NODELAY) failed errno %d (%s)\n",
+ (long) l.sl_sd, err,
+ err > -1 && err < sys_nerr
+ ? sys_errlist[err] : "unknown" );
+ }
+#endif
+
+ if ( bind( l.sl_sd, (struct sockaddr *) &l.sl_addr, sizeof(l.sl_addr) ) == -1 ) {
+ int err = errno;
+ Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno %d (%s)\n",
+ (long) l.sl_sd, err,
+ err > -1 && err < sys_nerr
+ ? sys_errlist[err] : "unknown" );
+ tcp_close( l.sl_sd );
+ return NULL;
+ }
+
+ l.sl_url = ch_strdup( url );
+
+ l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65336") );
+ s = inet_ntoa( l.sl_addr.sin_addr );
+ sprintf( l.sl_name, "IP=%s:%d",
+ s != NULL ? s : "unknown" , port );
+
+ li = ch_malloc( sizeof( Listener ) );
+ *li = l;
+
+ Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
+ l.sl_url, 0, 0 );
+
+ return li;
+}
+
+static int sockinit(void);
+static int sockdestroy(void);
+
+int slapd_daemon_init(char *urls, int port, int tls_port )