bool OnWebRequest(CWebSock& WebSock, const CString& sPageName, CTemplate& Tmpl) override { if (sPageName == "index") { if (CZNC::Get().GetManager().empty()) { return false; } std::priority_queue<CSocketSorter> socks = GetSockets(); while (!socks.empty()) { Csock* pSocket = socks.top().GetSock(); socks.pop(); CTemplate& Row = Tmpl.AddRow("SocketsLoop"); Row["Name"] = pSocket->GetSockName(); Row["Created"] = GetCreatedTime(pSocket); Row["State"] = GetSocketState(pSocket); Row["SSL"] = pSocket->GetSSL() ? "Yes" : "No"; Row["Local"] = GetLocalHost(pSocket, true); Row["Remote"] = GetRemoteHost(pSocket, true); Row["In"] = CString::ToByteStr(pSocket->GetBytesRead()); Row["Out"] = CString::ToByteStr(pSocket->GetBytesWritten()); } return true; } return false; }
int CLS_TCPSvr::Listen() { char *strIP; if(m_iStatus==SS_UNUSED) if(CreateSocket()<0) return -1; if(m_iStatus!=SS_LISTEN) { if(m_iStatus!=SS_BOUND) if(Bind()<0) return -2; if (listen(m_iSocket, 5)<0) { CloseSocket(); return -3; } if(!m_iLocalPort) { strIP=GetLocalHost(&m_iLocalPort); if(strIP) strcpy(m_strLocalIP, strIP); } m_iStatus=SS_LISTEN; } return 0; }
int CLS_UDP::Connect() { char *strIP; static struct sockaddr_in sockAddr; if(m_iStatus==SS_UNUSED) { if(CreateSocket()<0) return -1; } if(m_iStatus!=SS_CONNECT) { if(m_iStatus!=SS_BOUND) { if(Bind()<0) return -1; } memset(&sockAddr, 0, sizeof(sockAddr)); sockAddr.sin_family = AF_INET; sockAddr.sin_addr.s_addr = inet_addr(m_strPeerIP); sockAddr.sin_port = htons(m_iPeerPort); if(connect(m_iSocket, (struct sockaddr *)&sockAddr, sizeof(sockAddr))<0) { #ifdef _WIN32 m_iSocketError=GetLastError(); // if(!(m_iSocketError==WSAEINPROGRESS || m_iSocketError==WAEISCONN)) if(m_iSocketError!=WSAEISCONN) { if(m_iSocketError==WSAEINVAL) CloseSocket(); #else m_iSocketError=errno; // if(!(m_iSocketError==EINPROGRESS || m_iSocketError==EISCONN)) if(m_iSocketError!=EISCONN) { if(m_iSocketError==EINVAL) CloseSocket(); #endif return -3; } } if(!(m_strLocalIP[0]||m_iLocalPort)) { strIP=GetLocalHost(&m_iLocalPort); if(strIP) strcpy(m_strLocalIP, strIP); } m_iSocketError=0; m_iStatus=SS_CONNECT; } return 0; }
int CLS_UDP::Bind() { int iRet; char *strIP; iRet=CLS_Socket::Bind(m_strLocalIP, m_iLocalPort); if(!iRet && !m_iLocalPort) { strIP=GetLocalHost(&m_iLocalPort); if(strIP) strcpy(m_strLocalIP, strIP); } return iRet; }
nglString nuiTCPServer::GetDesc() const { nuiNetworkHost source(0, 0, nuiNetworkHost::eTCP); GetLocalHost(source); uint32 S = source.GetIP(); uint8* s = (uint8*)&S; nglString str; str.CFormat("%5d: %s - bound %d.%d.%d.%d:%d (%d clients) [ %s ]", GetSocket(), IsNonBlocking() ? "NoBlock" : "Block ", s[0], s[1], s[2], s[3], ntohs(source.GetPort()), mAcceptedCount, mName.GetChars()); return str; }
void ShowSocks(bool bShowHosts) { if (CZNC::Get().GetManager().empty()) { PutStatus("You have no open sockets."); return; } std::priority_queue<CSocketSorter> socks = GetSockets(); CTable Table; Table.AddColumn("Name"); Table.AddColumn("Created"); Table.AddColumn("State"); #ifdef HAVE_LIBSSL Table.AddColumn("SSL"); #endif Table.AddColumn("Local"); Table.AddColumn("Remote"); Table.AddColumn("In"); Table.AddColumn("Out"); while (!socks.empty()) { Csock* pSocket = socks.top().GetSock(); socks.pop(); Table.AddRow(); Table.SetCell("Name", pSocket->GetSockName()); Table.SetCell("Created", GetCreatedTime(pSocket)); Table.SetCell("State", GetSocketState(pSocket)); #ifdef HAVE_LIBSSL Table.SetCell("SSL", pSocket->GetSSL() ? "Yes" : "No"); #endif Table.SetCell("Local", GetLocalHost(pSocket, bShowHosts)); Table.SetCell("Remote", GetRemoteHost(pSocket, bShowHosts)); Table.SetCell("In", CString::ToByteStr(pSocket->GetBytesRead())); Table.SetCell("Out", CString::ToByteStr(pSocket->GetBytesWritten())); } PutModule(Table); return; }
nglString nuiSocket::GetDesc() const { nuiNetworkHost source(0, 0, nuiNetworkHost::eTCP); nuiNetworkHost dest(0, 0, nuiNetworkHost::eTCP); GetLocalHost(source); GetDistantHost(dest); uint32 S = source.GetIP(); uint32 D = dest.GetIP(); uint8* s = (uint8*)&S; uint8* d = (uint8*)&D; nglString str; str.CFormat("%5d: %s - from %d.%d.%d.%d:%d --> %d.%d.%d.%d:%d [ %s ]", GetSocket(), IsNonBlocking() ? "NoBlock" : "Block ", s[0], s[1], s[2], s[3], ntohs(source.GetPort()), d[0], d[1], d[2], d[3], ntohs(dest.GetPort()), mName.GetChars()); return str; }
nglString nuiTCPClient::GetDesc() const { nuiNetworkHost source(0, 0, nuiNetworkHost::eTCP); nuiNetworkHost dest(0, 0, nuiNetworkHost::eTCP); GetLocalHost(source); GetDistantHost(dest); uint32 S = source.GetIP(); uint32 D = dest.GetIP(); uint8* s = (uint8*)&S; uint8* d = (uint8*)&D; nglString str; str.CFormat("%5d: %s - from %d.%d.%d.%d:%d --> %d.%d.%d.%d:%d | i:%d / o:%d / a:%d / r:%lld / s:%lld| pool: %p%s [ %s ]", GetSocket(), IsNonBlocking() ? "NoBlock" : "Block ", s[0], s[1], s[2], s[3], ntohs(source.GetPort()), d[0], d[1], d[2], d[3], ntohs(dest.GetPort()), (int)mIn.GetSize(), (int)mOut.GetSize(), GetAvailable(), mReceived, mSent, mpPool, (mpAutoPool?" (auto)":""), mName.GetChars()); return str; }
int CLS_TCPClient::Connect() { char *strIP; static struct sockaddr_in sockAddr; if(m_iStatus==SS_UNUSED) { if(CreateSocket()<0) { // AfxMessageBox("CreateSocket Error!"); return -1; } } if(m_iStatus!=SS_CONNECT) { if(m_iStatus!=SS_BOUND) { if(Bind()<0) { #ifdef _WIN32 m_iSocketError=GetLastError(); #else m_iSocketError=errno; #endif CloseSocket(); return -1; } } memset(&sockAddr, 0, sizeof(sockAddr)); sockAddr.sin_family = AF_INET; sockAddr.sin_addr.s_addr = inet_addr(m_strSvrIP); sockAddr.sin_port = htons(m_iSvrPort); if(connect(m_iSocket, (struct sockaddr *)&sockAddr, sizeof(sockAddr))<0) { #ifdef _WIN32 m_iSocketError=GetLastError(); if(m_iSocketError!=WSAEISCONN) { if(m_iSocketError==WSAEINVAL) CloseSocket(); #else m_iSocketError=errno; if(m_iSocketError!=EISCONN) { if(m_iSocketError==EINVAL) CloseSocket(); #endif return -3; } } if(!(m_strLocalIP[0]||m_iLocalPort)) { strIP=GetLocalHost(&m_iLocalPort); if(strIP) strcpy(m_strLocalIP, strIP); } m_iSocketError=0; m_iStatus=SS_CONNECT; } return 0; } CLS_UDP::CLS_UDP() :CLS_Socket() { m_strLocalIP[0]=0; m_iLocalPort=0; m_strPeerIP[0]=0; m_iPeerPort=-1; } CLS_UDP::~CLS_UDP() { } void CLS_UDP::SetPeerIP(const char *strIP) { strcpy(m_strPeerIP, strIP); }
int CLogin::GetTqAuthToken(unsigned int &uin, const char *szStrid, const char *szPassWord, bool bAutoLogin) { int nlen = MAX_4096_LEN; char recvbuf[MAX_4096_LEN] = { 0 }; bool butf8(true); char myip[100]; if (this->m_pTqAuthClient == NULL) m_pTqAuthClient = new CTqAuthClient(m_manager->m_initConfig.sAuthAddr, m_manager->m_initConfig.nAuthPort, VERSION); else if (strlen(m_szAuthtoken) > 0) { m_pTqAuthClient->Logout(m_szAuthtoken, recvbuf, nlen, butf8); m_szAuthtoken[0] = 0; } GetLocalHost(myip); nlen = 4096; int nstate = SESSION_ACTION_ERR_NOTCONNECT; if (0 != m_pTqAuthClient->Login(uin, szStrid, szPassWord, 1, myip, recvbuf, nlen, butf8)) { return nstate; } CHttpParse p; p.ParseHead(recvbuf, nlen); ConvertMsg(recvbuf, sizeof(recvbuf)-1); string t; if (!p.GetPostBodyParams(recvbuf, "state", t)) { return nstate; } nstate = atoi(t.c_str()); if (nstate != 1) { return nstate; } if (p.GetPostBodyParams(recvbuf, "token", t)) { strncpy(m_szAuthtoken, t.c_str(), MAX_256_LEN); } if (p.GetPostBodyParams(recvbuf, "adminid", t)) { m_authAdminid = atol(t.c_str()); } if (p.GetPostBodyParams(recvbuf, "uin", t)) { uin = atol(t.c_str()); } // 手动登录的时候才使用认证返回的服务器地址 if (!bAutoLogin && p.GetPostBodyParams(recvbuf, "uilist", t)) { char server[MAX_128_LEN]; if (GetContentBetweenString(t.c_str(), "TCP:", ",", server)); { string info = server; int pos = info.find(":"); m_manager->m_server = info.substr(0, pos); info = info.substr(pos + 1, info.length() - 1); m_manager->m_port = atol(info.c_str()); } } return nstate; }