reply.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Presence Agent, reply building
  3. *
  4. * $Id$
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #include "../../dprint.h"
  30. #include "../../data_lump_rpl.h"
  31. #include "reply.h"
  32. #include "paerrno.h"
  33. #include "pa_mod.h"
  34. #include <presence/utils.h>
  35. #define MSG_200 "OK"
  36. #define MSG_400 "Bad Request"
  37. #define MSG_500 "Server Internal Error"
  38. #define MSG_403 "Forbidden"
  39. #define EI_PA_OK "No problem"
  40. #define EI_PA_PARSE_ERR "Error while parsing headers"
  41. #define EI_PA_CONTACT_MISS "Contact header field missing"
  42. #define EI_PA_FROM_MISS "From header field missing"
  43. #define EI_PA_EVENT_MISS "Event header field missing"
  44. #define EI_PA_EVENT_PARSE "Error while parsing Event header field"
  45. #define EI_PA_EXPIRES_PARSE "Error while parsing Expires header field"
  46. #define EI_PA_EVENT_UNSUPP "Unsupported event package"
  47. #define EI_PA_NO_MEMORY "No memory left on the server"
  48. #define EI_PA_TIMER_ERROR "Error while running timer"
  49. #define EI_PA_EXTRACT_USER "Cannot extract username from URI"
  50. #define EI_PA_CONT_PARSE "Error while parsing Contact"
  51. #define EI_PA_CONT_STAR "Start not allowed in Contact"
  52. #define EI_PA_FROM_ERROR "Error while parsing From"
  53. #define EI_PA_SMALL_BUFFER "Buffer too small on the server"
  54. #define EI_PA_UNSUPP_DOC "Unsupported document format"
  55. #define EI_PA_INTERNAL_ERROR "Internal Server Error"
  56. #define EI_PA_SUBSCRIPTION_REJECTED "Subscription rejected"
  57. str error_info[] = {
  58. {EI_PA_OK, sizeof(EI_PA_OK) - 1 },
  59. {EI_PA_PARSE_ERR, sizeof(EI_PA_PARSE_ERR) - 1 },
  60. {EI_PA_CONTACT_MISS, sizeof(EI_PA_CONTACT_MISS) - 1 },
  61. {EI_PA_FROM_MISS, sizeof(EI_PA_FROM_MISS) - 1 },
  62. {EI_PA_EVENT_MISS, sizeof(EI_PA_EVENT_MISS) - 1 },
  63. {EI_PA_EVENT_PARSE, sizeof(EI_PA_EVENT_PARSE) - 1 },
  64. {EI_PA_EXPIRES_PARSE, sizeof(EI_PA_EXPIRES_PARSE) - 1 },
  65. {EI_PA_EVENT_UNSUPP, sizeof(EI_PA_EVENT_UNSUPP) - 1 },
  66. {EI_PA_NO_MEMORY, sizeof(EI_PA_NO_MEMORY) - 1 },
  67. {EI_PA_TIMER_ERROR, sizeof(EI_PA_TIMER_ERROR) - 1 },
  68. {EI_PA_EXTRACT_USER, sizeof(EI_PA_EXTRACT_USER) - 1 },
  69. {EI_PA_CONT_PARSE, sizeof(EI_PA_CONT_PARSE) - 1 },
  70. {EI_PA_CONT_STAR, sizeof(EI_PA_CONT_STAR) - 1 },
  71. {EI_PA_FROM_ERROR, sizeof(EI_PA_FROM_ERROR) - 1 },
  72. {EI_PA_SMALL_BUFFER, sizeof(EI_PA_SMALL_BUFFER) - 1 },
  73. {EI_PA_UNSUPP_DOC, sizeof(EI_PA_UNSUPP_DOC) - 1 },
  74. {EI_PA_INTERNAL_ERROR, sizeof(EI_PA_INTERNAL_ERROR) - 1},
  75. {EI_PA_SUBSCRIPTION_REJECTED, sizeof(EI_PA_SUBSCRIPTION_REJECTED) - 1}
  76. };
  77. int codes[] = {
  78. 200, /* EI_PA_OK */
  79. 400, /* EI_PA_PARSE_ERR */
  80. 400, /* EI_PA_CONTACT_MISS */
  81. 400, /* EI_PA_FROM_MISS */
  82. 400, /* EI_PA_EVENT_MISS */
  83. 400, /* EI_PA_EVENT_PARSE */
  84. 400, /* EI_PA_EXPIRES_PARSE */
  85. 500, /* EI_PA_EVENT_UNSUPP */
  86. 500, /* EI_PA_NO_MEMORY */
  87. 500, /* EI_PA_TIMER_ERROR */
  88. 400, /* EI_PA_EXTRACT_USER */
  89. 400, /* EI_PA_CONT_PARSE */
  90. 400, /* EI_PA_CONT_STAR */
  91. 400, /* EI_PA_FROM_ERROR */
  92. 500, /* EI_PA_SMALL_BUFFER */
  93. 500, /* EI_PA_UNSUPP_DOC */
  94. 500, /* EI_PA_INTERNAL_ERROR */
  95. 403 /* EI_PA_SUBSCRIPTION_REJECTED */
  96. };
  97. /*
  98. * Send a reply
  99. */
  100. int send_reply(struct sip_msg* _m)
  101. {
  102. int code = 200;
  103. char* msg = MSG_200; /* makes gcc shut up */
  104. /* code = codes[paerrno]; */
  105. switch (paerrno) {
  106. case PA_OK: msg = MSG_200; code = 200; break;
  107. case PA_PARSE_ERR: msg = MSG_400; code = 400; break;
  108. case PA_FROM_MISS: msg = MSG_400; code = 400; break;
  109. case PA_EVENT_MISS:
  110. msg = "Unsupported event package";
  111. code = 489;
  112. break;
  113. case PA_EVENT_PARSE: msg = MSG_400; code = 400; break;
  114. case PA_EXPIRES_PARSE: msg = MSG_400; code = 400; break;
  115. case PA_EVENT_UNSUPP:
  116. msg = "Unsupported event package";
  117. code = 489;
  118. break;
  119. case PA_WRONG_ACCEPTS:
  120. msg = "Unsupported document format for given package";
  121. code = 415;
  122. break;
  123. case PA_NO_MEMORY: msg = MSG_500; code = 500; break;
  124. case PA_TIMER_ERROR: msg = MSG_500; code = 500; break;
  125. case PA_EXTRACT_USER: msg = MSG_400; code = 400; break;
  126. case PA_FROM_ERR: msg = MSG_400; code = 400; break;
  127. case PA_TO_ERR: msg = MSG_400; code = 400; break;
  128. case PA_SMALL_BUFFER: msg = MSG_500; code = 500; break;
  129. case PA_UNSUPP_DOC:
  130. msg = "Unsupported document format";
  131. code = 415;
  132. break;
  133. case PA_ACCEPT_PARSE: msg = MSG_400; code = 400; break;
  134. case PA_URI_PARSE: msg = MSG_400; code = 400; break;
  135. case PA_DIALOG_ERR: msg = MSG_500; code = 500; break;
  136. case PA_INTERNAL_ERROR: msg = MSG_500; code = 500; break;
  137. case PA_SUBSCRIPTION_REJECTED: msg = MSG_403; code = 403; break;
  138. case PA_NO_MATCHING_TUPLE: msg = "Conditional Request Failed";
  139. code = 412;
  140. break;
  141. case PA_OK_WAITING_FOR_AUTH:
  142. msg = "Accepted";
  143. code = 202;
  144. break;
  145. /* OK but waiting for auth -> should return 202 */
  146. case PA_SUBSCRIPTION_NOT_EXISTS:
  147. msg = "Subscription does not exist";
  148. code = 481;
  149. break;
  150. /* OK but waiting for auth -> should return 202 */
  151. }
  152. if ((code >= 200) && (code < 300)) {
  153. /* add Contact header field into response */
  154. str s;
  155. if (extract_server_contact(_m, &s, 0) == 0) {
  156. if (s.len > 0) {
  157. if (!add_lump_rpl(_m, s.s, s.len, LUMP_RPL_HDR)) {
  158. ERR("Can't add Contact header into the response\n");
  159. if (s.s) mem_free(s.s);
  160. return -1;
  161. }
  162. }
  163. if (s.s) mem_free(s.s);
  164. }
  165. }
  166. if (tmb.t_reply(_m, code, msg) < 0) {
  167. ERR("Error while sending %d %s\n", code, msg);
  168. return -1;
  169. } else return 0;
  170. }