error.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. * History:
  29. * --------
  30. * 2003-04-04 phrase length corrected not to include trailer 0 (jiri)
  31. */
  32. #include <stdio.h>
  33. #include "error.h"
  34. #include "str.h"
  35. #include "parser/msg_parser.h"
  36. #include "mem/mem.h"
  37. /* current function's error; */
  38. int ser_error=-1;
  39. /* previous error */
  40. int prev_ser_error=-1;
  41. int err2reason_phrase(
  42. int ser_error, /* current internal ser error */
  43. int *sip_error, /* the sip error code to which ser
  44. ser error will be turned */
  45. char *phrase, /* resulting error text */
  46. int etl, /* error text buffer length */
  47. char *signature ) /* extra text to be appended */
  48. {
  49. char *error_txt;
  50. switch( ser_error ) {
  51. case E_SEND:
  52. error_txt="Unfortunately error on sending to next hop occurred";
  53. *sip_error=-ser_error;
  54. break;
  55. case E_BAD_ADDRESS:
  56. error_txt="Unresolvable destination";
  57. *sip_error=-ser_error;
  58. break;
  59. case E_BAD_REQ:
  60. error_txt="Bad Request";
  61. *sip_error=-ser_error;
  62. break;
  63. case E_BAD_URI:
  64. error_txt="Regretfully, we were not able to process the URI";
  65. *sip_error=-ser_error;
  66. break;
  67. case E_BAD_TUPEL:
  68. error_txt="Transaction tuple incomplete";
  69. *sip_error=-E_BAD_REQ;
  70. break;
  71. case E_BAD_TO:
  72. error_txt="Bad To";
  73. *sip_error=-E_BAD_REQ;
  74. break;
  75. case E_EXEC:
  76. error_txt="Error in external logic";
  77. *sip_error=-E_BAD_SERVER;
  78. break;
  79. case E_TOO_MANY_BRANCHES:
  80. error_txt="Forking capacity exceeded";
  81. *sip_error=-E_BAD_SERVER;
  82. break;
  83. case E_Q_INV_CHAR:
  84. error_txt="Invalid character in q parameter";
  85. *sip_error=-E_BAD_REQ;
  86. break;
  87. case E_Q_EMPTY:
  88. error_txt="Empty q parameter";
  89. *sip_error=-E_BAD_REQ;
  90. break;;
  91. case E_Q_TOO_BIG:
  92. error_txt="q parameter too big";
  93. *sip_error=-E_BAD_REQ;
  94. break;
  95. case E_Q_DEC_MISSING:
  96. error_txt="Decimal part missing in q";
  97. *sip_error=-E_BAD_REQ;
  98. break;
  99. case E_CANCELED:
  100. error_txt="transaction canceled";
  101. *sip_error=-ser_error;
  102. break;
  103. case E_OUT_OF_MEM:
  104. /* dont disclose lack of mem in release mode */
  105. #ifdef EXTRA_DEBUG
  106. error_txt="Excuse me I ran out of memory";
  107. *sip_error=500;
  108. break;
  109. #endif
  110. case E_OK:
  111. error_txt="No error";
  112. *sip_error=500;
  113. break;
  114. default:
  115. error_txt="I'm terribly sorry, server error occurred";
  116. *sip_error=500;
  117. break;
  118. }
  119. return snprintf( phrase, etl, "%s (%d/%s)", error_txt,
  120. -ser_error, signature );
  121. }
  122. char *error_text( int code )
  123. {
  124. switch(code) {
  125. case 100: return "Trying";
  126. case 180: return "Ringing";
  127. case 181: return "Call is Being Forwarded";
  128. case 182: return "Queued";
  129. case 183: return "Session Progress";
  130. case 200: return "OK";
  131. case 300: return "Multiple Choices";
  132. case 301: return "Moved Permanently";
  133. case 302: return "Moved Temporarily";
  134. case 305: return "Use Proxy";
  135. case 380: return "Alternative Service";
  136. case 400: return "Bad Request";
  137. case 401: return "Unauthorized";
  138. case 402: return "Payment Required";
  139. case 403: return "Forbidden";
  140. case 404: return "Not Found";
  141. case 405: return "Method not Allowed";
  142. case 406: return "Not Acceptable";
  143. case 407: return "Proxy authentication Required";
  144. case 408: return "Request Timeout";
  145. case 410: return "Gone";
  146. case 413: return "Request Entity Too Large";
  147. case 414: return "Request-URI Too Long";
  148. case 415: return "Unsupported Media Type";
  149. case 416: return "Unsupported URI Scheme";
  150. case 417: return "Bad Extension";
  151. case 421: return "Extension Required";
  152. case 423: return "Interval Too Brief";
  153. case 480: return "Temporarily Unavailable";
  154. case 481: return "Call/Transaction Does not Exist";
  155. case 482: return "Loop Detected";
  156. case 483: return "Too Many Hops";
  157. case 484: return "Address Incomplete";
  158. case 485: return "Ambiguous";
  159. case 486: return "Busy Here";
  160. case 487: return "Request Terminated";
  161. case 488: return "Not Acceptable Here";
  162. case 491: return "Request Pending";
  163. case 500: return "Server Internal Error";
  164. case 501: return "Not Implemented";
  165. case 502: return "Bad Gateway";
  166. case 503: return "Service Unavailable";
  167. case 504: return "Server Time-out";
  168. case 505: return "Version not Supported";
  169. case 513: return "Message Too Large";
  170. case 600: return "Busy Everywhere";
  171. case 603: return "Decline";
  172. case 604: return "Does not Exist Anywhere";
  173. case 606: return "Not Acceptable";
  174. }
  175. if (code>=600) return "Global Failure";
  176. else if (code>=500) return "Server Failure";
  177. else if (code>=400) return "Request Failure";
  178. else if (code>=300) return "Redirection";
  179. else if (code>=200) return "Successful";
  180. else if (code>=100) return "Provisional";
  181. else return "Unspecified";
  182. }
  183. void get_reply_status( str *status, struct sip_msg *reply, int code )
  184. {
  185. str phrase;
  186. status->s=0;
  187. if (reply==0) {
  188. LOG(L_CRIT, "BUG: get_reply_status called with 0 msg\n");
  189. return;
  190. }
  191. if (reply==FAKED_REPLY) {
  192. phrase.s=error_text(code);
  193. phrase.len=strlen(phrase.s);
  194. } else {
  195. phrase=reply->first_line.u.reply.reason;
  196. }
  197. status->len=phrase.len+3/*code*/+1/*space*/;
  198. status->s=pkg_malloc(status->len+1/*ZT */);
  199. if (!status->s) {
  200. LOG(L_ERR, "ERROR: get_reply_status: no mem\n");
  201. return;
  202. }
  203. status->s[3]=' ';
  204. status->s[2]='0'+code % 10; code=code/10;
  205. status->s[1]='0'+code% 10; code=code/10;
  206. status->s[0]='0'+code % 10;
  207. memcpy(&status->s[4], phrase.s, phrase.len);
  208. status->s[status->len]=0;
  209. }