From 33eae55c5bf9e8f6edea4d2482c879ee8e7391dc Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Fri, 27 Sep 2013 22:04:34 -0500 Subject: [PATCH] clean version barring the fact that there is a free() error on close, have to look into it --- src/Makefile | 3 +- src/crypto.h | 2 +- src/sendfile | 106 ----------------------------------------- src/udtcat.cpp | 48 +++++++++---------- src/udtcat.h | 1 + src/udtcat_client.cpp | 9 ++-- src/udtcat_server.cpp | 6 +-- src/udtcat_threads.cpp | 56 ++++++++++++++++------ 8 files changed, 74 insertions(+), 157 deletions(-) delete mode 100755 src/sendfile diff --git a/src/Makefile b/src/Makefile index 6f6a1b3..a019854 100644 --- a/src/Makefile +++ b/src/Makefile @@ -41,13 +41,14 @@ DIR = $(shell pwd) APP = udtcat APPOUT = uc -all: $(APP) +all: $(APP) sendfile %.o: %.cpp $(C++) $(CCFLAGS) $< -c udtcat: udtcat.o udtcat_threads.o udtcat_server.o udtcat_client.o crypto.o $(C++) $^ -o $(APPOUT) $(LDFLAGS) + cp $(APPOUT) $(APP) clean: rm -f *.o $(APPOUT) diff --git a/src/crypto.h b/src/crypto.h index 2678ab7..a1ca161 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -18,7 +18,7 @@ and limitations under the License. #ifndef CRYPTO_H #define CRYPTO_H -#define N_CRYPTO_THREADS 16 +#define N_CRYPTO_THREADS 8 #define PASSPHRASE_SIZE 32 #define HEX_PASSPHRASE_SIZE 64 diff --git a/src/sendfile b/src/sendfile deleted file mode 100755 index 75e64e4..0000000 --- a/src/sendfile +++ /dev/null @@ -1,106 +0,0 @@ -#!/bin/bash -function Usage(){ -cat <<-ENDOFMESSAGE - -$0 [options] ip source destination - -options: - - -p --port specify port to use [default: 9000] - -u --user specify username for ssh connection [default: current user] - -h --help display this message - -ENDOFMESSAGE - exit 1 -} - -function error(){ - echo "$*" - exit 1 -} - -function argCheck(){ - if [ $1 -eq 0 -o "${1:0:1}" = "-" ]; then - error "The ${opt} option requires an argument." - fi -} - -function getOpts() { - port=9000 - user=`whoami` - verbose=0 - argv=() - while [ $# -gt 0 ] - do - opt=$1 - shift - case ${opt} in - -p|--port) - argCheck $# - port="$1" - shift;; - -u|--user) - argCheck $# - user="$1" - shift;; - -h|--help) - Usage;; - -v|--verbose) - verbose=1;; - -c|--remote-uc) - remote="$1" - shift;; - *) - if [ "${opt:0:1}" = "-" ]; then - error "${opt}: unknown option." - fi - argv+=(${opt});; - esac - done -} - -getOpts $* - -ip="${argv[0]}" -src="${argv[1]}" -dest="${argv[2]}" - -if [ "$ip" = "" ]; then - error "Please specify the ip address" -fi -if [ "$src" = "" ]; then - error "Please specify the source file" -fi -if [ "$dest" = "" ]; then - error "Please specify the destination file" -fi - -if [ "$verbose" == 1 ]; then - cat <<-ENDOFMESSAGE - Transferring - local file : $src - remote dest : $dest - host : $user@$ip - port : $port - -ENDOFMESSAGE -fi - -if [ "$verbose" == 1 ]; then - echo "Sending... " -fi - -# echo ssh $user@$ip "~/udtcat/src/uc -l $port > $dest" -# ssh $user@$ip "~/udtcat/src/uc -l $port > $dest" & -ssh jmiller@190.103.184.11 "~/udtcat/src/uc -l 9000 \> /home/jmiller/test2 & " & -sleep 10 -head $src -uc $ip $port < $src - -if [ "$verbose" == 1 ]; then - echo "Done." - fs=`du -h $src` - echo "Original file size : $fs" - fs=`ssh $user@$ip "du -h $dest"` - echo "Transferred file size: $fs" -fi diff --git a/src/udtcat.cpp b/src/udtcat.cpp index 4f58dc2..026ead5 100644 --- a/src/udtcat.cpp +++ b/src/udtcat.cpp @@ -63,8 +63,8 @@ int main(int argc, char *argv[]){ initialize_thread_args(&args); int use_crypto = 0; int verbose = 0; - char* path_to_password = NULL; - char* password = NULL; + char* path_to_key = NULL; + char* key = NULL; // ----------- [ Read in options while ((opt = getopt (argc, argv, "hvnlp:f:")) != -1){ @@ -84,11 +84,11 @@ int main(int argc, char *argv[]){ break; case 'p': - password = strdup(optarg); + key = strdup(optarg); break; case 'f': - path_to_password = strdup(optarg); + path_to_key = strdup(optarg); break; case 'h': @@ -103,34 +103,34 @@ int main(int argc, char *argv[]){ } } - if (use_crypto && (path_to_password && password)){ - fprintf(stderr, "error: Please specify either password or password file, not both.\n"); + if (use_crypto && (path_to_key && key)){ + fprintf(stderr, "error: Please specify either key or key file, not both.\n"); exit(1); } - if (path_to_password){ - FILE*password_file = fopen(path_to_password, "r"); - if (!password_file){ - fprintf(stderr, "password file: %s.\n", strerror(errno)); + if (path_to_key){ + FILE*key_file = fopen(path_to_key, "r"); + if (!key_file){ + fprintf(stderr, "key file: %s.\n", strerror(errno)); exit(1); } - fseek(password_file, 0, SEEK_END); - long size = ftell(password_file); - fseek(password_file, 0, SEEK_SET); - password = (char*)malloc(size); - fread(password, 1, size, password_file); + fseek(key_file, 0, SEEK_END); + long size = ftell(key_file); + fseek(key_file, 0, SEEK_SET); + key = (char*)malloc(size); + fread(key, 1, size, key_file); } - if (!use_crypto && password){ - fprintf(stderr, "warning: You've specified a password, but you don't have encryption turned on.\nProceeding without encryption.\n"); + if (!use_crypto && key){ + fprintf(stderr, "warning: You've specified a key, but you don't have encryption turned on.\nProceeding without encryption.\n"); } - if (use_crypto && !password){ + if (use_crypto && !key){ fprintf(stderr, "Please either: \n (1) %s\n (2) %s\n (3) %s\n", - "include password in cli [-p password]", - "read on in from file [-f /path/to/password/file]", + "include key in cli [-p key]", + "read on in from file [-f /path/to/key/file]", "choose not to use encryption, remove [-n]"); exit(1); } @@ -157,9 +157,8 @@ int main(int argc, char *argv[]){ if (use_crypto){ char* cipher = (char*) "aes-128"; - crypto enc(EVP_ENCRYPT, PASSPHRASE_SIZE, (unsigned char*)password, cipher); - crypto dec(EVP_DECRYPT, PASSPHRASE_SIZE, (unsigned char*)password, cipher); - memset(password, 0, strlen(password)); + crypto enc(EVP_ENCRYPT, PASSPHRASE_SIZE, (unsigned char*)key, cipher); + crypto dec(EVP_DECRYPT, PASSPHRASE_SIZE, (unsigned char*)key, cipher); args.enc = &enc; args.dec = &dec; @@ -170,7 +169,8 @@ int main(int argc, char *argv[]){ } - + if (key) + memset(key, 0, strlen(key)); // ----------- [ Spawn correct process if (operation == SERVER){ diff --git a/src/udtcat.h b/src/udtcat.h index 81470de..dc4df12 100644 --- a/src/udtcat.h +++ b/src/udtcat.h @@ -29,6 +29,7 @@ and limitations under the License. #include "udtcat_threads.h" #include "crypto.h" +/* #define BUFF_SIZE 327680 */ #define BUFF_SIZE 67108864 typedef struct rs_args{ diff --git a/src/udtcat_client.cpp b/src/udtcat_client.cpp index 43ef36c..901b42b 100644 --- a/src/udtcat_client.cpp +++ b/src/udtcat_client.cpp @@ -63,7 +63,7 @@ int run_client(thread_args *args) hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; - + if (0 != getaddrinfo(NULL, port, &hints, &local)){ cerr << "incorrect network address.\n" << endl; return 1; @@ -85,7 +85,7 @@ int run_client(thread_args *args) UDT::setsockopt(client, 0, UDT_SNDBUF, &udt_buff, sizeof(int)); UDT::setsockopt(client, 0, UDP_SNDBUF, &udp_buff, sizeof(int)); - freeaddrinfo(local); + // freeaddrinfo(local); if (0 != getaddrinfo(ip, port, &hints, &peer)) { cerr << "incorrect server/peer address. " << ip << ":" << port << endl; @@ -127,7 +127,7 @@ int run_client(thread_args *args) send_args.verbose = args->verbose; send_args.c = args->enc; - freeaddrinfo(peer); + // freeaddrinfo(peer); if (blast) { CUDPBlast* cchandle = NULL; @@ -142,9 +142,8 @@ int run_client(thread_args *args) void * retval; pthread_join(sndthread, &retval); - UDT::close(client); - UDT::cleanup(); + return 0; } diff --git a/src/udtcat_server.cpp b/src/udtcat_server.cpp index 6e14a8f..0cb697c 100644 --- a/src/udtcat_server.cpp +++ b/src/udtcat_server.cpp @@ -147,15 +147,11 @@ int run_server(thread_args *args){ send_args.verbose = args->verbose; send_args.c = args->enc; - // pthread_create(&sndthread, NULL, senddata, new UDTSOCKET(recver)); pthread_create(&sndthread, NULL, senddata, &send_args); - pthread_join(sndthread, NULL); - UDT::close(serv); - UDT::cleanup(); - freeaddrinfo(res); + return 0; } diff --git a/src/udtcat_threads.cpp b/src/udtcat_threads.cpp index c2ae039..cb17795 100644 --- a/src/udtcat_threads.cpp +++ b/src/udtcat_threads.cpp @@ -38,6 +38,7 @@ const int ECONNLOST = 2001; using std::cerr; using std::endl; +int READ_IN = 0; void print_bytes(const void *object, size_t size) { @@ -100,11 +101,14 @@ void* recvdata(void * _args) } 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; + int new_block = 1; int block_size = 0; int offset = sizeof(int)/sizeof(char); @@ -123,9 +127,11 @@ void* recvdata(void * _args) rs = UDT::recv(recver, (char*)&block_size, offset, 0); if (UDT::ERROR == rs) { - if (UDT::getlasterror().getErrorCode() != ECONNLOST) + if (UDT::getlasterror().getErrorCode() != ECONNLOST){ cerr << "recv:" << UDT::getlasterror().getErrorMessage() << endl; - exit(1); + exit(1); + } + exit(0); } new_block = 0; @@ -139,8 +145,11 @@ void* recvdata(void * _args) if (UDT::ERROR == rs) { - UDT::close(recver); - return NULL; + if (UDT::getlasterror().getErrorCode() != ECONNLOST){ + cerr << "recv:" << UDT::getlasterror().getErrorMessage() << endl; + exit(1); + } + exit(0); } @@ -172,9 +181,6 @@ void* recvdata(void * _args) buffer_cursor = 0; crypto_cursor = 0; new_block = 1; - - - } } @@ -187,8 +193,11 @@ void* recvdata(void * _args) rs = UDT::recv(recver, indata, BUFF_SIZE, 0); if (UDT::ERROR == rs) { - UDT::close(recver); - return NULL; + if (UDT::getlasterror().getErrorCode() != ECONNLOST){ + cerr << "recv:" << UDT::getlasterror().getErrorMessage() << endl; + exit(1); + } + exit(0); } written_bytes = write(fileno(stdout), indata, rs); @@ -232,9 +241,16 @@ void* senddata(void* _args) else local_openssl_version = 0; - UDT::send(client, (char*)&local_openssl_version, sizeof(long), 0); + if (UDT::send(client, (char*)&local_openssl_version, sizeof(long), 0) < 0){ + cerr << "send:" << UDT::getlasterror().getErrorMessage() << endl; + UDT::close(client); + exit(1); + } + while (!READ_IN){ + } + if (args->verbose) fprintf(stderr, "[send thread] Send thread listening on stdin.\n"); @@ -246,16 +262,17 @@ void* senddata(void* _args) bytes_read = read(fileno(stdin), outdata+offset, BUFF_SIZE); if(bytes_read < 0){ + cerr << "send:" << UDT::getlasterror().getErrorMessage() << endl; UDT::close(client); - return NULL; + exit(1); } if(bytes_read == 0) { + sleep(1); UDT::close(client); - return NULL; + exit(0); } - if(args->use_crypto){ *((int*)outdata) = bytes_read; @@ -282,7 +299,8 @@ void* senddata(void* _args) if (UDT::ERROR == (ss = UDT::send(client, outdata + ssize, bytes_read - ssize, 0))) { - fprintf(stderr, "send: Failed to send buffer.\n"); + + cerr << "send:" << UDT::getlasterror().getErrorMessage() << endl; return NULL; } @@ -299,11 +317,19 @@ void* senddata(void* _args) bytes_read = read(fileno(stdin), outdata, BUFF_SIZE); int ssize = 0; int ss; + + if(bytes_read == 0) { + UDT::close(client); + sleep(1); + exit(0); + } + + while(ssize < bytes_read) { if (UDT::ERROR == (ss = UDT::send(client, outdata + ssize, bytes_read - ssize, 0))) { - fprintf(stderr, "send: Failed to send buffer.\n"); + cerr << "send:" << UDT::getlasterror().getErrorMessage() << endl; return NULL; }