13 #include <sys/param.h> 15 #include <sys/types.h> 18 #include <sys/socket.h> 19 #include <arpa/inet.h> 20 #include <netinet/in.h> 21 #include <netinet/ip.h> 22 #include <netinet/tcp.h> 37 #include <gnutls/gnutls.h> 40 #ifdef HAVE_LINUX_SWAB_H 41 # include <linux/swab.h> 47 #define __swab16(x) ((uint16_t)( \ 48 (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \ 49 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8))) 51 #define __swab32(x) ((uint32_t)( \ 52 (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \ 53 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \ 54 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \ 55 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24))) 57 #define __swab64(x) ((uint64_t)( \ 58 (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \ 59 (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ 60 (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ 61 (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ 62 (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ 63 (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ 64 (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ 65 (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56))) 68 #define REMOTE_MSG_VERSION 1 69 #define ENDIAN_LOCAL 0xBADADBBD 71 struct remote_header_v0 {
96 static struct remote_header_v0 *
99 struct remote_header_v0 *header = (
struct remote_header_v0 *)remote->
buffer;
100 if(remote->
buffer_offset <
sizeof(
struct remote_header_v0)) {
108 crm_err(
"Invalid message detected, endian mismatch: %" PRIx32
109 " is neither %" PRIx32
" nor the swab'd %" PRIx32,
115 header->flags =
__swab64(header->flags);
116 header->endian =
__swab32(header->endian);
118 header->version =
__swab32(header->version);
119 header->size_total =
__swab32(header->size_total);
120 header->payload_offset =
__swab32(header->payload_offset);
121 header->payload_compressed =
__swab32(header->payload_compressed);
122 header->payload_uncompressed =
__swab32(header->payload_uncompressed);
130 send_tls(gnutls_session_t session,
struct iovec *iov)
132 const char *unsent = iov->iov_base;
133 size_t unsent_len = iov->iov_len;
136 if (unsent == NULL) {
140 crm_trace(
"Sending TLS message of %llu bytes",
141 (
unsigned long long) unsent_len);
143 gnutls_rc = gnutls_record_send(session, unsent, unsent_len);
145 if (gnutls_rc == GNUTLS_E_INTERRUPTED || gnutls_rc == GNUTLS_E_AGAIN) {
146 crm_trace(
"Retrying to send %llu bytes remaining",
147 (
unsigned long long) unsent_len);
149 }
else if (gnutls_rc < 0) {
151 crm_info(
"TLS connection terminated: %s " QB_XS
" rc=%lld",
152 gnutls_strerror((
int) gnutls_rc),
153 (
long long) gnutls_rc);
156 }
else if (gnutls_rc < unsent_len) {
157 crm_trace(
"Sent %lld of %llu bytes remaining",
158 (
long long) gnutls_rc, (
unsigned long long) unsent_len);
159 unsent_len -= gnutls_rc;
162 crm_trace(
"Sent all %lld bytes remaining", (
long long) gnutls_rc);
171 send_plaintext(
int sock,
struct iovec *iov)
173 const char *unsent = iov->iov_base;
174 size_t unsent_len = iov->iov_len;
177 if (unsent == NULL) {
181 crm_debug(
"Sending plaintext message of %llu bytes to socket %d",
182 (
unsigned long long) unsent_len, sock);
184 write_rc = write(sock, unsent, unsent_len);
188 if ((errno == EINTR) || (errno == EAGAIN)) {
189 crm_trace(
"Retrying to send %llu bytes remaining to socket %d",
190 (
unsigned long long) unsent_len, sock);
195 crm_info(
"Could not send message: %s " QB_XS
" rc=%d socket=%d",
199 }
else if (write_rc < unsent_len) {
200 crm_trace(
"Sent %lld of %llu bytes remaining",
201 (
long long) write_rc, (
unsigned long long) unsent_len);
203 unsent_len -= write_rc;
207 crm_trace(
"Sent all %lld bytes remaining: %.100s",
208 (
long long) write_rc, (
char *) (iov->iov_base));
217 remote_send_iovs(
pcmk__remote_t *remote,
struct iovec *iov,
int iovs)
221 for (
int lpc = 0; (lpc < iovs) && (rc ==
pcmk_rc_ok); lpc++) {
227 rc = send_plaintext(remote->
tcp_socket, &(iov[lpc]));
229 rc = ESOCKTNOSUPPORT;
248 static uint64_t
id = 0;
249 GString *xml_text = NULL;
252 struct remote_header_v0 *header;
254 CRM_CHECK((remote != NULL) && (msg != NULL),
return EINVAL);
256 xml_text = g_string_sized_new(1024);
259 g_string_free(xml_text, TRUE);
return EINVAL);
263 iov[0].iov_base = header;
264 iov[0].iov_len =
sizeof(
struct remote_header_v0);
266 iov[1].iov_len = 1 + xml_text->len;
267 iov[1].iov_base = g_string_free(xml_text, FALSE);
273 header->payload_offset = iov[0].iov_len;
274 header->payload_uncompressed = iov[1].iov_len;
275 header->size_total = iov[0].iov_len + iov[1].iov_len;
277 rc = remote_send_iovs(remote, iov, 2);
279 crm_err(
"Could not send remote message: %s " QB_XS
" rc=%d",
283 free(iov[0].iov_base);
284 g_free((gchar *) iov[1].iov_base);
301 struct remote_header_v0 *header = localized_remote_header(remote);
303 if (header == NULL) {
308 if (header->payload_compressed) {
310 unsigned int size_u = 1 + header->payload_uncompressed;
314 crm_trace(
"Decompressing message data %d bytes into %d bytes",
315 header->payload_compressed, size_u);
317 rc = BZ2_bzBuffToBuffDecompress(uncompressed + header->payload_offset, &size_u,
318 remote->
buffer + header->payload_offset,
319 header->payload_compressed, 1, 0);
323 crm_warn(
"Couldn't decompress v%d message, we only understand v%d",
329 crm_err(
"Decompression failed: %s " QB_XS
" rc=%d",
337 memcpy(uncompressed, remote->
buffer, header->payload_offset);
338 remote->
buffer_size = header->payload_offset + size_u;
341 remote->
buffer = uncompressed;
342 header = localized_remote_header(remote);
348 CRM_LOG_ASSERT(remote->
buffer[
sizeof(
struct remote_header_v0) + header->payload_uncompressed - 1] == 0);
352 crm_warn(
"Couldn't parse v%d message, we only understand v%d",
355 }
else if (xml == NULL) {
356 crm_err(
"Couldn't parse: '%.120s'", remote->
buffer + header->payload_offset);
367 void *sock_ptr = gnutls_transport_get_ptr(remote->
tls_session);
369 return GPOINTER_TO_INT(sock_ptr);
376 crm_err(
"Remote connection type undetermined (bug?)");
394 struct pollfd fds = { 0, };
400 sock = get_remote_socket(remote);
415 if (errno == EINTR && (
timeout > 0)) {
416 timeout = timeout_ms - ((time(NULL) - start) * 1000);
423 }
while (rc < 0 && errno == EINTR);
447 size_t read_len =
sizeof(
struct remote_header_v0);
448 struct remote_header_v0 *header = localized_remote_header(remote);
453 read_len = header->size_total;
459 crm_trace(
"Expanding buffer to %llu bytes",
468 if (read_rc == GNUTLS_E_INTERRUPTED) {
470 }
else if (read_rc == GNUTLS_E_AGAIN) {
472 }
else if (read_rc < 0) {
473 crm_debug(
"TLS receive failed: %s (%lld)",
474 gnutls_strerror(read_rc), (
long long) read_rc);
485 crm_err(
"Remote connection type undetermined (bug?)");
486 return ESOCKTNOSUPPORT;
494 crm_trace(
"Received %lld more bytes (%llu total)",
498 }
else if ((rc == EINTR) || (rc == EAGAIN)) {
499 crm_trace(
"No data available for non-blocking remote read: %s (%d)",
502 }
else if (read_rc == 0) {
503 crm_debug(
"End of remote data encountered after %llu bytes",
508 crm_debug(
"Error receiving remote data after %llu bytes: %s (%d)",
514 header = localized_remote_header(remote);
517 crm_trace(
"Read partial remote message (%llu of %u bytes)",
521 crm_trace(
"Read full remote message of %llu bytes",
544 time_t start = time(NULL);
545 int remaining_timeout = 0;
547 if (timeout_ms == 0) {
549 }
else if (timeout_ms < 0) {
553 remaining_timeout = timeout_ms;
554 while (remaining_timeout > 0) {
556 crm_trace(
"Waiting for remote data (%d ms of %d ms timeout remaining)",
557 remaining_timeout, timeout_ms);
561 crm_err(
"Timed out (%d ms) while waiting for remote data",
566 crm_debug(
"Wait for remote data aborted (will retry): %s " 573 }
else if (rc == EAGAIN) {
574 crm_trace(
"Waiting for more remote data");
576 crm_debug(
"Could not receive remote data: %s " QB_XS
" rc=%d",
582 if ((rc == ENOTCONN) || (rc == ESOCKTNOSUPPORT)) {
586 remaining_timeout = timeout_ms - ((time(NULL) - start) * 1000);
591 struct tcp_async_cb_data {
596 void (*callback) (
void *userdata,
int rc,
int sock);
601 check_connect_finished(gpointer userdata)
603 struct tcp_async_cb_data *cb_data = userdata;
607 struct timeval ts = { 0, };
609 if (cb_data->start == 0) {
617 FD_SET(cb_data->sock, &rset);
619 rc = select(cb_data->sock + 1, &rset, &wset, NULL, &ts);
623 if ((rc == EINPROGRESS) || (rc == EAGAIN)) {
630 crm_trace(
"Could not check socket %d for connection success: %s (%d)",
633 }
else if (rc == 0) {
637 crm_debug(
"Timed out while waiting for socket %d connection success",
643 }
else if (FD_ISSET(cb_data->sock, &rset)
644 || FD_ISSET(cb_data->sock, &wset)) {
648 socklen_t len =
sizeof(error);
650 if (getsockopt(cb_data->sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
652 crm_trace(
"Couldn't check socket %d for connection errors: %s (%d)",
654 }
else if (error != 0) {
656 crm_trace(
"Socket %d connected with error: %s (%d)",
663 crm_trace(
"select() succeeded, but socket %d not in resulting " 664 "read/write sets", cb_data->sock);
670 crm_trace(
"Socket %d is connected", cb_data->sock);
672 close(cb_data->sock);
676 if (cb_data->callback) {
677 cb_data->callback(cb_data->userdata, rc, cb_data->sock);
702 connect_socket_retry(
int sock,
const struct sockaddr *addr, socklen_t addrlen,
703 int timeout_ms,
int *timer_id,
void *userdata,
704 void (*callback) (
void *userdata,
int rc,
int sock))
709 struct tcp_async_cb_data *cb_data = NULL;
713 crm_warn(
"Could not set socket non-blocking: %s " QB_XS
" rc=%d",
718 rc = connect(sock, addr, addrlen);
719 if (rc < 0 && (errno != EINPROGRESS) && (errno != EAGAIN)) {
721 crm_warn(
"Could not connect socket: %s " QB_XS
" rc=%d",
727 cb_data->userdata = userdata;
728 cb_data->callback = callback;
729 cb_data->sock = sock;
730 cb_data->timeout_ms = timeout_ms;
740 cb_data->start = time(NULL);
752 crm_trace(
"Scheduling check in %dms for whether connect to fd %d finished",
775 connect_socket_once(
int sock,
const struct sockaddr *addr, socklen_t addrlen)
777 int rc = connect(sock, addr, addrlen);
781 crm_warn(
"Could not connect socket: %s " QB_XS
" rc=%d",
788 crm_warn(
"Could not set socket non-blocking: %s " QB_XS
" rc=%d",
814 int *sock_fd,
void *userdata,
815 void (*callback) (
void *userdata,
int rc,
int sock))
817 char buffer[INET6_ADDRSTRLEN];
818 struct addrinfo *res = NULL;
819 struct addrinfo *rp = NULL;
820 struct addrinfo hints;
821 const char *server =
host;
825 CRM_CHECK((
host != NULL) && (sock_fd != NULL),
return EINVAL);
828 memset(&hints, 0,
sizeof(
struct addrinfo));
829 hints.ai_family = AF_UNSPEC;
830 hints.ai_socktype = SOCK_STREAM;
831 hints.ai_flags = AI_CANONNAME;
833 rc = getaddrinfo(server, NULL, &hints, &res);
837 crm_err(
"Unable to get IP address info for %s: %s",
842 if (!res || !res->ai_addr) {
843 crm_err(
"Unable to get IP address info for %s: no result", server);
849 for (rp = res; rp != NULL; rp = rp->ai_next) {
850 struct sockaddr *addr = rp->ai_addr;
856 if (rp->ai_canonname) {
857 server = res->ai_canonname;
861 sock = socket(rp->ai_family, SOCK_STREAM, IPPROTO_TCP);
864 crm_warn(
"Could not create socket for remote connection to %s:%d: " 865 "%s " QB_XS
" rc=%d", server, port,
pcmk_rc_str(rc), rc);
871 if (addr->sa_family == AF_INET6) {
872 ((
struct sockaddr_in6 *)(
void*)addr)->sin6_port = htons(port);
874 ((
struct sockaddr_in *)(
void*)addr)->sin_port = htons(port);
879 crm_info(
"Attempting remote connection to %s:%d", buffer, port);
882 if (connect_socket_retry(sock, rp->ai_addr, rp->ai_addrlen,
timeout,
883 timer_id, userdata, callback) ==
pcmk_rc_ok) {
887 }
else if (connect_socket_once(sock, rp->ai_addr,
921 switch (((
const struct sockaddr *) sa)->sa_family) {
923 inet_ntop(AF_INET, &(((
const struct sockaddr_in *) sa)->sin_addr),
924 s, INET6_ADDRSTRLEN);
929 &(((
const struct sockaddr_in6 *) sa)->sin6_addr),
930 s, INET6_ADDRSTRLEN);
934 strcpy(s,
"<invalid>");
951 struct sockaddr_storage addr;
952 socklen_t laddr =
sizeof(addr);
953 char addr_str[INET6_ADDRSTRLEN];
954 #ifdef TCP_USER_TIMEOUT 955 long sbd_timeout = 0;
959 memset(&addr, 0,
sizeof(addr));
960 *csock = accept(ssock, (
struct sockaddr *)&addr, &laddr);
963 crm_err(
"Could not accept remote client connection: %s " 968 crm_info(
"Accepted new remote client connection from %s", addr_str);
972 crm_err(
"Could not set socket non-blocking: %s " QB_XS
" rc=%d",
979 #ifdef TCP_USER_TIMEOUT 981 if (sbd_timeout > 0) {
983 long half = sbd_timeout / 2;
984 unsigned int optval = (half <= UINT_MAX)? half : UINT_MAX;
986 rc = setsockopt(*csock, SOL_TCP, TCP_USER_TIMEOUT,
987 &optval,
sizeof(optval));
990 crm_err(
"Could not set TCP timeout to %d ms on remote connection: " 991 "%s " QB_XS
" rc=%d", optval,
pcmk_rc_str(rc), rc);
1010 static int port = 0;
1017 port = strtol(env, NULL, 10);
1018 if (errno || (port < 1) || (port > 65535)) {
1020 " has invalid value '%s', using %d instead",
#define CRM_CHECK(expr, failure_action)
int pcmk__set_nonblocking(int fd)
uint32_t payload_compressed
void pcmk__xml_string(const xmlNode *data, uint32_t options, GString *buffer, int depth)
#define PCMK__ENV_REMOTE_PORT
uint32_t payload_uncompressed
#define CRM_LOG_ASSERT(expr)
int crm_default_remote_port(void)
Get the default remote connection TCP port on this host.
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
const char * pcmk__env_option(const char *option)
Wrappers for and extensions to glib mainloop.
void pcmk__sockaddr2str(const void *sa, char *s)
#define DEFAULT_REMOTE_PORT
#define crm_warn(fmt, args...)
#define crm_debug(fmt, args...)
int pcmk__remote_ready(const pcmk__remote_t *remote, int timeout_ms)
guint pcmk__create_timer(guint interval_ms, GSourceFunc fn, gpointer data)
#define crm_trace(fmt, args...)
Wrappers for and extensions to libxml2.
int pcmk__read_available_remote_data(pcmk__remote_t *remote)
long pcmk__get_sbd_watchdog_timeout(void)
xmlNode * pcmk__xml_parse(const char *input)
struct tcp_async_cb_data __attribute__
#define pcmk__assert(expr)
int pcmk__bzlib2rc(int bz2)
Map a bz2 return code to the most similar Pacemaker return code.
#define REMOTE_MSG_VERSION
int pcmk__gaierror2rc(int gai)
Map a getaddrinfo() return code to the most similar Pacemaker return code.
#define crm_err(fmt, args...)
guint pcmk__timeout_ms2s(guint timeout_ms)
int pcmk__accept_remote_connection(int ssock, int *csock)
gnutls_session_t tls_session
#define crm_log_xml_trace(xml, text)
int pcmk__connect_remote(const char *host, int port, int timeout, int *timer_id, int *sock_fd, void *userdata, void(*callback)(void *userdata, int rc, int sock))
int pcmk__read_remote_message(pcmk__remote_t *remote, int timeout_ms)
xmlNode * pcmk__remote_message_xml(pcmk__remote_t *remote)
#define pcmk__assert_alloc(nmemb, size)
#define crm_info(fmt, args...)
int pcmk__remote_send_xml(pcmk__remote_t *remote, const xmlNode *msg)