ims_getters.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2012 Smile Communications, [email protected]
  5. * Copyright (C) 2012 Smile Communications, [email protected]
  6. * The initial version of this code was written by Dragos Vingarzan
  7. * (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
  8. * Fruanhofer Institute. It was and still is maintained in a separate
  9. * branch of the original SER. We are therefore migrating it to
  10. * Kamailio/SR and look forward to maintaining it from here on out.
  11. * 2011/2012 Smile Communications, Pty. Ltd.
  12. * ported/maintained/improved by
  13. * Jason Penton (jason(dot)penton(at)smilecoms.com and
  14. * Richard Good (richard(dot)good(at)smilecoms.com) as part of an
  15. * effort to add full IMS support to Kamailio/SR using a new and
  16. * improved architecture
  17. *
  18. * NB: Alot of this code was originally part of OpenIMSCore,
  19. * FhG Fokus.
  20. * Copyright (C) 2004-2006 FhG Fokus
  21. * Thanks for great work! This is an effort to
  22. * break apart the various CSCF functions into logically separate
  23. * components. We hope this will drive wider use. We also feel
  24. * that in this way the architecture is more complete and thereby easier
  25. * to manage in the Kamailio/SR environment
  26. *
  27. * This file is part of Kamailio, a free SIP server.
  28. *
  29. * Kamailio is free software; you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation; either version 2 of the License, or
  32. * (at your option) any later version
  33. *
  34. * Kamailio is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with this program; if not, write to the Free Software
  41. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  42. *
  43. */
  44. #ifndef __IMS_GETTERS_H
  45. #define __IMS_GETTERS_H
  46. #include "../../str.h"
  47. #include "../../parser/contact/parse_contact.h"
  48. /** Return and break the execution of routng script */
  49. #define CSCF_RETURN_BREAK 0
  50. /** Return true in the routing script */
  51. #define CSCF_RETURN_TRUE 1
  52. /** Return false in the routing script */
  53. #define CSCF_RETURN_FALSE -1
  54. /** Return error in the routing script */
  55. #define CSCF_RETURN_ERROR -2
  56. /** Enumeration for dialog directions */
  57. enum cscf_dialog_direction {
  58. CSCF_MOBILE_ORIGINATING=0,
  59. CSCF_MOBILE_TERMINATING=1,
  60. CSCF_MOBILE_UNKNOWN=2
  61. };
  62. /**
  63. * Duplicate a str, safely.
  64. * \Note This checks if:
  65. * - src was an empty string
  66. * - malloc failed
  67. * \Note On any error, the dst values are reset for safety
  68. * \Note A label "out_of_memory" must be defined in the calling function to handle
  69. * allocation errors.
  70. * @param dst - destination str
  71. * @param src - source src
  72. * @param mem - type of mem to duplicate into (shm/pkg)
  73. */
  74. #define str_dup(dst,src,mem) \
  75. do {\
  76. if ((src).len) {\
  77. (dst).s = mem##_malloc((src).len);\
  78. if (!(dst).s){\
  79. LM_ERR("Error allocating %d bytes in %s!\n",(src).len,#mem);\
  80. (dst).len = 0;\
  81. goto out_of_memory;\
  82. }\
  83. memcpy((dst).s,(src).s,(src).len);\
  84. (dst).len = (src).len;\
  85. }else{\
  86. (dst).s=0;(dst).len=0;\
  87. }\
  88. } while (0)
  89. /**
  90. * Frees a str content.
  91. * @param x - the str to free
  92. * @param mem - type of memory that the content is using (shm/pkg)
  93. */
  94. #define str_free(x,mem) \
  95. do {\
  96. if ((x).s) mem##_free((x).s);\
  97. (x).s=0;(x).len=0;\
  98. } while(0)
  99. /**
  100. * Parses all the contact headers.
  101. * @param msg - the SIP message
  102. * @returns the first contact_body
  103. */
  104. contact_body_t *cscf_parse_contacts(struct sip_msg *msg);
  105. /**
  106. * Returns the Private Identity extracted from the Authorization header.
  107. * If none found there takes the SIP URI in To without the "sip:" prefix
  108. * \todo - remove the fallback case to the To header
  109. * @param msg - the SIP message
  110. * @param realm - the realm to match in an Authorization header
  111. * @returns the str containing the private id, no mem dup
  112. */
  113. str cscf_get_private_identity(struct sip_msg *msg, str realm);
  114. /**
  115. * Returns the Public Identity extracted from the To header
  116. * @param msg - the SIP message
  117. * @returns the str containing the public id, no mem dup
  118. */
  119. str cscf_get_public_identity(struct sip_msg *msg);
  120. /**
  121. * Returns the expires value from the Expires header in the message.
  122. * It searches into the Expires header and if not found returns -1
  123. * @param msg - the SIP message, if available
  124. * @is_shm - msg from from shared memory
  125. * @returns the value of the expire or -1 if not found
  126. */
  127. int cscf_get_expires_hdr(struct sip_msg *msg, int is_shm);
  128. /**
  129. * Returns the expires value from the message.
  130. * First it searches into the Expires header and if not found it also looks
  131. * into the expires parameter in the contact header
  132. * @param msg - the SIP message
  133. * @param is_shm - msg from shared memory
  134. * @returns the value of the expire or the default 3600 if none found
  135. */
  136. int cscf_get_max_expires(struct sip_msg *msg, int is_shm);
  137. /**
  138. * Finds if the message contains the orig parameter in the first Route header
  139. * @param msg - the SIP message
  140. * @param str1 - not used
  141. * @param str2 - not used
  142. * @returns #CSCF_RETURN_TRUE if yes, else #CSCF_RETURN_FALSE
  143. */
  144. int cscf_has_originating(struct sip_msg *msg, char *str1, char *str2);
  145. /**
  146. * Looks for the P-Asserted-Identity header and extracts its content
  147. * @param msg - the sip message
  148. * @is_shm - is the message a shm message
  149. * @returns the asserted identity
  150. */
  151. str cscf_get_asserted_identity(struct sip_msg *msg, int is_shm);
  152. /**
  153. * Extracts the realm from a SIP/TEL URI.
  154. * - SIP - the hostname
  155. * - TEL - the phone-context parameter
  156. * @param msg - the SIP message
  157. * @returns the realm
  158. */
  159. str cscf_get_realm_from_uri(str uri);
  160. /**
  161. * Delivers the Realm from request URI
  162. * @param msg sip message
  163. * @returns realm as String on success 0 on fail
  164. */
  165. str cscf_get_realm_from_ruri(struct sip_msg *msg);
  166. /**
  167. * Get the Public Identity from the Request URI of the message
  168. * @param msg - the SIP message
  169. * @returns the public identity
  170. */
  171. str cscf_get_public_identity_from_requri(struct sip_msg *msg);
  172. /**
  173. * Get the contact from the Request URI of the message
  174. * NB: free returned result str when done from shm
  175. * @param msg - the SIP message
  176. * @returns the contact (don't forget to free from shm)
  177. *
  178. * NOTE: should only be called when REQ URI has been converted sip:user@IP_ADDRESS:PORT or tel:IP_ADDRESS:PORT
  179. */
  180. str cscf_get_contact_from_requri(struct sip_msg *msg);
  181. /**
  182. * Looks for the Call-ID header
  183. * @param msg - the sip message
  184. * @param hr - ptr to return the found hdr_field
  185. * @returns the callid value
  186. */
  187. str cscf_get_call_id(struct sip_msg *msg, struct hdr_field **hr);
  188. /**
  189. * Check if the contact has an URI parameter with the value "sos",
  190. * used for detecting an Emergency Registration
  191. * http://tools.ietf.org/html/draft-patel-ecrit-sos-parameter-0x
  192. * @param uri - contact uri to be checked
  193. * @return 1 if found, 0 if not, -1 on error
  194. */
  195. int cscf_get_sos_uri_param(str uri);
  196. /**
  197. * Return the P-Visited-Network-ID header
  198. * @param msg - the SIP message
  199. * @returns the str with the header's body
  200. */
  201. str cscf_get_visited_network_id(struct sip_msg *msg, struct hdr_field **h);
  202. /**
  203. * Adds a header to the message as the first one in the message
  204. * @param msg - the message to add a header to
  205. * @param content - the str containing the new header
  206. * @returns 1 on succes, 0 on failure
  207. */
  208. int cscf_add_header_first(struct sip_msg *msg, str *hdr, int type);
  209. /**
  210. * Returns the next header structure for a given header name.
  211. * @param msg - the SIP message to look into
  212. * @param header_name - the name of the header to search for
  213. * @param last_header - last header to ignore in the search, or NULL if to start from the first one
  214. * @returns the hdr_field on success or NULL if not found
  215. */
  216. struct hdr_field* cscf_get_next_header(struct sip_msg * msg,
  217. /**
  218. * Looks for the First Via header and returns its body.
  219. * @param msg - the SIP message
  220. * @param h - the hdr_field to fill with the result
  221. * @returns the first via_body
  222. */ str header_name, struct hdr_field* last_header);
  223. struct via_body* cscf_get_first_via(struct sip_msg *msg, struct hdr_field **h);
  224. /**
  225. * Looks for the Last Via header and returns it.
  226. * @param msg - the SIP message
  227. * @returns the last via body body
  228. */
  229. struct via_body* cscf_get_last_via(struct sip_msg *msg);
  230. /**
  231. * Looks for the UE Via in First Via header if its a request
  232. * or in the last if its a response and returns its body
  233. * @param msg - the SIP message
  234. * @returns the via of the UE
  235. */
  236. struct via_body* cscf_get_ue_via(struct sip_msg *msg);
  237. /**
  238. * Looks for the WWW-Authenticate header and returns its body.
  239. * @param msg - the SIP message
  240. * @param h - the hdr_field to fill with the result
  241. * @returns the www-authenticate body
  242. */
  243. str cscf_get_authenticate(struct sip_msg *msg, struct hdr_field **h);
  244. /**
  245. * Adds a header to the message
  246. * @param msg - the message to add a header to
  247. * @param content - the str containing the new header
  248. * @returns 1 on succes, 0 on failure
  249. */
  250. int cscf_add_header(struct sip_msg *msg, str *hdr, int type);
  251. /**
  252. * Get the expires header value from a message.
  253. * @param msg - the SIP message
  254. * @returns the expires value or -1 if not found
  255. */
  256. int cscf_get_expires(struct sip_msg *msg);
  257. /**
  258. * Check if the message is an initial request for a dialog.
  259. * - BYE, PRACK, UPDATE, NOTIFY belong to an already existing dialog
  260. * @param msg - the message to check
  261. * @returns 1 if initial, 0 if not
  262. */
  263. int cscf_is_initial_request(struct sip_msg *msg);
  264. /**
  265. * Get the public identity from P-Asserted-Identity, or From if asserted not found.
  266. * @param msg - the SIP message
  267. * @param uri - uri to fill into
  268. * @returns 1 if found, 0 if not
  269. */
  270. int cscf_get_originating_user(struct sip_msg * msg, str *uri);
  271. /**
  272. * Get public identity from Request-URI for terminating.
  273. * returns in uri the freshly pkg allocated uri - don't forget to free
  274. * @param msg - the SIP message
  275. * @param uri - uri to fill into
  276. * @returns 1 if found, else 0
  277. */
  278. int cscf_get_terminating_user(struct sip_msg * msg, str *uri);
  279. /**
  280. * Return the P-Access-Network-Info header
  281. * @param msg - the SIP message
  282. * @returns the str with the header's body
  283. */
  284. str cscf_get_access_network_info(struct sip_msg *msg, struct hdr_field **h);
  285. /**
  286. * Return the P-Charging-Vector header
  287. * @param msg - the SIP message
  288. * @returns the str with the header's body
  289. */
  290. str cscf_get_charging_vector(struct sip_msg *msg, struct hdr_field **h);
  291. /**
  292. * Return the P-Charging-Vector tokens
  293. * @param msg - the SIP message
  294. * @returns the str with icid, orig_ioi and term_ioi
  295. */
  296. int cscf_get_p_charging_vector(struct sip_msg *msg, str * icid, str * orig_ioi,
  297. str * term_ioi);
  298. /**
  299. * Get the to tag
  300. * @param msg - the SIP Message to look into
  301. * @param tag - the pointer to the tag to write to
  302. * @returns 0 on error or 1 on success
  303. */
  304. int cscf_get_to_tag(struct sip_msg* msg, str* tag);
  305. /**
  306. * Get the from tag
  307. * @param msg - the SIP message to look into
  308. * @param tag - the pointer to the tag to write to
  309. * @returns 0 on error or 1 on success
  310. */
  311. int cscf_get_from_tag(struct sip_msg* msg, str* tag);
  312. /**
  313. * Get the local uri from the From header.
  314. * @param msg - the message to look into
  315. * @param local_uri - ptr to fill with the value
  316. * @returns 1 on success or 0 on error
  317. */
  318. int cscf_get_from_uri(struct sip_msg* msg, str *local_uri);
  319. /**
  320. * Get the local uri from the To header.
  321. * @param msg - the message to look into
  322. * @param local_uri - ptr to fill with the value
  323. * @returns 1 on success or 0 on error
  324. */
  325. int cscf_get_to_uri(struct sip_msg* msg, str *local_uri);
  326. /**
  327. * Looks for the Event header and extracts its content.
  328. * @param msg - the sip message
  329. * @returns the string event value or an empty string if none found
  330. */
  331. str cscf_get_event(struct sip_msg *msg);
  332. /*! \brief
  333. * Check if the originating REGISTER message was formed correctly
  334. * The whole message must be parsed before calling the function
  335. * _s indicates whether the contact was star
  336. */
  337. int cscf_check_contacts(struct sip_msg* _m, int* _s);
  338. /*! \brief
  339. * parse all the messages required by the registrar
  340. */
  341. int cscf_parse_message_for_register(struct sip_msg* _m);
  342. /**
  343. * Returns the content of the P-Associated-URI header
  344. * Public_id is pkg_alloced and should be later freed.
  345. * Inside values are not duplicated.
  346. * @param msg - the SIP message to look into
  347. * @param public_id - array to be allocated and filled with the result
  348. * @param public_id_cnt - the size of the public_id array
  349. * @param is_shm - msg from shared memory
  350. * @returns 1 on success or 0 on error
  351. */
  352. int cscf_get_p_associated_uri(struct sip_msg *msg,str **public_id,int *public_id_cnt, int is_shm);
  353. /**
  354. * Looks for the realm parameter in the Authorization header and returns its value.
  355. * @param msg - the SIP message
  356. * @returns the realm
  357. */
  358. str cscf_get_realm(struct sip_msg *msg);
  359. /**
  360. * Returns the content of the Service-Route header.
  361. * data vector is pkg_alloced and should be later freed
  362. * inside values are not duplicated
  363. * @param msg - the SIP message
  364. * @param size - size of the returned vector, filled with the result
  365. * @param is_shm - msg from shared memory
  366. * @returns - the str vector of uris
  367. */
  368. str* cscf_get_service_route(struct sip_msg *msg, int *size, int is_shm);
  369. /**
  370. * Returns the s_dialog_direction from the direction string.
  371. * @param direction - "orig" or "term"
  372. * @returns the s_dialog_direction if ok or #DLG_MOBILE_UNKNOWN if not found
  373. */
  374. enum cscf_dialog_direction cscf_get_dialog_direction(char *direction);
  375. long cscf_get_content_length (struct sip_msg* msg);
  376. /**
  377. * Looks for the Contact header and extracts its content
  378. * @param msg - the sip message
  379. * @returns the first contact in the message
  380. */
  381. str cscf_get_contact(struct sip_msg *msg);
  382. /**
  383. * Adds a header to the reply message
  384. * @param msg - the request to add a header to its reply
  385. * @param content - the str containing the new header
  386. * @returns 1 on succes, 0 on failure
  387. */
  388. int cscf_add_header_rpl(struct sip_msg *msg, str *hdr);
  389. /**
  390. * Looks for the Call-ID header
  391. * @param msg - the sip message
  392. * @param hr - ptr to return the found hdr_field
  393. * @returns the callid value
  394. */
  395. int cscf_get_cseq(struct sip_msg *msg,struct hdr_field **hr);
  396. /**
  397. * Looks for the P-Called-Party-ID header and extracts the public identity from it
  398. * @param msg - the sip message
  399. * @param hr - ptr to return the found hdr_field
  400. * @returns the P-Called_Party-ID
  401. */
  402. str cscf_get_public_identity_from_called_party_id(struct sip_msg *msg,struct hdr_field **hr);
  403. #endif