summaryrefslogtreecommitdiffstats
path: root/src/server/helper.c
diff options
context:
space:
mode:
authorSimon Rettberg2014-12-31 23:39:21 +0100
committerSimon Rettberg2014-12-31 23:39:21 +0100
commit6788efa3459b9581b1e4197e37f504d8fb2e8e87 (patch)
treef177c92792884dd98c82b8d8828b092b2119a36c /src/server/helper.c
parent[SERVER] Minor tweaks and improvements (diff)
downloaddnbd3-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.c')
-rw-r--r--src/server/helper.c41
1 files changed, 21 insertions, 20 deletions
diff --git a/src/server/helper.c b/src/server/helper.c
index 8070fa0..87e0b7f 100644
--- a/src/server/helper.c
+++ b/src/server/helper.c
@@ -10,15 +10,16 @@
/**
* Parse IPv4 or IPv6 address in string representation to a suitable format usable by the BSD socket library
- * @string eg. "1.2.3.4" or "2a01::10:5", optially with port appended, eg "1.2.3.4:6666" or "[2a01::10:5]:6666"
- * @host pointer to dnbd3_host_t that will be filled with the following data:
- * type will contain either AF_INET or AF_INET6
- * addr will contain the address in network representation
- * port will contain the port in network representation, defaulting to #define PORT if none was given
- * returns TRUE on success, FALSE in failure. contents of af, addr and port are undefined in the latter case
- * !! Contents of @string might be modified by this function !!
+ * !! Contents of 'string' might be modified by this function !!
+ *
+ * @param string eg. "1.2.3.4" or "2a01::10:5", optially with port appended, eg "1.2.3.4:6666" or "[2a01::10:5]:6666"
+ * @param host pointer to dnbd3_host_t that will be filled with the following data:
+ * .type will contain either AF_INET or AF_INET6
+ * .addr will contain the address in network representation
+ * .port will contain the port in network representation, defaulting to #define PORT if none was given
+ * @return true on success, false in failure. contents of af, addr and port are undefined in the latter case
*/
-char parse_address(char *string, dnbd3_host_t *host)
+bool parse_address(char *string, dnbd3_host_t *host)
{
struct in_addr v4;
struct in6_addr v6;
@@ -29,14 +30,14 @@ char parse_address(char *string, dnbd3_host_t *host)
host->type = AF_INET;
memcpy( host->addr, &v4, 4 );
host->port = htons( PORT );
- return TRUE;
+ return true;
}
// Try IPv6 without port
if ( 1 == inet_pton( AF_INET6, string, &v6 ) ) {
host->type = AF_INET6;
memcpy( host->addr, &v6, 16 );
host->port = htons( PORT );
- return TRUE;
+ return true;
}
// Scan for port
@@ -45,7 +46,7 @@ char parse_address(char *string, dnbd3_host_t *host)
if ( *ptr == ':' ) portpos = ptr;
++ptr;
}
- if ( portpos == NULL ) return FALSE; // No port in string
+ if ( portpos == NULL ) return false; // No port in string
// Consider IP being surrounded by [ ]
if ( *string == '[' && *(portpos - 1) == ']' ) {
++string;
@@ -53,35 +54,35 @@ char parse_address(char *string, dnbd3_host_t *host)
}
*portpos++ = '\0';
int p = atoi( portpos );
- if ( p < 1 || p > 65535 ) return FALSE; // Invalid port
+ if ( p < 1 || p > 65535 ) return false; // Invalid port
host->port = htons( (uint16_t)p );
// Try IPv4 with port
if ( 1 == inet_pton( AF_INET, string, &v4 ) ) {
host->type = AF_INET;
memcpy( host->addr, &v4, 4 );
- return TRUE;
+ return true;
}
// Try IPv6 with port
if ( 1 == inet_pton( AF_INET6, string, &v6 ) ) {
host->type = AF_INET6;
memcpy( host->addr, &v6, 16 );
- return TRUE;
+ return true;
}
// FAIL
- return FALSE;
+ return false;
}
/**
* Convert a host and port (network byte order) to printable representation.
* Worst case required buffer len is 48, eg. [1234:1234:1234:1234:1234:1234:1234:1234]:12345 (+ \0)
- * Returns TRUE on success, FALSE on error
+ * Returns true on success, false on error
*/
-char host_to_string(const dnbd3_host_t *host, char *target, size_t targetlen)
+bool host_to_string(const dnbd3_host_t *host, char *target, size_t targetlen)
{
// Worst case: Port 5 chars, ':' to separate ip and port 1 char, terminating null 1 char = 7, [] for IPv6
- if ( targetlen < 10 ) return FALSE;
+ if ( targetlen < 10 ) return false;
if ( host->type == AF_INET6 ) {
*target++ = '[';
inet_ntop( AF_INET6, host->addr, target, targetlen - 10 );
@@ -92,14 +93,14 @@ char host_to_string(const dnbd3_host_t *host, char *target, size_t targetlen)
target += strlen( target );
} else {
snprintf( target, targetlen, "<?addrtype=%d>", (int)host->type );
- return FALSE;
+ return false;
}
*target = '\0';
if ( host->port != 0 ) {
// There are still at least 7 bytes left in the buffer, port is at most 5 bytes + ':' + '\0' = 7
snprintf( target, 7, ":%d", (int)ntohs( host->port ) );
}
- return TRUE;
+ return true;
}
void strtolower(char *string)