Logo
~Sockets~
~Examples~
~Contact~


ResolvSocket Class Reference
[Asynchronous DNS]

Async DNS resolver socket. More...

#include <ResolvSocket.h>

Inheritance diagram for ResolvSocket:

Inheritance graph
[legend]
Collaboration diagram for ResolvSocket:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ResolvSocket (ISocketHandler &, Socket *parent=NULL)
 ~ResolvSocket ()
void OnAccept ()
 Called when an incoming connection has been completed.
void OnLine (const std::string &line)
 Callback fires when a socket in line protocol has read one full line.
void OnDetached ()
 Callback fires when a new socket thread has started and this socket is ready for operation again.
void OnDelete ()
 Called before a socket class is deleted by the ISocketHandler.
void SetId (int x)
void SetHost (const std::string &x)
void SetAddress (ipaddr_t x)
void SetPort (port_t x)
void OnConnect ()
 Called when a connection has completed.

Private Member Functions

 ResolvSocket (const ResolvSocket &s)
ResolvSocketoperator= (const ResolvSocket &)

Private Attributes

std::string m_query
std::string m_data
bool m_bServer
Socketm_parent
 Pointer to ListenSocket class, valid for incoming sockets.
int m_resolv_id
std::string m_resolv_host
port_t m_resolv_port
ipaddr_t m_resolv_address

Detailed Description

Async DNS resolver socket.

Definition at line 42 of file ResolvSocket.h.


Constructor & Destructor Documentation

ResolvSocket::ResolvSocket ( ISocketHandler ,
Socket parent = NULL 
)

Definition at line 52 of file ResolvSocket.cpp.

References TcpSocket::SetLineProtocol().

00053 :TcpSocket(h)
00054 ,m_bServer(false)
00055 ,m_parent(parent)
00056 #ifdef ENABLE_IPV6
00057 ,m_resolve_ipv6(false)
00058 #endif
00059 {
00060         SetLineProtocol();
00061 }

ResolvSocket::~ResolvSocket (  ) 

Definition at line 64 of file ResolvSocket.cpp.

00065 {
00066 }

ResolvSocket::ResolvSocket ( const ResolvSocket s  )  [inline, private]

Definition at line 67 of file ResolvSocket.h.

00067 : TcpSocket(s) {} // copy constructor


Member Function Documentation

void ResolvSocket::OnAccept (  )  [inline, virtual]

Called when an incoming connection has been completed.

Reimplemented from Socket.

Definition at line 48 of file ResolvSocket.h.

00048 { m_bServer = true; }

void ResolvSocket::OnLine ( const std::string &  line  )  [virtual]

Callback fires when a socket in line protocol has read one full line.

Parameters:
line Line read

Reimplemented from TcpSocket.

Definition at line 69 of file ResolvSocket.cpp.

References DEB, Socket::Detach(), Parse::getrest(), Parse::getword(), Socket::Handler(), m_bServer, m_data, m_parent, m_query, m_resolv_host, m_resolv_id, m_resolv_port, TcpSocket::OnResolved(), Socket::OnResolveFailed(), Socket::OnReverseResolved(), Socket::SetCloseAndDelete(), and Utility::u2ip().

00070 {
00071         Parse pa(line, ":");
00072         if (m_bServer)
00073         {
00074                 m_query = pa.getword();
00075                 m_data = pa.getrest();
00076 DEB(            fprintf(stderr, "ResolvSocket server; query=%s, data=%s\n", m_query.c_str(), m_data.c_str());)
00077                 if (!Detach()) // detach failed?
00078                 {
00079                         SetCloseAndDelete();
00080                 }
00081                 return;
00082         }
00083         std::string key = pa.getword();
00084         std::string value = pa.getrest();
00085 
00086         if (key == "Failed" && m_parent)
00087         {
00088 DEB(            fprintf(stderr, "************ Resolve failed\n");)
00089                 if (Handler().Valid(m_parent))
00090                         m_parent -> OnResolveFailed(m_resolv_id);
00091                 m_parent = NULL;
00092         }
00093         else
00094         if (key == "Name" && !m_resolv_host.size() && m_parent)
00095         {
00096                 if (Handler().Valid(m_parent))
00097                         m_parent -> OnReverseResolved(m_resolv_id, value);
00098                 m_parent = NULL;
00099         }
00100         else
00101         if (key == "A" && m_parent)
00102         {
00103                 ipaddr_t l;
00104                 Utility::u2ip(value, l); // ip2ipaddr_t
00105                 if (Handler().Valid(m_parent))
00106                         m_parent -> OnResolved(m_resolv_id, l, m_resolv_port);
00107                 m_parent = NULL; // always use first ip in case there are several
00108         }
00109 #ifdef ENABLE_IPV6
00110 #ifdef IPPROTO_IPV6
00111         else
00112         if (key == "AAAA" && m_parent)
00113         {
00114                 in6_addr a;
00115                 Utility::u2ip(value, a);
00116                 if (Handler().Valid(m_parent))
00117                         m_parent -> OnResolved(m_resolv_id, a, m_resolv_port);
00118                 m_parent = NULL;
00119         }
00120 #endif
00121 #endif
00122 }

