Skip to content

Commit b084f68

Browse files
Change grammar in line with PR comments and add support to domain module
1 parent f4555f5 commit b084f68

File tree

12 files changed

+176
-111
lines changed

12 files changed

+176
-111
lines changed

cfg.lex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ CR \n
341341
342342
ANY "any"
343343
ANYCAST ("anycast"|"ANYCAST")
344-
SUBDOMAIN ("subdomain"|"SUBDOMAIN")
344+
ACCEPT_SUBDOMAIN ("accept_subdomain"|"ACCEPT_SUBDOMAIN")
345345
FRAG ("frag"|"FRAG")
346346
REUSE_PORT ("reuse_port"|"REUSE_PORT")
347347
@@ -616,7 +616,7 @@ SPACE [ ]
616616
<INITIAL>{CR} { count();/* return CR;*/ }
617617
<INITIAL>{ANY} { count(); return ANY; }
618618
<INITIAL>{ANYCAST} { count(); return ANYCAST; }
619-
<INITIAL>{SUBDOMAIN} { count(); return SUBDOMAIN; }
619+
<INITIAL>{ACCEPT_SUBDOMAIN} { count(); return ACCEPT_SUBDOMAIN; }
620620
<INITIAL>{REUSE_PORT} { count(); return REUSE_PORT; }
621621
<INITIAL>{FRAG} { count(); return FRAG; }
622622
<INITIAL>{SLASH} { count(); return SLASH; }

cfg.y

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static struct multi_str *tmp_mod;
206206
#define get_cfg_file_name \
207207
((finame) ? finame : cfg_file ? cfg_file : "default")
208208

209-
209+
#define si_subdomain_to_alias_flag(_flags) (int) _flags & SI_ACCEPT_SUBDOMAIN_ALIAS
210210

