MCPcopy Create free account
hub / github.com/aria2/aria2 / createQueryMessage

Method createQueryMessage

src/DHTMessageFactoryImpl.cc:201–247  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

199} // namespace
200
201std::unique_ptr<DHTQueryMessage> DHTMessageFactoryImpl::createQueryMessage(
202 const Dict* dict, const std::string& ipaddr, uint16_t port)
203{
204 const String* messageType = getString(dict, DHTQueryMessage::Q);
205 const String* transactionID = getString(dict, DHTMessage::T);
206 const String* y = getString(dict, DHTMessage::Y);
207 const Dict* aDict = getDictionary(dict, DHTQueryMessage::A);
208 if (y->s() != DHTQueryMessage::Q) {
209 throw DL_ABORT_EX("Malformed DHT message. y != q");
210 }
211 const String* id = getString(aDict, DHTMessage::ID);
212 validateID(id);
213 auto remoteNode = getRemoteNode(id->uc(), ipaddr, port);
214 auto msg = std::unique_ptr<DHTQueryMessage>{};
215 if (messageType->s() == DHTPingMessage::PING) {
216 msg = createPingMessage(remoteNode, transactionID->s());
217 }
218 else if (messageType->s() == DHTFindNodeMessage::FIND_NODE) {
219 const String* targetNodeID =
220 getString(aDict, DHTFindNodeMessage::TARGET_NODE);
221 validateID(targetNodeID);
222 msg = createFindNodeMessage(remoteNode, targetNodeID->uc(),
223 transactionID->s());
224 }
225 else if (messageType->s() == DHTGetPeersMessage::GET_PEERS) {
226 const String* infoHash = getString(aDict, DHTGetPeersMessage::INFO_HASH);
227 validateID(infoHash);
228 msg = createGetPeersMessage(remoteNode, infoHash->uc(), transactionID->s());
229 }
230 else if (messageType->s() == DHTAnnouncePeerMessage::ANNOUNCE_PEER) {
231 const String* infoHash =
232 getString(aDict, DHTAnnouncePeerMessage::INFO_HASH);
233 validateID(infoHash);
234 const Integer* port = getInteger(aDict, DHTAnnouncePeerMessage::PORT);
235 validatePort(port);
236 const String* token = getString(aDict, DHTAnnouncePeerMessage::TOKEN);
237 msg = createAnnouncePeerMessage(remoteNode, infoHash->uc(),
238 static_cast<uint16_t>(port->i()),
239 token->s(), transactionID->s());
240 }
241 else {
242 throw DL_ABORT_EX(
243 fmt("Unsupported message type: %s", messageType->s().c_str()));
244 }
245 setVersion(msg.get(), dict);
246 return msg;
247}
248
249std::unique_ptr<DHTResponseMessage>
250DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType,

Callers 1

receiveMessageMethod · 0.45

Calls 8

getDictionaryFunction · 0.85
fmtFunction · 0.85
setVersionFunction · 0.85
ucMethod · 0.80
iMethod · 0.80
getStringFunction · 0.70
getIntegerFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected