proxy.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * $Id$
  3. *
  4. * proxy list & assoc. functions
  5. *
  6. *
  7. * Copyright (C) 2001-2003 FhG Fokus
  8. *
  9. * This file is part of ser, a free SIP server.
  10. *
  11. * ser is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * For a license to use the ser software under conditions
  17. * other than those described here, or to purchase support for this
  18. * software, please contact iptel.org by e-mail at the following addresses:
  19. * [email protected]
  20. *
  21. * ser is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. /*
  31. * History:
  32. * -------
  33. * 2003-02-13 all *proxy functions are now proto aware (andrei)
  34. * 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  35. */
  36. #include "config.h"
  37. #include "proxy.h"
  38. #include "error.h"
  39. #include "dprint.h"
  40. #include "mem/mem.h"
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <sys/socket.h>
  44. #ifdef DNS_IP_HACK
  45. #include "ut.h"
  46. #endif
  47. #include "resolve.h"
  48. #include "ip_addr.h"
  49. #include "globals.h"
  50. struct proxy_l* proxies=0;
  51. /* searches for the proxy named 'name', on port 'port' with
  52. proto 'proto'; if proto==0 => proto wildcard (will match any proto)
  53. returns: pointer to proxy_l on success or 0 if not found */
  54. static struct proxy_l* find_proxy(str *name, unsigned short port, int proto)
  55. {
  56. struct proxy_l* t;
  57. for(t=proxies; t; t=t->next)
  58. if (((t->name.len == name->len) &&
  59. ((proto==PROTO_NONE)||(t->proto==proto))&&
  60. (strncasecmp(t->name.s, name->s, name->len)==0)) &&
  61. (t->port==port))
  62. break;
  63. return t;
  64. }
  65. /* copies a hostent structure*, returns 0 on success, <0 on error*/
  66. static int hostent_cpy(struct hostent *dst, struct hostent* src)
  67. {
  68. unsigned len,len2;
  69. int r,ret,i;
  70. /* start copying the host entry.. */
  71. /* copy h_name */
  72. len=strlen(src->h_name)+1;
  73. dst->h_name=(char*)pkg_malloc(sizeof(char) * len);
  74. if (dst->h_name) strncpy(dst->h_name,src->h_name, len);
  75. else{
  76. ser_error=ret=E_OUT_OF_MEM;
  77. goto error;
  78. }
  79. /* copy h_aliases */
  80. len=0;
  81. if (src->h_aliases)
  82. for (;src->h_aliases[len];len++);
  83. dst->h_aliases=(char**)pkg_malloc(sizeof(char*)*(len+1));
  84. if (dst->h_aliases==0){
  85. ser_error=ret=E_OUT_OF_MEM;
  86. pkg_free(dst->h_name);
  87. goto error;
  88. }
  89. memset((void*)dst->h_aliases, 0, sizeof(char*) * (len+1) );
  90. for (i=0;i<len;i++){
  91. len2=strlen(src->h_aliases[i])+1;
  92. dst->h_aliases[i]=(char*)pkg_malloc(sizeof(char)*len2);
  93. if (dst->h_aliases==0){
  94. ser_error=ret=E_OUT_OF_MEM;
  95. pkg_free(dst->h_name);
  96. for(r=0; r<i; r++) pkg_free(dst->h_aliases[r]);
  97. pkg_free(dst->h_aliases);
  98. goto error;
  99. }
  100. strncpy(dst->h_aliases[i], src->h_aliases[i], len2);
  101. }
  102. /* copy h_addr_list */
  103. len=0;
  104. if (src->h_addr_list)
  105. for (;src->h_addr_list[len];len++);
  106. dst->h_addr_list=(char**)pkg_malloc(sizeof(char*)*(len+1));
  107. if (dst->h_addr_list==0){
  108. ser_error=ret=E_OUT_OF_MEM;
  109. pkg_free(dst->h_name);
  110. for(r=0; dst->h_aliases[r]; r++) pkg_free(dst->h_aliases[r]);
  111. pkg_free(dst->h_aliases[r]);
  112. pkg_free(dst->h_aliases);
  113. goto error;
  114. }
  115. memset((void*)dst->h_addr_list, 0, sizeof(char*) * (len+1) );
  116. for (i=0;i<len;i++){
  117. dst->h_addr_list[i]=(char*)pkg_malloc(sizeof(char)*src->h_length);
  118. if (dst->h_addr_list[i]==0){
  119. ser_error=ret=E_OUT_OF_MEM;
  120. pkg_free(dst->h_name);
  121. for(r=0; dst->h_aliases[r]; r++) pkg_free(dst->h_aliases[r]);
  122. pkg_free(dst->h_aliases[r]);
  123. pkg_free(dst->h_aliases);
  124. for (r=0; r<i;r++) pkg_free(dst->h_addr_list[r]);
  125. pkg_free(dst->h_addr_list);
  126. goto error;
  127. }
  128. memcpy(dst->h_addr_list[i], src->h_addr_list[i], src->h_length);
  129. }
  130. /* copy h_addr_type & length */
  131. dst->h_addrtype=src->h_addrtype;
  132. dst->h_length=src->h_length;
  133. /*finished hostent copy */
  134. return 0;
  135. error:
  136. LOG(L_CRIT, "ERROR: hostent_cpy: memory allocation failure\n");
  137. return ret;
  138. }
  139. void free_hostent(struct hostent *dst)
  140. {
  141. int r;
  142. if (dst->h_name) pkg_free(dst->h_name);
  143. if (dst->h_aliases){
  144. for(r=0; dst->h_aliases[r]; r++) {
  145. pkg_free(dst->h_aliases[r]);
  146. }
  147. pkg_free(dst->h_aliases);
  148. }
  149. if (dst->h_addr_list){
  150. for (r=0; dst->h_addr_list[r];r++) {
  151. pkg_free(dst->h_addr_list[r]);
  152. }
  153. pkg_free(dst->h_addr_list);
  154. }
  155. }
  156. struct proxy_l* add_proxy(str* name, unsigned short port, int proto)
  157. {
  158. struct proxy_l* p;
  159. if ((p=find_proxy(name, port, proto))!=0) return p;
  160. if ((p=mk_proxy(name, port, proto))==0) goto error;
  161. /* add p to the proxy list */
  162. p->next=proxies;
  163. proxies=p;
  164. return p;
  165. error:
  166. return 0;
  167. }
  168. /* same as add_proxy, but it doesn't add the proxy to the list
  169. * uses also SRV if possible & port==0 (quick hack) */
  170. struct proxy_l* mk_proxy(str* name, unsigned short port, int protocol)
  171. {
  172. struct proxy_l* p;
  173. struct hostent* he;
  174. char proto;
  175. p=(struct proxy_l*) pkg_malloc(sizeof(struct proxy_l));
  176. if (p==0){
  177. ser_error=E_OUT_OF_MEM;
  178. LOG(L_CRIT, "ERROR: mk_proxy: memory allocation failure\n");
  179. goto error;
  180. }
  181. memset(p,0,sizeof(struct proxy_l));
  182. p->name=*name;
  183. p->port=port;
  184. DBG("DEBUG: mk_proxy: doing DNS lookup...\n");
  185. proto=protocol;
  186. he=sip_resolvehost(name, &(p->port), &proto);
  187. if (he==0){
  188. ser_error=E_BAD_ADDRESS;
  189. LOG(L_CRIT, "ERROR: mk_proxy: could not resolve hostname:"
  190. " \"%.*s\"\n", name->len, name->s);
  191. pkg_free(p);
  192. goto error;
  193. }
  194. if (hostent_cpy(&(p->host), he)!=0){
  195. pkg_free(p);
  196. goto error;
  197. }
  198. p->proto=proto;
  199. p->ok=1;
  200. return p;
  201. error:
  202. return 0;
  203. }
  204. /* same as mk_proxy, but get the host as an ip*/
  205. struct proxy_l* mk_proxy_from_ip(struct ip_addr* ip, unsigned short port,
  206. int proto)
  207. {
  208. struct proxy_l* p;
  209. p=(struct proxy_l*) pkg_malloc(sizeof(struct proxy_l));
  210. if (p==0){
  211. LOG(L_CRIT, "ERROR: mk_proxy_from_ip: memory allocation failure\n");
  212. goto error;
  213. }
  214. memset(p,0,sizeof(struct proxy_l));
  215. p->port=port;
  216. p->proto=proto;
  217. p->host.h_addrtype=ip->af;
  218. p->host.h_length=ip->len;
  219. p->host.h_addr_list=pkg_malloc(2*sizeof(char*));
  220. if (p->host.h_addr_list==0) goto error;
  221. p->host.h_addr_list[1]=0;
  222. p->host.h_addr_list[0]=pkg_malloc(ip->len+1);
  223. if (p->host.h_addr_list[0]==0){
  224. pkg_free(p->host.h_addr_list);
  225. goto error;
  226. }
  227. memcpy(p->host.h_addr_list[0], ip->u.addr, ip->len);
  228. p->host.h_addr_list[0][ip->len]=0;
  229. return p;
  230. error:
  231. return 0;
  232. }
  233. void free_proxy(struct proxy_l* p)
  234. {
  235. if (p) free_hostent(&p->host);
  236. }