File: lb_parser.c

package info (click to toggle)
opensips 2.2.2-3.1
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 31,160 kB
  • sloc: ansic: 334,348; xml: 91,231; perl: 6,659; sh: 5,148; sql: 4,175; makefile: 3,167; yacc: 2,499; python: 1,197; cpp: 611; php: 573
file content (180 lines) | stat: -rw-r--r-- 3,921 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
/*
 * load balancer module - complex call load balancing
 *
 * Copyright (C) 2009 Voice Sistem SRL
 *
 * This file is part of opensips, a free SIP server.
 *
 * opensips 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
 *
 * opensips 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:
 * --------
 *  2009-02-01 initial version (bogdan)
 */



#include <ctype.h>

#include "../../dprint.h"
#include "../../ut.h"
#include "../../mem/mem.h"

#include "lb_parser.h"


struct lb_res_str* search_resource_str( struct lb_res_str_list *lb_rl,
																	str*name)
{
	unsigned int i;

	for( i=0 ; i<lb_rl->n ; i++) {
		if (name->len==lb_rl->resources[i].name.len &&
		memcmp(name->s, lb_rl->resources[i].name.s, name->len)==0 )
			return &lb_rl->resources[i];
	}
	return NULL;
}


struct lb_res_str_list *parse_resources_list(char *r_list, int has_val)
{
	struct lb_res_str_list *lb_rl;
	unsigned int n;
	unsigned int len;
	char *p;
	char *s;
	char *end;
	str name;
	str val;

	/* validate and count */
	n = 0;
	len = 0;

	p = r_list;
	do {
		/* eat spaces */
		for( ; *p && isspace(*p) ; p++);
		if (!*p) break;
		/* name and value */
		end = strchr(p,';');
		if (end)
			*end = 0;
		name.s = p;
		p = strchr(p,'=');
		if (end)
			*end = ';';
		if (p) {
			if (!has_val) {
				LM_ERR("resource must not has value!\n");
				goto error;
			}
		} else {
			if (has_val) {
				LM_ERR("resource must has value!\n");
				goto error;
			}
			p = end?end:(r_list+strlen(r_list));
		}
		for(; (p-1)!=name.s && isspace(*(p-1)) ; p-- );
		if (p==name.s) {
			LM_ERR("empty resource name around %d\n",(unsigned int)(p-r_list));
			goto error;
		}
		name.len = p-name.s;
		/* mark */
		n++;
		len += name.len;
		/* next */
		p = end+1;
	} while(end && *p);

	if (n==0) {
		LM_ERR("empty list of resources\n");
		goto error;
	}
	LM_DBG("discovered %d resources\n",n);

	/* allocate stuff*/
	lb_rl = (struct lb_res_str_list *)pkg_malloc
		(sizeof(struct lb_res_str_list) + n*sizeof(struct lb_res_str) + len);
	if (lb_rl==NULL) {
		LM_ERR("no more pkg memory\n");
		goto error;
	}

	/* init the strucuture */
	lb_rl->n = n;
	lb_rl->resources =(struct lb_res_str*)(lb_rl+1);
	s = (char*)(lb_rl->resources + n);


	/* fill in the structures*/
	p = r_list;
	n = 0;
	do {
		/* eat spaces */
		for( ; *p && isspace(*p) ; p++);
		if (!*p) break;
		/* name .... */
		end = strchr(p,';');
		if (end)
			*end = 0;
		name.s = p;
		val.s = 0;
		p = strchr(p,'=');
		if (end)
			*end = ';';
		if (!p) {
			p = end?end:(r_list+strlen(r_list));
		} else {
			val.s = p+1;
		}
		for(; (p-1)!=name.s && isspace(*(p-1)) ; p-- );
		name.len = p-name.s;
		lb_rl->resources[n].name.len = name.len;
		lb_rl->resources[n].name.s = s;
		memcpy( s, name.s, name.len );
		s += name.len;
		/* ....and value */
		if (has_val) {
			/* eat spaces */
			for( ; *val.s && isspace(*val.s) ; val.s++);
			if (!*val.s) {
				LM_ERR("empty val !\n");
				goto error1;
			}
			val.len = ( end?end:(r_list+strlen(r_list)) ) - val.s;
			for( ; isspace(val.s[val.len-1]) ; val.len--);
			if (str2int( &val , &lb_rl->resources[n].val)!=0) {
				LM_ERR("invalid value [%.*s]\n",val.len,val.s);
				goto error1;
			}
		} else {
			lb_rl->resources[n].val = 0;
		}
		/* next */
		n++;
		p = end+1;
	} while(end && *p);

	return lb_rl;

error1:
	pkg_free(lb_rl);
error:
	return NULL;
}