error.c 6.0 KB

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