projects
/
tinc
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Move sys/mman.h into have.h
[tinc]
/
src
/
sptps_test.c
diff --git
a/src/sptps_test.c
b/src/sptps_test.c
index
87f9b51
..
0f62af0
100644
(file)
--- a/
src/sptps_test.c
+++ b/
src/sptps_test.c
@@
-1,6
+1,6
@@
/*
sptps_test.c -- Simple Peer-to-Peer Security test program
/*
sptps_test.c -- Simple Peer-to-Peer Security test program
- Copyright (C) 2011-20
14
Guus Sliepen <guus@tinc-vpn.org>
+ Copyright (C) 2011-20
22
Guus Sliepen <guus@tinc-vpn.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@
-31,27
+31,25
@@
#include "crypto.h"
#include "ecdsa.h"
#include "crypto.h"
#include "ecdsa.h"
+#include "meta.h"
+#include "protocol.h"
#include "sptps.h"
#include "utils.h"
#include "sptps.h"
#include "utils.h"
-#ifndef MAX
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-#endif
-
#ifndef HAVE_MINGW
#define closesocket(s) close(s)
#endif
// Symbols necessary to link with logger.o
#ifndef HAVE_MINGW
#define closesocket(s) close(s)
#endif
// Symbols necessary to link with logger.o
-bool send_request(
void
*c, const char *msg, ...) {
+bool send_request(
struct connection_t
*c, const char *msg, ...) {
(void)c;
(void)msg;
return false;
}
(void)c;
(void)msg;
return false;
}
-
struct list_t *connection_list = NULL
;
+
list_t connection_list
;
-bool send_meta(
void *c, const char *msg, in
t len) {
+bool send_meta(
struct connection_t *c, const void *msg, size_
t len) {
(void)c;
(void)msg;
(void)len;
(void)c;
(void)msg;
(void)len;
@@
-68,7
+66,7
@@
static bool readonly;
static bool writeonly;
static int in = 0;
static int out = 1;
static bool writeonly;
static int in = 0;
static int out = 1;
-
static
int addressfamily = AF_UNSPEC;
+int addressfamily = AF_UNSPEC;
static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
(void)type;
static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
(void)type;
@@
-76,13
+74,22
@@
static bool send_data(void *handle, uint8_t type, const void *data, size_t len)
bin2hex(data, hex, len);
if(verbose) {
bin2hex(data, hex, len);
if(verbose) {
- fprintf(stderr, "Sending %
d bytes of data:\n%s\n", (int
)len, hex);
+ fprintf(stderr, "Sending %
lu bytes of data:\n%s\n", (unsigned long
)len, hex);
}
const int *sock = handle;
}
const int *sock = handle;
+ const char *p = data;
+
+ while(len) {
+ ssize_t sent = send(*sock, p, len, 0);
- if((size_t)send(*sock, data, len, 0) != len) {
- return false;
+ if(sent <= 0) {
+ fprintf(stderr, "Error sending data: %s\n", strerror(errno));
+ return false;
+ }
+
+ p += sent;
+ len -= sent;
}
return true;
}
return true;
@@
-95,8
+102,22
@@
static bool receive_record(void *handle, uint8_t type, const void *data, uint16_
fprintf(stderr, "Received type %d record of %u bytes:\n", type, len);
}
fprintf(stderr, "Received type %d record of %u bytes:\n", type, len);
}
- if(!writeonly) {
- write(out, data, len);
+ if(writeonly) {
+ return true;
+ }
+
+ const char *p = data;
+
+ while(len) {
+ ssize_t written = write(out, p, len);
+
+ if(written <= 0) {
+ fprintf(stderr, "Error writing received data: %s\n", strerror(errno));
+ return false;
+ }
+
+ p += written;
+ len -= written;
}
return true;
}
return true;
@@
-117,9
+138,11
@@
static struct option const long_options[] = {
const char *program_name;
const char *program_name;
-static void usage() {
- fprintf(stderr, "Usage: %s [options] my_ed25519_key_file his_ed25519_key_file [host] port\n\n", program_name);
- fprintf(stderr, "Valid options are:\n"
+static void usage(void) {
+ static const char *message =
+ "Usage: %s [options] my_ed25519_key_file his_ed25519_key_file [host] port\n"
+ "\n"
+ "Valid options are:\n"
" -d, --datagram Enable datagram mode.\n"
" -q, --quit Quit when EOF occurs on stdin.\n"
" -r, --readonly Only send data from the socket to stdout.\n"
" -d, --datagram Enable datagram mode.\n"
" -q, --quit Quit when EOF occurs on stdin.\n"
" -r, --readonly Only send data from the socket to stdout.\n"
@@
-133,8
+156,10
@@
static void usage() {
" -v, --verbose Display debug messages.\n"
" -4 Use IPv4.\n"
" -6 Use IPv6.\n"
" -v, --verbose Display debug messages.\n"
" -4 Use IPv4.\n"
" -6 Use IPv6.\n"
- "\n");
- fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n");
+ "\n"
+ "Report bugs to tinc@tinc-vpn.org.\n";
+
+ fprintf(stderr, message, program_name);
}
#ifdef HAVE_MINGW
}
#ifdef HAVE_MINGW
@@
-146,7
+171,7
@@
int stdin_sock_fd = -1;
// 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.
// 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.
-void *stdin_reader_thread(void *arg) {
+
static
void *stdin_reader_thread(void *arg) {
struct sockaddr_in sa;
socklen_t sa_size = sizeof(sa);
struct sockaddr_in sa;
socklen_t sa_size = sizeof(sa);
@@
-203,9
+228,10
@@
void *stdin_reader_thread(void *arg) {
closesocket(stdin_sock_fd);
stdin_sock_fd = -1;
closesocket(stdin_sock_fd);
stdin_sock_fd = -1;
+ return NULL;
}
}
-
int start_input_reader(
) {
+
static int start_input_reader(void
) {
if(stdin_sock_fd != -1) {
fprintf(stderr, "stdin thread can only be started once.\n");
return -1;
if(stdin_sock_fd != -1) {
fprintf(stderr, "stdin thread can only be started once.\n");
return -1;
@@
-296,7
+322,6
@@
int main(int argc, char *argv[]) {
int packetloss = 0;
int r;
int option_index = 0;
int packetloss = 0;
int r;
int option_index = 0;
- ecdsa_t *mykey = NULL, *hiskey = NULL;
bool quit = false;
while((r = getopt_long(argc, argv, "dqrstwL:W:v46", long_options, &option_index)) != EOF) {
bool quit = false;
while((r = getopt_long(argc, argv, "dqrstwL:W:v46", long_options, &option_index)) != EOF) {
@@
-380,8
+405,6
@@
int main(int argc, char *argv[]) {
initiator = true;
}
initiator = true;
}
- srand(getpid());
-
#ifdef HAVE_LINUX
if(tun) {
#ifdef HAVE_LINUX
if(tun) {
@@
-433,6
+456,7
@@
int main(int argc, char *argv[]) {
if(sock < 0) {
fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno));
if(sock < 0) {
fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno));
+ freeaddrinfo(ai);
return 1;
}
return 1;
}
@@
-440,14
+464,24
@@
int main(int argc, char *argv[]) {
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
if(initiator) {
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
if(initiator) {
- if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
+ int res = connect(sock, ai->ai_addr, ai->ai_addrlen);
+
+ freeaddrinfo(ai);
+ ai = NULL;
+
+ if(res) {
fprintf(stderr, "Could not connect to peer: %s\n", sockstrerror(sockerrno));
return 1;
}
fprintf(stderr, "Connected\n");
} else {
fprintf(stderr, "Could not connect to peer: %s\n", sockstrerror(sockerrno));
return 1;
}
fprintf(stderr, "Connected\n");
} else {
- if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
+ int res = bind(sock, ai->ai_addr, ai->ai_addrlen);
+
+ freeaddrinfo(ai);
+ ai = NULL;
+
+ if(res) {
fprintf(stderr, "Could not bind socket: %s\n", sockstrerror(sockerrno));
return 1;
}
fprintf(stderr, "Could not bind socket: %s\n", sockstrerror(sockerrno));
return 1;
}
@@
-488,6
+522,7
@@
int main(int argc, char *argv[]) {
}
crypto_init();
}
crypto_init();
+ prng_init();
FILE *fp = fopen(argv[1], "r");
FILE *fp = fopen(argv[1], "r");
@@
-496,6
+531,8
@@
int main(int argc, char *argv[]) {
return 1;
}
return 1;
}
+ ecdsa_t *mykey = NULL;
+
if(!(mykey = ecdsa_read_pem_private_key(fp))) {
return 1;
}
if(!(mykey = ecdsa_read_pem_private_key(fp))) {
return 1;
}
@@
-506,10
+543,14
@@
int main(int argc, char *argv[]) {
if(!fp) {
fprintf(stderr, "Could not open %s: %s\n", argv[2], strerror(errno));
if(!fp) {
fprintf(stderr, "Could not open %s: %s\n", argv[2], strerror(errno));
+ free(mykey);
return 1;
}
return 1;
}
+ ecdsa_t *hiskey = NULL;
+
if(!(hiskey = ecdsa_read_pem_public_key(fp))) {
if(!(hiskey = ecdsa_read_pem_public_key(fp))) {
+ free(mykey);
return 1;
}
return 1;
}
@@
-522,6
+563,8
@@
int main(int argc, char *argv[]) {
sptps_t s;
if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record)) {
sptps_t s;
if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record)) {
+ free(mykey);
+ free(hiskey);
return 1;
}
return 1;
}
@@
-532,6
+575,8
@@
int main(int argc, char *argv[]) {
if(in < 0) {
fprintf(stderr, "Could not init stdin reader thread\n");
if(in < 0) {
fprintf(stderr, "Could not init stdin reader thread\n");
+ free(mykey);
+ free(hiskey);
return 1;
}
}
return 1;
}
}
@@
-558,6
+603,8
@@
int main(int argc, char *argv[]) {
FD_SET(sock, &fds);
if(select(max_fd + 1, &fds, NULL, NULL, NULL) <= 0) {
FD_SET(sock, &fds);
if(select(max_fd + 1, &fds, NULL, NULL, NULL) <= 0) {
+ free(mykey);
+ free(hiskey);
return 1;
}
return 1;
}
@@
-570,6
+617,8
@@
int main(int argc, char *argv[]) {
if(len < 0) {
fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
if(len < 0) {
fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
+ free(mykey);
+ free(hiskey);
return 1;
}
return 1;
}
@@
-600,6
+649,8
@@
int main(int argc, char *argv[]) {
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)) {
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);
return 1;
}
}
return 1;
}
}
@@
-609,6
+660,8
@@
int main(int argc, char *argv[]) {
if(len < 0) {
fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno));
if(len < 0) {
fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno));
+ free(mykey);
+ free(hiskey);
return 1;
}
return 1;
}
@@
-620,10
+673,10
@@
int main(int argc, char *argv[]) {
if(verbose) {
char hex[len * 2 + 1];
bin2hex(buf, hex, len);
if(verbose) {
char hex[len * 2 + 1];
bin2hex(buf, hex, len);
- fprintf(stderr, "Received %
d bytes of data:\n%s\n", (int
)len, hex);
+ fprintf(stderr, "Received %
ld bytes of data:\n%s\n", (long
)len, hex);
}
}
- if(packetloss && (
rand() %
100) < packetloss) {
+ if(packetloss && (
int)prng(
100) < packetloss) {
if(verbose) {
fprintf(stderr, "Dropped.\n");
}
if(verbose) {
fprintf(stderr, "Dropped.\n");
}
@@
-638,17
+691,24
@@
int main(int argc, char *argv[]) {
if(!done) {
if(!datagram) {
if(!done) {
if(!datagram) {
+ free(mykey);
+ free(hiskey);
return 1;
}
}
bufp += done;
return 1;
}
}
bufp += done;
- len -= done;
+ len -=
(ssize_t)
done;
}
}
}
}
}
}
- if(!sptps_stop(&s)) {
+ bool stopped = sptps_stop(&s);
+
+ free(mykey);
+ free(hiskey);
+
+ if(!stopped) {
return 1;
}
return 1;
}