22 #include <sys/param.h>
24 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
44 #ifdef HAVE_GNUTLS_GNUTLS_H
46 # include <gnutls/gnutls.h>
48 const int psk_tls_kx_order[] = {
53 const int anon_tls_kx_order[] = {
63 #ifdef HAVE_LINUX_SWAB_H
64 # include <linux/swab.h>
70 #define __swab16(x) ((uint16_t)( \
71 (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
72 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))
74 #define __swab32(x) ((uint32_t)( \
75 (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
76 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
77 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
78 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
80 #define __swab64(x) ((uint64_t)( \
81 (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
82 (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
83 (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
84 (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
85 (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
86 (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
87 (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
88 (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
91 #define REMOTE_MSG_VERSION 1
92 #define ENDIAN_LOCAL 0xBADADBBD
94 struct crm_remote_header_v0
109 static struct crm_remote_header_v0 *
112 struct crm_remote_header_v0 *header = (
struct crm_remote_header_v0 *)remote->
buffer;
113 if(remote->
buffer_offset <
sizeof(
struct crm_remote_header_v0)) {
121 crm_err(
"Invalid message detected, endian mismatch: %lx is neither %lx nor the swab'd %lx",
127 header->flags =
__swab64(header->flags);
128 header->endian =
__swab32(header->endian);
130 header->version =
__swab32(header->version);
131 header->size_total =
__swab32(header->size_total);
132 header->payload_offset =
__swab32(header->payload_offset);
133 header->payload_compressed =
__swab32(header->payload_compressed);
134 header->payload_uncompressed =
__swab32(header->payload_uncompressed);
140 #ifdef HAVE_GNUTLS_GNUTLS_H
147 time_t start = time(NULL);
150 rc = gnutls_handshake(*remote->tls_session);
151 if (rc == GNUTLS_E_INTERRUPTED || rc == GNUTLS_E_AGAIN) {
159 }
while (((time(NULL) - start) < (timeout_ms / 1000)) &&
160 (rc == GNUTLS_E_INTERRUPTED || rc == GNUTLS_E_AGAIN));
163 crm_trace(
"gnutls_handshake() failed with %d", rc);
174 gnutls_init(session, type);
175 # ifdef HAVE_GNUTLS_PRIORITY_SET_DIRECT
177 gnutls_priority_set_direct(*session,
"NORMAL:+ANON-DH", NULL);
180 gnutls_set_default_priority(*session);
181 gnutls_kx_set_priority(*session, anon_tls_kx_order);
183 gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t) GINT_TO_POINTER(csock));
186 gnutls_credentials_set(*session, GNUTLS_CRD_ANON,
187 (gnutls_anon_server_credentials_t) credentials);
190 gnutls_credentials_set(*session, GNUTLS_CRD_ANON,
191 (gnutls_anon_client_credentials_t) credentials);
203 gnutls_init(session, type);
204 # ifdef HAVE_GNUTLS_PRIORITY_SET_DIRECT
205 gnutls_priority_set_direct(*session,
"NORMAL:+DHE-PSK:+PSK", NULL);
207 gnutls_set_default_priority(*session);
208 gnutls_kx_set_priority(*session, psk_tls_kx_order);
210 gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t) GINT_TO_POINTER(csock));
213 gnutls_credentials_set(*session, GNUTLS_CRD_PSK,
214 (gnutls_psk_server_credentials_t) credentials);
217 gnutls_credentials_set(*session, GNUTLS_CRD_PSK,
218 (gnutls_psk_client_credentials_t) credentials);
228 const char *unsent = buf;
237 crm_trace(
"Message size: %llu", (
unsigned long long) len);
240 rc = gnutls_record_send(*session, unsent, len);
242 if (rc == GNUTLS_E_INTERRUPTED || rc == GNUTLS_E_AGAIN) {
244 (
unsigned long long) len);
248 gnutls_strerror(rc), rc);
252 }
else if (rc < len) {
253 crm_debug(
"Sent %d of %llu bytes", rc, (
unsigned long long) len);
262 return rc < 0 ? rc : total_send;
267 crm_send_plaintext(
int sock,
const char *buf,
size_t len)
271 const char *unsent = buf;
279 crm_trace(
"Message on socket %d: size=%llu",
280 sock, (
unsigned long long) len);
282 rc = write(sock, unsent, len);
291 crm_perror(LOG_ERR,
"Could only write %d of the remaining %d bytes", rc, (
int)len);
295 }
else if (rc < len) {
296 crm_trace(
"Only sent %d of %llu remaining bytes",
297 rc, (
unsigned long long) len);
303 crm_trace(
"Sent %d bytes: %.100s", rc, buf);
306 return rc < 0 ? rc : total_send;
311 crm_remote_sendv(
crm_remote_t * remote,
struct iovec * iov,
int iovs)
314 int rc = -ESOCKTNOSUPPORT;
316 for(; lpc < iovs; lpc++) {
318 #ifdef HAVE_GNUTLS_GNUTLS_H
319 if (remote->tls_session) {
320 rc = crm_send_tls(remote->tls_session, iov[lpc].iov_base, iov[lpc].iov_len);
325 rc = crm_send_plaintext(remote->
tcp_socket, iov[lpc].iov_base, iov[lpc].iov_len);
328 crm_err(
"Unsupported connection type");
338 static uint64_t
id = 0;
342 struct crm_remote_header_v0 *header;
344 if (xml_text == NULL) {
345 crm_err(
"Could not send remote message: no message provided");
349 header = calloc(1,
sizeof(
struct crm_remote_header_v0));
350 iov[0].iov_base = header;
351 iov[0].iov_len =
sizeof(
struct crm_remote_header_v0);
353 iov[1].iov_base = xml_text;
354 iov[1].iov_len = 1 + strlen(xml_text);
360 header->payload_offset = iov[0].iov_len;
361 header->payload_uncompressed = iov[1].iov_len;
362 header->size_total = iov[0].iov_len + iov[1].iov_len;
365 (
int)iov[0].iov_len, *(
int*)(
void*)xml_text);
366 rc = crm_remote_sendv(remote, iov, 2);
368 crm_err(
"Could not send remote message: %s " CRM_XS " rc=%d",
372 free(iov[0].iov_base);
373 free(iov[1].iov_base);
387 struct crm_remote_header_v0 *header = crm_remote_header(remote);
389 if (remote->
buffer == NULL || header == NULL) {
394 if (header->payload_compressed) {
396 unsigned int size_u = 1 + header->payload_uncompressed;
397 char *uncompressed = calloc(1, header->payload_offset + size_u);
399 crm_trace(
"Decompressing message data %d bytes into %d bytes",
400 header->payload_compressed, size_u);
402 rc = BZ2_bzBuffToBuffDecompress(uncompressed + header->payload_offset, &size_u,
403 remote->
buffer + header->payload_offset,
404 header->payload_compressed, 1, 0);
407 crm_warn(
"Couldn't decompress v%d message, we only understand v%d",
412 }
else if (rc != BZ_OK) {
418 CRM_ASSERT(size_u == header->payload_uncompressed);
420 memcpy(uncompressed, remote->
buffer, header->payload_offset);
421 remote->
buffer_size = header->payload_offset + size_u;
424 remote->
buffer = uncompressed;
425 header = crm_remote_header(remote);
431 CRM_LOG_ASSERT(remote->
buffer[
sizeof(
struct crm_remote_header_v0) + header->payload_uncompressed - 1] == 0);
435 crm_warn(
"Couldn't parse v%d message, we only understand v%d",
438 }
else if (xml == NULL) {
439 crm_err(
"Couldn't parse: '%.120s'", remote->
buffer + header->payload_offset);
457 struct pollfd fds = { 0, };
461 int timeout = total_timeout;
463 #ifdef HAVE_GNUTLS_GNUTLS_H
464 if (remote->tls_session) {
465 void *sock_ptr = gnutls_transport_get_ptr(*remote->tls_session);
467 sock = GPOINTER_TO_INT(sock_ptr);
474 crm_err(
"Unsupported connection type");
491 if (errno == EINTR && (timeout > 0)) {
492 timeout = total_timeout - ((time(NULL) - start) * 1000);
493 if (timeout < 1000) {
498 rc = poll(&fds, 1, timeout);
499 }
while (rc < 0 && errno == EINTR);
501 return (rc < 0)? -errno : rc;
519 size_t read_len =
sizeof(
struct crm_remote_header_v0);
520 struct crm_remote_header_v0 *header = crm_remote_header(remote);
524 read_len = header->size_total;
530 crm_trace(
"Expanding buffer to %llu bytes",
537 #ifdef HAVE_GNUTLS_GNUTLS_H
538 if (remote->tls_session) {
539 rc = gnutls_record_recv(*(remote->tls_session),
542 if (rc == GNUTLS_E_INTERRUPTED) {
544 }
else if (rc == GNUTLS_E_AGAIN) {
547 crm_debug(
"TLS receive failed: %s (%d)", gnutls_strerror(rc), rc);
563 crm_err(
"Unsupported connection type");
564 return -ESOCKTNOSUPPORT;
572 crm_trace(
"Received %u more bytes, %llu total",
575 }
else if (rc == -EINTR || rc == -EAGAIN) {
578 }
else if (rc == 0) {
579 crm_debug(
"EOF encoutered after %llu bytes",
584 crm_debug(
"Error receiving message after %llu bytes: %s (%d)",
590 header = crm_remote_header(remote);
593 crm_trace(
"Read less than the advertised length: %llu < %u bytes",
597 crm_trace(
"Read full message of %llu bytes",
620 time_t start = time(NULL);
621 int remaining_timeout = 0;
623 if (total_timeout == 0) {
624 total_timeout = 10000;
625 }
else if (total_timeout < 0) {
626 total_timeout = 60000;
630 remaining_timeout = total_timeout;
631 while ((remaining_timeout > 0) && !(*disconnected)) {
633 crm_trace(
"Waiting for remote data (%d of %d ms timeout remaining)",
634 remaining_timeout, total_timeout);
638 crm_err(
"Timed out (%d ms) while waiting for remote data",
643 crm_debug(
"Wait for remote data aborted, will try again: %s "
647 rc = crm_remote_recv_once(remote);
650 }
else if (rc == -EAGAIN) {
651 crm_trace(
"Still waiting for remote data");
658 if (rc == -ENOTCONN) {
663 remaining_timeout = total_timeout - ((time(NULL) - start) * 1000);
669 struct tcp_async_cb_data {
673 void (*callback) (
void *userdata,
int sock);
679 check_connect_finished(gpointer userdata)
681 struct tcp_async_cb_data *cb_data = userdata;
683 int sock = cb_data->sock;
687 socklen_t len =
sizeof(error);
688 struct timeval ts = { 0, };
690 if (cb_data->success == TRUE) {
698 crm_trace(
"fd %d: checking to see if connect finished", sock);
699 cb_arg = select(sock + 1, &rset, &wset, NULL, &ts);
703 if ((errno == EINPROGRESS) || (errno == EAGAIN)) {
705 if ((time(NULL) - cb_data->start) < (cb_data->timeout / 1000)) {
711 crm_trace(
"fd %d: select failed %d connect dispatch ", sock, cb_arg);
713 }
else if (cb_arg == 0) {
714 if ((time(NULL) - cb_data->start) < (cb_data->timeout / 1000)) {
717 crm_debug(
"fd %d: timeout during select", sock);
721 crm_trace(
"fd %d: select returned success", sock);
726 if (FD_ISSET(sock, &rset) || FD_ISSET(sock, &wset)) {
727 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
729 crm_trace(
"fd %d: call to getsockopt failed", sock);
733 crm_trace(
"fd %d: error returned from getsockopt: %d", sock, error);
738 crm_trace(
"neither read nor write set after select");
747 cb_arg = cb_data->sock;
753 if (cb_data->callback) {
754 cb_data->callback(cb_data->userdata, cb_arg);
766 internal_tcp_connect_async(
int sock,
767 const struct sockaddr *addr, socklen_t addrlen,
int timeout ,
768 int *timer_id,
void *userdata,
void (*callback) (
void *userdata,
int sock))
773 struct tcp_async_cb_data *cb_data = NULL;
777 crm_warn(
"Could not set socket non-blocking: %s " CRM_XS " rc=%d",
783 rc = connect(sock, addr, addrlen);
784 if (rc < 0 && (errno != EINPROGRESS) && (errno != EAGAIN)) {
789 cb_data = calloc(1,
sizeof(
struct tcp_async_cb_data));
790 cb_data->userdata = userdata;
791 cb_data->callback = callback;
792 cb_data->sock = sock;
793 cb_data->timeout = timeout;
794 cb_data->start = time(NULL);
801 cb_data->success = TRUE;
813 crm_trace(
"Scheduling check in %dms for whether connect to fd %d finished",
815 timer = g_timeout_add(interval, check_connect_finished, cb_data);
824 internal_tcp_connect(
int sock,
const struct sockaddr *addr, socklen_t addrlen)
826 int rc = connect(sock, addr, addrlen);
837 crm_warn(
"Could not set socket non-blocking: %s " CRM_XS " rc=%d",
860 int *timer_id,
void *userdata,
861 void (*callback) (
void *userdata,
int sock))
863 char buffer[INET6_ADDRSTRLEN];
864 struct addrinfo *res = NULL;
865 struct addrinfo *rp = NULL;
866 struct addrinfo hints;
867 const char *server =
host;
869 int sock = -ENOTCONN;
872 memset(&hints, 0,
sizeof(
struct addrinfo));
873 hints.ai_family = AF_UNSPEC;
874 hints.ai_socktype = SOCK_STREAM;
875 hints.ai_flags = AI_CANONNAME;
876 ret_ga = getaddrinfo(server, NULL, &hints, &res);
878 crm_err(
"Unable to get IP address info for %s: %s",
879 server, gai_strerror(ret_ga));
882 if (!res || !res->ai_addr) {
883 crm_err(
"Unable to get IP address info for %s: no result", server);
888 for (rp = res; rp != NULL; rp = rp->ai_next) {
889 struct sockaddr *addr = rp->ai_addr;
895 if (rp->ai_canonname) {
896 server = res->ai_canonname;
898 crm_debug(
"Got canonical name %s for %s", server, host);
900 sock = socket(rp->ai_family, SOCK_STREAM, IPPROTO_TCP);
902 crm_perror(LOG_WARNING,
"creating socket for connection to %s",
910 if (addr->sa_family == AF_INET6) {
911 ((
struct sockaddr_in6 *)(
void*)addr)->sin6_port = htons(port);
913 ((
struct sockaddr_in *)(
void*)addr)->sin_port = htons(port);
916 memset(buffer, 0,
DIMOF(buffer));
918 crm_info(
"Attempting TCP connection to %s:%d", buffer, port);
921 if (internal_tcp_connect_async
922 (sock, rp->ai_addr, rp->ai_addrlen, timeout, timer_id, userdata, callback) == 0) {
926 }
else if (internal_tcp_connect(sock, rp->ai_addr, rp->ai_addrlen) == 0) {
961 switch (((
struct sockaddr*)sa)->sa_family) {
963 inet_ntop(AF_INET, &(((
struct sockaddr_in *)sa)->sin_addr),
964 s, INET6_ADDRSTRLEN);
968 inet_ntop(AF_INET6, &(((
struct sockaddr_in6 *)sa)->sin6_addr),
969 s, INET6_ADDRSTRLEN);
973 strcpy(s,
"<invalid>");
983 struct sockaddr_storage addr;
984 char addr_str[INET6_ADDRSTRLEN];
985 #ifdef TCP_USER_TIMEOUT
991 laddr =
sizeof(addr);
992 memset(&addr, 0,
sizeof(addr));
993 csock = accept(ssock, (
struct sockaddr *)&addr, &laddr);
995 crm_info(
"New remote connection from %s", addr_str);
998 crm_err(
"accept socket failed");
1004 crm_err(
"Could not set socket non-blocking: %s " CRM_XS " rc=%d",
1010 #ifdef TCP_USER_TIMEOUT
1011 if (sbd_timeout > 0) {
1012 optval = sbd_timeout / 2;
1013 rc = setsockopt(csock, SOL_TCP, TCP_USER_TIMEOUT,
1014 &optval,
sizeof(optval));
1016 crm_err(
"setting TCP_USER_TIMEOUT (%d) on client socket failed",
1035 static int port = 0;
1038 const char *env = getenv(
"PCMK_remote_port");
1042 port = strtol(env, NULL, 10);
1043 if (errno || (port < 1) || (port > 65535)) {
1044 crm_warn(
"Environment variable PCMK_remote_port has invalid value '%s', using %d instead",
int crm_remote_ready(crm_remote_t *remote, int total_timeout)
gboolean crm_remote_recv(crm_remote_t *remote, int total_timeout, int *disconnected)
long crm_get_sbd_timeout(void)
uint32_t payload_compressed
const char * pcmk_strerror(int rc)
uint32_t payload_uncompressed
#define CRM_LOG_ASSERT(expr)
int crm_remote_accept(int ssock)
int crm_remote_tcp_connect(const char *host, int port)
Wrappers for and extensions to glib mainloop.
xmlNode * string2xml(const char *input)
#define DEFAULT_REMOTE_PORT
void crm_sockaddr2str(void *sa, char *s)
Convert an IP address (IPv4 or IPv6) to a string for logging.
#define crm_warn(fmt, args...)
#define crm_debug(fmt, args...)
int crm_initiate_client_tls_handshake(crm_remote_t *remote, int timeout_ms)
xmlNode * crm_remote_parse_buffer(crm_remote_t *remote)
#define crm_trace(fmt, args...)
enum crm_proc_flag __attribute__
int crm_set_nonblocking(int fd)
Wrappers for and extensions to libxml2.
int crm_remote_tcp_connect_async(const char *host, int port, int timeout, int *timer_id, void *userdata, void(*callback)(void *userdata, int sock))
#define crm_perror(level, fmt, args...)
Log a system error message.
#define REMOTE_MSG_VERSION
#define crm_err(fmt, args...)
const char * bz2_strerror(int rc)
char * dump_xml_unformatted(xmlNode *msg)
void * create_psk_tls_session(int csock, int type, void *credentials)
void * crm_create_anon_tls_session(int sock, int type, void *credentials)
int crm_remote_send(crm_remote_t *remote, xmlNode *msg)
#define crm_info(fmt, args...)
int crm_default_remote_port(void)
Get the default remote connection TCP port on this host.
enum crm_ais_msg_types type