diff options
author | V V <vvaltman@aurum> | 2015-11-09 18:13:30 +0300 |
---|---|---|
committer | V V <vvaltman@aurum> | 2015-11-09 18:13:30 +0300 |
commit | 9b133c477bbc5bca08500be1ac7cb9a60e7f928f (patch) | |
tree | 3de610ae0e03cd9d842d64dac8b5a19648156157 | |
parent | a7c7e38e0c2e6559bca972db05cfa24c3946a331 (diff) | |
parent | 0b3f0536d96eed793513ed181bf2dd0e500cb434 (diff) |
Merge branch 'test' of https://github.com/EionRobb/tgl
-rw-r--r-- | generate.c | 49 | ||||
-rw-r--r-- | mtproto-client.c | 40 | ||||
-rw-r--r-- | mtproto-common.c | 30 | ||||
-rw-r--r-- | mtproto-common.h | 9 | ||||
-rw-r--r-- | mtproto-utils.c | 4 | ||||
-rw-r--r-- | queries-encrypted.c | 12 | ||||
-rw-r--r-- | queries.c | 66 | ||||
-rw-r--r-- | structures.c | 44 | ||||
-rw-r--r-- | tgl-net.c | 10 | ||||
-rw-r--r-- | tgl.h | 2 | ||||
-rw-r--r-- | tools.c | 34 | ||||
-rw-r--r-- | tools.h | 5 | ||||
-rw-r--r-- | updates.c | 2 |
13 files changed, 207 insertions, 100 deletions
@@ -37,6 +37,35 @@ #include <assert.h> #include <string.h> +#if defined(_MSC_VER) || defined(__MINGW32__) + +/* Find the length of STRING, but scan at most MAXLEN characters. + If no '\0' terminator is found in that many characters, return MAXLEN. */ +size_t +strnlen (const char *string, size_t maxlen) +{ + const char *end = memchr (string, '\0', maxlen); + return end ? (size_t)(end - string) : maxlen; +} + +char * +strndup (const char *s, size_t n) +{ + size_t len = strnlen (s, n); + char *new = malloc (len + 1); + + if (new == NULL) + return NULL; + + new[len] = '\0'; + return memcpy (new, s, len); +} + +#define INT64_PRINTF_MODIFIER "I64" +#else +#define INT64_PRINTF_MODIFIER "ll" +#endif + #include "tl-parser/tl-tl.h" #include "generate.h" @@ -53,7 +82,7 @@ struct tree_tl_type *type_tree; struct tree_tl_combinator *function_tree; void tl_function_insert_by_name (struct tl_combinator *c) { - function_tree = tree_insert_tl_combinator (function_tree, c, lrand48 ()); + function_tree = tree_insert_tl_combinator (function_tree, c, rand ()); } struct tl_type *tl_type_get_by_name (int name) { @@ -64,7 +93,7 @@ struct tl_type *tl_type_get_by_name (int name) { } void tl_type_insert_by_name (struct tl_type *t) { - type_tree = tree_insert_tl_type (type_tree, t, lrand48 ()); + type_tree = tree_insert_tl_type (type_tree, t, rand ()); } int is_empty (struct tl_type *t) { @@ -318,7 +347,7 @@ int gen_uni_skip (struct tl_tree *t, char *cur_name, int *vars, int first, int f } return 0; case NODE_TYPE_NAT_CONST: - printf (" if (EVENP(%s) || ((long)%s) != %lld) { %s }\n", cur_name, cur_name, var_nat_const_to_int (t) * 2 + 1, fail); + printf (" if (EVENP(%s) || ((long)%s) != %" INT64_PRINTF_MODIFIER "d) { %s }\n", cur_name, cur_name, var_nat_const_to_int (t) * 2 + 1, fail); return 0; case NODE_TYPE_ARRAY: printf (" if (ODDP(%s) || %s->type->name != TL_TYPE_ARRAY) { %s }\n", cur_name, cur_name, fail); @@ -382,9 +411,9 @@ int gen_create (struct tl_tree *t, int *vars, int offset) { print_offset (offset + 2); t1 = (void *)t; if (t1->self.flags & FLAG_BARE) { - printf (".type = &(struct tl_type_descr) {.name = 0x%08x, .id = \"Bare_%s\", .params_num = %d, .params_types = %lld},\n", ~t1->type->name, t1->type->id, t1->type->arity, t1->type->params_types); + printf (".type = &(struct tl_type_descr) {.name = 0x%08x, .id = \"Bare_%s\", .params_num = %d, .params_types = %" INT64_PRINTF_MODIFIER "d},\n", ~t1->type->name, t1->type->id, t1->type->arity, t1->type->params_types); } else { - printf (".type = &(struct tl_type_descr) {.name = 0x%08x, .id = \"%s\", .params_num = %d, .params_types = %lld},\n", t1->type->name, t1->type->id, t1->type->arity, t1->type->params_types); + printf (".type = &(struct tl_type_descr) {.name = 0x%08x, .id = \"%s\", .params_num = %d, .params_types = %" INT64_PRINTF_MODIFIER "d},\n", t1->type->name, t1->type->id, t1->type->arity, t1->type->params_types); } if (t1->children_num) { print_offset (offset + 2); @@ -1219,7 +1248,7 @@ void gen_constructor_fetch (struct tl_combinator *c) { return; } else if (c->name == NAME_LONG) { printf (" if (in_remaining () < 8) { return -1;}\n"); - printf (" eprintf (\" %%lld\", fetch_long ());\n"); + printf (" eprintf (\" %%" INT64_PRINTF_MODIFIER "d\", fetch_long ());\n"); printf (" return 0;\n"); printf ("}\n"); return; @@ -1579,7 +1608,7 @@ void gen_constructor_print_ds (struct tl_combinator *c) { printf ("}\n"); return; } else if (c->name == NAME_LONG) { - printf (" eprintf (\" %%lld\", *DS);\n"); + printf (" eprintf (\" %%" INT64_PRINTF_MODIFIER "d\", *DS);\n"); printf (" return 0;\n"); printf ("}\n"); return; @@ -2568,13 +2597,13 @@ void gen_types_source (void) { printf (" .name = 0x%08x,\n", tps[i]->name); printf (" .id = \"%s\"\n,", tps[i]->id); printf (" .params_num = %d,\n", tps[i]->arity); - printf (" .params_types = %lld\n", tps[i]->params_types); + printf (" .params_types = %" INT64_PRINTF_MODIFIER "d\n", tps[i]->params_types); printf ("};\n"); printf ("struct tl_type_descr tl_type_bare_%s = {\n", tps[i]->print_id); printf (" .name = 0x%08x,\n", ~tps[i]->name); printf (" .id = \"Bare_%s\",\n", tps[i]->id); printf (" .params_num = %d,\n", tps[i]->arity); - printf (" .params_types = %lld\n", tps[i]->params_types); + printf (" .params_types = %" INT64_PRINTF_MODIFIER "d\n", tps[i]->params_types); printf ("};\n"); } } @@ -2985,7 +3014,7 @@ int main (int argc, char **argv) { int fd = open (argv[optind], O_RDONLY); if (fd < 0) { - fprintf (stderr, "Can not open file '%s'. Error %m\n", argv[optind]); + fprintf (stderr, "Can not open file '%s'. Error %s\n", argv[optind], strerror(errno)); exit (1); } buf_size = read (fd, buf, (1 << 20)); diff --git a/mtproto-client.c b/mtproto-client.c index 6cdea79..f8964a6 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -37,14 +37,18 @@ #include <sys/endian.h> #endif #include <sys/types.h> +#ifdef WIN32 +#include <winsock2.h> +#else #include <netdb.h> -#include "crypto/rand.h" -#include "crypto/rsa_pem.h" -#include "crypto/sha.h" #include <sys/socket.h> #include <netinet/in.h> #include <netinet/tcp.h> #include <poll.h> +#endif +#include "crypto/rand.h" +#include "crypto/rsa_pem.h" +#include "crypto/sha.h" //#include "telegram.h" #include "queries.h" @@ -706,7 +710,7 @@ static void init_enc_msg (struct tgl_state *TLS, struct tgl_session *S, int usef struct tgl_dc *DC = S->dc; assert (DC->state == st_authorized); assert (DC->temp_auth_key_id); - vlogprintf (E_DEBUG, "temp_auth_key_id = 0x%016llx, auth_key_id = 0x%016llx\n", DC->temp_auth_key_id, DC->auth_key_id); + vlogprintf (E_DEBUG, "temp_auth_key_id = 0x%016" INT64_PRINTF_MODIFIER "x, auth_key_id = 0x%016" INT64_PRINTF_MODIFIER "x\n", DC->temp_auth_key_id, DC->auth_key_id); enc_msg.auth_key_id = DC->temp_auth_key_id; enc_msg.server_salt = DC->server_salt; if (!S->session_id) { @@ -798,7 +802,7 @@ int tglmp_encrypt_inner_temp (struct tgl_state *TLS, struct connection *c, int * static int rpc_execute_answer (struct tgl_state *TLS, struct connection *c, long long msg_id); static int work_container (struct tgl_state *TLS, struct connection *c, long long msg_id) { - vlogprintf (E_DEBUG, "work_container: msg_id = %lld\n", msg_id); + vlogprintf (E_DEBUG, "work_container: msg_id = %" INT64_PRINTF_MODIFIER "d\n", msg_id); assert (fetch_int () == CODE_msg_container); int n = fetch_int (); int i; @@ -824,7 +828,7 @@ static int work_new_session_created (struct tgl_state *TLS, struct connection *c struct tgl_session *S = TLS->net_methods->get_session (c); struct tgl_dc *DC = TLS->net_methods->get_dc (c); - vlogprintf (E_NOTICE, "work_new_session_created: msg_id = %lld, dc = %d\n", msg_id, DC->id); + vlogprintf (E_NOTICE, "work_new_session_created: msg_id = %" INT64_PRINTF_MODIFIER "d, dc = %d\n", msg_id, DC->id); assert (fetch_int () == (int)CODE_new_session_created); fetch_long (); // first message id fetch_long (); // unique_id @@ -839,21 +843,21 @@ static int work_new_session_created (struct tgl_state *TLS, struct connection *c } static int work_msgs_ack (struct tgl_state *TLS, struct connection *c, long long msg_id) { - vlogprintf (E_DEBUG, "work_msgs_ack: msg_id = %lld\n", msg_id); + vlogprintf (E_DEBUG, "work_msgs_ack: msg_id = %" INT64_PRINTF_MODIFIER "d\n", msg_id); assert (fetch_int () == CODE_msgs_ack); assert (fetch_int () == CODE_vector); int n = fetch_int (); int i; for (i = 0; i < n; i++) { long long id = fetch_long (); - vlogprintf (E_DEBUG + 1, "ack for %lld\n", id); + vlogprintf (E_DEBUG + 1, "ack for %" INT64_PRINTF_MODIFIER "d\n", id); tglq_query_ack (TLS, id); } return 0; } static int work_rpc_result (struct tgl_state *TLS, struct connection *c, long long msg_id) { - vlogprintf (E_DEBUG, "work_rpc_result: msg_id = %lld\n", msg_id); + vlogprintf (E_DEBUG, "work_rpc_result: msg_id = %" INT64_PRINTF_MODIFIER "d\n", msg_id); assert (fetch_int () == (int)CODE_rpc_result); long long id = fetch_long (); int op = prefetch_int (); @@ -928,7 +932,7 @@ static int work_bad_msg_notification (struct tgl_state *TLS, struct connection * long long m1 = fetch_long (); int s = fetch_int (); int e = fetch_int (); - vlogprintf (E_NOTICE, "bad_msg_notification: msg_id = %lld, seq = %d, error = %d\n", m1, s, e); + vlogprintf (E_NOTICE, "bad_msg_notification: msg_id = %" INT64_PRINTF_MODIFIER "d, seq = %d, error = %d\n", m1, s, e); switch (e) { // Too low msg id case 16: @@ -940,11 +944,11 @@ static int work_bad_msg_notification (struct tgl_state *TLS, struct connection * break; // Bad container case 64: - vlogprintf (E_NOTICE, "bad_msg_notification: msg_id = %lld, seq = %d, error = %d\n", m1, s, e); + vlogprintf (E_NOTICE, "bad_msg_notification: msg_id = %" INT64_PRINTF_MODIFIER "d, seq = %d, error = %d\n", m1, s, e); tglq_regen_query (TLS, m1); break; default: - vlogprintf (E_NOTICE, "bad_msg_notification: msg_id = %lld, seq = %d, error = %d\n", m1, s, e); + vlogprintf (E_NOTICE, "bad_msg_notification: msg_id = %" INT64_PRINTF_MODIFIER "d, seq = %d, error = %d\n", m1, s, e); break; } @@ -1048,7 +1052,7 @@ static void fail_connection (struct tgl_state *TLS, struct connection *c) { } static void fail_session (struct tgl_state *TLS, struct tgl_session *S) { - vlogprintf (E_NOTICE, "failing session %lld\n", S->session_id); + vlogprintf (E_NOTICE, "failing session %" INT64_PRINTF_MODIFIER "d\n", S->session_id); struct tgl_dc *DC = S->dc; tgls_free_session (TLS, S); DC->sessions[0] = NULL; @@ -1067,7 +1071,7 @@ static int process_rpc_message (struct tgl_state *TLS, struct connection *c, str assert (len >= MINSZ && (len & 15) == (UNENCSZ & 15)); struct tgl_dc *DC = TLS->net_methods->get_dc (c); if (enc->auth_key_id != DC->temp_auth_key_id && enc->auth_key_id != DC->auth_key_id) { - vlogprintf (E_WARNING, "received msg from dc %d with auth_key_id %lld (perm_auth_key_id %lld temp_auth_key_id %lld). Dropping\n", + vlogprintf (E_WARNING, "received msg from dc %d with auth_key_id %" INT64_PRINTF_MODIFIER "d (perm_auth_key_id %" INT64_PRINTF_MODIFIER "d temp_auth_key_id %" INT64_PRINTF_MODIFIER "d). Dropping\n", DC->id, enc->auth_key_id, DC->auth_key_id, DC->temp_auth_key_id); return 0; } @@ -1118,7 +1122,7 @@ static int process_rpc_message (struct tgl_state *TLS, struct connection *c, str double st = get_server_time (DC); if (this_server_time < st - 300 || this_server_time > st + 30) { - vlogprintf (E_WARNING, "bad msg time: salt = %lld, session_id = %lld, msg_id = %lld, seq_no = %d, st = %lf, now = %lf\n", enc->server_salt, enc->session_id, enc->msg_id, enc->seq_no, st, get_utime (CLOCK_REALTIME)); + vlogprintf (E_WARNING, "bad msg time: salt = %" INT64_PRINTF_MODIFIER "d, session_id = %" INT64_PRINTF_MODIFIER "d, msg_id = %" INT64_PRINTF_MODIFIER "d, seq_no = %d, st = %lf, now = %lf\n", enc->server_salt, enc->session_id, enc->msg_id, enc->seq_no, st, get_utime (CLOCK_REALTIME)); fail_session (TLS, S); return -1; } @@ -1130,7 +1134,7 @@ static int process_rpc_message (struct tgl_state *TLS, struct connection *c, str assert (this_server_time >= st - 300 && this_server_time <= st + 30); //assert (enc->msg_id > server_last_msg_id && (enc->msg_id & 3) == 1); - vlogprintf (E_DEBUG, "received mesage id %016llx\n", enc->msg_id); + vlogprintf (E_DEBUG, "received mesage id %016" INT64_PRINTF_MODIFIER "x\n", enc->msg_id); //server_last_msg_id = enc->msg_id; //*(long long *)(longpoll_query + 3) = *(long long *)((char *)(&enc->msg_id) + 0x3c); @@ -1348,7 +1352,7 @@ void tgln_insert_msg_id (struct tgl_state *TLS, struct tgl_session *S, long long TLS->timer_methods->insert (S->ev, ACK_TIMEOUT); } if (!tree_lookup_long (S->ack_tree, id)) { - S->ack_tree = tree_insert_long (S->ack_tree, id, lrand48 ()); + S->ack_tree = tree_insert_long (S->ack_tree, id, rand ()); } } @@ -1419,7 +1423,7 @@ void tglmp_dc_create_session (struct tgl_state *TLS, struct tgl_dc *DC) { void tgl_do_send_ping (struct tgl_state *TLS, struct connection *c) { int x[3]; x[0] = CODE_ping; - *(long long *)(x + 1) = lrand48 () * (1ll << 32) + lrand48 (); + *(long long *)(x + 1) = rand () * (1ll << 32) + rand (); tglmp_encrypt_send_message (TLS, c, x, 3, 0); } diff --git a/mtproto-common.c b/mtproto-common.c index eee5d17..c7029ec 100644 --- a/mtproto-common.c +++ b/mtproto-common.c @@ -32,7 +32,12 @@ #include <unistd.h> #include <fcntl.h> #include <sys/types.h> +#ifdef WIN32 +#include <winsock2.h> +#else #include <netdb.h> +#endif + #include "crypto/aes.h" #include "crypto/rand.h" #include "crypto/sha.h" @@ -54,6 +59,7 @@ static long long rsa_encrypted_chunks, rsa_decrypted_chunks; //int verbosity; +#ifndef WIN32 static int get_random_bytes (struct tgl_state *TLS, unsigned char *buf, int n) { int r = 0, h = open ("/dev/random", O_RDONLY | O_NONBLOCK); if (h >= 0) { @@ -85,6 +91,22 @@ static int get_random_bytes (struct tgl_state *TLS, unsigned char *buf, int n) { return r; } +#else +static HCRYPTPROV hCryptoServiceProvider = 0; +static int get_random_bytes (struct tgl_state *TLS, unsigned char *buf, int n) { + if (hCryptoServiceProvider == 0) { + /* Crypto init */ + CryptAcquireContextA(&hCryptoServiceProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); + } + + if (!CryptGenRandom(hCryptoServiceProvider, n, buf)) { + return -1; + } + + + return n; +} +#endif /* RDTSC */ @@ -114,8 +136,10 @@ void tgl_prng_seed (struct tgl_state *TLS, const char *password_filename, int pa #endif unsigned short p = getpid (); TGLC_rand_add (&p, sizeof (p), 0.0); +#ifndef WIN32 p = getppid (); TGLC_rand_add (&p, sizeof (p), 0.0); +#endif unsigned char rb[32]; int s = get_random_bytes (TLS, rb, 32); if (s > 0) { @@ -123,14 +147,14 @@ void tgl_prng_seed (struct tgl_state *TLS, const char *password_filename, int pa } memset (rb, 0, sizeof (rb)); if (password_filename && password_length > 0) { - int fd = open (password_filename, O_RDONLY); + int fd = open (password_filename, O_RDONLY | O_BINARY); if (fd < 0) { - vlogprintf (E_WARNING, "Warning: fail to open password file - \"%s\", %m.\n", password_filename); + vlogprintf (E_WARNING, "Warning: fail to open password file - \"%s\", %s.\n", password_filename, strerror(errno)); } else { unsigned char *a = talloc0 (password_length); int l = read (fd, a, password_length); if (l < 0) { - vlogprintf (E_WARNING, "Warning: fail to read password file - \"%s\", %m.\n", password_filename); + vlogprintf (E_WARNING, "Warning: fail to read password file - \"%s\", %s.\n", password_filename, strerror(errno)); } else { vlogprintf (E_DEBUG, "read %d bytes from password file.\n", l); TGLC_rand_add (a, l, l); diff --git a/mtproto-common.h b/mtproto-common.h index d6783c6..9cecde7 100644 --- a/mtproto-common.h +++ b/mtproto-common.h @@ -27,6 +27,13 @@ #include <stdio.h> #include <assert.h> + +#if defined(_MSC_VER) || defined(__MINGW32__) +#define INT64_PRINTF_MODIFIER "I64" +#else +#define INT64_PRINTF_MODIFIER "ll" +#endif + //#include "interface.h" #include "tools.h" #include "auto/constants.h" @@ -377,7 +384,7 @@ static inline void hexdump_out (void) { hexdump (packet_buffer, packet_ptr); }*/ -#ifdef __MACH__ +#ifndef CLOCK_REALTIME #define CLOCK_REALTIME 0 #define CLOCK_MONOTONIC 1 #endif diff --git a/mtproto-utils.c b/mtproto-utils.c index 8e8a256..1f3a8d4 100644 --- a/mtproto-utils.c +++ b/mtproto-utils.c @@ -125,8 +125,8 @@ int bn_factorize (TGLC_bn *pq, TGLC_bn *p, TGLC_bn *q) { unsigned long long g = 0; int i; for (i = 0; i < 3 || it < 1000; i++) { - int q = ((lrand48() & 15) + 17) % what; - unsigned long long x = (long long)lrand48 () % (what - 1) + 1, y = x; + int q = ((rand() & 15) + 17) % what; + unsigned long long x = (long long)rand () % (what - 1) + 1, y = x; int lim = 1 << (i + 18); int j; for (j = 1; j < lim; j++) { diff --git a/queries-encrypted.c b/queries-encrypted.c index d8c8cc5..f7fd156 100644 --- a/queries-encrypted.c +++ b/queries-encrypted.c @@ -177,7 +177,7 @@ void tgl_do_send_encr_msg_action (struct tgl_state *TLS, struct tgl_message *M, out_long (M->permanent_id.id); encr_start (); out_int (CODE_decrypted_message_layer); - out_random (15 + 4 * (lrand48 () % 3)); + out_random (15 + 4 * (rand () % 3)); out_int (TGL_ENCRYPTED_LAYER); out_int (2 * P->encr_chat.in_seq_no + (P->encr_chat.admin_id != tgl_get_peer_id (TLS->our_id))); out_int (2 * P->encr_chat.out_seq_no + (P->encr_chat.admin_id == tgl_get_peer_id (TLS->our_id)) - 2); @@ -248,7 +248,7 @@ void tgl_do_send_encr_msg (struct tgl_state *TLS, struct tgl_message *M, void (* out_long (M->permanent_id.id); encr_start (); out_int (CODE_decrypted_message_layer); - out_random (15 + 4 * (lrand48 () % 3)); + out_random (15 + 4 * (rand () % 3)); out_int (TGL_ENCRYPTED_LAYER); out_int (2 * P->encr_chat.in_seq_no + (P->encr_chat.admin_id != tgl_get_peer_id (TLS->our_id))); out_int (2 * P->encr_chat.out_seq_no + (P->encr_chat.admin_id == tgl_get_peer_id (TLS->our_id)) - 2); @@ -344,7 +344,7 @@ static void send_file_encrypted_end (struct tgl_state *TLS, struct send_file *f, out_long (r); encr_start (); out_int (CODE_decrypted_message_layer); - out_random (15 + 4 * (lrand48 () % 3)); + out_random (15 + 4 * (rand () % 3)); out_int (TGL_ENCRYPTED_LAYER); out_int (2 * P->encr_chat.in_seq_no + (P->encr_chat.admin_id != tgl_get_peer_id (TLS->our_id))); out_int (2 * P->encr_chat.out_seq_no + (P->encr_chat.admin_id == tgl_get_peer_id (TLS->our_id))); @@ -629,7 +629,7 @@ void tgl_do_create_keys_end (struct tgl_state *TLS, struct tgl_secret_chat *U) { TGLC_sha1 ((void *)U->key, 256, sha_buffer); long long k = *(long long *)(sha_buffer + 12); if (k != U->key_fingerprint) { - vlogprintf (E_WARNING, "Key fingerprint mismatch (my 0x%llx 0x%llx)\n", (unsigned long long)k, (unsigned long long)U->key_fingerprint); + vlogprintf (E_WARNING, "Key fingerprint mismatch (my 0x%" INT64_PRINTF_MODIFIER "x 0x%" INT64_PRINTF_MODIFIER "x)\n", (unsigned long long)k, (unsigned long long)U->key_fingerprint); U->state = sc_deleted; } @@ -671,9 +671,9 @@ void tgl_do_send_create_encr_chat (struct tgl_state *TLS, void *x, unsigned char TGLC_bn_bn2bin (r, (void *)(g_a + (256 - TGLC_bn_num_bytes (r)))); - int t = lrand48 (); + int t = rand (); while (tgl_peer_get (TLS, TGL_MK_ENCR_CHAT (t))) { - t = lrand48 (); + t = rand (); } //bl_do_encr_chat_init (TLS, t, user_id, (void *)random, (void *)g_a); @@ -31,8 +31,9 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> +#ifndef WIN32 #include <sys/utsname.h> - +#endif #include "mtproto-client.h" #include "queries.h" @@ -126,7 +127,7 @@ struct query *tglq_query_get (struct tgl_state *TLS, long long id) { static int alarm_query (struct tgl_state *TLS, struct query *q) { assert (q); - vlogprintf (E_DEBUG - 2, "Alarm query %lld (type '%s')\n", q->msg_id, q->methods->name); + vlogprintf (E_DEBUG - 2, "Alarm query %" INT64_PRINTF_MODIFIER "d (type '%s')\n", q->msg_id, q->methods->name); TLS->timer_methods->insert (q->ev, q->methods->timeout ? q->methods->timeout : QUERY_TIMEOUT); @@ -148,8 +149,8 @@ static int alarm_query (struct tgl_state *TLS, struct query *q) { q->session = q->DC->sessions[0]; long long old_id = q->msg_id; q->msg_id = tglmp_encrypt_send_message (TLS, q->session->c, q->data, q->data_len, (q->flags & QUERY_FORCE_SEND) | 1); - vlogprintf (E_NOTICE, "Resent query #%lld as #%lld of size %d to DC %d\n", old_id, q->msg_id, 4 * q->data_len, q->DC->id); - TLS->queries_tree = tree_insert_query (TLS->queries_tree, q, lrand48 ()); + vlogprintf (E_NOTICE, "Resent query #%" INT64_PRINTF_MODIFIER "d as #%" INT64_PRINTF_MODIFIER "d of size %d to DC %d\n", old_id, q->msg_id, 4 * q->data_len, q->DC->id); + TLS->queries_tree = tree_insert_query (TLS->queries_tree, q, rand ()); q->session_id = q->session->session_id; if (!(q->session->dc->flags & 4) && !(q->flags & QUERY_FORCE_SEND)) { q->session_id = 0; @@ -170,7 +171,7 @@ void tglq_regen_query (struct tgl_state *TLS, long long id) { q->session_id = 0; } } - vlogprintf (E_NOTICE, "regen query %lld\n", id); + vlogprintf (E_NOTICE, "regen query %" INT64_PRINTF_MODIFIER "d\n", id); TLS->timer_methods->insert (q->ev, 0.001); } @@ -186,7 +187,7 @@ void tglq_regen_query_from_old_session (struct query *q, void *ex) { if (q->DC == T->DC) { if (!q->session || q->session_id != T->S->session_id || q->session != T->S) { q->session_id = 0; - vlogprintf (E_NOTICE, "regen query from old session %lld\n", q->msg_id); + vlogprintf (E_NOTICE, "regen query from old session %" INT64_PRINTF_MODIFIER "d\n", q->msg_id); TLS->timer_methods->insert (q->ev, q->methods->timeout ? 0.001 : 0.1); } } @@ -203,7 +204,7 @@ void tglq_regen_queries_from_old_session (struct tgl_state *TLS, struct tgl_dc * void tglq_query_restart (struct tgl_state *TLS, long long id) { struct query *q = tglq_query_get (TLS, id); if (q) { - vlogprintf (E_NOTICE, "restarting query %lld\n", id); + vlogprintf (E_NOTICE, "restarting query %" INT64_PRINTF_MODIFIER "d\n", id); TLS->timer_methods->remove (q->ev); alarm_query (TLS, q); } @@ -232,16 +233,16 @@ struct query *tglq_send_query_ex (struct tgl_state *TLS, struct tgl_dc *DC, int if (!(DC->flags & 4) && !(flags & QUERY_FORCE_SEND)) { q->session_id = 0; } - vlogprintf (E_DEBUG, "Msg_id is %lld %p\n", q->msg_id, q); - vlogprintf (E_NOTICE, "Sent query #%lld of size %d to DC %d\n", q->msg_id, 4 * ints, DC->id); + vlogprintf (E_DEBUG, "Msg_id is %" INT64_PRINTF_MODIFIER "d %p\n", q->msg_id, q); + vlogprintf (E_NOTICE, "Sent query #%" INT64_PRINTF_MODIFIER "d of size %d to DC %d\n", q->msg_id, 4 * ints, DC->id); q->methods = methods; q->type = methods->type; q->DC = DC; q->flags = flags & QUERY_FORCE_SEND; if (TLS->queries_tree) { - vlogprintf (E_DEBUG + 2, "%lld %lld\n", q->msg_id, TLS->queries_tree->x->msg_id); + vlogprintf (E_DEBUG + 2, "%" INT64_PRINTF_MODIFIER "d %" INT64_PRINTF_MODIFIER "d\n", q->msg_id, TLS->queries_tree->x->msg_id); } - TLS->queries_tree = tree_insert_query (TLS->queries_tree, q, lrand48 ()); + TLS->queries_tree = tree_insert_query (TLS->queries_tree, q, rand ()); q->ev = TLS->timer_methods->alloc (TLS, alarm_query_gateway, q); TLS->timer_methods->insert (q->ev, q->methods->timeout ? q->methods->timeout : QUERY_TIMEOUT); @@ -309,7 +310,7 @@ int tglq_query_error (struct tgl_state *TLS, long long id) { char *error = fetch_str (error_len); struct query *q = tglq_query_get (TLS, id); if (!q) { - vlogprintf (E_WARNING, "error for query '%s' #%lld: #%d :%.*s\n", q->methods->name, id, error_code, error_len, error); + vlogprintf (E_WARNING, "error for query '%s' #%" INT64_PRINTF_MODIFIER "d: #%d :%.*s\n", q->methods->name, id, error_code, error_len, error); vlogprintf (E_WARNING, "No such query\n"); } else { if (!(q->flags & QUERY_ACK_RECEIVED)) { @@ -404,9 +405,9 @@ int tglq_query_error (struct tgl_state *TLS, long long id) { } if (error_handled) { - vlogprintf (E_DEBUG - 2, "error for query #%lld: #%d %.*s (HANDLED)\n", id, error_code, error_len, error); + vlogprintf (E_DEBUG - 2, "error for query #%" INT64_PRINTF_MODIFIER "d: #%d %.*s (HANDLED)\n", id, error_code, error_len, error); } else { - vlogprintf (E_WARNING, "error for query '%s' #%lld: #%d %.*s\n", q->methods->name, id, error_code, error_len, error); + vlogprintf (E_WARNING, "error for query '%s' #%" INT64_PRINTF_MODIFIER "d: #%d %.*s\n", q->methods->name, id, error_code, error_len, error); if (q->methods && q->methods->on_error) { res = q->methods->on_error (TLS, q, error_code, error_len, error); } @@ -431,7 +432,7 @@ int tglq_query_error (struct tgl_state *TLS, long long id) { static int packed_buffer[MAX_PACKED_SIZE / 4]; int tglq_query_result (struct tgl_state *TLS, long long id) { - vlogprintf (E_DEBUG, "result for query #%lld. Size %ld bytes\n", id, (long)4 * (in_end - in_ptr)); + vlogprintf (E_DEBUG, "result for query #%" INT64_PRINTF_MODIFIER "d. Size %ld bytes\n", id, (long)4 * (in_end - in_ptr)); int op = prefetch_int (); int *end = 0; int *eend = 0; @@ -502,6 +503,7 @@ void tgl_do_insert_header (struct tgl_state *TLS) { out_int (TGL_SCHEME_LAYER); out_int (CODE_init_connection); out_int (TLS->app_id); +#ifndef WIN32 if (allow_send_linux_version) { struct utsname st; uname (&st); @@ -520,6 +522,14 @@ void tgl_do_insert_header (struct tgl_state *TLS) { out_string (buf); out_string ("en"); } +#else + out_string ("x86"); + out_string ("Windows"); + static char buf[4096]; + tsnprintf (buf, sizeof (buf) - 1, "%s (TGL %s)", TLS->app_version, TGL_VERSION); + out_string (buf); + out_string ("en"); +#endif } void tgl_set_query_error (struct tgl_state *TLS, int error_code, const char *format, ...) __attribute__ ((format (printf, 3, 4))); @@ -1161,9 +1171,9 @@ void tgl_do_reply_message (struct tgl_state *TLS, tgl_message_id_t *_reply_id, c /* {{{ Send text file */ void tgl_do_send_text (struct tgl_state *TLS, tgl_peer_id_t id, const char *file_name, unsigned long long flags, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { - int fd = open (file_name, O_RDONLY); + int fd = open (file_name, O_RDONLY | O_BINARY); if (fd < 0) { - tgl_set_query_error (TLS, EBADF, "Can not open file: %m"); + tgl_set_query_error (TLS, EBADF, "Can not open file: %s", strerror(errno)); if (callback) { callback (TLS, callback_extra, 0, NULL); } @@ -1172,7 +1182,7 @@ void tgl_do_send_text (struct tgl_state *TLS, tgl_peer_id_t id, const char *file static char buf[(1 << 20) + 1]; int x = read (fd, buf, (1 << 20) + 1); if (x < 0) { - tgl_set_query_error (TLS, EBADF, "Can not read from file: %m"); + tgl_set_query_error (TLS, EBADF, "Can not read from file: %s", strerror(errno)); close (fd); if (callback) { callback (TLS, callback_extra, 0, NULL); @@ -1935,7 +1945,7 @@ static void send_part (struct tgl_state *TLS, struct send_file *f, void *callbac memset (&aes_key, 0, sizeof (aes_key)); } out_cstring (buf, x); - vlogprintf (E_DEBUG, "offset=%lld size=%lld\n", f->offset, f->size); + vlogprintf (E_DEBUG, "offset=%" INT64_PRINTF_MODIFIER "d size=%" INT64_PRINTF_MODIFIER "d\n", f->offset, f->size); if (f->offset == f->size) { close (f->fd); f->fd = -1; @@ -1951,7 +1961,7 @@ static void send_part (struct tgl_state *TLS, struct send_file *f, void *callbac static void send_file_thumb (struct tgl_state *TLS, struct send_file *f, const void *thumb_data, int thumb_len, void *callback, void *callback_extra) { clear_packet (); - f->thumb_id = lrand48 () * (1ll << 32) + lrand48 (); + f->thumb_id = rand () * (1ll << 32) + rand (); out_int (CODE_upload_save_file_part); out_long (f->thumb_id); out_int (0); @@ -1961,9 +1971,9 @@ static void send_file_thumb (struct tgl_state *TLS, struct send_file *f, const v static void _tgl_do_send_photo (struct tgl_state *TLS, tgl_peer_id_t to_id, const char *file_name, tgl_peer_id_t avatar, int w, int h, int duration, const void *thumb_data, int thumb_len, const char *caption, int caption_len, unsigned long long flags, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { - int fd = open (file_name, O_RDONLY); + int fd = open (file_name, O_RDONLY | O_BINARY); if (fd < 0) { - tgl_set_query_error (TLS, EBADF, "Can not open file: %m"); + tgl_set_query_error (TLS, EBADF, "Can not open file: %s", strerror(errno)); if (!avatar.peer_id) { if (callback) { callback (TLS, callback_extra, 0, 0); @@ -2996,9 +3006,9 @@ static int download_on_answer (struct tgl_state *TLS, struct query *q, void *DD) struct download *D = q->extra; if (D->fd == -1) { - D->fd = open (D->name, O_CREAT | O_WRONLY, 0640); + D->fd = open (D->name, O_CREAT | O_WRONLY | O_BINARY, 0640); if (D->fd < 0) { - tgl_set_query_error (TLS, EBADF, "Can not open file for writing: %m"); + tgl_set_query_error (TLS, EBADF, "Can not open file for writing: %s", strerror(errno)); if (q->callback) { ((void (*)(struct tgl_state *, void *, int, char *))q->callback) (TLS, q->callback_extra, 0, NULL); } @@ -3083,12 +3093,12 @@ static void load_next_part (struct tgl_state *TLS, struct download *D, void *cal static char buf[PATH_MAX]; int l; if (!D->id) { - l = tsnprintf (buf, sizeof (buf), "%s/download_%lld_%d.jpg", TLS->downloads_directory, D->volume, D->local_id); + l = tsnprintf (buf, sizeof (buf), "%s/download_%" INT64_PRINTF_MODIFIER "d_%d.jpg", TLS->downloads_directory, D->volume, D->local_id); } else { if (D->ext) { - l = tsnprintf (buf, sizeof (buf), "%s/download_%lld.%s", TLS->downloads_directory, D->id, D->ext); + l = tsnprintf (buf, sizeof (buf), "%s/download_%" INT64_PRINTF_MODIFIER "d.%s", TLS->downloads_directory, D->id, D->ext); } else { - l = tsnprintf (buf, sizeof (buf), "%s/download_%lld", TLS->downloads_directory, D->id); + l = tsnprintf (buf, sizeof (buf), "%s/download_%" INT64_PRINTF_MODIFIER "d", TLS->downloads_directory, D->id); } } if (l >= (int) sizeof (buf)) { @@ -4275,7 +4285,7 @@ void tgl_do_get_message (struct tgl_state *TLS, tgl_message_id_t *_msg_id, void clear_packet (); - vlogprintf (E_ERROR, "id=%lld\n", msg_id.id); + vlogprintf (E_ERROR, "id=%" INT64_PRINTF_MODIFIER "d\n", msg_id.id); out_int (CODE_messages_get_messages); out_int (CODE_vector); out_int (1); diff --git a/structures.c b/structures.c index 0e7e2f1..75bc4a8 100644 --- a/structures.c +++ b/structures.c @@ -285,7 +285,7 @@ struct tgl_user *tglf_fetch_alloc_user (struct tgl_state *TLS, struct tl_ds_user TLS->users_allocated ++; U = talloc0 (sizeof (tgl_peer_t)); U->id = user_id; - TLS->peer_tree = tree_insert_peer (TLS->peer_tree, (tgl_peer_t *)U, lrand48 ()); + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, (tgl_peer_t *)U, rand ()); increase_peer_size (TLS); TLS->Peers[TLS->peer_num ++] = (tgl_peer_t *)U; } @@ -382,7 +382,7 @@ void str_to_256 (unsigned char *dst, char *src, int src_len) { if (src_len >= 256) { memcpy (dst, src + src_len - 256, 256); } else { - bzero (dst, 256 - src_len); + memset(dst, 0, 256 - src_len); memcpy (dst + 256 - src_len, src, src_len); } } @@ -391,7 +391,7 @@ void str_to_32 (unsigned char *dst, char *src, int src_len) { if (src_len >= 32) { memcpy (dst, src + src_len - 32, 32); } else { - bzero (dst, 32 - src_len); + memset(dst, 0, 32 - src_len); memcpy (dst + 32 - src_len, src, src_len); } } @@ -410,7 +410,7 @@ struct tgl_secret_chat *tglf_fetch_alloc_encrypted_chat (struct tgl_state *TLS, TLS->encr_chats_allocated ++; U = talloc0 (sizeof (tgl_peer_t)); U->id = chat_id; - TLS->peer_tree = tree_insert_peer (TLS->peer_tree, (tgl_peer_t *)U, lrand48 ()); + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, (tgl_peer_t *)U, rand ()); increase_peer_size (TLS); TLS->Peers[TLS->peer_num ++] = (tgl_peer_t *)U; } @@ -511,7 +511,7 @@ struct tgl_chat *tglf_fetch_alloc_chat (struct tgl_state *TLS, struct tl_ds_chat TLS->chats_allocated ++; C = talloc0 (sizeof (tgl_peer_t)); C->id = chat_id; - TLS->peer_tree = tree_insert_peer (TLS->peer_tree, (tgl_peer_t *)C, lrand48 ()); + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, (tgl_peer_t *)C, rand ()); increase_peer_size (TLS); TLS->Peers[TLS->peer_num ++] = (tgl_peer_t *)C; } @@ -615,7 +615,7 @@ struct tgl_channel *tglf_fetch_alloc_channel (struct tgl_state *TLS, struct tl_d TLS->channels_allocated ++; C = talloc0 (sizeof (tgl_peer_t)); C->id = chat_id; - TLS->peer_tree = tree_insert_peer (TLS->peer_tree, (tgl_peer_t *)C, lrand48 ()); + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, (tgl_peer_t *)C, rand ()); increase_peer_size (TLS); TLS->Peers[TLS->peer_num ++] = (tgl_peer_t *)C; } @@ -1678,7 +1678,7 @@ struct tgl_message *tglf_fetch_encrypted_message (struct tgl_state *TLS, struct struct tl_ds_decrypted_message *DS_DM = DS_DML->message; if (M->permanent_id.id != DS_LVAL (DS_DM->random_id)) { - vlogprintf (E_ERROR, "Incorrect message: id = %lld, new_id = %lld\n", M->permanent_id.id, DS_LVAL (DS_DM->random_id)); + vlogprintf (E_ERROR, "Incorrect message: id = %" INT64_PRINTF_MODIFIER "d, new_id = %" INT64_PRINTF_MODIFIER "d\n", M->permanent_id.id, DS_LVAL (DS_DM->random_id)); free_ds_type_decrypted_message_layer (DS_DML, TYPE_TO_PARAM(decrypted_message_layer)); return M; } @@ -1856,28 +1856,28 @@ struct tgl_message_reply_markup *tglf_fetch_alloc_reply_markup (struct tgl_state void tglp_insert_encrypted_chat (struct tgl_state *TLS, tgl_peer_t *P) { TLS->encr_chats_allocated ++; - TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, lrand48 ()); + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, rand ()); increase_peer_size (TLS); TLS->Peers[TLS->peer_num ++] = P; } void tglp_insert_user (struct tgl_state *TLS, tgl_peer_t *P) { TLS->users_allocated ++; - TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, lrand48 ()); + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, rand ()); increase_peer_size (TLS); TLS->Peers[TLS->peer_num ++] = P; } void tglp_insert_chat (struct tgl_state *TLS, tgl_peer_t *P) { TLS->chats_allocated ++; - TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, lrand48 ()); + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, rand ()); increase_peer_size (TLS); TLS->Peers[TLS->peer_num ++] = P; } void tglp_insert_channel (struct tgl_state *TLS, tgl_peer_t *P) { TLS->channels_allocated ++; - TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, lrand48 ()); + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, rand ()); increase_peer_size (TLS); TLS->Peers[TLS->peer_num ++] = P; } @@ -2197,7 +2197,7 @@ void tglm_message_add_peer (struct tgl_state *TLS, struct tgl_message *M) { TLS->encr_chats_allocated ++; break; } - TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, lrand48 ()); + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, rand ()); increase_peer_size (TLS); TLS->Peers[TLS->peer_num ++] = P; } @@ -2261,7 +2261,7 @@ struct tgl_message *tglm_message_alloc (struct tgl_state *TLS, tgl_message_id_t void tglm_message_insert_tree (struct tgl_state *TLS, struct tgl_message *M) { assert (M->permanent_id.id); - TLS->message_tree = tree_insert_message (TLS->message_tree, M, lrand48 ()); + TLS->message_tree = tree_insert_message (TLS->message_tree, M, rand ()); } void tglm_message_remove_tree (struct tgl_state *TLS, struct tgl_message *M) { @@ -2275,7 +2275,7 @@ void tglm_message_insert (struct tgl_state *TLS, struct tgl_message *M) { } void tglm_message_insert_unsent (struct tgl_state *TLS, struct tgl_message *M) { - TLS->message_unsent_tree = tree_insert_message (TLS->message_unsent_tree, M, lrand48 ()); + TLS->message_unsent_tree = tree_insert_message (TLS->message_unsent_tree, M, rand ()); } void tglm_message_remove_unsent (struct tgl_state *TLS, struct tgl_message *M) { @@ -2307,7 +2307,7 @@ struct tgl_photo *tgl_photo_get (struct tgl_state *TLS, long long id) { } void tgl_photo_insert (struct tgl_state *TLS, struct tgl_photo *P) { - TLS->photo_tree = tree_insert_photo (TLS->photo_tree, P, lrand48 ()); + TLS->photo_tree = tree_insert_photo (TLS->photo_tree, P, rand ()); } struct tgl_document *tgl_document_get (struct tgl_state *TLS, long long id) { @@ -2317,7 +2317,7 @@ struct tgl_document *tgl_document_get (struct tgl_state *TLS, long long id) { } void tgl_document_insert (struct tgl_state *TLS, struct tgl_document *P) { - TLS->document_tree = tree_insert_document (TLS->document_tree, P, lrand48 ()); + TLS->document_tree = tree_insert_document (TLS->document_tree, P, rand ()); } struct tgl_webpage *tgl_webpage_get (struct tgl_state *TLS, long long id) { @@ -2327,11 +2327,11 @@ struct tgl_webpage *tgl_webpage_get (struct tgl_state *TLS, long long id) { } void tgl_webpage_insert (struct tgl_state *TLS, struct tgl_webpage *P) { - TLS->webpage_tree = tree_insert_webpage (TLS->webpage_tree, P, lrand48 ()); + TLS->webpage_tree = tree_insert_webpage (TLS->webpage_tree, P, rand ()); } void tglp_peer_insert_name (struct tgl_state *TLS, tgl_peer_t *P) { - TLS->peer_by_name_tree = tree_insert_peer_by_name (TLS->peer_by_name_tree, P, lrand48 ()); + TLS->peer_by_name_tree = tree_insert_peer_by_name (TLS->peer_by_name_tree, P, rand ()); } void tglp_peer_delete_name (struct tgl_state *TLS, tgl_peer_t *P) { @@ -2565,7 +2565,7 @@ void tgls_insert_random2local (struct tgl_state *TLS, long long random_id, tgl_m struct random2local *R = tree_lookup_random_id (TLS->random_id_tree, X); assert (!R); - TLS->random_id_tree = tree_insert_random_id (TLS->random_id_tree, X, lrand48 ()); + TLS->random_id_tree = tree_insert_random_id (TLS->random_id_tree, X, rand ()); } tgl_message_id_t *tgls_get_local_by_random (struct tgl_state *TLS, long long random_id) { @@ -2588,7 +2588,7 @@ void tgls_insert_temp2local (struct tgl_state *TLS, int temp_id, tgl_message_id_ struct random2local *R = tree_lookup_random_id (TLS->temp_id_tree, X); assert (!R); - TLS->temp_id_tree = tree_insert_random_id (TLS->temp_id_tree, X, lrand48 ()); + TLS->temp_id_tree = tree_insert_random_id (TLS->temp_id_tree, X, rand ()); } }*/ @@ -2629,14 +2629,14 @@ void tgls_message_change_temp_id (struct tgl_state *TLS, struct tgl_message *M, if (M->temp_id == temp_id) { return; } assert (!M->temp_id); M->temp_id = temp_id; - TLS->temp_id_tree = tree_insert_temp_id (TLS->temp_id_tree, M, lrand48 ()); + TLS->temp_id_tree = tree_insert_temp_id (TLS->temp_id_tree, M, rand ()); } void tgls_message_change_random_id (struct tgl_state *TLS, struct tgl_message *M, long long random_id) { if (M->random_id == random_id) { return; } assert (!M->random_id); M->random_id = random_id; - TLS->random_id_tree = tree_insert_random_id (TLS->random_id_tree, M, lrand48 ()); + TLS->random_id_tree = tree_insert_random_id (TLS->random_id_tree, M, rand ()); } void tglm_message_del_temp_id (struct tgl_state *TLS, struct tgl_message *M) { @@ -267,7 +267,7 @@ static int my_connect (struct connection *c, const char *host) { int v6 = TLS->ipv6_enabled; int fd = socket (v6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0); if (fd < 0) { - vlogprintf (E_ERROR, "Can not create socket: %m\n"); + vlogprintf (E_ERROR, "Can not create socket: %s\n", strerror(errno)); start_fail_timer (c); return -1; } @@ -321,7 +321,7 @@ struct connection *tgln_create_connection (struct tgl_state *TLS, const char *ho int fd = my_connect (c, c->ip); if (fd < 0) { - vlogprintf (E_ERROR, "Can not connect to %s:%d %m\n", host, port); + vlogprintf (E_ERROR, "Can not connect to %s:%d %s\n", host, port, strerror(errno)); tfree (c, sizeof (*c)); return 0; } @@ -368,7 +368,7 @@ static void restart_connection (struct connection *c) { c->last_connect_time = time (0); int fd = my_connect (c, c->ip); if (fd < 0) { - vlogprintf (E_WARNING, "Can not connect to %s:%d %m\n", c->ip, c->port); + vlogprintf (E_WARNING, "Can not connect to %s:%d %s\n", c->ip, c->port, strerror(errno)); start_fail_timer (c); return; } @@ -441,7 +441,7 @@ static void try_write (struct connection *c) { delete_connection_buffer (b); } else { if (errno != EAGAIN && errno != EWOULDBLOCK) { - vlogprintf (E_NOTICE, "fail_connection: write_error %m\n"); + vlogprintf (E_NOTICE, "fail_connection: write_error %s\n", strerror(errno)); fail_connection (c); return; } else { @@ -520,7 +520,7 @@ static void try_read (struct connection *c) { c->in_tail = b; } else { if (errno != EAGAIN && errno != EWOULDBLOCK) { - vlogprintf (E_NOTICE, "fail_connection: read_error %m\n"); + vlogprintf (E_NOTICE, "fail_connection: read_error %s\n", strerror(errno)); fail_connection (c); return; } else { @@ -98,7 +98,7 @@ enum tgl_value_type { struct tgl_update_callback { void (*new_msg)(struct tgl_state *TLS, struct tgl_message *M); void (*marked_read)(struct tgl_state *TLS, int num, struct tgl_message *list[]); - void (*logprintf)(const char *format, ...) __attribute__ ((format (printf, 1, 2))); + void (*logprintf)(const char *format, ...) __attribute__ ((format (__printf__, 1, 2))); void (*get_values)(struct tgl_state *TLS, enum tgl_value_type type, const char *prompt, int num_values, void (*callback)(struct tgl_state *TLS, const char *string[], void *arg), void *arg); void (*logged_in)(struct tgl_state *TLS); @@ -41,11 +41,43 @@ #include <mach/mach.h> #endif -#ifdef __MACH__ +#ifndef CLOCK_REALTIME #define CLOCK_REALTIME 0 #define CLOCK_MONOTONIC 1 #endif +#ifdef WIN32 +#include <winsock2.h> +#include <windows.h> +int vasprintf(char ** __restrict__ ret, + const char * __restrict__ format, + va_list ap) { + int len; + /* Get Length */ + len = _vsnprintf(NULL,0,format,ap); + if (len < 0) return -1; + /* +1 for \0 terminator. */ + *ret = malloc(len + 1); + /* Check malloc fail*/ + if (!*ret) return -1; + /* Write String */ + _vsnprintf(*ret,len+1,format,ap); + /* Terminate explicitly */ + (*ret)[len] = '\0'; + return len; +} + +int clock_gettime(int ignored, struct timespec *spec) +{ + __int64 wintime; + GetSystemTimeAsFileTime((FILETIME*)&wintime); + wintime -= 116444736000000000; //1jan1601 to 1jan1970 + spec->tv_sec = wintime / 10000000; //seconds + spec->tv_nsec = wintime % 10000000 *100; //nano-seconds + return 0; +} +#endif + #ifdef VALGRIND_FIXES #include "valgrind/memcheck.h" #endif @@ -27,6 +27,7 @@ #include <string.h> //#include "tgl.h" #include "crypto/err.h" +#include "crypto/rand.h" struct tgl_allocator { void *(*alloc)(size_t size); @@ -99,8 +100,8 @@ void tgl_exists_release (void *ptr, int size); void *tgl_memdup (const void *s, size_t n); -int tgl_snprintf (char *buf, int len, const char *format, ...) __attribute__ ((format (printf, 3, 4))); -int tgl_asprintf (char **res, const char *format, ...) __attribute__ ((format (printf, 2, 3))); +int tgl_snprintf (char *buf, int len, const char *format, ...) __attribute__ ((format (__printf__, 3, 4))); +int tgl_asprintf (char **res, const char *format, ...) __attribute__ ((format (__printf__, 2, 3))); void tglt_secure_random (void *s, int l); void tgl_my_clock_gettime (int clock_id, struct timespec *T); @@ -798,7 +798,7 @@ static void status_notify (struct tgl_state *TLS, void *arg) { void tgl_insert_status_update (struct tgl_state *TLS, struct tgl_user *U) { if (!tree_lookup_user (TLS->online_updates, U)) { - TLS->online_updates = tree_insert_user (TLS->online_updates, U, lrand48 ()); + TLS->online_updates = tree_insert_user (TLS->online_updates, U, rand ()); } if (!TLS->online_updates_timer) { TLS->online_updates_timer = TLS->timer_methods->alloc (TLS, status_notify, 0); |