-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc-etcd.c
421 lines (333 loc) · 10.5 KB
/
c-etcd.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
/**
* @author Vaibhav Bhembre
* @version 2013/09/19
* gcc -g -Wall -Werror -Wshadow -Wwrite-strings -pedantic c-etcd.c -o etcd -lcurl -ljansson -std=c99
*/
#include "c-etcd.h"
/* Internal */
static char *etcd_url(const char *key, const char *prefix);
static const char *etcd_host();
static short etcd_port();
static const struct etcd_data *http_request(const char *url, etcd_method method, const char *post_data);
static size_t http_write_callback(void *ptr, size_t size, size_t nmemb, void *userdata);
static void init_etcd_data(struct etcd_data **data);
static bool is_valid_key(const char *);
static bool is_valid_value(const char *);
static void exit_debug(const char *msg);
static void exit_debug_status(const char *msg, int status);
static void debug(const char *msg, ...);
int main(int argc, char *argv[]) {
char *h;
etcd_response response;
const struct etcd_data *val;
const char *key = "/key1", *value = "value1";
if (argc <= 1) {
hostname = DEFAULT_HOSTNAME;
port = DEFAULT_PORT;
} else {
/* Input in format 'host:port' */
hostname = h = strdup(argv[1]);
h = strchr(hostname, ':');
*h++ = '\0';
port = atoi(h);
}
response = etcd_set(key, value, 0);
assert(response == ETCD_SUCCESS);
val = etcd_get(key);
assert(val->response == ETCD_SUCCESS);
assert(strcmp(val->value, value) == 0);
free((struct etcd_data *)val);
response = etcd_delete(key);
assert(response == ETCD_SUCCESS);
val = etcd_get(key);
assert(val->response == ETCD_FAILURE);
free((struct etcd_data *)val);
response = etcd_set(key, value, 5);
assert(response == ETCD_SUCCESS);
response = etcd_test_and_set(key, "value2", value, 0);
assert(response == ETCD_SUCCESS);
response = etcd_test_and_set(key, "value2", value, 0);
assert(response == ETCD_FAILURE);
return 0;
}
/******************************************************************
*** API Function definitions
******************************************************************/
etcd_response etcd_set(const char *key, const char *value, unsigned ttl) {
char *url, *data, *tmpdata;
const struct etcd_data *retdata;
int ret;
etcd_response response;
if (!is_valid_key(key) || !is_valid_value(value)) {
return ETCD_FAILURE;
}
url = etcd_url(key, NULL);
ret = asprintf(&data, "value=%s", value);
assert(ret >= 0);
if (ttl > 0) {
ret = asprintf(&tmpdata, "%s&ttl=%u", data, ttl);
assert(ret >= 0);
free(data);
data = tmpdata;
}
retdata = http_request(url, ETCD_SET, data);
assert(retdata != NULL);
response = retdata->response;
if (response == ETCD_FAILURE) {
debug(retdata->errmsg);
}
free(url);
free(data);
free((struct etcd_data *)retdata);
return response;
}
const struct etcd_data *etcd_get(const char *key) {
char *url;
const struct etcd_data *retdata;
if (!is_valid_key(key)) {
return NULL;
}
url = etcd_url(key, NULL);
retdata = http_request(url, ETCD_GET, NULL);
free(url);
return retdata;
}
etcd_response etcd_delete(const char *key) {
char *url;
const struct etcd_data *retdata;
etcd_response response;
if (!is_valid_key(key)) {
return ETCD_FAILURE;
}
url = etcd_url(key, NULL);
retdata = http_request(url, ETCD_DEL, NULL);
assert(retdata != NULL);
response = retdata->response;
if (response == ETCD_FAILURE) {
debug(retdata->errmsg);
}
free(url);
free((struct etcd_data *)retdata);
return response;
}
etcd_response etcd_test_and_set(const char *key, const char *value, const char *oldValue, unsigned ttl) {
char *url, *data, *tmpdata;
const struct etcd_data *retdata;
etcd_response response;
int ret;
if (!is_valid_key(key) || !is_valid_value(value)) {
return ETCD_FAILURE;
}
if (!is_valid_value(oldValue)) {
/* If the old value is NULL, then this should act like etcd_set() */
if (oldValue == NULL) {
return etcd_set(key, value, ttl);
}
return ETCD_FAILURE;
}
url = etcd_url(key, NULL);
ret = asprintf(&data, "value=%s&prevValue=%s", value, oldValue);
assert(ret >= 0);
if (ttl > 0) {
ret = asprintf(&tmpdata, "%s&ttl=%u", data, ttl);
assert(ret >= 0);
free(data);
data = tmpdata;
}
retdata = http_request(url, ETCD_SET, data);
assert(retdata != NULL);
response = retdata->response;
if (response == ETCD_FAILURE) {
debug(retdata->errmsg);
}
free(url);
free(data);
free((struct etcd_data *)retdata);
return response;
}
/******************************************************************
*** Internal functions
******************************************************************/
static char *etcd_url(const char *key, const char *prefix) {
char *url;
int ret, prefix_allocated = 0;
if (prefix == NULL) {
prefix = strdup("keys");
prefix_allocated = 1;
}
ret = asprintf(&url, ETCD_URL_FORMAT, etcd_host(), etcd_port(), prefix, key);
assert(ret >= 0);
if (prefix_allocated) {
free((char *)prefix);
}
return url;
}
static const char *etcd_host() {
if (!hostname) {
exit_debug("Incorrect hostname provided.");
}
return hostname;
}
static short etcd_port() {
if (port == 0) {
exit_debug("Incorrect port provided as 0.");
}
return port;
}
static bool is_valid_key(const char *key) {
if (key == NULL || !strlen(key)) {
debug("Invalid key provided.");
return false;
}
return true;
}
static bool is_valid_value(const char *value) {
if (value == NULL || !strlen(value)) {
debug("Invalid value provided.");
return false;
}
return true;
}
static const struct etcd_data *http_request(const char *url, etcd_method method, const char *post_data) {
CURL *curl;
CURLcode curl_code;
char *errmsg;
struct etcd_data *data;
long httpcode;
if (!url) {
debug("Incorrect 'url' provided.");
return NULL;
}
curl = curl_easy_init();
if (!curl) {
errmsg = strdup("'curl_easy_init()' failed.");
goto error;
}
init_etcd_data(&data);
if (!data) {
errmsg = strdup("init failed for etc_data.");
goto error_cleanup_curl;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, data);
if (method == ETCD_SET) {
if (!post_data) {
debug("No data provided to method: POST");
return NULL;
}
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data);
} else if (method == ETCD_DEL) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, DELETE_KEY);
}
curl_code = curl_easy_perform(curl);
if (curl_code != CURLE_OK) {
if (curl_code == CURLE_COULDNT_CONNECT) {
debug("Failed to recieve a response for request: %s. Aborting.", url);
exit(curl_code);
}
errmsg = strdup("'curl_easy_perform' failed.");
goto error_cleanup_curl;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpcode);
if (httpcode != HTTP_SUCCESS && httpcode != HTTP_BAD_REQ) {
errmsg = strdup("Server responded with status code: ");
sprintf(&errmsg[strlen(errmsg) - 3], "%ld", httpcode);
goto error_cleanup_curl;
}
curl_easy_cleanup(curl);
return data;
error_cleanup_curl:
curl_easy_cleanup(curl);
error:
debug(errmsg);
free(errmsg);
return NULL;
}
static size_t http_write_callback(void *ptr, size_t size, size_t nmemb, void *userdata) {
json_t *response, *value, *in;
json_error_t error;
const char *etcd_value;
const char *errmsg;
struct etcd_data *data;
int val;
data = userdata;
response = json_loads(ptr, 0, &error);
if (!json_is_object(response)) {
strcpy(data->errmsg, "'response' returned is not a json object");
goto error;
}
in = json_object_get(response, "index");
if (json_is_integer(in)) {
data->index = json_integer_value(in);
}
value = json_object_get(response, "value");
if (!json_is_string(value)) {
value = json_object_get(response, "action");
etcd_value = "";
if (json_is_string(value)) {
etcd_value = json_string_value(value);
}
if (strcmp(etcd_value, DELETE_KEY) == 0) {
value = json_object_get(response, "key");
etcd_value = json_string_value(value);
strcpy(data->value, ++etcd_value);
data->response = ETCD_SUCCESS;
json_decref(response);
return size * nmemb;
} else {
value = json_object_get(response, "errorCode");
if (!json_is_integer(value)) {
strcpy(data->errmsg, "Invalid error message.");
goto error;
}
val = json_integer_value(value);
value = json_object_get(response, "message");
if (!json_is_string(value)) {
strcpy(data->errmsg, "Invalid error message.");
goto error;
}
errmsg = json_string_value(value);
sprintf(data->errmsg, "%d:%s", val, errmsg);
goto error;
}
}
etcd_value = json_string_value(value);
memcpy(data->value, etcd_value, strlen(etcd_value) + 1);
data->response = ETCD_SUCCESS;
json_decref(response);
return size * nmemb;
error:
json_decref(response);
data->value = NULL;
data->response = ETCD_FAILURE;
return size * nmemb;
}
static void init_etcd_data(struct etcd_data **data) {
struct etcd_data *d;
d = calloc(1, sizeof(*d) + BUFSIZE);
if (!d) return;
d->index = -1;
d->value = &((char *)d)[sizeof(*d) + 1]; /* Trick to ensure only 1 malloc is required */
d->errmsg = d->value + (BUFSIZE / 2);
*data = d;
}
static void exit_debug(const char *msg) {
exit_debug_status(msg, EXIT_FAILURE);
}
static void exit_debug_status(const char *msg, int status) {
if (strlen(msg) > 0) {
fprintf(stderr, "Error: %s\n", msg);
}
exit(status);
}
static void debug(const char *msg, ...) {
#ifdef DEBUG
va_list args;
va_start(args, msg);
vfprintf(stdout, msg, args);
fprintf(stdout, "\n");
va_end(args);
#endif
}