summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--receiver.cpp42
-rw-r--r--sender.c39
-rwxr-xr-xtest-all.sh37
4 files changed, 107 insertions, 14 deletions
diff --git a/Makefile b/Makefile
index 099a02d..8a7ed87 100644
--- a/Makefile
+++ b/Makefile
@@ -11,5 +11,8 @@ sender: sender.c
11receiver: receiver.cpp 11receiver: receiver.cpp
12 $(CXX) -std=c++17 -o receiver receiver.cpp $(LDFLAGS) -I$(INCLUDES) -L$(LIBS) $(CXXFLAGS) 12 $(CXX) -std=c++17 -o receiver receiver.cpp $(LDFLAGS) -I$(INCLUDES) -L$(LIBS) $(CXXFLAGS)
13 13
14test: receiver sender
15 ./test-all.sh
16
14clean: 17clean:
15 rm -f sender receiver 18 rm -f sender receiver
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
265int main(int args, char **argv) { 265int 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
diff --git a/sender.c b/sender.c
index cda56f9..c0caf6a 100644
--- a/sender.c
+++ b/sender.c
@@ -23,6 +23,8 @@ static const unsigned char pubkey[] =
23"SwIDAQAB \n" 23"SwIDAQAB \n"
24"-----END PUBLIC KEY----- \n"; 24"-----END PUBLIC KEY----- \n";
25 25
26static char *pubkey_file = 0;
27
26static const unsigned char pp[] = "9bf308b7ae027baa46091d980632e27b"; 28static const unsigned char pp[] = "9bf308b7ae027baa46091d980632e27b";
27static const char *logging_host = "endpoint-de9XDJ0fH7.gsmk.de"; 29static const char *logging_host = "endpoint-de9XDJ0fH7.gsmk.de";
28static const char *logging_port = "8238"; 30static const char *logging_port = "8238";
@@ -66,9 +68,14 @@ void new_session(int sock, mbedtls_ctr_drbg_context *ctr_drbg) {
66 mbedtls_pk_context pk; 68 mbedtls_pk_context pk;
67 mbedtls_pk_init(&pk); 69 mbedtls_pk_init(&pk);
68 int ret = 0; 70 int ret = 0;
69printf("%zd\n", sizeof(pubkey)); 71
70 if ((ret = mbedtls_pk_parse_public_key(&pk, pubkey, sizeof(pubkey)) ) != 0 ) 72 if (pubkey_file) {
71 errx(-1, "mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret ); 73 if ((ret = mbedtls_pk_parse_public_keyfile(&pk, pubkey_file) ) != 0 )
74 errx(-1, "mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
75 } else {
76 if ((ret = mbedtls_pk_parse_public_key(&pk, pubkey, sizeof(pubkey)) ) != 0 )
77 errx(-1, "mbedtls_pk_parse_public_key returned -0x%04x\n", -ret );
78 }
72 79
73 size_t olen = 0; 80 size_t olen = 0;
74 if ((ret = mbedtls_pk_encrypt(&pk, aes_key, AES_KEY_LENGTH, output + 1 + SESSION_ID_LENGTH, &olen, 81 if ((ret = mbedtls_pk_encrypt(&pk, aes_key, AES_KEY_LENGTH, output + 1 + SESSION_ID_LENGTH, &olen,
@@ -83,12 +90,34 @@ printf("%zd\n", sizeof(pubkey));
83 mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, aes_key, 8 * AES_KEY_LENGTH); 90 mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, aes_key, 8 * AES_KEY_LENGTH);
84} 91}
85 92
86int main() { 93int main(int argc, char **argv) {
94 const char * host = logging_host, * port = logging_port;
95 char ch;
96 while ((ch = getopt(argc, argv, "h:p:c:")) != -1) {
97 switch (ch) {
98 case 'h':
99 host = optarg;
100 break;
101 case 'p':
102 port = optarg;
103 break;
104 case 'c':
105 pubkey_file = optarg;
106 break;
107 case '?':
108 default:
109 printf("Usage: %s [-h host] [-p port] [-c cert]", argv[0]);
110 exit(0);
111 }
112 }
113 argc -= optind;
114 argv += optind;
115
87 struct addrinfo hints, *result, *rp; 116 struct addrinfo hints, *result, *rp;
88 memset (&hints, 0, sizeof (hints)); 117 memset (&hints, 0, sizeof (hints));
89 hints.ai_socktype = SOCK_DGRAM; 118 hints.ai_socktype = SOCK_DGRAM;
90 119
91 int sock = -1, res = getaddrinfo(logging_host, logging_port, &hints, &result); 120 int sock = -1, res = getaddrinfo(host, port, &hints, &result);
92 if (res != 0) 121 if (res != 0)
93 errx(EXIT_FAILURE, "getaddrinfo: %s\n", gai_strerror(res)); 122 errx(EXIT_FAILURE, "getaddrinfo: %s\n", gai_strerror(res));
94 123
diff --git a/test-all.sh b/test-all.sh
new file mode 100755
index 0000000..81ee66d
--- /dev/null
+++ b/test-all.sh
@@ -0,0 +1,37 @@
1#!/bin/sh
2
3export PATH=$PATH:/usr/local/bin
4
5pkill -f udplogger_receiver
6logger_dir=`mktemp -d /tmp/udplogger_test.XXXXXXXX`
7
8if [ ! -d "${logger_dir}" ]; then
9 echo "Couldn't create logger dir ${logger_dir}"
10fi
11
12logger_key="${logger_dir}/key.pem"
13logger_crt="${logger_dir}/public.pem"
14
15openssl genrsa -out "${logger_key}" 2048
16openssl rsa -in "${logger_key}" -outform PEM -pubout -out "${logger_crt}"
17
18ln -sf receiver udplogger_receiver
19./udplogger_receiver -k "${logger_key}" "${logger_dir}" &
20
21sleep 2
22
23./sender -h localhost -p 58132 -c "${logger_crt}" "${logger_dir}"
24
25pkill -f udplogger_receiver
26
27grep -qri Juchu "${logger_dir}"
28
29retval=$?
30
31#if [ "${retval}" -eq 0 ]; then
32# echo "Found"
33#fi
34
35rm -r "${logger_dir}"
36
37exit $retval