File: proxy.c

package info (click to toggle)
kamailio 4.2.0-2+deb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 56,276 kB
  • sloc: ansic: 552,836; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (336 lines) | stat: -rw-r--r-- 9,133 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * $Id$
 *
 * proxy list & assoc. functions
 *
 *
 * Copyright (C) 2001-2003 FhG Fokus
 *
 * This file is part of ser, a free SIP server.
 *
 * ser is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * For a license to use the ser software under conditions
 * other than those described here, or to purchase support for this
 * software, please contact iptel.org by e-mail at the following addresses:
 *    info@iptel.org
 *
 * ser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
 /*
  * History:
  * -------
  *  2003-02-13  all *proxy functions are now proto aware (andrei)
  *  2003-03-19  replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  */

/*!
 * \file
 * \brief SIP-router core :: 
 * \ingroup core
 * Module: \ref core
 */



#include "config.h"
#include "proxy.h"
#include "error.h"
#include "dprint.h"
#include "mem/mem.h"
#include "mem/shm_mem.h"

#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>

#ifdef DNS_IP_HACK
#include "ut.h"
#endif

#include "resolve.h"
#include "ip_addr.h"
#include "globals.h"


struct proxy_l* proxies=0;



/* searches for the proxy named 'name', on port 'port' with 
   proto 'proto'; if proto==0 => proto wildcard (will match any proto)
   returns: pointer to proxy_l on success or 0 if not found */ 
static struct proxy_l* find_proxy(str *name, unsigned short port, int proto)
{
	struct proxy_l* t;
	for(t=proxies; t; t=t->next)
		if (((t->name.len == name->len) &&
			 ((proto==PROTO_NONE)||(t->proto==proto))&&
			(strncasecmp(t->name.s, name->s, name->len)==0)) &&
				(t->port==port))
			break;
	return t;
}


#define HOSTENT_CPY(dst, src, he_malloc, he_free)						\
	do {																\
		unsigned len,len2;												\
		int r,i;														\
																		\
		/* start copying the host entry.. */							\
		/* copy h_name */												\
		len=strlen(src->h_name)+1;										\
		dst->h_name=(char*)he_malloc(sizeof(char) * len);				\
		if (dst->h_name) strncpy(dst->h_name,src->h_name, len);			\
		else{															\
			ser_error=ret=E_OUT_OF_MEM;									\
			goto error;													\
		}																\
																		\
		/* copy h_aliases */											\
		len=0;															\
		if (src->h_aliases)												\
			for (;src->h_aliases[len];len++);							\
		dst->h_aliases=(char**)he_malloc(sizeof(char*)*(len+1));		\
		if (dst->h_aliases==0){											\
			ser_error=ret=E_OUT_OF_MEM;									\
			he_free(dst->h_name);										\
			goto error;													\
		}																\
		memset((void*)dst->h_aliases, 0, sizeof(char*) * (len+1) );		\
		for (i=0;i<len;i++){											\
			len2=strlen(src->h_aliases[i])+1;							\
			dst->h_aliases[i]=(char*)he_malloc(sizeof(char)*len2);		\
			if (dst->h_aliases==0){										\
				ser_error=ret=E_OUT_OF_MEM;								\
				he_free(dst->h_name);									\
				for(r=0; r<i; r++)	he_free(dst->h_aliases[r]);			\
				he_free(dst->h_aliases);								\
				goto error;												\
			}															\
			strncpy(dst->h_aliases[i], src->h_aliases[i], len2);		\
		}																\
		/* copy h_addr_list */											\
		len=0;															\
		if (src->h_addr_list)											\
			for (;src->h_addr_list[len];len++);							\
		dst->h_addr_list=(char**)he_malloc(sizeof(char*)*(len+1));		\
		if (dst->h_addr_list==0){										\
			ser_error=ret=E_OUT_OF_MEM;									\
			he_free(dst->h_name);										\
			for(r=0; dst->h_aliases[r]; r++)							\
				he_free(dst->h_aliases[r]);								\
			he_free(dst->h_aliases[r]);									\
			he_free(dst->h_aliases);									\
			goto error;													\
		}																\
		memset((void*)dst->h_addr_list, 0, sizeof(char*) * (len+1) );	\
		for (i=0;i<len;i++){											\
			dst->h_addr_list[i]=										\
				(char*)he_malloc(sizeof(char)*src->h_length);			\
			if (dst->h_addr_list[i]==0){								\
				ser_error=ret=E_OUT_OF_MEM;								\
				he_free(dst->h_name);									\
				for(r=0; dst->h_aliases[r]; r++)						\
					he_free(dst->h_aliases[r]);							\
				he_free(dst->h_aliases[r]);								\
				he_free(dst->h_aliases);								\
				for (r=0; r<i;r++) he_free(dst->h_addr_list[r]);		\
				he_free(dst->h_addr_list);								\
				goto error;												\
			}															\
			memcpy(dst->h_addr_list[i], src->h_addr_list[i],			\
				   src->h_length);										\
		}																\
																		\
		/* copy h_addr_type & length */									\
		dst->h_addrtype=src->h_addrtype;								\
		dst->h_length=src->h_length;									\
		/*finished hostent copy */										\
																		\
		return 0;														\
	} while(0)


