summaryrefslogtreecommitdiffstats
path: root/src/server/helper.h
diff options
context:
space:
mode:
authorSimon Rettberg2013-07-31 21:45:00 +0200
committerSimon Rettberg2013-07-31 21:45:00 +0200
commite7b3781c3e5c105fcd80a75546b9ab75eae8a2c9 (patch)
tree90e523671bb2ea349639b744b5bc3ee81f512fb7 /src/server/helper.h
parent[SERVER] Still working on the uplink... Almost there (diff)
downloaddnbd3-e7b3781c3e5c105fcd80a75546b9ab75eae8a2c9.tar.gz
dnbd3-e7b3781c3e5c105fcd80a75546b9ab75eae8a2c9.tar.xz
dnbd3-e7b3781c3e5c105fcd80a75546b9ab75eae8a2c9.zip
[SERVER] Uplink handing complete (untested, as alt servers can't be defined yet, so prepare for lots of fixes ;))
Diffstat (limited to 'src/server/helper.h')
-rw-r--r--src/server/helper.h67
1 files changed, 33 insertions, 34 deletions
diff --git a/src/server/helper.h b/src/server/helper.h
index daa6695..e068abd 100644
--- a/src/server/helper.h
+++ b/src/server/helper.h
@@ -8,6 +8,9 @@
#include <unistd.h>
#include "../types.h"
+#define ERROR_GOTO(jumplabel, errormsg) do { memlogf(errormsg); goto jumplabel; } while (0);
+#define ERROR_GOTO_VA(jumplabel, errormsg, ...) do { memlogf(errormsg, __VA_ARGS__); goto jumplabel; } while (0);
+
char parse_address(char *string, dnbd3_host_t *host);
char host_to_string(const dnbd3_host_t *host, char *target, size_t targetlen);
char is_valid_namespace(char *namespace);
@@ -17,11 +20,9 @@ void remove_trailing_slash(char *string);
int file_exists(char *file);
int file_writable(char *file);
-static inline int is_same_server(const dnbd3_host_t *const a, const dnbd3_host_t *const b)
+static inline int is_same_server(const dnbd3_host_t * const a, const dnbd3_host_t * const b)
{
- return (a->type == b->type)
- && (a->port == b->port)
- && (0 == memcmp(a->addr, b->addr, (a->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) ));
}
/**
@@ -29,25 +30,24 @@ static inline int is_same_server(const dnbd3_host_t *const a, const dnbd3_host_t
*/
static inline int send_data(int client_sock, void *data_in, int len)
{
- if (len <= 0) // Nothing to send
- return 1;
+ 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
+ {
+ 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;
- if (ret < 0)
- {
- if (errno != EAGAIN) // Some unexpected error
- return 0;
- usleep(1000); // 1ms
+ usleep( 1000 ); // 1ms
continue;
}
len -= ret;
- if (len <= 0) // Sent everything
- return 1;
+ if ( len <= 0 ) // Sent everything
+ return 1;
data += ret; // move target buffer pointer
}
return 0;
@@ -58,25 +58,24 @@ static inline int send_data(int client_sock, void *data_in, int len)
*/
static inline int recv_data(int client_sock, void *buffer_out, int len)
{
- if (len <= 0) // Nothing to receive
- return 1;
+ 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
+ {
+ 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;
- if (ret < 0)
- {
- if (errno != EAGAIN) // Some unexpected error
- return 0;
- usleep(1000); // 1ms
+ usleep( 1000 ); // 1ms
continue;
}
len -= ret;
- if (len <= 0) // Received everything
- return 1;
+ if ( len <= 0 ) // Received everything
+ return 1;
data += ret; // move target buffer pointer
}
return 0;
@@ -84,12 +83,12 @@ static inline int recv_data(int client_sock, void *buffer_out, int len)
static inline int strend(char *string, char *suffix)
{
- if (string == NULL) return FALSE;
- if (suffix == NULL || *suffix == '\0') return TRUE;
- const size_t len1 = strlen(string);
- const size_t len2 = strlen(suffix);
- if (len2 > len1) return FALSE;
- return strcmp(string + len1 - len2, suffix) == 0;
+ if ( string == NULL ) return FALSE;
+ if ( suffix == NULL || *suffix == '\0' ) return TRUE;
+ const size_t len1 = strlen( string );
+ const size_t len2 = strlen( suffix );
+ if ( len2 > len1 ) return FALSE;
+ return strcmp( string + len1 - len2, suffix ) == 0;
}
#endif