| 1 /* $Id$ */ |
|
| 2 |
|
| 3 /* |
|
| 4 * (C) Copyright 2009 Jakub Zawadzki <darkjames@darkjames.ath.cx> |
|
| 5 * |
|
| 6 * This program is free software; you can redistribute it and/or modify |
|
| 7 * it under the terms of the GNU Lesser General Public License Version |
|
| 8 * 2.1 as published by the Free Software Foundation. |
|
| 9 * |
|
| 10 * This program is distributed in the hope that it will be useful, |
|
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 13 * GNU Lesser General Public License for more details. |
|
| 14 * |
|
| 15 * You should have received a copy of the GNU Lesser General Public |
|
| 16 * License along with this program; if not, write to the Free Software |
|
| 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, |
|
| 18 * USA. |
|
| 19 */ |
|
| 20 |
|
| 21 #ifndef LIBGADU_INTERNAL_H |
|
| 22 #define LIBGADU_INTERNAL_H |
|
| 23 |
|
| 24 #include "libgadu.h" |
|
| 25 |
|
| 26 #define GG_DEFAULT_CLIENT_VERSION_100 "10.1.0.11070" |
|
| 27 #define GG_DEFAULT_CLIENT_VERSION_110 "11.3.45.10771" |
|
| 28 |
|
| 29 #ifdef _WIN32 |
|
| 30 # ifdef __COVERITY__ |
|
| 31 # define GG_SIZE_FMT "lu" |
|
| 32 # define _GG_INT64_MODIFIER "ll" |
|
| 33 # undef PRIu64 |
|
| 34 # undef PRIx64 |
|
| 35 # undef PRId64 |
|
| 36 # else |
|
| 37 # define GG_SIZE_FMT "Iu" |
|
| 38 # define _GG_INT64_MODIFIER "I64" |
|
| 39 # endif |
|
| 40 #elif defined(_LP64) |
|
| 41 # define GG_SIZE_FMT "zu" |
|
| 42 # define _GG_INT64_MODIFIER "l" |
|
| 43 #else |
|
| 44 # define GG_SIZE_FMT "zu" |
|
| 45 # define _GG_INT64_MODIFIER "ll" |
|
| 46 #endif |
|
| 47 |
|
| 48 #ifndef PRIu64 |
|
| 49 # define PRIu64 _GG_INT64_MODIFIER "u" |
|
| 50 #endif |
|
| 51 #ifndef PRIx64 |
|
| 52 # define PRIx64 _GG_INT64_MODIFIER "x" |
|
| 53 #endif |
|
| 54 #ifndef PRId64 |
|
| 55 # define PRId64 _GG_INT64_MODIFIER "d" |
|
| 56 #endif |
|
| 57 |
|
| 58 #define GG_LOGIN_PARAMS_HAS_FIELD(glp, member) \ |
|
| 59 (offsetof(struct gg_login_params, member) < (glp)->struct_size || \ |
|
| 60 offsetof(struct gg_login_params, member) <= offsetof(struct gg_login_params, struct_size)) |
|
| 61 |
|
| 62 #ifdef __GNUC__ |
|
| 63 # define GG_UNUSED __attribute__ ((unused)) |
|
| 64 # define GG_NORETURN __attribute__ ((noreturn)) |
|
| 65 # define GG_CDECL __attribute__ ((__cdecl__)) |
|
| 66 #else |
|
| 67 # define GG_UNUSED |
|
| 68 # define GG_NORETURN |
|
| 69 # define GG_CDECL |
|
| 70 #endif |
|
| 71 |
|
| 72 #define GG_STATIC_ASSERT(condition, message) \ |
|
| 73 { typedef char static_assertion_failed_ ## message \ |
|
| 74 [(condition) ? 1 : -1]; static_assertion_failed_ ## message dummy; \ |
|
| 75 (void)dummy; } |
|
| 76 |
|
| 77 #define GG_IMGOUT_WAITING_MAX 4 |
|
| 78 |
|
| 79 struct gg_dcc7_relay { |
|
| 80 uint32_t addr; |
|
| 81 uint16_t port; |
|
| 82 uint8_t family; |
|
| 83 }; |
|
| 84 |
|
| 85 typedef struct _gg_chat_list gg_chat_list_t; |
|
| 86 struct _gg_chat_list { |
|
| 87 uint64_t id; |
|
| 88 uint32_t version; |
|
| 89 uint32_t participants_count; |
|
| 90 uin_t *participants; |
|
| 91 |
|
| 92 gg_chat_list_t *next; |
|
| 93 }; |
|
| 94 |
|
| 95 typedef struct _gg_msg_list gg_msg_list_t; |
|
| 96 struct _gg_msg_list { |
|
| 97 int seq; |
|
| 98 uin_t *recipients; |
|
| 99 size_t recipients_count; |
|
| 100 |
|
| 101 gg_msg_list_t *next; |
|
| 102 }; |
|
| 103 |
|
| 104 typedef struct _gg_eventqueue gg_eventqueue_t; |
|
| 105 struct _gg_eventqueue { |
|
| 106 struct gg_event *event; |
|
| 107 |
|
| 108 gg_eventqueue_t *next; |
|
| 109 }; |
|
| 110 |
|
| 111 typedef struct _gg_imgout_queue_t gg_imgout_queue_t; |
|
| 112 struct _gg_imgout_queue_t { |
|
| 113 struct gg_send_msg msg_hdr; |
|
| 114 char buf[1910]; |
|
| 115 size_t buf_len; |
|
| 116 |
|
| 117 gg_imgout_queue_t *next; |
|
| 118 }; |
|
| 119 |
|
| 120 struct gg_session_private { |
|
| 121 gg_compat_t compatibility; |
|
| 122 |
|
| 123 gg_chat_list_t *chat_list; |
|
| 124 gg_msg_list_t *sent_messages; |
|
| 125 |
|
| 126 gg_eventqueue_t *event_queue; |
|
| 127 int check_after_queue; |
|
| 128 int fd_after_queue; |
|
| 129 |
|
| 130 gg_imgout_queue_t *imgout_queue; |
|
| 131 int imgout_waiting_ack; |
|
| 132 |
|
| 133 gg_socket_manager_type_t socket_manager_type; |
|
| 134 gg_socket_manager_t socket_manager; |
|
| 135 void *socket_handle; |
|
| 136 int socket_next_state; |
|
| 137 int socket_is_external; |
|
| 138 enum gg_failure_t socket_failure; |
|
| 139 |
|
| 140 int time_diff; |
|
| 141 |
|
| 142 int dummyfds_created; |
|
| 143 int dummyfds[2]; |
|
| 144 |
|
| 145 char **host_white_list; |
|
| 146 }; |
|
| 147 |
|
| 148 typedef enum |
|
| 149 { |
|
| 150 GG_COMPAT_FEATURE_ACK_EVENT, |
|
| 151 GG_COMPAT_FEATURE_LEGACY_CONFER |
|
| 152 } gg_compat_feature_t; |
|
| 153 |
|
| 154 typedef struct gg_dcc7_relay gg_dcc7_relay_t; |
|
| 155 |
|
| 156 void * gg_new0(size_t size); |
|
| 157 int gg_required_proto(struct gg_session *gs, int protocol_version); |
|
| 158 int gg_get_dummy_fd(struct gg_session *sess); |
|
| 159 |
|
| 160 int gg_compat_feature_is_enabled(struct gg_session *sess, gg_compat_feature_t feature); |
|
| 161 |
|
| 162 int gg_pubdir50_handle_reply_sess(struct gg_session *sess, struct gg_event *e, const char *packet, int length); |
|
| 163 |
|
| 164 int gg_resolve(int *fd, int *pid, const char *hostname); |
|
| 165 int gg_resolve_pthread(int *fd, void **resolver, const char *hostname); |
|
| 166 void gg_resolve_pthread_cleanup(void *resolver, int kill); |
|
| 167 |
|
| 168 int gg_login_hash_sha1_2(const char *password, uint32_t seed, uint8_t *result); |
|
| 169 |
|
| 170 int gg_chat_update(struct gg_session *sess, uint64_t id, uint32_t version, |
|
| 171 const uin_t *participants, unsigned int participants_count); |
|
| 172 gg_chat_list_t *gg_chat_find(struct gg_session *sess, uint64_t id); |
|
| 173 |
|
| 174 uin_t gg_str_to_uin(const char *str, int len); |
|
| 175 |
|
| 176 uint64_t gg_fix64(uint64_t x); |
|
| 177 void gg_connection_failure(struct gg_session *gs, struct gg_event *ge, |
|
| 178 enum gg_failure_t failure); |
|
| 179 |
|
| 180 time_t gg_server_time(struct gg_session *gs); |
|
| 181 |
|
| 182 int gg_session_init_ssl(struct gg_session *gs); |
|
| 183 void gg_close(struct gg_session *gs); |
|
| 184 |
|
| 185 struct gg_event *gg_eventqueue_add(struct gg_session *sess); |
|
| 186 |
|
| 187 void gg_compat_message_ack(struct gg_session *sess, int seq); |
|
| 188 |
|
| 189 void gg_image_sendout(struct gg_session *sess); |
|
| 190 |
|
| 191 void gg_strarr_free(char **strarr); |
|
| 192 char ** gg_strarr_dup(char **strarr); |
|
| 193 |
|
| 194 #ifdef _WIN32 |
|
| 195 |
|
| 196 #include <windows.h> |
|
| 197 |
|
| 198 typedef struct { |
|
| 199 void (*fnc)(); |
|
| 200 #ifdef _WIN64 |
|
| 201 uint8_t trap[12]; |
|
| 202 uint8_t original[12]; |
|
| 203 #else |
|
| 204 uint8_t trap[7]; |
|
| 205 uint8_t original[7]; |
|
| 206 #endif |
|
| 207 } gg_win32_hook_data_t; |
|
| 208 |
|
| 209 #define gg_win32_hook(orig_func, hook_func, data) \ |
|
| 210 gg_win32_hook_f((void (*)())(orig_func), (void (*)())(hook_func), (data)) |
|
| 211 |
|
| 212 static inline void |
|
| 213 gg_win32_hook_f(void (*orig_func)(), void (*hook_func)(), gg_win32_hook_data_t *data) |
|
| 214 { |
|
| 215 DWORD dPermission; |
|
| 216 uint8_t trap[] = { |
|
| 217 #ifdef _WIN64 |
|
| 218 0x48, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, /* mov rax, uint64_t */ |
|
| 219 0xff, 0xe0 /* jmp rax */ |
|
| 220 #else |
|
| 221 0xB8, 0, 0, 0, 0, /* mov eax, uint32_t */ |
|
| 222 0xff, 0xe0 /* jmp eax */ |
|
| 223 #endif |
|
| 224 }; |
|
| 225 |
|
| 226 #ifdef _WIN64 |
|
| 227 uint64_t addr = (uint64_t)hook_func; |
|
| 228 memcpy(&trap[2], &addr, sizeof(addr)); |
|
| 229 #else |
|
| 230 uint32_t addr = (uint32_t)hook_func; |
|
| 231 memcpy(&trap[1], &addr, sizeof(addr)); |
|
| 232 #endif |
|
| 233 |
|
| 234 VirtualProtect(orig_func, sizeof(trap), |
|
| 235 PAGE_EXECUTE_READWRITE, &dPermission); |
|
| 236 if (data != NULL) { |
|
| 237 data->fnc = orig_func; |
|
| 238 memcpy(data->trap, trap, sizeof(trap)); |
|
| 239 memcpy(data->original, orig_func, sizeof(trap)); |
|
| 240 } |
|
| 241 memcpy(orig_func, trap, sizeof(trap)); |
|
| 242 VirtualProtect(orig_func, sizeof(trap), |
|
| 243 dPermission, &dPermission); |
|
| 244 } |
|
| 245 |
|
| 246 static inline void |
|
| 247 gg_win32_hook_set_enabled(gg_win32_hook_data_t *data, int enabled) |
|
| 248 { |
|
| 249 DWORD dPermission; |
|
| 250 uint8_t *src; |
|
| 251 |
|
| 252 if (enabled) |
|
| 253 src = data->trap; |
|
| 254 else |
|
| 255 src = data->original; |
|
| 256 |
|
| 257 VirtualProtect(data->fnc, sizeof(data->trap), |
|
| 258 PAGE_EXECUTE_READWRITE, &dPermission); |
|
| 259 memcpy(data->fnc, src, sizeof(data->trap)); |
|
| 260 VirtualProtect(data->fnc, sizeof(data->trap), |
|
| 261 dPermission, &dPermission); |
|
| 262 } |
|
| 263 |
|
| 264 #endif /* _WIN32 */ |
|
| 265 |
|
| 266 #endif /* LIBGADU_INTERNAL_H */ |
|