bool CNetConnection::processInput() { std::vector<CData> input; if (!m_nextLayer->read(input)) return false; std::vector<CData>::iterator i; for (i = input.begin(); i != input.end(); i++) { CData data((*i).data(),(*i).length(),false); ireon::net::commandId id; data >> id; processCommand(id, data, netAddress(netAddress::atExternalAddress)); } return true; };
status_t DNSQuery::GetMXRecords(BString serverName, BObjectList<mx_record>* mxList, bigtime_t timeout) { // get the DNS server to ask for the mx record in_addr dnsAddress; if (ReadDNSServer(&dnsAddress) != B_OK) return B_ERROR; // create dns query package BRawNetBuffer buffer; dns_header header; _SetMXHeader(&header); _AppendQueryHeader(buffer, &header); BString serverNameConv = DNSTools::ConvertToDNSName(serverName); buffer.AppendString(serverNameConv.String()); buffer.AppendUint16(uint16(MX_RECORD)); buffer.AppendUint16(uint16(1)); // send the buffer PRINT("send buffer\n"); BNetAddress netAddress(dnsAddress, 53); BNetEndpoint netEndpoint(SOCK_DGRAM); if (netEndpoint.InitCheck() != B_OK) return B_ERROR; if (netEndpoint.Connect(netAddress) != B_OK) return B_ERROR; PRINT("Connected\n"); #ifdef DEBUG int32 bytesSend = #endif netEndpoint.Send(buffer.Data(), buffer.Size()); PRINT("bytes send %i\n", int(bytesSend)); // receive buffer BRawNetBuffer receiBuffer(512); netEndpoint.SetTimeout(timeout); #ifdef DEBUG int32 bytesRecei = #endif netEndpoint.ReceiveFrom(receiBuffer.Data(), 512, netAddress); PRINT("bytes received %i\n", int(bytesRecei)); dns_header receiHeader; _ReadQueryHeader(receiBuffer, &receiHeader); PRINT("Package contains :"); PRINT("%d Questions, ", receiHeader.q_count); PRINT("%d Answers, ", receiHeader.ans_count); PRINT("%d Authoritative Servers, ", receiHeader.auth_count); PRINT("%d Additional records\n", receiHeader.add_count); // remove name and Question BString dummyS; uint16 dummy; receiBuffer.ReadString(dummyS); receiBuffer.ReadUint16(dummy); receiBuffer.ReadUint16(dummy); bool mxRecordFound = false; for (int i = 0; i < receiHeader.ans_count; i++) { resource_record_head rrHead; _ReadResourceRecord(receiBuffer, &rrHead); if (rrHead.type == MX_RECORD) { mx_record *mxRec = new mx_record; _ReadMXRecord(receiBuffer, mxRec); PRINT("MX record found pri %i, name %s\n", mxRec->priority, mxRec->serverName.String()); // Add mx record to the list mxList->AddItem(mxRec); mxRecordFound = true; } else { buffer.SkipReading(rrHead.dataLength); } } if (!mxRecordFound) return B_ERROR; return B_OK; }
// _BroadcastListener int32 ServerManager::_BroadcastListener() { TaskManager taskManager; while (!fTerminating) { taskManager.RemoveDoneTasks(); // receive sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(kDefaultBroadcastPort); addr.sin_addr.s_addr = INADDR_ANY; socklen_t addrSize = sizeof(addr); BroadcastMessage message; //PRINT(("ServerManager::_BroadcastListener(): recvfrom()...\n")); ssize_t bytesRead = recvfrom(fBroadcastListenerSocket, &message, sizeof(message), 0, (sockaddr*)&addr, &addrSize); if (bytesRead < 0) { PRINT("ServerManager::_BroadcastListener(): recvfrom() " "failed: %s\n", strerror(errno)); continue; } // check message size, magic, and protocol version if (bytesRead != sizeof(BroadcastMessage)) { PRINT("ServerManager::_BroadcastListener(): received " "%ld bytes, but it should be %lu\n", bytesRead, sizeof(BroadcastMessage)); continue; } if (message.magic != B_HOST_TO_BENDIAN_INT32(BROADCAST_MESSAGE_MAGIC)) { PRINT("ServerManager::_BroadcastListener(): message has" " bad magic.\n"); continue; } if (message.protocolVersion != (int32)B_HOST_TO_BENDIAN_INT32(NETFS_PROTOCOL_VERSION)) { PRINT("ServerManager::_BroadcastListener(): protocol " "version does not match: %" B_PRId32 " vs. %d.\n", (int32)B_BENDIAN_TO_HOST_INT32( message.protocolVersion), NETFS_PROTOCOL_VERSION); continue; } // check, if the server is local NetAddress netAddress(addr); #ifndef ADD_SERVER_LOCALHOST if (netAddress.IsLocal()) continue; #endif // ADD_SERVER_LOCALHOST AutoLocker<Locker> locker(fLock); ExtendedServerInfo* oldServerInfo = fServerInfos->Get(netAddress); // examine the message switch (B_BENDIAN_TO_HOST_INT32(message.message)) { case BROADCAST_MESSAGE_SERVER_TICK: // PRINT(("ServerManager::_BroadcastListener(): " // "BROADCAST_MESSAGE_SERVER_TICK.\n")); if (oldServerInfo) continue; break; case BROADCAST_MESSAGE_SERVER_UPDATE: // PRINT(("ServerManager::_BroadcastListener(): " // "BROADCAST_MESSAGE_SERVER_UPDATE.\n")); break; case BROADCAST_MESSAGE_CLIENT_HELLO: // PRINT(("ServerManager::_BroadcastListener(): " // "BROADCAST_MESSAGE_CLIENT_HELLO. Ignoring.\n")); continue; break; } if (oldServerInfo && oldServerInfo->GetState() != STATE_READY) continue; // create a new server info and add it ExtendedServerInfo* serverInfo = new(std::nothrow) ExtendedServerInfo(netAddress); if (!serverInfo) return B_NO_MEMORY; serverInfo->SetState(STATE_ADDING); BReference<ExtendedServerInfo> serverInfoReference(serverInfo, true); if (oldServerInfo) { oldServerInfo->SetState(STATE_UPDATING); } else { status_t error = fServerInfos->Put(netAddress, serverInfo); if (error != B_OK) continue; serverInfo->AcquireReference(); } // create a task to add/update the server info ServerInfoTask* task = new(std::nothrow) ServerInfoTask(this, oldServerInfo, serverInfo); if (!task) { if (oldServerInfo) { oldServerInfo->SetState(STATE_READY); } else { fServerInfos->Remove(serverInfo->GetAddress()); serverInfo->ReleaseReference(); } continue; } // now the task has all info and will call the respective cleanup // method when being deleted if (task->Init() != B_OK) { delete task; continue; } status_t error = taskManager.RunTask(task); if (error != B_OK) { ERROR("ServerManager::_BroadcastListener(): Failed to start server " "info task: %s\n", strerror(error)); continue; } } return B_OK; }
void CCMApp::connectingHandler() { static uint reconnCounter = 0; if(m_rsConnectionManager->isConnected()) { CLog::instance()->log(CLog::msgFlagNetwork, CLog::msgLvlInfo,_("Connected to root server.\n")); CData tmp; tmp << m_id; m_rsConnectionManager->getConnection()->processCommandEx(ireon::net::cmrcCodes::scInitiateLogin, tmp, netAddress(netAddress::atInternalAddress)); setState(STATE_RUN); reconnCounter = 0; } else { ++reconnCounter; if( reconnCounter > 100 ) { CLog::instance()->log(CLog::msgFlagNetwork, CLog::msgLvlInfo,_("RS connection timeout, trying to reconnect to root server...\n")); if (!m_rsConnectionManager->reconnect()) shutdown(); reconnCounter = 0; }; } };