Add daemon binary

This commit is contained in:
Melon 2021-04-24 11:19:07 +01:00
parent f44cfcb1ce
commit fe7e922022

181
src/impl/daemon/main.c Normal file
View File

@ -0,0 +1,181 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <threads.h>
#include <stdatomic.h>
#include <signal.h>
#include "states.h"
#include "unix_sockets.h"
#include "melon_vpn_sock.h"
#include "config_file.h"
#define vpnconfig "/etc/melonvpn/client.cfg"
#define daemonconfig "/etc/melonvpn/daemon.cfg"
char *vpnserver = "";
char *devicename = "unknown";
bool restartvpn = false;
int start_daemon_server(void*);
int start_peers_server(void*);
static volatile int keepRunning = 1;
atomic_bool daemonthrbusy = false;
atomic_bool peersthrbusy = false;
thrd_t daemonthr;
thrd_t peersthr;
void read_vpn_config();
void read_daemon_config();
int main(int argc, char **argv) {
printf("Reading config files...\n");
read_vpn_config();
read_daemon_config();
printf("VPN server: %s\n", vpnserver);
printf("Device name: %s\n", devicename);
printf("Restart VPN: %s\n", restartvpn?"true":"false");
thrd_create(&daemonthr, start_daemon_server, NULL);
thrd_create(&peersthr, start_peers_server, NULL);
thrd_join(daemonthr, NULL);
thrd_join(peersthr, NULL);
}
void incoming_request(char *request) {
char *a;
char *b;
a=strtok(request,(char*)1);
b=strtok(NULL,"");
fprintf(stderr,a);
fprintf(stderr,b);
}
void incoming_peers(char *peers) {
char *a;
char *b;
fprintf(stderr,peers);
}
void read_vpn_config() {
FILE *fp;
fp = fopen(vpnconfig, "r");
keyvaluepair_t result;
while(true) {
result = config_read(fp);
if(!result.loop) break;
if(strcmp(result.key, "vpn")==0) {
vpnserver = malloc(strlen(result.value));
strcpy(vpnserver, result.value);
} else if(strcmp(result.key, "name")==0) {
devicename = malloc(strlen(result.value));
strcpy(devicename, result.value);
}
}
fclose(fp);
}
void read_daemon_config() {
FILE *fp;
fp = fopen(daemonconfig, "r");
keyvaluepair_t result;
while(true) {
result = config_read(fp);
if(!result.loop) break;
if(strcmp(result.key, "restart")==0) {
restartvpn = strcmp(result.value, "true")==0;
}
}
fclose(fp);
}
void write_daemon_config() {
FILE *fp;
fp = fopen(daemonconfig, "w");
config_write(fp, "restart", restartvpn?"true":"false");
fclose(fp);
}
int start_daemon_server(void *thr_data) {
(void)thr_data;
// Just too lazy to keep all this shit here...
// functions are better
int sfd = unix_socket_server_start(SV_DAEMON_SOCK_PATH);
ssize_t numRead;
char buf[BUF_SIZE];
// Client connections are handled iteratively
for (;;) {
int cfd = accept(sfd, NULL, NULL);
// Read at most BUF_SIZE bytes from the socket into buf.
while ((numRead = read(cfd, buf, BUF_SIZE)) > 0) {
// Then, write those bytes from buf into STDOUT.
incoming_request(buf);
if (write(STDOUT_FILENO, buf, numRead) != numRead) {
fprintf(stderr,"Partial/failed write");
return 0;
}
}
if (numRead == -1) {
fprintf(stderr,"Failed to read");
return 0;
}
if (close(cfd) == -1) {
fprintf(stderr,"Failed to close");
return 0;
}
}
return 0;
}
int start_peers_server(void *thr_data) {
(void)thr_data;
int sfd = unix_socket_server_start(SV_RPEERS_SOCK_PATH);
ssize_t numRead;
char buf[BUF_SIZE];
for(;;) {
int cfd = accept(sfd, NULL, NULL);
int MAX_PEERS_LEN = 1000;
int total = 0;
char peers[MAX_PEERS_LEN];
strcpy(peers, "");
while ((numRead = read(cfd, buf, BUF_SIZE)) > 0) {
if(total > MAX_PEERS_LEN-1) break;
total+=numRead;
strcat(peers, buf);
peers[MAX_PEERS_LEN-1]=0;
}
incoming_peers(buf);
}
return 0;
}