Anope IRC Services  Version 2.0
socket_clients.cpp
Go to the documentation of this file.
1 /*
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  */
12 
13 #include "services.h"
14 #include "anope.h"
15 #include "logger.h"
16 #include "sockets.h"
17 
18 #include <errno.h>
19 
20 void ConnectionSocket::Connect(const Anope::string &TargetHost, int Port)
21 {
22  this->io->Connect(this, TargetHost, Port);
23 }
24 
26 {
27  try
28  {
29  if (this->flags[SF_CONNECTED])
30  return true;
31  else if (this->flags[SF_CONNECTING])
32  this->flags[this->io->FinishConnect(this)] = true;
33  else
34  this->flags[SF_DEAD] = true;
35  }
36  catch (const SocketException &ex)
37  {
38  Log() << ex.GetReason();
39  }
40  return false;
41 }
42 
44 {
45  int optval = 0;
46  socklen_t optlen = sizeof(optval);
47  getsockopt(this->GetFD(), SOL_SOCKET, SO_ERROR, reinterpret_cast<char *>(&optval), &optlen);
48  errno = optval;
49  this->OnError(optval ? Anope::LastError() : "");
50 }
51 
53 {
54 }
55 
57 {
58  Log(LOG_DEBUG) << "Socket error: " << error;
59 }
60 
61 ClientSocket::ClientSocket(ListenSocket *l, const sockaddrs &addr) : ls(l), clientaddr(addr)
62 {
63 }
64 
66 {
67  try
68  {
69  if (this->flags[SF_ACCEPTED])
70  return true;
71  else if (this->flags[SF_ACCEPTING])
72  this->flags[this->io->FinishAccept(this)] = true;
73  else
74  this->flags[SF_DEAD] = true;
75  }
76  catch (const SocketException &ex)
77  {
78  Log() << ex.GetReason();
79  }
80  return false;
81 }
82 
84 {
85  int optval = 0;
86  socklen_t optlen = sizeof(optval);
87  getsockopt(this->GetFD(), SOL_SOCKET, SO_ERROR, reinterpret_cast<char *>(&optval), &optlen);
88  errno = optval;
89  this->OnError(optval ? Anope::LastError() : "");
90 }
91 
93 {
94 }
95 
97 {
98  Log(LOG_DEBUG) << "Socket error: " << error;
99 }
100 
std::bitset< SF_SIZE > flags
Definition: sockets.h:203
SocketIO * io
Definition: sockets.h:209
virtual void OnConnect()
CoreExport const string LastError()
Definition: misc.cpp:606
void ProcessError() anope_override
virtual void OnError(const Anope::string &error)
int GetFD() const
Definition: sockets.cpp:474
ClientSocket(ListenSocket *ls, const sockaddrs &addr)
void Connect(const Anope::string &TargetHost, int Port)
virtual void Connect(ConnectionSocket *s, const Anope::string &target, int port)
Definition: sockets.cpp:401
bool Process() anope_override
virtual SocketFlag FinishConnect(ConnectionSocket *s)
Definition: sockets.cpp:423
virtual void OnError(const Anope::string &error)
virtual SocketFlag FinishAccept(ClientSocket *cs)
Definition: sockets.cpp:389
bool Process() anope_override
virtual const Anope::string & GetReason() const
Definition: anope.h:672
virtual void OnAccept()
Definition: logger.h:53
void ProcessError() anope_override