Commit 56fe7bed authored by Vysheng's avatar Vysheng

Refactored code a bit to better separation of library. Library can now be...

Refactored code a bit to better separation of library. Library can now be compiled without libevent.
parent c01eb112
...@@ -21,7 +21,7 @@ EXE_LIST=${EXE}/generate ${EXE}/tlc ${EXE}/telegram-cli ...@@ -21,7 +21,7 @@ EXE_LIST=${EXE}/generate ${EXE}/tlc ${EXE}/telegram-cli
LIB_LIST=${LIB}/libtgl.a LIB_LIST=${LIB}/libtgl.a
TG_OBJECTS=${OBJ}/main.o ${OBJ}/loop.o ${OBJ}/interface.o ${OBJ}/lua-tg.o 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 TLC_OBJECTS=${OBJ}/tlc.o ${OBJ}/tl-parser.o ${OBJ}/crc32.o
TLD_OBJECTS=${OBJ}/dump-tl-file.o TLD_OBJECTS=${OBJ}/dump-tl-file.o
GENERATE_OBJECTS=${OBJ}/generate.o GENERATE_OBJECTS=${OBJ}/generate.o
......
...@@ -303,7 +303,7 @@ static void free_vars_to_be_freed (void) { ...@@ -303,7 +303,7 @@ static void free_vars_to_be_freed (void) {
fvars_pos = 0; 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) { if (index == -1) {
buffer_pos = data; buffer_pos = data;
buffer_end = data + data_len; buffer_end = data + data_len;
...@@ -330,7 +330,7 @@ int tglf_extf_autocomplete (const char *text, int text_len, int index, char **R, ...@@ -330,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_pos = (char *)data;
buffer_end = (char *)(data + data_len); buffer_end = (char *)(data + data_len);
local_next_token (); local_next_token ();
...@@ -432,7 +432,7 @@ static void print_offset (void) { ...@@ -432,7 +432,7 @@ 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; out_buf_pos = 0;
if (fetch_type_any (T) < 0) { return 0; } if (fetch_type_any (T) < 0) { return 0; }
return out_buf; return out_buf;
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -45,7 +45,7 @@ void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2))) ...@@ -45,7 +45,7 @@ void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2)))
#define vlogprintf(v,...) \ #define vlogprintf(v,...) \
do { \ do { \
if (tgl_state.verbosity >= (v)) {\ if (TLS->verbosity >= (v)) {\
logprintf (__VA_ARGS__);\ logprintf (__VA_ARGS__);\
}\ }\
} while (0);\ } while (0);\
......
This diff is collapsed.
This diff is collapsed.
...@@ -114,6 +114,8 @@ int port; ...@@ -114,6 +114,8 @@ int port;
int use_ids; int use_ids;
char *start_command; char *start_command;
struct tgl_state *TLS;
void set_default_username (const char *s) { void set_default_username (const char *s) {
if (default_username) { if (default_username) {
tfree_str (default_username); tfree_str (default_username);
...@@ -348,7 +350,7 @@ void parse_config (void) { ...@@ -348,7 +350,7 @@ void parse_config (void) {
strcpy (buf + l, "test"); strcpy (buf + l, "test");
config_lookup_bool (&conf, buf, &test_mode); config_lookup_bool (&conf, buf, &test_mode);
if (test_mode) { if (test_mode) {
tgl_set_test_mode (); tgl_set_test_mode (TLS);
} }
strcpy (buf + l, "log_level"); strcpy (buf + l, "log_level");
...@@ -378,20 +380,20 @@ void parse_config (void) { ...@@ -378,20 +380,20 @@ void parse_config (void) {
strcpy (buf + l, "pfs_enabled"); strcpy (buf + l, "pfs_enabled");
config_lookup_bool (&conf, buf, &pfs_enabled); config_lookup_bool (&conf, buf, &pfs_enabled);
if (pfs_enabled) { if (pfs_enabled) {
tgl_enable_pfs (); tgl_enable_pfs (TLS);
} }
if (binlog_enabled) { if (binlog_enabled) {
parse_config_val (&conf, &binlog_file_name, "binlog", BINLOG_FILE, config_directory); parse_config_val (&conf, &binlog_file_name, "binlog", BINLOG_FILE, config_directory);
tgl_set_binlog_mode (1); tgl_set_binlog_mode (TLS, 1);
tgl_set_binlog_path (binlog_file_name); tgl_set_binlog_path (TLS, binlog_file_name);
} else { } 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, &state_file_name, "state_file", STATE_FILE, config_directory);
parse_config_val (&conf, &secret_chat_file_name, "secret", SECRET_CHAT_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_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 (!mkdir (config_directory, CONFIG_DIRECTORY_MODE)) {
if (!disable_output) { if (!disable_output) {
...@@ -414,16 +416,16 @@ void parse_config (void) { ...@@ -414,16 +416,16 @@ void parse_config (void) {
if (binlog_enabled) { if (binlog_enabled) {
tasprintf (&binlog_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, BINLOG_FILE); tasprintf (&binlog_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, BINLOG_FILE);
tgl_set_binlog_mode (1); tgl_set_binlog_mode (TLS, 1);
tgl_set_binlog_path (binlog_file_name); tgl_set_binlog_path (TLS, binlog_file_name);
} else { } else {
tgl_set_binlog_mode (0); tgl_set_binlog_mode (TLS, 0);
//tgl_set_auth_file_path (auth_file_name; //tgl_set_auth_file_path (auth_file_name;
tasprintf (&auth_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, AUTH_KEY_FILE); 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 (&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); 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 (!mkdir (downloads_directory, CONFIG_DIRECTORY_MODE)) {
if (!disable_output) { if (!disable_output) {
printf ("[%s] created\n", downloads_directory); printf ("[%s] created\n", downloads_directory);
...@@ -568,6 +570,7 @@ int change_user_group () { ...@@ -568,6 +570,7 @@ int change_user_group () {
char *unix_socket; char *unix_socket;
void args_parse (int argc, char **argv) { void args_parse (int argc, char **argv) {
TLS = tgl_state_alloc ();
int opt = 0; int opt = 0;
while ((opt = getopt (argc, argv, "u:hk:vNl:fEwWCRdL:DU:G:qP:S:e:I" while ((opt = getopt (argc, argv, "u:hk:vNl:fEwWCRdL:DU:G:qP:S:e:I"
#ifdef HAVE_LIBCONFIG #ifdef HAVE_LIBCONFIG
...@@ -586,10 +589,10 @@ void args_parse (int argc, char **argv) { ...@@ -586,10 +589,10 @@ void args_parse (int argc, char **argv) {
break; break;
case 'k': case 'k':
//rsa_public_key_name = tstrdup (optarg); //rsa_public_key_name = tstrdup (optarg);
tgl_set_rsa_key (optarg); tgl_set_rsa_key (TLS, optarg);
break; break;
case 'v': case 'v':
tgl_incr_verbosity (); tgl_incr_verbosity (TLS);
verbosity ++; verbosity ++;
break; break;
case 'N': case 'N':
...@@ -722,8 +725,8 @@ void sig_term_handler (int signum __attribute__ ((unused))) { ...@@ -722,8 +725,8 @@ void sig_term_handler (int signum __attribute__ ((unused))) {
if (write (1, "SIGTERM/SIGINT received\n", 25) < 0) { if (write (1, "SIGTERM/SIGINT received\n", 25) < 0) {
// Sad thing // Sad thing
} }
if (tgl_state.ev_base) { if (TLS && TLS->ev_base) {
event_base_loopbreak(tgl_state.ev_base); event_base_loopbreak (TLS->ev_base);
} }
sigterm_cnt ++; sigterm_cnt ++;
} }
...@@ -841,8 +844,8 @@ int main (int argc, char **argv) { ...@@ -841,8 +844,8 @@ int main (int argc, char **argv) {
running_for_first_time (); running_for_first_time ();
parse_config (); parse_config ();
tgl_set_rsa_key ("/etc/" PROG_NAME "/server.pub"); tgl_set_rsa_key (TLS, "/etc/" PROG_NAME "/server.pub");
tgl_set_rsa_key ("tg-server.pub"); tgl_set_rsa_key (TLS, "tg-server.pub");
get_terminal_attributes (); get_terminal_attributes ();
......
This diff is collapsed.
...@@ -45,16 +45,16 @@ struct tgl_dc; ...@@ -45,16 +45,16 @@ struct tgl_dc;
struct connection; struct connection;
long long tglmp_encrypt_send_message (struct connection *c, int *msg, int msg_ints, int flags); 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_dc *DC); void tglmp_dc_create_session (struct tgl_state *TLS, struct tgl_dc *DC);
int tglmp_check_g (unsigned char p[256], BIGNUM *g); int tglmp_check_g (struct tgl_state *TLS, unsigned char p[256], BIGNUM *g);
int tglmp_check_DH_params (BIGNUM *p, int g); int tglmp_check_DH_params (struct tgl_state *TLS, BIGNUM *p, int g);
struct tgl_dc *tglmp_alloc_dc (int id, char *ip, int port); struct tgl_dc *tglmp_alloc_dc (struct tgl_state *TLS, int id, char *ip, int port);
void tglmp_regenerate_temp_auth_key (struct tgl_dc *D); void tglmp_regenerate_temp_auth_key (struct tgl_state *TLS, struct tgl_dc *D);
void tgln_insert_msg_id (struct tgl_session *S, long long id); void tgln_insert_msg_id (struct tgl_state *TLS, struct tgl_session *S, long long id);
void tglmp_on_start (void); void tglmp_on_start (struct tgl_state *TLS);
void tgl_dc_authorize (struct tgl_dc *DC); void tgl_dc_authorize (struct tgl_state *TLS, struct tgl_dc *DC);
void tgls_free_dc (struct tgl_dc *DC); void tgls_free_dc (struct tgl_state *TLS, struct tgl_dc *DC);
void tgls_free_pubkey (void); void tgls_free_pubkey (struct tgl_state *TLS);
#endif #endif
...@@ -57,7 +57,7 @@ static long long rsa_encrypted_chunks, rsa_decrypted_chunks; ...@@ -57,7 +57,7 @@ static long long rsa_encrypted_chunks, rsa_decrypted_chunks;
//int verbosity; //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); int r = 0, h = open ("/dev/random", O_RDONLY | O_NONBLOCK);
if (h >= 0) { if (h >= 0) {
r = read (h, buf, n); r = read (h, buf, n);
...@@ -107,7 +107,7 @@ static __inline__ unsigned long long rdtsc (void) { ...@@ -107,7 +107,7 @@ static __inline__ unsigned long long rdtsc (void) {
} }
#endif #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; struct timespec T;
tgl_my_clock_gettime (CLOCK_REALTIME, &T); tgl_my_clock_gettime (CLOCK_REALTIME, &T);
RAND_add (&T, sizeof (T), 4.0); RAND_add (&T, sizeof (T), 4.0);
...@@ -120,7 +120,7 @@ void tgl_prng_seed (const char *password_filename, int password_length) { ...@@ -120,7 +120,7 @@ void tgl_prng_seed (const char *password_filename, int password_length) {
p = getppid (); p = getppid ();
RAND_add (&p, sizeof (p), 0.0); RAND_add (&p, sizeof (p), 0.0);
unsigned char rb[32]; unsigned char rb[32];
int s = get_random_bytes (rb, 32); int s = get_random_bytes (TLS, rb, 32);
if (s > 0) { if (s > 0) {
RAND_add (rb, s, s); RAND_add (rb, s, s);
} }
...@@ -142,8 +142,8 @@ void tgl_prng_seed (const char *password_filename, int password_length) { ...@@ -142,8 +142,8 @@ void tgl_prng_seed (const char *password_filename, int password_length) {
tfree_secure (a, password_length); tfree_secure (a, password_length);
} }
} }
tgl_state.BN_ctx = BN_CTX_new (); TLS->BN_ctx = BN_CTX_new ();
ensure_ptr (tgl_state.BN_ctx); ensure_ptr (TLS->BN_ctx);
} }
int tgl_serialize_bignum (BIGNUM *b, char *buffer, int maxlen) { int tgl_serialize_bignum (BIGNUM *b, char *buffer, int maxlen) {
...@@ -250,7 +250,7 @@ int tgl_fetch_bignum (BIGNUM *x) { ...@@ -250,7 +250,7 @@ int tgl_fetch_bignum (BIGNUM *x) {
return l; 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 pad = (255000 - from_len - 32) % 255 + 32;
int chunks = (from_len + pad) / 255; int chunks = (from_len + pad) / 255;
int bits = BN_num_bits (N); 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 ...@@ -265,7 +265,7 @@ int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N
rsa_encrypted_chunks += chunks; rsa_encrypted_chunks += chunks;
for (i = 0; i < chunks; i++) { for (i = 0; i < chunks; i++) {
BN_bin2bn ((unsigned char *) from, 255, &x); 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); unsigned l = 256 - BN_num_bytes (&y);
assert (l <= 256); assert (l <= 256);
memset (to, 0, l); memset (to, 0, l);
...@@ -277,7 +277,7 @@ int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N ...@@ -277,7 +277,7 @@ int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N
return chunks * 256; 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)) { if (from_len < 0 || from_len > 0x1000 || (from_len & 0xff)) {
return -1; return -1;
} }
...@@ -292,7 +292,7 @@ int tgl_pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N ...@@ -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++) { for (i = 0; i < chunks; i++) {
++rsa_decrypted_chunks; ++rsa_decrypted_chunks;
BN_bin2bn ((unsigned char *) from, 256, &x); 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); int l = BN_num_bytes (&y);
if (l > 255) { if (l > 255) {
BN_free (&x); BN_free (&x);
......
...@@ -117,7 +117,7 @@ struct encrypted_message { ...@@ -117,7 +117,7 @@ struct encrypted_message {
//BN_CTX *BN_ctx; //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); int tgl_serialize_bignum (BIGNUM *b, char *buffer, int maxlen);
long long tgl_do_compute_rsa_key_fingerprint (RSA *key); long long tgl_do_compute_rsa_key_fingerprint (RSA *key);
...@@ -356,8 +356,8 @@ static inline int in_remaining (void) { ...@@ -356,8 +356,8 @@ static inline int in_remaining (void) {
//int get_random_bytes (unsigned char *buf, int n); //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_encrypt (struct tgl_state *TLS, 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_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; //extern long long rsa_encrypted_chunks, rsa_decrypted_chunks;
......
...@@ -71,6 +71,7 @@ static void fail_connection (struct connection *c); ...@@ -71,6 +71,7 @@ static void fail_connection (struct connection *c);
static void start_ping_timer (struct connection *c); static void start_ping_timer (struct connection *c);
static void ping_alarm (evutil_socket_t fd, short what, void *arg) { static void ping_alarm (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg; struct connection *c = arg;
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG + 2,"ping alarm\n"); vlogprintf (E_DEBUG + 2,"ping alarm\n");
assert (c->state == conn_ready || c->state == conn_connecting); assert (c->state == conn_ready || c->state == conn_connecting);
if (tglt_get_double_time () - c->last_receive_time > 6 * PING_TIMEOUT) { 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) { ...@@ -78,7 +79,7 @@ static void ping_alarm (evutil_socket_t fd, short what, void *arg) {
c->state = conn_failed; c->state = conn_failed;
fail_connection (c); fail_connection (c);
} else if (tglt_get_double_time () - c->last_receive_time > 3 * PING_TIMEOUT && c->state == conn_ready) { } 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); start_ping_timer (c);
} else { } else {
start_ping_timer (c); start_ping_timer (c);
...@@ -124,6 +125,7 @@ static void delete_connection_buffer (struct connection_buffer *b) { ...@@ -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) { 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); vlogprintf (E_DEBUG, "write_out: %d bytes\n", len);
const unsigned char *data = _data; const unsigned char *data = _data;
if (!len) { return 0; } if (!len) { return 0; }
...@@ -242,14 +244,16 @@ static void try_write (struct connection *c); ...@@ -242,14 +244,16 @@ static void try_write (struct connection *c);
static void conn_try_read (evutil_socket_t fd, short what, void *arg) { static void conn_try_read (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg; struct connection *c = arg;
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG + 1, "Try read. Fd = %d\n", c->fd); vlogprintf (E_DEBUG + 1, "Try read. Fd = %d\n", c->fd);
try_read (c); try_read (c);
} }
static void conn_try_write (evutil_socket_t fd, short what, void *arg) { static void conn_try_write (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg; struct connection *c = arg;
struct tgl_state *TLS = c->TLS;
if (c->state == conn_connecting) { if (c->state == conn_connecting) {
c->state = conn_ready; c->state = conn_ready;
c->methods->ready (c); c->methods->ready (TLS, c);
} }
try_write (c); try_write (c);
if (c->out_bytes) { if (c->out_bytes) {
...@@ -257,8 +261,9 @@ static void conn_try_write (evutil_socket_t fd, short what, void *arg) { ...@@ -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)); struct connection *c = talloc0 (sizeof (*c));
c->TLS = TLS;
int fd = socket (AF_INET, SOCK_STREAM, 0); int fd = socket (AF_INET, SOCK_STREAM, 0);
if (fd == -1) { if (fd == -1) {
vlogprintf (E_ERROR, "Can not create socket: %m\n"); 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 ...@@ -300,12 +305,12 @@ struct connection *tgln_create_connection (const char *host, int port, struct tg
assert (!Connections[fd]); assert (!Connections[fd]);
Connections[fd] = c; Connections[fd] = c;
c->ping_ev = evtimer_new (tgl_state.ev_base, ping_alarm, c); c->ping_ev = evtimer_new (TLS->ev_base, ping_alarm, c);
c->fail_ev = evtimer_new (tgl_state.ev_base, fail_alarm, c); c->fail_ev = evtimer_new (TLS->ev_base, fail_alarm, 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}; 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); event_add (c->read_ev, &tv);
start_ping_timer (c); start_ping_timer (c);
...@@ -322,6 +327,7 @@ struct connection *tgln_create_connection (const char *host, int port, struct tg ...@@ -322,6 +327,7 @@ struct connection *tgln_create_connection (const char *host, int port, struct tg
} }
static void restart_connection (struct connection *c) { static void restart_connection (struct connection *c) {
struct tgl_state *TLS = c->TLS;
if (c->last_connect_time == time (0)) { if (c->last_connect_time == time (0)) {
start_fail_timer (c); start_fail_timer (c);
return; return;
...@@ -369,10 +375,10 @@ static void restart_connection (struct connection *c) { ...@@ -369,10 +375,10 @@ static void restart_connection (struct connection *c) {
start_ping_timer (c); start_ping_timer (c);
Connections[fd] = 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}; 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); event_add (c->read_ev, &tv);
char byte = 0xef; char byte = 0xef;
...@@ -381,6 +387,7 @@ static void restart_connection (struct connection *c) { ...@@ -381,6 +387,7 @@ static void restart_connection (struct connection *c) {
} }
static void fail_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) { if (c->state == conn_ready || c->state == conn_connecting) {
stop_ping_timer (c); stop_ping_timer (c);
} }
...@@ -411,6 +418,7 @@ static void fail_connection (struct connection *c) { ...@@ -411,6 +418,7 @@ static void fail_connection (struct connection *c) {
//extern FILE *log_net_f; //extern FILE *log_net_f;
static void try_write (struct connection *c) { static void try_write (struct connection *c) {
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG, "try write: fd = %d\n", c->fd); vlogprintf (E_DEBUG, "try write: fd = %d\n", c->fd);
int x = 0; int x = 0;
while (c->out_head) { while (c->out_head) {
...@@ -443,6 +451,7 @@ static void try_write (struct connection *c) { ...@@ -443,6 +451,7 @@ static void try_write (struct connection *c) {
static void try_rpc_read (struct connection *c) { static void try_rpc_read (struct connection *c) {
assert (c->in_head); assert (c->in_head);
struct tgl_state *TLS = c->TLS;
while (1) { while (1) {
if (c->in_bytes < 1) { return; } if (c->in_bytes < 1) { return; }
...@@ -472,11 +481,12 @@ static void try_rpc_read (struct connection *c) { ...@@ -472,11 +481,12 @@ static void try_rpc_read (struct connection *c) {
len *= 4; len *= 4;
int op; int op;
assert (tgln_read_in_lookup (c, &op, 4) == 4); 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) { static void try_read (struct connection *c) {
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG, "try read: fd = %d\n", c->fd); vlogprintf (E_DEBUG, "try read: fd = %d\n", c->fd);
if (!c->in_tail) { if (!c->in_tail) {
c->in_head = c->in_tail = new_connection_buffer (1 << 20); c->in_head = c->in_tail = new_connection_buffer (1 << 20);
...@@ -518,7 +528,7 @@ static void try_read (struct connection *c) { ...@@ -518,7 +528,7 @@ static void try_read (struct connection *c) {
try_rpc_read (c); try_rpc_read (c);
} }
} }
/*
int tgl_connections_make_poll_array (struct pollfd *fds, int max) { int tgl_connections_make_poll_array (struct pollfd *fds, int max) {
int _max = max; int _max = max;
int i; int i;
...@@ -562,7 +572,7 @@ void tgl_connections_poll_result (struct pollfd *fds, int max) { ...@@ -562,7 +572,7 @@ void tgl_connections_poll_result (struct pollfd *fds, int max) {
} }
} }
} }
} }*/
static void incr_out_packet_num (struct connection *c) { static void incr_out_packet_num (struct connection *c) {
c->out_packet_num ++; c->out_packet_num ++;
......
...@@ -20,23 +20,6 @@ ...@@ -20,23 +20,6 @@
#ifndef __NET_H__ #ifndef __NET_H__
#define __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 { struct connection_buffer {
unsigned char *start; unsigned char *start;
unsigned char *end; unsigned char *end;
...@@ -71,6 +54,7 @@ struct connection { ...@@ -71,6 +54,7 @@ struct connection {
int last_connect_time; int last_connect_time;
int in_fail_timer; int in_fail_timer;
struct mtproto_methods *methods; struct mtproto_methods *methods;
struct tgl_state *TLS;
struct tgl_session *session; struct tgl_session *session;
struct tgl_dc *dc; struct tgl_dc *dc;
void *extra; void *extra;
...@@ -88,16 +72,16 @@ void tgln_flush_out (struct connection *c); ...@@ -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 (struct connection *c, void *data, int len);
int tgln_read_in_lookup (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; extern struct tgl_net_methods tgl_conn_methods;
//void create_all_outbound_connections (void); //void create_all_outbound_connections (void);
//struct connection *create_connection (const char *host, int port, struct tgl_session *session, struct connection_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); //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); //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 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) #define GET_DC(c) (c->session->dc)
#endif #endif
This diff is collapsed.
...@@ -24,16 +24,14 @@ ...@@ -24,16 +24,14 @@
#include "auto.h" #include "auto.h"
#include "tgl-layout.h" #include "tgl-layout.h"
struct event;
#define QUERY_ACK_RECEIVED 1 #define QUERY_ACK_RECEIVED 1
#define QUERY_FORCE_SEND 2 #define QUERY_FORCE_SEND 2
struct query; struct query;
struct query_methods { struct query_methods {
int (*on_answer)(struct query *q); int (*on_answer)(struct tgl_state *TLS, struct query *q);
int (*on_error)(struct query *q, int error_code, int len, char *error); int (*on_error)(struct tgl_state *TLS, struct query *q, int error_code, int len, char *error);
int (*on_timeout)(struct query *q); int (*on_timeout)(struct tgl_state *TLS, struct query *q);
struct paramed_type *type; struct paramed_type *type;
}; };
...@@ -45,7 +43,7 @@ struct query { ...@@ -45,7 +43,7 @@ struct query {
long long session_id; long long session_id;
void *data; void *data;
struct query_methods *methods; struct query_methods *methods;
struct event *ev; struct tgl_timer *ev;
struct tgl_dc *DC; struct tgl_dc *DC;
struct tgl_session *session; struct tgl_session *session;
struct paramed_type *type; struct paramed_type *type;
...@@ -55,20 +53,20 @@ struct query { ...@@ -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); 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 (long long id); void tglq_query_ack (struct tgl_state *TLS, long long id);
void tglq_query_error (long long id); void tglq_query_error (struct tgl_state *TLS, long long id);
void tglq_query_result (long long id); void tglq_query_result (struct tgl_state *TLS, long long id);
void tglq_query_restart (long long id); void tglq_query_restart (struct tgl_state *TLS, long long id);
double next_timer_in (void); //double next_timer_in (void);
void work_timers (void); //void work_timers (void);
//extern struct query_methods help_get_config_methods; //extern struct query_methods help_get_config_methods;
double get_double_time (void); 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 // For binlog
......
This diff is collapsed.
...@@ -23,34 +23,35 @@ ...@@ -23,34 +23,35 @@
#include <assert.h> #include <assert.h>
#include "tgl-layout.h" #include "tgl-layout.h"
#include "tgl-fetch.h" #include "tgl-fetch.h"
#include "tgl.h"
char *tgls_default_create_print_name (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4);
char *tgls_default_create_print_name (struct tgl_state *TLS, tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4);
void tgls_free_user (struct tgl_user *U);
void tgls_free_chat (struct tgl_chat *U); void tgls_free_user (struct tgl_state *TLS, struct tgl_user *U);
void tgls_free_photo (struct tgl_photo *P); void tgls_free_chat (struct tgl_state *TLS, struct tgl_chat *U);
void tgls_free_message (struct tgl_message *M); void tgls_free_photo (struct tgl_state *TLS, struct tgl_photo *P);
void tgls_clear_message (struct tgl_message *M); void tgls_free_message (struct tgl_state *TLS, struct tgl_message *M);
void tgls_clear_message (struct tgl_state *TLS, struct tgl_message *M);
struct tgl_message *tglm_message_alloc (long long id);
void tglm_message_insert_tree (struct tgl_message *M); struct tgl_message *tglm_message_alloc (struct tgl_state *TLS, long long id);
void tglm_update_message_id (struct tgl_message *M, long long id); void tglm_message_insert_tree (struct tgl_state *TLS, struct tgl_message *M);
void tglm_message_insert (struct tgl_message *M); void tglm_update_message_id (struct tgl_state *TLS, struct tgl_message *M, long long id);
void tglm_message_insert_unsent (struct tgl_message *M); void tglm_message_insert (struct tgl_state *TLS, struct tgl_message *M);
void tglm_message_remove_unsent (struct tgl_message *M); void tglm_message_insert_unsent (struct tgl_state *TLS, struct tgl_message *M);
void tglm_send_all_unsent (void); void tglm_message_remove_unsent (struct tgl_state *TLS, struct tgl_message *M);
void tglm_message_remove_tree (struct tgl_message *M); void tglm_send_all_unsent (struct tgl_state *TLS);
void tglm_message_add_peer (struct tgl_message *M); void tglm_message_remove_tree (struct tgl_state *TLS, struct tgl_message *M);
void tglm_message_del_peer (struct tgl_message *M); void tglm_message_add_peer (struct tgl_state *TLS, struct tgl_message *M);
void tglm_message_del_use (struct tgl_message *M); void tglm_message_del_peer (struct tgl_state *TLS, struct tgl_message *M);
void tglm_message_add_use (struct tgl_message *M); void tglm_message_del_use (struct tgl_state *TLS, struct tgl_message *M);
void tglm_message_add_use (struct tgl_state *TLS, struct tgl_message *M);
void tglp_peer_insert_name (tgl_peer_t *P);
void tglp_peer_delete_name (tgl_peer_t *P); void tglp_peer_insert_name (struct tgl_state *TLS, tgl_peer_t *P);
void tglp_insert_encrypted_chat (tgl_peer_t *P); void tglp_peer_delete_name (struct tgl_state *TLS, tgl_peer_t *P);
void tglp_insert_user (tgl_peer_t *P); void tglp_insert_encrypted_chat (struct tgl_state *TLS, tgl_peer_t *P);
void tglp_insert_chat (tgl_peer_t *P); void tglp_insert_user (struct tgl_state *TLS, tgl_peer_t *P);
void tglp_insert_chat (struct tgl_state *TLS, tgl_peer_t *P);
enum tgl_typing_status tglf_fetch_typing (void); enum tgl_typing_status tglf_fetch_typing (void);
#endif #endif
...@@ -19,35 +19,36 @@ ...@@ -19,35 +19,36 @@
*/ */
#ifndef __TGL_FETCH_H__ #ifndef __TGL_FETCH_H__
#define __TGL_FETCH_H__ #define __TGL_FETCH_H__
#include "tgl.h"
int tglf_fetch_file_location (struct tgl_file_location *loc);
int tglf_fetch_user_status (struct tgl_user_status *S); int tglf_fetch_file_location (struct tgl_state *TLS, struct tgl_file_location *loc);
int tglf_fetch_user (struct tgl_user *U); int tglf_fetch_user_status (struct tgl_state *TLS, struct tgl_user_status *S);
struct tgl_user *tglf_fetch_alloc_user (void); int tglf_fetch_user (struct tgl_state *TLS, struct tgl_user *U);
struct tgl_user *tglf_fetch_alloc_user_full (void); struct tgl_user *tglf_fetch_alloc_user (struct tgl_state *TLS);
struct tgl_chat *tglf_fetch_alloc_chat (void); struct tgl_user *tglf_fetch_alloc_user_full (struct tgl_state *TLS);
struct tgl_chat *tglf_fetch_alloc_chat_full (void); struct tgl_chat *tglf_fetch_alloc_chat (struct tgl_state *TLS);
struct tgl_secret_chat *tglf_fetch_alloc_encrypted_chat (void); struct tgl_chat *tglf_fetch_alloc_chat_full (struct tgl_state *TLS);
struct tgl_message *tglf_fetch_alloc_message (void); struct tgl_secret_chat *tglf_fetch_alloc_encrypted_chat (struct tgl_state *TLS);
struct tgl_message *tglf_fetch_alloc_geo_message (void); struct tgl_message *tglf_fetch_alloc_message (struct tgl_state *TLS);
struct tgl_message *tglf_fetch_alloc_message_short (void); struct tgl_message *tglf_fetch_alloc_geo_message (struct tgl_state *TLS);
struct tgl_message *tglf_fetch_alloc_message_short_chat (void); struct tgl_message *tglf_fetch_alloc_message_short (struct tgl_state *TLS);
struct tgl_message *tglf_fetch_alloc_encrypted_message (void); struct tgl_message *tglf_fetch_alloc_message_short_chat (struct tgl_state *TLS);
void tglf_fetch_encrypted_message_file (struct tgl_message_media *M); struct tgl_message *tglf_fetch_alloc_encrypted_message (struct tgl_state *TLS);
tgl_peer_id_t tglf_fetch_peer_id (void); void tglf_fetch_encrypted_message_file (struct tgl_state *TLS, struct tgl_message_media *M);
tgl_peer_id_t tglf_fetch_peer_id (struct tgl_state *TLS);
void tglf_fetch_message_media (struct tgl_message_media *M);
void tglf_fetch_message_media_encrypted (struct tgl_message_media *M); void tglf_fetch_message_media (struct tgl_state *TLS, struct tgl_message_media *M);
void tglf_fetch_message_action (struct tgl_message_action *M); void tglf_fetch_message_media_encrypted (struct tgl_state *TLS, struct tgl_message_media *M);
void tglf_fetch_message_action_encrypted (struct tgl_message_action *M); void tglf_fetch_message_action (struct tgl_state *TLS, struct tgl_message_action *M);
void tglf_fetch_photo (struct tgl_photo *P); void tglf_fetch_message_action_encrypted (struct tgl_state *TLS, struct tgl_message_action *M);
void tglf_fetch_photo (struct tgl_state *TLS, struct tgl_photo *P);
void tglf_fetch_chat (struct tgl_chat *C);
void tglf_fetch_chat_full (struct tgl_chat *C); void tglf_fetch_chat (struct tgl_state *TLS, struct tgl_chat *C);
void tglf_fetch_chat_full (struct tgl_state *TLS, struct tgl_chat *C);
void tglf_fetch_audio (struct tgl_audio *V);
void tglf_fetch_video (struct tgl_video *V); void tglf_fetch_audio (struct tgl_state *TLS, struct tgl_audio *V);
void tglf_fetch_document (struct tgl_document *V); void tglf_fetch_video (struct tgl_state *TLS, struct tgl_video *V);
void tglf_fetch_message (struct tgl_message *M); void tglf_fetch_document (struct tgl_state *TLS, struct tgl_document *V);
void tglf_fetch_geo_message (struct tgl_message *M); void tglf_fetch_message (struct tgl_state *TLS, struct tgl_message *M);
void tglf_fetch_geo_message (struct tgl_state *TLS, struct tgl_message *M);
#endif #endif
...@@ -23,8 +23,8 @@ ...@@ -23,8 +23,8 @@
#define vlogprintf(verbosity_level,...) \ #define vlogprintf(verbosity_level,...) \
do { \ do { \
if (tgl_state.verbosity >= verbosity_level) { \ if (TLS->verbosity >= verbosity_level) { \
tgl_state.callback.logprintf (__VA_ARGS__); \ TLS->callback.logprintf (__VA_ARGS__); \
} \ } \
} while (0) } while (0)
......
...@@ -62,8 +62,7 @@ struct tgl_session { ...@@ -62,8 +62,7 @@ struct tgl_session {
int seq_no; int seq_no;
struct connection *c; struct connection *c;
struct tree_long *ack_tree; struct tree_long *ack_tree;
struct event *ev; struct tgl_timer *ev;
//struct event_timer ev;
}; };
struct tgl_dc { struct tgl_dc {
...@@ -83,7 +82,7 @@ struct tgl_dc { ...@@ -83,7 +82,7 @@ struct tgl_dc {
long long temp_auth_key_id; long long temp_auth_key_id;
long long server_salt; long long server_salt;
struct event *ev; struct tgl_timer *ev;
int server_time_delta; int server_time_delta;
double server_time_udelta; double server_time_udelta;
......
/*
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
*/
#include "config.h"
#ifdef EVENT_V2
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#else
#include <event.h>
#include "event-old.h"
#endif
#include "tgl.h"
#include <stdlib.h>
static void timer_alarm (evutil_socket_t fd, short what, void *arg) {
void **p = arg;
((void (*)(struct tgl_state *, void *))p[1]) (p[0], p[2]);
}
struct tgl_timer *tgl_timer_alloc (struct tgl_state *TLS, void (*cb)(struct tgl_state *TLS, void *arg), void *arg) {
void **p = malloc (sizeof (void *) * 3);
p[0] = TLS;
p[1] = cb;
p[2] = arg;
return (void *)evtimer_new (TLS->ev_base, timer_alarm, p);
}
void tgl_timer_insert (struct tgl_timer *t, double p) {
if (p < 0) { p = 0; }
double e = p - (int)p;
if (e < 0) { e = 0; }
struct timeval pv = { (int)p, (int)(e * 1e6)};
event_add ((void *)t, &pv);
}
void tgl_timer_delete (struct tgl_timer *t) {
event_del ((void *)t);
}
void tgl_timer_free (struct tgl_timer *t) {
void *arg = event_get_callback_arg ((void *)t);
free (arg);
event_free ((void *)t);
}
struct tgl_timer_methods tgl_libevent_timers = {
.alloc = tgl_timer_alloc,
.insert = tgl_timer_insert,
.delete = tgl_timer_delete,
.free = tgl_timer_free
};
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