Index: libfdcore/sctp3436.c
===================================================================
--- libfdcore/sctp3436.c	(revision 1190)
+++ libfdcore/sctp3436.c	(revision 1344)
@@ -51,7 +51,7 @@
  - the push function sends the data on a certain stream.
  We also have a demux thread that reads the socket and store received data in the appropriate fifo
- 
+
  We have one gnutls_session per stream pair, and as many threads that read the gnutls records and save incoming data to the target queue.
- 
+
 This complexity is required because we cannot read a socket for a given stream only; we can only get the next message and find its stream.
 */
@@ -71,19 +71,19 @@
 	int	  event;
 	uint16_t  strid;
-	
+
 	TRACE_ENTRY("%p", arg);
 	CHECK_PARAMS_DO(conn && (conn->cc_socket > 0), goto out);
-	
+
 	/* Set the thread name */
 	{
-		char buf[48];
+		char buf[100];
 		snprintf(buf, sizeof(buf), "Demuxer (%d:%s)", conn->cc_socket, conn->cc_remid);
 		fd_log_threadname ( buf );
 	}
-	
+
 	ASSERT( conn->cc_proto == IPPROTO_SCTP );
 	ASSERT( fd_cnx_target_queue(conn) );
 	ASSERT( conn->cc_sctp3436_data.array );
-	
+
 	do {
 		CHECK_FCT_DO( fd_sctp_recvmeta(conn, &strid, &buf, &bufsz, &event), goto fatal );
@@ -98,23 +98,23 @@
 				}
 				break;
-				
+
 			case FDEVP_CNX_EP_CHANGE:
 				/* Send this event to the target queue */
 				CHECK_FCT_DO( fd_event_send( fd_cnx_target_queue(conn), event, bufsz, buf), goto fatal );
 				break;
-			
+
 			case FDEVP_CNX_ERROR:
 				goto out;
-				
+
 			case FDEVP_CNX_SHUTDOWN:
 				/* Just ignore the notification for now, we will get another error later anyway */
 				continue;
-				
+
 			default:
 				goto fatal;
 		}
-		
+
 	} while (conn->cc_loop);
-	
+
 out:
 	/* Signal termination of the connection to all decipher threads */
@@ -125,7 +125,7 @@
 	}
 	fd_cnx_markerror(conn);
-	TRACE_DEBUG(FULL, "Thread terminated");	
+	TRACE_DEBUG(FULL, "Thread terminated");
 	return NULL;
-	
+
 fatal:
 	/* An unrecoverable error occurred, stop the daemon */
@@ -139,22 +139,22 @@
 	struct sctp3436_ctx * ctx = arg;
 	struct cnxctx 	 *cnx;
-	
+
 	TRACE_ENTRY("%p", arg);
 	CHECK_PARAMS_DO(ctx && ctx->raw_recv && ctx->parent, goto error);
 	cnx = ctx->parent;
 	ASSERT( fd_cnx_target_queue(cnx) );
-	
+
 	/* Set the thread name */
 	{
-		char buf[48];
+		char buf[100];
 		snprintf(buf, sizeof(buf), "Decipher (%hu@%d:%s)", ctx->strid, cnx->cc_socket, cnx->cc_remid);
 		fd_log_threadname ( buf );
 	}
-	
+
 	/* The next function loops while there is no error */
 	CHECK_FCT_DO(fd_tls_rcvthr_core(cnx, ctx->strid ? ctx->session : cnx->cc_tls_para.session), /* continue */);
 error:
 	fd_cnx_markerror(cnx);
-	TRACE_DEBUG(FULL, "Thread terminated");	
+	TRACE_DEBUG(FULL, "Thread terminated");
 	return NULL;
 }
@@ -171,10 +171,10 @@
 	struct timespec tsstore, *ts = NULL;
 	int ret;
-	
+
 	TRACE_ENTRY("%p %d", tr, ms);
-	
+
 	if (ctx->partial.buf)
 		return 1; /* data is already available for pull */
-	
+
 	if (ms) {
 		CHECK_SYS_DO(  clock_gettime(CLOCK_REALTIME, &tsstore),  return -1  );
@@ -184,5 +184,5 @@
 		ts = &tsstore;
 	}
