proxy.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #include "config.h"
  31. #include "proxy.h"
  32. #include "error.h"
  33. #include "dprint.h"
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include <sys/socket.h>
  37. #ifdef DNS_IP_HACK
  38. #include "ut.h"
  39. #endif
  40. #include "resolve.h"
  41. #include "ip_addr.h"
  42. #include "globals.h"
  43. #ifdef DEBUG_DMALLOC
  44. #include <dmalloc.h>
  45. #endif
  46. struct proxy_l* proxies=0;
  47. /* searches for the proxy named 'name', on port 'port'
  48. returns: pointer to proxy_l on success or 0 if not found */
  49. static struct proxy_l* find_proxy(str *name, unsigned short port)
  50. {
  51. struct proxy_l* t;
  52. for(t=proxies; t; t=t->next)
  53. if (((t->name.len == name->len) && (strncasecmp(t->name.s, name->s, name->len)==0)) && (t->port==port))
  54. break;
  55. return t;
  56. }
  57. /* copies a hostent structure*, returns 0 on success, <0 on error*/
  58. static int hostent_cpy(struct hostent *dst, struct hostent* src)
  59. {
  60. unsigned len,len2;
  61. int r,ret,i;
  62. /* start copying the host entry.. */
  63. /* copy h_name */
  64. len=strlen(src->h_name)+1;
  65. dst->h_name=(char*)malloc(sizeof(char) * len);
  66. if (dst->h_name) strncpy(dst->h_name,src->h_name, len);
  67. else{
  68. ser_error=ret=E_OUT_OF_MEM;
  69. goto error;
  70. }
  71. /* copy h_aliases */
  72. len=0;
  73. if (src->h_aliases)
  74. for (;src->h_aliases[len];len++);
  75. dst->h_aliases=(char**)malloc(sizeof(char*)*(len+1));
  76. if (dst->h_aliases==0){
  77. ser_error=ret=E_OUT_OF_MEM;
  78. free(dst->h_name);
  79. goto error;
  80. }
  81. memset((void*)dst->h_aliases, 0, sizeof(char*) * (len+1) );
  82. for (i=0;i<len;i++){
  83. len2=strlen(src->h_aliases[i])+1;
  84. dst->h_aliases[i]=(char*)malloc(sizeof(char)*len2);
  85. if (dst->h_aliases==0){
  86. ser_error=ret=E_OUT_OF_MEM;
  87. free(dst->h_name);
  88. for(r=0; r<i; r++) free(dst->h_aliases[r]);
  89. free(dst->h_aliases);
  90. goto error;
  91. }
  92. strncpy(dst->h_aliases[i], src->h_aliases[i], len2);
  93. }
  94. /* copy h_addr_list */
  95. len=0;
  96. if (src->h_addr_list)
  97. for (;src->h_addr_list[len];len++);
  98. dst->h_addr_list=(char**)malloc(sizeof(char*)*(len+1));
  99. if (dst->h_addr_list==0){
  100. ser_error=ret=E_OUT_OF_MEM;
  101. free(dst->h_name);
  102. for(r=0; dst->h_aliases[r]; r++) free(dst->h_aliases[r]);
  103. free(dst->h_aliases[r]);
  104. free(dst->h_aliases);
  105. goto error;
  106. }
  107. memset((void*)dst->h_addr_list, 0, sizeof(char*) * (len+1) );
  108. for (i=0;i<len;i++){
  109. dst->h_addr_list[i]=(char*)malloc(sizeof(char)*src->h_length);
  110. if (dst->h_addr_list[i]==0){
  111. ser_error=ret=E_OUT_OF_MEM;
  112. free(dst->h_name);
  113. for(r=0; dst->h_aliases[r]; r++) free(dst->h_aliases[r]);
  114. free(dst->h_aliases[r]);
  115. free(dst->h_aliases);
  116. for (r=0; r<i;r++) free(dst->h_addr_list[r]);
  117. free(dst->h_addr_list);
  118. goto error;
  119. }
  120. memcpy(dst->h_addr_list[i], src->h_addr_list[i], src->h_length);
  121. }
  122. /* copy h_addr_type & length */
  123. dst->h_addrtype=src->h_addrtype;
  124. dst->h_length=src->h_length;
  125. /*finished hostent copy */
  126. return 0;
  127. error:
  128. LOG(L_CRIT, "ERROR: hostent_cpy: memory allocation failure\n");
  129. return ret;
  130. }
  131. void free_hostent(struct hostent *dst)
  132. {
  133. int r;
  134. if (dst->h_name) free(dst->h_name);
  135. if (dst->h_aliases){
  136. for(r=0; dst->h_aliases[r]; r++) free(dst->h_aliases[r]);
  137. free(dst->h_aliases[r]);
  138. free(dst->h_aliases);
  139. }
  140. if (dst->h_addr_list){
  141. for (r=0; dst->h_addr_list[r];r++) free(dst->h_addr_list[r]);
  142. free(dst->h_addr_list[r]);
  143. free(dst->h_addr_list);
  144. }
  145. }
  146. struct proxy_l* add_proxy(str* name, unsigned short port)
  147. {
  148. struct proxy_l* p;
  149. if ((p=find_proxy(name, port))!=0) return p;
  150. if ((p=mk_proxy(name, port))==0) goto error;
  151. /* add p to the proxy list */
  152. p->next=proxies;
  153. proxies=p;
  154. return p;
  155. error:
  156. return 0;
  157. }
  158. /* same as add_proxy, but it doesn't add the proxy to the list
  159. * uses also SRV if possible (quick hack) */
  160. struct proxy_l* mk_proxy(str* name, unsigned short port)
  161. {
  162. struct proxy_l* p;
  163. struct hostent* he;
  164. p=(struct proxy_l*) malloc(sizeof(struct proxy_l));
  165. if (p==0){
  166. ser_error=E_OUT_OF_MEM;
  167. LOG(L_CRIT, "ERROR: mk_proxy: memory allocation failure\n");
  168. goto error;
  169. }
  170. memset(p,0,sizeof(struct proxy_l));
  171. p->name=*name;
  172. p->port=port;
  173. DBG("DEBUG: mk_proxy: doing DNS lookup...\n");
  174. he=sip_resolvehost(name, &(p->port));
  175. if (he==0){
  176. ser_error=E_BAD_ADDRESS;
  177. LOG(L_CRIT, "ERROR: mk_proxy: could not resolve hostname:"
  178. " \"%.*s\"\n", name->len, name->s);
  179. free(p);
  180. goto error;
  181. }
  182. if (hostent_cpy(&(p->host), he)!=0){
  183. free(p);
  184. goto error;
  185. }
  186. p->ok=1;
  187. return p;
  188. error:
  189. return 0;
  190. }
  191. /* same as mk_proxy, but get the host as an ip*/
  192. struct proxy_l* mk_proxy_from_ip(struct ip_addr* ip, unsigned short port)
  193. {
  194. struct proxy_l* p;
  195. p=(struct proxy_l*) malloc(sizeof(struct proxy_l));
  196. if (p==0){
  197. LOG(L_CRIT, "ERROR: mk_proxy_from_ip: memory allocation failure\n");
  198. goto error;
  199. }
  200. memset(p,0,sizeof(struct proxy_l));
  201. p->port=port;
  202. p->host.h_addrtype=ip->af;
  203. p->host.h_length=ip->len;
  204. p->host.h_addr_list=malloc(2*sizeof(char*));
  205. if (p->host.h_addr_list==0) goto error;
  206. p->host.h_addr_list[1]=0;
  207. p->host.h_addr_list[0]=malloc(ip->len+1);
  208. if (p->host.h_addr_list[0]==0){
  209. free(p->host.h_addr_list);
  210. goto error;
  211. }
  212. memcpy(p->host.h_addr_list[0], ip->u.addr, ip->len);
  213. p->host.h_addr_list[0][ip->len]=0;
  214. return p;
  215. error:
  216. return 0;
  217. }
  218. void free_proxy(struct proxy_l* p)
  219. {
  220. if (p) free_hostent(&p->host);
  221. }