Module: kamailio
Branch: master
Commit: 6cb26e9e7045eeec9c08867fe264c71c697bb9d8
URL:
https://github.com/kamailio/kamailio/commit/6cb26e9e7045eeec9c08867fe264c71…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2024-05-06T18:40:41+02:00
core: cfg socket struct - option to bind to a range of sockets
- bind can be: proto:addr:portstart-portend
- bind=udp:127.0.0.1:5060-5080
- start and end ports are inclusive
---
Modified: src/core/cfg.y
Modified: src/core/ip_addr.h
Modified: src/core/socket_info.c
---
Diff:
https://github.com/kamailio/kamailio/commit/6cb26e9e7045eeec9c08867fe264c71…
Patch:
https://github.com/kamailio/kamailio/commit/6cb26e9e7045eeec9c08867fe264c71…
---
diff --git a/src/core/cfg.y b/src/core/cfg.y
index eb0ecaf2087..e026b1b8b9a 100644
--- a/src/core/cfg.y
+++ b/src/core/cfg.y
@@ -840,6 +840,19 @@ socket_lattr:
tmp_sa.bindaddr.len = strlen(tmp_sa.bindaddr.s);
tmp_sa.bindport = $5;
}
+ | BIND EQUAL proto COLON listen_id COLON port MINUS port {
+ tmp_sa.bindproto = $3;
+ tmp_sa.bindaddr.s = $5;
+ tmp_sa.bindaddr.len = strlen(tmp_sa.bindaddr.s);
+ tmp_sa.bindport = $7;
+ tmp_sa.bindportend = $9;
+ }
+ | BIND EQUAL listen_id COLON port MINUS port {
+ tmp_sa.bindaddr.s = $3;
+ tmp_sa.bindaddr.len = strlen(tmp_sa.bindaddr.s);
+ tmp_sa.bindport = $5;
+ tmp_sa.bindportend = $7;
+ }
| BIND EQUAL proto COLON listen_id {
tmp_sa.bindproto = $3;
tmp_sa.bindaddr.s = $5;
diff --git a/src/core/ip_addr.h b/src/core/ip_addr.h
index a8f9660b9c6..e475986d1a2 100644
--- a/src/core/ip_addr.h
+++ b/src/core/ip_addr.h
@@ -163,6 +163,7 @@ typedef struct socket_attrs
int bindproto;
str bindaddr;
int bindport;
+ int bindportend;
int useproto;
str useaddr;
int useport;
diff --git a/src/core/socket_info.c b/src/core/socket_info.c
index ab59df73dd6..fe7d04636e6 100644
--- a/src/core/socket_info.c
+++ b/src/core/socket_info.c
@@ -1255,6 +1255,9 @@ int add_listen_socket(socket_attrs_t *sa)
{
socket_info_t *newsi;
name_lst_t addr_l;
+ char sname[128];
+ char *psname = NULL;
+ int i;
if(sa->bindaddr.s == NULL) {
LM_ERR("no bind address provided\n");
@@ -1263,11 +1266,31 @@ int add_listen_socket(socket_attrs_t *sa)
memset(&addr_l, 0, sizeof(name_lst_t));
addr_l.name = sa->bindaddr.s;
- newsi = add_listen_socket_info(sa->bindaddr.s, &addr_l, sa->bindport,
- sa->bindproto, sa->useproto, sa->useaddr.s, sa->useport,
- sa->sockname.s, sa->sflags);
+ /* binding on single port */
+ if(sa->bindportend <= sa->bindport) {
+ newsi = add_listen_socket_info(sa->bindaddr.s, &addr_l, sa->bindport,
+ sa->bindproto, sa->useproto, sa->useaddr.s, sa->useport,
+ sa->sockname.s, sa->sflags);
+ return (newsi != NULL) ? 0 : -1;
+ }
- return (newsi != NULL) ? 0 : -1;
+ /* binding on a range of ports */
+ for(i = 0; sa->bindport + i <= sa->bindportend; i++) {
+ if(sa->sockname.s != NULL) {
+ /* socketname gets port appended */
+ snprintf(sname, 128, "%s%d", sa->sockname.s, sa->bindport + i);
+ psname = sname;
+ } else {
+ psname = NULL;
+ }
+ newsi = add_listen_socket_info(sa->bindaddr.s, &addr_l,
+ sa->bindport + i, sa->bindproto, sa->useproto, sa->useaddr.s,
+ sa->useport + i, psname, sa->sflags);
+ if(newsi == NULL) {
+ return -1;
+ }
+ }
+ return 0;
}
#ifdef __OS_linux