From c8f612471c22aa4e60bd8b497c632540bc16bd4d Mon Sep 17 00:00:00 2001 From: Christian Spielberger Date: Fri, 13 Sep 2024 10:01:18 +0200 Subject: [PATCH] mc: move DnD feature to multicast and fix build (#55) --- modules/multicast/multicast.c | 93 ++++++++++++++++++++++++++--------- modules/multicast/multicast.h | 1 + modules/multicast/receiver.c | 4 +- 3 files changed, 74 insertions(+), 24 deletions(-) diff --git a/modules/multicast/multicast.c b/modules/multicast/multicast.c index ff755d7..4974b9d 100644 --- a/modules/multicast/multicast.c +++ b/modules/multicast/multicast.c @@ -28,13 +28,20 @@ struct mccfg { char iface[128]; }; -static struct mccfg mccfg = { - 0, - 1, - 125, - "" +struct mc { + bool dnd; + struct mccfg cfg; }; +static struct mc mc = { + false, + { + 0, + 1, + 125, + "", + } +}; /** * Decode IP-address : @@ -117,7 +124,18 @@ static int check_rtp_pt(struct aucodec *ac) */ uint8_t multicast_callprio(void) { - return mccfg.callprio; + return mc.cfg.callprio; +} + + +/** + * Set the do not disturb flag + * + * @param v true if incoming calls should be rejected + */ +void multicast_set_dnd(bool v) +{ + mc.dnd = v; } @@ -128,7 +146,7 @@ uint8_t multicast_callprio(void) */ uint8_t multicast_ttl(void) { - return mccfg.ttl; + return mc.cfg.ttl; } @@ -139,7 +157,7 @@ uint8_t multicast_ttl(void) */ uint32_t multicast_fade_time(void) { - return mccfg.tfade; + return mc.cfg.tfade; } @@ -317,12 +335,12 @@ static int cmd_mcreg(struct re_printf *pf, void *arg) } #ifdef HAVE_GETIFADDRS - if (str_isset(mccfg.iface)) { + if (str_isset(mc.cfg.iface)) { unsigned int if_index; - if_index = if_nametoindex(mccfg.iface); + if_index = if_nametoindex(mc.cfg.iface); if (!if_index) { warning("multicast: could not find interface %s\n", - &mccfg.iface); + &mc.cfg.iface); if (sa_af(&addr) == AF_INET6) { err = EINVAL; goto out; @@ -658,20 +676,21 @@ static int module_read_config(void) int err = 0, prio = 1; struct sa laddr; - (void)conf_get_u32(conf_cur(), "multicast_call_prio", &mccfg.callprio); - if (mccfg.callprio > 255) - mccfg.callprio = 255; + (void)conf_get_u32(conf_cur(), "multicast_call_prio", + &mc.cfg.callprio); + if (mc.cfg.callprio > 255) + mc.cfg.callprio = 255; - (void)conf_get_u32(conf_cur(), "multicast_ttl", &mccfg.ttl); - if (mccfg.ttl > 255) - mccfg.ttl = 255; + (void)conf_get_u32(conf_cur(), "multicast_ttl", &mc.cfg.ttl); + if (mc.cfg.ttl > 255) + mc.cfg.ttl = 255; - (void)conf_get_u32(conf_cur(), "multicast_fade_time", &mccfg.tfade); - if (mccfg.tfade > 2000) - mccfg.tfade = 2000; + (void)conf_get_u32(conf_cur(), "multicast_fade_time", &mc.cfg.tfade); + if (mc.cfg.tfade > 2000) + mc.cfg.tfade = 2000; - (void)conf_get_str(conf_cur(), "multicast_iface", mccfg.iface, - sizeof(mccfg.iface)); + (void)conf_get_str(conf_cur(), "multicast_iface", mc.cfg.iface, + sizeof(mc.cfg.iface)); sa_init(&laddr, AF_INET); err = conf_apply(conf_cur(), "multicast_listener", @@ -705,12 +724,41 @@ static const struct cmd cmdv[] = { }; +static void event_handler(enum ua_event ev, struct bevent *event, void *arg) +{ + (void)arg; + const struct sip_msg *msg = bevent_get_msg(event); + + switch (ev) { + + case UA_EVENT_SIPSESS_CONN: + if (mc.dnd) { + (void)sip_treply(NULL, uag_sip(), msg, 480, + "Temporarily Unavailable"); + info("menu: incoming call from %r <%r> rejected: " + "480 Temporarily Unavailable\n", + &msg->from.dname, &msg->from.auri); + bevent_stop(event); + break; + } + + break; + + default: + break; + } +} + + static int module_init(void) { int err = 0; err = module_read_config(); err |= cmd_register(baresip_commands(), cmdv, RE_ARRAY_SIZE(cmdv)); + err |= bevent_register(event_handler, NULL); + if (err) + return err; err |= mcsource_init(); err |= mcplayer_init(); @@ -727,6 +775,7 @@ static int module_close(void) mcsender_stopall(); mcreceiver_unregall(); + bevent_unregister(event_handler); cmd_unregister(baresip_commands(), cmdv); mcsource_terminate(); diff --git a/modules/multicast/multicast.h b/modules/multicast/multicast.h index eaf4119..2a054a1 100644 --- a/modules/multicast/multicast.h +++ b/modules/multicast/multicast.h @@ -21,6 +21,7 @@ enum { uint8_t multicast_callprio(void); uint8_t multicast_ttl(void); uint32_t multicast_fade_time(void); +void multicast_set_dnd(bool v); /* Sender */ diff --git a/modules/multicast/receiver.c b/modules/multicast/receiver.c index e46cb95..4336241 100644 --- a/modules/multicast/receiver.c +++ b/modules/multicast/receiver.c @@ -195,7 +195,7 @@ static void resume_uag_state(void) } if (h > multicast_callprio()) { - uag_set_dnd(false); + multicast_set_dnd(false); uag_set_nodial(false); uag_hold_resume(NULL); } @@ -288,7 +288,7 @@ static int prio_handling(struct mcreceiver *mcreceiver, uint32_t ssrc) struct le *leua; struct ua *ua; - uag_set_dnd(true); + multicast_set_dnd(true); uag_set_nodial(true); for (leua = list_head(uag_list()); leua; leua = leua->next) {