Skip to content

Commit

Permalink
+ 8ad5088 add 'rtpproxy_args' parameter to set some extra argv-style …
Browse files Browse the repository at this point in the history
…args.
  • Loading branch information
sobomax committed Oct 5, 2024
1 parent eaabad0 commit e5e1c06
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 40 deletions.
3 changes: 2 additions & 1 deletion modules/rtp.io/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ auto_gen=
NAME=rtp.io.so

DEFS+=-I$(LOCALBASE)/include
LIBS += -L$(LOCALBASE)/lib -lrtpproxy -lm -lssl -lcrypto `pkg-config --libs --static libsrtp2`
LIBS += -Wl,--whole-archive $(LOCALBASE)/lib/librtpproxy.a -Wl,--no-whole-archive \
-lm -lssl -lcrypto `pkg-config --libs --static libsrtp2`

include ../../Makefile.modules
78 changes: 56 additions & 22 deletions modules/rtp.io/rtp_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "rtp_io.h"
#include "rtp_io_util.h"
#include "rtp_io_params.h"

static int mod_init(void);
static int child_init(int rank);
Expand All @@ -49,10 +50,17 @@ static int rtp_io_getchildsock(int);
/*
* Exported functions
*/

static const cmd_export_t cmds[] = {
{"rtp_io_getchildsock", (cmd_function)rtp_io_getchildsock, {{0,0,0}}, 0},
{0,0,{{0,0,0}},0}
{"rtp_io_getchildsock", (cmd_function)rtp_io_getchildsock, {0}, 0},
{0}
};

/*
* Exported params
*/
static const param_export_t params[] = {
{"rtpproxy_args", STR_PARAM|USE_FUNC_PARAM, (void*)rio_set_rtpp_args},
{0}
};

