diff options
-rw-r--r-- | include/socket.h | 3 | ||||
-rw-r--r-- | src/dns.cpp | 19 | ||||
-rw-r--r-- | src/inspsocket.cpp | 25 | ||||
-rw-r--r-- | src/socket.cpp | 55 |
4 files changed, 35 insertions, 67 deletions
diff --git a/include/socket.h b/include/socket.h index 4a91404e5..66578cca6 100644 --- a/include/socket.h +++ b/include/socket.h @@ -61,7 +61,6 @@ namespace irc */ typedef struct in6_addr insp_inaddr; #define AF_FAMILY AF_INET6 -#define PF_PROTOCOL PF_INET6 #else /** insp_sockaddr for ipv4 @@ -71,7 +70,6 @@ namespace irc */ typedef struct in_addr insp_inaddr; #define AF_FAMILY AF_INET -#define PF_PROTOCOL PF_INET #endif /** Match raw binary data using CIDR rules. @@ -193,6 +191,7 @@ class CoreExport ListenSocketBase : public EventHandler * The address family will always match that of "client" */ static irc::sockets::sockaddrs server; + public: /** Create a new listening socket */ diff --git a/src/dns.cpp b/src/dns.cpp index 79a2e2473..f088055f8 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -611,22 +611,17 @@ DNSResult DNS::GetResult() DNSHeader header; DNSRequest *req; unsigned char buffer[sizeof(DNSHeader)]; - sockaddr* from = new sockaddr[2]; -#ifdef IPV6 + irc::sockets::sockaddrs from; socklen_t x = this->socketfamily == AF_INET ? sizeof(sockaddr_in) : sizeof(sockaddr_in6); -#else - socklen_t x = sizeof(sockaddr_in); -#endif const char* ipaddr_from; unsigned short int port_from = 0; - int length = ServerInstance->SE->RecvFrom(this, (char*)buffer, sizeof(DNSHeader), 0, from, &x); + int length = ServerInstance->SE->RecvFrom(this, (char*)buffer, sizeof(DNSHeader), 0, &from.sa, &x); /* Did we get the whole header? */ if (length < 12) { /* Nope - something screwed up. */ - delete[] from; return DNSResult(-1,"",0,""); } @@ -643,18 +638,16 @@ DNSResult DNS::GetResult() char nbuf[MAXBUF]; if (this->socketfamily == AF_INET6) { - ipaddr_from = inet_ntop(AF_INET6, &((sockaddr_in6*)from)->sin6_addr, nbuf, sizeof(nbuf)); - port_from = ntohs(((sockaddr_in6*)from)->sin6_port); + ipaddr_from = inet_ntop(AF_INET6, &from.in6.sin6_addr, nbuf, sizeof(nbuf)); + port_from = ntohs(from.in6.sin6_port); } else #endif { - ipaddr_from = inet_ntoa(((sockaddr_in*)from)->sin_addr); - port_from = ntohs(((sockaddr_in*)from)->sin_port); + ipaddr_from = inet_ntoa(from.in4.sin_addr); + port_from = ntohs(from.in4.sin_port); } - delete[] from; - /* We cant perform this security check if you're using 4in6. * Tough luck to you, choose one or't other! */ diff --git a/src/inspsocket.cpp b/src/inspsocket.cpp index 7718fe70a..a23518148 100644 --- a/src/inspsocket.cpp +++ b/src/inspsocket.cpp @@ -105,56 +105,47 @@ void BufferedSocket::SetQueues() bool BufferedSocket::DoBindMagic(const std::string ¤t_ip, bool v6) { - /* The [2] is required because we may write a sockaddr_in6 here, and sockaddr_in6 is larger than sockaddr, where sockaddr_in4 is not. */ + irc::sockets::sockaddrs s; socklen_t size = sizeof(sockaddr_in); - sockaddr* s = new sockaddr[2]; #ifdef IPV6 if (v6) { - in6_addr n; - if (inet_pton(AF_INET6, current_ip.c_str(), &n) > 0) + if (inet_pton(AF_INET6, current_ip.c_str(), &s.in6.sin6_addr) > 0) { - memcpy(&((sockaddr_in6*)s)->sin6_addr, &n, sizeof(sockaddr_in6)); - ((sockaddr_in6*)s)->sin6_port = 0; - ((sockaddr_in6*)s)->sin6_family = AF_INET6; + s.in6.sin6_port = 0; + s.in6.sin6_family = AF_INET6; size = sizeof(sockaddr_in6); } else { // Well, this is as good as it's gonna get. errno = EADDRNOTAVAIL; - delete[] s; return false; } } else #endif { - in_addr n; - if (inet_aton(current_ip.c_str(), &n) > 0) + if (inet_aton(current_ip.c_str(), &s.in4.sin_addr) > 0) { - ((sockaddr_in*)s)->sin_addr = n; - ((sockaddr_in*)s)->sin_port = 0; - ((sockaddr_in*)s)->sin_family = AF_INET; + s.in4.sin_port = 0; + s.in4.sin_family = AF_INET; } else { // Well, this is as good as it's gonna get. errno = EADDRNOTAVAIL; - delete[] s; return false; } } - if (ServerInstance->SE->Bind(this->fd, s, size) < 0) + if (ServerInstance->SE->Bind(this->fd, &s.sa, size) < 0) { this->state = I_ERROR; this->OnError(I_ERR_BIND); - delete[] s; return false; } - delete[] s; return true; } diff --git a/src/socket.cpp b/src/socket.cpp index d18a8f5bc..d3e5fb072 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -16,6 +16,7 @@ #include "inspircd.h" #include "socket.h" #include "socketengine.h" +using irc::sockets::sockaddrs; /** This will bind a socket to a port. It works for UDP/TCP. * It can only bind to IP addresses, if you wish to bind to hostnames @@ -23,9 +24,8 @@ */ bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten) { - /* We allocate 2 of these, because sockaddr_in6 is larger than sockaddr (ugh, hax) */ - sockaddr* servaddr = new sockaddr[2]; - memset(servaddr,0,sizeof(sockaddr)*2); + sockaddrs servaddr; + memset(&servaddr, 0, sizeof(servaddr)); int ret, size; @@ -38,32 +38,22 @@ bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten) /* There is an address here. Is it ipv6? */ if (strchr(addr,':')) { - /* Yes it is */ - in6_addr addy; - if (inet_pton(AF_INET6, addr, &addy) < 1) + if (inet_pton(AF_INET6, addr, &servaddr.in6.sin6_addr) < 1) { - delete[] servaddr; return false; } - - ((sockaddr_in6*)servaddr)->sin6_family = AF_INET6; - memcpy(&(((sockaddr_in6*)servaddr)->sin6_addr), &addy, sizeof(in6_addr)); - ((sockaddr_in6*)servaddr)->sin6_port = htons(port); + servaddr.in6.sin6_family = AF_INET6; + servaddr.in6.sin6_port = htons(port); size = sizeof(sockaddr_in6); } else { - /* No, its not */ - in_addr addy; - if (inet_pton(AF_INET, addr, &addy) < 1) + if (inet_pton(AF_INET, addr, &servaddr.in4.sin_addr) < 1) { - delete[] servaddr; return false; } - - ((sockaddr_in*)servaddr)->sin_family = AF_INET; - ((sockaddr_in*)servaddr)->sin_addr = addy; - ((sockaddr_in*)servaddr)->sin_port = htons(port); + servaddr.in4.sin_family = AF_INET; + servaddr.in4.sin_port = htons(port); size = sizeof(sockaddr_in); } } @@ -74,45 +64,40 @@ bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten) /* Port -1: Means UDP IPV4 port binding - Special case * used by DNS engine. */ - ((sockaddr_in*)servaddr)->sin_family = AF_INET; - ((sockaddr_in*)servaddr)->sin_addr.s_addr = htonl(INADDR_ANY); - ((sockaddr_in*)servaddr)->sin_port = 0; + servaddr.in4.sin_family = AF_INET; + servaddr.in4.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.in4.sin_port = 0; size = sizeof(sockaddr_in); } else { - /* Theres no address here, default to ipv6 bind to all */ - ((sockaddr_in6*)servaddr)->sin6_family = AF_INET6; - memset(&(((sockaddr_in6*)servaddr)->sin6_addr), 0, sizeof(in6_addr)); - ((sockaddr_in6*)servaddr)->sin6_port = htons(port); + /* There's no address here, default to ipv6 bind to all */ + servaddr.in6.sin6_family = AF_INET6; + servaddr.in6.sin6_port = htons(port); size = sizeof(sockaddr_in6); } } #else /* If we aren't built with ipv6, the choice becomes simple */ - ((sockaddr_in*)servaddr)->sin_family = AF_INET; + servaddr.in4.sin_family = AF_INET; if (*addr) { /* There is an address here. */ - in_addr addy; - if (inet_pton(AF_INET, addr, &addy) < 1) + if (inet_pton(AF_INET, addr, &servaddr.in4.sin_addr) < 1) { - delete[] servaddr; return false; } - ((sockaddr_in*)servaddr)->sin_addr = addy; } else { /* Bind ipv4 to all */ - ((sockaddr_in*)servaddr)->sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.in4.sin_addr.s_addr = htonl(INADDR_ANY); } /* Bind ipv4 port number */ - ((sockaddr_in*)servaddr)->sin_port = htons(port); + servaddr.in4.sin_port = htons(port); size = sizeof(sockaddr_in); #endif - ret = SE->Bind(sockfd, servaddr, size); - delete[] servaddr; + ret = SE->Bind(sockfd, &servaddr.sa, size); if (ret < 0) { |