ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Socket.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2014 ZNC, see the NOTICE file for details.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SOCKET_H
18 #define SOCKET_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/Csocket.h>
22 #include <znc/Threads.h>
23 
24 class CModule;
25 
26 class CZNCSock : public Csock {
27 public:
28  CZNCSock(int timeout = 60) : Csock(timeout) {}
29  CZNCSock(const CString& sHost, u_short port, int timeout = 60) : Csock(sHost, port, timeout) {}
30  ~CZNCSock() {}
31 
32  virtual int ConvertAddress(const struct sockaddr_storage * pAddr, socklen_t iAddrLen, CS_STRING & sIP, u_short * piPort);
33 
34 #ifndef HAVE_ICU
35  // Don't fail to compile when ICU is not enabled
36  void SetEncoding(const CString&) {}
37 #endif
38 };
39 
40 enum EAddrType {
44 };
45 
46 class CSockManager : public TSocketManager<CZNCSock> {
47 public:
48  CSockManager();
49  virtual ~CSockManager();
50 
51  bool ListenHost(u_short iPort, const CString& sSockName, const CString& sBindHost, bool bSSL = false, int iMaxConns = SOMAXCONN, CZNCSock *pcSock = NULL, u_int iTimeout = 0, EAddrType eAddr = ADDR_ALL) {
52  CSListener L(iPort, sBindHost);
53 
54  L.SetSockName(sSockName);
55  L.SetIsSSL(bSSL);
56  L.SetTimeout(iTimeout);
57  L.SetMaxConns(iMaxConns);
58 
59 #ifdef HAVE_IPV6
60  switch (eAddr) {
61  case ADDR_IPV4ONLY:
63  break;
64  case ADDR_IPV6ONLY:
66  break;
67  case ADDR_ALL:
69  break;
70  }
71 #endif
72 
73  return Listen(L, pcSock);
74  }
75 
76  bool ListenAll(u_short iPort, const CString& sSockName, bool bSSL = false, int iMaxConns = SOMAXCONN, CZNCSock *pcSock = NULL, u_int iTimeout = 0, EAddrType eAddr = ADDR_ALL) {
77  return ListenHost(iPort, sSockName, "", bSSL, iMaxConns, pcSock, iTimeout, eAddr);
78  }
79 
80  u_short ListenRand(const CString& sSockName, const CString& sBindHost, bool bSSL = false, int iMaxConns = SOMAXCONN, CZNCSock *pcSock = NULL, u_int iTimeout = 0, EAddrType eAddr = ADDR_ALL) {
81  unsigned short uPort = 0;
82  CSListener L(0, sBindHost);
83 
84  L.SetSockName(sSockName);
85  L.SetIsSSL(bSSL);
86  L.SetTimeout(iTimeout);
87  L.SetMaxConns(iMaxConns);
88 
89 #ifdef HAVE_IPV6
90  switch (eAddr) {
91  case ADDR_IPV4ONLY:
93  break;
94  case ADDR_IPV6ONLY:
96  break;
97  case ADDR_ALL:
99  break;
100  }
101 #endif
102 
103  Listen(L, pcSock, &uPort);
104 
105  return uPort;
106  }
107 
108  u_short ListenAllRand(const CString& sSockName, bool bSSL = false, int iMaxConns = SOMAXCONN, CZNCSock *pcSock = NULL, u_int iTimeout = 0, EAddrType eAddr = ADDR_ALL) {
109  return(ListenRand(sSockName, "", bSSL, iMaxConns, pcSock, iTimeout, eAddr));
110  }
111 
112  void Connect(const CString& sHostname, u_short iPort, const CString& sSockName, int iTimeout = 60, bool bSSL = false, const CString& sBindHost = "", CZNCSock *pcSock = NULL);
113 
114  unsigned int GetAnonConnectionCount(const CString &sIP) const;
115 private:
116  void FinishConnect(const CString& sHostname, u_short iPort, const CString& sSockName, int iTimeout, bool bSSL, const CString& sBindHost, CZNCSock *pcSock);
117 
119  friend class CTDNSMonitorFD;
120 #ifdef HAVE_THREADED_DNS
121  struct TDNSTask {
122  CString sHostname;
123  u_short iPort;
124  CString sSockName;
125  int iTimeout;
126  bool bSSL;
127  CString sBindhost;
128  CZNCSock* pcSock;
129 
130  bool bDoneTarget;
131  bool bDoneBind;
132  addrinfo* aiTarget;
133  addrinfo* aiBind;
134  };
135  class CDNSJob : public CJob {
136  public:
137  CString sHostname;
138  TDNSTask* task;
139  CSockManager* pManager;
140  bool bBind;
141 
142  int iRes;
143  addrinfo* aiResult;
144 
145  void runThread();
146  void runMain();
147  };
148  void StartTDNSThread(TDNSTask* task, bool bBind);
149  void SetTDNSThreadFinished(TDNSTask* task, bool bBind, addrinfo* aiResult);
150  static void* TDNSThread(void* argument);
151 #endif
152 protected:
153 };
154 
164 class CSocket : public CZNCSock {
165 public:
170  CSocket(CModule* pModule);
178  CSocket(CModule* pModule, const CString& sHostname, unsigned short uPort, int iTimeout = 60);
179  virtual ~CSocket();
180 
181  using Csock::Connect;
182  using Csock::Listen;
183 
185  virtual void ReachedMaxBuffer();
186  virtual void SockError(int iErrno, const CString& sDescription);
187 
189  virtual bool ConnectionFrom(const CString& sHost, unsigned short uPort);
190 
192  bool Connect(const CString& sHostname, unsigned short uPort, bool bSSL = false, unsigned int uTimeout = 60);
194  bool Listen(unsigned short uPort, bool bSSL, unsigned int uTimeout = 0);
195 
196  // Getters
197  CModule* GetModule() const;
198  // !Getters
199 private:
200 protected:
202 };
203 
204 #endif /* SOCKET_H */
Ease of use templated socket manager.
Definition: Csocket.h:1541
EAddrType
Definition: Socket.h:40
Definition: Csocket.h:202
u_short ListenRand(const CString &sSockName, const CString &sBindHost, bool bSSL=false, int iMaxConns=SOMAXCONN, CZNCSock *pcSock=NULL, u_int iTimeout=0, EAddrType eAddr=ADDR_ALL)
Definition: Socket.h:80
CModule * GetModule() const
void SetTimeout(uint32_t i)
sets the listen timeout. The listener class will close after timeout has been reached if not 0 ...
Definition: Csocket.h:1298
bool ListenHost(u_short iPort, const CString &sSockName, const CString &sBindHost, bool bSSL=false, int iMaxConns=SOMAXCONN, CZNCSock *pcSock=NULL, u_int iTimeout=0, EAddrType eAddr=ADDR_ALL)
Definition: Socket.h:51
CZNCSock(int timeout=60)
Definition: Socket.h:28
bool ListenAll(u_short iPort, const CString &sSockName, bool bSSL=false, int iMaxConns=SOMAXCONN, CZNCSock *pcSock=NULL, u_int iTimeout=0, EAddrType eAddr=ADDR_ALL)
Definition: Socket.h:76
virtual void SockError(int iErrno, const CString &sDescription)
u_short ListenAllRand(const CString &sSockName, bool bSSL=false, int iMaxConns=SOMAXCONN, CZNCSock *pcSock=NULL, u_int iTimeout=0, EAddrType eAddr=ADDR_ALL)
Definition: Socket.h:108
Definition: Socket.h:46
Definition: Socket.h:26
CSocket(CModule *pModule)
ctor
Base Csock implementation to be used by modules.
Definition: Socket.h:164
Definition: Csocket.h:204
Basic socket class.
Definition: Csocket.h:537
void SetAFRequire(CSSockAddr::EAFRequire iAFRequire)
sets the AF family type required
Definition: Csocket.h:1300
String class that is used inside ZNC.
Definition: ZNCString.h:67
#define CS_STRING
Definition: Csocket.h:115
bool Listen(unsigned short uPort, bool bSSL, unsigned int uTimeout=0)
Ease of use Listen, assigned to the manager and is subsequently tracked.
void SetMaxConns(int i)
set max connections as called by accept()
Definition: Csocket.h:1296
void Connect(const CString &sHostname, u_short iPort, const CString &sSockName, int iTimeout=60, bool bSSL=false, const CString &sBindHost="", CZNCSock *pcSock=NULL)
The base class for your own ZNC modules.
Definition: Modules.h:322
Definition: Socket.h:41
virtual bool Listen(const CSListener &cListen, Csock *pcSock=NULL, uint16_t *piRandPort=NULL)
Sets up a listening socket.
options container to create a listener
Definition: Csocket.h:1247
virtual bool ConnectionFrom(const CString &sHost, unsigned short uPort)
This limits the global connections from this IP to defeat DoS attacks, feel free to override...
Definition: Socket.h:42
virtual bool Listen(uint16_t iPort, int iMaxConns=SOMAXCONN, const CS_STRING &sBindHost="", uint32_t iTimeout=0, bool bDetach=false)
Listens for connections.
void SetSockName(const CS_STRING &sSockName)
sets the sock name for later reference (ie FindSockByName)
Definition: Csocket.h:1290
virtual ~CSockManager()
Definition: Socket.h:43
unsigned int GetAnonConnectionCount(const CString &sIP) const
virtual int ConvertAddress(const struct sockaddr_storage *pAddr, socklen_t iAddrLen, CS_STRING &sIP, u_short *piPort)
friend class CTDNSMonitorFD
Definition: Socket.h:118
CModule * m_pModule
pointer to the module that this sock instance belongs to
Definition: Socket.h:201
virtual void ReachedMaxBuffer()
This defaults to closing the socket, feel free to override.
void SetEncoding(const CString &)
Definition: Socket.h:36
Definition: Csocket.h:206
~CZNCSock()
Definition: Socket.h:30
virtual bool Connect()
Create the connection, this is used by the socket manager, and shouldn&#39;t be called directly by the us...
void SetIsSSL(bool b)
set to true to enable SSL
Definition: Csocket.h:1294
virtual ~CSocket()
CZNCSock(const CString &sHost, u_short port, int timeout=60)
Definition: Socket.h:29