Mercurial > hg > freeDiameter
annotate freeDiameter/dispatch.c @ 112:6a294d977878
Default to add Session-Id in answers
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Tue, 08 Dec 2009 15:24:34 +0900 |
parents | 79e69f069831 |
children | 2cf9f62cdcc7 |
rev | line source |
---|---|
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
1 /********************************************************************************************************* |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
2 * Software License Agreement (BSD License) * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
3 * Author: Sebastien Decugis <sdecugis@nict.go.jp> * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
4 * * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
5 * Copyright (c) 2009, WIDE Project and NICT * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
6 * All rights reserved. * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
7 * * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
8 * Redistribution and use of this software in source and binary forms, with or without modification, are * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
9 * permitted provided that the following conditions are met: * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
10 * * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
11 * * Redistributions of source code must retain the above * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
12 * copyright notice, this list of conditions and the * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
13 * following disclaimer. * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
14 * * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
15 * * Redistributions in binary form must reproduce the above * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
16 * copyright notice, this list of conditions and the * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
17 * following disclaimer in the documentation and/or other * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
18 * materials provided with the distribution. * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
19 * * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
20 * * Neither the name of the WIDE Project or NICT nor the * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
21 * names of its contributors may be used to endorse or * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
22 * promote products derived from this software without * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
23 * specific prior written permission of WIDE Project and * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
24 * NICT. * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
25 * * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
28 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
34 *********************************************************************************************************/ |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
35 |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
36 #include "fD.h" |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
37 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
38 /* Add an application into the peer's supported apps */ |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
39 int fd_disp_app_support ( struct dict_object * app, struct dict_object * vendor, int auth, int acct ) |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
40 { |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
41 application_id_t aid = 0; |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
42 vendor_id_t vid = 0; |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
43 |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
44 TRACE_ENTRY("%p %p %d %d", app, vendor, auth, acct); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
45 CHECK_PARAMS( app && (auth || acct) ); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
46 |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
47 { |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
48 enum dict_object_type type = 0; |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
49 struct dict_application_data data; |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
50 CHECK_FCT( fd_dict_gettype(app, &type) ); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
51 CHECK_PARAMS( type == DICT_APPLICATION ); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
52 CHECK_FCT( fd_dict_getval(app, &data) ); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
53 aid = data.application_id; |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
54 } |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
55 |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
56 if (vendor) { |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
57 enum dict_object_type type = 0; |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
58 struct dict_vendor_data data; |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
59 CHECK_FCT( fd_dict_gettype(vendor, &type) ); |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
60 CHECK_PARAMS( type == DICT_VENDOR ); |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
61 CHECK_FCT( fd_dict_getval(vendor, &data) ); |
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
62 vid = data.vendor_id; |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
63 } |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
64 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
65 return fd_app_merge(&fd_g_config->cnf_apps, aid, vid, auth, acct); |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
66 } |
87 | 67 |
68 | |
90
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
69 /* The dispatching thread(s) */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
70 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
71 enum thread_state { INITIAL = 0, RUNNING = 1, TERMINATED = 2 }; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
72 static void cleanup_state(void * state_loc) |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
73 { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
74 if (state_loc) |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
75 *(enum thread_state *)state_loc = TERMINATED; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
76 } |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
77 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
78 static pthread_mutex_t order_lock = PTHREAD_MUTEX_INITIALIZER; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
79 static enum { RUN = 0, STOP = 1 } order_val; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
80 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
81 static void * dispatch_thread(void * arg) |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
82 { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
83 TRACE_ENTRY("%p", arg); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
84 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
85 /* Set the thread name */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
86 { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
87 char buf[48]; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
88 snprintf(buf, sizeof(buf), "Dispatch %p", arg); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
89 fd_log_threadname ( buf ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
90 } |
87 | 91 |
90
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
92 pthread_cleanup_push( cleanup_state, arg ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
93 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
94 /* Mark the thread running */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
95 *(enum thread_state *)arg = RUNNING; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
96 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
97 do { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
98 struct msg * msg; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
99 struct msg_hdr * hdr; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
100 int is_req = 0; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
101 struct session * sess; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
102 enum disp_action action; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
103 const char * ec = NULL; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
104 const char * em = NULL; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
105 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
106 /* Test the environment */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
107 { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
108 int must_stop; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
109 CHECK_POSIX_DO( pthread_mutex_lock(&order_lock), goto end ); /* we lock to flush the caches */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
110 must_stop = (order_val == STOP); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
111 CHECK_POSIX_DO( pthread_mutex_unlock(&order_lock), goto end ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
112 if (must_stop) |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
113 goto end; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
114 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
115 pthread_testcancel(); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
116 } |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
117 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
118 /* Ok, we are allowed to run */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
119 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
120 /* Get the next message from the queue */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
121 CHECK_FCT_DO( fd_fifo_get ( fd_g_local, &msg ), goto fatal_error ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
122 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
123 if (TRACE_BOOL(FULL)) { |
104
79e69f069831
Increase level for some debug messages
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
91
diff
changeset
|
124 TRACE_DEBUG(FULL, "Picked next message"); |
79e69f069831
Increase level for some debug messages
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
91
diff
changeset
|
125 fd_msg_dump_one(ANNOYING, msg); |
90
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
126 } |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
127 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
128 /* Read the message header */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
129 CHECK_FCT_DO( fd_msg_hdr(msg, &hdr), goto fatal_error ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
130 is_req = hdr->msg_flags & CMD_FLAG_REQUEST; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
131 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
132 /* Note: if the message is for local delivery, we should test for duplicate |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
133 (draft-asveren-dime-dupcons-00). This may conflict with path validation decisions, no clear answer yet */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
134 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
135 /* At this point, we probably need to understand the message content, so parse the message */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
136 CHECK_FCT_DO( fd_msg_parse_dict ( msg, fd_g_config->cnf_dict ), /* Ignore error */); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
137 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
138 /* First, if the original request was registered with a callback and we receive the answer, call it. */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
139 if ( ! is_req ) { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
140 struct msg * qry; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
141 void (*anscb)(void *, struct msg **) = NULL; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
142 void * data = NULL; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
143 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
144 /* Retrieve the corresponding query */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
145 CHECK_FCT_DO( fd_msg_answ_getq( msg, &qry ), goto fatal_error ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
146 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
147 /* Retrieve any registered handler */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
148 CHECK_FCT_DO( fd_msg_anscb_get( qry, &anscb, &data ), goto fatal_error ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
149 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
150 /* If a callback was registered, pass the message to it */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
151 if (anscb != NULL) { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
152 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
153 TRACE_DEBUG(FULL, "Calling callback registered when query was sent (%p, %p)", anscb, data); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
154 (*anscb)(data, &msg); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
155 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
156 if (msg == NULL) { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
157 /* Ok, the message is now handled, we can skip to the next one */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
158 continue; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
159 } |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
160 } |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
161 } |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
162 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
163 /* Retrieve the session of the message */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
164 CHECK_FCT_DO( fd_msg_sess_get(fd_g_config->cnf_dict, msg, &sess, NULL), goto fatal_error ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
165 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
166 /* Now, call any callback registered for the message */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
167 CHECK_FCT_DO( fd_msg_dispatch ( &msg, sess, &action, &ec), goto fatal_error ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
168 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
169 /* Now, act depending on msg and action and ec */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
170 if (!msg) |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
171 continue; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
172 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
173 switch ( action ) { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
174 case DISP_ACT_CONT: |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
175 /* No callback has handled the message, let's reply with a generic error */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
176 em = "The message was not handled by any extension callback"; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
177 ec = "DIAMETER_COMMAND_UNSUPPORTED"; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
178 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
179 case DISP_ACT_ERROR: |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
180 /* We have a problem with delivering the message */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
181 if (ec == NULL) { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
182 ec = "DIAMETER_UNABLE_TO_COMPLY"; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
183 } |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
184 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
185 if (!is_req) { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
186 TRACE_DEBUG(INFO, "Received an answer to a localy issued query, but no handler processed this answer!"); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
187 fd_msg_dump_walk(INFO, msg); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
188 fd_msg_free(msg); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
189 msg = NULL; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
190 break; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
191 } |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
192 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
193 /* Create an answer with the error code and message */ |
112
6a294d977878
Default to add Session-Id in answers
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
104
diff
changeset
|
194 CHECK_FCT_DO( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, &msg, 0 ), goto fatal_error ); |
90
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
195 CHECK_FCT_DO( fd_msg_rescode_set(msg, (char *)ec, (char *)em, NULL, 1 ), goto fatal_error ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
196 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
197 case DISP_ACT_SEND: |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
198 /* Now, send the message */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
199 CHECK_FCT_DO( fd_fifo_post(fd_g_outgoing, &msg), goto fatal_error ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
200 } |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
201 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
202 /* We're done with this message */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
203 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
204 } while (1); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
205 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
206 fatal_error: |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
207 TRACE_DEBUG(INFO, "An error occurred in dispatch module! Thread is terminating..."); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
208 CHECK_FCT_DO(fd_event_send(fd_g_config->cnf_main_ev, FDEV_TERMINATE, 0, NULL), ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
209 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
210 end: |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
211 /* Mark the thread as terminated */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
212 pthread_cleanup_pop(1); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
213 return NULL; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
214 } |
89
3f8b437bcb66
Added some default routing handlers
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
87
diff
changeset
|
215 |
90
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
216 /* Later (same as routing): TODO("Set threshold on local queue"); */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
217 static pthread_t my_dispatch = (pthread_t)NULL; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
218 static enum thread_state my_state = INITIAL; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
219 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
220 /* Initialize the Dispatch module */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
221 int fd_disp_init(void) |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
222 { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
223 order_val = RUN; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
224 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
225 CHECK_POSIX( pthread_create( &my_dispatch, NULL, dispatch_thread, &my_state ) ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
226 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
227 return 0; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
228 } |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
229 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
230 int fd_disp_cleanstop(void) |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
231 { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
232 CHECK_POSIX( pthread_mutex_lock(&order_lock) ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
233 order_val = STOP; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
234 CHECK_POSIX( pthread_mutex_unlock(&order_lock) ); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
235 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
236 return 0; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
237 } |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
238 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
239 int fd_disp_fini(void) |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
240 { |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
241 /* Wait for a few milliseconds for the thread to complete, by monitoring my_state */ |
91
d5ba667e17c0
Added note about missing code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
90
diff
changeset
|
242 TODO("Not implemented yet"); |
90
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
243 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
244 /* Then if needed, cancel the thread */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
245 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
246 /* Remove all remaining handlers */ |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
247 fd_disp_unregister_all(); |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
248 |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
249 return ENOTSUP; |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
250 } |
2c9444152e4b
Added the dispatch thread code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
89
diff
changeset
|
251 |