From 00e991e2a07fde6757dfea14a53cd81fe8bd2398 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 26 Jun 2020 17:14:26 -0700 Subject: [PATCH] [wpiutil] Add dual-IPv4/IPv6 uv::AddrToName() (#2545) --- wpiutil/src/main/native/include/wpi/uv/util.h | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/wpiutil/src/main/native/include/wpi/uv/util.h b/wpiutil/src/main/native/include/wpi/uv/util.h index 3795aa879f..14a021df7a 100644 --- a/wpiutil/src/main/native/include/wpi/uv/util.h +++ b/wpiutil/src/main/native/include/wpi/uv/util.h @@ -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 +int AddrToName(const sockaddr_storage& addr, T* ip, unsigned int* port) { + if (addr.ss_family == AF_INET) + return AddrToName(reinterpret_cast(addr), ip, port); + if (addr.ss_family == AF_INET6) + return AddrToName(reinterpret_cast(addr), ip, port); + char name[1]; + ip->assign(name, name); + return -1; +} + /** * Convert a binary IPv4 address to a string. * @param addr Binary address