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],
[
......
This diff is collapsed.
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 ++;
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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)
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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