#define FREE_HOSTENT(dst, he_free)						\
	do {												\
		int r;											\
		if (dst->h_name) he_free(dst->h_name);			\
		if (dst->h_aliases){							\
			for(r=0; dst->h_aliases[r]; r++) {			\
				he_free(dst->h_aliases[r]);				\
			}											\
			he_free(dst->h_aliases);					\
		}												\
		if (dst->h_addr_list){							\
			for (r=0; dst->h_addr_list[r];r++) {		\
				he_free(dst->h_addr_list[r]);			\
			}											\
			he_free(dst->h_addr_list);					\
		}												\
	} while(0)



/* copies a hostent structure*, returns 0 on success, <0 on error*/
static int hostent_cpy(struct hostent *dst, struct hostent* src)
{
	int ret;
	HOSTENT_CPY(dst, src, pkg_malloc, pkg_free);
error:
	LM_CRIT("memory allocation failure\n");
	return ret;
}


static int hostent_shm_cpy(struct hostent *dst, struct hostent* src)
{
	int ret;
	HOSTENT_CPY(dst, src, shm_malloc, shm_free);
error:
	LM_CRIT("memory allocation failure\n");
	return ret;
}


void free_hostent(struct hostent* dst)
{
	FREE_HOSTENT(dst, pkg_free);
}

void free_shm_hostent(struct hostent* dst)
{
	FREE_HOSTENT(dst, shm_free);
}



struct proxy_l* add_proxy(str* name, unsigned short port, int proto)
{
	struct proxy_l* p;
	
	if ((p=find_proxy(name, port, proto))!=0) return p;
	if ((p=mk_proxy(name, port, proto))==0) goto error;
	/* add p to the proxy list */
	p->next=proxies;
	proxies=p;
	return p;

error:
	return 0;
}


#define MK_PROXY(name, port, protocol, p_malloc, p_free, he_cpy)		\
	do {																\
		struct proxy_l* p;												\
		struct hostent* he;												\
		char proto;														\
																		\
		p=(struct proxy_l*) p_malloc(sizeof(struct proxy_l));			\
		if (p==0){														\
			ser_error=E_OUT_OF_MEM;										\
			LM_ERR("memory allocation failure\n");		\
			goto error;													\
		}																\
		memset(p,0,sizeof(struct proxy_l));								\
		p->name=*name;													\
		p->port=port;													\
																		\
		DBG("DEBUG: mk_proxy: doing DNS lookup...\n");					\
		proto=protocol;													\
		he=sip_resolvehost(name, &(p->port), &proto);					\
		if (he==0){														\
			ser_error=E_BAD_ADDRESS;									\
			LM_CRIT("could not resolve hostname:"	\
				" \"%.*s\"\n", name->len, name->s);						\
			p_free(p);													\
			goto error;													\
		}																\
		if (he_cpy(&(p->host), he)!=0){									\
			p_free(p);													\
			goto error;													\
		}																\
		p->proto=proto;													\
		p->ok=1;														\
		return p;														\
	error:																\
		return 0;														\
																		\
	} while(0)

/* same as add_proxy, but it doesn't add the proxy to the list
 * uses also SRV if possible & port==0 (quick hack) */

struct proxy_l* mk_proxy(str* name, unsigned short port, int protocol)
{
	MK_PROXY(name, port, protocol, pkg_malloc, pkg_free, hostent_cpy);
}


struct proxy_l* mk_shm_proxy(str* name, unsigned short port, int protocol)
{
	MK_PROXY(name, port, protocol, shm_malloc, shm_free, hostent_shm_cpy);
}



/* same as mk_proxy, but get the host as an ip*/
struct proxy_l* mk_proxy_from_ip(struct ip_addr* ip, unsigned short port,
									int proto)
{
	struct proxy_l* p;

	p=(struct proxy_l*) pkg_malloc(sizeof(struct proxy_l));
	if (p==0){
		LM_CRIT("memory allocation failure\n");
		goto error;
	}
	memset(p,0,sizeof(struct proxy_l));

	p->port=port;
	p->proto=proto;
	p->host.h_addrtype=ip->af;
	p->host.h_length=ip->len;
	p->host.h_addr_list=pkg_malloc(2*sizeof(char*));
	if (p->host.h_addr_list==0) goto error;
	p->host.h_addr_list[1]=0;
	p->host.h_addr_list[0]=pkg_malloc(ip->len+1);
	if (p->host.h_addr_list[0]==0){
		pkg_free(p->host.h_addr_list);
		goto error;
	}

	memcpy(p->host.h_addr_list[0], ip->u.addr, ip->len);
	p->host.h_addr_list[0][ip->len]=0;

	return p;

error:
	return 0;
}




void free_proxy(struct proxy_l* p)
{
	if (p) free_hostent(&p->host);
}


void free_shm_proxy(struct proxy_l* p)
{
	if (p) free_shm_hostent(&p->host);
}