Commit 807da5b3 authored by vvaltman's avatar vvaltman

Merge github.com:vysheng/tg

Conflicts:
	tgl.h
parents ff2cb23a 56fe7bed
......@@ -21,7 +21,7 @@ EXE_LIST=${EXE}/generate ${EXE}/tlc ${EXE}/telegram-cli
LIB_LIST=${LIB}/libtgl.a
TG_OBJECTS=${OBJ}/main.o ${OBJ}/loop.o ${OBJ}/interface.o ${OBJ}/lua-tg.o
TGL_OBJECTS=${OBJ}/net.o ${OBJ}/mtproto-common.o ${OBJ}/mtproto-client.o ${OBJ}/queries.o ${OBJ}/structures.o ${OBJ}/binlog.o ${OBJ}/auto/auto.o ${OBJ}/tgl.o ${OBJ}/updates.o
TGL_OBJECTS=${OBJ}/net.o ${OBJ}/mtproto-common.o ${OBJ}/mtproto-client.o ${OBJ}/queries.o ${OBJ}/structures.o ${OBJ}/binlog.o ${OBJ}/auto/auto.o ${OBJ}/tgl.o ${OBJ}/updates.o ${OBJ}/tgl-timers.o
TLC_OBJECTS=${OBJ}/tlc.o ${OBJ}/tl-parser.o ${OBJ}/crc32.o
TLD_OBJECTS=${OBJ}/dump-tl-file.o
GENERATE_OBJECTS=${OBJ}/generate.o
......
......@@ -19,8 +19,10 @@
*/
#include "mtproto-common.h"
#include "config.h"
#include <string.h>
#ifndef DISABLE_EXTF
static int cur_token_len;
static char *cur_token;
static int cur_token_real_len;
......@@ -301,7 +303,7 @@ static void free_vars_to_be_freed (void) {
fvars_pos = 0;
}
int tglf_extf_autocomplete (const char *text, int text_len, int index, char **R, char *data, int data_len) {
int tglf_extf_autocomplete (struct tgl_state *TLS, const char *text, int text_len, int index, char **R, char *data, int data_len) {
if (index == -1) {
buffer_pos = data;
buffer_end = data + data_len;
......@@ -328,7 +330,7 @@ int tglf_extf_autocomplete (const char *text, int text_len, int index, char **R,
}
}
struct paramed_type *tglf_extf_store (const char *data, int data_len) {
struct paramed_type *tglf_extf_store (struct tgl_state *TLS, const char *data, int data_len) {
buffer_pos = (char *)data;
buffer_end = (char *)(data + data_len);
local_next_token ();
......@@ -430,8 +432,9 @@ static void print_offset (void) {
}
}
char *tglf_extf_fetch (struct paramed_type *T) {
char *tglf_extf_fetch (struct tgl_state *TLS, struct paramed_type *T) {
out_buf_pos = 0;
if (fetch_type_any (T) < 0) { return 0; }
return out_buf;
}
#endif
This diff is collapsed.
This diff is collapsed.
/* config.h.in. Generated from configure.ac by autoheader. */
/* disable extf queries */
#undef DISABLE_EXTF
/* Use libevent v1 */
#undef EVENT_V1
......
......@@ -692,6 +692,7 @@ enable_option_checking
with_openssl
with_zlib
enable_libconfig
enable_extf
enable_liblua
with_progname
'
......@@ -1317,6 +1318,7 @@ Optional Features:
--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
--enable-FEATURE[=ARG] include FEATURE [ARG=yes]
--enable-libconfig/--disable-libconfig
--enable-extf/--disable-extf
--enable-liblua/--disable-liblua
Optional Packages:
......@@ -4502,6 +4504,18 @@ fi
fi
# Check whether --enable-extf was given.
if test "${enable_extf+set}" = set; then :
enableval=$enable_extf;
if test "x$enableval" = "xno" ; then
$as_echo "#define DISABLE_EXTF 1" >>confdefs.h
fi
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for liblua" >&5
$as_echo_n "checking for liblua... " >&6; }
# Check whether --enable-liblua was given.
......
......@@ -52,6 +52,14 @@ AC_ARG_ENABLE(libconfig,[--enable-libconfig/--disable-libconfig],
AC_CHECK_LIB([config],[config_init],[],AC_MSG_ERROR([No libconfig found. Try --disable-libconfig]))
])
AC_ARG_ENABLE(extf,[--enable-extf/--disable-extf],
[
if test "x$enableval" = "xno" ; then
AC_DEFINE([DISABLE_EXTF],[1],[disable extf queries])
fi
],[
])
AC_MSG_CHECKING([for liblua])
AC_ARG_ENABLE(liblua,[--enable-liblua/--disable-liblua],
[
......
......@@ -82,6 +82,11 @@ static char buf[1 << 20];
int buf_size;
int *buf_ptr = (int *)buf;
int *buf_end;
#ifndef DISABLE_EXTF
int skip_only = 0;
#else
int skip_only = 1;
#endif
int verbosity;
......@@ -872,6 +877,7 @@ void gen_constructor_store (struct tl_combinator *c) {
sprintf (s, "T");
int *vars = malloc0 (c->var_num * 4);;
assert (c->var_num <= 10);
gen_uni_skip (c->result, s, vars, 1, 0);
if (c->name == NAME_INT) {
......@@ -937,6 +943,7 @@ void gen_constructor_autocomplete (struct tl_combinator *c) {
sprintf (s, "T");
int *vars = malloc0 (c->var_num * 4);;
assert (c->var_num <= 10);
gen_uni_skip (c->result, s, vars, 1, 0);
if (c->name == NAME_INT) {
......@@ -1145,6 +1152,7 @@ void gen_function_store (struct tl_combinator *f) {
int i;
int *vars = malloc0 (f->var_num * 4);;
assert (f->var_num <= 10);
for (i = 0; i < f->args_num; i++) if (!(f->args[i]->flags & FLAG_OPT_VAR)) {
if (f->args[i]->flags & FLAG_EXCL) {
......@@ -1169,6 +1177,7 @@ void gen_function_autocomplete (struct tl_combinator *f) {
int i;
int *vars = malloc0 (f->var_num * 4);;
assert (f->var_num <= 10);
for (i = 0; i < f->args_num; i++) if (!(f->args[i]->flags & FLAG_OPT_VAR)) {
if (f->args[i]->flags & FLAG_EXCL) {
......@@ -1562,6 +1571,7 @@ int parse_tlo_file (void) {
assert (buf_ptr == buf_end);
int j;
for (i = 0; i < tn; i++) if (tps[i]->id[0] != '#' && strcmp (tps[i]->id, "Type")) {
tps[i]->name = 0;
......@@ -1579,22 +1589,28 @@ int parse_tlo_file (void) {
for (i = 0; i < tn; i++) {
for (j = 0; j < tps[i]->constructors_num; j ++) {
gen_constructor_skip (tps[i]->constructors[j]);
if (!skip_only) {
gen_constructor_store (tps[i]->constructors[j]);
gen_constructor_fetch (tps[i]->constructors[j]);
gen_constructor_autocomplete (tps[i]->constructors[j]);
}
}
}
for (i = 0; i < tn; i++) if (tps[i]->id[0] != '#' && strcmp (tps[i]->id, "Type")) {
gen_type_skip (tps[i]);
if (!skip_only) {
gen_type_store (tps[i]);
gen_type_fetch (tps[i]);
gen_type_autocomplete (tps[i]);
gen_type_do_autocomplete (tps[i]);
}
}
if (!skip_only) {
for (i = 0; i < fn; i++) {
gen_function_store (fns[i]);
gen_function_autocomplete (fns[i]);
}
}
printf ("int skip_type_any (struct paramed_type *T) {\n");
printf (" switch (T->type->name) {\n");
for (i = 0; i < tn; i++) if (tps[i]->id[0] != '#' && strcmp (tps[i]->id, "Type") && tps[i]->name) {
......@@ -1603,6 +1619,7 @@ int parse_tlo_file (void) {
}
printf (" default: return -1; }\n");
printf ("}\n");
if (!skip_only) {
printf ("int store_type_any (struct paramed_type *T) {\n");
printf (" switch (T->type->name) {\n");
for (i = 0; i < tn; i++) if (tps[i]->id[0] != '#' && strcmp (tps[i]->id, "Type") && tps[i]->name) {
......@@ -1673,6 +1690,7 @@ int parse_tlo_file (void) {
}
printf (" return 0;\n");
printf ("}\n");
}
} else {
for (i = 0; i < tn; i++) {
for (j = 0; j < tps[i]->constructors_num; j ++) {
......@@ -1726,7 +1744,6 @@ int parse_tlo_file (void) {
}
return 0;
}
......
This diff is collapsed.
......@@ -45,7 +45,7 @@ void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2)))
#define vlogprintf(v,...) \
do { \
if (tgl_state.verbosity >= (v)) {\
if (TLS->verbosity >= (v)) {\
logprintf (__VA_ARGS__);\
}\
} while (0);\
......
This diff is collapsed.
This diff is collapsed.
......@@ -114,6 +114,8 @@ int port;
int use_ids;
char *start_command;
struct tgl_state *TLS;
void set_default_username (const char *s) {
if (default_username) {
tfree_str (default_username);
......@@ -348,7 +350,7 @@ void parse_config (void) {
strcpy (buf + l, "test");
config_lookup_bool (&conf, buf, &test_mode);
if (test_mode) {
tgl_set_test_mode ();
tgl_set_test_mode (TLS);
}
strcpy (buf + l, "log_level");
......@@ -378,20 +380,20 @@ void parse_config (void) {
strcpy (buf + l, "pfs_enabled");
config_lookup_bool (&conf, buf, &pfs_enabled);
if (pfs_enabled) {
tgl_enable_pfs ();
tgl_enable_pfs (TLS);
}
if (binlog_enabled) {
parse_config_val (&conf, &binlog_file_name, "binlog", BINLOG_FILE, config_directory);
tgl_set_binlog_mode (1);
tgl_set_binlog_path (binlog_file_name);
tgl_set_binlog_mode (TLS, 1);
tgl_set_binlog_path (TLS, binlog_file_name);
} else {
tgl_set_binlog_mode (0);
tgl_set_binlog_mode (TLS, 0);
parse_config_val (&conf, &state_file_name, "state_file", STATE_FILE, config_directory);
parse_config_val (&conf, &secret_chat_file_name, "secret", SECRET_CHAT_FILE, config_directory);
//tgl_set_auth_file_path (auth_file_name);
}
tgl_set_download_directory (downloads_directory);
tgl_set_download_directory (TLS, downloads_directory);
if (!mkdir (config_directory, CONFIG_DIRECTORY_MODE)) {
if (!disable_output) {
......@@ -414,16 +416,16 @@ void parse_config (void) {
if (binlog_enabled) {
tasprintf (&binlog_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, BINLOG_FILE);
tgl_set_binlog_mode (1);
tgl_set_binlog_path (binlog_file_name);
tgl_set_binlog_mode (TLS, 1);
tgl_set_binlog_path (TLS, binlog_file_name);
} else {
tgl_set_binlog_mode (0);
tgl_set_binlog_mode (TLS, 0);
//tgl_set_auth_file_path (auth_file_name;
tasprintf (&auth_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, AUTH_KEY_FILE);
tasprintf (&state_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, STATE_FILE);
tasprintf (&secret_chat_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, SECRET_CHAT_FILE);
}
tgl_set_download_directory (downloads_directory);
tgl_set_download_directory (TLS, downloads_directory);
if (!mkdir (downloads_directory, CONFIG_DIRECTORY_MODE)) {
if (!disable_output) {
printf ("[%s] created\n", downloads_directory);
......@@ -568,6 +570,7 @@ int change_user_group () {
char *unix_socket;
void args_parse (int argc, char **argv) {
TLS = tgl_state_alloc ();
int opt = 0;
while ((opt = getopt (argc, argv, "u:hk:vNl:fEwWCRdL:DU:G:qP:S:e:I"
#ifdef HAVE_LIBCONFIG
......@@ -586,10 +589,10 @@ void args_parse (int argc, char **argv) {
break;
case 'k':
//rsa_public_key_name = tstrdup (optarg);
tgl_set_rsa_key (optarg);
tgl_set_rsa_key (TLS, optarg);
break;
case 'v':
tgl_incr_verbosity ();
tgl_incr_verbosity (TLS);
verbosity ++;
break;
case 'N':
......@@ -722,8 +725,8 @@ void sig_term_handler (int signum __attribute__ ((unused))) {
if (write (1, "SIGTERM/SIGINT received\n", 25) < 0) {
// Sad thing
}
if (tgl_state.ev_base) {
event_base_loopbreak(tgl_state.ev_base);
if (TLS && TLS->ev_base) {
event_base_loopbreak (TLS->ev_base);
}
sigterm_cnt ++;
}
......@@ -841,8 +844,8 @@ int main (int argc, char **argv) {
running_for_first_time ();
parse_config ();
tgl_set_rsa_key ("/etc/" PROG_NAME "/server.pub");
tgl_set_rsa_key ("tg-server.pub");
tgl_set_rsa_key (TLS, "/etc/" PROG_NAME "/server.pub");
tgl_set_rsa_key (TLS, "tg-server.pub");
get_terminal_attributes ();
......
This diff is collapsed.
......@@ -45,16 +45,16 @@ struct tgl_dc;
struct connection;
long long tglmp_encrypt_send_message (struct connection *c, int *msg, int msg_ints, int flags);
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 tgl_dc *tglmp_alloc_dc (int id, char *ip, int port);
void tglmp_regenerate_temp_auth_key (struct tgl_dc *D);
void tgln_insert_msg_id (struct tgl_session *S, long long id);
void tglmp_on_start (void);
void tgl_dc_authorize (struct tgl_dc *DC);
void tgls_free_dc (struct tgl_dc *DC);
void tgls_free_pubkey (void);
long long tglmp_encrypt_send_message (struct tgl_state *TLS, struct connection *c, int *msg, int msg_ints, int flags);
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);
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);
void tglmp_on_start (struct tgl_state *TLS);
void tgl_dc_authorize (struct tgl_state *TLS, struct tgl_dc *DC);
void tgls_free_dc (struct tgl_state *TLS, struct tgl_dc *DC);
void tgls_free_pubkey (struct tgl_state *TLS);
#endif
......@@ -57,7 +57,7 @@ static long long rsa_encrypted_chunks, rsa_decrypted_chunks;
//int verbosity;
static int get_random_bytes (unsigned char *buf, int n) {
static int get_random_bytes (struct tgl_state *TLS, unsigned char *buf, int n) {
int r = 0, h = open ("/dev/random", O_RDONLY | O_NONBLOCK);
if (h >= 0) {
r = read (h, buf, n);
......@@ -107,7 +107,7 @@ static __inline__ unsigned long long rdtsc (void) {
}
#endif
void tgl_prng_seed (const char *password_filename, int password_length) {
void tgl_prng_seed (struct tgl_state *TLS, const char *password_filename, int password_length) {
struct timespec T;
tgl_my_clock_gettime (CLOCK_REALTIME, &T);
RAND_add (&T, sizeof (T), 4.0);
......@@ -120,7 +120,7 @@ void tgl_prng_seed (const char *password_filename, int password_length) {
p = getppid ();
RAND_add (&p, sizeof (p), 0.0);
unsigned char rb[32];
int s = get_random_bytes (rb, 32);
int s = get_random_bytes (TLS, rb, 32);
if (s > 0) {
RAND_add (rb, s, s);
}
......@@ -142,8 +142,8 @@ void tgl_prng_seed (const char *password_filename, int password_length) {
tfree_secure (a, password_length);
}
}
tgl_state.BN_ctx = BN_CTX_new ();
ensure_ptr (tgl_state.BN_ctx);
TLS->BN_ctx = BN_CTX_new ();
ensure_ptr (TLS->BN_ctx);
}
int tgl_serialize_bignum (BIGNUM *b, char *buffer, int maxlen) {
......@@ -250,7 +250,7 @@ int tgl_fetch_bignum (BIGNUM *x) {
return l;
}
int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E) {
int tgl_pad_rsa_encrypt (struct tgl_state *TLS, char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E) {
int pad = (255000 - from_len - 32) % 255 + 32;
int chunks = (from_len + pad) / 255;
int bits = BN_num_bits (N);
......@@ -265,7 +265,7 @@ int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N
rsa_encrypted_chunks += chunks;
for (i = 0; i < chunks; i++) {
BN_bin2bn ((unsigned char *) from, 255, &x);
assert (BN_mod_exp (&y, &x, E, N, tgl_state.BN_ctx) == 1);
assert (BN_mod_exp (&y, &x, E, N, TLS->BN_ctx) == 1);
unsigned l = 256 - BN_num_bytes (&y);
assert (l <= 256);
memset (to, 0, l);
......@@ -277,7 +277,7 @@ int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N
return chunks * 256;
}
int tgl_pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D) {
int tgl_pad_rsa_decrypt (struct tgl_state *TLS, char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D) {
if (from_len < 0 || from_len > 0x1000 || (from_len & 0xff)) {
return -1;
}
......@@ -292,7 +292,7 @@ int tgl_pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N
for (i = 0; i < chunks; i++) {
++rsa_decrypted_chunks;
BN_bin2bn ((unsigned char *) from, 256, &x);
assert (BN_mod_exp (&y, &x, D, N, tgl_state.BN_ctx) == 1);
assert (BN_mod_exp (&y, &x, D, N, TLS->BN_ctx) == 1);
int l = BN_num_bytes (&y);
if (l > 255) {
BN_free (&x);
......
......@@ -117,7 +117,7 @@ struct encrypted_message {
//BN_CTX *BN_ctx;
void tgl_prng_seed (const char *password_filename, int password_length);
void tgl_prng_seed (struct tgl_state *TLS, const char *password_filename, int password_length);
int tgl_serialize_bignum (BIGNUM *b, char *buffer, int maxlen);
long long tgl_do_compute_rsa_key_fingerprint (RSA *key);
......@@ -356,8 +356,8 @@ static inline int in_remaining (void) {
//int get_random_bytes (unsigned char *buf, int n);
int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E);
int tgl_pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D);
int tgl_pad_rsa_encrypt (struct tgl_state *TLS, char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E);
int tgl_pad_rsa_decrypt (struct tgl_state *TLS, char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D);
//extern long long rsa_encrypted_chunks, rsa_decrypted_chunks;
......
......@@ -71,6 +71,7 @@ static void fail_connection (struct connection *c);
static void start_ping_timer (struct connection *c);
static void ping_alarm (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg;
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG + 2,"ping alarm\n");
assert (c->state == conn_ready || c->state == conn_connecting);
if (tglt_get_double_time () - c->last_receive_time > 6 * PING_TIMEOUT) {
......@@ -78,7 +79,7 @@ static void ping_alarm (evutil_socket_t fd, short what, void *arg) {
c->state = conn_failed;
fail_connection (c);
} else if (tglt_get_double_time () - c->last_receive_time > 3 * PING_TIMEOUT && c->state == conn_ready) {
tgl_do_send_ping (c);
tgl_do_send_ping (c->TLS, c);
start_ping_timer (c);
} else {
start_ping_timer (c);
......@@ -124,6 +125,7 @@ static void delete_connection_buffer (struct connection_buffer *b) {
}
int tgln_write_out (struct connection *c, const void *_data, int len) {
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG, "write_out: %d bytes\n", len);
const unsigned char *data = _data;
if (!len) { return 0; }
......@@ -242,14 +244,16 @@ static void try_write (struct connection *c);
static void conn_try_read (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg;
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG + 1, "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;
struct tgl_state *TLS = c->TLS;
if (c->state == conn_connecting) {
c->state = conn_ready;
c->methods->ready (c);
c->methods->ready (TLS, c);
}
try_write (c);
if (c->out_bytes) {
......@@ -257,8 +261,9 @@ static void conn_try_write (evutil_socket_t fd, short what, void *arg) {
}
}
struct connection *tgln_create_connection (const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods) {
struct connection *tgln_create_connection (struct tgl_state *TLS, const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods) {
struct connection *c = talloc0 (sizeof (*c));
c->TLS = TLS;
int fd = socket (AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
vlogprintf (E_ERROR, "Can not create socket: %m\n");
......@@ -300,12 +305,12 @@ struct connection *tgln_create_connection (const char *host, int port, struct tg
assert (!Connections[fd]);
Connections[fd] = c;
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->write_ev = event_new (tgl_state.ev_base, c->fd, EV_WRITE, conn_try_write, c);
c->ping_ev = evtimer_new (TLS->ev_base, ping_alarm, c);
c->fail_ev = evtimer_new (TLS->ev_base, fail_alarm, c);
c->write_ev = event_new (TLS->ev_base, c->fd, EV_WRITE, conn_try_write, c);
struct timeval tv = {5, 0};
c->read_ev = event_new (tgl_state.ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c);
c->read_ev = event_new (TLS->ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c);
event_add (c->read_ev, &tv);
start_ping_timer (c);
......@@ -322,6 +327,7 @@ struct connection *tgln_create_connection (const char *host, int port, struct tg
}
static void restart_connection (struct connection *c) {
struct tgl_state *TLS = c->TLS;
if (c->last_connect_time == time (0)) {
start_fail_timer (c);
return;
......@@ -369,10 +375,10 @@ static void restart_connection (struct connection *c) {
start_ping_timer (c);
Connections[fd] = c;
c->write_ev = event_new (tgl_state.ev_base, c->fd, EV_WRITE, conn_try_write, c);
c->write_ev = event_new (TLS->ev_base, c->fd, EV_WRITE, conn_try_write, c);
struct timeval tv = {5, 0};
c->read_ev = event_new (tgl_state.ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c);
c->read_ev = event_new (TLS->ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c);
event_add (c->read_ev, &tv);
char byte = 0xef;
......@@ -381,6 +387,7 @@ static void restart_connection (struct connection *c) {
}
static void fail_connection (struct connection *c) {
struct tgl_state *TLS = c->TLS;
if (c->state == conn_ready || c->state == conn_connecting) {
stop_ping_timer (c);
}
......@@ -411,6 +418,7 @@ static void fail_connection (struct connection *c) {
//extern FILE *log_net_f;
static void try_write (struct connection *c) {
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG, "try write: fd = %d\n", c->fd);
int x = 0;
while (c->out_head) {
......@@ -443,6 +451,7 @@ static void try_write (struct connection *c) {
static void try_rpc_read (struct connection *c) {
assert (c->in_head);
struct tgl_state *TLS = c->TLS;
while (1) {
if (c->in_bytes < 1) { return; }
......@@ -472,11 +481,12 @@ static void try_rpc_read (struct connection *c) {
len *= 4;
int op;
assert (tgln_read_in_lookup (c, &op, 4) == 4);
c->methods->execute (c, op, len);
c->methods->execute (TLS, c, op, len);
}
}
static void try_read (struct connection *c) {
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG, "try read: fd = %d\n", c->fd);
if (!c->in_tail) {
c->in_head = c->in_tail = new_connection_buffer (1 << 20);
......@@ -518,7 +528,7 @@ static void try_read (struct connection *c) {
try_rpc_read (c);
}
}
/*
int tgl_connections_make_poll_array (struct pollfd *fds, int max) {
int _max = max;
int i;
......@@ -562,7 +572,7 @@ void tgl_connections_poll_result (struct pollfd *fds, int max) {
}
}
}
}
}*/
static void incr_out_packet_num (struct connection *c) {
c->out_packet_num ++;
......
......@@ -20,23 +20,6 @@
#ifndef __NET_H__
#define __NET_H__
#include <poll.h>
#define DC_SERIALIZED_MAGIC 0x64582faa
#define DC_SERIALIZED_MAGIC_V2 0x94032abb
#define STATE_FILE_MAGIC 0x84217a0d
#define SECRET_CHAT_FILE_MAGIC 0xa9840add
struct tgl_dc_serialized {
int magic;
int port;
char ip[64];
char user[64];
char auth_key[256];
long long auth_key_id, server_salt;
int authorized;
};
struct connection_buffer {
unsigned char *start;
unsigned char *end;
......@@ -71,6 +54,7 @@ struct connection {
int last_connect_time;
int in_fail_timer;
struct mtproto_methods *methods;
struct tgl_state *TLS;
struct tgl_session *session;
struct tgl_dc *dc;
void *extra;
......@@ -88,16 +72,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 tgl_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 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);
//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 (struct tgl_state *TLS, 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
This diff is collapsed.
......@@ -24,16 +24,14 @@
#include "auto.h"
#include "tgl-layout.h"
struct event;
#define QUERY_ACK_RECEIVED 1
#define QUERY_FORCE_SEND 2
struct query;
struct query_methods {
int (*on_answer)(struct query *q);
int (*on_error)(struct query *q, int error_code, int len, char *error);
int (*on_timeout)(struct query *q);
int (*on_answer)(struct tgl_state *TLS, struct query *q);
int (*on_error)(struct tgl_state *TLS, struct query *q, int error_code, int len, char *error);
int (*on_timeout)(struct tgl_state *TLS, struct query *q);
struct paramed_type *type;
};
......@@ -45,7 +43,7 @@ struct query {
long long session_id;
void *data;
struct query_methods *methods;
struct event *ev;
struct tgl_timer *ev;
struct tgl_dc *DC;
struct tgl_session *session;
struct paramed_type *type;
......@@ -55,20 +53,20 @@ struct query {
};
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);
void tglq_query_restart (long long id);
struct query *tglq_send_query (struct tgl_state *TLS, struct tgl_dc *DC, int len, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra);
void tglq_query_ack (struct tgl_state *TLS, long long id);
void tglq_query_error (struct tgl_state *TLS, long long id);
void tglq_query_result (struct tgl_state *TLS, long long id);
void tglq_query_restart (struct tgl_state *TLS, long long id);
double next_timer_in (void);
void work_timers (void);
//double next_timer_in (void);
//void work_timers (void);
//extern struct query_methods help_get_config_methods;
double get_double_time (void);
void tgl_do_send_bind_temp_key (struct tgl_dc *D, long long nonce, int expires_at, void *data, int len, long long msg_id);
void tgl_do_send_bind_temp_key (struct tgl_state *TLS, struct tgl_dc *D, long long nonce, int expires_at, void *data, int len, long long msg_id);
// For binlog
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -23,8 +23,8 @@
#define vlogprintf(verbosity_level,...) \
do { \
if (tgl_state.verbosity >= verbosity_level) { \
tgl_state.callback.logprintf (__VA_ARGS__); \
if (TLS->verbosity >= verbosity_level) { \
TLS->callback.logprintf (__VA_ARGS__); \
} \
} while (0)
......
......@@ -62,8 +62,7 @@ struct tgl_session {
int seq_no;
struct connection *c;
struct tree_long *ack_tree;
struct event *ev;
//struct event_timer ev;
struct tgl_timer *ev;
};
struct tgl_dc {
......@@ -83,7 +82,7 @@ struct tgl_dc {
long long temp_auth_key_id;
long long server_salt;
struct event *ev;
struct tgl_timer *ev;
int server_time_delta;
double server_time_udelta;
......
This diff is collapsed.
/*
This file is part of tgl-library
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Copyright Vitaly Valtman 2013-2014
*/
#ifndef __TGL_TIMERS_H__
#define __TGL_TIMERS_H__
#include "tgl.h"
extern struct tgl_timer_methods tgl_libevent_timers;
#endif
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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