Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
config_system.c File Reference
#include "asterisk.h"
#include <pjsip.h>
#include <pjlib.h>
#include "asterisk/res_pjsip.h"
#include "asterisk/sorcery.h"
#include "include/res_pjsip_private.h"
#include "asterisk/threadpool.h"
#include "asterisk/dns.h"
#include "asterisk/res_pjsip_cli.h"
Include dependency graph for config_system.c:

Go to the source code of this file.

Data Structures

struct  system_config
 

Macros

#define DEFAULT_TIMER_B   32000
 
#define DEFAULT_TIMER_T1   500
 
#define TIMER_T1_MIN   100
 

Functions

void ast_sip_destroy_system (void)
 
void ast_sip_initialize_dns (void)
 
int ast_sip_initialize_system (void)
 
static struct system_configget_system_cfg (void)
 
int sip_cli_print_system (struct ast_sip_cli_context *context)
 
void sip_get_threadpool_options (struct ast_threadpool_options *threadpool_options)
 
static void * system_alloc (const char *name)
 
static int system_apply (const struct ast_sorcery *sorcery, void *obj)
 
static int system_create_resolver_and_set_nameservers (void *data)
 

Variables

static struct ast_threadpool_options sip_threadpool_options
 
static struct ast_sorcerysystem_sorcery
 

Macro Definition Documentation

◆ DEFAULT_TIMER_B

#define DEFAULT_TIMER_B   32000

Definition at line 33 of file config_system.c.

Referenced by ast_sip_initialize_system().

◆ DEFAULT_TIMER_T1

#define DEFAULT_TIMER_T1   500

Definition at line 32 of file config_system.c.

Referenced by ast_sip_initialize_system().

◆ TIMER_T1_MIN

#define TIMER_T1_MIN   100

Definition at line 31 of file config_system.c.

Referenced by system_apply().

Function Documentation

◆ ast_sip_destroy_system()

void ast_sip_destroy_system ( void  )

Definition at line 245 of file config_system.c.

References ast_sorcery_unref.

Referenced by unload_pjsip().

246 {
248 }
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition: sorcery.h:1502
static struct ast_sorcery * system_sorcery
Definition: config_system.c:75

◆ ast_sip_initialize_dns()

void ast_sip_initialize_dns ( void  )

Definition at line 314 of file config_system.c.

References ast_sip_push_task_wait_servant(), NULL, and system_create_resolver_and_set_nameservers().

Referenced by load_module(), and reload_configuration_task().

315 {
317 }
static int system_create_resolver_and_set_nameservers(void *data)
#define NULL
Definition: resample.c:96
int ast_sip_push_task_wait_servant(struct ast_taskprocessor *serializer, int(*sip_task)(void *), void *task_data)
Push a task to SIP servants and wait for it to complete.
Definition: res_pjsip.c:5204

◆ ast_sip_initialize_system()

int ast_sip_initialize_system ( void  )

Definition at line 175 of file config_system.c.

References __stringify, system_config::accept_multiple_sdp_answers, ao2_cleanup, ao2_container_count(), ast_log, AST_RETRIEVE_FLAG_ALL, AST_RETRIEVE_FLAG_MULTIPLE, ast_sorcery_alloc(), ast_sorcery_apply_default, ast_sorcery_load(), ast_sorcery_object_field_register, ast_sorcery_object_register_no_reload, ast_sorcery_open, ast_sorcery_retrieve_by_fields(), ast_sorcery_unref, system_config::compactheaders, DEFAULT_TIMER_B, DEFAULT_TIMER_T1, system_config::disable_rport, system_config::disable_tcp_switch, FLDSET, system_config::follow_early_media_fork, LOG_ERROR, NULL, OPT_BOOL_T, OPT_NOOP_T, OPT_UINT_T, RAII_VAR, system_alloc(), system_apply(), system_config::threadpool, system_config::timerb, and system_config::timert1.

Referenced by load_module().

176 {
177  RAII_VAR(struct ao2_container *, system_configs, NULL, ao2_cleanup);
178  RAII_VAR(struct system_config *, system, NULL, ao2_cleanup);
179 
181  if (!system_sorcery) {
182  ast_log(LOG_ERROR, "Failed to open SIP system sorcery\n");
183  return -1;
184  }
185 
186  ast_sorcery_apply_default(system_sorcery, "system", "config", "pjsip.conf,criteria=type=system,single_object=yes,explicit_name=system");
187 
189  ast_log(LOG_ERROR, "Failed to register with sorcery (is res_sorcery_config loaded?)\n");
192  return -1;
193  }
194 
195  ast_sorcery_object_field_register(system_sorcery, "system", "type", "", OPT_NOOP_T, 0, 0);
197  OPT_UINT_T, 0, FLDSET(struct system_config, timert1));
199  OPT_UINT_T, 0, FLDSET(struct system_config, timerb));
200  ast_sorcery_object_field_register(system_sorcery, "system", "compact_headers", "no",
201  OPT_BOOL_T, 1, FLDSET(struct system_config, compactheaders));
202  ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_initial_size", "0",
203  OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.initial_size));
204  ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_auto_increment", "5",
205  OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.auto_increment));
206  ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_idle_timeout", "60",
207  OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.idle_timeout));
208  ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_max_size", "50",
209  OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.max_size));
210  ast_sorcery_object_field_register(system_sorcery, "system", "disable_tcp_switch", "yes",
211  OPT_BOOL_T, 1, FLDSET(struct system_config, disable_tcp_switch));
212  ast_sorcery_object_field_register(system_sorcery, "system", "follow_early_media_fork", "yes",
213  OPT_BOOL_T, 1, FLDSET(struct system_config, follow_early_media_fork));
214  ast_sorcery_object_field_register(system_sorcery, "system", "accept_multiple_sdp_answers", "no",
215  OPT_BOOL_T, 1, FLDSET(struct system_config, accept_multiple_sdp_answers));
216  ast_sorcery_object_field_register(system_sorcery, "system", "disable_rport", "no",
217  OPT_BOOL_T, 1, FLDSET(struct system_config, disable_rport));
218 
220 
221  system_configs = ast_sorcery_retrieve_by_fields(system_sorcery, "system",
223 
224  if (ao2_container_count(system_configs)) {
225  return 0;
226  }
227 
228  /* No config present, allocate one and apply defaults */
229  system = ast_sorcery_alloc(system_sorcery, "system", NULL);
230  if (!system) {
231  ast_log(LOG_ERROR, "Unable to allocate default system config.\n");
233  return -1;
234  }
235 
236  if (system_apply(system_sorcery, system)) {
237  ast_log(LOG_ERROR, "Failed to apply default system config.\n");
239  return -1;
240  }
241 
242  return 0;
243 }
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
Perform no matching, return all objects.
Definition: sorcery.h:123
static void * system_alloc(const char *name)
Definition: config_system.c:77
Type for a default handler that should do nothing.
Return all matching objects.
Definition: sorcery.h:120
#define NULL
Definition: resample.c:96
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition: sorcery.h:1502
#define ast_sorcery_object_register_no_reload(sorcery, type, alloc, transform, apply)
Register an object type that is not reloadable.
Definition: sorcery.h:853
#define ast_log
Definition: astobj2.c:42
#define __stringify(x)
Definition: asterisk.h:214
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
Type for default option handler for unsigned integers.
static int system_apply(const struct ast_sorcery *sorcery, void *obj)
Definition: config_system.c:88
static struct ast_threadpool * threadpool
Thread pool for observers.
Definition: sorcery.c:86
#define LOG_ERROR
Definition: logger.h:285
Type for default option handler for bools (ast_true/ast_false)
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition: sorcery.h:477
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
#define DEFAULT_TIMER_B
Definition: config_system.c:33
static struct ast_sorcery * system_sorcery
Definition: config_system.c:75
void * ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
Retrieve an object or multiple objects using specific fields.
Definition: sorcery.c:1897
#define ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object.
Definition: sorcery.h:955
void ast_sorcery_load(const struct ast_sorcery *sorcery)
Inform any wizards to load persistent objects.
Definition: sorcery.c:1377
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define ast_sorcery_open()
Definition: sorcery.h:408
Generic container type.
#define DEFAULT_TIMER_T1
Definition: config_system.c:32

◆ get_system_cfg()

static struct system_config* get_system_cfg ( void  )
static

Definition at line 141 of file config_system.c.

References ao2_find, ao2_ref, AST_RETRIEVE_FLAG_ALL, AST_RETRIEVE_FLAG_MULTIPLE, ast_sorcery_retrieve_by_fields(), and NULL.

Referenced by sip_cli_print_system().

142 {
143  struct system_config *cfg;
144  struct ao2_container *systems;
147 
148  if (!systems) {
149  return NULL;
150  }
151 
152  cfg = ao2_find(systems, NULL, 0);
153  ao2_ref(systems, -1);
154  return cfg;
155 }
Perform no matching, return all objects.
Definition: sorcery.h:123
Return all matching objects.
Definition: sorcery.h:120
#define NULL
Definition: resample.c:96
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static struct ast_sorcery * system_sorcery
Definition: config_system.c:75
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
void * ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
Retrieve an object or multiple objects using specific fields.
Definition: sorcery.c:1897
Generic container type.

◆ sip_cli_print_system()

int sip_cli_print_system ( struct ast_sip_cli_context context)

Definition at line 157 of file config_system.c.

References ao2_ref, ast_sip_cli_print_sorcery_objectset(), ast_sorcery_alloc(), ast_str_append(), get_system_cfg(), NULL, and ast_sip_cli_context::output_buffer.

