Skip to content

Commit

Permalink
addition of timeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
millerjs committed Nov 10, 2013
1 parent be323a3 commit a20fa06
Show file tree
Hide file tree
Showing 22 changed files with 74 additions and 382 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Client side:
-p key turn on encryption and specify key in-line
-f path turn on encryption, path=path to key file
-v verbose

-t timeout force udpipe to timeout if no data transfered

### Basic exmple (unencrypted)

Expand Down
19 changes: 17 additions & 2 deletions src/udpipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ using std::endl;
void usage(){
fprintf(stderr, "usage: udpipe [udpipe options] host port\n");
fprintf(stderr, "options:\n");
// fprintf(stderr, "\t\t%s %s\t%s\n", "", "", "");
fprintf(stderr, "\t-l \t\t\tlisten for connection\n");
fprintf(stderr, "\t-n n_crypto_threads \tset number of encryption threads per send/recv thread to n_crypto_threads\n");
fprintf(stderr, "\t-p key \t\t\tturn on encryption and specify key in-line\n");
fprintf(stderr, "\t-f path \t\tturn on encryption, path=path to key file\n");
fprintf(stderr, "\t-v verbose\n");
fprintf(stderr, "\t-t timeout\t\tforce udpipe to timeout if no data transfered\n");


exit(1);
}
Expand All @@ -49,6 +55,7 @@ void initialize_thread_args(thread_args *args){
args->verbose = 0;
args->n_crypto_threads = 1;
args->print_speed = 0;
args->timeout = 0;
}

