1 /*
2  *
3  * (C) 2003-2020 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 #include "services.h"
13 #include "anope.h"
14 #include "logger.h"
15 #include "sockets.h"
16 
17 #include <errno.h>
18 
Connect(const Anope::string & TargetHost,int Port)19 void ConnectionSocket::Connect(const Anope::string &TargetHost, int Port)
20 {
21 	this->io->Connect(this, TargetHost, Port);
22 }
23 
Process()24 bool ConnectionSocket::Process()
25 {
26 	try
27 	{
28 		if (this->flags[SF_CONNECTED])
29 			return true;
30 		else if (this->flags[SF_CONNECTING])
31 			this->flags[this->io->FinishConnect(this)] = true;
32 		else
33 			this->flags[SF_DEAD] = true;
34 	}
35 	catch (const SocketException &ex)
36 	{
37 		Log() << ex.GetReason();
38 	}
39 	return false;
40 }
41 
ProcessError()42 void ConnectionSocket::ProcessError()
43 {
44 	int optval = 0;
45 	socklen_t optlen = sizeof(optval);
46 	getsockopt(this->GetFD(), SOL_SOCKET, SO_ERROR, reinterpret_cast<char *>(&optval), &optlen);
47 	errno = optval;
48 	this->OnError(optval ? Anope::LastError() : "");
49 }
50 
OnConnect()51 void ConnectionSocket::OnConnect()
52 {
53 }
54 
OnError(const Anope::string & error)55 void ConnectionSocket::OnError(const Anope::string &error)
56 {
57 	Log(LOG_DEBUG) << "Socket error: " << error;
58 }
59 
ClientSocket(ListenSocket * l,const sockaddrs & addr)60 ClientSocket::ClientSocket(ListenSocket *l, const sockaddrs &addr) : ls(l), clientaddr(addr)
61 {
62 }
63 
Process()64 bool ClientSocket::Process()
65 {
66 	try
67 	{
68 		if (this->flags[SF_ACCEPTED])
69 			return true;
70 		else if (this->flags[SF_ACCEPTING])
71 			this->flags[this->io->FinishAccept(this)] = true;
72 		else
73 			this->flags[SF_DEAD] = true;
74 	}
75 	catch (const SocketException &ex)
76 	{
77 		Log() << ex.GetReason();
78 	}
79 	return false;
80 }
81 
ProcessError()82 void ClientSocket::ProcessError()
83 {
84 	int optval = 0;
85 	socklen_t optlen = sizeof(optval);
86 	getsockopt(this->GetFD(), SOL_SOCKET, SO_ERROR, reinterpret_cast<char *>(&optval), &optlen);
87 	errno = optval;
88 	this->OnError(optval ? Anope::LastError() : "");
89 }
90 
OnAccept()91 void ClientSocket::OnAccept()
92 {
93 }
94 
OnError(const Anope::string & error)95 void ClientSocket::OnError(const Anope::string &error)
96 {
97 	Log(LOG_DEBUG) << "Socket error: " << error;
98 }
99