proxy.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. /*!
  37. * \file
  38. * \brief SIP-router core ::
  39. * \ingroup core
  40. * Module: \ref core
  41. */
  42. #include "config.h"
  43. #include "proxy.h"
  44. #include "error.h"
  45. #include "dprint.h"
  46. #include "mem/mem.h"
  47. #include "mem/shm_mem.h"
  48. #include <string.h>
  49. #include <stdlib.h>
  50. #include <sys/socket.h>
  51. #ifdef DNS_IP_HACK
  52. #include "ut.h"
  53. #endif
  54. #include "resolve.h"
  55. #include "ip_addr.h"
  56. #include "globals.h"
  57. struct proxy_l* proxies=0;
  58. /* searches for the proxy named 'name', on port 'port' with
  59. proto 'proto'; if proto==0 => proto wildcard (will match any proto)
  60. returns: pointer to proxy_l on success or 0 if not found */
  61. static struct proxy_l* find_proxy(str *name, unsigned short port, int proto)
  62. {
  63. struct proxy_l* t;
  64. for(t=proxies; t; t=t->next)
  65. if (((t->name.len == name->len) &&
  66. ((proto==PROTO_NONE)||(t->proto==proto))&&
  67. (strncasecmp(t->name.s, name->s, name->len)==0)) &&
  68. (t->port==port))
  69. break;
  70. return t;
  71. }
  72. #define HOSTENT_CPY(dst, src, he_malloc, he_free) \
  73. do { \
  74. unsigned len,len2; \
  75. int r,i; \
  76. \
  77. /* start copying the host entry.. */ \
  78. /* copy h_name */ \
  79. len=strlen(src->h_name)+1; \
  80. dst->h_name=(char*)he_malloc(sizeof(char) * len); \
  81. if (dst->h_name) strncpy(dst->h_name,src->h_name, len); \
  82. else{ \
  83. ser_error=ret=E_OUT_OF_MEM; \
  84. goto error; \
  85. } \
  86. \
  87. /* copy h_aliases */ \
  88. len=0; \
  89. if (src->h_aliases) \
  90. for (;src->h_aliases[len];len++); \
  91. dst->h_aliases=(char**)he_malloc(sizeof(char*)*(len+1)); \
  92. if (dst->h_aliases==0){ \
  93. ser_error=ret=E_OUT_OF_MEM; \
  94. he_free(dst->h_name); \
  95. goto error; \
  96. } \
  97. memset((void*)dst->h_aliases, 0, sizeof(char*) * (len+1) ); \
  98. for (i=0;i<len;i++){ \
  99. len2=strlen(src->h_aliases[i])+1; \
  100. dst->h_aliases[i]=(char*)he_malloc(sizeof(char)*len2); \
  101. if (dst->h_aliases==0){ \
  102. ser_error=ret=E_OUT_OF_MEM; \
  103. he_free(dst->h_name); \
  104. for(r=0; r<i; r++) he_free(dst->h_aliases[r]); \
  105. he_free(dst->h_aliases); \
  106. goto error; \
  107. } \
  108. strncpy(dst->h_aliases[i], src->h_aliases[i], len2); \
  109. } \
  110. /* copy h_addr_list */ \
  111. len=0; \
  112. if (src->h_addr_list) \
  113. for (;src->h_addr_list[len];len++); \
  114. dst->h_addr_list=(char**)he_malloc(sizeof(char*)*(len+1)); \
  115. if (dst->h_addr_list==0){ \
  116. ser_error=ret=E_OUT_OF_MEM; \
  117. he_free(dst->h_name); \
  118. for(r=0; dst->h_aliases[r]; r++) \
  119. he_free(dst->h_aliases[r]); \
  120. he_free(dst->h_aliases[r]); \
  121. he_free(dst->h_aliases); \
  122. goto error; \
  123. } \
  124. memset((void*)dst->h_addr_list, 0, sizeof(char*) * (len+1) ); \
  125. for (i=0;i<len;i++){ \
  126. dst->h_addr_list[i]= \
  127. (char*)he_malloc(sizeof(char)*src->h_length); \
  128. if (dst->h_addr_list[i]==0){ \
  129. ser_error=ret=E_OUT_OF_MEM; \
  130. he_free(dst->h_name); \
  131. for(r=0; dst->h_aliases[r]; r++) \
  132. he_free(dst->h_aliases[r]); \
  133. he_free(dst->h_aliases[r]); \
  134. he_free(dst->h_aliases); \
  135. for (r=0; r<i;r++) he_free(dst->h_addr_list[r]); \
  136. he_free(dst->h_addr_list); \
  137. goto error; \
  138. } \
  139. memcpy(dst->h_addr_list[i], src->h_addr_list[i], \
  140. src->h_length); \
  141. } \
  142. \
  143. /* copy h_addr_type & length */ \
  144. dst->h_addrtype=src->h_addrtype; \
  145. dst->h_length=src->h_length; \
  146. /*finished hostent copy */ \
  147. \
  148. return 0; \
  149. } while(0)
  150. #define FREE_HOSTENT(dst, he_free) \
  151. do { \
  152. int r; \
  153. if (dst->h_name) he_free(dst->h_name); \
  154. if (dst->h_aliases){ \
  155. for(r=0; dst->h_aliases[r]; r++) { \
  156. he_free(dst->h_aliases[r]); \
  157. } \
  158. he_free(dst->h_aliases); \
  159. } \
  160. if (dst->h_addr_list){ \
  161. for (r=0; dst->h_addr_list[r];r++) { \
  162. he_free(dst->h_addr_list[r]); \
  163. } \
  164. he_free(dst->h_addr_list); \
  165. } \
  166. } while(0)
  167. /* copies a hostent structure*, returns 0 on success, <0 on error*/
  168. static int hostent_cpy(struct hostent *dst, struct hostent* src)
  169. {
  170. int ret;
  171. HOSTENT_CPY(dst, src, pkg_malloc, pkg_free);
  172. error:
  173. LM_CRIT("memory allocation failure\n");
  174. return ret;
  175. }
  176. static int hostent_shm_cpy(struct hostent *dst, struct hostent* src)
  177. {
  178. int ret;
  179. HOSTENT_CPY(dst, src, shm_malloc, shm_free);
  180. error:
  181. LM_CRIT("memory allocation failure\n");
  182. return ret;
  183. }
  184. void free_hostent(struct hostent* dst)
  185. {
  186. FREE_HOSTENT(dst, pkg_free);
  187. }
  188. void free_shm_hostent(struct hostent* dst)
  189. {
  190. FREE_HOSTENT(dst, shm_free);
  191. }
  192. struct proxy_l* add_proxy(str* name, unsigned short port, int proto)
  193. {
  194. struct proxy_l* p;
  195. if ((p=find_proxy(name, port, proto))!=0) return p;
  196. if ((p=mk_proxy(name, port, proto))==0) goto error;
  197. /* add p to the proxy list */
  198. p->next=proxies;
  199. proxies=p;
  200. return p;
  201. error:
  202. return 0;
  203. }
  204. #define MK_PROXY(name, port, protocol, p_malloc, p_free, he_cpy) \
  205. do { \
  206. struct proxy_l* p; \
  207. struct hostent* he; \
  208. char proto; \
  209. \
  210. p=(struct proxy_l*) p_malloc(sizeof(struct proxy_l)); \
  211. if (p==0){ \
  212. ser_error=E_OUT_OF_MEM; \
  213. LM_ERR("memory allocation failure\n"); \
  214. goto error; \
  215. } \
  216. memset(p,0,sizeof(struct proxy_l)); \
  217. p->name=*name; \
  218. p->port=port; \
  219. \
  220. DBG("DEBUG: mk_proxy: doing DNS lookup...\n"); \
  221. proto=protocol; \
  222. he=sip_resolvehost(name, &(p->port), &proto); \
  223. if (he==0){ \
  224. ser_error=E_BAD_ADDRESS; \
  225. LM_CRIT("could not resolve hostname:" \
  226. " \"%.*s\"\n", name->len, name->s); \
  227. p_free(p); \
  228. goto error; \
  229. } \
  230. if (he_cpy(&(p->host), he)!=0){ \
  231. p_free(p); \
  232. goto error; \
  233. } \
  234. p->proto=proto; \
  235. p->ok=1; \
  236. return p; \
  237. error: \
  238. return 0; \
  239. \
  240. } while(0)
  241. /* same as add_proxy, but it doesn't add the proxy to the list
  242. * uses also SRV if possible & port==0 (quick hack) */
  243. struct proxy_l* mk_proxy(str* name, unsigned short port, int protocol)
  244. {
  245. MK_PROXY(name, port, protocol, pkg_malloc, pkg_free, hostent_cpy);
  246. }
  247. struct proxy_l* mk_shm_proxy(str* name, unsigned short port, int protocol)
  248. {
  249. MK_PROXY(name, port, protocol, shm_malloc, shm_free, hostent_shm_cpy);
  250. }
  251. /* same as mk_proxy, but get the host as an ip*/
  252. struct proxy_l* mk_proxy_from_ip(struct ip_addr* ip, unsigned short port,
  253. int proto)
  254. {
  255. struct proxy_l* p;
  256. p=(struct proxy_l*) pkg_malloc(sizeof(struct proxy_l));
  257. if (p==0){
  258. LM_CRIT("memory allocation failure\n");
  259. goto error;
  260. }
  261. memset(p,0,sizeof(struct proxy_l));
  262. p->port=port;
  263. p->proto=proto;
  264. p->host.h_addrtype=ip->af;
  265. p->host.h_length=ip->len;
  266. p->host.h_addr_list=pkg_malloc(2*sizeof(char*));
  267. if (p->host.h_addr_list==0) goto error;
  268. p->host.h_addr_list[1]=0;
  269. p->host.h_addr_list[0]=pkg_malloc(ip->len+1);
  270. if (p->host.h_addr_list[0]==0){
  271. pkg_free(p->host.h_addr_list);
  272. goto error;
  273. }
  274. memcpy(p->host.h_addr_list[0], ip->u.addr, ip->len);
  275. p->host.h_addr_list[0][ip->len]=0;
  276. return p;
  277. error:
  278. return 0;
  279. }
  280. void free_proxy(struct proxy_l* p)
  281. {
  282. if (p) free_hostent(&p->host);
  283. }
  284. void free_shm_proxy(struct proxy_l* p)
  285. {
  286. if (p) free_shm_hostent(&p->host);
  287. }