cfg_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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+1, /*!< 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. 1, /*!< dns_try_ipv6 -- on by default */
  78. 0, /*!< dns_try_naptr -- off by default */
  79. 30, /*!< udp transport preference (for naptr) */
  80. 20, /*!< tcp transport preference (for naptr) */
  81. 10, /*!< tls transport preference (for naptr) */
  82. 20, /*!< sctp transport preference (for naptr) */
  83. -1, /*!< dns_retr_time */
  84. -1, /*!< dns_retr_no */
  85. -1, /*!< dns_servers_no */
  86. 1, /*!< dns_search_list */
  87. 1, /*!< dns_search_fmatch */
  88. 0, /*!< dns_reinit */
  89. 1, /*!< dns_naptr_ignore_rfc */
  90. /* DNS cache */
  91. #ifdef USE_DNS_CACHE
  92. 1, /*!< use_dns_cache -- on by default */
  93. 0, /*!< dns_cache_flags */
  94. 0, /*!< use_dns_failover -- off by default */
  95. 0, /*!< dns_srv_lb -- off by default */
  96. DEFAULT_DNS_NEG_CACHE_TTL, /*!< neg. cache ttl */
  97. DEFAULT_DNS_CACHE_MIN_TTL, /*!< minimum ttl */
  98. DEFAULT_DNS_CACHE_MAX_TTL, /*!< maximum ttl */
  99. DEFAULT_DNS_MAX_MEM, /*!< dns_cache_max_mem */
  100. 0, /*!< dns_cache_del_nonexp -- delete only expired entries by default */
  101. 0, /*!< dns_cache_rec_pref -- 0 by default, do not check the existing entries. */
  102. #endif
  103. #ifdef PKG_MALLOC
  104. 0, /*!< mem_dump_pkg */
  105. #endif
  106. #ifdef SHM_MEM
  107. 0, /*!< mem_dump_shm */
  108. #endif
  109. DEFAULT_MAX_WHILE_LOOPS, /*!< max_while_loops */
  110. 0, /*!< udp_mtu (disabled by default) */
  111. 0, /*!< udp_mtu_try_proto -> default disabled */
  112. 0, /**< udp4_raw (disabled by default) */
  113. 1500, /**< udp4_raw_mtu (1500 by default) */
  114. -1, /**< udp4_raw_ttl (auto detect by default) */
  115. 0, /*!< force_rport */
  116. L_DBG+1, /*!< memlog */
  117. 3, /*!< mem_summary -flags: 0 off, 1 pkg_status, 2 shm_status,
  118. 4 pkg_sums, 8 shm_sums, 16 short_status */
  119. 0, /*!< mem_safety - 0 disabled */
  120. 0, /*!< mem_join - 0 disabled */
  121. L_ERR, /*!< corelog */
  122. L_ERR, /*!< latency log */
  123. 0, /*!< latency limit db */
  124. 0 /*!< latency limit action */
  125. };
  126. void *core_cfg = &default_core_cfg;
  127. static int check_raw_sock_support(void* cfg_h, str* gname, str* name,
  128. void** v)
  129. {
  130. int val;
  131. val = (int)(long)(*v);
  132. #ifndef USE_RAW_SOCKS
  133. if (val > 0) {
  134. ERR("no RAW_SOCKS support, please recompile with it enabled\n");
  135. return -1;
  136. }
  137. return 0;
  138. #else /* USE_RAW_SOCKS */
  139. if (raw_udp4_send_sock < 0) {
  140. if (val > 0) {
  141. ERR("could not intialize raw socket on startup, please "
  142. "restart as root or with CAP_NET_RAW\n");
  143. return -1;
  144. } else if (val < 0) {
  145. /* auto and no socket => disable */
  146. *v = (void*)(long)0;
  147. }
  148. } else if (val < 0) {
  149. /* auto and socket => enable */
  150. *v = (void*)(long)1;
  151. }
  152. return 0;
  153. #endif /* USE_RAW_SOCKS */
  154. }
  155. static int udp4_raw_ttl_fixup(void* cfg_h, str* gname, str* name, void** val)
  156. {
  157. int v;
  158. v = (int)(long)(*val);
  159. if (v < 0) {
  160. if (sendipv4)
  161. v = sock_get_ttl(sendipv4->socket);
  162. }
  163. if (v < 0) {
  164. /* some error => use a reasonable default */
  165. v = 63;
  166. }
  167. *val = (void*)(long)v;
  168. return 0;
  169. }
  170. cfg_def_t core_cfg_def[] = {
  171. {"debug", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  172. "debug level"},
  173. {"log_facility", CFG_VAR_INT|CFG_INPUT_STRING, 0, 0, log_facility_fixup, 0,
  174. "syslog facility, see \"man 3 syslog\""},
  175. {"memdbg", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  176. "log level for memory debugging messages"},
  177. #ifdef USE_DST_BLACKLIST
  178. /* blacklist */
  179. {"use_dst_blacklist", CFG_VAR_INT, 0, 1, use_dst_blacklist_fixup, 0,
  180. "enable/disable destination blacklisting"},
  181. {"dst_blacklist_expire", CFG_VAR_INT, 0, 0, 0, 0,
  182. "how much time (in s) a blacklisted destination is kept in the list"},
  183. {"dst_blacklist_mem", CFG_VAR_INT, 0, 0, blst_max_mem_fixup, 0,
  184. "maximum shared memory amount (in KB) used for keeping the blacklisted"
  185. " destinations"},
  186. {"dst_blacklist_udp_imask", CFG_VAR_INT, 0, 0, 0, blst_reinit_ign_masks,
  187. "blacklist event ignore mask for UDP"},
  188. {"dst_blacklist_tcp_imask", CFG_VAR_INT, 0, 0, 0, blst_reinit_ign_masks,
  189. "blacklist event ignore mask for TCP"},
  190. {"dst_blacklist_tls_imask", CFG_VAR_INT, 0, 0, 0, blst_reinit_ign_masks,
  191. "blacklist event ignore mask for TLS"},
  192. {"dst_blacklist_sctp_imask", CFG_VAR_INT, 0, 0, 0, blst_reinit_ign_masks,
  193. "blacklist event ignore mask for SCTP"},
  194. #endif
  195. /* resolver */
  196. #ifdef USE_DNS_CACHE
  197. {"dns_try_ipv6", CFG_VAR_INT, 0, 1, dns_try_ipv6_fixup, fix_dns_flags,
  198. #else
  199. {"dns_try_ipv6", CFG_VAR_INT, 0, 1, dns_try_ipv6_fixup, 0,
  200. #endif
  201. "enable/disable IPv6 DNS lookups"},
  202. #ifdef USE_DNS_CACHE
  203. {"dns_try_naptr", CFG_VAR_INT, 0, 1, 0, fix_dns_flags,
  204. #else
  205. {"dns_try_naptr", CFG_VAR_INT, 0, 1, 0, 0,
  206. #endif
  207. "enable/disable NAPTR DNS lookups"},
  208. {"dns_udp_pref", CFG_VAR_INT, 0, 0, 0, reinit_proto_prefs,
  209. "udp protocol preference when doing NAPTR lookups"},
  210. {"dns_tcp_pref", CFG_VAR_INT, 0, 0, 0, reinit_proto_prefs,
  211. "tcp protocol preference when doing NAPTR lookups"},
  212. {"dns_tls_pref", CFG_VAR_INT, 0, 0, 0, reinit_proto_prefs,
  213. "tls protocol preference when doing NAPTR lookups"},
  214. {"dns_sctp_pref", CFG_VAR_INT, 0, 0, 0, reinit_proto_prefs,
  215. "sctp protocol preference when doing NAPTR lookups"},
  216. {"dns_retr_time", CFG_VAR_INT, 0, 0, 0, resolv_reinit,
  217. "time in s before retrying a dns request"},
  218. {"dns_retr_no", CFG_VAR_INT, 0, 0, 0, resolv_reinit,
  219. "number of dns retransmissions before giving up"},
  220. {"dns_servers_no", CFG_VAR_INT, 0, 0, 0, resolv_reinit,
  221. "how many dns servers from the ones defined in "
  222. "/etc/resolv.conf will be used"},
  223. {"dns_use_search_list", CFG_VAR_INT, 0, 1, 0, resolv_reinit,
  224. "if set to 0, the search list in /etc/resolv.conf is ignored"},
  225. {"dns_search_full_match", CFG_VAR_INT, 0, 1, 0, 0,
  226. "enable/disable domain name checks against the search list "
  227. "in DNS answers"},
  228. {"dns_reinit", CFG_VAR_INT|CFG_INPUT_INT, 1, 1, dns_reinit_fixup,
  229. resolv_reinit,
  230. "set to 1 in order to reinitialize the DNS resolver"},
  231. {"dns_naptr_ignore_rfc", CFG_VAR_INT, 0, 0, 0, reinit_proto_prefs,
  232. "ignore the Order field required by RFC 2915"},
  233. /* DNS cache */
  234. #ifdef USE_DNS_CACHE
  235. {"use_dns_cache", CFG_VAR_INT, 0, 1, use_dns_cache_fixup, 0,
  236. "enable/disable the dns cache"},
  237. {"dns_cache_flags", CFG_VAR_INT, 0, 4, 0, fix_dns_flags,
  238. "dns cache specific resolver flags "
  239. "(1=ipv4 only, 2=ipv6 only, 4=prefer ipv6"},
  240. {"use_dns_failover", CFG_VAR_INT, 0, 1, use_dns_failover_fixup, 0,
  241. "enable/disable dns failover in case the destination "
  242. "resolves to multiple ip addresses and/or multiple SRV records "
  243. "(depends on use_dns_cache)"},
  244. {"dns_srv_lb", CFG_VAR_INT, 0, 1, 0, fix_dns_flags,
  245. "enable/disable load balancing to different srv records "
  246. "of the same priority based on the srv records weights "
  247. "(depends on dns_failover)"},
  248. {"dns_cache_negative_ttl", CFG_VAR_INT, 0, 0, 0, 0,
  249. "time to live for negative results (\"not found\") "
  250. "in seconds. Use 0 to disable"},
  251. {"dns_cache_min_ttl", CFG_VAR_INT, 0, 0, 0, 0,
  252. "minimum accepted time to live for a record, in seconds"},
  253. {"dns_cache_max_ttl", CFG_VAR_INT, 0, 0, 0, 0,
  254. "maximum accepted time to live for a record, in seconds"},
  255. {"dns_cache_mem", CFG_VAR_INT, 0, 0, dns_cache_max_mem_fixup, 0,
  256. "maximum memory used for the dns cache in Kb"},
  257. {"dns_cache_del_nonexp", CFG_VAR_INT, 0, 1, 0, 0,
  258. "allow deletion of non-expired records from the cache when "
  259. "there is no more space left for new ones"},
  260. {"dns_cache_rec_pref", CFG_VAR_INT, 0, 3, 0, 0,
  261. "DNS cache record preference: "
  262. " 0 - do not check duplicates"
  263. " 1 - prefer old records"
  264. " 2 - prefer new records"
  265. " 3 - prefer records with longer lifetime"},
  266. #endif
  267. #ifdef PKG_MALLOC
  268. {"mem_dump_pkg", CFG_VAR_INT, 0, 0, 0, mem_dump_pkg_cb,
  269. "dump process memory status, parameter: pid_number"},
  270. #endif
  271. #ifdef SHM_MEM
  272. {"mem_dump_shm", CFG_VAR_INT, 0, 0, mem_dump_shm_fixup, 0,
  273. "dump shared memory status"},
  274. #endif
  275. {"max_while_loops", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  276. "maximum iterations allowed for a while loop" },
  277. {"udp_mtu", CFG_VAR_INT|CFG_ATOMIC, 0, 65535, 0, 0,
  278. "fallback to a congestion controlled protocol if send size"
  279. " exceeds udp_mtu"},
  280. {"udp_mtu_try_proto", CFG_VAR_INT, 1, 4, 0, fix_global_req_flags,
  281. "if send size > udp_mtu use proto (1 udp, 2 tcp, 3 tls, 4 sctp)"},
  282. {"udp4_raw", CFG_VAR_INT | CFG_ATOMIC, -1, 1, check_raw_sock_support, 0,
  283. "enable/disable using a raw socket for sending UDP IPV4 packets."
  284. " Should be faster on multi-CPU linux running machines."},
  285. {"udp4_raw_mtu", CFG_VAR_INT | CFG_ATOMIC, 28, 65535, 0, 0,
  286. "set the MTU used when using raw sockets for udp sending."
  287. " This value will be used when deciding whether or not to fragment"
  288. " the packets."},
  289. {"udp4_raw_ttl", CFG_VAR_INT | CFG_ATOMIC, -1, 255, udp4_raw_ttl_fixup, 0,
  290. "set the IP TTL used when using raw sockets for udp sending."
  291. " -1 will use the same value as for normal udp sockets."},
  292. {"force_rport", CFG_VAR_INT, 0, 1, 0, fix_global_req_flags,
  293. "force rport for all the received messages" },
  294. {"memlog", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  295. "log level for memory status/summary information"},
  296. {"mem_summary", CFG_VAR_INT|CFG_ATOMIC, 0, 31, 0, 0,
  297. "memory debugging information displayed on exit (flags): "
  298. " 0 - off,"
  299. " 1 - dump all the pkg used blocks (status),"
  300. " 2 - dump all the shm used blocks (status),"
  301. " 4 - summary of pkg used blocks,"
  302. " 8 - summary of shm used blocks,"
  303. " 16 - short status instead of dump" },
  304. {"mem_safety", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  305. "safety level for memory operations"},
  306. {"mem_join", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  307. "join free memory fragments"},
  308. {"corelog", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  309. "log level for non-critical core error messages"},
  310. {"latency_log", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  311. "log level for latency limits alert messages"},
  312. {"latency_limit_db", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  313. "limit is ms for alerting on time consuming db commands"},
  314. {"latency_limit_action", CFG_VAR_INT|CFG_ATOMIC, 0, 0, 0, 0,
  315. "limit is ms for alerting on time consuming config actions"},
  316. {0, 0, 0, 0, 0, 0}
  317. };