Expand upon IpAddress header, take in feedback

pull/1257/head
Stephen Shelton 4 years ago
parent dff170712f
commit 1cab83ad01
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -60,7 +60,6 @@ set(LIB_PLATFORM_SRC
ev/ev_libuv.cpp
net/ip.cpp
net/net.cpp
net/net_addr.cpp
net/net_int.cpp
# for android shim
${ANDROID_PLATFORM_SRC}

@ -1,5 +1,7 @@
#pragma once
#include <net/sock_addr.hpp>
#include <string_view>
#include <string>
@ -12,42 +14,67 @@ namespace llarp
/// As a convenience, it can produce a SockAddr for dealing with network libraries which depend
/// sockaddr structs. However, it does not keep this as a member variable and isn't responsible
/// for its lifetime/memory/etc.
///
/// TODO: IPv6 is not currently supported.
struct IpAddress
{
/// Constructor. Takes a string which can be an IPv4 or IPv6 address optionally followed by
/// a colon and a port.
///
/// Examples:
///
/// 127.0.0.1
/// 1.2.3.4:53
///
/// Note that an IPv6 + port representation must be done in an unambiguous way, e.g. wrap the
/// IP portion of the string in brackets: [IPv6Addr]:port
///
/// @param str is a string representing an IP address and optionally a port
/// @throws std::invalid_argument if str cannot be parsed
IpAddress(std::string_view str);
/// Constructor. Takes an IP address (as above) and a port. The string may not contain a port.
IpAddress(std::string_view str, int32_t port);
///
/// @param str is a string representing an IP address and optionally a port
/// @throws std::invalid_argument if str cannot be parsed
IpAddress(std::string_view str, std::optional<uint16_t> port);
/// Return the port. Returns -1 if no port has been provided.
int32_t
///
/// @return the port, if present
std::optional<uint16_t>
getPort() const;
/// Set the port.
///
/// @param port
void
setPort(uint16_t port);
setPort(std::optional<uint16_t> port);
/// Returns true if this is an IPv4 address (or an IPv6 address representing an IPv4 address),
/// false otherwise.
/// Returns true if this is an IPv4 address (or an IPv6 address representing an IPv4 address)
///
/// TODO: could return an int (e.g. 4 or 6) or an enum
///
/// @return true if this is an IPv4 address, false otherwise
bool
isIPv4();
/// Creates an instance of SockAddr representing this IpAddress.
///
/// @return an instance of a SockAddr created from this IpAddress
SockAddr
createSockAddr() const;
// TODO: other utility functions left over from Addr which may be useful
// isTenPrivate(uint32_t byte);
// isOneSevenPrivate(uint32_t byte);
// isOneNinePrivate(uint32_t byte);
// IsBogon() const;
// isPrivate() const;
// struct Hash
// operator<(const Addr& other) const;
// operator==(const Addr& other) const;
// operator=(const sockaddr& other);
private:
std::string m_ipAddress;
std::optional<uint16_t> m_port;
};
} // namespace llarp

@ -1,5 +1,7 @@
#pragma once
#include <netinet/in.h>
#include <string_view>
#include <string>
@ -17,5 +19,7 @@ namespace llarp
SockAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port);
SockAddr(uint32_t addr);
SockAddr(std::string_view addr);
operator const sockaddr*() const;
};
} // namespace llarp

Loading…
Cancel
Save