changeset 132:9dfac05e0e48

Added some prototypes in peer module
author Sebastien Decugis <sdecugis@nict.go.jp>
date Tue, 19 Aug 2008 14:17:34 +0900
parents 5875067526ff
children 776a136f5a1f
files waaad/peer-events.c waaad/peer-internal.h waaad/peer-listener.c waaad/peer-sctp.c waaad/peer-tcp.c
diffstat 5 files changed, 359 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/waaad/peer-events.c	Tue Aug 19 14:17:34 2008 +0900
@@ -0,0 +1,54 @@
+/*********************************************************************************************************
+* 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 events to peers.
+ * The reception of events is done inside the peer state machine (peer-psm.c)
+ *
+ */
+
+#include "waaad-internal.h"
+#include "peer-internal.h"	
+
+/* Send an event to a peer; the peer lock must not be held. */
+int _peer_sendevent(_peer_t * peer, pevent_t event, void * data)
+{
+	
+	TRACE_ENTRY("%p %d %p", peer, event, data);
+	
+	TRACE_DEBUG(INFO, "Not implemented yet");
+	return ENOTSUP;
+}
--- a/waaad/peer-internal.h	Tue Aug 19 11:26:43 2008 +0900
+++ b/waaad/peer-internal.h	Tue Aug 19 14:17:34 2008 +0900
@@ -102,12 +102,28 @@
 
 /* A list element for peers lists */
 typedef struct _pi {
-	struct _pi 	*next;
-	struct _pi 	*prev;
-	struct _peer	*top;
-	struct _pi 	*sentinel;
+	struct _pi 	*next;		/* next peer in the list */
+	struct _pi 	*prev;		/* previous peer in the list */
+	struct _peer	*top;		/* this peer */
+	struct _pi 	*sentinel;	/* the head of the list */
 } _pi_t;
 
+/* Events that can be sent to the peer and handled by the peer state machine */
+typedef enum {
+	PEVENT_SHUTDOWN = 1,	/* The daemon requested to shutdown this peer resources */
+	PEVENT_DISCONNECTED,	/* the socket has been disconnected */
+	PEVENT_MAX	/* To be continued */
+} pevent_t;
+
+/* An event element */
+typedef struct _pe {
+	pevent_t	 event;	/* identifier of this event */
+	void		*data;	/* the data associated to the event, if appropriate */
+	struct _pe	*next;	/* next event in the list */
+	struct _pe	*prev;	/* prev event in the list */
+} _pe_t;
+	
+
 /* Flags definitions */
 #define	PEERFL_DYNAMIC		( 1 << 0 )	/* The peer is not statically configured and will expire */
 #define	PEERFL_EXPIRETS		( 1 << 1 )	/* The peer expires at the p_expire time. If not set, the (dynamic) peer expires at transport disconnection */
@@ -134,8 +150,9 @@
 	/* Peer state */
 	peer_state_t	 p_state;	/* State of the peer */
 	pthread_t	 p_psm;		/* The thread handling this peer state machine. */
-	pthread_cond_t	 p_condvar;	/* The cond var to be waited in the peer state machine (for timeouts and DWR) */
+	pthread_cond_t	 p_condvar;	/* The cond var to be waited in the peer state machine (for timeouts and DWR and events) */
 	struct timespec	 p_ts;		/* multi-purpose timespec (meaning depends on the state) */
+	_pe_t		 p_events;	/* queue of events received by the peer */
 	
 	/* Messages handling */
 	meq_t 		*p_in_q;	/* Incoming (received) message queue */
@@ -189,9 +206,9 @@
  * The functions 
  */
 
-/* Open listening sockets */
-int _listen_sctp(int * sock);
-int _listen_tcp(int * sock);
+/* TCP-related functions */
+
+/* SCTP-related functions */
 
 /* Code of the listening thread (one per listening socket) */
 void * _peer_listen_th(void * arg);
@@ -199,6 +216,9 @@
 /* Code of the thread for the peer state machine (one per peer) */
 void * _peer_state_machine_th(void * arg);
 
+/* Sending events to a peer */
+int _peer_sendevent(_peer_t * peer, pevent_t event, void * data);
+
 /* Code of the thread for outgoing messages (one per peer) */
 void * _peer_out_th(void * arg);
 
