summaryrefslogtreecommitdiffstats
path: root/src/server/helper.h
diff options
context:
space:
mode:
authorsr2012-09-04 19:22:57 +0200
committersr2012-09-04 19:22:57 +0200
commitbc4e381484024237df0b04bb667f742fe4846b35 (patch)
treea28ee79b9999eb9c1436f0f7f6e7042537b413d2 /src/server/helper.h
parent[SERVER] Check which dnbd3 devices are idle and ready to use for proxy mode (diff)
downloaddnbd3-bc4e381484024237df0b04bb667f742fe4846b35.tar.gz
dnbd3-bc4e381484024237df0b04bb667f742fe4846b35.tar.xz
dnbd3-bc4e381484024237df0b04bb667f742fe4846b35.zip
[SERVER] More work towards automatic server discovery and querying
Diffstat (limited to 'src/server/helper.h')
-rw-r--r--src/server/helper.h68
1 files changed, 64 insertions, 4 deletions
diff --git a/src/server/helper.h b/src/server/helper.h
index e787c42..fdcc3b5 100644
--- a/src/server/helper.h
+++ b/src/server/helper.h
@@ -4,6 +4,8 @@
#include "server.h"
#include <netinet/in.h>
#include <string.h>
+#include <errno.h>
+#include <unistd.h>
char parse_address(char *string, dnbd3_host_t *host);
char host_to_string(const dnbd3_host_t *host, char *target, size_t targetlen);
@@ -11,11 +13,69 @@ char is_valid_namespace(char *namespace);
char is_valid_imagename(char *namespace);
void strtolower(char *string);
-static inline int is_same_server(const dnbd3_trusted_server_t *const a, const dnbd3_trusted_server_t *const b)
+static inline int is_same_server(const dnbd3_host_t *const a, const dnbd3_host_t *const b)
{
- return (a->host.type == b->host.type)
- && (a->host.port == b->host.port)
- && (0 == memcmp(a->host.addr, b->host.addr, (a->host.type == AF_INET ? 4 : 16)));
+ return (a->type == b->type)
+ && (a->port == b->port)
+ && (0 == memcmp(a->addr, b->addr, (a->type == AF_INET ? 4 : 16)));
+}
+
+/**
+ * Send message to client, return !=0 on success, 0 on failure
+ */
+static inline int send_data(int client_sock, void *data_in, int len)
+{
+ if (len <= 0) // Nothing to send
+ return 1;
+ char *data = data_in; // Needed for pointer arithmetic
+ int ret, i;
+ for (i = 0; i < 3; ++i) // Retry at most 3 times, each try takes at most 0.5 seconds (socket timeout)
+ {
+ ret = send(client_sock, data, len, 0);
+ if (ret == 0) // Connection closed
+ return 0;
+ if (ret < 0)
+ {
+ if (errno != EAGAIN) // Some unexpected error
+ return 0;
+ usleep(1000); // 1ms
+ continue;
+ }
+ len -= ret;
+ if (len <= 0) // Sent everything
+ return 1;
+ data += ret; // move target buffer pointer
+ }
+ return 0;
+}
+
+/**
+ * Receive data from client, return !=0 on success, 0 on failure
+ */
+static inline int recv_data(int client_sock, void *buffer_out, int len)
+{
+ if (len <= 0) // Nothing to receive
+ return 1;
+ char *data = buffer_out; // Needed for pointer arithmetic
+ int ret, i;
+ for (i = 0; i < 3; ++i) // Retry at most 3 times, each try takes at most 0.5 seconds (socket timeout)
+ {
+ ret = recv(client_sock, data, len, MSG_WAITALL);
+ if (ret == 0) // Connection closed
+ return 0;
+ if (ret < 0)
+ {
+ if (errno != EAGAIN) // Some unexpected error
+ return 0;
+ usleep(1000); // 1ms
+ continue;
+ }
+ len -= ret;
+ if (len <= 0) // Received everything
+ return 1;
+ data += ret; // move target buffer pointer
+ }
+ return 0;
}
// one byte in the map covers 8 4kib blocks, so 32kib per byte