tmsrp_message.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (C) 2009 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou [at) doubango (DOT) org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. /**@file tmsrp_message.h
  23. * @brief MSRP message.
  24. *
  25. * @author Mamadou Diop <diopmamadou [at) doubango (DOT) org>
  26. *
  27. */
  28. #ifndef TINYMSRP_MESSAGE_H
  29. #define TINYMSRP_MESSAGE_H
  30. #include "tinymsrp_config.h"
  31. #include "tinymsrp/headers/tmsrp_header.h"
  32. #include "tinymsrp/headers/tmsrp_header_Byte-Range.h"
  33. #include "tinymsrp/headers/tmsrp_header_Content-Type.h"
  34. #include "tinymsrp/headers/tmsrp_header_Failure-Report.h"
  35. #include "tinymsrp/headers/tmsrp_header_From-Path.h"
  36. #include "tinymsrp/headers/tmsrp_header_Message-ID.h"
  37. #include "tinymsrp/headers/tmsrp_header_Status.h"
  38. #include "tinymsrp/headers/tmsrp_header_Success-Report.h"
  39. #include "tinymsrp/headers/tmsrp_header_To-Path.h"
  40. TMSRP_BEGIN_DECLS
  41. #define TMSRP_MESSAGE_IS_REQUEST(self) ((self) ? (self)->type == tmsrp_request : tsk_false)
  42. #define TMSRP_MESSAGE_IS_RESPONSE(self) ((self) ? (self)->type == tmsrp_response : tsk_false)
  43. #define TMSRP_MESSAGE(self) ((tmsrp_message_t*)(self))
  44. #define TMSRP_MESSAGE_AS_RESPONSE(self) ((tmsrp_response_t*)(self))
  45. #define TMSRP_MESSAGE_AS_REQUEST(self) ((tmsrp_request_t*)(self))
  46. #define TMSRP_RESPONSE_CODE(self) (TMSRP_MESSAGE_IS_RESPONSE((self)) ? (self)->line.response.status : 0)
  47. #define TMSRP_RESPONSE_PHRASE(self) (TMSRP_MESSAGE_IS_RESPONSE((self)) ? (self)->line.response.comment : tsk_null)
  48. #define TMSRP_REQUEST_METHOD(self) (TMSRP_MESSAGE_IS_REQUEST((self)) ? (self)->line.request.method : tsk_null)
  49. #define TMSRP_REQUEST_IS_SEND(self) (TMSRP_MESSAGE_IS_REQUEST(self) && (self)->line.request.type == tmsrp_SEND)
  50. #define TMSRP_REQUEST_IS_REPORT(self) (TMSRP_MESSAGE_IS_REQUEST(self) && (self)->line.request.type == tmsrp_REPORT)
  51. #define TMSRP_REQUEST_IS_AUTH(self) (TMSRP_MESSAGE_IS_REQUEST(self) && (self)->line.request.type == tmsrp_AUTH)
  52. #define TMSRP_MESSAGE_HAS_CONTENT(message) ((message) && (message)->Content && (message)->Content->data)
  53. #define TMSRP_MESSAGE_CONTENT(message) (TMSRP_MESSAGE_HAS_CONTENT(message) ? (message)->Content->data : 0)
  54. #define TMSRP_RESPONSE_IS(self, code) (TMSRP_RESPONSE_CODE((self)) == code)
  55. #define TMSRP_RESPONSE_IS_NXX(self, N) (TMSRP_MESSAGE_IS_RESPONSE((self)) && N##00<= TMSRP_RESPONSE_CODE((self)) && TMSRP_RESPONSE_CODE((self)) <= N##99)
  56. #define TMSRP_RESPONSE_IS_1XX(self) TMSRP_RESPONSE_IS_NXX(self, 1)
  57. #define TMSRP_RESPONSE_IS_2XX(self) TMSRP_RESPONSE_IS_NXX(self, 2)
  58. #define TMSRP_RESPONSE_IS_3XX(self) TMSRP_RESPONSE_IS_NXX(self, 3)
  59. #define TMSRP_RESPONSE_IS_4XX(self) TMSRP_RESPONSE_IS_NXX(self, 4)
  60. #define TMSRP_RESPONSE_IS_5XX(self) TMSRP_RESPONSE_IS_NXX(self, 5)
  61. #define TMSRP_RESPONSE_IS_6XX(self) TMSRP_RESPONSE_IS_NXX(self, 6)
  62. #define TMSRP_RESPONSE_IS_23456(self) (TMSRP_MESSAGE_IS_RESPONSE((self)) && 200<= TMSRP_RESPONSE_CODE((self)) && TMSRP_RESPONSE_CODE((self)) <= 699)
  63. /** Defines the message type (Request or Response).
  64. **/
  65. typedef enum tmsrp_message_type_e {
  66. tmsrp_unknown,
  67. tmsrp_request,
  68. tmsrp_response
  69. }
  70. tmsrp_message_type_t;
  71. typedef enum tmsrp_request_type_e {
  72. tmsrp_NONE = 0,
  73. tmsrp_SEND,
  74. tmsrp_REPORT,
  75. tmsrp_AUTH
  76. //...
  77. }
  78. tmsrp_request_type_t;
  79. typedef struct tmsrp_message_s {
  80. TSK_DECLARE_OBJECT;
  81. tmsrp_message_type_t type;
  82. char* tid;
  83. union {
  84. struct {
  85. char* method;
  86. tmsrp_request_type_t type;
  87. } request;
  88. struct {
  89. short status;
  90. char* comment;
  91. } response;
  92. } line;
  93. // Very common headers
  94. tmsrp_header_To_Path_t* To;
  95. tmsrp_header_From_Path_t* From;
  96. tmsrp_header_Message_ID_t* MessageID;
  97. tmsrp_header_Byte_Range_t* ByteRange;
  98. tmsrp_header_Failure_Report_t* FailureReport;
  99. tmsrp_header_Success_Report_t* SuccessReport;
  100. tmsrp_header_Status_t* Status;
  101. //! List of @ref tmsrp_header_t elements.
  102. tmsrp_headers_L_t* headers;
  103. // Content
  104. tmsrp_header_Content_Type_t* ContentType;
  105. tsk_buffer_t *Content;
  106. // End line
  107. struct {
  108. char* tid;
  109. char cflag;
  110. } end_line;
  111. }
  112. tmsrp_message_t;
  113. typedef tmsrp_message_t tmsrp_request_t; /**< MSRP request message. */
  114. typedef tmsrp_message_t tmsrp_response_t; /**< MSRP response message. */
  115. TINYMSRP_API tmsrp_message_t* tmsrp_message_create(tmsrp_message_type_t type, const char* tid, const char* method, short status, const char* comment);
  116. TINYMSRP_API tmsrp_message_t* tmsrp_request_create(const char* tid, const char* method);
  117. TINYMSRP_API tmsrp_message_t* tmsrp_response_create(const char* tid, short status, const char* comment);
  118. TINYMSRP_API tmsrp_message_t* tmsrp_message_create_null();
  119. TINYMSRP_API int tmsrp_message_add_header(tmsrp_message_t *self, const tmsrp_header_t *hdr);
  120. TINYMSRP_API int tmsrp_message_add_headers(tmsrp_message_t *self, ...);
  121. #if 0
  122. static void TMSRP_MESSAGE_ADD_HEADER(tmsrp_message_t *self, ...)
  123. {
  124. va_list ap;
  125. tmsrp_header_t *header;
  126. const tsk_object_def_t *objdef;
  127. va_start(ap, self);
  128. objdef = va_arg(ap, const tsk_object_def_t*);
  129. header = tsk_object_new_2(objdef, &ap);
  130. va_end(ap);
  131. tmsrp_message_add_header(self, header);
  132. tsk_object_unref(header);
  133. }
  134. #else
  135. #define TMSRP_MESSAGE_ADD_HEADER(self, objdef, ...) \
  136. { \
  137. tmsrp_header_t *header = (tmsrp_header_t *)tsk_object_new(objdef, ##__VA_ARGS__); \
  138. tmsrp_message_add_header(self, header); \
  139. tsk_object_unref(header); \
  140. }
  141. #endif
  142. TINYMSRP_API tmsrp_request_type_t tmsrp_request_get_type(const char* method);
  143. TINYMSRP_API const tmsrp_header_t *tmsrp_message_get_headerAt(const tmsrp_message_t *self, tmsrp_header_type_t type, tsk_size_t index);
  144. TINYMSRP_API const tmsrp_header_t *tmsrp_message_get_header(const tmsrp_message_t *self, tmsrp_header_type_t type);
  145. TINYMSRP_API const tmsrp_header_t *tmsrp_message_get_headerByName(const tmsrp_message_t *self, const char* name);
  146. TINYMSRP_API int tmsrp_message_add_content(tmsrp_message_t *self, const char* content_type, const void* content, tsk_size_t size);
  147. TINYMSRP_API int tmsrp_message_serialize(const tmsrp_message_t *self, tsk_buffer_t *output);
  148. TINYMSRP_API char* tmsrp_message_tostring(const tmsrp_message_t *self);
  149. TINYMSRP_GEXTERN const tsk_object_def_t *tmsrp_message_def_t;
  150. TMSRP_END_DECLS
  151. #endif /* TINYMSRP_MESSAGE_H */