1: 
   2: 
   3: /**********************
   4: *                     *
   5: *  COMPILER INCLUDES  *
   6: *                     *
   7: **********************/
   8: 
   9: 
  10: #include <cassert> 
  11: 
  12: 
  13: /*********************
  14: *                    *
  15: *  PROJECT INCLUDES  *
  16: *                    *
  17: *********************/
  18: 
  19: 
  20: #include "dnssocket.h" 
  21: #include "loghandle.h" 
  22: #include "watchptr.h" 
  23: #include "wwwi/mutexholder.h" 
  24: 
  25: 
  26: using WWWI::MutexHolder;
  27: 
  28: 
  29: /**************************
  30: *                         *
  31: *  DNSSOCKET CONSTRUCTOR  *
  32: *                         *
  33: **************************/
  34: 
  35: 
  36: DNSSocket::DNSSocket(IPAddress i_ipAddress, unsigned short i_usPort) {
  37:   this->SetSockOpt(SOL_SOCKET,SO_REUSEADDR);
  38:   this->Bind(i_ipAddress,i_usPort);
  39: }
  40: 
  41: 
  42: /*************************
  43: *                        *
  44: *  DNSSOCKET DESTRUCTOR  *
  45: *                        *
  46: *************************/
  47: 
  48: 
  49: DNSSocket::~DNSSocket() {
  50:   this->Close();
  51: }
  52: 
  53:   
  54: /**********************
  55: *                     *
  56: *  METHOD RECVPACKET  *
  57: *                     *
  58: **********************/
  59: 
  60: 
  61: DNSPacketPtr DNSSocket::RecvPacket() {
  62:   LogHandle lh(LF_UDP,LY_DEBUG);
  63:   DNSPacketPtr dppOut = new DNSPacket;
  64:   WatchPtr<DNSPacketPtr> watch_dppOut(dppOut);
  65: 
  66: #ifndef HAVE_REENTRANT_READ 
  67:   MutexHolder mh(&m_mx);
  68: #endif 
  69:   dppOut->m_szBuffer = this->RecvFrom(dppOut->m_ucpBuffer,
  70:                                       dppOut->m_szBufferMax,
  71:                                       dppOut->m_sipRemote);
  72: 
  73: 
  74:   lh << "udp recv " << dppOut->m_szBuffer << " byte packet from " << inet_ntoa(dppOut->m_sipRemote->sin_addr) << endl;
  75:   lh << LY_EXTRA_DEBUG << dppOut; lh();
  76:   watch_dppOut.Release();
  77:   return dppOut;
  78: }
  79: 
  80: 
  81: /***********************
  82: *                      *
  83: *  METHOD SENDREQUEST  *
  84: *                      *
  85: ***********************/
  86: 
  87: 
  88: void DNSSocket::SendPacket(DNSPacketConstPtr i_dppOut) {
  89:   LogHandle lh(LF_UDP,LY_DEBUG);
  90:   lh << "udp xmit " << i_dppOut->GetSize() << " bytes to " << inet_ntoa(i_dppOut->m_sipRemote->sin_addr) << endl;
  91:   lh << LY_EXTRA_DEBUG << i_dppOut; lh();
  92:   this->SendTo((const char*)i_dppOut->m_ucpBuffer,i_dppOut->m_szBuffer,i_dppOut->m_sipRemote);
  93: }
  94: 
  95: 
  96: /****************************
  97: *                           *
  98: *  METHOD RECVREQUESTTIMED  *
  99: *                           *
 100: ****************************/
 101: 
 102: 
 103: DNSPacketPtr DNSSocket::RecvPacketTimed(unsigned i_uTimeoutMS) {
 104:   if (this->PollRead(i_uTimeoutMS)==false) return NULL;
 105:   return this->RecvPacket();
 106: }
 107: 
 108: 
 109: