diff options
Diffstat (limited to 'receiver.cpp')
-rw-r--r-- | receiver.cpp | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/receiver.cpp b/receiver.cpp index 4d599da..4a0269a 100644 --- a/receiver.cpp +++ b/receiver.cpp | |||
@@ -262,23 +262,47 @@ static void import_sessions(const char *root_dir) { | |||
262 | closedir(dirp); | 262 | closedir(dirp); |
263 | } | 263 | } |
264 | 264 | ||
265 | int main(int args, char **argv) { | 265 | int main(int argc, char **argv) { |
266 | mbedtls_ctr_drbg_context ctr_drbg; | 266 | mbedtls_ctr_drbg_context ctr_drbg; |
267 | mbedtls_entropy_context entropy; | 267 | mbedtls_entropy_context entropy; |
268 | mbedtls_pk_context pk; | 268 | mbedtls_pk_context pk; |
269 | int ret = 0; | 269 | int ret = 0, clport = PORT; |
270 | char * privkey_file = 0; | ||
271 | char * me = argv[0]; | ||
272 | |||
273 | char ch; | ||
274 | while ((ch = getopt(argc, argv, "p:k:")) != -1) { | ||
275 | switch (ch) { | ||
276 | case 'k': | ||
277 | privkey_file = optarg; | ||
278 | break; | ||
279 | case 'p': | ||
280 | clport = std::stoi(optarg); | ||
281 | break; | ||
282 | default: | ||
283 | errx(-1, "Syntax: %s [sessionlogdir] xx", me); | ||
284 | break; | ||
285 | } | ||
286 | } | ||
287 | argc -= optind; | ||
288 | argv += optind; | ||
270 | 289 | ||
271 | if (args != 2) | 290 | if (argc != 1) |
272 | errx(-1, "Syntax: %s [sessionlogdir]", argv[0]); | 291 | errx(-1, "Syntax: %s [sessionlogdir]", me); |
273 | chdir(argv[1]); | 292 | chdir(argv[0]); |
274 | 293 | ||
275 | mbedtls_pk_init( &pk ); | 294 | mbedtls_pk_init( &pk ); |
276 | mbedtls_entropy_init( &entropy ); | 295 | mbedtls_entropy_init( &entropy ); |
277 | mbedtls_ctr_drbg_init( &ctr_drbg ); | 296 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
278 | mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, pp, sizeof(pp)); | 297 | mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, pp, sizeof(pp)); |
279 | 298 | ||
280 | if ((ret = mbedtls_pk_parse_key(&pk, privkey, sizeof(privkey), NULL, 0) ) != 0 ) | 299 | if (privkey_file) { |
281 | errx(-1, "Fatal: mbedtls_pk_parse_key returned -0x%04x\n", -ret ); | 300 | if ((ret = mbedtls_pk_parse_keyfile(&pk, privkey_file, NULL) ) != 0 ) |
301 | errx(-1, "Fatal: mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret ); | ||
302 | } else { | ||
303 | if ((ret = mbedtls_pk_parse_key(&pk, privkey, sizeof(privkey), NULL, 0) ) != 0 ) | ||
304 | errx(-1, "Fatal: mbedtls_pk_parse_key returned -0x%04x\n", -ret ); | ||
305 | } | ||
282 | 306 | ||
283 | int sock = socket(AF_INET, SOCK_DGRAM, 0); | 307 | int sock = socket(AF_INET, SOCK_DGRAM, 0); |
284 | if (sock < 0) | 308 | if (sock < 0) |
@@ -287,9 +311,9 @@ int main(int args, char **argv) { | |||
287 | struct sockaddr_in servaddr, peer; | 311 | struct sockaddr_in servaddr, peer; |
288 | servaddr.sin_family = AF_INET; | 312 | servaddr.sin_family = AF_INET; |
289 | servaddr.sin_addr.s_addr = INADDR_ANY; | 313 | servaddr.sin_addr.s_addr = INADDR_ANY; |
290 | servaddr.sin_port = htons(PORT); | 314 | servaddr.sin_port = htons(clport); |
291 | if (bind(sock, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) | 315 | if (bind(sock, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) |
292 | errx(-1, "Fatal: Can't bind to port %d\n", PORT); | 316 | errx(-1, "Fatal: Can't bind to port %d\n", clport); |
293 | 317 | ||
294 | import_sessions("."); | 318 | import_sessions("."); |
295 | 319 | ||