void ResolvSocket::OnDetached (  )  [virtual]

Callback fires when a new socket thread has started and this socket is ready for operation again.

See also:
ResolvSocket

Reimplemented from Socket.

Definition at line 125 of file ResolvSocket.cpp.

References DEB, Socket::Handler(), Utility::isipv4(), Utility::isipv6(), Utility::l2ip(), ISocketHandler::LogError(), m_data, m_query, Utility::reverse(), TcpSocket::Send(), Socket::SetCloseAndDelete(), and Utility::u2ip().

00126 {
00127 DEB(    fprintf(stderr, "ResolvSocket::OnDetached(); query=%s, data=%s\n", m_query.c_str(), m_data.c_str());)
00128         if (m_query == "gethostbyname")
00129         {
00130                 struct sockaddr_in sa;
00131                 if (Utility::u2ip(m_data, sa))
00132                 {
00133                         std::string ip;
00134                         Utility::l2ip(sa.sin_addr, ip);
00135                         Send("A: " + ip + "\n");
00136                 }
00137                 else
00138                 {
00139                         Send("Failed\n");
00140                 }
00141                 Send("\n");
00142         }
00143         else
00144 #ifdef ENABLE_IPV6
00145 #ifdef IPPROTO_IPV6
00146         if (m_query == "gethostbyname2")
00147         {
00148                 struct sockaddr_in6 sa;
00149                 if (Utility::u2ip(m_data, sa))
00150                 {
00151                         std::string ip;
00152                         Utility::l2ip(sa.sin6_addr, ip);
00153                         Send("AAAA: " + ip + "\n");
00154                 }
00155                 else
00156                 {
00157                         Send("Failed\n");
00158                 }
00159                 Send("\n");
00160         }
00161         else
00162 #endif
00163 #endif
00164         if (m_query == "gethostbyaddr")
00165         {
00166                 if (Utility::isipv4( m_data ))
00167                 {
00168                         struct sockaddr_in sa;
00169                         if (!Utility::u2ip(m_data, sa, AI_NUMERICHOST))
00170                         {
00171                                 Send("Failed: convert to sockaddr_in failed\n");
00172                         }
00173                         else
00174                         {
00175                                 std::string name;
00176                                 if (!Utility::reverse( (struct sockaddr *)&sa, sizeof(sa), name))
00177                                 {
00178                                         Send("Failed: ipv4 reverse lookup of " + m_data + "\n");
00179                                 }
00180                                 else
00181                                 {
00182                                         Send("Name: " + name + "\n");
00183                                 }
00184                         }
00185                 }
00186                 else
00187 #ifdef ENABLE_IPV6
00188 #ifdef IPPROTO_IPV6
00189                 if (Utility::isipv6( m_data ))
00190                 {
00191                         struct sockaddr_in6 sa;
00192                         if (!Utility::u2ip(m_data, sa, AI_NUMERICHOST))
00193                         {
00194                                 Send("Failed: convert to sockaddr_in6 failed\n");
00195                         }
00196                         else
00197                         {
00198                                 std::string name;
00199                                 if (!Utility::reverse( (struct sockaddr *)&sa, sizeof(sa), name))
00200                                 {
00201                                         Send("Failed: ipv6 reverse lookup of " + m_data + "\n");
00202                                 }
00203                                 else
00204                                 {
00205                                         Send("Name: " + name + "\n");
00206                                 }
00207                         }
00208                 }
00209                 else
00210 #endif
00211 #endif
00212                 {
00213                         Send("Failed: malformed address\n");
00214                 }
00215                 Send("\n");
00216         }
00217         else
00218         {
00219                 std::string msg = "Unknown query type: " + m_query;
00220                 Handler().LogError(this, "OnDetached", 0, msg);
00221                 Send("Unknown\n\n");
00222         }
00223         SetCloseAndDelete();
00224 }

