2013-10-23 10:26:17 -04:00
|
|
|
/*
|
|
|
|
This file is part of telegram-client.
|
|
|
|
|
|
|
|
Telegram-client is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Telegram-client is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this telegram-client. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright Nikolay Durov, Andrey Lopatin 2012-2013
|
|
|
|
Copyright Vitaly Valtman 2013
|
|
|
|
*/
|
2014-01-13 08:05:25 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2013-12-20 08:57:00 -05:00
|
|
|
#ifdef __FreeBSD__
|
2013-12-16 13:30:36 -05:00
|
|
|
#include <sys/endian.h>
|
2013-12-20 08:57:00 -05:00
|
|
|
#endif
|
2013-10-11 16:52:20 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <aio.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <sys/socket.h>
|
2013-12-16 13:30:36 -05:00
|
|
|
#include <netinet/in.h>
|
2013-10-11 16:52:20 -04:00
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <poll.h>
|
|
|
|
|
|
|
|
#include "net.h"
|
|
|
|
#include "include.h"
|
|
|
|
#include "queries.h"
|
|
|
|
#include "loop.h"
|
2013-10-13 06:18:08 -04:00
|
|
|
#include "interface.h"
|
2013-10-16 15:19:39 -04:00
|
|
|
#include "structures.h"
|
2013-11-14 19:08:24 -05:00
|
|
|
#include "binlog.h"
|
2013-10-11 16:52:20 -04:00
|
|
|
|
2013-12-16 13:30:36 -05:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
#define __builtin_bswap32(x) bswap32(x)
|
|
|
|
#endif
|
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
#define sha1 SHA1
|
|
|
|
|
|
|
|
#include "mtproto-common.h"
|
|
|
|
|
|
|
|
#define MAX_NET_RES (1L << 16)
|
2013-11-15 11:14:25 -05:00
|
|
|
extern int log_level;
|
2013-10-11 16:52:20 -04:00
|
|
|
|
|
|
|
int verbosity;
|
|
|
|
int auth_success;
|
|
|
|
enum dc_state c_state;
|
|
|
|
char nonce[256];
|
|
|
|
char new_nonce[256];
|
|
|
|
char server_nonce[256];
|
2013-11-14 19:08:24 -05:00
|
|
|
extern int binlog_enabled;
|
2013-12-18 10:21:49 -05:00
|
|
|
extern int disable_auto_accept;
|
2013-12-24 18:42:05 -05:00
|
|
|
extern int allow_weak_random;
|
2013-11-21 14:35:49 -05:00
|
|
|
|
|
|
|
int total_packets_sent;
|
|
|
|
long long total_data_sent;
|
|
|
|
|
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
int rpc_execute (struct connection *c, int op, int len);
|
|
|
|
int rpc_becomes_ready (struct connection *c);
|
|
|
|
int rpc_close (struct connection *c);
|
|
|
|
|
|
|
|
struct connection_methods auth_methods = {
|
|
|
|
.execute = rpc_execute,
|
|
|
|
.ready = rpc_becomes_ready,
|
|
|
|
.close = rpc_close
|
|
|
|
};
|
|
|
|
|
|
|
|
long long precise_time;
|
|
|
|
long long precise_time_rdtsc;
|
|
|
|
double get_utime (int clock_id) {
|
|
|
|
struct timespec T;
|
2013-11-11 06:34:45 -05:00
|
|
|
my_clock_gettime (clock_id, &T);
|
2013-10-11 16:52:20 -04:00
|
|
|
double res = T.tv_sec + (double) T.tv_nsec * 1e-9;
|
|
|
|
if (clock_id == CLOCK_REALTIME) {
|
|
|
|
precise_time = (long long) (res * (1LL << 32));
|
|
|
|
precise_time_rdtsc = rdtsc ();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-12-24 19:44:36 -05:00
|
|
|
void secure_random (void *s, int l) {
|
2013-12-24 18:42:05 -05:00
|
|
|
if (RAND_bytes (s, l) < 0) {
|
|
|
|
if (allow_weak_random) {
|
|
|
|
RAND_pseudo_bytes (s, l);
|
|
|
|
} else {
|
|
|
|
assert (0 && "End of random. If you want, you can start with -w");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
|
|
|
|
|
|
|
|
#define STATS_BUFF_SIZE (64 << 10)
|
|
|
|
int stats_buff_len;
|
|
|
|
char stats_buff[STATS_BUFF_SIZE];
|
|
|
|
|
|
|
|
#define MAX_RESPONSE_SIZE (1L << 24)
|
|
|
|
|
|
|
|
char Response[MAX_RESPONSE_SIZE];
|
|
|
|
int Response_len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* STATE MACHINE
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-10-26 15:57:22 -04:00
|
|
|
char *rsa_public_key_name = "tg.pub";
|
2013-10-11 16:52:20 -04:00
|
|
|
RSA *pubKey;
|
|
|
|
long long pk_fingerprint;
|
|
|
|
|
|
|
|
static int rsa_load_public_key (const char *public_key_name) {
|
|
|
|
pubKey = NULL;
|
|
|
|
FILE *f = fopen (public_key_name, "r");
|
|
|
|
if (f == NULL) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "Couldn't open public key file: %s\n", public_key_name);
|
2013-10-11 16:52:20 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pubKey = PEM_read_RSAPublicKey (f, NULL, NULL, NULL);
|
|
|
|
fclose (f);
|
|
|
|
if (pubKey == NULL) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "PEM_read_RSAPublicKey returns NULL.\n");
|
2013-10-11 16:52:20 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int auth_work_start (struct connection *c);
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* UNAUTHORIZED (DH KEY EXCHANGE) PROTOCOL PART
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
BIGNUM dh_prime, dh_g, g_a, dh_power, auth_key_num;
|
|
|
|
char s_power [256];
|
|
|
|
|
|
|
|
struct {
|
|
|
|
long long auth_key_id;
|
|
|
|
long long out_msg_id;
|
|
|
|
int msg_len;
|
|
|
|
} unenc_msg_header;
|
|
|
|
|
|
|
|
|
|
|
|
#define ENCRYPT_BUFFER_INTS 16384
|
|
|
|
int encrypt_buffer[ENCRYPT_BUFFER_INTS];
|
|
|
|
|
|
|
|
#define DECRYPT_BUFFER_INTS 16384
|
|
|
|
int decrypt_buffer[ENCRYPT_BUFFER_INTS];
|
|
|
|
|
|
|
|
int encrypt_packet_buffer (void) {
|
|
|
|
return pad_rsa_encrypt ((char *) packet_buffer, (packet_ptr - packet_buffer) * 4, (char *) encrypt_buffer, ENCRYPT_BUFFER_INTS * 4, pubKey->n, pubKey->e);
|
|
|
|
}
|
|
|
|
|
|
|
|
int encrypt_packet_buffer_aes_unauth (const char server_nonce[16], const char hidden_client_nonce[32]) {
|
|
|
|
init_aes_unauth (server_nonce, hidden_client_nonce, AES_ENCRYPT);
|
|
|
|
return pad_aes_encrypt ((char *) packet_buffer, (packet_ptr - packet_buffer) * 4, (char *) encrypt_buffer, ENCRYPT_BUFFER_INTS * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int rpc_send_packet (struct connection *c) {
|
|
|
|
int len = (packet_ptr - packet_buffer) * 4;
|
|
|
|
c->out_packet_num ++;
|
|
|
|
long long next_msg_id = (long long) ((1LL << 32) * get_utime (CLOCK_REALTIME)) & -4;
|
|
|
|
if (next_msg_id <= unenc_msg_header.out_msg_id) {
|
|
|
|
unenc_msg_header.out_msg_id += 4;
|
|
|
|
} else {
|
|
|
|
unenc_msg_header.out_msg_id = next_msg_id;
|
|
|
|
}
|
|
|
|
unenc_msg_header.msg_len = len;
|
|
|
|
|
|
|
|
int total_len = len + 20;
|
|
|
|
assert (total_len > 0 && !(total_len & 0xfc000003));
|
|
|
|
total_len >>= 2;
|
|
|
|
if (total_len < 0x7f) {
|
|
|
|
assert (write_out (c, &total_len, 1) == 1);
|
|
|
|
} else {
|
|
|
|
total_len = (total_len << 8) | 0x7f;
|
|
|
|
assert (write_out (c, &total_len, 4) == 4);
|
|
|
|
}
|
|
|
|
write_out (c, &unenc_msg_header, 20);
|
|
|
|
write_out (c, packet_buffer, len);
|
|
|
|
flush_out (c);
|
2013-11-21 14:35:49 -05:00
|
|
|
|
|
|
|
total_packets_sent ++;
|
|
|
|
total_data_sent += total_len;
|
2013-10-11 16:52:20 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_send_message (struct connection *c, void *data, int len) {
|
|
|
|
assert (len > 0 && !(len & 0xfc000003));
|
|
|
|
int total_len = len >> 2;
|
|
|
|
if (total_len < 0x7f) {
|
|
|
|
assert (write_out (c, &total_len, 1) == 1);
|
|
|
|
} else {
|
|
|
|
total_len = (total_len << 8) | 0x7f;
|
|
|
|
assert (write_out (c, &total_len, 4) == 4);
|
|
|
|
}
|
|
|
|
c->out_packet_num ++;
|
2013-12-19 17:50:31 -05:00
|
|
|
assert (write_out (c, data, len) == len);
|
2013-10-11 16:52:20 -04:00
|
|
|
flush_out (c);
|
2013-11-21 14:35:49 -05:00
|
|
|
|
|
|
|
total_packets_sent ++;
|
|
|
|
total_data_sent += total_len;
|
2013-10-11 16:52:20 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int send_req_pq_packet (struct connection *c) {
|
|
|
|
assert (c_state == st_init);
|
2013-12-24 19:44:36 -05:00
|
|
|
secure_random (nonce, 16);
|
2013-10-11 16:52:20 -04:00
|
|
|
unenc_msg_header.out_msg_id = 0;
|
|
|
|
clear_packet ();
|
|
|
|
out_int (CODE_req_pq);
|
|
|
|
out_ints ((int *)nonce, 4);
|
|
|
|
rpc_send_packet (c);
|
|
|
|
c_state = st_reqpq_sent;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long long gcd (unsigned long long a, unsigned long long b) {
|
|
|
|
return b ? gcd (b, a % b) : a;
|
|
|
|
}
|
|
|
|
|
|
|
|
//typedef unsigned int uint128_t __attribute__ ((mode(TI)));
|
|
|
|
unsigned long long what;
|
|
|
|
unsigned p1, p2;
|
|
|
|
|
|
|
|
int process_respq_answer (struct connection *c, char *packet, int len) {
|
|
|
|
int i;
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "process_respq_answer(), len=%d\n", len);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
assert (len >= 76);
|
|
|
|
assert (!*(long long *) packet);
|
|
|
|
assert (*(int *) (packet + 16) == len - 20);
|
|
|
|
assert (!(len & 3));
|
|
|
|
assert (*(int *) (packet + 20) == CODE_resPQ);
|
|
|
|
assert (!memcmp (packet + 24, nonce, 16));
|
|
|
|
memcpy (server_nonce, packet + 40, 16);
|
|
|
|
char *from = packet + 56;
|
|
|
|
int clen = *from++;
|
|
|
|
assert (clen <= 8);
|
|
|
|
what = 0;
|
|
|
|
for (i = 0; i < clen; i++) {
|
|
|
|
what = (what << 8) + (unsigned char)*from++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (((unsigned long)from) & 3) ++from;
|
|
|
|
|
|
|
|
p1 = 0, p2 = 0;
|
|
|
|
|
|
|
|
if (verbosity >= 2) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "%lld received\n", what);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int it = 0;
|
|
|
|
unsigned long long g = 0;
|
|
|
|
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 lim = 1 << (i + 18);
|
|
|
|
int j;
|
|
|
|
for (j = 1; j < lim; j++) {
|
|
|
|
++it;
|
|
|
|
unsigned long long a = x, b = x, c = q;
|
|
|
|
while (b) {
|
|
|
|
if (b & 1) {
|
|
|
|
c += a;
|
|
|
|
if (c >= what) {
|
|
|
|
c -= what;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a += a;
|
|
|
|
if (a >= what) {
|
|
|
|
a -= what;
|
|
|
|
}
|
|
|
|
b >>= 1;
|
|
|
|
}
|
|
|
|
x = c;
|
|
|
|
unsigned long long z = x < y ? what + x - y : x - y;
|
|
|
|
g = gcd (z, what);
|
|
|
|
if (g != 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!(j & (j - 1))) {
|
|
|
|
y = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (g > 1 && g < what) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (g > 1 && g < what);
|
|
|
|
p1 = g;
|
|
|
|
p2 = what / g;
|
|
|
|
if (p1 > p2) {
|
|
|
|
unsigned t = p1; p1 = p2; p2 = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "p1 = %d, p2 = %d, %d iterations\n", p1, p2, it);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ++p1; ///
|
|
|
|
|
|
|
|
assert (*(int *) (from) == CODE_vector);
|
|
|
|
int fingerprints_num = *(int *)(from + 4);
|
|
|
|
assert (fingerprints_num >= 1 && fingerprints_num <= 64 && len == fingerprints_num * 8 + 8 + (from - packet));
|
|
|
|
long long *fingerprints = (long long *) (from + 8);
|
|
|
|
for (i = 0; i < fingerprints_num; i++) {
|
|
|
|
if (fingerprints[i] == pk_fingerprint) {
|
2013-10-13 06:18:08 -04:00
|
|
|
//logprintf ( "found our public key at position %d\n", i);
|
2013-10-11 16:52:20 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == fingerprints_num) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "fatal: don't have any matching keys (%016llx expected)\n", pk_fingerprint);
|
2013-10-11 16:52:20 -04:00
|
|
|
exit (2);
|
|
|
|
}
|
|
|
|
// create inner part (P_Q_inner_data)
|
|
|
|
clear_packet ();
|
|
|
|
packet_ptr += 5;
|
|
|
|
out_int (CODE_p_q_inner_data);
|
|
|
|
out_cstring (packet + 57, clen);
|
|
|
|
//out_int (0x0f01); // pq=15
|
|
|
|
|
|
|
|
if (p1 < 256) {
|
|
|
|
clen = 1;
|
|
|
|
} else if (p1 < 65536) {
|
|
|
|
clen = 2;
|
|
|
|
} else if (p1 < 16777216) {
|
|
|
|
clen = 3;
|
|
|
|
} else {
|
|
|
|
clen = 4;
|
|
|
|
}
|
|
|
|
p1 = __builtin_bswap32 (p1);
|
|
|
|
out_cstring ((char *)&p1 + 4 - clen, clen);
|
|
|
|
p1 = __builtin_bswap32 (p1);
|
|
|
|
|
|
|
|
if (p2 < 256) {
|
|
|
|
clen = 1;
|
|
|
|
} else if (p2 < 65536) {
|
|
|
|
clen = 2;
|
|
|
|
} else if (p2 < 16777216) {
|
|
|
|
clen = 3;
|
|
|
|
} else {
|
|
|
|
clen = 4;
|
|
|
|
}
|
|
|
|
p2 = __builtin_bswap32 (p2);
|
|
|
|
out_cstring ((char *)&p2 + 4 - clen, clen);
|
|
|
|
p2 = __builtin_bswap32 (p2);
|
|
|
|
|
|
|
|
//out_int (0x0301); // p=3
|
|
|
|
//out_int (0x0501); // q=5
|
|
|
|
out_ints ((int *) nonce, 4);
|
|
|
|
out_ints ((int *) server_nonce, 4);
|
2013-12-24 19:44:36 -05:00
|
|
|
secure_random (new_nonce, 32);
|
2013-10-11 16:52:20 -04:00
|
|
|
out_ints ((int *) new_nonce, 8);
|
|
|
|
sha1 ((unsigned char *) (packet_buffer + 5), (packet_ptr - packet_buffer - 5) * 4, (unsigned char *) packet_buffer);
|
|
|
|
|
|
|
|
int l = encrypt_packet_buffer ();
|
|
|
|
|
|
|
|
clear_packet ();
|
|
|
|
out_int (CODE_req_DH_params);
|
|
|
|
out_ints ((int *) nonce, 4);
|
|
|
|
out_ints ((int *) server_nonce, 4);
|
|
|
|
//out_int (0x0301); // p=3
|
|
|
|
//out_int (0x0501); // q=5
|
|
|
|
if (p1 < 256) {
|
|
|
|
clen = 1;
|
|
|
|
} else if (p1 < 65536) {
|
|
|
|
clen = 2;
|
|
|
|
} else if (p1 < 16777216) {
|
|
|
|
clen = 3;
|
|
|
|
} else {
|
|
|
|
clen = 4;
|
|
|
|
}
|
|
|
|
p1 = __builtin_bswap32 (p1);
|
|
|
|
out_cstring ((char *)&p1 + 4 - clen, clen);
|
|
|
|
p1 = __builtin_bswap32 (p1);
|
|
|
|
if (p2 < 256) {
|
|
|
|
clen = 1;
|
|
|
|
} else if (p2 < 65536) {
|
|
|
|
clen = 2;
|
|
|
|
} else if (p2 < 16777216) {
|
|
|
|
clen = 3;
|
|
|
|
} else {
|
|
|
|
clen = 4;
|
|
|
|
}
|
|
|
|
p2 = __builtin_bswap32 (p2);
|
|
|
|
out_cstring ((char *)&p2 + 4 - clen, clen);
|
|
|
|
p2 = __builtin_bswap32 (p2);
|
|
|
|
|
|
|
|
out_long (pk_fingerprint);
|
|
|
|
out_cstring ((char *) encrypt_buffer, l);
|
|
|
|
|
|
|
|
c_state = st_reqdh_sent;
|
|
|
|
|
|
|
|
return rpc_send_packet (c);
|
|
|
|
}
|
|
|
|
|
2013-12-23 05:45:40 -05:00
|
|
|
int check_DH_params (BIGNUM *p, int g) {
|
|
|
|
if (g < 2 || g > 7) { return -1; }
|
|
|
|
BIGNUM t;
|
|
|
|
BN_init (&t);
|
|
|
|
|
|
|
|
BN_init (&dh_g);
|
2014-01-10 06:32:57 -05:00
|
|
|
ensure (BN_set_word (&dh_g, 4 * g));
|
2013-12-23 05:45:40 -05:00
|
|
|
|
2014-01-10 06:32:57 -05:00
|
|
|
ensure (BN_mod (&t, p, &dh_g, BN_ctx));
|
2013-12-23 05:45:40 -05:00
|
|
|
int x = BN_get_word (&t);
|
|
|
|
assert (x >= 0 && x < 4 * g);
|
|
|
|
|
|
|
|
BN_clear (&dh_g);
|
|
|
|
|
|
|
|
switch (g) {
|
|
|
|
case 2:
|
|
|
|
if (x != 7) { return -1; }
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (x % 3 != 2 ) { return -1; }
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
if (x % 5 != 1 && x % 5 != 4) { return -1; }
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
if (x != 19 && x != 23) { return -1; }
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
if (x % 7 != 3 && x % 7 != 5 && x % 7 != 6) { return -1; }
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BN_is_prime (p, BN_prime_checks, 0, BN_ctx, 0)) { return -1; }
|
|
|
|
|
|
|
|
BIGNUM b;
|
|
|
|
BN_init (&b);
|
2014-01-10 06:32:57 -05:00
|
|
|
ensure (BN_set_word (&b, 2));
|
|
|
|
ensure (BN_div (&t, 0, p, &b, BN_ctx));
|
2013-12-23 05:45:40 -05:00
|
|
|
if (!BN_is_prime (&t, BN_prime_checks, 0, BN_ctx, 0)) { return -1; }
|
|
|
|
BN_clear (&b);
|
|
|
|
BN_clear (&t);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-24 06:29:18 -05:00
|
|
|
int check_g (unsigned char p[256], BIGNUM *g) {
|
2013-12-23 05:45:40 -05:00
|
|
|
static unsigned char s[256];
|
|
|
|
memset (s, 0, 256);
|
|
|
|
assert (BN_num_bytes (g) <= 256);
|
|
|
|
BN_bn2bin (g, s);
|
|
|
|
int ok = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 64; i++) {
|
|
|
|
if (s[i]) {
|
|
|
|
ok = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ok) { return -1; }
|
|
|
|
ok = 0;
|
|
|
|
for (i = 0; i < 64; i++) {
|
|
|
|
if (s[255 - i]) {
|
|
|
|
ok = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ok) { return -1; }
|
2013-12-24 06:29:18 -05:00
|
|
|
ok = 0;
|
|
|
|
for (i = 0; i < 64; i++) {
|
|
|
|
if (s[i] < p[i]) {
|
|
|
|
ok = 1;
|
|
|
|
break;
|
|
|
|
} else if (s[i] > p[i]) {
|
|
|
|
logprintf ("i = %d (%d %d)\n", i, (int)s[i], (int)p[i]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ok) { return -1; }
|
2013-12-23 05:45:40 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-24 06:29:18 -05:00
|
|
|
int check_g_bn (BIGNUM *p, BIGNUM *g) {
|
|
|
|
static unsigned char s[256];
|
|
|
|
memset (s, 0, 256);
|
|
|
|
assert (BN_num_bytes (p) <= 256);
|
|
|
|
BN_bn2bin (p, s);
|
|
|
|
return check_g (s, g);
|
|
|
|
}
|
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
int process_dh_answer (struct connection *c, char *packet, int len) {
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "process_dh_answer(), len=%d\n", len);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
if (len < 116) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "%u * %u = %llu", p1, p2, what);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
assert (len >= 116);
|
|
|
|
assert (!*(long long *) packet);
|
|
|
|
assert (*(int *) (packet + 16) == len - 20);
|
|
|
|
assert (!(len & 3));
|
|
|
|
assert (*(int *) (packet + 20) == (int)CODE_server_DH_params_ok);
|
|
|
|
assert (!memcmp (packet + 24, nonce, 16));
|
|
|
|
assert (!memcmp (packet + 40, server_nonce, 16));
|
|
|
|
init_aes_unauth (server_nonce, new_nonce, AES_DECRYPT);
|
|
|
|
in_ptr = (int *)(packet + 56);
|
|
|
|
in_end = (int *)(packet + len);
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
assert (l > 0);
|
|
|
|
l = pad_aes_decrypt (fetch_str (l), l, (char *) decrypt_buffer, DECRYPT_BUFFER_INTS * 4 - 16);
|
|
|
|
assert (in_ptr == in_end);
|
|
|
|
assert (l >= 60);
|
|
|
|
assert (decrypt_buffer[5] == (int)CODE_server_DH_inner_data);
|
|
|
|
assert (!memcmp (decrypt_buffer + 6, nonce, 16));
|
|
|
|
assert (!memcmp (decrypt_buffer + 10, server_nonce, 16));
|
2013-12-23 05:09:57 -05:00
|
|
|
int g = decrypt_buffer[14];
|
2013-10-11 16:52:20 -04:00
|
|
|
in_ptr = decrypt_buffer + 15;
|
|
|
|
in_end = decrypt_buffer + (l >> 2);
|
|
|
|
BN_init (&dh_prime);
|
|
|
|
BN_init (&g_a);
|
|
|
|
assert (fetch_bignum (&dh_prime) > 0);
|
|
|
|
assert (fetch_bignum (&g_a) > 0);
|
2013-12-24 06:29:18 -05:00
|
|
|
assert (check_g_bn (&dh_prime, &g_a) >= 0);
|
2013-10-11 16:52:20 -04:00
|
|
|
int server_time = *in_ptr++;
|
|
|
|
assert (in_ptr <= in_end);
|
|
|
|
|
2013-12-23 05:45:40 -05:00
|
|
|
assert (check_DH_params (&dh_prime, g) >= 0);
|
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
static char sha1_buffer[20];
|
|
|
|
sha1 ((unsigned char *) decrypt_buffer + 20, (in_ptr - decrypt_buffer - 5) * 4, (unsigned char *) sha1_buffer);
|
|
|
|
assert (!memcmp (decrypt_buffer, sha1_buffer, 20));
|
|
|
|
assert ((char *) in_end - (char *) in_ptr < 16);
|
|
|
|
|
|
|
|
GET_DC(c)->server_time_delta = server_time - time (0);
|
|
|
|
GET_DC(c)->server_time_udelta = server_time - get_utime (CLOCK_MONOTONIC);
|
2013-10-13 06:18:08 -04:00
|
|
|
//logprintf ( "server time is %d, delta = %d\n", server_time, server_time_delta);
|
2013-10-11 16:52:20 -04:00
|
|
|
|
|
|
|
// Build set_client_DH_params answer
|
|
|
|
clear_packet ();
|
|
|
|
packet_ptr += 5;
|
|
|
|
out_int (CODE_client_DH_inner_data);
|
|
|
|
out_ints ((int *) nonce, 4);
|
|
|
|
out_ints ((int *) server_nonce, 4);
|
|
|
|
out_long (0LL);
|
|
|
|
|
|
|
|
BN_init (&dh_g);
|
2014-01-10 06:32:57 -05:00
|
|
|
ensure (BN_set_word (&dh_g, g));
|
2013-10-11 16:52:20 -04:00
|
|
|
|
2013-12-24 19:44:36 -05:00
|
|
|
secure_random (s_power, 256);
|
2014-01-10 06:32:57 -05:00
|
|
|
BIGNUM *dh_power = BN_bin2bn ((unsigned char *)s_power, 256, 0);
|
|
|
|
ensure_ptr (dh_power);
|
2013-10-11 16:52:20 -04:00
|
|
|
|
|
|
|
BIGNUM *y = BN_new ();
|
2014-01-10 06:32:57 -05:00
|
|
|
ensure_ptr (y);
|
|
|
|
ensure (BN_mod_exp (y, &dh_g, dh_power, &dh_prime, BN_ctx));
|
2013-10-11 16:52:20 -04:00
|
|
|
out_bignum (y);
|
|
|
|
BN_free (y);
|
|
|
|
|
|
|
|
BN_init (&auth_key_num);
|
2014-01-10 06:32:57 -05:00
|
|
|
ensure (BN_mod_exp (&auth_key_num, &g_a, dh_power, &dh_prime, BN_ctx));
|
2013-10-11 16:52:20 -04:00
|
|
|
l = BN_num_bytes (&auth_key_num);
|
|
|
|
assert (l >= 250 && l <= 256);
|
|
|
|
assert (BN_bn2bin (&auth_key_num, (unsigned char *)GET_DC(c)->auth_key));
|
|
|
|
memset (GET_DC(c)->auth_key + l, 0, 256 - l);
|
|
|
|
BN_free (dh_power);
|
|
|
|
BN_free (&auth_key_num);
|
|
|
|
BN_free (&dh_g);
|
|
|
|
BN_free (&g_a);
|
|
|
|
BN_free (&dh_prime);
|
|
|
|
|
|
|
|
//hexdump (auth_key, auth_key + 256);
|
|
|
|
|
|
|
|
sha1 ((unsigned char *) (packet_buffer + 5), (packet_ptr - packet_buffer - 5) * 4, (unsigned char *) packet_buffer);
|
|
|
|
|
|
|
|
//hexdump ((char *)packet_buffer, (char *)packet_ptr);
|
|
|
|
|
|
|
|
l = encrypt_packet_buffer_aes_unauth (server_nonce, new_nonce);
|
|
|
|
|
|
|
|
clear_packet ();
|
|
|
|
out_int (CODE_set_client_DH_params);
|
|
|
|
out_ints ((int *) nonce, 4);
|
|
|
|
out_ints ((int *) server_nonce, 4);
|
|
|
|
out_cstring ((char *) encrypt_buffer, l);
|
|
|
|
|
|
|
|
c_state = st_client_dh_sent;
|
|
|
|
|
|
|
|
return rpc_send_packet (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int process_auth_complete (struct connection *c UU, char *packet, int len) {
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "process_dh_answer(), len=%d\n", len);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
assert (len == 72);
|
|
|
|
assert (!*(long long *) packet);
|
|
|
|
assert (*(int *) (packet + 16) == len - 20);
|
|
|
|
assert (!(len & 3));
|
|
|
|
assert (*(int *) (packet + 20) == CODE_dh_gen_ok);
|
|
|
|
assert (!memcmp (packet + 24, nonce, 16));
|
|
|
|
assert (!memcmp (packet + 40, server_nonce, 16));
|
|
|
|
static unsigned char tmp[44], sha1_buffer[20];
|
|
|
|
memcpy (tmp, new_nonce, 32);
|
|
|
|
tmp[32] = 1;
|
2013-11-21 14:35:49 -05:00
|
|
|
//GET_DC(c)->auth_key_id = *(long long *)(sha1_buffer + 12);
|
|
|
|
|
|
|
|
bl_do_set_auth_key_id (GET_DC(c)->id, (unsigned char *)GET_DC(c)->auth_key);
|
2013-10-11 16:52:20 -04:00
|
|
|
sha1 ((unsigned char *)GET_DC(c)->auth_key, 256, sha1_buffer);
|
2013-11-21 14:35:49 -05:00
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
memcpy (tmp + 33, sha1_buffer, 8);
|
|
|
|
sha1 (tmp, 41, sha1_buffer);
|
|
|
|
assert (!memcmp (packet + 56, sha1_buffer + 4, 16));
|
|
|
|
GET_DC(c)->server_salt = *(long long *)server_nonce ^ *(long long *)new_nonce;
|
2013-11-21 14:35:49 -05:00
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
if (verbosity >= 3) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "auth_key_id=%016llx\n", GET_DC(c)->auth_key_id);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
//kprintf ("OK\n");
|
|
|
|
|
|
|
|
//c->status = conn_error;
|
|
|
|
//sleep (1);
|
|
|
|
|
|
|
|
c_state = st_authorized;
|
|
|
|
//return 1;
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "Auth success\n");
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
auth_success ++;
|
|
|
|
GET_DC(c)->flags |= 1;
|
|
|
|
write_auth_file ();
|
2013-11-14 19:08:24 -05:00
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* AUTHORIZED (MAIN) PROTOCOL PART
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct encrypted_message enc_msg;
|
|
|
|
|
|
|
|
long long client_last_msg_id, server_last_msg_id;
|
|
|
|
|
|
|
|
double get_server_time (struct dc *DC) {
|
|
|
|
if (!DC->server_time_udelta) {
|
|
|
|
DC->server_time_udelta = get_utime (CLOCK_REALTIME) - get_utime (CLOCK_MONOTONIC);
|
|
|
|
}
|
|
|
|
return get_utime (CLOCK_MONOTONIC) + DC->server_time_udelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long generate_next_msg_id (struct dc *DC) {
|
|
|
|
long long next_id = (long long) (get_server_time (DC) * (1LL << 32)) & -4;
|
|
|
|
if (next_id <= client_last_msg_id) {
|
|
|
|
next_id = client_last_msg_id += 4;
|
|
|
|
} else {
|
|
|
|
client_last_msg_id = next_id;
|
|
|
|
}
|
|
|
|
return next_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_enc_msg (struct session *S, int useful) {
|
|
|
|
struct dc *DC = S->dc;
|
|
|
|
assert (DC->auth_key_id);
|
|
|
|
enc_msg.auth_key_id = DC->auth_key_id;
|
2013-11-14 19:08:24 -05:00
|
|
|
// assert (DC->server_salt);
|
2013-10-11 16:52:20 -04:00
|
|
|
enc_msg.server_salt = DC->server_salt;
|
|
|
|
if (!S->session_id) {
|
2013-12-24 19:44:36 -05:00
|
|
|
secure_random (&S->session_id, 8);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
enc_msg.session_id = S->session_id;
|
|
|
|
//enc_msg.auth_key_id2 = auth_key_id;
|
|
|
|
enc_msg.msg_id = generate_next_msg_id (DC);
|
|
|
|
//enc_msg.msg_id -= 0x10000000LL * (lrand48 () & 15);
|
|
|
|
//kprintf ("message id %016llx\n", enc_msg.msg_id);
|
|
|
|
enc_msg.seq_no = S->seq_no;
|
|
|
|
if (useful) {
|
|
|
|
enc_msg.seq_no |= 1;
|
|
|
|
}
|
|
|
|
S->seq_no += 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
int aes_encrypt_message (struct dc *DC, struct encrypted_message *enc) {
|
|
|
|
unsigned char sha1_buffer[20];
|
|
|
|
const int MINSZ = offsetof (struct encrypted_message, message);
|
|
|
|
const int UNENCSZ = offsetof (struct encrypted_message, server_salt);
|
|
|
|
int enc_len = (MINSZ - UNENCSZ) + enc->msg_len;
|
|
|
|
assert (enc->msg_len >= 0 && enc->msg_len <= MAX_MESSAGE_INTS * 4 - 16 && !(enc->msg_len & 3));
|
|
|
|
sha1 ((unsigned char *) &enc->server_salt, enc_len, sha1_buffer);
|
|
|
|
//printf ("enc_len is %d\n", enc_len);
|
|
|
|
if (verbosity >= 2) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "sending message with sha1 %08x\n", *(int *)sha1_buffer);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
memcpy (enc->msg_key, sha1_buffer + 4, 16);
|
|
|
|
init_aes_auth (DC->auth_key, enc->msg_key, AES_ENCRYPT);
|
|
|
|
//hexdump ((char *)enc, (char *)enc + enc_len + 24);
|
|
|
|
return pad_aes_encrypt ((char *) &enc->server_salt, enc_len, (char *) &enc->server_salt, MAX_MESSAGE_INTS * 4 + (MINSZ - UNENCSZ));
|
|
|
|
}
|
|
|
|
|
|
|
|
long long encrypt_send_message (struct connection *c, int *msg, int msg_ints, int useful) {
|
|
|
|
struct dc *DC = GET_DC(c);
|
|
|
|
struct session *S = c->session;
|
|
|
|
assert (S);
|
|
|
|
const int UNENCSZ = offsetof (struct encrypted_message, server_salt);
|
|
|
|
if (msg_ints <= 0 || msg_ints > MAX_MESSAGE_INTS - 4) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (msg) {
|
|
|
|
memcpy (enc_msg.message, msg, msg_ints * 4);
|
|
|
|
enc_msg.msg_len = msg_ints * 4;
|
|
|
|
} else {
|
|
|
|
if ((enc_msg.msg_len & 0x80000003) || enc_msg.msg_len > MAX_MESSAGE_INTS * 4 - 16) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
init_enc_msg (S, useful);
|
|
|
|
|
|
|
|
//hexdump ((char *)msg, (char *)msg + (msg_ints * 4));
|
|
|
|
int l = aes_encrypt_message (DC, &enc_msg);
|
|
|
|
//hexdump ((char *)&enc_msg, (char *)&enc_msg + l + 24);
|
|
|
|
assert (l > 0);
|
|
|
|
rpc_send_message (c, &enc_msg, l + UNENCSZ);
|
|
|
|
|
|
|
|
return client_last_msg_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
int longpoll_count, good_messages;
|
|
|
|
|
|
|
|
int auth_work_start (struct connection *c UU) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_execute_answer (struct connection *c, long long msg_id UU);
|
2013-10-16 15:19:39 -04:00
|
|
|
|
2013-10-24 11:44:54 -04:00
|
|
|
int unread_messages;
|
|
|
|
int our_id;
|
2013-10-31 19:18:34 -04:00
|
|
|
int pts;
|
|
|
|
int qts;
|
2013-11-04 12:34:27 -05:00
|
|
|
int last_date;
|
|
|
|
int seq;
|
2013-10-31 19:18:34 -04:00
|
|
|
|
|
|
|
void fetch_pts (void) {
|
|
|
|
int p = fetch_int ();
|
2013-11-04 12:34:27 -05:00
|
|
|
if (p <= pts) { return; }
|
2013-10-31 19:18:34 -04:00
|
|
|
if (p != pts + 1) {
|
|
|
|
if (pts) {
|
2013-11-04 12:34:27 -05:00
|
|
|
//logprintf ("Hole in pts p = %d, pts = %d\n", p, pts);
|
2013-10-31 19:18:34 -04:00
|
|
|
|
|
|
|
// get difference should be here
|
2013-11-04 12:34:27 -05:00
|
|
|
pts = p;
|
2013-10-31 19:18:34 -04:00
|
|
|
} else {
|
|
|
|
pts = p;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pts ++;
|
|
|
|
}
|
2013-11-21 16:40:31 -05:00
|
|
|
bl_do_set_pts (pts);
|
2013-10-31 19:18:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void fetch_qts (void) {
|
|
|
|
int p = fetch_int ();
|
2013-11-04 12:34:27 -05:00
|
|
|
if (p <= qts) { return; }
|
2013-10-31 19:18:34 -04:00
|
|
|
if (p != qts + 1) {
|
|
|
|
if (qts) {
|
2013-11-04 12:34:27 -05:00
|
|
|
//logprintf ("Hole in qts\n");
|
2013-10-31 19:18:34 -04:00
|
|
|
// get difference should be here
|
2013-11-04 12:34:27 -05:00
|
|
|
qts = p;
|
2013-10-31 19:18:34 -04:00
|
|
|
} else {
|
|
|
|
qts = p;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qts ++;
|
|
|
|
}
|
2013-11-21 16:40:31 -05:00
|
|
|
bl_do_set_qts (qts);
|
2013-10-31 19:18:34 -04:00
|
|
|
}
|
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
void fetch_date (void) {
|
|
|
|
int p = fetch_int ();
|
|
|
|
if (p > last_date) {
|
|
|
|
last_date = p;
|
2013-11-21 16:40:31 -05:00
|
|
|
bl_do_set_date (last_date);
|
2013-11-04 12:34:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fetch_seq (void) {
|
|
|
|
int x = fetch_int ();
|
|
|
|
if (x > seq + 1) {
|
|
|
|
logprintf ("Hole in seq: seq = %d, x = %d\n", seq, x);
|
2013-12-06 11:40:52 -05:00
|
|
|
//do_get_difference ();
|
2013-11-21 16:40:31 -05:00
|
|
|
//seq = x;
|
|
|
|
} else if (x == seq + 1) {
|
2013-11-04 12:34:27 -05:00
|
|
|
seq = x;
|
2013-11-21 16:40:31 -05:00
|
|
|
bl_do_set_seq (seq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void work_update_binlog (void) {
|
|
|
|
unsigned op = fetch_int ();
|
|
|
|
switch (op) {
|
|
|
|
case CODE_update_user_name:
|
|
|
|
{
|
|
|
|
peer_id_t user_id = MK_USER (fetch_int ());
|
|
|
|
peer_t *UC = user_chat_get (user_id);
|
|
|
|
if (UC) {
|
|
|
|
struct user *U = &UC->user;
|
2014-01-10 09:37:56 -05:00
|
|
|
if (U->first_name) { tfree_str (U->first_name); }
|
|
|
|
if (U->last_name) { tfree_str (U->last_name); }
|
|
|
|
if (U->print_name) { tfree_str (U->print_name); }
|
2013-11-21 16:40:31 -05:00
|
|
|
U->first_name = fetch_str_dup ();
|
|
|
|
U->last_name = fetch_str_dup ();
|
|
|
|
U->print_name = create_print_name (U->id, U->first_name, U->last_name, 0, 0);
|
|
|
|
} else {
|
2013-12-19 17:50:31 -05:00
|
|
|
fetch_skip_str ();
|
|
|
|
fetch_skip_str ();
|
2013-11-21 16:40:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_user_photo:
|
|
|
|
{
|
|
|
|
peer_id_t user_id = MK_USER (fetch_int ());
|
|
|
|
peer_t *UC = user_chat_get (user_id);
|
|
|
|
fetch_date ();
|
|
|
|
if (UC) {
|
|
|
|
struct user *U = &UC->user;
|
|
|
|
|
|
|
|
unsigned y = fetch_int ();
|
|
|
|
if (y == CODE_user_profile_photo_empty) {
|
|
|
|
U->photo_id = 0;
|
|
|
|
U->photo_big.dc = -2;
|
|
|
|
U->photo_small.dc = -2;
|
|
|
|
} else {
|
|
|
|
assert (y == CODE_user_profile_photo);
|
|
|
|
U->photo_id = fetch_long ();
|
|
|
|
fetch_file_location (&U->photo_small);
|
|
|
|
fetch_file_location (&U->photo_big);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct file_location t;
|
|
|
|
unsigned y = fetch_int ();
|
|
|
|
if (y == CODE_user_profile_photo_empty) {
|
|
|
|
} else {
|
|
|
|
assert (y == CODE_user_profile_photo);
|
|
|
|
fetch_long (); // photo_id
|
|
|
|
fetch_file_location (&t);
|
|
|
|
fetch_file_location (&t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fetch_bool ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert (0);
|
2013-11-04 12:34:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 15:19:39 -04:00
|
|
|
void work_update (struct connection *c UU, long long msg_id UU) {
|
|
|
|
unsigned op = fetch_int ();
|
|
|
|
switch (op) {
|
|
|
|
case CODE_update_new_message:
|
|
|
|
{
|
|
|
|
struct message *M = fetch_alloc_message ();
|
2013-12-19 17:50:31 -05:00
|
|
|
assert (M);
|
2013-10-31 19:18:34 -04:00
|
|
|
fetch_pts ();
|
2013-10-24 11:44:54 -04:00
|
|
|
unread_messages ++;
|
2013-10-16 15:19:39 -04:00
|
|
|
print_message (M);
|
2013-10-28 18:33:13 -04:00
|
|
|
update_prompt ();
|
2013-10-16 15:19:39 -04:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
case CODE_update_message_i_d:
|
|
|
|
{
|
|
|
|
int id = fetch_int (); // id
|
|
|
|
int new = fetch_long (); // random_id
|
|
|
|
struct message *M = message_get (new);
|
2013-11-06 17:16:46 -05:00
|
|
|
if (M) {
|
2013-11-29 16:43:56 -05:00
|
|
|
bl_do_set_msg_id (M, id);
|
2013-11-06 17:16:46 -05:00
|
|
|
}
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_read_messages:
|
|
|
|
{
|
|
|
|
assert (fetch_int () == (int)CODE_vector);
|
|
|
|
int n = fetch_int ();
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
int id = fetch_int ();
|
|
|
|
struct message *M = message_get (id);
|
|
|
|
if (M) {
|
2013-11-29 16:43:56 -05:00
|
|
|
bl_do_set_unread (M, 0);
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
}
|
2013-10-31 19:18:34 -04:00
|
|
|
fetch_pts ();
|
2013-11-15 11:14:25 -05:00
|
|
|
if (log_level >= 1) {
|
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
|
|
|
print_date (time (0));
|
|
|
|
printf (" %d messages marked as read\n", n);
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
|
|
|
}
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_user_typing:
|
|
|
|
{
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_id_t id = MK_USER (fetch_int ());
|
|
|
|
peer_t *U = user_chat_get (id);
|
2013-11-15 11:14:25 -05:00
|
|
|
if (log_level >= 2) {
|
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
|
|
|
print_date (time (0));
|
|
|
|
printf (" User ");
|
|
|
|
print_user_name (id, U);
|
|
|
|
printf (" is typing....\n");
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
|
|
|
}
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_chat_user_typing:
|
|
|
|
{
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_id_t chat_id = MK_CHAT (fetch_int ());
|
|
|
|
peer_id_t id = MK_USER (fetch_int ());
|
|
|
|
peer_t *C = user_chat_get (chat_id);
|
|
|
|
peer_t *U = user_chat_get (id);
|
2013-11-15 11:14:25 -05:00
|
|
|
if (log_level >= 2) {
|
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
|
|
|
print_date (time (0));
|
|
|
|
printf (" User ");
|
|
|
|
print_user_name (id, U);
|
|
|
|
printf (" is typing in chat ");
|
|
|
|
print_chat_name (chat_id, C);
|
|
|
|
printf ("....\n");
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
|
|
|
}
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_user_status:
|
|
|
|
{
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_id_t user_id = MK_USER (fetch_int ());
|
|
|
|
peer_t *U = user_chat_get (user_id);
|
2013-10-16 15:19:39 -04:00
|
|
|
if (U) {
|
|
|
|
fetch_user_status (&U->user.status);
|
2013-11-15 11:14:25 -05:00
|
|
|
if (log_level >= 3) {
|
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
|
|
|
print_date (time (0));
|
|
|
|
printf (" User ");
|
|
|
|
print_user_name (user_id, U);
|
|
|
|
printf (" is now ");
|
|
|
|
printf ("%s\n", (U->user.status.online > 0) ? "online" : "offline");
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
|
|
|
}
|
2013-10-16 15:19:39 -04:00
|
|
|
} else {
|
|
|
|
struct user_status t;
|
|
|
|
fetch_user_status (&t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_user_name:
|
|
|
|
{
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_id_t user_id = MK_USER (fetch_int ());
|
|
|
|
peer_t *UC = user_chat_get (user_id);
|
2013-11-25 11:13:36 -05:00
|
|
|
if (UC && (UC->flags & FLAG_CREATED)) {
|
|
|
|
int l1 = prefetch_strlen ();
|
|
|
|
char *f = fetch_str (l1);
|
|
|
|
int l2 = prefetch_strlen ();
|
|
|
|
char *l = fetch_str (l2);
|
2013-10-16 15:19:39 -04:00
|
|
|
struct user *U = &UC->user;
|
2013-11-29 16:43:56 -05:00
|
|
|
bl_do_set_user_real_name (U, f, l1, l, l2);
|
2013-10-21 14:24:31 -04:00
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
2013-11-06 19:08:16 -05:00
|
|
|
print_date (time (0));
|
|
|
|
printf (" User ");
|
2013-10-21 14:24:31 -04:00
|
|
|
print_user_name (user_id, UC);
|
|
|
|
printf (" changed name to ");
|
|
|
|
print_user_name (user_id, UC);
|
|
|
|
printf ("\n");
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
2013-10-16 15:19:39 -04:00
|
|
|
} else {
|
2013-12-19 17:50:31 -05:00
|
|
|
fetch_skip_str ();
|
|
|
|
fetch_skip_str ();
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_user_photo:
|
|
|
|
{
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_id_t user_id = MK_USER (fetch_int ());
|
|
|
|
peer_t *UC = user_chat_get (user_id);
|
2013-11-11 06:34:45 -05:00
|
|
|
fetch_date ();
|
2013-11-25 11:13:36 -05:00
|
|
|
if (UC && (UC->flags & FLAG_CREATED)) {
|
2013-10-16 15:19:39 -04:00
|
|
|
struct user *U = &UC->user;
|
2013-11-25 11:13:36 -05:00
|
|
|
unsigned y = fetch_int ();
|
|
|
|
long long photo_id;
|
|
|
|
struct file_location big;
|
|
|
|
struct file_location small;
|
|
|
|
memset (&big, 0, sizeof (big));
|
|
|
|
memset (&small, 0, sizeof (small));
|
|
|
|
if (y == CODE_user_profile_photo_empty) {
|
|
|
|
photo_id = 0;
|
|
|
|
big.dc = -2;
|
|
|
|
small.dc = -2;
|
|
|
|
} else {
|
|
|
|
assert (y == CODE_user_profile_photo);
|
|
|
|
photo_id = fetch_long ();
|
|
|
|
fetch_file_location (&small);
|
|
|
|
fetch_file_location (&big);
|
|
|
|
}
|
|
|
|
bl_do_set_user_profile_photo (U, photo_id, &big, &small);
|
2013-10-21 14:24:31 -04:00
|
|
|
|
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
2013-11-06 19:08:16 -05:00
|
|
|
print_date (time (0));
|
|
|
|
printf (" User ");
|
2013-10-21 14:24:31 -04:00
|
|
|
print_user_name (user_id, UC);
|
|
|
|
printf (" updated profile photo\n");
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
2013-10-16 15:19:39 -04:00
|
|
|
} else {
|
|
|
|
struct file_location t;
|
|
|
|
unsigned y = fetch_int ();
|
|
|
|
if (y == CODE_user_profile_photo_empty) {
|
|
|
|
} else {
|
|
|
|
assert (y == CODE_user_profile_photo);
|
2013-11-15 07:59:06 -05:00
|
|
|
fetch_long (); // photo_id
|
2013-10-16 15:19:39 -04:00
|
|
|
fetch_file_location (&t);
|
|
|
|
fetch_file_location (&t);
|
|
|
|
}
|
|
|
|
}
|
2013-11-11 06:34:45 -05:00
|
|
|
fetch_bool ();
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
2013-10-21 15:27:29 -04:00
|
|
|
break;
|
|
|
|
case CODE_update_restore_messages:
|
|
|
|
{
|
|
|
|
assert (fetch_int () == CODE_vector);
|
|
|
|
int n = fetch_int ();
|
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
2013-11-06 19:08:16 -05:00
|
|
|
print_date (time (0));
|
|
|
|
printf (" Restored %d messages\n", n);
|
2013-10-21 15:27:29 -04:00
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
|
|
|
fetch_skip (n);
|
2013-10-31 19:18:34 -04:00
|
|
|
fetch_pts ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_delete_messages:
|
|
|
|
{
|
|
|
|
assert (fetch_int () == CODE_vector);
|
|
|
|
int n = fetch_int ();
|
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
2013-11-06 19:08:16 -05:00
|
|
|
print_date (time (0));
|
|
|
|
printf (" Deleted %d messages\n", n);
|
2013-10-31 19:18:34 -04:00
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
|
|
|
fetch_skip (n);
|
|
|
|
fetch_pts ();
|
2013-10-21 15:27:29 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_chat_participants:
|
|
|
|
{
|
2013-11-22 12:48:23 -05:00
|
|
|
unsigned x = fetch_int ();
|
|
|
|
assert (x == CODE_chat_participants || x == CODE_chat_participants_forbidden);
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_id_t chat_id = MK_CHAT (fetch_int ());
|
2013-11-22 12:48:23 -05:00
|
|
|
int n = 0;
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_t *C = user_chat_get (chat_id);
|
2013-11-25 11:13:36 -05:00
|
|
|
if (C && (C->flags & FLAG_CREATED)) {
|
|
|
|
if (x == CODE_chat_participants) {
|
|
|
|
bl_do_set_chat_admin (&C->chat, fetch_int ());
|
|
|
|
assert (fetch_int () == CODE_vector);
|
|
|
|
n = fetch_int ();
|
2014-01-10 06:32:57 -05:00
|
|
|
struct chat_user *users = talloc (12 * n);
|
2013-11-25 11:13:36 -05:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
assert (fetch_int () == (int)CODE_chat_participant);
|
|
|
|
users[i].user_id = fetch_int ();
|
|
|
|
users[i].inviter_id = fetch_int ();
|
|
|
|
users[i].date = fetch_int ();
|
|
|
|
}
|
|
|
|
int version = fetch_int ();
|
|
|
|
bl_do_set_chat_participants (&C->chat, version, n, users);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (x == CODE_chat_participants) {
|
|
|
|
fetch_int (); // admin_id
|
|
|
|
assert (fetch_int () == CODE_vector);
|
|
|
|
n = fetch_int ();
|
|
|
|
fetch_skip (n * 4);
|
|
|
|
fetch_int (); // version
|
|
|
|
}
|
|
|
|
}
|
2013-10-21 15:27:29 -04:00
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
2013-11-06 19:08:16 -05:00
|
|
|
print_date (time (0));
|
|
|
|
printf (" Chat ");
|
2013-10-31 19:18:34 -04:00
|
|
|
print_chat_name (chat_id, C);
|
2013-11-22 12:48:23 -05:00
|
|
|
if (x == CODE_chat_participants) {
|
|
|
|
printf (" changed list: now %d members\n", n);
|
|
|
|
} else {
|
|
|
|
printf (" changed list, but we are forbidden to know about it (Why this update even was sent to us?\n");
|
|
|
|
}
|
2013-10-21 15:27:29 -04:00
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_contact_registered:
|
|
|
|
{
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_id_t user_id = MK_USER (fetch_int ());
|
|
|
|
peer_t *U = user_chat_get (user_id);
|
2013-10-21 15:27:29 -04:00
|
|
|
fetch_int (); // date
|
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
2013-11-06 19:08:16 -05:00
|
|
|
print_date (time (0));
|
|
|
|
printf (" User ");
|
2013-10-21 15:27:29 -04:00
|
|
|
print_user_name (user_id, U);
|
|
|
|
printf (" registered\n");
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_contact_link:
|
|
|
|
{
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_id_t user_id = MK_USER (fetch_int ());
|
|
|
|
peer_t *U = user_chat_get (user_id);
|
2013-10-21 15:27:29 -04:00
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
2013-11-06 19:08:16 -05:00
|
|
|
print_date (time (0));
|
|
|
|
printf (" Updated link with user ");
|
2013-10-21 15:27:29 -04:00
|
|
|
print_user_name (user_id, U);
|
|
|
|
printf ("\n");
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
|
|
|
unsigned t = fetch_int ();
|
|
|
|
assert (t == CODE_contacts_my_link_empty || t == CODE_contacts_my_link_requested || t == CODE_contacts_my_link_contact);
|
|
|
|
if (t == CODE_contacts_my_link_requested) {
|
|
|
|
fetch_bool (); // has_phone
|
|
|
|
}
|
|
|
|
t = fetch_int ();
|
|
|
|
assert (t == CODE_contacts_foreign_link_unknown || t == CODE_contacts_foreign_link_requested || t == CODE_contacts_foreign_link_mutual);
|
|
|
|
if (t == CODE_contacts_foreign_link_requested) {
|
|
|
|
fetch_bool (); // has_phone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_activation:
|
|
|
|
{
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_id_t user_id = MK_USER (fetch_int ());
|
|
|
|
peer_t *U = user_chat_get (user_id);
|
2013-10-21 15:27:29 -04:00
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
2013-11-06 19:08:16 -05:00
|
|
|
print_date (time (0));
|
|
|
|
printf (" User ");
|
2013-10-21 15:27:29 -04:00
|
|
|
print_user_name (user_id, U);
|
|
|
|
printf (" activated\n");
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_new_authorization:
|
|
|
|
{
|
|
|
|
fetch_long (); // auth_key_id
|
|
|
|
fetch_int (); // date
|
|
|
|
char *s = fetch_str_dup ();
|
|
|
|
char *location = fetch_str_dup ();
|
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
2013-11-06 19:08:16 -05:00
|
|
|
print_date (time (0));
|
|
|
|
printf (" New autorization: device='%s' location='%s'\n",
|
2013-10-21 15:27:29 -04:00
|
|
|
s, location);
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree_str (s);
|
|
|
|
tfree_str (location);
|
2013-10-21 15:27:29 -04:00
|
|
|
}
|
2013-10-16 15:19:39 -04:00
|
|
|
break;
|
2013-11-01 15:57:57 -04:00
|
|
|
case CODE_update_new_geo_chat_message:
|
|
|
|
{
|
|
|
|
struct message *M = fetch_alloc_geo_message ();
|
|
|
|
unread_messages ++;
|
|
|
|
print_message (M);
|
|
|
|
update_prompt ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_new_encrypted_message:
|
|
|
|
{
|
2013-11-02 13:01:22 -04:00
|
|
|
struct message *M = fetch_alloc_encrypted_message ();
|
|
|
|
unread_messages ++;
|
|
|
|
print_message (M);
|
|
|
|
update_prompt ();
|
|
|
|
fetch_qts ();
|
2013-11-01 15:57:57 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_encryption:
|
|
|
|
{
|
2013-11-02 13:01:22 -04:00
|
|
|
struct secret_chat *E = fetch_alloc_encrypted_chat ();
|
2013-11-21 14:35:49 -05:00
|
|
|
if (verbosity >= 2) {
|
|
|
|
logprintf ("Secret chat state = %d\n", E->state);
|
|
|
|
}
|
2013-11-02 13:01:22 -04:00
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
2013-11-06 19:08:16 -05:00
|
|
|
print_date (time (0));
|
2013-11-02 13:01:22 -04:00
|
|
|
switch (E->state) {
|
|
|
|
case sc_none:
|
|
|
|
break;
|
|
|
|
case sc_waiting:
|
2013-11-06 19:08:16 -05:00
|
|
|
printf (" Encrypted chat ");
|
2013-11-02 13:01:22 -04:00
|
|
|
print_encr_chat_name (E->id, (void *)E);
|
|
|
|
printf (" is now in wait state\n");
|
|
|
|
break;
|
|
|
|
case sc_request:
|
2013-11-06 19:08:16 -05:00
|
|
|
printf (" Encrypted chat ");
|
2013-11-02 13:01:22 -04:00
|
|
|
print_encr_chat_name (E->id, (void *)E);
|
|
|
|
printf (" is now in request state. Sending request ok\n");
|
|
|
|
break;
|
|
|
|
case sc_ok:
|
2013-11-06 19:08:16 -05:00
|
|
|
printf (" Encrypted chat ");
|
2013-11-02 13:01:22 -04:00
|
|
|
print_encr_chat_name (E->id, (void *)E);
|
|
|
|
printf (" is now in ok state\n");
|
|
|
|
break;
|
|
|
|
case sc_deleted:
|
2013-11-06 19:08:16 -05:00
|
|
|
printf (" Encrypted chat ");
|
2013-11-02 13:01:22 -04:00
|
|
|
print_encr_chat_name (E->id, (void *)E);
|
|
|
|
printf (" is now in deleted state\n");
|
|
|
|
break;
|
|
|
|
}
|
2013-11-04 12:34:27 -05:00
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
2013-12-18 10:21:49 -05:00
|
|
|
if (E->state == sc_request && !disable_auto_accept) {
|
2013-11-02 13:01:22 -04:00
|
|
|
do_accept_encr_chat_request (E);
|
2013-11-04 12:34:27 -05:00
|
|
|
}
|
2013-11-02 13:01:22 -04:00
|
|
|
fetch_int (); // date
|
2013-11-01 15:57:57 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_encrypted_chat_typing:
|
|
|
|
{
|
2013-11-02 13:01:22 -04:00
|
|
|
peer_id_t id = MK_ENCR_CHAT (fetch_int ());
|
|
|
|
peer_t *P = user_chat_get (id);
|
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
2013-11-06 19:08:16 -05:00
|
|
|
print_date (time (0));
|
2013-11-02 13:01:22 -04:00
|
|
|
if (P) {
|
2013-11-06 19:08:16 -05:00
|
|
|
printf (" User ");
|
2013-11-02 13:01:22 -04:00
|
|
|
peer_id_t user_id = MK_USER (P->encr_chat.user_id);
|
|
|
|
print_user_name (user_id, user_chat_get (user_id));
|
|
|
|
printf (" typing in secret chat ");
|
|
|
|
print_encr_chat_name (id, P);
|
|
|
|
printf ("\n");
|
|
|
|
} else {
|
2013-11-06 19:08:16 -05:00
|
|
|
printf (" Some user is typing in unknown secret chat\n");
|
2013-11-02 13:01:22 -04:00
|
|
|
}
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
2013-11-01 15:57:57 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_encrypted_messages_read:
|
|
|
|
{
|
2013-11-02 13:01:22 -04:00
|
|
|
peer_id_t id = MK_ENCR_CHAT (fetch_int ()); // chat_id
|
2013-11-01 15:57:57 -04:00
|
|
|
fetch_int (); // max_date
|
|
|
|
fetch_int (); // date
|
2013-11-04 18:15:24 -05:00
|
|
|
peer_t *P = user_chat_get (id);
|
|
|
|
int x = -1;
|
|
|
|
if (P && P->last) {
|
|
|
|
x = 0;
|
|
|
|
struct message *M = P->last;
|
|
|
|
while (M && (!M->out || M->unread)) {
|
|
|
|
if (M->out) {
|
|
|
|
M->unread = 0;
|
|
|
|
x ++;
|
|
|
|
}
|
|
|
|
M = M->next;
|
|
|
|
}
|
|
|
|
}
|
2013-11-15 11:14:25 -05:00
|
|
|
if (log_level >= 1) {
|
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
|
|
|
print_date (time (0));
|
|
|
|
printf (" Encrypted chat ");
|
|
|
|
print_encr_chat_name_full (id, user_chat_get (id));
|
|
|
|
printf (": %d messages marked read \n", x);
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
|
|
|
}
|
2013-11-01 15:57:57 -04:00
|
|
|
}
|
|
|
|
break;
|
2013-11-12 10:47:41 -05:00
|
|
|
case CODE_update_chat_participant_add:
|
|
|
|
{
|
|
|
|
peer_id_t chat_id = MK_CHAT (fetch_int ());
|
|
|
|
peer_id_t user_id = MK_USER (fetch_int ());
|
|
|
|
peer_id_t inviter_id = MK_USER (fetch_int ());
|
2013-11-25 11:13:36 -05:00
|
|
|
int version = fetch_int ();
|
|
|
|
|
|
|
|
peer_t *C = user_chat_get (chat_id);
|
|
|
|
if (C && (C->flags & FLAG_CREATED)) {
|
|
|
|
bl_do_chat_add_user (&C->chat, version, get_peer_id (user_id), get_peer_id (inviter_id), time (0));
|
|
|
|
}
|
2013-11-12 10:47:41 -05:00
|
|
|
|
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
|
|
|
print_date (time (0));
|
|
|
|
printf (" Chat ");
|
|
|
|
print_chat_name (chat_id, user_chat_get (chat_id));
|
|
|
|
printf (": user ");
|
|
|
|
print_user_name (user_id, user_chat_get (user_id));
|
|
|
|
printf (" added by user ");
|
|
|
|
print_user_name (inviter_id, user_chat_get (inviter_id));
|
|
|
|
printf ("\n");
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_update_chat_participant_delete:
|
|
|
|
{
|
|
|
|
peer_id_t chat_id = MK_CHAT (fetch_int ());
|
|
|
|
peer_id_t user_id = MK_USER (fetch_int ());
|
2013-11-25 11:13:36 -05:00
|
|
|
int version = fetch_int ();
|
|
|
|
|
|
|
|
peer_t *C = user_chat_get (chat_id);
|
|
|
|
if (C && (C->flags & FLAG_CREATED)) {
|
|
|
|
bl_do_chat_del_user (&C->chat, version, get_peer_id (user_id));
|
|
|
|
}
|
2013-11-12 10:47:41 -05:00
|
|
|
|
|
|
|
print_start ();
|
|
|
|
push_color (COLOR_YELLOW);
|
|
|
|
print_date (time (0));
|
|
|
|
printf (" Chat ");
|
|
|
|
print_chat_name (chat_id, user_chat_get (chat_id));
|
|
|
|
printf (": user ");
|
|
|
|
print_user_name (user_id, user_chat_get (user_id));
|
|
|
|
printf (" deleted\n");
|
|
|
|
pop_color ();
|
|
|
|
print_end ();
|
|
|
|
}
|
|
|
|
break;
|
2013-10-16 15:19:39 -04:00
|
|
|
default:
|
|
|
|
logprintf ("Unknown update type %08x\n", op);
|
2013-11-21 14:35:49 -05:00
|
|
|
;
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void work_update_short (struct connection *c, long long msg_id) {
|
|
|
|
assert (fetch_int () == CODE_update_short);
|
|
|
|
work_update (c, msg_id);
|
2013-11-04 12:34:27 -05:00
|
|
|
fetch_date ();
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void work_updates (struct connection *c, long long msg_id) {
|
|
|
|
assert (fetch_int () == CODE_updates);
|
|
|
|
assert (fetch_int () == CODE_vector);
|
|
|
|
int n = fetch_int ();
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
work_update (c, msg_id);
|
|
|
|
}
|
|
|
|
assert (fetch_int () == CODE_vector);
|
|
|
|
n = fetch_int ();
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
fetch_alloc_user ();
|
|
|
|
}
|
|
|
|
assert (fetch_int () == CODE_vector);
|
|
|
|
n = fetch_int ();
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
fetch_alloc_chat ();
|
|
|
|
}
|
2013-12-02 12:19:08 -05:00
|
|
|
bl_do_set_date (fetch_int ());
|
|
|
|
bl_do_set_seq (fetch_int ());
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void work_update_short_message (struct connection *c UU, long long msg_id UU) {
|
|
|
|
assert (fetch_int () == (int)CODE_update_short_message);
|
|
|
|
struct message *M = fetch_alloc_message_short ();
|
2013-10-24 11:44:54 -04:00
|
|
|
unread_messages ++;
|
2013-10-16 15:19:39 -04:00
|
|
|
print_message (M);
|
2013-10-28 18:33:13 -04:00
|
|
|
update_prompt ();
|
2013-11-04 12:34:27 -05:00
|
|
|
if (M->date > last_date) {
|
|
|
|
last_date = M->date;
|
|
|
|
}
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void work_update_short_chat_message (struct connection *c UU, long long msg_id UU) {
|
|
|
|
assert (fetch_int () == CODE_update_short_chat_message);
|
|
|
|
struct message *M = fetch_alloc_message_short_chat ();
|
2013-10-24 11:44:54 -04:00
|
|
|
unread_messages ++;
|
2013-10-16 15:19:39 -04:00
|
|
|
print_message (M);
|
2013-10-28 18:33:13 -04:00
|
|
|
update_prompt ();
|
2013-11-04 12:34:27 -05:00
|
|
|
if (M->date > last_date) {
|
|
|
|
last_date = M->date;
|
|
|
|
}
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
void work_container (struct connection *c, long long msg_id UU) {
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "work_container: msg_id = %lld\n", msg_id);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
assert (fetch_int () == CODE_msg_container);
|
|
|
|
int n = fetch_int ();
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
long long id = fetch_long ();
|
2014-01-02 13:52:41 -05:00
|
|
|
//int seqno = fetch_int ();
|
|
|
|
fetch_int (); // seq_no
|
|
|
|
if (id & 1) {
|
|
|
|
insert_msg_id (c->session, id);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
int bytes = fetch_int ();
|
2013-10-26 15:57:22 -04:00
|
|
|
int *t = in_end;
|
|
|
|
in_end = in_ptr + (bytes / 4);
|
2013-10-11 16:52:20 -04:00
|
|
|
rpc_execute_answer (c, id);
|
2013-10-26 15:57:22 -04:00
|
|
|
assert (in_ptr == in_end);
|
|
|
|
in_end = t;
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void work_new_session_created (struct connection *c, long long msg_id UU) {
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "work_new_session_created: msg_id = %lld\n", msg_id);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
assert (fetch_int () == (int)CODE_new_session_created);
|
|
|
|
fetch_long (); // first message id
|
|
|
|
//DC->session_id = fetch_long ();
|
|
|
|
fetch_long (); // unique_id
|
|
|
|
GET_DC(c)->server_salt = fetch_long ();
|
2013-11-14 19:08:24 -05:00
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void work_msgs_ack (struct connection *c UU, long long msg_id UU) {
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "work_msgs_ack: msg_id = %lld\n", msg_id);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
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 ();
|
2013-10-24 15:48:51 -04:00
|
|
|
if (verbosity) {
|
|
|
|
logprintf ("ack for %lld\n", id);
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
query_ack (id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void work_rpc_result (struct connection *c UU, long long msg_id UU) {
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "work_rpc_result: msg_id = %lld\n", msg_id);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
assert (fetch_int () == (int)CODE_rpc_result);
|
|
|
|
long long id = fetch_long ();
|
|
|
|
int op = prefetch_int ();
|
|
|
|
if (op == CODE_rpc_error) {
|
|
|
|
query_error (id);
|
|
|
|
} else {
|
|
|
|
query_result (id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-11 13:35:31 -05:00
|
|
|
#define MAX_PACKED_SIZE (1 << 24)
|
2013-10-18 12:00:47 -04:00
|
|
|
void work_packed (struct connection *c, long long msg_id) {
|
|
|
|
assert (fetch_int () == CODE_gzip_packed);
|
|
|
|
static int in_gzip;
|
|
|
|
static int buf[MAX_PACKED_SIZE >> 2];
|
|
|
|
assert (!in_gzip);
|
|
|
|
in_gzip = 1;
|
|
|
|
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
char *s = fetch_str (l);
|
|
|
|
|
2014-01-10 11:54:47 -05:00
|
|
|
int total_out = tinflate (s, l, buf, MAX_PACKED_SIZE);
|
2013-10-18 12:00:47 -04:00
|
|
|
int *end = in_ptr;
|
|
|
|
int *eend = in_end;
|
2014-01-10 11:54:47 -05:00
|
|
|
//assert (total_out % 4 == 0);
|
2013-10-18 12:00:47 -04:00
|
|
|
in_ptr = buf;
|
2014-01-10 11:54:47 -05:00
|
|
|
in_end = in_ptr + total_out / 4;
|
2013-10-18 12:00:47 -04:00
|
|
|
if (verbosity >= 4) {
|
|
|
|
logprintf ( "Unzipped data: ");
|
|
|
|
hexdump_in ();
|
|
|
|
}
|
|
|
|
rpc_execute_answer (c, msg_id);
|
|
|
|
in_ptr = end;
|
|
|
|
in_end = eend;
|
|
|
|
in_gzip = 0;
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:08:52 -04:00
|
|
|
void work_bad_server_salt (struct connection *c UU, long long msg_id UU) {
|
|
|
|
assert (fetch_int () == (int)CODE_bad_server_salt);
|
|
|
|
long long id = fetch_long ();
|
|
|
|
query_restart (id);
|
|
|
|
fetch_int (); // seq_no
|
|
|
|
fetch_int (); // error_code
|
|
|
|
long long new_server_salt = fetch_long ();
|
|
|
|
GET_DC(c)->server_salt = new_server_salt;
|
|
|
|
}
|
|
|
|
|
2013-10-26 11:32:20 -04:00
|
|
|
void work_pong (struct connection *c UU, long long msg_id UU) {
|
|
|
|
assert (fetch_int () == CODE_pong);
|
|
|
|
fetch_long (); // msg_id
|
|
|
|
fetch_long (); // ping_id
|
|
|
|
}
|
|
|
|
|
2013-11-25 12:21:08 -05:00
|
|
|
void work_detailed_info (struct connection *c UU, long long msg_id UU) {
|
|
|
|
assert (fetch_int () == CODE_msg_detailed_info);
|
2013-10-26 11:32:20 -04:00
|
|
|
fetch_long (); // msg_id
|
|
|
|
fetch_long (); // answer_msg_id
|
|
|
|
fetch_int (); // bytes
|
|
|
|
fetch_int (); // status
|
|
|
|
}
|
|
|
|
|
2013-11-25 12:21:08 -05:00
|
|
|
void work_new_detailed_info (struct connection *c UU, long long msg_id UU) {
|
2014-01-02 13:52:41 -05:00
|
|
|
assert (fetch_int () == (int)CODE_msg_new_detailed_info);
|
2013-11-25 12:21:08 -05:00
|
|
|
fetch_long (); // answer_msg_id
|
|
|
|
fetch_int (); // bytes
|
|
|
|
fetch_int (); // status
|
|
|
|
}
|
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
void work_updates_to_long (struct connection *c UU, long long msg_id UU) {
|
|
|
|
assert (fetch_int () == (int)CODE_updates_too_long);
|
2013-11-12 10:47:41 -05:00
|
|
|
logprintf ("updates to long... Getting difference\n");
|
2013-11-04 12:34:27 -05:00
|
|
|
do_get_difference ();
|
|
|
|
}
|
2014-01-02 13:52:41 -05:00
|
|
|
|
|
|
|
void work_bad_msg_notification (struct connection *c UU, long long msg_id UU) {
|
|
|
|
assert (fetch_int () == (int)CODE_bad_msg_notification);
|
|
|
|
long long m1 = fetch_long ();
|
|
|
|
int s = fetch_int ();
|
|
|
|
int e = fetch_int ();
|
|
|
|
logprintf ("bad_msg_notification: msg_id = %lld, seq = %d, error = %d\n", m1, s, e);
|
|
|
|
}
|
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
void rpc_execute_answer (struct connection *c, long long msg_id UU) {
|
2013-10-16 15:19:39 -04:00
|
|
|
if (verbosity >= 5) {
|
2013-10-26 11:32:20 -04:00
|
|
|
logprintf ("rpc_execute_answer: fd=%d\n", c->fd);
|
2013-10-16 15:19:39 -04:00
|
|
|
hexdump_in ();
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
int op = prefetch_int ();
|
|
|
|
switch (op) {
|
|
|
|
case CODE_msg_container:
|
|
|
|
work_container (c, msg_id);
|
|
|
|
return;
|
|
|
|
case CODE_new_session_created:
|
|
|
|
work_new_session_created (c, msg_id);
|
|
|
|
return;
|
|
|
|
case CODE_msgs_ack:
|
|
|
|
work_msgs_ack (c, msg_id);
|
|
|
|
return;
|
|
|
|
case CODE_rpc_result:
|
|
|
|
work_rpc_result (c, msg_id);
|
|
|
|
return;
|
2013-10-16 15:19:39 -04:00
|
|
|
case CODE_update_short:
|
|
|
|
work_update_short (c, msg_id);
|
|
|
|
return;
|
|
|
|
case CODE_updates:
|
|
|
|
work_updates (c, msg_id);
|
|
|
|
return;
|
|
|
|
case CODE_update_short_message:
|
|
|
|
work_update_short_message (c, msg_id);
|
|
|
|
return;
|
|
|
|
case CODE_update_short_chat_message:
|
|
|
|
work_update_short_chat_message (c, msg_id);
|
|
|
|
return;
|
2013-10-18 12:00:47 -04:00
|
|
|
case CODE_gzip_packed:
|
|
|
|
work_packed (c, msg_id);
|
|
|
|
return;
|
2013-10-25 17:08:52 -04:00
|
|
|
case CODE_bad_server_salt:
|
|
|
|
work_bad_server_salt (c, msg_id);
|
|
|
|
return;
|
2013-10-26 11:32:20 -04:00
|
|
|
case CODE_pong:
|
|
|
|
work_pong (c, msg_id);
|
|
|
|
return;
|
2013-11-25 12:21:08 -05:00
|
|
|
case CODE_msg_detailed_info:
|
|
|
|
work_detailed_info (c, msg_id);
|
|
|
|
return;
|
|
|
|
case CODE_msg_new_detailed_info:
|
|
|
|
work_new_detailed_info (c, msg_id);
|
2013-10-26 11:32:20 -04:00
|
|
|
return;
|
2013-11-04 12:34:27 -05:00
|
|
|
case CODE_updates_too_long:
|
|
|
|
work_updates_to_long (c, msg_id);
|
|
|
|
return;
|
2014-01-02 13:52:41 -05:00
|
|
|
case CODE_bad_msg_notification:
|
|
|
|
work_bad_msg_notification (c, msg_id);
|
|
|
|
return;
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "Unknown message: \n");
|
2013-10-11 16:52:20 -04:00
|
|
|
hexdump_in ();
|
2013-10-26 15:57:22 -04:00
|
|
|
in_ptr = in_end; // Will not fail due to assertion in_ptr == in_end
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int process_rpc_message (struct connection *c UU, struct encrypted_message *enc, int len) {
|
|
|
|
const int MINSZ = offsetof (struct encrypted_message, message);
|
|
|
|
const int UNENCSZ = offsetof (struct encrypted_message, server_salt);
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "process_rpc_message(), len=%d\n", len);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
assert (len >= MINSZ && (len & 15) == (UNENCSZ & 15));
|
|
|
|
struct dc *DC = GET_DC(c);
|
|
|
|
assert (enc->auth_key_id == DC->auth_key_id);
|
|
|
|
assert (DC->auth_key_id);
|
|
|
|
init_aes_auth (DC->auth_key + 8, enc->msg_key, AES_DECRYPT);
|
|
|
|
int l = pad_aes_decrypt ((char *)&enc->server_salt, len - UNENCSZ, (char *)&enc->server_salt, len - UNENCSZ);
|
|
|
|
assert (l == len - UNENCSZ);
|
|
|
|
//assert (enc->auth_key_id2 == enc->auth_key_id);
|
|
|
|
assert (!(enc->msg_len & 3) && enc->msg_len > 0 && enc->msg_len <= len - MINSZ && len - MINSZ - enc->msg_len <= 12);
|
|
|
|
static unsigned char sha1_buffer[20];
|
|
|
|
sha1 ((void *)&enc->server_salt, enc->msg_len + (MINSZ - UNENCSZ), sha1_buffer);
|
|
|
|
assert (!memcmp (&enc->msg_key, sha1_buffer + 4, 16));
|
|
|
|
//assert (enc->server_salt == server_salt); //in fact server salt can change
|
|
|
|
if (DC->server_salt != enc->server_salt) {
|
|
|
|
DC->server_salt = enc->server_salt;
|
|
|
|
write_auth_file ();
|
|
|
|
}
|
2013-10-18 14:08:20 -04:00
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
int this_server_time = enc->msg_id >> 32LL;
|
2013-10-18 14:08:20 -04:00
|
|
|
if (!DC->server_time_delta) {
|
2014-01-02 13:52:41 -05:00
|
|
|
DC->server_time_delta = this_server_time - get_utime (CLOCK_REALTIME);
|
2013-10-18 14:08:20 -04:00
|
|
|
DC->server_time_udelta = this_server_time - get_utime (CLOCK_MONOTONIC);
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
double st = get_server_time (DC);
|
2014-01-02 13:52:41 -05:00
|
|
|
if (this_server_time < st - 300 || this_server_time > st + 30) {
|
|
|
|
logprintf ("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));
|
|
|
|
in_ptr = enc->message;
|
|
|
|
in_end = in_ptr + (enc->msg_len / 4);
|
|
|
|
hexdump_in ();
|
|
|
|
}
|
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
assert (this_server_time >= st - 300 && this_server_time <= st + 30);
|
|
|
|
//assert (enc->msg_id > server_last_msg_id && (enc->msg_id & 3) == 1);
|
2013-11-08 13:58:28 -05:00
|
|
|
if (verbosity >= 1) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "received mesage id %016llx\n", enc->msg_id);
|
2013-11-08 13:58:28 -05:00
|
|
|
hexdump_in ();
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
server_last_msg_id = enc->msg_id;
|
|
|
|
|
|
|
|
//*(long long *)(longpoll_query + 3) = *(long long *)((char *)(&enc->msg_id) + 0x3c);
|
|
|
|
//*(long long *)(longpoll_query + 5) = *(long long *)((char *)(&enc->msg_id) + 0x3c);
|
|
|
|
|
|
|
|
assert (l >= (MINSZ - UNENCSZ) + 8);
|
|
|
|
//assert (enc->message[0] == CODE_rpc_result && *(long long *)(enc->message + 1) == client_last_msg_id);
|
|
|
|
++good_messages;
|
|
|
|
|
|
|
|
in_ptr = enc->message;
|
|
|
|
in_end = in_ptr + (enc->msg_len / 4);
|
|
|
|
|
2014-01-02 13:52:41 -05:00
|
|
|
if (enc->msg_id & 1) {
|
|
|
|
insert_msg_id (c->session, enc->msg_id);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
assert (c->session->session_id == enc->session_id);
|
|
|
|
rpc_execute_answer (c, enc->msg_id);
|
2013-10-26 11:32:20 -04:00
|
|
|
assert (in_ptr == in_end);
|
2013-10-11 16:52:20 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int rpc_execute (struct connection *c, int op, int len) {
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "outbound rpc connection #%d : received rpc answer %d with %d content bytes\n", c->fd, op, len);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
2013-11-08 13:58:28 -05:00
|
|
|
/* if (op < 0) {
|
2013-11-07 12:37:12 -05:00
|
|
|
assert (read_in (c, Response, Response_len) == Response_len);
|
|
|
|
return 0;
|
2013-11-08 13:58:28 -05:00
|
|
|
}*/
|
2013-10-11 16:52:20 -04:00
|
|
|
|
|
|
|
if (len >= MAX_RESPONSE_SIZE/* - 12*/ || len < 0/*12*/) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "answer too long (%d bytes), skipping\n", len);
|
2013-10-11 16:52:20 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Response_len = len;
|
|
|
|
|
2013-10-23 13:56:29 -04:00
|
|
|
if (verbosity >= 2) {
|
|
|
|
logprintf ("Response_len = %d\n", Response_len);
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
assert (read_in (c, Response, Response_len) == Response_len);
|
|
|
|
Response[Response_len] = 0;
|
|
|
|
if (verbosity >= 2) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "have %d Response bytes\n", Response_len);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
|
2013-12-16 13:30:36 -05:00
|
|
|
#if !defined(__MACH__) && !defined(__FreeBSD__)
|
2013-10-11 16:52:20 -04:00
|
|
|
setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
|
2013-11-11 06:34:45 -05:00
|
|
|
#endif
|
2013-10-11 16:52:20 -04:00
|
|
|
int o = c_state;
|
|
|
|
if (GET_DC(c)->flags & 1) { o = st_authorized;}
|
|
|
|
switch (o) {
|
|
|
|
case st_reqpq_sent:
|
|
|
|
process_respq_answer (c, Response/* + 8*/, Response_len/* - 12*/);
|
2013-12-16 13:30:36 -05:00
|
|
|
#if !defined(__MACH__) && !defined(__FreeBSD__)
|
2013-10-11 16:52:20 -04:00
|
|
|
setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
|
2013-11-11 06:34:45 -05:00
|
|
|
#endif
|
2013-10-11 16:52:20 -04:00
|
|
|
return 0;
|
|
|
|
case st_reqdh_sent:
|
|
|
|
process_dh_answer (c, Response/* + 8*/, Response_len/* - 12*/);
|
2013-12-16 13:30:36 -05:00
|
|
|
#if !defined(__MACH__) && !defined(__FreeBSD__)
|
2013-10-11 16:52:20 -04:00
|
|
|
setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
|
2013-11-11 06:34:45 -05:00
|
|
|
#endif
|
2013-10-11 16:52:20 -04:00
|
|
|
return 0;
|
|
|
|
case st_client_dh_sent:
|
|
|
|
process_auth_complete (c, Response/* + 8*/, Response_len/* - 12*/);
|
2013-12-16 13:30:36 -05:00
|
|
|
#if !defined(__MACH__) && !defined(__FreeBSD__)
|
2013-10-11 16:52:20 -04:00
|
|
|
setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
|
2013-11-11 06:34:45 -05:00
|
|
|
#endif
|
2013-10-11 16:52:20 -04:00
|
|
|
return 0;
|
|
|
|
case st_authorized:
|
2013-11-14 19:08:24 -05:00
|
|
|
if (op < 0 && op >= -999) {
|
|
|
|
logprintf ("Server error %d\n", op);
|
|
|
|
} else {
|
|
|
|
process_rpc_message (c, (void *)(Response/* + 8*/), Response_len/* - 12*/);
|
|
|
|
}
|
2013-12-16 13:30:36 -05:00
|
|
|
#if !defined(__MACH__) && !defined(__FreeBSD__)
|
2013-10-11 16:52:20 -04:00
|
|
|
setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
|
2013-11-11 06:34:45 -05:00
|
|
|
#endif
|
2013-10-11 16:52:20 -04:00
|
|
|
return 0;
|
|
|
|
default:
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "fatal: cannot receive answer in state %d\n", c_state);
|
2013-10-11 16:52:20 -04:00
|
|
|
exit (2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int tc_close (struct connection *c, int who) {
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "outbound http connection #%d : closing by %d\n", c->fd, who);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tc_becomes_ready (struct connection *c) {
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "outbound connection #%d becomes ready\n", c->fd);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
char byte = 0xef;
|
|
|
|
assert (write_out (c, &byte, 1) == 1);
|
|
|
|
flush_out (c);
|
|
|
|
|
2013-12-16 13:30:36 -05:00
|
|
|
#if !defined(__MACH__) && !defined(__FreeBSD__)
|
2013-10-11 16:52:20 -04:00
|
|
|
setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
|
2013-11-11 06:34:45 -05:00
|
|
|
#endif
|
2013-10-11 16:52:20 -04:00
|
|
|
int o = c_state;
|
|
|
|
if (GET_DC(c)->flags & 1) { o = st_authorized; }
|
|
|
|
switch (o) {
|
|
|
|
case st_init:
|
|
|
|
send_req_pq_packet (c);
|
|
|
|
break;
|
|
|
|
case st_authorized:
|
|
|
|
auth_work_start (c);
|
|
|
|
break;
|
|
|
|
default:
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "c_state = %d\n", c_state);
|
2013-10-11 16:52:20 -04:00
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_becomes_ready (struct connection *c) {
|
|
|
|
return tc_becomes_ready (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_close (struct connection *c) {
|
|
|
|
return tc_close (c, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int auth_is_success (void) {
|
|
|
|
return auth_success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_start (void) {
|
|
|
|
prng_seed (0, 0);
|
|
|
|
|
|
|
|
if (rsa_load_public_key (rsa_public_key_name) < 0) {
|
|
|
|
perror ("rsa_load_public_key");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "public key '%s' loaded successfully\n", rsa_public_key_name);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
pk_fingerprint = compute_rsa_key_fingerprint (pubKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
int auth_ok (void) {
|
|
|
|
return auth_success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dc_authorize (struct dc *DC) {
|
|
|
|
c_state = 0;
|
|
|
|
auth_success = 0;
|
|
|
|
if (!DC->sessions[0]) {
|
|
|
|
dc_create_session (DC);
|
|
|
|
}
|
|
|
|
if (verbosity) {
|
2013-10-13 06:18:08 -04:00
|
|
|
logprintf ( "Starting authorization for DC #%d: %s:%d\n", DC->id, DC->ip, DC->port);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
net_loop (0, auth_ok);
|
|
|
|
}
|