ht2-parser.dox 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @ingroup libosip2 The GNU oSIP stack
  3. * @defgroup howto_parser How-To parse SIP message.
  4. * @section howto_parser1 Parser API
  5. For the SIP parser, the API is documented in osip_message.h
  6. * @section howto_parser2 Basic Parser operations
  7. + Here is the sequence needed to parse a given buffer containing
  8. a sip request or response.
  9. Because the SIP message can contains binary data in its body part,
  10. the length of the buffer must be given to the API.
  11. ~~~~~~~{.c}
  12. osip_message_t *sip;
  13. int i;
  14. i=osip_message_init(&sip);
  15. if (i!=0) { fprintf(stderr, "cannot allocate\n"); return -1; }
  16. i=osip_message_parse(sip, buffer, length_of_buffer);
  17. if (i!=0) { fprintf(stderr, "cannot parse sip message\n"); }
  18. osip_message_free(sip);
  19. ~~~~~~~
  20. + Here is the sequence needed to convert the message into a printable
  21. string.
  22. **Note**: dest is allocated dynamically and must be released at the end
  23. of the call sequence to avoid memory leaks.
  24. When converting SIP message, the final length of the allocated buffer
  25. will be returned in the third argument. You then have the knowledge of
  26. the length of the data received.
  27. ~~~~~~~{.c}
  28. char *dest=NULL;
  29. int length=0;
  30. i = osip_message_to_str(sip, &dest, &length);
  31. if (i!=0) { fprintf(stderr, "cannot get printable message\n"); return -1; }
  32. fprintf(stdout, "message:\n%s\n", dest);
  33. osip_free(dest);
  34. ~~~~~~~
  35. + Here is the code to directly create an event for osip2 transactions.
  36. When using libosip2 and its transaction management features, you'll generally
  37. only need to create a suitable events. Thus, you'll probably use this API (only
  38. for SIP message that you receive!):
  39. ~~~~~~~{.c}
  40. osip_event_t *evt;
  41. int length = size_of_buffer;
  42. evt = osip_parse(buffer, i);
  43. ~~~~~~~
  44. **Note**: It is important to understand that the libosip2 parser will not check
  45. completely if the message is compliant and well formed. The application
  46. layer is still responsible for this.
  47. The libosip2 parser will not detect all errors (like missing headers) and it will
  48. be up to you to verify at every action that things are as you are
  49. expecting them to be!
  50. * @section howto_parser3 Built-in Header Operations
  51. osip_message_t structure contains pointers to most common headers. Other
  52. headers (Any of them, even ones not being defined by rfc) will be saved
  53. into a list of other headers.
  54. + Here is the list of parameters for headers with built-in support:
  55. ~~~~~~~{.c}
  56. struct osip_message {
  57. ...
  58. osip_list_t accepts;
  59. osip_list_t accept_encodings;
  60. osip_list_t accept_languages;
  61. osip_list_t alert_infos;
  62. osip_list_t allows;
  63. osip_list_t authentication_infos;
  64. osip_list_t authorizations;
  65. osip_call_id_t *call_id;
  66. osip_list_t call_infos;
  67. osip_list_t contacts;
  68. osip_list_t content_encodings;
  69. osip_content_length_t *content_length;
  70. osip_content_type_t *content_type;
  71. osip_cseq_t *cseq;
  72. osip_list_t error_infos;
  73. osip_from_t *from;
  74. osip_mime_version_t *mime_version;
  75. osip_list_t proxy_authenticates;
  76. osip_list_t proxy_authentication_infos:
  77. osip_list_t proxy_authorizations;
  78. osip_list_t record_routes;
  79. osip_list_t routes;
  80. osip_to_t *to;
  81. osip_list_t vias;
  82. osip_list_t www_authenticates;
  83. ...
  84. };
  85. ~~~~~~~
  86. * @section howto_parser4 Other Header Operations
  87. Any other headers will be saved into a list of other headers.
  88. + Here is the osip_list for headers without built-in support:
  89. ~~~~~~~{.c}
  90. struct osip_message {
  91. ...
  92. osip_list_t headers;
  93. ...
  94. };
  95. ~~~~~~~
  96. **Note**: Those headers, even if not built-in, may have the same syntax
  97. as other headers (many contains URI for example)µ. So you can easily re-use
  98. already existing headers API to analyse them.
  99. + Here is code to add header in a sip message:
  100. ~~~~~~~{.c}
  101. osip_message_set_header (msg, "Refer-to", refer_to_header);
  102. ~~~~~~~
  103. ~~~~~~~{.c}
  104. osip_message_set_header (msg, "Subscription-State", subscription_state_header);
  105. ~~~~~~~
  106. ~~~~~~~{.c}
  107. osip_message_set_header (msg, "Require", "timer");
  108. ~~~~~~~
  109. ~~~~~~~{.c}
  110. osip_message_set_header (msg, "RAck", rack_header);
  111. ~~~~~~~
  112. ~~~~~~~{.c}
  113. osip_message_set_header (msg, "Min-SE", min_se_header);
  114. ~~~~~~~
  115. ~~~~~~~{.c}
  116. osip_message_set_header (msg, "x-header", x_header);
  117. ~~~~~~~
  118. + Here is code to find the FIRST specific header in a sip message:
  119. ~~~~~~~{.c}
  120. osip_message_header_get_byname (msg, "min-se", 0, &min_se_header);
  121. if (min_se_header != NULL && min_se_header->hvalue != NULL) {
  122. ~~~~~~~
  123. **Note**: if you are looking for all headers with same name,
  124. you need to find the first one and re-use the return position as
  125. a parameter to search for the next one.
  126. + Here is code to find all headers with same name in a sip message:
  127. ~~~~~~~{.c}
  128. int pos=0;
  129. while (1) {
  130. pos = osip_message_header_get_byname (msg, "x-header", pos, &min_se_new);
  131. if (min_se_new == NULL) {
  132. break; //no more headers
  133. }
  134. }
  135. ~~~~~~~
  136. + Here is code to handle short header usage
  137. In some case, short name are used in SIP message for some headers.
  138. For example, "x" is equivalent to "session-expires". In that case,
  139. you need to search for both:
  140. ~~~~~~~{.c}
  141. osip_message_header_get_byname (msg, "session-expires", 0, &exp);
  142. if (exp == NULL)
  143. osip_message_header_get_byname (msg, "x", 0, &exp);
  144. ~~~~~~~
  145. * @section howto_parser5 Known header parser
  146. You can browse the API documentation to know more about header parser.
  147. libosipparser2 provides specific parser for the built-in headers (like
  148. via, from, to, call-id, route, etc... see above for full list)
  149. ~~~~~~~{.c}
  150. #include <osipparser2/headers/osip_header.h>
  151. #include <osipparser2/headers/osip_accept.h>
  152. #include <osipparser2/headers/osip_accept_encoding.h>
  153. #include <osipparser2/headers/osip_accept_language.h>
  154. #include <osipparser2/headers/osip_alert_info.h>
  155. #include <osipparser2/headers/osip_allow.h>
  156. #include <osipparser2/headers/osip_authentication_info.h>
  157. #include <osipparser2/headers/osip_authorization.h>
  158. #include <osipparser2/headers/osip_call_id.h>
  159. #include <osipparser2/headers/osip_call_info.h>
  160. #include <osipparser2/headers/osip_contact.h>
  161. #include <osipparser2/headers/osip_content_disposition.h>
  162. #include <osipparser2/headers/osip_content_encoding.h>
  163. #include <osipparser2/headers/osip_content_length.h>
  164. #include <osipparser2/headers/osip_content_type.h>
  165. #include <osipparser2/headers/osip_cseq.h>
  166. #include <osipparser2/headers/osip_error_info.h>
  167. #include <osipparser2/headers/osip_from.h>
  168. #include <osipparser2/headers/osip_mime_version.h>
  169. #include <osipparser2/headers/osip_proxy_authenticate.h>
  170. #include <osipparser2/headers/osip_proxy_authentication_info.h>
  171. #include <osipparser2/headers/osip_proxy_authorization.h>
  172. #include <osipparser2/headers/osip_record_route.h>
  173. #include <osipparser2/headers/osip_route.h>
  174. #include <osipparser2/headers/osip_to.h>
  175. #include <osipparser2/headers/osip_via.h>
  176. #include <osipparser2/headers/osip_www_authenticate.h>
  177. ~~~~~~~
  178. Here are some sample code for Via.
  179. + Get Via from message:
  180. ~~~~~~~{.c}
  181. osip_via_t *dest;
  182. osip_message_get_via (msg, 0, &dest);
  183. ~~~~~~~
  184. + Add Via in message
  185. ~~~~~~~{.c}
  186. char tmp[200];
  187. snprintf (tmp, 200, "SIP/2.0/UDP %s:%s;rport;branch=z9hG4bK%u", ip, port, osip_build_random_number ());
  188. osip_message_set_via (msg, tmp);
  189. ~~~~~~~
  190. + Parse Via:
  191. ~~~~~~~{.c}
  192. osip_via_t *header;
  193. char tmp[200];
  194. snprintf (tmp, 200, "SIP/2.0/UDP %s:%s;rport;branch=z9hG4bK%u", ip, port, osip_build_random_number ());
  195. osip_via_init (&header);
  196. osip_via_parse (header, tmp);
  197. osip_free(header);
  198. ~~~~~~~
  199. + Many other getter/setter API exists, see:
  200. ~~~~~~~{.c}
  201. #include <osipparser2/headers/osip_via.h>
  202. ~~~~~~~
  203. */