-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathcommon.c
124 lines (103 loc) · 4.14 KB
/
common.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
/*
+----------------------------------------------------------------------+
| Copyright (c) 1997-2017 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: Zhenyu Wu <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "common.h"
#include <arpa/inet.h>
#include "ext/standard/php_var.h"
#include "ext/standard/info.h"
#include "ext/json/php_json.h"
#include "ext/standard/php_string.h"
#include "zend_smart_str.h"
int send_identify(zval *nsq_obj, int sock)
{
//IDENTIFY
zval * nsq_config;
zval rv3;
zval json;
nsq_config = zend_read_property(Z_OBJCE_P(nsq_obj), NSQ_COMPAT_OBJ_P(nsq_obj), "nsqConfig", sizeof("nsqConfig")-1, 1, &rv3);
smart_str json_buf = {0};
if(Z_TYPE_P(nsq_config) != IS_NULL){
php_json_encode(&json_buf, nsq_config, 0) ;
smart_str_0(&json_buf);
ZVAL_NEW_STR(&json,json_buf.s);
char * identify_command = emalloc(256);
memset(identify_command, '\0', 256);
int identify_len = sprintf(identify_command, "%s", "IDENTIFY\n");
uint32_t json_len = htonl(Z_STRLEN(json));
memcpy(identify_command + identify_len, &json_len, 4);
int len_2 = sprintf(identify_command + identify_len + 4, "%s", Z_STRVAL(json));
send(sock,identify_command, identify_len+Z_STRLEN(json)+4 ,0);
zval *negotiation = zend_hash_str_find(Z_ARRVAL_P(nsq_config), "feature_negotiation", sizeof("feature_negotiation") - 1);
int l = 0;
int current_l = 0;
int msg_size;
char *message;
char *msg_size_char = malloc(4);
memset(msg_size_char, 0x00, 4);
int size;
again_size:
size = read(sock, msg_size_char, 4);
if(size <= 0){
goto again_size;
}
readI32((const unsigned char *) msg_size_char, &msg_size);
free(msg_size_char);
message = emalloc(msg_size + 1);
memset(message, 0x00, msg_size);
again:
l += read(sock, message +l , msg_size);
if( l < msg_size ){
goto again;
}
efree(message);
efree(identify_command);
zval_ptr_dtor(nsq_config);
zval_ptr_dtor(&json);
}
return 0;
}
int readI16(const unsigned char *pData, uint16_t *pValue) {
*pValue = (pData[0] << 8) | pData[1];
return 0;
}
int readI32(const unsigned char *pData, int32_t *pValue) {
*pValue = (pData[0] << 24) | (pData[1] << 16) | (pData[2] << 8) | pData[3];
return 0;
}
int readI64(const unsigned char *data, int64_t *pValue) {
*pValue = ((uint64_t) data[0] << 56) | ((uint64_t) data[1] << 48) | ((uint64_t) data[2] << 40) |
((uint64_t) data[3] << 32) | ((uint64_t) data[4] << 24) | ((uint64_t) data[5] << 16) |
((uint64_t) data[6] << 8) | (uint64_t) data[7];
return 0;
}
uint64_t ntoh64(const uint8_t *data) {
return (uint64_t) (data[7]) | (uint64_t) (data[6]) << 8 |
(uint64_t) (data[5]) << 16 | (uint64_t) (data[4]) << 24 |
(uint64_t) (data[3]) << 32 | (uint64_t) (data[2]) << 40 |
(uint64_t) (data[1]) << 48 | (uint64_t) (data[0]) << 56;
}
int check_ipaddr(const char *str) {
if (str == NULL || *str == '\0') {
return 0;
}
struct sockaddr_in6 addr6;
struct sockaddr_in addr4;
if (1 == inet_pton(AF_INET, str, &addr4.sin_addr)) {
return 1;
} else if (1 == inet_pton(AF_INET6, str, &addr6.sin6_addr)) {
return 1;
}
return 0;
}