#include <linux/if_tun.h>
#endif
-#include <getopt.h>
-
-#ifdef HAVE_WINDOWS
-#include <pthread.h>
-#endif
-
#include "crypto.h"
#include "ecdsa.h"
#include "meta.h"
#include "sptps.h"
#include "utils.h"
#include "names.h"
+#include "random.h"
#ifndef HAVE_WINDOWS
#define closesocket(s) close(s)
static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
(void)type;
- char hex[len * 2 + 1];
+ char *hex = alloca(len * 2 + 1);
bin2hex(data, hex, len);
if(verbose) {
// separate thread between the stdin and the sptps loop way below. This thread
// reads stdin and sends its content to the main thread through a TCP socket,
// which can be properly select()'ed.
-static void *stdin_reader_thread(void *arg) {
+static DWORD WINAPI stdin_reader_thread(LPVOID arg) {
struct sockaddr_in sa;
socklen_t sa_size = sizeof(sa);
closesocket(stdin_sock_fd);
stdin_sock_fd = -1;
- return NULL;
+ return 0;
}
static int start_input_reader(void) {
fprintf(stderr, "stdin thread is listening on :%d\n", ntohs(connect_sa.sin_port));
}
- pthread_t th;
- int err = pthread_create(&th, NULL, stdin_reader_thread, NULL);
-
- if(err) {
- fprintf(stderr, "Could not start reader thread: %s\n", strerror(err));
+ if(!CreateThread(NULL, 0, stdin_reader_thread, NULL, 0, NULL)) {
+ fprintf(stderr, "Could not start reader thread: %d\n", GetLastError());
goto server_err;
}
#endif // HAVE_WINDOWS
-int main(int argc, char *argv[]) {
+static void print_listening_msg(int sock) {
+ sockaddr_t sa = {0};
+ socklen_t salen = sizeof(sa);
+ int port = 0;
+
+ if(!getsockname(sock, &sa.sa, &salen)) {
+ port = ntohs(sa.in.sin_port);
+ }
+
+ fprintf(stderr, "Listening on %d...\n", port);
+ fflush(stderr);
+}
+
+static int run_test(int argc, char *argv[]) {
program_name = argv[0];
bool initiator = false;
bool datagram = false;
return 1;
}
- fprintf(stderr, "Listening...\n");
+ print_listening_msg(sock);
sock = accept(sock, NULL, NULL);
return 1;
}
} else {
- fprintf(stderr, "Listening...\n");
+ print_listening_msg(sock);
char buf[65536];
struct sockaddr addr;
fprintf(stderr, "Connected\n");
}
- crypto_init();
- prng_init();
-
FILE *fp = fopen(argv[1], "r");
if(!fp) {
if(!fp) {
fprintf(stderr, "Could not open %s: %s\n", argv[2], strerror(errno));
- free(mykey);
+ ecdsa_free(mykey);
return 1;
}
ecdsa_t *hiskey = NULL;
if(!(hiskey = ecdsa_read_pem_public_key(fp))) {
- free(mykey);
+ ecdsa_free(mykey);
return 1;
}
sptps_t s;
if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record)) {
- free(mykey);
- free(hiskey);
+ ecdsa_free(mykey);
+ ecdsa_free(hiskey);
return 1;
}
if(in < 0) {
fprintf(stderr, "Could not init stdin reader thread\n");
- free(mykey);
- free(hiskey);
+ ecdsa_free(mykey);
+ ecdsa_free(hiskey);
return 1;
}
}
FD_SET(sock, &fds);
if(select(max_fd + 1, &fds, NULL, NULL, NULL) <= 0) {
- free(mykey);
- free(hiskey);
+ ecdsa_free(mykey);
+ ecdsa_free(hiskey);
return 1;
}
if(len < 0) {
fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
- free(mykey);
- free(hiskey);
+ ecdsa_free(mykey);
+ ecdsa_free(hiskey);
return 1;
}
sptps_send_record(&s, 0, buf, len);
}
} else if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof(buf) : (size_t)len)) {
- free(mykey);
- free(hiskey);
+ ecdsa_free(mykey);
+ ecdsa_free(hiskey);
return 1;
}
}
if(len < 0) {
fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno));
- free(mykey);
- free(hiskey);
+ ecdsa_free(mykey);
+ ecdsa_free(hiskey);
return 1;
}
}
if(verbose) {
- char hex[len * 2 + 1];
+ char *hex = alloca(len * 2 + 1);
bin2hex(buf, hex, len);
fprintf(stderr, "Received %ld bytes of data:\n%s\n", (long)len, hex);
}
if(!done) {
if(!datagram) {
- free(mykey);
- free(hiskey);
+ ecdsa_free(mykey);
+ ecdsa_free(hiskey);
return 1;
}
}
bool stopped = sptps_stop(&s);
- free(mykey);
- free(hiskey);
+ ecdsa_free(mykey);
+ ecdsa_free(hiskey);
+ closesocket(sock);
- if(!stopped) {
- return 1;
- }
+ return !stopped;
+}
- closesocket(sock);
+int main(int argc, char *argv[]) {
+ random_init();
+ crypto_init();
+ prng_init();
- return 0;
+ int result = run_test(argc, argv);
+
+ random_exit();
+
+ return result;
}