error.c 5.6 KB

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