proxy.c 8.9 KB

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