Skip to content

Commit 2a1cf22

Browse files
authored
Merge pull request #3560 from purecloudlabs/TELECOM-10654_master
permissions: Rewrite the in-memory storage backend
2 parents b868e00 + 8a98fbe commit 2a1cf22

File tree

9 files changed

+785
-794
lines changed

9 files changed

+785
-794
lines changed

modules/permissions/address.c

Lines changed: 20 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ int reload_address_table(struct pm_part_struct *part_struct)
121121
db_row_t* row;
122122
db_val_t* val;
123123

124-
struct address_list **new_hash_table;
125-
struct subnet *new_subnet_table;
124+
p_address_table_t *new_hash_table;
126125
int i, mask, proto, group, port, id;
127126
struct ip_addr *ip_addr;
128127
struct net *subnet;
@@ -160,15 +159,6 @@ int reload_address_table(struct pm_part_struct *part_struct)
160159
new_hash_table = part_struct->hash_table_1;
161160
}
162161

163-
/* Choose new subnet table */
164-
if (*part_struct->subnet_table == part_struct->subnet_table_1) {
165-
empty_subnet_table(part_struct->subnet_table_2);
166-
new_subnet_table = part_struct->subnet_table_2;
167-
} else {
168-
empty_subnet_table(part_struct->subnet_table_1);
169-
new_subnet_table = part_struct->subnet_table_1;
170-
}
171-
172162
row = RES_ROWS(res);
173163
LM_DBG("number of rows in address table: %d\n", RES_ROW_N(res));
174164

@@ -295,40 +285,27 @@ int reload_address_table(struct pm_part_struct *part_struct)
295285
port = (unsigned int) VAL_INT(val + 3);
296286
mask = (unsigned int) VAL_INT(val + 2);
297287

