| 1 | /* |
| 2 | This file is part of Mtproto-proxy Library. |
| 3 | |
| 4 | Mtproto-proxy Library is free software: you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published by |
| 6 | the Free Software Foundation, either version 2 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | Mtproto-proxy Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with Mtproto-proxy Library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | Copyright 2013 Vkontakte Ltd |
| 18 | 2013 Vitaliy Valtman |
| 19 | 2013 Anton Maydell |
| 20 | |
| 21 | Copyright 2014 Telegram Messenger Inc |
| 22 | 2014 Vitaly Valtman |
| 23 | 2014 Anton Maydell |
| 24 | |
| 25 | Copyright 2015-2016 Telegram Messenger Inc |
| 26 | 2015-2016 Vitaliy Valtman |
| 27 | */ |
| 28 | |
| 29 | #pragma once |
| 30 | |
| 31 | // GLIBC DEFINES RTMAX as function |
| 32 | // engine_init () asserts, that OUT_SIGRTMAX == SIGRTMAX |
| 33 | #define OUR_SIGRTMAX 64 |
| 34 | |
| 35 | #include "common/common-stats.h" |
| 36 | #include "engine/engine-rpc.h" |
| 37 | #include "common/tl-parse.h" |
| 38 | |
| 39 | #include "net/net-connections.h" |
| 40 | #include "net/net-tcp-rpc-server.h" |
| 41 | #include "net/net-http-server.h" |
| 42 | |
| 43 | #define DEFAULT_LONG_QUERY_THRES 0.1 |
| 44 | |
| 45 | #define SIG2INT(sig) (((sig) == 64) ? 1ull : (1ull << (unsigned long long)(sig))) |
| 46 | #define SIG_INTERRUPT_MASK (SIG2INT(SIGTERM) | SIG2INT(SIGINT)) |
| 47 | |
| 48 | extern double precise_now_diff; |
| 49 | |
| 50 | #pragma pack(push,4) |
| 51 | |
| 52 | struct rpc_custom_op { |
| 53 | unsigned op; |
| 54 | void (*func)(struct tl_in_state *tlio_in, struct query_work_params *params); |
| 55 | }; |
| 56 | |
| 57 | #pragma pack(pop) |
| 58 | |
| 59 | #define ENGINE_NO_AUTO_APPEND 2 |
| 60 | #define ENGINE_NO_PORT 4 |
| 61 | |
| 62 | #define ENGINE_ENABLE_IPV6 0x4ull |
| 63 | #define ENGINE_ENABLE_TCP 0x10ull |
| 64 | #define ENGINE_ENABLE_MULTITHREAD 0x1000000ull |
| 65 | #define ENGINE_ENABLE_SLAVE_MODE 0x2000000ull |
| 66 | |
| 67 | #define ENGINE_DEFAULT_ENABLED_MODULES (ENGINE_ENABLE_TCP) |
| 68 | |
| 69 | typedef struct { |
| 70 | void (*cron) (void); |
| 71 | void (*precise_cron) (void); |
| 72 | void (*on_exit) (void); |
| 73 | int (*on_waiting_exit) (void); //returns 0 -> stop wait and exit, X > 0 wait X microsenconds */ |
| 74 | void (*on_safe_quit) (void); |
| 75 | |
| 76 | void (*close_net_sockets) (void); |
| 77 | |
| 78 | unsigned long long flags; |
| 79 | unsigned long long allowed_signals; |
| 80 | unsigned long long forbidden_signals; |
| 81 | unsigned long long default_modules; |
| 82 | unsigned long long default_modules_disabled; |
| 83 | |
| 84 | void (*prepare_stats)(stats_buffer_t *sb); |
| 85 | |
| 86 | void (*prepare_parse_options)(void); |
| 87 | int (*parse_option)(int val); |
| 88 | void (*)(int count, char *args[]); |
| 89 | |
| 90 | void (*pre_init)(void); |
| 91 | |
| 92 | void (*pre_start)(void); |
| 93 | |
| 94 | void (*pre_loop)(void); |
| 95 | int (*run_script)(void); |
| 96 | |
| 97 | const char *FullVersionStr; |
| 98 | const char *ShortVersionStr; |
| 99 | |
| 100 | int epoll_timeout; |
| 101 | double aio_timeout; |
| 102 | |
| 103 | struct tl_act_extra *(*parse_function) (struct tl_in_state *tlio_in, long long actor_id); |
| 104 | int (*get_op)(struct tl_in_state *tlio_in); |
| 105 | |
| 106 | void (*signal_handlers[65])(void); |
| 107 | struct rpc_custom_op *custom_ops; |
| 108 | |
| 109 | struct tcp_rpc_server_functions *tcp_methods; |
| 110 | |
| 111 | conn_type_t *http_type; |
| 112 | struct http_server_functions *http_functions; |
| 113 | |
| 114 | int cron_subclass; |
| 115 | int precise_cron_subclass; |
| 116 | } server_functions_t; |
| 117 | |
| 118 | typedef struct { |
| 119 | struct in_addr settings_addr; |
| 120 | int do_not_open_port; |
| 121 | int epoll_wait_timeout; |
| 122 | int sfd; |
| 123 | |
| 124 | unsigned long long modules; |
| 125 | int port; |
| 126 | int start_port, end_port; |
| 127 | |
| 128 | int backlog; |
| 129 | int maxconn; |
| 130 | int required_io_threads; |
| 131 | int required_cpu_threads; |
| 132 | int required_tcp_cpu_threads; |
| 133 | int required_tcp_io_threads; |
| 134 | |
| 135 | char *aes_pwd_file; |
| 136 | |
| 137 | server_functions_t *F; |
| 138 | } engine_t; |
| 139 | |
| 140 | typedef struct event_precise_cron { |
| 141 | struct event_precise_cron *next, *prev; |
| 142 | void (*wakeup)(struct event_precise_cron *arg); |
| 143 | } event_precise_cron_t; |
| 144 | |
| 145 | void precise_cron_function_insert (struct event_precise_cron *ev); |
| 146 | void precise_cron_function_remove (struct event_precise_cron *ev); |
| 147 | |
| 148 | void set_signals_handlers (void); |
| 149 | void engine_init (const char *const pwd_filename, int do_not_open_port); |
| 150 | void engine_set_epoll_wait_timeout (int epoll_wait_timeout); |
| 151 | int signal_check_pending_and_clear (int sig); |
| 152 | int signal_check_pending (int sig); |
| 153 | void signal_set_pending (int sig); |
| 154 | |
| 155 | #define ENGINE_FLAG_PARAM(name,flag) \ |
| 156 | static inline void engine_enable_ ## name (void) { engine_state->modules |= ENGINE_ENABLE_ ## flag; } \ |
| 157 | static inline void engine_disable_ ## name (void) { engine_state->modules &= ~ENGINE_ENABLE_ ## flag; } \ |
| 158 | static inline int engine_check_ ## name ## _enabled (void) { return (engine_state->modules & ENGINE_ENABLE_ ## flag) != 0; } \ |
| 159 | static inline int engine_check_ ## name ## _disabled (void) { return (engine_state->modules & ENGINE_ENABLE_ ## flag) == 0; } \ |
| 160 | |
| 161 | #define ENGINE_STR_PARAM(name,field) \ |
| 162 | static inline void engine_set_ ## name (const char *s) { \ |
| 163 | if (engine_state->field) { \ |
| 164 | free (engine_state->field); \ |
| 165 | } \ |
| 166 | engine_state->field = s ? strdup (s) : NULL; \ |
| 167 | } \ |
| 168 | static inline const char *engine_get_ ## name (void) { \ |
| 169 | return engine_state->field; \ |
| 170 | } \ |
| 171 | |
| 172 | |
| 173 | #define ENGINE_T_PARAM(type,name,field) \ |
| 174 | static inline void engine_set_ ## name (type s) { \ |
| 175 | engine_state->field = s;\ |
| 176 | } \ |
| 177 | static inline type engine_get_ ## name (void) { \ |
| 178 | return engine_state->field; \ |
| 179 | } \ |
| 180 | |
| 181 | #define ENGINE_INT_PARAM(name,field) ENGINE_T_PARAM(int,name,field) |
| 182 | #define ENGINE_DOUBLE_PARAM(name,field) ENGINE_T_PARAM(double,name,field) |
| 183 | #define ENGINE_LONG_PARAM(name,field) ENGINE_T_PARAM(long long,name,field) |
| 184 | |
| 185 | extern engine_t *engine_state; |
| 186 | |
| 187 | ENGINE_FLAG_PARAM(ipv6,IPV6) |
| 188 | ENGINE_FLAG_PARAM(tcp,TCP) |
| 189 | |
| 190 | ENGINE_FLAG_PARAM(multithread,MULTITHREAD) |
| 191 | ENGINE_FLAG_PARAM(slave_mode,SLAVE_MODE) |
| 192 | |
| 193 | ENGINE_STR_PARAM(aes_pwd_file,aes_pwd_file) |
| 194 | |
| 195 | ENGINE_INT_PARAM(backlog,backlog); |
| 196 | ENGINE_INT_PARAM(required_io_threads,required_io_threads); |
| 197 | ENGINE_INT_PARAM(required_cpu_threads,required_cpu_threads); |
| 198 | ENGINE_INT_PARAM(required_tcp_cpu_threads,required_tcp_cpu_threads); |
| 199 | ENGINE_INT_PARAM(required_tcp_io_threads,required_tcp_io_threads); |
| 200 | |
| 201 | void reopen_logs (void); |
| 202 | int default_main (server_functions_t *F, int argc, char *argv[]); |
| 203 | void (int argc, char *argv[]); |
| 204 | void engine_tl_init (struct tl_act_extra *(*parse)(struct tl_in_state *,long long), void (*stat)(), int (get_op)(struct tl_in_state *), double timeout, const char *name); |
| 205 | void server_init (conn_type_t *listen_connection_type, void *); |
| 206 | void usage (void); |
| 207 | |
| 208 | extern engine_t *engine_state; |
| 209 | |