Skip to content

Commit

Permalink
Unifying code
Browse files Browse the repository at this point in the history
  • Loading branch information
Uglymotha committed Jul 21, 2024
1 parent 56b19c2 commit 2560322
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 177 deletions.
28 changes: 11 additions & 17 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,10 @@ int openCliFd(void) {
cli_sa.sun_family = AF_UNIX;

// Open the socket, set permissions and mode.
if ( ! strcpy(cli_sa.sun_path, CONF->runPath)
#ifdef __linux__
if (cli_fd == -1) {
if ( ! strcpy(cli_sa.sun_path, CONF->runPath)
|| ! snprintf(cli_sa.sun_path + strlen(cli_sa.sun_path), PATH_MAX - strlen(cli_sa.sun_path),
mrt_tbl ? "cli-%d.sock" : "cli.sock", mrt_tbl)
#else
|| ! strcat(cli_sa.sun_path, "cli.sock")
#endif
|| (stat(cli_sa.sun_path, &st) == 0 && unlink(cli_sa.sun_path) < 0)
|| ! (cli_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0))
#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
Expand All @@ -74,11 +71,11 @@ int openCliFd(void) {
|| listen(cli_fd, CONF->reqQsz) < 0
|| ( chown(cli_sa.sun_path, CONF->user ? CONF->user->pw_uid : -1, CONF->group->gr_gid))
|| chmod(cli_sa.sun_path, 0660)) {
LOG(LOG_WARNING, errno, "Cannot open CLI Socket %s. CLI connections will not be available.", cli_sa.sun_path);
cli_fd = -1;
} else
LOG(LOG_INFO, 0, "openCliFd: Opened CLI socket %s.", cli_sa.sun_path);

LOG(LOG_WARNING, errno, "Cannot open CLI Socket %s. CLI connections will not be available.", cli_sa.sun_path);
cli_fd = -1;
} else
LOG(LOG_INFO, 0, "openCliFd: Opened CLI socket %s.", cli_sa.sun_path);
}
return cli_fd;
}

