Permalink
Browse files

Fixed SockAddr

  • Loading branch information...
1 parent dc3c514 commit c05a92345958cb784589868fb4c3948dbe3c572c @vshymanskyy committed Oct 12, 2012
Showing with 69 additions and 6 deletions.
  1. +5 −0 inc/XDebug.h
  2. +27 −4 inc/net/XSockAddr.h
  3. +36 −1 src/net/XSockAddr.cpp
  4. +1 −1 test/net/XAddrTS.h
View
@@ -38,4 +38,9 @@
#define X_ASSERT_LT(expr, val, fmt) { if (!((expr) < (val))) { X_DBG_PRINT("Assertion '" #expr " < " #val "' failed ( " fmt " >= " fmt " ) at %s:%d\n", (expr), (val), __FILE__, __LINE__); X_DBG_BREAK(); } }
#define X_ASSERT_LE(expr, val, fmt) { if (!((expr) <= (val))) { X_DBG_PRINT("Assertion '" #expr " <= " #val "' failed ( " fmt " > " fmt " ) at %s:%d\n", (expr), (val), __FILE__, __LINE__); X_DBG_BREAK(); } }
+class XStackTrace {
+public:
+ XStackTrace();
+};
+
#endif /* _X_DEBUG_H_ */
View
@@ -58,7 +58,7 @@ union XSockAddr
XSockAddr(const XString& str);
XString ToString() const;
- XString Resolve() const;
+ XString ResolveName() const;
sockaddr* SA() { return &sa; };
sockaddr_in* SA4() { return &sa_in; };
@@ -68,9 +68,32 @@ union XSockAddr
const sockaddr_in6* SA6() const { return &sa_in6; };
bool IsLocal() const;
- bool IsLoopback() const;
+
+ bool IsLoopback() const {
+ switch (sa.sa_family) {
+ case AF_INET: return sa_in.sin_addr.s_addr == INADDR_LOOPBACK;
+ case AF_INET6: return !memcmp(&sa_in6.sin6_addr, &in6addr_loopback, sizeof(sa_in6.sin6_addr));
+ default: return false;
+ }
+ }
+
bool IsMulticast() const;
- bool IsAny() const;
+
+ bool IsAny() const {
+ switch (sa.sa_family) {
+ case AF_INET: return sa_in.sin_addr.s_addr == INADDR_ANY;
+ case AF_INET6: return !memcmp(&sa_in6.sin6_addr, &in6addr_any, sizeof(sa_in6.sin6_addr));
+ default: return false;
+ }
+ }
+
+ bool IsBroadcast() const {
+ switch (sa.sa_family) {
+ case AF_INET: return sa_in.sin_addr.s_addr == INADDR_BROADCAST;
+ case AF_INET6: return false;
+ default: return false;
+ }
+ }
socklen_t SA_LEN() const {
switch (sa.sa_family) {
@@ -87,7 +110,7 @@ union XSockAddr
}
}
- unsigned Port () {
+ unsigned Port () const {
switch (sa.sa_family) {
case AF_INET: return ntohs(sa_in.sin_port); break;
case AF_INET6: return ntohs(sa_in6.sin6_port); break;
View
@@ -62,7 +62,7 @@ XString XSockAddr::ToString() const
return XString();
}
-XString XSockAddr::Resolve() const
+XString XSockAddr::ResolveName() const
{
char addr[NI_MAXHOST];
if (0 == getnameinfo(&sa, sizeof(XSockAddr), addr, NI_MAXHOST, NULL, 0, 0)) {
@@ -78,6 +78,26 @@ XList<XSockAddr> XSockAddr::Lookup(const XString& str)
memset(&hint, 0, sizeof(hint));
hint.ai_family = AF_UNSPEC;
hint.ai_protocol = SOCK_DGRAM;
+
+ // Detect IPv6 with port
+ if (str[0] == '[') {
+ int e = str.Find("]:", 1);
+ if (e > 0) {
+ XString name = str.Substring(0, e-1);
+ XString serv = str.Substring(e+2);
+ if (0 == getaddrinfo(name, serv, &hint, &info)) {
+ for(addrinfo* i = info; i != NULL; i = i->ai_next) {
+ XSockAddr addr;
+ memcpy(&addr.sa, i->ai_addr, i->ai_addrlen);
+ res.Append(addr);
+ }
+ freeaddrinfo(info);
+ return res;
+ }
+ }
+ }
+
+ // Detect addr without port
if (0 == getaddrinfo(str, NULL, &hint, &info)) {
for(addrinfo* i = info; i != NULL; i = i->ai_next) {
XSockAddr addr;
@@ -87,6 +107,21 @@ XList<XSockAddr> XSockAddr::Lookup(const XString& str)
freeaddrinfo(info);
return res;
}
+
+ { // Detect IPv4 addr with port
+ int e = str.Find(":");
+ XString name = str.Substring(0, e);
+ XString serv = str.Substring(e+1);
+ if (0 == getaddrinfo(name, serv, &hint, &info)) {
+ for(addrinfo* i = info; i != NULL; i = i->ai_next) {
+ XSockAddr addr;
+ memcpy(&addr.sa, i->ai_addr, i->ai_addrlen);
+ res.Append(addr);
+ }
+ freeaddrinfo(info);
+ return res;
+ }
+ }
return res;
}
View
@@ -102,7 +102,7 @@ class XSockAddrTS: public CxxTest::TestSuite
for (unsigned i=0; i<X_COUNTOF(addr); i+=2) {
- XString addr1 = XSockAddr(addr[i]).Resolve();
+ XString addr1 = XSockAddr(addr[i]).ResolveName();
if (addr1 != addr[i+1]) {
TS_FAIL((char*)(addr1 + " - " + addr[i+1]));
}

0 comments on commit c05a923

Please sign in to comment.