struct module_exports exports= {
Expand All @@ -64,7 +72,7 @@ struct module_exports exports= {
&deps, /* OpenSIPS module dependencies */
cmds, /* exported functions */
0, /* exported async functions */
0, /* param exports */
params, /* param exports */
0, /* exported statistics */
0, /* exported MI functions */
NULL, /* exported pseudo-variables */
Expand All @@ -88,7 +96,7 @@ static struct opt_save {
int optopt;
int opterr;
int optreset;
} opt_save;
} opt_save = {.optind = 1};

#define OPT_SAVE() (opt_save = (struct opt_save){optarg, optind, optopt, opterr, optreset})
#define OPT_RESTORE() ({ \
Expand All @@ -101,7 +109,13 @@ static struct opt_save {

#define howmany(x, y) (sizeof(x) / sizeof(y))

static struct rtp_io_desc *rpi_descp;
static struct rtpp_env argv0 = {.cp = "rtpproxy"};

static struct rtp_io_desc rpi_desc = {
.env = {.len = 1, .first = &argv0, .last = &argv0},
};

struct rtp_io_desc *rpi_descp = &rpi_desc;

#define ENV_ADD(x, elabel, ...) { \
struct rtpp_env *_e = rtp_io_env_asprintf((x), ##__VA_ARGS__); \
Expand All @@ -110,17 +124,38 @@ static struct rtp_io_desc *rpi_descp;
rtp_io_env_append(&rpi_descp->env, _e); \
}

static int
rio_socks_init(int nsocks)
{
size_t asize;
struct rtp_io_socks *socks;

LM_DBG("allocating %s(%d)\n", exports.name, nsocks);

asize = sizeof(struct rtp_io_socks) + (nsocks * sizeof(int) * 2);
socks = malloc(asize);
if (socks == NULL)
goto e0;

memset(socks, '\0', asize);
rpi_descp->socks = socks;
return 0;
e1:
free(socks);
e0:
return -1;
}

static int mod_init(void)
{
int nsocks;
size_t asize;
const char * const argv_stat[] = {
"rtpproxy",
"-d", "err",
"-n", "tcp:127.0.0.1:9642",
"--dso", "acct_csv",
"--dso", "catch_dtmf",
"--dso", "dtls_gw",
"--dso", "ice_lite",
};

nsocks = count_child_processes();
Expand All @@ -130,19 +165,15 @@ static int mod_init(void)

LM_DBG("initializing %s(%d)\n", exports.name, nsocks);

asize = sizeof(struct rtp_io_desc) + (nsocks * sizeof(int) * 2);
rpi_descp = malloc(asize);
if (rpi_descp == NULL)
if (rio_socks_init(nsocks) != 0)
goto e0;

memset(rpi_descp, '\0', asize);

for (int i = 0; i < howmany(argv_stat, *argv_stat); i++) {
ENV_ADD(argv_stat[i], e1);
}

for (int i = 0; i < nsocks; i++) {
int *fdp = &rpi_descp->rtpp_socks_holder[i * 2];
int *fdp = &rpi_descp->socks->holder[i * 2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdp) < 0)
goto e1;
ENV_ADD("-s", e1);
Expand All @@ -161,7 +192,7 @@ static int mod_init(void)
if (rpi_descp->rtpp_cfsp == NULL)
goto e1;

rpi_descp->nsocks = nsocks;
rpi_descp->socks->n = nsocks;

return 0;
e1:
Expand All @@ -176,10 +207,13 @@ void mod_destroy(void)

LM_DBG("cleaning up %s...\n", exports.name);
rtpp_shutdown(rpi_descp->rtpp_cfsp);
for (struct rtpp_env *e = rpi_descp->env.first; e != NULL; e = enext) {
for (const struct rtpp_env *e = rpi_descp->env.first; e != NULL; e = enext) {
enext = e->next;
free(e->cp);
free(e);
if (e->atype == env_heap)
free(e->_cp);
if (e == &argv0)
continue;
free((void *)e);
}
free(rpi_descp);
}
Expand All @@ -192,8 +226,8 @@ child_init(int rank)

assert(rank >= 0);

for (int i = 0; i < rpi_descp->nsocks; i++) {
int *fdp = &rpi_descp->rtpp_socks_holder[i * 2];
for (int i = 0; i < rpi_descp->socks->n; i++) {
int *fdp = &rpi_descp->socks->holder[i * 2];
if (close(fdp[0]) < 0)
return (-1);
fdp[0] = -1;
Expand All @@ -210,9 +244,9 @@ child_init(int rank)
static int rtp_io_getchildsock(int rank)
{

if (rank < 1 || rank > rpi_descp->nsocks)
if (rank < 1 || rank > rpi_descp->socks->n)
return (-1);

int *fdp = &rpi_descp->rtpp_socks_holder[(rank - 1) * 2];
int *fdp = &rpi_descp->socks->holder[(rank - 1) * 2];
return (fdp[1]);
}
23 changes: 18 additions & 5 deletions modules/rtp.io/rtp_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,32 @@
struct rtpp_cfg;

struct rtpp_env {
char *cp;
union {
const char *cp;
char *_cp;
};
enum {env_heap, env_static=0} atype;
struct rtpp_env *next;
};

struct rtpp_env_hd {
int len;
struct rtpp_env *first;
struct rtpp_env *last;
const struct rtpp_env *first;
union {
const struct rtpp_env *last;
struct rtpp_env *_last;
};
};

struct rtp_io_socks {
int n;
int holder[];
};

struct rtp_io_desc {
int nsocks;
struct rtpp_cfg *rtpp_cfsp;
struct rtpp_env_hd env;
int rtpp_socks_holder[];
struct rtp_io_socks *socks;
};

extern struct rtp_io_desc *rpi_descp;
38 changes: 38 additions & 0 deletions modules/rtp.io/rtp_io_params.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <ctype.h>
#include <string.h>

#include "../../sr_module.h"

#include "rtp_io.h"
#include "rtp_io_util.h"
#include "rtp_io_params.h"

int
rio_set_rtpp_args(modparam_t type, void *val)
{
char * p;

p = (char *)val;


if (p == NULL || *p == '\0') {
return 0;
}
do {
char *ep = strchr(p, ' ');
if (ep != NULL) {
*ep = '\0';
}
struct rtpp_env *rep = rtp_io_env_strref(p);
if (rep == NULL) {
return -1;
}
rtp_io_env_append(&rpi_descp->env, rep);
if (ep == NULL)
break;
p = ep + 1;
while (isspace(*p))
p++;
} while (*p != '\0');
return 0;
}
3 changes: 3 additions & 0 deletions modules/rtp.io/rtp_io_params.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

int rio_set_rtpp_args(modparam_t, void *);
37 changes: 26 additions & 11 deletions modules/rtp.io/rtp_io_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,36 @@ struct rtpp_env *
rtp_io_env_asprintf(const char *format, ...)
{
va_list ap;
struct rtpp_env *rep;
int rc;
char *cp;

va_start(ap, format);
rc = vasprintf(&cp, format, ap);
va_end(ap);
if (rc < 0)
goto e0;
struct rtpp_env *rep = rtp_io_env_strref(cp);
if (rep != NULL) {
rep->atype = env_heap;
} else {
free(cp);
}
return rep;
e0:
return (NULL);
}

struct rtpp_env *
rtp_io_env_strref(const char *cp)
{
struct rtpp_env *rep;

rep = malloc(sizeof(struct rtpp_env));
if (rep == NULL)
goto e0;
memset(rep, '\0', sizeof(struct rtpp_env));
va_start(ap, format);
rc = vasprintf(&rep->cp, format, ap);
va_end(ap);
if (rc < 0)
goto e1;
rep->cp = cp;
return (rep);
e1:
free(rep);
e0:
return (NULL);
}
Expand All @@ -36,7 +51,7 @@ rtp_io_env_append(struct rtpp_env_hd *ecp, struct rtpp_env *ep)
if (ecp->first == NULL) {
ecp->last = ecp->first = ep;
} else {
ecp->last->next = ep;
ecp->_last->next = ep;
ecp->last = ep;
}
ecp->len += 1;
Expand All @@ -45,8 +60,8 @@ rtp_io_env_append(struct rtpp_env_hd *ecp, struct rtpp_env *ep)
const char *const *
rtp_io_env_gen_argv(struct rtpp_env_hd *ecp, int *lenp)
{
char **rval;
struct rtpp_env *ep;
const char **rval;
const struct rtpp_env *ep;

size_t asize = ecp->len * sizeof(rval[0]);
rval = malloc(asize);
Expand Down
2 changes: 1 addition & 1 deletion modules/rtp.io/rtp_io_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct rtpp_env;
struct rtpp_env_hd;

struct rtpp_env *rtp_io_env_asprintf(const char *, ...);
struct rtpp_env *rtp_io_env_strref(const char *);

void rtp_io_env_append(struct rtpp_env_hd *, struct rtpp_env *);
const char *const * rtp_io_env_gen_argv(struct rtpp_env_hd *, int *);

0 comments on commit e5e1c06

Please sign in to comment.