diff options
author | Peter Powell <petpow@saberuk.com> | 2018-07-13 00:47:53 +0100 |
---|---|---|
committer | Peter Powell <petpow@saberuk.com> | 2018-07-18 19:21:45 +0100 |
commit | 87e328a1fbfcacafc013ba580d31dd4123f1e7e2 (patch) | |
tree | 5e7368079fec41e74cd0c30a6b80fa6da7b9599b | |
parent | 8f5952d1e56c9dbefebbacfc7e40546a9df901e8 (diff) |
Add the family() member to the sockaddrs union.
-rw-r--r-- | include/socket.h | 2 | ||||
-rw-r--r-- | src/coremods/core_dns.cpp | 8 | ||||
-rw-r--r-- | src/coremods/core_hostname_lookup.cpp | 4 | ||||
-rw-r--r-- | src/inspsocket.cpp | 4 | ||||
-rw-r--r-- | src/listensocket.cpp | 6 | ||||
-rw-r--r-- | src/modules/m_cloaking.cpp | 4 | ||||
-rw-r--r-- | src/modules/m_connectban.cpp | 2 | ||||
-rw-r--r-- | src/modules/m_dnsbl.cpp | 4 | ||||
-rw-r--r-- | src/modules/m_ident.cpp | 6 | ||||
-rw-r--r-- | src/modules/m_spanningtree/main.cpp | 2 | ||||
-rw-r--r-- | src/socket.cpp | 31 | ||||
-rw-r--r-- | src/users.cpp | 4 |
12 files changed, 42 insertions, 35 deletions
diff --git a/include/socket.h b/include/socket.h index aec06b526..6e9a20518 100644 --- a/include/socket.h +++ b/include/socket.h @@ -59,6 +59,8 @@ namespace irc struct sockaddr sa; struct sockaddr_in in4; struct sockaddr_in6 in6; + /** Return the family of the socket (e.g. AF_INET). */ + int family() const; /** Return the size of the structure for syscall passing */ socklen_t sa_size() const; /** Return port number or -1 if invalid */ diff --git a/src/coremods/core_dns.cpp b/src/coremods/core_dns.cpp index 4caea9c48..002686bc2 100644 --- a/src/coremods/core_dns.cpp +++ b/src/coremods/core_dns.cpp @@ -688,7 +688,7 @@ class MyManager : public Manager, public Timer, public EventHandler irc::sockets::aptosa(dnsserver, DNS::PORT, myserver); /* Initialize mastersocket */ - int s = socket(myserver.sa.sa_family, SOCK_DGRAM, 0); + int s = socket(myserver.family(), SOCK_DGRAM, 0); this->SetFd(s); /* Have we got a socket? */ @@ -701,9 +701,9 @@ class MyManager : public Manager, public Timer, public EventHandler if (sourceaddr.empty()) { // set a sourceaddr for irc::sockets::aptosa() based on the servers af type - if (myserver.sa.sa_family == AF_INET) + if (myserver.family() == AF_INET) sourceaddr = "0.0.0.0"; - else if (myserver.sa.sa_family == AF_INET6) + else if (myserver.family() == AF_INET6) sourceaddr = "::"; } irc::sockets::aptosa(sourceaddr, sourceport, bindto); @@ -722,7 +722,7 @@ class MyManager : public Manager, public Timer, public EventHandler this->SetFd(-1); } - if (bindto.sa.sa_family != myserver.sa.sa_family) + if (bindto.family() != myserver.family()) ServerInstance->Logs->Log(MODNAME, LOG_SPARSE, "Nameserver address family differs from source address family - hostnames might not resolve"); } else diff --git a/src/coremods/core_hostname_lookup.cpp b/src/coremods/core_hostname_lookup.cpp index 4320b1e57..bf882abf6 100644 --- a/src/coremods/core_hostname_lookup.cpp +++ b/src/coremods/core_hostname_lookup.cpp @@ -80,7 +80,7 @@ class UserResolver : public DNS::Request ph->set(bound_user, ans_record->rdata); UserResolver* res_forward; - if (bound_user->client_sa.sa.sa_family == AF_INET6) + if (bound_user->client_sa.family() == AF_INET6) { /* IPV6 forward lookup */ res_forward = new UserResolver(this->manager, this->creator, bound_user, ans_record->rdata, DNS::QUERY_AAAA); @@ -109,7 +109,7 @@ class UserResolver : public DNS::Request irc::sockets::sockaddrs* user_ip = &bound_user->client_sa; bool rev_match = false; - if (user_ip->sa.sa_family == AF_INET6) + if (user_ip->family() == AF_INET6) { struct in6_addr res_bin; if (inet_pton(AF_INET6, ans_record->rdata.c_str(), &res_bin)) diff --git a/src/inspsocket.cpp b/src/inspsocket.cpp index 42e2640a6..69c427212 100644 --- a/src/inspsocket.cpp +++ b/src/inspsocket.cpp @@ -83,12 +83,12 @@ BufferedSocketError BufferedSocket::BeginConnect(const std::string& ipaddr, int BufferedSocketError BufferedSocket::BeginConnect(const irc::sockets::sockaddrs& dest, const irc::sockets::sockaddrs& bind, unsigned int timeout) { if (fd < 0) - fd = socket(dest.sa.sa_family, SOCK_STREAM, 0); + fd = socket(dest.family(), SOCK_STREAM, 0); if (fd < 0) return I_ERR_SOCKET; - if (bind.sa.sa_family != 0) + if (bind.family() != 0) { if (SocketEngine::Bind(fd, bind) < 0) return I_ERR_BIND; diff --git a/src/listensocket.cpp b/src/listensocket.cpp index 4ec6c2b06..71364238b 100644 --- a/src/listensocket.cpp +++ b/src/listensocket.cpp @@ -29,7 +29,7 @@ ListenSocket::ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_t : bind_tag(tag) , bind_sa(bind_to) { - fd = socket(bind_to.sa.sa_family, SOCK_STREAM, 0); + fd = socket(bind_to.family(), SOCK_STREAM, 0); if (this->fd == -1) return; @@ -41,7 +41,7 @@ ListenSocket::ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_t * is "::" or an IPv6 address, disable support so that an IPv4 bind will * work on the port (by us or another application). */ - if (bind_to.sa.sa_family == AF_INET6) + if (bind_to.family() == AF_INET6) { std::string addr = tag->getString("address"); /* This must be >= sizeof(DWORD) on Windows */ @@ -134,7 +134,7 @@ void ListenSocket::OnEventHandlerRead() ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Can't get peername: %s", strerror(errno)); } - if (client.sa.sa_family == AF_INET6) + if (client.family() == AF_INET6) { /* * This case is the be all and end all patch to catch and nuke 4in6 diff --git a/src/modules/m_cloaking.cpp b/src/modules/m_cloaking.cpp index 515244231..87ff14a9d 100644 --- a/src/modules/m_cloaking.cpp +++ b/src/modules/m_cloaking.cpp @@ -223,7 +223,7 @@ class ModuleCloaking : public Module size_t hop1, hop2, hop3; size_t len1, len2; std::string rv; - if (ip.sa.sa_family == AF_INET6) + if (ip.family() == AF_INET6) { bindata = std::string((const char*)ip.in6.sin6_addr.s6_addr, 16); hop1 = 8; @@ -267,7 +267,7 @@ class ModuleCloaking : public Module } else { - if (ip.sa.sa_family == AF_INET6) + if (ip.family() == AF_INET6) { rv.append(InspIRCd::Format(".%02x%02x.%02x%02x%s", ip.in6.sin6_addr.s6_addr[2], ip.in6.sin6_addr.s6_addr[3], diff --git a/src/modules/m_connectban.cpp b/src/modules/m_connectban.cpp index 906724249..d57ffca02 100644 --- a/src/modules/m_connectban.cpp +++ b/src/modules/m_connectban.cpp @@ -54,7 +54,7 @@ class ModuleConnectBan : public Module unsigned char range = 32; - switch (u->client_sa.sa.sa_family) + switch (u->client_sa.family()) { case AF_INET6: range = ipv6_cidr; diff --git a/src/modules/m_dnsbl.cpp b/src/modules/m_dnsbl.cpp index 16694de93..95913c235 100644 --- a/src/modules/m_dnsbl.cpp +++ b/src/modules/m_dnsbl.cpp @@ -348,7 +348,7 @@ class ModuleDNSBL : public Module, public Stats::EventListener ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User has no connect class in OnSetUserIP"); std::string reversedip; - if (user->client_sa.sa.sa_family == AF_INET) + if (user->client_sa.family() == AF_INET) { unsigned int a, b, c, d; d = (unsigned int) (user->client_sa.in4.sin_addr.s_addr >> 24) & 0xFF; @@ -358,7 +358,7 @@ class ModuleDNSBL : public Module, public Stats::EventListener reversedip = ConvToStr(d) + "." + ConvToStr(c) + "." + ConvToStr(b) + "." + ConvToStr(a); } - else if (user->client_sa.sa.sa_family == AF_INET6) + else if (user->client_sa.family() == AF_INET6) { const unsigned char* ip = user->client_sa.in6.sin6_addr.s6_addr; diff --git a/src/modules/m_ident.cpp b/src/modules/m_ident.cpp index 4b110ccd6..f645a77ff 100644 --- a/src/modules/m_ident.cpp +++ b/src/modules/m_ident.cpp @@ -92,7 +92,7 @@ class IdentRequestSocket : public EventHandler { age = ServerInstance->Time(); - SetFd(socket(user->server_sa.sa.sa_family, SOCK_STREAM, 0)); + SetFd(socket(user->server_sa.family(), SOCK_STREAM, 0)); if (GetFd() == -1) throw ModuleException("Could not create socket"); @@ -105,7 +105,7 @@ class IdentRequestSocket : public EventHandler memcpy(&bindaddr, &user->server_sa, sizeof(bindaddr)); memcpy(&connaddr, &user->client_sa, sizeof(connaddr)); - if (connaddr.sa.sa_family == AF_INET6) + if (connaddr.family() == AF_INET6) { bindaddr.in6.sin6_port = 0; connaddr.in6.sin6_port = htons(113); @@ -148,7 +148,7 @@ class IdentRequestSocket : public EventHandler /* Build request in the form 'localport,remoteport\r\n' */ int req_size; - if (user->client_sa.sa.sa_family == AF_INET6) + if (user->client_sa.family() == AF_INET6) req_size = snprintf(req, sizeof(req), "%d,%d\r\n", ntohs(user->client_sa.in6.sin6_port), ntohs(user->server_sa.in6.sin6_port)); else diff --git a/src/modules/m_spanningtree/main.cpp b/src/modules/m_spanningtree/main.cpp index 2fd578698..0b311a0bd 100644 --- a/src/modules/m_spanningtree/main.cpp +++ b/src/modules/m_spanningtree/main.cpp @@ -243,7 +243,7 @@ void ModuleSpanningTree::ConnectServer(Link* x, Autoconnect* y) irc::sockets::sockaddrs bind; if ((!x->Bind.empty()) && (irc::sockets::aptosa(x->Bind, 0, bind))) { - if (bind.sa.sa_family == AF_INET) + if (bind.family() == AF_INET) start_type = DNS::QUERY_A; } diff --git a/src/socket.cpp b/src/socket.cpp index 6133d9ee4..5252e01e9 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -131,11 +131,16 @@ bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::socka return false; } +int irc::sockets::sockaddrs::family() const +{ + return sa.sa_family; +} + int irc::sockets::sockaddrs::port() const { - if (sa.sa_family == AF_INET) + if (family() == AF_INET) return ntohs(in4.sin_port); - if (sa.sa_family == AF_INET6) + if (family() == AF_INET6) return ntohs(in6.sin6_port); return -1; } @@ -143,13 +148,13 @@ int irc::sockets::sockaddrs::port() const std::string irc::sockets::sockaddrs::addr() const { char addrv[INET6_ADDRSTRLEN+1]; - if (sa.sa_family == AF_INET) + if (family() == AF_INET) { if (!inet_ntop(AF_INET, (void*)&in4.sin_addr, addrv, sizeof(addrv))) return ""; return addrv; } - else if (sa.sa_family == AF_INET6) + else if (family() == AF_INET6) { if (!inet_ntop(AF_INET6, (void*)&in6.sin6_addr, addrv, sizeof(addrv))) return ""; @@ -160,14 +165,14 @@ std::string irc::sockets::sockaddrs::addr() const std::string irc::sockets::sockaddrs::str() const { - if (sa.sa_family == AF_INET) + if (family() == AF_INET) { char ipaddr[INET_ADDRSTRLEN]; inet_ntop(AF_INET, (void*)&in4.sin_addr, ipaddr, sizeof(ipaddr)); return InspIRCd::Format("%s:%u", ipaddr, ntohs(in4.sin_port)); } - if (sa.sa_family == AF_INET6) + if (family() == AF_INET6) { char ipaddr[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, (void*)&in6.sin6_addr, ipaddr, sizeof(ipaddr)); @@ -180,20 +185,20 @@ std::string irc::sockets::sockaddrs::str() const socklen_t irc::sockets::sockaddrs::sa_size() const { - if (sa.sa_family == AF_INET) + if (family() == AF_INET) return sizeof(in4); - if (sa.sa_family == AF_INET6) + if (family() == AF_INET6) return sizeof(in6); return 0; } bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const { - if (sa.sa_family != other.sa.sa_family) + if (family() != other.family()) return false; - if (sa.sa_family == AF_INET) + if (family() == AF_INET) return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr); - if (sa.sa_family == AF_INET6) + if (family() == AF_INET6) return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16); return !memcmp(this, &other, sizeof(*this)); } @@ -202,7 +207,7 @@ static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs { const unsigned char* base; unsigned char target_byte; - cidr.type = sa.sa.sa_family; + cidr.type = sa.family(); memset(cidr.bits, 0, sizeof(cidr.bits)); @@ -301,7 +306,7 @@ bool irc::sockets::cidr_mask::operator<(const cidr_mask& other) const bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const { - if (addr.sa.sa_family != type) + if (addr.family() != type) return false; irc::sockets::cidr_mask tmp(addr, length); return tmp == *this; diff --git a/src/users.cpp b/src/users.cpp index 1e2554107..aa5100ce7 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -325,7 +325,7 @@ CullResult User::cull() if (!quitting) ServerInstance->Users->QuitUser(this, "Culled without QuitUser"); - if (client_sa.sa.sa_family != AF_UNSPEC) + if (client_sa.family() != AF_UNSPEC) ServerInstance->Users->RemoveCloneCounts(this); return Extensible::cull(); @@ -706,7 +706,7 @@ const std::string& User::GetRealHost() const irc::sockets::cidr_mask User::GetCIDRMask() { unsigned char range = 0; - switch (client_sa.sa.sa_family) + switch (client_sa.family()) { case AF_INET6: range = ServerInstance->Config->c_ipv6_range; |