-
Notifications
You must be signed in to change notification settings - Fork 532
/
Copy pathdyn_florida.c
202 lines (162 loc) · 5.51 KB
/
dyn_florida.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
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <netdb.h>
#include "dyn_seeds_provider.h"
#include "dyn_core.h"
#include "dyn_string.h"
/***************************************************************************
* Keep polling the local REST service at the address
* http://127.0.0.1:8080/REST/v1/admin/get_seeds
* Expect response is a list of peers in the format:
* peer_host1:peer_listen_port:rack:dc:peer_token1|peer_host2:peer_listen_port:rack:dc:peer_token2|...
* For example:
* ec2-54-145-17-101.compute-1.amazonaws.com:8101:dyno_pds--useast1e:us-east-1:1383429731|ec2-54-101-51-17.eu-west-1.compute.amazonaws.com:8101:dyno_pds--euwest1c:eu-west-1:1383429731
****************************************************************************/
#ifndef FLORIDA_IP
#define FLORIDA_IP "127.0.0.1"
#endif
#ifndef FLORIDA_PORT
#define FLORIDA_PORT 8080
#endif
#ifndef FLORIDA_REQUEST
#define FLORIDA_REQUEST "GET /REST/v1/admin/get_seeds HTTP/1.0\r\nHost: 127.0.0.1\r\nUser-Agent: HTMLGET 1.0\r\n\r\n";
#endif
static char * floridaIp = NULL;
static int floridaPort = NULL;
static char * request = NULL;
static int isOsVarEval = 0;
static void evalOSVar();
static uint32_t create_tcp_socket();
static int64_t last = 0; //storing last time for seeds check
static uint32_t last_seeds_hash = 0;
static void evalOSVar(){
if (isOsVarEval==0){
request = (getenv("DYNOMITE_FLORIDA_REQUEST")!=NULL) ? getenv("DYNOMITE_FLORIDA_REQUEST") : FLORIDA_REQUEST;
floridaPort = (getenv("DYNOMITE_FLORIDA_PORT")!=NULL) ? atoi(getenv("DYNOMITE_FLORIDA_PORT")) : FLORIDA_PORT;
floridaIp = (getenv("DYNOMITE_FLORIDA_IP")!=NULL) ? getenv("DYNOMITE_FLORIDA_IP") : FLORIDA_IP;
isOsVarEval = 1;
}
}
static bool seeds_check()
{
msec_t now = dn_msec_now();
int64_t delta = (int64_t)(now - last);
log_debug(LOG_VERB, "Delta or elapsed time : %lu", delta);
log_debug(LOG_VERB, "Seeds check internal %d", SEEDS_CHECK_INTERVAL);
if (delta > SEEDS_CHECK_INTERVAL) {
last = now;
return true;
}
return false;
}
static uint32_t
hash_seeds(uint8_t *seeds, size_t length)
{
const uint8_t *ptr = seeds;
uint32_t value = 0;
while (length--) {
uint32_t val = (uint32_t) *ptr++;
value += val;
value += (value << 10);
value ^= (value >> 6);
}
value += (value << 3);
value ^= (value >> 11);
value += (value << 15);
return value;
}
uint8_t
florida_get_seeds(struct context * ctx, struct mbuf *seeds_buf) {
evalOSVar();
struct sockaddr_in *remote;
uint32_t sock;
uint32_t tmpres;
uint8_t buf[BUFSIZ + 1];
log_debug(LOG_VVERB, "Running florida_get_seeds!");
if (!seeds_check()) {
return DN_NOOPS;
}
sock = create_tcp_socket();
if (sock == -1) {
log_debug(LOG_VVERB, "Unable to create a socket");
return DN_ERROR;
}
remote = (struct sockaddr_in *) dn_alloc(sizeof(struct sockaddr_in *));
remote->sin_family = AF_INET;
tmpres = inet_pton(AF_INET, floridaIp, (void *)(&(remote->sin_addr.s_addr)));
remote->sin_port = htons(floridaPort);
if(connect(sock, (struct sockaddr *)remote, sizeof(struct sockaddr)) < 0) {
log_debug(LOG_VVERB, "Unable to connect the destination");
return DN_ERROR;
}
uint32_t sent = 0;
while(sent < dn_strlen(request))
{
tmpres = send(sock, request+sent, dn_strlen(request)-sent, 0);
if(tmpres == -1){
log_debug(LOG_VVERB, "Unable to send query");
close(sock);
dn_free(remote);
return DN_ERROR;
}
sent += tmpres;
}
mbuf_rewind(seeds_buf);
memset(buf, 0, sizeof(buf));
uint32_t htmlstart = 0;
uint8_t * htmlcontent;
uint8_t *ok = NULL;
//assume that the respsone payload is under BUF_SIZE
while ((tmpres = recv(sock, buf, BUFSIZ, 0)) > 0) {
// Look for a OK response in the first buffer output.
if (!ok)
ok = (uint8_t *) strstr((char *)buf, "200 OK\r\n");
if (ok == NULL) {
log_error("Received Error from Florida while getting seeds");
loga_hexdump(buf, tmpres, "Florida Response with %ld bytes of data", tmpres);
close(sock);
dn_free(remote);
return DN_ERROR;
}
if (htmlstart == 0) {
/* Under certain conditions this will not work.
* If the \r\n\r\n part is splitted into two messages
* it will fail to detect the beginning of HTML content
*/
htmlcontent = (uint8_t *) strstr((char *)buf, "\r\n\r\n");
if(htmlcontent != NULL) {
htmlstart = 1;
htmlcontent += 4;
}
} else {
htmlcontent = buf;
}
if(htmlstart) {
mbuf_copy(seeds_buf, htmlcontent, tmpres - (htmlcontent - buf));
}
memset(buf, 0, tmpres);
}
if(tmpres < 0) {
log_debug(LOG_VVERB, "Error receiving data");
}
close(sock);
dn_free(remote);
uint32_t seeds_hash = hash_seeds(seeds_buf->pos, mbuf_length(seeds_buf));
if (last_seeds_hash != seeds_hash) {
last_seeds_hash = seeds_hash;
} else {
return DN_NOOPS;
}
return DN_OK;
}
uint32_t create_tcp_socket()
{
uint32_t sock;
if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
log_debug(LOG_VVERB, "Unable to create TCP socket");
return DN_ERROR;
}
return sock;
}