36#include <openssl/err.h>
53 perror(
"cluster_new");
57 perror(
"cluster_new host");
62 perror(
"cluster_new user");
63 free(rsclustercon->
host);
68 perror(
"cluster_new pass");
69 free(rsclustercon->
user);
70 free(rsclustercon->
host);
75 perror(
"cluster_new cacert");
76 free(rsclustercon->
pass);
77 free(rsclustercon->
user);
78 free(rsclustercon->
host);
87 struct sockaddr_in server_addr;
90 rsclustercon->
sock = socket(AF_INET, SOCK_STREAM, 0);
91 if (rsclustercon->
sock < 0) {
92 perror(
"Socket creation error");
96 server_addr.sin_family = AF_INET;
97 server_addr.sin_port = htons(9443);
98 if (inet_pton(AF_INET, rsclustercon->
host, &server_addr.sin_addr) <= 0) {
99 perror(
"Invalid address");
104 if (connect(rsclustercon->
sock, (
struct sockaddr *)&server_addr,
sizeof(server_addr)) != 0) {
105 perror(
"Connection failed");
112 OpenSSL_add_ssl_algorithms();
113 SSL_load_error_strings();
116 rsclustercon->
ctx = SSL_CTX_new(SSLv23_client_method());
117 if (!rsclustercon->
ctx) {
118 ERR_print_errors_fp(stderr);
124 if (SSL_CTX_load_verify_locations(rsclustercon->
ctx, rsclustercon->
cacert,
NULL) != 1) {
125 ERR_print_errors_fp(stderr);
131 SSL_CTX_set_verify(rsclustercon->
ctx, SSL_VERIFY_NONE,
NULL);
134 SSL_CTX_set_verify(rsclustercon->
ctx, SSL_VERIFY_PEER,
NULL);
138 rsclustercon->
ssl = SSL_new(rsclustercon->
ctx);
139 SSL_set_fd(rsclustercon->
ssl, rsclustercon->
sock);
142 if (SSL_connect(rsclustercon->
ssl) <= 0) {
143 ERR_print_errors_fp(stderr);
157 if (
NULL==(auth_clear = (
char*)malloc(strlen(rsclustercon->
user)+1+strlen(rsclustercon->
pass)+1))) {
158 perror(
"cluster_queryget malloc(auth)");
161 strcpy(auth_clear,rsclustercon->
user);
162 strcat(auth_clear,
":");
163 strcat(auth_clear,rsclustercon->
pass);
169 char http_request[512];
170 snprintf(http_request,
sizeof(http_request),
171 "GET %s HTTP/1.1\r\n"
173 "Content-Type: application/json\r\n"
174 "Authorization: Basic %s\r\n"
175 "Connection: close\r\n\r\n",
176 endpoint,rsclustercon->
host,auth_encoded
181 SSL_write(rsclustercon->
ssl, http_request, strlen(http_request));
186 if (
NULL==(reply=strdup(
""))) {
187 perror(
"cluster_queryget strdup");
190 while ((bytes=SSL_read(rsclustercon->
ssl, buf, 1024 - 1)) > 0) {
193 if ((newreply=(
char*)realloc(reply,strlen(reply)+bytes+1))==
NULL) {
194 perror(
"Unable to allocate reply buffer");
205 if (
NULL==(http_body = strstr(reply,
"\r\n\r\n")))
208 retval_txt=strdup(http_body);
211 cJSON* retval_json = cJSON_Parse(retval_txt);
218 SSL_shutdown(rsclustercon->
ssl);
219 SSL_free(rsclustercon->
ssl);
220 close(rsclustercon->
sock);
221 SSL_CTX_free(rsclustercon->
ctx);
226 free(rsclustercon->
cacert);
227 free(rsclustercon->
pass);
228 free(rsclustercon->
user);
229 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)
struct rsclustercon_s rsclustercon_t
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