~ubuntu-branches/ubuntu/trusty/znc/trusty

« back to all changes in this revision

Viewing changes to Socket.cpp

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-05-06 09:18:27 UTC
  • mfrom: (21.1.5 experimental)
  • Revision ID: package-import@ubuntu.com-20130506091827-08sixjiyy3hjfx6b
Tags: 1.0-4
* Change section from znc-tcl to interpreters.
* Uploading to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2004-2011  See the AUTHORS file for details.
3
 
 *
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.
7
 
 */
8
 
 
9
 
#include "Socket.h"
10
 
#include "Modules.h"
11
 
#include "User.h"
12
 
#include "znc.h"
13
 
 
14
 
unsigned int CSockManager::GetAnonConnectionCount(const CString &sIP) const {
15
 
        const_iterator it;
16
 
        unsigned int ret = 0;
17
 
 
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::") {
23
 
                        ret++;
24
 
                }
25
 
        }
26
 
 
27
 
        DEBUG("There are [" << ret << "] clients from [" << sIP << "]");
28
 
 
29
 
        return ret;
30
 
}
31
 
 
32
 
CS_STRING CZNCSock::ConvertAddress(void *addr, bool ipv6) {
33
 
        CString sRet = Csock::ConvertAddress(addr, ipv6);
34
 
        sRet.TrimPrefix("::ffff:");
35
 
        return sRet;
36
 
}
37
 
 
38
 
/////////////////// CSocket ///////////////////
39
 
CSocket::CSocket(CModule* pModule) : CZNCSock() {
40
 
        m_pModule = pModule;
41
 
        if (m_pModule) m_pModule->AddSocket(this);
42
 
        EnableReadLine();
43
 
        SetMaxBufferThreshold(10240);
44
 
}
45
 
 
46
 
CSocket::CSocket(CModule* pModule, const CString& sHostname, unsigned short uPort, int iTimeout) : CZNCSock(sHostname, uPort, iTimeout) {
47
 
        m_pModule = pModule;
48
 
        if (m_pModule) m_pModule->AddSocket(this);
49
 
        EnableReadLine();
50
 
        SetMaxBufferThreshold(10240);
51
 
}
52
 
 
53
 
CSocket::~CSocket() {
54
 
        CUser *pUser = NULL;
55
 
 
56
 
        // CWebSock could cause us to have a NULL pointer here
57
 
        if (m_pModule) {
58
 
                pUser = m_pModule->GetUser();
59
 
                m_pModule->UnlinkSocket(this);
60
 
        }
61
 
 
62
 
        if (pUser && m_pModule && !m_pModule->IsGlobal()) {
63
 
                pUser->AddBytesWritten(GetBytesWritten());
64
 
                pUser->AddBytesRead(GetBytesRead());
65
 
        } else {
66
 
                CZNC::Get().AddBytesWritten(GetBytesWritten());
67
 
                CZNC::Get().AddBytesRead(GetBytesRead());
68
 
        }
69
 
}
70
 
 
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!");
74
 
        Close();
75
 
}
76
 
 
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.
81
 
                Close();
82
 
        }
83
 
}
84
 
 
85
 
bool CSocket::ConnectionFrom(const CString& sHost, unsigned short uPort) {
86
 
        return CZNC::Get().AllowConnectionFrom(sHost);
87
 
}
88
 
 
89
 
bool CSocket::Connect(const CString& sHostname, unsigned short uPort, bool bSSL, unsigned int uTimeout) {
90
 
        if (!m_pModule) {
91
 
                DEBUG("ERROR: CSocket::Connect called on instance without m_pModule handle!");
92
 
                return false;
93
 
        }
94
 
 
95
 
        CUser* pUser = m_pModule->GetUser();
96
 
        CString sSockName = "MOD::C::" + m_pModule->GetModName();
97
 
        CString sBindHost;
98
 
 
99
 
        if (pUser) {
100
 
                sSockName += "::" + pUser->GetUserName();
101
 
                sBindHost = m_pModule->GetUser()->GetBindHost();
102
 
        }
103
 
 
104
 
        // Don't overwrite the socket name if one is already set
105
 
        if (!GetSockName().empty()) {
106
 
                sSockName = GetSockName();
107
 
        }
108
 
 
109
 
        return m_pModule->GetManager()->Connect(sHostname, uPort, sSockName, uTimeout, bSSL, sBindHost, this);
110
 
}
111
 
 
112
 
bool CSocket::Listen(unsigned short uPort, bool bSSL, unsigned int uTimeout) {
113
 
        if (!m_pModule) {
114
 
                DEBUG("ERROR: CSocket::Listen called on instance without m_pModule handle!");
115
 
                return false;
116
 
        }
117
 
 
118
 
        CUser* pUser = m_pModule->GetUser();
119
 
        CString sSockName = "MOD::L::" + m_pModule->GetModName();
120
 
 
121
 
        if (pUser) {
122
 
                sSockName += "::" + pUser->GetUserName();
123
 
        }
124
 
        // Don't overwrite the socket name if one is already set
125
 
        if (!GetSockName().empty()) {
126
 
                sSockName = GetSockName();
127
 
        }
128
 
 
129
 
        return m_pModule->GetManager()->ListenAll(uPort, sSockName, bSSL, SOMAXCONN, this);
130
 
}
131
 
 
132
 
CModule* CSocket::GetModule() const { return m_pModule; }
133
 
/////////////////// !CSocket ///////////////////