eX_setup.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. eXosip - This is the eXtended osip library.
  3. Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com
  4. eXosip is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. eXosip is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. In addition, as a special exception, the copyright holders give
  16. permission to link the code of portions of this program with the
  17. OpenSSL library under certain conditions as described in each
  18. individual source file, and distribute linked combinations
  19. including the two.
  20. You must obey the GNU General Public License in all respects
  21. for all of the code used other than OpenSSL. If you modify
  22. file(s) with this exception, you may extend this exception to your
  23. version of the file(s), but you are not obligated to do so. If you
  24. do not wish to do so, delete this exception statement from your
  25. version. If you delete this exception statement from all source
  26. files in the program, then also delete it here.
  27. */
  28. #ifdef ENABLE_MPATROL
  29. #include <mpatrol.h>
  30. #endif
  31. #ifndef __EX_SETUP_H__
  32. #define __EX_SETUP_H__
  33. #include <eXosip2/eXosip.h>
  34. #include <osipparser2/osip_message.h>
  35. #include <time.h>
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. struct eXosip_t;
  40. struct osip_srv_record;
  41. struct osip_naptr;
  42. /**
  43. * @file eX_setup.h
  44. * @brief eXosip setup API
  45. *
  46. * This file provide the API needed to setup and configure
  47. * the SIP endpoint.
  48. *
  49. */
  50. /**
  51. * @defgroup eXosip2_conf eXosip2 configuration API
  52. * @ingroup eXosip2_setup
  53. * @{
  54. */
  55. /**
  56. * Allocate an eXosip context.
  57. *
  58. * @return a new allocated eXosip_t instance.
  59. */
  60. struct eXosip_t *eXosip_malloc(void);
  61. /**
  62. * Initiate the eXtented oSIP library.
  63. *
  64. * @param excontext eXosip_t instance.
  65. */
  66. int eXosip_init(struct eXosip_t *excontext);
  67. /**
  68. * Release ressource used by the eXtented oSIP library.
  69. *
  70. * @param excontext eXosip_t instance.
  71. */
  72. void eXosip_quit(struct eXosip_t *excontext);
  73. /**
  74. * Lock the eXtented oSIP library.
  75. *
  76. * @param excontext eXosip_t instance.
  77. */
  78. int eXosip_lock(struct eXosip_t *excontext);
  79. /**
  80. * UnLock the eXtented oSIP library.
  81. *
  82. * @param excontext eXosip_t instance.
  83. */
  84. int eXosip_unlock(struct eXosip_t *excontext);
  85. /**
  86. * Process (non-threaded mode ONLY) eXosip events.
  87. *
  88. * @param excontext eXosip_t instance.
  89. */
  90. int eXosip_execute(struct eXosip_t *excontext);
  91. #define EXOSIP_OPT_BASE_OPTION 0
  92. #define EXOSIP_OPT_UDP_KEEP_ALIVE (EXOSIP_OPT_BASE_OPTION + 1) /**< int *: interval for keep alive packets (UDP, TCP, TLS, DTLS) */
  93. #define EXOSIP_OPT_AUTO_MASQUERADE_CONTACT (EXOSIP_OPT_BASE_OPTION + 2) /**< int *: specific re-usage of "rport" */
  94. #define EXOSIP_OPT_UDP_LEARN_PORT EXOSIP_OPT_AUTO_MASQUERADE_CONTACT /** EXOSIP_OPT_UDP_LEARN_PORT is obsolete / replaced by EXOSIP_OPT_AUTO_MASQUERADE_CONTACT */
  95. #define EXOSIP_OPT_USE_RPORT (EXOSIP_OPT_BASE_OPTION + 7) /**< int *: enable or disable rport in via */
  96. #define EXOSIP_OPT_SET_IPV4_FOR_GATEWAY (EXOSIP_OPT_BASE_OPTION + 8) /**< char *: usually, this is the proxy address */
  97. #define EXOSIP_OPT_ADD_DNS_CACHE (EXOSIP_OPT_BASE_OPTION + 9) /**< struct eXosip_dns_cache *: force some cache entry to avoid DNS */
  98. #define EXOSIP_OPT_DELETE_DNS_CACHE (EXOSIP_OPT_BASE_OPTION + 10) /**< struct eXosip_dns_cache *: force removal of some cache entry to avoid DNS */
  99. #define EXOSIP_OPT_SET_IPV6_FOR_GATEWAY (EXOSIP_OPT_BASE_OPTION + 12) /**< char *: usually, this is the proxy address */
  100. #define EXOSIP_OPT_ADD_ACCOUNT_INFO (EXOSIP_OPT_BASE_OPTION + 13) /**< struct eXosip_account_info *: internal stuff */
  101. #define EXOSIP_OPT_DNS_CAPABILITIES (EXOSIP_OPT_BASE_OPTION + 14) /**< int *: 0 to disable, 2 (default) to use NAPTR/SRV record */
  102. #define EXOSIP_OPT_SET_DSCP (EXOSIP_OPT_BASE_OPTION + 15) /**< int *: set a dscp value for SIP socket */
  103. #define EXOSIP_OPT_REGISTER_WITH_DATE (EXOSIP_OPT_BASE_OPTION + 16) /**< int *: enable usage of Date header in REGISTER */
  104. #define EXOSIP_OPT_SET_HEADER_USER_AGENT (EXOSIP_OPT_BASE_OPTION + 17) /**< char *: set the User-Agent header */
  105. #define EXOSIP_OPT_ENABLE_DNS_CACHE (EXOSIP_OPT_BASE_OPTION + 18) /**< int *: 0 to disable use of cache*/
  106. #define EXOSIP_OPT_ENABLE_AUTOANSWERBYE (EXOSIP_OPT_BASE_OPTION + 19) /**< int *: 0 to disable automatic answer of BYE */
  107. #define EXOSIP_OPT_ENABLE_IPV6 (EXOSIP_OPT_BASE_OPTION + 20) /**< int *: 0 to disable, 1 to use only IPv6, 2 to use choose the best -this is a per-eXosip_t parameter for using IPv6 DNS request */
  108. #define EXOSIP_OPT_ENABLE_REUSE_TCP_PORT (EXOSIP_OPT_BASE_OPTION + 21) /**< int *: 0 to disable, 1 to enable reusing local tcp port for outgoing tcp connection */
  109. #define EXOSIP_OPT_ENABLE_USE_EPHEMERAL_PORT (EXOSIP_OPT_BASE_OPTION + 22) /**< int *: 0 to disable, 1 to enable usage of emphemeral tcp port in Contact headers instead of local listening port for TCP/TLS */
  110. #define EXOSIP_OPT_SET_CALLBACK_WAKELOCK (EXOSIP_OPT_BASE_OPTION + 23) /**< CbSipWakeLock *: set a callback to be called upon start/end of transaction */
  111. #define EXOSIP_OPT_ENABLE_OUTBOUND (EXOSIP_OPT_BASE_OPTION + 24) /**< int *: 0 to disable, 1 to enable usage of ob parmeter (rfc 5626) in dialog's contact headers */
  112. #define EXOSIP_OPT_SET_OC_LOCAL_ADDRESS (EXOSIP_OPT_BASE_OPTION + 25) /**< char *: set the ip address to bind for outbound connection */
  113. #define EXOSIP_OPT_SET_OC_PORT_RANGE (EXOSIP_OPT_BASE_OPTION + 26) /**< int[2] *: set the port range (min, max) to bind for outbound connection (if EXOSIP_OPT_SET_OC_LOCAL_ADDRESS is not used, this options apply to TCP/TLS only) */
  114. #define EXOSIP_OPT_REMOVE_PREROUTESET (EXOSIP_OPT_BASE_OPTION + 27) /**< int *: 0: keep pre-route set in initial INVITE/SUBSCRIBE/REFER, 1 (default): remove pre-route set*/
  115. #define EXOSIP_OPT_SET_SIP_INSTANCE (EXOSIP_OPT_BASE_OPTION + 28) /**< char *: define +sip.instance parameter in Contact headers (example: f81d4fae-7dec-11d0-a765-00a0c91e6bf6) */
  116. #define EXOSIP_OPT_SET_MAX_MESSAGE_TO_READ (EXOSIP_OPT_BASE_OPTION + 29) /**< int: set the number of message to read at once for each network processing (high load traffic use-case: DO NOT USE FOR COMMON USAGE) */
  117. #define EXOSIP_OPT_SET_MAX_READ_TIMEOUT (EXOSIP_OPT_BASE_OPTION + 30) /**< long int: set the period in nano seconds during we read for sip message. (high load traffic use-case: DO NOT USE FOR COMMON USAGE)*/
  118. #define EXOSIP_OPT_SET_DEFAULT_CONTACT_DISPLAYNAME (EXOSIP_OPT_BASE_OPTION + 31) /**< char *: define a display name to be added in Contact headers (example: "john Doe") */
  119. #define EXOSIP_OPT_SET_SESSIONTIMERS_FORCE \
  120. (EXOSIP_OPT_BASE_OPTION + 32) /**< int *: 0 (default): activate "session timers" if supported on both side, 1: if remote side (UAS) do not indicate support for "session timers", activate feature on UAC (local) side */
  121. #define EXOSIP_OPT_FORCE_CONNECTIONREUSE (EXOSIP_OPT_BASE_OPTION + 33) /**< int *: 0 to disable, 1 to force reusing established connection for ACK and dialog message */
  122. #define EXOSIP_OPT_SET_TLS_VERIFY_CERTIFICATE (EXOSIP_OPT_BASE_OPTION + 500) /**< int *: enable verification of certificate for TLS connection */
  123. #define EXOSIP_OPT_SET_TLS_CERTIFICATES_INFO (EXOSIP_OPT_BASE_OPTION + 501) /**< eXosip_tls_ctx_t *: client and/or server certificate/ca-root/key info */
  124. #define EXOSIP_OPT_SET_TLS_CLIENT_CERTIFICATE_NAME (EXOSIP_OPT_BASE_OPTION + 502) /**< char*: user can choose a specific certifcate present in Windows Certificate Store */
  125. #define EXOSIP_OPT_SET_TLS_SERVER_CERTIFICATE_NAME (EXOSIP_OPT_BASE_OPTION + 503) /**< char*: user can choose a specific certifcate present in Windows Certificate Store */
  126. /* non standard option: need a compilation flag to activate */
  127. #define EXOSIP_OPT_KEEP_ALIVE_OPTIONS_METHOD (EXOSIP_OPT_BASE_OPTION + 1000)
  128. #define EXOSIP_OPT_SET_TSC_SERVER (EXOSIP_OPT_BASE_OPTION + 1001) /**< deprecated */
  129. #define EXOSIP_OPT_GET_STATISTICS (EXOSIP_OPT_BASE_OPTION + 2000) /**< struct eXosip_stats*: retreive numerous statistics about transactions, registrations, calls, publications and subscriptions... */
  130. /**
  131. * structure used to for inserting a DNS cache entry and avoid DNS resolution.
  132. * @struct eXosip_dns_cache
  133. */
  134. struct eXosip_dns_cache {
  135. char host[1024];
  136. char ip[256];
  137. };
  138. struct eXosip_account_info {
  139. char proxy[1024];
  140. char nat_ip[256];
  141. int nat_port;
  142. };
  143. struct eXosip_http_auth {
  144. char pszCallId[64];
  145. osip_proxy_authenticate_t *wa;
  146. char pszCNonce[64];
  147. int iNonceCount;
  148. int answer_code;
  149. };
  150. #ifndef MINISIZE
  151. /**
  152. * Structure used to retrieve eXosip internal statistics.
  153. * Total numbers are provided since last start or restart of eXosip.
  154. * Average values are calculated over the last EXOSIP_STATS_PERIOD
  155. * which default to 3600 seconds.
  156. *
  157. * @struct eXosip_stats
  158. */
  159. struct eXosip_stats {
  160. int allocated_transactions; /**< current number of allocated transactions. */
  161. float average_transactions; /**< average number of new transactions/hour. (default period: 1 hour) */
  162. int allocated_registrations; /**< current number of allocated registrations. (should remains 1 in standard usage) */
  163. float average_registrations; /**< average number of new registrations/hour. (default period: 1 hour) */
  164. int allocated_calls; /**< current number of allocated calls. */
  165. float average_calls; /**< average number of new calls/hour. (default period: 1 hour) */
  166. int allocated_publications; /**< current number of allocated publications. */
  167. float average_publications; /**< average number of new publication/hour. (default period: 1 hour) */
  168. int allocated_subscriptions; /**< current number of allocated outgoing subscriptions. */
  169. float average_subscriptions; /**< average number of new outgoing subscriptions/hour. (default period: 1 hour) */
  170. int allocated_insubscriptions; /**< current number of allocated incoming subscriptions. */
  171. float average_insubscriptions; /**< average number of new incoming subscriptions/hour. (default period: 1 hour) */
  172. int reserved1[20]; /**< reserved for future usage without breaking ABI */
  173. };
  174. #endif
  175. /**
  176. * Set eXosip options.
  177. * See eXosip_option for available options.
  178. *
  179. * @param excontext eXosip_t instance.
  180. * @param opt option to configure.
  181. * @param value value for options.
  182. *
  183. */
  184. int eXosip_set_option(struct eXosip_t *excontext, int opt, const void *value);
  185. /**
  186. * structure used to describe credentials for a client or server
  187. * consists of a certificate, a corresponding private key and its password
  188. *
  189. * If the server requires a certificate from the client, you must provide
  190. * the certificate, private key, and your private key password.
  191. *
  192. * You can "pin" your public key certificate if you have received it.
  193. * public_key_pinned must contains your public key file in DER format.
  194. * To extract your public key from a PEM certificate in DER format, you
  195. * can use the following command line:
  196. * openssl x509 -in server-cert.pem -pubkey -noout | openssl enc -base64 -d > pub_key.der
  197. *
  198. * @struct eXosip_tls_credentials_s
  199. */
  200. typedef struct eXosip_tls_credentials_s {
  201. char priv_key[1024]; /**< absolute path to a file with a private key */
  202. char priv_key_pw[1024]; /**< password to open private key */
  203. char cert[1024]; /**< absolute path to a file with a certificate for the private key */
  204. char public_key_pinned[1024]; /**< absolute path to a file with the expected public key of server */
  205. } eXosip_tls_credentials_t;
  206. /**
  207. * structure to describe the whole TLS-context for eXosip
  208. * consists of a certificate, a corresponding private key and its password
  209. *
  210. * When a client connects to a server, if you wish to verify certificate, you
  211. * just have to configure the root_ca_cert parameter to a file with all your
  212. * trusted CA. (example file at https://pki.google.com/roots.pem)
  213. *
  214. * On Windows & Macosx, the trusted certificates from the store are loaded automatically.
  215. *
  216. * @struct eXosip_tls_ctx_s
  217. */
  218. typedef struct eXosip_tls_ctx_s {
  219. char random_file[1024]; /**< absolute path to a file with random(!) data */
  220. char dh_param[1024]; /**< absolute path to a file necessary for diffie hellman key exchange */
  221. char root_ca_cert[1024]; /**< absolute path to the file with known rootCAs */
  222. char cipher_list[2048]; /**< openssl cipher list (default: HIGH:!COMPLEMENTOFDEFAULT:!kRSA:!PSK:!SRP) */
  223. unsigned long tls_flags; /**< openssl additionnal flag (example: SSL_OP_NO_TLSv1, SSL_OP_NO_TLSv1_1) */
  224. unsigned long dtls_flags; /**< openssl additionnal flag (example: SSL_OP_NO_DTLSv1) */
  225. eXosip_tls_credentials_t client; /**< credential of the client */
  226. eXosip_tls_credentials_t server; /**< credential of the server */
  227. } eXosip_tls_ctx_t;
  228. /**
  229. * An enumeration which describes the error which can occur while setting the eXosip_tls_ctx
  230. */
  231. typedef enum {
  232. TLS_OK = 0, /**< yippieh, everything is fine :) */
  233. TLS_ERR_NO_RAND = -1, /**< no absolute path to the random file was specified */
  234. TLS_ERR_NO_DH_PARAM = -2, /**< no absolute path to the diifie hellman file was specified */
  235. TLS_ERR_NO_PW = -3, /**< no password was specified */
  236. TLS_ERR_NO_ROOT_CA = -4, /**< no absolute path to the rootCA file was specified */
  237. TLS_ERR_MISSING_AUTH_PART = -5 /**< something is missing: the private key or the certificate */
  238. } eXosip_tls_ctx_error;
  239. /**
  240. * Start and return osip_naptr context.
  241. * Note that DNS results might not yet be available.
  242. *
  243. * If you provide a FQDN, a NAPTR query will be done on it.
  244. * For example: "antisip.com"
  245. *
  246. * If you wish to do a ENUM query, you need to specify both the domain to query
  247. * and the AUS (ie, the number dialed). You will use a "!" separator between them.
  248. * For example, to query "+123456789" on "e164.org", please use: "e164.org!+123456789"
  249. *
  250. * @param excontext eXosip_t instance.
  251. * @param domain domain name for NAPTR record OR ENUM query (such as e164.org!+123456789)
  252. * @param protocol protocol to use ("SIP")
  253. * @param transport transport to use ("UDP")
  254. * @param keep_in_cache keep result in cache if >0
  255. */
  256. struct osip_naptr *eXosip_dnsutils_naptr(struct eXosip_t *excontext, const char *domain, const char *protocol, const char *transport, int keep_in_cache);
  257. /**
  258. * For every eXosip_dnsutils_naptr query you make
  259. * you must call eXosip_dnsutils_release to release
  260. * the memory.
  261. *
  262. * @param naptr_record the naptr structure to release.
  263. */
  264. void eXosip_dnsutils_release(struct osip_naptr *naptr_record);
  265. /**
  266. * Continue to process asynchronous DNS request (if implemented).
  267. *
  268. * @param output_record result structure.
  269. * @param force force waiting for final answer if >0
  270. */
  271. int eXosip_dnsutils_dns_process(struct osip_naptr *output_record, int force);
  272. /**
  273. * Rotate first SRV entry to last SRV entry.
  274. *
  275. * @param output_record result structure.
  276. */
  277. int eXosip_dnsutils_rotate_srv(struct osip_srv_record *output_record);
  278. /**
  279. * Listen on a specified socket.
  280. *
  281. * @param excontext eXosip_t instance.
  282. * @param transport IPPROTO_UDP for udp. (soon to come: TCP/TLS?)
  283. * @param addr the address to bind (NULL for all interface)
  284. * @param port the listening port. (0 for random port)
  285. * @param family the IP family (AF_INET or AF_INET6).
  286. * @param secure 0 for UDP or TCP, 1 for TLS (with TCP).
  287. */
  288. int eXosip_listen_addr(struct eXosip_t *excontext, int transport, const char *addr, int port, int family, int secure);
  289. /**
  290. * Reset transport sockets.
  291. *
  292. * @param excontext eXosip_t instance.
  293. */
  294. int eXosip_reset_transports(struct eXosip_t *excontext);
  295. /**
  296. * Listen on a specified socket.
  297. *
  298. * @param excontext eXosip_t instance.
  299. * @param transport IPPROTO_UDP for udp. (soon to come: TCP/TLS?)
  300. * @param socket socket to use for listening to UDP sip messages.
  301. * @param port the listening port for masquerading.
  302. */
  303. int eXosip_set_socket(struct eXosip_t *excontext, int transport, int socket, int port);
  304. /**
  305. * Set the SIP User-Agent: header string.
  306. *
  307. * @param excontext eXosip_t instance.
  308. * @param user_agent the User-Agent header to insert in messages.
  309. */
  310. void eXosip_set_user_agent(struct eXosip_t *excontext, const char *user_agent);
  311. /**
  312. * Get the eXosip version as a sring
  313. *
  314. */
  315. const char *eXosip_get_version(void);
  316. #ifdef WIN32
  317. typedef void(__stdcall *CbSipCallback)(osip_message_t *msg, int received);
  318. typedef void(__stdcall *CbSipWakeLock)(int state);
  319. #else
  320. typedef void (*CbSipCallback)(osip_message_t *msg, int received);
  321. typedef void (*CbSipWakeLock)(int state);
  322. #endif
  323. /**
  324. * Set a callback to get sent and received SIP messages.
  325. *
  326. * @param excontext eXosip_t instance.
  327. * @param cbsipCallback the callback to retreive messages.
  328. */
  329. int eXosip_set_cbsip_message(struct eXosip_t *excontext, CbSipCallback cbsipCallback);
  330. /**
  331. * This method is used to replace contact address with
  332. * the public address of your NAT. The ip address should
  333. * be retreived manually (fixed IP address) or with STUN.
  334. * This address will only be used when the remote
  335. * correspondant appears to be on an DIFFERENT LAN.
  336. *
  337. * @param excontext eXosip_t instance.
  338. * @param public_address the ip address.
  339. * @param port the port for masquerading.
  340. *
  341. * If set to NULL, then the local ip address will be guessed
  342. * automatically (returns to default mode).
  343. */
  344. void eXosip_masquerade_contact(struct eXosip_t *excontext, const char *public_address, int port);
  345. /**
  346. * This method is used to find out an free IPPROTO_UDP or IPPROTO_TCP port.
  347. *
  348. * @param excontext eXosip_t instance.
  349. * @param free_port initial port for search.
  350. * @param transport IPPROTO_UDP or IPPROTO_TCP protocol.
  351. *
  352. */
  353. int eXosip_find_free_port(struct eXosip_t *excontext, int free_port, int transport);
  354. #ifndef DOXYGEN
  355. /**
  356. * Wake Up the eXosip_event_wait method.
  357. *
  358. * @param excontext eXosip_t instance.
  359. */
  360. void eXosip_wakeup_event(struct eXosip_t *excontext);
  361. #endif
  362. /** @} */
  363. /**
  364. * @defgroup eXosip2_network eXosip2 network API
  365. * @ingroup eXosip2_setup
  366. * @{
  367. */
  368. /**
  369. * Modify the transport protocol used to send SIP message.
  370. *
  371. * @param msg The SIP message to modify
  372. * @param transport transport protocol to use ("UDP", "TCP" or "TLS")
  373. */
  374. int eXosip_transport_set(osip_message_t *msg, const char *transport);
  375. /**
  376. * Find the current localip (interface with default route).
  377. *
  378. * @param excontext eXosip_t instance.
  379. * @param family AF_INET or AF_INET6
  380. * @param address a string containing the local IP address.
  381. * @param size The size of the string
  382. */
  383. int eXosip_guess_localip(struct eXosip_t *excontext, int family, char *address, int size);
  384. /** @} */
  385. #ifdef __cplusplus
  386. }
  387. #endif
  388. #endif