Commit f6e30b9e by michaelpastushkov

cipher update freq

parent a8080874
......@@ -63,7 +63,7 @@ static struct option long_options[] = {
const char *name = NAME;
unsigned char cipher[256];
int cipher_day;
int cipher_time;
char *get_current_timestamp(void) {
static char date_str[20];
......@@ -103,29 +103,29 @@ unsigned int get_hash(unsigned int source) {
return hash;
}
int get_utc_day_of_month() {
time_t now = time(NULL);
struct tm *utc_time = gmtime(&now);
return utc_time->tm_mday;
int get_time()
{
time_t now = time(NULL);
return now / (60 * 10); /* changing cipher every 10 minutes */
}
void update_cipher() {
int i;
unsigned int seed;
int day = get_utc_day_of_month();
if (day == cipher_day)
int time = get_time();
if (time == cipher_time)
return;
for (i = 0; i < 256; i++)
cipher[i] = i;
seed = get_hash(options.secret * day);
seed = get_hash(options.secret * time);
shuffle(cipher, sizeof(cipher), seed);
if (options.log) {
printf("%s new cipher %i\n", get_current_timestamp(), day);
if (options.log > 1) {
printf("%s new cipher %i\n", get_current_timestamp(), time);
if (options.log > 2) {
for (i=0; i<256; i++) {
printf("%d ", cipher[i]);
......@@ -134,7 +134,7 @@ void update_cipher() {
}
}
}
cipher_day = day;
cipher_time = time;
}
int encode(unsigned char* buf, int len) {
......@@ -236,7 +236,7 @@ int build_tcp(void) {
return 0;
}
int wait_for_clients(void) {
int wait_connection(void) {
#if defined(__MINGW32__) || defined(__CYGWIN__)
int client_addr_size;
......@@ -268,11 +268,11 @@ int wait_for_clients(void) {
return 0;
}
int build_tunnel(void) {
int build(void) {
rc.remote_host = gethostbyname(options.remote_host);
if (rc.remote_host == NULL) {
perror("build_tunnel: gethostbyname()");
perror("build: gethostbyname()");
return 1;
}
......@@ -283,20 +283,20 @@ int build_tunnel(void) {
rc.remote_socket = socket(AF_INET, options.proto, 0);
if (rc.remote_socket < 0) {
perror("build_tunnel: socket()");
perror("build: socket()");
return 1;
}
if (options.proto == SOCK_STREAM) {
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 0;
}
int use_tunnel(void)
int use(void)
{
fd_set io;
unsigned char buffer[options.buffer_size];
......@@ -314,7 +314,7 @@ int use_tunnel(void)
/* Waiting for data */
if (select(max_fd+1, &io, NULL, NULL, NULL) < 0) {
perror("use_tunnel: select()");
perror("use: select()");
break;
}
......@@ -326,7 +326,7 @@ int use_tunnel(void)
recv(rc.client_socket, buffer, sizeof(buffer), 0);
if (count_recv < 0) {
perror("use_tunnel: recv(rc.client_socket)");
perror("use: recv(rc.client_socket)");
close(rc.client_socket);
close(rc.remote_socket);
return 1;
......@@ -358,7 +358,7 @@ int use_tunnel(void)
recv(rc.remote_socket, buffer, sizeof(buffer), 0);
if (count_recv < 0) {
perror("use_tunnel: recv(rc.remote_socket)");
perror("use: recv(rc.remote_socket)");
close(rc.client_socket);
close(rc.remote_socket);
return 1;
......@@ -384,25 +384,25 @@ int use_tunnel(void)
return 0;
}
void handle_tunnel(void) {
if (build_tunnel() == 0)
use_tunnel();
void run(void) {
if (build() == 0)
use();
}
void handle_client(void) {
void serve(void) {
#ifdef __MINGW32__
handle_tunnel();
run();
#else
if (options.fork) {
if (fork() == 0) {
close(rc.server_socket);
handle_tunnel();
run();
exit(0);
}
close(rc.client_socket);
}
else {
handle_tunnel();
run();
}
#endif
}
......@@ -457,7 +457,10 @@ void init_options() {
memset(&options, 0, sizeof(options));
/* 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.proto = SOCK_STREAM;
options.log = 1;
......@@ -638,9 +641,9 @@ int main(int argc, char *argv[]) {
do
{
if (wait_for_clients() == 0)
if (wait_connection() == 0)
{
handle_client();
serve();
}
}
while (options.stay_alive);
......
......@@ -3,6 +3,13 @@
#define NAME "bytevia"
#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 REMOTE_HOST_OPTION 'b'
......@@ -20,12 +27,6 @@
#define SECRET_OPTION 'n'
#define PROTO_OPTION 'o'
#define MODE_CLIENT 0
#define MODE_SERVER 1
#define MINIMUM_BUFFER_SIZE 4096
#define PATH_SEPARATOR '/'
struct struct_options {
unsigned int local_port;
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