cfg_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2007 iptelorg GmbH
  5. *
  6. * This file is part of SIP-router, a free SIP server.
  7. *
  8. * SIP-router is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * SIP-router is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * History
  23. * -------
  24. * 2007-12-03 Initial version (Miklos)
  25. * 2008-01-31 added DNS resolver parameters (Miklos)
  26. */
  27. /** core runtime config.
  28. * @file cfg_core.c
  29. * @ingroup core
  30. * Module: @ref core
  31. *
  32. * See
  33. * - @ref ConfigCoreDoc
  34. * - @ref ConfigEngine
  35. * - @ref cfg_core.h
  36. */
  37. /*!
  38. * \page ConfigCoreDoc Documentation of configuration parser
  39. * \section ConfigParser Configuration parser
  40. * Further information
  41. * - \ref ConfigEngine
  42. * - \ref cfg_core.h
  43. * \verbinclude cfg.txt
  44. *
  45. */
  46. #include "dprint.h"
  47. #ifdef USE_DST_BLACKLIST
  48. #include "dst_blacklist.h"
  49. #endif
  50. #include "resolve.h"
  51. #ifdef USE_DNS_CACHE
  52. #include "dns_cache.h"
  53. #endif
  54. #if defined PKG_MALLOC || defined SHM_MEM
  55. #include "pt.h"
  56. #endif
  57. #include "msg_translator.h" /* fix_global_req_flags() */
  58. #include "globals.h"
  59. #include "sock_ut.h"
  60. #include "cfg/cfg.h"
  61. #include "cfg_core.h"
  62. struct cfg_group_core default_core_cfg = {
  63. L_WARN, /*!< print only msg. < L_WARN */
  64. LOG_DAEMON, /*!< log_facility -- see syslog(3) */
  65. L_DBG, /*!< memdbg */
  66. #ifdef USE_DST_BLACKLIST
  67. /* blacklist */
  68. 0, /*!< dst blacklist is disabled by default */
  69. DEFAULT_BLST_TIMEOUT,
  70. DEFAULT_BLST_MAX_MEM,
  71. 0, /* blst_udp_imask */
  72. 0, /* blst_tcp_imask */
  73. 0, /* blst_tls_imask */
  74. 0, /* blst_sctp_imask */
  75. #endif
  76. /* resolver */
  77. #ifdef USE_IPV6
  78. 1, /*!< dns_try_ipv6 -- on by default */
  79. #else
  80. 0, /*!< dns_try_ipv6 -- off, if no ipv6 support */
  81. #endif
  82. 0, /*!< dns_try_naptr -- off by default */
  83. 30, /*!< udp transport preference (for naptr) */
  84. 20, /*!< tcp transport preference (for naptr) */
  85. 10, /*!< tls transport preference (for naptr) */
  86. 20, /*!< sctp transport preference (for naptr) */
  87. -1, /*!< dns_retr_time */
  88. -1, /*!< dns_retr_no */
  89. -1, /*!< dns_servers_no */
  90. 1, /*!< dns_search_list */
  91. 1, /*!< dns_search_fmatch */
  92. 0, /*!< dns_reinit */
  93. /* DNS cache */
  94. #ifdef USE_DNS_CACHE
  95. 1, /*!< use_dns_cache -- on by default */
  96. 0, /*!< dns_cache_flags */
  97. 0, /*!< use_dns_failover -- off by default */
  98. 0, /*!< dns_srv_lb -- off by default */
  99. DEFAULT_DNS_NEG_CACHE_TTL, /*!< neg. cache ttl */
  100. DEFAULT_DNS_CACHE_MIN_TTL, /*!< minimum ttl */
  101. DEFAULT_DNS_CACHE_MAX_TTL, /*!< maximum ttl */
  102. DEFAULT_DNS_MAX_MEM, /*!< dns_cache_max_mem */
  103. 0, /*!< dns_cache_del_nonexp -- delete only expired entries by default */
  104. 0, /*!< dns_cache_rec_pref -- 0 by default, do not check the existing entries. */
  105. #endif
  106. #ifdef PKG_MALLOC
  107. 0, /*!< mem_dump_pkg */
  108. #endif
  109. #ifdef SHM_MEM
  110. 0, /*!< mem_dump_shm */
  111. #endif
  112. DEFAULT_MAX_WHILE_LOOPS, /*!< max_while_loops */
  113. 0, /*!< udp_mtu (disabled by default) */
  114. 0, /*!< udp_mtu_try_proto -> default disabled */
  115. 0, /**< udp4_raw (disabled by default) */
  116. 1500, /**< udp4_raw_mtu (1500 by default) */
  117. -1, /**< udp4_raw_ttl (auto detect by default) */
  118. 0, /*!< force_rport */
  119. L_DBG, /*!< memlog */
  120. 3, /*!< mem_summary -flags: 0 off, 1 pkg_status, 2 shm_status,
  121. 4 pkg_sums, 8 shm_sums, 16 short_status */
  122. 0, /*!< mem_safety - 0 disabled */
  123. 0, /*!< mem_join - 0 disabled */
  124. L_ERR, /*!< corelog */
  125. L_ERR, /*!< latency log */
  126. 0, /*!< latency limit db */
  127. 0 /*!< latency limit action */
  128. };
  129. void *core_cfg = &default_core_cfg;
  130. static int check_raw_sock_support(void* cfg_h, str* gname, str* name,
  131. void** v)
  132. {
  133. int val;
  134. val = (int)(long)(*v);
  135. #ifndef USE_RAW_SOCKS
  136. if (val > 0) {
  137. ERR("no RAW_SOCKS support, please recompile with it enabled\n");
  138. return -1;
  139. }
  140. return 0;
  141. #else /* USE_RAW_SOCKS */
  142. if (raw_udp4_send_sock < 0) {
  143. if (val > 0) {
  144. ERR("could not intialize raw socket on startup, please "
  145. "restart as root or with CAP_NET_RAW\n");
  146. return -1;
  147. } else if (val < 0) {
  148. /* auto and no socket => disable */
  149. *v = (void*)(long)0;
  150. }
  151. } else if (val < 0) {
  152. /* auto and socket => enable */
  153. *v = (void*)(long)1;
  154. }
  155. return 0;
  156. #endif /* USE_RAW_SOCKS */
  157. }
  158. static int udp4_raw_ttl_fixup(void* cfg_h, str* gname, str* name, void** val)
  159. {
  160. int v;
  161. v = (int)(long)(*val);
  162. if (v < 0) {
  163. if (sendipv4)
  164. v = sock_get_ttl(sendipv4->socket);
  165. }
  166. if (v < 0) {
  167. /* some error => use a reasonable default */
  168. v = 63;
  169. }
  170. *val = (void*)(long)v;
  171. return 0;
  172. }
  173. cfg_def_t core_cfg_def[] = {
  174. {"debug", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  175. "debug level"},
  176. {"log_facility", CFG_VAR_INT|CFG_INPUT_STRING, 0, 0, log_facility_fixup, 0,
  177. "syslog facility, see \"man 3 syslog\""},
  178. {"memdbg", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  179. "log level for memory debugging messages"},
  180. #ifdef USE_DST_BLACKLIST
  181. /* blacklist */
  182. {"use_dst_blacklist", CFG_VAR_INT, 0, 1, use_dst_blacklist_fixup, 0,
  183. "enable/disable destination blacklisting"},
  184. {"dst_blacklist_expire", CFG_VAR_INT, 0, 0, 0, 0,
  185. "how much time (in s) a blacklisted destination is kept in the list"},
  186. {"dst_blacklist_mem", CFG_VAR_INT, 0, 0, blst_max_mem_fixup, 0,
  187. "maximum shared memory amount (in KB) used for keeping the blacklisted"
  188. " destinations"},
  189. {"dst_blacklist_udp_imask", CFG_VAR_INT, 0, 0, 0, blst_reinit_ign_masks,
  190. "blacklist event ignore mask for UDP"},
  191. {"dst_blacklist_tcp_imask", CFG_VAR_INT, 0, 0, 0, blst_reinit_ign_masks,
  192. "blacklist event ignore mask for TCP"},
  193. {"dst_blacklist_tls_imask", CFG_VAR_INT, 0, 0, 0, blst_reinit_ign_masks,
  194. "blacklist event ignore mask for TLS"},
  195. {"dst_blacklist_sctp_imask", CFG_VAR_INT, 0, 0, 0, blst_reinit_ign_masks,
  196. "blacklist event ignore mask for SCTP"},
  197. #endif
  198. /* resolver */
  199. #ifdef USE_DNS_CACHE
  200. {"dns_try_ipv6", CFG_VAR_INT, 0, 1, dns_try_ipv6_fixup, fix_dns_flags,
  201. #else
  202. {"dns_try_ipv6", CFG_VAR_INT, 0, 1, dns_try_ipv6_fixup, 0,
  203. #endif
  204. "enable/disable IPv6 DNS lookups"},
  205. #ifdef USE_DNS_CACHE
  206. {"dns_try_naptr", CFG_VAR_INT, 0, 1, 0, fix_dns_flags,
  207. #else
  208. {"dns_try_naptr", CFG_VAR_INT, 0, 1, 0, 0,
  209. #endif
  210. "enable/disable NAPTR DNS lookups"},
  211. {"dns_udp_pref", CFG_VAR_INT, 0, 0, 0, reinit_naptr_proto_prefs,
  212. "udp protocol preference when doing NAPTR lookups"},
  213. {"dns_tcp_pref", CFG_VAR_INT, 0, 0, 0, reinit_naptr_proto_prefs,
  214. "tcp protocol preference when doing NAPTR lookups"},
  215. {"dns_tls_pref", CFG_VAR_INT, 0, 0, 0, reinit_naptr_proto_prefs,
  216. "tls protocol preference when doing NAPTR lookups"},
  217. {"dns_sctp_pref", CFG_VAR_INT, 0, 0, 0, reinit_naptr_proto_prefs,
  218. "sctp protocol preference when doing NAPTR lookups"},
  219. {"dns_retr_time", CFG_VAR_INT, 0, 0, 0, resolv_reinit,
  220. "time in s before retrying a dns request"},
  221. {"dns_retr_no", CFG_VAR_INT, 0, 0, 0, resolv_reinit,
  222. "number of dns retransmissions before giving up"},
  223. {"dns_servers_no", CFG_VAR_INT, 0, 0, 0, resolv_reinit,
  224. "how many dns servers from the ones defined in "
  225. "/etc/resolv.conf will be used"},
  226. {"dns_use_search_list", CFG_VAR_INT, 0, 1, 0, resolv_reinit,
  227. "if set to 0, the search list in /etc/resolv.conf is ignored"},
  228. {"dns_search_full_match", CFG_VAR_INT, 0, 1, 0, 0,
  229. "enable/disable domain name checks against the search list "
  230. "in DNS answers"},
  231. {"dns_reinit", CFG_VAR_INT|CFG_INPUT_INT, 1, 1, dns_reinit_fixup,
  232. resolv_reinit,
  233. "set to 1 in order to reinitialize the DNS resolver"},
  234. /* DNS cache */
  235. #ifdef USE_DNS_CACHE
  236. {"use_dns_cache", CFG_VAR_INT, 0, 1, use_dns_cache_fixup, 0,
  237. "enable/disable the dns cache"},
  238. {"dns_cache_flags", CFG_VAR_INT, 0, 4, 0, fix_dns_flags,
  239. "dns cache specific resolver flags "
  240. "(1=ipv4 only, 2=ipv6 only, 4=prefer ipv6"},
  241. {"use_dns_failover", CFG_VAR_INT, 0, 1, use_dns_failover_fixup, 0,
  242. "enable/disable dns failover in case the destination "
  243. "resolves to multiple ip addresses and/or multiple SRV records "
  244. "(depends on use_dns_cache)"},
  245. {"dns_srv_lb", CFG_VAR_INT, 0, 1, 0, fix_dns_flags,
  246. "enable/disable load balancing to different srv records "
  247. "of the same priority based on the srv records weights "
  248. "(depends on dns_failover)"},
  249. {"dns_cache_negative_ttl", CFG_VAR_INT, 0, 0, 0, 0,
  250. "time to live for negative results (\"not found\") "
  251. "in seconds. Use 0 to disable"},
  252. {"dns_cache_min_ttl", CFG_VAR_INT, 0, 0, 0, 0,
  253. "minimum accepted time to live for a record, in seconds"},
  254. {"dns_cache_max_ttl", CFG_VAR_INT, 0, 0, 0, 0,
  255. "maximum accepted time to live for a record, in seconds"},
  256. {"dns_cache_mem", CFG_VAR_INT, 0, 0, dns_cache_max_mem_fixup, 0,
  257. "maximum memory used for the dns cache in Kb"},
  258. {"dns_cache_del_nonexp", CFG_VAR_INT, 0, 1, 0, 0,
  259. "allow deletion of non-expired records from the cache when "
  260. "there is no more space left for new ones"},
  261. {"dns_cache_rec_pref", CFG_VAR_INT, 0, 3, 0, 0,
  262. "DNS cache record preference: "
  263. " 0 - do not check duplicates"
  264. " 1 - prefer old records"
  265. " 2 - prefer new records"
  266. " 3 - prefer records with longer lifetime"},
  267. #endif
  268. #ifdef PKG_MALLOC
  269. {"mem_dump_pkg", CFG_VAR_INT, 0, 0, 0, mem_dump_pkg_cb,
  270. "dump process memory status, parameter: pid_number"},
  271. #endif
  272. #ifdef SHM_MEM
  273. {"mem_dump_shm", CFG_VAR_INT, 0, 0, mem_dump_shm_fixup, 0,
  274. "dump shared memory status"},
  275. #endif
  276. {"max_while_loops", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  277. "maximum iterations allowed for a while loop" },
  278. {"udp_mtu", CFG_VAR_INT|CFG_ATOMIC, 0, 65535, 0, 0,
  279. "fallback to a congestion controlled protocol if send size"
  280. " exceeds udp_mtu"},
  281. {"udp_mtu_try_proto", CFG_VAR_INT, 1, 4, 0, fix_global_req_flags,
  282. "if send size > udp_mtu use proto (1 udp, 2 tcp, 3 tls, 4 sctp)"},
  283. {"udp4_raw", CFG_VAR_INT | CFG_ATOMIC, -1, 1, check_raw_sock_support, 0,
  284. "enable/disable using a raw socket for sending UDP IPV4 packets."
  285. " Should be faster on multi-CPU linux running machines."},
  286. {"udp4_raw_mtu", CFG_VAR_INT | CFG_ATOMIC, 28, 65535, 0, 0,
  287. "set the MTU used when using raw sockets for udp sending."
  288. " This value will be used when deciding whether or not to fragment"
  289. " the packets."},
  290. {"udp4_raw_ttl", CFG_VAR_INT | CFG_ATOMIC, -1, 255, udp4_raw_ttl_fixup, 0,
  291. "set the IP TTL used when using raw sockets for udp sending."
  292. " -1 will use the same value as for normal udp sockets."},
  293. {"force_rport", CFG_VAR_INT, 0, 1, 0, fix_global_req_flags,
  294. "force rport for all the received messages" },
  295. {"memlog", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  296. "log level for memory status/summary information"},
  297. {"mem_summary", CFG_VAR_INT|CFG_ATOMIC, 0, 31, 0, 0,
  298. "memory debugging information displayed on exit (flags): "
  299. " 0 - off,"
  300. " 1 - dump all the pkg used blocks (status),"
  301. " 2 - dump all the shm used blocks (status),"
  302. " 4 - summary of pkg used blocks,"
  303. " 8 - summary of shm used blocks,"
  304. " 16 - short status instead of dump" },
  305. {"mem_safety", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  306. "safety level for memory operations"},
  307. {"mem_join", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  308. "join free memory fragments"},
  309. {"corelog", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  310. "log level for non-critical core error messages"},
  311. {"latency_log", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  312. "log level for latency limits alert messages"},
  313. {"latency_limit_db", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  314. "limit is ms for alerting on time consuming db commands"},
  315. {"latency_limit_action", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  316. "limit is ms for alerting on time consuming config actions"},
  317. {0, 0, 0, 0, 0, 0}
  318. };