Index: freeDiameter/fD.h
===================================================================
--- freeDiameter/fD.h	(revision 20)
+++ freeDiameter/fD.h	(revision 29)
@@ -152,18 +152,35 @@
 enum {
 	/* Dump all info about this peer in the debug log */
-	 FDEVP_DUMP_ALL = 2000
+	 FDEVP_DUMP_ALL = 1500
 	
 	/* request to terminate this peer : disconnect, requeue all messages */
 	,FDEVP_TERMINATE
 	
-	/* A message was received in the peer */
-	,FDEVP_MSG_INCOMING
+	/* A connection object has received a message. (data contains the buffer) */
+	,FDEVP_CNX_MSG_RECV
+			 
+	/* A connection object has encountered an error (disconnected). */
+	,FDEVP_CNX_ERROR
+	
+	/* Endpoints of a connection have been changed (multihomed SCTP). */
+	,FDEVP_CNX_EP_CHANGE
+	
+	/* A new connection has been established (data contains the appropriate info) */
+	,FDEVP_CNX_INCOMING
 	
 	/* The PSM state is expired */
 	,FDEVP_PSM_TIMEOUT
+	
 };
 const char * fd_pev_str(int event);
 #define CHECK_EVENT( _e ) \
 	(((int)(_e) >= FDEVP_DUMP_ALL) && ((int)(_e) <= FDEVP_PSM_TIMEOUT))
+
+/* The data structure for FDEVP_CNX_INCOMING events */
+struct cnx_incoming {
+	struct msg	* cer;		/* the CER message received on this connection */
+	struct cnxctx	* cnx;		/* The connection context */
+	int  		  validate;	/* The peer is new, it must be validated (by an extension) or error CEA to be sent */
+};
 
 /* Structure to store a sent request */
@@ -173,45 +190,14 @@
 };
 
-/* The connection context structure */
-struct cnxctx {
-	int 		cc_socket;	/* The socket object of the connection -- <=0 if no socket is created */
-	
-	struct fifo   **cc_events;	/* Location of the events list to send connection events */
-	
-	int 		cc_proto;	/* IPPROTO_TCP or IPPROTO_SCTP */
-	int		cc_tls;		/* Is TLS already started ? */
-	
-	uint16_t	cc_port;	/* Remote port of the connection, when we are client */
-	struct fd_list	cc_ep_remote;	/* The remote address(es) of the connection */
-	struct fd_list	cc_ep_local;	/* The local address(es) of the connection */
-	
-	/* If cc_proto == SCTP */
-	struct	{
-		int		str_out;/* Out streams */
-		int		str_in;	/* In streams */
-		int		pairs;	/* max number of pairs ( = min(in, out)) */
-		int		next;	/* # of stream the next message will be sent to */
-	} 		cc_sctp_para;
-	
-	/* If cc_tls == true */
-	struct {
-		int				 mode; 		/* GNUTLS_CLIENT / GNUTLS_SERVER */
-		gnutls_session_t 		 session;	/* Session object (stream #0 in case of SCTP) */
-	}		cc_tls_para;
-	
-	/* If both conditions */
-	struct {
-		gnutls_session_t 		*res_sessions;	/* Sessions of other pairs of streams, resumed from the first */
-		/* Buffers, threads, ... */
-	}		cc_sctp_tls_para;
-};
 
 /* Functions */
-int fd_peer_fini();
+int  fd_peer_fini();
 void fd_peer_dump_list(int details);
 void fd_peer_dump(struct fd_peer * peer, int details);
-int fd_peer_alloc(struct fd_peer ** ptr);
-int fd_peer_free(struct fd_peer ** ptr);
+int  fd_peer_alloc(struct fd_peer ** ptr);
+int  fd_peer_free(struct fd_peer ** ptr);
+int fd_peer_handle_newCER( struct msg ** cer, struct cnxctx ** cnx );
 /* fd_peer_add declared in freeDiameter.h */
+int fd_peer_validate( struct fd_peer * peer );
 
 /* Peer expiry */
@@ -221,25 +207,33 @@
 
 /* Peer state machine */
-int fd_psm_start();
-int fd_psm_begin(struct fd_peer * peer );
-int fd_psm_terminate(struct fd_peer * peer );
+int  fd_psm_start();
+int  fd_psm_begin(struct fd_peer * peer );
+int  fd_psm_terminate(struct fd_peer * peer );
 void fd_psm_abord(struct fd_peer * peer );
 
 /* Server sockets */
 void fd_servers_dump();
-int fd_servers_start();
-void fd_servers_stop();
-
-/* Connection contexts */
-struct cnxctx * fd_cnx_init(int sock, int proto);
-int fd_cnx_handshake(struct cnxctx * conn, int mode);
-
-/* SCTP */
-#ifndef DISABLE_SCTP
-int fd_sctp_create_bind_server( int * socket, uint16_t port );
-int fd_sctp_get_str_info( int socket, int *in, int *out );
-
-#endif /* DISABLE_SCTP */
-
+int  fd_servers_start();
+int  fd_servers_stop();
+
+/* Connection contexts -- there are also definitions in cnxctx.h for the relevant files */
+struct cnxctx * fd_cnx_serv_tcp(uint16_t port, int family, struct fd_endpoint * ep);
+struct cnxctx * fd_cnx_serv_sctp(uint16_t port, struct fd_list * ep_list);
+int             fd_cnx_serv_listen(struct cnxctx * conn);
+struct cnxctx * fd_cnx_serv_accept(struct cnxctx * serv);
+struct cnxctx * fd_cnx_cli_connect_tcp(sSA * sa, socklen_t addrlen);
+struct cnxctx * fd_cnx_cli_connect_sctp(int no_ip6, uint16_t port, struct fd_list * list);
+int             fd_cnx_start_clear(struct cnxctx * conn, int loop);
+int             fd_cnx_handshake(struct cnxctx * conn, int mode, char * priority, void * alt_creds);
+char *          fd_cnx_getid(struct cnxctx * conn);
+int		fd_cnx_getproto(struct cnxctx * conn);
+int		fd_cnx_getTLS(struct cnxctx * conn);
+int             fd_cnx_getcred(struct cnxctx * conn, const gnutls_datum_t **cert_list, unsigned int *cert_list_size);
+int             fd_cnx_getendpoints(struct cnxctx * conn, struct fd_list * local, struct fd_list * remote);
+char *          fd_cnx_getremoteid(struct cnxctx * conn);
+int             fd_cnx_receive(struct cnxctx * conn, struct timespec * timeout, unsigned char **buf, size_t * len);
+int             fd_cnx_recv_setaltfifo(struct cnxctx * conn, struct fifo * alt_fifo); /* send FDEVP_CNX_MSG_RECV event to the fifo list */
+int             fd_cnx_send(struct cnxctx * conn, unsigned char * buf, size_t len);
+void            fd_cnx_destroy(struct cnxctx * conn);
 
 