-	
+
 	ret = fd_fifo_select ( ctx->raw_recv, ts );
 	if (ret < 0) {
@@ -190,5 +190,5 @@
 		ret = -1;
 	}
-		
+
 	return ret;
 }
@@ -201,11 +201,11 @@
 	struct sctp3436_ctx * ctx = (struct sctp3436_ctx *) tr;
 	struct iovec iov;
-	
+
 	TRACE_ENTRY("%p %p %zd", tr, data, len);
 	CHECK_PARAMS_DO( tr && data, { errno = EINVAL; return -1; } );
-	
+
 	iov.iov_base = (void *)data;
 	iov.iov_len  = len;
-	
+
 	return fd_sctp_sendstrv(ctx->parent, ctx->strid, &iov, 1);
 }
@@ -214,8 +214,8 @@
 {
 	struct sctp3436_ctx * ctx = (struct sctp3436_ctx *) tr;
-	
+
 	TRACE_ENTRY("%p %p %d", tr, iov, iovcnt);
 	CHECK_PARAMS_DO( tr && iov, { errno = EINVAL; return -1; } );
-	
+
 	return fd_sctp_sendstrv(ctx->parent, ctx->strid, (const struct iovec *)iov, iovcnt);
 }
@@ -228,8 +228,8 @@
 	size_t pulled = 0;
 	int emptied;
-	
+
 	TRACE_ENTRY("%p %p %zd", tr, buf, len);
 	CHECK_PARAMS_DO( tr && buf, { errno = EINVAL; goto error; } );
-	
+
 	/* If we don't have data available now, pull new message from the fifo -- this is blocking (until the queue is destroyed) */
 	if (!ctx->partial.buf) {
@@ -241,5 +241,5 @@
 		}
 	}
-		
+
 	pulled = ctx->partial.bufsz - ctx->partial.offset;
 	if (pulled <= len) {
@@ -264,5 +264,5 @@
 	/* We are done */
 	return pulled;
-	
+
 error:
 	gnutls_transport_set_errno (ctx->session, errno);
@@ -278,5 +278,5 @@
 	/* Set the transport pointer passed to push & pull callbacks */
 	GNUTLS_TRACE( gnutls_transport_set_ptr( session, (gnutls_transport_ptr_t) ctx ) );
-	
+
 	/* Reset the low water value, since we don't use sockets */
 #ifndef GNUTLS_VERSION_300
@@ -287,5 +287,5 @@
 	GNUTLS_TRACE( gnutls_transport_set_pull_timeout_function( session, sctp3436_pull_timeout ) );
 #endif /* GNUTLS_VERSION_300 */
-	
+
 	/* Set the push and pull callbacks */
 	GNUTLS_TRACE( gnutls_transport_set_pull_function(session, sctp3436_pull) );
@@ -325,12 +325,12 @@
 	TRACE_ENTRY("%p", conn);
 	CHECK_PARAMS( conn && !conn->cc_sctp3436_data.sess_store );
-	
+
 	CHECK_MALLOC( conn->cc_sctp3436_data.sess_store = malloc(sizeof(struct sr_store)) );
 	memset(conn->cc_sctp3436_data.sess_store, 0, sizeof(struct sr_store));
-	
+
 	fd_list_init(&conn->cc_sctp3436_data.sess_store->list, NULL);
 	CHECK_POSIX( pthread_rwlock_init(&conn->cc_sctp3436_data.sess_store->lock, NULL) );
 	conn->cc_sctp3436_data.sess_store->parent = conn;
-	
+
 	return 0;
 }
@@ -342,10 +342,10 @@
 	TRACE_ENTRY("%p", conn);
 	CHECK_PARAMS_DO( conn, return );
-	
+
 	if (!conn->cc_sctp3436_data.sess_store)
 		return;
-	
+
 	CHECK_POSIX_DO( pthread_rwlock_destroy(&conn->cc_sctp3436_data.sess_store->lock), /* continue */ );
-	
+
 	while (!FD_IS_LIST_EMPTY(&conn->cc_sctp3436_data.sess_store->list)) {
 		struct sr_data * sr = (struct sr_data *) conn->cc_sctp3436_data.sess_store->list.next;
@@ -355,5 +355,5 @@
 		free(sr);
 	}
