work in progress
This commit is contained in:
3
Makefile
3
Makefile
@@ -243,7 +243,8 @@ AUTOMAKE = ${SHELL} '/home/ziggi/Projects/helmet/missing' automake-1.16
|
||||
AWK = mawk
|
||||
CC = gcc
|
||||
CCDEPMODE = depmode=none
|
||||
CFLAGS = -g -O2 -std=c99 -Wall -I. -I ./libxasync -I ./libxtools
|
||||
CFLAGS = -g -O2 -std=c99 -Wall -I. -I ./libxasync -I ./libxtools \
|
||||
-D_POSIX_C_SOURCE=1
|
||||
CPPFLAGS =
|
||||
CSCOPE = cscope
|
||||
CTAGS = ctags
|
||||
|
||||
@@ -5,6 +5,7 @@ libxtools_NAME = libxtools.a
|
||||
libxasync_NAME = libxasync.a
|
||||
|
||||
CFLAGS += -std=c99 -Wall -I. -I ./libxasync -I ./libxtools
|
||||
CFLAGS += -D_POSIX_C_SOURCE=1
|
||||
|
||||
sbin_PROGRAMS = helmetd
|
||||
bin_PROGRAMS = helmetctl
|
||||
|
||||
@@ -243,7 +243,8 @@ AUTOMAKE = @AUTOMAKE@
|
||||
AWK = @AWK@
|
||||
CC = @CC@
|
||||
CCDEPMODE = @CCDEPMODE@
|
||||
CFLAGS = @CFLAGS@ -std=c99 -Wall -I. -I ./libxasync -I ./libxtools
|
||||
CFLAGS = @CFLAGS@ -std=c99 -Wall -I. -I ./libxasync -I ./libxtools \
|
||||
-D_POSIX_C_SOURCE=1
|
||||
CPPFLAGS = @CPPFLAGS@
|
||||
CSCOPE = @CSCOPE@
|
||||
CTAGS = @CTAGS@
|
||||
|
||||
@@ -12,9 +12,9 @@ int main(int argc, char** argv) {
|
||||
int res = 0;
|
||||
|
||||
logger_dprintf("main start");
|
||||
logger_init(argc, argv);
|
||||
logger_init();
|
||||
|
||||
server_init();
|
||||
server_init(argc, argv);
|
||||
server_config();
|
||||
server_run();
|
||||
|
||||
|
||||
42
logger.c
42
logger.c
@@ -7,37 +7,55 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
typedef struct {
|
||||
FILE* logout;
|
||||
} logger_t;
|
||||
|
||||
static logger_t logger;
|
||||
|
||||
int logger_init(void) {
|
||||
//int fd = fileno(stdout);
|
||||
//int newfd = dup(fd);
|
||||
//logger.logout = fdopen(newfd, "w");
|
||||
logger.logout = stdout;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void logger_dprintf(char* message, ...) {
|
||||
printf("debug: ");
|
||||
fprintf(logger.logout, "debug: ");
|
||||
va_list argptr;
|
||||
|
||||
va_start(argptr, message);
|
||||
vprintf(message, argptr);
|
||||
vfprintf(logger.logout, message, argptr);
|
||||
va_end(argptr);
|
||||
printf("\n");
|
||||
fprintf(logger.logout, "\n");
|
||||
}
|
||||
|
||||
void logger_iprintf(char* message, ...) {
|
||||
printf("info: ");
|
||||
fprintf(logger.logout, "warning: ");
|
||||
va_list argptr;
|
||||
|
||||
va_start(argptr, message);
|
||||
vprintf(message, argptr);
|
||||
vfprintf(logger.logout, message, argptr);
|
||||
va_end(argptr);
|
||||
printf("\n");
|
||||
fprintf(logger.logout, "\n");
|
||||
}
|
||||
|
||||
void logger_wprintf(char* message, ...) {
|
||||
printf("warning: ");
|
||||
fprintf(logger.logout, "warning: ");
|
||||
va_list argptr;
|
||||
|
||||
va_start(argptr, message);
|
||||
vprintf(message, argptr);
|
||||
vfprintf(logger.logout, message, argptr);
|
||||
va_end(argptr);
|
||||
printf("\n");
|
||||
fprintf(logger.logout, "\n");
|
||||
}
|
||||
|
||||
void logger_eprintf(char* message, ...) {
|
||||
fprintf(logger.logout, "error: ");
|
||||
va_list argptr;
|
||||
va_start(argptr, message);
|
||||
vfprintf(logger.logout, message, argptr);
|
||||
va_end(argptr);
|
||||
fprintf(logger.logout, "\n");
|
||||
}
|
||||
|
||||
1
logger.h
1
logger.h
@@ -6,5 +6,6 @@ int logger_init(void);
|
||||
void logger_dprintf(char* message, ...);
|
||||
void logger_wprintf(char* message, ...);
|
||||
void logger_iprintf(char* message, ...);
|
||||
void logger_eprintf(char* message, ...);
|
||||
|
||||
#endif
|
||||
|
||||
125
server.c
125
server.c
@@ -1,4 +1,23 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <pthread.h>
|
||||
//#include <sys/event.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/time.h>
|
||||
#include <err.h>
|
||||
|
||||
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
@@ -8,6 +27,8 @@
|
||||
#include <tconfig.h>
|
||||
#include <massert.h>
|
||||
|
||||
|
||||
|
||||
/*
|
||||
static char* strcopy(char* src) {
|
||||
size_t srcsize = strlen(src) + 1;
|
||||
@@ -42,6 +63,7 @@ int server_config(void) {
|
||||
logger_dprintf("server configuration");
|
||||
|
||||
tconfig_t tconfig;
|
||||
|
||||
tconfig_init(&tconfig);
|
||||
|
||||
server.logpath = "/var/log/helmetd.log";
|
||||
@@ -51,26 +73,32 @@ int server_config(void) {
|
||||
tconfig_bind(&tconfig, TCONF_INT, "port", &server.port);
|
||||
|
||||
ssize_t rsize = tconfig_read(&tconfig, "helmet.conf");
|
||||
|
||||
if (rsize < 0) {
|
||||
return SERVER_CONFIG_ERROR;
|
||||
}
|
||||
int res = tconfig_parse(&tconfig);
|
||||
|
||||
if (res < 0) {
|
||||
char* errstr = tconfig_geterr(&tconfig);
|
||||
|
||||
logger_dprintf("tconfig parsing error: %s", errstr);
|
||||
return SERVER_CONFIG_ERROR;
|
||||
}
|
||||
tconfig_destroy(&tconfig);
|
||||
|
||||
clconfig_t clconfig;
|
||||
|
||||
clconfig_init(&clconfig, server.argc, server.argv);
|
||||
|
||||
clconfig_bind(&clconfig, GCONF_INT, "port", &(server.port));
|
||||
clconfig_bind(&clconfig, GCONF_STR, "log", &(server.logpath));
|
||||
|
||||
// TODO: memory leak on server.logpath
|
||||
res = clconfig_parse(&clconfig);
|
||||
if (res < 0) {
|
||||
char* errstr = clconfig_geterr(&clconfig);
|
||||
|
||||
logger_dprintf("clconfig parsing error: %s", errstr);
|
||||
return SERVER_CONFIG_ERROR;
|
||||
}
|
||||
@@ -81,7 +109,104 @@ int server_config(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int loop(void);
|
||||
|
||||
int server_run(void) {
|
||||
logger_dprintf("start the server");
|
||||
loop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int socket_create(int port, int backlog) {
|
||||
int sock;
|
||||
|
||||
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
logger_dprintf("cannot create socket");
|
||||
return -1;
|
||||
}
|
||||
int optval;
|
||||
|
||||
optval = 1;
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
|
||||
logger_dprintf("cannot set socket option, exit\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct sockaddr addr;
|
||||
struct sockaddr_in* paddr = (struct sockaddr_in *)&addr;
|
||||
|
||||
paddr->sin_family = AF_INET;
|
||||
paddr->sin_addr.s_addr = INADDR_ANY;
|
||||
paddr->sin_port = htons(port);
|
||||
|
||||
if (bind(sock, (struct sockaddr *)paddr, sizeof(struct sockaddr_in)) < 0) {
|
||||
logger_dprintf("cannot bind socket");
|
||||
return -1;
|
||||
}
|
||||
if (listen(sock, backlog) < 0) {
|
||||
logger_dprintf("cannot listen socket");
|
||||
return -1;
|
||||
}
|
||||
return sock;
|
||||
}
|
||||
|
||||
|
||||
int loop(void) {
|
||||
|
||||
int port = 1025;
|
||||
int backlog = 4096;
|
||||
int sock;
|
||||
|
||||
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
logger_eprintf("Cannot create socket");
|
||||
return -1;
|
||||
}
|
||||
int optval;
|
||||
|
||||
optval = 1;
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
|
||||
logger_eprintf("Cannot set socket option, exit\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct sockaddr addr;
|
||||
struct sockaddr_in* paddr = (struct sockaddr_in *)&addr;
|
||||
|
||||
paddr->sin_family = AF_INET;
|
||||
paddr->sin_addr.s_addr = INADDR_ANY;
|
||||
paddr->sin_port = htons(port);
|
||||
|
||||
if (bind(sock, (struct sockaddr *)paddr, sizeof(struct sockaddr_in)) < 0) {
|
||||
logger_eprintf("Cannot bind socket");
|
||||
return -1;
|
||||
}
|
||||
if (listen(sock, backlog) < 0) {
|
||||
logger_eprintf("Cannot listen socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
int newsock = 0;
|
||||
if ((newsock = accept(sock, NULL, 0)) > 3) {
|
||||
struct timeval tv = {
|
||||
.tv_sec = 3,
|
||||
.tv_usec = 0
|
||||
};
|
||||
tv.tv_sec = 3;
|
||||
tv.tv_usec = 0;
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)) < 0) {
|
||||
logger_eprintf("Cannot set socket option");
|
||||
return -1;
|
||||
}
|
||||
logger_dprintf("hello");
|
||||
char* message = "hello\n";
|
||||
write(newsock, message, strlen(message));
|
||||
close(newsock);
|
||||
}
|
||||
}
|
||||
|
||||
close(sock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
//#include <fcntl.h>
|
||||
//#include <clconfig.h>
|
||||
#include <massert.h>
|
||||
#include <logger.h>
|
||||
|
||||
#include <server.h>
|
||||
|
||||
@@ -16,15 +17,22 @@ int main(int argc, char** argv) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
logger_init();
|
||||
|
||||
char* _argv[] = { argv[0], "--log=/var/log/mylog", "--port=1029" };
|
||||
int _argc = 2;
|
||||
|
||||
server_init(_argc, _argv);
|
||||
int res = 0;
|
||||
|
||||
if ((res = server_config()) < 0) {
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((res = server_run()) < 0) {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user