From e769e5448fb387dd9cb4de80a1eff69fa26670e6 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Thu, 8 Dec 2016 22:17:19 +1000 Subject: [PATCH] New broadcast ability, to send notifications of log in to clients on network --- bbs.c | 51 +++++++++++++++++++++++++++++++++++++----- bbs.h | 2 ++ config_default/bbs.ini | 2 ++ main.c | 9 ++++++++ 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/bbs.c b/bbs.c index ef90eba..43b1190 100644 --- a/bbs.c +++ b/bbs.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "bbs.h" #include "lua/lua.h" #include "lua/lualib.h" @@ -41,6 +42,44 @@ void sigint_handler(int s) { // do nothing... } +void broadcast(char *mess, ...) { + char buffer[512]; + struct sockaddr_in s; + int bcast_sock; + int broadcastEnable=1; + int ret; + + + + if (conf.broadcast_enable) { + bcast_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + ret=setsockopt(bcast_sock, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable)); + + if (ret) { + dolog("broadcast: Couldn't set socket to broadcast mode"); + close(bcast_sock); + return; + } + memset(&s, 0, sizeof(struct sockaddr_in)); + + s.sin_family=AF_INET; + s.sin_addr.s_addr = htonl(INADDR_ANY); + s.sin_port = htons((unsigned short)conf.broadcast_port); + 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); + + ret = sendto(bcast_sock, buffer, strlen(buffer) + 1, 0, (struct sockaddr *)&s, sizeof(struct sockaddr_in)); + + if (ret < 0) { + dolog("broadcast: Couldn't send broadcast"); + } + close(bcast_sock); + } +} void dolog(char *fmt, ...) { char buffer[512]; @@ -61,12 +100,12 @@ void dolog(char *fmt, ...) { dolog("Error opening log file!"); return; } - va_list ap; - va_start(ap, fmt); - vsnprintf(buffer, 512, fmt, ap); - va_end(ap); + va_list ap; + va_start(ap, fmt); + vsnprintf(buffer, 512, fmt, ap); + va_end(ap); - fprintf(logfptr, "%02d:%02d:%02d [%d][%s] %s\n", time_now.tm_hour, time_now.tm_min, time_now.tm_sec, mypid, ipaddress, buffer); + fprintf(logfptr, "%02d:%02d:%02d [%d][%s] %s\n", time_now.tm_hour, time_now.tm_min, time_now.tm_sec, mypid, ipaddress, buffer); fclose(logfptr); } @@ -680,6 +719,7 @@ tryagain: // do post-login dolog("%s logged in, on node %d", user->loginname, mynode); + broadcast("%s: %s logged in, on node %d", conf.bbs_name, user->loginname, mynode); // check time left now = time(NULL); localtime_r(&now, &thetime); @@ -749,6 +789,7 @@ tryagain: dolog("%s is logging out, on node %d", user->loginname, mynode); + broadcast("%s: %s is logging out, on node %d", conf.bbs_name, user->loginname, mynode); disconnect("Log out"); } diff --git a/bbs.h b/bbs.h index 7af09eb..f00c236 100644 --- a/bbs.h +++ b/bbs.h @@ -127,6 +127,8 @@ struct bbs_config { int nodes; int newuserlvl; int automsgwritelvl; + int broadcast_enable; + int broadcast_port; int mail_conference_count; struct mail_conference **mail_conferences; int door_count; diff --git a/config_default/bbs.ini b/config_default/bbs.ini index 775c1da..9e1aef3 100644 --- a/config_default/bbs.ini +++ b/config_default/bbs.ini @@ -24,6 +24,8 @@ QWK Max Messages = 5000 ZIP Command = zip -j *a *f UNZIP Command = unzip -j -o *a -d *d Archivers = config/archivers.ini +Broadcast Enable = false +Broadcast Port = 2027 [paths] WWW Path = /home/andrew/MagickaBBS/www diff --git a/main.c b/main.c index 6a3b296..0638b6d 100644 --- a/main.c +++ b/main.c @@ -379,6 +379,14 @@ static int handler(void* user, const char* section, const char* name, conf->bwave_max_msgs = atoi(value); } else if (strcasecmp(name, "archivers") == 0) { conf->archiver_path = strdup(value); + } else if (strcasecmp(name, "broadcast enable") == 0) { + if (strcasecmp(value, "true") == 0) { + conf->broadcast_enable = 1; + } else { + conf->broadcast_enable = 0; + } + } else if (strcasecmp(name, "broadcast port") == 0) { + conf->broadcast_port = atoi(value); } } else if (strcasecmp(section, "paths") == 0){ if (strcasecmp(name, "ansi path") == 0) { @@ -842,6 +850,7 @@ int main(int argc, char **argv) { conf.www_path = NULL; conf.archiver_path = NULL; conf.archiver_count = 0; + conf.broadcast_enable = 0; // Load BBS data if (ini_parse(argv[1], handler, &conf) <0) {