298-
if ( (mask == 32 && ip_addr->af==AF_INET) ||
299-
(mask == 128 && ip_addr->af==AF_INET6) ) {
300-
if (pm_hash_insert(new_hash_table, ip_addr, group, port, proto,
301-
&str_pattern, &str_info) == -1) {
302-
LM_ERR("hash table insert error\n");
303-
goto error;
304-
}
305-
LM_DBG("Tuple <%.*s, %u, %u, %u, %.*s, %.*s> inserted into "
306-
"address hash table\n", str_src_ip.len, str_src_ip.s,
307-
group, port, proto, str_pattern.len, str_pattern.s,
308-
str_info.len,str_info.s);
309-
} else {
310-
subnet = mk_net_bitlen(ip_addr, mask);
311-
if (subnet_table_insert(new_subnet_table, group, subnet,
312-
port, proto, &str_pattern, &str_info) == -1) {
313-
LM_ERR("subnet table problem\n");
314-
if (subnet) {
315-
pkg_free(subnet);
316-
}
317-
goto error;
288+
subnet = mk_net_bitlen(ip_addr, mask);
289+
if (pm_hash_insert(new_hash_table, subnet, group, port, proto,
290+
&str_pattern, &str_info, mask) == -1) {
291+
LM_ERR("hash table insert error\n");
292+
if (subnet) {
293+
pkg_free(subnet);
318294
}
319-
LM_DBG("Tuple <%.*s, %u, %u, %u> inserted into subnet table\n",
320-
str_src_ip.len, str_src_ip.s, group, mask, port);
321-
/* subnet in pkg; needs to be freed since was copied to shm */
322-
if (subnet) {
323-
pkg_free(subnet);
324-
}
295+
goto error;
296+
}
297+
LM_DBG("Tuple <%.*s, %u, %u, %u, %.*s, %.*s> inserted into "
298+
"address hash table\n", str_src_ip.len, str_src_ip.s,
299+
group, port, proto, str_pattern.len, str_pattern.s,
300+
str_info.len,str_info.s);
301+
if (subnet) {
302+
pkg_free(subnet);
325303
}
326304
}
327305

328306
part_struct->perm_dbf.free_result(part_struct->db_handle, res);
329307

330308
*part_struct->hash_table = new_hash_table;
331-
*part_struct->subnet_table = new_subnet_table;
332309
LM_DBG("address table reloaded successfully.\n");
333310

334311
return 1;
@@ -395,23 +372,11 @@ int init_address_part(struct pm_partition *partition)
395372
part_struct->hash_table_2 = pm_hash_create();
396373
if (!part_struct->hash_table_2) goto error;
397374

398-
part_struct->hash_table = (struct address_list ***)shm_malloc
399-
(sizeof(struct address_list **));
375+
part_struct->hash_table = (p_address_table_t **)shm_malloc(sizeof(p_address_table_t*));
400376
if (!part_struct->hash_table) goto error;
401377

402378
*part_struct->hash_table = part_struct->hash_table_1;
403379

404-
part_struct->subnet_table_1 = new_subnet_table();
405-
if (!part_struct->subnet_table_1) goto error;
406-
407-
part_struct->subnet_table_2 = new_subnet_table();
408-
if (!part_struct->subnet_table_2) goto error;
409-
410-
part_struct->subnet_table = (struct subnet **)shm_malloc(sizeof(struct subnet *));
411-
if (!part_struct->subnet_table) goto error;
412-
413-
*part_struct->subnet_table = part_struct->subnet_table_1;
414-
415380
if (reload_address_table(part_struct) == -1) {
416381
LM_CRIT("reload of address table failed\n");
417382
goto error;
@@ -438,19 +403,6 @@ int init_address_part(struct pm_partition *partition)
438403
part_struct->hash_table = 0;
439404
}
440405

441-
if (part_struct->subnet_table_1) {
442-
free_subnet_table(part_struct->subnet_table_1);
443-
part_struct->subnet_table_1 = 0;
444-
}
445-
446-
if (part_struct->subnet_table_2) {
447-
free_subnet_table(part_struct->subnet_table_2);
448-
part_struct->subnet_table_2 = 0;
449-
}
450-
if (part_struct->subnet_table) {
451-
shm_free(part_struct->subnet_table);
452-
part_struct->subnet_table = 0;
453-
}
454406
part_struct->perm_dbf.close(part_struct->db_handle);
455407
part_struct->db_handle = 0;
456408

@@ -499,7 +451,6 @@ int check_addr(struct sip_msg* msg, int* grp, str* s_ip, int *port, long proto,
499451
pv_spec_t *info, char *pattern, struct pm_part_struct *part)
500452
{
501453
struct ip_addr *ip;
502-
int hash_ret, subnet_ret;
503454

504455
/* ip addr */
505456
if ( (ip=str2ip(s_ip))==NULL && (ip=str2ip6(s_ip))==NULL ) {
@@ -511,23 +462,15 @@ int check_addr(struct sip_msg* msg, int* grp, str* s_ip, int *port, long proto,
511462
part->name.len, part->name.s, *grp,
512463
s_ip->len, s_ip->s, (int)proto, *port, ZSW(pattern) );
513464

514-
hash_ret = pm_hash_match(msg, *part->hash_table, *grp,
465+
return pm_hash_match(msg, *part->hash_table, *grp,
515466
ip, *port, (int)proto, pattern, info);
516-
if (hash_ret < 0) {
517-
subnet_ret = match_subnet_table(msg, *part->subnet_table, *grp,
518-
ip, *port, (int)proto, pattern, info);
519-
hash_ret = (hash_ret > subnet_ret) ? hash_ret : subnet_ret;
520-
}
521-
522-
return hash_ret;
523467
}
524468

525469

526470
int check_src_addr(struct sip_msg *msg, int *grp,
527471
pv_spec_t *info, char* pattern, struct pm_part_struct *part)
528472
{
529473

530-
int hash_ret, subnet_ret;
531474
struct ip_addr *ip;
532475

533476
ip = &msg->rcv.src_ip;
@@ -536,15 +479,8 @@ int check_src_addr(struct sip_msg *msg, int *grp,
536479
part->name.len, part->name.s, *grp,
537480
ip_addr2a(ip), msg->rcv.proto, msg->rcv.src_port, ZSW(pattern) );
538481

539-
hash_ret = pm_hash_match(msg, *part->hash_table, *grp, ip,
482+
return pm_hash_match(msg, *part->hash_table, *grp, ip,
540483
msg->rcv.src_port, msg->rcv.proto, pattern, info);
541-
if (hash_ret < 0) {
542-
subnet_ret = match_subnet_table(msg, *part->subnet_table,
543-
*grp, ip, msg->rcv.src_port, msg->rcv.proto, pattern,info);
544-
hash_ret = (hash_ret > subnet_ret) ? hash_ret : subnet_ret;
545-
}
546-
547-
return hash_ret;
548484
}
549485

550486

@@ -559,21 +495,9 @@ int get_source_group(struct sip_msg* msg, pv_spec_t *out_var,
559495
LM_DBG("Looking for <%s, %u> in address table\n",
560496
ip_addr2a(ip), msg->rcv.src_port);
561497

562-
group = find_group_in_hash_table(*part->hash_table,
498+
group = pm_hash_find_group(*part->hash_table,
563499
ip, msg->rcv.src_port);
564-
if (group == -1) {
565-
566-
LM_DBG("Looking for <%x, %u> in subnet table\n",
567-
msg->rcv.src_ip.u.addr32[0], msg->rcv.src_port);
568-
569-
group = find_group_in_subnet_table(*part->subnet_table,
570-
ip, msg->rcv.src_port);
571-
if (group == -1) {
572-
LM_DBG("IP <%s:%u> not found in any group\n",
573-
ip_addr2a(ip), msg->rcv.src_port);
574-
return -1;
575-
}
576-
}
500+
if (group == -1) return -1;
577501
LM_DBG("Found <%d>\n", group);
578502

579503
pvt.flags = PV_VAL_INT|PV_TYPE_INT;

0 commit comments

Comments
 (0)