From b3ab841ca252248338823806f31ca105a048a783 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Thu, 23 Mar 2017 08:39:38 +1000 Subject: [PATCH] Forgot to include these files --- utils/magimail/src/magimail/broadcast.c | 47 +++++++++++++++++++++++++ utils/magimail/src/magimail/broadcast.h | 6 ++++ 2 files changed, 53 insertions(+) create mode 100644 utils/magimail/src/magimail/broadcast.c create mode 100644 utils/magimail/src/magimail/broadcast.h diff --git a/utils/magimail/src/magimail/broadcast.c b/utils/magimail/src/magimail/broadcast.c new file mode 100644 index 0000000..2e8e1f9 --- /dev/null +++ b/utils/magimail/src/magimail/broadcast.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include + +#include "magimail.h" +#include "broadcast.h" + +void broadcast(char *mess, ...) { + char json[1024]; + char buffer[512]; + struct sockaddr_in s; + int bcast_sock; + int broadcastEnable=1; + int ret; + + + + if (config.broadcastPort> 1024 && config.broadcastPort < 65536 && strlen(config.broadcastAddr) > 0) { + bcast_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + ret=setsockopt(bcast_sock, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable)); + + if (ret) { + close(bcast_sock); + return; + } + memset(&s, 0, sizeof(struct sockaddr_in)); + + s.sin_family=AF_INET; + s.sin_addr.s_addr = inet_addr(config.broadcastAddr); + s.sin_port = htons((unsigned short)config.broadcastPort); + bind(bcast_sock, (struct sockaddr *)&s, sizeof(struct sockaddr_in)); + + va_list ap; + va_start(ap, mess); + vsnprintf(buffer, 512, mess, ap); + va_end(ap); + + snprintf(json, 1024, "{\"System\": \"%s\", \"Program\": \"MagiMail\", \"Message\": \"%s\"}", config.cfg_bbsname, buffer); + + ret = sendto(bcast_sock, json, strlen(json) + 1, 0, (struct sockaddr *)&s, sizeof(struct sockaddr_in)); + + close(bcast_sock); + } +} \ No newline at end of file diff --git a/utils/magimail/src/magimail/broadcast.h b/utils/magimail/src/magimail/broadcast.h new file mode 100644 index 0000000..b722eb9 --- /dev/null +++ b/utils/magimail/src/magimail/broadcast.h @@ -0,0 +1,6 @@ +#ifndef __BROADCAST_H +#define __BROADCAST_H + +extern void broadcast(char *mess, ...); + +#endif