This commit is contained in:
vvaltman 2014-08-21 07:24:52 +04:00
parent 9efad0f349
commit d33d24d1f0
17 changed files with 308 additions and 111 deletions

View File

@ -1212,6 +1212,7 @@ static void create_new_binlog (void) {
void tgl_replay_log (void) { void tgl_replay_log (void) {
if (!tgl_state.binlog_enabled) { return; }
if (access (get_binlog_file_name (), F_OK) < 0) { if (access (get_binlog_file_name (), F_OK) < 0) {
printf ("No binlog found. Creating new one\n"); printf ("No binlog found. Creating new one\n");
create_new_binlog (); create_new_binlog ();
@ -1413,7 +1414,7 @@ void bl_do_user_set_friend (struct tgl_user *U, int friend) {
} }
void bl_do_dc_option (int id, int l1, const char *name, int l2, const char *ip, int port) { void bl_do_dc_option (int id, int l1, const char *name, int l2, const char *ip, int port) {
struct dc *DC = tgl_state.DC_list[id]; struct tgl_dc *DC = tgl_state.DC_list[id];
if (DC) { return; } if (DC) { return; }
clear_packet (); clear_packet ();
@ -1921,3 +1922,14 @@ void bl_do_msg_update (long long id) {
out_long (id); out_long (id);
add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer));
} }
/*void bl_do_add_dc (int id, const char *ip, int l, int port, long long auth_key_id, const char *auth_key) {
clear_packet ();
out_int (CODE_binlog_add_dc);
out_long (id);
out_cstring (ip, l);
out_int (port);
out_long (auth_key_id);
out_ints ((void *)auth_key, 64);
add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer));
}*/

View File

@ -86,4 +86,6 @@ void bl_do_delete_msg (struct tgl_message *M);
void bl_do_msg_seq_update (long long id); void bl_do_msg_seq_update (long long id);
void bl_do_msg_update (long long id); void bl_do_msg_update (long long id);
//void bl_do_add_dc (int id, const char *ip, int l, int port, long long auth_key_id, const char *auth_key);
#endif #endif

View File

@ -68,3 +68,5 @@ binlog.deleteMsg id:long = binlog.Update;
binlog.msgSeqUpdate id:long = binlog.Update; binlog.msgSeqUpdate id:long = binlog.Update;
binlog.msgUpdate id:long = binlog.Update; binlog.msgUpdate id:long = binlog.Update;
//binlog.addDc id:int ip:string port:int auth_key_id:long auth_key:64*[int] = binlog.Update;

View File

@ -21,6 +21,10 @@
/* Define to 1 if you have the <lauxlib.h> header file. */ /* Define to 1 if you have the <lauxlib.h> header file. */
#undef HAVE_LAUXLIB_H #undef HAVE_LAUXLIB_H
/* Define to 1 if you have the `AES_set_encrypt_key' library
(-lAES_set_encrypt_key). */
#undef HAVE_LIBAES_SET_ENCRYPT_KEY
/* Define to 1 if you have the `config' library (-lconfig). */ /* Define to 1 if you have the `config' library (-lconfig). */
#undef HAVE_LIBCONFIG #undef HAVE_LIBCONFIG

45
configure vendored
View File

@ -3469,6 +3469,51 @@ _ACEOF
fi fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for ssl in -lAES_set_encrypt_key" >&5
$as_echo_n "checking for ssl in -lAES_set_encrypt_key... " >&6; }
if ${ac_cv_lib_AES_set_encrypt_key_ssl+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lAES_set_encrypt_key $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
/* Override any GCC internal prototype to avoid an error.
Use char because int might match the return type of a GCC
builtin and then its argument prototype would still apply. */
#ifdef __cplusplus
extern "C"
#endif
char ssl ();
int
main ()
{
return ssl ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
ac_cv_lib_AES_set_encrypt_key_ssl=yes
else
ac_cv_lib_AES_set_encrypt_key_ssl=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_AES_set_encrypt_key_ssl" >&5
$as_echo "$ac_cv_lib_AES_set_encrypt_key_ssl" >&6; }
if test "x$ac_cv_lib_AES_set_encrypt_key_ssl" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_LIBAES_SET_ENCRYPT_KEY 1
_ACEOF
LIBS="-lAES_set_encrypt_key $LIBS"
fi
EXTRA_LIBS="" EXTRA_LIBS=""
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for rl_save_prompt in -lreadline" >&5 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for rl_save_prompt in -lreadline" >&5

View File

@ -78,7 +78,6 @@ int readline_active;
int log_level; int log_level;
char *line_ptr; char *line_ptr;
int in_chat_mode; int in_chat_mode;

131
loop.c
View File

@ -50,6 +50,7 @@
#include "loop.h" #include "loop.h"
#include "lua-tg.h" #include "lua-tg.h"
#include "tgl.h" #include "tgl.h"
#include "binlog.h"
int verbosity; int verbosity;
@ -149,7 +150,7 @@ int main_loop (void) {
return 0; return 0;
} }
struct dc *cur_a_dc; struct tgl_dc *cur_a_dc;
int is_authorized (void) { int is_authorized (void) {
return tgl_authorized_dc (cur_a_dc); return tgl_authorized_dc (cur_a_dc);
} }
@ -229,16 +230,127 @@ int wait_dialog_list;
extern struct tgl_update_callback upd_cb; extern struct tgl_update_callback upd_cb;
#define DC_SERIALIZED_MAGIC 0x868aa81d
void write_dc (struct tgl_dc *DC, void *extra) {
int auth_file_fd = *(int *)extra;
if (!DC) {
int x = 0;
assert (write (auth_file_fd, &x, 4) == 4);
return;
} else {
int x = 1;
assert (write (auth_file_fd, &x, 4) == 4);
}
assert (DC->has_auth);
assert (write (auth_file_fd, &DC->port, 4) == 4);
int l = strlen (DC->ip);
assert (write (auth_file_fd, &l, 4) == 4);
assert (write (auth_file_fd, DC->ip, l) == l);
assert (write (auth_file_fd, &DC->auth_key_id, 8) == 8);
assert (write (auth_file_fd, DC->auth_key, 256) == 256);
}
char *get_auth_key_filename (void);
void write_auth_file (void) {
if (binlog_enabled) { return; }
int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, 0600);
assert (auth_file_fd >= 0);
int x = DC_SERIALIZED_MAGIC;
assert (write (auth_file_fd, &x, 4) == 4);
assert (write (auth_file_fd, &tgl_state.max_dc_num, 4) == 4);
assert (write (auth_file_fd, &tgl_state.dc_working_num, 4) == 4);
tgl_dc_iterator_ex (write_dc, &auth_file_fd);
assert (write (auth_file_fd, &tgl_state.our_id, 4) == 4);
close (auth_file_fd);
}
void read_dc (int auth_file_fd, int id, unsigned ver) {
int port = 0;
assert (read (auth_file_fd, &port, 4) == 4);
int l = 0;
assert (read (auth_file_fd, &l, 4) == 4);
assert (l >= 0 && l < 100);
char ip[100];
assert (read (auth_file_fd, ip, l) == l);
ip[l] = 0;
long long auth_key_id;
static unsigned char auth_key[256];
assert (read (auth_file_fd, &auth_key_id, 8) == 8);
assert (read (auth_file_fd, auth_key, 256) == 256);
//bl_do_add_dc (id, ip, l, port, auth_key_id, auth_key);
bl_do_dc_option (id, 2, "DC", l, ip, port);
bl_do_set_auth_key_id (id, auth_key);
bl_do_dc_signed (id);
}
void empty_auth_file (void) {
char *ip = tgl_state.test_mode ? TG_SERVER_TEST : TG_SERVER;
bl_do_dc_option (1, 3, "DC1", strlen (ip), ip, 443);
bl_do_set_working_dc (1);
}
int need_dc_list_update;
void read_auth_file (void) {
if (binlog_enabled) { return; }
int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, 0600);
if (auth_file_fd < 0) {
empty_auth_file ();
return;
}
assert (auth_file_fd >= 0);
unsigned x;
unsigned m;
if (read (auth_file_fd, &m, 4) < 4 || (m != DC_SERIALIZED_MAGIC)) {
close (auth_file_fd);
empty_auth_file ();
return;
}
assert (read (auth_file_fd, &x, 4) == 4);
assert (x > 0);
int dc_working_num;
assert (read (auth_file_fd, &dc_working_num, 4) == 4);
int i;
for (i = 0; i <= (int)x; i++) {
int y;
assert (read (auth_file_fd, &y, 4) == 4);
if (y) {
read_dc (auth_file_fd, i, m);
}
}
bl_do_set_working_dc (dc_working_num);
int our_id;
int l = read (auth_file_fd, &our_id, 4);
if (l < 4) {
assert (!l);
}
if (our_id) {
bl_do_set_our_id (our_id);
}
close (auth_file_fd);
}
int loop (void) { int loop (void) {
//on_start (); //on_start ();
tgl_set_callback (&upd_cb); tgl_set_callback (&upd_cb);
tgl_init (); tgl_init ();
double t = tglt_get_double_time (); if (binlog_enabled) {
logprintf ("replay log start\n"); double t = tglt_get_double_time ();
tgl_replay_log (); logprintf ("replay log start\n");
logprintf ("replay log end in %lf seconds\n", tglt_get_double_time () - t); tgl_replay_log ();
tgl_reopen_binlog_for_writing (); logprintf ("replay log end in %lf seconds\n", tglt_get_double_time () - t);
tgl_reopen_binlog_for_writing ();
} else {
read_auth_file ();
}
binlog_read = 1; binlog_read = 1;
//exit (0); //exit (0);
#ifdef USE_LUA #ifdef USE_LUA
@ -357,10 +469,11 @@ int loop (void) {
for (i = 0; i <= tgl_state.max_dc_num; i++) if (tgl_state.DC_list[i] && !tgl_signed_dc (tgl_state.DC_list[i])) { for (i = 0; i <= tgl_state.max_dc_num; i++) if (tgl_state.DC_list[i] && !tgl_signed_dc (tgl_state.DC_list[i])) {
tgl_do_export_auth (i, export_auth_callback, (void*)(long)tgl_state.DC_list[i]); tgl_do_export_auth (i, export_auth_callback, (void*)(long)tgl_state.DC_list[i]);
cur_a_dc = tgl_state.DC_working; cur_a_dc = tgl_state.DC_list[i];
net_loop (0, dc_signed_in); net_loop (0, dc_signed_in);
assert (tgl_signed_dc (tgl_state.DC_list[i]));
} }
//write_auth_file (); write_auth_file ();
fflush (stdout); fflush (stdout);
fflush (stderr); fflush (stderr);

2
main.c
View File

@ -269,7 +269,7 @@ void parse_config_val (config_t *conf, char **s, char *param_name, const char *d
*s = tstrdup (r); *s = tstrdup (r);
} }
} else { } else {
if (path) { if (path && default_name) {
tasprintf (s, "%s/%s", path, default_name); tasprintf (s, "%s/%s", path, default_name);
} else { } else {
*s = default_name ? tstrdup (default_name) : 0; *s = default_name ? tstrdup (default_name) : 0;

View File

@ -76,7 +76,7 @@
//int verbosity; //int verbosity;
static int auth_success; static int auth_success;
//static enum dc_state c_state; //static enum tgl_dc_state c_state;
static char nonce[256]; static char nonce[256];
static char new_nonce[256]; static char new_nonce[256];
static char server_nonce[256]; static char server_nonce[256];
@ -231,7 +231,7 @@ static int rpc_send_message (struct connection *c, void *data, int len) {
} }
static int send_req_pq_packet (struct connection *c) { static int send_req_pq_packet (struct connection *c) {
struct dc *D = tgl_state.net_methods->get_dc (c); struct tgl_dc *D = tgl_state.net_methods->get_dc (c);
assert (D->state == st_init); assert (D->state == st_init);
tglt_secure_random (nonce, 16); tglt_secure_random (nonce, 16);
@ -555,7 +555,7 @@ static int process_dh_answer (struct connection *c, char *packet, int len) {
assert (!memcmp (decrypt_buffer, sha1_buffer, 20)); assert (!memcmp (decrypt_buffer, sha1_buffer, 20));
assert ((char *) in_end - (char *) in_ptr < 16); assert ((char *) in_end - (char *) in_ptr < 16);
struct dc *D = tgl_state.net_methods->get_dc (c); struct tgl_dc *D = tgl_state.net_methods->get_dc (c);
D->server_time_delta = server_time - time (0); D->server_time_delta = server_time - time (0);
D->server_time_udelta = server_time - get_utime (CLOCK_MONOTONIC); D->server_time_udelta = server_time - get_utime (CLOCK_MONOTONIC);
//logprintf ( "server time is %d, delta = %d\n", server_time, server_time_delta); //logprintf ( "server time is %d, delta = %d\n", server_time, server_time_delta);
@ -627,7 +627,7 @@ static int process_auth_complete (struct connection *c UU, char *packet, int len
tmp[32] = 1; tmp[32] = 1;
//GET_DC(c)->auth_key_id = *(long long *)(sha1_buffer + 12); //GET_DC(c)->auth_key_id = *(long long *)(sha1_buffer + 12);
struct dc *D = tgl_state.net_methods->get_dc (c); struct tgl_dc *D = tgl_state.net_methods->get_dc (c);
bl_do_set_auth_key_id (D->id, (unsigned char *)D->auth_key); bl_do_set_auth_key_id (D->id, (unsigned char *)D->auth_key);
sha1 ((unsigned char *)D->auth_key, 256, sha1_buffer); sha1 ((unsigned char *)D->auth_key, 256, sha1_buffer);
@ -661,14 +661,14 @@ static struct encrypted_message enc_msg;
static long long client_last_msg_id, server_last_msg_id; static long long client_last_msg_id, server_last_msg_id;
static double get_server_time (struct dc *DC) { static double get_server_time (struct tgl_dc *DC) {
if (!DC->server_time_udelta) { if (!DC->server_time_udelta) {
DC->server_time_udelta = get_utime (CLOCK_REALTIME) - get_utime (CLOCK_MONOTONIC); DC->server_time_udelta = get_utime (CLOCK_REALTIME) - get_utime (CLOCK_MONOTONIC);
} }
return get_utime (CLOCK_MONOTONIC) + DC->server_time_udelta; return get_utime (CLOCK_MONOTONIC) + DC->server_time_udelta;
} }
static long long generate_next_msg_id (struct dc *DC) { static long long generate_next_msg_id (struct tgl_dc *DC) {
long long next_id = (long long) (get_server_time (DC) * (1LL << 32)) & -4; long long next_id = (long long) (get_server_time (DC) * (1LL << 32)) & -4;
if (next_id <= client_last_msg_id) { if (next_id <= client_last_msg_id) {
next_id = client_last_msg_id += 4; next_id = client_last_msg_id += 4;
@ -678,8 +678,8 @@ static long long generate_next_msg_id (struct dc *DC) {
return next_id; return next_id;
} }
static void init_enc_msg (struct session *S, int useful) { static void init_enc_msg (struct tgl_session *S, int useful) {
struct dc *DC = S->dc; struct tgl_dc *DC = S->dc;
assert (DC->auth_key_id); assert (DC->auth_key_id);
enc_msg.auth_key_id = DC->auth_key_id; enc_msg.auth_key_id = DC->auth_key_id;
// assert (DC->server_salt); // assert (DC->server_salt);
@ -699,7 +699,7 @@ static void init_enc_msg (struct session *S, int useful) {
S->seq_no += 2; S->seq_no += 2;
}; };
static int aes_encrypt_message (struct dc *DC, struct encrypted_message *enc) { static int aes_encrypt_message (struct tgl_dc *DC, struct encrypted_message *enc) {
unsigned char sha1_buffer[20]; unsigned char sha1_buffer[20];
const int MINSZ = offsetof (struct encrypted_message, message); const int MINSZ = offsetof (struct encrypted_message, message);
const int UNENCSZ = offsetof (struct encrypted_message, server_salt); const int UNENCSZ = offsetof (struct encrypted_message, server_salt);
@ -715,8 +715,8 @@ static int aes_encrypt_message (struct dc *DC, struct encrypted_message *enc) {
} }
long long tglmp_encrypt_send_message (struct connection *c, int *msg, int msg_ints, int useful) { long long tglmp_encrypt_send_message (struct connection *c, int *msg, int msg_ints, int useful) {
struct dc *DC = tgl_state.net_methods->get_dc (c); struct tgl_dc *DC = tgl_state.net_methods->get_dc (c);
struct session *S = tgl_state.net_methods->get_session (c); struct tgl_session *S = tgl_state.net_methods->get_session (c);
assert (S); assert (S);
const int UNENCSZ = offsetof (struct encrypted_message, server_salt); const int UNENCSZ = offsetof (struct encrypted_message, server_salt);
@ -928,7 +928,7 @@ static int process_rpc_message (struct connection *c UU, struct encrypted_messag
const int UNENCSZ = offsetof (struct encrypted_message, server_salt); const int UNENCSZ = offsetof (struct encrypted_message, server_salt);
vlogprintf (E_DEBUG, "process_rpc_message(), len=%d\n", len); vlogprintf (E_DEBUG, "process_rpc_message(), len=%d\n", len);
assert (len >= MINSZ && (len & 15) == (UNENCSZ & 15)); assert (len >= MINSZ && (len & 15) == (UNENCSZ & 15));
struct dc *DC = tgl_state.net_methods->get_dc (c); struct tgl_dc *DC = tgl_state.net_methods->get_dc (c);
assert (enc->auth_key_id == DC->auth_key_id); assert (enc->auth_key_id == DC->auth_key_id);
assert (DC->auth_key_id); assert (DC->auth_key_id);
tgl_init_aes_auth (DC->auth_key + 8, enc->msg_key, AES_DECRYPT); tgl_init_aes_auth (DC->auth_key + 8, enc->msg_key, AES_DECRYPT);
@ -972,7 +972,7 @@ static int process_rpc_message (struct connection *c UU, struct encrypted_messag
in_ptr = enc->message; in_ptr = enc->message;
in_end = in_ptr + (enc->msg_len / 4); in_end = in_ptr + (enc->msg_len / 4);
struct session *S = tgl_state.net_methods->get_session (c); struct tgl_session *S = tgl_state.net_methods->get_session (c);
if (enc->msg_id & 1) { if (enc->msg_id & 1) {
tgln_insert_msg_id (S, enc->msg_id); tgln_insert_msg_id (S, enc->msg_id);
} }
@ -1004,7 +1004,7 @@ static int rpc_execute (struct connection *c, int op, int len) {
#if !defined(__MACH__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined (__CYGWIN__) #if !defined(__MACH__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined (__CYGWIN__)
// setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4); // setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
#endif #endif
struct dc *D = tgl_state.net_methods->get_dc (c); struct tgl_dc *D = tgl_state.net_methods->get_dc (c);
int o = D->state; int o = D->state;
if (D->flags & 1) { o = st_authorized;} if (D->flags & 1) { o = st_authorized;}
switch (o) { switch (o) {
@ -1059,7 +1059,7 @@ static int tc_becomes_ready (struct connection *c) {
#if !defined(__MACH__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined (__CYGWIN__) #if !defined(__MACH__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined (__CYGWIN__)
// setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4); // setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
#endif #endif
struct dc *D = tgl_state.net_methods->get_dc (c); struct tgl_dc *D = tgl_state.net_methods->get_dc (c);
int o = D->state; int o = D->state;
if (D->flags & 1) { o = st_authorized; } if (D->flags & 1) { o = st_authorized; }
switch (o) { switch (o) {
@ -1108,7 +1108,7 @@ void tglmp_on_start (const char *key) {
// return auth_success; // return auth_success;
//} //}
void tgl_dc_authorize (struct dc *DC) { void tgl_dc_authorize (struct tgl_dc *DC) {
//c_state = 0; //c_state = 0;
//auth_success = 0; //auth_success = 0;
if (!DC->sessions[0]) { if (!DC->sessions[0]) {
@ -1121,7 +1121,7 @@ void tgl_dc_authorize (struct dc *DC) {
#define long_cmp(a,b) ((a) > (b) ? 1 : (a) == (b) ? 0 : -1) #define long_cmp(a,b) ((a) > (b) ? 1 : (a) == (b) ? 0 : -1)
DEFINE_TREE(long,long long,long_cmp,0) DEFINE_TREE(long,long long,long_cmp,0)
static int send_all_acks (struct session *S) { static int send_all_acks (struct tgl_session *S) {
clear_packet (); clear_packet ();
out_int (CODE_msgs_ack); out_int (CODE_msgs_ack);
out_int (CODE_vector); out_int (CODE_vector);
@ -1140,7 +1140,7 @@ static void send_all_acks_gateway (evutil_socket_t fd, short what, void *arg) {
} }
void tgln_insert_msg_id (struct session *S, long long id) { void tgln_insert_msg_id (struct tgl_session *S, long long id) {
if (!S->ack_tree) { if (!S->ack_tree) {
static struct timeval ptimeout = { ACK_TIMEOUT, 0}; static struct timeval ptimeout = { ACK_TIMEOUT, 0};
event_add (S->ev, &ptimeout); event_add (S->ev, &ptimeout);
@ -1150,11 +1150,11 @@ void tgln_insert_msg_id (struct session *S, long long id) {
} }
} }
//extern struct dc *DC_list[]; //extern struct tgl_dc *DC_list[];
struct dc *tglmp_alloc_dc (int id, char *ip, int port UU) { struct tgl_dc *tglmp_alloc_dc (int id, char *ip, int port UU) {
assert (!tgl_state.DC_list[id]); assert (!tgl_state.DC_list[id]);
struct dc *DC = talloc0 (sizeof (*DC)); struct tgl_dc *DC = talloc0 (sizeof (*DC));
DC->id = id; DC->id = id;
DC->ip = ip; DC->ip = ip;
DC->port = port; DC->port = port;
@ -1171,8 +1171,8 @@ static struct mtproto_methods mtproto_methods = {
.close = rpc_close .close = rpc_close
}; };
void tglmp_dc_create_session (struct dc *DC) { void tglmp_dc_create_session (struct tgl_dc *DC) {
struct session *S = talloc0 (sizeof (*S)); struct tgl_session *S = talloc0 (sizeof (*S));
assert (RAND_pseudo_bytes ((unsigned char *) &S->session_id, 8) >= 0); assert (RAND_pseudo_bytes ((unsigned char *) &S->session_id, 8) >= 0);
S->dc = DC; S->dc = DC;
S->c = tgl_state.net_methods->create_connection (DC->ip, DC->port, S, DC, &mtproto_methods); S->c = tgl_state.net_methods->create_connection (DC->ip, DC->port, S, DC, &mtproto_methods);
@ -1191,3 +1191,17 @@ void tgl_do_send_ping (struct connection *c) {
*(long long *)(x + 1) = lrand48 () * (1ll << 32) + lrand48 (); *(long long *)(x + 1) = lrand48 () * (1ll << 32) + lrand48 ();
tglmp_encrypt_send_message (c, x, 3, 0); tglmp_encrypt_send_message (c, x, 3, 0);
} }
void tgl_dc_iterator (void (*iterator)(struct tgl_dc *DC)) {
int i;
for (i = 0; i <= tgl_state.max_dc_num; i++) {
iterator (tgl_state.DC_list[i]);
}
}
void tgl_dc_iterator_ex (void (*iterator)(struct tgl_dc *DC, void *extra), void *extra) {
int i;
for (i = 0; i <= tgl_state.max_dc_num; i++) {
iterator (tgl_state.DC_list[i], extra);
}
}

View File

@ -23,7 +23,7 @@
#include <openssl/bn.h> #include <openssl/bn.h>
//void on_start (void); //void on_start (void);
//..long long encrypt_send_message (struct connection *c, int *msg, int msg_ints, int useful); //..long long encrypt_send_message (struct connection *c, int *msg, int msg_ints, int useful);
//void dc_authorize (struct dc *DC); //void dc_authorize (struct tgl_dc *DC);
//void work_update (struct connection *c, long long msg_id); //void work_update (struct connection *c, long long msg_id);
//void work_update_binlog (void); //void work_update_binlog (void);
//int check_g (unsigned char p[256], BIGNUM *g); //int check_g (unsigned char p[256], BIGNUM *g);
@ -31,11 +31,11 @@
//int check_DH_params (BIGNUM *p, int g); //int check_DH_params (BIGNUM *p, int g);
//void secure_random (void *s, int l); //void secure_random (void *s, int l);
#include "tgl.h"
struct connection; struct connection;
struct dc; struct tgl_dc;
//#include "queries.h" //#include "queries.h"
#define TG_SERVER "173.240.5.1"
#define TG_SERVER_TEST "173.240.5.253"
#define TG_APP_HASH "36722c72256a24c1225de00eb6a1ca74" #define TG_APP_HASH "36722c72256a24c1225de00eb6a1ca74"
#define TG_APP_ID 2899 #define TG_APP_ID 2899
@ -44,51 +44,13 @@ struct dc;
struct connection; struct connection;
enum dc_state {
st_init,
st_reqpq_sent,
st_reqdh_sent,
st_client_dh_sent,
st_authorized,
st_error
};
#define MAX_DC_SESSIONS 3
struct session {
struct dc *dc;
long long session_id;
int seq_no;
struct connection *c;
struct tree_long *ack_tree;
struct event *ev;
//struct event_timer ev;
};
struct dc {
int id;
int port;
int flags;
enum dc_state state;
char *ip;
char *user;
struct session *sessions[MAX_DC_SESSIONS];
char auth_key[256];
long long auth_key_id;
long long server_salt;
int server_time_delta;
double server_time_udelta;
int has_auth;
};
long long tglmp_encrypt_send_message (struct connection *c, int *msg, int msg_ints, int useful); long long tglmp_encrypt_send_message (struct connection *c, int *msg, int msg_ints, int useful);
void tglmp_dc_create_session (struct dc *DC); void tglmp_dc_create_session (struct tgl_dc *DC);
int tglmp_check_g (unsigned char p[256], BIGNUM *g); int tglmp_check_g (unsigned char p[256], BIGNUM *g);
int tglmp_check_DH_params (BIGNUM *p, int g); int tglmp_check_DH_params (BIGNUM *p, int g);
struct dc *tglmp_alloc_dc (int id, char *ip, int port); struct tgl_dc *tglmp_alloc_dc (int id, char *ip, int port);
void tgln_insert_msg_id (struct session *S, long long id); void tgln_insert_msg_id (struct tgl_session *S, long long id);
void tglmp_on_start (const char *key); void tglmp_on_start (const char *key);
void tgl_dc_authorize (struct dc *DC); void tgl_dc_authorize (struct tgl_dc *DC);
#endif #endif

6
net.c
View File

@ -251,7 +251,7 @@ static void conn_try_write (evutil_socket_t fd, short what, void *arg) {
} }
} }
struct connection *tgln_create_connection (const char *host, int port, struct session *session, struct dc *dc, struct mtproto_methods *methods) { struct connection *tgln_create_connection (const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods) {
struct connection *c = talloc0 (sizeof (*c)); struct connection *c = talloc0 (sizeof (*c));
int fd = socket (AF_INET, SOCK_STREAM, 0); int fd = socket (AF_INET, SOCK_STREAM, 0);
if (fd == -1) { if (fd == -1) {
@ -588,11 +588,11 @@ static void incr_out_packet_num (struct connection *c) {
c->out_packet_num ++; c->out_packet_num ++;
} }
static struct dc *get_dc (struct connection *c) { static struct tgl_dc *get_dc (struct connection *c) {
return c->dc; return c->dc;
} }
static struct session *get_session (struct connection *c) { static struct tgl_session *get_session (struct connection *c) {
return c->session; return c->session;
} }

16
net.h
View File

@ -26,7 +26,7 @@
#define STATE_FILE_MAGIC 0x84217a0d #define STATE_FILE_MAGIC 0x84217a0d
#define SECRET_CHAT_FILE_MAGIC 0xa9840add #define SECRET_CHAT_FILE_MAGIC 0xa9840add
struct dc_serialized { struct tgl_dc_serialized {
int magic; int magic;
int port; int port;
char ip[64]; char ip[64];
@ -70,8 +70,8 @@ struct connection {
int last_connect_time; int last_connect_time;
int in_fail_timer; int in_fail_timer;
struct mtproto_methods *methods; struct mtproto_methods *methods;
struct session *session; struct tgl_session *session;
struct dc *dc; struct tgl_dc *dc;
void *extra; void *extra;
struct event *ping_ev; struct event *ping_ev;
struct event *fail_ev; struct event *fail_ev;
@ -87,16 +87,16 @@ void tgln_flush_out (struct connection *c);
int tgln_read_in (struct connection *c, void *data, int len); int tgln_read_in (struct connection *c, void *data, int len);
int tgln_read_in_lookup (struct connection *c, void *data, int len); int tgln_read_in_lookup (struct connection *c, void *data, int len);
void tgln_insert_msg_id (struct session *S, long long id); void tgln_insert_msg_id (struct tgl_session *S, long long id);
extern struct tgl_net_methods tgl_conn_methods; extern struct tgl_net_methods tgl_conn_methods;
//void create_all_outbound_connections (void); //void create_all_outbound_connections (void);
//struct connection *create_connection (const char *host, int port, struct session *session, struct connection_methods *methods); //struct connection *create_connection (const char *host, int port, struct tgl_session *session, struct connection_methods *methods);
struct dc *tgln_alloc_dc (int id, char *ip, int port); struct tgl_dc *tgln_alloc_dc (int id, char *ip, int port);
void tgln_dc_create_session (struct dc *DC, struct mtproto_methods *methods); void tgln_dc_create_session (struct tgl_dc *DC, struct mtproto_methods *methods);
struct connection *tgln_create_connection (const char *host, int port, struct session *session, struct dc *dc, struct mtproto_methods *methods); struct connection *tgln_create_connection (const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods);
#define GET_DC(c) (c->session->dc) #define GET_DC(c) (c->session->dc)
#endif #endif

View File

@ -125,7 +125,7 @@ static void alarm_query_gateway (evutil_socket_t fd, short what, void *arg) {
} }
struct query *tglq_send_query (struct dc *DC, int ints, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra) { struct query *tglq_send_query (struct tgl_dc *DC, int ints, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra) {
assert (DC); assert (DC);
assert (DC->auth_key_id); assert (DC->auth_key_id);
if (!DC->sessions[0]) { if (!DC->sessions[0]) {
@ -275,8 +275,8 @@ void tglq_query_result (long long id UU) {
//int max_bcast_size; //int max_bcast_size;
//int want_dc_num; //int want_dc_num;
//int new_dc_num; //int new_dc_num;
//extern struct dc *DC_list[]; //extern struct tgl_dc *DC_list[];
//extern struct dc *tgl_state.DC_working; //extern struct tgl_dc *tgl_state.DC_working;
static void out_random (int n) { static void out_random (int n) {
assert (n <= 32); assert (n <= 32);
@ -2184,7 +2184,7 @@ static int import_auth_on_answer (struct query *q UU) {
fetch_int (); // expires fetch_int (); // expires
tglf_fetch_alloc_user (); tglf_fetch_alloc_user ();
bl_do_dc_signed (((struct dc *)q->extra)->id); bl_do_dc_signed (((struct tgl_dc *)q->extra)->id);
if (q->callback) { if (q->callback) {
((void (*)(void *, int))q->callback) (q->callback_extra, 1); ((void (*)(void *, int))q->callback) (q->callback_extra, 1);

View File

@ -43,15 +43,15 @@ struct query {
void *data; void *data;
struct query_methods *methods; struct query_methods *methods;
struct event *ev; struct event *ev;
struct dc *DC; struct tgl_dc *DC;
struct session *session; struct tgl_session *session;
void *extra; void *extra;
void *callback; void *callback;
void *callback_extra; void *callback_extra;
}; };
struct query *tglq_send_query (struct dc *DC, int len, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra); struct query *tglq_send_query (struct tgl_dc *DC, int len, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra);
void tglq_query_ack (long long id); void tglq_query_ack (long long id);
void tglq_query_error (long long id); void tglq_query_error (long long id);
void tglq_query_result (long long id); void tglq_query_result (long long id);

View File

@ -22,6 +22,44 @@
typedef struct { int type; int id; } tgl_peer_id_t; typedef struct { int type; int id; } tgl_peer_id_t;
enum tgl_dc_state {
st_init,
st_reqpq_sent,
st_reqdh_sent,
st_client_dh_sent,
st_authorized,
st_error
};
#define MAX_DC_SESSIONS 3
struct tgl_session {
struct tgl_dc *dc;
long long session_id;
int seq_no;
struct connection *c;
struct tree_long *ack_tree;
struct event *ev;
//struct event_timer ev;
};
struct tgl_dc {
int id;
int port;
int flags;
enum tgl_dc_state state;
char *ip;
char *user;
struct tgl_session *sessions[MAX_DC_SESSIONS];
char auth_key[256];
long long auth_key_id;
long long server_salt;
int server_time_delta;
double server_time_udelta;
int has_auth;
};
enum tgl_message_media_type { enum tgl_message_media_type {
tgl_message_media_none, tgl_message_media_none,
tgl_message_media_photo, tgl_message_media_photo,

4
tgl.c
View File

@ -51,12 +51,12 @@ void tgl_init (void) {
tglmp_on_start (tgl_state.rsa_key); tglmp_on_start (tgl_state.rsa_key);
} }
int tgl_authorized_dc (struct dc *DC) { int tgl_authorized_dc (struct tgl_dc *DC) {
assert (DC); assert (DC);
return DC->auth_key_id; return DC->auth_key_id;
} }
int tgl_signed_dc (struct dc *DC) { int tgl_signed_dc (struct tgl_dc *DC) {
assert (DC); assert (DC);
return DC->has_auth; return DC->has_auth;
} }

26
tgl.h
View File

@ -5,6 +5,8 @@
#include <string.h> #include <string.h>
#define TGL_MAX_DC_NUM 100 #define TGL_MAX_DC_NUM 100
#define TG_SERVER "173.240.5.1"
#define TG_SERVER_TEST "173.240.5.253"
// JUST RANDOM STRING // JUST RANDOM STRING
#define TGL_BUILD "1828" #define TGL_BUILD "1828"
@ -12,8 +14,8 @@
struct connection; struct connection;
struct mtproto_methods; struct mtproto_methods;
struct session; struct tgl_session;
struct dc; struct tgl_dc;
struct bingnum_ctx; struct bingnum_ctx;
#define TGL_UPDATE_CREATED 1 #define TGL_UPDATE_CREATED 1
@ -79,10 +81,10 @@ struct tgl_net_methods {
int (*read_in_lookup) (struct connection *c, void *data, int len); int (*read_in_lookup) (struct connection *c, void *data, int len);
void (*flush_out) (struct connection *c); void (*flush_out) (struct connection *c);
void (*incr_out_packet_num) (struct connection *c); void (*incr_out_packet_num) (struct connection *c);
struct dc *(*get_dc) (struct connection *c); struct tgl_dc *(*get_dc) (struct connection *c);
struct session *(*get_session) (struct connection *c); struct tgl_session *(*get_session) (struct connection *c);
struct connection *(*create_connection) (const char *host, int port, struct session *session, struct dc *dc, struct mtproto_methods *methods); struct connection *(*create_connection) (const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods);
}; };
@ -109,8 +111,8 @@ struct tgl_state {
int unread_messages; int unread_messages;
long long locks; long long locks;
struct dc *DC_list[TGL_MAX_DC_NUM]; struct tgl_dc *DC_list[TGL_MAX_DC_NUM];
struct dc *DC_working; struct tgl_dc *DC_working;
int max_dc_num; int max_dc_num;
int dc_working_num; int dc_working_num;
@ -246,8 +248,8 @@ void tgl_do_visualize_key (tgl_peer_id_t id, unsigned char buf[16]);
void tgl_do_send_ping (struct connection *c); void tgl_do_send_ping (struct connection *c);
int tgl_authorized_dc (struct dc *DC); int tgl_authorized_dc (struct tgl_dc *DC);
int tgl_signed_dc (struct dc *DC); int tgl_signed_dc (struct tgl_dc *DC);
//void tgl_do_get_suggested (void); //void tgl_do_get_suggested (void);
@ -261,8 +263,12 @@ struct mtproto_methods {
}; };
void tgl_init (void); void tgl_init (void);
void tgl_dc_authorize (struct dc *DC); void tgl_dc_authorize (struct tgl_dc *DC);
void tgl_dc_iterator (void (*iterator)(struct tgl_dc *DC));
void tgl_dc_iterator_ex (void (*iterator)(struct tgl_dc *DC, void *extra), void *extra);
double tglt_get_double_time (void); double tglt_get_double_time (void);
#endif #endif