No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

hace 10 meses
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #ifndef _sip_header_h_
  2. #define _sip_header_h_
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <stddef.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include <errno.h>
  10. #include "cstring.h"
  11. #include "darray.h"
  12. #if defined(__cplusplus)
  13. extern "C" {
  14. #endif
  15. // 10.2.6 Discovering a Registrar (p62)
  16. #define SIP_MULTICAST_HOST "sip.mcast.net"
  17. #define SIP_MULTICAST_ADDRESS "224.0.1.75"
  18. #define DARRAY_DECLARE(name) \
  19. struct name##s_t \
  20. { \
  21. struct darray_t arr; \
  22. }; \
  23. \
  24. void name##s_init(struct name##s_t* p); \
  25. void name##s_free(struct name##s_t* p); \
  26. int name##s_count(const struct name##s_t* p); \
  27. int name##s_push(struct name##s_t* p, struct name##_t* item); \
  28. struct name##_t* name##s_get(const struct name##s_t* p, int index);
  29. #define DARRAY_IMPLEMENT(name, N) \
  30. static inline void name##s_arrfree(struct darray_t *arr) \
  31. { \
  32. int i; \
  33. struct name##s_t* p; \
  34. p = (struct name##s_t*)(((char*)arr) - offsetof(struct name##s_t, arr)); \
  35. for(i = 0; i < darray_count(arr); i++) \
  36. name##_free(name##s_get(p, i)); \
  37. if(arr && arr->elements) \
  38. free(arr->elements); \
  39. } \
  40. \
  41. static inline void* name##s_arralloc(struct darray_t *arr, size_t size) \
  42. { \
  43. return realloc(arr->elements, size);\
  44. } \
  45. \
  46. void name##s_init(struct name##s_t* p) \
  47. { \
  48. p->arr.free = name##s_arrfree; \
  49. p->arr.alloc = name##s_arralloc; \
  50. darray_init(&p->arr, sizeof(struct name##_t), N); \
  51. } \
  52. \
  53. void name##s_free(struct name##s_t* p) \
  54. { \
  55. darray_free(&p->arr); \
  56. } \
  57. \
  58. int name##s_push(struct name##s_t* p, struct name##_t* item) \
  59. { \
  60. return darray_insert(&p->arr, -1, item); \
  61. } \
  62. \
  63. struct name##_t* name##s_get(const struct name##s_t* p, int index) \
  64. { \
  65. return (struct name##_t*)darray_get(&((struct name##s_t*)p)->arr, index); \
  66. } \
  67. \
  68. int name##s_count(const struct name##s_t* p) \
  69. { \
  70. return darray_count(&p->arr); \
  71. }
  72. struct sip_param_t
  73. {
  74. struct cstring_t name;
  75. struct cstring_t value;
  76. };
  77. DARRAY_DECLARE(sip_param);
  78. struct sip_uri_t
  79. {
  80. struct cstring_t scheme;
  81. struct cstring_t host; // userinfo@host:port
  82. struct sip_params_t parameters;
  83. struct cstring_t transport; // udp/tcp/sctp/tls/other
  84. struct cstring_t method;
  85. struct cstring_t maddr; // the server address to be contacted for this user, overriding any address derived from the host field
  86. struct cstring_t user; // phone/ip
  87. int ttl;
  88. int lr;
  89. int rport; // 0-not found, -1-no-value, other-value
  90. struct sip_params_t headers;
  91. };
  92. DARRAY_DECLARE(sip_uri);
  93. struct sip_requestline_t
  94. {
  95. struct cstring_t method;
  96. struct sip_uri_t uri;
  97. };
  98. struct sip_statusline_t
  99. {
  100. int code;
  101. int verminor, vermajor;
  102. char protocol[64];
  103. struct cstring_t reason;
  104. };
  105. struct sip_contact_t
  106. {
  107. struct sip_uri_t uri;
  108. struct cstring_t nickname;
  109. // parameters
  110. struct cstring_t tag; // TO/FROM
  111. double q; // c-p-q
  112. int64_t expires; // delta-seconds, default 3600
  113. struct sip_params_t params; // include tag/q/expires
  114. };
  115. DARRAY_DECLARE(sip_contact);
  116. struct sip_via_t
  117. {
  118. struct cstring_t protocol;
  119. struct cstring_t version;
  120. struct cstring_t transport;
  121. struct cstring_t host; // sent-by host:port
  122. // parameters
  123. struct cstring_t branch; // token
  124. struct cstring_t maddr; // host
  125. struct cstring_t received; // IPv4address / IPv6address
  126. int ttl; // 0-255
  127. int rport; // 0-not found, -1-no-value, other-value
  128. struct sip_params_t params; // include branch/maddr/received/ttl/rport
  129. };
  130. DARRAY_DECLARE(sip_via);
  131. struct sip_cseq_t
  132. {
  133. uint32_t id;
  134. struct cstring_t method;
  135. };
  136. struct sip_substate_t
  137. {
  138. struct cstring_t state;
  139. // parameters
  140. struct cstring_t reason;
  141. uint32_t expires; // expires
  142. uint32_t retry; // retry-after
  143. struct sip_params_t params; // include reason/expires/retry
  144. };
  145. int sip_header_param(const char* s, const char* end, struct sip_param_t* param);
  146. int sip_header_params(char sep, const char* s, const char* end, struct sip_params_t* params);
  147. int sip_param_write(const struct sip_param_t* param, char* data, const char* end);
  148. int sip_params_write(const struct sip_params_t* params, char* data, const char* end, char sep);
  149. const struct sip_param_t* sip_params_find(const struct sip_params_t* params, const char* name, int bytes);
  150. const struct cstring_t* sip_params_find_string(const struct sip_params_t* params, const char* name, int bytes);
  151. int sip_params_find_int(const struct sip_params_t* params, const char* name, int bytes, int* value);
  152. int sip_params_find_int64(const struct sip_params_t* params, const char* name, int bytes, int64_t* value);
  153. int sip_params_find_double(const struct sip_params_t* params, const char* name, int bytes, double* value);
  154. int sip_params_add_or_update(struct sip_params_t* params, const char* name, int bytes, const struct cstring_t* value);
  155. /// @return 0-ok, other-error
  156. int sip_header_cseq(const char* s, const char* end, struct sip_cseq_t* cseq);
  157. /// @return write length, >0-ok, <0-error
  158. int sip_cseq_write(const struct sip_cseq_t* cseq, char* data, const char* end);
  159. int sip_header_uri(const char* s, const char* end, struct sip_uri_t* uri);
  160. int sip_uri_write(const struct sip_uri_t* uri, char* data, const char* end);
  161. int sip_uri_equal(const struct sip_uri_t* l, const struct sip_uri_t* r);
  162. int sip_uri_username(const struct sip_uri_t* uri, struct cstring_t* user);
  163. int sip_request_uri_write(const struct sip_uri_t* uri, char* data, const char* end);
  164. int sip_header_via(const char* s, const char* end, struct sip_via_t* via);
  165. int sip_header_vias(const char* s, const char* end, struct sip_vias_t* vias);
  166. int sip_via_write(const struct sip_via_t* via, char* data, const char* end);
  167. const struct cstring_t* sip_vias_top_branch(const struct sip_vias_t* vias);
  168. int sip_header_contact(const char* s, const char* end, struct sip_contact_t* contact);
  169. int sip_header_contacts(const char* s, const char* end, struct sip_contacts_t* contacts);
  170. int sip_contact_write(const struct sip_contact_t* contact, char* data, const char* end);
  171. int sip_contacts_match_any(const struct sip_contacts_t* contacts);
  172. char* cstring_clone(char* ptr, const char* end, struct cstring_t* clone, const char* s, size_t n);
  173. char* sip_uri_clone(char* ptr, const char* end, struct sip_uri_t* clone, const struct sip_uri_t* uri);
  174. char* sip_via_clone(char* ptr, const char* end, struct sip_via_t* clone, const struct sip_via_t* via);
  175. char* sip_contact_clone(char* ptr, const char* end, struct sip_contact_t* clone, const struct sip_contact_t* contact);
  176. void sip_uri_free(struct sip_uri_t* uri);
  177. void sip_via_free(struct sip_via_t* via);
  178. void sip_contact_free(struct sip_contact_t* contact);
  179. void sip_substate_free(struct sip_substate_t* substate);
  180. /// @return 0-ok, other-error
  181. int sip_header_substate(const char* s, const char* end, struct sip_substate_t* substate);
  182. /// @return write length, >0-ok, <0-error
  183. int sip_substate_write(const struct sip_substate_t* substate, char* data, const char* end);
  184. #if defined(__cplusplus)
  185. }
  186. #endif
  187. #endif /* !_sip_header_h_ */