osip_message.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
  3. Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #ifndef _OSIP_MESSAGE_H_
  17. #define _OSIP_MESSAGE_H_
  18. #include <osipparser2/osip_const.h>
  19. #include <osipparser2/osip_headers.h>
  20. #include <osipparser2/osip_body.h>
  21. /**
  22. * @file osip_message.h
  23. * @brief oSIP SIP Message Accessor Routines
  24. *
  25. * This is the SIP accessor and parser related API.
  26. */
  27. /**
  28. * @defgroup oSIP_MESSAGE oSIP message API
  29. * @ingroup osip2_parser
  30. * @{
  31. */
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /**
  36. * Structure for SIP Message (REQUEST and RESPONSE).
  37. * @var osip_message_t
  38. */
  39. typedef struct osip_message osip_message_t;
  40. /**
  41. * Structure for SIP Message (REQUEST and RESPONSE).
  42. * @struct osip_message
  43. */
  44. struct osip_message {
  45. char *sip_version; /**< SIP version (SIP request only) */
  46. osip_uri_t *req_uri; /**< Request-Uri (SIP request only) */
  47. char *sip_method; /**< METHOD (SIP request only) */
  48. int status_code; /**< Status Code (SIP answer only) */
  49. char *reason_phrase; /**< Reason Phrase (SIP answer only) */
  50. #ifndef MINISIZE
  51. osip_list_t accepts; /**< Accept headers */
  52. osip_list_t accept_encodings; /**< Accept-Encoding headers */
  53. osip_list_t accept_languages; /**< Accept-Language headers */
  54. osip_list_t alert_infos; /**< Alert-Info headers */
  55. osip_list_t allows; /**< Allows headers */
  56. osip_list_t authentication_infos; /**< authentication_info headers */
  57. #endif
  58. osip_list_t authorizations; /**< Authorizations headers */
  59. osip_call_id_t *call_id; /**< Call-ID header */
  60. osip_list_t call_infos; /**< Call-Infos header */
  61. osip_list_t contacts; /**< Contacts headers */
  62. #ifndef MINISIZE
  63. osip_list_t content_encodings; /**< Content-Encodings headers */
  64. #endif
  65. osip_content_length_t *content_length; /**< Content-Length header */
  66. osip_content_type_t *content_type; /**< Content-Type header */
  67. osip_cseq_t *cseq; /**< CSeq header */
  68. #ifndef MINISIZE
  69. osip_list_t error_infos; /**< Error-Info headers */
  70. #endif
  71. osip_from_t *from; /**< From header */
  72. osip_mime_version_t *mime_version; /**< Mime-Version header */
  73. osip_list_t proxy_authenticates; /**< Proxy-Authenticate headers */
  74. #ifndef MINISIZE
  75. osip_list_t proxy_authentication_infos; /**< P-Authentication-Info headers */
  76. #endif
  77. osip_list_t proxy_authorizations; /**< Proxy-authorization headers */
  78. osip_list_t record_routes; /**< Record-Route headers */
  79. osip_list_t routes; /**< Route headers */
  80. osip_to_t *to; /**< To header */
  81. osip_list_t vias; /**< Vias headers */
  82. osip_list_t www_authenticates; /**< WWW-Authenticate headers */
  83. osip_list_t headers; /**< Other headers */
  84. osip_list_t bodies; /**< List of attachements */
  85. /*
  86. 1: structure and buffer "message" are identical.
  87. 2: buffer "message" is not up to date with the structure info (call osip_message_to_str to update it).
  88. */
  89. int message_property; /**< internal value */
  90. char *message; /**< internal value */
  91. size_t message_length; /**< internal value */
  92. void *application_data; /**< can be used by upper layer*/
  93. };
  94. #ifndef SIP_MESSAGE_MAX_LENGTH
  95. /**
  96. * You can re-define your own maximum length for SIP message.
  97. */
  98. #define SIP_MESSAGE_MAX_LENGTH 8000
  99. #endif
  100. #ifndef BODY_MESSAGE_MAX_SIZE
  101. /**
  102. * You can define the maximum length for a body inside a SIP message.
  103. */
  104. #define BODY_MESSAGE_MAX_SIZE 4000
  105. #endif
  106. /**
  107. * Allocate a osip_message_t element.
  108. * @param sip The element to allocate.
  109. */
  110. int osip_message_init(osip_message_t **sip);
  111. /**
  112. * Free all resource in a osip_message_t element.
  113. * @param sip The element to free.
  114. */
  115. void osip_message_free(osip_message_t *sip);
  116. /**
  117. * Parse a osip_message_t element.
  118. * @param sip The resulting element.
  119. * @param buf The buffer to parse.
  120. * @param length The length of the buffer to parse.
  121. */
  122. int osip_message_parse(osip_message_t *sip, const char *buf, size_t length);
  123. /**
  124. * Parse a message/sipfrag part and store it in an osip_message_t element.
  125. * @param sip The resulting element.
  126. * @param buf The buffer to parse.
  127. * @param length The length of the buffer to parse.
  128. */
  129. int osip_message_parse_sipfrag(osip_message_t *sip, const char *buf, size_t length);
  130. /**
  131. * Get a string representation of a osip_message_t element.
  132. * NOTE: You need to release the sip buffer returned by this API when you
  133. * are done with the buffer. ie: osip_free(dest)
  134. * @param sip The element to work on.
  135. * @param dest new allocated buffer returned.
  136. * @param message_length The length of the returned buffer.
  137. */
  138. int osip_message_to_str(osip_message_t *sip, char **dest, size_t *message_length);
  139. /**
  140. * Get a string representation of a message/sipfrag part
  141. * stored in an osip_message_t element.
  142. * NOTE: You need to release the sip buffer returned by this API when you
  143. * are done with the buffer. ie: osip_free(dest)
  144. * @param sip The element to work on.
  145. * @param dest new allocated buffer returned.
  146. * @param message_length The length of the returned buffer.
  147. */
  148. int osip_message_to_str_sipfrag(osip_message_t *sip, char **dest, size_t *message_length);
  149. /**
  150. * Clone a osip_message_t element.
  151. * @param sip The element to clone.
  152. * @param dest The new allocated element cloned.
  153. */
  154. int osip_message_clone(const osip_message_t *sip, osip_message_t **dest);
  155. /**
  156. * Set the reason phrase. This is entirely free in SIP.
  157. * @param sip The element to work on.
  158. * @param reason The reason phrase.
  159. */
  160. void osip_message_set_reason_phrase(osip_message_t *sip, char *reason);
  161. /**
  162. * Get the reason phrase. This is entirely free in SIP.
  163. * @param sip The element to work on.
  164. */
  165. char *osip_message_get_reason_phrase(const osip_message_t *sip);
  166. /**
  167. * Set the status code. This is entirely free in SIP.
  168. * @param sip The element to work on.
  169. * @param statuscode The status code.
  170. */
  171. void osip_message_set_status_code(osip_message_t *sip, int statuscode);
  172. /**
  173. * Get the status code.
  174. * @param sip The element to work on.
  175. */
  176. int osip_message_get_status_code(const osip_message_t *sip);
  177. /**
  178. * Set the method. You can set any string here.
  179. * @param sip The element to work on.
  180. * @param method The method name.
  181. */
  182. void osip_message_set_method(osip_message_t *sip, char *method);
  183. /**
  184. * Get the method name.
  185. * @param sip The element to work on.
  186. */
  187. char *osip_message_get_method(const osip_message_t *sip);
  188. /**
  189. * Set the SIP version used. (default is "SIP/2.0")
  190. * @param sip The element to work on.
  191. * @param version The version of SIP.
  192. */
  193. void osip_message_set_version(osip_message_t *sip, char *version);
  194. /**
  195. * Get the SIP version.
  196. * @param sip The element to work on.
  197. */
  198. char *osip_message_get_version(const osip_message_t *sip);
  199. /**
  200. * Set the Request-URI.
  201. * @param sip The element to work on.
  202. * @param uri The uri to set.
  203. */
  204. void osip_message_set_uri(osip_message_t *sip, osip_uri_t *uri);
  205. /**
  206. * Get the Request-URI.
  207. * @param sip The element to work on.
  208. */
  209. osip_uri_t *osip_message_get_uri(const osip_message_t *sip);
  210. /*
  211. * These are helpfull MACROs to test messages type.
  212. */
  213. /**
  214. * Test if the message is a SIP RESPONSE
  215. * @param msg the SIP message.
  216. */
  217. #define MSG_IS_RESPONSE(msg) ((msg)->status_code != 0)
  218. /**
  219. * Test if the message is a SIP REQUEST
  220. * @param msg the SIP message.
  221. */
  222. #define MSG_IS_REQUEST(msg) ((msg)->status_code == 0)
  223. /**
  224. * Test if the message is an INVITE REQUEST
  225. * @param msg the SIP message.
  226. */
  227. #define MSG_IS_INVITE(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "INVITE"))
  228. /**
  229. * Test if the message is an ACK REQUEST
  230. * @param msg the SIP message.
  231. */
  232. #define MSG_IS_ACK(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "ACK"))
  233. /**
  234. * Test if the message is a REGISTER REQUEST
  235. * @param msg the SIP message.
  236. */
  237. #define MSG_IS_REGISTER(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "REGISTER"))
  238. /**
  239. * Test if the message is a BYE REQUEST
  240. * @param msg the SIP message.
  241. */
  242. #define MSG_IS_BYE(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "BYE"))
  243. /**
  244. * Test if the message is an OPTIONS REQUEST
  245. * @param msg the SIP message.
  246. */
  247. #define MSG_IS_OPTIONS(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "OPTIONS"))
  248. /**
  249. * Test if the message is an INFO REQUEST
  250. * @param msg the SIP message.
  251. */
  252. #define MSG_IS_INFO(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "INFO"))
  253. /**
  254. * Test if the message is a CANCEL REQUEST
  255. * @param msg the SIP message.
  256. */
  257. #define MSG_IS_CANCEL(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "CANCEL"))
  258. /**
  259. * Test if the message is a REFER REQUEST
  260. * @param msg the SIP message.
  261. */
  262. #define MSG_IS_REFER(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "REFER"))
  263. /**
  264. * Test if the message is a NOTIFY REQUEST
  265. * @param msg the SIP message.
  266. */
  267. #define MSG_IS_NOTIFY(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "NOTIFY"))
  268. /**
  269. * Test if the message is a SUBSCRIBE REQUEST
  270. * @def MSG_IS_SUBSCRIBE
  271. * @param msg the SIP message.
  272. */
  273. #define MSG_IS_SUBSCRIBE(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "SUBSCRIBE"))
  274. /**
  275. * Test if the message is a MESSAGE REQUEST
  276. * @param msg the SIP message.
  277. */
  278. #define MSG_IS_MESSAGE(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "MESSAGE"))
  279. /**
  280. * Test if the message is a PRACK REQUEST (!! PRACK IS NOT SUPPORTED by the fsm!!)
  281. * @param msg the SIP message.
  282. */
  283. #define MSG_IS_PRACK(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "PRACK"))
  284. /**
  285. * Test if the message is an UPDATE REQUEST
  286. * @param msg the SIP message.
  287. */
  288. #define MSG_IS_UPDATE(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "UPDATE"))
  289. /**
  290. * Test if the message is an UPDATE REQUEST
  291. * @param msg the SIP message.
  292. */
  293. #define MSG_IS_PUBLISH(msg) (MSG_IS_REQUEST(msg) && 0 == strcmp((msg)->sip_method, "PUBLISH"))
  294. /**
  295. * Test if the message is a response with status between 100 and 199
  296. * @param msg the SIP message.
  297. */
  298. #define MSG_IS_STATUS_1XX(msg) ((msg)->status_code >= 100 && (msg)->status_code < 200)
  299. /**
  300. * Test if the message is a response with status between 200 and 299
  301. * @param msg the SIP message.
  302. */
  303. #define MSG_IS_STATUS_2XX(msg) ((msg)->status_code >= 200 && (msg)->status_code < 300)
  304. /**
  305. * Test if the message is a response with status between 300 and 399
  306. * @param msg the SIP message.
  307. */
  308. #define MSG_IS_STATUS_3XX(msg) ((msg)->status_code >= 300 && (msg)->status_code < 400)
  309. /**
  310. * Test if the message is a response with status between 400 and 499
  311. * @param msg the SIP message.
  312. */
  313. #define MSG_IS_STATUS_4XX(msg) ((msg)->status_code >= 400 && (msg)->status_code < 500)
  314. /**
  315. * Test if the message is a response with status between 500 and 599
  316. * @param msg the SIP message.
  317. */
  318. #define MSG_IS_STATUS_5XX(msg) ((msg)->status_code >= 500 && (msg)->status_code < 600)
  319. /**
  320. * Test if the message is a response with status between 600 and 699
  321. * @param msg the SIP message.
  322. */
  323. #define MSG_IS_STATUS_6XX(msg) ((msg)->status_code >= 600 && (msg)->status_code < 700)
  324. /**
  325. * Test if the message is a response with a status set to the code value.
  326. * @param msg the SIP message.
  327. * @param code the status code.
  328. */
  329. #define MSG_TEST_CODE(msg, code) (MSG_IS_RESPONSE(msg) && (code) == (msg)->status_code)
  330. /**
  331. * Test if the message is a response for a REQUEST of certain type
  332. * @param msg the SIP message.
  333. * @param requestname the method name to match.
  334. */
  335. #define MSG_IS_RESPONSE_FOR(msg, requestname) (MSG_IS_RESPONSE(msg) && 0 == strcmp((msg)->cseq->method, (requestname)))
  336. /**
  337. * Allocate a generic parameter element.
  338. * @param GP The element to work on.
  339. */
  340. #define osip_generic_param_init(GP) osip_uri_param_init(GP)
  341. /**
  342. * Free a generic parameter element.
  343. * @param GP The element to work on.
  344. */
  345. #define osip_generic_param_free(GP) osip_uri_param_free(GP)
  346. /**
  347. * Set values of a generic parameter element.
  348. * @param GP The element to work on.
  349. * @param NAME The token name.
  350. * @param VALUE The token value.
  351. */
  352. #define osip_generic_param_set(GP, NAME, VALUE) osip_uri_param_set(GP, NAME, VALUE)
  353. /**
  354. * Clone a generic parameter element.
  355. * @param GP The element to work on.
  356. * @param DEST The resulting new allocated buffer.
  357. */
  358. #define osip_generic_param_clone osip_uri_param_clone
  359. #ifndef DOXYGEN
  360. /*
  361. * Free a list of a generic parameter element.
  362. * @param LIST The list of generic parameter element to free.
  363. */
  364. #define osip_generic_param_freelist(LIST) osip_uri_param_freelist(LIST)
  365. #endif
  366. /**
  367. * Allocate and add a generic parameter element in a list.
  368. * @param LIST The list of generic parameter element to work on.
  369. * @param NAME The token name.
  370. * @param VALUE The token value.
  371. */
  372. #define osip_generic_param_add(LIST, NAME, VALUE) osip_uri_param_add(LIST, NAME, VALUE)
  373. /**
  374. * Find in a generic parameter element in a list.
  375. * @param LIST The list of generic parameter element to work on.
  376. * @param NAME The name of the parameter element to find.
  377. * @param DEST A pointer on the element found.
  378. */
  379. #define osip_generic_param_get_byname(LIST, NAME, DEST) osip_uri_param_get_byname(LIST, NAME, DEST)
  380. /**
  381. * Set the name of a generic parameter element.
  382. * @param generic_param The element to work on.
  383. * @param name the token name to set.
  384. */
  385. void osip_generic_param_set_name(osip_generic_param_t *generic_param, char *name);
  386. /**
  387. * Get the name of a generic parameter element.
  388. * @param generic_param The element to work on.
  389. */
  390. char *osip_generic_param_get_name(const osip_generic_param_t *generic_param);
  391. /**
  392. * Set the value of a generic parameter element.
  393. * @param generic_param The element to work on.
  394. * @param value the token name to set.
  395. */
  396. void osip_generic_param_set_value(osip_generic_param_t *generic_param, char *value);
  397. /**
  398. * Get the value of a generic parameter element.
  399. * @param generic_param The element to work on.
  400. */
  401. char *osip_generic_param_get_value(const osip_generic_param_t *generic_param);
  402. /**
  403. * Get the a known header from a list of known header.
  404. * @param header_list The element to work on.
  405. * @param pos The index of the element to get.
  406. * @param dest A pointer on the header found.
  407. */
  408. int osip_message_get_knownheaderlist(osip_list_t *header_list, int pos, void **dest);
  409. /** @} */
  410. #ifdef __cplusplus
  411. }
  412. #endif
  413. #endif