-	
+
 	free(conn->cc_sctp3436_data.sess_store);
 	conn->cc_sctp3436_data.sess_store = NULL;
@@ -366,19 +366,19 @@
 	struct fd_list * ret;
 	*match = 0;
-	
+
 	for (ret = sto->list.next; ret != &sto->list; ret = ret->next) {
 		int cmp = 0;
 		struct sr_data * sr = (struct sr_data *)ret;
-		
+
 		cmp = fd_os_cmp(key.data, key.size, sr->key.data, sr->key.size);
 		if (cmp > 0)
 			continue;
-		
+
 		if (cmp == 0)
 			*match = 1;
-		
+
 		break;
 	}
-	
+
 	return ret;
 }
@@ -392,15 +392,15 @@
 	int match = 0;
 	int ret = 0;
-	
+
 	TRACE_DEBUG( GNUTLS_DBG_LEVEL, "GNUTLS Callback: %s", __PRETTY_FUNCTION__ );
 	CHECK_PARAMS_DO( sto && key.data && data.data, return -1 );
-	
+
 	CHECK_POSIX_DO( pthread_rwlock_wrlock(&sto->lock), return -1 );
 	TRACE_BUFFER(FD_LOG_DEBUG, GNUTLS_DBG_LEVEL, "Session store [key ", key.data, key.size, "]");
-	
+
 	li = find_or_next(sto, key, &match);
 	if (match) {
 		sr = (struct sr_data *)li;
-		
+
 		/* Check the data is the same */
 		if ((data.size != sr->data.size) || memcmp(data.data, sr->data.data, data.size)) {
@@ -409,5 +409,5 @@
 			TRACE_BUFFER(FD_LOG_DEBUG, INFO, "  -- old data [", sr->data.data, sr->data.size, "]");
 			TRACE_BUFFER(FD_LOG_DEBUG, INFO, "  -- new data [", data.data, data.size, "]");
-			
+
 			ret = -1;
 		} else {
@@ -416,5 +416,5 @@
 		goto out;
 	}
-	
+
 	/* Create a new entry */
 	CHECK_MALLOC_DO( sr = malloc(sizeof(struct sr_data)), { ret = -1; goto out; } );
@@ -430,9 +430,9 @@
 	sr->data.size = data.size;
 	memcpy(sr->data.data, data.data, data.size);
-	
+
 	/* Save this new entry in the list, we are done */
 	fd_list_insert_before(li, &sr->chain);
 
-out:	
+out:
 	CHECK_POSIX_DO( pthread_rwlock_unlock(&sto->lock), return -1 );
 	return ret;
@@ -446,15 +446,15 @@
 	int match = 0;
 	int ret = 0;
-	
+
 	TRACE_DEBUG( GNUTLS_DBG_LEVEL, "GNUTLS Callback: %s", __PRETTY_FUNCTION__ );
 	CHECK_PARAMS_DO( sto && key.data, return -1 );
-	
+
 	CHECK_POSIX_DO( pthread_rwlock_wrlock(&sto->lock), return -1 );
 	TRACE_BUFFER(FD_LOG_DEBUG, GNUTLS_DBG_LEVEL, "Session delete [key ", key.data, key.size, "]");
-	
+
 	li = find_or_next(sto, key, &match);
 	if (match) {
 		sr = (struct sr_data *)li;
-		
+
 		/* Destroy this data */
 		fd_list_unlink(li);
@@ -466,5 +466,5 @@
 		ret = -1;
 	}
-	
+
 	CHECK_POSIX_DO( pthread_rwlock_unlock(&sto->lock), return -1 );
 	return ret;
@@ -485,5 +485,5 @@
 	CHECK_POSIX_DO( pthread_rwlock_rdlock(&sto->lock), return error );
 	TRACE_BUFFER(FD_LOG_DEBUG, GNUTLS_DBG_LEVEL, "Session fetch [key ", key.data, key.size, "]");
-	
+
 	li = find_or_next(sto, key, &match);
 	if (match) {
@@ -493,5 +493,5 @@
 		memcpy(res.data, sr->data.data, res.size);
 	}
-out:	
+out:
 	TRACE_DEBUG(GNUTLS_DBG_LEVEL, "Fetched (%p, %d) from store %p", res.data, res.size, sto);
 	CHECK_POSIX_DO( pthread_rwlock_unlock(&sto->lock), return error);
@@ -503,10 +503,10 @@
 {
 	TRACE_ENTRY("%p", conn);
-	
+
 	GNUTLS_TRACE( gnutls_db_set_retrieve_function(session, sr_fetch));
 	GNUTLS_TRACE( gnutls_db_set_remove_function  (session, sr_remove));
 	GNUTLS_TRACE( gnutls_db_set_store_function   (session, sr_store));
 	GNUTLS_TRACE( gnutls_db_set_ptr              (session, conn->cc_sctp3436_data.sess_store));
-	
+
 	return;
 }
@@ -517,7 +517,7 @@
 	struct sctp3436_ctx * ctx = (struct sctp3436_ctx *) arg;
 	int resumed;
-	
+
 	TRACE_ENTRY("%p", arg);
-	
+
 	/* Set the thread name */
 	{
@@ -526,9 +526,9 @@
 		fd_log_threadname ( buf );
 	}
-	
+
 	TRACE_DEBUG(FULL, "Starting TLS resumed handshake on stream %hu", ctx->strid);
 
 	CHECK_GNUTLS_DO( gnutls_handshake( ctx->session ), return NULL);
-			
+
 	GNUTLS_TRACE( resumed = gnutls_session_is_resumed(ctx->session) );
 	#ifndef GNUTLS_VERSION_300
@@ -545,5 +545,5 @@
 		}
 	}
