void CZNCSock::SSLHandShakeFinished() { if (GetType() != ETConn::OUTBOUND) { return; } X509* pCert = GetX509(); if (!pCert) { DEBUG(GetSockName() + ": No cert"); CallSockError(errnoBadSSLCert, "Anonymous SSL cert is not allowed"); Close(); return; } CString sHostVerifyError; if (!ZNC_SSLVerifyHost(m_HostToVerifySSL, pCert, sHostVerifyError)) { m_ssCertVerificationErrors.insert(sHostVerifyError); } X509_free(pCert); if (m_ssCertVerificationErrors.empty()) { DEBUG(GetSockName() + ": Good cert"); return; } CString sFP = GetSSLPeerFingerprint(); if (m_ssTrustedFingerprints.count(sFP) != 0) { DEBUG(GetSockName() + ": Cert explicitly trusted by user: "******": Bad cert"); CString sErrorMsg = "Invalid SSL certificate: "; sErrorMsg += CString(", ").Join(begin(m_ssCertVerificationErrors), end(m_ssCertVerificationErrors)); CallSockError(errnoBadSSLCert, sErrorMsg); Close(); }
bool CSocket::Connect(const CString& sHostname, unsigned short uPort, bool bSSL, unsigned int uTimeout #ifdef HAVE_SHOES , const CString& sSocksAddr, unsigned short uSocksPort #endif /* HAVE_SHOES */ ) { if (!m_pModule) { DEBUG("ERROR: CSocket::Connect called on instance without m_pModule handle!"); return false; } CUser* pUser = m_pModule->GetUser(); CString sSockName = "MOD::C::" + m_pModule->GetModName(); CString sBindHost; if (pUser) { sSockName += "::" + pUser->GetUserName(); sBindHost = m_pModule->GetUser()->GetBindHost(); } // Don't overwrite the socket name if one is already set if (!GetSockName().empty()) { sSockName = GetSockName(); } return m_pModule->GetManager()->Connect(sHostname, uPort, sSockName, uTimeout, bSSL, sBindHost, this #ifdef HAVE_SHOES , sSocksAddr, uSocksPort #endif /* HAVE_SHOES */ ); }
bool CSocket::Connect(const CString& sHostname, unsigned short uPort, bool bSSL, unsigned int uTimeout) { if (!m_pModule) { DEBUG("ERROR: CSocket::Connect called on instance without m_pModule handle!"); return false; } CUser* pUser = m_pModule->GetUser(); CString sSockName = "MOD::C::" + m_pModule->GetModName(); CString sBindHost; if (pUser) { sSockName += "::" + pUser->GetUserName(); sBindHost = pUser->GetBindHost(); CIRCNetwork* pNetwork = m_pModule->GetNetwork(); if (pNetwork) { sSockName += "::" + pNetwork->GetName(); sBindHost = pNetwork->GetBindHost(); } } // Don't overwrite the socket name if one is already set if (!GetSockName().empty()) { sSockName = GetSockName(); } m_pModule->GetManager()->Connect(sHostname, uPort, sSockName, uTimeout, bSSL, sBindHost, this); return true; }
void CDCCBounce::ReadLine(const CString& sData) { CString sLine = sData.TrimRight_n("\r\n"); DEBUG(GetSockName() << " <- [" << sLine << "]"); PutPeer(sLine); }
/** * @brief Attach this VSocket to a pre-existing socket handle. * * @remarks Attaches this object to an existing socket handle and * enters the processing loop. * * @param pHandle * Handle to the socket to attach to. *------------------------------------------------------------------*/ bool VSocket::Attach(SOCKET pHandle) { char vAddress[51]; VUSHORT vPort = 0; BEG_FUNC("Attach")("%d", pHandle); if (mStatus == SS_CONNECTED) { VTRACE("Error-socket already connected to %s:%d\n", mEndPoint.GetAddr(), mEndPoint.GetPort()); mError = EISCONN; return END_FUNC(false); } if (mStatus == SS_BOUND) { VTRACE("Error-socket already bound to %s:%d\n", mLocal.GetAddr(), mLocal.GetPort()); mError = EISCONN; return END_FUNC(false); } mStatus = SS_ALLOCATED; mHandle = pHandle; GetPeerName(vAddress, 50, &vPort); mEndPoint.SetPort(vPort); mEndPoint.SetAddr(vAddress); memset(vAddress, 0, 50); GetSockName(vAddress, 50, &vPort); mLocal.SetPort(vPort); mLocal.SetAddr(vAddress); mStatus = SS_CONNECTED; return END_FUNC(true); }
bool CSocket::GetSockName(SOCKADDR_IN* lpSockInAddr) { memset(lpSockInAddr, 0, sizeof(SOCKADDR_IN)); socklen_t nSockAddrLen = sizeof(SOCKADDR_IN); return GetSockName((SOCKADDR*)lpSockInAddr, &nSockAddrLen); }
bool CRealListener::ConnectionFrom(const CString& sHost, unsigned short uPort) { bool bHostAllowed = CZNC::Get().IsHostAllowed(sHost); DEBUG(GetSockName() << " == ConnectionFrom(" << sHost << ", " << uPort << ") [" << (bHostAllowed ? "Allowed" : "Not allowed") << "]"); return bHostAllowed; }
void CSocket::ReachedMaxBuffer() { DEBUG(GetSockName() << " == ReachedMaxBuffer()"); if (m_pModule) m_pModule->PutModule( "Some socket reached its max buffer limit and was closed!"); Close(); }
void CGnuLocal::JoinLan(CString LanName) { m_pPrefs->m_LanName = LanName; CString LocalHost; UINT nPort; GetSockName(LocalHost, nPort); std::map<int, LanNode>::iterator itLanNode; for(itLanNode = m_LanNodeIDMap.begin(); itLanNode != m_LanNodeIDMap.end(); itLanNode++) if(itLanNode->second.Name == LanName) { Node PermNode; PermNode.Host = itLanNode->second.Host; PermNode.Port = itLanNode->second.Port; if(PermNode.Host != LocalHost) m_pComm->m_pCache->m_GnuPerm.push_back(PermNode); } m_pCore->Disconnect(NETWORK_GNUTELLA); m_pCore->Disconnect(NETWORK_G2); m_pCore->Connect(NETWORK_GNUTELLA); }
void CSocket::SockError(int iErrno) { DEBUG(GetSockName() << " == SockError(" << strerror(iErrno) << ")"); if (iErrno == EMFILE) { // We have too many open fds, this can cause a busy loop. Close(); } }
void CSocket::SockError(int iErrno, const CString& sDescription) { DEBUG(GetSockName() << " == SockError(" << sDescription << ", " << strerror(iErrno) << ")"); if (iErrno == EMFILE) { // We have too many open fds, this can cause a busy loop. Close(); } }
//passive send CM_PASSIVE command, the server should return the passive data channel address bool CServerSession::OnPassive( CNcpMessage* pMsg, int nBufSize ) { ASSERT( pMsg->GetCmd()==CM_PASSIVE ); //create the passive socket if it not yet if( m_datasock==0 ) { m_datasock = Socket( PF_INET, SOCK_STREAM, 0 ); SOCKADDR_IN sockaddr; sockaddr.sin_family = AF_INET; sockaddr.sin_port = 0; sockaddr.sin_addr.s_addr = INADDR_ANY; Bind( m_datasock, (SOCKADDR*)&sockaddr, sizeof(SOCKADDR_IN) ); Listen( m_datasock, 5 ); } //send the socket address informaton back to the client SOCKADDR_IN sockaddr; socklen_t nlen = sizeof(SOCKADDR_IN); GetSockName( m_datasock, (SOCKADDR*)&sockaddr, &nlen ); //first is IN_ADDR, then is port number. pMsg->Init( CM_ACK ); short *pport = (short*)( pMsg->GetData() ); *pport = sockaddr.sin_port; pMsg->SetDataSize( sizeof(short) ); pMsg->Send( m_sock ); return true; }
void Socket::SetIpTos (uint8_t tos) { Address address; GetSockName (address); m_manualIpTos = true; m_ipTos = tos; }
void CDCCBounce::ReachedMaxBuffer() { DEBUG(GetSockName() << " == ReachedMaxBuffer()"); CString sType = (m_bIsChat) ? "Chat" : "Xfer"; m_pModule->PutModule("DCC " + sType + " Bounce (" + m_sRemoteNick + "): Too long line received"); Close(); }
void CRealListener::SockError(int iErrno) { DEBUG(GetSockName() << " == SockError(" << strerror(iErrno) << ")"); if (iErrno == EMFILE) { // We have too many open fds, let's close this listening port to be able to continue // to work, next rehash will (try to) reopen it. Close(); } }
int DzGetSockName( int fd, struct sockaddr* addr, socklen_t* addrLen ) { DzHost* host = GetHost(); assert( host ); assert( fd >= 0 ); assert( ( fd & HANDLE_HOST_ID_MASK ) == host->hostId ); return GetSockName( host, fd, addr, addrLen ); }
void CDCCSock::Connected() { DEBUG(GetSockName() << " == Connected(" << GetRemoteIP() << ")"); m_pModule->PutModule(((m_bSend) ? "DCC -> [" : "DCC <- [") + m_sRemoteNick + "][" + m_sFileName + "] - Transfer Started."); if (m_bSend) { SendPacket(); } SetTimeout(120); }
bool CSocket::Listen(unsigned short uPort, bool bSSL, unsigned int uTimeout) { if (!m_pModule) { DEBUG("ERROR: CSocket::Listen called on instance without m_pModule handle!"); return false; } CUser* pUser = m_pModule->GetUser(); CString sSockName = "MOD::L::" + m_pModule->GetModName(); if (pUser) { sSockName += "::" + pUser->GetUserName(); } // Don't overwrite the socket name if one is already set if (!GetSockName().empty()) { sSockName = GetSockName(); } return m_pModule->GetManager()->ListenAll(uPort, sSockName, bSSL, SOMAXCONN, this); }
otError UdpSocket::SendTo(Message &aMessage, const MessageInfo &aMessageInfo) { otError error = OT_ERROR_NONE; MessageInfo messageInfoLocal; UdpHeader udpHeader; messageInfoLocal = aMessageInfo; if (messageInfoLocal.GetSockAddr().IsUnspecified()) { messageInfoLocal.SetSockAddr(GetSockName().GetAddress()); } if (GetSockName().mPort == 0) { GetSockName().mPort = static_cast<Udp *>(mTransport)->GetEphemeralPort(); } if (messageInfoLocal.GetPeerAddr().IsUnspecified()) { VerifyOrExit(!GetPeerName().GetAddress().IsUnspecified(), error = OT_ERROR_INVALID_ARGS); messageInfoLocal.SetPeerAddr(GetPeerName().GetAddress()); } if (messageInfoLocal.mPeerPort == 0) { VerifyOrExit(GetPeerName().mPort != 0, error = OT_ERROR_INVALID_ARGS); messageInfoLocal.mPeerPort = GetPeerName().mPort; } udpHeader.SetSourcePort(GetSockName().mPort); udpHeader.SetDestinationPort(messageInfoLocal.mPeerPort); udpHeader.SetLength(sizeof(udpHeader) + aMessage.GetLength()); udpHeader.SetChecksum(0); SuccessOrExit(error = aMessage.Prepend(&udpHeader, sizeof(udpHeader))); aMessage.SetOffset(0); SuccessOrExit(error = static_cast<Udp *>(mTransport)->SendDatagram(aMessage, messageInfoLocal, kProtoUdp)); exit: return error; }
otError UdpSocket::Bind(const SockAddr &aSockAddr) { mSockName = aSockAddr; if (GetSockName().mPort == 0) { mSockName.mPort = static_cast<Udp *>(mTransport)->GetEphemeralPort(); } return OT_ERROR_NONE; }
void CRealListener::SockError(int iErrno, const CString& sDescription) { DEBUG(GetSockName() << " == SockError(" << sDescription << ", " << strerror(iErrno) << ")"); if (iErrno == EMFILE) { // We have too many open fds, let's close this listening port to be able to continue // to work, next rehash will (try to) reopen it. CZNC::Get().Broadcast("We hit the FD limit, closing listening socket on [" + GetLocalIP() + " : " + CString(GetLocalPort()) + "]"); CZNC::Get().Broadcast("An admin has to rehash to reopen the listening port"); Close(); } }
/*======================================================================================== FUNCTION: LBaseSock::GetListenPort() DESCRIPT: Get the listening port #. RETURN : ARGUMENT: None UPDATE : 2004/07/01, In-hyeok Paek; First work! ========================================================================================*/ int LBaseSock::GetListenPort() { CString sAddr; // IP address unsigned int nPort; // Port # if( !GetSockName( sAddr, nPort ) ) { return nPort; } else { TRACE( "Socket get peer name failed: %d - %s\n", m_nLastErr, GetLastErrorString() ); return m_nLastErr; } }
bool CSocket::GetSockName(string& rSocketAddress, UINT& rSocketPort) { SOCKADDR_IN sockAddr; bool bResult = GetSockName(&sockAddr); if (bResult) { rSocketPort = ntohs(sockAddr.sin_port); rSocketAddress = inet_ntoa(sockAddr.sin_addr); } return bResult; }
CSocketAddress CGSocket::GetSockName() const { struct sockaddr_in SockAddrIn; int iRet = GetSockName( &SockAddrIn ); if ( iRet ) { return( CSocketAddress( INADDR_BROADCAST, 0 )); // invalid. } else { return( CSocketAddress( SockAddrIn )); } }
void CDCCBounce::ReadData(const char* data, size_t len) { if (m_pPeer) { m_pPeer->Write(data, len); size_t BufLen = m_pPeer->GetInternalWriteBuffer().length(); if (BufLen >= m_uiMaxDCCBuffer) { DEBUG(GetSockName() << " The send buffer is over the " "limit (" << BufLen <<"), throttling"); PauseRead(); } } }
void CDCCBounce::ConnectionRefused() { DEBUG(GetSockName() << " == ConnectionRefused()"); CString sType = (m_bIsChat) ? "Chat" : "Xfer"; CString sHost = Csock::GetHostName(); if (!sHost.empty()) { sHost = " to [" + sHost + " " + CString(Csock::GetPort()) + "]"; } else { sHost = "."; } m_pModule->PutModule("DCC " + sType + " Bounce (" + m_sRemoteNick + "): Connection Refused while connecting" + sHost); }
BOOL CAsyncSocket::GetSockName(CString& rSocketAddress, UINT& rSocketPort) { SOCKADDR_IN sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); int nSockAddrLen = sizeof(sockAddr); BOOL bResult = GetSockName((SOCKADDR*)&sockAddr, &nSockAddrLen); if (bResult) { rSocketPort = ntohs(sockAddr.sin_port); rSocketAddress = inet_ntoa(sockAddr.sin_addr); } return bResult; }
CString CZNCSock::GetSSLPeerFingerprint() const { #ifdef HAVE_LIBSSL // Csocket's version returns insecure SHA-1 // This one is SHA-256 const EVP_MD* evp = EVP_sha256(); X509* pCert = GetX509(); if (!pCert) { DEBUG(GetSockName() + ": GetSSLPeerFingerprint: Anonymous cert"); return ""; } unsigned char buf[256/8]; unsigned int _32 = 256/8; int iSuccess = X509_digest(pCert, evp, buf, &_32); X509_free(pCert); if (!iSuccess) { DEBUG(GetSockName() + ": GetSSLPeerFingerprint: Couldn't find digest"); return ""; } return CString(reinterpret_cast<const char*>(buf), sizeof buf).Escape_n(CString::EASCII, CString::EHEXCOLON); #else return ""; #endif }
BOOL CAsyncSocketExLayer::GetSockNameNext( CString& rSockAddress, UINT& rSockPort ) { if (m_pNextLayer) return m_pNextLayer->GetSockName(rSockAddress, rSockPort); else { SOCKADDR* sockAddr = NULL; int nSockAddrLen = 0; if (m_nFamily == AF_INET6) { sockAddr = (SOCKADDR*)new SOCKADDR_IN6; nSockAddrLen = sizeof(SOCKADDR_IN6); } else if (m_nFamily == AF_INET) { sockAddr = (SOCKADDR*)new SOCKADDR_IN; nSockAddrLen = sizeof(SOCKADDR_IN); } memset(sockAddr, 0, nSockAddrLen); BOOL bResult = GetSockName(sockAddr, &nSockAddrLen); if (bResult) { if (m_nFamily == AF_INET6) { rSockPort = ntohs(((SOCKADDR_IN6*)sockAddr)->sin6_port); LPTSTR buf = Inet6AddrToString(((SOCKADDR_IN6*)sockAddr)->sin6_addr); rSockAddress = buf; nb_free(buf); } else if (m_nFamily == AF_INET) { rSockPort = ntohs(((SOCKADDR_IN*)sockAddr)->sin_port); rSockAddress = inet_ntoa(((SOCKADDR_IN*)sockAddr)->sin_addr); } else { delete sockAddr; return FALSE; } } delete sockAddr; return bResult; } }
void CDCCBounce::SockError(int iErrno) { DEBUG(GetSockName() << " == SockError(" << iErrno << ")"); CString sType = (m_bIsChat) ? "Chat" : "Xfer"; if (IsRemote()) { CString sHost = Csock::GetHostName(); if (!sHost.empty()) { sHost = "[" + sHost + " " + CString(Csock::GetPort()) + "]"; } m_pModule->PutModule("DCC " + sType + " Bounce (" + m_sRemoteNick + "): Socket error [" + CString(strerror(iErrno)) + "]" + sHost); } else { m_pModule->PutModule("DCC " + sType + " Bounce (" + m_sRemoteNick + "): Socket error [" + CString(strerror(iErrno)) + "] [" + Csock::GetLocalIP() + ":" + CString(Csock::GetLocalPort()) + "]"); } }