[wpiutil] Add dual-IPv4/IPv6 uv::AddrToName() (#2545)

This commit is contained in:
Peter Johnson
2020-06-26 17:14:26 -07:00
committed by GitHub
parent 8a80f97c06
commit 00e991e2a0

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -57,6 +57,24 @@ int AddrToName(const sockaddr_in6& addr, T* ip, unsigned int* port) {
return err;
}
/**
* Convert a binary structure containing an IPv4 or IPv6 address to a string.
* @param addr Binary structure
* @param ip Output string (any type that has `assign(char*, char*)`)
* @param port Output port number
* @return Error (same as `uv_ip6_name()`).
*/
template <typename T>
int AddrToName(const sockaddr_storage& addr, T* ip, unsigned int* port) {
if (addr.ss_family == AF_INET)
return AddrToName(reinterpret_cast<const sockaddr_in&>(addr), ip, port);
if (addr.ss_family == AF_INET6)
return AddrToName(reinterpret_cast<const sockaddr_in6&>(addr), ip, port);
char name[1];
ip->assign(name, name);
return -1;
}
/**
* Convert a binary IPv4 address to a string.
* @param addr Binary address