Expand Down Expand Up @@ -106,7 +103,7 @@ int closeCliFd(void) {
*/
void acceptCli(void)
{
int pid = 0, fd = -1, len = 0, s = sizeof(struct sockaddr);
int pid = -1, fd = -1, len = 0, s = sizeof(struct sockaddr);
uint32_t addr = (uint32_t)-1, mask = (uint32_t)-1;
char buf[CLI_CMD_BUF] = {0}, msg[CLI_CMD_BUF];
struct sockaddr cli_sa;
Expand All @@ -125,10 +122,8 @@ void acceptCli(void)
sighandled |= buf[0] == 'c' ? GOT_SIGUSR1 : GOT_SIGUSR2;
buf[0] == 'c' ? send(fd, "Reloading Configuration.\n", 26, MSG_DONTWAIT)
: send(fd, "Rebuilding Interfaces.\n", 24, MSG_DONTWAIT);
#ifdef __linux__
IF_FOR_IF(mrt_tbl < 0, int i = 0; i < chld.nr; i++, chld.c[i].pid > 0)
IF_FOR_IF(mrt_tbl < 0 && chld.nr, int i = 0; i < chld.nr; i++, chld.c[i].pid > 0)
kill(chld.c[i].pid, buf[0] == 'c' ? SIGUSR1 : SIGUSR2);
#endif
} else if ((pid = fork()) != 0)
pid < 0 ? LOG(LOG_WARNING, eNOFORK, "Cannot fork().") : LOG(LOG_DEBUG, 0, "acceptCli: Forked PID: %d", pid);
if (pid != 0 || buf[0] == 'c' || buf[0] == 'b') {
Expand All @@ -150,9 +145,9 @@ void acceptCli(void)
debugQueue("", len > 1 && buf[1] == 'h' ? 0 : 1, fd);
} else if (buf[0] == 'm') {
getMemStats(buf[1] == 'h' ? 0 : 1, fd);
#ifdef __linux__
} else if (buf[0] == 'p' && mrt_tbl < 0) {
send(fd, "Currently Running Processes:\n", 29, MSG_DONTWAIT);
sprintf(msg, "Monitor PID: %d\n", getppid());
send(fd, msg, strlen(msg), MSG_DONTWAIT);
for (int i = 0; i < chld.nr; i++) {
if (chld.c[i].pid > 0)
sprintf(msg, "Table: %d - PID: %d\n", chld.c[i].tbl, chld.c[i].pid);
Expand All @@ -163,7 +158,6 @@ void acceptCli(void)
} else if (buf[0] == 'p' && mrt_tbl >= 0) {
sprintf(msg, "Table: %d - PID: %d\n", mrt_tbl, getppid());
send(fd, msg, strlen(msg), MSG_DONTWAIT);
#endif
} else
send(fd, "GO AWAY\n", 9, MSG_DONTWAIT);

Expand Down
46 changes: 20 additions & 26 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,6 @@ static inline bool parsePhyintToken(char *token) {
else {
tmpPtr->tbl = intToken;
LOG(LOG_INFO, 0, "Config (%s): Assigning to table %d.", tmpPtr->name, intToken);
if (intToken > 0 && mrt_tbl < 0)
igmpProxyFork(0);
if (mrt_tbl < 0) // Check again becasue of fork().
igmpProxyFork(intToken);
}
Expand Down Expand Up @@ -661,11 +659,8 @@ bool loadConfig(char *cfgFile) {
LOG(LOG_NOTICE, 0, "Config: Default table id should be between 0 and 999999999.");
else {
conf.defaultTable = intToken;
if (mrt_tbl < 0 && conf.defaultTable > 0) {
igmpProxyFork(0);
if (mrt_tbl < 0) // Check again because of fork().
igmpProxyFork(conf.defaultTable);
}
if (mrt_tbl < 0 && conf.defaultTable > 0)
igmpProxyFork(conf.defaultTable);
LOG(LOG_NOTICE, 0, "Config: Default to table %d for interfaces.", conf.defaultTable);
}
#else
Expand Down Expand Up @@ -952,10 +947,6 @@ bool loadConfig(char *cfgFile) {
timers.bwControl = timer_setTimer(conf.bwControlInterval * 10, "Bandwidth Control",
bwControl, &timers.bwControl);
}
#ifdef __linux__
if (mrt_tbl < 0 && !chld.nr)
mrt_tbl = 0;
#endif
return !logwarning;
}

Expand Down Expand Up @@ -1007,7 +998,7 @@ void configureVifs(void) {
struct IfDesc *IfDp = NULL;
struct vifConfig *vconf = NULL, *oconf = NULL;
struct filters *fil, *ofil;
bool quickLeave = false;
bool quickLeave = false, tbl0 = false;
uint32_t vifcount = 0, upvifcount = 0, downvifcount = 0;

uVifs = 0;
Expand Down Expand Up @@ -1037,6 +1028,8 @@ void configureVifs(void) {
oconf = IfDp->conf;
IfDp->conf = vconf;
}
if (!SHUTDOWN && !IFREBUILD && mrt_tbl < 0 && chld.nr && IfDp->conf->tbl == 0 && !tbl0++)
igmpProxyFork(0);

// Evaluate to old and new state of interface.
if (!CONFRELOAD && !(IfDp->state & 0x40)) {
Expand All @@ -1050,15 +1043,15 @@ void configureVifs(void) {
} else
// Existing interface, oldstate is current state, newstate is configured state.
IfDp->state = ((IfDp->state & 0x3) << 2) | (IfDp->mtu && (IfDp->Flags & IFF_MULTICAST) ? IfDp->conf->state : 0);
#ifdef __linux__
if (mrt_tbl >= 0 && IfDp->conf->tbl != mrt_tbl) {

if (IfDp->conf->tbl != mrt_tbl) {
// Check if Interface is in table for current process.
LOG(LOG_NOTICE, 0, "Not enabling table %d interface %s", IfDp->conf->tbl, IfDp->Name);
IfDp->state &= ~0x3; // Keep old state, new state disabled.
} else if (mrt_tbl < 0)
// Monitor process only needs config and state.
}
if (mrt_tbl < 0)
// Monitor process only needs config.
continue;
#endif
register uint8_t oldstate = IF_OLDSTATE(IfDp), newstate = IF_NEWSTATE(IfDp);
quickLeave |= !IS_DISABLED(IfDp->state) && IfDp->conf->quickLeave;

Expand Down Expand Up @@ -1095,19 +1088,21 @@ void configureVifs(void) {
if (IS_UPSTREAM(newstate)) {
upvifcount++;
BIT_SET(uVifs, IfDp->index);
}
} else
BIT_CLR(uVifs, IfDp->index);
}

// Do maintenance on vifs according to their old and new state.
if ( IS_DISABLED(oldstate) && IS_UPSTREAM(newstate)) { ctrlQuerier(1, IfDp); clearGroups(IfDp); }
else if ((STARTUP || IS_DISABLED(oldstate)) && IS_DOWNSTREAM(newstate)) { ctrlQuerier(1, IfDp); }
else if (!STARTUP && !IS_DISABLED(oldstate) && IS_DISABLED(newstate)) { ctrlQuerier(0, IfDp); clearGroups(IfDp); }
else if (!STARTUP && oldstate != newstate) { ctrlQuerier(2, IfDp); clearGroups(IfDp); }
else if ( oldstate == newstate && !IS_DISABLED(newstate)) { if (!IFREBUILD) clearGroups(IfDp); }
if ( IS_DISABLED(oldstate) && IS_UPSTREAM(newstate)) { ctrlQuerier(1, IfDp); clearGroups(IfDp); }
else if ((STARTUP || IS_DISABLED(oldstate)) && IS_DOWNSTREAM(newstate)) { ctrlQuerier(1, IfDp); }
else if (!STARTUP && !IS_DISABLED(oldstate) && IS_DISABLED(newstate)) { ctrlQuerier(0, IfDp); clearGroups(IfDp); }
else if (!STARTUP && oldstate != newstate) { ctrlQuerier(2, IfDp); clearGroups(IfDp); }
else if ( IFREBUILD && oldstate == newstate && !IS_DISABLED(newstate)) { clearGroups(IfDp); }
IfDp->filCh = false;

// Check if vif needs to be removed.
if (IS_DISABLED(newstate) && IfDp->index != (uint8_t)-1) {
BIT_CLR(uVifs, IfDp->index);
k_delVIF(IfDp);
if (vifcount)
vifcount--;
Expand All @@ -1117,11 +1112,10 @@ void configureVifs(void) {
upvifcount--;
}
}
#ifdef __linux__
if (mrt_tbl < 0)
// Monitor process only needs config and state.
return;
#endif

// Set hashtable size to 0 when quickleave is not enabled on any interface.
if (!quickLeave) {
LOG(LOG_NOTICE, 0, "Disabling quickleave, no interfaces have it enabled.");
Expand All @@ -1138,5 +1132,5 @@ void configureVifs(void) {

// All vifs created / updated, check if there is an upstream and at least one downstream.
if (!SHUTDOWN && !RESTART && (vifcount < 2 || upvifcount == 0 || downvifcount == 0))
LOG(LOG_ERR, eNOCONF, "There must be at least 2 interfaces, 1 upstream and 1 dowstream.");
LOG(LOG_ERR, 0 - eNOINIT, "There must be at least 2 interfaces, 1 upstream and 1 dowstream.");
}
8 changes: 2 additions & 6 deletions src/ifvc.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void rebuildIfVc(uint64_t *tid) {
// Build new IfDEsc table on SIGHUP, SIGUSR2 or timed rebuild.
if (tid)
sigstatus |= GOT_SIGUSR2;
if (!IfDescL || !(CONFRELOAD || SHUTDOWN || RESTART))
if (! IfDescL || IFREBUILD || SHUP || STARTUP)
buildIfVc();

// Call configureVifs to link the new IfDesc table.
Expand Down Expand Up @@ -211,11 +211,7 @@ void getIfStats(struct IfDesc *IfDp, int h, int fd) {
sprintf(buf, "%lu,%lu\n", IfDp->rqCnt, IfDp->sqCnt);
send(fd, buf, strlen(buf), MSG_DONTWAIT);
return;
#ifdef __linux__
} else for (IFL(IfDp), i++) if (!mrt_tbl || !IS_DISABLED(IfDp->state)) {
#else
} else for (IFL(IfDp), i++) {
#endif
} else for (IFL(IfDp), i++) if (mrt_tbl == -1 || !chld.nr || !IS_DISABLED(IfDp->state)) {
if (h) {
total = (struct totals){ total.bytes + IfDp->bytes, total.rate + IfDp->rate, total.ratelimit + IfDp->conf->ratelimit };
strcpy(msg, "%4d |%15s| %2d| v%1d|%15s|%12s|%8s|%10s|%15s|%14lld B | %10lld B/s | %10lld B/s\n");
Expand Down
5 changes: 2 additions & 3 deletions src/igmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ int initIgmp(bool activate) {
}
if (fd == -1)
fd = k_enableMRouter();
#ifdef __linux__
if (mrt_tbl < 0)
return fd;
#endif

if (! _calloc(rcv_buf, 1, rcv, CONF->pBufsz) || ! _calloc(snd_buf, 1, snd, CONF->pBufsz))
LOG(LOG_ERR, eNOMEM, "initIgmp: Out of Memory."); // Freed by Self
struct ip *ip = (struct ip *)snd_buf;
Expand Down Expand Up @@ -125,7 +124,7 @@ static bool checkIgmp(struct IfDesc *IfDp, register uint32_t src, register uint3
inetFmt(group,1), IfDp->Name, inetFmt(src, 2));
else if (src == IfDp->InAdr.s_addr || (IfDp->querier.ip == IfDp->conf->qry.ip && src == IfDp->querier.ip))
LOG(LOG_DEBUG, 0, "checkIgmp: The request from %s on %s is from myself. Ignoring.", inetFmt(src, 1), IfDp->Name);
else if ((IfDp->state & ifstate) == 0) {
else if ((IfDp->state & ifstate) == 0 || IfDp->conf->tbl != mrt_tbl) {
strcat(strcpy(msg, ""), IS_UPSTREAM(IfDp->state) ? "upstream interface "
: IS_DOWNSTREAM(IfDp->state) ? "downstream interface " : "disabled interface ");
LOG(LOG_INFO, 0, "checkIgmp: Message for %s from %s was received on %s interface %s. Ignoring.",
Expand Down
Loading

0 comments on commit 2560322

Please sign in to comment.