-
Notifications
You must be signed in to change notification settings - Fork 37.6k
NODE_EXT_SERVICE: Advertise other services also available at this node. #4657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
|
||
#include <vector> | ||
#include "serialize.h" | ||
#include "util.h" | ||
#include "version.h" | ||
#include "net.h" | ||
#include "extservices.h" | ||
#include <boost/foreach.hpp> | ||
#include "json/json_spirit_value.h" | ||
#include "json/json_spirit_reader_template.h" | ||
#include "json/json_spirit_utils.h" | ||
|
||
using namespace std; | ||
using namespace json_spirit; | ||
|
||
static const unsigned int MAX_JSON_SRV_SZ = (640 * 1024); // enough for anybody | ||
|
||
class CExtServices { | ||
private: | ||
vector<CExtService> srvList; | ||
|
||
public: | ||
void getArray(Array& arr); | ||
void getVec(vector<CExtService>& vec); | ||
void add(const CExtService& val) { srvList.push_back(val); } | ||
size_t count() const { return srvList.size(); } | ||
}; | ||
|
||
static CExtServices extServices; | ||
|
||
bool CExtService::parseValue(const Value& val) | ||
{ | ||
if (val.type() != obj_type) | ||
return false; | ||
|
||
const Object& srvObj = val.get_obj(); | ||
|
||
const Value& vName = find_value(srvObj, "name"); | ||
if (vName.type() != str_type) | ||
return false; | ||
|
||
name = vName.get_str(); | ||
|
||
const Value& vPort = find_value(srvObj, "port"); | ||
if (vPort.type() == null_type) { | ||
// do nothing | ||
} else if (vPort.type() == int_type) { | ||
int i = vPort.get_int(); | ||
if (i < 1 || i > 65535) | ||
return false; | ||
|
||
port = i; | ||
} else | ||
return false; | ||
|
||
const Value& vAttrib = find_value(srvObj, "attrib"); | ||
if (vAttrib.type() == null_type) { | ||
// do nothing | ||
} else if (vAttrib.type() == obj_type) { | ||
const Object& attribObj = vAttrib.get_obj(); | ||
BOOST_FOREACH(const Pair& s, attribObj) { | ||
const Value& av = s.value_; | ||
if (av.type() != str_type) | ||
return false; | ||
|
||
CKeyValue kv(s.name_, av.get_str()); | ||
if (kv.key.size() < 1 || kv.key.size() > 100 || | ||
kv.value.size() > 1000) | ||
return false; | ||
|
||
attrib.push_back(kv); | ||
} | ||
} else | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
void CExtServices::getArray(Array& arr) | ||
{ | ||
BOOST_FOREACH(const CExtService& srv, srvList) { | ||
Object obj; | ||
obj.push_back(Pair("name", srv.name)); | ||
if (srv.port > 0) | ||
obj.push_back(Pair("port", srv.port)); | ||
|
||
arr.push_back(obj); | ||
} | ||
} | ||
|
||
void CExtServices::getVec(vector<CExtService>& vec) | ||
{ | ||
BOOST_FOREACH(const CExtService& srv, srvList) { | ||
vec.push_back(srv); | ||
} | ||
} | ||
|
||
static bool ReadJsonFile(const string& filename, size_t maxSz, | ||
Value& valOut, string& strErr) | ||
{ | ||
CAutoFile srvfile(fopen(filename.c_str(), "r"), SER_DISK, CLIENT_VERSION); | ||
if (!srvfile) { | ||
strErr = strprintf("Cannot open %s\n", filename); | ||
return false; | ||
} | ||
|
||
string rawJson; | ||
while (1) { | ||
char buf[4096]; | ||
|
||
size_t bread = fread(buf, 1, sizeof(buf), srvfile); | ||
rawJson.append(buf, bread); | ||
|
||
if (maxSz && (rawJson.size() > maxSz)) { | ||
strErr = strprintf("Error reading %s, too large\n", filename); | ||
return false; | ||
} | ||
|
||
if (bread < sizeof(buf)) { | ||
if (ferror(srvfile)) { | ||
strErr = strprintf("Error reading %s\n", filename); | ||
return false; | ||
} | ||
|
||
// eof | ||
break; | ||
} | ||
} | ||
|
||
if (!read_string(rawJson, valOut) || | ||
(valOut.type() != obj_type)) { | ||
strErr = strprintf("Error parsing JSON in %s\n", filename); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
static bool ReadExtServiceFile(const string& filename) | ||
{ | ||
Value valSrv; | ||
string strErr; | ||
if (!ReadJsonFile(filename, MAX_JSON_SRV_SZ, valSrv, strErr)) { | ||
LogPrintf("%s: %s", filename, strErr); | ||
return false; | ||
} | ||
|
||
if (valSrv.type() != array_type) { | ||
LogPrintf("Unable to parse array in %s\n", filename); | ||
return false; | ||
} | ||
|
||
const Array& serviceList = valSrv.get_array(); | ||
for (unsigned int i = 0; i < serviceList.size(); i++) { | ||
if (serviceList[i].type() != obj_type) { | ||
LogPrintf("Service list member not object\n"); | ||
return false; | ||
} | ||
|
||
CExtService srv; | ||
if (!srv.parseValue(serviceList[i])) | ||
return false; | ||
|
||
extServices.add(srv); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool ReadExtServices() | ||
{ | ||
if (!mapArgs.count("-extservices")) | ||
return true; | ||
if (!ReadExtServiceFile(mapArgs["-extservice"])) | ||
return false; | ||
|
||
if (extServices.count() > 0) | ||
nLocalServices |= NODE_EXT_SERVICES; | ||
|
||
return true; | ||
} | ||
|
||
Array ListExtServices() | ||
{ | ||
Array ret; | ||
extServices.getArray(ret); | ||
return ret; | ||
} | ||
|
||
void GetExtServicesVec(vector<CExtService>& vec) | ||
{ | ||
extServices.getVec(vec); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#ifndef __BITCOIN_EXTSERVICES_H__ | ||
#define __BITCOIN_EXTSERVICES_H__ | ||
|
||
#include <stdint.h> | ||
#include <string> | ||
#include <vector> | ||
#include "json/json_spirit_value.h" | ||
#include "serialize.h" | ||
|
||
class CKeyValue { | ||
public: | ||
std::string key; | ||
std::string value; | ||
|
||
CKeyValue() | ||
{ | ||
SetNull(); | ||
} | ||
CKeyValue(std::string key_, std::string value_) | ||
{ | ||
key = key_; | ||
value = value_; | ||
} | ||
|
||
IMPLEMENT_SERIALIZE | ||
( | ||
READWRITE(key); | ||
READWRITE(value); | ||
) | ||
|
||
void SetNull() | ||
{ | ||
key.clear(); | ||
value.clear(); | ||
} | ||
}; | ||
|
||
class CExtService { | ||
public: | ||
std::string name; | ||
int32_t port; | ||
std::vector<CKeyValue> attrib; | ||
|
||
CExtService(const std::string& name_ = "", int32_t port_ = -1) { | ||
name = name_; | ||
port = port_; | ||
} | ||
bool parseValue(const json_spirit::Value& val); | ||
|
||
IMPLEMENT_SERIALIZE | ||
( | ||
READWRITE(name); | ||
READWRITE(port); | ||
READWRITE(attrib); | ||
) | ||
}; | ||
|
||
extern bool ReadExtServices(); | ||
extern json_spirit::Array ListExtServices(); | ||
extern void GetExtServicesVec(std::vector<CExtService>& vec); | ||
|
||
#endif // __BITCOIN_EXTSERVICES_H__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you at least try to respect the coding style rules we have (include ordering etc.)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this continues to produce hectoring, then the most appropriate action is to remove that rule from doc/coding.md. At some point you need to ask yourself whether this is satisfying your personal OCD versus making the bitcoin project and its developers more (or less!) productive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Diapolo is quite right to point this out if it goes against coding standards and @jgarzik is quite right to point out that the coding standards may need revising.
May I take this moment to suggest reading the "four agreements"? (http://crossfitcetro.com/wp-content/uploads/2012/08/the_four_agreements.jpg) - I'm seeing all too often people taking things a bit too personally on here, however, it could almost go without saying that I for one am grateful to @jgarzik for so many of the code changes he's contributed, and I hope other user's sterling efforts (OCD fuelled or not) to enforce style rules don't deter future contributions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense to have a well-defined coarse include order. I.e. first your own include, then dependencies within the project, then dependencies outside the project. This makes sure that the compiler checks that your own include file (in this case extservices.h) includes everything it needs on its own.
As for alphabetical sorting, it could be argued that that is a step too far, although if you want to define some fixed ordering within the groups alphabetical makes most sense. For example it makes it easier to see duplicates.