error.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 itnernal 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 occured";
  53. *sip_error=-ser_error;
  54. break;
  55. case E_BAD_ADDRESS:
  56. error_txt="Unresolveable 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="Regretfuly, we were not able to process the URI";
  65. *sip_error=-ser_error;
  66. break;
  67. case E_BAD_TUPEL:
  68. error_txt="Transaction tupel 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_OUT_OF_MEM:
  84. /* dont disclose lack of mem in release mode */
  85. #ifdef DEBUG
  86. error_txt="Excuse me I ran out of memory";
  87. *sip_error=500;
  88. break;
  89. #endif
  90. default:
  91. error_txt="I'm terribly sorry, server error occured";
  92. *sip_error=500;
  93. break;
  94. }
  95. return snprintf( phrase, etl, "%s (%d/%s)", error_txt,
  96. -ser_error, signature );
  97. }
  98. char *error_text( int code )
  99. {
  100. switch(code) {
  101. case 100: return "Trying";
  102. case 180: return "Ringing";
  103. case 181: return "Call is Being Forwarded";
  104. case 182: return "Queued";
  105. case 183: return "Session Progress";
  106. case 200: return "OK";
  107. case 300: return "Multiple Choices";
  108. case 301: return "Moved Permanently";
  109. case 302: return "Moved Temporarily";
  110. case 305: return "Use Proxy";
  111. case 380: return "Alternative Service";
  112. case 400: return "Bad Request";
  113. case 401: return "Unauthorized";
  114. case 402: return "Payement Required";
  115. case 403: return "Forbidden";
  116. case 404: return "Not Found";
  117. case 405: return "Method not Allowed";
  118. case 406: return "Not Acceptable";
  119. case 407: return "Proxy authentication Required";
  120. case 408: return "Request Timeout";
  121. case 410: return "Gone";
  122. case 413: return "Request Entity Too Large";
  123. case 414: return "Request-URI Too Long";
  124. case 415: return "Unsupported Media Type";
  125. case 416: return "Unsupported URI Scheme";
  126. case 417: return "Bad Extension";
  127. case 421: return "Extension Required";
  128. case 423: return "Interval Too Brief";
  129. case 480: return "Temporarily Unavailable";
  130. case 481: return "Call/Transaction Does not Exist";
  131. case 482: return "Loop Detected";
  132. case 483: return "Too Many Hops";
  133. case 484: return "Address Incomplete";
  134. case 485: return "Ambigous";
  135. case 486: return "Busy Here";
  136. case 487: return "Request Terminated";
  137. case 488: return "Not Acceptable Here";
  138. case 491: return "Request Pending";
  139. case 500: return "Server Internal Error";
  140. case 501: return "Not Implemented";
  141. case 502: return "Bad Gateway";
  142. case 503: return "Service Unavailable";
  143. case 504: return "Server Time-out";
  144. case 505: return "Version not Supported";
  145. case 513: return "Message Too Large";
  146. case 600: return "Busy Everywhere";
  147. case 603: return "Decline";
  148. case 604: return "Does not Exist Anywhere";
  149. case 606: return "Not Acceptable";
  150. }
  151. if (code>=600) return "Global Failure";
  152. else if (code>=500) return "Server Failure";
  153. else if (code>=400) return "Request Failure";
  154. else if (code>=300) return "Redirection";
  155. else if (code>=200) return "Successful";
  156. else if (code>=100) return "Provisional";
  157. else return "Unspecified";
  158. }
  159. void get_reply_status( str *status, struct sip_msg *reply, int code )
  160. {
  161. str phrase;
  162. status->s=0;
  163. if (reply==0) {
  164. LOG(L_CRIT, "BUG: get_reply_status called with 0 msg\n");
  165. return;
  166. }
  167. if (reply==FAKED_REPLY) {
  168. phrase.s=error_text(code);
  169. phrase.len=strlen(phrase.s);
  170. } else {
  171. phrase=reply->first_line.u.reply.reason;
  172. }
  173. status->len=phrase.len+3/*code*/+1/*space*/;
  174. status->s=pkg_malloc(status->len+1/*ZT */);
  175. if (!status->s) {
  176. LOG(L_ERR, "ERROR: get_reply_status: no mem\n");
  177. return;
  178. }
  179. status->s[3]=' ';
  180. status->s[2]='0'+code % 10; code=code/10;
  181. status->s[1]='0'+code% 10; code=code/10;
  182. status->s[0]='0'+code % 10;
  183. memcpy(&status->s[4], phrase.s, phrase.len);
  184. status->s[status->len]=0;
  185. }