From 929392cff56e48157b8fb8761d82bdd4210ab4fe Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Sat, 26 Aug 2017 19:42:54 +0200
Subject: [PATCH] remoted: moar code, still doing nothing
---
core/communication.c | 4 +---
core/error.h | 6 ++++--
core/logger.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
core/logger.h | 15 +++++++++++++++
remote/app/README.md | 9 +++++++--
remote/app/remotec.c | 5 +----
remote/app/remoted.c | 42 +++++++++++++++++++++++++++++++++---------
remote/lib/remoted.c | 2 ++
remote/lib/remoted.h | 4 ++++
9 files changed, 111 insertions(+), 20 deletions(-)
create mode 100644 core/logger.c
create mode 100644 core/logger.h
diff --git a/core/communication.c b/core/communication.c
index b306ba1..491012e 100644
--- a/core/communication.c
+++ b/core/communication.c
@@ -34,9 +34,7 @@ int srv_init (int argc, char **argv, char **env
// gets the service path
service_path (srv->spath, sname, srv->index, srv->version);
- usock_init (&srv->service_fd, srv->spath);
-
- return 0;
+ return usock_init (&srv->service_fd, srv->spath);
}
int srv_accept (struct service *srv, struct process *p)
diff --git a/core/error.h b/core/error.h
index 7eda28d..0ba9e86 100644
--- a/core/error.h
+++ b/core/error.h
@@ -1,10 +1,12 @@
#ifndef __ERROR_H__
#define __ERROR_H__
+#include "logger.h"
+
#define handle_error(msg) \
- do { perror(msg); exit(EXIT_FAILURE); } while (0)
+ do { log_error (msg); exit(EXIT_FAILURE); } while (0)
#define handle_err(fun,msg)\
- fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg);
+ do { log_error ("%s: file %s line %d %s", fun, __FILE__, __LINE__, msg); } while (0)
#endif
diff --git a/core/logger.c b/core/logger.c
new file mode 100644
index 0000000..5fda936
--- /dev/null
+++ b/core/logger.c
@@ -0,0 +1,44 @@
+#include "logger.h"
+#include
+#include
+#include
+#include
+
+void log_format (const char* tag, const char* message, va_list args) {
+ time_t now;
+
+ time(&now);
+
+ char * date =ctime(&now);
+ date[strlen(date) - 1] = '\0';
+ printf("%s:%s: ", date, tag);
+ vprintf(message, args);
+
+ printf("\n");
+}
+
+void log_error (const char* message, ...) {
+ va_list args;
+ va_start(args, message);
+
+ log_format("error", message, args);
+
+ va_end(args);
+}
+
+void log_info (const char* message, ...) {
+ va_list args;
+ va_start(args, message);
+
+ log_format("info", message, args);
+ va_end(args);
+}
+
+void log_debug (const char* message, ...) {
+ va_list args;
+ va_start(args, message);
+
+ log_format("debug", message, args);
+
+ va_end(args);
+}
diff --git a/core/logger.h b/core/logger.h
new file mode 100644
index 0000000..4759803
--- /dev/null
+++ b/core/logger.h
@@ -0,0 +1,15 @@
+#ifndef __LOGGER_H__
+#define __LOGGER_H__
+
+#define LOG
+
+#include
+
+void log_error (const char* message, ...);
+void log_info (const char* message, ...);
+void log_debug (const char* message, ...);
+
+// please use previous functions
+void log_format (const char* tag, const char* message, va_list args);
+
+#endif
diff --git a/remote/app/README.md b/remote/app/README.md
index 9a566c3..551fdd2 100644
--- a/remote/app/README.md
+++ b/remote/app/README.md
@@ -8,10 +8,15 @@ This service creates a path on the relevent remote location, going through anyth
The idea is to have a simple configuration file for authentication of remote connections, such as:
+ table dynusers # dynamic user table
clients = { "client123", alice.example.com, john@doe.com }
+ localclients = { pamuser1, }
+
level1services = { pongd, weather }
-
+
ifext = enp0s25
pass in on $ifext from any for all to local services $level1services
- pass in on $ifext from any for all to local services $level1services
+ pass out on $ifext from local for $localclients to any services $level1services
+
+ block all
diff --git a/remote/app/remotec.c b/remote/app/remotec.c
index 6316b29..666f8fd 100644
--- a/remote/app/remotec.c
+++ b/remote/app/remotec.c
@@ -55,16 +55,13 @@ void main_loop (int argc, char **argv, char **env
(void) argv;
(void) env;
+#if 0
struct service srv;
memset (&srv, 0, sizeof (struct service));
remote_connection (argc, argv, env, &srv);
printf ("connected\n");
- if (strncmp (cmd, "sub", 3) == 0) {
- chan_sub (&srv, chan);
- }
-
printf ("main_loop\n");
struct remote_msg msg;
diff --git a/remote/app/remoted.c b/remote/app/remoted.c
index 1a484a9..26f16cf 100644
--- a/remote/app/remoted.c
+++ b/remote/app/remoted.c
@@ -4,6 +4,8 @@
#include "../lib/remoted.h"
#include
+#include "../../core/logger.h"
+
#include
#include
#include
@@ -19,20 +21,34 @@ void handle_signal (int signalnumber)
handle_error("srv_close < 0");
}
- fprintf (stderr, "received a signal %d\n", signalnumber);
+ log_info ("remoted received a signal %d\n", signalnumber);
exit (EXIT_SUCCESS);
}
-void remoted_init ()
+/* TODO: handle command line arguments */
+void remoted_cmd_args (int argc, char **argv, char **env
+ , struct remoted_ctx *ctx)
{
- /* TODO
- * handle authorizations
- */
+ (void) argc;
+ (void) argv;
+ (void) env;
+ (void) ctx;
}
+/* TODO: handle authorizations */
+int remoted_auth_conf (struct remoted_ctx *ctx)
+{
+ (void) ctx;
+ return 0;
+}
+
+
int
main(int argc, char **argv, char **env)
{
+ struct remoted_ctx ctx;
+ memset (&ctx, 0, sizeof (struct remoted_ctx));
+
memset (&srv, 0, sizeof (struct service));
srv.index = 0;
srv.version = 0;
@@ -41,15 +57,22 @@ main(int argc, char **argv, char **env)
signal(SIGINT, handle_signal);
signal(SIGQUIT, handle_signal);
- remoted_init ();
+ remoted_cmd_args (argc, argv, env, &ctx);
+
+ log_info ("remoted started");
+ // load configuration
+ if (remoted_auth_conf (&ctx)) {
+ log_error ("remoted cannot load configuration");
+ }
+ else
+ log_info ("remoted configuration loaded");
if (srv_init (argc, argv, env, &srv, REMOTED_SERVICE_NAME) < 0) {
handle_error("srv_init < 0");
- return EXIT_FAILURE;
}
- printf ("Listening on %s.\n", srv.spath);
+ log_info ("remoted listening on %s", srv.spath);
- printf("MAIN: server created\n" );
+ // TODO: here comes pledge (openbsd)
// the service will loop until the end of time, a specific message, a signal
remoted_main_loop (&srv);
@@ -58,6 +81,7 @@ main(int argc, char **argv, char **env)
if (srv_close (&srv) < 0) {
handle_error("srv_close < 0");
}
+ log_info ("remoted ended");
return EXIT_SUCCESS;
}
diff --git a/remote/lib/remoted.c b/remote/lib/remoted.c
index b40e634..e442fa2 100644
--- a/remote/lib/remoted.c
+++ b/remote/lib/remoted.c
@@ -3,6 +3,7 @@
#include "../../core/process.h"
#include "../../core/utils.h"
#include "../../core/error.h"
+#include "../../core/logger.h"
#include "remoted.h"
@@ -13,5 +14,6 @@
void remoted_main_loop (struct service *srv)
{
(void) srv;
+ log_debug ("remoted entering main loop");
/* TODO */
}
diff --git a/remote/lib/remoted.h b/remote/lib/remoted.h
index cee0d6c..4269c03 100644
--- a/remote/lib/remoted.h
+++ b/remote/lib/remoted.h
@@ -6,6 +6,10 @@
#define REMOTED_SERVICE_NAME "remoted"
+struct remoted_ctx {
+ /* TODO */
+};
+
void remoted_main_loop (struct service *srv);
#endif