aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorGravatar Sadie Powell2022-08-11 14:20:18 +0100
committerGravatar Sadie Powell2022-08-11 15:40:57 +0100
commitf88fbaa2fe91c22f1c7280ecd96c2bcf92f73a4d (patch)
treee35803070a6ca8e50ee2d07b2e6c0d71230d53f3 /include
parentClean up the sockaddrs documentation comments. (diff)
Move aptosa/untosa into the sockaddrs union and add from/from_ip.
The struct will also now always be zero-initialized by default which removes the footgun which has happened previously where the union has been accessed before being initialized leading to it containing weird values.
Diffstat (limited to 'include')
-rw-r--r--include/socket.h43
1 files changed, 28 insertions, 15 deletions
diff --git a/include/socket.h b/include/socket.h
index a490ebcaf..8347ef84d 100644
--- a/include/socket.h
+++ b/include/socket.h
@@ -56,12 +56,40 @@ namespace irc
struct sockaddr_in6 in6;
struct sockaddr_un un;
+ /** Initializes this sockaddrs optionally as an unspecified socket address. */
+ explicit sockaddrs(bool initialize = true);
+
/** Returns the address segment of the socket address as a string. */
std::string addr() const;
/** Returns the family of the socket address (e.g. AF_INET). */
int family() const;
+ /** Store an IP address or UNIX socket path in this socket address.
+ * @param addr An IPv4 address, IPv6 address, or UNIX socket path.
+ * @return True if the IP address or UNIX socket paht was stored in this socket address; otherwise, false.
+ */
+ inline bool from(const std::string& addr) { return addr.find('/') == std::string::npos ? from_ip(addr) : from_unix(addr); }
+
+ /** Store an IP address in this socket address.
+ * @param addr An IPv4 or IPv6 address.
+ * @return True if the IP was stored in this socket address; otherwise, false.
+ */
+ inline bool from_ip(const std::string& addr) { return from_ip_port(addr, 0); }
+
+ /** Store an IP address and TCP port pair in this socket address.
+ * @param addr An IPv4 or IPv6 address.
+ * @param port A TCP port.
+ * @return True if the IP/port was stored in this socket address; otherwise, false.
+ */
+ bool from_ip_port(const std::string& addr, int port);
+
+ /** Store a UNIX socket path in this socket address.
+ * @param path A path to a UNIX socket.
+ * @return True if the UNIX socket path was stored in this socket address; otherwise, false.
+ */
+ bool from_unix(const std::string& path);
+
/** Returns the TCP port number of the socket address or 0 if not relevant to this family. */
int port() const;
@@ -114,21 +142,6 @@ namespace irc
*/
CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
- /** Convert an address-port pair into a binary sockaddr
- * @param addr The IP address, IPv4 or IPv6
- * @param port The port, 0 for unspecified
- * @param sa The structure to place the result in. Will be zeroed prior to conversion
- * @return true if the conversion was successful, false if not.
- */
- CoreExport bool aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa);
-
- /** Convert a UNIX socket path to a binary sockaddr.
- * @param path The path to the UNIX socket.
- * @param sa The structure to place the result in. Will be zeroed prior to conversion.
- * @return True if the conversion was successful; otherwise, false.
- */
- CoreExport bool untosa(const std::string& path, irc::sockets::sockaddrs& sa);
-
/** Determines whether the specified file is a UNIX socket.
* @param file The path to the file to check.
* @return True if the file is a UNIX socket; otherwise, false.