summaryrefslogtreecommitdiffstats
path: root/src/client
diff options
context:
space:
mode:
authorSimon Rettberg2017-10-28 14:56:58 +0200
committerSimon Rettberg2017-10-28 14:56:58 +0200
commitc8c62246f84b5d9d4a496097f043696e2d9ba0bb (patch)
tree27a896f6677791ef8087fa44b1aab7cb2071d774 /src/client
parent[SERVER] Add function to parse x-www-form-urlencoded strings (diff)
downloaddnbd3-c8c62246f84b5d9d4a496097f043696e2d9ba0bb.tar.gz
dnbd3-c8c62246f84b5d9d4a496097f043696e2d9ba0bb.tar.xz
dnbd3-c8c62246f84b5d9d4a496097f043696e2d9ba0bb.zip
[*] Introduce constants for IPv4/6 in dnbd3_host_t
AF_INET luckily was "2" on all platforms checked, so no problems there with interoperation, but AF_INET6 is different between Linux, BSD, Windows and possibly others, so map back and forth between AF_INET/AF_INET6 and HOST_IP4/HOST_IP6 to fix this.
Diffstat (limited to 'src/client')
-rw-r--r--src/client/client.c17
1 files changed, 9 insertions, 8 deletions
diff --git a/src/client/client.c b/src/client/client.c
index 685dbfd..37f0558 100644
--- a/src/client/client.c
+++ b/src/client/client.c
@@ -82,12 +82,12 @@ static char host_to_string(const dnbd3_host_t *host, char *target, size_t target
{
// 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 ( host->type == AF_INET6 ) {
+ if ( host->type == HOST_IP6 ) {
*target++ = '[';
inet_ntop( AF_INET6, host->addr, target, targetlen - 10 );
target += strlen( target );
*target++ = ']';
- } else if ( host->type == AF_INET ) {
+ } else if ( host->type == HOST_IP4 ) {
inet_ntop( AF_INET, host->addr, target, targetlen - 8 );
target += strlen( target );
} else {
@@ -106,7 +106,7 @@ static char host_to_string(const dnbd3_host_t *host, char *target, size_t target
/**
* 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"
- * @af will contain either AF_INET or AF_INET6
+ * @af will contain either HOST_IP4 or HOST_IP6
* @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 1 on success, 0 in failure. contents of af, addr and port are undefined in the latter case
@@ -119,14 +119,14 @@ static char parse_address(char *string, dnbd3_host_t *host)
// Try IPv4 without port
if ( 1 == inet_pton( AF_INET, string, &v4 ) ) {
- host->type = AF_INET;
+ host->type = HOST_IP4;
memcpy( host->addr, &v4, 4 );
host->port = htons( PORT );
return 1;
}
// Try IPv6 without port
if ( 1 == inet_pton( AF_INET6, string, &v6 ) ) {
- host->type = AF_INET6;
+ host->type = HOST_IP6;
memcpy( host->addr, &v6, 16 );
host->port = htons( PORT );
return 1;
@@ -153,13 +153,13 @@ static char parse_address(char *string, dnbd3_host_t *host)
// Try IPv4 with port
if ( 1 == inet_pton( AF_INET, string, &v4 ) ) {
- host->type = AF_INET;
+ host->type = HOST_IP4;
memcpy( host->addr, &v4, 4 );
return 1;
}
// Try IPv6 with port
if ( 1 == inet_pton( AF_INET6, string, &v6 ) ) {
- host->type = AF_INET6;
+ host->type = HOST_IP6;
memcpy( host->addr, &v6, 16 );
return 1;
}
@@ -178,10 +178,11 @@ static int dnbd3_get_ip(char *hostname, dnbd3_host_t *host)
return false;
}
- host->type = (uint8_t)hent->h_addrtype;
if ( hent->h_addrtype == AF_INET ) {
+ host->type = HOST_IP4;
memcpy( host->addr, hent->h_addr, 4);
} else if (hent->h_addrtype == AF_INET6) {
+ host->type = HOST_IP6;
memcpy(host->addr, hent->h_addr, 16);
} else {
printf("FATAL: Unknown address type: %d\n", hent->h_addrtype);