aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: f6daa43ad6400cf242d726631faaa9640e932419 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* main.c
 * (c) Jonas Gunz, 2019
 * License: MIT
 * */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#include <unistd.h>

#include "log.h"
#include "server.h"

void print_help( char *_argv0 ) {
	printf(
		"dns\n"
		"Usage: %s [OPTIONS]\n\n"
		"Options:\n"
		"	-h		Display this help text\n"
		"	-i IP		IP to bind to, default: 0.0.0.0\n"
		"	-p PORT 	Port to listen, default: 53 on\n"
		"	-z FILE 	Zonefile\n", _argv0
	);
}

void parse_args( server_config_t *_config, int argc, char* argv[]) {
	unsigned int i, o;
	memset( _config, 0, sizeof( server_config_t ) );

	_config->bind_ip = "0.0.0.0";
	_config->bind_port = 53;
	_config->zonefile = "/nofile";

	for( i = 1; i < (unsigned int)argc; i++ ) {
		const unsigned int icpy = i;
		if ( argv[i][0] != '-' ) {
			print_help( argv[0] );
			exit( 1 );
		}

		for( o = 1; o < strlen(argv[icpy]); o++ ) {
			switch( argv[icpy][o] ) {
				case 'h':
					print_help( argv[0] );
					exit( 0 );
				case 'i':
					_config->bind_ip = argv[++i];
					break;
				case 'p':
					_config->bind_port = (uint16_t)atoi( argv[++i] );
					break;
				case 'z':
					_config->zonefile = argv[++i];
					break;
				default:
					printf( "Unkown option \"%c\"\n", argv[icpy][o] );
					print_help( argv[0] );
					exit( 1 );
			}
		}
	}
}

int main(int argc, char* argv[])
{
	server_config_t config;
	parse_args( &config, argc, argv );

	log_init_stdout(_LOG_DEBUG);

	if ( getuid() == 0 )
		LOGPRINTF(_LOG_WARNING, "Running as root is not a good idea. Use setcap or unprivileged port instead.");

	server_start( &config );

	return 0;
}