Commit f6e30b9e by michaelpastushkov

cipher update freq

parent a8080874
...@@ -63,7 +63,7 @@ static struct option long_options[] = { ...@@ -63,7 +63,7 @@ static struct option long_options[] = {
const char *name = NAME; const char *name = NAME;
unsigned char cipher[256]; unsigned char cipher[256];
int cipher_day; int cipher_time;
char *get_current_timestamp(void) { char *get_current_timestamp(void) {
static char date_str[20]; static char date_str[20];
...@@ -103,29 +103,29 @@ unsigned int get_hash(unsigned int source) { ...@@ -103,29 +103,29 @@ unsigned int get_hash(unsigned int source) {
return hash; return hash;
} }
int get_utc_day_of_month() { int get_time()
time_t now = time(NULL); {
struct tm *utc_time = gmtime(&now); time_t now = time(NULL);
return utc_time->tm_mday; return now / (60 * 10); /* changing cipher every 10 minutes */
} }
void update_cipher() { void update_cipher() {
int i; int i;
unsigned int seed; unsigned int seed;
int day = get_utc_day_of_month(); int time = get_time();
if (day == cipher_day) if (time == cipher_time)
return; return;
for (i = 0; i < 256; i++) for (i = 0; i < 256; i++)
cipher[i] = i; cipher[i] = i;
seed = get_hash(options.secret * day); seed = get_hash(options.secret * time);
shuffle(cipher, sizeof(cipher), seed); shuffle(cipher, sizeof(cipher), seed);
if (options.log) { if (options.log > 1) {
printf("%s new cipher %i\n", get_current_timestamp(), day); printf("%s new cipher %i\n", get_current_timestamp(), time);
if (options.log > 2) { if (options.log > 2) {
for (i=0; i<256; i++) { for (i=0; i<256; i++) {
printf("%d ", cipher[i]); printf("%d ", cipher[i]);
...@@ -134,7 +134,7 @@ void update_cipher() { ...@@ -134,7 +134,7 @@ void update_cipher() {
} }
} }
} }
cipher_day = day; cipher_time = time;
} }
int encode(unsigned char* buf, int len) { int encode(unsigned char* buf, int len) {
...@@ -236,7 +236,7 @@ int build_tcp(void) { ...@@ -236,7 +236,7 @@ int build_tcp(void) {
return 0; return 0;
} }
int wait_for_clients(void) { int wait_connection(void) {
#if defined(__MINGW32__) || defined(__CYGWIN__) #if defined(__MINGW32__) || defined(__CYGWIN__)
int client_addr_size; int client_addr_size;
...@@ -268,11 +268,11 @@ int wait_for_clients(void) { ...@@ -268,11 +268,11 @@ int wait_for_clients(void) {
return 0; return 0;
} }
int build_tunnel(void) { int build(void) {
rc.remote_host = gethostbyname(options.remote_host); rc.remote_host = gethostbyname(options.remote_host);
if (rc.remote_host == NULL) { if (rc.remote_host == NULL) {
perror("build_tunnel: gethostbyname()"); perror("build: gethostbyname()");
return 1; return 1;
} }
...@@ -283,20 +283,20 @@ int build_tunnel(void) { ...@@ -283,20 +283,20 @@ int build_tunnel(void) {
rc.remote_socket = socket(AF_INET, options.proto, 0); rc.remote_socket = socket(AF_INET, options.proto, 0);
if (rc.remote_socket < 0) { if (rc.remote_socket < 0) {
perror("build_tunnel: socket()"); perror("build: socket()");
return 1; return 1;
} }
if (options.proto == SOCK_STREAM) { if (options.proto == SOCK_STREAM) {
if (connect(rc.remote_socket, (struct sockaddr *)&rc.remote_addr, sizeof(rc.remote_addr)) < 0) { if (connect(rc.remote_socket, (struct sockaddr *)&rc.remote_addr, sizeof(rc.remote_addr)) < 0) {
perror("build_tunnel: connect()"); perror("build: connect()");
return 1; return 1;
} }
} }
return 0; return 0;
} }
int use_tunnel(void) int use(void)
{ {
fd_set io; fd_set io;
unsigned char buffer[options.buffer_size]; unsigned char buffer[options.buffer_size];
...@@ -314,7 +314,7 @@ int use_tunnel(void) ...@@ -314,7 +314,7 @@ int use_tunnel(void)
/* Waiting for data */ /* Waiting for data */
if (select(max_fd+1, &io, NULL, NULL, NULL) < 0) { if (select(max_fd+1, &io, NULL, NULL, NULL) < 0) {
perror("use_tunnel: select()"); perror("use: select()");
break; break;
} }
...@@ -326,7 +326,7 @@ int use_tunnel(void) ...@@ -326,7 +326,7 @@ int use_tunnel(void)
recv(rc.client_socket, buffer, sizeof(buffer), 0); recv(rc.client_socket, buffer, sizeof(buffer), 0);
if (count_recv < 0) { if (count_recv < 0) {
perror("use_tunnel: recv(rc.client_socket)"); perror("use: recv(rc.client_socket)");
close(rc.client_socket); close(rc.client_socket);
close(rc.remote_socket); close(rc.remote_socket);
return 1; return 1;
...@@ -358,7 +358,7 @@ int use_tunnel(void) ...@@ -358,7 +358,7 @@ int use_tunnel(void)
recv(rc.remote_socket, buffer, sizeof(buffer), 0); recv(rc.remote_socket, buffer, sizeof(buffer), 0);
if (count_recv < 0) { if (count_recv < 0) {
perror("use_tunnel: recv(rc.remote_socket)"); perror("use: recv(rc.remote_socket)");
close(rc.client_socket); close(rc.client_socket);
close(rc.remote_socket); close(rc.remote_socket);
return 1; return 1;
...@@ -384,25 +384,25 @@ int use_tunnel(void) ...@@ -384,25 +384,25 @@ int use_tunnel(void)
return 0; return 0;
} }
void handle_tunnel(void) { void run(void) {
if (build_tunnel() == 0) if (build() == 0)
use_tunnel(); use();
} }
void handle_client(void) { void serve(void) {
#ifdef __MINGW32__ #ifdef __MINGW32__
handle_tunnel(); run();
#else #else
if (options.fork) { if (options.fork) {
if (fork() == 0) { if (fork() == 0) {
close(rc.server_socket); close(rc.server_socket);
handle_tunnel(); run();
exit(0); exit(0);
} }
close(rc.client_socket); close(rc.client_socket);
} }
else { else {
handle_tunnel(); run();
} }
#endif #endif
} }
...@@ -457,7 +457,10 @@ void init_options() { ...@@ -457,7 +457,10 @@ void init_options() {
memset(&options, 0, sizeof(options)); memset(&options, 0, sizeof(options));
/* defaults */ /* defaults */
options.buffer_size = MINIMUM_BUFFER_SIZE; options.local_port = LOCAL_PORT;
options.remote_host = REMOTE_HOST;
options.remote_port = REMOTE_PORT;
options.buffer_size = BUFFER_SIZE;
options.bind_address = "0.0.0.0"; options.bind_address = "0.0.0.0";
options.proto = SOCK_STREAM; options.proto = SOCK_STREAM;
options.log = 1; options.log = 1;
...@@ -638,9 +641,9 @@ int main(int argc, char *argv[]) { ...@@ -638,9 +641,9 @@ int main(int argc, char *argv[]) {
do do
{ {
if (wait_for_clients() == 0) if (wait_connection() == 0)
{ {
handle_client(); serve();
} }
} }
while (options.stay_alive); while (options.stay_alive);
......
...@@ -3,6 +3,13 @@ ...@@ -3,6 +3,13 @@
#define NAME "bytevia" #define NAME "bytevia"
#define VERSION "1.0" #define VERSION "1.0"
#define REMOTE_HOST "p4pn.net"
#define REMOTE_PORT 1948
#define LOCAL_PORT 1984
#define BUFFER_SIZE 4096
#define MODE_CLIENT 0
#define MODE_SERVER 1
#define LOCAL_PORT_OPTION 'a' #define LOCAL_PORT_OPTION 'a'
#define REMOTE_HOST_OPTION 'b' #define REMOTE_HOST_OPTION 'b'
...@@ -20,12 +27,6 @@ ...@@ -20,12 +27,6 @@
#define SECRET_OPTION 'n' #define SECRET_OPTION 'n'
#define PROTO_OPTION 'o' #define PROTO_OPTION 'o'
#define MODE_CLIENT 0
#define MODE_SERVER 1
#define MINIMUM_BUFFER_SIZE 4096
#define PATH_SEPARATOR '/'
struct struct_options { struct struct_options {
unsigned int local_port; unsigned int local_port;
char *remote_host; char *remote_host;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment