From 65edc1789b4a4e9e440edda642690b9208d8caa0 Mon Sep 17 00:00:00 2001 From: vysheng Date: Mon, 11 Nov 2013 15:34:45 +0400 Subject: [PATCH 1/2] Maybe it would compile on mac now --- Makefile | 6 +++--- Makefile.in | 4 ++-- configure | 43 +++++++++++++++++++++++++++++++++++++++++++ configure.ac | 8 ++++++++ interface.c | 9 ++++++--- loop.c | 4 ++-- mtproto-client.c | 22 ++++++++++++++++------ mtproto-common.c | 24 +++++++++++++++++++++++- mtproto-common.h | 6 ++++++ net.c | 4 ++++ queries.c | 2 +- tree.h | 2 ++ 12 files changed, 116 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 52d0e3e..8d7759a 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/Makefile.in b/Makefile.in index 89a0ab9..cf92f28 100644 --- a/Makefile.in +++ b/Makefile.in @@ -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 diff --git a/configure b/configure index 4bb34ea..49c45d3 100755 --- a/configure +++ b/configure @@ -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. diff --git a/configure.ac b/configure.ac index f319a33..a1d2d47 100644 --- a/configure.ac +++ b/configure.ac @@ -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], [ diff --git a/interface.c b/interface.c index 28cc5dd..3d87912 100644 --- a/interface.c +++ b/interface.c @@ -30,8 +30,8 @@ #include #include #else -#include -#include +#include +#include #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 ()); - rl_redisplay (); + if (readline_active) { + rl_redisplay (); + } print_end (); } diff --git a/loop.c b/loop.c index 15821f1..4bb3515 100644 --- a/loop.c +++ b/loop.c @@ -28,8 +28,8 @@ #include #include #else -#include -#include +#include +#include #endif #include diff --git a/mtproto-client.c b/mtproto-client.c index 23b4063..c538e44 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -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: @@ -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) { diff --git a/mtproto-common.c b/mtproto-common.c index 201ab68..ab3614e 100644 --- a/mtproto-common.c +++ b/mtproto-common.c @@ -36,6 +36,13 @@ #include "mtproto-common.h" #include "interface.h" +#include "include.h" + +#ifdef __MACH__ +#include +#include +#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); diff --git a/mtproto-common.h b/mtproto-common.h index 94c97ac..df696cc 100644 --- a/mtproto-common.h +++ b/mtproto-common.h @@ -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 diff --git a/net.c b/net.c index a17e3bb..4db2677 100644 --- a/net.c +++ b/net.c @@ -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; diff --git a/queries.c b/queries.c index 74b01c9..61664b8 100644 --- a/queries.c +++ b/queries.c @@ -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; } diff --git a/tree.h b/tree.h index 8f38e17..d5cdb73 100644 --- a/tree.h +++ b/tree.h @@ -21,7 +21,9 @@ #include #include +#ifdef HAVE_MALLOC_H #include +#endif #include #define DEFINE_TREE(X_NAME, X_TYPE, X_CMP, X_UNSET) \ From e80c87c9bd02de852fa2d13467774617035ea184 Mon Sep 17 00:00:00 2001 From: vysheng Date: Mon, 11 Nov 2013 22:35:31 +0400 Subject: [PATCH 2/2] Fixed bug in read config --- config.sample | 26 +++++++++++++------------- main.c | 9 +++------ mtproto-client.c | 2 +- queries.c | 2 +- structures.c | 6 ++++-- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/config.sample b/config.sample index 4415c46..588090f 100644 --- a/config.sample +++ b/config.sample @@ -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; +}; diff --git a/main.c b/main.c index 4fbc5fb..7873210 100644 --- a/main.c +++ b/main.c @@ -44,6 +44,7 @@ #include "loop.h" #include "mtproto-client.h" +#include "interface.h" #define PROGNAME "telegram-client" #define VERSION "0.01" @@ -162,7 +163,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 (); @@ -172,17 +173,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); diff --git a/mtproto-client.c b/mtproto-client.c index c538e44..8435e34 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -1207,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; diff --git a/queries.c b/queries.c index 61664b8..33c2166 100644 --- a/queries.c +++ b/queries.c @@ -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) { diff --git a/structures.c b/structures.c index bad5cc3..2e7788f 100644 --- a/structures.c +++ b/structures.c @@ -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); } } }