-			
+
 	/* Finished, OK */
 	return arg;
@@ -559,8 +559,8 @@
 {
 	uint16_t i;
-	
+
 	TRACE_ENTRY("%p", conn);
 	CHECK_PARAMS( conn && (conn->cc_sctp_para.pairs > 1) && (!conn->cc_sctp3436_data.array) );
-	
+
 	/* First, alloc the array and initialize the non-TLS data */
 	CHECK_MALLOC( conn->cc_sctp3436_data.array = calloc(conn->cc_sctp_para.pairs, sizeof(struct sctp3436_ctx))  );
@@ -570,14 +570,14 @@
 		CHECK_FCT( fd_fifo_new(&conn->cc_sctp3436_data.array[i].raw_recv, 10) );
 	}
-	
+
 	/* Set push/pull functions in the master session, using fifo in array[0] */
 	set_sess_transport(conn->cc_tls_para.session, &conn->cc_sctp3436_data.array[0]);
-	
+
 	/* For server side, we also initialize the resuming capabilities */
 	if (conn->cc_tls_para.mode == GNUTLS_SERVER) {
-		
+
 		/* Prepare the store for sessions data */
 		CHECK_FCT( store_init(conn) );
-		
+
 		/* Set the callbacks for resuming in the master session */
 		set_resume_callbacks(conn->cc_tls_para.session, conn);
@@ -586,5 +586,5 @@
 	/* Start the demux thread */
 	CHECK_POSIX( pthread_create( &conn->cc_rcvthr, NULL, demuxer, conn ) );
-	
+
 	return 0;
 }
@@ -596,5 +596,5 @@
 	int errors = 0;
 	gnutls_datum_t 	master_data;
-	
+
 	TRACE_ENTRY("%p %p", conn, priority);
 	CHECK_PARAMS( conn && (conn->cc_sctp_para.pairs > 1) && conn->cc_sctp3436_data.array );
@@ -602,5 +602,5 @@
 	/* Server side: we set all the parameters, the resume callback will take care of resuming the session */
 	/* Client side: we duplicate the parameters of the master session, then set the transport pointer */
-	
+
 	/* For client side, retrieve the master session parameters */
 	if (conn->cc_tls_para.mode == GNUTLS_CLIENT) {
@@ -614,10 +614,10 @@
 		}
 	}
-	
+
 	/* Initialize the session objects and start the handshake in a separate thread */
 	for (i = 1; i < conn->cc_sctp_para.pairs; i++) {
 		/* Set credentials and priority */
 		CHECK_FCT( fd_tls_prepare(&conn->cc_sctp3436_data.array[i].session, conn->cc_tls_para.mode, 0, priority, alt_creds) );
-		
+
 		/* additional initialization for gnutls 3.x */
 		#ifdef GNUTLS_VERSION_300
@@ -644,17 +644,17 @@
 			set_resume_callbacks(conn->cc_sctp3436_data.array[i].session, conn);
 		}
