This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
beehive.c
444 lines (396 loc) · 13 KB
/
beehive.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_beehive.h"
#ifndef _NTOHLL_H
#define _NTOHLL_H
#ifdef __WINDOWS__
#define ntohll(x) (((int64_t)(ntohl((int)((x << 32) >> 32))) << 32) | \
(unsigned int)ntohl(((int)(x >> 32)))) //By Runner
#define htonll(x) ntohll(x)
#else
#if __BYTE_ORDER == __BIG_ENDIAN
# define ntohll(x) (x)
# define htonll(x) (x)
#else
# if __BYTE_ORDER == __LITTLE_ENDIAN/* LB: removed that, didn't compile: && defined __bswap_64 */
# define ntohll(x) __bswap_64 (x)
# define htonll(x) __bswap_64 (x)
# else
# error No function/macros available to switch between uint64_t endianness.
# endif
#endif
#endif
#endif
#define BEEHIVE_FIXED_HEADER_LEN 28
#if PHP_MAJOR_VERSION < 7
static inline int beehive_zend_hash_find(HashTable *ht, char *k, int len, void **v)
{
zval **tmp = NULL;
if (zend_hash_find(ht, k, len, (void **) &tmp) == SUCCESS)
{
*v = *tmp;
return SUCCESS;
}
else
{
*v = NULL;
return FAILURE;
}
}
#else
static inline int beehive_zend_hash_find(HashTable *ht, char *k, int len, void **v)
{
zval *value = zend_hash_str_find(ht, k, len - 1);
if (value == NULL)
{
return FAILURE;
}
else
{
*v = (void *) value;
return SUCCESS;
}
}
#endif
#define php_beehive_array_get_value(ht, str, v) (beehive_zend_hash_find(ht, str, sizeof(str), (void **) &v) == SUCCESS && !ZVAL_IS_NULL(v))
/* If you declare any globals in php_beehive.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(beehive)
*/
/* True global resources - no need for thread safety here */
static int le_beehive;
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("beehive.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_beehive_globals, beehive_globals)
STD_PHP_INI_ENTRY("beehive.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_beehive_globals, beehive_globals)
PHP_INI_END()
*/
/* }}} */
/* Remove the following function when you have successfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_beehive_compiled(string arg)
Return a string to confirm that the module is compiled in */
typedef struct {
uint32_t packet_len; //定义包体长度,后续的包长度,不包括自身
uint16_t header_len; //定义包头长度后续到BODY的边界长度,不包括自身
uint16_t flag; //协议标记
uint32_t service; //服务id
uint32_t time; //调用时间(s)
uint32_t uniqid; //单机下调用时间下的唯一值
uint32_t askid; //请求编号,反端响应原路返回
uint16_t code; //协议结果0表示成功,其它失败,一般用于返回
uint8_t routers; //路由总数,用于标记路由的线路
uint8_t dst_mode; //目标查找模式 0--默认 1--HASH查找 2--指定目标
} Header;
PHP_FUNCTION(beehive_packet_unpack)
{
char *arg = NULL;
int arg_len, len;
zval *router_items;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE)
{
return;
}
array_init(return_value);
if (arg_len < 6)
{
return php_error_docref(NULL, E_WARNING, "packet lenght is to short!");
}
Header *header = (Header*) arg;
uint32_t packet_len = (uint32_t) ntohl(header->packet_len);
uint16_t header_len = (uint16_t) ntohs(header->header_len);
if (arg_len < packet_len)
{
return php_error_docref(NULL, E_WARNING, "packet header lenght is to short! %d -> %d-> %d", arg_len, packet_len, header->packet_len);
}
MAKE_STD_ZVAL(router_items);
array_init(router_items);
add_assoc_long(return_value, "packet_len", packet_len);
add_assoc_long(return_value, "header_len", header_len);
add_assoc_long(return_value, "flag", (uint16_t) ntohs(header->flag));
add_assoc_long(return_value, "service", (uint32_t) ntohl(header->service));
add_assoc_long(return_value, "time", (uint32_t) ntohl(header->time));
add_assoc_long(return_value, "uniqid", (uint32_t) ntohl(header->uniqid));
add_assoc_long(return_value, "askid", (uint32_t) ntohl(header->askid));
add_assoc_long(return_value, "code", (uint16_t) ntohs(header->code));
add_assoc_long(return_value, "routers", header->routers);
add_assoc_long(return_value, "dst_mode", header->dst_mode);
//开始解 routers;
uint64_t router_item = 0;
int start = BEEHIVE_FIXED_HEADER_LEN;
int i;
for (i = 0; i < header->routers; i ++)
{
router_item = *(uint64_t*) (arg + start);
router_item = (uint64_t) ntohll(router_item);
start = start + 8;
add_next_index_long(router_items, router_item);
}
uint64_t dst = 0;
if (header->dst_mode > 0)
{
dst = *(uint64_t*) (arg + start);
dst = (uint64_t) ntohll(dst);
start = start + 8;
}
add_assoc_long(return_value, "dst", dst);
add_assoc_zval(return_value, "routers_list", router_items);
#if PHP_MAJOR_VERSION < 7
add_assoc_stringl(return_value, "body", arg + start, packet_len - header_len - 2, 1);
#else
add_assoc_stringl(return_value, "body", arg + start, packet_len - header_len - 2);
#endif
}
PHP_FUNCTION(beehive_packet_pack)
{
zval *zset = NULL;
HashTable *vht;
zval *v;
zval **data;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zset) == FAILURE)
{
return;
}
vht = Z_ARRVAL_P(zset);
Header header;
memset(&header, 0, sizeof(header));
if (php_beehive_array_get_value(vht, "flag", v))
{
convert_to_long(v);
header.flag = htons((uint16_t) Z_LVAL_P(v));
}
if (php_beehive_array_get_value(vht, "service", v))
{
convert_to_long(v);
header.service = htonl((uint32_t) Z_LVAL_P(v));
}
if (php_beehive_array_get_value(vht, "time", v))
{
convert_to_long(v);
header.time = htonl((uint32_t) Z_LVAL_P(v));
}
if (php_beehive_array_get_value(vht, "uniqid", v))
{
convert_to_long(v);
header.uniqid = htonl((uint32_t) Z_LVAL_P(v));
}
if (php_beehive_array_get_value(vht, "askid", v))
{
convert_to_long(v);
header.askid = htonl((uint32_t) Z_LVAL_P(v));
}
if (php_beehive_array_get_value(vht, "code", v))
{
convert_to_long(v);
header.code = htons((uint16_t) Z_LVAL_P(v));
}
if (php_beehive_array_get_value(vht, "dst_mode", v))
{
convert_to_long(v);
header.dst_mode = (uint8_t) Z_LVAL_P(v);
}
//获取数由表
HashTable *router_table = NULL;
if (php_beehive_array_get_value(vht, "routers_list", v))
{
router_table = Z_ARRVAL_P(v);
header.routers = zend_hash_num_elements(router_table);
}
//计算包长
int header_len = BEEHIVE_FIXED_HEADER_LEN - 6 + header.routers * 8;
if (header.dst_mode > 0)
{
header_len = header_len + 8;
}
uint32_t body_len = 0;
if (php_beehive_array_get_value(vht, "body", v))
{
convert_to_string(v);
body_len = strlen(Z_STRVAL_P(v));
}
header.header_len = htons(header_len);
uint32_t packet_len_total = header_len + body_len + 6;
header.packet_len = htonl(packet_len_total - 4);
char *ret = (char*)emalloc(header_len + body_len + 6 + 1);
memcpy(ret, &header, sizeof(header));
int start = BEEHIVE_FIXED_HEADER_LEN;
if (header.routers > 0)
{
HashPosition pointer;
uint64_t router_item = 0;
for(zend_hash_internal_pointer_reset_ex(router_table, &pointer);
#if PHP_MAJOR_VERSION < 7
zend_hash_get_current_data_ex(router_table, (void**) &data, &pointer) == SUCCESS;
#else
zend_hash_get_current_data_ex(router_table, (void**) &data) == SUCCESS;
#endif
zend_hash_move_forward_ex(router_table, &pointer))
{
convert_to_long_ex(data);
router_item = htonll(Z_LVAL_PP(data));
memcpy(ret + start, &router_item, 8);
start = start + 8;
}
}
if (header.dst_mode > 0)
{
uint64_t dst = 0;
if (php_beehive_array_get_value(vht, "dst", v))
{
convert_to_long(v);
dst = htonll(Z_LVAL_P(v));
}
memcpy(ret + start, &dst, 8);
start = start + 8;
}
if (php_beehive_array_get_value(vht, "body", v))
{
//convert_to_string(v);
memcpy(ret + start, Z_STRVAL_P(v), body_len);
}
ret[header_len + body_len + 6] = 0;
#if PHP_MAJOR_VERSION < 7
RETURN_STRINGL(ret, packet_len_total, 0);
#else
RETURN_STRINGL(ret, packet_len_total);
#endif
}
// array_init(return_value);
// add_assoc_long(return_value, "port", ntohs(cli->remote_addr.addr.inet_v4.sin_port));
// sw_add_assoc_string(return_value, "host", inet_ntoa(cli->remote_addr.addr.inet_v4.sin_addr), 1);
// PHP_FUNCTION(confirm_beehive_compiled)
// {
// char *arg = NULL;
// int arg_len, len;
// char *strg;
// if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
// return;
// }
// len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "beehive", arg);
// RETURN_STRINGL(strg, len, 0);
// }
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
/* {{{ php_beehive_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_beehive_init_globals(zend_beehive_globals *beehive_globals)
{
beehive_globals->global_value = 0;
beehive_globals->global_string = NULL;
}
*/
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(beehive)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(beehive)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(beehive)
{
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(beehive)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(beehive)
{
php_info_print_table_start();
php_info_print_table_header(2, "beehive support", "enabled");
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
/* {{{ beehive_functions[]
* Every user visible function must have an entry in beehive_functions[].
*/
const zend_function_entry beehive_functions[] = {
//PHP_FE(confirm_beehive_compiled, NULL) /* For testing, remove later. */
PHP_FE(beehive_packet_pack, NULL)
PHP_FE(beehive_packet_unpack, NULL)
PHP_FE_END /* Must be the last line in beehive_functions[] */
};
/* }}} */
/* {{{ beehive_module_entry
*/
zend_module_entry beehive_module_entry = {
STANDARD_MODULE_HEADER,
"beehive",
beehive_functions,
PHP_MINIT(beehive),
PHP_MSHUTDOWN(beehive),
PHP_RINIT(beehive), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(beehive), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(beehive),
PHP_BEEHIVE_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_BEEHIVE
ZEND_GET_MODULE(beehive)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/