summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2017-04-06 22:34:52 +0200
committerPaweł Redman <pawel.redman@gmail.com>2017-04-06 22:34:52 +0200
commit61f2d0e71b7df4ee6fdd1747c6149541d0411044 (patch)
treeefe44564ba4cb2b1ea8b6a9a70660296cc109499 /src
parent10a0bd03c08dd6b6023d26dd090352620c5cbd6e (diff)
Add command-line arguments.
Diffstat (limited to 'src')
-rw-r--r--src/main.c54
-rw-r--r--src/shared.h5
2 files changed, 52 insertions, 7 deletions
diff --git a/src/main.c b/src/main.c
index b58f143..44d831d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -63,7 +63,7 @@ static int parse_req_addr(char *begin, char *end, uint32_t *out)
return 0;
}
-void handle_request(struct sockaddr_in *ret_addr, uint32_t query)
+static void handle_request(struct sockaddr_in *ret_addr, uint32_t query)
{
db_entry *entry;
job_t *reply_job;
@@ -130,7 +130,7 @@ static volatile sig_atomic_t signals_reload = false;
// NOTE: The code assumes that signal_handler will only be called in the main
// thread, which (as far as I know) is always the case on Linux.
-void signal_handler(int signum)
+static void signal_handler(int signum)
{
if (signum == SIGINT || signum == SIGTERM)
signals_terminate = true;
@@ -139,7 +139,7 @@ void signal_handler(int signum)
signals_reload = true;
}
-int handle_signals(void)
+static int handle_signals(void)
{
if (signals_terminate) {
DEBUG("received a terminating signal\n");
@@ -160,9 +160,47 @@ int handle_signals(void)
return 0;
}
+static struct {
+ int port;
+ char *lists_file;
+} config = {
+ .port = 1337,
+ .lists_file = "schachts.list"
+};
+
+static int parse_argv(int argc, char **argv)
+{
+ int opt;
+
+ while ((opt = getopt(argc, argv, "vhp:l:")) != -1) {
+ switch (opt) {
+ case 'v':
+ puts(PROGRAM_NAME " " PROGRAM_VERSION);
+ exit(0);
+
+ case 'h':
+ puts("usage: " PROGRAM_NAME " [-h] [-v] [-p PORT] [-l LISTS-FILE]");
+ exit();
+
+ case 'p':
+ config.port = atoi(optarg);
+ break;
+
+ case 'l':
+ config.lists_file = optarg;
+ break;
+
+ case '?':
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
#define NUM_WORKERS 8 // FIXME: shouldn't be hardcoded
-int main(void)
+int main(int argc, char **argv)
{
int error = 0 /* the exit code */, flags;
struct sigaction sigact = {.sa_handler = signal_handler};
@@ -172,11 +210,14 @@ int main(void)
clock_gettime(CLOCK_MONOTONIC_RAW, &time_ref);
+ if (parse_argv(argc, argv))
+ return 1;
+
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGUSR1, &sigact, NULL);
- if (lists_load("schachts.list", 0)) {
+ if (lists_load(config.lists_file, 0)) {
eprintf("fatal error: couldn't load the lists\n");
goto error_lists;
}
@@ -194,10 +235,11 @@ int main(void)
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = INADDR_ANY;
- sockaddr.sin_port = htons(1337);
+ sockaddr.sin_port = htons(config.port);
if (bind(server_sockfd, (void*)&sockaddr, sizeof(sockaddr)) == -1) {
perror("bind");
+ eprintf("couldn't bind to UDP port %hi\n", ntohs(sockaddr.sin_port));
error = 1;
goto error_bind;
}
diff --git a/src/shared.h b/src/shared.h
index 187edcc..46c900e 100644
--- a/src/shared.h
+++ b/src/shared.h
@@ -33,8 +33,11 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "eli.h"
+#define PROGRAM_NAME "schachtmeister2"
+#define PROGRAM_VERSION "v0.1.0"
+
#define eprintf(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
-#define DEBUG(fmt, ...) fprintf(stderr, "%s: " fmt, __func__, ##__VA_ARGS__)
+#define DEBUG(fmt, ...) fprintf(stderr, "[DEBUG] %s: " fmt, __func__, ##__VA_ARGS__)
#define LOCALHOST_NETWORK 0x7F000000
#define LOCALHOST_MASK 0xFF000000