http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_inet.cc ---------------------------------------------------------------------- diff --git a/lib/ts/ink_inet.cc b/lib/ts/ink_inet.cc index 6fe7a3d..1812f75 100644 --- a/lib/ts/ink_inet.cc +++ b/lib/ts/ink_inet.cc @@ -25,18 +25,17 @@ #include "ts/TestBox.h" #if defined(darwin) -extern "C" -{ - struct hostent *gethostbyname_r(const char *name, struct hostent *result, char *buffer, int buflen, int *h_errnop); - struct hostent *gethostbyaddr_r(const char *name, size_t size, int type, - struct hostent *result, char *buffer, int buflen, int *h_errnop); +extern "C" { +struct hostent *gethostbyname_r(const char *name, struct hostent *result, char *buffer, int buflen, int *h_errnop); +struct hostent *gethostbyaddr_r(const char *name, size_t size, int type, struct hostent *result, char *buffer, int buflen, + int *h_errnop); } #endif IpAddr const IpAddr::INVALID; struct hostent * -ink_gethostbyname_r(char *hostname, ink_gethostbyname_r_data * data) +ink_gethostbyname_r(char *hostname, ink_gethostbyname_r_data *data) { #ifdef RENTRENT_GETHOSTBYNAME struct hostent *r = gethostbyname(hostname); @@ -44,48 +43,39 @@ ink_gethostbyname_r(char *hostname, ink_gethostbyname_r_data * data) data->ent = *r; data->herrno = errno; -#else //RENTRENT_GETHOSTBYNAME +#else // RENTRENT_GETHOSTBYNAME #if GETHOSTBYNAME_R_GLIBC2 struct hostent *addrp = NULL; - int res = gethostbyname_r(hostname, &data->ent, data->buf, - INK_GETHOSTBYNAME_R_DATA_SIZE, &addrp, - &data->herrno); + int res = gethostbyname_r(hostname, &data->ent, data->buf, INK_GETHOSTBYNAME_R_DATA_SIZE, &addrp, &data->herrno); struct hostent *r = NULL; if (!res && addrp) r = addrp; #else - struct hostent *r = gethostbyname_r(hostname, &data->ent, data->buf, - INK_GETHOSTBYNAME_R_DATA_SIZE, - &data->herrno); + struct hostent *r = gethostbyname_r(hostname, &data->ent, data->buf, INK_GETHOSTBYNAME_R_DATA_SIZE, &data->herrno); #endif #endif return r; } struct hostent * -ink_gethostbyaddr_r(char *ip, int len, int type, ink_gethostbyaddr_r_data * data) +ink_gethostbyaddr_r(char *ip, int len, int type, ink_gethostbyaddr_r_data *data) { #if GETHOSTBYNAME_R_GLIBC2 struct hostent *r = NULL; struct hostent *addrp = NULL; - int res = gethostbyaddr_r((char *) ip, len, type, &data->ent, data->buf, - INK_GETHOSTBYNAME_R_DATA_SIZE, &addrp, - &data->herrno); + int res = gethostbyaddr_r((char *)ip, len, type, &data->ent, data->buf, INK_GETHOSTBYNAME_R_DATA_SIZE, &addrp, &data->herrno); if (!res && addrp) r = addrp; #else #ifdef RENTRENT_GETHOSTBYADDR - struct hostent *r = gethostbyaddr((const void *) ip, len, type); + struct hostent *r = gethostbyaddr((const void *)ip, len, type); #else - struct hostent *r = gethostbyaddr_r((char *) ip, len, type, &data->ent, - data->buf, - INK_GETHOSTBYNAME_R_DATA_SIZE, - &data->herrno); + struct hostent *r = gethostbyaddr_r((char *)ip, len, type, &data->ent, data->buf, INK_GETHOSTBYNAME_R_DATA_SIZE, &data->herrno); #endif -#endif //LINUX +#endif // LINUX return r; } @@ -93,7 +83,7 @@ uint32_t ink_inet_addr(const char *s) { uint32_t u[4]; - uint8_t *pc = (uint8_t *) s; + uint8_t *pc = (uint8_t *)s; int n = 0; uint32_t base = 10; @@ -102,7 +92,6 @@ ink_inet_addr(const char *s) } while (n < 4) { - u[n] = 0; base = 10; @@ -157,9 +146,10 @@ ink_inet_addr(const char *s) return htonl((uint32_t)-1); } -const char *ats_ip_ntop(const struct sockaddr *addr, char *dst, size_t size) +const char * +ats_ip_ntop(const struct sockaddr *addr, char *dst, size_t size) { - char const* zret = 0; + char const *zret = 0; switch (addr->sa_family) { case AF_INET: @@ -176,40 +166,39 @@ const char *ats_ip_ntop(const struct sockaddr *addr, char *dst, size_t size) return zret; } -char const* -ats_ip_family_name(int family) { - return AF_INET == family ? "IPv4" - : AF_INET6 == family ? "IPv6" - : "Unspec" - ; +char const * +ats_ip_family_name(int family) +{ + return AF_INET == family ? "IPv4" : AF_INET6 == family ? "IPv6" : "Unspec"; } -char const* ats_ip_nptop( - sockaddr const* addr, - char* dst, size_t size -) { +char const * +ats_ip_nptop(sockaddr const *addr, char *dst, size_t size) +{ char buff[INET6_ADDRPORTSTRLEN]; - snprintf(dst, size, "%s:%u", - ats_ip_ntop(addr, buff, sizeof(buff)), - ats_ip_port_host_order(addr) - ); + snprintf(dst, size, "%s:%u", ats_ip_ntop(addr, buff, sizeof(buff)), ats_ip_port_host_order(addr)); return dst; } int -ats_ip_parse(ts::ConstBuffer src, ts::ConstBuffer* addr, ts::ConstBuffer* port, ts::ConstBuffer* rest) { +ats_ip_parse(ts::ConstBuffer src, ts::ConstBuffer *addr, ts::ConstBuffer *port, ts::ConstBuffer *rest) +{ // In case the incoming arguments are null. ts::ConstBuffer localAddr, localPort; - if (!addr) addr = &localAddr; - if (!port) port = &localPort; + if (!addr) + addr = &localAddr; + if (!port) + port = &localPort; addr->reset(); port->reset(); - if (rest) rest->reset(); + if (rest) + rest->reset(); // Let's see if we can find out what's in the address string. if (src) { bool colon_p = false; - while (src && ParseRules::is_ws(*src)) ++src; + while (src && ParseRules::is_ws(*src)) + ++src; // Check for brackets. if ('[' == *src) { /* Ugly. In a number of places we must use bracket notation @@ -234,8 +223,8 @@ ats_ip_parse(ts::ConstBuffer src, ts::ConstBuffer* addr, ts::ConstBuffer* port, } } else { ts::ConstBuffer post = src.after(':'); - if (post.data() && ! post.find(':')) { - *addr = src.splitOn(post.data()-1); + if (post.data() && !post.find(':')) { + *addr = src.splitOn(post.data() - 1); colon_p = true; } else { // presume no port, use everything. *addr = src; @@ -247,19 +236,20 @@ ats_ip_parse(ts::ConstBuffer src, ts::ConstBuffer* addr, ts::ConstBuffer* port, while (src && ParseRules::is_digit(*src)) ++src; - if (tmp.data() == src.data()) { // no digits at all - src.set(tmp.data()-1, tmp.size()+1); // back up to include colon + if (tmp.data() == src.data()) { // no digits at all + src.set(tmp.data() - 1, tmp.size() + 1); // back up to include colon } else { *port = tmp.clip(src.data()); } } - if (rest) *rest = src; + if (rest) + *rest = src; } return *addr ? 0 : -1; // true if we found an address. } int -ats_ip_pton(const ts::ConstBuffer& src, sockaddr* ip) +ats_ip_pton(const ts::ConstBuffer &src, sockaddr *ip) { int zret = -1; ts::ConstBuffer addr, port; @@ -267,8 +257,8 @@ ats_ip_pton(const ts::ConstBuffer& src, sockaddr* ip) ats_ip_invalidate(ip); if (0 == ats_ip_parse(src, &addr, &port)) { // Copy if not terminated. - if (0 != addr[addr.size()-1]) { - char* tmp = static_cast<char*>(alloca(addr.size()+1)); + if (0 != addr[addr.size() - 1]) { + char *tmp = static_cast<char *>(alloca(addr.size() + 1)); memcpy(tmp, addr.data(), addr.size()); tmp[addr.size()] = 0; addr.set(tmp, addr.size()); @@ -295,7 +285,8 @@ ats_ip_pton(const ts::ConstBuffer& src, sockaddr* ip) } uint32_t -ats_ip_hash(sockaddr const* addr) { +ats_ip_hash(sockaddr const *addr) +{ union md5sum { unsigned char c[16]; uint32_t i; @@ -305,46 +296,46 @@ ats_ip_hash(sockaddr const* addr) { if (ats_is_ip4(addr)) { zret.i = ats_ip4_addr_cast(addr); } else if (ats_is_ip6(addr)) { - ink_code_md5(const_cast<uint8_t*>(ats_ip_addr8_cast(addr)), TS_IP6_SIZE, zret.c); + ink_code_md5(const_cast<uint8_t *>(ats_ip_addr8_cast(addr)), TS_IP6_SIZE, zret.c); } return zret.i; } int -ats_ip_to_hex(sockaddr const* src, char* dst, size_t len) { +ats_ip_to_hex(sockaddr const *src, char *dst, size_t len) +{ int zret = 0; ink_assert(len); - char const* dst_limit = dst + len - 1; // reserve null space. + char const *dst_limit = dst + len - 1; // reserve null space. if (ats_is_ip(src)) { - uint8_t const* data = ats_ip_addr8_cast(src); - for ( uint8_t const* src_limit = data + ats_ip_addr_size(src) - ; data < src_limit && dst+1 < dst_limit - ; ++data, zret += 2 - ) { - uint8_t n1 = (*data >> 4) & 0xF; // high nybble. - uint8_t n0 = *data & 0xF; // low nybble. - - *dst++ = n1 > 9 ? n1 + 'A' - 10 : n1 + '0'; - *dst++ = n0 > 9 ? n0 + 'A' - 10 : n0 + '0'; + uint8_t const *data = ats_ip_addr8_cast(src); + for (uint8_t const *src_limit = data + ats_ip_addr_size(src); data < src_limit && dst + 1 < dst_limit; ++data, zret += 2) { + uint8_t n1 = (*data >> 4) & 0xF; // high nybble. + uint8_t n0 = *data & 0xF; // low nybble. + + *dst++ = n1 > 9 ? n1 + 'A' - 10 : n1 + '0'; + *dst++ = n0 > 9 ? n0 + 'A' - 10 : n0 + '0'; } } *dst = 0; // terminate but don't include that in the length. return zret; } -sockaddr* ats_ip_set( - sockaddr* dst, - IpAddr const& addr, - uint16_t port -) { - if (AF_INET == addr._family) ats_ip4_set(dst, addr._addr._ip4, port); - else if (AF_INET6 == addr._family) ats_ip6_set(dst, addr._addr._ip6, port); - else ats_ip_invalidate(dst); +sockaddr * +ats_ip_set(sockaddr *dst, IpAddr const &addr, uint16_t port) +{ + if (AF_INET == addr._family) + ats_ip4_set(dst, addr._addr._ip4, port); + else if (AF_INET6 == addr._family) + ats_ip6_set(dst, addr._addr._ip6, port); + else + ats_ip_invalidate(dst); return dst; } int -IpAddr::load(char const* text) { +IpAddr::load(char const *text) +{ IpEndpoint ip; int zret = ats_ip_pton(text, &ip); *this = ip; @@ -352,7 +343,7 @@ IpAddr::load(char const* text) { } int -IpAddr::load(ts::ConstBuffer const& text) +IpAddr::load(ts::ConstBuffer const &text) { IpEndpoint ip; int zret = ats_ip_pton(text, &ip.sa); @@ -360,8 +351,9 @@ IpAddr::load(ts::ConstBuffer const& text) return zret; } -char* -IpAddr::toString(char* dest, size_t len) const { +char * +IpAddr::toString(char *dest, size_t len) const +{ IpEndpoint ip; ip.assign(*this); ats_ip_ntop(&ip, dest, len); @@ -369,14 +361,13 @@ IpAddr::toString(char* dest, size_t len) const { } bool -IpAddr::isMulticast() const { - return (AF_INET == _family && 0xe == (_addr._byte[0] >> 4)) || - (AF_INET6 == _family && IN6_IS_ADDR_MULTICAST(&_addr._ip6)) - ; +IpAddr::isMulticast() const +{ + return (AF_INET == _family && 0xe == (_addr._byte[0] >> 4)) || (AF_INET6 == _family && IN6_IS_ADDR_MULTICAST(&_addr._ip6)); } -bool -operator == (IpAddr const& lhs, sockaddr const* rhs) { +bool operator==(IpAddr const &lhs, sockaddr const *rhs) +{ bool zret = false; if (lhs._family == rhs->sa_family) { if (AF_INET == lhs._family) { @@ -406,7 +397,8 @@ operator == (IpAddr const& lhs, sockaddr const* rhs) { - 1 if @a lhs is greater than @a rhs. */ int -IpAddr::cmp(self const& that) const { +IpAddr::cmp(self const &that) const +{ int zret = 0; uint16_t rtype = that._family; uint16_t ltype = _family; @@ -417,9 +409,12 @@ IpAddr::cmp(self const& that) const { if (AF_INET == rtype) { in_addr_t la = ntohl(_addr._ip4); in_addr_t ra = ntohl(that._addr._ip4); - if (la < ra) zret = -1; - else if (la > ra) zret = 1; - else zret = 0; + if (la < ra) + zret = -1; + else if (la > ra) + zret = 1; + else + zret = 0; } else if (AF_INET6 == rtype) { // IPv4 < IPv6 zret = -1; } else { // IP > not IP @@ -442,24 +437,24 @@ IpAddr::cmp(self const& that) const { } int -ats_ip_getbestaddrinfo(char const* host, - IpEndpoint* ip4, - IpEndpoint* ip6 -) { +ats_ip_getbestaddrinfo(char const *host, IpEndpoint *ip4, IpEndpoint *ip6) +{ int zret = -1; int port = 0; // port value to assign if we find an address. addrinfo ai_hints; - addrinfo* ai_result; + addrinfo *ai_result; ts::ConstBuffer addr_text, port_text; - ts::ConstBuffer src(host, strlen(host)+1); + ts::ConstBuffer src(host, strlen(host) + 1); - if (ip4) ats_ip_invalidate(ip4); - if (ip6) ats_ip_invalidate(ip6); + if (ip4) + ats_ip_invalidate(ip4); + if (ip6) + ats_ip_invalidate(ip6); if (0 == ats_ip_parse(src, &addr_text, &port_text)) { // Copy if not terminated. - if (0 != addr_text[addr_text.size()-1]) { - char* tmp = static_cast<char*>(alloca(addr_text.size()+1)); + if (0 != addr_text[addr_text.size() - 1]) { + char *tmp = static_cast<char *>(alloca(addr_text.size() + 1)); memcpy(tmp, addr_text.data(), addr_text.size()); tmp[addr_text.size()] = 0; addr_text.set(tmp, addr_text.size()); @@ -478,23 +473,28 @@ ats_ip_getbestaddrinfo(char const* host, PR, // Private. MC, // Multicast. GL // Global. - } spot_type = NA, ip4_type = NA, ip6_type = NA; - sockaddr const* ip4_src = 0; - sockaddr const* ip6_src = 0; - - for ( addrinfo* ai_spot = ai_result - ; ai_spot - ; ai_spot = ai_spot->ai_next - ) { - sockaddr const* ai_ip = ai_spot->ai_addr; - if (!ats_is_ip(ai_ip)) spot_type = NA; - else if (ats_is_ip_loopback(ai_ip)) spot_type = LO; - else if (ats_is_ip_linklocal(ai_ip)) spot_type = LL; - else if (ats_is_ip_private(ai_ip)) spot_type = PR; - else if (ats_is_ip_multicast(ai_ip)) spot_type = MC; - else spot_type = GL; - - if (spot_type == NA) continue; // Next! + } spot_type = NA, + ip4_type = NA, ip6_type = NA; + sockaddr const *ip4_src = 0; + sockaddr const *ip6_src = 0; + + for (addrinfo *ai_spot = ai_result; ai_spot; ai_spot = ai_spot->ai_next) { + sockaddr const *ai_ip = ai_spot->ai_addr; + if (!ats_is_ip(ai_ip)) + spot_type = NA; + else if (ats_is_ip_loopback(ai_ip)) + spot_type = LO; + else if (ats_is_ip_linklocal(ai_ip)) + spot_type = LL; + else if (ats_is_ip_private(ai_ip)) + spot_type = PR; + else if (ats_is_ip_multicast(ai_ip)) + spot_type = MC; + else + spot_type = GL; + + if (spot_type == NA) + continue; // Next! if (ats_is_ip4(ai_ip)) { if (spot_type > ip4_type) { @@ -508,97 +508,105 @@ ats_ip_getbestaddrinfo(char const* host, } } } - if (ip4_type > NA) ats_ip_copy(ip4, ip4_src); - if (ip6_type > NA) ats_ip_copy(ip6, ip6_src); + if (ip4_type > NA) + ats_ip_copy(ip4, ip4_src); + if (ip6_type > NA) + ats_ip_copy(ip6, ip6_src); freeaddrinfo(ai_result); // free *after* the copy. - } } // We don't really care if the port is null terminated - the parser // would get all the digits so the next character is a non-digit (null or // not) and atoi will do the right thing in either case. - if (port_text.size()) port = htons(atoi(port_text.data())); - if (ats_is_ip(ip4)) ats_ip_port_cast(ip4) = port; - if (ats_is_ip(ip6)) ats_ip_port_cast(ip6) = port; - - if (!ats_is_ip(ip4) && !ats_is_ip(ip6)) zret = -1; + if (port_text.size()) + port = htons(atoi(port_text.data())); + if (ats_is_ip(ip4)) + ats_ip_port_cast(ip4) = port; + if (ats_is_ip(ip6)) + ats_ip_port_cast(ip6) = port; + + if (!ats_is_ip(ip4) && !ats_is_ip(ip6)) + zret = -1; return zret; } int -ats_ip_check_characters(ts::ConstBuffer text) { +ats_ip_check_characters(ts::ConstBuffer text) +{ bool found_colon = false; bool found_hex = false; - for ( char const *p = text.data(), *limit = p + text.size() - ; p < limit - ; ++p - ) - if (':' == *p) found_colon = true; - else if ('.' == *p || isdigit(*p)) /* empty */; - else if (isxdigit(*p)) found_hex = true; - else return AF_UNSPEC; - - return found_hex && !found_colon ? AF_UNSPEC - : found_colon ? AF_INET6 - : AF_INET - ; + for (char const *p = text.data(), *limit = p + text.size(); p < limit; ++p) + if (':' == *p) + found_colon = true; + else if ('.' == *p || isdigit(*p)) /* empty */ + ; + else if (isxdigit(*p)) + found_hex = true; + else + return AF_UNSPEC; + + return found_hex && !found_colon ? AF_UNSPEC : found_colon ? AF_INET6 : AF_INET; } // Need to declare this type globally so gcc 4.4 can use it in the countof() template ... -struct ip_parse_spec { char const* hostspec; char const* host; char const* port; char const* rest;}; - -REGRESSION_TEST(Ink_Inet) (RegressionTest * t, int /* atype */, int * pstatus) { +struct ip_parse_spec { + char const *hostspec; + char const *host; + char const *port; + char const *rest; +}; + +REGRESSION_TEST(Ink_Inet)(RegressionTest *t, int /* atype */, int *pstatus) +{ TestBox box(t, pstatus); - IpEndpoint ep; - IpAddr addr; + IpEndpoint ep; + IpAddr addr; box = REGRESSION_TEST_PASSED; // Test ats_ip_parse() ... { - struct ip_parse_spec names[] = { - { "::", "::", NULL, NULL }, - { "[::1]:99", "::1", "99", NULL }, - { "127.0.0.1:8080", "127.0.0.1", "8080", NULL }, - { "127.0.0.1:8080-Bob", "127.0.0.1", "8080", "-Bob" }, - { "127.0.0.1:", "127.0.0.1", NULL, ":" }, - { "foo.example.com", "foo.example.com", NULL, NULL }, - { "foo.example.com:99", "foo.example.com", "99", NULL }, - { "ffee::24c3:3349:3cee:0143", "ffee::24c3:3349:3cee:0143", NULL }, - { "fe80:88b5:4a:20c:29ff:feae:1c33:8080", "fe80:88b5:4a:20c:29ff:feae:1c33:8080", NULL, NULL }, - { "[ffee::24c3:3349:3cee:0143]", "ffee::24c3:3349:3cee:0143", NULL }, - { "[ffee::24c3:3349:3cee:0143]:80", "ffee::24c3:3349:3cee:0143", "80", NULL }, - { "[ffee::24c3:3349:3cee:0143]:8080x", "ffee::24c3:3349:3cee:0143", "8080", "x" } - }; + struct ip_parse_spec names[] = {{"::", "::", NULL, NULL}, + {"[::1]:99", "::1", "99", NULL}, + {"127.0.0.1:8080", "127.0.0.1", "8080", NULL}, + {"127.0.0.1:8080-Bob", "127.0.0.1", "8080", "-Bob"}, + {"127.0.0.1:", "127.0.0.1", NULL, ":"}, + {"foo.example.com", "foo.example.com", NULL, NULL}, + {"foo.example.com:99", "foo.example.com", "99", NULL}, + {"ffee::24c3:3349:3cee:0143", "ffee::24c3:3349:3cee:0143", NULL}, + {"fe80:88b5:4a:20c:29ff:feae:1c33:8080", "fe80:88b5:4a:20c:29ff:feae:1c33:8080", NULL, NULL}, + {"[ffee::24c3:3349:3cee:0143]", "ffee::24c3:3349:3cee:0143", NULL}, + {"[ffee::24c3:3349:3cee:0143]:80", "ffee::24c3:3349:3cee:0143", "80", NULL}, + {"[ffee::24c3:3349:3cee:0143]:8080x", "ffee::24c3:3349:3cee:0143", "8080", "x"}}; for (unsigned i = 0; i < countof(names); ++i) { - ip_parse_spec const& s = names[i]; + ip_parse_spec const &s = names[i]; ts::ConstBuffer host, port, rest; size_t len; - box.check(ats_ip_parse(ts::ConstBuffer(s.hostspec, strlen(s.hostspec)), &host, &port, &rest) == 0, - "ats_ip_parse(%s)", s.hostspec); + box.check(ats_ip_parse(ts::ConstBuffer(s.hostspec, strlen(s.hostspec)), &host, &port, &rest) == 0, "ats_ip_parse(%s)", + s.hostspec); len = strlen(s.host); - box.check(len == host.size() && strncmp(host.data(), s.host, host.size()) == 0, - "ats_ip_parse(%s) gave addr '%.*s'", s.hostspec, static_cast<int>(host.size()), host.data()); + box.check(len == host.size() && strncmp(host.data(), s.host, host.size()) == 0, "ats_ip_parse(%s) gave addr '%.*s'", + s.hostspec, static_cast<int>(host.size()), host.data()); if (s.port) { len = strlen(s.port); - box.check(len == port.size() && strncmp(port.data(), s.port, port.size()) == 0, - "ats_ip_parse(%s) gave port '%.*s'", s.hostspec, static_cast<int>(port.size()), port.data()); + box.check(len == port.size() && strncmp(port.data(), s.port, port.size()) == 0, "ats_ip_parse(%s) gave port '%.*s'", + s.hostspec, static_cast<int>(port.size()), port.data()); } else { - box.check(port.size() == 0, - "ats_ip_parse(%s) gave port '%.*s' instead of empty", s.hostspec, static_cast<int>(port.size()), port.data()); + box.check(port.size() == 0, "ats_ip_parse(%s) gave port '%.*s' instead of empty", s.hostspec, static_cast<int>(port.size()), + port.data()); } if (s.rest) { len = strlen(s.rest); - box.check(len == rest.size() && strncmp(rest.data(), s.rest, len) == 0 - , "ats_ip_parse(%s) gave rest '%.*s' instead of '%s'", s.hostspec, static_cast<int>(rest.size()), rest.data(), s.rest); + box.check(len == rest.size() && strncmp(rest.data(), s.rest, len) == 0, "ats_ip_parse(%s) gave rest '%.*s' instead of '%s'", + s.hostspec, static_cast<int>(rest.size()), rest.data(), s.rest); } else { - box.check(rest.size() == 0, - "ats_ip_parse(%s) gave rest '%.*s' instead of empty", s.hostspec, static_cast<int>(rest.size()), rest.data()); + box.check(rest.size() == 0, "ats_ip_parse(%s) gave rest '%.*s' instead of empty", s.hostspec, static_cast<int>(rest.size()), + rest.data()); } } }
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_inet.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_inet.h b/lib/ts/ink_inet.h index 5e4c5d4..553f444 100644 --- a/lib/ts/ink_inet.h +++ b/lib/ts/ink_inet.h @@ -22,7 +22,7 @@ */ -#if !defined (_ink_inet_h_) +#if !defined(_ink_inet_h_) #define _ink_inet_h_ #include <netinet/in.h> @@ -35,12 +35,14 @@ #define INK_GETHOSTBYNAME_R_DATA_SIZE 1024 #define INK_GETHOSTBYADDR_R_DATA_SIZE 1024 -#if ! TS_HAS_IN6_IS_ADDR_UNSPECIFIED +#if !TS_HAS_IN6_IS_ADDR_UNSPECIFIED #if defined(IN6_IS_ADDR_UNSPECIFIED) #undef IN6_IS_ADDR_UNSPECIFIED #endif -static inline bool IN6_IS_ADDR_UNSPECIFIED(in6_addr const* addr) { - uint64_t const* w = reinterpret_cast<uint64_t const*>(addr); +static inline bool +IN6_IS_ADDR_UNSPECIFIED(in6_addr const *addr) +{ + uint64_t const *w = reinterpret_cast<uint64_t const *>(addr); return 0 == w[0] && 0 == w[1]; } #endif @@ -65,18 +67,16 @@ struct IpAddr; // forward declare. union IpEndpoint { typedef IpEndpoint self; ///< Self reference type. - struct sockaddr sa; ///< Generic address. - struct sockaddr_in sin; ///< IPv4 - struct sockaddr_in6 sin6; ///< IPv6 + struct sockaddr sa; ///< Generic address. + struct sockaddr_in sin; ///< IPv4 + struct sockaddr_in6 sin6; ///< IPv6 - self& assign( - sockaddr const* ip ///< Source address, family, port. - ); + self &assign(sockaddr const *ip ///< Source address, family, port. + ); /// Construct from an @a addr and @a port. - self& assign( - IpAddr const& addr, ///< Address and address family. - in_port_t port = 0 ///< Port (network order). - ); + self &assign(IpAddr const &addr, ///< Address and address family. + in_port_t port = 0 ///< Port (network order). + ); /// Test for valid IP address. bool isValid() const; @@ -90,35 +90,31 @@ union IpEndpoint { /// Set to be any address for family @a family. /// @a family must be @c AF_INET or @c AF_INET6. /// @return This object. - self& setToAnyAddr( - int family ///< Address family. - ); + self &setToAnyAddr(int family ///< Address family. + ); /// Set to be loopback for family @a family. /// @a family must be @c AF_INET or @c AF_INET6. /// @return This object. - self& setToLoopback( - int family ///< Address family. - ); + self &setToLoopback(int family ///< Address family. + ); /// Port in network order. - in_port_t& port(); + in_port_t &port(); /// Port in network order. in_port_t port() const; - operator sockaddr* () { return &sa; } - operator sockaddr const* () const { return &sa; } + operator sockaddr *() { return &sa; } + operator sockaddr const *() const { return &sa; } }; -struct ink_gethostbyname_r_data -{ +struct ink_gethostbyname_r_data { int herrno; struct hostent ent; char buf[INK_GETHOSTBYNAME_R_DATA_SIZE]; }; -struct ink_gethostbyaddr_r_data -{ +struct ink_gethostbyaddr_r_data { int herrno; struct hostent ent; char buf[INK_GETHOSTBYADDR_R_DATA_SIZE]; @@ -133,7 +129,7 @@ struct ink_gethostbyaddr_r_data @param data pointer to ink_gethostbyname_r_data allocated by the caller */ -struct hostent *ink_gethostbyname_r(char *hostname, ink_gethostbyname_r_data * data); +struct hostent *ink_gethostbyname_r(char *hostname, ink_gethostbyname_r_data *data); /** Wrapper for gethostbyaddr_r(). If successful, returns a pointer @@ -146,7 +142,7 @@ struct hostent *ink_gethostbyname_r(char *hostname, ink_gethostbyname_r_data * d @param data pointer to ink_gethostbyname_r_data allocated by the caller */ -struct hostent *ink_gethostbyaddr_r(char *ip, int len, int type, ink_gethostbyaddr_r_data * data); +struct hostent *ink_gethostbyaddr_r(char *ip, int len, int type, ink_gethostbyaddr_r_data *data); /** Parse a string for pieces of an IP address. @@ -161,13 +157,11 @@ struct hostent *ink_gethostbyaddr_r(char *ip, int len, int type, ink_gethostbyad @return 0 if an address was found, non-zero otherwise. */ -int -ats_ip_parse( - ts::ConstBuffer src, ///< [in] String to search. - ts::ConstBuffer* addr, ///< [out] Range containing IP address. - ts::ConstBuffer* port, ///< [out] Range containing port. - ts::ConstBuffer* rest = 0 ///< [out] Remnant past the addr/port if any. -); +int ats_ip_parse(ts::ConstBuffer src, ///< [in] String to search. + ts::ConstBuffer *addr, ///< [out] Range containing IP address. + ts::ConstBuffer *port, ///< [out] Range containing port. + ts::ConstBuffer *rest = 0 ///< [out] Remnant past the addr/port if any. + ); /** Check to see if a buffer contains only IP address characters. @return @@ -175,8 +169,7 @@ ats_ip_parse( - AF_INET - only digits and dots. - AF_INET6 - colons found. */ -int -ats_ip_check_characters(ts::ConstBuffer text); +int ats_ip_check_characters(ts::ConstBuffer text); /** Wrapper for inet_addr(). @@ -194,87 +187,108 @@ static size_t const TS_IP6_SIZE = sizeof(in6_addr); /// Reset an address to invalid. /// @note Useful for marking a member as not yet set. -inline void ats_ip_invalidate(sockaddr* addr) { +inline void +ats_ip_invalidate(sockaddr *addr) +{ addr->sa_family = AF_UNSPEC; } -inline void ats_ip_invalidate(sockaddr_in6* addr) { +inline void +ats_ip_invalidate(sockaddr_in6 *addr) +{ addr->sin6_family = AF_UNSPEC; } -inline void ats_ip_invalidate(IpEndpoint* ip) { +inline void +ats_ip_invalidate(IpEndpoint *ip) +{ ip->sa.sa_family = AF_UNSPEC; } /** Get a string name for an IP address family. @return The string name (never @c NULL). */ -char const* -ats_ip_family_name(int family); +char const *ats_ip_family_name(int family); /// Test for IP protocol. /// @return @c true if the address is IP, @c false otherwise. -inline bool ats_is_ip(sockaddr const* addr) { - return addr - && (AF_INET == addr->sa_family || AF_INET6 == addr->sa_family); +inline bool +ats_is_ip(sockaddr const *addr) +{ + return addr && (AF_INET == addr->sa_family || AF_INET6 == addr->sa_family); } /// @return @c true if the address is IP, @c false otherwise. -inline bool ats_is_ip(IpEndpoint const* addr) { - return addr - && (AF_INET == addr->sa.sa_family || AF_INET6 == addr->sa.sa_family); +inline bool +ats_is_ip(IpEndpoint const *addr) +{ + return addr && (AF_INET == addr->sa.sa_family || AF_INET6 == addr->sa.sa_family); } /// Test for IP protocol. /// @return @c true if the value is an IP address family, @c false otherwise. -inline bool ats_is_ip(int family) { +inline bool +ats_is_ip(int family) +{ return AF_INET == family || AF_INET6 == family; } /// Test for IPv4 protocol. /// @return @c true if the address is IPv4, @c false otherwise. -inline bool ats_is_ip4(sockaddr const* addr) { +inline bool +ats_is_ip4(sockaddr const *addr) +{ return addr && AF_INET == addr->sa_family; } /// Test for IPv4 protocol. /// @note Convenience overload. /// @return @c true if the address is IPv4, @c false otherwise. -inline bool ats_is_ip4(IpEndpoint const* addr) { +inline bool +ats_is_ip4(IpEndpoint const *addr) +{ return addr && AF_INET == addr->sa.sa_family; } /// Test for IPv6 protocol. /// @return @c true if the address is IPv6, @c false otherwise. -inline bool ats_is_ip6(sockaddr const* addr) { +inline bool +ats_is_ip6(sockaddr const *addr) +{ return addr && AF_INET6 == addr->sa_family; } /// Test for IPv6 protocol. /// @note Convenience overload. /// @return @c true if the address is IPv6, @c false otherwise. -inline bool ats_is_ip6(IpEndpoint const* addr) { +inline bool +ats_is_ip6(IpEndpoint const *addr) +{ return addr && AF_INET6 == addr->sa.sa_family; } /// @return @c true if the address families are compatible. -inline bool ats_ip_are_compatible( - sockaddr const* lhs, ///< Address to test. - sockaddr const* rhs ///< Address to test. -) { +inline bool +ats_ip_are_compatible(sockaddr const *lhs, ///< Address to test. + sockaddr const *rhs ///< Address to test. + ) +{ return lhs->sa_family == rhs->sa_family; } /// @return @c true if the address families are compatible. -inline bool ats_ip_are_compatible( - IpEndpoint const* lhs, ///< Address to test. - IpEndpoint const* rhs ///< Address to test. -) { +inline bool +ats_ip_are_compatible(IpEndpoint const *lhs, ///< Address to test. + IpEndpoint const *rhs ///< Address to test. + ) +{ return ats_ip_are_compatible(&lhs->sa, &rhs->sa); } /// @return @c true if the address families are compatible. -inline bool ats_ip_are_compatible( - int lhs, ///< Address family to test. - sockaddr const* rhs ///< Address to test. -) { +inline bool +ats_ip_are_compatible(int lhs, ///< Address family to test. + sockaddr const *rhs ///< Address to test. + ) +{ return lhs == rhs->sa_family; } /// @return @c true if the address families are compatible. -inline bool ats_ip_are_compatible( - sockaddr const* lhs, ///< Address to test. - int rhs ///< Family to test. -) { +inline bool +ats_ip_are_compatible(sockaddr const *lhs, ///< Address to test. + int rhs ///< Family to test. + ) +{ return lhs->sa_family == rhs; } @@ -284,108 +298,140 @@ inline bool ats_ip_are_compatible( // ip4_cast converts to sockaddr_in (because that's effectively an IPv4 addr). // ip6_cast converts to sockaddr_in6 -inline sockaddr* ats_ip_sa_cast(sockaddr_storage* a) { - return static_cast<sockaddr*>(static_cast<void*>(a)); +inline sockaddr * +ats_ip_sa_cast(sockaddr_storage *a) +{ + return static_cast<sockaddr *>(static_cast<void *>(a)); } -inline sockaddr const* ats_ip_sa_cast(sockaddr_storage const* a) { - return static_cast<sockaddr const*>(static_cast<void const*>(a)); +inline sockaddr const * +ats_ip_sa_cast(sockaddr_storage const *a) +{ + return static_cast<sockaddr const *>(static_cast<void const *>(a)); } -inline sockaddr* ats_ip_sa_cast(sockaddr_in* a) { - return static_cast<sockaddr*>(static_cast<void*>(a)); +inline sockaddr * +ats_ip_sa_cast(sockaddr_in *a) +{ + return static_cast<sockaddr *>(static_cast<void *>(a)); } -inline sockaddr const* ats_ip_sa_cast(sockaddr_in const* a) { - return static_cast<sockaddr const*>(static_cast<void const*>(a)); +inline sockaddr const * +ats_ip_sa_cast(sockaddr_in const *a) +{ + return static_cast<sockaddr const *>(static_cast<void const *>(a)); } -inline sockaddr* ats_ip_sa_cast(sockaddr_in6* a) { - return static_cast<sockaddr*>(static_cast<void*>(a)); +inline sockaddr * +ats_ip_sa_cast(sockaddr_in6 *a) +{ + return static_cast<sockaddr *>(static_cast<void *>(a)); } -inline sockaddr const* ats_ip_sa_cast(sockaddr_in6 const* a) { - return static_cast<sockaddr const*>(static_cast<void const*>(a)); +inline sockaddr const * +ats_ip_sa_cast(sockaddr_in6 const *a) +{ + return static_cast<sockaddr const *>(static_cast<void const *>(a)); } -inline sockaddr_storage* ats_ip_ss_cast(sockaddr* a) { - return static_cast<sockaddr_storage*>(static_cast<void*>(a)); +inline sockaddr_storage * +ats_ip_ss_cast(sockaddr *a) +{ + return static_cast<sockaddr_storage *>(static_cast<void *>(a)); } -inline sockaddr_storage const* ats_ip_ss_cast(sockaddr const* a) { - return static_cast<sockaddr_storage const*>(static_cast<void const*>(a)); +inline sockaddr_storage const * +ats_ip_ss_cast(sockaddr const *a) +{ + return static_cast<sockaddr_storage const *>(static_cast<void const *>(a)); } -inline sockaddr_in* ats_ip4_cast(sockaddr* a) { - return static_cast<sockaddr_in*>(static_cast<void*>(a)); +inline sockaddr_in * +ats_ip4_cast(sockaddr *a) +{ + return static_cast<sockaddr_in *>(static_cast<void *>(a)); } -inline sockaddr_in const* ats_ip4_cast(sockaddr const* a) { - return static_cast<sockaddr_in const*>(static_cast<void const*>(a)); +inline sockaddr_in const * +ats_ip4_cast(sockaddr const *a) +{ + return static_cast<sockaddr_in const *>(static_cast<void const *>(a)); } -inline sockaddr_in& ats_ip4_cast(sockaddr& a) { - return *static_cast<sockaddr_in*>(static_cast<void*>(&a)); +inline sockaddr_in & +ats_ip4_cast(sockaddr &a) +{ + return *static_cast<sockaddr_in *>(static_cast<void *>(&a)); } -inline sockaddr_in const& ats_ip4_cast(sockaddr const& a) { - return *static_cast<sockaddr_in const*>(static_cast<void const*>(&a)); +inline sockaddr_in const & +ats_ip4_cast(sockaddr const &a) +{ + return *static_cast<sockaddr_in const *>(static_cast<void const *>(&a)); } -inline sockaddr_in* ats_ip4_cast(sockaddr_in6* a) { - return static_cast<sockaddr_in*>(static_cast<void*>(a)); +inline sockaddr_in * +ats_ip4_cast(sockaddr_in6 *a) +{ + return static_cast<sockaddr_in *>(static_cast<void *>(a)); } -inline sockaddr_in const* ats_ip4_cast(sockaddr_in6 const* a) { - return static_cast<sockaddr_in const*>(static_cast<void const*>(a)); +inline sockaddr_in const * +ats_ip4_cast(sockaddr_in6 const *a) +{ + return static_cast<sockaddr_in const *>(static_cast<void const *>(a)); } -inline sockaddr_in& ats_ip4_cast(sockaddr_in6& a) { - return *static_cast<sockaddr_in*>(static_cast<void*>(&a)); +inline sockaddr_in & +ats_ip4_cast(sockaddr_in6 &a) +{ + return *static_cast<sockaddr_in *>(static_cast<void *>(&a)); } -inline sockaddr_in const& ats_ip4_cast(sockaddr_in6 const& a) { - return *static_cast<sockaddr_in const*>(static_cast<void const*>(&a)); +inline sockaddr_in const & +ats_ip4_cast(sockaddr_in6 const &a) +{ + return *static_cast<sockaddr_in const *>(static_cast<void const *>(&a)); } -inline sockaddr_in6* ats_ip6_cast(sockaddr* a) { - return static_cast<sockaddr_in6*>(static_cast<void*>(a)); +inline sockaddr_in6 * +ats_ip6_cast(sockaddr *a) +{ + return static_cast<sockaddr_in6 *>(static_cast<void *>(a)); } -inline sockaddr_in6 const* ats_ip6_cast(sockaddr const* a) { - return static_cast<sockaddr_in6 const*>(static_cast<void const*>(a)); +inline sockaddr_in6 const * +ats_ip6_cast(sockaddr const *a) +{ + return static_cast<sockaddr_in6 const *>(static_cast<void const *>(a)); } -inline sockaddr_in6& ats_ip6_cast(sockaddr& a) { - return *static_cast<sockaddr_in6*>(static_cast<void*>(&a)); +inline sockaddr_in6 & +ats_ip6_cast(sockaddr &a) +{ + return *static_cast<sockaddr_in6 *>(static_cast<void *>(&a)); } -inline sockaddr_in6 const& ats_ip6_cast(sockaddr const& a) { - return *static_cast<sockaddr_in6 const*>(static_cast<void const*>(&a)); +inline sockaddr_in6 const & +ats_ip6_cast(sockaddr const &a) +{ + return *static_cast<sockaddr_in6 const *>(static_cast<void const *>(&a)); } /// @return The @c sockaddr size for the family of @a addr. -inline size_t ats_ip_size( - sockaddr const* addr ///< Address object. -) { - return AF_INET == addr->sa_family ? sizeof(sockaddr_in) - : AF_INET6 == addr->sa_family ? sizeof(sockaddr_in6) - : 0 - ; -} -inline size_t ats_ip_size( - IpEndpoint const* addr ///< Address object. -) { - return AF_INET == addr->sa.sa_family ? sizeof(sockaddr_in) - : AF_INET6 == addr->sa.sa_family ? sizeof(sockaddr_in6) - : 0 - ; +inline size_t +ats_ip_size(sockaddr const *addr ///< Address object. + ) +{ + return AF_INET == addr->sa_family ? sizeof(sockaddr_in) : AF_INET6 == addr->sa_family ? sizeof(sockaddr_in6) : 0; +} +inline size_t +ats_ip_size(IpEndpoint const *addr ///< Address object. + ) +{ + return AF_INET == addr->sa.sa_family ? sizeof(sockaddr_in) : AF_INET6 == addr->sa.sa_family ? sizeof(sockaddr_in6) : 0; } /// @return The size of the IP address only. -inline size_t ats_ip_addr_size( - sockaddr const* addr ///< Address object. -) { - return AF_INET == addr->sa_family ? sizeof(in_addr_t) - : AF_INET6 == addr->sa_family ? sizeof(in6_addr) - : 0 - ; -} -inline size_t ats_ip_addr_size( - IpEndpoint const* addr ///< Address object. -) { - return AF_INET == addr->sa.sa_family ? sizeof(in_addr_t) - : AF_INET6 == addr->sa.sa_family ? sizeof(in6_addr) - : 0 - ; +inline size_t +ats_ip_addr_size(sockaddr const *addr ///< Address object. + ) +{ + return AF_INET == addr->sa_family ? sizeof(in_addr_t) : AF_INET6 == addr->sa_family ? sizeof(in6_addr) : 0; +} +inline size_t +ats_ip_addr_size(IpEndpoint const *addr ///< Address object. + ) +{ + return AF_INET == addr->sa.sa_family ? sizeof(in_addr_t) : AF_INET6 == addr->sa.sa_family ? sizeof(in6_addr) : 0; } /** Get a reference to the port in an address. @@ -395,22 +441,25 @@ inline size_t ats_ip_addr_size( @internal This is primarily for internal use but it might be handy for clients so it is exposed. */ -inline in_port_t& ats_ip_port_cast(sockaddr* sa) { +inline in_port_t & +ats_ip_port_cast(sockaddr *sa) +{ static in_port_t dummy = 0; - return ats_is_ip4(sa) - ? ats_ip4_cast(sa)->sin_port - : ats_is_ip6(sa) - ? ats_ip6_cast(sa)->sin6_port - : (dummy = 0) - ; + return ats_is_ip4(sa) ? ats_ip4_cast(sa)->sin_port : ats_is_ip6(sa) ? ats_ip6_cast(sa)->sin6_port : (dummy = 0); } -inline in_port_t const& ats_ip_port_cast(sockaddr const* sa) { - return ats_ip_port_cast(const_cast<sockaddr*>(sa)); +inline in_port_t const & +ats_ip_port_cast(sockaddr const *sa) +{ + return ats_ip_port_cast(const_cast<sockaddr *>(sa)); } -inline in_port_t const& ats_ip_port_cast(IpEndpoint const* ip) { - return ats_ip_port_cast(const_cast<sockaddr*>(&ip->sa)); +inline in_port_t const & +ats_ip_port_cast(IpEndpoint const *ip) +{ + return ats_ip_port_cast(const_cast<sockaddr *>(&ip->sa)); } -inline in_port_t& ats_ip_port_cast(IpEndpoint* ip) { +inline in_port_t & +ats_ip_port_cast(IpEndpoint *ip) +{ return ats_ip_port_cast(&ip->sa); } @@ -422,12 +471,11 @@ inline in_port_t& ats_ip_port_cast(IpEndpoint* ip) { @return A reference to the IPv4 address in @a addr. */ -inline in_addr_t& ats_ip4_addr_cast(sockaddr* addr) { +inline in_addr_t & +ats_ip4_addr_cast(sockaddr *addr) +{ static in_addr_t dummy = 0; - return ats_is_ip4(addr) - ? ats_ip4_cast(addr)->sin_addr.s_addr - : (dummy = 0) - ; + return ats_is_ip4(addr) ? ats_ip4_cast(addr)->sin_addr.s_addr : (dummy = 0); } /** Access the IPv4 address. @@ -438,12 +486,11 @@ inline in_addr_t& ats_ip4_addr_cast(sockaddr* addr) { @return A reference to the IPv4 address in @a addr. */ -inline in_addr_t const& ats_ip4_addr_cast(sockaddr const* addr) { +inline in_addr_t const & +ats_ip4_addr_cast(sockaddr const *addr) +{ static in_addr_t dummy = 0; - return ats_is_ip4(addr) - ? ats_ip4_cast(addr)->sin_addr.s_addr - : static_cast<in_addr_t const&>(dummy = 0) - ; + return ats_is_ip4(addr) ? ats_ip4_cast(addr)->sin_addr.s_addr : static_cast<in_addr_t const &>(dummy = 0); } /** Access the IPv4 address. @@ -455,7 +502,9 @@ inline in_addr_t const& ats_ip4_addr_cast(sockaddr const* addr) { @return A reference to the IPv4 address in @a addr. */ -inline in_addr_t& ats_ip4_addr_cast(IpEndpoint* ip) { +inline in_addr_t & +ats_ip4_addr_cast(IpEndpoint *ip) +{ return ats_ip4_addr_cast(&ip->sa); } @@ -468,7 +517,9 @@ inline in_addr_t& ats_ip4_addr_cast(IpEndpoint* ip) { @return A reference to the IPv4 address in @a addr. */ -inline in_addr_t const& ats_ip4_addr_cast(IpEndpoint const* ip) { +inline in_addr_t const & +ats_ip4_addr_cast(IpEndpoint const *ip) +{ return ats_ip4_addr_cast(&ip->sa); } @@ -480,16 +531,24 @@ inline in_addr_t const& ats_ip4_addr_cast(IpEndpoint const* ip) { @return A reference to the IPv6 address in @a addr. */ -inline in6_addr& ats_ip6_addr_cast(sockaddr* addr) { +inline in6_addr & +ats_ip6_addr_cast(sockaddr *addr) +{ return ats_ip6_cast(addr)->sin6_addr; } -inline in6_addr const& ats_ip6_addr_cast(sockaddr const* addr) { +inline in6_addr const & +ats_ip6_addr_cast(sockaddr const *addr) +{ return ats_ip6_cast(addr)->sin6_addr; } -inline in6_addr& ats_ip6_addr_cast(IpEndpoint* ip) { +inline in6_addr & +ats_ip6_addr_cast(IpEndpoint *ip) +{ return ip->sin6.sin6_addr; } -inline in6_addr const& ats_ip6_addr_cast(IpEndpoint const* ip) { +inline in6_addr const & +ats_ip6_addr_cast(IpEndpoint const *ip) +{ return ip->sin6.sin6_addr; } @@ -499,16 +558,24 @@ inline in6_addr const& ats_ip6_addr_cast(IpEndpoint const* ip) { @return A pointer to the address information in @a addr or @c NULL if @a addr is not an IP address. */ -inline uint32_t* ats_ip_addr32_cast(sockaddr* addr) { - uint32_t* zret = 0; - switch(addr->sa_family) { - case AF_INET: zret = reinterpret_cast<uint32_t*>(&ats_ip4_addr_cast(addr)); break; - case AF_INET6: zret = reinterpret_cast<uint32_t*>(&ats_ip6_addr_cast(addr)); break; +inline uint32_t * +ats_ip_addr32_cast(sockaddr *addr) +{ + uint32_t *zret = 0; + switch (addr->sa_family) { + case AF_INET: + zret = reinterpret_cast<uint32_t *>(&ats_ip4_addr_cast(addr)); + break; + case AF_INET6: + zret = reinterpret_cast<uint32_t *>(&ats_ip6_addr_cast(addr)); + break; } return zret; } -inline uint32_t const* ats_ip_addr32_cast(sockaddr const* addr) { - return ats_ip_addr32_cast(const_cast<sockaddr*>(addr)); +inline uint32_t const * +ats_ip_addr32_cast(sockaddr const *addr) +{ + return ats_ip_addr32_cast(const_cast<sockaddr *>(addr)); } /** Cast an IP address to an array of @c uint8_t. @@ -518,68 +585,81 @@ inline uint32_t const* ats_ip_addr32_cast(sockaddr const* addr) { if @a addr is not an IP address. @see ats_ip_addr_size */ -inline uint8_t* ats_ip_addr8_cast(sockaddr* addr) { - uint8_t* zret = 0; - switch(addr->sa_family) { - case AF_INET: zret = reinterpret_cast<uint8_t*>(&ats_ip4_addr_cast(addr)); break; - case AF_INET6: zret = reinterpret_cast<uint8_t*>(&ats_ip6_addr_cast(addr)); break; +inline uint8_t * +ats_ip_addr8_cast(sockaddr *addr) +{ + uint8_t *zret = 0; + switch (addr->sa_family) { + case AF_INET: + zret = reinterpret_cast<uint8_t *>(&ats_ip4_addr_cast(addr)); + break; + case AF_INET6: + zret = reinterpret_cast<uint8_t *>(&ats_ip6_addr_cast(addr)); + break; } return zret; } -inline uint8_t const* ats_ip_addr8_cast(sockaddr const* addr) { - return ats_ip_addr8_cast(const_cast<sockaddr*>(addr)); +inline uint8_t const * +ats_ip_addr8_cast(sockaddr const *addr) +{ + return ats_ip_addr8_cast(const_cast<sockaddr *>(addr)); } -inline uint8_t* ats_ip_addr8_cast(IpEndpoint* ip) { +inline uint8_t * +ats_ip_addr8_cast(IpEndpoint *ip) +{ return ats_ip_addr8_cast(&ip->sa); } -inline uint8_t const* ats_ip_addr8_cast(IpEndpoint const* ip) { +inline uint8_t const * +ats_ip_addr8_cast(IpEndpoint const *ip) +{ return ats_ip_addr8_cast(&ip->sa); } /// Check for loopback. /// @return @c true if this is an IP loopback address, @c false otherwise. -inline bool ats_is_ip_loopback(sockaddr const* ip) { - return ip - && ( - (AF_INET == ip->sa_family && 0x7F == ats_ip_addr8_cast(ip)[0]) - || - (AF_INET6 == ip->sa_family && IN6_IS_ADDR_LOOPBACK(&ats_ip6_addr_cast(ip))) - ); +inline bool +ats_is_ip_loopback(sockaddr const *ip) +{ + return ip && ((AF_INET == ip->sa_family && 0x7F == ats_ip_addr8_cast(ip)[0]) || + (AF_INET6 == ip->sa_family && IN6_IS_ADDR_LOOPBACK(&ats_ip6_addr_cast(ip)))); } /// Check for loopback. /// @return @c true if this is an IP loopback address, @c false otherwise. -inline bool ats_is_ip_loopback(IpEndpoint const* ip) { +inline bool +ats_is_ip_loopback(IpEndpoint const *ip) +{ return ats_is_ip_loopback(&ip->sa); } /// Check for multicast. /// @return @true if @a ip is multicast. -inline bool ats_is_ip_multicast(sockaddr const* ip) { - return ip - && ( - (AF_INET == ip->sa_family && 0xe == *ats_ip_addr8_cast(ip)) - || - (AF_INET6 == ip->sa_family && IN6_IS_ADDR_MULTICAST(&ats_ip6_addr_cast(ip))) - ); +inline bool +ats_is_ip_multicast(sockaddr const *ip) +{ + return ip && ((AF_INET == ip->sa_family && 0xe == *ats_ip_addr8_cast(ip)) || + (AF_INET6 == ip->sa_family && IN6_IS_ADDR_MULTICAST(&ats_ip6_addr_cast(ip)))); } /// Check for multicast. /// @return @true if @a ip is multicast. -inline bool ats_is_ip_multicast(IpEndpoint const* ip) { +inline bool +ats_is_ip_multicast(IpEndpoint const *ip) +{ return ats_is_ip_multicast(&ip->sa); } /// Check for Private. /// @return @true if @a ip is private. inline bool -ats_is_ip_private(sockaddr const* ip) { +ats_is_ip_private(sockaddr const *ip) +{ bool zret = false; if (ats_is_ip4(ip)) { in_addr_t a = ats_ip4_addr_cast(ip); zret = ((a & htonl(0xFF000000)) == htonl(0x0A000000)) || // 10.0.0.0/8 - ((a & htonl(0xFFC00000)) == htonl(0x64400000)) || // 100.64.0.0/10 - ((a & htonl(0xFFF00000)) == htonl(0xAC100000)) || // 172.16.0.0/12 - ((a & htonl(0xFFFF0000)) == htonl(0xC0A80000)) // 192.168.0.0/16 + ((a & htonl(0xFFC00000)) == htonl(0x64400000)) || // 100.64.0.0/10 + ((a & htonl(0xFFF00000)) == htonl(0xAC100000)) || // 172.16.0.0/12 + ((a & htonl(0xFFFF0000)) == htonl(0xC0A80000)) // 192.168.0.0/16 ; } else if (ats_is_ip6(ip)) { in6_addr a = ats_ip6_addr_cast(ip); @@ -592,14 +672,16 @@ ats_is_ip_private(sockaddr const* ip) { /// Check for Private. /// @return @true if @a ip is private. inline bool -ats_is_ip_private(IpEndpoint const* ip) { +ats_is_ip_private(IpEndpoint const *ip) +{ return ats_is_ip_private(&ip->sa); } /// Check for Link Local. /// @return @true if @a ip is link local. inline bool -ats_is_ip_linklocal(sockaddr const* ip) { +ats_is_ip_linklocal(sockaddr const *ip) +{ bool zret = false; if (ats_is_ip4(ip)) { in_addr_t a = ats_ip4_addr_cast(ip); @@ -616,16 +698,18 @@ ats_is_ip_linklocal(sockaddr const* ip) { /// Check for Link Local. /// @return @true if @a ip is link local. inline bool -ats_is_ip_linklocal(IpEndpoint const* ip) { +ats_is_ip_linklocal(IpEndpoint const *ip) +{ return ats_is_ip_linklocal(&ip->sa); } /// Check for being "any" address. /// @return @c true if @a ip is the any / unspecified address. -inline bool ats_is_ip_any(sockaddr const* ip) { +inline bool +ats_is_ip_any(sockaddr const *ip) +{ return (ats_is_ip4(ip) && INADDR_ANY == ats_ip4_addr_cast(ip)) || - (ats_is_ip6(ip) && IN6_IS_ADDR_UNSPECIFIED(&ats_ip6_addr_cast(ip))) - ; + (ats_is_ip6(ip) && IN6_IS_ADDR_UNSPECIFIED(&ats_ip6_addr_cast(ip))); } /// @name Address operators @@ -637,15 +721,20 @@ inline bool ats_is_ip_any(sockaddr const* ip) { @a dst is marked as invalid. @return @c true if @a src was an IP address, @c false otherwise. */ -inline bool ats_ip_copy( - sockaddr* dst, ///< Destination object. - sockaddr const* src ///< Source object. -) { +inline bool +ats_ip_copy(sockaddr *dst, ///< Destination object. + sockaddr const *src ///< Source object. + ) +{ size_t n = 0; if (src) { switch (src->sa_family) { - case AF_INET: n = sizeof(sockaddr_in); break; - case AF_INET6: n = sizeof(sockaddr_in6); break; + case AF_INET: + n = sizeof(sockaddr_in); + break; + case AF_INET6: + n = sizeof(sockaddr_in6); + break; } } if (n) { @@ -659,22 +748,23 @@ inline bool ats_ip_copy( return n != 0; } -inline bool ats_ip_copy( - IpEndpoint* dst, ///< Destination object. - sockaddr const* src ///< Source object. -) { +inline bool +ats_ip_copy(IpEndpoint *dst, ///< Destination object. + sockaddr const *src ///< Source object. + ) +{ return ats_ip_copy(&dst->sa, src); } -inline bool ats_ip_copy( - IpEndpoint* dst, ///< Destination object. - IpEndpoint const* src ///< Source object. -) { +inline bool +ats_ip_copy(IpEndpoint *dst, ///< Destination object. + IpEndpoint const *src ///< Source object. + ) +{ return ats_ip_copy(&dst->sa, &src->sa); } -inline bool ats_ip_copy( - sockaddr* dst, - IpEndpoint const* src -) { +inline bool +ats_ip_copy(sockaddr *dst, IpEndpoint const *src) +{ return ats_ip_copy(dst, &src->sa); } @@ -696,10 +786,11 @@ inline bool ats_ip_copy( @internal This looks like a lot of code for an inline but I think it should compile down to something reasonable. */ -inline int ats_ip_addr_cmp( - sockaddr const* lhs, ///< Left hand operand. - sockaddr const* rhs ///< Right hand operand. -) { +inline int +ats_ip_addr_cmp(sockaddr const *lhs, ///< Left hand operand. + sockaddr const *rhs ///< Right hand operand. + ) +{ int zret = 0; uint16_t rtype = rhs->sa_family; uint16_t ltype = lhs->sa_family; @@ -710,9 +801,12 @@ inline int ats_ip_addr_cmp( if (AF_INET == rtype) { in_addr_t la = ntohl(ats_ip4_cast(lhs)->sin_addr.s_addr); in_addr_t ra = ntohl(ats_ip4_cast(rhs)->sin_addr.s_addr); - if (la < ra) zret = -1; - else if (la > ra) zret = 1; - else zret = 0; + if (la < ra) + zret = -1; + else if (la > ra) + zret = 1; + else + zret = 0; } else if (AF_INET6 == rtype) { // IPv4 < IPv6 zret = -1; } else { // IP > not IP @@ -720,12 +814,8 @@ inline int ats_ip_addr_cmp( } } else if (AF_INET6 == ltype) { if (AF_INET6 == rtype) { - sockaddr_in6 const* lhs_in6 = ats_ip6_cast(lhs); - zret = memcmp( - &lhs_in6->sin6_addr, - &ats_ip6_cast(rhs)->sin6_addr, - sizeof(lhs_in6->sin6_addr) - ); + sockaddr_in6 const *lhs_in6 = ats_ip6_cast(lhs); + zret = memcmp(&lhs_in6->sin6_addr, &ats_ip6_cast(rhs)->sin6_addr, sizeof(lhs_in6->sin6_addr)); } else { zret = 1; // IPv6 greater than any other type. } @@ -744,7 +834,9 @@ inline int ats_ip_addr_cmp( @note Convenience overload. @see ats_ip_addr_cmp(sockaddr const* lhs, sockaddr const* rhs) */ -inline int ats_ip_addr_cmp(IpEndpoint const* lhs, IpEndpoint const* rhs) { +inline int +ats_ip_addr_cmp(IpEndpoint const *lhs, IpEndpoint const *rhs) +{ return ats_ip_addr_cmp(&lhs->sa, &rhs->sa); } @@ -752,22 +844,30 @@ inline int ats_ip_addr_cmp(IpEndpoint const* lhs, IpEndpoint const* rhs) { @return @c true if @a lhs and @a rhs point to equal addresses, @c false otherwise. */ -inline bool ats_ip_addr_eq(sockaddr const* lhs, sockaddr const* rhs) { +inline bool +ats_ip_addr_eq(sockaddr const *lhs, sockaddr const *rhs) +{ return 0 == ats_ip_addr_cmp(lhs, rhs); } -inline bool ats_ip_addr_eq(IpEndpoint const* lhs, IpEndpoint const* rhs) { +inline bool +ats_ip_addr_eq(IpEndpoint const *lhs, IpEndpoint const *rhs) +{ return 0 == ats_ip_addr_cmp(&lhs->sa, &rhs->sa); } -inline bool operator == (IpEndpoint const& lhs, IpEndpoint const& rhs) { +inline bool operator==(IpEndpoint const &lhs, IpEndpoint const &rhs) +{ return 0 == ats_ip_addr_cmp(&lhs.sa, &rhs.sa); } -inline bool operator != (IpEndpoint const& lhs, IpEndpoint const& rhs) { +inline bool operator!=(IpEndpoint const &lhs, IpEndpoint const &rhs) +{ return 0 != ats_ip_addr_cmp(&lhs.sa, &rhs.sa); } /// Compare address and port for equality. -inline bool ats_ip_addr_port_eq(sockaddr const* lhs, sockaddr const* rhs) { +inline bool +ats_ip_addr_port_eq(sockaddr const *lhs, sockaddr const *rhs) +{ bool zret = false; if (lhs->sa_family == rhs->sa_family && ats_ip_port_cast(lhs) == ats_ip_port_cast(rhs)) { if (AF_INET == lhs->sa_family) @@ -783,41 +883,45 @@ inline bool ats_ip_addr_port_eq(sockaddr const* lhs, sockaddr const* rhs) { /// Get IP TCP/UDP port. /// @return The port in host order for an IPv4 or IPv6 address, /// or zero if neither. -inline in_port_t ats_ip_port_host_order( - sockaddr const* addr ///< Address with port. -) { +inline in_port_t +ats_ip_port_host_order(sockaddr const *addr ///< Address with port. + ) +{ // We can discard the const because this function returns // by value. - return ntohs(ats_ip_port_cast(const_cast<sockaddr*>(addr))); + return ntohs(ats_ip_port_cast(const_cast<sockaddr *>(addr))); } /// Get IP TCP/UDP port. /// @return The port in host order for an IPv4 or IPv6 address, /// or zero if neither. -inline in_port_t ats_ip_port_host_order( - IpEndpoint const* ip ///< Address with port. -) { +inline in_port_t +ats_ip_port_host_order(IpEndpoint const *ip ///< Address with port. + ) +{ // We can discard the const because this function returns // by value. - return ntohs(ats_ip_port_cast(const_cast<sockaddr*>(&ip->sa))); + return ntohs(ats_ip_port_cast(const_cast<sockaddr *>(&ip->sa))); } /** Extract the IPv4 address. @return Host order IPv4 address. */ -inline in_addr_t ats_ip4_addr_host_order( - sockaddr const* addr ///< Address object. -) { - return ntohl(ats_ip4_addr_cast(const_cast<sockaddr*>(addr))); +inline in_addr_t +ats_ip4_addr_host_order(sockaddr const *addr ///< Address object. + ) +{ + return ntohl(ats_ip4_addr_cast(const_cast<sockaddr *>(addr))); } /// Write IPv4 data to storage @a dst. -inline sockaddr* ats_ip4_set( - sockaddr_in* dst, ///< Destination storage. - in_addr_t addr, ///< address, IPv4 network order. - in_port_t port = 0 ///< port, network order. -) { +inline sockaddr * +ats_ip4_set(sockaddr_in *dst, ///< Destination storage. + in_addr_t addr, ///< address, IPv4 network order. + in_port_t port = 0 ///< port, network order. + ) +{ ink_zero(*dst); #if HAVE_STRUCT_SOCKADDR_IN_SIN_LEN dst->sin_len = sizeof(sockaddr_in); @@ -831,11 +935,12 @@ inline sockaddr* ats_ip4_set( /** Write IPv4 data to @a dst. @note Convenience overload. */ -inline sockaddr* ats_ip4_set( - IpEndpoint* dst, ///< Destination storage. - in_addr_t ip4, ///< address, IPv4 network order. - in_port_t port = 0 ///< port, network order. -) { +inline sockaddr * +ats_ip4_set(IpEndpoint *dst, ///< Destination storage. + in_addr_t ip4, ///< address, IPv4 network order. + in_port_t port = 0 ///< port, network order. + ) +{ return ats_ip4_set(&dst->sin, ip4, port); } @@ -844,21 +949,23 @@ inline sockaddr* ats_ip4_set( This is the generic overload. Caller must verify that @a dst is at least @c sizeof(sockaddr_in) bytes. */ -inline sockaddr* ats_ip4_set( - sockaddr* dst, ///< Destination storage. - in_addr_t ip4, ///< address, IPv4 network order. - in_port_t port = 0 ///< port, network order. -) { +inline sockaddr * +ats_ip4_set(sockaddr *dst, ///< Destination storage. + in_addr_t ip4, ///< address, IPv4 network order. + in_port_t port = 0 ///< port, network order. + ) +{ return ats_ip4_set(ats_ip4_cast(dst), ip4, port); } /** Write IPv6 data to storage @a dst. @return @a dst cast to @c sockaddr*. */ -inline sockaddr* ats_ip6_set( - sockaddr_in6* dst, ///< Destination storage. - in6_addr const& addr, ///< address in network order. - in_port_t port = 0 ///< Port, network order. -) { +inline sockaddr * +ats_ip6_set(sockaddr_in6 *dst, ///< Destination storage. + in6_addr const &addr, ///< address in network order. + in_port_t port = 0 ///< Port, network order. + ) +{ ink_zero(*dst); #if HAVE_STRUCT_SOCKADDR_IN_SIN_LEN dst->sin6_len = sizeof(sockaddr_in6); @@ -871,41 +978,43 @@ inline sockaddr* ats_ip6_set( /** Write IPv6 data to storage @a dst. @return @a dst cast to @c sockaddr*. */ -inline sockaddr* ats_ip6_set( - sockaddr* dst, ///< Destination storage. - in6_addr const& addr, ///< address in network order. - in_port_t port = 0 ///< Port, network order. -) { +inline sockaddr * +ats_ip6_set(sockaddr *dst, ///< Destination storage. + in6_addr const &addr, ///< address in network order. + in_port_t port = 0 ///< Port, network order. + ) +{ return ats_ip6_set(ats_ip6_cast(dst), addr, port); } /** Write IPv6 data to storage @a dst. @return @a dst cast to @c sockaddr*. */ -inline sockaddr* ats_ip6_set( - IpEndpoint* dst, ///< Destination storage. - in6_addr const& addr, ///< address in network order. - in_port_t port = 0 ///< Port, network order. -) { +inline sockaddr * +ats_ip6_set(IpEndpoint *dst, ///< Destination storage. + in6_addr const &addr, ///< address in network order. + in_port_t port = 0 ///< Port, network order. + ) +{ return ats_ip6_set(&dst->sin6, addr, port); } /** Write a null terminated string for @a addr to @a dst. A buffer of size INET6_ADDRSTRLEN suffices, including a terminating nul. */ -char const* ats_ip_ntop( - const sockaddr *addr, ///< Address. - char *dst, ///< Output buffer. - size_t size ///< Length of buffer. -); +char const *ats_ip_ntop(const sockaddr *addr, ///< Address. + char *dst, ///< Output buffer. + size_t size ///< Length of buffer. + ); /** Write a null terminated string for @a addr to @a dst. A buffer of size INET6_ADDRSTRLEN suffices, including a terminating nul. */ -inline char const* ats_ip_ntop( - IpEndpoint const* addr, ///< Address. - char *dst, ///< Output buffer. - size_t size ///< Length of buffer. -) { +inline char const * +ats_ip_ntop(IpEndpoint const *addr, ///< Address. + char *dst, ///< Output buffer. + size_t size ///< Length of buffer. + ) +{ return ats_ip_ntop(&addr->sa, dst, size); } @@ -919,20 +1028,20 @@ typedef char ip_port_text_buffer[INET6_ADDRPORTSTRLEN]; /** Write a null terminated string for @a addr to @a dst with port. A buffer of size INET6_ADDRPORTSTRLEN suffices, including a terminating nul. */ -char const* ats_ip_nptop( - const sockaddr *addr, ///< Address. - char *dst, ///< Output buffer. - size_t size ///< Length of buffer. -); +char const *ats_ip_nptop(const sockaddr *addr, ///< Address. + char *dst, ///< Output buffer. + size_t size ///< Length of buffer. + ); /** Write a null terminated string for @a addr to @a dst with port. A buffer of size INET6_ADDRPORTSTRLEN suffices, including a terminating nul. */ -inline char const* ats_ip_nptop( - IpEndpoint const*addr, ///< Address. - char *dst, ///< Output buffer. - size_t size ///< Length of buffer. -) { +inline char const * +ats_ip_nptop(IpEndpoint const *addr, ///< Address. + char *dst, ///< Output buffer. + size_t size ///< Length of buffer. + ) +{ return ats_ip_nptop(&addr->sa, dst, size); } @@ -953,10 +1062,9 @@ inline char const* ats_ip_nptop( @return 0 on success, non-zero on failure. */ -int ats_ip_pton( - const ts::ConstBuffer& text, ///< [in] text. - sockaddr* addr ///< [out] address -); +int ats_ip_pton(const ts::ConstBuffer &text, ///< [in] text. + sockaddr *addr ///< [out] address + ); /** Convert @a text to an IP address and write it to @a addr. @@ -969,31 +1077,35 @@ int ats_ip_pton( @return 0 on success, non-zero on failure. */ -inline int ats_ip_pton( - char const* text, ///< [in] text. - sockaddr_in6* addr ///< [out] address -) { +inline int +ats_ip_pton(char const *text, ///< [in] text. + sockaddr_in6 *addr ///< [out] address + ) +{ return ats_ip_pton(ts::ConstBuffer(text, strlen(text)), ats_ip_sa_cast(addr)); } -inline int ats_ip_pton( - const ts::ConstBuffer& text, ///< [in] text. - IpEndpoint* addr ///< [out] address -) { +inline int +ats_ip_pton(const ts::ConstBuffer &text, ///< [in] text. + IpEndpoint *addr ///< [out] address + ) +{ return ats_ip_pton(text, &addr->sa); } -inline int ats_ip_pton( - const char * text, ///< [in] text. - IpEndpoint* addr ///< [out] address -) { +inline int +ats_ip_pton(const char *text, ///< [in] text. + IpEndpoint *addr ///< [out] address + ) +{ return ats_ip_pton(ts::ConstBuffer(text, strlen(text)), &addr->sa); } -inline int ats_ip_pton( - const char * text, ///< [in] text. - sockaddr * addr ///< [out] address -) { +inline int +ats_ip_pton(const char *text, ///< [in] text. + sockaddr *addr ///< [out] address + ) +{ return ats_ip_pton(ts::ConstBuffer(text, strlen(text)), addr); } @@ -1022,28 +1134,24 @@ inline int ats_ip_pton( @see getaddrinfo */ -int -ats_ip_getbestaddrinfo( - char const* name, ///< [in] Address name (IPv4, IPv6, or host name) - IpEndpoint* ip4, ///< [out] Storage for IPv4 address. - IpEndpoint* ip6 ///< [out] Storage for IPv6 address -); +int ats_ip_getbestaddrinfo(char const *name, ///< [in] Address name (IPv4, IPv6, or host name) + IpEndpoint *ip4, ///< [out] Storage for IPv4 address. + IpEndpoint *ip6 ///< [out] Storage for IPv6 address + ); /** Generic IP address hash function. */ -uint32_t ats_ip_hash(sockaddr const* addr); +uint32_t ats_ip_hash(sockaddr const *addr); /** Convert address to string as a hexidecimal value. The string is always nul terminated, the output string is clipped if @a dst is insufficient. @return The length of the resulting string (not including nul). */ -int -ats_ip_to_hex( - sockaddr const* addr, ///< Address to convert. Must be IP. - char* dst, ///< Destination buffer. - size_t len ///< Length of @a dst. -); +int ats_ip_to_hex(sockaddr const *addr, ///< Address to convert. Must be IP. + char *dst, ///< Destination buffer. + size_t len ///< Length of @a dst. + ); /** Storage for an IP address. In some cases we want to store just the address and not the @@ -1056,91 +1164,78 @@ struct IpAddr { /// Default construct (invalid address). IpAddr() : _family(AF_UNSPEC) {} /// Construct as IPv4 @a addr. - explicit IpAddr( - in_addr_t addr ///< Address to assign. - ) : _family(AF_INET) { + explicit IpAddr(in_addr_t addr ///< Address to assign. + ) + : _family(AF_INET) + { _addr._ip4 = addr; } /// Construct as IPv6 @a addr. - explicit IpAddr( - in6_addr const& addr ///< Address to assign. - ) : _family(AF_INET6) { + explicit IpAddr(in6_addr const &addr ///< Address to assign. + ) + : _family(AF_INET6) + { _addr._ip6 = addr; } /// Construct from @c sockaddr. - explicit IpAddr(sockaddr const* addr) { this->assign(addr); } + explicit IpAddr(sockaddr const *addr) { this->assign(addr); } /// Construct from @c sockaddr_in6. - explicit IpAddr(sockaddr_in6 const& addr) { this->assign(ats_ip_sa_cast(&addr)); } + explicit IpAddr(sockaddr_in6 const &addr) { this->assign(ats_ip_sa_cast(&addr)); } /// Construct from @c sockaddr_in6. - explicit IpAddr(sockaddr_in6 const* addr) { this->assign(ats_ip_sa_cast(addr)); } + explicit IpAddr(sockaddr_in6 const *addr) { this->assign(ats_ip_sa_cast(addr)); } /// Construct from @c IpEndpoint. - explicit IpAddr(IpEndpoint const& addr) { this->assign(&addr.sa); } + explicit IpAddr(IpEndpoint const &addr) { this->assign(&addr.sa); } /// Construct from @c IpEndpoint. - explicit IpAddr(IpEndpoint const* addr) { this->assign(&addr->sa); } + explicit IpAddr(IpEndpoint const *addr) { this->assign(&addr->sa); } /// Assign sockaddr storage. - self& assign( - sockaddr const* addr ///< May be @c NULL - ); + self &assign(sockaddr const *addr ///< May be @c NULL + ); /// Assign from end point. - self& operator = (IpEndpoint const& ip) { - return this->assign(&ip.sa); - } + self &operator=(IpEndpoint const &ip) { return this->assign(&ip.sa); } /// Assign from IPv4 raw address. - self& operator = ( - in_addr_t ip ///< Network order IPv4 address. - ); + self &operator=(in_addr_t ip ///< Network order IPv4 address. + ); /// Assign from IPv6 raw address. - self& operator = ( - in6_addr const& ip - ); + self &operator=(in6_addr const &ip); /** Load from string. The address is copied to this object if the conversion is successful, otherwise this object is invalidated. @return 0 on success, non-zero on failure. */ - int load( - char const* str ///< Nul terminated input string. - ); + int load(char const *str ///< Nul terminated input string. + ); /** Load from string. The address is copied to this object if the conversion is successful, otherwise this object is invalidated. @return 0 on success, non-zero on failure. */ - int load( - ts::ConstBuffer const& str ///< Text of IP address. - ); + int load(ts::ConstBuffer const &str ///< Text of IP address. + ); /** Output to a string. @return The string @a dest. */ - char* toString( - char* dest, ///< [out] Destination string buffer. - size_t len ///< [in] Size of buffer. - ) const; + char *toString(char *dest, ///< [out] Destination string buffer. + size_t len ///< [in] Size of buffer. + ) const; /// Equality. - bool operator==(self const& that) const { - return _family == AF_INET - ? (that._family == AF_INET && _addr._ip4 == that._addr._ip4) - : _family == AF_INET6 - ? (that._family == AF_INET6 - && 0 == memcmp(&_addr._ip6, &that._addr._ip6, TS_IP6_SIZE) - ) - : (_family == AF_UNSPEC && that._family == AF_UNSPEC) - ; + bool operator==(self const &that) const + { + return _family == AF_INET ? (that._family == AF_INET && _addr._ip4 == that._addr._ip4) : _family == AF_INET6 ? + (that._family == AF_INET6 && 0 == memcmp(&_addr._ip6, &that._addr._ip6, TS_IP6_SIZE)) : + (_family == AF_UNSPEC && that._family == AF_UNSPEC); } /// Inequality. - bool operator!=(self const& that) { - return ! (*this == that); - } + bool operator!=(self const &that) { return !(*this == that); } /// Generic compare. - int cmp(self const& that) const; + int cmp(self const &that) const; /** Return a normalized hash value. - Ipv4: the address in host order. @@ -1153,14 +1248,12 @@ struct IpAddr { @see hash */ struct Hasher { - uint32_t operator() (self const& ip) const { - return ip.hash(); - } + uint32_t operator()(self const &ip) const { return ip.hash(); } }; /// Test for same address family. /// @c return @c true if @a that is the same address family as @a this. - bool isCompatibleWith(self const& that); + bool isCompatibleWith(self const &that); /// Get the address family. /// @return The address family. @@ -1171,9 +1264,18 @@ struct IpAddr { bool isIp6() const; /// Test for validity. - bool isValid() const { return _family == AF_INET || _family == AF_INET6; } + bool + isValid() const + { + return _family == AF_INET || _family == AF_INET6; + } /// Make invalid. - self& invalidate() { _family = AF_UNSPEC; return *this; } + self & + invalidate() + { + _family = AF_UNSPEC; + return *this; + } /// Test for multicast bool isMulticast() const; /// Test for loopback @@ -1182,50 +1284,64 @@ struct IpAddr { uint16_t _family; ///< Protocol family. /// Address data. union { - in_addr_t _ip4; ///< IPv4 address storage. - in6_addr _ip6; ///< IPv6 address storage. - uint8_t _byte[TS_IP6_SIZE]; ///< As raw bytes. - uint32_t _u32[TS_IP6_SIZE/(sizeof(uint32_t)/sizeof(uint8_t))]; ///< As 32 bit chunks. - uint64_t _u64[TS_IP6_SIZE/(sizeof(uint64_t)/sizeof(uint8_t))]; ///< As 64 bit chunks. + in_addr_t _ip4; ///< IPv4 address storage. + in6_addr _ip6; ///< IPv6 address storage. + uint8_t _byte[TS_IP6_SIZE]; ///< As raw bytes. + uint32_t _u32[TS_IP6_SIZE / (sizeof(uint32_t) / sizeof(uint8_t))]; ///< As 32 bit chunks. + uint64_t _u64[TS_IP6_SIZE / (sizeof(uint64_t) / sizeof(uint8_t))]; ///< As 64 bit chunks. } _addr; ///< Pre-constructed invalid instance. static self const INVALID; }; -inline IpAddr& -IpAddr::operator = (in_addr_t ip) { +inline IpAddr &IpAddr::operator=(in_addr_t ip) +{ _family = AF_INET; _addr._ip4 = ip; return *this; } -inline IpAddr& -IpAddr::operator = (in6_addr const& ip) { +inline IpAddr &IpAddr::operator=(in6_addr const &ip) +{ _family = AF_INET6; _addr._ip6 = ip; return *this; } -inline uint16_t IpAddr::family() const { return _family; } +inline uint16_t +IpAddr::family() const +{ + return _family; +} inline bool -IpAddr::isCompatibleWith(self const& that) { +IpAddr::isCompatibleWith(self const &that) +{ return this->isValid() && _family == that._family; } -inline bool IpAddr::isIp4() const { return AF_INET == _family; } -inline bool IpAddr::isIp6() const { return AF_INET6 == _family; } +inline bool +IpAddr::isIp4() const +{ + return AF_INET == _family; +} +inline bool +IpAddr::isIp6() const +{ + return AF_INET6 == _family; +} -inline bool IpAddr::isLoopback() const { - return (AF_INET == _family && 0x7F == _addr._byte[0]) || - (AF_INET6 == _family && IN6_IS_ADDR_LOOPBACK(&_addr._ip6)) - ; +inline bool +IpAddr::isLoopback() const +{ + return (AF_INET == _family && 0x7F == _addr._byte[0]) || (AF_INET6 == _family && IN6_IS_ADDR_LOOPBACK(&_addr._ip6)); } /// Assign sockaddr storage. -inline IpAddr& -IpAddr::assign(sockaddr const* addr) { +inline IpAddr & +IpAddr::assign(sockaddr const *addr) +{ if (addr) { _family = addr->sa_family; if (ats_is_ip4(addr)) { @@ -1242,47 +1358,59 @@ IpAddr::assign(sockaddr const* addr) { } // Associated operators. -bool operator == (IpAddr const& lhs, sockaddr const* rhs); -inline bool operator == (sockaddr const* lhs, IpAddr const& rhs) { +bool operator==(IpAddr const &lhs, sockaddr const *rhs); +inline bool operator==(sockaddr const *lhs, IpAddr const &rhs) +{ return rhs == lhs; } -inline bool operator != (IpAddr const& lhs, sockaddr const* rhs) { - return ! (lhs == rhs); +inline bool operator!=(IpAddr const &lhs, sockaddr const *rhs) +{ + return !(lhs == rhs); } -inline bool operator != (sockaddr const* lhs, IpAddr const& rhs) { - return ! (rhs == lhs); +inline bool operator!=(sockaddr const *lhs, IpAddr const &rhs) +{ + return !(rhs == lhs); } -inline bool operator == (IpAddr const& lhs, IpEndpoint const& rhs) { +inline bool operator==(IpAddr const &lhs, IpEndpoint const &rhs) +{ return lhs == &rhs.sa; } -inline bool operator == (IpEndpoint const& lhs, IpAddr const& rhs) { +inline bool operator==(IpEndpoint const &lhs, IpAddr const &rhs) +{ return &lhs.sa == rhs; } -inline bool operator != (IpAddr const& lhs, IpEndpoint const& rhs) { - return ! (lhs == &rhs.sa); +inline bool operator!=(IpAddr const &lhs, IpEndpoint const &rhs) +{ + return !(lhs == &rhs.sa); } -inline bool operator != (IpEndpoint const& lhs, IpAddr const& rhs) { - return ! (rhs == &lhs.sa); +inline bool operator!=(IpEndpoint const &lhs, IpAddr const &rhs) +{ + return !(rhs == &lhs.sa); } -inline bool operator < (IpAddr const& lhs, IpAddr const& rhs) { +inline bool operator<(IpAddr const &lhs, IpAddr const &rhs) +{ return -1 == lhs.cmp(rhs); } -inline bool operator >= (IpAddr const& lhs, IpAddr const& rhs) { +inline bool operator>=(IpAddr const &lhs, IpAddr const &rhs) +{ return lhs.cmp(rhs) >= 0; } -inline bool operator > (IpAddr const& lhs, IpAddr const& rhs) { +inline bool operator>(IpAddr const &lhs, IpAddr const &rhs) +{ return 1 == lhs.cmp(rhs); } -inline bool operator <= (IpAddr const& lhs, IpAddr const& rhs) { - return lhs.cmp(rhs) <= 0; +inline bool operator<=(IpAddr const &lhs, IpAddr const &rhs) +{ + return lhs.cmp(rhs) <= 0; } inline uint32_t -IpAddr::hash() const { +IpAddr::hash() const +{ uint32_t zret = 0; if (this->isIp4()) { zret = ntohl(_addr._ip4); @@ -1294,56 +1422,74 @@ IpAddr::hash() const { /// Write IP @a addr to storage @a dst. /// @return @s dst. -sockaddr* ats_ip_set( - sockaddr* dst, ///< Destination storage. - IpAddr const& addr, ///< source address. - in_port_t port = 0 ///< port, network order. -); +sockaddr *ats_ip_set(sockaddr *dst, ///< Destination storage. + IpAddr const &addr, ///< source address. + in_port_t port = 0 ///< port, network order. + ); /** Convert @a text to an IP address and write it to @a addr. Convenience overload. @return 0 on success, non-zero on failure. */ -inline int ats_ip_pton( - char const* text, ///< [in] text. - IpAddr& addr ///< [out] address -) { +inline int +ats_ip_pton(char const *text, ///< [in] text. + IpAddr &addr ///< [out] address + ) +{ return addr.load(text) ? 0 : -1; } -inline IpEndpoint& -IpEndpoint::assign(IpAddr const& addr, in_port_t port) { +inline IpEndpoint & +IpEndpoint::assign(IpAddr const &addr, in_port_t port) +{ ats_ip_set(&sa, addr, port); return *this; } -inline IpEndpoint& -IpEndpoint::assign(sockaddr const* ip) { +inline IpEndpoint & +IpEndpoint::assign(sockaddr const *ip) +{ ats_ip_copy(&sa, ip); return *this; } -inline in_port_t& -IpEndpoint::port() { +inline in_port_t & +IpEndpoint::port() +{ return ats_ip_port_cast(&sa); } inline in_port_t -IpEndpoint::port() const { +IpEndpoint::port() const +{ return ats_ip_port_cast(&sa); } inline bool -IpEndpoint::isValid() const { +IpEndpoint::isValid() const +{ return ats_is_ip(this); } -inline bool IpEndpoint::isIp4() const { return AF_INET == sa.sa_family; } -inline bool IpEndpoint::isIp6() const { return AF_INET6 == sa.sa_family; } -inline uint16_t IpEndpoint::family() const { return sa.sa_family; } +inline bool +IpEndpoint::isIp4() const +{ + return AF_INET == sa.sa_family; +} +inline bool +IpEndpoint::isIp6() const +{ + return AF_INET6 == sa.sa_family; +} +inline uint16_t +IpEndpoint::family() const +{ + return sa.sa_family; +} -inline IpEndpoint& -IpEndpoint::setToAnyAddr(int family) { +inline IpEndpoint & +IpEndpoint::setToAnyAddr(int family) +{ ink_zero(*this); sa.sa_family = family; if (AF_INET == family) { @@ -1360,8 +1506,9 @@ IpEndpoint::setToAnyAddr(int family) { return *this; } -inline IpEndpoint& -IpEndpoint::setToLoopback(int family) { +inline IpEndpoint & +IpEndpoint::setToLoopback(int family) +{ ink_zero(*this); sa.sa_family = family; if (AF_INET == family) { http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_inout.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_inout.h b/lib/ts/ink_inout.h index fd6e701..56b04b3 100644 --- a/lib/ts/ink_inout.h +++ b/lib/ts/ink_inout.h @@ -32,45 +32,55 @@ // source of good macros.. #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ #include <arpa/nameser.h> #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ -#define GETCHAR(s, cp) { \ - (s) = *(cp)++; \ -} +#define GETCHAR(s, cp) \ + { \ + (s) = *(cp)++; \ + } -#define PUTCHAR(s, cp) { \ - *(cp)++ = (s); \ -} +#define PUTCHAR(s, cp) \ + { \ + *(cp)++ = (s); \ + } -#define GETLONGLONG(l, cp) { \ - (l) = *(cp)++ << 8; \ - (l) |= *(cp)++; (l) <<= 8; \ - (l) |= *(cp)++; (l) <<= 8; \ - (l) |= *(cp)++; (l) <<= 8; \ - (l) |= *(cp)++; (l) <<= 8; \ - (l) |= *(cp)++; (l) <<= 8; \ - (l) |= *(cp)++; (l) <<= 8; \ - (l) |= *(cp)++; \ -} +#define GETLONGLONG(l, cp) \ + { \ + (l) = *(cp)++ << 8; \ + (l) |= *(cp)++; \ + (l) <<= 8; \ + (l) |= *(cp)++; \ + (l) <<= 8; \ + (l) |= *(cp)++; \ + (l) <<= 8; \ + (l) |= *(cp)++; \ + (l) <<= 8; \ + (l) |= *(cp)++; \ + (l) <<= 8; \ + (l) |= *(cp)++; \ + (l) <<= 8; \ + (l) |= *(cp)++; \ + } /* * Warning: PUTLONGLONG destroys its first argument. */ -#define PUTLONGLONG(l, cp) { \ - (cp)[7] = l; \ - (cp)[6] = (l >>= 8); \ - (cp)[5] = (l >>= 8); \ - (cp)[4] = (l >>= 8); \ - (cp)[3] = (l >>= 8); \ - (cp)[2] = (l >>= 8); \ - (cp)[1] = (l >>= 8); \ - (cp)[0] = l >> 8; \ - (cp) += 8; \ -} +#define PUTLONGLONG(l, cp) \ + { \ + (cp)[7] = l; \ + (cp)[6] = (l >>= 8); \ + (cp)[5] = (l >>= 8); \ + (cp)[4] = (l >>= 8); \ + (cp)[3] = (l >>= 8); \ + (cp)[2] = (l >>= 8); \ + (cp)[1] = (l >>= 8); \ + (cp)[0] = l >> 8; \ + (cp) += 8; \ + } -#endif /* #ifndef _INOUT_H */ +#endif /* #ifndef _INOUT_H */ http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_llqueue.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_llqueue.h b/lib/ts/ink_llqueue.h index 67df59d..fa25ea0 100644 --- a/lib/ts/ink_llqueue.h +++ b/lib/ts/ink_llqueue.h @@ -32,28 +32,26 @@ A simple linked list queue. #include "ink_mutex.h" #include "ink_thread.h" -typedef struct llqrec_s -{ +typedef struct llqrec_s { struct llqrec_s *next; void *data; } LLQrec; -typedef struct llq_s -{ - LLQrec * head, *tail, *free; +typedef struct llq_s { + LLQrec *head, *tail, *free; uint64_t len, highwater; ink_mutex mux; ink_semaphore sema; } LLQ; LLQ *create_queue(void); -int enqueue(LLQ * q, void *data); -void *dequeue(LLQ * q); -bool queue_is_empty(LLQ * q); -uint64_t queue_len(LLQ * Q); -uint64_t queue_highwater(LLQ * Q); -void delete_queue(LLQ * Q); /* only deletes an empty queue but - provides symmetry. */ +int enqueue(LLQ *q, void *data); +void *dequeue(LLQ *q); +bool queue_is_empty(LLQ *q); +uint64_t queue_len(LLQ *Q); +uint64_t queue_highwater(LLQ *Q); +void delete_queue(LLQ *Q); /* only deletes an empty queue but + provides symmetry. */ #endif http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_lockfile.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_lockfile.h b/lib/ts/ink_lockfile.h index 900c932..6dc6bab 100644 --- a/lib/ts/ink_lockfile.h +++ b/lib/ts/ink_lockfile.h @@ -27,34 +27,28 @@ #include "ink_defs.h" #include "ink_string.h" -#define COP_LOCK "cop.lock" -#define MANAGER_LOCK "manager.lock" -#define SERVER_LOCK "server.lock" +#define COP_LOCK "cop.lock" +#define MANAGER_LOCK "manager.lock" +#define SERVER_LOCK "server.lock" class Lockfile { public: - Lockfile(void):fd(0) - { - fname[0] = '\0'; - } + Lockfile(void) : fd(0) { fname[0] = '\0'; } // coverity[uninit_member] - Lockfile(const char *filename):fd(0) - { - ink_strlcpy(fname, filename, sizeof(fname)); - } + Lockfile(const char *filename) : fd(0) { ink_strlcpy(fname, filename, sizeof(fname)); } - ~Lockfile(void) - { - } + ~Lockfile(void) {} - void SetLockfileName(const char *filename) + void + SetLockfileName(const char *filename) { ink_strlcpy(fname, filename, sizeof(fname)); } - const char *GetLockfileName(void) + const char * + GetLockfileName(void) { return fname; } @@ -65,7 +59,7 @@ public: // -errno on error // 0 if someone is holding the lock (with holding_pid set) // 1 if we now have a writable lock file - int Open(pid_t * holding_pid); + int Open(pid_t *holding_pid); // Get() // @@ -74,7 +68,7 @@ public: // -errno on error // 0 if someone is holding the lock (with holding_pid set) // 1 if we now have a writable lock file - int Get(pid_t * holding_pid); + int Get(pid_t *holding_pid); // Close() // http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_memory.cc ---------------------------------------------------------------------- diff --git a/lib/ts/ink_memory.cc b/lib/ts/ink_memory.cc index d2e94d6..4c18c43 100644 --- a/lib/ts/ink_memory.cc +++ b/lib/ts/ink_memory.cc @@ -53,7 +53,7 @@ ats_malloc(size_t size) } } return ptr; -} /* End ats_malloc */ +} /* End ats_malloc */ void * ats_calloc(size_t nelem, size_t elsize) @@ -64,7 +64,7 @@ ats_calloc(size_t nelem, size_t elsize) ink_fatal("ats_calloc: couldn't allocate %zu %zu byte elements", nelem, elsize); } return ptr; -} /* End ats_calloc */ +} /* End ats_calloc */ void * ats_realloc(void *ptr, size_t size) @@ -75,7 +75,7 @@ ats_realloc(void *ptr, size_t size) ink_fatal("ats_realloc: couldn't reallocate %zu bytes", size); } return newptr; -} /* End ats_realloc */ +} /* End ats_realloc */ // TODO: For Win32 platforms, we need to figure out what to do with memalign. // The older code had ifdef's around such calls, turning them into ats_malloc(). @@ -90,46 +90,43 @@ ats_memalign(size_t alignment, size_t size) #if defined(openbsd) if (alignment > PAGE_SIZE) - alignment = PAGE_SIZE; + alignment = PAGE_SIZE; #endif int retcode = posix_memalign(&ptr, alignment, size); if (unlikely(retcode)) { if (retcode == EINVAL) { - ink_fatal("ats_memalign: couldn't allocate %zu bytes at alignment %zu - invalid alignment parameter", - size, alignment); + ink_fatal("ats_memalign: couldn't allocate %zu bytes at alignment %zu - invalid alignment parameter", size, alignment); } else if (retcode == ENOMEM) { - ink_fatal("ats_memalign: couldn't allocate %zu bytes at alignment %zu - insufficient memory", - size, alignment); + ink_fatal("ats_memalign: couldn't allocate %zu bytes at alignment %zu - insufficient memory", size, alignment); } else { - ink_fatal("ats_memalign: couldn't allocate %zu bytes at alignment %zu - unknown error %d", - size, alignment, retcode); + ink_fatal("ats_memalign: couldn't allocate %zu bytes at alignment %zu - unknown error %d", size, alignment, retcode); } } #else ptr = memalign(alignment, size); if (unlikely(ptr == NULL)) { - ink_fatal("ats_memalign: couldn't allocate %zu bytes at alignment %zu", size, alignment); + ink_fatal("ats_memalign: couldn't allocate %zu bytes at alignment %zu", size, alignment); } #endif return ptr; -} /* End ats_memalign */ +} /* End ats_memalign */ void ats_free(void *ptr) { if (likely(ptr != NULL)) free(ptr); -} /* End ats_free */ +} /* End ats_free */ -void* +void * ats_free_null(void *ptr) { if (likely(ptr != NULL)) free(ptr); return NULL; -} /* End ats_free_null */ +} /* End ats_free_null */ void ats_memalign_free(void *ptr) @@ -166,11 +163,11 @@ ats_msync(caddr_t addr, size_t len, caddr_t end, int flags) size_t pagesize = ats_pagesize(); // align start back to page boundary - caddr_t a = (caddr_t) (((uintptr_t) addr) & ~(pagesize - 1)); + caddr_t a = (caddr_t)(((uintptr_t)addr) & ~(pagesize - 1)); // align length to page boundry covering region size_t l = (len + (addr - a) + (pagesize - 1)) & ~(pagesize - 1); if ((a + l) > end) - l = end - a; // strict limit + l = end - a; // strict limit #if defined(linux) /* Fix INKqa06500 Under Linux, msync(..., MS_SYNC) calls are painfully slow, even on @@ -191,13 +188,13 @@ int ats_madvise(caddr_t addr, size_t len, int flags) { #if defined(linux) - (void) addr; - (void) len; - (void) flags; + (void)addr; + (void)len; + (void)flags; return 0; #else size_t pagesize = ats_pagesize(); - caddr_t a = (caddr_t) (((uintptr_t) addr) & ~(pagesize - 1)); + caddr_t a = (caddr_t)(((uintptr_t)addr) & ~(pagesize - 1)); size_t l = (len + (addr - a) + pagesize - 1) & ~(pagesize - 1); int res = 0; #if HAVE_POSIX_MADVISE @@ -214,7 +211,7 @@ ats_mlock(caddr_t addr, size_t len) { size_t pagesize = ats_pagesize(); - caddr_t a = (caddr_t) (((uintptr_t) addr) & ~(pagesize - 1)); + caddr_t a = (caddr_t)(((uintptr_t)addr) & ~(pagesize - 1)); size_t l = (len + (addr - a) + pagesize - 1) & ~(pagesize - 1); int res = mlock(a, l); return res; @@ -225,7 +222,7 @@ ats_mlock(caddr_t addr, size_t len) Moved from old ink_resource.h -------------------------------------------------------------------------*/ char * -_xstrdup(const char *str, int length, const char* /* path ATS_UNUSED */) +_xstrdup(const char *str, int length, const char * /* path ATS_UNUSED */) { char *newstr; @@ -239,7 +236,7 @@ _xstrdup(const char *str, int length, const char* /* path ATS_UNUSED */) *newstr = '\0'; } else { strncpy(newstr, str, length); // we cannot do length + 1 because the string isn't - newstr[length] = '\0'; // guaranteeed to be null terminated! + newstr[length] = '\0'; // guaranteeed to be null terminated! } return newstr; }
