Merge github.com:vysheng/tg

This commit is contained in:
vysheng 2013-11-12 01:59:54 +04:00
commit 568a0b8ab7
15 changed files with 138 additions and 41 deletions

View File

@ -4,10 +4,10 @@ CFLAGS=-g -O2
LDFLAGS=
CPPFLAGS=
DEFS=-DHAVE_CONFIG_H
COMPILE_FLAGS=${CFLAGS} ${CPPFLAGS} ${DEFS} -Wall -Wextra -Werror -fno-strict-aliasing -fno-omit-frame-pointer -ggdb
COMPILE_FLAGS=${CFLAGS} ${CPPFLAGS} ${DEFS} -Wall -Wextra -Werror -Wno-deprecated -fno-strict-aliasing -fno-omit-frame-pointer -ggdb
EXTRA_LIBS= -lreadline -lconfig
LOCAL_LDFLAGS=-lm -lcrypto -lz -lssl -lrt ${EXTRA_LIBS}
EXTRA_LIBS= -lreadline -lrt -lconfig
LOCAL_LDFLAGS=-lm -lcrypto -lz -lssl -rdynamic ${EXTRA_LIBS}
LINK_FLAGS=${LDFLAGS} ${LOCAL_LDFLAGS}
HEADERS= ${srcdir}/constants.h ${srcdir}/include.h ${srcdir}/interface.h ${srcdir}/LICENSE.h ${srcdir}/loop.h ${srcdir}/mtproto-client.h ${srcdir}/mtproto-common.h ${srcdir}/net.h ${srcdir}/no-preview.h ${srcdir}/queries.h ${srcdir}/structures.h ${srcdir}/telegram.h ${srcdir}/tree.h ${srcdir}/config.h

View File

@ -4,10 +4,10 @@ CFLAGS=@CFLAGS@
LDFLAGS=@LDFLAGS@
CPPFLAGS=@CPPFLAGS@
DEFS=@DEFS@
COMPILE_FLAGS=${CFLAGS} ${CPPFLAGS} ${DEFS} -Wall -Wextra -Werror -fno-strict-aliasing -fno-omit-frame-pointer -ggdb
COMPILE_FLAGS=${CFLAGS} ${CPPFLAGS} ${DEFS} -Wall -Wextra -Werror -Wno-deprecated -fno-strict-aliasing -fno-omit-frame-pointer -ggdb
EXTRA_LIBS=@EXTRA_LIBS@
LOCAL_LDFLAGS=-lm -lcrypto -lz -lssl -lrt ${EXTRA_LIBS}
LOCAL_LDFLAGS=-lm -lcrypto -lz -lssl -rdynamic ${EXTRA_LIBS}
LINK_FLAGS=${LDFLAGS} ${LOCAL_LDFLAGS}
HEADERS= ${srcdir}/constants.h ${srcdir}/include.h ${srcdir}/interface.h ${srcdir}/LICENSE.h ${srcdir}/loop.h ${srcdir}/mtproto-client.h ${srcdir}/mtproto-common.h ${srcdir}/net.h ${srcdir}/no-preview.h ${srcdir}/queries.h ${srcdir}/structures.h ${srcdir}/telegram.h ${srcdir}/tree.h ${srcdir}/config.h

View File

@ -1,22 +1,22 @@
# This is my real config
# Feel free to edit it
default_profile = "tele2"
default_profile = "tele2";
mts = {
test = false
config_directory = ".telegram/mts"
msg_num = true
}
test = false;
config_directory = ".telegram/mts";
msg_num = true;
};
tele2 = {
test = false
config_directory = ".telegram/tele2"
msg_num = true
}
test = false;
config_directory = ".telegram/tele2";
msg_num = true;
};
test = {
test = true
config_directory = ".telegram/test"
msg_num = true
}
test = true;
config_directory = ".telegram/test";
msg_num = true;
};

43
configure vendored
View File

@ -3127,6 +3127,49 @@ $as_echo "#define READLINE_EDIT 1" >>confdefs.h
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for clock_gettime in -lrt" >&5
$as_echo_n "checking for clock_gettime in -lrt... " >&6; }
if ${ac_cv_lib_rt_clock_gettime+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lrt $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
/* Override any GCC internal prototype to avoid an error.
Use char because int might match the return type of a GCC
builtin and then its argument prototype would still apply. */
#ifdef __cplusplus
extern "C"
#endif
char clock_gettime ();
int
main ()
{
return clock_gettime ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
ac_cv_lib_rt_clock_gettime=yes
else
ac_cv_lib_rt_clock_gettime=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_rt_clock_gettime" >&5
$as_echo "$ac_cv_lib_rt_clock_gettime" >&6; }
if test "x$ac_cv_lib_rt_clock_gettime" = xyes; then :
EXTRA_LIBS+=" -lrt" ;
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking Checking for libconfig" >&5
$as_echo_n "checking Checking for libconfig... " >&6; }
# Check whether --enable-libconfig was given.

