error.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * Copyright (C) 2001-2003 FhG Fokus
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. *
  28. * History:
  29. * --------
  30. * 2003-04-04 phrase length corrected not to include trailer 0 (jiri)
  31. */
  32. /*!
  33. * \file
  34. * \brief SIP-router core ::
  35. * \ingroup core
  36. * Module: \ref core
  37. */
  38. #include <stdio.h>
  39. #include "error.h"
  40. #include "str.h"
  41. #include "parser/msg_parser.h"
  42. #include "mem/mem.h"
  43. /* current function's error; */
  44. int ser_error=-1;
  45. /* previous error */
  46. int prev_ser_error=-1;
  47. int err2reason_phrase(
  48. int ser_error, /* current internal ser error */
  49. int *sip_error, /* the sip error code to which ser
  50. ser error will be turned */
  51. char *phrase, /* resulting error text */
  52. int etl, /* error text buffer length */
  53. char *signature ) /* extra text to be appended */
  54. {
  55. char *error_txt;
  56. switch( ser_error ) {
  57. case E_SEND:
  58. error_txt="Unfortunately error on sending to next hop occurred";
  59. *sip_error=-ser_error;
  60. break;
  61. case E_BAD_ADDRESS:
  62. error_txt="Unresolvable destination";
  63. *sip_error=-ser_error;
  64. break;
  65. case E_BAD_REQ:
  66. error_txt="Bad Request";
  67. *sip_error=-ser_error;
  68. break;
  69. case E_BAD_URI:
  70. error_txt="Regretfully, we were not able to process the URI";
  71. *sip_error=-ser_error;
  72. break;
  73. case E_BAD_TUPEL:
  74. error_txt="Transaction tuple incomplete";
  75. *sip_error=-E_BAD_REQ;
  76. break;
  77. case E_BAD_TO:
  78. error_txt="Bad To";
  79. *sip_error=-E_BAD_REQ;
  80. break;
  81. case E_EXEC:
  82. error_txt="Error in external logic";
  83. *sip_error=-E_BAD_SERVER;
  84. break;
  85. case E_TOO_MANY_BRANCHES:
  86. error_txt="Forking capacity exceeded";
  87. *sip_error=-E_BAD_SERVER;
  88. break;
  89. case E_Q_INV_CHAR:
  90. error_txt="Invalid character in q parameter";
  91. *sip_error=-E_BAD_REQ;
  92. break;
  93. case E_Q_EMPTY:
  94. error_txt="Empty q parameter";
  95. *sip_error=-E_BAD_REQ;
  96. break;;
  97. case E_Q_TOO_BIG:
  98. error_txt="q parameter too big";
  99. *sip_error=-E_BAD_REQ;
  100. break;
  101. case E_Q_DEC_MISSING:
  102. error_txt="Decimal part missing in q";
  103. *sip_error=-E_BAD_REQ;
  104. break;
  105. case E_CANCELED:
  106. error_txt="transaction canceled";
  107. *sip_error=-ser_error;
  108. break;
  109. case E_OUT_OF_MEM:
  110. /* dont disclose lack of mem in release mode */
  111. #ifdef EXTRA_DEBUG
  112. error_txt="Excuse me I ran out of memory";
  113. *sip_error=500;
  114. break;
  115. #endif
  116. case E_OK:
  117. error_txt="No error";
  118. *sip_error=500;
  119. break;
  120. default:
  121. error_txt="I'm terribly sorry, server error occurred";
  122. *sip_error=500;
  123. break;
  124. }
  125. return snprintf( phrase, etl, "%s (%d/%s)", error_txt,
  126. -ser_error, signature );
  127. }
  128. char *error_text( int code )
  129. {
  130. switch(code) {
  131. case 100: return "Trying";
  132. case 180: return "Ringing";
  133. case 181: return "Call is Being Forwarded";
  134. case 182: return "Queued";
  135. case 183: return "Session Progress";
  136. case 200: return "OK";
  137. case 300: return "Multiple Choices";
  138. case 301: return "Moved Permanently";
  139. case 302: return "Moved Temporarily";
  140. case 305: return "Use Proxy";
  141. case 380: return "Alternative Service";
  142. case 400: return "Bad Request";
  143. case 401: return "Unauthorized";
  144. case 402: return "Payment Required";
  145. case 403: return "Forbidden";
  146. case 404: return "Not Found";
  147. case 405: return "Method not Allowed";
  148. case 406: return "Not Acceptable";
  149. case 407: return "Proxy authentication Required";
  150. case 408: return "Request Timeout";
  151. case 410: return "Gone";
  152. case 413: return "Request Entity Too Large";
  153. case 414: return "Request-URI Too Long";
  154. case 415: return "Unsupported Media Type";
  155. case 416: return "Unsupported URI Scheme";
  156. case 417: return "Bad Extension";
  157. case 421: return "Extension Required";
  158. case 423: return "Interval Too Brief";
  159. case 480: return "Temporarily Unavailable";
  160. case 481: return "Call/Transaction Does not Exist";
  161. case 482: return "Loop Detected";
  162. case 483: return "Too Many Hops";
  163. case 484: return "Address Incomplete";
  164. case 485: return "Ambiguous";
  165. case 486: return "Busy Here";
  166. case 487: return "Request Terminated";
  167. case 488: return "Not Acceptable Here";
  168. case 491: return "Request Pending";
  169. case 500: return "Server Internal Error";
  170. case 501: return "Not Implemented";
  171. case 502: return "Bad Gateway";
  172. case 503: return "Service Unavailable";
  173. case 504: return "Server Time-out";
  174. case 505: return "Version not Supported";
  175. case 513: return "Message Too Large";
  176. case 600: return "Busy Everywhere";
  177. case 603: return "Decline";
  178. case 604: return "Does not Exist Anywhere";
  179. case 606: return "Not Acceptable";
  180. }
  181. if (code>=600) return "Global Failure";
  182. else if (code>=500) return "Server Failure";
  183. else if (code>=400) return "Request Failure";
  184. else if (code>=300) return "Redirection";
  185. else if (code>=200) return "Successful";
  186. else if (code>=100) return "Provisional";
  187. else return "Unspecified";
  188. }
  189. void get_reply_status( str *status, struct sip_msg *reply, int code )
  190. {
  191. str phrase;
  192. status->s=0;
  193. if (reply==0) {
  194. LM_CRIT("0 msg\n");
  195. return;
  196. }
  197. if (reply==FAKED_REPLY) {
  198. phrase.s=error_text(code);
  199. phrase.len=strlen(phrase.s);
  200. } else {
  201. phrase=reply->first_line.u.reply.reason;
  202. }
  203. status->len=phrase.len+3/*code*/+1/*space*/;
  204. status->s=pkg_malloc(status->len+1/*ZT */);
  205. if (!status->s) {
  206. LM_ERR("no mem\n");
  207. return;
  208. }
  209. status->s[3]=' ';
  210. status->s[2]='0'+code % 10; code=code/10;
  211. status->s[1]='0'+code% 10; code=code/10;
  212. status->s[0]='0'+code % 10;
  213. memcpy(&status->s[4], phrase.s, phrase.len);
  214. status->s[status->len]=0;
  215. }