void ResolvSocket::OnDelete (  )  [virtual]

Called before a socket class is deleted by the ISocketHandler.

Reimplemented from Socket.

Definition at line 255 of file ResolvSocket.cpp.

References Socket::Handler(), m_parent, m_resolv_id, and Socket::OnResolveFailed().

00256 {
00257         if (m_parent)
00258         {
00259                 if (Handler().Valid(m_parent))
00260                         m_parent -> OnResolveFailed(m_resolv_id);
00261                 m_parent = NULL;
00262         }
00263 }

void ResolvSocket::SetId ( int  x  )  [inline]

Definition at line 53 of file ResolvSocket.h.

00053 { m_resolv_id = x; }

void ResolvSocket::SetHost ( const std::string &  x  )  [inline]

Definition at line 54 of file ResolvSocket.h.

00054 { m_resolv_host = x; }

void ResolvSocket::SetAddress ( ipaddr_t  x  )  [inline]

Definition at line 55 of file ResolvSocket.h.

00055 { m_resolv_address = x; }

void ResolvSocket::SetPort ( port_t  x  )  [inline]

Definition at line 59 of file ResolvSocket.h.

00059 { m_resolv_port = x; }

void ResolvSocket::OnConnect (  )  [virtual]

Called when a connection has completed.

Reimplemented from Socket.

Definition at line 227 of file ResolvSocket.cpp.

References Utility::l2ip(), m_resolv_address, m_resolv_host, and TcpSocket::Send().

00228 {
00229         if (m_resolv_host.size())
00230         {
00231 #ifdef ENABLE_IPV6
00232                 std::string msg = (m_resolve_ipv6 ? "gethostbyname2 " : "gethostbyname ") + m_resolv_host + "\n";
00233 #else
00234                 std::string msg = "gethostbyname " + m_resolv_host + "\n";
00235 #endif
00236                 Send( msg );
00237                 return;
00238         }
00239 #ifdef ENABLE_IPV6
00240         if (m_resolve_ipv6)
00241         {
00242                 std::string tmp;
00243                 Utility::l2ip(m_resolv_address6, tmp);
00244                 std::string msg = "gethostbyaddr " + tmp + "\n";
00245                 Send( msg );
00246         }
00247 #endif
00248         std::string tmp;
00249         Utility::l2ip(m_resolv_address, tmp);
00250         std::string msg = "gethostbyaddr " + tmp + "\n";
00251         Send( msg );
00252 }

ResolvSocket& ResolvSocket::operator= ( const ResolvSocket  )  [inline, private]

Definition at line 68 of file ResolvSocket.h.

00068 { return *this; } // assignment operator


Member Data Documentation

std::string ResolvSocket::m_query [private]

Definition at line 70 of file ResolvSocket.h.

Referenced by OnDetached(), and OnLine().

std::string ResolvSocket::m_data [private]

Definition at line 71 of file ResolvSocket.h.

Referenced by OnDetached(), and OnLine().

bool ResolvSocket::m_bServer [private]

Definition at line 72 of file ResolvSocket.h.

Referenced by OnLine().

Pointer to ListenSocket class, valid for incoming sockets.

Reimplemented from Socket.

Definition at line 73 of file ResolvSocket.h.

Referenced by OnDelete(), and OnLine().

Definition at line 74 of file ResolvSocket.h.

Referenced by OnDelete(), and OnLine().

std::string ResolvSocket::m_resolv_host [private]

Definition at line 75 of file ResolvSocket.h.

Referenced by OnConnect(), and OnLine().

Definition at line 76 of file ResolvSocket.h.

Referenced by OnLine().

Definition at line 77 of file ResolvSocket.h.

Referenced by OnConnect().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4