Referenced by cli_show_settings().

158 {
159  struct system_config *cfg = get_system_cfg();
160 
161  if (!cfg) {
162  cfg = ast_sorcery_alloc(system_sorcery, "system", NULL);
163  if (!cfg) {
164  return -1;
165  }
166  }
167 
168  ast_str_append(&context->output_buffer, 0, "\nSystem Settings:\n\n");
169  ast_sip_cli_print_sorcery_objectset(cfg, context, 0);
170 
171  ao2_ref(cfg, -1);
172  return 0;
173 }
struct ast_str * output_buffer
Definition: res_pjsip_cli.h:36
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
#define NULL
Definition: resample.c:96
#define ao2_ref(o, delta)
Definition: astobj2.h:464
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
static struct ast_sorcery * system_sorcery
Definition: config_system.c:75
static struct system_config * get_system_cfg(void)
int ast_sip_cli_print_sorcery_objectset(void *obj, void *arg, int flags)
Prints a sorcery object&#39;s ast_variable list.
Definition: pjsip_cli.c:36

◆ sip_get_threadpool_options()

void sip_get_threadpool_options ( struct ast_threadpool_options threadpool_options)

Definition at line 70 of file config_system.c.

References sip_threadpool_options.

Referenced by load_module().

71 {
72  *threadpool_options = sip_threadpool_options;
73 }
static struct ast_threadpool_options sip_threadpool_options
Definition: config_system.c:66

◆ system_alloc()

static void* system_alloc ( const char *  name)
static

Definition at line 77 of file config_system.c.

References ast_sorcery_generic_alloc(), and NULL.

Referenced by ast_sip_initialize_system().

78 {
79  struct system_config *system = ast_sorcery_generic_alloc(sizeof(*system), NULL);
80 
81  if (!system) {
82  return NULL;
83  }
84 
85  return system;
86 }
#define NULL
Definition: resample.c:96
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition: sorcery.c:1728

◆ system_apply()

static int system_apply ( const struct ast_sorcery sorcery,
void *  obj 
)
static

Definition at line 88 of file config_system.c.

References system_config::accept_multiple_sdp_answers, ast_log, system_config::auto_increment, ast_threadpool_options::auto_increment, system_config::compactheaders, system_config::disable_rport, system_config::disable_tcp_switch, system_config::follow_early_media_fork, system_config::idle_timeout, ast_threadpool_options::idle_timeout, system_config::initial_size, ast_threadpool_options::initial_size, LOG_WARNING, system_config::max_size, ast_threadpool_options::max_size, system_config::threadpool, TIMER_T1_MIN, system_config::timerb, and system_config::timert1.

Referenced by ast_sip_initialize_system().

89 {
90  struct system_config *system = obj;
91  int min_timerb;
92 
93  if (system->timert1 < TIMER_T1_MIN) {
94  ast_log(LOG_WARNING, "Timer T1 setting is too low. Setting to %d\n", TIMER_T1_MIN);
95  system->timert1 = TIMER_T1_MIN;
96  }
97 
98  min_timerb = 64 * system->timert1;
99 
100  if (system->timerb < min_timerb) {
101  ast_log(LOG_WARNING, "Timer B setting is too low. Setting to %d\n", min_timerb);
102  system->timerb = min_timerb;
103  }
104 
105  pjsip_cfg()->tsx.t1 = system->timert1;
106  pjsip_cfg()->tsx.td = system->timerb;
107 
108  pjsip_cfg()->endpt.follow_early_media_fork = system->follow_early_media_fork;
109 #ifdef HAVE_PJSIP_INV_ACCEPT_MULTIPLE_SDP_ANSWERS
110  pjsip_cfg()->endpt.accept_multiple_sdp_answers = system->accept_multiple_sdp_answers;
111 #else
112  if (system->accept_multiple_sdp_answers) {
114  "The accept_multiple_sdp_answers flag is not supported in this version of pjproject. Ignoring\n");
115  }
116 #endif
117 
118  if (system->compactheaders) {
119 #ifdef HAVE_PJSIP_ENDPOINT_COMPACT_FORM
120  pjsip_cfg()->endpt.use_compact_form = PJ_TRUE;
121 #else
122  extern pj_bool_t pjsip_use_compact_form;
123 
124  pjsip_use_compact_form = PJ_TRUE;
125 #endif
126  }
127 
132 
133  pjsip_cfg()->endpt.disable_tcp_switch =
134  system->disable_tcp_switch ? PJ_TRUE : PJ_FALSE;
135 
136  pjsip_cfg()->endpt.disable_rport = system->disable_rport ? PJ_TRUE : PJ_FALSE;
137 
138  return 0;
139 }
int auto_increment
Number of threads to increment pool by.
Definition: threadpool.h:90
unsigned int disable_rport
Definition: config_system.c:63
int idle_timeout
Time limit in seconds for idle threads.
Definition: threadpool.h:79
unsigned int timert1
Definition: config_system.c:38
int initial_size
Number of threads the pool will start with.
Definition: threadpool.h:100
int max_size
Maximum number of threads a pool may have.
Definition: threadpool.h:110
#define LOG_WARNING
Definition: logger.h:274
unsigned int compactheaders
Definition: config_system.c:42
#define ast_log
Definition: astobj2.c:42
unsigned int disable_tcp_switch
Definition: config_system.c:54
unsigned int follow_early_media_fork
Definition: config_system.c:60
unsigned int timerb
Definition: config_system.c:40
#define TIMER_T1_MIN
Definition: config_system.c:31
static struct ast_threadpool_options sip_threadpool_options
Definition: config_system.c:66
struct system_config::@478 threadpool
unsigned int accept_multiple_sdp_answers
Definition: config_system.c:61

◆ system_create_resolver_and_set_nameservers()

static int system_create_resolver_and_set_nameservers ( void *  data)
static

Definition at line 250 of file config_system.c.

References ao2_container_count(), ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_dns_get_nameservers(), ast_log, ast_sip_get_pjsip_endpoint(), LOG_ERROR, NULL, and status.

Referenced by ast_sip_initialize_dns().

251 {
252  struct ao2_container *discovered_nameservers;
253  struct ao2_iterator it_nameservers;
254  char *nameserver;
255  pj_status_t status;
256  pj_dns_resolver *resolver;
257  pj_str_t nameservers[PJ_DNS_RESOLVER_MAX_NS];
258  unsigned int count = 0;
259 
260  discovered_nameservers = ast_dns_get_nameservers();
261  if (!discovered_nameservers) {
262  ast_log(LOG_ERROR, "Could not retrieve local system nameservers, resorting to system resolution\n");
263  return 0;
264  }
265 
266  if (!ao2_container_count(discovered_nameservers)) {
267  ast_log(LOG_ERROR, "There are no local system nameservers configured, resorting to system resolution\n");
268  ao2_ref(discovered_nameservers, -1);
269  return -1;
270  }
271 
272  if (!(resolver = pjsip_endpt_get_resolver(ast_sip_get_pjsip_endpoint()))) {
273  status = pjsip_endpt_create_resolver(ast_sip_get_pjsip_endpoint(), &resolver);
274  if (status != PJ_SUCCESS) {
275  ast_log(LOG_ERROR, "Could not create DNS resolver(%d), resorting to system resolution\n", status);
276  ao2_ref(discovered_nameservers, -1);
277  return 0;
278  }
279  }
280 
281  it_nameservers = ao2_iterator_init(discovered_nameservers, 0);
282  while ((nameserver = ao2_iterator_next(&it_nameservers))) {
283  pj_strset2(&nameservers[count++], nameserver);
284  ao2_ref(nameserver, -1);
285 
286  if (count == (PJ_DNS_RESOLVER_MAX_NS - 1)) {
287  break;
288  }
289  }
290  ao2_iterator_destroy(&it_nameservers);
291 
292  status = pj_dns_resolver_set_ns(resolver, count, nameservers, NULL);
293 
294  /* Since we no longer need the nameservers we can drop the list of them */
295  ao2_ref(discovered_nameservers, -1);
296 
297  if (status != PJ_SUCCESS) {
298  ast_log(LOG_ERROR, "Could not set nameservers on DNS resolver in PJSIP(%d), resorting to system resolution\n",
299  status);
300  return 0;
301  }
302 
303  if (!pjsip_endpt_get_resolver(ast_sip_get_pjsip_endpoint())) {
304  status = pjsip_endpt_set_resolver(ast_sip_get_pjsip_endpoint(), resolver);
305  if (status != PJ_SUCCESS) {
306  ast_log(LOG_ERROR, "Could not set DNS resolver in PJSIP(%d), resorting to system resolution\n", status);
307  return 0;
308  }
309  }
310 
311  return 0;
312 }
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define NULL
Definition: resample.c:96
struct ao2_container * ast_dns_get_nameservers(void)
Retrieve the configured nameservers of the system.
Definition: dns.c:583
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define LOG_ERROR
Definition: logger.h:285
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition: res_pjsip.c:3718
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
Generic container type.
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
jack_status_t status
Definition: app_jack.c:146

Variable Documentation

◆ sip_threadpool_options

struct ast_threadpool_options sip_threadpool_options
static
Initial value:
= {
}
#define AST_THREADPOOL_OPTIONS_VERSION
Definition: threadpool.h:71

Definition at line 66 of file config_system.c.

Referenced by sip_get_threadpool_options().

◆ system_sorcery

struct ast_sorcery* system_sorcery
static

Definition at line 75 of file config_system.c.