test/net/TestIPv[46]Address: fix Windows build errors

This commit is contained in:
Max Kellermann 2020-04-01 15:53:43 +02:00
parent 90184e0ce7
commit 60610e90b1
2 changed files with 22 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2012-2020 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -34,13 +34,22 @@
#include <stdexcept> #include <stdexcept>
#ifndef _WIN32
#include <arpa/inet.h> #include <arpa/inet.h>
#endif
static std::string static std::string
ToString(const struct in_addr &a) ToString(const struct in_addr &a)
{ {
#ifdef _WIN32
/* on mingw32, the parameter is non-const (PVOID) */
const auto p = const_cast<struct in_addr *>(&a);
#else
const auto p = &a;
#endif
char buffer[256]; char buffer[256];
const char *result = inet_ntop(AF_INET, &a, buffer, sizeof(buffer)); const char *result = inet_ntop(AF_INET, p, buffer, sizeof(buffer));
if (result == nullptr) if (result == nullptr)
throw std::runtime_error("inet_ntop() failed"); throw std::runtime_error("inet_ntop() failed");
return result; return result;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2012-2020 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -34,13 +34,22 @@
#include <stdexcept> #include <stdexcept>
#ifndef _WIN32
#include <arpa/inet.h> #include <arpa/inet.h>
#endif
static std::string static std::string
ToString(const struct in6_addr &a) ToString(const struct in6_addr &a)
{ {
#ifdef _WIN32
/* on mingw32, the parameter is non-const (PVOID) */
const auto p = const_cast<struct in6_addr *>(&a);
#else
const auto p = &a;
#endif
char buffer[256]; char buffer[256];
const char *result = inet_ntop(AF_INET6, &a, buffer, sizeof(buffer)); const char *result = inet_ntop(AF_INET6, p, buffer, sizeof(buffer));
if (result == nullptr) if (result == nullptr)
throw std::runtime_error("inet_ntop() failed"); throw std::runtime_error("inet_ntop() failed");
return result; return result;