Skip to content

Commit

Permalink
network: use full 32-bits of transaction IDs
Browse files Browse the repository at this point in the history
We used to use the first 16 bits for the message type.
  • Loading branch information
aberaud committed Oct 20, 2017
1 parent 4501217 commit 476fbb1
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 180 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -107,6 +107,7 @@ list (APPEND opendht_SOURCES
src/storage.h
src/listener.h
src/search.h
src/net.h
src/parsed_message.h
src/request.h
src/callbacks.cpp
Expand Down Expand Up @@ -134,7 +135,6 @@ list (APPEND opendht_HEADERS
include/opendht/routing_table.h
include/opendht/node_cache.h
include/opendht/network_engine.h
include/opendht/net.h
include/opendht/scheduler.h
include/opendht/rate_limiter.h
include/opendht/securedht.h
Expand Down
2 changes: 1 addition & 1 deletion MSVC/opendht.vcxproj
Expand Up @@ -47,7 +47,6 @@
<ClInclude Include="..\include\opendht\infohash.h" />
<ClInclude Include="..\include\opendht\log.h" />
<ClInclude Include="..\include\opendht\log_enable.h" />
<ClInclude Include="..\include\opendht\net.h" />
<ClInclude Include="..\include\opendht\network_engine.h" />
<ClInclude Include="..\include\opendht\node.h" />
<ClInclude Include="..\include\opendht\node_cache.h" />
Expand All @@ -60,6 +59,7 @@
<ClInclude Include="..\include\opendht\sockaddr.h" />
<ClInclude Include="..\include\opendht\utils.h" />
<ClInclude Include="..\include\opendht\value.h" />
<ClInclude Include="..\src\net.h" />
<ClInclude Include="..\src\listener.h" />
<ClInclude Include="..\src\parsed_message.h" />
<ClInclude Include="..\src\search.h" />
Expand Down
67 changes: 0 additions & 67 deletions include/opendht/net.h

This file was deleted.

24 changes: 12 additions & 12 deletions include/opendht/network_engine.h
Expand Up @@ -27,7 +27,6 @@
#include "utils.h"
#include "rng.h"
#include "rate_limiter.h"
#include "net.h"

#include <vector>
#include <string>
Expand All @@ -41,6 +40,7 @@ namespace net {

struct Request;
struct Socket;
struct TransId;

#ifndef MSG_CONFIRM
#define MSG_CONFIRM 0
Expand Down Expand Up @@ -169,7 +169,7 @@ class NetworkEngine final
std::function<RequestAnswer(Sp<Node>,
const InfoHash&,
const Blob&,
uint32_t,
Tid,
const Query&)> onListen {};
/**
* Called on announce request.
Expand Down Expand Up @@ -231,7 +231,7 @@ class NetworkEngine final
* @param nodes6 The ipv6 closest nodes.
* @param values The values to send.
*/
void tellListener(Sp<Node> n, uint32_t socket_id, const InfoHash& hash, want_t want, const Blob& ntoken,
void tellListener(Sp<Node> n, Tid socket_id, const InfoHash& hash, want_t want, const Blob& ntoken,
std::vector<Sp<Node>>&& nodes, std::vector<Sp<Node>>&& nodes6,
std::vector<Sp<Value>>&& values, const Query& q);

Expand Down Expand Up @@ -465,18 +465,18 @@ class NetworkEngine final
// basic wrapper for socket sendto function
int send(const char *buf, size_t len, int flags, const SockAddr& addr);

void sendValueParts(TransId tid, const std::vector<Blob>& svals, const SockAddr& addr);
void sendValueParts(const TransId& tid, const std::vector<Blob>& svals, const SockAddr& addr);
std::vector<Blob> packValueHeader(msgpack::sbuffer&, const std::vector<Sp<Value>>&);
void maintainRxBuffer(const TransId& tid);
void maintainRxBuffer(Tid tid);

/*************
* Answers *
*************/
/* answer to a ping request */
void sendPong(const SockAddr& addr, TransId tid);
void sendPong(const SockAddr& addr, Tid tid);
/* answer to findnodes/getvalues request */
void sendNodesValues(const SockAddr& addr,
TransId tid,
Tid tid,
const Blob& nodes,
const Blob& nodes6,
const std::vector<Sp<Value>>& st,
Expand All @@ -490,12 +490,12 @@ class NetworkEngine final
std::vector<Sp<Node>>& nodes,
std::vector<Sp<Node>>& nodes6);
/* answer to a listen request */
void sendListenConfirmation(const SockAddr& addr, TransId tid);
void sendListenConfirmation(const SockAddr& addr, Tid tid);
/* answer to put request */
void sendValueAnnounced(const SockAddr& addr, TransId, Value::Id);
void sendValueAnnounced(const SockAddr& addr, Tid, Value::Id);
/* answer in case of error */
void sendError(const SockAddr& addr,
TransId tid,
Tid tid,
uint16_t code,
const std::string& message,
bool include_id=false);
Expand All @@ -519,8 +519,8 @@ class NetworkEngine final
size_t limiter_maintenance {0};

// requests handling
std::map<TransId, Sp<Request>> requests {};
std::map<TransId, PartialMessage> partial_messages;
std::map<Tid, Sp<Request>> requests {};
std::map<Tid, PartialMessage> partial_messages;

MessageStats in_stats {}, out_stats {};
std::set<SockAddr> blacklist {};
Expand Down
21 changes: 10 additions & 11 deletions include/opendht/node.h
Expand Up @@ -22,7 +22,6 @@
#include "infohash.h" // includes socket structures
#include "utils.h"
#include "sockaddr.h"
#include "net.h"

#include <list>
#include <map>
Expand All @@ -36,8 +35,8 @@ struct Socket;
struct RequestAnswer;
} /* namespace net */

using Tid = uint32_t;
using SocketCb = std::function<void(const Sp<Node>&, net::RequestAnswer&&)>;
using SocketId = uint32_t;
struct Socket {
Socket() {}
Socket(SocketCb&& on_receive) :
Expand Down Expand Up @@ -102,7 +101,7 @@ struct Node {

void requested(const Sp<net::Request>& req);
void received(time_point now, const Sp<net::Request>& req);
Sp<net::Request> getRequest(const net::TransId& tid);
Sp<net::Request> getRequest(Tid tid);
void cancelRequest(const Sp<net::Request>& req);

void setExpired();
Expand All @@ -116,16 +115,16 @@ struct Node {
*
* @return the socket.
*/
SocketId openSocket(SocketCb&& cb);
Tid openSocket(SocketCb&& cb);

Socket* getSocket(SocketId id);
Socket* getSocket(Tid id);

/**
* Closes a socket so that no further data will be red on that socket.
*
* @param socket The socket to close.
*/
void closeSocket(SocketId id);
void closeSocket(Tid id);

/**
* Resets the state of the node so it's not expired anymore.
Expand All @@ -137,9 +136,9 @@ struct Node {
*
* @return the new id.
*/
uint16_t getNewTid() {
Tid getNewTid() {
++transaction_id;
return transaction_id == net::TransId::INVALID ? ++transaction_id : transaction_id;
return transaction_id ? ++transaction_id : transaction_id;
}

std::string toString() const;
Expand All @@ -164,11 +163,11 @@ struct Node {
time_point reply_time {time_point::min()}; /* time of last correct reply received */
unsigned auth_errors {0};
bool expired_ {false};
SocketId transaction_id;
Tid transaction_id;
using TransactionDist = std::uniform_int_distribution<decltype(transaction_id)>;

std::map<net::TransId, Sp<net::Request>> requests_ {};
std::map<SocketId, Socket> sockets_;
std::map<Tid, Sp<net::Request>> requests_ {};
std::map<Tid, Socket> sockets_;
};

}
2 changes: 1 addition & 1 deletion src/Makefile.am
Expand Up @@ -9,6 +9,7 @@ libopendht_la_SOURCES = \
listener.h \
request.h \
search.h \
net.h \
parsed_message.h \
node_cache.cpp \
callbacks.cpp \
Expand Down Expand Up @@ -37,7 +38,6 @@ nobase_include_HEADERS = \
../include/opendht/node_cache.h \
../include/opendht/routing_table.h \
../include/opendht/network_engine.h \
../include/opendht/net.h \
../include/opendht/scheduler.h \
../include/opendht/rate_limiter.h \
../include/opendht/utils.h \
Expand Down
37 changes: 37 additions & 0 deletions src/net.h
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2017 Savoir-faire Linux Inc.
* Author(s) : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

namespace dht {
namespace net {

enum class MessageType {
Error = 0,
Reply,
Ping,
FindNode,
GetValues,
AnnounceValue,
Refresh,
Listen,
ValueData
};

} /* namespace net */
} /* dht */

0 comments on commit 476fbb1

Please sign in to comment.