211211
#define mk_action_(_res, _type, _no, _elems) \
212212
do { \
@@ -467,7 +467,7 @@ extern int cfg_parse_only_routes;
467467
%token COLON
468468
%token ANY
469469
%token ANYCAST
470-
%token SUBDOMAIN
470+
%token ACCEPT_SUBDOMAIN
471471
%token FRAG
472472
%token REUSE_PORT
473473
%token SCRIPTVARERR
@@ -710,22 +710,12 @@ any_alias: listen_id { IFOR();
710710
| phostport
711711
;
712712

713-
id_lst_param: SUBDOMAIN { IFOR();
714-
p_tmp.flags |= SI_IS_SUBDOMAIN_ALIAS;
713+
alias_def: any_alias { $$=$1; }
714+
| any_alias ACCEPT_SUBDOMAIN {
715+
$$=$1;
716+
$$->flags |= SI_ACCEPT_SUBDOMAIN_ALIAS;
715717
}
716-
;
717-
718-
id_lst_params: id_lst_param
719-
| id_lst_param id_lst_params
720-
;
721-
722-
alias_def: any_alias { $$=$1; }
723-
| any_alias { IFOR();
724-
memset(&p_tmp, 0, sizeof(p_tmp));
725-
} id_lst_params { IFOR();
726-
$$=$1; fill_alias_socket(&p_tmp, $$);
727-
}
728-
;
718+
;
729719

730720
id_lst: alias_def { IFOR(); $$=$1 ; }
731721
| alias_def id_lst { IFOR(); $$=$1; $$->next=$2; }
@@ -750,8 +740,8 @@ socket_def_param: ANYCAST { IFOR();
750740
| REUSE_PORT { IFOR();
751741
p_tmp.flags |= SI_REUSEPORT;
752742
}
753-
| SUBDOMAIN { IFOR();
754-
p_tmp.flags |= SI_IS_SUBDOMAIN_ALIAS;
743+
| ACCEPT_SUBDOMAIN { IFOR();
744+
p_tmp.flags |= SI_ACCEPT_SUBDOMAIN_ALIAS;
755745
}
756746
| USE_WORKERS NUMBER { IFOR();
757747
p_tmp.workers=$2;
@@ -1507,7 +1497,7 @@ assign_stm: LOGLEVEL EQUAL snumber { IFOR();
15071497
| ALIAS EQUAL id_lst { IFOR();
15081498
for(lst_tmp=$3; lst_tmp; lst_tmp=lst_tmp->next)
15091499
add_alias(lst_tmp->name, strlen(lst_tmp->name),
1510-
lst_tmp->port, lst_tmp->proto, lst_tmp->flags);
1500+
lst_tmp->port, lst_tmp->proto, si_subdomain_to_alias_flag(lst_tmp->flags));
15111501
}
15121502
| ALIAS EQUAL error { yyerror("hostname expected (use quotes"
15131503
" if the hostname includes config keywords)"); }

db/schema/domain.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
<null/>
4545
</column>
4646

47+
<column id="accept_subdomain">
48+
<name>accept_subdomain</name>
49+
<type>unsigned int</type>
50+
<size>11</size>
51+
<default>0</default>
52+
<description>Accept subdomain</description>
53+
</column>
4754

4855
<column>
4956
<name>last_modified</name>

ip_addr.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ union sockaddr_union{
8989

9090

9191
enum si_flags { SI_NONE=0, SI_IS_IP=1, SI_IS_LO=2, SI_IS_MCAST=4,
92-
SI_IS_ANYCAST=8, SI_FRAG=16, SI_REUSEPORT=32, SI_INTERNAL=64, SI_IS_SUBDOMAIN_ALIAS=128 };
92+
SI_IS_ANYCAST=8, SI_FRAG=16, SI_REUSEPORT=32, SI_INTERNAL=64, SI_ACCEPT_SUBDOMAIN_ALIAS=128 };
9393

9494
struct receive_info {
9595
struct ip_addr src_ip;
@@ -126,8 +126,6 @@ struct socket_id {
126126
struct socket_id* next;
127127
};
128128

129-
130-
131129
/* len of the sockaddr */
132130
#ifdef HAVE_SOCKADDR_SA_LEN
133131
#define sockaddru_len(su) ((su).s.sa_len)

modules/domain/domain.c

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "../../route.h"
3838
#include "../../pvar.h"
3939
#include "../../str.h"
40+
#include "../../name_alias.h"
4041

4142
#define DOMAIN_TABLE_VERSION 3
4243

@@ -92,54 +93,58 @@ int domain_db_ver(str* name, int version)
9293
return db_check_table_version(&domain_dbf, db_handle, name, version);
9394
}
9495

95-
96-
/*
97-
* Check if domain is local and store attributes in a pvar
98-
*/
99-
int is_domain_local_pvar(struct sip_msg *msg, str* _host, pv_spec_t *pv)
96+
int db_table_lookup(struct sip_msg *msg, str *lookup_domain, str *host, pv_spec_t *pv)
10097
{
10198
pv_value_t val;
10299
db_val_t *values;
103100

104-
if (db_mode == 0) {
105-
db_key_t keys[1];
106-
db_val_t vals[1];
107-
db_key_t cols[2];
108-
db_res_t* res = NULL;
101+
db_key_t keys[1];
102+
db_val_t vals[1];
103+
db_key_t cols[3];
104+
db_res_t* res = NULL;
109105

110-
keys[0] = &domain_col;
111-
cols[0] = &domain_col;
112-
cols[1] = &domain_attrs_col;
106+
int accept_subdomain;
113107

114-
if (domain_dbf.use_table(db_handle, &domain_table) < 0) {
115-
LM_ERR("Error while trying to use domain table\n");
116-
return -3;
117-
}
108+
keys[0] = &domain_col;
109+
cols[0] = &domain_col;
110+
cols[1] = &domain_attrs_col;
111+
cols[2] = &domain_accept_subdomain_col;
118112

119-
VAL_TYPE(vals) = DB_STR;
120-
VAL_NULL(vals) = 0;
113+
if (domain_dbf.use_table(db_handle, &domain_table) < 0) {
114+
LM_ERR("Error while trying to use domain table\n");
115+
return -3;
116+
}
121117

122-
VAL_STR(vals).s = _host->s;
123-
VAL_STR(vals).len = _host->len;
118+
VAL_TYPE(vals) = DB_STR;
119+
VAL_NULL(vals) = 0;
124120

125-
if (domain_dbf.query(db_handle, keys, 0, vals, cols, 1, 2, 0, &res) < 0
126-
) {
127-
LM_ERR("Error while querying database\n");
128-
return -3;
129-
}
121+
VAL_STR(vals).s = lookup_domain->s;
122+
VAL_STR(vals).len = lookup_domain->len;
130123

131-
if (RES_ROW_N(res) == 0) {
132-
LM_DBG("Realm '%.*s' is not local\n",
133-
_host->len, ZSW(_host->s));
134-
domain_dbf.free_result(db_handle, res);
135-
return -1;
124+
if (domain_dbf.query(db_handle, keys, 0, vals, cols, 1, 3, 0, &res) < 0) {
125+
LM_ERR("Error while querying database\n");
126+
return -3;
127+
}
128+
129+
if (RES_ROW_N(res) > 0) {
130+
values = ROW_VALUES(RES_ROWS(res));
131+
132+
if (VAL_NULL(values + 2)) {
133+
accept_subdomain = 0;
134+
} else if (VAL_TYPE(values + 2) == DB_INT) {
135+
accept_subdomain = VAL_INT(values + 2);
136136
} else {
137-
LM_DBG("Realm '%.*s' is local\n",
138-
_host->len, ZSW(_host->s));
137+
LM_ERR("Error setting accept_subdomain, default to 0\n");
138+
accept_subdomain = 0;
139+
}
140+
141+
LM_DBG("Checking realm '%.*s' against domain entry '%.*s' accept_subdomain is '%d' \n",
142+
host->len, ZSW(host->s), lookup_domain->len, ZSW(lookup_domain->s), accept_subdomain);
143+
144+
if (match_domain(lookup_domain->s, lookup_domain->len, host->s, host->len, accept_subdomain)) {
139145
if (pv) {
140146
/* XXX: what shall we do if there are duplicate entries? */
141147
/* we only check the first row - razvanc */
142-
values = ROW_VALUES(RES_ROWS(res));
143148
if (!VAL_NULL(values +1)) {
144149
if (VAL_TYPE(values + 1) == DB_STR) {
145150
val.rs = VAL_STR(values + 1);
@@ -152,13 +157,47 @@ int is_domain_local_pvar(struct sip_msg *msg, str* _host, pv_spec_t *pv)
152157
LM_ERR("Cannot set attributes value\n");
153158
}
154159
}
160+
155161
domain_dbf.free_result(db_handle, res);
156162
return 1;
157163
}
158-
} else {
159-
return hash_table_lookup (msg, _host, pv);
160164
}
161165

166+
domain_dbf.free_result(db_handle, res);
167+
return -1;
168+
}
169+
170+
/*
171+
* Check if domain is local and store attributes in a pvar
172+
*/
173+
int is_domain_local_pvar(struct sip_msg *msg, str* _host, pv_spec_t *pv)
174+
{
175+
char *next_domain;
176+
str lookup_domain = { _host->s, _host->len };
177+
178+
do {
179+
if (db_mode == 0) {
180+
if (db_table_lookup(msg, &lookup_domain, _host, pv) == 1)
181+
return 1;
182+
} else {
183+
if (hash_table_lookup(msg, &lookup_domain, _host, pv) == 1)
184+
return 1;
185+
}
186+
187+
LM_DBG("Realm '%.*s' is not local\n", lookup_domain.len, ZSW(lookup_domain.s));
188+
189+
next_domain = strchr(lookup_domain.s, '.');
190+
191+
if (next_domain == NULL)
192+
break;
193+
194+
next_domain++;
195+
196+
lookup_domain.len = lookup_domain.len - (next_domain - lookup_domain.s);
197+
lookup_domain.s = next_domain;
198+
} while(strrchr(next_domain, '.') != NULL);
199+
200+
return -1;
162201
}
163202

164203
/*
@@ -214,25 +253,27 @@ int w_is_domain_local(struct sip_msg* _msg, str *domain, pv_spec_t* _s2)
214253
*/
215254
int reload_domain_table ( void )
216255
{
217-
db_key_t cols[2];
256+
db_key_t cols[3];
218257
db_res_t* res = NULL;
219258
db_row_t* row;
220259
db_val_t* val;
221260

222261
struct domain_list **new_hash_table;
223262
int i;
263+
int accept_subdomain;
224264

225265
str domain, attrs;
226266

227267
cols[0] = &domain_col;
228268
cols[1] = &domain_attrs_col;
269+
cols[2] = &domain_accept_subdomain_col;
229270

230271
if (domain_dbf.use_table(db_handle, &domain_table) < 0) {
231272
LM_ERR("Error while trying to use domain table\n");
232273
return -3;
233274
}
234275

235-
if (domain_dbf.query(db_handle, NULL, 0, NULL, cols, 0, 2, 0, &res) < 0) {
276+
if (domain_dbf.query(db_handle, NULL, 0, NULL, cols, 0, 3, 0, &res) < 0) {
236277
LM_ERR("Error while querying database\n");
237278
return -3;
238279
}
@@ -252,6 +293,7 @@ int reload_domain_table ( void )
252293

253294
for (i = 0; i < RES_ROW_N(res); i++) {
254295
val = ROW_VALUES(row + i);
296+
255297
if (VAL_TYPE(val) == DB_STRING) {
256298
domain.s = (char *)VAL_STRING(val);
257299
domain.len = strlen(domain.s);
@@ -262,6 +304,7 @@ int reload_domain_table ( void )
262304
domain_dbf.free_result(db_handle, res);
263305
return -3;
264306
}
307+
265308
if (VAL_NULL(val + 1)) {
266309
/* add a marker to determine whether the attributes exist or not */
267310
attrs.len = 0;
@@ -278,9 +321,20 @@ int reload_domain_table ( void )
278321
domain_dbf.free_result(db_handle, res);
279322
return -3;
280323
}
281-
LM_DBG("Value: %s inserted into domain hash table\n",VAL_STRING(val));
282324

283-
if (hash_table_install(new_hash_table, &domain, &attrs)==-1){
325+
if (VAL_NULL(val + 2)) {
326+
accept_subdomain = 0;
327+
} else if (VAL_TYPE(val + 2) == DB_INT) {
328+
accept_subdomain = VAL_INT(val + 2);
329+
} else {
330+
LM_ERR("Database problem on accept_subdomain column\n");
331+
domain_dbf.free_result(db_handle, res);
332+
return -3;
333+
}
334+
335+
LM_DBG("Value: %s with accept_subdomain %d inserted into domain hash table\n", VAL_STRING(val), accept_subdomain);
336+
337+
if (hash_table_install(new_hash_table, &domain, &attrs, accept_subdomain)==-1){
284338
LM_ERR("Hash table problem\n");
285339
domain_dbf.free_result(db_handle, res);
286340
return -3;

modules/domain/domain_mod.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ static int mi_child_init(void);
7171
#define DOMAIN_ATTRS_COL "attrs"
7272
#define DOMAIN_ATTRS_COL_LEN (sizeof(DOMAIN_ATTRS_COL) - 1)
7373

74+
#define DOMAIN_ACCEPT_SUBDOMAIN_COL "accept_subdomain"
75+
#define DOMAIN_ACCEPT_SUBDOMAIN_COL_LEN (sizeof(DOMAIN_ACCEPT_SUBDOMAIN_COL) - 1)
76+
7477
/*
7578
* Module parameter variables
7679
*/
@@ -79,6 +82,7 @@ int db_mode = 0; /* Database usage mode: 0 = no cache, 1 = cache */
7982
str domain_table = {DOMAIN_TABLE, DOMAIN_TABLE_LEN}; /* Name of domain table */
8083
str domain_col = {DOMAIN_COL, DOMAIN_COL_LEN}; /* Name of domain column */
8184
str domain_attrs_col = {DOMAIN_ATTRS_COL, DOMAIN_ATTRS_COL_LEN}; /* Name of attributes column */
85+
str domain_accept_subdomain_col = {DOMAIN_ACCEPT_SUBDOMAIN_COL, DOMAIN_ATTRS_COL_LEN}; /* Name of accept_subdomain column */
8286

8387
/*
8488
* Other module variables
@@ -116,11 +120,12 @@ static const cmd_export_t cmds[] = {
116120
* Exported parameters
117121
*/
118122
static const param_export_t params[] = {
119-
{"db_url", STR_PARAM, &db_url.s },
120-
{"db_mode", INT_PARAM, &db_mode },
121-
{"domain_table", STR_PARAM, &domain_table.s},
122-
{"domain_col", STR_PARAM, &domain_col.s },
123-
{"attrs_col", STR_PARAM, &domain_attrs_col.s },
123+
{"db_url", STR_PARAM, &db_url.s },
124+
{"db_mode", INT_PARAM, &db_mode },
125+
{"domain_table", STR_PARAM, &domain_table.s},
126+
{"domain_col", STR_PARAM, &domain_col.s },
127+
{"attrs_col", STR_PARAM, &domain_attrs_col.s },
128+
{"accept_subdomain_col", STR_PARAM, &domain_accept_subdomain_col.s },
124129
{0, 0, 0}
125130
};
126131

@@ -189,6 +194,7 @@ static int mod_init(void)
189194
domain_table.len = strlen(domain_table.s);
190195
domain_col.len = strlen(domain_col.s);
191196
domain_attrs_col.len = strlen(domain_attrs_col.s);
197+
domain_accept_subdomain_col.len = strlen(domain_accept_subdomain_col.s);
192198

193199
/* Check if database module has been loaded */
194200
if (domain_db_bind(&db_url) < 0) return -1;

0 commit comments

Comments
 (0)