WPILibC++ 2023.4.3-108-ge5452e3
util.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPINET_UV_UTIL_H_
6#define WPINET_UV_UTIL_H_
7
8#include <uv.h>
9
10#include <cstring>
11#include <string_view>
12
13#ifdef _WIN32
14#pragma comment(lib, "Ws2_32.lib")
15#endif
16
17namespace wpi::uv {
18
19/**
20 * Convert a binary structure containing an IPv4 address to a string.
21 * @param addr Binary structure
22 * @param ip Output string (any type that has `assign(char*, char*)`)
23 * @param port Output port number
24 * @return Error (same as `uv_ip4_name()`).
25 */
26template <typename T>
27int AddrToName(const sockaddr_in& addr, T* ip, unsigned int* port) {
28 char name[128];
29 int err = uv_ip4_name(&addr, name, 128);
30 if (err == 0) {
31 ip->assign(name, name + std::strlen(name));
32 *port = ntohs(addr.sin_port);
33 } else {
34 ip->assign(name, name);
35 }
36 return err;
37}
38
39/**
40 * Convert a binary structure containing an IPv6 address to a string.
41 * @param addr Binary structure
42 * @param ip Output string (any type that has `assign(char*, char*)`)
43 * @param port Output port number
44 * @return Error (same as `uv_ip6_name()`).
45 */
46template <typename T>
47int AddrToName(const sockaddr_in6& addr, T* ip, unsigned int* port) {
48 char name[128];
49 int err = uv_ip6_name(&addr, name, 128);
50 if (err == 0) {
51 ip->assign(name, name + std::strlen(name));
52 *port = ntohs(addr.sin6_port);
53 } else {
54 ip->assign(name, name);
55 }
56 return err;
57}
58
59/**
60 * Convert a binary structure containing an IPv4 or IPv6 address to a string.
61 * @param addr Binary structure
62 * @param ip Output string (any type that has `assign(char*, char*)`)
63 * @param port Output port number
64 * @return Error (same as `uv_ip6_name()`).
65 */
66template <typename T>
67int AddrToName(const sockaddr_storage& addr, T* ip, unsigned int* port) {
68 if (addr.ss_family == AF_INET) {
69 return AddrToName(reinterpret_cast<const sockaddr_in&>(addr), ip, port);
70 }
71 if (addr.ss_family == AF_INET6) {
72 return AddrToName(reinterpret_cast<const sockaddr_in6&>(addr), ip, port);
73 }
74 char name[1] = {'\0'};
75 ip->assign(name, name);
76 return -1;
77}
78
79/**
80 * Convert a binary IPv4 address to a string.
81 * @param addr Binary address
82 * @param ip Output string (any type that has `assign(char*, char*)`)
83 * @return Error (same as `uv_inet_ntop()`).
84 */
85template <typename T>
86int AddrToName(const in_addr& addr, T* ip) {
87 char name[128];
88 int err = uv_inet_ntop(AF_INET, &addr, name, 128);
89 if (err == 0) {
90 ip->assign(name, name + std::strlen(name));
91 } else {
92 ip->assign(name, name);
93 }
94 return err;
95}
96
97/**
98 * Convert a binary IPv6 address to a string.
99 * @param addr Binary address
100 * @param ip Output string (any type that has `assign(char*, char*)`)
101 * @return Error (same as `uv_inet_ntop()`).
102 */
103template <typename T>
104int AddrToName(const in6_addr& addr, T* ip) {
105 char name[128];
106 int err = uv_inet_ntop(AF_INET6, &addr, name, 128);
107 if (err == 0) {
108 ip->assign(name, name + std::strlen(name));
109 } else {
110 ip->assign(name, name);
111 }
112 return err;
113}
114
115/**
116 * Convert a string containing an IPv4 address to a binary structure.
117 * @param ip IPv4 address string
118 * @param port Port number
119 * @param addr Output binary structure
120 * @return Error (same as `uv_ip4_addr()`).
121 */
122int NameToAddr(std::string_view ip, unsigned int port, sockaddr_in* addr);
123
124/**
125 * Convert a string containing an IPv6 address to a binary structure.
126 * @param ip IPv6 address string
127 * @param port Port number
128 * @param addr Output binary structure
129 * @return Error (same as `uv_ip6_addr()`).
130 */
131int NameToAddr(std::string_view ip, unsigned int port, sockaddr_in6* addr);
132
133/**
134 * Convert a string containing an IPv4 address to binary format.
135 * @param ip IPv4 address string
136 * @param addr Output binary
137 * @return Error (same as `uv_inet_pton()`).
138 */
139int NameToAddr(std::string_view ip, in_addr* addr);
140
141/**
142 * Convert a string containing an IPv6 address to binary format.
143 * @param ip IPv6 address string
144 * @param addr Output binary
145 * @return Error (same as `uv_inet_pton()`).
146 */
147int NameToAddr(std::string_view ip, in6_addr* addr);
148
149} // namespace wpi::uv
150
151#endif // WPINET_UV_UTIL_H_
basic_string_view< char > string_view
Definition: core.h:520
Definition: Buffer.h:18
int NameToAddr(std::string_view ip, unsigned int port, sockaddr_in *addr)
Convert a string containing an IPv4 address to a binary structure.
int AddrToName(const sockaddr_in &addr, T *ip, unsigned int *port)
Convert a binary structure containing an IPv4 address to a string.
Definition: util.h:27
UV_EXTERN int uv_ip4_name(const struct sockaddr_in *src, char *dst, size_t size)
UV_EXTERN int uv_inet_ntop(int af, const void *src, char *dst, size_t size)
UV_EXTERN int uv_ip6_name(const struct sockaddr_in6 *src, char *dst, size_t size)