-		
+
 		/* Set transport parameters */
 		set_sess_transport(conn->cc_sctp3436_data.array[i].session, &conn->cc_sctp3436_data.array[i]);
-		
+
 		/* Start the handshake thread */
 		CHECK_POSIX( pthread_create( &conn->cc_sctp3436_data.array[i].thr, NULL, handshake_resume_th, &conn->cc_sctp3436_data.array[i] ) );
 	}
-	
+
 	/* We can now release the memory of master session data if any */
 	if (conn->cc_tls_para.mode == GNUTLS_CLIENT) {
 		GNUTLS_TRACE( gnutls_free(master_data.data) );
 	}
-	
+
 	/* Now wait for all handshakes to finish */
 	for (i = 1; i < conn->cc_sctp_para.pairs; i++) {
@@ -666,5 +666,5 @@
 		}
 	}
-	
+
 	if (errors) {
 		TRACE_DEBUG(INFO, "Handshake failed on %d/%hd stream pairs", errors, conn->cc_sctp_para.pairs);
@@ -672,5 +672,5 @@
 		return ENOTCONN;
 	}
-	
+
 	return 0;
 }
@@ -680,8 +680,8 @@
 {
 	uint16_t i;
-	
+
 	TRACE_ENTRY("%p", conn);
 	CHECK_PARAMS( conn && conn->cc_sctp3436_data.array );
-	
+
 	if (others) {
 		for (i = 1; i < conn->cc_sctp_para.pairs; i++) {
@@ -700,7 +700,7 @@
 {
 	uint16_t i;
-	
+
 	CHECK_PARAMS_DO( conn && conn->cc_sctp3436_data.array, return );
-	
+
 	/* End all TLS sessions, in series (not as efficient as paralel, but simpler) */
 	for (i = 1; i < conn->cc_sctp_para.pairs; i++) {
@@ -715,8 +715,8 @@
 {
 	uint16_t i;
-	
+
 	TRACE_ENTRY("%p", conn);
 	CHECK_PARAMS_DO( conn && conn->cc_sctp3436_data.array, return );
-	
+
 	for (i = 0; i < conn->cc_sctp_para.pairs; i++) {
 		if (conn->cc_sctp3436_data.array[i].thr != (pthread_t)NULL) {
@@ -732,8 +732,8 @@
 {
 	uint16_t i;
-	
+
 	TRACE_ENTRY("%p", conn);
 	CHECK_PARAMS_DO( conn && conn->cc_sctp3436_data.array, return );
-	
+
 	for (i = 1; i < conn->cc_sctp_para.pairs; i++) {
 		if (conn->cc_sctp3436_data.array[i].session) {
@@ -749,8 +749,8 @@
 {
 	uint16_t i;
-	
+
 	TRACE_ENTRY("%p", conn);
 	CHECK_PARAMS_DO( conn && conn->cc_sctp3436_data.array, return );
-	
+
 	for (i = 0; i < conn->cc_sctp_para.pairs; i++) {
 		CHECK_FCT_DO( fd_thr_term(&conn->cc_sctp3436_data.array[i].thr), /* continue */ );
@@ -763,13 +763,13 @@
 {
 	uint16_t i;
-	
+
 	CHECK_PARAMS_DO( conn && conn->cc_sctp3436_data.array, return );
-	
+
 	/* Terminate all receiving threads in case we did not do it yet */
 	fd_sctp3436_stopthreads(conn);
-	
+
 	/* Now, stop the demux thread */
 	CHECK_FCT_DO( fd_thr_term(&conn->cc_rcvthr), /* continue */ );
-	
+
 	/* Free remaining data in the array */
 	for (i = 0; i < conn->cc_sctp_para.pairs; i++) {
@@ -782,12 +782,12 @@
 		}
 	}
-	
+
 	/* Free the array itself now */
 	free(conn->cc_sctp3436_data.array);
 	conn->cc_sctp3436_data.array = NULL;
-	
+
 	/* Delete the store of sessions */
 	store_destroy(conn);
-	
+
 	return ;
 }
