Navigation


source: freeDiameter/libfdcore/fdcore-internal.h @ 706:4ffbc9f1e922

Last change on this file since 706:4ffbc9f1e922 was 706:4ffbc9f1e922, checked in by Sebastien Decugis <sdecugis@nict.go.jp>, 11 years ago

Large UNTESTED commit with the following changes:

  • Improved DiameterIdentity? handling (esp. interationalization issues), and improve efficiency of some string operations in peers, sessions, and dictionary modules (closes #7)
  • Cleanup in the session module to free only unreferenced sessions (#16)
  • Removed fd_cpu_flush_cache(), replaced by more robust alternatives.
  • Improved peer state machine algorithm to counter SCTP multistream race condition.
File size: 14.3 KB
Line 
1/*********************************************************************************************************
2* Software License Agreement (BSD License)                                                               *
3* Author: Sebastien Decugis <sdecugis@nict.go.jp>                                                        *
4*                                                                                                        *
5* Copyright (c) 2011, WIDE Project and NICT                                                              *
6* All rights reserved.                                                                                   *
7*                                                                                                        *
8* Redistribution and use of this software in source and binary forms, with or without modification, are  *
9* permitted provided that the following conditions are met:                                              *
10*                                                                                                        *
11* * Redistributions of source code must retain the above                                                 *
12*   copyright notice, this list of conditions and the                                                    *
13*   following disclaimer.                                                                                *
14*                                                                                                        *
15* * Redistributions in binary form must reproduce the above                                              *
16*   copyright notice, this list of conditions and the                                                    *
17*   following disclaimer in the documentation and/or other                                               *
18*   materials provided with the distribution.                                                            *
19*                                                                                                        *
20* * Neither the name of the WIDE Project or NICT nor the                                                 *
21*   names of its contributors may be used to endorse or                                                  *
22*   promote products derived from this software without                                                  *
23*   specific prior written permission of WIDE Project and                                                *
24*   NICT.                                                                                                *
25*                                                                                                        *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT     *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS    *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                                             *
34*********************************************************************************************************/
35
36/* This file contains the definitions for internal use in the freeDiameter core library */
37
38#ifndef _FDCORE_INTERNAL_H
39#define _FDCORE_INTERNAL_H
40
41#include <freeDiameter/freeDiameter-host.h>
42#include <freeDiameter/libfdcore.h>
43
44#ifdef DISABLE_SCTP
45#undef IPPROTO_SCTP
46#define IPPROTO_SCTP    (2 = 4) /* some compilation error to spot the references */
47#endif /* DISABLE_SCTP */
48
49/* Timeout for establishing a connection */
50#ifndef CNX_TIMEOUT
51#define  CNX_TIMEOUT    10      /* in seconds */
52#endif /* CNX_TIMEOUT */
53
54/* Timeout for receiving a CER after incoming connection is established */
55#ifndef INCNX_TIMEOUT
56#define  INCNX_TIMEOUT   20     /* in seconds */
57#endif /* INCNX_TIMEOUT */
58
59/* Timeout for receiving a CEA after CER is sent */
60#ifndef CEA_TIMEOUT
61#define  CEA_TIMEOUT    10      /* in seconds */
62#endif /* CEA_TIMEOUT */
63
64/* The timeout value to wait for answer to a DPR */
65#ifndef DPR_TIMEOUT
66#define DPR_TIMEOUT     15      /* in seconds */
67#endif /* DPR_TIMEOUT */
68
69/* The Vendor-Id to advertise in CER/CEA */
70#ifndef MY_VENDOR_ID
71#define MY_VENDOR_ID    0       /* Reserved value to tell it must be ignored */
72#endif /* MY_VENDOR_ID */
73
74
75
76/* Configuration */
77int fd_conf_init();
78int fd_conf_deinit();
79void fd_conf_dump();
80int fd_conf_parse();
81int fddparse(struct fd_config * conf); /* yacc generated */
82
83/* Extensions */
84int fd_ext_add( char * filename, char * conffile );
85int fd_ext_load();
86void fd_ext_dump(void);
87int fd_ext_term(void);
88
89/* Messages */
90int fd_msg_init(void);
91struct dict_object * fd_dict_avp_OSI; /* Origin-State-Id */
92struct dict_object * fd_dict_cmd_CER; /* Capabilities-Exchange-Request */
93struct dict_object * fd_dict_cmd_DWR; /* Device-Watchdog-Request */
94struct dict_object * fd_dict_avp_DC;  /* Disconnect-Cause */
95struct dict_object * fd_dict_cmd_DPR; /* Disconnect-Peer-Request */
96
97/* Global message queues */
98extern struct fifo * fd_g_incoming; /* all messages received from other peers, except local messages (CER, ...) */
99extern struct fifo * fd_g_outgoing; /* messages to be sent to other peers on the network following routing procedure */
100extern struct fifo * fd_g_local; /* messages to be handled to local extensions */
101/* Message queues */
102int fd_queues_init(void);
103int fd_queues_fini(struct fifo ** queue);
104
105/* Trigged events */
106int fd_event_trig_call_cb(int trigger_val);
107int fd_event_trig_fini(void);
108
109/* Create all the dictionary objects defined in the Diameter base RFC. */
110int fd_dict_base_protocol(struct dictionary * dict);
111
112/* Routing */
113int fd_rtdisp_init(void);
114int fd_rtdisp_cleanstop(void);
115int fd_rtdisp_fini(void);
116int fd_rtdisp_cleanup(void);
117
118/* Sentinel for the sent requests list */
119struct sr_list {
120        struct fd_list  srs; /* requests ordered by hop-by-hop id */
121        struct fd_list  exp; /* requests that have a timeout set, ordered by timeout */
122        pthread_mutex_t mtx; /* mutex to protect these lists */
123        pthread_cond_t  cnd; /* cond var used by the thread that handles timeouts */
124        pthread_t       thr; /* the thread that handles timeouts (and calls the anscb) */
125};
126
127/* Peers */
128struct fd_peer { /* The "real" definition of the peer structure */
129       
130        /* The public data */
131        struct peer_hdr  p_hdr;
132       
133        /* Eye catcher, EYEC_PEER */
134        int              p_eyec;
135        #define EYEC_PEER       0x373C9336
136       
137        /* Origin of this peer object, for debug */
138        char            *p_dbgorig;
139       
140        /* State of the peer, and its lock */
141        enum peer_state  p_state;
142        pthread_mutex_t  p_state_mtx;
143       
144        /* Chaining in peers sublists */
145        struct fd_list   p_actives;     /* list of peers in the STATE_OPEN state -- used by routing */
146        struct fd_list   p_expiry;      /* list of expiring peers, ordered by their timeout value */
147        struct timespec  p_exp_timer;   /* Timestamp where the peer will expire; updated each time activity is seen on the peer (except DW) */
148       
149        /* Some flags influencing the peer state machine */
150        struct {
151                unsigned pf_responder   : 1;    /* The peer has been created to handle incoming connection */
152                unsigned pf_delete      : 1;    /* Destroy the peer when the connection is terminated */
153                unsigned pf_localterm   : 1;    /* If the latest DPR/DPA was initiated from this side */
154               
155                unsigned pf_dw_pending  : 1;    /* A DWR message was sent and not answered yet */
156               
157                unsigned pf_cnx_pb      : 1;    /* The peer was disconnected because of watchdogs; must exchange 3 watchdogs before putting back to normal */
158                unsigned pf_reopen_cnt  : 2;    /* remaining DW to be exchanged after re-established connection */
159               
160        }                p_flags;
161       
162        /* The events queue, peer state machine thread, timer for states timeouts */
163        struct fifo     *p_events;      /* The mutex of this FIFO list protects also the state and timer information */
164        pthread_t        p_psm;
165        struct timespec  p_psm_timer;
166       
167        /* Outgoing message queue, and thread managing sending the messages */
168        struct fifo     *p_tosend;
169        pthread_t        p_outthr;
170       
171        /* The next hop-by-hop id value for the link, only read & modified by p_outthr */
172        uint32_t         p_hbh;
173       
174        /* Sent requests (for fallback), list of struct sentreq ordered by hbh */
175        struct sr_list   p_sr;
176       
177        /* Data for transitional states before the peer is in OPEN state */
178        struct {
179                struct cnxctx * p_receiver;     /* Only used in case of election */
180                struct msg    * p_cer;          /* Only used in case of election */
181               
182                pthread_t       p_ini_thr;      /* Initiator thread for establishing a connection */
183                struct fd_list  p_connparams;   /* The list of connection attempts, see p_cnx.c */
184        };
185       
186        /* connection context: socket and related information */
187        struct cnxctx   *p_cnxctx;
188       
189        /* Callback for peer validation after the handshake */
190        int             (*p_cb2)(struct peer_info *);
191       
192        /* Callback on initial connection success / failure after the peer was added */
193        void            (*p_cb)(struct peer_info *, void *);
194        void            *p_cb_data;
195       
196};
197#define CHECK_PEER( _p ) \
198        (((_p) != NULL) && (((struct fd_peer *)(_p))->p_eyec == EYEC_PEER))
199
200#define fd_peer_getstate(peer)  fd_peer_get_state((struct peer_hdr *)(peer))
201
202
203/* Events codespace for struct fd_peer->p_events */
204enum {
205        /* Dump all info about this peer in the debug log */
206         FDEVP_DUMP_ALL = 1500
207       
208        /* request to terminate this peer : disconnect, requeue all messages */
209        ,FDEVP_TERMINATE
210       
211        /* A connection object has received a message. (data contains the buffer) */
212        ,FDEVP_CNX_MSG_RECV
213                         
214        /* A connection object has encountered an error (disconnected). */
215        ,FDEVP_CNX_ERROR
216       
217        /* Endpoints of a connection have been changed (multihomed SCTP). */
218        ,FDEVP_CNX_EP_CHANGE
219       
220        /* The connection is being shutdown (SCTP notification). */
221        ,FDEVP_CNX_SHUTDOWN
222       
223        /* A new connection (with a CER) has been received */
224        ,FDEVP_CNX_INCOMING
225       
226        /* A new connection has been established to the remote peer (event data is the cnxctx object) */
227        ,FDEVP_CNX_ESTABLISHED
228       
229        /* A connection attempt (initiator side) has failed */
230        ,FDEVP_CNX_FAILED
231       
232        /* The PSM state is expired */
233        ,FDEVP_PSM_TIMEOUT
234       
235};
236#define CHECK_PEVENT( _e ) \
237        (((int)(_e) >= FDEVP_DUMP_ALL) && ((int)(_e) <= FDEVP_PSM_TIMEOUT))
238/* The following macro is actually called in p_psm.c -- another solution would be to declare it static inline */
239#define DECLARE_PEV_STR()                               \
240const char * fd_pev_str(int event)                      \
241{                                                       \
242        switch (event) {                                \
243                case_str(FDEVP_DUMP_ALL);               \
244                case_str(FDEVP_TERMINATE);              \
245                case_str(FDEVP_CNX_MSG_RECV);           \
246                case_str(FDEVP_CNX_ERROR);              \
247                case_str(FDEVP_CNX_EP_CHANGE);          \
248                case_str(FDEVP_CNX_INCOMING);           \
249                case_str(FDEVP_CNX_ESTABLISHED);        \
250                case_str(FDEVP_CNX_FAILED);             \
251                case_str(FDEVP_PSM_TIMEOUT);            \
252        }                                               \
253        TRACE_DEBUG(FULL, "Unknown event : %d", event); \
254        return "Unknown event";                         \
255}
256const char * fd_pev_str(int event);
257
258/* The data structure for FDEVP_CNX_INCOMING event */
259struct cnx_incoming {
260        struct msg      * cer;          /* the CER message received on this connection */
261        struct cnxctx   * cnx;          /* The connection context */
262        int               validate;     /* The peer is new, it must be validated (by an extension) or error CEA to be sent */
263};
264
265
266/* Functions */
267int  fd_peer_fini();
268void fd_peer_dump_list(int details);
269void fd_peer_dump(struct fd_peer * peer, int details);
270int  fd_peer_alloc(struct fd_peer ** ptr);
271int  fd_peer_free(struct fd_peer ** ptr);
272int fd_peer_handle_newCER( struct msg ** cer, struct cnxctx ** cnx );
273/* fd_peer_add declared in freeDiameter.h */
274int fd_peer_validate( struct fd_peer * peer );
275void fd_peer_failover_msg(struct fd_peer * peer);
276
277/* Peer expiry */
278int fd_p_expi_init(void);
279int fd_p_expi_fini(void);
280int fd_p_expi_update(struct fd_peer * peer );
281
282/* Peer state machine */
283int  fd_psm_start();
284int  fd_psm_begin(struct fd_peer * peer );
285int  fd_psm_terminate(struct fd_peer * peer, char * reason );
286void fd_psm_abord(struct fd_peer * peer );
287void fd_psm_next_timeout(struct fd_peer * peer, int add_random, int delay);
288int fd_psm_change_state(struct fd_peer * peer, int new_state);
289void fd_psm_cleanup(struct fd_peer * peer, int terminate);
290
291/* Peer out */
292int fd_out_send(struct msg ** msg, struct cnxctx * cnx, struct fd_peer * peer, uint32_t flags);
293int fd_out_start(struct fd_peer * peer);
294int fd_out_stop(struct fd_peer * peer);
295
296/* Initiating connections */
297int fd_p_cnx_init(struct fd_peer * peer);
298void fd_p_cnx_abort(struct fd_peer * peer, int cleanup_all);
299
300/* Peer sent requests cache */
301int fd_p_sr_store(struct sr_list * srlist, struct msg **req, uint32_t *hbhloc, uint32_t hbh_restore);
302int fd_p_sr_fetch(struct sr_list * srlist, uint32_t hbh, struct msg **req);
303int fd_p_sr_start(struct sr_list * srlist);
304int fd_p_sr_stop(struct sr_list * srlist);
305void fd_p_sr_failover(struct sr_list * srlist);
306
307/* Local Link messages (CER/CEA, DWR/DWA, DPR/DPA) */
308int fd_p_ce_msgrcv(struct msg ** msg, int req, struct fd_peer * peer);
309int fd_p_ce_handle_newCER(struct msg ** msg, struct fd_peer * peer, struct cnxctx ** cnx, int valid);
310int fd_p_ce_handle_newcnx(struct fd_peer * peer, struct cnxctx * initiator);
311int fd_p_ce_process_receiver(struct fd_peer * peer);
312void fd_p_ce_clear_cnx(struct fd_peer * peer, struct cnxctx ** cnx_kept);
313int fd_p_dw_handle(struct msg ** msg, int req, struct fd_peer * peer);
314int fd_p_dw_timeout(struct fd_peer * peer);
315int fd_p_dw_reopen(struct fd_peer * peer);
316int fd_p_dp_handle(struct msg ** msg, int req, struct fd_peer * peer);
317int fd_p_dp_initiate(struct fd_peer * peer, char * reason);
318int fd_p_dp_newdelay(struct fd_peer * peer);
319
320/* Active peers -- routing process should only ever take the read lock, the write lock is managed by PSMs */
321extern struct fd_list fd_g_activ_peers;
322extern pthread_rwlock_t fd_g_activ_peers_rw; /* protect the list */
323
324
325/* Server sockets */
326void fd_servers_dump();
327int  fd_servers_start();
328int  fd_servers_stop();
329
330/* Connection contexts -- there are also definitions in cnxctx.h for the relevant files */
331struct cnxctx * fd_cnx_serv_tcp(uint16_t port, int family, struct fd_endpoint * ep);
332struct cnxctx * fd_cnx_serv_sctp(uint16_t port, struct fd_list * ep_list);
333int             fd_cnx_serv_listen(struct cnxctx * conn);
334struct cnxctx * fd_cnx_serv_accept(struct cnxctx * serv);
335struct cnxctx * fd_cnx_cli_connect_tcp(sSA * sa, socklen_t addrlen);
336struct cnxctx * fd_cnx_cli_connect_sctp(int no_ip6, uint16_t port, struct fd_list * list);
337int             fd_cnx_start_clear(struct cnxctx * conn, int loop);
338void            fd_cnx_sethostname(struct cnxctx * conn, DiamId_t hn);
339int             fd_cnx_handshake(struct cnxctx * conn, int mode, char * priority, void * alt_creds);
340char *          fd_cnx_getid(struct cnxctx * conn);
341int             fd_cnx_getproto(struct cnxctx * conn);
342int             fd_cnx_getTLS(struct cnxctx * conn);
343int             fd_cnx_isMultichan(struct cnxctx * conn);
344int             fd_cnx_getcred(struct cnxctx * conn, const gnutls_datum_t **cert_list, unsigned int *cert_list_size);
345int             fd_cnx_get_local_eps(struct fd_list * list);
346int             fd_cnx_getremoteeps(struct cnxctx * conn, struct fd_list * eps);
347char *          fd_cnx_getremoteid(struct cnxctx * conn);
348int             fd_cnx_receive(struct cnxctx * conn, struct timespec * timeout, unsigned char **buf, size_t * len);
349int             fd_cnx_recv_setaltfifo(struct cnxctx * conn, struct fifo * alt_fifo); /* send FDEVP_CNX_MSG_RECV event to the fifo list */
350int             fd_cnx_send(struct cnxctx * conn, unsigned char * buf, size_t len, uint32_t flags);
351void            fd_cnx_destroy(struct cnxctx * conn);
352
353/* Flags for the fd_cnx_send function : */
354#define FD_CNX_ORDERED          (1 << 0)        /* All messages sent with this flag set will be delivered in the same order. No guarantee on other messages */
355
356#endif /* _FDCORE_INTERNAL_H */
Note: See TracBrowser for help on using the repository browser.