// If an ip address is passed in, just return that. If a hostname is passed // in, look up its ip and return that. Returns "" on failure. std::string hostbyname(const char* hostname) { SockAddr sockAddr(hostname, 0, IPv6Enabled() ? AF_UNSPEC : AF_INET); if (!sockAddr.isValid() || sockAddr.getAddr() == "0.0.0.0") return ""; else return sockAddr.getAddr(); }
// If an ip address is passed in, just return that. If a hostname is passed // in, look up its ip and return that. Returns "" on failure. string hostbyname(const char *hostname) { SockAddr sockAddr(hostname, 0); if (!sockAddr.isValid() || sockAddr.getAddr() == "0.0.0.0") return ""; else return sockAddr.getAddr(); }
CAcceptor::CAcceptor(const char *host, int bind_port, \ int backlog, int timeout) : \ CSock(AF_INET, SOCK_STREAM) { CInetAddr sockAddr(host, bind_port); int res; if (setsockopt(m_fd, SOL_SOCKET, \ SO_REUSEADDR, &res, sizeof(int)) < 0) { KL_SYS_ERRORLOG("file: "__FILE__", line: %d, " \ "set SO_REUSEADDR failed, err: %s", \ __LINE__, strerror(errno)); throw errno; } if(bind(m_fd, sockAddr.getsockaddr(), \ sizeof(struct sockaddr)) == -1){ KL_SYS_ERRORLOG("file: "__FILE__", line: %d, " \ "call bind failed, err: %s", \ __LINE__, strerror(errno)); throw errno; } listen(m_fd, backlog); if(timeout > 0) //server option { if((res = setserveropt(timeout)) != 0) { KL_SYS_ERRORLOG("file: "__FILE__", line: %d, " \ "acceptor set server option failed, err: %s", \ __LINE__, strerror(res)); throw res; } } if((res = setnonblocking()) != 0) { KL_SYS_ERRORLOG("file: "__FILE__", line: %d, " \ "acceptor set nonblocking failed, err: %s", \ __LINE__, strerror(res)); throw res; } }
bool LiveAppModuleImpl::DispatchUdpPacket(BYTE* data, int size, const InetSocketAddress& remoteSockAddr, UINT proxyType, ExtraProxyEnum extraProxy) { m_Statistics.TotalFlow.Download.Record( size ); const size_t totalDataSize = size; APP_EVENT( "DispatchUdpPacket " << make_buffer_pair(data, size) << " from " << remoteSockAddr );//<< " " << HexEncoding::Encode(string((const char*)data, size)) ); //BYTE* rawData = data; //size_t rawSize = size; this->RecordDownload(size); SimpleSocketAddress sockAddr(remoteSockAddr); if ((size_t) size >= sizeof( OLD_UDP_PACKET_HEAD ) + sizeof( GUID ) ) { #ifndef _PPL_TEMP_DISABLE_TRACKER PacketInputStream is( data, size ); // 可能是老报文,尝试由tracker模块处理 if (m_tracker->TryHandleResponse( is, remoteSockAddr, (BYTE)proxyType )) { m_trackerFlow.Download.Record(size); return true; } #endif } // 解混淆 int len = PacketObfuscator::UnObfuscate( reinterpret_cast<BYTE*>( data ), size ); if ( len <= 0 ) { // 解混淆失败,可能是vod的报文 if ( VODProxy::HandlePacket( data, size, remoteSockAddr, m_BootModule.get(), m_StunModule.get() ) ) { return true; } m_Statistics.TotalInvalidPackets++; if ( size >= 3 ) { VIEW_ERROR( "LiveAppModuleImpl::DispatchUdpPacket unshuffle failed 1 " << make_tuple( size, len ) << sockAddr << " action=" << strings::format( "0x%02X%02X 0x%02X", data[0], data[1], data[2] ) ); } else { VIEW_ERROR( "LiveAppModuleImpl::DispatchUdpPacket unshuffle failed 1 " << make_tuple( size, len ) << sockAddr ); } //LIVE_ASSERT( false ); return false; } if ( size < len ) { m_Statistics.TotalInvalidPackets++; VIEW_ERROR( "LiveAppModuleImpl::DispatchUdpPacket unshuffle failed 2 " << make_tuple( size, len ) << sockAddr ); return false; } // 截去混淆部分的头,取出真正的报文部分 data += len; size -= len; PacketInputStream is( data, size ); NEW_UDP_PACKET_HEAD head; is >> head; if ( !is ) { m_Statistics.TotalInvalidPackets++; VIEW_ERROR( "LiveAppModuleImpl::DispatchUdpPacket invalid 2 " << size << " " << sockAddr ); return false; } if ( head.ProtocolVersion < SYNACAST_VERSION_3 ) { m_Statistics.TotalInvalidPackets++; // 检查版本号与MAGIC APP_ERROR("Invalid protocol version " << head.ProtocolVersion << " " << sockAddr); return false; } UDPT_INFO("LiveAppModuleImpl::DispatchUdp "<<sockAddr << " " << (int)head.Action); // session 处理完 if ( head.Action & PPL_P2P_CONNECTION_ACTION_FLAG ) { UDP_SESSION_INFO sessionInfo; is >> sessionInfo; if ( !is ) { m_Statistics.TotalInvalidPackets++; APP_ERROR("Invalid session size " << size << " " << sockAddr); return false; } bool res = m_Manager->HandleUDPSessionPacket(is, head, sessionInfo, sockAddr); if ( false == res ) { m_Statistics.TotalInvalidPackets++; } else { m_Statistics.UDPConnectionFlow.Download.Record(totalDataSize); } return res; }