View File

@ -24,6 +24,14 @@ AC_CHECK_LIB([readline], [rl_save_prompt],
]
)
AC_CHECK_LIB([rt], [clock_gettime],
[
[ EXTRA_LIBS+=" -lrt" ; ]
],
[
]
)
AC_MSG_CHECKING([Checking for libconfig])
AC_ARG_ENABLE(libconfig,[--enable-libconfig/--disable-libconfig],
[

View File

@ -30,8 +30,8 @@
#include <readline/readline.h>
#include <readline/history.h>
#else
#include <editline/readline.h>
#include <editline/history.h>
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include "include.h"
@ -48,6 +48,7 @@ int unread_messages;
int msg_num_mode;
int in_readline;
int readline_active;
long long cur_uploading_bytes;
long long cur_uploaded_bytes;
@ -236,7 +237,9 @@ void set_prompt (const char *s) {
void update_prompt (void) {
print_start ();
set_prompt (get_default_prompt ());
if (readline_active) {
rl_redisplay ();
}
print_end ();
}

4
loop.c
View File

@ -28,8 +28,8 @@
#include <readline/readline.h>
#include <readline/history.h>
#else
#include <editline/readline.h>
#include <editline/history.h>
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include <errno.h>

9
main.c
View File

@ -44,6 +44,7 @@
#include "loop.h"
#include "mtproto-client.h"
#include "interface.h"
#define PROGNAME "telegram-client"
#define VERSION "0.01"
@ -163,7 +164,7 @@ void running_for_first_time (void) {
assert (asprintf (&config_filename, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, CONFIG_FILE) >= 0);
config_filename = make_full_path (config_filename);
struct stat *config_file_stat = NULL;
static struct stat config_file_stat;
int config_file_fd;
char *config_directory = get_config_directory ();
//char *downloads_directory = get_downloads_directory ();
@ -173,17 +174,13 @@ void running_for_first_time (void) {
}
// see if config file is there
if (stat (config_filename, config_file_stat) != 0) {
if (stat (config_filename, &config_file_stat) != 0) {
// config file missing, so touch it
config_file_fd = open (config_filename, O_CREAT | O_RDWR, 0600);
if (config_file_fd == -1) {
perror ("open[config_file]");
exit (EXIT_FAILURE);
}
if (fchmod (config_file_fd, CONFIG_DIRECTORY_MODE) != 0) {
perror ("fchmod[" CONFIG_FILE "]");
exit (EXIT_FAILURE);
}
if (write (config_file_fd, DEFAULT_CONFIG_CONTENTS, strlen (DEFAULT_CONFIG_CONTENTS)) <= 0) {
perror ("write[config_file]");
exit (EXIT_FAILURE);

View File

@ -72,13 +72,8 @@ long long precise_time;
long long precise_time_rdtsc;
double get_utime (int clock_id) {
struct timespec T;
#if _POSIX_TIMERS
assert (clock_gettime (clock_id, &T) >= 0);
my_clock_gettime (clock_id, &T);
double res = T.tv_sec + (double) T.tv_nsec * 1e-9;
#else
#error "No high-precision clock"
double res = time ();
#endif
if (clock_id == CLOCK_REALTIME) {
precise_time = (long long) (res * (1LL << 32));
precise_time_rdtsc = rdtsc ();
@ -835,6 +830,7 @@ void work_update (struct connection *c UU, long long msg_id UU) {
{
peer_id_t user_id = MK_USER (fetch_int ());
peer_t *UC = user_chat_get (user_id);
fetch_date ();
if (UC) {
struct user *U = &UC->user;
@ -852,6 +848,7 @@ void work_update (struct connection *c UU, long long msg_id UU) {
U->photo_small.dc = -2;
} else {
assert (y == CODE_user_profile_photo);
fetch_long (); // photo_id
fetch_file_location (&U->photo_small);
fetch_file_location (&U->photo_big);
}
@ -865,6 +862,7 @@ void work_update (struct connection *c UU, long long msg_id UU) {
fetch_file_location (&t);
}
}
fetch_bool ();
}
break;
case CODE_update_restore_messages:
@ -1209,7 +1207,7 @@ void work_rpc_result (struct connection *c UU, long long msg_id UU) {
}
}
#define MAX_PACKED_SIZE (1 << 20)
#define MAX_PACKED_SIZE (1 << 24)
void work_packed (struct connection *c, long long msg_id) {
assert (fetch_int () == CODE_gzip_packed);
static int in_gzip;
@ -1412,25 +1410,35 @@ int rpc_execute (struct connection *c, int op, int len) {
logprintf ( "have %d Response bytes\n", Response_len);
}
#ifndef __MACH__
setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
#endif
int o = c_state;
if (GET_DC(c)->flags & 1) { o = st_authorized;}
switch (o) {
case st_reqpq_sent:
process_respq_answer (c, Response/* + 8*/, Response_len/* - 12*/);
#ifndef __MACH__
setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
#endif
return 0;
case st_reqdh_sent:
process_dh_answer (c, Response/* + 8*/, Response_len/* - 12*/);
#ifndef __MACH__
setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
#endif
return 0;
case st_client_dh_sent:
process_auth_complete (c, Response/* + 8*/, Response_len/* - 12*/);
#ifndef __MACH__
setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
#endif
return 0;
case st_authorized:
process_rpc_message (c, (void *)(Response/* + 8*/), Response_len/* - 12*/);
#ifndef __MACH__
setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
#endif
return 0;
default:
logprintf ( "fatal: cannot receive answer in state %d\n", c_state);
@ -1456,7 +1464,9 @@ int tc_becomes_ready (struct connection *c) {
assert (write_out (c, &byte, 1) == 1);
flush_out (c);
#ifndef __MACH__
setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
#endif
int o = c_state;
if (GET_DC(c)->flags & 1) { o = st_authorized; }
switch (o) {

View File

@ -36,6 +36,13 @@
#include "mtproto-common.h"
#include "interface.h"
#include "include.h"
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
int __packet_buffer[PACKET_BUFFER_SIZE], *packet_ptr;
int *packet_buffer = __packet_buffer + 16;
@ -78,13 +85,28 @@ int get_random_bytes (void *buf, int n) {
return r;
}
void my_clock_gettime (int clock_id UU, struct timespec *T) {
#ifdef __MACH__
// We are ignoring MONOTONIC and hope time doesn't go back to often
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
T->tv_sec = mts.tv_sec;
T->tv_nsec = mts.tv_nsec;
#else
assert (clock_gettime(clock_id, T) >= 0);
#endif
}
void prng_seed (const char *password_filename, int password_length) {
unsigned char *a = calloc (64 + password_length, 1);
assert (a != NULL);
long long r = rdtsc ();
struct timespec T;
assert (clock_gettime(CLOCK_REALTIME, &T) >= 0);
my_clock_gettime (CLOCK_REALTIME, &T);
memcpy (a, &T.tv_sec, 4);
memcpy (a+4, &T.tv_nsec, 4);
memcpy (a+8, &r, 8);

View File

@ -385,4 +385,10 @@ static inline void hexdump_in (void) {
static inline void hexdump_out (void) {
hexdump (packet_buffer, packet_ptr);
}
#ifdef __MACH__
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 1
#endif
void my_clock_gettime (int clock_id, struct timespec *T);
#endif

4
net.c
View File

@ -37,6 +37,10 @@
#include "tree.h"
#include "interface.h"
#ifdef __MACH__
#define POLLRDHUP 0
#endif
DEFINE_TREE(int,int,int_cmp,0)
int verbosity;

View File

@ -64,7 +64,7 @@ struct tree_query *queries_tree;
double get_double_time (void) {
struct timespec tv;
clock_gettime (CLOCK_REALTIME, &tv);
my_clock_gettime (CLOCK_REALTIME, &tv);
return tv.tv_sec + 1e-9 * tv.tv_nsec;
}
@ -179,7 +179,7 @@ void query_error (long long id) {
}
}
#define MAX_PACKED_SIZE (1 << 20)
#define MAX_PACKED_SIZE (1 << 24)
static int packed_buffer[MAX_PACKED_SIZE / 4];
void query_result (long long id UU) {

View File

@ -456,8 +456,10 @@ void fetch_photo_size (struct photo_size *S) {
S->size = fetch_int ();
} else {
S->size = prefetch_strlen ();
S->data = malloc (S->size);
memcpy (S->data, fetch_str (S->size), S->size);
// S->data = malloc (S->size);
// assert (S->data);
fetch_str (S->size);
// memcpy (S->data, fetch_str (S->size), S->size);
}
}
}

2
tree.h
View File

@ -21,7 +21,9 @@
#include <stdio.h>
#include <memory.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <assert.h>
#define DEFINE_TREE(X_NAME, X_TYPE, X_CMP, X_UNSET) \