Skip to content

Commit

Permalink
layer 30 support
Browse files Browse the repository at this point in the history
  • Loading branch information
vvaltman committed Jun 16, 2015
1 parent 41121e9 commit ed9217f
Show file tree
Hide file tree
Showing 13 changed files with 798 additions and 36 deletions.
44 changes: 39 additions & 5 deletions binlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,25 @@ static int fetch_comb_binlog_dc_option (struct tgl_state *TLS, struct tl_ds_binl
DS_LVAL (DS_U->port)
);

tglmp_alloc_dc (TLS,
tglmp_alloc_dc (TLS,
0,
DS_LVAL (DS_U->dc),
DS_STR_DUP (DS_U->ip),
DS_LVAL (DS_U->port)
);
return 0;
}

static int fetch_comb_binlog_dc_option_new (struct tgl_state *TLS, struct tl_ds_binlog_update *DS_U) {
vlogprintf (E_NOTICE, "DC%d '%.*s' update: %.*s:%d\n",
DS_LVAL (DS_U->dc),
DS_RSTR (DS_U->name),
DS_RSTR (DS_U->ip),
DS_LVAL (DS_U->port)
);

tglmp_alloc_dc (TLS,
DS_LVAL (DS_U->flags),
DS_LVAL (DS_U->dc),
DS_STR_DUP (DS_U->ip),
DS_LVAL (DS_U->port)
Expand Down Expand Up @@ -915,6 +933,7 @@ static void replay_log_event (struct tgl_state *TLS) {
switch (op) {
FETCH_COMBINATOR_FUNCTION (binlog_start)
FETCH_COMBINATOR_FUNCTION (binlog_dc_option)
FETCH_COMBINATOR_FUNCTION (binlog_dc_option_new)
FETCH_COMBINATOR_FUNCTION (binlog_auth_key)
FETCH_COMBINATOR_FUNCTION (binlog_default_dc)
FETCH_COMBINATOR_FUNCTION (binlog_dc_signed)
Expand Down Expand Up @@ -1105,20 +1124,35 @@ static void add_log_event (struct tgl_state *TLS, const int *data, int len) {
in_end = end;
}

void bl_do_dc_option (struct tgl_state *TLS, int id, const char *name, int l1, const char *ip, int l2, int port) {
void bl_do_dc_option_new (struct tgl_state *TLS, int flags, int id, const char *name, int l1, const char *ip, int l2, int port) {
struct tgl_dc *DC = TLS->DC_list[id];
if (DC && !strncmp (ip, DC->ip, l2)) { return; }


if (DC) {
struct tgl_dc_option *O = DC->options[flags & 3];
while (O) {
if (!strncmp (O->ip, ip, l2)) {
return;
}
O = O->next;
}
}

clear_packet ();
out_int (CODE_binlog_dc_option);
out_int (CODE_binlog_dc_option_new);
out_int (flags);
out_int (id);

out_cstring (name, l1);
out_cstring (ip, l2);
out_int (port);

add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer));
}

void bl_do_dc_option (struct tgl_state *TLS, int id, const char *name, int l1, const char *ip, int l2, int port) {
bl_do_dc_option_new (TLS, 0, id, name, l1, ip, l2, port);
}

void bl_do_set_working_dc (struct tgl_state *TLS, int num) {
int *ev = alloc_log_event (8);
ev[0] = CODE_binlog_default_dc;
Expand Down
2 changes: 2 additions & 0 deletions binlog.tl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ binlog.encrKey key:64*[int] = binlog.EncrKey;
binlog.start = binlog.Update;

binlog.dcOption dc:int name:string ip:string port:int = binlog.Update;
binlog.dcOptionNew flags:int dc:int name:string ip:string port:int = binlog.Update;

binlog.authKey dc:int key:%binlog.EncrKey = binlog.Update;
binlog.defaultDc dc:int = binlog.Update;
binlog.dcSigned dc:int = binlog.Update;
Expand Down
62 changes: 41 additions & 21 deletions mtproto-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,6 @@ long long tglmp_encrypt_send_message (struct tgl_state *TLS, struct connection *

int l = aes_encrypt_message (TLS, DC->temp_auth_key, &enc_msg);
assert (l > 0);
vlogprintf (E_DEBUG, "Sending message to DC%d: %s:%d with key_id=%lld\n", DC->id, DC->ip, DC->port, enc_msg.auth_key_id);
rpc_send_message (TLS, c, &enc_msg, l + UNENCSZ);

return S->last_msg_id;
Expand Down Expand Up @@ -980,7 +979,7 @@ static int rpc_execute_answer (struct tgl_state *TLS, struct connection *c, long

static struct mtproto_methods mtproto_methods;
void tgls_free_session (struct tgl_state *TLS, struct tgl_session *S);

/*
static char *get_ipv6 (struct tgl_state *TLS, int num) {
static char res[1<< 10];
if (TLS->test_mode) {
Expand Down Expand Up @@ -1020,12 +1019,22 @@ static char *get_ipv6 (struct tgl_state *TLS, int num) {
}
return res;
}
*/

static void create_session_connect (struct tgl_state *TLS, struct tgl_session *S) {
struct tgl_dc *DC = S->dc;

if (TLS->ipv6_enabled) {
S->c = TLS->net_methods->create_connection (TLS, DC->options[1]->ip, DC->options[1]->port, S, DC, &mtproto_methods);
} else {
S->c = TLS->net_methods->create_connection (TLS, DC->options[0]->ip, DC->options[0]->port, S, DC, &mtproto_methods);
}
}

static void fail_connection (struct tgl_state *TLS, struct connection *c) {
struct tgl_session *S = TLS->net_methods->get_session (c);
struct tgl_dc *DC = TLS->net_methods->get_dc (c);
TLS->net_methods->free (c);
S->c = TLS->net_methods->create_connection (TLS, TLS->ipv6_enabled ? get_ipv6 (TLS, DC->id) : DC->ip, DC->port, S, DC, &mtproto_methods);
create_session_connect (TLS, S);
}

static void fail_session (struct tgl_state *TLS, struct tgl_session *S) {
Expand Down Expand Up @@ -1139,7 +1148,6 @@ static int process_rpc_message (struct tgl_state *TLS, struct connection *c, str

static int rpc_execute (struct tgl_state *TLS, struct connection *c, int op, int len) {
struct tgl_dc *DC = TLS->net_methods->get_dc (c);
vlogprintf (E_DEBUG, "outbound rpc connection from dc #%d (%s:%d) : received rpc answer %d with %d content bytes\n", DC->id, DC->ip, DC->port, op, len);

if (len >= MAX_RESPONSE_SIZE/* - 12*/ || len < 0/*12*/) {
vlogprintf (E_WARNING, "answer too long (%d bytes), skipping\n", len);
Expand Down Expand Up @@ -1284,7 +1292,7 @@ void tgl_dc_authorize (struct tgl_state *TLS, struct tgl_dc *DC) {
if (!DC->sessions[0]) {
tglmp_dc_create_session (TLS, DC);
}
vlogprintf (E_DEBUG, "Starting authorization for DC #%d: %s:%d\n", DC->id, DC->ip, DC->port);
vlogprintf (E_DEBUG, "Starting authorization for DC #%d\n", DC->id);
//net_loop (0, auth_ok);
}

Expand Down Expand Up @@ -1326,26 +1334,41 @@ static void regen_temp_key_gw (struct tgl_state *TLS, void *arg) {
tglmp_regenerate_temp_auth_key (TLS, arg);
}

struct tgl_dc *tglmp_alloc_dc (struct tgl_state *TLS, int id, char *ip, int port) {
struct tgl_dc *tglmp_alloc_dc (struct tgl_state *TLS, int flags, int id, char *ip, int port) {
//assert (!TLS->DC_list[id]);

if (!TLS->DC_list[id]) {
struct tgl_dc *DC = talloc0 (sizeof (*DC));
DC->id = id;
DC->ip = ip;
DC->port = port;
TLS->DC_list[id] = DC;
if (id > TLS->max_dc_num) {
TLS->max_dc_num = id;
}
DC->ev = TLS->timer_methods->alloc (TLS, regen_temp_key_gw, DC);
TLS->timer_methods->insert (DC->ev, 0);
return DC;
} else {
struct tgl_dc *DC = TLS->DC_list[id];
tfree_str (DC->ip);
DC->ip = tstrdup (ip);
return DC;
}

struct tgl_dc *DC = TLS->DC_list[id];

struct tgl_dc_option *O = DC->options[flags & 3];

struct tgl_dc_option *O2 = O;
while (O2) {
if (!strcmp (O2->ip, ip)) {
tfree_str (ip);
return DC;
}
O2 = O2->next;
}

struct tgl_dc_option *T = talloc (sizeof (*T));
T->ip = ip;
T->port = port;
T->next = O;
DC->options[flags & 3] = T;


return DC;
}

static struct mtproto_methods mtproto_methods = {
Expand All @@ -1359,11 +1382,8 @@ void tglmp_dc_create_session (struct tgl_state *TLS, struct tgl_dc *DC) {
assert (RAND_pseudo_bytes ((unsigned char *) &S->session_id, 8) >= 0);
S->dc = DC;
//S->c = TLS->net_methods->create_connection (TLS, DC->ip, DC->port, S, DC, &mtproto_methods);
S->c = TLS->net_methods->create_connection (TLS, TLS->ipv6_enabled ? get_ipv6 (TLS, DC->id) : DC->ip, DC->port, S, DC, &mtproto_methods);
if (!S->c) {
vlogprintf (E_DEBUG, "Can not create connection to DC. Is network down?\n");
exit (1);
}

create_session_connect (TLS, S);
S->ev = TLS->timer_methods->alloc (TLS, send_all_acks_gateway, S);
assert (!DC->sessions[0]);
DC->sessions[0] = S;
Expand Down Expand Up @@ -1428,7 +1448,7 @@ void tgls_free_session (struct tgl_state *TLS, struct tgl_session *S) {
}

void tgls_free_dc (struct tgl_state *TLS, struct tgl_dc *DC) {
if (DC->ip) { tfree_str (DC->ip); }
//if (DC->ip) { tfree_str (DC->ip); }

struct tgl_session *S = DC->sessions[0];
if (S) { tgls_free_session (TLS, S); }
Expand Down
2 changes: 1 addition & 1 deletion mtproto-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ long long tglmp_encrypt_send_message (struct tgl_state *TLS, struct connection *
void tglmp_dc_create_session (struct tgl_state *TLS, struct tgl_dc *DC);
//int tglmp_check_g (struct tgl_state *TLS, unsigned char p[256], BIGNUM *g);
//int tglmp_check_DH_params (struct tgl_state *TLS, BIGNUM *p, int g);
struct tgl_dc *tglmp_alloc_dc (struct tgl_state *TLS, int id, char *ip, int port);
struct tgl_dc *tglmp_alloc_dc (struct tgl_state *TLS, int flags, int id, char *ip, int port);
void tglmp_regenerate_temp_auth_key (struct tgl_state *TLS, struct tgl_dc *D);

void tgln_insert_msg_id (struct tgl_state *TLS, struct tgl_session *S, long long id);
Expand Down
37 changes: 34 additions & 3 deletions queries.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ struct query *tglq_send_query_ex (struct tgl_state *TLS, struct tgl_dc *DC, int
if (!DC->sessions[0]) {
tglmp_dc_create_session (TLS, DC);
}
vlogprintf (E_DEBUG, "Sending query of size %d to DC (%s:%d)\n", 4 * ints, DC->ip, DC->port);
vlogprintf (E_DEBUG, "Sending query of size %d to DC %d\n", 4 * ints, DC->id);
struct query *q = talloc0 (sizeof (*q));
q->data_len = ints;
q->data = talloc (4 * ints);
Expand Down Expand Up @@ -533,7 +533,7 @@ static int q_list_on_error (struct tgl_state *TLS, struct query *q, int error_co
/* {{{ Get config */

static void fetch_dc_option (struct tgl_state *TLS, struct tl_ds_dc_option *DS_DO) {
bl_do_dc_option (TLS, DS_LVAL (DS_DO->id), DS_STR (DS_DO->hostname), DS_STR (DS_DO->ip_address), DS_LVAL (DS_DO->port));
bl_do_dc_option_new (TLS, DS_LVAL (DS_DO->flags), DS_LVAL (DS_DO->id), DS_STR (DS_DO->hostname), DS_STR (DS_DO->ip_address), DS_LVAL (DS_DO->port));
}

static int help_get_config_on_answer (struct tgl_state *TLS, struct query *q, void *DS) {
Expand Down Expand Up @@ -688,6 +688,17 @@ int tgl_do_send_code_result_auth (struct tgl_state *TLS, const char *phone, int
tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra);
return 0;
}

int tgl_do_send_bot_auth (struct tgl_state *TLS, const char *code, int code_len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) {
clear_packet ();
out_int (CODE_auth_import_bot_authorization);
out_int (0);
out_int (TLS->app_id);
out_string (TLS->app_hash);
out_cstring (code, code_len);
tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra);
return 0;
}
/* }}} */

/* {{{ Get contacts */
Expand Down Expand Up @@ -987,6 +998,7 @@ static struct query_methods mark_read_methods = {
};

void tgl_do_messages_mark_read (struct tgl_state *TLS, tgl_peer_id_t id, int max_id, int offset, void (*callback)(struct tgl_state *TLS, void *callback_extra, int), void *callback_extra) {
if (TLS->is_bot) { return; }
clear_packet ();
out_int (CODE_messages_read_history);
out_peer_id (TLS, id);
Expand Down Expand Up @@ -4204,9 +4216,28 @@ void tgl_sign_in_phone (struct tgl_state *TLS, const char *phone, void *arg) {
tgl_do_send_code (TLS, E->phone, E->phone_len, tgl_sign_in_phone_cb, E);
}

void tgl_bot_hash (struct tgl_state *TLS, const char *code, void *arg);

void tgl_sign_in_bot_cb (struct tgl_state *TLS, void *_T, int success, struct tgl_user *U) {
if (!success) {
vlogprintf (E_ERROR, "incorrect bot hash\n");
TLS->callback.get_string (TLS, "bot hash:", 0, tgl_bot_hash, NULL);
return;
}
tgl_export_all_auth (TLS);
}

void tgl_bot_hash (struct tgl_state *TLS, const char *code, void *arg) {
tgl_do_send_bot_auth (TLS, code, strlen (code), tgl_sign_in_bot_cb, NULL);
}

void tgl_sign_in (struct tgl_state *TLS) {
if (!tgl_signed_dc (TLS, TLS->DC_working)) {
TLS->callback.get_string (TLS, "phone number:", 0, tgl_sign_in_phone, NULL);
if (TLS->is_bot) {
TLS->callback.get_string (TLS, "bot hash:", 0, tgl_bot_hash, NULL);
} else {
TLS->callback.get_string (TLS, "phone number:", 0, tgl_sign_in_phone, NULL);
}
} else {
tgl_export_all_auth (TLS);
}
Expand Down
2 changes: 1 addition & 1 deletion scheme.tl
Loading

0 comments on commit ed9217f

Please sign in to comment.