#include <cassert>
#include "requestworker.h"
#include "watchptr.h"
using WWWI::IPDump;
RequestWorker::RequestWorker(DNSSocketPtr i_dspClients) {
m_rvp = GetSingleton<Resolver>();
m_dspClients = i_dspClients;
}
bool RequestWorker::Wait() {
m_dppClient = m_dspClients->RecvPacket();
if (m_dppClient==NULL) return false;
return true;
}
void RequestWorker::Work() {
unsigned short usID;
RequestPtr rqpClient;
ResponsePtr rspClient;
SockAddrIn *sipClient;
LogHandle lh(LF_MISC,LY_DEBUG);
WatchPtr<RequestPtr> watch1(rqpClient);
WatchPtr<ResponsePtr> watch2(rspClient);
WatchPtr<DNSPacketPtr> watch3(m_dppClient);
sipClient = m_dppClient->RemoveAddr();
m_dppClient->Parse(usID,rqpClient,rspClient);
SoftDelete(m_dppClient);
assert(rspClient==NULL);
if (rspClient!=NULL) {
lh << LY_INFO << "unsolicited response from ";
IPDump(lh,sipClient);
lh << " will be ignored."; lh();
return;
}
assert(rqpClient->GetQuestionCount()==1);
if ((rspClient!=NULL)||
(rqpClient->GetQuestionCount()!=1)) {
SoftDelete(rspClient);
rspClient = new Response(RCODE_FORMERR);
goto processResponse;
}
if ((rqpClient->GetOpcode()!=OPCODE_QUERY)||
(rqpClient->GetQuestion()->GetClass()!=CL_IN)) {
rspClient = new Response(RCODE_NOTIMP);
goto processResponse;
}
lh << "received request from ";
IPDump(lh,sipClient);
lh << " " << endl;
lh << LY_EXTRA_DEBUG << "Request:" << endl << *rqpClient << endl; lh();
rspClient = m_rvp->QueryRecursive(rqpClient->GetQuestion(),32);
lh << "sending response to ";
IPDump(lh,sipClient);
lh << " " << endl;
lh << LY_EXTRA_DEBUG << "Response:" << endl << *rspClient << "(response ends)" << endl; lh();
processResponse:
m_dppClient = new DNSPacket(sipClient,usID,rqpClient,rspClient);
m_dspClients->SendPacket(m_dppClient);
SoftDelete(m_dppClient);
SoftDelete(rspClient);
SoftDelete(rqpClient);
}