From 6788efa3459b9581b1e4197e37f504d8fb2e8e87 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 31 Dec 2014 23:39:21 +0100 Subject: [SERVER] Use stdbool.h for booleans; minor refactoring of variable and function names --- src/server/helper.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'src/server/helper.c') 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, "", (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) -- cgit v1.2.3-55-g7522