2
* Copyright (C) 2004-2011 See the AUTHORS file for details.
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 2 as published
6
* by the Free Software Foundation.
14
unsigned int CSockManager::GetAnonConnectionCount(const CString &sIP) const {
18
for (it = begin(); it != end(); ++it) {
19
CZNCSock *pSock = *it;
20
// Logged in CClients have "USR::<username>" as their sockname
21
if (pSock->GetType() == Csock::INBOUND && pSock->GetRemoteIP() == sIP
22
&& pSock->GetSockName().Left(5) != "USR::") {
27
DEBUG("There are [" << ret << "] clients from [" << sIP << "]");
32
CS_STRING CZNCSock::ConvertAddress(void *addr, bool ipv6) {
33
CString sRet = Csock::ConvertAddress(addr, ipv6);
34
sRet.TrimPrefix("::ffff:");
38
/////////////////// CSocket ///////////////////
39
CSocket::CSocket(CModule* pModule) : CZNCSock() {
41
if (m_pModule) m_pModule->AddSocket(this);
43
SetMaxBufferThreshold(10240);
46
CSocket::CSocket(CModule* pModule, const CString& sHostname, unsigned short uPort, int iTimeout) : CZNCSock(sHostname, uPort, iTimeout) {
48
if (m_pModule) m_pModule->AddSocket(this);
50
SetMaxBufferThreshold(10240);
56
// CWebSock could cause us to have a NULL pointer here
58
pUser = m_pModule->GetUser();
59
m_pModule->UnlinkSocket(this);
62
if (pUser && m_pModule && !m_pModule->IsGlobal()) {
63
pUser->AddBytesWritten(GetBytesWritten());
64
pUser->AddBytesRead(GetBytesRead());
66
CZNC::Get().AddBytesWritten(GetBytesWritten());
67
CZNC::Get().AddBytesRead(GetBytesRead());
71
void CSocket::ReachedMaxBuffer() {
72
DEBUG(GetSockName() << " == ReachedMaxBuffer()");
73
if (m_pModule) m_pModule->PutModule("Some socket reached its max buffer limit and was closed!");
77
void CSocket::SockError(int iErrno) {
78
DEBUG(GetSockName() << " == SockError(" << strerror(iErrno) << ")");
79
if (iErrno == EMFILE) {
80
// We have too many open fds, this can cause a busy loop.
85
bool CSocket::ConnectionFrom(const CString& sHost, unsigned short uPort) {
86
return CZNC::Get().AllowConnectionFrom(sHost);
89
bool CSocket::Connect(const CString& sHostname, unsigned short uPort, bool bSSL, unsigned int uTimeout) {
91
DEBUG("ERROR: CSocket::Connect called on instance without m_pModule handle!");
95
CUser* pUser = m_pModule->GetUser();
96
CString sSockName = "MOD::C::" + m_pModule->GetModName();
100
sSockName += "::" + pUser->GetUserName();
101
sBindHost = m_pModule->GetUser()->GetBindHost();
104
// Don't overwrite the socket name if one is already set
105
if (!GetSockName().empty()) {
106
sSockName = GetSockName();
109
return m_pModule->GetManager()->Connect(sHostname, uPort, sSockName, uTimeout, bSSL, sBindHost, this);
112
bool CSocket::Listen(unsigned short uPort, bool bSSL, unsigned int uTimeout) {
114
DEBUG("ERROR: CSocket::Listen called on instance without m_pModule handle!");
118
CUser* pUser = m_pModule->GetUser();
119
CString sSockName = "MOD::L::" + m_pModule->GetModName();
122
sSockName += "::" + pUser->GetUserName();
124
// Don't overwrite the socket name if one is already set
125
if (!GetSockName().empty()) {
126
sSockName = GetSockName();
129
return m_pModule->GetManager()->ListenAll(uPort, sSockName, bSSL, SOMAXCONN, this);
132
CModule* CSocket::GetModule() const { return m_pModule; }
133
/////////////////// !CSocket ///////////////////