36#include <openssl/err.h>
42 perror(
"cluster_new");
46 perror(
"cluster_new host");
51 perror(
"cluster_new user");
52 free(rsclustercon->
host);
57 perror(
"cluster_new pass");
58 free(rsclustercon->
user);
59 free(rsclustercon->
host);
64 perror(
"cluster_new cacert");
65 free(rsclustercon->
pass);
66 free(rsclustercon->
user);
67 free(rsclustercon->
host);
76 struct sockaddr_in server_addr;
79 rsclustercon->
sock = socket(AF_INET, SOCK_STREAM, 0);
80 if (rsclustercon->
sock < 0) {
81 perror(
"Socket creation error");
85 server_addr.sin_family = AF_INET;
86 server_addr.sin_port = htons(9443);
87 if (inet_pton(AF_INET, rsclustercon->
host, &server_addr.sin_addr) <= 0) {
88 perror(
"Invalid address");
93 if (connect(rsclustercon->
sock, (
struct sockaddr *)&server_addr,
sizeof(server_addr)) != 0) {
94 perror(
"Connection failed");
101 OpenSSL_add_ssl_algorithms();
102 SSL_load_error_strings();
105 rsclustercon->
ctx = SSL_CTX_new(SSLv23_client_method());
106 if (!rsclustercon->
ctx) {
107 ERR_print_errors_fp(stderr);
113 if (SSL_CTX_load_verify_locations(rsclustercon->
ctx, rsclustercon->
cacert,
NULL) != 1) {
114 ERR_print_errors_fp(stderr);
120 SSL_CTX_set_verify(rsclustercon->
ctx, SSL_VERIFY_NONE,
NULL);
123 SSL_CTX_set_verify(rsclustercon->
ctx, SSL_VERIFY_PEER,
NULL);
127 rsclustercon->
ssl = SSL_new(rsclustercon->
ctx);
128 SSL_set_fd(rsclustercon->
ssl, rsclustercon->
sock);
131 if (SSL_connect(rsclustercon->
ssl) <= 0) {
132 ERR_print_errors_fp(stderr);
146 if (
NULL==(auth_clear = (
char*)malloc(strlen(rsclustercon->
user)+1+strlen(rsclustercon->
pass)+1))) {
147 perror(
"cluster_queryget malloc(auth)");
150 strcpy(auth_clear,rsclustercon->
user);
151 strcat(auth_clear,
":");
152 strcat(auth_clear,rsclustercon->
pass);
158 char http_request[512];
159 snprintf(http_request,
sizeof(http_request),
160 "GET %s HTTP/1.1\r\n"
162 "Content-Type: application/json\r\n"
163 "Authorization: Basic %s\r\n"
164 "Connection: close\r\n\r\n",
165 endpoint,rsclustercon->
host,auth_encoded
170 SSL_write(rsclustercon->
ssl, http_request, strlen(http_request));
175 if (
NULL==(reply=strdup(
""))) {
176 perror(
"cluster_queryget strdup");
179 while ((bytes=SSL_read(rsclustercon->
ssl, buf, 1024 - 1)) > 0) {
182 if ((newreply=(
char*)realloc(reply,strlen(reply)+bytes+1))==
NULL) {
183 perror(
"Unable to allocate reply buffer");
194 if (
NULL==(http_body = strstr(reply,
"\r\n\r\n")))
197 retval_txt=strdup(http_body);
200 cJSON* retval_json = cJSON_Parse(retval_txt);
207 SSL_shutdown(rsclustercon->
ssl);
208 SSL_free(rsclustercon->
ssl);
209 close(rsclustercon->
sock);
210 SSL_CTX_free(rsclustercon->
ctx);
215 free(rsclustercon->
cacert);
216 free(rsclustercon->
pass);
217 free(rsclustercon->
user);
218 free(rsclustercon->
host);
char * base64_encode(char *plain)
Encode a zero terminated C string in Base64.
Simple Base64 encoding and decoding functions.
void cluster_close(rsclustercon_t *rsclustercon)
void cluster_del(rsclustercon_t *rsclustercon)
rsclustercon_t * cluster_new(const cluster_t *cluster)
int cluster_open(rsclustercon_t *rsclustercon)
cJSON * cluster_queryget(const rsclustercon_t *rsclustercon, const char *endpoint)
unsigned short int insecure