Commit cedc1d17 authored by vvaltman's avatar vvaltman

many fixes

parent e2e7d0c9
......@@ -7,7 +7,7 @@ DEFS=@DEFS@
COMPILE_FLAGS=${CFLAGS} ${CPPFLAGS} ${DEFS} -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter
EXTRA_LIBS=@LIBS@ @EXTRA_LIBS@
LOCAL_LDFLAGS=-rdynamic -ggdb ${EXTRA_LIBS}
LOCAL_LDFLAGS=-rdynamic -ggdb -levent ${EXTRA_LIBS}
LINK_FLAGS=${LDFLAGS} ${LOCAL_LDFLAGS}
DEP=${srcdir}/dep
......
......@@ -46,6 +46,8 @@
#include "tgl.h"
#include "auto.h"
#include "structures.h"
#include <openssl/sha.h>
#define BINLOG_BUFFER_SIZE (1 << 20)
......@@ -1327,7 +1329,7 @@ void bl_do_set_our_id (int id) {
ev[0] = CODE_binlog_our_id;
ev[1] = id;
add_log_event (ev, 8);
write_auth_file ();
//write_auth_file ();
}
void bl_do_user_add (int id, const char *f, int fl, const char *l, int ll, long long access_token, const char *p, int pl, int contact) {
......
......@@ -20,6 +20,7 @@
#define __BINLOG_H__
//#include "structures.h"
#include "tgl.h"
void bl_do_set_auth_key_id (int num, unsigned char *buf);
......
This diff is collapsed.
......@@ -62,7 +62,7 @@ extern int unknown_user_list_pos;
extern int unknown_user_list[];
int register_mode;
extern int safe_quit;
extern int queries_num;
int queries_num;
void got_it (char *line, int len);
......@@ -77,6 +77,9 @@ static void stdin_read_callback (evutil_socket_t fd, short what, void *arg) {
}
}
void net_loop (int flags, int (*is_end)(void)) {
if (verbosity) {
logprintf ("Starting netloop\n");
}
struct event *ev = 0;
if (flags & 3) {
ev = event_new (tgl_state.ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback, (void *)(long)flags);
......@@ -106,6 +109,10 @@ void net_loop (int flags, int (*is_end)(void)) {
if (ev) {
event_free (ev);
}
if (verbosity) {
logprintf ("End of netloop\n");
}
}
char **_s;
......@@ -201,6 +208,16 @@ void export_auth_callback (void *DC, int success) {
}
}
int d_got_ok;
void get_difference_callback (void *extra, int success) {
assert (success);
d_got_ok = 1;
}
int dgot (void) {
return d_got_ok;
}
int zero[512];
......@@ -208,8 +225,11 @@ int readline_active;
int new_dc_num;
int wait_dialog_list;
extern struct tgl_update_callback upd_cb;
int loop (void) {
//on_start ();
tgl_set_callback (&upd_cb);
tgl_init ();
double t = tglt_get_double_time ();
......@@ -328,7 +348,7 @@ int loop (void) {
}
net_loop (0, signed_in);
bl_do_dc_signed (tgl_state.DC_working);
//bl_do_dc_signed (tgl_state.DC_working);
}
for (i = 0; i <= tgl_state.max_dc_num; i++) if (tgl_state.DC_list[i] && !tgl_signed_dc (tgl_state.DC_list[i])) {
......@@ -346,7 +366,7 @@ int loop (void) {
set_interface_callbacks ();
tgl_do_get_difference (0, 0);
tgl_do_get_difference (0, get_difference_callback, 0);
net_loop (0, dgot);
#ifdef USE_LUA
lua_diff_end ();
......@@ -354,11 +374,11 @@ int loop (void) {
tglm_send_all_unsent ();
tgl_do_get_dialog_list ();
/*tgl_do_get_dialog_list (get_dialogs_callback, 0);
if (wait_dialog_list) {
dialog_list_got = 0;
net_loop (0, dlgot);
}
}*/
return main_loop ();
}
......
......@@ -13,14 +13,11 @@
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <event2/event.h>
lua_State *luaState;
#include "structures.h"
#include "interface.h"
#include "auto/constants.h"
#include "tools.h"
#include "queries.h"
#include "net.h"
#include "tgl.h"
extern int verbosity;
......@@ -183,7 +180,7 @@ void push_message (struct tgl_message *M) {
lua_newtable (luaState);
static char s[30];
tsnprintf (s, 30, "%lld", M->id);
snprintf (s, 30, "%lld", M->id);
lua_add_string_field ("id", s);
lua_add_num_field ("flags", M->flags);
......@@ -355,19 +352,19 @@ void lua_do_all (void) {
int f = (long)lua_ptr[p ++];
switch (f) {
case 0:
tgl_do_send_message (((tgl_peer_t *)lua_ptr[p])->id, lua_ptr[p + 1], strlen (lua_ptr[p + 1]));
tfree_str (lua_ptr[p + 1]);
tgl_do_send_message (((tgl_peer_t *)lua_ptr[p])->id, lua_ptr[p + 1], strlen (lua_ptr[p + 1]), 0, 0);
free (lua_ptr[p + 1]);
p += 2;
break;
case 1:
tgl_do_forward_message (((tgl_peer_t *)lua_ptr[p])->id, (long)lua_ptr[p + 1]);
tgl_do_forward_message (((tgl_peer_t *)lua_ptr[p])->id, (long)lua_ptr[p + 1], 0, 0);
p += 2;
break;
case 2:
#ifdef DEBUG
texists (lua_ptr[p], sizeof (tgl_peer_t));
#endif
tgl_do_mark_read (((tgl_peer_t *)lua_ptr[p])->id);
tgl_do_mark_read (((tgl_peer_t *)lua_ptr[p])->id, 0, 0);
p += 1;
break;
default:
......@@ -404,7 +401,7 @@ static int send_msg_from_lua (lua_State *L) {
lua_ptr[pos ++] = (void *)2l;
lua_ptr[pos ++] = (void *)0l;
lua_ptr[pos ++] = P;
lua_ptr[pos ++] = tstrdup (msg);
lua_ptr[pos ++] = strdup (msg);
logprintf ("msg = %s\n", msg);
lua_pushboolean (L, 1);
......@@ -469,8 +466,8 @@ static int mark_read_from_lua (lua_State *L) {
return 1;
}
int lua_postpone_alarm (void *self) {
int *t = self;
static void lua_postpone_alarm (evutil_socket_t fd, short what, void *arg) {
int *t = arg;
lua_settop (luaState, 0);
//lua_checkstack (luaState, 20);
......@@ -488,9 +485,7 @@ int lua_postpone_alarm (void *self) {
if (r) {
logprintf ("lua: %s\n", lua_tostring (luaState, -1));
}
tfree (*(void **)(t + 2), sizeof (struct event_timer));
tfree (t, 16);
return 0;
}
static int postpone_from_lua (lua_State *L) {
......@@ -510,16 +505,14 @@ static int postpone_from_lua (lua_State *L) {
int a1 = luaL_ref (L, LUA_REGISTRYINDEX);
int a2 = luaL_ref (L, LUA_REGISTRYINDEX);
struct event_timer *ev = talloc (sizeof (*ev));
int *t = talloc (16);
int *t = malloc (16);
struct event *ev = evtimer_new (tgl_state.ev_base, lua_postpone_alarm, t);
t[0] = a1;
t[1] = a2;
*(void **)(t + 2) = ev;
ev->timeout = get_double_time () + timeout;
ev->alarm = (void *)lua_postpone_alarm;
ev->self = t;
insert_event_timer (ev);
struct timeval ts= { timeout, 0};
event_add (ev, &ts);
lua_pushboolean (L, 1);
return 1;
......
......@@ -75,6 +75,7 @@
"# This is an empty config file\n" \
"# Feel free to put something here\n"
int verbosity;
char *default_username;
char *auth_token;
int msg_num_mode;
......@@ -410,6 +411,7 @@ void args_parse (int argc, char **argv) {
break;
case 'v':
tgl_incr_verbosity ();
verbosity ++;
break;
case 'N':
msg_num_mode ++;
......
......@@ -76,7 +76,7 @@
//int verbosity;
static int auth_success;
static enum dc_state c_state;
//static enum dc_state c_state;
static char nonce[256];
static char new_nonce[256];
static char server_nonce[256];
......@@ -197,6 +197,7 @@ static int rpc_send_packet (struct connection *c) {
int total_len = len + 20;
assert (total_len > 0 && !(total_len & 0xfc000003));
total_len >>= 2;
vlogprintf (E_DEBUG, "writing packet: total_len = %d, len = %d\n", total_len, len);
if (total_len < 0x7f) {
assert (tgl_state.net_methods->write_out (c, &total_len, 1) == 1);
} else {
......@@ -257,7 +258,7 @@ static int process_respq_answer (struct connection *c, char *packet, int len) {
unsigned long long what;
unsigned p1, p2;
int i;
vlogprintf (E_DEBUG, "process_respq_answer(), len=%d\n", len);
vlogprintf (E_DEBUG, "process_respq_answer(), len=%d, op=0x%08x\n", len, *(int *)(packet + 20));
assert (len >= 76);
assert (!*(long long *) packet);
assert (*(int *) (packet + 16) == len - 20);
......@@ -414,7 +415,7 @@ static int process_respq_answer (struct connection *c, char *packet, int len) {
out_long (pk_fingerprint);
out_cstring ((char *) encrypt_buffer, l);
c_state = st_reqdh_sent;
tgl_state.net_methods->get_dc (c)->state = st_reqdh_sent;
return rpc_send_packet (c);
}
......@@ -608,7 +609,7 @@ static int process_dh_answer (struct connection *c, char *packet, int len) {
out_ints ((int *) server_nonce, 4);
out_cstring ((char *) encrypt_buffer, l);
c_state = st_client_dh_sent;
tgl_state.net_methods->get_dc (c)->state = st_client_dh_sent;
return rpc_send_packet (c);
}
......@@ -642,7 +643,7 @@ static int process_auth_complete (struct connection *c UU, char *packet, int len
//c->status = conn_error;
//sleep (1);
c_state = st_authorized;
tgl_state.net_methods->get_dc (c)->state = st_authorized;
//return 1;
vlogprintf (E_DEBUG, "Auth success\n");
auth_success ++;
......@@ -1038,7 +1039,7 @@ static int rpc_execute (struct connection *c, int op, int len) {
#endif
return 0;
default:
vlogprintf (E_ERROR, "fatal: cannot receive answer in state %d\n", c_state);
vlogprintf (E_ERROR, "fatal: cannot receive answer in state %d\n", D->state);
exit (2);
}
......@@ -1053,9 +1054,9 @@ static int tc_close (struct connection *c, int who) {
static int tc_becomes_ready (struct connection *c) {
vlogprintf (E_DEBUG, "outbound rpc connection from dc #%d becomed ready\n", tgl_state.net_methods->get_dc(c)->id);
char byte = 0xef;
assert (tgl_state.net_methods->write_out (c, &byte, 1) == 1);
tgl_state.net_methods->flush_out (c);
//char byte = 0xef;
//assert (tgl_state.net_methods->write_out (c, &byte, 1) == 1);
//tgl_state.net_methods->flush_out (c);
#if !defined(__MACH__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined (__CYGWIN__)
// setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
......@@ -1067,8 +1068,10 @@ static int tc_becomes_ready (struct connection *c) {
case st_init:
send_req_pq_packet (c);
break;
case st_authorized:
break;
default:
vlogprintf (E_DEBUG, "c_state = %d\n", c_state);
vlogprintf (E_DEBUG, "c_state = %d\n", D->state);
assert (0);
}
return 0;
......
......@@ -48,6 +48,7 @@
//#include "mtproto-client.h"
//#include "mtproto-common.h"
#include "tree.h"
#include "tools.h"
#ifndef POLLRDHUP
#define POLLRDHUP 0
......@@ -66,11 +67,11 @@ static void ping_alarm (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg;
vlogprintf (E_DEBUG + 2,"ping alarm\n");
assert (c->state == conn_ready || c->state == conn_connecting);
if (get_double_time () - c->last_receive_time > 20 * PING_TIMEOUT) {
if (tglt_get_double_time () - c->last_receive_time > 20 * PING_TIMEOUT) {
vlogprintf (E_WARNING, "fail connection: reason: ping timeout\n");
c->state = conn_failed;
fail_connection (c);
} else if (get_double_time () - c->last_receive_time > 5 * PING_TIMEOUT && c->state == conn_ready) {
} else if (tglt_get_double_time () - c->last_receive_time > 5 * PING_TIMEOUT && c->state == conn_ready) {
tgl_do_send_ping (c);
start_ping_timer (c);
} else {
......@@ -117,6 +118,7 @@ static void delete_connection_buffer (struct connection_buffer *b) {
}
int tgln_write_out (struct connection *c, const void *_data, int len) {
vlogprintf (E_DEBUG, "write_out: %d bytes\n", len);
const unsigned char *data = _data;
if (!len) { return 0; }
assert (len > 0);
......@@ -234,10 +236,15 @@ static void try_write (struct connection *c);
static void conn_try_read (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg;
vlogprintf (2, "Try read. Fd = %d\n", c->fd);
try_read (c);
}
static void conn_try_write (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg;
if (c->state == conn_connecting) {
c->state = conn_ready;
c->methods->ready (c);
}
try_write (c);
if (c->out_bytes) {
event_add (c->write_ev, 0);
......@@ -269,6 +276,7 @@ struct connection *tgln_create_connection (const char *host, int port, struct se
fcntl (fd, F_SETFL, O_NONBLOCK);
if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) == -1) {
//vlogprintf (E_ERROR, "Can not connect to %s:%d %m\n", host, port);
if (errno != EINPROGRESS) {
vlogprintf (E_ERROR, "Can not connect to %s:%d %m\n", host, port);
close (fd);
......@@ -279,7 +287,7 @@ struct connection *tgln_create_connection (const char *host, int port, struct se
c->fd = fd;
c->state = conn_connecting;
c->last_receive_time = get_double_time ();
c->last_receive_time = tglt_get_double_time ();
c->ip = tstrdup (host);
c->flags = 0;
c->port = port;
......@@ -288,9 +296,11 @@ struct connection *tgln_create_connection (const char *host, int port, struct se
c->ping_ev = evtimer_new (tgl_state.ev_base, ping_alarm, c);
c->fail_ev = evtimer_new (tgl_state.ev_base, fail_alarm, c);
c->read_ev = event_new (tgl_state.ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c);
c->write_ev = event_new (tgl_state.ev_base, c->fd, EV_WRITE, conn_try_write, c);
event_add (c->read_ev, 0);
struct timeval tv = {5, 0};
c->read_ev = event_new (tgl_state.ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c);
event_add (c->read_ev, &tv);
start_ping_timer (c);
......@@ -345,7 +355,7 @@ static void restart_connection (struct connection *c) {
c->fd = fd;
c->state = conn_connecting;
c->last_receive_time = get_double_time ();
c->last_receive_time = tglt_get_double_time ();
start_ping_timer (c);
Connections[fd] = c;
......@@ -499,7 +509,7 @@ static void try_read (struct connection *c) {
fflush (log_net_f);
}*/
if (r > 0) {
c->last_receive_time = get_double_time ();
c->last_receive_time = tglt_get_double_time ();
stop_ping_timer (c);
start_ping_timer (c);
}
......@@ -565,7 +575,7 @@ void tgl_connections_poll_result (struct pollfd *fds, int max) {
if (c->state == conn_connecting) {
vlogprintf (E_DEBUG, "connection ready\n");
c->state = conn_ready;
c->last_receive_time = get_double_time ();
c->last_receive_time = tglt_get_double_time ();
}
if (c->out_bytes) {
try_write (c);
......
This diff is collapsed.
......@@ -279,7 +279,7 @@ void tglf_fetch_encrypted_chat (struct tgl_secret_chat *U) {
return;
}
bl_do_encr_chat_delete (U);
write_secret_chat_file ();
//write_secret_chat_file ();
return;
}
......@@ -311,7 +311,7 @@ void tglf_fetch_encrypted_chat (struct tgl_secret_chat *U) {
}
bl_do_encr_chat_requested (U, access_hash, date, admin_id, user_id, (void *)g_key, (void *)nonce);
write_secret_chat_file ();
//write_secret_chat_file ();
} else {
bl_do_encr_chat_set_access_hash (U, fetch_long ());
bl_do_encr_chat_set_date (U, fetch_int ());
......@@ -325,7 +325,7 @@ void tglf_fetch_encrypted_chat (struct tgl_secret_chat *U) {
}
if (x == CODE_encrypted_chat_waiting) {
bl_do_encr_chat_set_state (U, sc_waiting);
write_secret_chat_file ();
//write_secret_chat_file ();
return; // We needed only access hash from here
}
......@@ -339,7 +339,7 @@ void tglf_fetch_encrypted_chat (struct tgl_secret_chat *U) {
return; // Duplicate?
}
bl_do_encr_chat_accepted (U, (void *)g_key, (void *)nonce, fetch_long ());
write_secret_chat_file ();
//write_secret_chat_file ();
}
}
......
......@@ -8,6 +8,9 @@
#include "net.h"
#include <event2/event.h>
#include <assert.h>
struct tgl_state tgl_state;
......
......@@ -297,7 +297,7 @@ void my_clock_gettime (int clock_id, struct timespec *T) {
#endif
}
double get_double_time (void) {
double tglt_get_double_time (void) {
struct timespec tv;
my_clock_gettime (CLOCK_REALTIME, &tv);
return tv.tv_sec + 1e-9 * tv.tv_nsec;
......
......@@ -3,6 +3,7 @@
#include "mtproto-common.h"
#include "binlog.h"
#include "auto.h"
#include "structures.h"
#include <assert.h>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment