diff --git a/netwerk/dns/DNS.cpp b/netwerk/dns/DNS.cpp index 4b2fe6b06bce..ddf06944a732 100644 --- a/netwerk/dns/DNS.cpp +++ b/netwerk/dns/DNS.cpp @@ -198,6 +198,24 @@ bool NetAddr::IsIPAddrAny() const { NetAddr::NetAddr(const PRNetAddr* prAddr) { PRNetAddrToNetAddr(prAddr, this); } +nsresult NetAddr::InitFromString(const nsACString& aString, uint16_t aPort) { + PRNetAddr prAddr{}; + memset(&prAddr, 0, sizeof(PRNetAddr)); + if (PR_StringToNetAddr(PromiseFlatCString(aString).get(), &prAddr) != + PR_SUCCESS) { + return NS_ERROR_FAILURE; + } + + PRNetAddrToNetAddr(&prAddr, this); + + if (this->raw.family == PR_AF_INET) { + this->inet.port = PR_htons(aPort); + } else if (this->raw.family == PR_AF_INET6) { + this->inet6.port = PR_htons(aPort); + } + return NS_OK; +} + bool NetAddr::IsIPAddrV4() const { return this->raw.family == AF_INET; } bool NetAddr::IsIPAddrV4Mapped() const { diff --git a/netwerk/dns/DNS.h b/netwerk/dns/DNS.h index e98d80a00d83..3a3c9cc8527d 100644 --- a/netwerk/dns/DNS.h +++ b/netwerk/dns/DNS.h @@ -140,6 +140,12 @@ union NetAddr { NetAddr() { memset(this, 0, sizeof(NetAddr)); } explicit NetAddr(const PRNetAddr* prAddr); + // Will parse aString into a NetAddr using PR_StringToNetAddr. + // Returns an error code if parsing fails. + // If aPort is non-0 will set the NetAddr's port to (the network endian + // value of) that. + nsresult InitFromString(const nsACString& aString, uint16_t aPort = 0); + bool IsIPAddrAny() const; bool IsLoopbackAddr() const; bool IsLoopBackAddressWithoutIPv6Mapping() const;