Skip to content

Commit 7374d22

Browse files
marty1885an-tao
andauthored
add isUnspecified() to indicate if parsing failed (#128)
Co-authored-by: an-tao <antao2002@gmail.com>
1 parent 634984e commit 7374d22

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

trantor/net/InetAddress.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ InetAddress::InetAddress(uint16_t port, bool loopbackOnly, bool ipv6)
8181
addr_.sin_addr.s_addr = htonl(ip);
8282
addr_.sin_port = htons(port);
8383
}
84+
isUnspecified_ = false;
8485
}
8586

8687
InetAddress::InetAddress(const std::string &ip, uint16_t port, bool ipv6)
@@ -93,8 +94,7 @@ InetAddress::InetAddress(const std::string &ip, uint16_t port, bool ipv6)
9394
addr6_.sin6_port = htons(port);
9495
if (::inet_pton(AF_INET6, ip.c_str(), &addr6_.sin6_addr) <= 0)
9596
{
96-
// LOG_SYSERR << "sockets::fromIpPort";
97-
// abort();
97+
return;
9898
}
9999
}
100100
else
@@ -104,10 +104,10 @@ InetAddress::InetAddress(const std::string &ip, uint16_t port, bool ipv6)
104104
addr_.sin_port = htons(port);
105105
if (::inet_pton(AF_INET, ip.c_str(), &addr_.sin_addr) <= 0)
106106
{
107-
// LOG_SYSERR << "sockets::fromIpPort";
108-
// abort();
107+
return;
109108
}
110109
}
110+
isUnspecified_ = false;
111111
}
112112

113113
std::string InetAddress::toIpPort() const

trantor/net/InetAddress.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ class InetAddress
7171
*
7272
* @param addr
7373
*/
74-
explicit InetAddress(const struct sockaddr_in &addr) : addr_(addr)
74+
explicit InetAddress(const struct sockaddr_in &addr)
75+
: addr_(addr), isUnspecified_(false)
7576
{
7677
}
7778

@@ -82,7 +83,7 @@ class InetAddress
8283
* @param addr
8384
*/
8485
explicit InetAddress(const struct sockaddr_in6 &addr)
85-
: addr6_(addr), isIpV6_(true)
86+
: addr6_(addr), isIpV6_(true), isUnspecified_(false)
8687
{
8788
}
8889

@@ -163,6 +164,7 @@ class InetAddress
163164
{
164165
addr6_ = addr6;
165166
isIpV6_ = (addr6_.sin6_family == AF_INET6);
167+
isUnspecified_ = false;
166168
}
167169

168170
/**
@@ -200,13 +202,22 @@ class InetAddress
200202
addr_.sin_port = port;
201203
}
202204

205+
/**
206+
* @brief Return true if the address is not initalized.
207+
*/
208+
inline bool isUnspecified() const
209+
{
210+
return isUnspecified_;
211+
}
212+
203213
private:
204214
union
205215
{
206216
struct sockaddr_in addr_;
207217
struct sockaddr_in6 addr6_;
208218
};
209219
bool isIpV6_{false};
220+
bool isUnspecified_{true};
210221
};
211222

212223
} // namespace trantor

trantor/unittests/InetAddressUnittest.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ TEST(InetAddress, innerIpTest)
1111
EXPECT_EQ(true, InetAddress("10.0.0.1", 0).isIntranetIp());
1212
EXPECT_EQ(true, InetAddress("172.31.10.1", 0).isIntranetIp());
1313
EXPECT_EQ(true, InetAddress("127.0.0.1", 0).isIntranetIp());
14+
EXPECT_EQ(true, InetAddress("example.com", 0).isUnspecified());
15+
EXPECT_EQ(false, InetAddress("127.0.0.2", 0).isUnspecified());
16+
EXPECT_EQ(false, InetAddress("0.0.0.0", 0).isUnspecified());
1417
}
1518
int main(int argc, char **argv)
1619
{

0 commit comments

Comments
 (0)