#include <cassert>
#include "dnssocket.h"
#include "loghandle.h"
#include "watchptr.h"
#include "wwwi/mutexholder.h"
using WWWI::MutexHolder;
DNSSocket::DNSSocket(IPAddress i_ipAddress, unsigned short i_usPort) {
this->SetSockOpt(SOL_SOCKET,SO_REUSEADDR);
this->Bind(i_ipAddress,i_usPort);
}
DNSSocket::~DNSSocket() {
this->Close();
}
DNSPacketPtr DNSSocket::RecvPacket() {
LogHandle lh(LF_UDP,LY_DEBUG);
DNSPacketPtr dppOut = new DNSPacket;
WatchPtr<DNSPacketPtr> watch_dppOut(dppOut);
#ifndef HAVE_REENTRANT_READ
MutexHolder mh(&m_mx);
#endif
dppOut->m_szBuffer = this->RecvFrom(dppOut->m_ucpBuffer,
dppOut->m_szBufferMax,
dppOut->m_sipRemote);
lh << "udp recv " << dppOut->m_szBuffer << " byte packet from " << inet_ntoa(dppOut->m_sipRemote->sin_addr) << endl;
lh << LY_EXTRA_DEBUG << dppOut; lh();
watch_dppOut.Release();
return dppOut;
}
void DNSSocket::SendPacket(DNSPacketConstPtr i_dppOut) {
LogHandle lh(LF_UDP,LY_DEBUG);
lh << "udp xmit " << i_dppOut->GetSize() << " bytes to " << inet_ntoa(i_dppOut->m_sipRemote->sin_addr) << endl;
lh << LY_EXTRA_DEBUG << i_dppOut; lh();
this->SendTo((const char*)i_dppOut->m_ucpBuffer,i_dppOut->m_szBuffer,i_dppOut->m_sipRemote);
}
DNSPacketPtr DNSSocket::RecvPacketTimed(unsigned i_uTimeoutMS) {
if (this->PollRead(i_uTimeoutMS)==false) return NULL;
return this->RecvPacket();
}