changeset 108:219c6d78480f

Started the peer module implementation, TBC
author Sebastien Decugis <sdecugis@nict.go.jp>
date Mon, 28 Jul 2008 18:32:55 +0900
parents c956cc2dbaed
children 7fe2dff0424e
files waaad/Makefile.am waaad/peer-internal.h waaad/peer-listener.c waaad/peer-psm.c waaad/peer-recv.c waaad/peer-send.c waaad/peer-thctl.c waaad/peer.c waaad/peer.h
diffstat 9 files changed, 530 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/waaad/Makefile.am	Mon Jul 28 18:11:58 2008 +0900
+++ b/waaad/Makefile.am	Mon Jul 28 18:32:55 2008 +0900
@@ -26,7 +26,8 @@
 		extensions.h extensions.c \
 		dictionary.h dictionary.c \
 		dict-base.h dict-base.c \
-		peer.h peer.c \
+		peer.h peer.c peer-thctl.c peer-listener.c \
+		peer-psm.c peer-send.c peer-recv.c \
 		message.h message.c \
 		queues.h queues.c \
 		routing.h routing.c \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/waaad/peer-internal.h	Mon Jul 28 18:32:55 2008 +0900
@@ -0,0 +1,178 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Sebastien Decugis <sdecugis@nict.go.jp>							 *
+*													 *
+* Copyright (c) 2008, WIDE Project and NICT								 *
+* All rights reserved.											 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the WIDE Project or NICT nor the 						 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of WIDE Project and 						 *
+*   NICT.												 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+
+/* Peers management module - internal header.
+ * 
+ *  This header contains the private definitions to the peer module. 
+ * It should not be included outside this module.
+ *
+ *
+ *  Here is a little more design details:
+ *
+ *  Each peer to which the local host is directly connected is represented by a _peer_t object.
+ * Such objects are created either explicitely by calls to peer_add, or implicitely when a new
+ * incoming connection is accepted.
+ *
+ *  Once the peer_start function has been called, a thread is associated to each peer object to
+ * handle this peer state machine, with the exception of peers in the STATE_DISABLED state.
+ *
+ *  In addition, the peer state machine thread creates more threads for the peer for specific tasks:
+ * waiting for incoming messages on the socket, sending messages on the socket, ...
+ *
+ */
+
+#ifndef _PEER_INTERNAL_H
+#define _PEER_INTERNAL_H
+
+#include <pthread.h>
+#include <errno.h>
+#include <sys/socket.h>
+
+/* States of a peer */
+typedef enum {
+	/* Stable states */
+	STATE_DISABLED = 1,	/* No connexion must be attempted */
+	STATE_OPEN,		/* Connexion established */
+	/* Transitional states */
+	STATE_CLOSED,		/* No connexion established */
+	STATE_CLOSING,		/* the connexion is being shutdown */
+	STATE_WAIT_CNX,		/* Trying to establish a connexion at transport level, waiting for ACK/NACK or new cnx initiated by remote peer (concurrent attempts) */
+	STATE_WAIT_CEA		/* CER has been sent, waiting for CEA or timeout */
+	/* TBC... */
+
+	/* Other states from the RFC: are they needed? Wait-Conn-Ack/Elect, Wait-Returns */
+	/* Also need the states for "Suspect" peers from the watchdog mechanism. */
+	
+} peer_state_t;
+
+
+/* List of sent requests, ordered by hop-by-hop id */
+typedef struct _sent_r {
+	/* Chaining information */
+	struct _sent_r	*next;	/* The next sent request */
+	struct _sent_r	*prev;	/* The previous sent request */
+
+	/* Pointer to the message */
+	msg_t		*msg;
+
+	/* The following two fields are copied from message for faster processing on message reception */
+	uint32_t	 hbh;	/* The hop-by-hop value of the message */
+	int	 	 rtb;	/* Is this a routable message? */
+	
+} _sent_req_t;
+
+
+struct _peer;
+
+/* A list element for peers lists */
+typedef struct _pi {
+	struct _pi 	*next;
+	struct _pi 	*prev;
+	struct _peer	*top;
+} _pi_t;
+
+
+/* Peer internal description */
+typedef struct _peer {
+	/* Chaining of the peer in global lists */
+	_pi_t		 p_global;	/* List of peers this peer belongs to */
+	_pi_t		 p_active;	/* Sublist containing only the active peers */
+
+	/* Peer data */
+	char 		*p_diamid;	/* The Diameter-Id of this peer, once known */
+	uint32_t	 p_hbh;		/* current Hop-by-hop value */
+	pthread_mutex_t	 p_lock;	/* Mutex to protect this object */
+	
+	/* Peer state */
+	peer_state_t	 p_state;	/* State of the peer */
+	pthread_t	 p_psm;		/* The thread handling this peer state machine. */
+	
+	/* Messages handling */
+	meq_t 		*p_in_q;	/* Incoming message queue */
+	pthread_t	 p_in_th;	/* The thread handling messages reception, created by p_psm */
+	
+	meq_t 		*p_out_q;	/* Outgoing message queue */
+	pthread_t	 p_out_th;	/* The thread handling messages envoy, created by p_psm */
+	
+	_sent_req_t	 p_sent;	/* List of sent requests */
+	
+	/* Connection information */
+	int		 p_sock;	/* We use standard Berkeley sockets for both TCP and SCTP connections */
+	
+	/* Security information */
+	_sec_item_t 	*p_sec_list;	/* Used only before a sucessful CER/CEA exchange */
+	sec_mod_hdl_t 	*p_sec_hdl;	/* store reference to the security handler, to call sec_modunlink later */
+	sec_module_t	*p_secmod;	/* the security callbacks */
+	sec_session_t	 p_sec_session;	/* structure passed back to the security module, for connection information */
+	void		*p_ext_session;	/* opaque data that can be used by the security extension to store internal states */
+	
+	/* Supported applications (result of CER/CEA) */
+	size_t		 p_app_size;	/* The size of the array pointed by p_app_list */
+	peer_appl_t	*p_app_list;	/* points to a 0-terminated array of supported applications */
+	
+} _peer_t;
+
+/* The peers global vars */
+extern _pi_t		g_peer_list_all;	/* The peers for which the diameter id is known */
+extern _pi_t		g_peer_list_active;	/* Sentinel for the p_active list, sublist of g_peer_list_all */
+extern _pi_t		g_peer_list_unknown;	/* List of the peers that have not yet advertized their Diameter-Id */
+extern pthread_mutex_t	g_peer_list_lock;	/* Protect the lists */
+
+
+/*
+ * The functions 
+ */
+
+/* Terminate a thread by cancelling it */
+int _peer_cancel_th(pthread_t * thread);
+
+/* Code of the listening threads */
+void * _peer_listen_th(void * arg);
+
+/* Open listening sockets */
+int _listen_sctp(int * sock);
+int _listen_tcp(int * sock);
+
+/* Code of the thread for the peer state machine */
+void * _peer_state_machine_th(void * arg);
+
+/* Code of the thread for outgoing messages */
+void * _peer_out_th(void * arg);
+
+/* Code of the thread for incoming messages */
+void * _peer_in_th(void * arg);
+
+#endif /* ! _PEER_INTERNAL_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/waaad/peer-listener.c	Mon Jul 28 18:32:55 2008 +0900
@@ -0,0 +1,76 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Sebastien Decugis <sdecugis@nict.go.jp>							 *
+*													 *
+* Copyright (c) 2008, WIDE Project and NICT								 *
+* All rights reserved.											 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the WIDE Project or NICT nor the 						 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of WIDE Project and 						 *
+*   NICT.												 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+
+/* Peers facility.
+ *
+ * This file contains the code to open the listening ports and accept new incoming connections.
+ *
+ * We always open two sockets for TCP and SCTP and listen on the port defined in the configuration file,
+ * then create two threads waiting for incoming connections.
+ *
+ */
+
+#include "waaad-internal.h"
+#include "peer-internal.h"	
+
+/* The code of the thread waiting for incoming connection. There are two instances running, for TCP and for SCTP */
+void * _peer_listen_th(void * arg)
+{
+	int sock = 0;
+	
+	TRACE_ENTRY( "%p", arg );
+	
+	/* sock = *(int *)arg; */
+	
+	TRACE_DEBUG (INFO, "@@@ %s: not implemented yet.", __FUNCTION__ );
+	return NULL;
+}
+
+/* Code to open the TCP socket */
+int _listen_tcp(int * sock)
+{
+	TRACE_ENTRY( "" );
+	TRACE_DEBUG (INFO, "@@@ %s: not implemented yet.", __FUNCTION__ );
+	return ENOTSUP;
+}
+	
+/* Code to open the SCTP socket */
+int _listen_sctp(int * sock)
+{
+	TRACE_ENTRY( "" );
+	TRACE_DEBUG (INFO, "@@@ %s: not implemented yet.", __FUNCTION__ );
+	return ENOTSUP;
+}
+	
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/waaad/peer-psm.c	Mon Jul 28 18:32:55 2008 +0900
@@ -0,0 +1,55 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Sebastien Decugis <sdecugis@nict.go.jp>							 *
+*													 *
+* Copyright (c) 2008, WIDE Project and NICT								 *
+* All rights reserved.											 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the WIDE Project or NICT nor the 						 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of WIDE Project and 						 *
+*   NICT.												 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+
+/* Peers facility.
+ *
+ * This file contains the code to send messages with a peer.
+ *
+ */
+
+#include "waaad-internal.h"
+#include "peer-internal.h"	
+
+/* The code of the p_psm_th tread */
+void * _peer_state_machine_th(void * arg)
+{
+	_peer_t * peer = (_peer_t *)arg;
+	
+	TRACE_ENTRY( "%p", arg );
+	
+	TRACE_DEBUG (INFO, "@@@ %s: not implemented yet.", __FUNCTION__ );
+	return NULL;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/waaad/peer-recv.c	Mon Jul 28 18:32:55 2008 +0900
@@ -0,0 +1,55 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Sebastien Decugis <sdecugis@nict.go.jp>							 *
+*													 *
+* Copyright (c) 2008, WIDE Project and NICT								 *
+* All rights reserved.											 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the WIDE Project or NICT nor the 						 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of WIDE Project and 						 *
+*   NICT.												 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+
+/* Peers facility.
+ *
+ * This file contains the code to receive messages on a peer.
+ *
+ */
+
+#include "waaad-internal.h"
+#include "peer-internal.h"	
+
+/* The code of the p_in_th thread */
+void * _peer_in_th(void * arg)
+{
+	_peer_t * peer = (_peer_t *)arg;
+	
+	TRACE_ENTRY( "%p", arg );
+	
+	
+	TRACE_DEBUG (INFO, "@@@ %s: not implemented yet.", __FUNCTION__ );
+	return NULL;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/waaad/peer-send.c	Mon Jul 28 18:32:55 2008 +0900
@@ -0,0 +1,86 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Sebastien Decugis <sdecugis@nict.go.jp>							 *
+*													 *
+* Copyright (c) 2008, WIDE Project and NICT								 *
+* All rights reserved.											 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the WIDE Project or NICT nor the 						 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of WIDE Project and 						 *
+*   NICT.												 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+
+/* Peers facility.
+ *
+ * This file contains the code to send messages pending on a peer.
+ *
+ */
+
+#include "waaad-internal.h"
+#include "peer-internal.h"	
+
+/* The code of the p_out_th tread */
+void * _peer_out_th(void * arg)
+{
+	_peer_t * peer = (_peer_t *)arg;
+	
+	TRACE_ENTRY( "%p", arg );
+	
+	/* This thread pulls messages from the p_out_q queue, and deal with them */
+	
+	/* Get the next message. The hop-by-hop is already set */
+	
+	/* Render the message as a buffer */
+	
+	/* Now send the message using the appropriate security module */
+	
+	/* Save the message in the p_sent queue as needed */
+	
+	
+	TRACE_DEBUG (INFO, "@@@ %s: not implemented yet.", __FUNCTION__ );
+	return NULL;
+}
+	
+
+/* Queue a message in the given peer */
+int peer_send ( peer_t * peer, msg_t * msg )
+{
+	TRACE_ENTRY( "%p %p", peer, msg );
+	
+	/* Lock the peer object */
+	
+	/* Check the state of the peer is valid */
+	
+	/* Allocate a new hop-by-hop */
+	
+	/* put the message in the queue */
+	
+	/* unlock and return */
+	
+	TRACE_DEBUG (INFO, "@@@ %s: not implemented yet.", __FUNCTION__ );
+	return ENOTSUP;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/waaad/peer-thctl.c	Mon Jul 28 18:32:55 2008 +0900
@@ -0,0 +1,67 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Sebastien Decugis <sdecugis@nict.go.jp>							 *
+*													 *
+* Copyright (c) 2008, WIDE Project and NICT								 *
+* All rights reserved.											 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the WIDE Project or NICT nor the 						 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of WIDE Project and 						 *
+*   NICT.												 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+
+/* Peers facility.
+ *
+ * This file contains the code to control threads creation and destruction.
+ *
+ */
+
+#include "waaad-internal.h"
+#include "peer-internal.h"	
+
+int _peer_cancel_th(pthread_t * thread)
+{
+	int ret = 0;
+	
+	TRACE_ENTRY( "%p", thread );
+	
+	/* Cancel the thread. */
+	ret = pthread_cancel(*thread);
+	if (ret != 0) {
+		TRACE_DEBUG(INFO, "pthread_cancel failed: %s", strerror(ret));
+		return ret;
+	}
+	
+	/* Now wait for the thread to terminate cleanly */
+	ret = pthread_join(*thread, NULL);
+	if (ret != 0) {
+		TRACE_DEBUG(INFO, "pthread_wait failed: %s", strerror(ret));
+		return ret;
+	}
+	
+	/* We're done */
+	return 0;
+}
--- a/waaad/peer.c	Mon Jul 28 18:11:58 2008 +0900
+++ b/waaad/peer.c	Mon Jul 28 18:32:55 2008 +0900
@@ -41,73 +41,9 @@
  *
  */
 
-#include <pthread.h>
-#include <errno.h>
-
 #include "waaad-internal.h"
 
-/* States of a peer */
-typedef enum {
-	/* Stable states */
-	STATE_DISABLED = 1,	/* No connexion must be attempted */
-	STATE_OPEN,		/* Connexion established */
-	/* Transitional states */
-	STATE_CLOSED,		/* No connexion established */
-	STATE_CLOSING,		/* the connexion is being shutdown */
-	STATE_WAIT_CNX,		/* Trying to establish a connexion at transport level, waiting for ACK/NACK or new cnx initiated by remote peer (concurrent attempts) */
-	STATE_WAIT_CEA		/* CER has been sent, waiting for CEA or timeout */
-	/* TBC... */
-
-	/* Other states from the RFC: are they needed? Wait-Conn-Ack/Elect, Wait-Returns */
-	/* Also need the states for "Suspect" peers from the watchdog mechanism. */
-	
-} peer_state_t;
-
-/* List of sent requests, ordered by hop-by-hop id */
-typedef struct _sent_req {
-	/* Chaining information */
-	struct _sent_r	*next;	/* The next sent request */
-	struct _sent_r	*prev;	/* The previous sent request */
-
-	/* Pointer to the message */
-	msg_t		*msg;
-
-	/* The following two fields are copied from message for faster processing on message reception */
-	uint32_t	 hbh;	/* The hop-by-hop value of the message */
-	int	 	 rtb;	/* Is this a routable message? */
-	
-} _sent_req_t;
-
-/* Peer internal description */
-typedef struct _peer {
-	char 		*p_diamid;	/* The Diameter-Id of this peer */
-	
-	struct _peer	*p_next;	/* The next peer in the global list, sorted by Diameter-Id */
-	struct _peer	*p_prev;	/* The previous peer in the global list, sorted by Diameter-Id */
-	
-	peer_state_t	 p_state;	/* State of the peer */
-
-	struct _peer	*p_nextval;	/* The next peer in the STATE_OPEN state, or NULL if this peer is not in this state */
-	
-	pthread_t	*p_thread;	/* The thread handling this peer */
-
-	uint32_t	 p_hbh;		/* current Hop-by-hop value */
-	meq_t 		*p_outgoing;	/* Outgoing message queue */
-	_sent_req_t	 p_sent;	/* List of sent requests - No need for mutex since only the peer thread will access this */
-	
-	/* Connexion information */
-	
-	/* Security information (callbacks) */
-	
-	/* Supported applications (result of CER/CEA) */
-	size_t		 p_app_size;	/* The size of the array pointed by p_app_list */
-	peer_appl_t	*p_app_list;	/* points to a 0-terminated array of supported applications */
-	
-	/* what else? */
-	
-} _peer_t;
-
-	
+#include "peer-internal.h"	
 
 
 /* Initialize the module */
@@ -137,7 +73,16 @@
 /* Allow the peers to start connexions */
 int peer_start ( void )
 {
+	int ret = 0;
+	
 	TRACE_ENTRY( "" );
+	
+	/* Accept incoming connections */
+	
+	/* Now move all peers in the "closed" state and start their state machine threads */
+	
+	/* ... */
+	
 	TRACE_DEBUG (INFO, "@@@ %s: not implemented yet.", __FUNCTION__ );
 	return ENOTSUP;
 }
@@ -150,10 +95,3 @@
 	return ENOTSUP;
 }
 
-/* Queue a message in the given peer */
-int peer_send ( peer_t * peer, msg_t * msg )
-{
-	TRACE_ENTRY( "%p %p", peer, msg );
-	TRACE_DEBUG (INFO, "@@@ %s: not implemented yet.", __FUNCTION__ );
-	return ENOTSUP;
-}
--- a/waaad/peer.h	Mon Jul 28 18:11:58 2008 +0900
+++ b/waaad/peer.h	Mon Jul 28 18:32:55 2008 +0900
@@ -110,7 +110,7 @@
  *   Queue a message for sending to a specific peer.
  *  If the peer is not in OPEN state, an error is returned.
  *  msg_update_length is called on the message, then it is queued and sent asynchronously.
- *  If an error occurs while the message is being sent, the caller of this function is not notified.
+ *  Note: if an error occurs while the message is being sent, the caller of this function is not notified.
  *
  * RETURN VALUE:
  *  0        : The message has been queued.
"Welcome to our mercurial repository"