summaryrefslogtreecommitdiffstats
path: root/src/config.y
blob: 9868d571326a0b992a368df7461877c742654546 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
%define api.pure
%define api.push-pull push
%name-prefix "fastd_config_"
%parse-param {fastd_context *ctx}
%parse-param {fastd_config *conf}

%code requires {
	#include <fastd.h>
	#include <arpa/inet.h>
}

%union {
	int num;
	char* str;
	struct in_addr addr;
	struct in6_addr addr6;
}

%token <num> TOK_INTEGER
%token <str> TOK_STRING
%token <str> TOK_IDENTIFIER

%token <str> TOK_INTERFACE
%token <str> TOK_BIND
%token <str> TOK_MTU
%token <str> TOK_MODE
%token <str> TOK_PROTOCOL
%token <str> TOK_PEER
%token <str> TOK_ADDRESS
%token <str> TOK_SECRET
%token <str> TOK_KEY

%token <addr> TOK_ADDR
%token <addr6> TOK_ADDR6

%token <str> TOK_ANY
%token <str> TOK_TAP
%token <str> TOK_TUN

%code {
	#include <config.h>
	#include <stdint.h>
	#include <peer.h>

	void fastd_config_error(fastd_context *ctx, fastd_config *conf, char *s);

	extern fastd_protocol fastd_protocol_null;

	#ifdef WITH_PROTOCOL_ECFXP
	extern fastd_protocol fastd_protocol_ec25519_fhmqvc_xsalsa20_poly1305;
	#endif
}


%type <str> maybe_string

%type <num> port
%type <num> maybe_port
%type <num> maybe_port_default

%%
config:		config statement
	|
	;

statement:	TOK_INTERFACE interface ';'
	| 	TOK_BIND bind ';'
	|	TOK_MTU mtu ';'
	|	TOK_MODE mode ';'
	|	TOK_PROTOCOL protocol ';'
	|	TOK_SECRET secret ';'
	|	TOK_PEER peer '{' peer_conf '}'
	;

interface:	TOK_STRING	{ free(conf->ifname); conf->ifname = strdup($1); }
	;

bind:		TOK_ADDR maybe_port {
			conf->bind_addr_in.sin_family = AF_INET;
			conf->bind_addr_in.sin_addr = $1;
			conf->bind_addr_in.sin_port = $2;
		}
	|	TOK_ADDR6 maybe_port {
			conf->bind_addr_in6.sin6_family = AF_INET6;
			conf->bind_addr_in6.sin6_addr = $1;
			conf->bind_addr_in6.sin6_port = $2;
		}
	|	TOK_ANY maybe_port {
			conf->bind_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
			conf->bind_addr_in.sin_port = $2;
			conf->bind_addr_in6.sin6_addr = in6addr_any;
			conf->bind_addr_in6.sin6_port = $2;
		}
	;

mtu:		TOK_INTEGER	{ conf->mtu = $1; }
	;

mode:		TOK_TAP		{ conf->mode = MODE_TAP; }
	|	TOK_TUN		{ conf->mode = MODE_TUN; }
	;

protocol:	maybe_string {
			if (!strcmp($1, "null"))
				conf->protocol = &fastd_protocol_null;
#ifdef WITH_PROTOCOL_ECFXP
			else if (!strcmp($1, "ecfxp"))
				conf->protocol = &fastd_protocol_ec25519_fhmqvc_xsalsa20_poly1305;
#endif
			else
				exit_error(ctx, "config error: invalid protocol `%s'", $1);
}
	;

secret:		TOK_STRING	{ free(conf->secret); conf->secret = strdup($1); }
	;

peer:		maybe_string {
			fastd_peer_config *current_peer = malloc(sizeof(fastd_peer_config));
			current_peer->next = conf->peers;
			conf->peers = current_peer;

			memset(&current_peer->address, 0, sizeof(fastd_peer_address));

			current_peer->address.sa.sa_family = AF_UNSPEC;
		}
	;

peer_conf:	peer_conf peer_statement
	|
	;

peer_statement: TOK_ADDRESS peer_address ';'
	|	TOK_KEY peer_key ';'
	;

peer_address:	TOK_ADDR maybe_port_default {
			conf->peers->address.in.sin_family = AF_INET;
			conf->peers->address.in.sin_addr = $1;
			conf->peers->address.in.sin_port = $2;
		}
	|	TOK_ADDR6 maybe_port_default {
			conf->peers->address.in6.sin6_family = AF_INET6;
			conf->peers->address.in6.sin6_addr = $1;
			conf->peers->address.in6.sin6_port = $2;
		}
	;

peer_key:	TOK_STRING	{ free(conf->peers->key); conf->peers->key = strdup($1); }
	;


maybe_string:	TOK_STRING
	|			{ $$ = ""; }
	;

maybe_port:	':' port	{ $$ = $2; }
	|			{ $$ = 0; }
	;

maybe_port_default: ':' port	{ $$ = $2; }
	|			{ $$ = htons(1337); }
	;

port:		TOK_INTEGER {
			if ($1 < 0 || $1 > 65635)
				exit_error(ctx, "invalid port %i", $1);
			$$ = htons($1);
		}
	;
%%
void fastd_config_error(fastd_context *ctx, fastd_config *conf, char *s) {
	exit_error(ctx, "config error: %s", s);
}