diff --git a/binlog.c b/binlog.c index 9c4558d..70f6ff8 100644 --- a/binlog.c +++ b/binlog.c @@ -1212,6 +1212,7 @@ static void create_new_binlog (void) { void tgl_replay_log (void) { + if (!tgl_state.binlog_enabled) { return; } if (access (get_binlog_file_name (), F_OK) < 0) { printf ("No binlog found. Creating new one\n"); 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) { - struct dc *DC = tgl_state.DC_list[id]; + struct tgl_dc *DC = tgl_state.DC_list[id]; if (DC) { return; } clear_packet (); @@ -1921,3 +1922,14 @@ void bl_do_msg_update (long long id) { out_long (id); 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)); +}*/ diff --git a/binlog.h b/binlog.h index cdaaa29..9393c2c 100644 --- a/binlog.h +++ b/binlog.h @@ -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_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 diff --git a/binlog.tl b/binlog.tl index a63b018..0c0c71e 100644 --- a/binlog.tl +++ b/binlog.tl @@ -68,3 +68,5 @@ binlog.deleteMsg id:long = binlog.Update; binlog.msgSeqUpdate 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; diff --git a/config.h.in b/config.h.in index f0315e0..dd0d841 100644 --- a/config.h.in +++ b/config.h.in @@ -21,6 +21,10 @@ /* Define to 1 if you have the header file. */ #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). */ #undef HAVE_LIBCONFIG diff --git a/configure b/configure index bf5cc3e..1809eef 100755 --- a/configure +++ b/configure @@ -3469,6 +3469,51 @@ _ACEOF 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="" { $as_echo "$as_me:${as_lineno-$LINENO}: checking for rl_save_prompt in -lreadline" >&5 diff --git a/interface.c b/interface.c index b396af7..bbcd098 100644 --- a/interface.c +++ b/interface.c @@ -78,7 +78,6 @@ int readline_active; int log_level; - char *line_ptr; int in_chat_mode; diff --git a/loop.c b/loop.c index fd8be3f..c99968e 100644 --- a/loop.c +++ b/loop.c @@ -50,6 +50,7 @@ #include "loop.h" #include "lua-tg.h" #include "tgl.h" +#include "binlog.h" int verbosity; @@ -149,7 +150,7 @@ int main_loop (void) { return 0; } -struct dc *cur_a_dc; +struct tgl_dc *cur_a_dc; int is_authorized (void) { return tgl_authorized_dc (cur_a_dc); } @@ -229,16 +230,127 @@ int wait_dialog_list; 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) { //on_start (); tgl_set_callback (&upd_cb); tgl_init (); - - double t = tglt_get_double_time (); - logprintf ("replay log start\n"); - tgl_replay_log (); - logprintf ("replay log end in %lf seconds\n", tglt_get_double_time () - t); - tgl_reopen_binlog_for_writing (); + + if (binlog_enabled) { + double t = tglt_get_double_time (); + logprintf ("replay log start\n"); + tgl_replay_log (); + 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; //exit (0); #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])) { 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); + assert (tgl_signed_dc (tgl_state.DC_list[i])); } - //write_auth_file (); + write_auth_file (); fflush (stdout); fflush (stderr); diff --git a/main.c b/main.c index 308421c..cf9eb77 100644 --- a/main.c +++ b/main.c @@ -269,7 +269,7 @@ void parse_config_val (config_t *conf, char **s, char *param_name, const char *d *s = tstrdup (r); } } else { - if (path) { + if (path && default_name) { tasprintf (s, "%s/%s", path, default_name); } else { *s = default_name ? tstrdup (default_name) : 0; diff --git a/mtproto-client.c b/mtproto-client.c index 43be3a7..51a4f50 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -76,7 +76,7 @@ //int verbosity; static int auth_success; -//static enum dc_state c_state; +//static enum tgl_dc_state c_state; static char nonce[256]; static char new_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) { - 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); 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 ((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_udelta = server_time - get_utime (CLOCK_MONOTONIC); //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; //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); 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 double get_server_time (struct dc *DC) { +static double get_server_time (struct tgl_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; } -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; if (next_id <= client_last_msg_id) { next_id = client_last_msg_id += 4; @@ -678,8 +678,8 @@ static long long generate_next_msg_id (struct dc *DC) { return next_id; } -static void init_enc_msg (struct session *S, int useful) { - struct dc *DC = S->dc; +static void init_enc_msg (struct tgl_session *S, int useful) { + struct tgl_dc *DC = S->dc; assert (DC->auth_key_id); enc_msg.auth_key_id = DC->auth_key_id; // assert (DC->server_salt); @@ -699,7 +699,7 @@ static void init_enc_msg (struct session *S, int useful) { 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]; const int MINSZ = offsetof (struct encrypted_message, message); 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) { - struct dc *DC = tgl_state.net_methods->get_dc (c); - struct session *S = tgl_state.net_methods->get_session (c); + struct tgl_dc *DC = tgl_state.net_methods->get_dc (c); + struct tgl_session *S = tgl_state.net_methods->get_session (c); assert (S); 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); vlogprintf (E_DEBUG, "process_rpc_message(), len=%d\n", len); 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 (DC->auth_key_id); 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_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) { 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__) // setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4); #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; if (D->flags & 1) { o = st_authorized;} switch (o) { @@ -1059,7 +1059,7 @@ static int tc_becomes_ready (struct connection *c) { #if !defined(__MACH__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined (__CYGWIN__) // setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4); #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; if (D->flags & 1) { o = st_authorized; } switch (o) { @@ -1108,7 +1108,7 @@ void tglmp_on_start (const char *key) { // return auth_success; //} -void tgl_dc_authorize (struct dc *DC) { +void tgl_dc_authorize (struct tgl_dc *DC) { //c_state = 0; //auth_success = 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_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 (); out_int (CODE_msgs_ack); 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) { static struct timeval ptimeout = { ACK_TIMEOUT, 0}; 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]); - struct dc *DC = talloc0 (sizeof (*DC)); + struct tgl_dc *DC = talloc0 (sizeof (*DC)); DC->id = id; DC->ip = ip; DC->port = port; @@ -1171,8 +1171,8 @@ static struct mtproto_methods mtproto_methods = { .close = rpc_close }; -void tglmp_dc_create_session (struct dc *DC) { - struct session *S = talloc0 (sizeof (*S)); +void tglmp_dc_create_session (struct tgl_dc *DC) { + struct tgl_session *S = talloc0 (sizeof (*S)); assert (RAND_pseudo_bytes ((unsigned char *) &S->session_id, 8) >= 0); S->dc = DC; 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 (); 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); + } +} diff --git a/mtproto-client.h b/mtproto-client.h index b4bd80c..77e573c 100644 --- a/mtproto-client.h +++ b/mtproto-client.h @@ -23,7 +23,7 @@ #include //void on_start (void); //..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_binlog (void); //int check_g (unsigned char p[256], BIGNUM *g); @@ -31,11 +31,11 @@ //int check_DH_params (BIGNUM *p, int g); //void secure_random (void *s, int l); +#include "tgl.h" + struct connection; -struct dc; +struct tgl_dc; //#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_ID 2899 @@ -44,51 +44,13 @@ struct dc; 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); -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_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 tgl_dc_authorize (struct dc *DC); +void tgl_dc_authorize (struct tgl_dc *DC); #endif diff --git a/net.c b/net.c index 054d990..0ab60f7 100644 --- a/net.c +++ b/net.c @@ -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)); int fd = socket (AF_INET, SOCK_STREAM, 0); if (fd == -1) { @@ -588,11 +588,11 @@ static void incr_out_packet_num (struct connection *c) { c->out_packet_num ++; } -static struct dc *get_dc (struct connection *c) { +static struct tgl_dc *get_dc (struct connection *c) { return c->dc; } -static struct session *get_session (struct connection *c) { +static struct tgl_session *get_session (struct connection *c) { return c->session; } diff --git a/net.h b/net.h index ca90624..667bd7c 100644 --- a/net.h +++ b/net.h @@ -26,7 +26,7 @@ #define STATE_FILE_MAGIC 0x84217a0d #define SECRET_CHAT_FILE_MAGIC 0xa9840add -struct dc_serialized { +struct tgl_dc_serialized { int magic; int port; char ip[64]; @@ -70,8 +70,8 @@ struct connection { int last_connect_time; int in_fail_timer; struct mtproto_methods *methods; - struct session *session; - struct dc *dc; + struct tgl_session *session; + struct tgl_dc *dc; void *extra; struct event *ping_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_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; //void create_all_outbound_connections (void); -//struct connection *create_connection (const char *host, int port, struct session *session, struct connection_methods *methods); -struct dc *tgln_alloc_dc (int id, char *ip, int port); -void tgln_dc_create_session (struct 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 *create_connection (const char *host, int port, struct tgl_session *session, struct connection_methods *methods); +struct tgl_dc *tgln_alloc_dc (int id, char *ip, int port); +void tgln_dc_create_session (struct tgl_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) #endif diff --git a/queries.c b/queries.c index c8404ce..fa93689 100644 --- a/queries.c +++ b/queries.c @@ -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->auth_key_id); if (!DC->sessions[0]) { @@ -275,8 +275,8 @@ void tglq_query_result (long long id UU) { //int max_bcast_size; //int want_dc_num; //int new_dc_num; -//extern struct dc *DC_list[]; -//extern struct dc *tgl_state.DC_working; +//extern struct tgl_dc *DC_list[]; +//extern struct tgl_dc *tgl_state.DC_working; static void out_random (int n) { assert (n <= 32); @@ -2184,7 +2184,7 @@ static int import_auth_on_answer (struct query *q UU) { fetch_int (); // expires 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) { ((void (*)(void *, int))q->callback) (q->callback_extra, 1); diff --git a/queries.h b/queries.h index b908a3b..91b0588 100644 --- a/queries.h +++ b/queries.h @@ -43,15 +43,15 @@ struct query { void *data; struct query_methods *methods; struct event *ev; - struct dc *DC; - struct session *session; + struct tgl_dc *DC; + struct tgl_session *session; void *extra; void *callback; 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_error (long long id); void tglq_query_result (long long id); diff --git a/tgl-layout.h b/tgl-layout.h index cf15aae..5a830c7 100644 --- a/tgl-layout.h +++ b/tgl-layout.h @@ -22,6 +22,44 @@ 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 { tgl_message_media_none, tgl_message_media_photo, diff --git a/tgl.c b/tgl.c index 594bd78..403f698 100644 --- a/tgl.c +++ b/tgl.c @@ -51,12 +51,12 @@ void tgl_init (void) { tglmp_on_start (tgl_state.rsa_key); } -int tgl_authorized_dc (struct dc *DC) { +int tgl_authorized_dc (struct tgl_dc *DC) { assert (DC); return DC->auth_key_id; } -int tgl_signed_dc (struct dc *DC) { +int tgl_signed_dc (struct tgl_dc *DC) { assert (DC); return DC->has_auth; } diff --git a/tgl.h b/tgl.h index 59b4d2b..ad11d1c 100644 --- a/tgl.h +++ b/tgl.h @@ -5,6 +5,8 @@ #include #define TGL_MAX_DC_NUM 100 +#define TG_SERVER "173.240.5.1" +#define TG_SERVER_TEST "173.240.5.253" // JUST RANDOM STRING #define TGL_BUILD "1828" @@ -12,8 +14,8 @@ struct connection; struct mtproto_methods; -struct session; -struct dc; +struct tgl_session; +struct tgl_dc; struct bingnum_ctx; #define TGL_UPDATE_CREATED 1 @@ -79,10 +81,10 @@ struct tgl_net_methods { int (*read_in_lookup) (struct connection *c, void *data, int len); void (*flush_out) (struct connection *c); void (*incr_out_packet_num) (struct connection *c); - struct dc *(*get_dc) (struct connection *c); - struct session *(*get_session) (struct connection *c); + struct tgl_dc *(*get_dc) (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; long long locks; - struct dc *DC_list[TGL_MAX_DC_NUM]; - struct dc *DC_working; + struct tgl_dc *DC_list[TGL_MAX_DC_NUM]; + struct tgl_dc *DC_working; int max_dc_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); -int tgl_authorized_dc (struct dc *DC); -int tgl_signed_dc (struct dc *DC); +int tgl_authorized_dc (struct tgl_dc *DC); +int tgl_signed_dc (struct tgl_dc *DC); //void tgl_do_get_suggested (void); @@ -261,8 +263,12 @@ struct mtproto_methods { }; 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); + + #endif