@@ -208,5 +228,6 @@
 /* Terminate a thread by canceling it */
 int _peer_cancel_th(pthread_t * thread);
 
+
 #endif /* ! _PEER_INTERNAL_H */
 
--- a/waaad/peer-listener.c	Tue Aug 19 11:26:43 2008 +0900
+++ b/waaad/peer-listener.c	Tue Aug 19 14:17:34 2008 +0900
@@ -80,22 +80,6 @@
 	return NULL;
 }
 
-/* Code to open a TCP socket */
-int _listen_tcp(int * sock)
-{
-	TRACE_ENTRY( "" );
-	TRACE_DEBUG (INFO, "@@@ %s: not implemented yet.", __FUNCTION__ );
-	return ENOTSUP;
-}
-	
-/* Code to open a SCTP socket */
-int _listen_sctp(int * sock)
-{
-	TRACE_ENTRY( "" );
-	TRACE_DEBUG (INFO, "@@@ %s: not implemented yet.", __FUNCTION__ );
-	return ENOTSUP;
-}
-
 /* Code to start listening */
 /* -- must use these --
 	int		 disable_inet4;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/waaad/peer-sctp.c	Tue Aug 19 14:17:34 2008 +0900
@@ -0,0 +1,182 @@
+/*********************************************************************************************************
+* 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 handle SCTP connections to a peer.
+ *
+ */
+
+#include "waaad-internal.h"
+#include "peer-internal.h"	
+
+/*
+ * FUNCTION:	_peer_sctp_create_server
+ *
+ * PARAMETERS:
+ *  sock    : (out) On return, the identifier of the bound socket is stored here.
+ *
+ * DESCRIPTION: 
+ *  Creates a listening SCTP bound socket.
+ * It uses the data from configuration (local_addr_pri_sa, ...) to configure the server.
+ *
+ * RETURN VALUE:
+ *   0 : The socket has been created, bound, and is listening for connections.
+ *  !0 : an error occurred (standard errno are returned, such as ENOTSUP)
+ */
+int _peer_sctp_create_server( int * sock )
+{
+	int ret = 0;
+	
+	TRACE_ENTRY("%p", sock);
+	
+	TRACE_DEBUG(INFO, "Not implemented yet");
+	return ENOTSUP;
+}
+
+/*
+ * FUNCTION:	_peer_sctp_accept
+ *
+ * PARAMETERS:
+ *  sock    : The listening server socket from which to accept clients.
+ *  new_sd  : on return, the new socket for this client.
+ *  ostreams: the number of outgoing streams.
+ *  istreams: the number of incoming streams.
+ *
+ * DESCRIPTION: 
+ *  Accepts an incoming connection and get the number of bi-lateral streams.
+ *
+ * RETURN VALUE:
+ *   0 : A new client has arrived.
+ *  !0 : an error occurred (standard errno are returned)
+ */
+int _peer_sctp_accept( int sock, int * new_sd, uint16_t *ostreams, uint16_t *istreams )
+{
+	int ret = 0;
+	
+	TRACE_ENTRY("%d %p %p %p", sock, new_sd, ostreams, istreams);
+	
+	TRACE_DEBUG(INFO, "Not implemented yet");
+	return ENOTSUP;
+}
+
+/*
+ * FUNCTION:	_peer_sctp_connect
+ *
+ * PARAMETERS:
+ *  addr    : primary address of the server, that is passed to connect() function.
+ *  addrlen : length of addr structure.
+ *  sock    : (out) The client socket is stored here on return.
+ *  ostreams: (in/out) the number of outbound streams (desired / negociated).
+ *  istreams: (out) the number of inbound streams.
+ *
+ * DESCRIPTION: 
+ *  Try and connect to a server and retrieve the number of streams in each direction.
+ *
+ * RETURN VALUE:
+ *   0 : The client is connected.
+ *  !0 : an error occurred (standard errno are returned)
+ */
+int _peer_sctp_connect( const sSA *addr, socklen_t addrlen, int *sock, uint16_t *ostreams, uint16_t *istreams )
+{
+	int ret = 0;
+	
+	TRACE_ENTRY("%p %d %p %p %p", addr, addrlen, sock, ostreams, istreams);
+	
+	TRACE_DEBUG(INFO, "Not implemented yet");
+	return ENOTSUP;
+}
+
+/*
+ * FUNCTION:	_peer_sctp_send
+ *
+ * PARAMETERS:
+ *  socket  : The socket on which to send a message.
+ *  streamid: The stream on which the message must be sent.
+ *  data    : pointer to the data that must be sent.
+ *  len     : length of the data buffer.
+ *
+ * DESCRIPTION: 
+ *  Send some data over an SCTP association on a given stream.
+ * The return values and error codes are the same as sendmsg().
+ * If streamid is '-1' on function entry, a valid stream is picked
+ * and set on function return. This can be use to automatically rotate
+ * the streams on which messages are sent.
+ *
+ * RETURN VALUE:
+ *   -1 : error (errno is set)
+ *    0 : the socket has been close.
+ *   >0 : number of bytes sent.
+ */
+ssize_t _peer_sctp_send(_peer_t * peer, int *streamid, const void * data, size_t len)
+{
+	int ret = 0;
+	
+	TRACE_ENTRY("%d %p(%d) %p %d", socket, streamid, streamid?*streamid:0, data, len);
+	
+	TRACE_DEBUG(INFO, "Not implemented yet");
+	return ENOTSUP;
+}
+
+/*
+ * FUNCTION:	_peer_sctp_recv
+ *
+ * PARAMETERS:
+ *  peer    : The peer from which to receive the next message.
+ *  streamid: the stream id on which data is received.
+ *  data    : a buffer containing the received data.
+ *  len     : the size of the data buffer
+ *
+ * DESCRIPTION: 
+ *  Receive data and notifications on a SCTP socket.
+ * When notifications or errors are received, the corresponding event is sent to the peer.
+ * The data buffer is allocated and must be freed after use.
+ * The return values and error codes are the same as recvmsg().
+ *
+ * RETURN VALUE:
+ *  -1 : an error occurred
+ *   0 : shutdown is in progress.
+ *  >0 : length of data written in the data buffer ( also is *len ).
+ */
+ssize_t _peer_sctp_recv(_peer_t * peer, int * streamid, void ** data, size_t *len)
+{
+	int ret = 0;
+	
+	TRACE_ENTRY("%d %p %p %p", socket, streamid,  data, len);
+	
+	TRACE_DEBUG(INFO, "Not implemented yet");
+	return ENOTSUP;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/waaad/peer-tcp.c	Tue Aug 19 14:17:34 2008 +0900
@@ -0,0 +1,94 @@
+/*********************************************************************************************************
+* 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 handle TCP connections to a peer.
+ *
+ */
+
+#include "waaad-internal.h"
+#include "peer-internal.h"	
+
+/*
+ * FUNCTION:	_peer_tcp_create_server
+ *
+ * PARAMETERS:
+ *  sock    : (out) On return, the identifier of the bound socket is stored here.
+ *  sa      : the socket address on which the server must be bound.
+ *  salen   : length of sa structure.
+ *
+ * DESCRIPTION: 
+ *  Creates a listening TCP bound socket.
+ *
+ * RETURN VALUE:
+ *   0 : The socket has been created, bound, and is listening for connections.
+ *  !0 : an error occurred (standard errno are returned, such as ENOTSUP)
+ */
+int _peer_tcp_create_server( int * sock, sSA *addr, socklen_t salen )
+{
+	int ret = 0;
+	
+	TRACE_ENTRY("%p %p %d", sock, addr, salen);
+	
+	TRACE_DEBUG(INFO, "Not implemented yet");
+	return ENOTSUP;
+}
+
+/*
+ * FUNCTION:	_peer_tcp_connect
+ *
+ * PARAMETERS:
+ *  addr    : primary address of the server, that is passed to connect() function.
+ *  addrlen : length of addr structure.
+ *  sock    : (out) The client socket is stored here on return.
+ *
+ * DESCRIPTION: 
+ *  Try and connect to a server in TCP.
+ *
+ * RETURN VALUE:
+ *   0 : The client is connected.
+ *  !0 : an error occurred (standard errno are returned)
+ */
+int _peer_tcp_connect( const sSA *addr, socklen_t addrlen, int *sock )
+{
+	int ret = 0;
+	
+	TRACE_ENTRY("%p %d %p", addr, addrlen, sock);
+	
+	TRACE_DEBUG(INFO, "Not implemented yet");
+	return ENOTSUP;
+}
+
"Welcome to our mercurial repository"