Explorar o código

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
Daniel-Constantin Mierla hai 1 ano
pai
achega
6cb26e9e70
Modificáronse 3 ficheiros con 41 adicións e 4 borrados
  1. 13 0
      src/core/cfg.y
  2. 1 0
      src/core/ip_addr.h
  3. 27 4
      src/core/socket_info.c

+ 13 - 0
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;

+ 1 - 0
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;

+ 27 - 4
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