From d0301b3252702d841356d1784618fd51f3678dad Mon Sep 17 00:00:00 2001 From: Vysheng Date: Sat, 26 Oct 2013 23:57:22 +0400 Subject: [PATCH] Interface fixes --- loop.c | 4 ++-- main.c | 6 +++--- mtproto-client.c | 12 ++++++------ net.c | 30 +++++++++++++++++++++--------- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/loop.c b/loop.c index 1f3fd0c..0aa9fa1 100644 --- a/loop.c +++ b/loop.c @@ -120,7 +120,7 @@ void write_dc (int auth_file_fd, struct dc *DC) { int our_id; void write_auth_file (void) { - int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, S_IRWXU); + int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, 0600); assert (auth_file_fd >= 0); int x = DC_SERIALIZED_MAGIC; assert (write (auth_file_fd, &x, 4) == 4); @@ -169,7 +169,7 @@ void empty_auth_file (void) { } void read_auth_file (void) { - int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, S_IRWXU); + int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, 0600); if (auth_file_fd < 0) { empty_auth_file (); } diff --git a/main.c b/main.c index d5431cd..e49744f 100644 --- a/main.c +++ b/main.c @@ -158,7 +158,7 @@ void running_for_first_time (void) { // see if config file is there if (stat (config_filename, config_file_stat) != 0) { // config file missing, so touch it - config_file_fd = open (config_filename, O_CREAT | O_RDWR, S_IRWXU); + config_file_fd = open (config_filename, O_CREAT | O_RDWR, 0600); if (config_file_fd == -1) { perror ("open[config_file]"); exit (EXIT_FAILURE); @@ -172,7 +172,7 @@ void running_for_first_time (void) { exit (EXIT_FAILURE); } close (config_file_fd); - int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, S_IRWXU); + int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, 0600); int x = -1; assert (write (auth_file_fd, &x, 4) == 4); close (auth_file_fd); @@ -194,7 +194,7 @@ void inner_main (void) { } void usage (void) { - printf ("%s [-u username]\n", PROGNAME); + printf ("%s [-u username] [-h] [-k public key name]\n", PROGNAME); exit (1); } diff --git a/mtproto-client.c b/mtproto-client.c index fd6bf21..bd46ea2 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -103,7 +103,7 @@ int Response_len; * */ -char *rsa_public_key_name = "id_rsa.pub"; +char *rsa_public_key_name = "tg.pub"; RSA *pubKey; long long pk_fingerprint; @@ -964,9 +964,11 @@ void work_container (struct connection *c, long long msg_id UU) { insert_seqno (c->session, seqno); } int bytes = fetch_int (); - int *t = in_ptr; + int *t = in_end; + in_end = in_ptr + (bytes / 4); rpc_execute_answer (c, id); - assert (in_ptr == t + (bytes / 4)); + assert (in_ptr == in_end); + in_end = t; } } @@ -1122,6 +1124,7 @@ void rpc_execute_answer (struct connection *c, long long msg_id UU) { } logprintf ( "Unknown message: \n"); hexdump_in (); + in_ptr = in_end; // Will not fail due to assertion in_ptr == in_end } int process_rpc_message (struct connection *c UU, struct encrypted_message *enc, int len) { @@ -1166,9 +1169,6 @@ int process_rpc_message (struct connection *c UU, struct encrypted_message *enc, assert (l >= (MINSZ - UNENCSZ) + 8); //assert (enc->message[0] == CODE_rpc_result && *(long long *)(enc->message + 1) == client_last_msg_id); - if (verbosity >= 2) { - logprintf ( "OK, message is good!\n"); - } ++good_messages; in_ptr = enc->message; diff --git a/net.c b/net.c index 7d74d79..b03a5c6 100644 --- a/net.c +++ b/net.c @@ -195,12 +195,11 @@ int max_connection_fd; struct connection *create_connection (const char *host, int port, struct session *session, struct connection_methods *methods) { struct connection *c = malloc (sizeof (*c)); memset (c, 0, sizeof (*c)); - struct hostent *h; - if (!(h = gethostbyname (host)) || h->h_addrtype != AF_INET || h->h_length != 4 || !h->h_addr_list || !h->h_addr) { - assert (0); + int fd = socket (AF_INET, SOCK_STREAM, 0); + if (fd == -1) { + logprintf ("Can not create socket: %m\n"); + exit (1); } - int fd; - assert ((fd = socket (AF_INET, SOCK_STREAM, 0)) != -1); assert (fd >= 0 && fd < MAX_CONNECTIONS); if (fd > max_connection_fd) { max_connection_fd = fd; @@ -231,8 +230,13 @@ struct connection *create_connection (const char *host, int port, struct session s.fd = fd; s.events = POLLOUT | POLLERR | POLLRDHUP | POLLHUP; - if (poll (&s, 1, 10000) <= 0 || !(s.revents & POLLOUT)) { - perror ("poll"); + while (poll (&s, 1, 10000) <= 0 || !(s.revents & POLLOUT)) { + if (errno == EINTR) { continue; } + if (errno) { + logprintf ("Problems in poll: %m\n"); + exit (1); + } + logprintf ("Connect timeout\n"); close (fd); free (c); return 0; @@ -260,12 +264,16 @@ struct connection *create_connection (const char *host, int port, struct session void restart_connection (struct connection *c) { if (c->last_connect_time == time (0)) { + start_fail_timer (c); return; } c->last_connect_time = time (0); - int fd; - assert ((fd = socket (AF_INET, SOCK_STREAM, 0)) != -1); + int fd = socket (AF_INET, SOCK_STREAM, 0); + if (fd == -1) { + logprintf ("Can not create socket: %m\n"); + exit (1); + } assert (fd >= 0 && fd < MAX_CONNECTIONS); if (fd > max_connection_fd) { max_connection_fd = fd; @@ -563,6 +571,10 @@ void dc_create_session (struct dc *DC) { assert (RAND_pseudo_bytes ((unsigned char *) &S->session_id, 8) >= 0); S->dc = DC; S->c = create_connection (DC->ip, DC->port, S, &auth_methods); + if (!S->c) { + logprintf ("Can not create connection to DC. Is network down?\n"); + exit (1); + } assert (!DC->sessions[0]); DC->sessions[0] = S; }