proxy.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /*
  29. * History:
  30. * -------
  31. * 2003-02-13 added proto to struct proxy_l & to *_proxy functions (andrei)
  32. */
  33. #ifndef proxy_h
  34. #define proxy_h
  35. #include <netdb.h>
  36. #include "ip_addr.h"
  37. #include "str.h"
  38. #include "config.h"
  39. struct proxy_l{
  40. struct proxy_l* next;
  41. str name; /* original name */
  42. struct hostent host; /* addresses */
  43. unsigned short port;
  44. unsigned short reserved; /*align*/
  45. int proto;
  46. /* socket ? */
  47. int addr_idx; /* crt. addr. idx. */
  48. int ok; /* 0 on error */
  49. /*statistics*/
  50. int tx;
  51. int tx_bytes;
  52. int errors;
  53. };
  54. extern struct proxy_l* proxies;
  55. struct proxy_l* add_proxy(str* name, unsigned short port, int proto);
  56. struct proxy_l* mk_proxy(str* name, unsigned short port, int proto);
  57. struct proxy_l* mk_shm_proxy(str* name, unsigned short port, int proto);
  58. struct proxy_l* mk_proxy_from_ip(struct ip_addr* ip, unsigned short port,
  59. int proto);
  60. void free_proxy(struct proxy_l* p);
  61. void free_shm_proxy(struct proxy_l* p);
  62. /* returns 0 on success, -1 on error (unknown af/bug) */
  63. inline static int proxy2su(union sockaddr_union* su, struct proxy_l* p)
  64. {
  65. /* if error try next ip address if possible */
  66. if (p->ok==0){
  67. if (p->host.h_addr_list[p->addr_idx+1])
  68. p->addr_idx++;
  69. else p->addr_idx=0;
  70. p->ok=1;
  71. }
  72. return hostent2su(su, &p->host, p->addr_idx,
  73. (p->port)?p->port:((p->proto==PROTO_TLS)?SIPS_PORT:SIP_PORT) );
  74. }
  75. /* mark as proxy either as ok (err>=0) or as bad (err<0) */
  76. inline static void proxy_mark(struct proxy_l* p, int err)
  77. {
  78. if (err<0){
  79. p->errors++;
  80. p->ok=0;
  81. }else{
  82. p->tx++;
  83. }
  84. }
  85. #endif