diff options
author | Simon Rettberg | 2014-12-31 23:39:21 +0100 |
---|---|---|
committer | Simon Rettberg | 2014-12-31 23:39:21 +0100 |
commit | 6788efa3459b9581b1e4197e37f504d8fb2e8e87 (patch) | |
tree | f177c92792884dd98c82b8d8828b092b2119a36c /src/server/helper.h | |
parent | [SERVER] Minor tweaks and improvements (diff) | |
download | dnbd3-6788efa3459b9581b1e4197e37f504d8fb2e8e87.tar.gz dnbd3-6788efa3459b9581b1e4197e37f504d8fb2e8e87.tar.xz dnbd3-6788efa3459b9581b1e4197e37f504d8fb2e8e87.zip |
[SERVER] Use stdbool.h for booleans; minor refactoring of variable and function names
Diffstat (limited to 'src/server/helper.h')
-rw-r--r-- | src/server/helper.h | 56 |
1 files changed, 27 insertions, 29 deletions
diff --git a/src/server/helper.h b/src/server/helper.h index 95a5157..0ecbdfd 100644 --- a/src/server/helper.h +++ b/src/server/helper.h @@ -11,87 +11,85 @@ #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); +bool parse_address(char *string, dnbd3_host_t *host); +bool host_to_string(const dnbd3_host_t *host, char *target, size_t targetlen); void strtolower(char *string); void remove_trailing_slash(char *string); void trim_right(char * const string); void setThreadName(char *name); void blockNoncriticalSignals(); -static inline int isSameAddress(const dnbd3_host_t * const a, const dnbd3_host_t * const b) +static inline bool isSameAddress(const dnbd3_host_t * const a, const dnbd3_host_t * const b) { return (a->type == b->type) && (0 == memcmp( a->addr, b->addr, (a->type == AF_INET ? 4 : 16) )); } -static inline int isSameAddressPort(const dnbd3_host_t * const a, const dnbd3_host_t * const b) +static inline bool isSameAddressPort(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) )); } /** - * Send message to client, return !=0 on success, 0 on failure + * Send message to client. + * @return true on success, false on failure */ static inline int send_data(int client_sock, void *data_in, int len) { - if ( len <= 0 ) // Nothing to send - return 1; + if ( len <= 0 ) return true; // Nothing to send 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 ) return false; // Connection closed if ( ret < 0 ) { - if ( errno != EAGAIN ) // Some unexpected error - return 0; + if ( errno != EAGAIN ) return false; // Some unexpected error usleep( 1000 ); // 1ms continue; } len -= ret; - if ( len <= 0 ) // Sent everything - return 1; + if ( len <= 0 ) return true; // Sent everything data += ret; // move target buffer pointer } - return 0; + return false; } /** - * Receive data from client, return !=0 on success, 0 on failure + * Receive data from client. + * @return true on success, false otherwise */ -static inline int recv_data(int client_sock, void *buffer_out, int len) +static inline bool recv_data(int client_sock, void *buffer_out, int len) { - if ( len <= 0 ) // Nothing to receive - return 1; + if ( len <= 0 ) return true; // Nothing to receive 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 ) return false; // Connection closed if ( ret < 0 ) { - if ( errno != EAGAIN ) // Some unexpected error - return 0; + if ( errno != EAGAIN ) return false; // Some unexpected error usleep( 1000 ); // 1ms continue; } len -= ret; - if ( len <= 0 ) // Received everything - return 1; + if ( len <= 0 ) return true; // Received everything data += ret; // move target buffer pointer } - return 0; + return false; } +/** + * Test whether string ends in suffix. + * @return true if string =~ /suffix$/ + */ static inline int strend(char *string, char *suffix) { - if ( string == NULL ) return FALSE; - if ( suffix == NULL || *suffix == '\0' ) return TRUE; + 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; + if ( len2 > len1 ) return false; return strcmp( string + len1 - len2, suffix ) == 0; } |