looks like this, walks like this or tastes like this.
*/
+#ifndef WIN32
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
+#else
+#include "inspircd_win32wrapper.h"
+#include "inspircd_se_config.h"
+#endif
+
#include "dns.h"
#include "inspircd.h"
#include "socketengine.h"
#include "configreader.h"
#include "socket.h"
-using irc::sockets::insp_sockaddr;
using irc::sockets::insp_inaddr;
using irc::sockets::insp_ntoa;
using irc::sockets::insp_aton;
+using irc::sockets::OpenTCPSocket;
/** Masks to mask off the responses we get from the DNSRequest methods
*/
unsigned char* res; /* Result processing buffer */
unsigned int rr_class; /* Request class */
QueryType type; /* Request type */
- insp_inaddr myserver; /* DNS server address*/
DNS* dnsobj; /* DNS caller (where we get our FD from) */
unsigned long ttl; /* Time to live */
std::string orig; /* Original requested name/ip */
- DNSRequest(InspIRCd* Instance, DNS* dns, insp_inaddr server, int id, const std::string &original);
+ DNSRequest(InspIRCd* Instance, DNS* dns, int id, const std::string &original);
~DNSRequest();
DNSInfo ResultIsReady(DNSHeader &h, int length);
int SendRequests(const DNSHeader *header, const int length, QueryType qt);
};
/* Allocate the processing buffer */
-DNSRequest::DNSRequest(InspIRCd* Instance, DNS* dns, insp_inaddr server, int id, const std::string &original) : dnsobj(dns)
+DNSRequest::DNSRequest(InspIRCd* Instance, DNS* dns, int id, const std::string &original) : dnsobj(dns)
{
res = new unsigned char[512];
*res = 0;
- memcpy(&myserver, &server, sizeof(insp_inaddr));
orig = original;
RequestTimeout* RT = new RequestTimeout(Instance->Config->dns_timeout ? Instance->Config->dns_timeout : 5, Instance, this, id);
Instance->Timers->AddTimer(RT); /* The timer manager frees this */
/** Send requests we have previously built down the UDP socket */
int DNSRequest::SendRequests(const DNSHeader *header, const int length, QueryType qt)
{
- insp_sockaddr addr;
unsigned char payload[sizeof(DNSHeader)];
this->rr_class = 1;
DNS::EmptyHeader(payload,header,length);
- memset(&addr,0,sizeof(addr));
#ifdef IPV6
- memcpy(&addr.sin6_addr,&myserver,sizeof(addr.sin6_addr));
- addr.sin6_family = AF_FAMILY;
- addr.sin6_port = htons(DNS::QUERY_PORT);
+ if (this->dnsobj->socketfamily == AF_INET6)
+ {
+ sockaddr_in6 addr;
+ memset(&addr,0,sizeof(addr));
+ memcpy(&addr.sin6_addr,&dnsobj->myserver6,sizeof(addr.sin6_addr));
+ addr.sin6_family = AF_INET6;
+ addr.sin6_port = htons(DNS::QUERY_PORT);
+ if (sendto(dnsobj->GetFd(), payload, length + 12, 0, (sockaddr *) &addr, sizeof(addr)) != length+12)
+ return -1;
+ }
+ else
+ {
+ sockaddr_in addr;
+ memset(&addr,0,sizeof(addr));
+ memcpy(&addr.sin_addr.s_addr,&dnsobj->myserver4,sizeof(addr.sin_addr));
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(DNS::QUERY_PORT);
+ if (sendto(dnsobj->GetFd(), payload, length + 12, 0, (sockaddr *) &addr, sizeof(addr)) != length+12)
+ return -1;
+ }
#else
- memcpy(&addr.sin_addr.s_addr,&myserver,sizeof(addr.sin_addr));
- addr.sin_family = AF_FAMILY;
+ sockaddr_in addr;
+ memset(&addr,0,sizeof(addr));
+ memcpy(&addr.sin_addr.s_addr, &dnsobj->myserver4, sizeof(addr.sin_addr));
+ addr.sin_family = AF_INET;
addr.sin_port = htons(DNS::QUERY_PORT);
-#endif
- if (sendto(dnsobj->GetFd(), payload, length + 12, 0, (sockaddr *) &addr, sizeof(addr)) == -1)
+ if (sendto(dnsobj->GetFd(), (const char*)payload, length + 12, 0, (sockaddr *) &addr, sizeof(addr)) != length+12)
return -1;
+#endif
return 0;
}
while (requests[id])
id = this->PRNG() & DNS::MAX_REQUEST_ID;
- DNSRequest* req = new DNSRequest(ServerInstance, this, this->myserver, id, original);
+ DNSRequest* req = new DNSRequest(ServerInstance, this, id, original);
header->id[0] = req->id[0] = id >> 8;
header->id[1] = req->id[1] = id & 0xFF;
void DNS::Rehash()
{
- insp_inaddr addr;
ip6munge = false;
+ int portpass = 0;
if (this->GetFd() > -1)
{
this->cache = new dnscache();
}
- if (insp_aton(ServerInstance->Config->DNSServer,&addr) > 0)
+ if ((strstr(ServerInstance->Config->DNSServer,"::ffff:") == (char*)&ServerInstance->Config->DNSServer) || (strstr(ServerInstance->Config->DNSServer,"::FFFF:") == (char*)&ServerInstance->Config->DNSServer))
{
- memcpy(&myserver,&addr,sizeof(insp_inaddr));
- if ((strstr(ServerInstance->Config->DNSServer,"::ffff:") == (char*)&ServerInstance->Config->DNSServer) || (strstr(ServerInstance->Config->DNSServer,"::FFFF:") == (char*)&ServerInstance->Config->DNSServer))
- {
- ServerInstance->Log(DEFAULT,"WARNING: Using IPv4 addresses over IPv6 forces some DNS checks to be disabled.");
- ServerInstance->Log(DEFAULT," This should not cause a problem, however it is recommended you migrate");
- ServerInstance->Log(DEFAULT," to a true IPv6 environment.");
- this->ip6munge = true;
- }
+ ServerInstance->Log(DEFAULT,"WARNING: Using IPv4 addresses over IPv6 forces some DNS checks to be disabled.");
+ ServerInstance->Log(DEFAULT," This should not cause a problem, however it is recommended you migrate");
+ ServerInstance->Log(DEFAULT," to a true IPv6 environment.");
+ this->ip6munge = true;
}
- /* Initialize mastersocket */
- this->SetFd(socket(PF_PROTOCOL, SOCK_DGRAM, 0));
- if (this->GetFd() != -1)
+ this->socketfamily = AF_INET;
+#ifdef IPV6
+ if (strchr(ServerInstance->Config->DNSServer,':'))
{
- /* Did it succeed? */
- if (fcntl(this->GetFd(), F_SETFL, O_NONBLOCK) != 0)
- {
- /* Couldn't make the socket nonblocking */
- shutdown(this->GetFd(),2);
- close(this->GetFd());
- this->SetFd(-1);
- }
+ this->socketfamily = AF_INET6;
+ inet_pton(AF_INET6, ServerInstance->Config->DNSServer, &this->myserver6);
}
+ else
+ {
+ inet_aton(ServerInstance->Config->DNSServer, &this->myserver4);
+ portpass = -1;
+ }
+#else
+ inet_aton(ServerInstance->Config->DNSServer, &this->myserver4);
+#endif
+
+ /* Initialize mastersocket */
+ int s = OpenTCPSocket(ServerInstance->Config->DNSServer, SOCK_DGRAM);
+ this->SetFd(s);
/* Have we got a socket and is it nonblocking? */
if (this->GetFd() != -1)
{
-#ifdef IPV6
- insp_sockaddr addr;
- memset(&addr,0,sizeof(addr));
- addr.sin6_family = AF_FAMILY;
- addr.sin6_port = 0;
- addr.sin6_addr = in6addr_any;
-#else
- insp_sockaddr addr;
- memset(&addr,0,sizeof(addr));
- addr.sin_family = AF_FAMILY;
- addr.sin_port = 0;
- addr.sin_addr.s_addr = INADDR_ANY;
-#endif
- /* Bind the port */
- if (bind(this->GetFd(),(sockaddr *)&addr,sizeof(addr)) != 0)
+ /* Bind the port - port 0 INADDR_ANY */
+ if (!ServerInstance->BindSocket(this->GetFd(), portpass, "", false))
{
/* Failed to bind */
shutdown(this->GetFd(),2);
DNSHeader header;
DNSRequest *req;
unsigned char buffer[sizeof(DNSHeader)];
- sockaddr from;
- socklen_t x = sizeof(from);
- const char* ipaddr_from = "";
+ sockaddr* from = new sockaddr[2];
+#ifdef IPV6
+ 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 = recvfrom(this->GetFd(),buffer,sizeof(DNSHeader),0,&from,&x);
+ int length = _recvfrom(this->GetFd(),(char*)buffer,sizeof(DNSHeader),0,from,&x);
/* Did we get the whole header? */
if (length < 12)
{
/* Nope - something screwed up. */
+ delete[] from;
return DNSResult(-1,"",0,"");
}
* -- Thanks jilles for pointing this one out.
*/
#ifdef IPV6
- ipaddr_from = insp_ntoa(((sockaddr_in6*)&from)->sin6_addr);
- port_from = ntohs(((sockaddr_in6*)&from)->sin6_port);
-#else
- ipaddr_from = insp_ntoa(((sockaddr_in*)&from)->sin_addr);
- port_from = ntohs(((sockaddr_in*)&from)->sin_port);
+ 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);
+ }
+ else
#endif
+ {
+ ipaddr_from = inet_ntoa(((sockaddr_in*)from)->sin_addr);
+ port_from = ntohs(((sockaddr_in*)from)->sin_port);
+ }
+
+ delete[] from;
/* We cant perform this security check if you're using 4in6.
* Tough luck to you, choose one or't other!
* as the last parameter on the line with a value ":1".
*/
if (*formatted == ':')
- resultstr = "0" + resultstr;
+ resultstr.insert(0, "0");
}
break;
/** Generate pseudo-random number */
unsigned long DNS::PRNG()
{
+#ifndef WIN32
unsigned long val = 0;
timeval n;
serverstats* s = ServerInstance->stats;
gettimeofday(&n,NULL);
val = (n.tv_usec ^ getpid() ^ geteuid() ^ (this->currid++)) ^ s->statsAccept + n.tv_sec;
val = val + s->statsCollisions ^ s->statsDnsGood - s->statsDnsBad;
- val += (s->statsConnects ^ (unsigned long)s->statsSent ^ (unsigned long)s->statsRecv) - s->BoundPortCount;
+ val += (s->statsConnects ^ (unsigned long)s->statsSent ^ (unsigned long)s->statsRecv) - ServerInstance->Config->ports.size();
return val;
+#else
+ unsigned long val = 0;
+ serverstats* s = ServerInstance->stats;
+ val = (time(NULL) ^ GetCurrentProcessId() ^ GetCurrentThreadId() ^ (this->currid++)) ^ s->statsAccept + time(NULL);
+ val = val + s->statsCollisions ^ s->statsDnsGood - s->statsDnsBad;
+ val += (s->statsConnects ^ (unsigned long)s->statsSent ^ (unsigned long)s->statsRecv);
+ return val;
+#endif
}