int main(int argc, char *argv[]){
Expand All @@ -65,13 +72,17 @@ int main(int argc, char *argv[]){
int n_crypto_threads = 1;

// ----------- [ Read in options
while ((opt = getopt (argc, argv, "hvsn:lp:f:")) != -1){
while ((opt = getopt (argc, argv, "t:hvsn:lp:f:")) != -1){
switch (opt){

case 's':
args.print_speed = 1;
break;

case 't':
args.timeout = atoi(optarg);
break;

case 'l':
operation = SERVER;
break;
Expand Down Expand Up @@ -152,6 +163,10 @@ int main(int argc, char *argv[]){
}
}

if (args.verbose)
fprintf(stderr, "Timeout set to %d seconds\n", args.timeout);


// Check port input
if (optind < argc){
args.port = strdup(argv[optind++]);
Expand Down
3 changes: 2 additions & 1 deletion src/udpipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef struct rs_args{
int use_crypto;
int verbose;
int n_crypto_threads;

int timeout;
} rs_args;

typedef struct thread_args{
Expand All @@ -55,6 +55,7 @@ typedef struct thread_args{
int verbose;
int n_crypto_threads;
int print_speed;
int timeout;
} thread_args;

void* send_buf_threaded(void*_args);
2 changes: 2 additions & 0 deletions src/udpipe_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ int run_client(thread_args *args)
rcvargs.verbose = args->verbose;
rcvargs.n_crypto_threads = args->n_crypto_threads;
rcvargs.c = args->dec;
rcvargs.timeout = args->timeout;

pthread_create(&rcvthread, NULL, recvdata, &rcvargs);
pthread_detach(rcvthread);
Expand All @@ -128,6 +129,7 @@ int run_client(thread_args *args)
send_args.verbose = args->verbose;
send_args.n_crypto_threads = args->n_crypto_threads;
send_args.c = args->enc;
send_args.timeout = args->timeout;

// freeaddrinfo(peer);

Expand Down
3 changes: 3 additions & 0 deletions src/udpipe_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ int run_server(thread_args *args){
rcvargs.verbose = args->verbose;
rcvargs.n_crypto_threads = args->n_crypto_threads;
rcvargs.c = args->dec;
rcvargs.timeout = args->timeout;

pthread_create(&rcvthread, NULL, recvdata, &rcvargs);
pthread_detach(rcvthread);
Expand All @@ -148,6 +149,8 @@ int run_server(thread_args *args){
send_args.verbose = args->verbose;
send_args.n_crypto_threads = args->n_crypto_threads;
send_args.c = args->enc;
send_args.timeout = args->timeout;


if (args->print_speed){
pthread_t mon_thread;
Expand Down
89 changes: 49 additions & 40 deletions src/udpipe_threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@ void print_bytes(const void *object, size_t size)
fprintf(stderr, "]\n");
}

int timeout_sem;
void *monitor_timeout(void* arg) {

int timeout = *(int*) arg;

while (1){

sleep(timeout);
if (timeout_sem == 0){
fprintf(stderr, "Exiting on timeout.\n");
exit(0);

} else {
// continue on as normal
}

// If timeout_sem == 2, the connection has not been made -> no timeout next round
if (timeout_sem != 2)
timeout_sem = 0;

}
}



void send_full(UDTSOCKET sock, char* buffer, int len){

int sent = 0;
Expand Down Expand Up @@ -112,11 +137,15 @@ void auth_peer(rs_args* args)
// fprintf(stderr, "key: "); print_bytes(key, 16);
// fprintf(stderr, "signed_key: "); print_bytes(signed_key, 16);

int crypt_len = KEY_LEN/4;
for (int i = 0; i < crypt_len; i += crypt_len)
pass_to_enc_thread(signed_key+i, signed_key+i, crypt_len, args->c);

int crypt_len = KEY_LEN/args->n_crypto_threads;
for (int i = 0; i < args->n_crypto_threads; i ++)
pass_to_enc_thread(signed_key+i*crypt_len, signed_key+i*crypt_len,
crypt_len, args->c);

join_all_encryption_threads(args->c);


if (memcmp(key, signed_key, KEY_LEN)){
fprintf(stderr, "Authorization failed\n");
exit(1);
Expand All @@ -133,16 +162,13 @@ void sign_auth(rs_args* args)

recv_full(*args->usocket, key, KEY_LEN);

// fprintf(stderr, "signing: "); print_bytes(key, 16);

int crypt_len = KEY_LEN/4;
for (int i = 0; i < crypt_len; i += crypt_len)
pass_to_enc_thread(key+i, key+i, crypt_len, args->c);
int crypt_len = KEY_LEN/args->n_crypto_threads;
for (int i = 0; i < args->n_crypto_threads; i ++)
pass_to_enc_thread(key+i*crypt_len, key+i*crypt_len,
crypt_len, args->c);

join_all_encryption_threads(args->c);

// fprintf(stderr, "signed: "); print_bytes(key, 16);

send_full(*args->usocket, key, KEY_LEN);

signed_auth = 1;
Expand Down Expand Up @@ -175,38 +201,14 @@ void* recvdata(void * _args)
if (args->use_crypto)
auth_peer(args);

// if (args->verbose)
// fprintf(stderr, "[recv thread] Checking encryption...\n");

// long remote_ssl_version = 0;
// int rs = UDT::recv(recver, (char*)&remote_ssl_version, sizeof(long), 0);
timeout_sem = 2;
// Create a monitor thread to watch for timeouts
if (args->timeout > 0){
pthread_t monitor_thread;
pthread_create(&monitor_thread, NULL, &monitor_timeout, &args->timeout);

// if (UDT::ERROR == rs) {
// if (UDT::getlasterror().getErrorCode() != ECONNLOST)
// cerr << "recv:" << UDT::getlasterror().getErrorMessage() <<
// "Unable to determine remote crypto method" << endl;
// exit(1);
// }
}

// if (args->use_crypto){
// if (remote_ssl_version == 0){
// cerr << "recv: Encryption mismatch: local[None] to remote[OpenSSL]" << endl;
// UDT::close(recver);
// exit(1);
// }

// if (remote_ssl_version != OPENSSL_VERSION_NUMBER){
// // versions don't match
// }

// } else {
// if (remote_ssl_version != 0) {
// cerr << "recv: Encryption mismatch: local[OpenSSL] to remote[None]" << endl;
// write(fileno(stderr), &remote_ssl_version, sizeof(long));
// UDT::close(recver);
// exit(1);
// }
// }

READ_IN = 1;

Expand All @@ -218,6 +220,10 @@ void* recvdata(void * _args)
if (args->verbose)
fprintf(stderr, "[recv thread] Listening on receive thread.\n");

// Set monitor thread to expect a timeout
if (args->timeout)
timeout_sem = 1;

if(args->use_crypto) {
while(true) {
int rs;
Expand Down Expand Up @@ -253,6 +259,9 @@ void* recvdata(void * _args)
exit(0);
}

// Cancel timeout for another args->timeout seconds
if (args->timeout)
timeout_sem = 1;

buffer_cursor += rs;

Expand Down
Binary file removed udt/src/api.o
Binary file not shown.
Binary file removed udt/src/buffer.o
Binary file not shown.
Binary file removed udt/src/cache.o
Binary file not shown.
Binary file removed udt/src/ccc.o
Binary file not shown.
Binary file removed udt/src/channel.o
Binary file not shown.
Binary file removed udt/src/common.o
Binary file not shown.
Binary file removed udt/src/core.o
Binary file not shown.
Binary file removed udt/src/epoll.o
Binary file not shown.
Binary file removed udt/src/libudt.a
Binary file not shown.
Binary file removed udt/src/libudt.so
Binary file not shown.
Binary file removed udt/src/list.o
Binary file not shown.
Binary file removed udt/src/md5.o
Binary file not shown.
Binary file removed udt/src/packet.o
Binary file not shown.
Binary file removed udt/src/queue.o
Binary file not shown.
Loading

0 comments on commit a20fa06

Please sign in to comment.