fixed merge with upstream/master

This commit is contained in:
csoler 2022-01-02 16:17:53 +01:00
commit 7367fb3e46
284 changed files with 12153 additions and 10835 deletions

3
.gitmodules vendored
View File

@ -17,3 +17,6 @@
[submodule "supportlibs/libsam3"]
path = supportlibs/libsam3
url = https://github.com/i2p/libsam3.git
[submodule "supportlibs/jni.hpp"]
path = supportlibs/jni.hpp
url = https://github.com/RetroShare/jni.hpp.git

View File

@ -2,7 +2,7 @@
## image name must match gitlab repository name, you can play just with the tag
## the part after :
# export CI_IMAGE_NAME="registry.gitlab.com/retroshare/retroshare:android_arm_base"
# docker build --squash -t "${CI_REGISTRY_IMAGE}" \
# docker build --squash --tag "${CI_IMAGE_NAME}" \
# --build-arg QT_INSTALLER_JWT_TOKEN="your qt JWT token goes here" .
#
# To build Android ARMv8 (64 bit) package pass also
@ -25,14 +25,16 @@
FROM ubuntu:20.04
ENV DEBIAN_FRONTEND=noninteractive
ENV APT_UNAT="--assume-yes --quiet"
RUN apt-get update && apt-get clean
RUN apt-get install -y -qq \
RUN apt-get update $APT_UNAT && apt-get upgrade --show-upgraded $APT_UNAT && \
apt-get clean $APT_UNAT
RUN apt-get install --no-install-recommends $APT_UNAT \
bash build-essential bzip2 cmake curl chrpath doxygen \
git p7zip python qt5-default qttools5-dev tclsh unzip wget zip
# Dependencies to create Android pkg
RUN apt-get install -y -qq \
RUN apt-get install --no-install-recommends $APT_UNAT \
openjdk-8-jre openjdk-8-jdk openjdk-8-jdk-headless gradle
ARG FRESHCLONE=0
@ -74,11 +76,11 @@ ARG QT_INSTALLER_JWT_TOKEN
RUN $PREPARE_TOOLCHAIN install_qt_android
# Avoid Qt account details leak into the image
RUN rm -f /root/.local/share/Qt/qtaccount.ini
# Shrink image by removing unneded Qt components
# Shrink image by removing unneeded Qt components
RUN rm -r \
$NATIVE_LIBS_TOOLCHAIN_PATH/Qt/Docs/ \
$NATIVE_LIBS_TOOLCHAIN_PATH/Qt/Examples/ \
$NATIVE_LIBS_TOOLCHAIN_PATH/Qt/Tools/
$NATIVE_LIBS_TOOLCHAIN_PATH/Qt/Tools/
RUN mkdir /jsonapi-generator-build
WORKDIR /jsonapi-generator-build/

View File

@ -112,6 +112,9 @@ define_default_value MVPTREE_SOURCE_VERSION origin/master
define_default_value REPORT_DIR "$(pwd)/$(basename ${NATIVE_LIBS_TOOLCHAIN_PATH})_build_report/"
define_default_value RS_SRC_DIR "$(realpath $(dirname $BASH_SOURCE)/../../)"
cArch=""
eABI=""
cmakeABI=""
@ -829,6 +832,15 @@ build_phash()
popd
}
task_register fetch_jni_hpp
fetch_jni_hpp()
{
local rDir="supportlibs/jni.hpp/"
[ "$(ls "${RS_SRC_DIR}/${rDir}" | wc -l)" -gt "4" ] ||
git -C ${RS_SRC_DIR} submodule update --init ${rDir}
}
task_register build_mvptree
build_mvptree()
{
@ -862,6 +874,7 @@ build_default_toolchain()
task_run build_xapian || return $?
task_run build_miniupnpc || return $?
task_run build_phash || return $?
task_run fetch_jni_hpp || return $?
task_run deduplicate_includes || return $?
task_run get_native_libs_toolchain_path || return $?
}

View File

@ -1,7 +1,9 @@
ARG ANDROID_NDK_ARCH=arm
FROM registry.gitlab.com/retroshare/retroshare:android_${ANDROID_NDK_ARCH}_base
RUN apt-get update -y && apt-get upgrade -y
ENV APT_UNAT="--assume-yes --quiet"
RUN apt-get update $APT_UNAT && apt-get upgrade $APT_UNAT --show-upgraded
ARG REPO_URL=https://gitlab.com/RetroShare/RetroShare.git
ARG REPO_BRANCH=master

@ -1 +1 @@
Subproject commit b260e58346b1eec782bdf88a7e8f3c9d36fd3ecb
Subproject commit df16cb915465d058c75277678799ce4dadeae287

View File

@ -1,447 +1,10 @@
// SPDX-FileCopyrightText: (C) 2004-2019 Retroshare Team <contact@retroshare.cc>
// SPDX-FileCopyrightText: (C) 2021 Retroshare Team <contact@retroshare.cc>
// SPDX-License-Identifier: CC0-1.0
RetroShare JSON API
===================
= Moved
:Cxx: C&#43;&#43;
JSON API generator is now part of `libretroshare` look under `src/jsonapi/`
directory in `libretroshare` repository.
== How to use RetroShare JSON API
Look for methods marked with +@jsonapi+ doxygen custom command into
+libretroshare/src/retroshare+. The method path is composed by service instance
pointer name like +rsGxsChannels+ for +RsGxsChannels+, and the method name like
+createGroup+ and pass the input paramethers as a JSON object.
.Service instance pointer in rsgxschannels.h
[source,cpp]
--------------------------------------------------------------------------------
/**
* Pointer to global instance of RsGxsChannels service implementation
* @jsonapi{development}
*/
extern RsGxsChannels* rsGxsChannels;
--------------------------------------------------------------------------------
.Method declaration in rsgxschannels.h
[source,cpp]
--------------------------------------------------------------------------------
/**
* @brief Request channel creation.
* The action is performed asyncronously, so it could fail in a subsequent
* phase even after returning true.
* @jsonapi{development}
* @param[out] token Storage for RsTokenService token to track request
* status.
* @param[in] group Channel data (name, description...)
* @return false on error, true otherwise
*/
virtual bool createGroup(uint32_t& token, RsGxsChannelGroup& group) = 0;
--------------------------------------------------------------------------------
.paramethers.json
[source,json]
--------------------------------------------------------------------------------
{
"group":{
"mMeta":{
"mGroupName":"JSON test group",
"mGroupFlags":4,
"mSignFlags":520
},
"mDescription":"JSON test group description"
},
"caller_data":"Here can go any kind of JSON data (even objects) that the caller want to get back together with the response"
}
--------------------------------------------------------------------------------
.Calling the JSON API with curl on the terminal
[source,bash]
--------------------------------------------------------------------------------
curl -u $API_USER --data @paramethers.json http://127.0.0.1:9092/rsGxsChannels/createGroup
--------------------------------------------------------------------------------
.JSON API call result
[source,json]
--------------------------------------------------------------------------------
{
"caller_data": "Here can go any kind of JSON data (even objects) that the caller want to get back together with the response",
"retval": true,
"token": 3
}
--------------------------------------------------------------------------------
Even if it is less efficient because of URL encoding HTTP +GET+ method is
supported too, so in cases where the client cannot use +POST+ she can still use
+GET+ taking care of encoding the JSON data. With +curl+ this can be done at
least in two different ways.
.Calling the JSON API with GET method with curl on the terminal
[source,bash]
--------------------------------------------------------------------------------
curl -u $API_USER --get --data-urlencode jsonData@paramethers.json \
http://127.0.0.1:9092/rsGxsChannels/createGroup
--------------------------------------------------------------------------------
Letting +curl+ do the encoding is much more elegant but it is semantically
equivalent to the following.
.Calling the JSON API with GET method and pre-encoded data with curl on the terminal
--------------------------------------------------------------------------------
curl -u $API_USER http://127.0.0.1:9092/rsGxsChannels/createGroup?jsonData=%7B%0A%20%20%20%20%22group%22%3A%7B%0A%20%20%20%20%20%20%20%20%22mMeta%22%3A%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mGroupName%22%3A%22JSON%20test%20group%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mGroupFlags%22%3A4%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mSignFlags%22%3A520%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%22mDescription%22%3A%22JSON%20test%20group%20description%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22caller_data%22%3A%22Here%20can%20go%20any%20kind%20of%20JSON%20data%20%28even%20objects%29%20that%20the%20caller%20want%20to%20get%20back%20together%20with%20the%20response%22%0A%7D
--------------------------------------------------------------------------------
Note that using +GET+ method +?jsonData=+ and then the JSON data URL encoded are
added after the path in the HTTP request.
== JSON API authentication
Most of JSON API methods require authentication as they give access to
RetroShare user data, and we don't want any application running on the system
eventually by other users be able to access private data indiscriminately.
JSON API support HTTP Basic as authentication scheme, this is enough as JSON API
server is intented for usage on the same system (127.0.0.1) not over an
untrusted network.
If you need to use JSON API over an untrusted network consider using a reverse
proxy with HTTPS such as NGINX in front of JSON API server.
If RetroShare login has been effectuated through the JSON API you can use your
location SSLID as username and your PGP password as credential for the JSON API,
but we suggests you use specific meaningful and human readable credentials for
each JSON API client so the human user can have better control over which client
can access the JSON API.
.NewToken.json
[source,json]
--------------------------------------------------------------------------------
{
"token": "myNewUser:myNewPassword"
}
--------------------------------------------------------------------------------
.An authenticated client can authorize new tokens like this
--------------------------------------------------------------------------------
curl -u $API_USER --data @NewToken.json http://127.0.0.1:9092/jsonApiServer/authorizeToken
--------------------------------------------------------------------------------
.An unauthenticated JSON API client can request access with
--------------------------------------------------------------------------------
curl --data @NewToken.json http://127.0.0.1:9092/jsonApiServer/requestNewTokenAutorization
--------------------------------------------------------------------------------
When an unauthenticated client request his token to be authorized, JSON API
server will try to ask confirmation to the human user if possible through
+mNewAccessRequestCallback+, if it is not possible or the user didn't authorized
the token +false+ is returned.
== Offer new RetroShare services through JSON API
To offer a retroshare service through the JSON API, first of all one need find
the global pointer to the service instance and document it in doxygen syntax,
plus marking with the custom doxygen command +@jsonapi{RS_VERSION}+ where
+RS_VERSION+ is the retroshare version in which this service became available
with the current semantic (major changes to the service semantic, changes the
meaning of the service itself, so the version should be updated in the
documentation in that case).
.Service instance pointer in rsgxschannels.h
[source,cpp]
--------------------------------------------------------------------------------
/**
* Pointer to global instance of RsGxsChannels service implementation
* @jsonapi{development}
*/
extern RsGxsChannels* rsGxsChannels;
--------------------------------------------------------------------------------
Once the service instance itself is known to the JSON API you need to document
in doxygen syntax and mark with the custom doxygen command
+@jsonapi{RS_VERSION}+ the methods of the service that you want to make
available through JSON API.
.Offering RsGxsChannels::getChannelDownloadDirectory in rsgxschannels.h
[source,cpp]
--------------------------------------------------------------------------------
/**
* Get download directory for the given channel
* @jsonapi{development}
* @param[in] channelId id of the channel
* @param[out] directory reference to string where to store the path
* @return false on error, true otherwise
*/
virtual bool getChannelDownloadDirectory( const RsGxsGroupId& channelId,
std::string& directory ) = 0;
--------------------------------------------------------------------------------
For each paramether you must specify if it is used as input +@param[in]+ as
output +@param[out]+ or both +@param[inout]+. Paramethers and return value
types must be of a type supported by +RsTypeSerializer+ which already support
most basic types (+bool+, +std::string+...), +RsSerializable+ and containers of
them like +std::vector<std::string>+. Paramethers passed by value and by
reference of those types are both supported, while passing by pointer is not
supported. If your paramether or return +class+/+struct+ type is not supported
yet by +RsTypeSerializer+ most convenient approach is to make it derive from
+RsSerializable+ and implement +serial_process+ method like I did with
+RsGxsChannelGroup+.
.Deriving RsGxsChannelGroup from RsSerializable in rsgxschannels.h
[source,cpp]
--------------------------------------------------------------------------------
struct RsGxsChannelGroup : RsSerializable
{
RsGroupMetaData mMeta;
std::string mDescription;
RsGxsImage mImage;
bool mAutoDownload;
/// @see RsSerializable
virtual void serial_process( RsGenericSerializer::SerializeJob j,
RsGenericSerializer::SerializeContext& ctx )
{
RS_SERIAL_PROCESS(mMeta);
RS_SERIAL_PROCESS(mDescription);
RS_SERIAL_PROCESS(mImage);
RS_SERIAL_PROCESS(mAutoDownload);
}
};
--------------------------------------------------------------------------------
You can do the same recursively for any member of your +struct+ that is not yet
supported by +RsTypeSerializer+.
Some Retroshare {Cxx} API functions are asyncronous, historically RetroShare
didn't follow a policy on how to expose asyncronous API so differents services
and some times even differents method of the same service follow differents
asyncronous patterns, thus making automatic generation of JSON API wrappers for
those methods impractical. Instead of dealing with all those differents patterns
I have chosed to support only one new pattern taking advantage of modern {Cxx}11
and restbed features. On the {Cxx}11 side lambdas and +std::function+s are used,
on the restbed side Server Side Events are used to send asyncronous results.
Lets see an example so it will be much esier to understand.
.RsGxsChannels::turtleSearchRequest asyncronous API
[source,cpp]
--------------------------------------------------------------------------------
/**
* @brief Request remote channels search
* @jsonapi{development}
* @param[in] matchString string to look for in the search
* @param multiCallback function that will be called each time a search
* result is received
* @param[in] maxWait maximum wait time in seconds for search results
* @return false on error, true otherwise
*/
virtual bool turtleSearchRequest(
const std::string& matchString,
const std::function<void (const RsGxsGroupSummary& result)>& multiCallback,
std::time_t maxWait = 300 ) = 0;
--------------------------------------------------------------------------------
+RsGxsChannels::turtleSearchRequest(...)+ is an asyncronous method because it
send a channel search request on turtle network and then everytime a result is
received from the network +multiCallback+ is called and the result is passed as
parameter. To be supported by the automatic JSON API wrappers generator an
asyncronous method need a parameter of type +std::function<void (...)>+ called
+callback+ if the callback will be called only once or +multiCallback+ if the
callback is expected to be called more then once like in this case.
A second mandatory parameter is +maxWait+ of type +std::time_t+ it indicates the
maximum amount of time in seconds for which the caller is willing to wait for
results, in case the timeout is reached the callback will not be called anymore.
[IMPORTANT]
================================================================================
+callback+ and +multiCallback+ parameters documentation must *not* specify
+[in]+, +[out]+, +[inout]+, in Doxygen documentation as this would fool the
automatic wrapper generator, and ultimately break the compilation.
================================================================================
.RsFiles::turtleSearchRequest asyncronous JSON API usage example
[source,bash]
--------------------------------------------------------------------------------
$ cat turtle_search.json
{
"matchString":"linux"
}
$ curl --data @turtle_search.json http://127.0.0.1:9092/rsFiles/turtleSearchRequest
data: {"retval":true}
data: {"results":[{"size":157631,"hash":"69709b4d01025584a8def5cd78ebbd1a3cf3fd05","name":"kill_bill_linux_1024x768.jpg"},{"size":192560,"hash":"000000000000000000009a93e5be8486c496f46c","name":"coffee_box_linux2.jpg"},{"size":455087,"hash":"9a93e5be8486c496f46c00000000000000000000","name":"Linux.png"},{"size":182004,"hash":"e8845280912ebf3779e400000000000000000000","name":"Linux_2_6.png"}]}
data: {"results":[{"size":668,"hash":"e8845280912ebf3779e400000000000000000000","name":"linux.png"},{"size":70,"hash":"e8845280912ebf3779e400000000000000000000","name":"kali-linux-2016.2-amd64.txt.sha1sum"},{"size":3076767744,"hash":"e8845280912ebf3779e400000000000000000000","name":"kali-linux-2016.2-amd64.iso"},{"size":2780872,"hash":"e8845280912ebf3779e400000000000000000000","name":"openwrt-ar71xx-generic-vmlinux.bin"},{"size":917504,"hash":"e8845280912ebf3779e400000000000000000000","name":"openwrt-ar71xx-generic-vmlinux.lzma"},{"size":2278404096,"hash":"e8845280912ebf3779e400000000000000000000","name":"gentoo-linux-livedvd-amd64-multilib-20160704.iso"},{"size":151770333,"hash":"e8845280912ebf3779e400000000000000000000","name":"flashtool-0.9.23.0-linux.tar.7z"},{"size":2847372,"hash":"e8845280912ebf3779e400000000000000000000","name":"openwrt-ar71xx-generic-vmlinux.elf"},{"size":1310720,"hash":"e8845280912ebf3779e400000000000000000000","name":"openwrt-ar71xx-generic-vmlinux.gz"},{"size":987809,"hash":"e8845280912ebf3779e400000000000000000000","name":"openwrt-ar71xx-generic-vmlinux-lzma.elf"}]}
--------------------------------------------------------------------------------
By default JSON API methods requires client authentication and their wrappers
are automatically generated by +json-api-generator+.
In some cases methods need do be accessible without authentication such as
+rsLoginHelper/getLocations+ so in the doxygen documentaion they have the custom
command +@jsonapi{RS_VERSION,unauthenticated}+.
Other methods such as +/rsControl/rsGlobalShutDown+ need special care so they
are marked with the custom doxygen command +@jsonapi{RS_VERSION,manualwrapper}+
and their wrappers are not automatically generated but written manually into
+JsonApiServer::JsonApiServer(...)+.
== Quirks
=== 64 bits integers handling
While JSON doesn't have problems representing 64 bits integers JavaScript, Dart
and other languages are not capable to handle those numbers natively.
To overcome this limitation JSON API output 64 bit integers as an object with
two keys, one as proper integer and one as string representation.
.JSON API 64 bit integer output example
[source,json]
--------------------------------------------------------------------------------
"lobby_id": { "xint64": 6215642878098695544, "xstr64": "6215642878098695544" }
--------------------------------------------------------------------------------
So from languages that have proper 64bit integers support like Python or C++ one
better read from `xint64` which is represented as a JSON integer, from languages
where there is no proper 64bit integers support like JavaScript one can read from
`xstr64` which is represented as JSON string (note that the first is not wrapped
in "" while the latter is).
When one input a 64bit integer into the JSON API it first try to parse it as if
it was sent the old way for retrocompatibility.
.JSON API 64 bit integer deprecated format input example
[source,json]
--------------------------------------------------------------------------------
"lobby_id":6215642878098695544
--------------------------------------------------------------------------------
This way is *DEPRECATED* and may disappear in the future, it is TEMPORALLY kept
only for retrocompatibiliy with old clients.
If retrocompatible parsing attempt fail then it try to parse with the new way
with proper JSON integer format.
.JSON API 64 bit integer new proper integer format input example
[source,json]
--------------------------------------------------------------------------------
lobby_id": { "xint64": 6215642878098695544 }
--------------------------------------------------------------------------------
If this fails then it try to parse with the new way with JSON string format.
.JSON API 64 bit integer new string format input example
[source,json]
--------------------------------------------------------------------------------
"lobby_id": { "xstr64": "6215642878098695544" }
--------------------------------------------------------------------------------
[WARNING]
================================================================================
Clients written in languages without proper 64bit integers support must
use *ONLY* the string format otherwise they will send approximated values and
get unexpected results from the JSON API, because parsing will success but the
value will not be exactly the one you believe you sent.
================================================================================
== A bit of history
=== First writings about this
The previous attempt of exposing a RetroShare JSON API is called +libresapi+ and
unfortunatley it requires a bunch of boilerplate code when we want to expose
something present in the {Cxx} API in the JSON API.
As an example here you can see the libresapi that exposes part of the retroshare
chat {Cxx} API and lot of boilerplate code just to convert {Cxx} objects to JSON
https://github.com/RetroShare/RetroShare/blob/v0.6.4/libresapi/src/api/ChatHandler.cpp#L44
To avoid the {Cxx} to JSON and back conversion boilerplate code I have worked out
an extension to our {Cxx} serialization code so it is capable to serialize and
deserialize to JSON you can see it in this pull request
https://github.com/RetroShare/RetroShare/pull/1155
So first step toward having a good API is to take advantage of the fact that RS
is now capable of converting C++ objects from and to JSON.
The current API is accessible via HTTP and unix socket, there is no
authentication in both of them, so anyone having access to the HTTP server or to
the unix socket can access the API without extra restrictions.
Expecially for the HTTP API this is a big risk because also if the http server
listen on 127.0.0.1 every application on the machine (even rogue javascript
running on your web browser) can access that and for example on android it is
not safe at all (because of that I implemented the unix socket access so at
least in android API was reasonably safe) because of this.
A second step to improve the API would be to implement some kind of API
authentication mechanism (it would be nice that the mechanism is handled at API
level and not at transport level so we can use it for any API trasport not just
HTTP for example)
The HTTP server used by libresapi is libmicrohttpd server that is very minimal,
it doesn't provide HTTPS nor modern HTTP goodies, like server notifications,
websockets etc. because the lack of support we have a token polling mechanism in
libresapi to avoid polling for every thing but it is still ugly, so if we can
completely get rid of polling in the API that would be really nice.
I have done a crawl to look for a replacement and briefly looked at
- https://www.gnu.org/software/libmicrohttpd/
- http://wolkykim.github.io/libasyncd/
- https://github.com/corvusoft/restbed
- https://code.facebook.com/posts/1503205539947302/introducing-proxygen-facebook-s-c-http-framework/
- https://github.com/cmouse/yahttp
taking in account a few metrics like modern HTTP goodies support, license,
platform support, external dependencies and documentation it seemed to me that
restbed is the more appropriate.
Another source of boilerplate code into libresapi is the mapping between JSON
API requests and C++ API methods as an example you can look at this
https://github.com/RetroShare/RetroShare/blob/v0.6.4/libresapi/src/api/ChatHandler.cpp#L158
and this
https://github.com/RetroShare/RetroShare/blob/v0.6.4/libresapi/src/api/ApiServer.cpp#L253
The abstract logic of this thing is, when libreasapi get a request like
+/chat/initiate_distant_chat+ then call
+ChatHandler::handleInitiateDistantChatConnexion+ which in turn is just a
wrapper of +RsMsgs::initiateDistantChatConnexion+ all this process is basically
implemented as boilerplate code and would be unnecessary in a smarter design of
the API because almost all the information needed is already present in the
C++ API +libretroshare/src/retroshare+.
So a third step to improve the JSON API would be to remove this source of
boilerplate code by automatizing the mapping between C++ and JSON API call.
This may result a little tricky as language parsing or other adevanced things
may be required.
Hope this dive is useful for you +
Cheers +
G10h4ck
=== Second writings about this
I have been investigating a bit more about:
[verse, G10h4ck]
________________________________________________________________________________
So a third step to improve the JSON API would be to remove this source of
boilerplate code by automatizing the mapping between C++ and JSON API call
________________________________________________________________________________
After spending some hours investigating this topic the most reasonable approach
seems to:
1. Properly document headers in +libretroshare/src/retroshare/+ in doxygen syntax
specifying wihich params are input and/or output (doxygen sysntax for this is
+@param[in/out/inout]+) this will be the API documentation too.
2. At compile time use doxygen to generate XML description of the headers and use
the XML to generate the JSON api server stub.
http://www.stack.nl/~dimitri/doxygen/manual/customize.html#xmlgenerator
3. Enjoy
This directory and all it's content is kept as is only for retro-compatibility
with the old, deprecated `qmake` build system.

View File

@ -1,80 +0,0 @@
/*******************************************************************************
* RetroShare JSON API *
* *
* Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License version 3 as *
* published by the Free Software Foundation. *
* *
* 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 Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
registerHandler( "$%apiPath%$",
[this](const std::shared_ptr<rb::Session> session)
{
const std::multimap<std::string, std::string> headers
{
{ "Connection", "keep-alive" },
{ "Content-Type", "text/event-stream" }
};
session->yield(rb::OK, headers);
size_t reqSize = session->get_request()->get_header("Content-Length", 0);
session->fetch( reqSize, [this](
const std::shared_ptr<rb::Session> session,
const rb::Bytes& body )
{
INITIALIZE_API_CALL_JSON_CONTEXT;
if( !checkRsServicePtrReady(
$%instanceName%$, "$%instanceName%$", cAns, session ) )
return;
$%paramsDeclaration%$
$%inputParamsDeserialization%$
const std::weak_ptr<rb::Service> weakService(mService);
const std::weak_ptr<rb::Session> weakSession(session);
$%callbackName%$ = [weakService, weakSession]($%callbackParams%$)
{
auto session = weakSession.lock();
if(!session || session->is_closed()) return;
auto lService = weakService.lock();
if(!lService || lService->is_down()) return;
$%callbackParamsSerialization%$
std::stringstream sStream;
sStream << "data: " << compactJSON << ctx.mJson << "\n\n";
const std::string message = sStream.str();
lService->schedule( [weakSession, message]()
{
auto session = weakSession.lock();
if(!session || session->is_closed()) return;
session->yield(message);
$%sessionEarlyClose%$
} );
};
$%functionCall%$
$%outputParamsSerialization%$
// return them to the API caller
std::stringstream message;
message << "data: " << compactJSON << cAns.mJson << "\n\n";
session->yield(message.str());
$%sessionDelayedClose%$
} );
}, $%requiresAuth%$ );

View File

@ -0,0 +1 @@
../../libretroshare/src/jsonapi/async-method-wrapper-template.cpp.tmpl

View File

@ -1,230 +0,0 @@
DOXYFILE_ENCODING = UTF-8
PROJECT_NAME = "libretroshare"
ALIASES += jsonapi{1}="\xmlonly<jsonapi minversion=\"\1\"/>\endxmlonly"
ALIASES += jsonapi{2}="\xmlonly<jsonapi minversion=\"\1\" access=\"\2\"/>\endxmlonly"
# If the INHERIT_DOCS tag is set to YES then an undocumented member inherits the
# documentation from any documented member that it re-implements.
# The default value is: YES.
INHERIT_DOCS = YES
# If the MARKDOWN_SUPPORT tag is enabled then doxygen pre-processes all comments
# according to the Markdown format, which allows for more readable
# documentation. See http://daringfireball.net/projects/markdown/ for details.
# The output of markdown processing is further processed by doxygen, so you can
# mix doxygen, HTML, and XML commands with Markdown formatting. Disable only in
# case of backward compatibilities issues.
# The default value is: YES.
MARKDOWN_SUPPORT = YES
# When enabled doxygen tries to link words that correspond to documented
# classes, or namespaces to their corresponding documentation. Such a link can
# be prevented in individual cases by putting a % sign in front of the word or
# globally by setting AUTOLINK_SUPPORT to NO.
# The default value is: YES.
AUTOLINK_SUPPORT = YES
# If the EXTRACT_ALL tag is set to YES, doxygen will assume all entities in
# documentation are documented, even if no documentation was available. Private
# class members and static file members will be hidden unless the
# EXTRACT_PRIVATE respectively EXTRACT_STATIC tags are set to YES.
# Note: This will also disable the warnings about undocumented members that are
# normally produced when WARNINGS is set to YES.
# The default value is: NO.
EXTRACT_ALL = YES
# If the HIDE_UNDOC_MEMBERS tag is set to YES, doxygen will hide all
# undocumented members inside documented classes or files. If set to NO these
# members will be included in the various overviews, but no documentation
# section is generated. This option has no effect if EXTRACT_ALL is enabled.
# The default value is: NO.
HIDE_UNDOC_MEMBERS = NO
# If the HIDE_UNDOC_CLASSES tag is set to YES, doxygen will hide all
# undocumented classes that are normally visible in the class hierarchy. If set
# to NO, these classes will be included in the various overviews. This option
# has no effect if EXTRACT_ALL is enabled.
# The default value is: NO.
HIDE_UNDOC_CLASSES = NO
#---------------------------------------------------------------------------
# Configuration options related to the input files
#---------------------------------------------------------------------------
# The INPUT tag is used to specify the files and/or directories that contain
# documented source files. You may enter file names like myfile.cpp or
# directories like /usr/src/myproject. Separate the files or directories with
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
# Note: If this tag is empty the current directory is searched.
#INPUT =
# This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
# libiconv (or the iconv built into libc) for the transcoding. See the libiconv
# documentation (see: https://www.gnu.org/software/libiconv/) for the list of
# possible encodings.
# The default value is: UTF-8.
INPUT_ENCODING = UTF-8
# If the value of the INPUT tag contains directories, you can use the
# FILE_PATTERNS tag to specify one or more wildcard patterns (like *.cpp and
# *.h) to filter out the source-files in the directories.
#
# Note that for custom extensions or not directly supported extensions you also
# need to set EXTENSION_MAPPING for the extension otherwise the files are not
# read by doxygen.
#
# If left blank the following patterns are tested:*.c, *.cc, *.cxx, *.cpp,
# *.c++, *.java, *.ii, *.ixx, *.ipp, *.i++, *.inl, *.idl, *.ddl, *.odl, *.h,
# *.hh, *.hxx, *.hpp, *.h++, *.cs, *.d, *.php, *.php4, *.php5, *.phtml, *.inc,
# *.m, *.markdown, *.md, *.mm, *.dox, *.py, *.pyw, *.f90, *.f95, *.f03, *.f08,
# *.f, *.for, *.tcl, *.vhd, *.vhdl, *.ucf and *.qsf.
FILE_PATTERNS = *.c \
*.cc \
*.cxx \
*.cpp \
*.c++ \
*.java \
*.ii \
*.ixx \
*.ipp \
*.i++ \
*.inl \
*.idl \
*.ddl \
*.odl \
*.h \
*.hh \
*.hxx \
*.hpp \
*.h++ \
*.cs \
*.d \
*.php \
*.php4 \
*.php5 \
*.phtml \
*.inc \
*.m \
*.markdown \
*.md \
*.mm \
*.dox \
*.py \
*.pyw \
*.f90 \
*.f95 \
*.f03 \
*.f08 \
*.f \
*.for \
*.tcl \
*.vhd \
*.vhdl \
*.ucf \
*.qsf
# The RECURSIVE tag can be used to specify whether or not subdirectories should
# be searched for input files as well.
# The default value is: NO.
RECURSIVE = YES
# The EXCLUDE tag can be used to specify files and/or directories that should be
# excluded from the INPUT source files. This way you can easily exclude a
# subdirectory from a directory tree whose root is specified with the INPUT tag.
#
# Note that relative paths are relative to the directory from which doxygen is
# run.
EXCLUDE =
# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
# directories that are symbolic links (a Unix file system feature) are excluded
# from the input.
# The default value is: NO.
EXCLUDE_SYMLINKS = NO
# If the value of the INPUT tag contains directories, you can use the
# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude
# certain files from those directories.
#
# Note that the wildcards are matched against the file with absolute path, so to
# exclude all test directories for example use the pattern */test/*
EXCLUDE_PATTERNS =
# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names
# (namespaces, classes, functions, etc.) that should be excluded from the
# output. The symbol name can be a fully qualified name, a word, or if the
# wildcard * is used, a substring. Examples: ANamespace, AClass,
# AClass::ANamespace, ANamespace::*Test
#
# Note that the wildcards are matched against the file with absolute path, so to
# exclude all test directories use the pattern */test/*
EXCLUDE_SYMBOLS =
#---------------------------------------------------------------------------
# Configuration options related to the HTML output
#---------------------------------------------------------------------------
# If the GENERATE_HTML tag is set to YES, doxygen will generate HTML output
# The default value is: YES.
GENERATE_HTML = NO
# If the GENERATE_LATEX tag is set to YES, doxygen will generate LaTeX output.
# The default value is: YES.
GENERATE_LATEX = NO
# If the GENERATE_XML tag is set to YES, doxygen will generate an XML file that
# captures the structure of the code including all documentation.
# The default value is: NO.
GENERATE_XML = YES
#---------------------------------------------------------------------------
# Configuration options related to the preprocessor
#---------------------------------------------------------------------------
# If the ENABLE_PREPROCESSING tag is set to YES, doxygen will evaluate all
# C-preprocessor directives found in the sources and include files.
# The default value is: YES.
ENABLE_PREPROCESSING = YES
# If the MACRO_EXPANSION tag is set to YES, doxygen will expand all macro names
# in the source code. If set to NO, only conditional compilation will be
# performed. Macro expansion can be done in a controlled way by setting
# EXPAND_ONLY_PREDEF to YES.
# The default value is: NO.
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
MACRO_EXPANSION = NO
# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES then
# the macro expansion is limited to the macros specified with the PREDEFINED and
# EXPAND_AS_DEFINED tags.
# The default value is: NO.
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
EXPAND_ONLY_PREDEF = NO

View File

@ -0,0 +1 @@
../../libretroshare/src/jsonapi/jsonapi-generator-doxygen.conf

View File

@ -1,50 +0,0 @@
/*******************************************************************************
* RetroShare JSON API *
* *
* Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero 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 Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
registerHandler( "$%apiPath%$",
[](const std::shared_ptr<rb::Session> session)
{
size_t reqSize = session->get_request()->get_header("Content-Length", 0);
session->fetch( reqSize, [](
const std::shared_ptr<rb::Session> session,
const rb::Bytes& body )
{
INITIALIZE_API_CALL_JSON_CONTEXT;
if( !checkRsServicePtrReady(
$%instanceName%$, "$%instanceName%$", cAns, session ) )
return;
$%paramsDeclaration%$
// deserialize input parameters from JSON
$%inputParamsDeserialization%$
// call retroshare C++ API
$%functionCall%$
// serialize out parameters and return value to JSON
$%outputParamsSerialization%$
// return them to the API caller
DEFAULT_API_CALL_JSON_RETURN(rb::OK);
} );
}, $%requiresAuth%$ );

View File

@ -0,0 +1 @@
../../libretroshare/src/jsonapi/method-wrapper-template.cpp.tmpl

19
libbitdht/CMakeLists.txt Normal file
View File

@ -0,0 +1,19 @@
# RetroShare decentralized communication platform
#
# Copyright (C) 2021 Gioacchino Mazzurco <gio@eigenlab.org>
# Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net>
#
# SPDX-License-Identifier: CC0-1.0
cmake_minimum_required (VERSION 2.8.12)
project(bitdht)
file(
GLOB BITDHT_SOURCES
src/bitdht/*.c src/bitdht/*.cc src/udp/*.cc src/util/*.cc )
add_library(${PROJECT_NAME} ${BITDHT_SOURCES})
target_include_directories(
${PROJECT_NAME}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src )

View File

@ -0,0 +1,411 @@
# RetroShare decentralized communication platform
#
# Copyright (C) 2021 Gioacchino Mazzurco <gio@eigenlab.org>
# Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net>
#
# SPDX-License-Identifier: CC0-1.0
cmake_minimum_required (VERSION 3.18.0)
project(retroshare)
include(CMakeDependentOption)
set(FETCHCONTENT_QUIET OFF)
include(FetchContent)
# sqlcipher
option(
RS_SQLCIPHER
"SQLCipher encryption for GXS database"
ON )
# rs_gxs_send_all
option(
RS_GXS_SEND_ALL
"GXS distribute all available messages on request, indipendently from \
local sync timer"
ON )
# bitdht
option(
RS_BITDHT
"Use bitdht (BitTorrent DHT own implementation) to look for online peers"
ON )
# use_dht_stunner
cmake_dependent_option(
RS_BITDHT_STUNNER
"Use bitdht (BitTorrent DHT own implementation) for NAT type discovery and \
attempt the STUN (Session Traversal Utilities for NAT)"
ON
"RS_BITDHT"
OFF )
# use_dht_stunner_ext_ip
cmake_dependent_option(
RS_BITDHT_STUNNER_EXT_IP
"Use bitdht (BitTorrent DHT own implementation) stunner to figure out our \
external IP. As this purely relying on random DHT peers that answer our \
request, it can easily be abused. Therefore, it is turned off by default."
OFF
"RS_BITDHT_STUNNER"
OFF )
# rs_jsonapi
option(
RS_JSON_API
"Use restbed to expose libretroshare as JSON API via HTTP"
OFF )
# rs_deep_forums_index
option(
RS_FORUM_DEEP_INDEX
"Xapian based full text index and search of GXS forums"
OFF )
# rs_broadcast_discovery
option(
RS_BRODCAST_DISCOVERY
"Local area network peer discovery via udp-discovery-cpp"
ON )
# rs_dh_init_check
option(
RS_DH_PRIME_INIT_CHECK
"Check Diffie Hellman prime at each startup. This is not necessary and on \
all Android mobile phones tested this take at least one minute at startup \
which is untolerable for most phone users."
ON )
option(
RS_MINIUPNPC
"Forward ports in NAT router via miniupnpc"
ON )
cmake_dependent_option(
RS_LIBUPNP
"Forward ports in NAT router via libupnp (unstable)"
OFF
"NOT RS_MINIUPNPC"
OFF )
option(
RS_LIBRETROSHARE_STATIC
"Build RetroShare static library"
ON )
cmake_dependent_option(
RS_LIBRETROSHARE_SHARED
"Build RetroShare shared library"
OFF
"NOT RS_LIBRETROSHARE_STATIC"
OFF )
# rs_deprecatedwarning
option(
RS_WARN_DEPRECATED
"Print warning about RetroShare deprecated components usage during build"
ON )
# rs_cppwarning
option(
RS_WARN_LESS
"Silence a few at the moment very common warnings about RetroShare \
components during build"
OFF )
# rs_v07_changes
option(
RS_V07_BREAKING_CHANGES
"Enable retro-compatibility breaking changes planned for RetroShare 0.7.0"
OFF )
set(
RS_DATA_DIR
"${CMAKE_INSTALL_PREFIX}/share/retroshare"
CACHE STRING
"Path where to install RetroShare system wide data" )
################################################################################
find_package(Git REQUIRED)
#function(check_submodule sPath)
# if(NOT EXISTS "${sPath}/.git" )
# message("Initializing submodule ${sPath}")
# execute_process(
# COMMAND "${GIT_EXECUTABLE}" submodule update --init
# WORKING_DIRECTORY "${sPath}"
# COMMAND_ECHO STDERR
# COMMAND_ERROR_IS_FATAL ANY)
# endif()
#endfunction()
################################################################################
include(src/CMakeLists.txt)
list(TRANSFORM RS_SOURCES PREPEND src/)
list(TRANSFORM RS_PUBLIC_HEADERS PREPEND src/)
if(RS_LIBRETROSHARE_STATIC)
add_library(${PROJECT_NAME} STATIC ${RS_SOURCES})
endif(RS_LIBRETROSHARE_STATIC)
if(RS_LIBRETROSHARE_SHARED)
add_library(${PROJECT_NAME} SHARED ${RS_SOURCES})
## Ensure statically linked libraries such as openpgpsdk are compiled with
## PIC Which is needed for shared library
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif(RS_LIBRETROSHARE_SHARED)
target_include_directories(
${PROJECT_NAME}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src )
find_package(OpenSSL REQUIRED)
target_include_directories(${PROJECT_NAME} PRIVATE ${OPENSSL_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::SSL OpenSSL::Crypto)
################################################################################
set(OPENPGPSDK_DEVEL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../openpgpsdk/")
if(EXISTS "${OPENPGPSDK_DEVEL_DIR}/.git" )
message(
CHECK_PASS
"openpgpsdk submodule found at ${OPENPGPSDK_DEVEL_DIR} using it" )
add_subdirectory(${OPENPGPSDK_DEVEL_DIR} ${CMAKE_BINARY_DIR}/openpgpsdk)
else()
FetchContent_Declare(
openpgpsdk
GIT_REPOSITORY "https://gitlab.com/RetroShare/openpgpsdk.git"
GIT_TAG "origin/master"
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
TIMEOUT 10
)
FetchContent_MakeAvailable(openpgpsdk)
endif()
target_link_libraries(${PROJECT_NAME} PRIVATE openpgpsdk)
################################################################################
if(RS_BITDHT)
set(BITDHT_DEVEL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../libbitdht/")
if(EXISTS "${BITDHT_DEVEL_DIR}/.git" )
message(
CHECK_PASS
"BitDHT submodule found at ${BITDHT_DEVEL_DIR} using it" )
add_subdirectory(${BITDHT_DEVEL_DIR} ${CMAKE_BINARY_DIR}/bitdht)
else()
FetchContent_Declare(
bitdht
GIT_REPOSITORY "https://gitlab.com/RetroShare/bitdht.git"
GIT_TAG "origin/master"
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
TIMEOUT 10
)
FetchContent_MakeAvailable(bitdht)
endif()
add_compile_definitions(RS_USE_BITDHT)
target_link_libraries(${PROJECT_NAME} PRIVATE bitdht)
if(RS_BITDHT_STUNNER)
add_compile_definitions(RS_USE_DHT_STUNNER)
if(RS_BITDHT_STUNNER_EXT_IP)
# TODO: Refactor this define to use proper naming
add_compile_definitions(ALLOW_DHT_STUNNER)
endif(RS_BITDHT_STUNNER_EXT_IP)
endif(RS_BITDHT_STUNNER)
endif(RS_BITDHT)
################################################################################
if(RS_JSON_API)
find_package(Doxygen REQUIRED)
find_package(Python3 REQUIRED)
## TODO: execute at build time instead that at cofiguration time see
## add_custom_command or add_custom_target
set(
JSON_API_GENERATOR_WORK_DIR
"${CMAKE_BINARY_DIR}/jsonapi-generator.workdir/" )
set(
JSON_API_GENERATOR_DOXYFILE
"${JSON_API_GENERATOR_WORK_DIR}/jsonapi-generator-doxygen.conf" )
set(
JSONAPI_GENERATOR_OUTPUT_DIR
"${JSON_API_GENERATOR_WORK_DIR}/src/" )
set(
JSONAPI_GENERATOR_SOURCE_DIR
"${CMAKE_CURRENT_SOURCE_DIR}/src/jsonapi/" )
set(
JSONAPI_GENERATOR_EXECUTABLE
"${JSONAPI_GENERATOR_SOURCE_DIR}/jsonapi-generator.py" )
file(
COPY "src/jsonapi/jsonapi-generator-doxygen.conf"
DESTINATION "${JSON_API_GENERATOR_WORK_DIR}" )
file(
APPEND
"${JSON_API_GENERATOR_DOXYFILE}"
"OUTPUT_DIRECTORY=${JSONAPI_GENERATOR_OUTPUT_DIR}\n"
"INPUT=${CMAKE_CURRENT_SOURCE_DIR}" )
add_custom_command(
OUTPUT
"${JSONAPI_GENERATOR_OUTPUT_DIR}/jsonapi-includes.inl"
"${JSONAPI_GENERATOR_OUTPUT_DIR}/jsonapi-wrappers.inl"
COMMAND ${DOXYGEN_EXECUTABLE} ${JSON_API_GENERATOR_DOXYFILE}
COMMAND
${Python3_EXECUTABLE} ${JSONAPI_GENERATOR_EXECUTABLE}
${JSONAPI_GENERATOR_SOURCE_DIR} ${JSONAPI_GENERATOR_OUTPUT_DIR}
MAIN_DEPENDENCY "${JSONAPI_GENERATOR_EXECUTABLE}"
DEPENDS ${JSON_API_GENERATOR_DOXYFILE} ${RS_PUBLIC_HEADERS} )
target_sources(
${PROJECT_NAME} PRIVATE
"${JSONAPI_GENERATOR_OUTPUT_DIR}/jsonapi-includes.inl"
"${JSONAPI_GENERATOR_OUTPUT_DIR}/jsonapi-wrappers.inl" )
include_directories(${JSONAPI_GENERATOR_OUTPUT_DIR})
set(BUILD_TESTS OFF CACHE BOOL "Do not build restbed tests")
set(BUILD_SSL OFF CACHE BOOL "Do not build restbed SSL support")
FetchContent_Declare(
restbed
GIT_REPOSITORY "https://github.com/Corvusoft/restbed.git"
GIT_TAG "4.8"
GIT_SUBMODULES dependency/asio dependency/catch
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
TIMEOUT 10
)
FetchContent_MakeAvailable(restbed)
target_link_libraries(${PROJECT_NAME} PRIVATE restbed)
## TODO: work around target_include_directories should be added upstream
include_directories(${restbed_SOURCE_DIR}/source/)
add_compile_definitions(RS_JSONAPI)
endif(RS_JSON_API)
################################################################################
if(RS_FORUM_DEEP_INDEX)
find_package(Xapian REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE ${XAPIAN_LIBRARIES})
add_compile_definitions(RS_DEEP_FORUMS_INDEX)
endif(RS_FORUM_DEEP_INDEX)
################################################################################
## TODO: Check if https://github.com/rbock/sqlpp11 or
## https://github.com/rbock/sqlpp17 may improve GXS code
if(RS_SQLCIPHER)
find_library(RS_SQL_LIB "sqlcipher" REQUIRED)
find_path(
RS_SQL_LIB_INCLUDE "sqlcipher/sqlite3.h"
PATH_SUFFIXES "include" "includes"
REQUIRED )
target_include_directories(
${PROJECT_NAME}
PRIVATE "${RS_SQL_LIB_INCLUDE}/sqlcipher" )
target_link_libraries(${PROJECT_NAME} PRIVATE ${RS_SQL_LIB})
else()
add_compile_definitions(NO_SQLCIPHER)
find_package(SQLite3 REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE SQLite::SQLite3)
endif()
add_compile_definitions(
SQLITE_HAS_CODEC
RS_ENABLE_GXS
GXS_ENABLE_SYNC_MSGS
RS_USE_GXS_DISTANT_SYNC
RS_GXS_TRANS
V07_NON_BACKWARD_COMPATIBLE_CHANGE_001
V07_NON_BACKWARD_COMPATIBLE_CHANGE_002
V07_NON_BACKWARD_COMPATIBLE_CHANGE_003 )
if(RS_V07_BREAKING_CHANGES)
add_compile_definitions(
V07_NON_BACKWARD_COMPATIBLE_CHANGE_004
V07_NON_BACKWARD_COMPATIBLE_CHANGE_UNNAMED )
endif()
if(RS_DH_PRIME_INIT_CHECK)
add_compile_definitions(RS_DISABLE_DIFFIE_HELLMAN_INIT_CHECK)
endif(RS_DH_PRIME_INIT_CHECK)
if(RS_MINIUPNPC)
add_compile_definitions(RS_USE_LIBMINIUPNPC)
endif(RS_MINIUPNPC)
if(RS_LIBUPNP)
message(FATAL_ERROR "UPnP support via libupnp is currently not supported")
#add_compile_definitions(RS_USE_LIBUPNP)
endif(RS_LIBUPNP)
if(RS_GXS_SEND_ALL)
add_compile_definitions(RS_GXS_SEND_ALL)
endif(RS_GXS_SEND_ALL)
if(RS_BRODCAST_DISCOVERY)
## TODO: upstream option to disable tests building
set(BUILD_EXAMPLE OFF CACHE BOOL "Do not build udp-discovery-cpp examples")
set(BUILD_TOOL OFF CACHE BOOL "Do not build udp-discovery-tool application")
FetchContent_Declare(
udp-discovery-cpp
GIT_REPOSITORY "https://github.com/truvorskameikin/udp-discovery-cpp.git"
GIT_TAG "origin/master"
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
TIMEOUT 10
)
FetchContent_MakeAvailable(udp-discovery-cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE udp-discovery-cpp)
## TODO: Temporary work around target_include_directories should be added
## upstream
include_directories(${udp-discovery-cpp_SOURCE_DIR})
endif(RS_BRODCAST_DISCOVERY)
if(NOT RS_WARN_DEPRECATED)
add_compile_definitions(RS_NO_WARN_DEPRECATED)
target_compile_options(
${PROJECT_NAME} PRIVATE
-Wno-deprecated -Wno-deprecated-declarations )
endif(NOT RS_WARN_DEPRECATED)
if(RS_WARN_LESS)
add_compile_definitions(RS_NO_WARN_CPP)
target_compile_options(
${PROJECT_NAME} PRIVATE
-Wno-cpp -Wno-inconsistent-missing-override )
endif(RS_WARN_LESS)
################################################################################
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_compile_definitions(RS_DATA_DIR="${RS_DATA_DIR}")
endif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
## Useful to debug CMake
#set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

View File

@ -0,0 +1,409 @@
# RetroShare decentralized communication platform
#
# Copyright (C) 2021 Gioacchino Mazzurco <gio@eigenlab.org>
# Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net>
#
# SPDX-License-Identifier: CC0-1.0
list(
APPEND RS_PUBLIC_HEADERS
retroshare/rsexpr.h
retroshare/rsgxsdistsync.h
retroshare/rsiface.h
retroshare/rsrtt.h
retroshare/rsbanlist.h
retroshare/rsconfig.h
retroshare/rsdisc.h
retroshare/rsflags.h
retroshare/rsgrouter.h
retroshare/rsgxsflags.h
retroshare/rsgxsservice.h
retroshare/rsgxstrans.h
retroshare/rsgxstunnel.h
retroshare/rsids.h
retroshare/rsnotify.h
retroshare/rsphoto.h
retroshare/rsplugin.h
retroshare/rsreputations.h
retroshare/rsservicecontrol.h
retroshare/rstokenservice.h
retroshare/rsturtle.h
retroshare/rsgossipdiscovery.h
retroshare/rsgxscommon.h
retroshare/rsposted.h
retroshare/rsstatus.h
retroshare/rsversion.h
retroshare/rsgxsifacehelper.h
retroshare/rshistory.h
retroshare/rsidentity.h
retroshare/rsmsgs.h
retroshare/rsgxschannels.h
retroshare/rsgxscircles.h
retroshare/rsgxsiface.h
retroshare/rsgxsifacetypes.h
retroshare/rstypes.h
retroshare/rsgxsforums.h
retroshare/rsevents.h
retroshare/rsfiles.h
retroshare/rsinit.h
retroshare/rspeers.h )
list(
APPEND RS_SOURCES
chat/distantchat.cc
chat/p3chatservice.cc
chat/rschatitems.cc
chat/distributedchat.cc
crypto/chacha20.cpp
crypto/hashstream.cc
crypto/rsaes.cc
crypto/rscrypto.cpp )
if(RS_BITDHT)
list(
APPEND RS_PUBLIC_HEADERS
retroshare/rsdht.h )
list(
APPEND RS_SOURCES
dht/connectstatebox.cc
dht/p3bitdht.cc
dht/p3bitdht_interface.cc
dht/p3bitdht_peernet.cc
dht/p3bitdht_peers.cc
dht/p3bitdht_relay.cc )
endif(RS_BITDHT)
list(
APPEND RS_SOURCES
file_sharing/filelist_io.cc
file_sharing/rsfilelistitems.cc
file_sharing/file_tree.cc
file_sharing/directory_updater.cc
file_sharing/p3filelists.cc
file_sharing/hash_cache.cc
file_sharing/dir_hierarchy.cc
file_sharing/directory_storage.cc
ft/ftchunkmap.cc
ft/ftfilecreator.cc
ft/ftfileprovider.cc
ft/ftfilesearch.cc
ft/ftturtlefiletransferitem.cc
ft/fttransfermodule.cc
ft/ftcontroller.cc
ft/ftdatamultiplex.cc
ft/ftextralist.cc
ft/ftserver.cc )
list(
APPEND RS_SOURCES
grouter/groutermatrix.cc
grouter/grouteritems.cc
grouter/p3grouter.cc )
list(
APPEND RS_SOURCES
gxs/rsgxsdata.cc
gxs/rsgxsrequesttypes.cc
gxs/gxssecurity.cc
gxs/gxstokenqueue.cc
gxs/rsdataservice.cc
gxs/rsgxsdataaccess.cc
gxs/rsgxsnetutils.cc
gxs/rsgxsnettunnel.cc
gxs/rsgxsutil.cc
gxs/rsnxsobserver.cpp
gxs/rsgenexchange.cc
gxs/rsgxsnetservice.cc )
list(
APPEND RS_SOURCES
gxstrans/p3gxstransitems.cc
gxstrans/p3gxstrans.cc )
list(
APPEND RS_SOURCES
gxstunnel/rsgxstunnelitems.cc
gxstunnel/p3gxstunnel.cc )
if(RS_JSON_API)
list(
APPEND RS_PUBLIC_HEADERS
retroshare/rsjsonapi.h )
list(
APPEND RS_SOURCES
jsonapi/jsonapi.cpp )
endif(RS_JSON_API)
list(
APPEND RS_SOURCES
pgp/pgpkeyutil.cc
pgp/pgpauxutils.cc
pgp/pgphandler.cc
pgp/rscertificate.cc )
#./plugins/dlfcn_win32.cc
#./plugins/dlfcn_win32.h
#./plugins/pluginmanager.h
#./plugins/rscacheservice.h
#./plugins/rspqiservice.h
#./plugins/pluginmanager.cc
list(
APPEND RS_SOURCES
pqi/pqibin.cc
pqi/pqiipset.cc
pqi/pqiloopback.cc
pqi/pqimonitor.cc
pqi/pqipersongrp.cc
pqi/pqiqos.cc
pqi/pqiqosstreamer.cc
pqi/pqisslproxy.cc
pqi/pqistore.cc
pqi/authgpg.cc
pqi/p3cfgmgr.cc
pqi/p3notify.cc
pqi/p3servicecontrol.cc
pqi/pqinetstatebox.cc
pqi/pqiperson.cc
pqi/pqiservice.cc
pqi/pqissllistener.cc
pqi/pqissludp.cc
pqi/pqithreadstreamer.cc
pqi/sslfns.cc
pqi/authssl.cc
pqi/p3historymgr.cc
pqi/p3linkmgr.cc
pqi/pqihandler.cc
pqi/pqistreamer.cc
pqi/p3netmgr.cc
pqi/p3peermgr.cc
pqi/pqinetwork.cc
pqi/pqissl.cc
pqi/pqisslpersongrp.cc )
#./pqi/pqissli2psam3.cpp
#./pqi/pqissli2psam3.h
list(
APPEND RS_SOURCES
rsitems/rsbanlistitems.cc
rsitems/rsbwctrlitems.cc
rsitems/rsconfigitems.cc
rsitems/rsfiletransferitems.cc
rsitems/rsgxscommentitems.cc
rsitems/rsgxsforumitems.cc
rsitems/rsgxsiditems.cc
rsitems/rsgxsrecognitems.cc
rsitems/rsgxsreputationitems.cc
rsitems/rsgxsupdateitems.cc
rsitems/rshistoryitems.cc
rsitems/rsrttitems.cc
rsitems/rsserviceinfoitems.cc )
#retroshare/rswiki.h
#./rsitems/rswikiitems.cc
#./rsitems/rswikiitems.h
#./rsitems/rswireitems.h
list(
APPEND RS_SOURCES
rsitems/rsgxschannelitems.cc
rsitems/rsgxscircleitems.cc
rsitems/rsgxsitems.cc
rsitems/rsmsgitems.cc )
#./rsitems/rsphotoitems.cc
#./rsitems/rsphotoitems.h
#./rsitems/rsposteditems.cc
#./rsitems/rsposteditems.h
#./rsitems/rswireitems.cc
#retroshare/rswire.h
list(
APPEND RS_SOURCES
rsitems/rsnxsitems.cc )
list(
APPEND RS_SOURCES
rsserver/p3status.cc
rsserver/p3face-config.cc
rsserver/p3face-info.cc
rsserver/p3history.cc
rsserver/p3serverconfig.cc
rsserver/rsloginhandler.cc
rsserver/p3face-server.cc
rsserver/p3msgs.cc
rsserver/p3peers.cc
rsserver/rsaccounts.cc
rsserver/rsinit.cc )
list(
APPEND RS_SOURCES
serialiser/rsbaseserial.cc
serialiser/rsserializable.cc
serialiser/rstlvaddrs.cc
serialiser/rstlvbanlist.cc
serialiser/rstlvbase.cc
serialiser/rstlvbinary.cc
serialiser/rstlvfileitem.cc
serialiser/rstlvgenericmap.inl
serialiser/rstlvgenericparam.cc
serialiser/rstlvidset.cc
serialiser/rstlvimage.cc
serialiser/rstlvitem.cc
serialiser/rstlvkeys.cc
serialiser/rstlvkeyvalue.cc
serialiser/rstlvstring.cc
serialiser/rsserializer.cc
serialiser/rstypeserializer.cc
serialiser/rsserial.cc )
# ./services/autoproxy
#./services/autoproxy/p3i2psam3.cpp
#./services/autoproxy/p3i2psam3.h
list(
APPEND RS_SOURCES
services/autoproxy/rsautoproxymonitor.cc
services/p3bwctrl.cc
services/p3heartbeat.cc
services/p3service.cc
services/p3serviceinfo.cc
services/p3statusservice.cc
services/p3banlist.cc
services/p3rtt.cc
services/rseventsservice.cc
services/p3gxscircles.cc
services/p3gxscommon.cc
services/p3gxsreputation.cc
services/p3msgservice.cc
services/p3idservice.cc
services/p3gxschannels.cc
services/p3gxsforums.cc )
#./services/p3wiki.cc
#./services/p3wiki.h
#./services/p3wire.cc
#./services/p3wire.h
#./services/p3photoservice.cc
#./services/p3photoservice.h
#./services/p3postbase.cc
#./services/p3postbase.h
#./services/p3posted.cc
#./services/p3posted.h
if(RS_BRODCAST_DISCOVERY)
list(
APPEND RS_PUBLIC_HEADERS
retroshare/rsbroadcastdiscovery.h )
list(
APPEND RS_SOURCES
services/broadcastdiscoveryservice.cc )
endif(RS_BRODCAST_DISCOVERY)
list(
APPEND RS_SOURCES
tcponudp/tcppacket.cc
tcponudp/tcpstream.cc
tcponudp/tou.cc
tcponudp/udppeer.cc
tcponudp/bss_tou.cc
tcponudp/udprelay.cc )
if(RS_BITDHT_STUNNER)
list(
APPEND RS_SOURCES
tcponudp/udpstunner.cc )
endif(RS_BITDHT_STUNNER)
list(
APPEND RS_SOURCES
turtle/rsturtleitem.cc
turtle/p3turtle.cc )
list(
APPEND RS_SOURCES
# util/contentvalue.cc
# util/exampletst.c
# util/rsdbbind.cc
# util/rsdiscspace.cc
util/rsexpr.cc
util/rsprint.cc
# util/rsrecogn.cc
# util/rssharedptr.h
# util/rstickevent.cc
util/rstime.cc
util/smallobject.cc
# util/retrodb.cc
util/rsbase64.cc
util/rsjson.cc
# util/rskbdinput.cc
util/rsrandom.cc
util/rsstring.cc
util/rsurl.cc
util/folderiterator.cc
util/rsdir.cc
util/rsfile.cc
util/dnsresolver.cc
util/extaddrfinder.cc
util/rsdebug.cc
util/rsdnsutils.cc
util/rsnet.cc
util/rsnet_ss.cc
util/rsthreads.cc )
# util/i2pcommon.cpp
# util/i2pcommon.h
if(RS_FORUM_DEEP_INDEX)
list(
APPEND RS_SOURCES
deep_search/commonutils.cpp
deep_search/forumsindex.cpp )
endif(RS_FORUM_DEEP_INDEX)
#./deep_search/filesflacindexer.hpp
#./deep_search/filesoggindexer.hpp
#./deep_search/filestaglibindexer.hpp
#./deep_search/filesindex.cpp
#./deep_search/filesindex.hpp
#./deep_search/channelsindex.cpp
#./deep_search/channelsindex.hpp
list(
APPEND RS_SOURCES
gossipdiscovery/gossipdiscoveryitems.cc
gossipdiscovery/p3gossipdiscovery.cc )
if(RS_MINIUPNPC)
list(
APPEND RS_SOURCES
rs_upnp/upnphandler_miniupnp.cc )
endif(RS_MINIUPNPC)
#./rs_upnp/UPnPBase.cpp
#./rs_upnp/upnphandler_libupnp.cc
#./rs_upnp/upnptest.cc
#./rs_upnp/upnputil.cc
#./rs_android/LocalArray.h
#./rs_android/README-ifaddrs-android.adoc
#./rs_android/ScopedFd.h
#./rs_android/androidcoutcerrcatcher.hpp
#./rs_android/errorconditionwrap.cpp
#./rs_android/ifaddrs-android.h
#./rs_android/org
#./rs_android/org/retroshare
#./rs_android/org/retroshare/service
#./rs_android/org/retroshare/service/AssetHelper.java
#./rs_android/org/retroshare/service/ErrorConditionWrap.java
#./rs_android/org/retroshare/service/RetroShareServiceAndroid.java
#./rs_android/retroshareserviceandroid.cpp
#./rs_android/retroshareserviceandroid.hpp
#./rs_android/rsjni.cpp
#./rs_android/rsjni.hpp

View File

@ -46,7 +46,9 @@ std::error_condition DeepForumsIndex::search(
// End of prefix configuration.
// And parse the query.
Xapian::Query query = queryparser.parse_query(queryStr);
using XQP = Xapian::QueryParser;
Xapian::Query query = queryparser.parse_query(
queryStr, XQP::FLAG_WILDCARD | XQP::FLAG_DEFAULT );
// Use an Enquire object on the database to run the query.
Xapian::Enquire enquire(db);

View File

@ -90,7 +90,7 @@ bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,st
RsDbg() << "Sending item to friend server at \"" << address << ":" << port ;
int CreateSocket = 0,n = 0;
int CreateSocket = 0;
char dataReceived[1024];
struct sockaddr_in ipOfServer;
@ -130,7 +130,7 @@ bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,st
// FsSerializer().serialise(item,data,&size);
// write(CreateSocket,data,size); // shouldn't we use the pqistreamer in R/W mode instead?
RsFdBinInterface *bio = new RsFdBinInterface(CreateSocket); // deleted by ~pqistreamer()
RsFdBinInterface *bio = new RsFdBinInterface(CreateSocket,true); // deleted by ~pqistreamer()
pqithreadstreamer p(this,rss,RsPeerId(),bio,BIN_FLAGS_READABLE | BIN_FLAGS_WRITEABLE | BIN_FLAGS_NO_CLOSE);
p.start();

View File

@ -107,7 +107,7 @@ p3discovery2::p3discovery2(
addSerialType(new RsDiscSerialiser());
// Add self into PGP FriendList.
mFriendList[AuthGPG::getAuthGPG()->getGPGOwnId()] = DiscPgpInfo();
mFriendList[AuthPGP::getPgpOwnId()] = DiscPgpInfo();
}
@ -219,7 +219,7 @@ void p3discovery2::removeFriend(const RsPeerId &sslId)
std::cerr << std::endl;
#endif
/* pgp peer without any ssl entries -> check if they are still a real friend */
if (!(AuthGPG::getAuthGPG()->isGPGAccepted(pgpId)))
if (!(AuthPGP::isPGPAccepted(pgpId)))
{
#ifdef P3DISC_DEBUG
std::cerr << "p3discovery2::addFriend() pgpId is no longer a friend, removing";
@ -604,8 +604,8 @@ void p3discovery2::updatePgpFriendList()
std::list<RsPgpId>::iterator lit;
std::map<RsPgpId, DiscPgpInfo>::iterator it;
RsPgpId ownPgpId = AuthGPG::getAuthGPG()->getGPGOwnId();
AuthGPG::getAuthGPG()->getGPGAcceptedList(pgpList);
RsPgpId ownPgpId = AuthPGP::getPgpOwnId();
AuthPGP::getPgpAcceptedList(pgpList);
pgpList.push_back(ownPgpId);
// convert to set for ordering.
@ -723,7 +723,7 @@ void p3discovery2::processPGPList(const RsPeerId &fromId, const RsDiscPgpListIte
std::set<RsPgpId>::const_iterator fit;
for(fit = item->pgpIdSet.ids.begin(); fit != item->pgpIdSet.ids.end(); ++fit)
{
if (!AuthGPG::getAuthGPG()->isGPGId(*fit))
if (!AuthPGP::isPGPId(*fit))
{
#ifdef P3DISC_DEBUG
std::cerr << "p3discovery2::processPGPList() requesting certificate for PgpId: " << *fit;
@ -1058,11 +1058,11 @@ void p3discovery2::recvPGPCertificateRequest( const RsPeerId& fromId, const RsDi
return;
}
RsPgpId ownPgpId = AuthGPG::getAuthGPG()->getGPGOwnId();
RsPgpId ownPgpId = AuthPGP::getPgpOwnId();
for(const RsPgpId& pgpId : item->pgpIdSet.ids)
if (pgpId == ownPgpId)
sendPGPCertificate(pgpId, fromId);
else if(ps.vs_disc != RS_VS_DISC_OFF && AuthGPG::getAuthGPG()->isGPGAccepted(pgpId))
else if(ps.vs_disc != RS_VS_DISC_OFF && AuthPGP::isPGPAccepted(pgpId))
sendPGPCertificate(pgpId, fromId);
else
std::cerr << "(WW) not sending certificate " << pgpId << " asked by friend " << fromId << " because this either this cert is not a friend, or discovery is off" << std::endl;
@ -1078,7 +1078,7 @@ void p3discovery2::sendPGPCertificate(const RsPgpId &aboutId, const RsPeerId &to
unsigned char *bin_data;
size_t bin_len;
if(!AuthGPG::getAuthGPG()->exportPublicKey(aboutId,bin_data,bin_len,false,true))
if(!AuthPGP::exportPublicKey(aboutId,bin_data,bin_len,false,true))
{
std::cerr << "(EE) cannot export public key " << aboutId << " requested by peer " << toId << std::endl;
return ;
@ -1098,7 +1098,7 @@ void p3discovery2::recvPGPCertificate(const RsPeerId& fromId, RsDiscPgpKeyItem*
std::string cert_name;
std::list<RsPgpId> cert_signers;
if(!AuthGPG::getAuthGPG()->getGPGDetailsFromBinaryBlock( (unsigned char*)item->bin_data,item->bin_len, cert_pgp_id, cert_name, cert_signers ))
if(!AuthPGP::getPgpDetailsFromBinaryBlock( (unsigned char*)item->bin_data,item->bin_len, cert_pgp_id, cert_name, cert_signers ))
{
std::cerr << "(EE) cannot parse own PGP key sent by " << fromId << std::endl;
return;
@ -1147,7 +1147,7 @@ void p3discovery2::recvPGPCertificate(const RsPeerId& fromId, RsDiscPgpKeyItem*
// otherwise the connection should already be accepted. This only happens when the short invite peer sends its own PGP key.
if(det.skip_pgp_signature_validation)
AuthGPG::getAuthGPG()->AllowConnection(det.gpg_id,true);
AuthPGP::AllowConnection(det.gpg_id,true);
}
/************* from pqiServiceMonitor *******************/

View File

@ -41,6 +41,7 @@
#include "rsserver/p3face.h"
#include "retroshare/rsevents.h"
#include "util/radix64.h"
#include "util/cxx17retrocompat.h"
#define PUB_GRP_MASK 0x000f
#define RESTR_GRP_MASK 0x00f0
@ -2285,8 +2286,9 @@ bool RsGenExchange::processGrpMask(const RsGxsGroupId& grpId, ContentValue &grpC
void RsGenExchange::publishMsgs()
{
bool atLeastOneMessageCreatedSuccessfully = false;
RS_STACK_MUTEX(mGenMtx) ;
RS_STACK_MUTEX(mGenMtx);
rstime_t now = time(NULL);
@ -2463,6 +2465,8 @@ void RsGenExchange::publishMsgs()
// add to published to allow acknowledgement
mMsgNotify.insert(std::make_pair(mit->first, std::make_pair(grpId, msgId)));
mDataAccess->updatePublicRequestStatus(mit->first, RsTokenService::COMPLETE);
atLeastOneMessageCreatedSuccessfully = true;
}
else
{
@ -2496,6 +2500,8 @@ void RsGenExchange::publishMsgs()
mNotifications.push_back(ch);
}
if(atLeastOneMessageCreatedSuccessfully) mNetService->requestPull();
}
RsGenExchange::ServiceCreate_Return RsGenExchange::service_CreateGroup(RsGxsGrpItem* /* grpItem */,
@ -2723,7 +2729,8 @@ bool RsGenExchange::checkKeys(const RsTlvSecurityKeySet& keySet)
void RsGenExchange::publishGrps()
{
std::list<RsGxsGroupId> groups_to_subscribe ;
bool atLeastOneGroupCreatedSuccessfully = false;
std::list<RsGxsGroupId> groups_to_subscribe;
{
RS_STACK_MUTEX(mGenMtx) ;
@ -2954,6 +2961,8 @@ void RsGenExchange::publishGrps()
// add to published to allow acknowledgement
toNotify.insert(std::make_pair(token, GrpNote(true,ggps.mIsUpdate,grpId)));
atLeastOneGroupCreatedSuccessfully = true;
}
}
@ -2972,9 +2981,14 @@ void RsGenExchange::publishGrps()
// This is done off-mutex to avoid possible cross deadlocks with the net service.
if(mNetService!=NULL)
for(std::list<RsGxsGroupId>::const_iterator it(groups_to_subscribe.begin());it!=groups_to_subscribe.end();++it)
mNetService->subscribeStatusChanged((*it),true) ;
if(mNetService != nullptr)
{
for(auto& grpId : std::as_const(groups_to_subscribe))
mNetService->subscribeStatusChanged(grpId, true);
if(atLeastOneGroupCreatedSuccessfully)
mNetService->requestPull();
}
}
uint32_t RsGenExchange::generatePublicToken()

View File

@ -35,7 +35,7 @@
// |
// +----------- sharePublishKeys()
// |
// +----------- syncWithPeers()
// +----------- checkUpdatesFromPeers()
// | |
// | +--if AutoSync--- send global UpdateTS of each peer to itself => the peer knows the last
// | | time current peer has received an updated from himself
@ -127,14 +127,14 @@
// (Set at server side to be mGrpServerUpdateItem->grpUpdateTS)
//
// Only updated in processCompletedIncomingTransaction() from Grp list transaction.
// Used in syncWithPeers() sending in RsNxsSyncGrp once to all peers: peer will send data if
// Used in checkUpdatesFromPeers() sending in RsNxsSyncGrp once to all peers: peer will send data if
// has something new. All time comparisons are in the friends' clock time.
//
// mClientMsgUpdateMap: map< RsPeerId, map<grpId,TimeStamp > >
//
// Last msg list modification time sent by that peer Id
// Updated in processCompletedIncomingTransaction() from Grp list trans.
// Used in syncWithPeers() sending in RsNxsSyncGrp once to all peers.
// Used in checkUpdatesFromPeers() sending in RsNxsSyncGrp once to all peers.
// Set at server to be mServerMsgUpdateMap[grpId]->msgUpdateTS
//
// mGrpServerUpdateItem: TimeStamp Last group local modification timestamp over all groups
@ -150,7 +150,7 @@
//
// tick() tick()
// | |
// +---- SyncWithPeers +-- recvNxsItemQueue()
// +---- checkUpdatesFromPeers() +-- recvNxsItemQueue()
// | |
// +---------------- Send global UpdateTS of each peer to itself => the peer knows +---------> +------ handleRecvSyncGroup( RsNxsSyncGrp*)
// | the last msg sent (stored in mClientGrpUpdateMap[peer_id]), | | - parse all subscribed groups. For each, send a RsNxsSyncGrpItem with publish TS
@ -457,7 +457,7 @@ int RsGxsNetService::tick()
if((elapsed) < now)
{
syncWithPeers();
checkUpdatesFromPeers();
syncGrpStatistics();
checkDistantSyncState();
@ -570,39 +570,40 @@ RsGxsGroupId RsGxsNetService::hashGrpId(const RsGxsGroupId& gid,const RsPeerId&
return RsGxsGroupId( RsDirUtil::sha1sum(tmpmem,SIZE).toByteArray() );
}
void RsGxsNetService::syncWithPeers()
std::error_condition RsGxsNetService::checkUpdatesFromPeers(
std::set<RsPeerId> peers )
{
#ifdef NXS_NET_DEBUG_0
GXSNETDEBUG___ << "RsGxsNetService::syncWithPeers() this=" << (void*)this << ". serviceInfo=" << mServiceInfo << std::endl;
RS_DBG("this=", (void*)this, ". serviceInfo=", mServiceInfo);
#endif
static RsNxsSerialiser ser(mServType) ; // this is used to estimate bandwidth.
RS_STACK_MUTEX(mNxsMutex) ;
std::set<RsPeerId> peers;
mNetMgr->getOnlineList(mServiceInfo.mServiceType, peers);
if(mAllowDistSync && mGxsNetTunnel != NULL)
/* If specific peers are passed as paramether ask only to them */
if(peers.empty())
{
// Grab all online virtual peers of distant tunnels for the current service.
mNetMgr->getOnlineList(mServiceInfo.mServiceType, peers);
std::list<RsGxsNetTunnelVirtualPeerId> vpids ;
mGxsNetTunnel->getVirtualPeers(vpids);
if(mAllowDistSync && mGxsNetTunnel != nullptr)
{
/* Grab all online virtual peers of distant tunnels for the current
* service. */
for(auto it(vpids.begin());it!=vpids.end();++it)
peers.insert(RsPeerId(*it)) ;
std::list<RsGxsNetTunnelVirtualPeerId> vpids ;
mGxsNetTunnel->getVirtualPeers(vpids);
for(auto it(vpids.begin());it!=vpids.end();++it)
peers.insert(RsPeerId(*it)) ;
}
}
if (peers.empty()) {
// nothing to do
return;
}
// Still empty? Reports there are no available peers
if (peers.empty()) return std::errc::network_down;
RS_STACK_MUTEX(mNxsMutex);
// for now just grps
for(auto sit = peers.begin(); sit != peers.end(); ++sit)
{
const RsPeerId peerId = *sit;
ClientGrpMap::const_iterator cit = mClientGrpUpdateMap.find(peerId);
@ -624,8 +625,7 @@ void RsGxsNetService::syncWithPeers()
generic_sendItem(grp);
}
if(!mAllowMsgSync)
return ;
if(!mAllowMsgSync) return std::error_condition();
#ifndef GXS_DISABLE_SYNC_MSGS
@ -743,10 +743,12 @@ void RsGxsNetService::syncWithPeers()
}
}
#endif
#endif // ndef GXS_DISABLE_SYNC_MSGS
return std::error_condition();
}
void RsGxsNetService::generic_sendItem(RsNxsItem *si)
void RsGxsNetService::generic_sendItem(rs_owner_ptr<RsItem> si)
{
// check if the item is to be sent to a distant peer or not
@ -1022,32 +1024,32 @@ void RsGxsNetService::locked_resetClientTS(const RsGxsGroupId& grpId)
it->second.msgUpdateInfos.erase(grpId) ;
}
void RsGxsNetService::subscribeStatusChanged(const RsGxsGroupId& grpId,bool subscribed)
void RsGxsNetService::subscribeStatusChanged(
const RsGxsGroupId& grpId, bool subscribed )
{
RS_STACK_MUTEX(mNxsMutex) ;
if(!subscribed)
return ;
if(!subscribed) return;
// When we subscribe, we reset the time stamps, so that the entire group list
// gets requested once again, for a proper update.
RS_STACK_MUTEX(mNxsMutex);
#ifdef NXS_NET_DEBUG_0
GXSNETDEBUG__G(grpId) << "Changing subscribe status for grp " << grpId << " to " << subscribed << ": reseting all server msg time stamps for this group, and server global TS." << std::endl;
std::map<RsGxsGroupId,RsGxsServerMsgUpdate>::iterator it = mServerMsgUpdateMap.find(grpId) ;
RS_DBG( "Changing subscribe status for grp", grpId, " to ", subscribed,
": reseting all server msg time stamps for this group, and "
"server global TS." );
#endif
RsGxsServerMsgUpdate& item(mServerMsgUpdateMap[grpId]) ;
RsGxsServerMsgUpdate& item(mServerMsgUpdateMap[grpId]);
item.msgUpdateTS = static_cast<uint32_t>(time(nullptr));
item.msgUpdateTS = time(NULL) ;
/* We also update mGrpServerUpdateItem so as to trigger a new grp list
* exchange with friends (friends will send their known ClientTS which
* will be lower than our own grpUpdateTS, triggering our sending of the
* new subscribed grp list. */
mGrpServerUpdate.grpUpdateTS = static_cast<uint32_t>(time(nullptr));
// We also update mGrpServerUpdateItem so as to trigger a new grp list exchange with friends (friends will send their known ClientTS which
// will be lower than our own grpUpdateTS, triggering our sending of the new subscribed grp list.
mGrpServerUpdate.grpUpdateTS = time(NULL) ;
if(subscribed)
locked_resetClientTS(grpId) ;
locked_resetClientTS(grpId);
}
bool RsGxsNetService::fragmentMsg(RsNxsMsg& msg, MsgFragments& msgFragments) const
@ -1718,13 +1720,25 @@ RsItem *RsGxsNetService::generic_recvItem()
void RsGxsNetService::recvNxsItemQueue()
{
RsItem *item ;
RsItem* item;
while(NULL != (item=generic_recvItem()))
{
while(nullptr != (item=generic_recvItem()))
{
#ifdef NXS_NET_DEBUG_1
GXSNETDEBUG_P_(item->PeerId()) << "Received RsGxsNetService Item:" << (void*)item << " type=" << std::hex << item->PacketId() << std::dec << std::endl ;
RS_DBG( "Received RsGxsNetService Item: ", (void*)item, " type=",
item->PacketId() );
#endif
/* Handle pull request and other new items here to not mess with all the
* old nested code and items hell */
switch(static_cast<RsNxsSubtype>(item->PacketSubType()))
{
case RsNxsSubtype::PULL_REQUEST:
std::unique_ptr<RsNxsPullRequestItem> pullItem(
static_cast<RsNxsPullRequestItem*>(item) );
handlePullRequest(std::move(pullItem));
continue;
}
// RsNxsItem needs dynamic_cast, since they have derived siblings.
//
RsNxsItem *ni = dynamic_cast<RsNxsItem*>(item) ;
@ -4690,7 +4704,7 @@ bool RsGxsNetService::checkPermissionsForFriendGroup(const RsPeerId& sslId,const
if(!grpMeta.mInternalCircle.isNull())
{
RsGroupInfo ginfo ;
RsPgpId pgpId = mPgpUtils->getPGPId(sslId) ;
RsPgpId pgpId = mPgpUtils->getPgpId(sslId) ;
#ifdef NXS_NET_DEBUG_4
GXSNETDEBUG_PG(sslId,grpMeta.mGroupId) << " Group internal circle: " << grpMeta.mInternalCircle << ", We're owner. Sending to everyone in the group." << std::endl;
@ -5075,6 +5089,46 @@ void RsGxsNetService::handleRecvPublishKeys(RsNxsGroupPublishKeyItem *item)
}
}
std::error_condition RsGxsNetService::requestPull(std::set<RsPeerId> peers)
{
/* If specific peers are passed as paramether ask only to them */
if(peers.empty())
{
mNetMgr->getOnlineList(mServiceInfo.mServiceType, peers);
if(mAllowDistSync && mGxsNetTunnel != nullptr)
{
/* Grab all online virtual peers of distant tunnels for the current
* service. */
std::list<RsGxsNetTunnelVirtualPeerId> vpids ;
mGxsNetTunnel->getVirtualPeers(vpids);
for(auto it(vpids.begin());it!=vpids.end();++it)
peers.insert(RsPeerId(*it)) ;
}
}
// Still empty? Reports there are no available peers
if (peers.empty()) return std::errc::network_down;
for(auto& peerId: std::as_const(peers))
{
auto item = new RsNxsPullRequestItem(
static_cast<RsServiceType>(mServType) );
item->PeerId(peerId);
generic_sendItem(item);
}
return std::error_condition();
}
void RsGxsNetService::handlePullRequest(
std::unique_ptr<RsNxsPullRequestItem> item )
{
checkUpdatesFromPeers(std::set<RsPeerId>{item->PeerId()});
}
bool RsGxsNetService::getGroupServerUpdateTS(const RsGxsGroupId& gid,rstime_t& group_server_update_TS, rstime_t& msg_server_update_TS)
{
RS_STACK_MUTEX(mNxsMutex) ;

View File

@ -250,6 +250,15 @@ public:
void threadTick() override; /// @see RsTickingThread
/// @see RsNetworkExchangeService
std::error_condition checkUpdatesFromPeers(
std::set<RsPeerId> peers = std::set<RsPeerId>() ) override;
/// @see RsNetworkExchangeService
std::error_condition requestPull(
std::set<RsPeerId> peers = std::set<RsPeerId>() ) override;
private:
/*!
@ -423,6 +432,8 @@ private:
*/
void handleRecvPublishKeys(RsNxsGroupPublishKeyItem*) ;
void handlePullRequest(std::unique_ptr<RsNxsPullRequestItem> item);
/** E: item handlers **/
@ -459,7 +470,7 @@ private:
void locked_pushMsgRespFromList(std::list<RsNxsItem*>& itemL, const RsPeerId& sslId, const RsGxsGroupId &grp_id, const uint32_t& transN);
void checkDistantSyncState();
void syncWithPeers();
void syncGrpStatistics();
void addGroupItemToList(NxsTransaction*& tr,
const RsGxsGroupId& grpId, uint32_t& transN,
@ -559,7 +570,7 @@ private:
void cleanRejectedMessages();
void processObserverNotifications();
void generic_sendItem(RsNxsItem *si);
void generic_sendItem(rs_owner_ptr<RsItem> si);
RsItem *generic_recvItem();
private:

View File

@ -223,7 +223,7 @@ bool GrpCircleVetting::canSend(
{
if(mCircles->isLoaded(circleId))
{
const RsPgpId& pgpId = mPgpUtils->getPGPId(peerId);
const RsPgpId& pgpId = mPgpUtils->getPgpId(peerId);
return mCircles->canSend(circleId, pgpId,should_encrypt);
}
@ -302,7 +302,7 @@ bool MsgCircleIdsRequestVetting::cleared()
if(filtered_out_msgs>0)
std::cerr << "(WW) " << filtered_out_msgs << " messages not sent because they are signed by author(s) not member of that circle " << mCircleId << std::endl;
RsPgpId pgpId = mPgpUtils->getPGPId(mPeerId);
RsPgpId pgpId = mPgpUtils->getPgpId(mPeerId);
bool can_send_res = mCircles->canSend(mCircleId, pgpId,mShouldEncrypt);
if(mShouldEncrypt) // that means the circle is external

View File

@ -26,7 +26,7 @@
#include <set>
#include <string>
#include <stdlib.h>
#include <cstdlib>
#include <list>
#include <map>
@ -325,4 +325,20 @@ public:
return RsReputationLevel::NEUTRAL;
}
}
/**
* @brief Check if new stuff is available from peers
* @param peers peers to check, if empty all available peers are checked
*/
virtual std::error_condition checkUpdatesFromPeers(
std::set<RsPeerId> peers = std::set<RsPeerId>() ) = 0;
/**
* @brief request online peers to pull updates from our node ASAP
* @param peers peers to which request pull from, if empty all available
* peers are requested to pull
* @return success or error details
*/
virtual std::error_condition requestPull(
std::set<RsPeerId> peers = std::set<RsPeerId>() ) = 0;
};

View File

@ -0,0 +1,447 @@
// SPDX-FileCopyrightText: (C) 2004-2019 Retroshare Team <contact@retroshare.cc>
// SPDX-License-Identifier: CC0-1.0
RetroShare JSON API
===================
:Cxx: C&#43;&#43;
== How to use RetroShare JSON API
Look for methods marked with +@jsonapi+ doxygen custom command into
+libretroshare/src/retroshare+. The method path is composed by service instance
pointer name like +rsGxsChannels+ for +RsGxsChannels+, and the method name like
+createGroup+ and pass the input paramethers as a JSON object.
.Service instance pointer in rsgxschannels.h
[source,cpp]
--------------------------------------------------------------------------------
/**
* Pointer to global instance of RsGxsChannels service implementation
* @jsonapi{development}
*/
extern RsGxsChannels* rsGxsChannels;
--------------------------------------------------------------------------------
.Method declaration in rsgxschannels.h
[source,cpp]
--------------------------------------------------------------------------------
/**
* @brief Request channel creation.
* The action is performed asyncronously, so it could fail in a subsequent
* phase even after returning true.
* @jsonapi{development}
* @param[out] token Storage for RsTokenService token to track request
* status.
* @param[in] group Channel data (name, description...)
* @return false on error, true otherwise
*/
virtual bool createGroup(uint32_t& token, RsGxsChannelGroup& group) = 0;
--------------------------------------------------------------------------------
.paramethers.json
[source,json]
--------------------------------------------------------------------------------
{
"group":{
"mMeta":{
"mGroupName":"JSON test group",
"mGroupFlags":4,
"mSignFlags":520
},
"mDescription":"JSON test group description"
},
"caller_data":"Here can go any kind of JSON data (even objects) that the caller want to get back together with the response"
}
--------------------------------------------------------------------------------
.Calling the JSON API with curl on the terminal
[source,bash]
--------------------------------------------------------------------------------
curl -u $API_USER --data @paramethers.json http://127.0.0.1:9092/rsGxsChannels/createGroup
--------------------------------------------------------------------------------
.JSON API call result
[source,json]
--------------------------------------------------------------------------------
{
"caller_data": "Here can go any kind of JSON data (even objects) that the caller want to get back together with the response",
"retval": true,
"token": 3
}
--------------------------------------------------------------------------------
Even if it is less efficient because of URL encoding HTTP +GET+ method is
supported too, so in cases where the client cannot use +POST+ she can still use
+GET+ taking care of encoding the JSON data. With +curl+ this can be done at
least in two different ways.
.Calling the JSON API with GET method with curl on the terminal
[source,bash]
--------------------------------------------------------------------------------
curl -u $API_USER --get --data-urlencode jsonData@paramethers.json \
http://127.0.0.1:9092/rsGxsChannels/createGroup
--------------------------------------------------------------------------------
Letting +curl+ do the encoding is much more elegant but it is semantically
equivalent to the following.
.Calling the JSON API with GET method and pre-encoded data with curl on the terminal
--------------------------------------------------------------------------------
curl -u $API_USER http://127.0.0.1:9092/rsGxsChannels/createGroup?jsonData=%7B%0A%20%20%20%20%22group%22%3A%7B%0A%20%20%20%20%20%20%20%20%22mMeta%22%3A%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mGroupName%22%3A%22JSON%20test%20group%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mGroupFlags%22%3A4%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mSignFlags%22%3A520%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%22mDescription%22%3A%22JSON%20test%20group%20description%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22caller_data%22%3A%22Here%20can%20go%20any%20kind%20of%20JSON%20data%20%28even%20objects%29%20that%20the%20caller%20want%20to%20get%20back%20together%20with%20the%20response%22%0A%7D
--------------------------------------------------------------------------------
Note that using +GET+ method +?jsonData=+ and then the JSON data URL encoded are
added after the path in the HTTP request.
== JSON API authentication
Most of JSON API methods require authentication as they give access to
RetroShare user data, and we don't want any application running on the system
eventually by other users be able to access private data indiscriminately.
JSON API support HTTP Basic as authentication scheme, this is enough as JSON API
server is intented for usage on the same system (127.0.0.1) not over an
untrusted network.
If you need to use JSON API over an untrusted network consider using a reverse
proxy with HTTPS such as NGINX in front of JSON API server.
If RetroShare login has been effectuated through the JSON API you can use your
location SSLID as username and your PGP password as credential for the JSON API,
but we suggests you use specific meaningful and human readable credentials for
each JSON API client so the human user can have better control over which client
can access the JSON API.
.NewToken.json
[source,json]
--------------------------------------------------------------------------------
{
"token": "myNewUser:myNewPassword"
}
--------------------------------------------------------------------------------
.An authenticated client can authorize new tokens like this
--------------------------------------------------------------------------------
curl -u $API_USER --data @NewToken.json http://127.0.0.1:9092/jsonApiServer/authorizeToken
--------------------------------------------------------------------------------
.An unauthenticated JSON API client can request access with
--------------------------------------------------------------------------------
curl --data @NewToken.json http://127.0.0.1:9092/jsonApiServer/requestNewTokenAutorization
--------------------------------------------------------------------------------
When an unauthenticated client request his token to be authorized, JSON API
server will try to ask confirmation to the human user if possible through
+mNewAccessRequestCallback+, if it is not possible or the user didn't authorized
the token +false+ is returned.
== Offer new RetroShare services through JSON API
To offer a retroshare service through the JSON API, first of all one need find
the global pointer to the service instance and document it in doxygen syntax,
plus marking with the custom doxygen command +@jsonapi{RS_VERSION}+ where
+RS_VERSION+ is the retroshare version in which this service became available
with the current semantic (major changes to the service semantic, changes the
meaning of the service itself, so the version should be updated in the
documentation in that case).
.Service instance pointer in rsgxschannels.h
[source,cpp]
--------------------------------------------------------------------------------
/**
* Pointer to global instance of RsGxsChannels service implementation
* @jsonapi{development}
*/
extern RsGxsChannels* rsGxsChannels;
--------------------------------------------------------------------------------
Once the service instance itself is known to the JSON API you need to document
in doxygen syntax and mark with the custom doxygen command
+@jsonapi{RS_VERSION}+ the methods of the service that you want to make
available through JSON API.
.Offering RsGxsChannels::getChannelDownloadDirectory in rsgxschannels.h
[source,cpp]
--------------------------------------------------------------------------------
/**
* Get download directory for the given channel
* @jsonapi{development}
* @param[in] channelId id of the channel
* @param[out] directory reference to string where to store the path
* @return false on error, true otherwise
*/
virtual bool getChannelDownloadDirectory( const RsGxsGroupId& channelId,
std::string& directory ) = 0;
--------------------------------------------------------------------------------
For each paramether you must specify if it is used as input +@param[in]+ as
output +@param[out]+ or both +@param[inout]+. Paramethers and return value
types must be of a type supported by +RsTypeSerializer+ which already support
most basic types (+bool+, +std::string+...), +RsSerializable+ and containers of
them like +std::vector<std::string>+. Paramethers passed by value and by
reference of those types are both supported, while passing by pointer is not
supported. If your paramether or return +class+/+struct+ type is not supported
yet by +RsTypeSerializer+ most convenient approach is to make it derive from
+RsSerializable+ and implement +serial_process+ method like I did with
+RsGxsChannelGroup+.
.Deriving RsGxsChannelGroup from RsSerializable in rsgxschannels.h
[source,cpp]
--------------------------------------------------------------------------------
struct RsGxsChannelGroup : RsSerializable
{
RsGroupMetaData mMeta;
std::string mDescription;
RsGxsImage mImage;
bool mAutoDownload;
/// @see RsSerializable
virtual void serial_process( RsGenericSerializer::SerializeJob j,
RsGenericSerializer::SerializeContext& ctx )
{
RS_SERIAL_PROCESS(mMeta);
RS_SERIAL_PROCESS(mDescription);
RS_SERIAL_PROCESS(mImage);
RS_SERIAL_PROCESS(mAutoDownload);
}
};
--------------------------------------------------------------------------------
You can do the same recursively for any member of your +struct+ that is not yet
supported by +RsTypeSerializer+.
Some Retroshare {Cxx} API functions are asyncronous, historically RetroShare
didn't follow a policy on how to expose asyncronous API so differents services
and some times even differents method of the same service follow differents
asyncronous patterns, thus making automatic generation of JSON API wrappers for
those methods impractical. Instead of dealing with all those differents patterns
I have chosed to support only one new pattern taking advantage of modern {Cxx}11
and restbed features. On the {Cxx}11 side lambdas and +std::function+s are used,
on the restbed side Server Side Events are used to send asyncronous results.
Lets see an example so it will be much esier to understand.
.RsGxsChannels::turtleSearchRequest asyncronous API
[source,cpp]
--------------------------------------------------------------------------------
/**
* @brief Request remote channels search
* @jsonapi{development}
* @param[in] matchString string to look for in the search
* @param multiCallback function that will be called each time a search
* result is received
* @param[in] maxWait maximum wait time in seconds for search results
* @return false on error, true otherwise
*/
virtual bool turtleSearchRequest(
const std::string& matchString,
const std::function<void (const RsGxsGroupSummary& result)>& multiCallback,
std::time_t maxWait = 300 ) = 0;
--------------------------------------------------------------------------------
+RsGxsChannels::turtleSearchRequest(...)+ is an asyncronous method because it
send a channel search request on turtle network and then everytime a result is
received from the network +multiCallback+ is called and the result is passed as
parameter. To be supported by the automatic JSON API wrappers generator an
asyncronous method need a parameter of type +std::function<void (...)>+ called
+callback+ if the callback will be called only once or +multiCallback+ if the
callback is expected to be called more then once like in this case.
A second mandatory parameter is +maxWait+ of type +std::time_t+ it indicates the
maximum amount of time in seconds for which the caller is willing to wait for
results, in case the timeout is reached the callback will not be called anymore.
[IMPORTANT]
================================================================================
+callback+ and +multiCallback+ parameters documentation must *not* specify
+[in]+, +[out]+, +[inout]+, in Doxygen documentation as this would fool the
automatic wrapper generator, and ultimately break the compilation.
================================================================================
.RsFiles::turtleSearchRequest asyncronous JSON API usage example
[source,bash]
--------------------------------------------------------------------------------
$ cat turtle_search.json
{
"matchString":"linux"
}
$ curl --data @turtle_search.json http://127.0.0.1:9092/rsFiles/turtleSearchRequest
data: {"retval":true}
data: {"results":[{"size":157631,"hash":"69709b4d01025584a8def5cd78ebbd1a3cf3fd05","name":"kill_bill_linux_1024x768.jpg"},{"size":192560,"hash":"000000000000000000009a93e5be8486c496f46c","name":"coffee_box_linux2.jpg"},{"size":455087,"hash":"9a93e5be8486c496f46c00000000000000000000","name":"Linux.png"},{"size":182004,"hash":"e8845280912ebf3779e400000000000000000000","name":"Linux_2_6.png"}]}
data: {"results":[{"size":668,"hash":"e8845280912ebf3779e400000000000000000000","name":"linux.png"},{"size":70,"hash":"e8845280912ebf3779e400000000000000000000","name":"kali-linux-2016.2-amd64.txt.sha1sum"},{"size":3076767744,"hash":"e8845280912ebf3779e400000000000000000000","name":"kali-linux-2016.2-amd64.iso"},{"size":2780872,"hash":"e8845280912ebf3779e400000000000000000000","name":"openwrt-ar71xx-generic-vmlinux.bin"},{"size":917504,"hash":"e8845280912ebf3779e400000000000000000000","name":"openwrt-ar71xx-generic-vmlinux.lzma"},{"size":2278404096,"hash":"e8845280912ebf3779e400000000000000000000","name":"gentoo-linux-livedvd-amd64-multilib-20160704.iso"},{"size":151770333,"hash":"e8845280912ebf3779e400000000000000000000","name":"flashtool-0.9.23.0-linux.tar.7z"},{"size":2847372,"hash":"e8845280912ebf3779e400000000000000000000","name":"openwrt-ar71xx-generic-vmlinux.elf"},{"size":1310720,"hash":"e8845280912ebf3779e400000000000000000000","name":"openwrt-ar71xx-generic-vmlinux.gz"},{"size":987809,"hash":"e8845280912ebf3779e400000000000000000000","name":"openwrt-ar71xx-generic-vmlinux-lzma.elf"}]}
--------------------------------------------------------------------------------
By default JSON API methods requires client authentication and their wrappers
are automatically generated by +json-api-generator+.
In some cases methods need do be accessible without authentication such as
+rsLoginHelper/getLocations+ so in the doxygen documentaion they have the custom
command +@jsonapi{RS_VERSION,unauthenticated}+.
Other methods such as +/rsControl/rsGlobalShutDown+ need special care so they
are marked with the custom doxygen command +@jsonapi{RS_VERSION,manualwrapper}+
and their wrappers are not automatically generated but written manually into
+JsonApiServer::JsonApiServer(...)+.
== Quirks
=== 64 bits integers handling
While JSON doesn't have problems representing 64 bits integers JavaScript, Dart
and other languages are not capable to handle those numbers natively.
To overcome this limitation JSON API output 64 bit integers as an object with
two keys, one as proper integer and one as string representation.
.JSON API 64 bit integer output example
[source,json]
--------------------------------------------------------------------------------
"lobby_id": { "xint64": 6215642878098695544, "xstr64": "6215642878098695544" }
--------------------------------------------------------------------------------
So from languages that have proper 64bit integers support like Python or C++ one
better read from `xint64` which is represented as a JSON integer, from languages
where there is no proper 64bit integers support like JavaScript one can read from
`xstr64` which is represented as JSON string (note that the first is not wrapped
in "" while the latter is).
When one input a 64bit integer into the JSON API it first try to parse it as if
it was sent the old way for retrocompatibility.
.JSON API 64 bit integer deprecated format input example
[source,json]
--------------------------------------------------------------------------------
"lobby_id":6215642878098695544
--------------------------------------------------------------------------------
This way is *DEPRECATED* and may disappear in the future, it is TEMPORALLY kept
only for retrocompatibiliy with old clients.
If retrocompatible parsing attempt fail then it try to parse with the new way
with proper JSON integer format.
.JSON API 64 bit integer new proper integer format input example
[source,json]
--------------------------------------------------------------------------------
lobby_id": { "xint64": 6215642878098695544 }
--------------------------------------------------------------------------------
If this fails then it try to parse with the new way with JSON string format.
.JSON API 64 bit integer new string format input example
[source,json]
--------------------------------------------------------------------------------
"lobby_id": { "xstr64": "6215642878098695544" }
--------------------------------------------------------------------------------
[WARNING]
================================================================================
Clients written in languages without proper 64bit integers support must
use *ONLY* the string format otherwise they will send approximated values and
get unexpected results from the JSON API, because parsing will success but the
value will not be exactly the one you believe you sent.
================================================================================
== A bit of history
=== First writings about this
The previous attempt of exposing a RetroShare JSON API is called +libresapi+ and
unfortunatley it requires a bunch of boilerplate code when we want to expose
something present in the {Cxx} API in the JSON API.
As an example here you can see the libresapi that exposes part of the retroshare
chat {Cxx} API and lot of boilerplate code just to convert {Cxx} objects to JSON
https://github.com/RetroShare/RetroShare/blob/v0.6.4/libresapi/src/api/ChatHandler.cpp#L44
To avoid the {Cxx} to JSON and back conversion boilerplate code I have worked out
an extension to our {Cxx} serialization code so it is capable to serialize and
deserialize to JSON you can see it in this pull request
https://github.com/RetroShare/RetroShare/pull/1155
So first step toward having a good API is to take advantage of the fact that RS
is now capable of converting C++ objects from and to JSON.
The current API is accessible via HTTP and unix socket, there is no
authentication in both of them, so anyone having access to the HTTP server or to
the unix socket can access the API without extra restrictions.
Expecially for the HTTP API this is a big risk because also if the http server
listen on 127.0.0.1 every application on the machine (even rogue javascript
running on your web browser) can access that and for example on android it is
not safe at all (because of that I implemented the unix socket access so at
least in android API was reasonably safe) because of this.
A second step to improve the API would be to implement some kind of API
authentication mechanism (it would be nice that the mechanism is handled at API
level and not at transport level so we can use it for any API trasport not just
HTTP for example)
The HTTP server used by libresapi is libmicrohttpd server that is very minimal,
it doesn't provide HTTPS nor modern HTTP goodies, like server notifications,
websockets etc. because the lack of support we have a token polling mechanism in
libresapi to avoid polling for every thing but it is still ugly, so if we can
completely get rid of polling in the API that would be really nice.
I have done a crawl to look for a replacement and briefly looked at
- https://www.gnu.org/software/libmicrohttpd/
- http://wolkykim.github.io/libasyncd/
- https://github.com/corvusoft/restbed
- https://code.facebook.com/posts/1503205539947302/introducing-proxygen-facebook-s-c-http-framework/
- https://github.com/cmouse/yahttp
taking in account a few metrics like modern HTTP goodies support, license,
platform support, external dependencies and documentation it seemed to me that
restbed is the more appropriate.
Another source of boilerplate code into libresapi is the mapping between JSON
API requests and C++ API methods as an example you can look at this
https://github.com/RetroShare/RetroShare/blob/v0.6.4/libresapi/src/api/ChatHandler.cpp#L158
and this
https://github.com/RetroShare/RetroShare/blob/v0.6.4/libresapi/src/api/ApiServer.cpp#L253
The abstract logic of this thing is, when libreasapi get a request like
+/chat/initiate_distant_chat+ then call
+ChatHandler::handleInitiateDistantChatConnexion+ which in turn is just a
wrapper of +RsMsgs::initiateDistantChatConnexion+ all this process is basically
implemented as boilerplate code and would be unnecessary in a smarter design of
the API because almost all the information needed is already present in the
C++ API +libretroshare/src/retroshare+.
So a third step to improve the JSON API would be to remove this source of
boilerplate code by automatizing the mapping between C++ and JSON API call.
This may result a little tricky as language parsing or other adevanced things
may be required.
Hope this dive is useful for you +
Cheers +
G10h4ck
=== Second writings about this
I have been investigating a bit more about:
[verse, G10h4ck]
________________________________________________________________________________
So a third step to improve the JSON API would be to remove this source of
boilerplate code by automatizing the mapping between C++ and JSON API call
________________________________________________________________________________
After spending some hours investigating this topic the most reasonable approach
seems to:
1. Properly document headers in +libretroshare/src/retroshare/+ in doxygen syntax
specifying wihich params are input and/or output (doxygen sysntax for this is
+@param[in/out/inout]+) this will be the API documentation too.
2. At compile time use doxygen to generate XML description of the headers and use
the XML to generate the JSON api server stub.
http://www.stack.nl/~dimitri/doxygen/manual/customize.html#xmlgenerator
3. Enjoy

View File

@ -0,0 +1,80 @@
/*******************************************************************************
* RetroShare JSON API *
* *
* Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License version 3 as *
* published by the Free Software Foundation. *
* *
* 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 Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
registerHandler( "$%apiPath%$",
[this](const std::shared_ptr<rb::Session> session)
{
const std::multimap<std::string, std::string> headers
{
{ "Connection", "keep-alive" },
{ "Content-Type", "text/event-stream" }
};
session->yield(rb::OK, headers);
size_t reqSize = session->get_request()->get_header("Content-Length", 0);
session->fetch( reqSize, [this](
const std::shared_ptr<rb::Session> session,
const rb::Bytes& body )
{
INITIALIZE_API_CALL_JSON_CONTEXT;
if( !checkRsServicePtrReady(
$%instanceName%$, "$%instanceName%$", cAns, session ) )
return;
$%paramsDeclaration%$
$%inputParamsDeserialization%$
const std::weak_ptr<rb::Service> weakService(mService);
const std::weak_ptr<rb::Session> weakSession(session);
$%callbackName%$ = [weakService, weakSession]($%callbackParams%$)
{
auto session = weakSession.lock();
if(!session || session->is_closed()) return;
auto lService = weakService.lock();
if(!lService || lService->is_down()) return;
$%callbackParamsSerialization%$
std::stringstream sStream;
sStream << "data: " << compactJSON << ctx.mJson << "\n\n";
const std::string message = sStream.str();
lService->schedule( [weakSession, message]()
{
auto session = weakSession.lock();
if(!session || session->is_closed()) return;
session->yield(message);
$%sessionEarlyClose%$
} );
};
$%functionCall%$
$%outputParamsSerialization%$
// return them to the API caller
std::stringstream message;
message << "data: " << compactJSON << cAns.mJson << "\n\n";
session->yield(message.str());
$%sessionDelayedClose%$
} );
}, $%requiresAuth%$ );

View File

@ -0,0 +1,230 @@
DOXYFILE_ENCODING = UTF-8
PROJECT_NAME = "libretroshare"
ALIASES += jsonapi{1}="\xmlonly<jsonapi minversion=\"\1\"/>\endxmlonly"
ALIASES += jsonapi{2}="\xmlonly<jsonapi minversion=\"\1\" access=\"\2\"/>\endxmlonly"
# If the INHERIT_DOCS tag is set to YES then an undocumented member inherits the
# documentation from any documented member that it re-implements.
# The default value is: YES.
INHERIT_DOCS = YES
# If the MARKDOWN_SUPPORT tag is enabled then doxygen pre-processes all comments
# according to the Markdown format, which allows for more readable
# documentation. See http://daringfireball.net/projects/markdown/ for details.
# The output of markdown processing is further processed by doxygen, so you can
# mix doxygen, HTML, and XML commands with Markdown formatting. Disable only in
# case of backward compatibilities issues.
# The default value is: YES.
MARKDOWN_SUPPORT = YES
# When enabled doxygen tries to link words that correspond to documented
# classes, or namespaces to their corresponding documentation. Such a link can
# be prevented in individual cases by putting a % sign in front of the word or
# globally by setting AUTOLINK_SUPPORT to NO.
# The default value is: YES.
AUTOLINK_SUPPORT = YES
# If the EXTRACT_ALL tag is set to YES, doxygen will assume all entities in
# documentation are documented, even if no documentation was available. Private
# class members and static file members will be hidden unless the
# EXTRACT_PRIVATE respectively EXTRACT_STATIC tags are set to YES.
# Note: This will also disable the warnings about undocumented members that are
# normally produced when WARNINGS is set to YES.
# The default value is: NO.
EXTRACT_ALL = YES
# If the HIDE_UNDOC_MEMBERS tag is set to YES, doxygen will hide all
# undocumented members inside documented classes or files. If set to NO these
# members will be included in the various overviews, but no documentation
# section is generated. This option has no effect if EXTRACT_ALL is enabled.
# The default value is: NO.
HIDE_UNDOC_MEMBERS = NO
# If the HIDE_UNDOC_CLASSES tag is set to YES, doxygen will hide all
# undocumented classes that are normally visible in the class hierarchy. If set
# to NO, these classes will be included in the various overviews. This option
# has no effect if EXTRACT_ALL is enabled.
# The default value is: NO.
HIDE_UNDOC_CLASSES = NO
#---------------------------------------------------------------------------
# Configuration options related to the input files
#---------------------------------------------------------------------------
# The INPUT tag is used to specify the files and/or directories that contain
# documented source files. You may enter file names like myfile.cpp or
# directories like /usr/src/myproject. Separate the files or directories with
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
# Note: If this tag is empty the current directory is searched.
#INPUT =
# This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
# libiconv (or the iconv built into libc) for the transcoding. See the libiconv
# documentation (see: https://www.gnu.org/software/libiconv/) for the list of
# possible encodings.
# The default value is: UTF-8.
INPUT_ENCODING = UTF-8
# If the value of the INPUT tag contains directories, you can use the
# FILE_PATTERNS tag to specify one or more wildcard patterns (like *.cpp and
# *.h) to filter out the source-files in the directories.
#
# Note that for custom extensions or not directly supported extensions you also
# need to set EXTENSION_MAPPING for the extension otherwise the files are not
# read by doxygen.
#
# If left blank the following patterns are tested:*.c, *.cc, *.cxx, *.cpp,
# *.c++, *.java, *.ii, *.ixx, *.ipp, *.i++, *.inl, *.idl, *.ddl, *.odl, *.h,
# *.hh, *.hxx, *.hpp, *.h++, *.cs, *.d, *.php, *.php4, *.php5, *.phtml, *.inc,
# *.m, *.markdown, *.md, *.mm, *.dox, *.py, *.pyw, *.f90, *.f95, *.f03, *.f08,
# *.f, *.for, *.tcl, *.vhd, *.vhdl, *.ucf and *.qsf.
FILE_PATTERNS = *.c \
*.cc \
*.cxx \
*.cpp \
*.c++ \
*.java \
*.ii \
*.ixx \
*.ipp \
*.i++ \
*.inl \
*.idl \
*.ddl \
*.odl \
*.h \
*.hh \
*.hxx \
*.hpp \
*.h++ \
*.cs \
*.d \
*.php \
*.php4 \
*.php5 \
*.phtml \
*.inc \
*.m \
*.markdown \
*.md \
*.mm \
*.dox \
*.py \
*.pyw \
*.f90 \
*.f95 \
*.f03 \
*.f08 \
*.f \
*.for \
*.tcl \
*.vhd \
*.vhdl \
*.ucf \
*.qsf
# The RECURSIVE tag can be used to specify whether or not subdirectories should
# be searched for input files as well.
# The default value is: NO.
RECURSIVE = YES
# The EXCLUDE tag can be used to specify files and/or directories that should be
# excluded from the INPUT source files. This way you can easily exclude a
# subdirectory from a directory tree whose root is specified with the INPUT tag.
#
# Note that relative paths are relative to the directory from which doxygen is
# run.
EXCLUDE =
# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
# directories that are symbolic links (a Unix file system feature) are excluded
# from the input.
# The default value is: NO.
EXCLUDE_SYMLINKS = NO
# If the value of the INPUT tag contains directories, you can use the
# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude
# certain files from those directories.
#
# Note that the wildcards are matched against the file with absolute path, so to
# exclude all test directories for example use the pattern */test/*
EXCLUDE_PATTERNS =
# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names
# (namespaces, classes, functions, etc.) that should be excluded from the
# output. The symbol name can be a fully qualified name, a word, or if the
# wildcard * is used, a substring. Examples: ANamespace, AClass,
# AClass::ANamespace, ANamespace::*Test
#
# Note that the wildcards are matched against the file with absolute path, so to
# exclude all test directories use the pattern */test/*
EXCLUDE_SYMBOLS =
#---------------------------------------------------------------------------
# Configuration options related to the HTML output
#---------------------------------------------------------------------------
# If the GENERATE_HTML tag is set to YES, doxygen will generate HTML output
# The default value is: YES.
GENERATE_HTML = NO
# If the GENERATE_LATEX tag is set to YES, doxygen will generate LaTeX output.
# The default value is: YES.
GENERATE_LATEX = NO
# If the GENERATE_XML tag is set to YES, doxygen will generate an XML file that
# captures the structure of the code including all documentation.
# The default value is: NO.
GENERATE_XML = YES
#---------------------------------------------------------------------------
# Configuration options related to the preprocessor
#---------------------------------------------------------------------------
# If the ENABLE_PREPROCESSING tag is set to YES, doxygen will evaluate all
# C-preprocessor directives found in the sources and include files.
# The default value is: YES.
ENABLE_PREPROCESSING = YES
# If the MACRO_EXPANSION tag is set to YES, doxygen will expand all macro names
# in the source code. If set to NO, only conditional compilation will be
# performed. Macro expansion can be done in a controlled way by setting
# EXPAND_ONLY_PREDEF to YES.
# The default value is: NO.
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
MACRO_EXPANSION = NO
# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES then
# the macro expansion is limited to the macros specified with the PREDEFINED and
# EXPAND_AS_DEFINED tags.
# The default value is: NO.
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
EXPAND_ONLY_PREDEF = NO

View File

@ -0,0 +1,365 @@
#!/usr/bin/python3
# RetroShare JSON API generator
#
# Copyright (C) 2019 selankon <selankon@selankon.xyz>
# Copyright (C) 2021 Gioacchino Mazzurco <gio@eigenlab.org>
# Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation, version 3.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>
#
# SPDX-FileCopyrightText: Retroshare Team <contact@retroshare.cc>
# SPDX-License-Identifier: AGPL-3.0-only
# Original idea and implementation by G10h4ck (jsonapi-generator.cpp)
# Initial python reimplementation by Sehraf
#
# This python 3 script has superseded the original C++/Qt implementation this
# and is now used at build time in without depending on Qt.
import os
import sys
import xml.etree.ElementTree as ET
from string import Template
class MethodParam:
_type = ''
_name = ''
_defval = ''
_in = False
_out = False
_isMultiCallback = False
_isSingleCallback = False
class TemplateOwn(Template):
delimiter = '$%'
pattern = '''
\$%(?:
(?P<escaped>\$\%) | # Escape sequence of two delimiters
(?P<named>[_a-z][_a-z0-9]*)%\$ | # delimiter and a Python identifier
{(?P<braced>[_a-z][_a-z0-9]*)} | # delimiter and a braced identifier
(?P<invalid>) # Other ill-formed delimiter exprs
)
'''
def getText(e):
return "".join(e.itertext())
def processFile(file):
try:
dom1 = ET.parse(file).getroot()
except FileNotFoundError:
print('Can\'t open:', file)
headerFileInfo = dom1[0].findall('location')[0].attrib['file']
headerRelPath = os.path.dirname(headerFileInfo).split('/')[-1] + '/' + os.path.basename(headerFileInfo)
for sectDef in dom1.findall('.//memberdef'):
if sectDef.attrib['kind'] != 'variable' or sectDef.find('.//jsonapi') == None:
continue
instanceName = sectDef.find('name').text
typeName = sectDef.find('type/ref').text
typeFilePath = sectDef.find('type/ref').attrib['refid']
try:
dom2 = ET.parse(doxPrefix + typeFilePath + '.xml').getroot()
except FileNotFoundError:
print('Can\'t open:', doxPrefix + typeFilePath + '.xml')
for member in dom2.findall('.//member'):
refid = member.attrib['refid']
methodName = member.find('name').text
requiresAuth = True
defFilePath = refid.split('_')[0] + '.xml'
defFile = defFilePath
print('Looking for', typeName, methodName, 'into', typeFilePath)
try:
defDoc = ET.parse(doxPrefix + defFilePath).getroot()
except FileNotFoundError:
print('Can\'t open:', doxPrefix + defFilePath)
memberdef = None
for tmpMBD in defDoc.findall('.//memberdef'):
tmpId = tmpMBD.attrib['id']
tmpKind = tmpMBD.attrib['kind']
tmpJsonApiTagList = tmpMBD.findall('.//jsonapi')
if len(tmpJsonApiTagList) != 0 and tmpId == refid and tmpKind == 'function':
tmpJsonApiTag = tmpJsonApiTagList[0]
tmpAccessValue = None
if 'access' in tmpJsonApiTag.attrib:
tmpAccessValue = tmpJsonApiTag.attrib['access']
requiresAuth = 'unauthenticated' != tmpAccessValue;
if 'manualwrapper' != tmpAccessValue:
memberdef = tmpMBD
break
if memberdef == None:
continue
apiPath = '/' + instanceName + '/' + methodName
retvalType = getText(memberdef.find('type'))
# Apparently some xml declarations include new lines ('\n') and/or multiple spaces
# Strip them using python magic
retvalType = ' '.join(retvalType.split())
paramsMap = {}
orderedParamNames = []
hasInput = False
hasOutput = False
hasSingleCallback = False
hasMultiCallback = False
callbackName = ''
callbackParams = ''
for tmpPE in memberdef.findall('param'):
mp = MethodParam()
pName = getText(tmpPE.find('declname'))
tmpDefval = tmpPE.find('defval')
mp._defval = getText(tmpDefval) if tmpDefval != None else ''
pType = getText(tmpPE.find('type'))
if pType.startswith('const '): pType = pType[6:]
if pType.startswith('std::function'):
if pType.endswith('&'): pType = pType[:-1]
if pName.startswith('multiCallback'):
mp._isMultiCallback = True
hasMultiCallback = True
elif pName.startswith('callback'):
mp._isSingleCallback = True
hasSingleCallback = True
callbackName = pName
callbackParams = pType
else:
pType = pType.replace('&', '').replace(' ', '')
# Apparently some xml declarations include new lines ('\n') and/or multiple spaces
# Strip them using python magic
pType = ' '.join(pType.split())
mp._defval = ' '.join(mp._defval.split())
mp._type = pType
mp._name = pName
paramsMap[pName] = mp
orderedParamNames.append(pName)
for tmpPN in memberdef.findall('.//parametername'):
tmpParam = paramsMap[tmpPN.text]
tmpD = tmpPN.attrib['direction'] if 'direction' in tmpPN.attrib else ''
if 'in' in tmpD:
tmpParam._in = True
hasInput = True
if 'out' in tmpD:
tmpParam._out = True
hasOutput = True
# Params sanity check
for pmKey in paramsMap:
pm = paramsMap[pmKey]
if not (pm._isMultiCallback or pm._isSingleCallback or pm._in or pm._out):
print('ERROR', 'Parameter:', pm._name, 'of:', apiPath,
'declared in:', headerRelPath,
'miss doxygen parameter direction attribute!',
defFile)
sys.exit()
functionCall = '\t\t'
if retvalType != 'void':
functionCall += retvalType + ' retval = '
hasOutput = True
functionCall += instanceName + '->' + methodName + '('
functionCall += ', '.join(orderedParamNames) + ');\n'
print(instanceName, apiPath, retvalType, typeName, methodName)
for pn in orderedParamNames:
mp = paramsMap[pn]
print('\t', mp._type, mp._name, mp._in, mp._out)
inputParamsDeserialization = ''
if hasInput:
inputParamsDeserialization += '\t\t{\n'
inputParamsDeserialization += '\t\t\tRsGenericSerializer::SerializeContext& ctx(cReq);\n'
inputParamsDeserialization += '\t\t\tRsGenericSerializer::SerializeJob j(RsGenericSerializer::FROM_JSON);\n';
outputParamsSerialization = ''
if hasOutput:
outputParamsSerialization += '\t\t{\n'
outputParamsSerialization += '\t\t\tRsGenericSerializer::SerializeContext& ctx(cAns);\n'
outputParamsSerialization += '\t\t\tRsGenericSerializer::SerializeJob j(RsGenericSerializer::TO_JSON);\n';
paramsDeclaration = ''
for pn in orderedParamNames:
mp = paramsMap[pn]
paramsDeclaration += '\t\t' + mp._type + ' ' + mp._name
if mp._defval != '':
paramsDeclaration += ' = ' + mp._defval
paramsDeclaration += ';\n'
if mp._in:
inputParamsDeserialization += '\t\t\tRS_SERIAL_PROCESS('
inputParamsDeserialization += mp._name + ');\n'
if mp._out:
outputParamsSerialization += '\t\t\tRS_SERIAL_PROCESS('
outputParamsSerialization += mp._name + ');\n'
if hasInput:
inputParamsDeserialization += '\t\t}\n'
if retvalType != 'void':
outputParamsSerialization += '\t\t\tRS_SERIAL_PROCESS(retval);\n'
if hasOutput:
outputParamsSerialization += '\t\t}\n'
captureVars = ''
sessionEarlyClose = ''
if hasSingleCallback:
sessionEarlyClose = 'session->close();'
sessionDelayedClose = ''
if hasMultiCallback:
sessionDelayedClose = """
RsThread::async( [=]()
{
std::this_thread::sleep_for(
std::chrono::seconds(maxWait+120) );
auto lService = weakService.lock();
if(!lService || lService->is_down()) return;
lService->schedule( [=]()
{
auto session = weakSession.lock();
if(session && session->is_open())
session->close();
} );
} );
"""
captureVars = 'this'
callbackParamsSerialization = ''
if hasSingleCallback or hasMultiCallback or (callbackParams.find('(') + 2 < callbackParams.find(')')):
cbs = ''
callbackParams = callbackParams.split('(')[1]
callbackParams = callbackParams.split(')')[0]
cbs += '\t\t\tRsGenericSerializer::SerializeContext ctx;\n'
for cbPar in callbackParams.split(','):
isConst = cbPar.startswith('const ')
pSep = ' '
isRef = '&' in cbPar
if isRef: pSep = '&'
sepIndex = cbPar.rfind(pSep) + 1
cpt = cbPar[0:sepIndex][6:]
cpn = cbPar[sepIndex:]
cbs += '\t\t\tRsTypeSerializer::serial_process('
cbs += 'RsGenericSerializer::TO_JSON, ctx, '
if isConst:
cbs += 'const_cast<'
cbs += cpt
cbs += '>('
cbs += cpn
if isConst: cbs += ')'
cbs += ', "'
cbs += cpn
cbs += '" );\n'
callbackParamsSerialization += cbs
substitutionsMap = dict()
substitutionsMap['paramsDeclaration'] = paramsDeclaration
substitutionsMap['inputParamsDeserialization'] = inputParamsDeserialization
substitutionsMap['outputParamsSerialization'] = outputParamsSerialization
substitutionsMap['instanceName'] = instanceName
substitutionsMap['functionCall'] = functionCall
substitutionsMap['apiPath'] = apiPath
substitutionsMap['sessionEarlyClose'] = sessionEarlyClose
substitutionsMap['sessionDelayedClose'] = sessionDelayedClose
substitutionsMap['captureVars'] = captureVars
substitutionsMap['callbackName'] = callbackName
substitutionsMap['callbackParams'] = callbackParams
substitutionsMap['callbackParamsSerialization'] = callbackParamsSerialization
substitutionsMap['requiresAuth'] = 'true' if requiresAuth else 'false'
# print(substitutionsMap)
templFilePath = sourcePath
if hasMultiCallback or hasSingleCallback:
templFilePath += '/async-method-wrapper-template.cpp.tmpl'
else:
templFilePath += '/method-wrapper-template.cpp.tmpl'
templFile = open(templFilePath, 'r')
wrapperDef = TemplateOwn(templFile.read())
tmp = wrapperDef.substitute(substitutionsMap)
wrappersDefFile.write(tmp)
cppApiIncludesSet.add('#include "' + headerRelPath + '"\n')
if len(sys.argv) != 3:
print('Usage:', sys.argv[0], 'SOURCE_PATH OUTPUT_PATH Got:', sys.argv[:])
sys.exit(-1)
sourcePath = str(sys.argv[1])
outputPath = str(sys.argv[2])
doxPrefix = outputPath + '/xml/'
try:
wrappersDefFile = open(outputPath + '/jsonapi-wrappers.inl', 'w')
except FileNotFoundError:
print('Can\'t open:', outputPath + '/jsonapi-wrappers.inl')
try:
cppApiIncludesFile = open(outputPath + '/jsonapi-includes.inl', 'w');
except FileNotFoundError:
print('Can\'t open:', outputPath + '/jsonapi-includes.inl')
cppApiIncludesSet = set()
filesIterator = None
try:
filesIterator = os.listdir(doxPrefix)
except FileNotFoundError:
print("Doxygen xml output dir not found: ", doxPrefix)
os.exit(-1)
for file in filesIterator:
if file.endswith("8h.xml"):
processFile(os.path.join(doxPrefix, file))
for incl in cppApiIncludesSet:
cppApiIncludesFile.write(incl)

View File

@ -796,8 +796,9 @@ void JsonApiServer::run()
}
catch(std::exception& e)
{
RsErr() << __PRETTY_FUNCTION__ << " Failure starting JSON API server: "
<< e.what() << std::endl;
/* TODO: find a way to report back programmatically if failed listening
* port */
RS_ERR("Failure starting JSON API server: ", e.what());
print_stacktrace();
return;
}

View File

@ -0,0 +1,50 @@
/*******************************************************************************
* RetroShare JSON API *
* *
* Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero 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 Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
registerHandler( "$%apiPath%$",
[](const std::shared_ptr<rb::Session> session)
{
size_t reqSize = session->get_request()->get_header("Content-Length", 0);
session->fetch( reqSize, [](
const std::shared_ptr<rb::Session> session,
const rb::Bytes& body )
{
INITIALIZE_API_CALL_JSON_CONTEXT;
if( !checkRsServicePtrReady(
$%instanceName%$, "$%instanceName%$", cAns, session ) )
return;
$%paramsDeclaration%$
// deserialize input parameters from JSON
$%inputParamsDeserialization%$
// call retroshare C++ API
$%functionCall%$
// serialize out parameters and return value to JSON
$%outputParamsSerialization%$
// return them to the API caller
DEFAULT_API_CALL_JSON_RETURN(rb::OK);
} );
}, $%requiresAuth%$ );

View File

@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: (C) 2004-2019 Retroshare Team <contact@retroshare.cc>
# SPDX-FileCopyrightText: (C) 2004-2021 Retroshare Team <contact@retroshare.cc>
# SPDX-License-Identifier: CC0-1.0
!include("../../retroshare.pri"): error("Could not include file ../../retroshare.pri")
@ -9,13 +9,15 @@ libretroshare_shared {
} else {
CONFIG += staticlib
}
CONFIG -= qt
TARGET = retroshare
TARGET_PRL = libretroshare
DESTDIR = lib
!include("use_libretroshare.pri"):error("Including")
QMAKE_CXXFLAGS += -fPIC
# treat warnings as error for better removing
#QMAKE_CFLAGS += -Werror
#QMAKE_CXXFLAGS += -Werror
@ -206,7 +208,7 @@ linux-* {
LIBS *= -ldl
DEFINES *= PLUGIN_DIR=\"\\\"$${PLUGIN_DIR}\\\"\"
DEFINES *= DATA_DIR=\"\\\"$${DATA_DIR}\\\"\"
DEFINES *= RS_DATA_DIR=\"\\\"$${RS_DATA_DIR}\\\"\"
}
linux-g++ {
@ -290,7 +292,7 @@ mac {
#LIBS += -lsqlite3
DEFINES *= PLUGIN_DIR=\"\\\"$${PLUGIN_DIR}\\\"\"
DEFINES *= DATA_DIR=\"\\\"$${DATA_DIR}\\\"\"
DEFINES *= RS_DATA_DIR=\"\\\"$${RS_DATA_DIR}\\\"\"
}
################################# FreeBSD ##########################################
@ -364,6 +366,7 @@ HEADERS += chat/distantchat.h \
HEADERS += pqi/authssl.h \
pqi/authgpg.h \
pgp/pgphandler.h \
pgp/openpgpsdkhandler.h \
pgp/pgpkeyutil.h \
pqi/pqifdbin.h \
pqi/rstcpsocket.h \
@ -447,7 +450,7 @@ HEADERS += rsitems/rsitem.h \
serialiser/rstlvkeyvalue.h \
serialiser/rstlvgenericparam.h \
serialiser/rstlvgenericmap.h \
serialiser/rstlvgenericmap.inl \
serialiser/rstlvgenericmap.inl \
serialiser/rstlvlist.h \
serialiser/rstlvmaps.h \
serialiser/rstlvbanlist.h \
@ -489,6 +492,7 @@ HEADERS += util/folderiterator.h \
util/rsmemory.h \
util/smallobject.h \
util/rsdir.h \
util/rsfile.h \
util/argstream.h \
util/rsdiscspace.h \
util/rsnet.h \
@ -514,7 +518,8 @@ HEADERS += util/folderiterator.h \
util/cxx11retrocompat.h \
util/cxx14retrocompat.h \
util/cxx17retrocompat.h \
util/rsurl.h
util/cxx23retrocompat.h \
util/rsurl.h
SOURCES += ft/ftchunkmap.cc \
ft/ftcontroller.cc \
@ -541,6 +546,7 @@ SOURCES += chat/distantchat.cc \
SOURCES += pqi/authgpg.cc \
pqi/authssl.cc \
pgp/pgphandler.cc \
pgp/openpgpsdkhandler.cc \
pgp/pgpkeyutil.cc \
pgp/rscertificate.cc \
pgp/pgpauxutils.cc \
@ -644,6 +650,7 @@ SOURCES += util/folderiterator.cc \
util/rsexpr.cc \
util/smallobject.cc \
util/rsdir.cc \
util/rsfile.cc \
util/rsdiscspace.cc \
util/rsnet.cc \
util/rsnet_ss.cc \
@ -725,6 +732,41 @@ SOURCES += rsitems/rsnxsitems.cc \
gxs/rsgxsrequesttypes.cc \
gxs/rsnxsobserver.cpp
# Tor
HEADERS += retroshare/rstor.h
HEADERS += tor/AddOnionCommand.h \
tor/AuthenticateCommand.h \
tor/CryptoKey.h \
tor/GetConfCommand.h \
tor/HiddenService.h \
tor/PendingOperation.h \
tor/ProtocolInfoCommand.h \
tor/TorTypes.h \
tor/SetConfCommand.h \
tor/StrUtil.h \
tor/bytearray.h \
tor/TorControl.h \
tor/TorControlCommand.h \
tor/TorControlSocket.h \
tor/TorManager.h \
tor/TorProcess.h
SOURCES += tor/AddOnionCommand.cpp \
tor/AuthenticateCommand.cpp \
tor/GetConfCommand.cpp \
tor/HiddenService.cpp \
tor/ProtocolInfoCommand.cpp \
tor/SetConfCommand.cpp \
tor/TorControlCommand.cpp \
tor/TorControl.cpp \
tor/TorControlSocket.cpp \
tor/TorManager.cpp \
tor/TorProcess.cpp \
tor/CryptoKey.cpp \
tor/PendingOperation.cpp \
tor/StrUtil.cpp
# gxs tunnels
HEADERS += gxstunnel/p3gxstunnel.h \
gxstunnel/rsgxstunnelitems.h \
@ -1103,6 +1145,8 @@ test_bitdht {
################################# Android #####################################
android-* {
lessThan(ANDROID_API_VERSION, 24) {
## TODO: This probably disable largefile support and maybe is not necessary with
## __ANDROID_API__ >= 24 hence should be made conditional or moved to a
## compatibility header
@ -1110,12 +1154,26 @@ android-* {
DEFINES *= "fseeko64=fseeko"
DEFINES *= "ftello64=ftello"
## @See: rs_android/README-ifaddrs-android.adoc
HEADERS += \
rs_android/ifaddrs-android.h \
rs_android/LocalArray.h \
rs_android/ScopedFd.h
}
## Static library are very susceptible to order in command line
sLibs = bz2 $$RS_UPNP_LIB $$RS_SQL_LIB ssl crypto
LIBS += $$linkStaticLibs(sLibs)
PRE_TARGETDEPS += $$pretargetStaticLibs(sLibs)
HEADERS += util/androiddebug.h
HEADERS += \
rs_android/androidcoutcerrcatcher.hpp \
rs_android/retroshareserviceandroid.hpp \
rs_android/rsjni.hpp
SOURCES += rs_android/rsjni.cpp \
rs_android/retroshareserviceandroid.cpp \
rs_android/errorconditionwrap.cpp
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,116 @@
/*******************************************************************************
* libretroshare/src/pgp: pgphandler.h *
* *
* libretroshare: retroshare core library *
* *
* Copyright 2018 Cyril Soler <csoler@users.sourceforge.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#pragma once
#include <stdint.h>
#include <string>
#include <list>
#include <map>
#include <set>
#include "util/rsthreads.h"
#include "pgp/pgphandler.h"
#include "retroshare/rstypes.h"
extern "C" {
// we should make sure later on to get rid of these structures in the .h
#include "openpgpsdk/keyring.h"
}
/// This class offer an abstract pgp handler to be used in RetroShare.
class OpenPGPSDKHandler: public PGPHandler
{
public:
OpenPGPSDKHandler( const std::string& path_to_public_keyring,
const std::string& path_to_secret_keyring,
const std::string& path_to_trust_database,
const std::string& pgp_lock_file) ;
virtual ~OpenPGPSDKHandler() ;
//================================================================================================//
// Implemented API from PGPHandler //
//================================================================================================//
//virtual std::string makeRadixEncodedPGPKey(uint32_t key_index,bool include_signatures) override;
virtual bool removeKeysFromPGPKeyring(const std::set<RsPgpId>& key_ids,std::string& backup_file,uint32_t& error_code) override;
virtual bool availableGPGCertificatesWithPrivateKeys(std::list<RsPgpId>& ids) override;
virtual bool GeneratePGPCertificate(const std::string& name, const std::string& email, const std::string& passphrase, RsPgpId& pgpId, const int keynumbits, std::string& errString) override;
virtual std::string SaveCertificateToString(const RsPgpId& id,bool include_signatures) const override;
virtual bool exportPublicKey( const RsPgpId& id, unsigned char*& mem_block, size_t& mem_size, bool armoured, bool include_signatures ) const override;
virtual bool exportGPGKeyPair(const std::string& filename,const RsPgpId& exported_key_id) const override;
virtual bool exportGPGKeyPairToString( std::string& data, const RsPgpId& exportedKeyId, bool includeSignatures, std::string& errorMsg ) const override;
virtual bool getGPGDetailsFromBinaryBlock(const unsigned char *mem_block,size_t mem_size,RsPgpId& key_id, std::string& name, std::list<RsPgpId>& signers) const override;
virtual bool importGPGKeyPair(const std::string& filename,RsPgpId& imported_key_id,std::string& import_error) override;
virtual bool importGPGKeyPairFromString(const std::string &data, RsPgpId &imported_key_id, std::string &import_error) override;
virtual bool LoadCertificateFromBinaryData(const unsigned char *data,uint32_t data_len,RsPgpId& id,std::string& error_string) override;
virtual bool LoadCertificateFromString(const std::string& pgp_cert,RsPgpId& id,std::string& error_string) override;
virtual bool encryptTextToFile(const RsPgpId& key_id,const std::string& text,const std::string& outfile) override;
virtual bool encryptDataBin(const RsPgpId& key_id,const void *data, const uint32_t len, unsigned char *encrypted_data, unsigned int *encrypted_data_len) override;
virtual bool decryptDataBin(const RsPgpId& /*key_id*/,const void *encrypted_data, const uint32_t encrypted_len, unsigned char *data, unsigned int *data_len) override;
virtual bool decryptTextFromFile(const RsPgpId&,std::string& text,const std::string& inputfile) override;
virtual bool SignDataBin(const RsPgpId& id,const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen,bool use_raw_signature, std::string reason /* = "" */) override;
virtual bool privateSignCertificate(const RsPgpId& ownId,const RsPgpId& id_of_key_to_sign) override;
virtual bool VerifySignBin(const void *literal_data, uint32_t literal_data_length, unsigned char *sign, unsigned int sign_len, const PGPFingerprintType& key_fingerprint) override;
virtual bool getKeyFingerprint(const RsPgpId& id, RsPgpFingerprint& fp) const override;
virtual bool haveSecretKey(const RsPgpId& id) const override;
virtual bool syncDatabase() override;
private:
bool locked_syncPublicKeyring() ;
void initCertificateInfo(PGPCertificateInfo& cert,const ops_keydata_t *keydata,uint32_t i) ;
bool LoadCertificate(const unsigned char *data,uint32_t data_len,bool armoured,RsPgpId& id,std::string& error_string) ;
// Returns true if the signatures have been updated
//
bool validateAndUpdateSignatures(PGPCertificateInfo& cert,const ops_keydata_t *keydata) ;
/** Check public/private key and import them into the keyring
* @param keyring keyring with the new public/private key pair. Will be freed by the function.
* @param imported_key_id PGP id of the imported key
* @param import_error human readbale error message
* @returns true on success
* */
bool checkAndImportKeyPair(ops_keyring_t *keyring, RsPgpId& imported_key_id,std::string& import_error);
const ops_keydata_t *locked_getPublicKey(const RsPgpId&,bool stamp_the_key) const;
const ops_keydata_t *locked_getSecretKey(const RsPgpId&) const ;
void locked_mergeKeyringFromDisk(ops_keyring_t *keyring, std::map<RsPgpId,PGPCertificateInfo>& kmap, const std::string& keyring_file) ;
bool locked_addOrMergeKey(ops_keyring_t *keyring,std::map<RsPgpId,PGPCertificateInfo>& kmap,const ops_keydata_t *keydata) ;
// Members.
//
ops_keyring_t *_pubring ;
ops_keyring_t *_secring ;
void printOPSKeys() const;
// Helper functions.
//
static std::string makeRadixEncodedPGPKey(const ops_keydata_t *key,bool include_signatures) ;
static ops_keyring_t *allocateOPSKeyring() ;
static void addNewKeyToOPSKeyring(ops_keyring_t*, const ops_keydata_t&) ;
static bool mergeKeySignatures(ops_keydata_t *dst,const ops_keydata_t *src) ; // returns true if signature lists are different
};

View File

@ -34,17 +34,17 @@ PgpAuxUtilsImpl::PgpAuxUtilsImpl()
const RsPgpId& PgpAuxUtilsImpl::getPGPOwnId()
{
return AuthGPG::getAuthGPG()->getGPGOwnId();
return AuthPGP::getPgpOwnId();
}
RsPgpId PgpAuxUtilsImpl::getPGPId(const RsPeerId& sslid)
RsPgpId PgpAuxUtilsImpl::getPgpId(const RsPeerId& sslid)
{
return rsPeers->getGPGId(sslid);
}
bool PgpAuxUtilsImpl::getKeyFingerprint(const RsPgpId& id,PGPFingerprintType& fp) const
{
return AuthGPG::getAuthGPG()->getKeyFingerprint(id, fp);
return AuthPGP::getKeyFingerprint(id, fp);
}
bool PgpAuxUtilsImpl::VerifySignBin(const void *data,
@ -54,17 +54,17 @@ bool PgpAuxUtilsImpl::VerifySignBin(const void *data,
const PGPFingerprintType& withfingerprint)
{
return AuthGPG::getAuthGPG()->VerifySignBin(data, len, sign, signlen, withfingerprint);
return AuthPGP::VerifySignBin(data, len, sign, signlen, withfingerprint);
}
bool PgpAuxUtilsImpl::getGPGAllList(std::list<RsPgpId> &ids)
bool PgpAuxUtilsImpl::getPgpAllList(std::list<RsPgpId> &ids)
{
return AuthGPG::getAuthGPG()->getGPGAllList(ids);
return AuthPGP::getPgpAllList(ids);
}
bool PgpAuxUtilsImpl::parseSignature(unsigned char *sign, unsigned int signlen, RsPgpId& issuer) const
{
return AuthGPG::getAuthGPG()->parseSignature(sign,signlen,issuer);
return AuthPGP::parseSignature(sign,signlen,issuer);
}

View File

@ -35,8 +35,8 @@ class PgpAuxUtils
virtual ~PgpAuxUtils(){}
virtual const RsPgpId &getPGPOwnId() = 0;
virtual RsPgpId getPGPId(const RsPeerId& sslid) = 0;
virtual bool getGPGAllList(std::list<RsPgpId> &ids) = 0;
virtual RsPgpId getPgpId(const RsPeerId& sslid) = 0;
virtual bool getPgpAllList(std::list<RsPgpId> &ids) = 0;
virtual bool getKeyFingerprint(const RsPgpId& id,PGPFingerprintType& fp) const = 0;
virtual bool parseSignature(unsigned char *sign, unsigned int signlen, RsPgpId& issuer) const =0;
@ -49,12 +49,12 @@ public:
PgpAuxUtilsImpl();
virtual const RsPgpId &getPGPOwnId();
virtual RsPgpId getPGPId(const RsPeerId& sslid);
virtual RsPgpId getPgpId(const RsPeerId& sslid);
virtual bool parseSignature(unsigned char *sign, unsigned int signlen, RsPgpId& issuer) const ;
virtual bool getKeyFingerprint(const RsPgpId& id,PGPFingerprintType& fp) const;
virtual bool VerifySignBin(const void *data, uint32_t len, unsigned char *sign, unsigned int signlen, const PGPFingerprintType& withfingerprint);
virtual bool getGPGAllList(std::list<RsPgpId> &ids);
virtual bool getPgpAllList(std::list<RsPgpId> &ids);
};

File diff suppressed because it is too large Load Diff

View File

@ -29,12 +29,6 @@
#include <util/rsthreads.h>
#include <retroshare/rstypes.h>
extern "C" {
#include <openpgpsdk/types.h>
#include <openpgpsdk/keyring.h>
#include <openpgpsdk/keyring_local.h>
}
typedef std::string (*PassphraseCallback)(void *data, const char *uid_title, const char *uid_hint, const char *passphrase_info, int prev_was_bad,bool *cancelled) ;
class PGPCertificateInfo
@ -56,9 +50,11 @@ class PGPCertificateInfo
mutable rstime_t _time_stamp ; // last time the key was used (received, used for signature verification, etc)
PGPFingerprintType _fpr; /* fingerprint */
// RsPgpId _key_id ;
uint32_t _key_index ; // index to array of keys in the public keyring
// Index to array of keys in the public keyring. Dependign on the specific implementation
// of how the keyring is stored, this may be used differently.
uint32_t _key_index ;
static const uint32_t PGP_CERTIFICATE_FLAG_ACCEPT_CONNEXION = 0x0001 ;
static const uint32_t PGP_CERTIFICATE_FLAG_HAS_OWN_SIGNATURE = 0x0002 ;
@ -80,53 +76,87 @@ class PGPCertificateInfo
class PGPHandler
{
public:
PGPHandler( const std::string& path_to_public_keyring,
PGPHandler( const std::string& path_to_public_keyring,
const std::string& path_to_secret_keyring,
const std::string& path_to_trust_database,
const std::string& pgp_lock_file) ;
virtual ~PGPHandler() ;
/**
//=======================================================================================//
// Methods that needs to be derived depending on how PGP is implemented //
//=======================================================================================//
// Removes the given keys from the keyring. Also backup the keyring to a file which name is automatically generated
// and given pack for proper display.
//
virtual bool removeKeysFromPGPKeyring(const std::set<RsPgpId>& key_ids,std::string& backup_file,uint32_t& error_code) =0;
//virtual std::string makeRadixEncodedPGPKey(uint32_t key_index,bool include_signatures) =0;
virtual bool availableGPGCertificatesWithPrivateKeys(std::list<RsPgpId>& ids)=0;
virtual bool GeneratePGPCertificate(const std::string& name, const std::string& email, const std::string& passwd, RsPgpId& pgpId, const int keynumbits, std::string& errString) =0;
virtual std::string SaveCertificateToString(const RsPgpId& id,bool include_signatures) const=0;
/** The caller is in charge of freeing `mem` once finished */
virtual bool exportPublicKey( const RsPgpId& id, unsigned char*& mem_block, size_t& mem_size, bool armoured, bool include_signatures ) const =0;
virtual bool exportGPGKeyPair(const std::string& filename,const RsPgpId& exported_key_id) const=0;
virtual bool exportGPGKeyPairToString( std::string& data, const RsPgpId& exportedKeyId, bool includeSignatures, std::string& errorMsg ) const =0;
// Gets info about the key. Who are the signers, what's the owner's name, etc.
//
virtual bool getGPGDetailsFromBinaryBlock(const unsigned char *mem,size_t mem_size,RsPgpId& key_id, std::string& name, std::list<RsPgpId>& signers) const =0;
virtual bool importGPGKeyPair(const std::string& filename,RsPgpId& imported_id,std::string& import_error) =0;
/**
* @param ids list of gpg certificate ids (note, not the actual certificates)
*/
virtual bool importGPGKeyPairFromString(const std::string& data,RsPgpId& imported_id,std::string& import_error) =0;
virtual bool LoadCertificateFromString(const std::string& pem, RsPgpId& gpg_id, std::string& error_string)=0;
virtual bool LoadCertificateFromBinaryData(const unsigned char *bin_data,uint32_t bin_data_len, RsPgpId& gpg_id, std::string& error_string)=0;
virtual bool encryptTextToFile(const RsPgpId& key_id,const std::string& text,const std::string& outfile) =0;
virtual bool decryptTextFromFile(const RsPgpId& key_id,std::string& text,const std::string& encrypted_inputfile) =0;
// The client should supply a memory chunk to store the data. The length will be updated to the real length of the data.
//
virtual bool encryptDataBin(const RsPgpId& key_id,const void *data, const uint32_t len , unsigned char *encrypted_data, unsigned int *encrypted_data_len) =0;
virtual bool decryptDataBin(const RsPgpId& key_id,const void *encrypted_data, const uint32_t encrypted_len , unsigned char *data, unsigned int *data_len) =0;
virtual bool SignDataBin(const RsPgpId& id, const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen, bool make_raw_signature=false, std::string reason = "") =0;
virtual bool privateSignCertificate(const RsPgpId& own_id,const RsPgpId& id_of_key_to_sign) =0;
virtual bool VerifySignBin(const void *data, uint32_t data_len, unsigned char *sign, unsigned int sign_len, const PGPFingerprintType& withfingerprint) =0;
/**
* @brief Get PGP fingerprint for the given key
* @param id PGP 64bit key id
* @param fp storage for the retrived key fingerpring, the contained value
* is meaningfull only if true is returned
* @return true if the key was found, false if not
*/
virtual bool getKeyFingerprint(const RsPgpId& id, RsPgpFingerprint& fp) const=0;
virtual bool haveSecretKey(const RsPgpId& id) const =0;
// Syncs the keyrings and trust database between memory and disk. The algorithm is:
// 1 - lock the keyrings
// 2 - compare file modification dates with last writing date
// - if file is modified, load it, and merge with memory
// 3 - look into memory modification flags
// - if flag says keyring has changed, write to disk
//
virtual bool syncDatabase() =0;
//=======================================================================================//
// Common methods to PGPHandler //
//=======================================================================================//
bool getGPGFilteredList(std::list<RsPgpId>& list,bool (*filter)(const PGPCertificateInfo&) = NULL) const ;
bool haveSecretKey(const RsPgpId& id) const ;
bool importGPGKeyPair(const std::string& filename,RsPgpId& imported_id,std::string& import_error) ;
bool importGPGKeyPairFromString(const std::string& data,RsPgpId& imported_id,std::string& import_error) ;
bool exportGPGKeyPair(const std::string& filename,const RsPgpId& exported_id) const ;
bool exportGPGKeyPairToString(
std::string& data, const RsPgpId& exportedKeyId,
bool includeSignatures, std::string& errorMsg ) const;
bool availableGPGCertificatesWithPrivateKeys(std::list<RsPgpId>& ids);
bool GeneratePGPCertificate(const std::string& name, const std::string& email, const std::string& passwd, RsPgpId& pgpId, const int keynumbits, std::string& errString) ;
bool LoadCertificateFromString(const std::string& pem, RsPgpId& gpg_id, std::string& error_string);
bool LoadCertificateFromBinaryData(const unsigned char *bin_data,uint32_t bin_data_len, RsPgpId& gpg_id, std::string& error_string);
std::string SaveCertificateToString(const RsPgpId& id,bool include_signatures) const ;
/** The caller is in charge of freeing `mem` once finished */
bool exportPublicKey( const RsPgpId& id,
unsigned char*& mem, size_t& mem_size,
bool armoured, bool include_signatures) const;
bool parseSignature(unsigned char *sign, unsigned int signlen,RsPgpId& issuer_id) ;
bool SignDataBin(const RsPgpId& id, const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen, bool make_raw_signature=false, std::string reason = "") ;
bool VerifySignBin(const void *data, uint32_t data_len, unsigned char *sign, unsigned int sign_len, const PGPFingerprintType& withfingerprint) ;
bool privateSignCertificate(const RsPgpId& own_id,const RsPgpId& id_of_key_to_sign) ;
// The client should supply a memory chunk to store the data. The length will be updated to the real length of the data.
//
bool encryptDataBin(const RsPgpId& key_id,const void *data, const uint32_t len
, unsigned char *encrypted_data, unsigned int *encrypted_data_len) ;
bool decryptDataBin(const RsPgpId& key_id,const void *encrypted_data, const uint32_t encrypted_len
, unsigned char *data, unsigned int *data_len) ;
bool encryptTextToFile(const RsPgpId& key_id,const std::string& text,const std::string& outfile) ;
bool decryptTextFromFile(const RsPgpId& key_id,std::string& text,const std::string& encrypted_inputfile) ;
bool parseSignature(unsigned char *sign, unsigned int signlen,RsPgpId& issuer_id) ;
void setAcceptConnexion(const RsPgpId&,bool) ;
@ -135,11 +165,6 @@ public:
void locked_updateOwnSignatureFlag(PGPCertificateInfo&, const RsPgpId&, PGPCertificateInfo&, const RsPgpId&) ;
// Removes the given keys from the keyring. Also backup the keyring to a file which name is automatically generated
// and given pack for proper display.
//
bool removeKeysFromPGPKeyring(const std::set<RsPgpId>& key_ids,std::string& backup_file,uint32_t& error_code) ;
//bool isKeySupported(const RsPgpId& id) const ;
bool privateTrustCertificate(const RsPgpId& id,int valid_level) ;
@ -174,66 +199,18 @@ public:
*/
static RsPgpId pgpIdFromFingerprint(const RsPgpFingerprint& f);
/**
* @brief Get PGP fingerprint for the given key
* @param id PGP 64bit key id
* @param fp storage for the retrived key fingerpring, the contained value
* is meaningfull only if true is returned
* @return true if the key was found, false if not
*/
bool getKeyFingerprint(const RsPgpId& id, RsPgpFingerprint& fp) const;
// Gets info about the key. Who are the signers, what's the owner's name, etc.
//
bool getGPGDetailsFromBinaryBlock(const unsigned char *mem,size_t mem_size,RsPgpId& key_id, std::string& name, std::list<RsPgpId>& signers) const ;
// Debug stuff.
virtual bool printKeys() const ;
// Syncs the keyrings and trust database between memory and disk. The algorithm is:
// 1 - lock the keyrings
// 2 - compare file modification dates with last writing date
// - if file is modified, load it, and merge with memory
// 3 - look into memory modification flags
// - if flag says keyring has changed, write to disk
//
bool syncDatabase() ;
private:
bool LoadCertificate(const unsigned char *bin_data,uint32_t bin_data_len, bool armoured, RsPgpId& gpg_id, std::string& error_string);
void initCertificateInfo(PGPCertificateInfo& cert,const ops_keydata_t *keydata,uint32_t i) ;
// Returns true if the signatures have been updated
//
bool validateAndUpdateSignatures(PGPCertificateInfo& cert,const ops_keydata_t *keydata) ;
/** Check public/private key and import them into the keyring
* @param keyring keyring with the new public/private key pair. Will be freed by the function.
* @param imported_key_id PGP id of the imported key
* @param import_error human readbale error message
* @returns true on success
* */
bool checkAndImportKeyPair(ops_keyring_t *keyring, RsPgpId& imported_key_id,std::string& import_error);
const ops_keydata_t *locked_getPublicKey(const RsPgpId&,bool stamp_the_key) const;
const ops_keydata_t *locked_getSecretKey(const RsPgpId&) const ;
protected:
void locked_readPrivateTrustDatabase() ;
bool locked_writePrivateTrustDatabase() ;
bool locked_syncPublicKeyring() ;
bool locked_syncTrustDatabase() ;
void locked_mergeKeyringFromDisk(ops_keyring_t *keyring, std::map<RsPgpId,PGPCertificateInfo>& kmap, const std::string& keyring_file) ;
bool locked_addOrMergeKey(ops_keyring_t *keyring,std::map<RsPgpId,PGPCertificateInfo>& kmap,const ops_keydata_t *keydata) ;
bool locked_syncTrustDatabase() ;
// Members.
//
mutable RsMutex pgphandlerMtx ;
ops_keyring_t *_pubring ;
ops_keyring_t *_secring ;
std::map<RsPgpId,PGPCertificateInfo> _public_keyring_map ; // used for fast access to keys. Gives the index in the keyring.
std::map<RsPgpId,PGPCertificateInfo> _secret_keyring_map ;
@ -249,11 +226,5 @@ public:
rstime_t _secring_last_update_time ;
rstime_t _trustdb_last_update_time ;
// Helper functions.
//
static std::string makeRadixEncodedPGPKey(const ops_keydata_t *key,bool include_signatures) ;
static ops_keyring_t *allocateOPSKeyring() ;
static void addNewKeyToOPSKeyring(ops_keyring_t*, const ops_keydata_t&) ;
static PassphraseCallback _passphrase_callback ;
static bool mergeKeySignatures(ops_keydata_t *dst,const ops_keydata_t *src) ; // returns true if signature lists are different
};

View File

@ -553,26 +553,24 @@ unsigned short RsCertificate::loc_port_us() const
return (int)ipv4_internal_ip_and_port[4]*256 + (int)ipv4_internal_ip_and_port[5] ;
}
bool RsCertificate::cleanCertificate( const std::string& input, std::string& output, Format& format, uint32_t& error_code, bool check_content )
bool RsCertificate::cleanCertificate( const std::string& input, std::string& output, Format& format, uint32_t& error_code, bool check_content, RsPeerDetails& details)
{
if(cleanRadix64(input,output,error_code))
{
RsPeerDetails details;
if(rsPeers->parseShortInvite(output,details,error_code))
{
format = RS_CERTIFICATE_SHORT_RADIX;
return true;
}
if(rsPeers->parseShortInvite(output,details,error_code))
{
format = RS_CERTIFICATE_SHORT_RADIX;
return true;
}
//Clear details. As parseShortInvite may make it dirty.
details = RsPeerDetails();
format = RS_CERTIFICATE_RADIX;
if(!check_content) return true;
uint32_t errCode;
auto crt = RsCertificate::fromString(input, errCode);
error_code = static_cast<int>(errCode);
return crt != nullptr;
return rsPeers->loadDetailsFromStringCert(input,details,error_code);
}
return false;

View File

@ -101,7 +101,7 @@ public:
static bool cleanCertificate(
const std::string& input, std::string& output,
RsCertificate::Format& format, uint32_t& error_code, bool check_content);
RsCertificate::Format& format, uint32_t& error_code, bool check_content, RsPeerDetails& details);
const std::set<RsUrl>& locators() const { return mLocators; }

View File

@ -46,7 +46,7 @@
//const rstime_t STORE_KEY_TIMEOUT = 1 * 60 * 60; //store key is call around every hour
AuthGPG *AuthGPG::_instance = NULL ;
AuthPGP *AuthPGP::_instance = NULL ;
void cleanupZombies(int numkill); // function to cleanup zombies under OSX.
@ -54,34 +54,46 @@ void cleanupZombies(int numkill); // function to cleanup zombies under OSX.
/* Function to sign X509_REQ via GPGme. */
bool AuthGPG::decryptTextFromFile(std::string& text,const std::string& inputfile)
int AuthPGP::availablePgpCertificatesWithPrivateKeys(std::list<RsPgpId>& pgpIds)
{
return PGPHandler::decryptTextFromFile(mOwnGpgId,text,inputfile) ;
return instance()->mPgpHandler->availableGPGCertificatesWithPrivateKeys(pgpIds);
}
bool AuthPGP::getPgpDetailsFromBinaryBlock(const unsigned char *mem,size_t mem_size,RsPgpId& key_id, std::string& name, std::list<RsPgpId>& signers)
{
return instance()->mPgpHandler->getGPGDetailsFromBinaryBlock(mem,mem_size,key_id,name,signers);
}
void AuthPGP::registerToConfigMgr(const std::string& fname,p3ConfigMgr *CfgMgr)
{
CfgMgr->addConfiguration(fname, instance());
}
bool AuthPGP::decryptTextFromFile(std::string& text,const std::string& inputfile)
{
return instance()->mPgpHandler->decryptTextFromFile(instance()->mOwnGpgId,text,inputfile) ;
}
bool AuthGPG::removeKeysFromPGPKeyring(const std::set<RsPgpId>& pgp_ids,std::string& backup_file,uint32_t& error_code)
bool AuthPGP::removeKeysFromPGPKeyring(const std::set<RsPgpId>& pgp_ids,std::string& backup_file,uint32_t& error_code)
{
// std::list<RsPgpId> pids ;
//
// for(std::list<RsPgpId>::const_iterator it(pgp_ids.begin());it!=pgp_ids.end();++it)
// pids.push_back(RsPgpId(*it)) ;
return PGPHandler::removeKeysFromPGPKeyring(pgp_ids,backup_file,error_code) ;
return instance()->mPgpHandler->removeKeysFromPGPKeyring(pgp_ids,backup_file,error_code) ;
}
// bool AuthGPG::decryptTextFromString(std::string& encrypted_text,std::string& output)
// {
// return PGPHandler::decryptTextFromString(mOwnGpgId,encrypted_text,output) ;
// return instance()->mPgpHandler->decryptTextFromString(mOwnGpgId,encrypted_text,output) ;
// }
bool AuthGPG::encryptTextToFile(const std::string& text,const std::string& outfile)
bool AuthPGP::encryptTextToFile(const std::string& text,const std::string& outfile)
{
return PGPHandler::encryptTextToFile(mOwnGpgId,text,outfile) ;
return instance()->mPgpHandler->encryptTextToFile(instance()->mOwnGpgId,text,outfile) ;
}
// bool AuthGPG::encryptTextToString(const std::string& pgp_id,const std::string& text,std::string& outstr)
// {
// return PGPHandler::encryptTextToString(RsPgpId(pgp_id),text,outstr) ;
// return instance()->mPgpHandler->encryptTextToString(RsPgpId(pgp_id),text,outstr) ;
// }
std::string pgp_pwd_callback(void * /*hook*/, const char *uid_title, const char *uid_hint, const char * /*passphrase_info*/, int prev_was_bad,bool *cancelled)
@ -95,7 +107,7 @@ std::string pgp_pwd_callback(void * /*hook*/, const char *uid_title, const char
return password ;
}
void AuthGPG::init(
void AuthPGP::init(
const std::string& path_to_public_keyring,
const std::string& path_to_secret_keyring,
const std::string& path_to_trustdb,
@ -107,14 +119,14 @@ void AuthGPG::init(
std::cerr << "AuthGPG::init() called twice!" << std::endl ;
}
// if(cb) PGPHandler::setPassphraseCallback(cb);else
PGPHandler::setPassphraseCallback(pgp_pwd_callback);
_instance = new AuthGPG( path_to_public_keyring,
// if(cb) instance()->mPgpHandler->setPassphraseCallback(cb);else
instance()->mPgpHandler->setPassphraseCallback(pgp_pwd_callback);
_instance = new AuthPGP( path_to_public_keyring,
path_to_secret_keyring,
path_to_trustdb, pgp_lock_file );
}
void AuthGPG::exit()
void AuthPGP::exit()
{
if(_instance)
{
@ -124,9 +136,8 @@ void AuthGPG::exit()
}
}
AuthGPG::AuthGPG(const std::string& path_to_public_keyring,const std::string& path_to_secret_keyring,const std::string& path_to_trustdb,const std::string& pgp_lock_file)
AuthPGP::AuthPGP(const std::string& path_to_public_keyring,const std::string& path_to_secret_keyring,const std::string& path_to_trustdb,const std::string& pgp_lock_file)
:p3Config(),
PGPHandler(path_to_public_keyring,path_to_secret_keyring,path_to_trustdb,pgp_lock_file),
gpgMtxService("AuthGPG-service"),
gpgMtxEngine("AuthGPG-engine"),
gpgMtxData("AuthGPG-data"),
@ -135,7 +146,9 @@ AuthGPG::AuthGPG(const std::string& path_to_public_keyring,const std::string& pa
_force_sync_database(false),
mCount(0)
{
start("AuthGPG");
mPgpHandler = new OpenPGPSDKHandler(path_to_public_keyring,path_to_secret_keyring,path_to_trustdb,pgp_lock_file);
start("AuthGPG");
}
/* This function is called when retroshare is first started
@ -149,7 +162,7 @@ AuthGPG::AuthGPG(const std::string& path_to_public_keyring,const std::string& pa
//{
// std::list<RsPgpId> pids ;
//
// PGPHandler::availableGPGCertificatesWithPrivateKeys(pids) ;
// mPgpHandler->availableGPGCertificatesWithPrivateKeys(pids) ;
//
// for(std::list<RsPgpId>::const_iterator it(pids.begin());it!=pids.end();++it)
// ids.push_back( (*it).toStdString() ) ;
@ -165,17 +178,17 @@ AuthGPG::AuthGPG(const std::string& path_to_public_keyring,const std::string& pa
* This function must be called successfully (return == 1)
* before anything else can be done. (except above fn).
*/
int AuthGPG::GPGInit(const RsPgpId &ownId)
int AuthPGP::PgpInit(const RsPgpId &ownId)
{
#ifdef DEBUG_AUTHGPG
std::cerr << "AuthGPG::GPGInit() called with own gpg id : " << ownId.toStdString() << std::endl;
#endif
mOwnGpgId = RsPgpId(ownId);
instance()->mOwnGpgId = ownId;
//force the validity of the private key. When set to unknown, it caused signature and text encryptions bugs
privateTrustCertificate(ownId, 5);
updateOwnSignatureFlag(mOwnGpgId) ;
instance()->privateTrustCertificate(ownId, 5);
instance()->mPgpHandler->updateOwnSignatureFlag(ownId) ;
#ifdef DEBUG_AUTHGPG
std::cerr << "AuthGPG::GPGInit finished." << std::endl;
@ -184,11 +197,11 @@ int AuthGPG::GPGInit(const RsPgpId &ownId)
return 1;
}
AuthGPG::~AuthGPG()
AuthPGP::~AuthPGP()
{
}
void AuthGPG::threadTick()
void AuthPGP::threadTick()
{
rstime::rs_usleep(100 * 1000); //100 msec
@ -204,13 +217,13 @@ void AuthGPG::threadTick()
/// - checks whether the keyring has changed on disk.
/// - merges/updates according to status.
///
PGPHandler::syncDatabase() ;
mPgpHandler->syncDatabase() ;
mCount = 0;
_force_sync_database = false ;
}//if (++count >= 100 || _force_sync_database)
}
void AuthGPG::processServices()
void AuthPGP::processServices()
{
AuthGPGOperation *operation = NULL;
AuthGPGService *service = NULL;
@ -251,7 +264,7 @@ void AuthGPG::processServices()
/* don't bother loading - if we already have the certificate */
if (isGPGId(loadOrSave->m_certGpgId))
if (mPgpHandler->isGPGId(loadOrSave->m_certGpgId))
{
#ifdef GPG_DEBUG
std::cerr << "AuthGPGimpl::processServices() Skipping load - already have it" << std::endl;
@ -305,66 +318,66 @@ void AuthGPG::processServices()
delete operation;
}
bool AuthGPG::DoOwnSignature(const void *data, unsigned int datalen, void *buf_sigout, unsigned int *outl, std::string reason /* = "" */)
bool AuthPGP::DoOwnSignature(const void *data, unsigned int datalen, void *buf_sigout, unsigned int *outl, std::string reason /* = "" */)
{
return PGPHandler::SignDataBin(mOwnGpgId,data,datalen,(unsigned char *)buf_sigout,outl,false,reason) ;
return instance()->mPgpHandler->SignDataBin(mOwnGpgId,data,datalen,(unsigned char *)buf_sigout,outl,false,reason) ;
}
/* import to GnuPG and other Certificates */
bool AuthGPG::VerifySignature(const void *data, int datalen, const void *sig, unsigned int siglen, const PGPFingerprintType& withfingerprint)
bool AuthPGP::VerifySignature(const void *data, int datalen, const void *sig, unsigned int siglen, const PGPFingerprintType& withfingerprint)
{
return PGPHandler::VerifySignBin((unsigned char*)data,datalen,(unsigned char*)sig,siglen,withfingerprint) ;
return instance()->mPgpHandler->VerifySignBin((unsigned char*)data,datalen,(unsigned char*)sig,siglen,withfingerprint) ;
}
bool AuthGPG::parseSignature(const void *sig, unsigned int siglen, RsPgpId& issuer_id)
bool AuthPGP::parseSignature(const void *sig, unsigned int siglen, RsPgpId& issuer_id)
{
return PGPHandler::parseSignature((unsigned char*)sig,siglen,issuer_id) ;
return instance()->mPgpHandler->parseSignature((unsigned char*)sig,siglen,issuer_id) ;
}
bool AuthGPG::exportProfile(const std::string& fname,const RsPgpId& exported_id)
bool AuthPGP::exportProfile(const std::string& fname,const RsPgpId& exported_id)
{
return PGPHandler::exportGPGKeyPair(fname,exported_id) ;
return instance()->mPgpHandler->exportGPGKeyPair(fname,exported_id) ;
}
bool AuthGPG::exportIdentityToString(
bool AuthPGP::exportIdentityToString(
std::string& data, const RsPgpId& pgpId, bool includeSignatures,
std::string& errorMsg )
{
return PGPHandler::exportGPGKeyPairToString(
return instance()->mPgpHandler->exportGPGKeyPairToString(
data, pgpId, includeSignatures, errorMsg);
}
bool AuthGPG::importProfile(const std::string& fname,RsPgpId& imported_id,std::string& import_error)
bool AuthPGP::importProfile(const std::string& fname,RsPgpId& imported_id,std::string& import_error)
{
return PGPHandler::importGPGKeyPair(fname,imported_id,import_error) ;
return instance()->mPgpHandler->importGPGKeyPair(fname,imported_id,import_error) ;
}
bool AuthGPG::importProfileFromString(const std::string &data, RsPgpId &gpg_id, std::string &import_error)
bool AuthPGP::importProfileFromString(const std::string &data, RsPgpId &gpg_id, std::string &import_error)
{
return PGPHandler::importGPGKeyPairFromString(data, gpg_id, import_error);
return instance()->mPgpHandler->importGPGKeyPairFromString(data, gpg_id, import_error);
}
bool AuthGPG::active()
bool AuthPGP::active()
{
RsStackMutex stack(gpgMtxData); /******* LOCKED ******/
RsStackMutex stack(instance()->gpgMtxData); /******* LOCKED ******/
return gpgKeySelected;
return instance()->gpgKeySelected;
}
bool AuthGPG::GeneratePGPCertificate(const std::string& name, const std::string& email, const std::string& passwd, RsPgpId& pgpId, const int keynumbits, std::string& errString)
bool AuthPGP::GeneratePgpCertificate(const std::string& name, const std::string& email, const std::string& passwd, RsPgpId& pgpId, const int keynumbits, std::string& errString)
{
RsStackMutex stack(gpgMtxEngine); /******* LOCKED ******/
RsStackMutex stack(instance()->gpgMtxEngine); /******* LOCKED ******/
return PGPHandler::GeneratePGPCertificate(name, email, passwd, pgpId, keynumbits, errString) ;
return instance()->mPgpHandler->GeneratePGPCertificate(name, email, passwd, pgpId, keynumbits, errString) ;
}
/**** These Two are common */
std::string AuthGPG::getGPGName(const RsPgpId& id,bool *success)
std::string AuthPGP::getPgpName(const RsPgpId& id,bool *success)
{
RsStackMutex stack(gpgMtxData); /******* LOCKED ******/
RsStackMutex stack(instance()->gpgMtxData); /******* LOCKED ******/
const PGPCertificateInfo *info = getCertificateInfo(id) ;
const PGPCertificateInfo *info = instance()->mPgpHandler->getCertificateInfo(id) ;
if(info != NULL)
{
@ -378,11 +391,25 @@ std::string AuthGPG::getGPGName(const RsPgpId& id,bool *success)
}
}
/**** These Two are common */
std::string AuthGPG::getGPGEmail(const RsPgpId& id,bool *success)
AuthPGP *AuthPGP::instance()
{
RsStackMutex stack(gpgMtxData); /******* LOCKED ******/
const PGPCertificateInfo *info = getCertificateInfo(id) ;
if(!_instance)
{
RsFatal() << "AuthGPG::instance() called before AuthGPG::init()! This should not happen." << std::endl;
return nullptr;
}
return _instance;
}
bool AuthPGP::isPGPId(const RsPgpId& id)
{
return instance()->mPgpHandler->isGPGId(id);
}
/**** These Two are common */
std::string AuthPGP::getPgpEmail(const RsPgpId& id,bool *success)
{
RsStackMutex stack(instance()->gpgMtxData); /******* LOCKED ******/
const PGPCertificateInfo *info = instance()->mPgpHandler->getCertificateInfo(id) ;
if(info != NULL)
{
@ -398,30 +425,30 @@ std::string AuthGPG::getGPGEmail(const RsPgpId& id,bool *success)
/**** GPG versions ***/
const RsPgpId& AuthGPG::getGPGOwnId()
const RsPgpId& AuthPGP::getPgpOwnId()
{
RsStackMutex stack(gpgMtxData); /******* LOCKED ******/
return mOwnGpgId ;
RsStackMutex stack(instance()->gpgMtxData); /******* LOCKED ******/
return instance()->mOwnGpgId ;
}
std::string AuthGPG::getGPGOwnName()
std::string AuthPGP::getPgpOwnName()
{
return getGPGName(mOwnGpgId) ;
return getPgpName(instance()->mOwnGpgId) ;
}
bool AuthGPG::getGPGAllList(std::list<RsPgpId> &ids)
bool AuthPGP::getPgpAllList(std::list<RsPgpId> &ids)
{
RsStackMutex stack(gpgMtxData); /******* LOCKED ******/
RsStackMutex stack(instance()->gpgMtxData); /******* LOCKED ******/
PGPHandler::getGPGFilteredList(ids) ;
instance()->mPgpHandler->getGPGFilteredList(ids) ;
return true;
}
const PGPCertificateInfo *AuthGPG::getCertInfoFromStdString(const std::string& pgp_id) const
const PGPCertificateInfo *AuthPGP::getCertInfoFromStdString(const std::string& pgp_id) const
{
try
{
return PGPHandler::getCertificateInfo(RsPgpId(pgp_id)) ;
return instance()->mPgpHandler->getCertificateInfo(RsPgpId(pgp_id)) ;
}
catch(std::exception& e)
{
@ -429,13 +456,13 @@ const PGPCertificateInfo *AuthGPG::getCertInfoFromStdString(const std::string& p
return NULL ;
}
}
bool AuthGPG::haveSecretKey(const RsPgpId& id) const
bool AuthPGP::haveSecretKey(const RsPgpId& id)
{
return PGPHandler::haveSecretKey(id) ;
return instance()->mPgpHandler->haveSecretKey(id) ;
}
bool AuthGPG::isKeySupported(const RsPgpId& id) const
bool AuthPGP::isKeySupported(const RsPgpId& id)
{
const PGPCertificateInfo *pc = getCertificateInfo(id) ;
const PGPCertificateInfo *pc = instance()->mPgpHandler->getCertificateInfo(id) ;
if(pc == NULL)
return false ;
@ -443,11 +470,11 @@ bool AuthGPG::isKeySupported(const RsPgpId& id) const
return !(pc->_flags & PGPCertificateInfo::PGP_CERTIFICATE_FLAG_UNSUPPORTED_ALGORITHM) ;
}
bool AuthGPG::getGPGDetails(const RsPgpId& pgp_id, RsPeerDetails &d)
bool AuthPGP::getPgpDetails(const RsPgpId& pgp_id, RsPeerDetails &d)
{
RsStackMutex stack(gpgMtxData); /******* LOCKED ******/
RsStackMutex stack(instance()->gpgMtxData); /******* LOCKED ******/
const PGPCertificateInfo *pc = PGPHandler::getCertificateInfo(pgp_id) ;
const PGPCertificateInfo *pc = instance()->mPgpHandler->getCertificateInfo(pgp_id) ;
if(pc == NULL)
return false ;
@ -474,28 +501,26 @@ bool AuthGPG::getGPGDetails(const RsPgpId& pgp_id, RsPeerDetails &d)
return true;
}
bool AuthGPG::getGPGFilteredList(std::list<RsPgpId>& list,bool (*filter)(const PGPCertificateInfo&))
bool AuthPGP::getGPGFilteredList(std::list<RsPgpId>& list,bool (*filter)(const PGPCertificateInfo&))
{
RsStackMutex stack(gpgMtxData); /******* LOCKED ******/
return PGPHandler::getGPGFilteredList(list,filter) ;
return instance()->mPgpHandler->getGPGFilteredList(list,filter) ;
}
static bool filter_Validity(const PGPCertificateInfo& /*info*/) { return true ; } //{ return info._validLvl >= PGPCertificateInfo::GPGME_VALIDITY_MARGINAL ; }
static bool filter_Accepted(const PGPCertificateInfo& info) { return info._flags & PGPCertificateInfo::PGP_CERTIFICATE_FLAG_ACCEPT_CONNEXION ; }
static bool filter_OwnSigned(const PGPCertificateInfo& info) { return info._flags & PGPCertificateInfo::PGP_CERTIFICATE_FLAG_HAS_OWN_SIGNATURE ; }
bool AuthGPG::getGPGValidList(std::list<RsPgpId> &ids)
bool AuthPGP::getPgpValidList(std::list<RsPgpId> &ids)
{
return getGPGFilteredList(ids,&filter_Validity);
}
bool AuthGPG::getGPGAcceptedList(std::list<RsPgpId> &ids)
bool AuthPGP::getPgpAcceptedList(std::list<RsPgpId> &ids)
{
return getGPGFilteredList(ids,&filter_Accepted);
}
bool AuthGPG::getGPGSignedList(std::list<RsPgpId> &ids)
bool AuthPGP::getPgpSignedList(std::list<RsPgpId> &ids)
{
return getGPGFilteredList(ids,&filter_OwnSigned);
}
@ -504,9 +529,9 @@ bool AuthGPG::getGPGSignedList(std::list<RsPgpId> &ids)
// {
// RsStackMutex stack(gpgMtxData); /******* LOCKED ******/
// #ifdef LIMIT_CERTIFICATE_SIZE
// certificate = PGPHandler::SaveCertificateToString(RsPgpId(id),false) ;
// certificate = instance()->mPgpHandler->SaveCertificateToString(RsPgpId(id),false) ;
// #else
// certificate = PGPHandler::SaveCertificateToString(RsPgpId(id),true) ;
// certificate = instance()->mPgpHandler->SaveCertificateToString(RsPgpId(id),true) ;
// #endif
//
// // #ifdef LIMIT_CERTIFICATE_SIZE
@ -528,20 +553,20 @@ bool AuthGPG::getGPGSignedList(std::list<RsPgpId> &ids)
/* SKTAN : do not know how to use std::string id */
std::string AuthGPG::SaveCertificateToString(const RsPgpId &id,bool include_signatures)
std::string AuthPGP::SaveCertificateToString(const RsPgpId &id,bool include_signatures)
{
RsStackMutex stack(gpgMtxEngine); /******* LOCKED ******/
RsStackMutex stack(instance()->gpgMtxEngine); /******* LOCKED ******/
return PGPHandler::SaveCertificateToString(id,include_signatures) ;
return instance()->mPgpHandler->SaveCertificateToString(id,include_signatures) ;
}
/* import to GnuPG and other Certificates */
bool AuthGPG::LoadPGPKeyFromBinaryData(const unsigned char *data,uint32_t data_len, RsPgpId& gpg_id,std::string& error_string)
bool AuthPGP::LoadPGPKeyFromBinaryData(const unsigned char *data,uint32_t data_len, RsPgpId& gpg_id,std::string& error_string)
{
RsStackMutex stack(gpgMtxEngine); /******* LOCKED ******/
RsStackMutex stack(instance()->gpgMtxEngine); /******* LOCKED ******/
if(PGPHandler::LoadCertificateFromBinaryData(data,data_len,gpg_id,error_string))
if(instance()->mPgpHandler->LoadCertificateFromBinaryData(data,data_len,gpg_id,error_string))
{
updateOwnSignatureFlag(gpg_id,mOwnGpgId) ;
instance()->mPgpHandler->updateOwnSignatureFlag(gpg_id,instance()->mOwnGpgId) ;
return true ;
}
@ -549,13 +574,13 @@ bool AuthGPG::LoadPGPKeyFromBinaryData(const unsigned char *data,uint32_t data_l
}
/* import to GnuPG and other Certificates */
bool AuthGPG::LoadCertificateFromString(const std::string &str, RsPgpId& gpg_id,std::string& error_string)
bool AuthPGP::LoadCertificateFromString(const std::string &str, RsPgpId& gpg_id,std::string& error_string)
{
RsStackMutex stack(gpgMtxEngine); /******* LOCKED ******/
RsStackMutex stack(instance()->gpgMtxEngine); /******* LOCKED ******/
if(PGPHandler::LoadCertificateFromString(str,gpg_id,error_string))
if(instance()->mPgpHandler->LoadCertificateFromString(str,gpg_id,error_string))
{
updateOwnSignatureFlag(gpg_id,mOwnGpgId) ;
instance()->mPgpHandler->updateOwnSignatureFlag(gpg_id,instance()->mOwnGpgId) ;
return true ;
}
@ -576,7 +601,7 @@ bool AuthGPG::LoadCertificateFromString(const std::string &str, RsPgpId& gpg_id,
/*************************************/
/* These take PGP Ids */
bool AuthGPG::AllowConnection(const RsPgpId& gpg_id, bool accept)
bool AuthPGP::AllowConnection(const RsPgpId& gpg_id, bool accept)
{
#ifdef GPG_DEBUG
std::cerr << "AuthGPG::AllowConnection(" << gpg_id << ")" << std::endl;
@ -584,11 +609,11 @@ bool AuthGPG::AllowConnection(const RsPgpId& gpg_id, bool accept)
/* Was a "Reload Certificates" here -> be shouldn't be needed -> and very expensive, try without. */
{
RsStackMutex stack(gpgMtxData);
PGPHandler::setAcceptConnexion(gpg_id,accept) ;
RsStackMutex stack(instance()->gpgMtxData);
instance()->mPgpHandler->setAcceptConnexion(gpg_id,accept) ;
}
IndicateConfigChanged();
instance()->IndicateConfigChanged();
RsServer::notify()->notifyListChange(NOTIFY_LIST_FRIENDS, accept ? NOTIFY_TYPE_ADD : NOTIFY_TYPE_DEL);
@ -596,16 +621,16 @@ bool AuthGPG::AllowConnection(const RsPgpId& gpg_id, bool accept)
}
/* These take PGP Ids */
bool AuthGPG::SignCertificateLevel0(const RsPgpId &id)
bool AuthPGP::SignCertificateLevel0(const RsPgpId &id)
{
#ifdef GPG_DEBUG
std::cerr << "AuthGPG::SignCertificat(" << id << ")" << std::endl;
#endif
return privateSignCertificate(id) ;
return instance()->privateSignCertificate(id) ;
}
bool AuthGPG::RevokeCertificate(const RsPgpId &id)
bool AuthPGP::RevokeCertificate(const RsPgpId &id)
{
/* remove unused parameter warnings */
(void) id;
@ -617,46 +642,59 @@ bool AuthGPG::RevokeCertificate(const RsPgpId &id)
return false;
}
bool AuthGPG::TrustCertificate(const RsPgpId& id, int trustlvl)
bool AuthPGP::TrustCertificate(const RsPgpId& id, int trustlvl)
{
#ifdef GPG_DEBUG
std::cerr << "AuthGPG::TrustCertificate(" << id << ", " << trustlvl << ")" << std::endl;
#endif
return privateTrustCertificate(id, trustlvl) ;
return instance()->privateTrustCertificate(id, trustlvl) ;
}
bool AuthGPG::encryptDataBin(const RsPgpId& pgp_id,const void *data, unsigned int datalen, unsigned char *sign, unsigned int *signlen)
bool AuthPGP::encryptDataBin(const RsPgpId& pgp_id,const void *data, unsigned int datalen, unsigned char *sign, unsigned int *signlen)
{
return PGPHandler::encryptDataBin(RsPgpId(pgp_id),data,datalen,sign,signlen) ;
return instance()->mPgpHandler->encryptDataBin(RsPgpId(pgp_id),data,datalen,sign,signlen) ;
}
bool AuthGPG::decryptDataBin(const void *data, unsigned int datalen, unsigned char *sign, unsigned int *signlen)
bool AuthPGP::decryptDataBin(const void *data, unsigned int datalen, unsigned char *sign, unsigned int *signlen)
{
return PGPHandler::decryptDataBin(mOwnGpgId,data,datalen,sign,signlen) ;
return instance()->mPgpHandler->decryptDataBin(instance()->mOwnGpgId,data,datalen,sign,signlen) ;
}
bool AuthGPG::SignDataBin(const void *data, unsigned int datalen, unsigned char *sign, unsigned int *signlen, std::string reason /*= ""*/)
bool AuthPGP::SignDataBin(const void *data, unsigned int datalen, unsigned char *sign, unsigned int *signlen, std::string reason /*= ""*/)
{
return DoOwnSignature(data, datalen, sign, signlen, reason);
return instance()->DoOwnSignature(data, datalen, sign, signlen, reason);
}
bool AuthGPG::VerifySignBin(const void *data, uint32_t datalen, unsigned char *sign, unsigned int signlen, const PGPFingerprintType& withfingerprint)
bool AuthPGP::exportPublicKey( const RsPgpId& id, unsigned char*& mem_block, size_t& mem_size, bool armoured, bool include_signatures )
{
return VerifySignature(data, datalen, sign, signlen, withfingerprint);
return instance()->mPgpHandler->exportPublicKey(id,mem_block,mem_size,armoured,include_signatures);
}
bool AuthPGP::isPgpPubKeyAvailable(const RsPgpId& pgp_id)
{
return instance()->mPgpHandler->isPgpPubKeyAvailable(pgp_id);
}
bool AuthPGP::getKeyFingerprint(const RsPgpId& id,PGPFingerprintType& fp)
{
return instance()->mPgpHandler->getKeyFingerprint(id,fp);
}
bool AuthPGP::VerifySignBin(const void *data, uint32_t datalen, unsigned char *sign, unsigned int signlen, const PGPFingerprintType& withfingerprint)
{
return instance()->VerifySignature(data, datalen, sign, signlen, withfingerprint);
}
/* Sign/Trust stuff */
int AuthGPG::privateSignCertificate(const RsPgpId &id)
int AuthPGP::privateSignCertificate(const RsPgpId &id)
{
RsStackMutex stack(gpgMtxData); /******* LOCKED ******/
int ret = PGPHandler::privateSignCertificate(mOwnGpgId,id) ;
int ret = mPgpHandler->privateSignCertificate(mOwnGpgId,id) ;
_force_sync_database = true ;
return ret ;
}
/* revoke the signature on Certificate */
int AuthGPG::privateRevokeCertificate(const RsPgpId &/*id*/)
int AuthPGP::privateRevokeCertificate(const RsPgpId &/*id*/)
{
//RsStackMutex stack(gpgMtx); /******* LOCKED ******/
std::cerr << __PRETTY_FUNCTION__ << ": not implemented!" << std::endl;
@ -664,7 +702,7 @@ int AuthGPG::privateRevokeCertificate(const RsPgpId &/*id*/)
return 0;
}
int AuthGPG::privateTrustCertificate(const RsPgpId& id, int trustlvl)
int AuthPGP::privateTrustCertificate(const RsPgpId& id, int trustlvl)
{
RsStackMutex stack(gpgMtxData); /******* LOCKED ******/
@ -672,10 +710,10 @@ int AuthGPG::privateTrustCertificate(const RsPgpId& id, int trustlvl)
// The trust level is only a user-defined property that has nothing to
// do with the fact that we allow connections or not.
if(!isGPGAccepted(id))
if(!isPGPAccepted(id))
return 0;
int res = PGPHandler::privateTrustCertificate(id,trustlvl) ;
int res = instance()->mPgpHandler->privateTrustCertificate(id,trustlvl) ;
_force_sync_database = true ;
return res ;
}
@ -684,20 +722,24 @@ int AuthGPG::privateTrustCertificate(const RsPgpId& id, int trustlvl)
// -------------------------------- Config functions ------------------------------ //
// -----------------------------------------------------------------------------------//
//
RsSerialiser *AuthGPG::setupSerialiser()
RsSerialiser *AuthPGP::setupSerialiser()
{
RsSerialiser *rss = new RsSerialiser ;
rss->addSerialType(new RsGeneralConfigSerialiser());
return rss ;
}
bool AuthPGP::isPGPAccepted(const RsPgpId& id)
{
return instance()->mPgpHandler->isGPGAccepted(id);
}
bool AuthGPG::saveList(bool& cleanup, std::list<RsItem*>& lst)
bool AuthPGP::saveList(bool& cleanup, std::list<RsItem*>& lst)
{
#ifdef GPG_DEBUG
std::cerr << "AuthGPG::saveList() called" << std::endl ;
#endif
std::list<RsPgpId> ids ;
getGPGAcceptedList(ids) ; // needs to be done before the lock
getPgpAcceptedList(ids) ; // needs to be done before the lock
RsStackMutex stack(gpgMtxData); /******* LOCKED ******/
@ -722,7 +764,7 @@ bool AuthGPG::saveList(bool& cleanup, std::list<RsItem*>& lst)
return true;
}
bool AuthGPG::loadList(std::list<RsItem*>& load)
bool AuthPGP::loadList(std::list<RsItem*>& load)
{
#ifdef GPG_DEBUG
std::cerr << "AuthGPG::loadList() Item Count: " << load.size() << std::endl;
@ -745,7 +787,7 @@ bool AuthGPG::loadList(std::list<RsItem*>& load)
std::list<RsTlvKeyValue>::iterator kit;
for(kit = vitem->tlvkvs.pairs.begin(); kit != vitem->tlvkvs.pairs.end(); ++kit)
if (kit->key != mOwnGpgId.toStdString())
PGPHandler::setAcceptConnexion(RsPgpId(kit->key), (kit->value == "TRUE"));
instance()->mPgpHandler->setAcceptConnexion(RsPgpId(kit->key), (kit->value == "TRUE"));
}
delete (*it);
}
@ -753,16 +795,16 @@ bool AuthGPG::loadList(std::list<RsItem*>& load)
return true;
}
bool AuthGPG::addService(AuthGPGService *service)
bool AuthPGP::addService(AuthGPGService *service)
{
RsStackMutex stack(gpgMtxService); /********* LOCKED *********/
RsStackMutex stack(instance()->gpgMtxService); /********* LOCKED *********/
if (std::find(services.begin(), services.end(), service) != services.end()) {
if (std::find(instance()->services.begin(), instance()->services.end(), service) != instance()->services.end()) {
/* it exists already! */
return false;
}
services.push_back(service);
instance()->services.push_back(service);
return true;
}

View File

@ -34,7 +34,7 @@
#include "util/rsthreads.h"
#include "pqi/p3cfgmgr.h"
#include "pgp/pgphandler.h"
#include "pgp/openpgpsdkhandler.h"
#define MAX_GPG_SIGNATURE_SIZE 4096
@ -89,16 +89,19 @@ public:
virtual void setGPGOperation(AuthGPGOperation *operation) = 0;
};
class AuthGPG: public p3Config, public RsTickingThread, public PGPHandler
class AuthPGP: public p3Config, public RsTickingThread
{
public:
static void init(const std::string& path_to_pubring,
const std::string& path_to_secring,
const std::string& path_to_trustdb,
const std::string& pgp_lock_file);
static void init(const std::string& path_to_pubring,
const std::string& path_to_secring,
const std::string& path_to_trustdb,
const std::string& pgp_lock_file);
static void exit();
static AuthGPG *getAuthGPG() { return _instance ; }
static void registerToConfigMgr(const std::string& fname,p3ConfigMgr *CfgMgr);
static void exit();
static bool isPGPId(const RsPgpId& id) ;
static bool isPGPAccepted(const RsPgpId& id) ;
/**
* @param ids list of gpg certificate ids (note, not the actual certificates)
@ -118,7 +121,7 @@ public:
* (see storage at the end of the class)
*
****/
virtual bool active();
static bool active();
// /* Initialize */
// virtual bool InitAuth ();
@ -126,10 +129,13 @@ public:
/* Init by generating new Own PGP Cert, or selecting existing PGP Cert */
virtual int GPGInit(const RsPgpId &ownId);
virtual bool GeneratePGPCertificate(const std::string& name, const std::string& email, const std::string& passwd, RsPgpId &pgpId, const int keynumbits, std::string &errString);
static int PgpInit(const RsPgpId &ownId);
static bool GeneratePgpCertificate(const std::string& name, const std::string& email, const std::string& passwd, RsPgpId &pgpId, const int keynumbits, std::string &errString);
/*********************************************************************************/
static bool getPgpDetailsFromBinaryBlock(const unsigned char *mem,size_t mem_size,RsPgpId& key_id, std::string& name, std::list<RsPgpId>& signers) ;
static int availablePgpCertificatesWithPrivateKeys(std::list<RsPgpId>& pgpIds);
/*********************************************************************************/
/************************* STAGE 3 ***********************************************/
/*********************************************************************************/
/*****
@ -140,29 +146,33 @@ public:
* provide access to details in cache list.
*
****/
virtual std::string getGPGName(const RsPgpId &pgp_id,bool *success = NULL);
virtual std::string getGPGEmail(const RsPgpId &pgp_id,bool *success = NULL);
static std::string getPgpName(const RsPgpId &pgp_id,bool *success = NULL);
static std::string getPgpEmail(const RsPgpId &pgp_id,bool *success = NULL);
/* PGP web of trust management */
virtual const RsPgpId& getGPGOwnId();
virtual std::string getGPGOwnName();
static bool exportPublicKey( const RsPgpId& id, unsigned char*& mem_block, size_t& mem_size, bool armoured, bool include_signatures );
//virtual std::string getGPGOwnEmail();
virtual bool isKeySupported(const RsPgpId &id) const ;
virtual bool haveSecretKey(const RsPgpId &id) const ;
virtual bool getGPGDetails(const RsPgpId& id, RsPeerDetails &d);
virtual bool getGPGAllList(std::list<RsPgpId> &ids);
virtual bool getGPGValidList(std::list<RsPgpId> &ids);
virtual bool getGPGAcceptedList(std::list<RsPgpId> &ids);
virtual bool getGPGSignedList(std::list<RsPgpId> &ids);
virtual bool importProfile(const std::string& filename,RsPgpId& gpg_id,std::string& import_error) ;
virtual bool importProfileFromString(const std::string& data,RsPgpId& gpg_id,std::string& import_error) ;
virtual bool exportProfile(const std::string& filename,const RsPgpId& gpg_id) ;
virtual bool exportIdentityToString(
/* PGP web of trust management */
static const RsPgpId& getPgpOwnId();
static std::string getPgpOwnName();
//virtual std::string getGPGOwnEmail();
static bool getKeyFingerprint(const RsPgpId& id,PGPFingerprintType& fp) ;
static bool isKeySupported(const RsPgpId &id) ;
static bool isPgpPubKeyAvailable(const RsPgpId& pgp_id);
static bool haveSecretKey(const RsPgpId &id) ;
static bool getPgpDetails(const RsPgpId& id, RsPeerDetails &d);
static bool getPgpAllList(std::list<RsPgpId> &ids);
static bool getPgpValidList(std::list<RsPgpId> &ids);
static bool getPgpAcceptedList(std::list<RsPgpId> &ids);
static bool getPgpSignedList(std::list<RsPgpId> &ids);
static bool importProfile(const std::string& filename,RsPgpId& gpg_id,std::string& import_error) ;
static bool importProfileFromString(const std::string& data,RsPgpId& gpg_id,std::string& import_error) ;
static bool exportProfile(const std::string& filename,const RsPgpId& gpg_id) ;
static bool exportIdentityToString(
std::string& data, const RsPgpId& pgpId, bool includeSignatures,
std::string& errorMsg );
virtual bool removeKeysFromPGPKeyring(const std::set<RsPgpId> &pgp_ids,std::string& backup_file,uint32_t& error_code) ;
static bool removeKeysFromPGPKeyring(const std::set<RsPgpId> &pgp_ids,std::string& backup_file,uint32_t& error_code) ;
/*********************************************************************************/
/************************* STAGE 4 ***********************************************/
@ -171,9 +181,9 @@ public:
* STAGE 4: Loading and Saving Certificates. (Strings and Files)
*
****/
virtual bool LoadCertificateFromString(const std::string &pem, RsPgpId& gpg_id,std::string& error_string);
virtual bool LoadPGPKeyFromBinaryData(const unsigned char *data,uint32_t data_len, RsPgpId& gpg_id,std::string& error_string);
virtual std::string SaveCertificateToString(const RsPgpId &id,bool include_signatures) ;
static bool LoadCertificateFromString(const std::string &pem, RsPgpId& gpg_id,std::string& error_string);
static bool LoadPGPKeyFromBinaryData(const unsigned char *data,uint32_t data_len, RsPgpId& gpg_id,std::string& error_string);
static std::string SaveCertificateToString(const RsPgpId &id,bool include_signatures) ;
// Cached certificates.
//bool getCachedGPGCertificate(const RsPgpId &id, std::string &certificate);
@ -188,12 +198,12 @@ public:
* done in gpgroot already.
*
****/
virtual bool AllowConnection(const RsPgpId &gpg_id, bool accept);
static bool AllowConnection(const RsPgpId &gpg_id, bool accept);
virtual bool SignCertificateLevel0(const RsPgpId &id);
virtual bool RevokeCertificate(const RsPgpId &id); /* Particularly hard - leave for later */
static bool SignCertificateLevel0(const RsPgpId &id);
static bool RevokeCertificate(const RsPgpId &id); /* Particularly hard - leave for later */
virtual bool TrustCertificate(const RsPgpId& id, int trustlvl); //trustlvl is 2 for none, 3 for marginal and 4 for full trust
static bool TrustCertificate(const RsPgpId& id, int trustlvl); //trustlvl is 2 for none, 3 for marginal and 4 for full trust
/*********************************************************************************/
/************************* STAGE 7 ***********************************************/
@ -204,39 +214,39 @@ public:
* There should also be Encryption Functions... (do later).
*
****/
virtual bool SignDataBin(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen, std::string reason = "");
virtual bool VerifySignBin(const void*, uint32_t, unsigned char*, unsigned int, const PGPFingerprintType& withfingerprint);
virtual bool parseSignature(const void *sig, unsigned int siglen, RsPgpId& issuer_id);
static bool SignDataBin(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen, std::string reason = "");
static bool VerifySignBin(const void*, uint32_t, unsigned char*, unsigned int, const PGPFingerprintType& withfingerprint);
static bool parseSignature(const void *sig, unsigned int siglen, RsPgpId& issuer_id);
virtual bool encryptDataBin(const RsPgpId& pgp_id,const void *data, const uint32_t len, unsigned char *encr, unsigned int *encrlen);
virtual bool decryptDataBin(const void *data, const uint32_t len, unsigned char *decr, unsigned int *decrlen);
static bool encryptDataBin(const RsPgpId& pgp_id,const void *data, const uint32_t len, unsigned char *encr, unsigned int *encrlen);
static bool decryptDataBin(const void *data, const uint32_t len, unsigned char *decr, unsigned int *decrlen);
virtual bool decryptTextFromFile( std::string& text,const std::string& filename);
virtual bool encryptTextToFile (const std::string& text,const std::string& filename);
static bool decryptTextFromFile( std::string& text,const std::string& filename);
static bool encryptTextToFile (const std::string& text,const std::string& filename);
// virtual bool decryptTextFromString( std::string& encrypted_text,std::string& clear_string);
// virtual bool encryptTextToString (const std::string& pgp_id,const std::string& clear_text,std::string& encrypted_string);
bool getGPGFilteredList(std::list<RsPgpId>& list,bool (*filter)(const PGPCertificateInfo&) = NULL) ;
static bool getGPGFilteredList(std::list<RsPgpId>& list,bool (*filter)(const PGPCertificateInfo&) = NULL) ;
//END of PGP public functions
/* GPG service */
virtual bool addService(AuthGPGService *service) ;
static bool addService(AuthGPGService *service) ;
// This is for debug purpose only. Don't use it !!
static void setAuthGPG_debug(AuthGPG *auth_gpg) { _instance = auth_gpg ; }
static void setAuthGPG_debug(AuthPGP *auth_gpg) { _instance = auth_gpg ; }
protected:
AuthGPG(const std::string& path_to_pubring, const std::string& path_to_secring,const std::string& path_to_trustdb,const std::string& pgp_lock_file);
virtual ~AuthGPG();
AuthPGP(const std::string& path_to_pubring, const std::string& path_to_secring,const std::string& path_to_trustdb,const std::string& pgp_lock_file);
virtual ~AuthPGP();
/*****************************************************************/
/*********************** p3config ******************************/
/* Key Functions to be overloaded for Full Configuration */
virtual RsSerialiser *setupSerialiser();
virtual bool saveList(bool &cleanup, std::list<RsItem *>&);
virtual bool loadList(std::list<RsItem *>& load);
virtual RsSerialiser *setupSerialiser() override;
virtual bool saveList(bool &cleanup, std::list<RsItem *>&) override;
virtual bool loadList(std::list<RsItem *>& load) override;
/*****************************************************************/
private:
@ -274,8 +284,7 @@ private:
void threadTick() override; /// @see RsTickingThread
private:
static AuthGPG *instance_gpg; // pointeur vers le singleton
static AuthPGP *instance();
RsMutex gpgMtxService;
RsMutex gpgMtxEngine;
@ -290,6 +299,8 @@ private:
rstime_t mStoreKeyTime;
PGPHandler *mPgpHandler;
RsPgpId mOwnGpgId;
bool gpgKeySelected;
bool _force_sync_database ;
@ -297,7 +308,7 @@ private:
std::list<AuthGPGService*> services ;
static AuthGPG *_instance ;
static AuthPGP *_instance ;
};
#endif

View File

@ -759,8 +759,7 @@ X509 *AuthSSLimpl::SignX509ReqWithGPG(X509_REQ *req, long /*days*/)
//long version = 0x00;
unsigned long chtype = MBSTRING_UTF8;
X509_NAME *issuer_name = X509_NAME_new();
X509_NAME_add_entry_by_txt(issuer_name, "CN", chtype,
(unsigned char *) AuthGPG::getAuthGPG()->getGPGOwnId().toStdString().c_str(), -1, -1, 0);
X509_NAME_add_entry_by_txt(issuer_name, "CN", chtype, (unsigned char *) AuthPGP::getPgpOwnId().toStdString().c_str(), -1, -1, 0);
/****
X509_NAME_add_entry_by_NID(issuer_name, 48, 0,
(unsigned char *) "email@email.com", -1, -1, 0);
@ -770,7 +769,7 @@ X509 *AuthSSLimpl::SignX509ReqWithGPG(X509_REQ *req, long /*days*/)
(unsigned char *) "loc", -1, -1, 0);
****/
std::cerr << "AuthSSLimpl::SignX509Req() Issuer name: " << AuthGPG::getAuthGPG()->getGPGOwnId().toStdString() << std::endl;
std::cerr << "AuthSSLimpl::SignX509Req() Issuer name: " << AuthPGP::getPgpOwnId().toStdString() << std::endl;
#ifdef V07_NON_BACKWARD_COMPATIBLE_CHANGE_002
static const uint64_t CERTIFICATE_SERIAL_NUMBER = RS_CERTIFICATE_VERSION_NUMBER_07_0001 ;
@ -945,7 +944,7 @@ X509 *AuthSSLimpl::SignX509ReqWithGPG(X509_REQ *req, long /*days*/)
std::cerr << "Buffers Allocated" << std::endl;
/* NOW Sign via GPG Functions */
if (!AuthGPG::getAuthGPG()->SignDataBin(buf_in, inl, buf_sigout, (unsigned int *) &sigoutl,"AuthSSLimpl::SignX509ReqWithGPG()"))
if (!AuthPGP::SignDataBin(buf_in, inl, buf_sigout, (unsigned int *) &sigoutl,"AuthSSLimpl::SignX509ReqWithGPG()"))
{
sigoutl = 0;
goto err;
@ -1040,7 +1039,7 @@ bool AuthSSLimpl::AuthX509WithGPG(X509 *x509,bool verbose, uint32_t& diagnostic)
{
RsPgpId issuer = RsX509Cert::getCertIssuer(*x509);
RsPeerDetails pd;
if (!AuthGPG::getAuthGPG()->getGPGDetails(issuer, pd))
if (!AuthPGP::getPgpDetails(issuer, pd))
{
RsInfo() << __PRETTY_FUNCTION__ << " X509 NOT authenticated : "
<< "AuthGPG::getAuthGPG()->getGPGDetails(" << issuer
@ -1185,9 +1184,7 @@ bool AuthSSLimpl::AuthX509WithGPG(X509 *x509,bool verbose, uint32_t& diagnostic)
// passed, verify the signature itself
if (!AuthGPG::getAuthGPG()->VerifySignBin(
signed_data, signed_data_length, signature->data,
static_cast<unsigned int>(signature->length), pd.fpr ))
if (!AuthPGP::VerifySignBin( signed_data, signed_data_length, signature->data, static_cast<unsigned int>(signature->length), pd.fpr ))
{
diagnostic = RS_SSL_HANDSHAKE_DIAGNOSTIC_WRONG_SIGNATURE;
goto err;
@ -1383,7 +1380,7 @@ int AuthSSLimpl::VerifyX509Callback(int /*preverify_ok*/, X509_STORE_CTX* ctx)
std::cerr << "******* VerifyX509Callback cert: " << std::hex << ctx->cert <<std::dec << std::endl;
#endif
if ( !isSslOnlyFriend && pgpId != AuthGPG::getAuthGPG()->getGPGOwnId() && !AuthGPG::getAuthGPG()->isGPGAccepted(pgpId) )
if ( !isSslOnlyFriend && pgpId != AuthPGP::getPgpOwnId() && !AuthPGP::isPGPAccepted(pgpId) )
{
std::string errMsg = "Connection attempt signed by PGP key id: " +
pgpId.toStdString() + " not accepted because it is not"

View File

@ -812,11 +812,11 @@ int p3PeerMgrIMPL::getFriendCount(bool ssl, bool online)
// count all gpg id's
std::list<RsPgpId> gpgIds;
AuthGPG::getAuthGPG()->getGPGAcceptedList(gpgIds);
AuthPGP::getPgpAcceptedList(gpgIds);
// add own gpg id, if we have more than one location
std::list<RsPeerId> ownSslIds;
getAssociatedPeers(AuthGPG::getAuthGPG()->getGPGOwnId(), ownSslIds);
getAssociatedPeers(AuthPGP::getPgpOwnId(), ownSslIds);
return gpgIds.size() + ((ownSslIds.size() > 0) ? 1 : 0);
}
@ -962,7 +962,7 @@ bool p3PeerMgrIMPL::addFriend(const RsPeerId& input_id, const RsPgpId& input_gpg
// check that the PGP key is known
if(!AuthGPG::getAuthGPG()->isGPGId(gpg_id))
if(!AuthPGP::isPGPId(gpg_id))
{
RsErr() << "Trying to add SSL id (" << id << ") to be validated with unknown PGP key (" << gpg_id << ". This is a bug!" << std::endl;
return false;
@ -970,7 +970,7 @@ bool p3PeerMgrIMPL::addFriend(const RsPeerId& input_id, const RsPgpId& input_gpg
//Authentication is now tested at connection time, we don't store the ssl cert anymore
//
if (!AuthGPG::getAuthGPG()->isGPGAccepted(gpg_id) && gpg_id != AuthGPG::getAuthGPG()->getGPGOwnId())
if (!AuthPGP::isPGPAccepted(gpg_id) && gpg_id != AuthPGP::getPgpOwnId())
{
#ifdef PEER_DEBUG
std::cerr << "p3PeerMgrIMPL::addFriend() gpg is not accepted" << std::endl;
@ -1024,7 +1024,7 @@ bool p3PeerMgrIMPL::addFriend(const RsPeerId& input_id, const RsPgpId& input_gpg
pstate.id = id;
pstate.gpg_id = gpg_id;
pstate.name = AuthGPG::getAuthGPG()->getGPGName(gpg_id);
pstate.name = AuthPGP::getPgpName(gpg_id);
pstate.vs_disc = vs_disc;
pstate.vs_dht = vs_dht;
@ -1126,8 +1126,8 @@ bool p3PeerMgrIMPL::addSslOnlyFriend( const RsPeerId& sslId, const RsPgpId& pgp_
* superficially set to true the PGP signature verification would have been
* skipped and the attacker connection would be accepted.
* If the PGP key is available add it as full friend. */
if(AuthGPG::getAuthGPG()->isPgpPubKeyAvailable(pgp_id))
AuthGPG::getAuthGPG()->AllowConnection(pgp_id, true);
if(AuthPGP::isPgpPubKeyAvailable(pgp_id))
AuthPGP::AllowConnection(pgp_id, true);
else
pstate.skip_pgp_signature_validation = true;
@ -1398,11 +1398,11 @@ bool p3PeerMgrIMPL::UpdateOwnAddress( const sockaddr_storage& pLocalAddr,
sockaddr_storage_copy(pExtAddr, extAddr);
sockaddr_storage_ipv6_to_ipv4(extAddr);
//#ifdef PEER_DEBUG
#ifdef PEER_DEBUG
std::cerr << "p3PeerMgrIMPL::UpdateOwnAddress("
<< sockaddr_storage_tostring(localAddr) << ", "
<< sockaddr_storage_tostring(extAddr) << ")" << std::endl;
//#endif
#endif
if( rsBanList &&
!rsBanList->isAddressAccepted(localAddr,
@ -2470,7 +2470,7 @@ bool p3PeerMgrIMPL::loadList(std::list<RsItem *>& load)
setOwnNetworkMode(pitem->netMode);
setOwnVisState(pitem->vs_disc, pitem->vs_dht);
mOwnState.gpg_id = AuthGPG::getAuthGPG()->getGPGOwnId();
mOwnState.gpg_id = AuthPGP::getPgpOwnId();
mOwnState.location = AuthSSL::getAuthSSL()->getOwnLocation();
}
else
@ -2642,7 +2642,7 @@ bool p3PeerMgrIMPL::loadList(std::list<RsItem *>& load)
#endif
for(uint32_t i=0;i<sitem->pgp_ids.size();++i)
if(AuthGPG::getAuthGPG()->isGPGAccepted(sitem->pgp_ids[i]) || sitem->pgp_ids[i] == AuthGPG::getAuthGPG()->getGPGOwnId())
if(AuthPGP::isPGPAccepted(sitem->pgp_ids[i]) || sitem->pgp_ids[i] == AuthPGP::getPgpOwnId())
{
mFriendsPermissionFlags[sitem->pgp_ids[i]] = sitem->service_flags[i] ;
#ifdef PEER_DEBUG
@ -2684,7 +2684,7 @@ bool p3PeerMgrIMPL::loadList(std::list<RsItem *>& load)
for(auto group_pair:groupList)
{
for(auto profileIdIt(group_pair.second.peerIds.begin());profileIdIt!=group_pair.second.peerIds.end();)
if(AuthGPG::getAuthGPG()->isGPGAccepted(*profileIdIt) || *profileIdIt == AuthGPG::getAuthGPG()->getGPGOwnId())
if(AuthPGP::isPGPAccepted(*profileIdIt) || *profileIdIt == AuthPGP::getPgpOwnId())
++profileIdIt;
else
{

View File

@ -21,24 +21,46 @@
******************************************************************************/
#include "util/rsprint.h"
#include "util/rsfile.h"
#include "pqi/pqifdbin.h"
RsFdBinInterface::RsFdBinInterface(int file_descriptor)
: mCLintConnt(file_descriptor),mIsActive(file_descriptor!=0)
RsFdBinInterface::RsFdBinInterface(int file_descriptor, bool is_socket)
: mCLintConnt(file_descriptor),mIsSocket(is_socket),mIsActive(false)
{
mTotalReadBytes=0;
mTotalInBufferBytes=0;
mTotalWrittenBytes=0;
mTotalOutBufferBytes=0;
if(file_descriptor!=0)
setSocket(file_descriptor);
}
void RsFdBinInterface::setSocket(int s)
{
if(mIsActive != 0)
{
RsErr() << "Changing socket to active FsBioInterface! Canceling all pending R/W data." ;
close();
}
if(mIsActive != 0)
{
RsErr() << "Changing socket to active FsBioInterface! Canceling all pending R/W data." ;
close();
}
#ifndef WINDOWS_SYS
int flags = fcntl(s,F_GETFL);
if(!(flags & O_NONBLOCK))
{
RsWarn() << "Trying to use a blocking file descriptor in RsFdBinInterface. This is not going to work! Setting the socket to be non blocking.";
unix_fcntl_nonblock(s);
}
#else
// On windows, there is no way to determine whether a socket is blocking or not, so we set it to non blocking whatsoever.
if (mIsSocket) {
unix_fcntl_nonblock(s);
} else {
RsFileUtil::set_fd_nonblock(s);
}
#endif
mCLintConnt = s;
mIsActive = (s!=0);
}
@ -64,7 +86,15 @@ int RsFdBinInterface::read_pending()
char inBuffer[1025];
memset(inBuffer,0,1025);
ssize_t readbytes = recv(mCLintConnt, inBuffer, sizeof(inBuffer),MSG_DONTWAIT);
ssize_t readbytes;
#if WINDOWS_SYS
if (mIsSocket)
// Windows needs recv for sockets
readbytes = recv(mCLintConnt, inBuffer, sizeof(inBuffer), 0);
else
#endif
readbytes = read(mCLintConnt, inBuffer, sizeof(inBuffer)); // Needs read instead of recv which is only for sockets.
// Sockets should be set to non blocking by the client process.
if(readbytes == 0)
{
@ -76,20 +106,28 @@ int RsFdBinInterface::read_pending()
}
if(readbytes < 0)
{
if(errno != EWOULDBLOCK && errno != EAGAIN)
if(errno != 0 && errno != EWOULDBLOCK && errno != EAGAIN)
#ifdef WINDOWS_SYS
// A non blocking read to file descriptor gets ERROR_NO_DATA for empty data
if (mIsSocket == true || GetLastError() != ERROR_NO_DATA)
#endif
RsErr() << "read() failed. Errno=" << errno ;
return mTotalInBufferBytes;
}
#ifdef DEBUG_FS_BIN
RsDbg() << "clintConnt: " << mCLintConnt << ", readbytes: " << readbytes ;
#endif
// display some debug info
if(readbytes > 0)
{
#ifdef DEBUG_FS_BIN
RsDbg() << "Received the following bytes: " << RsUtil::BinToHex( reinterpret_cast<unsigned char*>(inBuffer),readbytes,50) << std::endl;
//RsDbg() << "Received the following bytes: " << std::string(inBuffer,readbytes) << std::endl;
RsDbg() << "Received the following bytes: " << std::string(inBuffer,readbytes) << std::endl;
#endif
void *ptr = malloc(readbytes);
@ -102,7 +140,9 @@ int RsFdBinInterface::read_pending()
mTotalInBufferBytes += readbytes;
mTotalReadBytes += readbytes;
#ifdef DEBUG_FS_BIN
RsDbg() << "Socket: " << mCLintConnt << ". Total read: " << mTotalReadBytes << ". Buffer size: " << mTotalInBufferBytes ;
#endif
}
return mTotalInBufferBytes;
}
@ -113,7 +153,14 @@ int RsFdBinInterface::write_pending()
return mTotalOutBufferBytes;
auto& p = out_buffer.front();
int written = write(mCLintConnt, p.first, p.second);
int written;
#if WINDOWS_SYS
if (mIsSocket)
// Windows needs send for sockets
written = send(mCLintConnt, (char*) p.first, p.second, 0);
else
#endif
written = write(mCLintConnt, p.first, p.second);
if(written < 0)
{
@ -129,11 +176,15 @@ int RsFdBinInterface::write_pending()
return mTotalOutBufferBytes;
}
#ifdef DEBUG_FS_BIN
RsDbg() << "clintConnt: " << mCLintConnt << ", written: " << written ;
#endif
// display some debug info
#ifdef DEBUG_FS_BIN
RsDbg() << "Sent the following bytes: " << RsUtil::BinToHex( reinterpret_cast<unsigned char*>(p.first),written,50) << std::endl;
#endif
if(written < p.second)
{
@ -173,6 +224,19 @@ void RsFdBinInterface::clean()
in_buffer.clear();
out_buffer.clear();
}
int RsFdBinInterface::readline(void *data, int len)
{
int n=0;
for(auto p:in_buffer)
for(int i=0;i<p.second;++i,++n)
if((n+1==len) || static_cast<unsigned char*>(p.first)[i] == '\n')
return readdata(data,n+1);
return 0;
}
int RsFdBinInterface::readdata(void *data, int len)
{
// read incoming bytes in the buffer
@ -254,6 +318,12 @@ bool RsFdBinInterface::moretoread(uint32_t /* usec */)
{
return mTotalInBufferBytes > 0;
}
bool RsFdBinInterface::moretowrite(uint32_t /* usec */)
{
return mTotalOutBufferBytes > 0 ;
}
bool RsFdBinInterface::cansend(uint32_t)
{
return isactive();

View File

@ -25,7 +25,7 @@
class RsFdBinInterface: public BinInterface
{
public:
RsFdBinInterface(int file_descriptor);
RsFdBinInterface(int file_descriptor, bool is_socket);
~RsFdBinInterface();
// Implements BinInterface methods
@ -41,9 +41,14 @@ public:
//
int readdata(void *data, int len) override;
// Read at most len bytes only if \n is encountered within that range. Otherwise, nothing is changed.
//
int readline(void *data, int len) ;
int netstatus() override;
int isactive() override;
bool moretoread(uint32_t usec) override;
bool moretowrite(uint32_t usec) ;
bool cansend(uint32_t usec) override;
int close() override;
@ -65,6 +70,7 @@ private:
int write_pending();
int mCLintConnt;
bool mIsSocket;
bool mIsActive;
uint32_t mTotalReadBytes;
uint32_t mTotalInBufferBytes;

View File

@ -21,15 +21,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#ifdef WINDOWS_SYS
# include "util/rswin.h"
# include "util/rsmemory.h"
# include <ws2tcpip.h>
#endif // WINDOWS_SYS
#ifdef __ANDROID__
# include <android/api-level.h>
#endif // def __ANDROID__
#include <cerrno>
#include <iostream>
@ -43,6 +34,28 @@
#include "util/rsnet.h"
#include "util/stacktrace.h"
#ifdef WINDOWS_SYS
# include "util/rswin.h"
# include "util/rsmemory.h"
# include <ws2tcpip.h>
#endif // WINDOWS_SYS
/// @See: android_ifaddrs/README.adoc
#ifdef __ANDROID__
# include <android/api-level.h>
#endif // def __ANDROID__
#ifdef WINDOWS_SYS /* Windows - define errno */
int errno;
#else /* Windows - define errno */
#include <netdb.h>
#endif
#ifdef __HAIKU__
# include <sys/sockio.h>
# define IFF_RUNNING 0x0001
#endif
static struct RsLog::logInfo pqinetzoneInfo = {RsLog::Default, "pqinet"};
#define pqinetzone &pqinetzoneInfo
@ -50,21 +63,6 @@ static struct RsLog::logInfo pqinetzoneInfo = {RsLog::Default, "pqinet"};
* #define NET_DEBUG 1
****/
#ifdef WINDOWS_SYS /* Windows - define errno */
int errno;
#else /* Windows - define errno */
#include <netdb.h>
#endif
#ifdef __HAIKU__
#include <sys/sockio.h>
#define IFF_RUNNING 0x0001
#endif
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef WINDOWS_SYS
@ -270,18 +268,16 @@ int inet_aton(const char *name, struct in_addr *addr)
#endif
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
#include "util/cxx17retrocompat.h"
#include <sys/types.h>
#ifdef WINDOWS_SYS
# include <winsock2.h>
# include <iphlpapi.h>
# pragma comment(lib, "IPHLPAPI.lib")
#elif defined(__ANDROID__) && __ANDROID_API__ < 24
# include <string>
# include <QString>
# include <QHostAddress>
# include <QNetworkInterface>
#else // not __ANDROID__ nor WINDOWS => Linux and other unixes
/// @See: android_ifaddrs/README.adoc
# include "rs_android/ifaddrs-android.h"
#else // not WINDOWS => Linux and other unixes
# include <ifaddrs.h>
# include <net/if.h>
#endif // WINDOWS_SYS
@ -324,20 +320,12 @@ bool getLocalAddresses(std::vector<sockaddr_storage>& addrs)
}
}
free(adapter_addresses);
#elif defined(__ANDROID__) && __ANDROID_API__ < 24
for(auto& qAddr: QNetworkInterface::allAddresses())
{
sockaddr_storage tmpAddr;
sockaddr_storage_clear(tmpAddr);
if(sockaddr_storage_ipv4_aton(tmpAddr, qAddr.toString().toStdString().c_str()))
addrs.push_back(tmpAddr);
}
#else // not WINDOWS_SYS not ANDROID => Linux and other unixes
#else // not WINDOWS_SYS => Linux and other unixes
struct ifaddrs *ifsaddrs, *ifa;
if(getifaddrs(&ifsaddrs) != 0)
{
std::cerr << __PRETTY_FUNCTION__ << " FATAL ERROR: " << errno << " "
<< strerror(errno) << std::endl;
RS_ERR( "getifaddrs failed with: ", errno, " ",
rs_errno_to_condition(errno) );
print_stacktrace();
return false;
}
@ -346,18 +334,19 @@ bool getLocalAddresses(std::vector<sockaddr_storage>& addrs)
{
sockaddr_storage tmp;
sockaddr_storage_clear(tmp);
if (sockaddr_storage_copyip(tmp, *reinterpret_cast<sockaddr_storage*>(ifa->ifa_addr)))
if(sockaddr_storage_copyip(
tmp,
*reinterpret_cast<sockaddr_storage*>(ifa->ifa_addr) ))
addrs.push_back(tmp);
}
freeifaddrs(ifsaddrs);
#endif // WINDOWS_SYS
#ifdef NET_DEBUG
std::list<sockaddr_storage>::iterator it;
std::cout << "getLocalAddresses(...) returning: <" ;
for(it = addrs.begin(); it != addrs.end(); ++it)
std::cout << sockaddr_storage_iptostring(*it) << ", ";
std::cout << ">" << std::endl;
auto&& dbg = RS_DBG("returning: [");
for(auto& addr: std::as_const(addrs))
dbg << sockaddr_storage_iptostring(addr) << ", ";
dbg << "]" << std::endl;
#endif
return !addrs.empty();

View File

@ -4,7 +4,8 @@
* libretroshare: retroshare core library *
* *
* Copyright (C) 2004-2006 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2015-2018 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2015-2021 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
@ -20,8 +21,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#ifndef MRK_PQI_NETWORKING_HEADER
#define MRK_PQI_NETWORKING_HEADER
#pragma once
#include <vector>
@ -86,9 +86,13 @@ extern int errno; /* Define extern errno, to duplicate unix behaviour */
#include <string>
#include <list>
#include "util/rsdeprecate.h"
// Same def - different functions...
RS_DEPRECATED_FOR("use std::error_condition instead")
void showSocketError(std::string &out);
RS_DEPRECATED_FOR("use std::error_condition instead")
std::string socket_errorType(int err);
bool getLocalAddresses(std::vector<sockaddr_storage> & addrs);
@ -103,10 +107,6 @@ int unix_getsockopt_error(int sockfd, int *err);
#ifdef WINDOWS_SYS // WINDOWS
/******************* WINDOWS SPECIFIC PART ******************/
RS_DEPRECATED_FOR("use std::error_condition instead")
int WinToUnixError(int error);
#endif
#endif

View File

@ -1213,8 +1213,7 @@ int pqissl::Authorise_SSL_Connection()
}
RsPgpId pgpId = RsX509Cert::getCertIssuer(*peercert);
if( !isSslOnlyFriend && pgpId != AuthGPG::getAuthGPG()->getGPGOwnId() &&
!AuthGPG::getAuthGPG()->isGPGAccepted(pgpId) )
if( !isSslOnlyFriend && pgpId != AuthPGP::getPgpOwnId() && !AuthPGP::isPGPAccepted(pgpId) )
{
RsFatal() << __PRETTY_FUNCTION__ << " pgpId: " << pgpId
<< " is not friend. It is very unlikely to happen at this "

View File

@ -797,8 +797,7 @@ int pqissllistener::completeConnection(int fd, IncomingSSLInfo& info)
exit(failure);
}
if( !isSslOnlyFriend && pgpId != AuthGPG::getAuthGPG()->getGPGOwnId() &&
!AuthGPG::getAuthGPG()->isGPGAccepted(pgpId) )
if( !isSslOnlyFriend && pgpId != AuthPGP::getPgpOwnId() && !AuthPGP::isPGPAccepted(pgpId) )
{
RsFatal() << __PRETTY_FUNCTION__ << " pgpId: " << pgpId
<< " is not friend. It is very unlikely to happen at this "

View File

@ -1,16 +1,43 @@
/******************************* BEGIN WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef WINDOWS_SYS
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#else
#include <ws2tcpip.h>
// Missing defines in MinGW
#ifndef MSG_WAITALL
#define MSG_WAITALL 8
#endif
#endif
/********************************* END WINDOWS/UNIX SPECIFIC PART ******************/
#include <string.h>
#include <iostream>
#include "rstcpsocket.h"
RsTcpSocket::RsTcpSocket(const std::string& tcp_address,uint16_t tcp_port)
:RsFdBinInterface(0),mState(DISCONNECTED),mConnectAddress(tcp_address),mConnectPort(tcp_port),mSocket(0)
:RsFdBinInterface(0, true),mState(DISCONNECTED),mConnectAddress(tcp_address),mConnectPort(tcp_port),mSocket(0)
{
}
int RsTcpSocket::connect()
RsTcpSocket::RsTcpSocket()
:RsFdBinInterface(0, true),mState(DISCONNECTED),mConnectAddress("0.0.0.0"),mConnectPort(0),mSocket(0)
{
}
bool RsTcpSocket::connect(const std::string& tcp_address,uint16_t tcp_port)
{
if(mState == CONNECTED)
close();
mConnectPort = tcp_port;
mConnectAddress = tcp_address;
return connect();
}
bool RsTcpSocket::connect()
{
int CreateSocket = 0;
char dataReceived[1024];
@ -23,18 +50,19 @@ int RsTcpSocket::connect()
printf("Socket not created \n");
return false;
}
ipOfServer.sin_family = AF_INET;
ipOfServer.sin_port = htons(mConnectPort);
ipOfServer.sin_addr.s_addr = inet_addr(mConnectAddress.c_str());
if(::connect(mSocket, (struct sockaddr *)&ipOfServer, sizeof(ipOfServer))<0)
if(::connect(CreateSocket, (struct sockaddr *)&ipOfServer, sizeof(ipOfServer))<0)
{
printf("Connection failed due to port and ip problems, or server is not available\n");
printf("Connection failed due to port and ip problems, or server is not available. Socket=%d,ConnectPort=%d,ConnectAddress=%s Errno=%d\n",mSocket,mConnectPort,mConnectAddress.c_str(),errno);
return false;
}
mState = CONNECTED;
setSocket(mSocket);
unix_fcntl_nonblock(CreateSocket);
setSocket(CreateSocket);
return true;
}
@ -42,6 +70,7 @@ int RsTcpSocket::connect()
int RsTcpSocket::close()
{
RsFdBinInterface::close();
mState = DISCONNECTED;
return !::close(mSocket);
}
@ -50,16 +79,12 @@ RsThreadedTcpSocket::RsThreadedTcpSocket(const std::string& tcp_address,uint16_t
: RsTcpSocket(tcp_address,tcp_port)
{
}
RsThreadedTcpSocket::RsThreadedTcpSocket() : RsTcpSocket()
{
}
void RsThreadedTcpSocket::run()
{
if(!connect())
{
RsErr() << "Cannot connect socket to " << connectAddress() << ":" << connectPort() ;
return ;
}
while(connectionState() == CONNECTED)
while(!shouldStop() && connectionState() == CONNECTED)
{
tick();
std::this_thread::sleep_for(std::chrono::milliseconds(200));

View File

@ -6,6 +6,8 @@ class RsTcpSocket: public RsFdBinInterface
{
public:
RsTcpSocket(const std::string& tcp_address,uint16_t tcp_port);
RsTcpSocket();
virtual ~RsTcpSocket()=default;
enum State: uint8_t {
UNKNOWN = 0x00,
@ -13,8 +15,10 @@ public:
CONNECTED = 0x02
};
// Return 1 when OK, 0 otherwise.
int connect();
// Return true when OK, false otherwise.
bool connect();
bool connect(const std::string& tcp_address,uint16_t tcp_port);
// Returns 1 when OK, 0 otherwise.
int close();
@ -34,6 +38,7 @@ class RsThreadedTcpSocket: public RsTcpSocket, public RsThread
{
public:
RsThreadedTcpSocket(const std::string& tcp_address,uint16_t tcp_port);
RsThreadedTcpSocket();
virtual ~RsThreadedTcpSocket();
virtual void run() override;

View File

@ -103,10 +103,16 @@ enum class RsEventType : uint32_t
/// @see rspeers.h
NETWORK = 16,
/// @see RsMailTagEvent
MAIL_TAG = 17,
/** Emitted to update library clients about file hashing being completed */
FILE_HASHING_COMPLETED = 20,
__MAX /// Used internally, keep last
/// @see rspeers.h
TOR_MANAGER = 21,
__MAX /// Used internally, keep last
};
enum class RsEventsErrorNum : int32_t

View File

@ -450,6 +450,17 @@ public:
const std::string& matchString,
std::vector<RsGxsSearchResult>& searchResults ) = 0;
/**
* @brief Request Synchronization with available peers
* Usually syncronization already happen automatically so be carefull
* to call this method only if necessary.
* It has been thinked for use cases like mobile phone where internet
* connection is intermittent and calling this may be useful when a system
* event about connection being available or about to go offline is received
* @jsonapi{development}
* @return Success or error details
*/
virtual std::error_condition requestSynchronization() = 0;
////////////////////////////////////////////////////////////////////////////
/* Following functions are deprecated and should not be considered a stable

View File

@ -159,6 +159,7 @@ public:
ERR_ALREADY_RUNNING, /// Another istance is running already
ERR_CANT_ACQUIRE_LOCK, /// Another istance is already running?
ERR_NO_AVAILABLE_ACCOUNT, /// Used in retroshare-service -U list when no account is available
ERR_CANNOT_CONFIGURE_TOR, /// cannot start/configure Tor for an auto-tor node
ERR_UNKNOWN /// Unkown error, maybe password is wrong?
};
@ -184,6 +185,7 @@ public:
static bool isPortable();
static bool isWindowsXP();
static bool collectEntropy(uint32_t bytes) ;
static bool startAutoTor();
/*!
* \brief lockFilePath
@ -218,6 +220,7 @@ public:
static void setAutoLogin(bool autoLogin);
static bool RsClearAutoLogin() ;
static std::string executablePath() ;
private:
/** @brief Lock profile directory
* param[in] accountDir account directory to lock

View File

@ -308,6 +308,9 @@ enum class RsMailStatusEventCode: uint8_t
/// An error occurred attempting to sign the message
SIGNATURE_FAILED = 0x04,
MESSAGE_CHANGED = 0x05,
TAG_CHANGED = 0x06,
};
struct RsMailStatusEvent : RsEvent
@ -329,6 +332,32 @@ struct RsMailStatusEvent : RsEvent
~RsMailStatusEvent() override = default;
};
enum class RsMailTagEventCode: uint8_t
{
TAG_ADDED = 0x00,
TAG_CHANGED = 0x01,
TAG_REMOVED = 0x02,
};
struct RsMailTagEvent : RsEvent
{
RsMailTagEvent() : RsEvent(RsEventType::MAIL_TAG) {}
RsMailTagEventCode mMailTagEventCode;
std::set<std::string> mChangedMsgTagIds;
/// @see RsEvent
void serial_process( RsGenericSerializer::SerializeJob j,
RsGenericSerializer::SerializeContext& ctx) override
{
RsEvent::serial_process(j, ctx);
RS_SERIAL_PROCESS(mChangedMsgTagIds);
RS_SERIAL_PROCESS(mMailTagEventCode);
}
~RsMailTagEvent() override = default;
};
#define RS_CHAT_PUBLIC 0x0001
#define RS_CHAT_PRIVATE 0x0002
#define RS_CHAT_AVATAR_AVAILABLE 0x0004

View File

@ -128,14 +128,12 @@ const uint32_t RS_MESSAGE_CONNECT_ATTEMPT = 0x0001;
const int NOTIFY_LIST_NEIGHBOURS = 1;
const int NOTIFY_LIST_FRIENDS = 2;
const int NOTIFY_LIST_SEARCHLIST = 4;
const int NOTIFY_LIST_MESSAGELIST = 5;
const int NOTIFY_LIST_CHANNELLIST = 6;
const int NOTIFY_LIST_TRANSFERLIST = 7;
const int NOTIFY_LIST_CONFIG = 8;
const int NOTIFY_LIST_DIRLIST_LOCAL = 9;
const int NOTIFY_LIST_DIRLIST_FRIENDS = 10;
const int NOTIFY_LIST_FORUMLIST_LOCKED = 11; // use connect with Qt::QueuedConnection
const int NOTIFY_LIST_MESSAGE_TAGS = 12;
const int NOTIFY_LIST_PUBLIC_CHAT = 13;
const int NOTIFY_LIST_PRIVATE_INCOMING_CHAT = 14;
const int NOTIFY_LIST_PRIVATE_OUTGOING_CHAT = 15;

View File

@ -887,7 +887,7 @@ public:
// Certificate utils
virtual bool cleanCertificate(
const std::string& certstr, std::string& cleanCert,
bool& is_short_format, uint32_t& error_code ) = 0;
bool& is_short_format, uint32_t& error_code, RsPeerDetails& details) = 0;
virtual std::string saveCertificateToString(const RsPeerId &id) = 0;
virtual bool signGPGCertificate(const RsPgpId &gpg_id,const std::string& gpg_passphrase) = 0;

View File

@ -0,0 +1,182 @@
#pragma once
// This code stands as an interface for the automatic configuration
// of Tor hidden services to be used by retroshare.
//
// The correct way to use it is to:
//
// 1 - properly set data and hidden service directories. This allowd the TorManager
// to save its keys for the hidden service, or to load one that has previously been created
//
// 2 - call setupHiddenService(). This creates/loads the hidden service.
//
// 3 - call RsTor::start()
//
// 4 - loop/wait until RsTor::getHiddenServiceStatus(service_id)
// returns RsTorHiddenServiceStatus::ONLINE
//
// 5 - call RsTor::getHiddenserviceInfo to properly setup RS internal ports and addresses:
//
// RsTor::getHiddenServiceInfo(service_id,onion_address,service_port,service_target_address,service_target_port);
// RsTor::getProxyServerInfo(proxy_server_address,proxy_server_port) ;
//
// rsPeers->setLocalAddress(rsPeers->getOwnId(), service_target_address, service_target_port);
// rsPeers->setHiddenNode(rsPeers->getOwnId(), onion_address, service_port);
// rsPeers->setProxyServer(RS_HIDDEN_TYPE_TOR, proxy_server_address,proxy_server_port) ;
#include "retroshare/rsevents.h"
namespace Tor {
class TorManager;
}
enum class RsTorManagerEventCode: uint8_t
{
UNKNOWN = 0x00,
TOR_STATUS_CHANGED = 0x01,
BOOTSTRAP_STATUS_CHANGED = 0x02,
TOR_CONNECTIVITY_CHANGED = 0x03,
TOR_MANAGER_ERROR = 0x04,
CONFIGURATION_NEEDED = 0x05,
TOR_MANAGER_STOPPED = 0x06,
};
// Status of the Tor hidden service setup/loaded by RS
enum class RsTorHiddenServiceStatus: uint8_t {
ERROR = 0x00,
NOT_CREATED = 0x01,
OFFLINE = 0x02,
ONLINE = 0x03
};
// Status of the connection/authentication between RS and the Tor service
enum class RsTorConnectivityStatus: uint8_t {
ERROR = 0x00,
NOT_CONNECTED = 0x01,
CONNECTING = 0x02,
SOCKET_CONNECTED = 0x03,
AUTHENTICATING = 0x04,
AUTHENTICATED = 0x05,
HIDDEN_SERVICE_READY = 0x06,
UNKNOWN = 0x07
};
// Status of the Tor service with which RS is talking.
enum class RsTorStatus: uint8_t {
UNKNOWN = 0x00,
OFFLINE = 0x01,
READY = 0x02
};
struct RsTorManagerEvent: public RsEvent
{
RsTorManagerEvent(): RsEvent(RsEventType::TOR_MANAGER),
mTorManagerEventType(RsTorManagerEventCode::UNKNOWN)
{}
RsTorManagerEventCode mTorManagerEventType;
RsTorConnectivityStatus mTorConnectivityStatus;
RsTorStatus mTorStatus;
std::string mErrorMessage;
///* @see RsEvent @see RsSerializable
void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) override
{
RsEvent::serial_process(j, ctx);
RS_SERIAL_PROCESS(mTorManagerEventType);
RS_SERIAL_PROCESS(mTorConnectivityStatus);
RS_SERIAL_PROCESS(mTorStatus);
RS_SERIAL_PROCESS(mErrorMessage);
}
~RsTorManagerEvent() = default;
};
class RsTor
{
public:
/*!
* \brief isTorAvailable
* \return true if a Tor executble has been found. False otherwise.
*/
static bool isTorAvailable() ;
/*!
* \brief torStatus
* \return Status of the Tor service used by RS
*/
static RsTorStatus torStatus() ;
/*!
* \brief torConnectivityStatus
* \return Status of the connectivity/authentication between RS and Tor
*/
static RsTorConnectivityStatus torConnectivityStatus() ;
static void setTorDataDirectory(const std::string& dir);
static void setHiddenServiceDirectory(const std::string& dir);
static bool setupHiddenService();
/*!
* \brief getProxyServerInfo
* \param server_address Address of the proxy used by RS to send data in the Tor network. Usually 127.0.0.1
* \param server_port Port of the proxy used by RS to send data in the Tor network. Usually 9050.
*/
static void getProxyServerInfo(std::string& server_address, uint16_t& server_port);
/*!
* \brief getHiddenServiceStatus
* \param service_id onion address of the hidden service (if the service is OFFLINE or ONLINE)
* \return Status of the created/loaded hidden service.
*/
static RsTorHiddenServiceStatus getHiddenServiceStatus(std::string& service_id);
/*!
* \brief start
* Launches the Tor management threads.
*/
static bool start();
/*!
* \brief stop
* Stop the Tor management threads.
*/
static void stop();
/*!
* \brief getHiddenServiceInfo
* Gets information about the hidden service setup by RS to run.
* \param service_id
* \param service_onion_address
* \param service_port
* \param service_target_address
* \param target_port
* \return
*/
static bool getHiddenServiceInfo(std::string& service_id,
std::string& service_onion_address,
uint16_t& service_port,
std::string& service_target_address,
uint16_t& target_port);
/*!
* \brief bootstrapStatus
* \return Log messages of the Tor bootstrapping status.
*/
static std::map<std::string,std::string> bootstrapStatus();
static std::list<std::string> logMessages();
static std::string socksAddress();
static uint16_t socksPort();
static bool hasError();
static std::string errorMessage();
private:
static Tor::TorManager *instance();
};

View File

@ -0,0 +1,75 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LOCAL_ARRAY_H_included
#define LOCAL_ARRAY_H_included
#include <cstddef>
#include <new>
/**
* A fixed-size array with a size hint. That number of bytes will be allocated
* on the stack, and used if possible, but if more bytes are requested at
* construction time, a buffer will be allocated on the heap (and deallocated
* by the destructor).
*
* The API is intended to be a compatible subset of C++0x's std::array.
*/
template <size_t STACK_BYTE_COUNT>
class LocalArray {
public:
/**
* Allocates a new fixed-size array of the given size. If this size is
* less than or equal to the template parameter STACK_BYTE_COUNT, an
* internal on-stack buffer will be used. Otherwise a heap buffer will
* be allocated.
*/
LocalArray(size_t desiredByteCount) : mSize(desiredByteCount) {
if (desiredByteCount > STACK_BYTE_COUNT) {
mPtr = new char[mSize];
} else {
mPtr = &mOnStackBuffer[0];
}
}
/**
* Frees the heap-allocated buffer, if there was one.
*/
~LocalArray() {
if (mPtr != &mOnStackBuffer[0]) {
delete[] mPtr;
}
}
// Capacity.
size_t size() { return mSize; }
bool empty() { return mSize == 0; }
// Element access.
char& operator[](size_t n) { return mPtr[n]; }
const char& operator[](size_t n) const { return mPtr[n]; }
private:
char mOnStackBuffer[STACK_BYTE_COUNT];
char* mPtr;
size_t mSize;
// Disallow copy and assignment.
LocalArray(const LocalArray&);
void operator=(const LocalArray&);
};
#endif // LOCAL_ARRAY_H_included

View File

@ -0,0 +1,49 @@
= README Android ifaddrs
Android API level < 24 doesn't provide `getifaddrs` and related functions, we
have tested multiple ways to overcome this issue.
== Non Weorking alternative implementations
https://github.com/kmackay/android-ifaddrs
https://github.com/morristech/android-ifaddrs
https://www.openhub.net/p/android-ifaddrs/
Compiles but then segfault at runtime.
== Qt implementation
Using `QNetworkInterface::allAddresses()` provided by Qt works but at time of
writing (Q4 2021) on newer Android the log is flooded by warnings as we reported
here
https://bugreports.qt.io/browse/QTBUG-78659
plus depending on Qt networking module just for this is frustrating.
Update: the warning flood seems have been fixed in later Qt versions
https://bugreports.qt.io/browse/QTBUG-86394
This solution was the first working we implemented in our code it has been
removed to avoid dependency on Qt, as lighter alternatives are possible.
== Code copied from Android Gingerbread release
As explained here
https://stackoverflow.com/a/57112520
even older Android have `getifaddrs` implementations but doesn't make them
accessible in the API, in particular the one included in Android Gingerbread
https://android.googlesource.com/platform/libcore/+/refs/heads/gingerbread-release/luni/src/main/native/ifaddrs-android.h
https://android.googlesource.com/platform/libcore/+/refs/heads/gingerbread-release/
is particularly easy to include in our code base and compile.
This solution seems the best fitting and doesn't introduce dependency on Qt.
Newer Android releases (expecially 11) have introduced multiple restrictions
on network information access so we suggest you to prepare different APK for
different API level in order to use the `getifaddrs` provided by Android NDK
which deal gracefully with those restrictions as soon as available.

View File

@ -0,0 +1,46 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SCOPED_FD_H_included
#define SCOPED_FD_H_included
#include <unistd.h>
// A smart pointer that closes the given fd on going out of scope.
// Use this when the fd is incidental to the purpose of your function,
// but needs to be cleaned up on exit.
class ScopedFd {
public:
explicit ScopedFd(int fd) : fd(fd) {
}
~ScopedFd() {
close(fd);
}
int get() const {
return fd;
}
private:
int fd;
// Disallow copy and assignment.
ScopedFd(const ScopedFd&);
void operator=(const ScopedFd&);
};
#endif // SCOPED_FD_H_included

View File

@ -1,9 +1,9 @@
/*******************************************************************************
* libretroshare/src/util: androiddebug.h *
* *
* libretroshare: retroshare core library *
* *
* Copyright (C) 2016 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2016-2021 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
@ -36,10 +36,10 @@
* class at the beginning of the main of your program to get them (stdout and
* stderr) on logcat output.
*/
class AndroidStdIOCatcher
class AndroidCoutCerrCatcher
{
public:
AndroidStdIOCatcher(const std::string& dTag = "RetroShare",
AndroidCoutCerrCatcher(const std::string& dTag = "RetroShare",
android_LogPriority stdout_pri = ANDROID_LOG_INFO,
android_LogPriority stderr_pri = ANDROID_LOG_ERROR) :
tag(dTag), cout_pri(stdout_pri), cerr_pri(stderr_pri), should_stop(false)
@ -63,10 +63,10 @@ public:
pthread_detach(thr);
}
~AndroidStdIOCatcher()
~AndroidCoutCerrCatcher()
{
should_stop = true;
pthread_join(thr, NULL);
pthread_join(thr, nullptr);
}
private:
@ -79,11 +79,13 @@ private:
pthread_t thr;
std::atomic<bool> should_stop;
static void *thread_func(void* instance)
static void* thread_func(void* instance)
{
__android_log_write(ANDROID_LOG_INFO, "RetroShare", "Android debugging start");
__android_log_write(
ANDROID_LOG_INFO, "RetroShare",
"Android standard I/O catcher start" );
AndroidStdIOCatcher &i = *static_cast<AndroidStdIOCatcher*>(instance);
AndroidCoutCerrCatcher &i = *static_cast<AndroidCoutCerrCatcher*>(instance);
std::string out_buf;
std::string err_buf;
@ -113,9 +115,11 @@ private:
usleep(10000);
}
__android_log_write(ANDROID_LOG_INFO, "RetroShare", "Android debugging stop");
__android_log_write(
ANDROID_LOG_INFO, "RetroShare",
"Android standard I/O catcher stop" );
return NULL;
return nullptr;
}
};

View File

@ -0,0 +1,42 @@
/*******************************************************************************
* *
* libretroshare: retroshare core library *
* *
* Copyright (C) 2021 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#include "rs_android/rsjni.hpp"
namespace jni
{
Local<Object<RsJni::ErrorConditionWrap>> MakeAnything(
ThingToMake<RsJni::ErrorConditionWrap>, JNIEnv& env,
const std::error_condition& ec )
{
auto& clazz = jni::Class<RsJni::ErrorConditionWrap>::Singleton(env);
static auto method =
clazz.GetConstructor<jni::jint, jni::String, jni::String>(env);
jni::jint value = ec.value();
auto message = jni::Make<jni::String>(env, ec.message());
auto category = jni::Make<jni::String>(env, ec.category().name());
return clazz.New(env, method, value, message, category);
}
}

View File

@ -0,0 +1,228 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef IFADDRS_ANDROID_H_included
#define IFADDRS_ANDROID_H_included
#include <arpa/inet.h>
#include <cstring>
#include <errno.h>
#include <net/if.h>
#include <netinet/in.h>
#include <new>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include "LocalArray.h"
#include "ScopedFd.h"
// Android (bionic) doesn't have getifaddrs(3)/freeifaddrs(3).
// We fake it here, so java_net_NetworkInterface.cpp can use that API
// with all the non-portable code being in this file.
// Source-compatible subset of the BSD struct.
struct ifaddrs {
// Pointer to next struct in list, or NULL at end.
ifaddrs* ifa_next;
// Interface name.
char* ifa_name;
// Interface flags.
unsigned int ifa_flags;
// Interface network address.
sockaddr* ifa_addr;
// Interface netmask.
sockaddr* ifa_netmask;
ifaddrs(ifaddrs* next)
: ifa_next(next), ifa_name(NULL), ifa_flags(0), ifa_addr(NULL), ifa_netmask(NULL)
{
}
~ifaddrs() {
delete ifa_next;
delete[] ifa_name;
delete ifa_addr;
delete ifa_netmask;
}
// Sadly, we can't keep the interface index for portability with BSD.
// We'll have to keep the name instead, and re-query the index when
// we need it later.
bool setNameAndFlagsByIndex(int interfaceIndex) {
// Get the name.
char buf[IFNAMSIZ];
char* name = if_indextoname(interfaceIndex, buf);
if (name == NULL) {
return false;
}
ifa_name = new char[strlen(name) + 1];
strcpy(ifa_name, name);
// Get the flags.
ScopedFd fd(socket(AF_INET, SOCK_DGRAM, 0));
if (fd.get() == -1) {
return false;
}
ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, name);
int rc = ioctl(fd.get(), SIOCGIFFLAGS, &ifr);
if (rc == -1) {
return false;
}
ifa_flags = ifr.ifr_flags;
return true;
}
// Netlink gives us the address family in the header, and the
// sockaddr_in or sockaddr_in6 bytes as the payload. We need to
// stitch the two bits together into the sockaddr that's part of
// our portable interface.
void setAddress(int family, void* data, size_t byteCount) {
// Set the address proper...
sockaddr_storage* ss = new sockaddr_storage;
memset(ss, 0, sizeof(*ss));
ifa_addr = reinterpret_cast<sockaddr*>(ss);
ss->ss_family = family;
uint8_t* dst = sockaddrBytes(family, ss);
memcpy(dst, data, byteCount);
}
// Netlink gives us the prefix length as a bit count. We need to turn
// that into a BSD-compatible netmask represented by a sockaddr*.
void setNetmask(int family, size_t prefixLength) {
// ...and work out the netmask from the prefix length.
sockaddr_storage* ss = new sockaddr_storage;
memset(ss, 0, sizeof(*ss));
ifa_netmask = reinterpret_cast<sockaddr*>(ss);
ss->ss_family = family;
uint8_t* dst = sockaddrBytes(family, ss);
memset(dst, 0xff, prefixLength / 8);
if ((prefixLength % 8) != 0) {
dst[prefixLength/8] = (0xff << (8 - (prefixLength % 8)));
}
}
// Returns a pointer to the first byte in the address data (which is
// stored in network byte order).
uint8_t* sockaddrBytes(int family, sockaddr_storage* ss) {
if (family == AF_INET) {
sockaddr_in* ss4 = reinterpret_cast<sockaddr_in*>(ss);
return reinterpret_cast<uint8_t*>(&ss4->sin_addr);
} else if (family == AF_INET6) {
sockaddr_in6* ss6 = reinterpret_cast<sockaddr_in6*>(ss);
return reinterpret_cast<uint8_t*>(&ss6->sin6_addr);
}
return NULL;
}
private:
// Disallow copy and assignment.
ifaddrs(const ifaddrs&);
void operator=(const ifaddrs&);
};
// FIXME: use iovec instead.
struct addrReq_struct {
nlmsghdr netlinkHeader;
ifaddrmsg msg;
};
inline bool sendNetlinkMessage(int fd, const void* data, size_t byteCount) {
ssize_t sentByteCount = TEMP_FAILURE_RETRY(send(fd, data, byteCount, 0));
return (sentByteCount == static_cast<ssize_t>(byteCount));
}
inline ssize_t recvNetlinkMessage(int fd, char* buf, size_t byteCount) {
return TEMP_FAILURE_RETRY(recv(fd, buf, byteCount, 0));
}
// Source-compatible with the BSD function.
inline int getifaddrs(ifaddrs** result) {
// Simplify cleanup for callers.
*result = NULL;
// Create a netlink socket.
ScopedFd fd(socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE));
if (fd.get() < 0) {
return -1;
}
// Ask for the address information.
addrReq_struct addrRequest;
memset(&addrRequest, 0, sizeof(addrRequest));
addrRequest.netlinkHeader.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH;
addrRequest.netlinkHeader.nlmsg_type = RTM_GETADDR;
addrRequest.netlinkHeader.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(addrRequest)));
addrRequest.msg.ifa_family = AF_UNSPEC; // All families.
addrRequest.msg.ifa_index = 0; // All interfaces.
if (!sendNetlinkMessage(fd.get(), &addrRequest, addrRequest.netlinkHeader.nlmsg_len)) {
return -1;
}
// Read the responses.
LocalArray<0> buf(65536); // We don't necessarily have std::vector.
ssize_t bytesRead;
while ((bytesRead = recvNetlinkMessage(fd.get(), &buf[0], buf.size())) > 0) {
nlmsghdr* hdr = reinterpret_cast<nlmsghdr*>(&buf[0]);
for (; NLMSG_OK(hdr, (size_t)bytesRead); hdr = NLMSG_NEXT(hdr, bytesRead)) {
switch (hdr->nlmsg_type) {
case NLMSG_DONE:
return 0;
case NLMSG_ERROR:
return -1;
case RTM_NEWADDR:
{
ifaddrmsg* address = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(hdr));
rtattr* rta = IFA_RTA(address);
size_t ifaPayloadLength = IFA_PAYLOAD(hdr);
while (RTA_OK(rta, ifaPayloadLength)) {
if (rta->rta_type == IFA_LOCAL) {
int family = address->ifa_family;
if (family == AF_INET || family == AF_INET6) {
*result = new ifaddrs(*result);
if (!(*result)->setNameAndFlagsByIndex(address->ifa_index)) {
return -1;
}
(*result)->setAddress(family, RTA_DATA(rta), RTA_PAYLOAD(rta));
(*result)->setNetmask(family, address->ifa_prefixlen);
}
}
rta = RTA_NEXT(rta, ifaPayloadLength);
}
}
break;
}
}
}
// We only get here if recv fails before we see a NLMSG_DONE.
return -1;
}
// Source-compatible with the BSD function.
inline void freeifaddrs(ifaddrs* addresses) {
delete addresses;
}
#endif // IFADDRS_ANDROID_H_included

View File

@ -0,0 +1,98 @@
/*
* RetroShare
* Copyright (C) 2021 Gioacchino Mazzurco <gio@eigenlab.org>
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-FileCopyrightText: Retroshare Team <contact@retroshare.cc>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package org.retroshare.service;
import android.util.Log;
import android.content.Context;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
public class AssetHelper
{
public static boolean copyAsset(
Context ctx, String assetPath, String destinationFilePath )
{
Log.d(TAG, "copyAsset " + assetPath + " -> " + destinationFilePath);
InputStream in;
OutputStream out;
try { in = ctx.getAssets().open(assetPath); }
catch(Exception e)
{
Log.e(
TAG,
"Failure opening asset: " + assetPath + " " + e.getMessage() );
return false;
}
try { out = new FileOutputStream(destinationFilePath); }
catch(Exception e)
{
Log.e(
TAG,
"Failure opening destination: " + destinationFilePath + " " +
e.getMessage() );
return false;
}
try
{
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
}
catch(IOException e)
{
Log.e(
TAG,
"Failure coping: " + assetPath + " -> " + destinationFilePath +
" " + e.getMessage() );
return false;
}
try { in.close(); }
catch(IOException e)
{
Log.e(TAG, "Failure closing: " + assetPath + " " + e.getMessage() );
return false;
}
try { out.close(); }
catch(IOException e)
{
Log.e(
TAG,
"Failure closing: " + destinationFilePath + " " +
e.getMessage() );
return false;
}
return true;
}
private static final String TAG = "RetroShare AssetHelper.java";
}

View File

@ -0,0 +1,48 @@
/*
* RetroShare
* Copyright (C) 2021 Gioacchino Mazzurco <gio@eigenlab.org>
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-FileCopyrightText: Retroshare Team <contact@retroshare.cc>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package org.retroshare.service;
public class ErrorConditionWrap
{
public ErrorConditionWrap(
int value, String message, String categoryName )
{
mValue = value;
mMessage = message;
mCategoryName = categoryName;
}
public int value() { return mValue; }
public String message() { return mMessage; }
public String categoryName() { return mCategoryName; }
public boolean toBool() { return mValue != 0; }
@Override
public String toString()
{ return String.format("%d", mValue)+" "+mMessage+" [" + mCategoryName+ "]"; }
private int mValue = 0;
private String mMessage;
private String mCategoryName;
}

View File

@ -0,0 +1,141 @@
/*
* RetroShare
* Copyright (C) 2016-2021 Gioacchino Mazzurco <gio@eigenlab.org>
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-FileCopyrightText: Retroshare Team <contact@retroshare.cc>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package org.retroshare.service;
import android.app.Service;
import android.os.IBinder;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.app.ActivityManager;
public class RetroShareServiceAndroid extends Service
{
public static final int DEFAULT_JSON_API_PORT = 9092;
public static final String DEFAULT_JSON_API_BINDING_ADDRESS = "127.0.0.1";
static { System.loadLibrary("retroshare-service"); }
public static void start(
Context ctx, int jsonApiPort, String jsonApiBindAddress )
{
Log.d(TAG, "start");
Intent intent = new Intent(ctx, RetroShareServiceAndroid.class);
intent.putExtra(JSON_API_PORT_KEY, jsonApiPort);
intent.putExtra(JSON_API_BIND_ADDRESS_KEY, jsonApiBindAddress);
ctx.startService(intent);
}
public static void stop(Context ctx)
{
Log.d(TAG, "stop");
ctx.stopService(new Intent(ctx, RetroShareServiceAndroid.class));
}
public static boolean isRunning(Context ctx)
{
Log.d(TAG, "isRunning");
ActivityManager manager =
(ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
for( ActivityManager.RunningServiceInfo service :
manager.getRunningServices(Integer.MAX_VALUE) )
if( RetroShareServiceAndroid.class.getName()
.equals(service.service.getClassName()) )
return true;
return false;
}
public static Context getServiceContext()
{
if(sServiceContext == null)
{
Log.e(TAG, "getServiceContext() called before onCreate");
throw new NullPointerException();
}
return sServiceContext;
}
@Override
public int onStartCommand(
Intent intent, int flags, int startId )
{
if(intent == null)
{
Log.i(TAG, "onStartCommand called without intent");
return Service.START_REDELIVER_INTENT;
}
int jsonApiPort = DEFAULT_JSON_API_PORT;
String jsonApiBindAddress = DEFAULT_JSON_API_BINDING_ADDRESS;
Bundle args = intent.getExtras();
if(args.containsKey(JSON_API_PORT_KEY))
jsonApiPort = args.getInt(JSON_API_PORT_KEY);
if(args.containsKey(JSON_API_BIND_ADDRESS_KEY))
jsonApiBindAddress =
args.getString(JSON_API_BIND_ADDRESS_KEY);
ErrorConditionWrap ec = nativeStart(jsonApiPort, jsonApiBindAddress);
if(ec.toBool()) Log.e(TAG, "onStartCommand(...) " + ec.toString());
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate ()
{
super.onCreate();
sServiceContext = this;
}
@Override
public void onDestroy()
{
ErrorConditionWrap ec = nativeStop();
if(ec.toBool()) Log.e(TAG, "onDestroy() " + ec.toString());
sServiceContext = null;
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) { return null; }
private static final String JSON_API_PORT_KEY =
RetroShareServiceAndroid.class.getCanonicalName() +
"/JSON_API_PORT_KEY";
private static final String JSON_API_BIND_ADDRESS_KEY =
RetroShareServiceAndroid.class.getCanonicalName() +
"/JSON_API_BIND_ADDRESS_KEY" ;
private static final String TAG = "RetroShareServiceAndroid.java";
private static Context sServiceContext;
protected static native ErrorConditionWrap nativeStart(
int jsonApiPort, String jsonApiBindAddress );
protected static native ErrorConditionWrap nativeStop();
}

View File

@ -0,0 +1,103 @@
/*
* RetroShare Service Android
* Copyright (C) 2016-2021 Gioacchino Mazzurco <gio@eigenlab.org>
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-FileCopyrightText: Retroshare Team <contact@retroshare.cc>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <limits>
#include <cstdint>
#include "util/stacktrace.h"
#include "retroshare/rsinit.h"
#include "retroshare/rsiface.h"
#include "util/rsdebug.h"
#include "rs_android/retroshareserviceandroid.hpp"
#include "rs_android/rsjni.hpp"
/*static*/ std::unique_ptr<AndroidCoutCerrCatcher>
RetroShareServiceAndroid::sAndroidCoutCerrCatcher = nullptr;
using ErrorConditionWrap = RsJni::ErrorConditionWrap;
/*static*/ jni::Local<jni::Object<ErrorConditionWrap>>
RetroShareServiceAndroid::start(
JNIEnv& env, jni::Class<RetroShareServiceAndroid>&,
jni::jint jsonApiPort, const jni::String& jsonApiBindAddress )
{
if(jsonApiPort < 0 || jsonApiPort > std::numeric_limits<uint16_t>::max())
{
RS_ERR("Got invalid JSON API port: ", jsonApiPort);
return jni::Make<ErrorConditionWrap>(env, std::errc::invalid_argument);
}
RsInfo() << "\n" <<
"+================================================================+\n"
"| o---o o |\n"
"| \\ / - Retroshare Service Android - / \\ |\n"
"| o o---o |\n"
"+================================================================+"
<< std::endl << std::endl;
sAndroidCoutCerrCatcher = std::make_unique<AndroidCoutCerrCatcher>();
RsInit::InitRsConfig();
RsControl::earlyInitNotificationSystem();
RsConfigOptions conf;
conf.jsonApiPort = static_cast<uint16_t>(jsonApiPort);
conf.jsonApiBindAddress = jni::Make<std::string>(env, jsonApiBindAddress);
// Dirty workaround plugins not supported on Android ATM
conf.main_executable_path = " ";
int initResult = RsInit::InitRetroShare(conf);
if(initResult != RS_INIT_OK)
{
RS_ERR("Retroshare core initalization failed with: ", initResult);
return jni::Make<ErrorConditionWrap>(env, std::errc::no_child_process);
}
return jni::Make<ErrorConditionWrap>(env, std::error_condition());
}
jni::Local<jni::Object<ErrorConditionWrap>> RetroShareServiceAndroid::stop(
JNIEnv& env, jni::Class<RetroShareServiceAndroid>& )
{
if(RsControl::instance()->isReady())
{
RsControl::instance()->rsGlobalShutDown();
return jni::Make<ErrorConditionWrap>(env, std::error_condition());
}
sAndroidCoutCerrCatcher.reset();
return jni::Make<ErrorConditionWrap>(env, std::errc::no_such_process);
}
jni::Local<jni::Object<RetroShareServiceAndroid::Context> >
RetroShareServiceAndroid::getAndroidContext(JNIEnv& env)
{
auto& clazz = jni::Class<RetroShareServiceAndroid>::Singleton(env);
static auto method =
clazz.GetStaticMethod<jni::Object<RetroShareServiceAndroid::Context>()>(
env, "getServiceContext" );
return clazz.Call(env, method);
}

View File

@ -0,0 +1,84 @@
/*
* RetroShare Service Android
* Copyright (C) 2016-2021 Gioacchino Mazzurco <gio@eigenlab.org>
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-FileCopyrightText: Retroshare Team <contact@retroshare.cc>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <system_error>
#include <memory>
#include <jni/jni.hpp>
#include "rs_android/rsjni.hpp"
#include "rs_android/androidcoutcerrcatcher.hpp"
#include "util/stacktrace.h"
/** Provide native methods that are registered into corresponding Java class
* to start/stop RetroShare with reasonable comfort on Android platform */
struct RetroShareServiceAndroid
{
static constexpr auto Name()
{ return "org/retroshare/service/RetroShareServiceAndroid"; }
using ErrorConditionWrap = RsJni::ErrorConditionWrap;
/**
* Called from RetroShareServiceAndroid Java to init libretroshare
* @param[in] env the usual JNI parafernalia
* @param[in] jclass the usual JNI parafernalia
* @param[in] jsonApiPort port on which JSON API server will listen
* @param[in] jsonApiBindAddress binding address of the JSON API server
* @note Yeah you read it well we use a full 32 bit signed integer for JSON
* API port. This is because Java lack even the minimum decency to implement
* unsigned integral types so we need to wrap the port (16 bit unsigned
* integer everywhere reasonable) into a full integer and then check at
* runtime the value.
*/
static jni::Local<jni::Object<ErrorConditionWrap>> start(
JNIEnv& env, jni::Class<RetroShareServiceAndroid>& jclass,
jni::jint jsonApiPort, const jni::String& jsonApiBindAddress );
/**
* Called from RetroShareServiceAndroid Java to shutdown libretroshare
* @param[in] env the usual JNI parafernalia
* @param[in] jclass the usual JNI parafernalia
*/
static jni::Local<jni::Object<ErrorConditionWrap>> stop(
JNIEnv& env, jni::Class<RetroShareServiceAndroid>& );
struct Context
{
/// JNI parafernalia
static constexpr auto Name() { return "android/content/Context"; }
};
/// Return RetroShare Service Android Context
static jni::Local<jni::Object<Context>> getAndroidContext(JNIEnv& env);
private:
/** Doesn't involve complex liftime handling stuff better let the runtime
* handle costruction (ASAP)/destruction for us */
static CrashStackTrace CrashStackTrace;
/** Involve threads, file descriptors etc. better handle lifetime
* explicitely */
static std::unique_ptr<AndroidCoutCerrCatcher> sAndroidCoutCerrCatcher;
};

View File

@ -0,0 +1,70 @@
/*******************************************************************************
* RetroShare JNI utilities *
* *
* libretroshare: retroshare core library *
* *
* Copyright (C) 2021 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#include "rs_android/rsjni.hpp"
#include "rs_android/retroshareserviceandroid.hpp"
rs_view_ptr<JavaVM> RsJni::mJvm = nullptr;
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad_retroshare(JavaVM* vm, void*)
{
RS_DBG(vm);
RsJni::mJvm = vm;
jni::JNIEnv& env { jni::GetEnv(*vm) };
/** Ensure singleton refereces to our own Java classes are inizialized here
* because default Java class loader which is the one accessible by native
* threads which is not main even if attached, is not capable to find them.
* https://stackoverflow.com/questions/20752352/classnotfoundexception-when-finding-a-class-in-jni-background-thread
* https://groups.google.com/g/android-ndk/c/2gkr1mXKn_E */
jni::Class<RsJni::AssetHelper>::Singleton(env);
jni::Class<RsJni::ErrorConditionWrap>::Singleton(env);
jni::RegisterNatives(
env, *jni::Class<RetroShareServiceAndroid>::Singleton(env),
jni::MakeNativeMethod<
decltype(&RetroShareServiceAndroid::start),
&RetroShareServiceAndroid::start >("nativeStart"),
jni::MakeNativeMethod<
decltype(&RetroShareServiceAndroid::stop),
&RetroShareServiceAndroid::stop >("nativeStop")
);
return jni::Unwrap(jni::jni_version_1_2);
}
#ifdef RS_LIBRETROSHARE_EXPORT_JNI_ONLOAD
/** If libretroshare is linked statically to other components which already
* export JNI_OnLoad then a symbol clash may happen
* if RS_LIBRETROSHARE_EXPORT_JNI_ONLOAD is defined.
* @see JNI_OnLoad_retroshare should instead be called from the exported
* JNI_OnLoad */
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* _reserved)
{
RS_DBG(vm);
return JNI_OnLoad_retroshare(vm, _reserved);
}
#endif // def RS_LIBRETROSHARE_EXPORT_JNI_ONLOAD

View File

@ -0,0 +1,90 @@
/*******************************************************************************
* RetroShare JNI utilities *
* *
* libretroshare: retroshare core library *
* *
* Copyright (C) 2021 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#pragma once
#include <system_error>
#include <cstdlib>
#include <jni/jni.hpp>
#include "util/rsmemory.h"
#include "util/cxx23retrocompat.h"
/** Store JVM pointer safely and register native methods */
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad_retroshare(JavaVM* vm, void*);
/** Provide library wide JVM access with some safe measures
* The JVM pointer is set properly by @see JNI_OnLoad_retroshare
*/
class RsJni
{
public:
static inline JavaVM& getVM()
{
if(!mJvm) // [[unlikely]]
{
RS_FATAL( "Attempt to access JVM before JNI_OnLoad_retroshare ",
std::errc::bad_address );
print_stacktrace();
std::exit(std::to_underlying(std::errc::bad_address));
}
return *mJvm;
}
friend jint JNI_OnLoad_retroshare(JavaVM* vm, void*);
/** Provide a comfortable way to access Android package assets like
* bdboot.txt from C++ */
struct AssetHelper
{
static constexpr auto Name()
{ return "org/retroshare/service/AssetHelper"; }
};
/** Provide a comfortable way to propagate C++ error_conditions to Java
* callers */
struct ErrorConditionWrap
{
static constexpr auto Name()
{ return "org/retroshare/service/ErrorConditionWrap"; }
};
private:
static rs_view_ptr<JavaVM> mJvm;
};
namespace jni
{
/** Provides idiomatic way of creating instances via
@code{.cpp}
jni::Make<ErrorConditionWrap>(env, std::error_condition());
@endcode */
jni::Local<jni::Object<RsJni::ErrorConditionWrap>>
MakeAnything(
jni::ThingToMake<RsJni::ErrorConditionWrap>, JNIEnv& env,
const std::error_condition& ec );
}

View File

@ -3,7 +3,9 @@
* *
* libretroshare: retroshare core library *
* *
* Copyright 2011-2011 by Cyril Soler <csoler@users.sourceforge.net> *
* Copyright (C) 2011-2018 Cyril Soler <csoler@users.sourceforge.net> *
* Copyright (C) 2021 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
@ -21,7 +23,9 @@
*******************************************************************************/
#pragma once
#include <stdint.h>
#include <cstdint>
using RsItemPriority = uint8_t;
// This file centralises QoS priorities for all transfer RsItems
//

View File

@ -3,7 +3,8 @@
* *
* libretroshare: retroshare core library *
* *
* Copyright (C) 2018 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2018-2021 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
@ -28,6 +29,9 @@
#include "serialiser/rsserializer.h"
#include "serialiser/rsserializable.h"
#include "util/stacktrace.h"
#include "rsitems/itempriorities.h"
#include "rsitems/rsserviceids.h"
#include <typeinfo>
@ -42,8 +46,13 @@ struct RsItem : RsMemoryManagement::SmallObject, RsSerializable
virtual ~RsItem();
/// TODO: Do this make sense with the new serialization system?
virtual void clear() = 0;
/** TODO: Does the existence of this method make sense with the new
* serialization system? **/
virtual void clear()
{
RS_ERR("Called without being overridden, report to developers");
print_stacktrace();
}
/// @deprecated use << ostream operator instead
RS_DEPRECATED_FOR("<< ostream operator")
@ -70,14 +79,21 @@ struct RsItem : RsMemoryManagement::SmallObject, RsSerializable
uint8_t PacketType();
uint8_t PacketSubType() const;
/** For Service Packets, @deprecated use the costructor with priority
* paramether instead */
RS_DEPRECATED RsItem(uint8_t ver, uint16_t service, uint8_t subtype);
/// For Service Packets
RsItem(uint8_t ver, uint16_t service, uint8_t subtype);
RsItem( uint8_t ver, RsServiceType service, uint8_t subtype,
RsItemPriority prio );
uint16_t PacketService() const; /* combined Packet class/type (mid 16bits) */
void setPacketService(uint16_t service);
inline uint8_t priority_level() const { return _priority_level ;}
inline void setPriorityLevel(uint8_t l) { _priority_level = l ;}
#ifdef RS_DEAD_CODE
/*
* TODO: This default implementation should be removed and childs structs
* implement ::serial_process(...) as soon as all the codebase is ported to
@ -90,11 +106,12 @@ struct RsItem : RsMemoryManagement::SmallObject, RsSerializable
"overriding Class is: ", typeid(*this).name() );
print_stacktrace();
}
#endif //def RS_DEAD_CODE
protected:
uint32_t type;
RsPeerId peerId;
uint8_t _priority_level;
RsItemPriority _priority_level;
};
/// TODO: Do this make sense with the new serialization system?
@ -108,9 +125,17 @@ public:
uint32_t getRawLength() { return len; }
void * getRawData() { return data; }
virtual void clear() {}
// virtual void clear() override {}
virtual std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual void serial_process(RsGenericSerializer::SerializeJob,
RsGenericSerializer::SerializeContext&) override
{
RS_ERR( "called by an item using new serialization system ",
typeid(*this).name() );
print_stacktrace();
}
private:
void *data;
uint32_t len;

View File

@ -64,6 +64,12 @@ RsItem *RsNxsSerialiser::create_item(uint16_t service_id,uint8_t item_subtype) c
if(service_id != SERVICE_TYPE)
return NULL ;
switch(static_cast<RsNxsSubtype>(item_subtype))
{
case RsNxsSubtype::PULL_REQUEST:
return new RsNxsPullRequestItem(static_cast<RsServiceType>(service_id));
}
switch(item_subtype)
{
case RS_PKT_SUBTYPE_NXS_SYNC_GRP_REQ_ITEM: return new RsNxsSyncGrpReqItem(SERVICE_TYPE) ;

View File

@ -3,7 +3,10 @@
* *
* libretroshare: retroshare core library *
* *
* Copyright 2012 Christopher Evi-Parker,Robert Fernie<retroshare@lunamutt.com>*
* Copyright (C) 2012 Christopher Evi-Parker *
* Copyright (C) 2012 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2021 Gioacchino Mazzurco <gio@altermundi.net> *
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
@ -19,8 +22,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#ifndef RSNXSITEMS_H
#define RSNXSITEMS_H
#pragma once
#include <map>
#include <openssl/ssl.h>
@ -33,8 +35,13 @@
#include "serialiser/rstlvkeys.h"
#include "gxs/rsgxsdata.h"
// These items have "flag type" numbers, but this is not used.
enum class RsNxsSubtype : uint8_t
{
PULL_REQUEST = 0x90 /// @see RsNxsPullRequestItem
};
// These items have "flag type" numbers, but this is not used.
// TODO: refactor as C++11 enum class
const uint8_t RS_PKT_SUBTYPE_NXS_SYNC_GRP_REQ_ITEM = 0x01;
const uint8_t RS_PKT_SUBTYPE_NXS_SYNC_GRP_ITEM = 0x02;
const uint8_t RS_PKT_SUBTYPE_NXS_SYNC_GRP_STATS_ITEM = 0x03;
@ -47,14 +54,15 @@ const uint8_t RS_PKT_SUBTYPE_NXS_MSG_ITEM = 0x20;
const uint8_t RS_PKT_SUBTYPE_NXS_TRANSAC_ITEM = 0x40;
const uint8_t RS_PKT_SUBTYPE_NXS_GRP_PUBLISH_KEY_ITEM = 0x80;
// possibility create second service to deal with this functionality
#ifdef RS_DEAD_CODE
// possibility create second service to deal with this functionality
const uint8_t RS_PKT_SUBTYPE_EXT_SEARCH_GRP = 0x0001;
const uint8_t RS_PKT_SUBTYPE_EXT_SEARCH_MSG = 0x0002;
const uint8_t RS_PKT_SUBTYPE_EXT_DELETE_GRP = 0x0004;
const uint8_t RS_PKT_SUBTYPE_EXT_DELETE_MSG = 0x0008;
const uint8_t RS_PKT_SUBTYPE_EXT_SEARCH_REQ = 0x0010;
#endif // def RS_DEAD_CODE
/*!
* Base class for Network exchange service
@ -65,17 +73,14 @@ const uint8_t RS_PKT_SUBTYPE_EXT_SEARCH_REQ = 0x0010;
*/
class RsNxsItem : public RsItem
{
public:
RsNxsItem(uint16_t servtype, uint8_t subtype) : RsItem(RS_PKT_VERSION_SERVICE, servtype, subtype), transactionNumber(0)
{
setPriorityLevel(QOS_PRIORITY_RS_GXS_NET);
return;
}
virtual ~RsNxsItem(){}
virtual void clear() = 0;
RsNxsItem(uint16_t servtype, uint8_t subtype):
RsItem(RS_PKT_VERSION_SERVICE, servtype, subtype), transactionNumber(0)
{ setPriorityLevel(QOS_PRIORITY_RS_GXS_NET); }
uint32_t transactionNumber; // set to zero if this is not a transaction item
virtual ~RsNxsItem() = default;
uint32_t transactionNumber; // set to zero if this is not a transaction item
};
@ -362,6 +367,22 @@ public:
};
/*!
* Used to request to a peer pull updates from us ASAP without waiting GXS sync
* timer */
struct RsNxsPullRequestItem: RsItem
{
explicit RsNxsPullRequestItem(RsServiceType servtype):
RsItem( RS_PKT_VERSION_SERVICE,
servtype,
static_cast<uint8_t>(RsNxsSubtype::PULL_REQUEST),
QOS_PRIORITY_RS_GXS_NET ) {}
/// @see RsSerializable
void serial_process( RsGenericSerializer::SerializeJob,
RsGenericSerializer::SerializeContext& ) override {}
};
/*!
* Used to respond to a RsGrpMsgsReq
@ -401,6 +422,7 @@ struct RsNxsMsg : RsNxsItem
RsGxsMsgMetaData* metaData;
};
#ifdef RS_DEAD_CODE
/*!
* Used to request a search of user data
*/
@ -422,7 +444,7 @@ public:
RsTlvBinaryData serviceSearchItem; // service aware of item class
uint32_t expiration; // expiration date
};
#endif //def RS_DEAD_CODE
#ifdef UNUSED_CODE
@ -503,14 +525,12 @@ class RsNxsSerialiser : public RsServiceSerializer
{
public:
explicit RsNxsSerialiser(uint16_t servtype) : RsServiceSerializer(servtype), SERVICE_TYPE(servtype) {}
virtual ~RsNxsSerialiser() {}
explicit RsNxsSerialiser(uint16_t servtype):
RsServiceSerializer(servtype), SERVICE_TYPE(servtype) {}
virtual ~RsNxsSerialiser() = default;
virtual RsItem *create_item(uint16_t service_id,uint8_t item_subtype) const ;
protected:
const uint16_t SERVICE_TYPE;
};
#endif // RSNXSITEMS_H

View File

@ -118,7 +118,7 @@ void RsServer::rsGlobalShutDown()
// if(mWire) mWire->join();
// #endif
AuthGPG::exit();
AuthPGP::exit();
mShutdownCallback(0);
}

View File

@ -255,7 +255,7 @@ bool p3Peers::setPeerMaximumRates(const RsPgpId& pid,uint32_t maxUploadRate,uint
bool p3Peers::haveSecretKey(const RsPgpId& id)
{
return AuthGPG::getAuthGPG()->haveSecretKey(id);
return AuthPGP::haveSecretKey(id);
}
/* There are too many dependancies of this function
@ -274,7 +274,7 @@ bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d)
if (id == sOwnId)
{
mPeerMgr->getOwnNetStatus(ps);
ps.gpg_id = AuthGPG::getAuthGPG()->getGPGOwnId();
ps.gpg_id = AuthPGP::getPgpOwnId();
}
else if (!mPeerMgr->getFriendNetStatus(id, ps))
{
@ -560,17 +560,17 @@ bool p3Peers::isProxyAddress(const uint32_t type, const sockaddr_storage& addr)
bool p3Peers::isKeySupported(const RsPgpId& id)
{
return AuthGPG::getAuthGPG()->isKeySupported(id);
return AuthPGP::isKeySupported(id);
}
std::string p3Peers::getGPGName(const RsPgpId &gpg_id)
{
/* get from mAuthMgr as it should have more peers? */
return AuthGPG::getAuthGPG()->getGPGName(gpg_id);
return AuthPGP::getPgpName(gpg_id);
}
bool p3Peers::isPgpFriend(const RsPgpId& pgpId)
{ return AuthGPG::getAuthGPG()->isGPGAccepted(pgpId); }
{ return AuthPGP::isPGPAccepted(pgpId); }
bool p3Peers::isSslOnlyFriend(const RsPeerId& sslId)
{
@ -598,7 +598,7 @@ std::string p3Peers::getPeerName(const RsPeerId& ssl)
#endif
std::string name;
if (ssl == AuthSSL::getAuthSSL()->OwnId())
return AuthGPG::getAuthGPG()->getGPGOwnName();
return AuthPGP::getPgpOwnName();
if (mPeerMgr->getPeerName(ssl, name))
{
@ -618,7 +618,7 @@ bool p3Peers::getGPGAllList(std::list<RsPgpId> &ids)
#endif
/* get from mAuthMgr */
AuthGPG::getAuthGPG()->getGPGAllList(ids);
AuthPGP::getPgpAllList(ids);
return true;
}
@ -629,7 +629,7 @@ bool p3Peers::getGPGValidList(std::list<RsPgpId> &ids)
#endif
/* get from mAuthMgr */
AuthGPG::getAuthGPG()->getGPGValidList(ids);
AuthPGP::getPgpValidList(ids);
return true;
}
@ -640,14 +640,14 @@ bool p3Peers::getGPGSignedList(std::list<RsPgpId> &ids)
#endif
/* get from mAuthMgr */
AuthGPG::getAuthGPG()->getGPGSignedList(ids);
AuthPGP::getPgpSignedList(ids);
return true;
}
bool p3Peers::getPgpFriendList(std::vector<RsPgpId>& pgpIds)
{
std::list<RsPgpId> ids;
if(AuthGPG::getAuthGPG()->getGPGAcceptedList(ids))
if(AuthPGP::getPgpAcceptedList(ids))
{
pgpIds.clear();
std::copy(ids.begin(), ids.end(), std::back_inserter(pgpIds));
@ -661,7 +661,7 @@ bool p3Peers::getGPGAcceptedList(std::list<RsPgpId> &ids)
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::getGPGAcceptedList()" << std::endl;
#endif
AuthGPG::getAuthGPG()->getGPGAcceptedList(ids);
AuthPGP::getPgpAcceptedList(ids);
return true;
}
@ -677,7 +677,7 @@ bool p3Peers::getAssociatedSSLIds(const RsPgpId &gpg_id, std::list<RsPeerId> &id
bool p3Peers::gpgSignData(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen, std::string reason /* = "" */)
{
return AuthGPG::getAuthGPG()->SignDataBin(data,len,sign,signlen, reason);
return AuthPGP::SignDataBin(data,len,sign,signlen, reason);
}
RsPgpId p3Peers::pgpIdFromFingerprint(const RsPgpFingerprint& fpr)
@ -692,7 +692,7 @@ bool p3Peers::getGPGDetails(const RsPgpId &pgp_id, RsPeerDetails &d)
#endif
/* get from mAuthMgr */
bool res = AuthGPG::getAuthGPG()->getGPGDetails(pgp_id, d);
bool res = AuthPGP::getPgpDetails(pgp_id, d);
d.isOnlyGPGdetail = true ;
d.service_perm_flags = mPeerMgr->servicePermissionFlags(pgp_id) ;
@ -707,7 +707,7 @@ const RsPgpId& p3Peers::getGPGOwnId()
#endif
/* get from mAuthMgr */
return AuthGPG::getAuthGPG()->getGPGOwnId();
return AuthPGP::getPgpOwnId();
}
RsPgpId p3Peers::getGPGId(const RsPeerId& sslid)
@ -719,7 +719,7 @@ RsPgpId p3Peers::getGPGId(const RsPeerId& sslid)
/* get from mAuthMgr */
if (sslid == AuthSSL::getAuthSSL()->OwnId())
{
return AuthGPG::getAuthGPG()->getGPGOwnId();
return AuthPGP::getPgpOwnId();
}
peerState pcs;
if (mPeerMgr->getFriendNetStatus(sslid, pcs))
@ -740,12 +740,12 @@ bool p3Peers::addFriend(const RsPeerId &ssl_id, const RsPgpId &gpg_id,ServicePe
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::addFriend() with : id : " << id << "; gpg_id : " << gpg_id << std::endl;
#endif
if(AuthGPG::getAuthGPG()->isGPGId(gpg_id))
if(AuthPGP::isPGPId(gpg_id))
{
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::addFriend() Authorising GPG Id: " << gpg_id << std::endl;
#endif
if (AuthGPG::getAuthGPG()->AllowConnection(gpg_id, true))
if (AuthPGP::AllowConnection(gpg_id, true))
{
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::addFriend() Authorization OK." << std::endl;
@ -798,7 +798,7 @@ bool p3Peers::addSslOnlyFriend( const RsPeerId& sslId, const RsPgpId& pgp_id,con
bool p3Peers::removeKeysFromPGPKeyring(const std::set<RsPgpId>& pgp_ids,std::string& backup_file,uint32_t& error_code)
{
return AuthGPG::getAuthGPG()->removeKeysFromPGPKeyring(pgp_ids,backup_file,error_code) ;
return AuthPGP::removeKeysFromPGPKeyring(pgp_ids,backup_file,error_code) ;
}
bool p3Peers::removeFriendLocation(const RsPeerId &sslId)
@ -818,7 +818,7 @@ bool p3Peers::removeFriend(const RsPgpId& gpgId)
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::removeFriend() " << gpgId << std::endl;
#endif
if (gpgId == AuthGPG::getAuthGPG()->getGPGOwnId()) {
if (gpgId == AuthPGP::getPgpOwnId()) {
std::cerr << "p3Peers::removeFriend() ERROR we're not going to remove our own GPG id." << std::endl;
return false;
}
@ -826,7 +826,7 @@ bool p3Peers::removeFriend(const RsPgpId& gpgId)
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::removeFriend() Removing GPG Id: " << gpgId << std::endl;
#endif
if (AuthGPG::getAuthGPG()->AllowConnection(gpgId, false))
if (AuthPGP::AllowConnection(gpgId, false))
{
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::removeFriend() OK." << std::endl;
@ -1114,9 +1114,7 @@ std::string p3Peers::getPGPKey(const RsPgpId& pgp_id,bool include_signatures)
rs_owner_ptr<unsigned char> mem_block = nullptr;
size_t mem_block_size = 0;
if( !AuthGPG::getAuthGPG()->exportPublicKey(
RsPgpId(pgp_id), mem_block, mem_block_size,
false, include_signatures ) )
if( !AuthPGP::exportPublicKey( RsPgpId(pgp_id), mem_block, mem_block_size, false, include_signatures ) )
{
RsErr() << __PRETTY_FUNCTION__
<< " Failure retriving certificate for id " << pgp_id
@ -1147,8 +1145,7 @@ bool p3Peers::GetPGPBase64StringAndCheckSum(
rs_owner_ptr<unsigned char> mem_block = nullptr;
size_t mem_block_size = 0;
if(!AuthGPG::getAuthGPG()->exportPublicKey(
gpg_id,mem_block,mem_block_size,false,false ))
if(!AuthPGP::exportPublicKey( gpg_id,mem_block,mem_block_size,false,false ))
return false;
RsBase64::encode(mem_block, mem_block_size, gpg_base64_string, true, false);
@ -1506,7 +1503,7 @@ std::string p3Peers::GetRetroshareInvite( const RsPeerId& sslId, RetroshareInvit
unsigned char *mem_block = nullptr;
size_t mem_block_size = 0;
if(!AuthGPG::getAuthGPG()->exportPublicKey( RsPgpId(detail.gpg_id), mem_block, mem_block_size, false, !!(invite_flags & RetroshareInviteFlags::PGP_SIGNATURES) ))
if(!AuthPGP::exportPublicKey( RsPgpId(detail.gpg_id), mem_block, mem_block_size, false, !!(invite_flags & RetroshareInviteFlags::PGP_SIGNATURES) ))
{
std::cerr << "Cannot output certificate for id \"" << detail.gpg_id
<< "\". Sorry." << std::endl;
@ -1542,7 +1539,7 @@ bool p3Peers::loadCertificateFromString(
}
RsPgpId gpgid;
bool res = AuthGPG::getAuthGPG()->LoadCertificateFromString( crt->armouredPGPKey(), gpgid, error_string );
bool res = AuthPGP::LoadCertificateFromString( crt->armouredPGPKey(), gpgid, error_string );
gpg_id = gpgid;
ssl_id = crt->sslid();
@ -1559,7 +1556,7 @@ bool p3Peers::loadCertificateFromString(
}
bool p3Peers::loadPgpKeyFromBinaryData( const unsigned char *bin_key_data,uint32_t bin_key_len, RsPgpId& gpg_id, std::string& error_string )
{
bool res = AuthGPG::getAuthGPG()->LoadPGPKeyFromBinaryData( bin_key_data,bin_key_len, gpg_id, error_string );
bool res = AuthPGP::LoadPGPKeyFromBinaryData( bin_key_data,bin_key_len, gpg_id, error_string );
if(res)
mPeerMgr->notifyPgpKeyReceived(gpg_id);
@ -1578,9 +1575,7 @@ bool p3Peers::loadDetailsFromStringCert( const std::string &certstr,
RsCertificate& cert = *certPtr;
if(!AuthGPG::getAuthGPG()->getGPGDetailsFromBinaryBlock(
cert.pgp_key(), cert.pgp_key_size(),
pd.gpg_id, pd.name, pd.gpgSigners ))
if(!AuthPGP::getPgpDetailsFromBinaryBlock( cert.pgp_key(), cert.pgp_key_size(), pd.gpg_id, pd.name, pd.gpgSigners ))
return false;
Dbg4() << __PRETTY_FUNCTION__ << " Parsing cert for sslid, location, ext "
@ -1620,11 +1615,11 @@ bool p3Peers::loadDetailsFromStringCert( const std::string &certstr,
return true;
}
bool p3Peers::cleanCertificate(const std::string &certstr, std::string &cleanCert,bool& is_short_format,uint32_t& error_code)
bool p3Peers::cleanCertificate(const std::string &certstr, std::string &cleanCert,bool& is_short_format,uint32_t& error_code,RsPeerDetails& details)
{
RsCertificate::Format format ;
bool res = RsCertificate::cleanCertificate(certstr,cleanCert,format,error_code,true) ;
bool res = RsCertificate::cleanCertificate(certstr,cleanCert,format,error_code,true,details) ;
if(format == RsCertificate::RS_CERTIFICATE_RADIX)
is_short_format = false;
@ -1658,7 +1653,7 @@ bool p3Peers::signGPGCertificate(const RsPgpId &id, const std::string &gpg_pass
rsNotify->cachePgpPassphrase(gpg_passphrase);
rsNotify->setDisableAskPassword(true);
bool res = AuthGPG::getAuthGPG()->SignCertificateLevel0(id);
bool res = AuthPGP::SignCertificateLevel0(id);
rsNotify->clearPgpPassphrase();
rsNotify->setDisableAskPassword(false);
@ -1672,7 +1667,7 @@ bool p3Peers::trustGPGCertificate(const RsPgpId &id, uint32_t trustlvl)
std::cerr << "p3Peers::TrustCertificate() " << id;
std::cerr << std::endl;
#endif
return AuthGPG::getAuthGPG()->TrustCertificate(id, trustlvl);
return AuthPGP::TrustCertificate(id, trustlvl);
}
/* Group Stuff */

View File

@ -163,7 +163,7 @@ public:
virtual bool loadPgpKeyFromBinaryData( const unsigned char *bin_key_data,uint32_t bin_key_len, RsPgpId& gpg_id, std::string& error_string ) override;
virtual bool loadDetailsFromStringCert(const std::string &cert, RsPeerDetails &pd, uint32_t& error_code) override;
virtual bool cleanCertificate(const std::string &certstr, std::string &cleanCert, bool &is_short_format, uint32_t& error_code) override;
virtual bool cleanCertificate(const std::string &certstr, std::string &cleanCert, bool &is_short_format, uint32_t& error_code, RsPeerDetails& details) override;
virtual std::string saveCertificateToString(const RsPeerId &id) override;
virtual bool signGPGCertificate(const RsPgpId &id,const std::string& gpg_passphrase) override;

View File

@ -140,7 +140,7 @@ bool p3ServerConfig::setConfigurationOption(uint32_t key, const std::string &opt
int p3ServerConfig::getConfigNetStatus(RsConfigNetStatus &status)
{
status.ownId = AuthSSL::getAuthSSL()->OwnId();
status.ownName = AuthGPG::getAuthGPG()->getGPGOwnName();
status.ownName = AuthPGP::getPgpOwnName();
// Details from PeerMgr.
peerState pstate;

View File

@ -32,12 +32,12 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <system_error>
#include <iostream>
#include "retroshare/rsinit.h"
#include "rsaccounts.h"
#include "util/rsdebug.h"
#include "util/rsdir.h"
#include "util/rsstring.h"
#include "util/folderiterator.h"
@ -48,6 +48,11 @@
#include <openssl/ssl.h>
#ifdef __ANDROID__
# include "rs_android/rsjni.hpp"
# include "rs_android/retroshareserviceandroid.hpp"
#endif
// Global singleton declaration of data.
RsAccountsDetail* RsAccounts::rsAccountsDetails = nullptr;
@ -328,22 +333,7 @@ bool RsAccountsDetail::defaultBaseDirectory()
{
std::string basedir;
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef WINDOWS_SYS
// unix: homedir + /.retroshare
char *h = getenv("HOME");
if (h == NULL)
{
std::cerr << "defaultBaseDirectory() Error: cannot determine $HOME dir"
<< std::endl;
return false ;
}
basedir = h;
basedir += "/.retroshare";
#else
#ifdef WINDOWS_SYS
if (RsInit::isPortable())
{
// use directory "Data" in portable version
@ -375,13 +365,53 @@ bool RsAccountsDetail::defaultBaseDirectory()
}
basedir += "\\RetroShare";
}
#endif
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
#elif defined (__ANDROID__) // def WINDOWS_SYS
struct ApplicationInfo
{
static constexpr auto Name()
{ return "android/content/pm/ApplicationInfo"; }
};
auto uenv = jni::GetAttachedEnv(RsJni::getVM());
JNIEnv& env = *uenv;
auto androidContext = RetroShareServiceAndroid::getAndroidContext(env);
auto& contextClass =
jni::Class<RetroShareServiceAndroid::Context>::Singleton(env);
auto& applicationInfoClass = jni::Class<ApplicationInfo>::Singleton(env);
auto getApplicationInfo =
contextClass.GetMethod<jni::Object<ApplicationInfo> ()>(
env, "getApplicationInfo" );
auto applicationInfo = androidContext.Call(env, getApplicationInfo);
auto dataDirField = jni::Field<ApplicationInfo, jni::String>(
env, applicationInfoClass, "dataDir" );
jni::Local<jni::String> dataDir = applicationInfo.Get<jni::String>(
env, dataDirField );
basedir = jni::Make<std::string>(env, dataDir) + "/.retroshare/";
#else // def WINDOWS_SYS, if defined (__ANDROID__)
// unix: homedir + /.retroshare
char* h = getenv("HOME");
if(h == nullptr)
{
RS_ERR("cannot determine $HOME dir");
return false ;
}
basedir = h;
basedir += "/.retroshare";
#endif // def WINDOWS_SYS
/* store to class variable */
mBaseDirectory = basedir;
std::cerr << "defaultBaseDirectory() = " << mBaseDirectory;
std::cerr << std::endl;
RS_INFO(mBaseDirectory);
return true;
}
@ -701,10 +731,10 @@ static bool checkAccount(const std::string &accountdir, AccountDetails &account,
if(! RsAccounts::GetPGPLoginDetails(account.mPgpId, account.mPgpName, account.mPgpEmail))
return false ;
if(!AuthGPG::getAuthGPG()->haveSecretKey(account.mPgpId))
if(!AuthPGP::haveSecretKey(account.mPgpId))
return false ;
if(!AuthGPG::getAuthGPG()->isKeySupported(account.mPgpId))
if(!AuthPGP::isKeySupported(account.mPgpId))
{
std::string keystring = account.mPgpId.toStdString() + " " + account.mPgpName + "&#60;" + account.mPgpEmail ;
unsupported_keys[keystring].push_back("Location: " + account.mLocation + "&nbsp;&nbsp;(" + account.mSslId.toStdString() + ")") ;
@ -811,14 +841,15 @@ static bool checkAccount(const std::string &accountdir, AccountDetails &account,
/* Use RetroShare's exe dir */
dataDirectory = ".";
#elif defined(ANDROID)
#elif defined(__ANDROID__)
// TODO: This is probably not really used on Android
dataDirectory = PathBaseDirectory()+"/usr/share/retroshare";
#elif defined(DATA_DIR)
#elif defined(RS_DATA_DIR)
// cppcheck-suppress ConfigurationNotChecked
dataDirectory = DATA_DIR;
dataDirectory = RS_DATA_DIR;
// For all other OS the data directory must be set in libretroshare.pro
#else
# error "For your target OS automatic data dir discovery is not supported, cannot compile if DATA_DIR variable not set."
# error "For your target OS automatic data dir discovery is not supported, cannot compile if RS_DATA_DIR variable not set."
#endif
if (!check)
@ -851,9 +882,10 @@ static bool checkAccount(const std::string &accountdir, AccountDetails &account,
/* Generating GPGme Account */
int RsAccountsDetail::GetPGPLogins(std::list<RsPgpId> &pgpIds) {
AuthGPG::getAuthGPG()->availableGPGCertificatesWithPrivateKeys(pgpIds);
return 1;
int RsAccountsDetail::GetPGPLogins(std::list<RsPgpId>& pgpIds)
{
AuthPGP::availablePgpCertificatesWithPrivateKeys(pgpIds);
return 1;
}
int RsAccountsDetail::GetPGPLoginDetails(const RsPgpId& id, std::string &name, std::string &email)
@ -863,10 +895,10 @@ int RsAccountsDetail::GetPGPLoginDetails(const RsPgpId& id, std::string &na
#endif
bool ok = true ;
name = AuthGPG::getAuthGPG()->getGPGName(id,&ok);
name = AuthPGP::getPgpName(id,&ok);
if(!ok)
return 0 ;
email = AuthGPG::getAuthGPG()->getGPGEmail(id,&ok);
email = AuthPGP::getPgpEmail(id,&ok);
if(!ok)
return 0 ;
@ -886,7 +918,7 @@ bool RsAccountsDetail::SelectPGPAccount(const RsPgpId& pgpId)
{
bool retVal = false;
if (0 < AuthGPG::getAuthGPG() -> GPGInit(pgpId))
if (0 < AuthPGP::PgpInit(pgpId))
{
retVal = true;
#ifdef DEBUG_ACCOUNTS
@ -906,7 +938,7 @@ bool RsAccountsDetail::SelectPGPAccount(const RsPgpId& pgpId)
bool RsAccountsDetail::GeneratePGPCertificate(const std::string& name, const std::string& email, const std::string& passwd, RsPgpId &pgpId, const int keynumbits, std::string &errString)
{
return AuthGPG::getAuthGPG()->GeneratePGPCertificate(name, email, passwd, pgpId, keynumbits, errString);
return AuthPGP::GeneratePgpCertificate(name, email, passwd, pgpId, keynumbits, errString);
}
// PGP Support Functions.
@ -918,29 +950,34 @@ void RsAccountsDetail::getUnsupportedKeys(std::map<std::string,std::vector<std::
bool RsAccountsDetail::exportIdentity(const std::string& fname,const RsPgpId& id)
{
return AuthGPG::getAuthGPG()->exportProfile(fname,id);
return AuthPGP::exportProfile(fname,id);
}
bool RsAccountsDetail::importIdentity(const std::string& fname,RsPgpId& id,std::string& import_error)
{
return AuthGPG::getAuthGPG()->importProfile(fname,id,import_error);
return AuthPGP::importProfile(fname,id,import_error);
}
bool RsAccountsDetail::importIdentityFromString(const std::string &data, RsPgpId &imported_pgp_id, std::string &import_error)
{
return AuthGPG::getAuthGPG()->importProfileFromString(data, imported_pgp_id, import_error);
return AuthPGP::importProfileFromString(data, imported_pgp_id, import_error);
}
bool RsAccountsDetail::exportIdentityToString(
std::string& data, const RsPgpId& pgpId, bool includeSignatures,
std::string& errorMsg )
{
return AuthGPG::getAuthGPG()->exportIdentityToString(
return AuthPGP::exportIdentityToString(
data, pgpId, includeSignatures, errorMsg );
}
bool RsAccountsDetail::copyGnuPGKeyrings()
{
#ifdef __ANDROID__
RS_ERR(std::errc::not_supported);
print_stacktrace();
return false;
#else
std::string pgp_dir = PathPGPDirectory() ;
if(!RsDirUtil::checkCreateDirectory(pgp_dir))
@ -992,6 +1029,7 @@ bool RsAccountsDetail::copyGnuPGKeyrings()
}
return true ;
#endif // def __ANDROID__
}
@ -1020,7 +1058,7 @@ bool RsAccountsDetail::GenerateSSLCertificate(const RsPgpId& pgp_id, const s
int nbits = 4096;
//std::string pgp_name = AuthGPG::getAuthGPG()->getGPGName(pgp_id);
//std::string pgp_name = AuthGPG::getGPGName(pgp_id);
// Create the filename .....
// Temporary Directory for creating files....
@ -1295,7 +1333,7 @@ bool RsAccounts::init(const std::string& opt_base_dir,int& error_code)
if(!RsDirUtil::checkCreateDirectory(pgp_dir))
throw std::runtime_error("Cannot create pgp directory " + pgp_dir) ;
AuthGPG::init( pgp_dir + "/retroshare_public_keyring.gpg",
AuthPGP::init( pgp_dir + "/retroshare_public_keyring.gpg",
pgp_dir + "/retroshare_secret_keyring.gpg",
pgp_dir + "/retroshare_trustdb.gpg",
pgp_dir + "/lock");

View File

@ -32,8 +32,9 @@
#endif
#ifdef __ANDROID__
# include <QFile> // To install bdboot.txt
# include <QString> // for QString::fromStdString(...)
# include <jni/jni.hpp>
# include "rs_android/rsjni.hpp"
# include "rs_android/retroshareserviceandroid.hpp"
#endif
#include "util/argstream.h"
@ -43,6 +44,7 @@
#include "util/folderiterator.h"
#include "util/rsstring.h"
#include "retroshare/rsinit.h"
#include "retroshare/rstor.h"
#include "retroshare/rsnotify.h"
#include "retroshare/rsiface.h"
#include "plugins/pluginmanager.h"
@ -144,6 +146,7 @@ struct RsInitConfig
{}
RsFileHash main_executable_hash;
std::string mainExecutablePath;
#ifdef WINDOWS_SYS
bool portable;
@ -195,7 +198,7 @@ static const int SSLPWD_LEN = 64;
void RsInit::InitRsConfig()
{
RsInfo() << " libretroshare version: " << RS_HUMAN_READABLE_VERSION
RsInfo() << "libretroshare version: " << RS_HUMAN_READABLE_VERSION
<< std::endl;
rsInitConfig = new RsInitConfig;
@ -305,6 +308,7 @@ int RsInit::InitRetroShare(const RsConfigOptions& conf)
rsInitConfig->optBaseDir = conf.optBaseDir;
rsInitConfig->jsonApiPort = conf.jsonApiPort;
rsInitConfig->jsonApiBindAddress = conf.jsonApiBindAddress;
rsInitConfig->mainExecutablePath = conf.main_executable_path;
#ifdef PTW32_STATIC_LIB
// for static PThreads under windows... we need to init the library...
@ -512,7 +516,7 @@ RsInit::LoadCertificateStatus RsInit::LockAndLoadCertificates(
if(!RsAccounts::GetAccountDetails(accountId, pgpId, pgpName, pgpEmail, location))
throw RsInit::ERR_UNKNOWN; // invalid PreferredAccount;
if(0 == AuthGPG::getAuthGPG() -> GPGInit(pgpId))
if(0 == AuthPGP::PgpInit(pgpId))
throw RsInit::ERR_UNKNOWN; // PGP Error.
LoadCertificateStatus retVal =
@ -912,8 +916,8 @@ int RsServer::StartupRetroShare()
/* History Manager */
mHistoryMgr = new p3HistoryMgr();
mPeerMgr = new p3PeerMgrIMPL( AuthSSL::getAuthSSL()->OwnId(),
AuthGPG::getAuthGPG()->getGPGOwnId(),
AuthGPG::getAuthGPG()->getGPGOwnName(),
AuthPGP::getPgpOwnId(),
AuthPGP::getPgpOwnName(),
AuthSSL::getAuthSSL()->getOwnLocation());
mNetMgr = new p3NetMgrIMPL();
mLinkMgr = new p3LinkMgrIMPL(mPeerMgr, mNetMgr);
@ -1012,32 +1016,32 @@ int RsServer::StartupRetroShare()
uint64_t tmp_size ;
if (!RsDirUtil::checkFile(bootstrapfile,tmp_size,true))
{
std::cerr << "DHT bootstrap file not in ConfigDir: " << bootstrapfile
<< std::endl;
#ifdef __ANDROID__
QFile bdbootRF("assets:/values/bdboot.txt");
if(!bdbootRF.open(QIODevice::ReadOnly | QIODevice::Text))
std::cerr << __PRETTY_FUNCTION__
<< " bdbootRF(assets:/values/bdboot.txt).open(...) fail: "
<< bdbootRF.errorString().toStdString() << std::endl;
else
{
QFile bdbootCF(QString::fromStdString(bootstrapfile));
if(!bdbootCF.open(QIODevice::WriteOnly | QIODevice::Text))
std::cerr << __PRETTY_FUNCTION__ << " bdbootCF("
<< bootstrapfile << ").open(...) fail: "
<< bdbootRF.errorString().toStdString() << std::endl;
else
{
bdbootCF.write(bdbootRF.readAll());
bdbootCF.close();
std::cerr << "Installed DHT bootstrap file not in ConfigDir: "
<< bootstrapfile << std::endl;
}
RS_INFO("DHT bootstrap file not in ConfigDir: ", bootstrapfile);
bdbootRF.close();
}
#else
#ifdef __ANDROID__
auto uenv = jni::GetAttachedEnv(RsJni::getVM());
JNIEnv& env = *uenv;
using AContext = RetroShareServiceAndroid::Context;
auto& assetHelperClass = jni::Class<RsJni::AssetHelper>::Singleton(env);
static auto copyAsset =
assetHelperClass.GetStaticMethod<
jni::jboolean(jni::Object<AContext>, jni::String, jni::String)>(
env, "copyAsset" );
auto androidContext = RetroShareServiceAndroid::getAndroidContext(env);
jni::jboolean result = assetHelperClass.Call(
env, copyAsset,
androidContext,
jni::Make<jni::String>(env, "values/bdboot.txt"),
jni::Make<jni::String>(env, bootstrapfile) );
if(!result) RS_ERR("Failure installing ", bootstrapfile);
#else // def __ANDROID__
std::cerr << "Checking for Installation DHT bootstrap file " << installfile << std::endl;
if ((installfile != "") && (RsDirUtil::checkFile(installfile,tmp_size)))
{
@ -1616,7 +1620,8 @@ int RsServer::StartupRetroShare()
//mConfigMgr->addConfiguration("ftserver.cfg", ftserver);
//
mConfigMgr->addConfiguration("gpg_prefs.cfg" , AuthGPG::getAuthGPG());
AuthPGP::registerToConfigMgr(std::string("gpg_prefs.cfg"),mConfigMgr);
mConfigMgr->addConfiguration("gxsnettunnel.cfg", mGxsNetTunnel);
mConfigMgr->addConfiguration("peers.cfg" , mPeerMgr);
mConfigMgr->addConfiguration("general.cfg" , mGeneralConfig);
@ -1802,7 +1807,7 @@ int RsServer::StartupRetroShare()
/* Add AuthGPG services */
/**************************************************************************/
//AuthGPG::getAuthGPG()->addService(mDisc);
//AuthGPG::addService(mDisc);
/**************************************************************************/
/* Force Any Last Configuration Options */
@ -1933,6 +1938,52 @@ int RsServer::StartupRetroShare()
return 1;
}
std::string RsInit::executablePath()
{
return rsInitConfig->mainExecutablePath;
}
bool RsInit::startAutoTor()
{
std::cerr << "(II) node is an automated Tor node => launching Tor auto-configuration." << std::endl;
// Now that we know the Tor service running, and we know the SSL id, we can make sure it provides a viable hidden service
std::string tor_hidden_service_dir = RsAccounts::AccountDirectory() + "/hidden_service/" ;
RsTor::setTorDataDirectory(RsAccounts::ConfigDirectory() + "/tor/");
RsTor::setHiddenServiceDirectory(tor_hidden_service_dir); // re-set it, because now it's changed to the specific location that is run
RsDirUtil::checkCreateDirectory(std::string(tor_hidden_service_dir)) ;
if(! RsTor::start() || RsTor::hasError())
{
std::cerr << "(EE) Tor cannot be started on your system: "+RsTor::errorMessage() << std::endl ;
return false ;
}
std::cerr << "(II) Tor has been started." << std::endl;
// now start/create the hidden service as needed.
std::string service_id;
RsTor::setupHiddenService();
while(RsTor::torStatus() != RsTorStatus::READY && RsTor::getHiddenServiceStatus(service_id) != RsTorHiddenServiceStatus::ONLINE) // runs until some status is reached: either tor works, or it fails.
{
rstime::rs_usleep(0.5*1000*1000) ;
std::cerr << "(II) Hidden service ID: " << service_id << ", status: " << (int)RsTor::getHiddenServiceStatus(service_id) << std::endl;
if(RsTor::hasError())
{
std::string error_msg = RsTor::errorMessage();
std::cerr << "(EE) Tor hidden service cannot be started: " << error_msg << std::endl;
return false;
}
// process Qt event loop to deal with messages of online/offline info
// QCoreApplication::processEvents();
}
return true;
}
RsInit::LoadCertificateStatus RsLoginHelper::attemptLogin(const RsPeerId& account, const std::string& password)
{
if(isLoggedIn()) return RsInit::ERR_ALREADY_RUNNING;
@ -1952,6 +2003,16 @@ RsInit::LoadCertificateStatus RsLoginHelper::attemptLogin(const RsPeerId& accoun
rsNotify->setDisableAskPassword(false) ;
rsNotify->clearPgpPassphrase() ;
bool is_hidden_node = false;
bool is_auto_tor = false ;
bool is_first_time = false ;
RsAccounts::getCurrentAccountOptions(is_hidden_node,is_auto_tor,is_first_time);
if(is_auto_tor)
if(!RsInit::startAutoTor())
return RsInit::ERR_CANNOT_CONFIGURE_TOR;
if(ret == RsInit::OK && RsControl::instance()->StartupRetroShare() == 1)
return RsInit::OK;

View File

@ -60,8 +60,7 @@ bool RsLoginHandler::checkAndStoreSSLPasswdIntoGPGFile(
return true ;
}
bool ok = AuthGPG::getAuthGPG()->encryptTextToFile(
ssl_passwd, getSSLPasswdFileName(ssl_id));
bool ok = AuthPGP::encryptTextToFile( ssl_passwd, getSSLPasswdFileName(ssl_id));
if (!ok) std::cerr << "Encrypting went wrong !" << std::endl;
@ -90,7 +89,7 @@ bool RsLoginHandler::getSSLPasswdFromGPGFile(const RsPeerId& ssl_id,std::string&
#endif
std::string plain;
if ( AuthGPG::getAuthGPG()->decryptTextFromFile( plain, getSSLPasswdFileName(ssl_id)) )
if ( AuthPGP::decryptTextFromFile( plain, getSSLPasswdFileName(ssl_id)) )
{
sslPassword = plain;
#ifdef DEBUG_RSLOGINHANDLER

View File

@ -3,7 +3,9 @@
* *
* libretroshare: retroshare core library *
* *
* Copyright 2007-2008 by Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2007-2008 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2021 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
@ -20,21 +22,20 @@
* *
*******************************************************************************/
#include "serialiser/rsbaseserial.h"
#include "util/rsthreads.h"
#include "util/rsstring.h"
#include "util/rsprint.h"
#include "rsitems/rsitem.h"
#include "rsitems/itempriorities.h"
#include <math.h>
#include <cmath>
#include <map>
#include <vector>
#include <iostream>
#include <typeinfo>
#include "serialiser/rsbaseserial.h"
#include "util/cxx23retrocompat.h"
#include "util/rsthreads.h"
#include "util/rsstring.h"
#include "util/rsprint.h"
#include "rsitems/rsitem.h"
#include "rsitems/itempriorities.h"
/***
* #define RSSERIAL_DEBUG 1
@ -166,11 +167,17 @@ uint8_t RsItem::PacketSubType() const
/* For Service Packets */
RsItem::RsItem(uint8_t ver, uint16_t service, uint8_t subtype)
{
_priority_level = QOS_PRIORITY_UNKNOWN ; // This value triggers PQIInterface to complain about undefined priorities
// This value triggers PQIInterface to complain about undefined priorities
_priority_level = QOS_PRIORITY_UNKNOWN;
type = (ver << 24) + (service << 8) + subtype;
return;
}
RsItem::RsItem( uint8_t ver, RsServiceType service, uint8_t subtype,
RsItemPriority prio ):
type(static_cast<uint32_t>(
(ver << 24) + (std::to_underlying(service) << 8) + subtype )),
_priority_level(prio) {}
uint16_t RsItem::PacketService() const
{
return (type >> 8) & 0xFFFF;

View File

@ -1,7 +1,8 @@
/*******************************************************************************
* RetroShare Broadcast Domain Discovery *
* *
* Copyright (C) 2019 Gioacchino Mazzurco <gio@altermundi.net> *
* Copyright (C) 2019-2021 Gioacchino Mazzurco <gio@altermundi.net> *
* Copyright (C) 2019-2021 Asociación Civil Altermundi <info@altermundi.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
@ -25,16 +26,17 @@
#include <vector>
#include <iostream>
#ifdef __ANDROID__
# include <QtAndroid>
#endif // def __ANDROID__
#include "services/broadcastdiscoveryservice.h"
#include "retroshare/rspeers.h"
#include "serialiser/rsserializable.h"
#include "serialiser/rsserializer.h"
#include "retroshare/rsevents.h"
#ifdef __ANDROID__
# include "rs_android/retroshareserviceandroid.hpp"
#endif // def __ANDROID__
/*extern*/ RsBroadcastDiscovery* rsBroadcastDiscovery = nullptr;
struct BroadcastDiscoveryPack : RsSerializable
@ -99,7 +101,7 @@ BroadcastDiscoveryService::BroadcastDiscoveryService(
if(mRsPeers.isHiddenNode(mRsPeers.getOwnId())) return;
#ifdef __ANDROID__
createMulticastLock();
createAndroidMulticastLock();
#endif // def __ANDROID__
enableMulticastListening();
@ -228,19 +230,47 @@ RsBroadcastDiscoveryResult BroadcastDiscoveryService::createResult(
bool BroadcastDiscoveryService::isMulticastListeningEnabled()
{
#ifdef __ANDROID__
return assertMulticastLockIsvalid() &&
mWifiMulticastLock.callMethod<jboolean>("isHeld");
#endif // def __ANDROID__
if(!mAndroidWifiMulticastLock)
{
RS_ERR("Android multicast lock not initialized!");
return false;
}
auto uenv = jni::GetAttachedEnv(RsJni::getVM());
JNIEnv& env = *uenv;
auto& multicastLockClass = jni::Class<AndroidMulticastLock>::Singleton(env);
auto isHeld =
multicastLockClass.GetMethod<jni::jboolean()>(
env, "isHeld" );
return mAndroidWifiMulticastLock.Call(env, isHeld);
#else // def __ANDROID__
return true;
#endif // def __ANDROID__
}
bool BroadcastDiscoveryService::enableMulticastListening()
{
#ifdef __ANDROID__
if(assertMulticastLockIsvalid() && !isMulticastListeningEnabled())
if(!mAndroidWifiMulticastLock)
{
mWifiMulticastLock.callMethod<void>("acquire");
RS_ERR("Android multicast lock not initialized!");
return false;
}
if(!isMulticastListeningEnabled())
{
auto uenv = jni::GetAttachedEnv(RsJni::getVM());
JNIEnv& env = *uenv;
auto& multicastLockClass = jni::Class<AndroidMulticastLock>::Singleton(env);
auto acquire =
multicastLockClass.GetMethod<void()>(
env, "acquire" );
mAndroidWifiMulticastLock.Call(env, acquire);
return true;
}
#endif // def __ANDROID__
@ -251,9 +281,24 @@ bool BroadcastDiscoveryService::enableMulticastListening()
bool BroadcastDiscoveryService::disableMulticastListening()
{
#ifdef __ANDROID__
if(assertMulticastLockIsvalid() && isMulticastListeningEnabled())
if(!mAndroidWifiMulticastLock)
{
mWifiMulticastLock.callMethod<void>("release");
RS_ERR("Android multicast lock not initialized!");
return false;
}
if(isMulticastListeningEnabled())
{
auto uenv = jni::GetAttachedEnv(RsJni::getVM());
JNIEnv& env = *uenv;
auto& multicastLockClass = jni::Class<AndroidMulticastLock>::Singleton(env);
auto release =
multicastLockClass.GetMethod<void()>(
env, "release" );
mAndroidWifiMulticastLock.Call(env, release);
return true;
}
#endif // def __ANDROID__
@ -262,56 +307,57 @@ bool BroadcastDiscoveryService::disableMulticastListening()
}
#ifdef __ANDROID__
bool BroadcastDiscoveryService::createMulticastLock()
bool BroadcastDiscoveryService::createAndroidMulticastLock()
{
Dbg2() << __PRETTY_FUNCTION__ << std::endl;
constexpr auto fname = __PRETTY_FUNCTION__;
const auto failure = [&](const std::string& err)
if(mAndroidWifiMulticastLock)
{
RsErr() << fname << " " << err << std::endl;
return false;
};
if(mWifiMulticastLock.isValid())
return failure("mWifiMulticastLock is already initialized");
QAndroidJniObject context = QtAndroid::androidContext();
if(!context.isValid())
return failure("Cannot retrieve Android context");
QAndroidJniObject WIFI_SERVICE = QAndroidJniObject::getStaticObjectField(
"android.content.Context", "WIFI_SERVICE", "Ljava/lang/String;");
if(!WIFI_SERVICE.isValid())
return failure("Cannot retrieve Context.WIFI_SERVICE value");
QAndroidJniObject wifiManager = context.callObjectMethod(
"getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;",
WIFI_SERVICE.object<jstring>() );
if(!wifiManager.isValid())
return failure("Cannot retrieve Android Wifi Manager");
mWifiMulticastLock = wifiManager.callObjectMethod(
"createMulticastLock",
"(Ljava/lang/String;)Landroid/net/wifi/WifiManager$MulticastLock;",
QAndroidJniObject::fromString(fname).object<jstring>() );
if(!mWifiMulticastLock.isValid())
return failure("Cannot create WifiManager.MulticastLock");
return true;
}
bool BroadcastDiscoveryService::assertMulticastLockIsvalid()
{
if(!mWifiMulticastLock.isValid())
{
RsErr() << __PRETTY_FUNCTION__ << " mWifiMulticastLock is invalid!"
<< std::endl;
RS_ERR("Android multicast lock is already initialized");
print_stacktrace();
return false;
}
auto uenv = jni::GetAttachedEnv(RsJni::getVM());
JNIEnv& env = *uenv;
using AContextTag = RetroShareServiceAndroid::Context;
using AContext = jni::Class<AContextTag>;
static auto& contextClass = AContext::Singleton(env);
auto wifiServiceField = jni::StaticField<AContextTag, jni::String>(
env, contextClass, "WIFI_SERVICE");
jni::Local<jni::String> WIFI_SERVICE = contextClass.Get(
env, wifiServiceField );
auto androidContext = RetroShareServiceAndroid::getAndroidContext(env);
auto getSystemService =
contextClass.GetMethod<jni::Object<jni::ObjectTag> (jni::String)>(
env, "getSystemService" );
struct WifiManager
{ static constexpr auto Name() { return "android/net/wifi/WifiManager"; } };
auto& wifiManagerClass = jni::Class<WifiManager>::Singleton(env);
auto wifiManager = jni::Cast<WifiManager>(
env, wifiManagerClass,
androidContext.Call(env, getSystemService, WIFI_SERVICE) );
auto createMulticastLock =
wifiManagerClass.GetMethod<jni::Object<AndroidMulticastLock>(jni::String)>(
env, "createMulticastLock" );
mAndroidWifiMulticastLock = jni::NewGlobal(
env, wifiManager.Call(
env, createMulticastLock,
jni::Make<jni::String>(
env, "RetroShare BroadcastDiscoveryService" ) ) );
return true;
}
#endif // def __ANDROID__
RsBroadcastDiscovery::~RsBroadcastDiscovery() = default;

View File

@ -1,7 +1,8 @@
/*******************************************************************************
* RetroShare Broadcast Domain Discovery *
* *
* Copyright (C) 2019 Gioacchino Mazzurco <gio@altermundi.net> *
* Copyright (C) 2019-2021 Gioacchino Mazzurco <gio@altermundi.net> *
* Copyright (C) 2019-2021 Asociación Civil Altermundi <info@altermundi.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
@ -27,14 +28,16 @@
#include <udp_discovery_peer.hpp>
#ifdef __ANDROID__
# include <QtAndroidExtras/QAndroidJniObject>
#endif // def __ANDROID__
#include "retroshare/rsbroadcastdiscovery.h"
#include "util/rsthreads.h"
#include "util/rsdebug.h"
#ifdef __ANDROID__
# include <jni/jni.hpp>
# include "rs_android/rsjni.hpp"
#endif // def __ANDROID__
namespace UDC = udpdiscovery;
class RsPeers;
@ -42,7 +45,7 @@ class BroadcastDiscoveryService :
public RsBroadcastDiscovery, public RsTickingThread
{
public:
BroadcastDiscoveryService(RsPeers& pRsPeers);
explicit BroadcastDiscoveryService(RsPeers& pRsPeers);
~BroadcastDiscoveryService() override;
/// @see RsBroadcastDiscovery
@ -71,26 +74,27 @@ protected:
std::map<UDC::IpPort, std::string> mDiscoveredData;
RsMutex mDiscoveredDataMutex;
RsPeers& mRsPeers; // TODO: std::shared_ptr<RsPeers> mRsPeers;
RsPeers& mRsPeers;
RsBroadcastDiscoveryResult createResult(
const UDC::IpPort& ipp, const std::string& uData );
#ifdef __ANDROID__
/** Android WifiManager.MulticastLock */
QAndroidJniObject mWifiMulticastLock;
struct AndroidMulticastLock
{
static constexpr auto Name()
{ return "android/net/wifi/WifiManager$MulticastLock"; }
};
jni::Global<jni::Object<AndroidMulticastLock>> mAndroidWifiMulticastLock;
/** Initialize the wifi multicast lock without acquiring it
* Needed to enable multicast listening in Android, for RetroShare broadcast
* discovery inspired by:
* https://github.com/flutter/flutter/issues/16335#issuecomment-420547860
*/
bool createMulticastLock();
/** Return false if mWifiMulticastLock is invalid and print error messages */
bool assertMulticastLockIsvalid();
#endif // def __ANDROID__
bool createAndroidMulticastLock();
#endif
RS_SET_CONTEXT_DEBUG_LEVEL(3)
};

View File

@ -900,10 +900,19 @@ bool p3GxsForums::markRead(const RsGxsGrpMsgIdPair& msgId, bool read)
bool p3GxsForums::subscribeToForum(const RsGxsGroupId& groupId, bool subscribe )
{
uint32_t token;
if( !RsGenExchange::subscribeToGroup(token, groupId, subscribe) || waitToken(token) != RsTokenService::COMPLETE ) return false;
if( !RsGenExchange::subscribeToGroup(token, groupId, subscribe) ||
waitToken(token) != RsTokenService::COMPLETE ) return false;
RsGxsGroupId grp;
acknowledgeGrp(token,grp);
RsGxsGroupId grp;
acknowledgeGrp(token, grp);
/* Since subscribe has been requested, the caller is most probably
* interested in getting the group messages ASAP so check updates from peers
* without waiting GXS sync timer.
* Do it here as this is meaningful or not depending on the service.
* Do it only after the token has been completed otherwise the pull have no
* effect. */
if(subscribe) RsGenExchange::netService()->checkUpdatesFromPeers();
return true;
}
@ -1150,6 +1159,13 @@ std::error_condition p3GxsForums::setPostKeepForever(
}
}
std::error_condition p3GxsForums::requestSynchronization()
{
auto errc = RsGenExchange::netService()->checkUpdatesFromPeers();
if(errc) return errc;
return RsGenExchange::netService()->requestPull();
}
/* so we need the same tick idea as wiki for generating dummy forums
*/

View File

@ -175,6 +175,8 @@ public:
rs_owner_ptr<uint8_t>& resultData, uint32_t& resultSize ) override;
#endif
std::error_condition requestSynchronization() override;
/// implementation of rsGxsGorums
///
bool getGroupData(const uint32_t &token, std::vector<RsGxsForumGroup> &groups) override;

View File

@ -1067,7 +1067,7 @@ bool p3IdService::createIdentity(uint32_t& token, RsIdentityParameters &params)
if(params.isPgpLinked)
{
ssdata.pgp.pgpId = AuthGPG::getAuthGPG()->getGPGOwnId();
ssdata.pgp.pgpId = AuthPGP::getPgpOwnId();
ssdata.pgp.lastCheckTs = time(nullptr);
}
@ -3619,7 +3619,7 @@ RsGenExchange::ServiceCreate_Return p3IdService::service_CreateGroup(
unsigned int sign_size = MAX_SIGN_SIZE;
memset(signarray,0,MAX_SIGN_SIZE) ; // just in case.
int result = AuthGPG::getAuthGPG()->SignDataBin(
int result = AuthPGP::SignDataBin(
static_cast<const void*>(hash.toByteArray()),
hash.SIZE_IN_BYTES, signarray, &sign_size,
__PRETTY_FUNCTION__ )
@ -4096,7 +4096,7 @@ void p3IdService::getPgpIdList()
#endif // DEBUG_IDS
std::list<RsPgpId> list;
mPgpUtils->getGPGAllList(list);
mPgpUtils->getPgpAllList(list);
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
@ -4593,7 +4593,7 @@ void p3IdService::generateDummy_FriendPGP()
// Now Generate for friends.
std::list<RsPgpId> gpgids;
std::list<RsPgpId>::const_iterator it;
mPgpUtils->getGPGAllList(gpgids);
mPgpUtils->getPgpAllList(gpgids);
RsGxsIdGroup id;

View File

@ -220,8 +220,6 @@ void p3MsgService::processIncomingMsg(RsMsgItem *mi)
for(std::list<RsTlvFileItem>::const_iterator it(mi->attachment.items.begin());it!=mi->attachment.items.end();++it)
rsFiles->FileRequest((*it).name,(*it).hash,(*it).filesize,std::string(),RS_FILE_REQ_ANONYMOUS_ROUTING,srcIds) ;
}
RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGELIST,NOTIFY_TYPE_ADD);
}
bool p3MsgService::checkAndRebuildPartialMessage(RsMsgItem *ci)
@ -283,16 +281,11 @@ int p3MsgService::incomingMsgs()
void p3MsgService::handleIncomingItem(RsMsgItem *mi)
{
bool changed = false ;
// only returns true when a msg is complete.
if(checkAndRebuildPartialMessage(mi))
{
processIncomingMsg(mi);
changed = true ;
}
if(changed)
RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGELIST,NOTIFY_TYPE_MOD);
}
void p3MsgService::statusChange(const std::list<pqiServicePeer> &plist)
@ -350,7 +343,6 @@ void p3MsgService::checkSizeAndSendMessage(RsMsgItem *msg)
int p3MsgService::checkOutgoingMessages()
{
bool changed = false;
std::list<RsMsgItem*> output_queue;
auto pEvent = std::make_shared<RsMailStatusEvent>();
@ -400,7 +392,6 @@ int p3MsgService::checkOutgoingMessages()
{
(mit->second)->msgFlags &= ~RS_MSG_FLAGS_PENDING;
toErase.push_back(mit->first);
changed = true;
}
else
{
@ -442,9 +433,6 @@ int p3MsgService::checkOutgoingMessages()
else
checkSizeAndSendMessage(*it);
if(changed)
RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGELIST,NOTIFY_TYPE_MOD);
if(rsEvents && !pEvent->mChangedMsgIds.empty())
rsEvents->postEvent(pEvent);
@ -954,8 +942,6 @@ bool p3MsgService::removeMsgId(const std::string &mid)
setMessageTag(mid, 0, false);
setMsgParentId(msgId, 0);
RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGELIST,NOTIFY_TYPE_MOD);
}
if(rsEvents && !pEvent->mChangedMsgIds.empty())
@ -968,7 +954,6 @@ bool p3MsgService::markMsgIdRead(const std::string &mid, bool unreadByUser)
{
std::map<uint32_t, RsMsgItem *>::iterator mit;
uint32_t msgId = atoi(mid.c_str());
bool changed = false;
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
@ -992,18 +977,18 @@ bool p3MsgService::markMsgIdRead(const std::string &mid, bool unreadByUser)
if (mi->msgFlags != msgFlags)
{
changed = true;
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
auto pEvent = std::make_shared<RsMailStatusEvent>();
pEvent->mMailStatusEventCode = RsMailStatusEventCode::MESSAGE_CHANGED;
pEvent->mChangedMsgIds.insert(mid);
rsEvents->postEvent(pEvent);
}
} else {
return false;
}
} /* UNLOCKED */
if (changed) {
RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGELIST,NOTIFY_TYPE_MOD);
}
return true;
}
@ -1012,8 +997,6 @@ bool p3MsgService::setMsgFlag(const std::string &mid, uint32_t flag, uint32_t
std::map<uint32_t, RsMsgItem *>::iterator mit;
uint32_t msgId = atoi(mid.c_str());
bool changed = false;
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
@ -1033,15 +1016,15 @@ bool p3MsgService::setMsgFlag(const std::string &mid, uint32_t flag, uint32_t
mit->second->msgFlags |= flag;
if (mit->second->msgFlags != oldFlag) {
changed = true;
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
auto pEvent = std::make_shared<RsMailStatusEvent>();
pEvent->mMailStatusEventCode = RsMailStatusEventCode::MESSAGE_CHANGED;
pEvent->mChangedMsgIds.insert(mid);
rsEvents->postEvent(pEvent);
}
} /* UNLOCKED */
if (changed) {
RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGELIST,NOTIFY_TYPE_MOD);
}
return true;
}
@ -1137,7 +1120,10 @@ uint32_t p3MsgService::sendMessage(RsMsgItem* item)
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGELIST, NOTIFY_TYPE_ADD); // deprecated
auto pEvent = std::make_shared<RsMailStatusEvent>();
pEvent->mMailStatusEventCode = RsMailStatusEventCode::MESSAGE_SENT;
pEvent->mChangedMsgIds.insert(std::to_string(item->msgId));
rsEvents->postEvent(pEvent);
return item->msgId;
}
@ -1175,8 +1161,11 @@ uint32_t p3MsgService::sendDistantMessage(RsMsgItem *item, const RsGxsId& from)
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
RsServer::notify()->notifyListChange( NOTIFY_LIST_MESSAGELIST,
NOTIFY_TYPE_ADD );
auto pEvent = std::make_shared<RsMailStatusEvent>();
pEvent->mMailStatusEventCode = RsMailStatusEventCode::MESSAGE_SENT;
pEvent->mChangedMsgIds.insert(std::to_string(item->msgId));
rsEvents->postEvent(pEvent);
return item->msgId;
}
@ -1210,8 +1199,6 @@ bool p3MsgService::MessageSend(MessageInfo &info)
// Update info for caller
info.msgId = std::to_string(msg->msgId);
info .msgflags = msg->msgFlags;
RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGELIST,NOTIFY_TYPE_ADD);// deprecated. Should be removed. Oct. 28, 2020
}
auto pEvent = std::make_shared<RsMailStatusEvent>();
@ -1418,10 +1405,9 @@ bool p3MsgService::MessageToDraft(MessageInfo &info, const std::string &msgParen
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
// RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGELIST,NOTIFY_TYPE_MOD);
auto pEvent = std::make_shared<RsMailStatusEvent>();
pEvent->mMailStatusEventCode = RsMailStatusEventCode::MESSAGE_SENT;
pEvent->mChangedMsgIds.insert(std::to_string(msg->msgId));
rsEvents->postEvent(pEvent);
return true;
@ -1452,7 +1438,7 @@ bool p3MsgService::getMessageTagTypes(MsgTagType& tags)
bool p3MsgService::setMessageTagType(uint32_t tagId, std::string& text, uint32_t rgb_color)
{
int nNotifyType = 0;
auto ev = std::make_shared<RsMailTagEvent>();
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
@ -1475,7 +1461,8 @@ bool p3MsgService::setMessageTagType(uint32_t tagId, std::string& text, uint32
mTags.insert(std::pair<uint32_t, RsMsgTagType*>(tagId, tagType));
nNotifyType = NOTIFY_TYPE_ADD;
ev->mMailTagEventCode = RsMailTagEventCode::TAG_ADDED;
ev->mChangedMsgTagIds.insert(std::to_string(tagId));
} else {
if (mit->second->text != text || mit->second->rgb_color != rgb_color) {
/* modify existing tag */
@ -1489,17 +1476,18 @@ bool p3MsgService::setMessageTagType(uint32_t tagId, std::string& text, uint32
}
mit->second->rgb_color = rgb_color;
nNotifyType = NOTIFY_TYPE_MOD;
ev->mMailTagEventCode = RsMailTagEventCode::TAG_CHANGED;
ev->mChangedMsgTagIds.insert(std::to_string(tagId));
}
}
} /* UNLOCKED */
if (nNotifyType) {
if (!ev->mChangedMsgTagIds.empty()) {
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGE_TAGS, nNotifyType);
rsEvents->postEvent(ev);
return true;
}
@ -1513,6 +1501,9 @@ bool p3MsgService::removeMessageTagType(uint32_t tagId)
return false;
}
auto msgEvent = std::make_shared<RsMailStatusEvent>();
msgEvent->mMailStatusEventCode = RsMailStatusEventCode::TAG_CHANGED;
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
@ -1540,7 +1531,10 @@ bool p3MsgService::removeMessageTagType(uint32_t tagId)
delete(tag);
mMsgTags.erase(mit1++);
continue;
}
if (msgEvent->mChangedMsgIds.find(std::to_string(mit1->first)) == msgEvent->mChangedMsgIds.end()) {
msgEvent->mChangedMsgIds.insert(std::to_string(mit1->first));
}
}
++mit1;
@ -1554,7 +1548,14 @@ bool p3MsgService::removeMessageTagType(uint32_t tagId)
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGE_TAGS, NOTIFY_TYPE_DEL);
auto ev = std::make_shared<RsMailTagEvent>();
ev->mMailTagEventCode = RsMailTagEventCode::TAG_REMOVED;
ev->mChangedMsgTagIds.insert(std::to_string(tagId));
rsEvents->postEvent(ev);
if (!msgEvent->mChangedMsgIds.empty()) {
rsEvents->postEvent(msgEvent);
}
return true;
}
@ -1595,7 +1596,8 @@ bool p3MsgService::setMessageTag(const std::string &msgId, uint32_t tagId, bool
}
}
int nNotifyType = 0;
auto ev = std::make_shared<RsMailStatusEvent>();
ev->mMailStatusEventCode = RsMailStatusEventCode::TAG_CHANGED;
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
@ -1614,7 +1616,7 @@ bool p3MsgService::setMessageTag(const std::string &msgId, uint32_t tagId, bool
mMsgTags.insert(std::pair<uint32_t, RsMsgTags*>(tag->msgId, tag));
nNotifyType = NOTIFY_TYPE_ADD;
ev->mChangedMsgIds.insert(msgId);
}
} else {
RsMsgTags* tag = mit->second;
@ -1632,18 +1634,18 @@ bool p3MsgService::setMessageTag(const std::string &msgId, uint32_t tagId, bool
tag->tagIds.push_back(tagId);
/* keep the list sorted */
tag->tagIds.sort();
nNotifyType = NOTIFY_TYPE_ADD;
ev->mChangedMsgIds.insert(msgId);
}
} else {
if (tagId == 0) {
/* remove all */
delete(tag);
mMsgTags.erase(mit);
nNotifyType = NOTIFY_TYPE_DEL;
ev->mChangedMsgIds.insert(msgId);
} else {
if (lit != tag->tagIds.end()) {
tag->tagIds.erase(lit);
nNotifyType = NOTIFY_TYPE_DEL;
ev->mChangedMsgIds.insert(msgId);
if (tag->tagIds.empty()) {
/* remove empty tag */
@ -1657,10 +1659,10 @@ bool p3MsgService::setMessageTag(const std::string &msgId, uint32_t tagId, bool
} /* UNLOCKED */
if (nNotifyType) {
if (!ev->mChangedMsgIds.empty()) {
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGE_TAGS, nNotifyType);
rsEvents->postEvent(ev);
return true;
}
@ -1687,8 +1689,9 @@ bool p3MsgService::MessageToTrash(const std::string &mid, bool bTrash)
std::map<uint32_t, RsMsgItem *>::iterator mit;
uint32_t msgId = atoi(mid.c_str());
bool bChanged = false;
bool bFound = false;
auto pEvent = std::make_shared<RsMailStatusEvent>();
pEvent->mMailStatusEventCode = RsMailStatusEventCode::MESSAGE_CHANGED;
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
@ -1711,23 +1714,25 @@ bool p3MsgService::MessageToTrash(const std::string &mid, bool bTrash)
if (bTrash) {
if ((mi->msgFlags & RS_MSG_FLAGS_TRASH) == 0) {
mi->msgFlags |= RS_MSG_FLAGS_TRASH;
bChanged = true;
pEvent->mChangedMsgIds.insert(std::to_string(mi->msgId));
}
} else {
if (mi->msgFlags & RS_MSG_FLAGS_TRASH) {
mi->msgFlags &= ~RS_MSG_FLAGS_TRASH;
bChanged = true;
pEvent->mChangedMsgIds.insert(std::to_string(mi->msgId));
}
}
}
}
if (bChanged) {
if (!pEvent->mChangedMsgIds.empty()) {
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
checkOutgoingMessages();
RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGELIST,NOTIFY_TYPE_MOD);
if(rsEvents) {
rsEvents->postEvent(pEvent);
}
}
return bFound;
@ -2111,14 +2116,12 @@ void p3MsgService::notifyDataStatus( const GRouterMsgPropagationId& id,
msgOutgoing.erase(it2);
#endif
RsServer::notify()->notifyListChange( NOTIFY_LIST_MESSAGELIST,
NOTIFY_TYPE_ADD );
IndicateConfigChanged();
if(rsEvents)
{
auto pEvent = std::make_shared<RsMailStatusEvent>();
pEvent->mMailStatusEventCode = RsMailStatusEventCode::NEW_MESSAGE;
pEvent->mMailStatusEventCode = RsMailStatusEventCode::MESSAGE_CHANGED;
pEvent->mChangedMsgIds.insert(std::to_string(msg_id));
rsEvents->postEvent(pEvent);
}
@ -2267,8 +2270,6 @@ bool p3MsgService::notifyGxsTransSendStatus( RsGxsTransId mailId,
}
}
RsServer::notify()->notifyListChange( NOTIFY_LIST_MESSAGELIST,
NOTIFY_TYPE_ADD );
IndicateConfigChanged();
}
else if( status >= GxsTransSendStatus::FAILED_RECEIPT_SIGNATURE )

View File

@ -38,19 +38,19 @@
using namespace Tor;
AddOnionCommand::AddOnionCommand(HiddenService *service)
: m_service(service)
: m_service(service), mSucceeded([](){}), mFailed([](int){})
{
Q_ASSERT(m_service);
assert(m_service);
}
bool AddOnionCommand::isSuccessful() const
{
return statusCode() == 250 && m_errorMessage.isEmpty();
return statusCode() == 250 && m_errorMessage.empty();
}
QByteArray AddOnionCommand::build()
ByteArray AddOnionCommand::build()
{
QByteArray out("ADD_ONION");
ByteArray out("ADD_ONION");
if (m_service->privateKey().isLoaded()) {
out += " ";
@ -61,42 +61,44 @@ QByteArray AddOnionCommand::build()
out += " NEW:BEST"; // this is v3, but without control of key type. Generates a RSA1024 key on older Tor versions.
}
foreach (const HiddenService::Target &target, m_service->targets()) {
for(const HiddenService::Target& target: m_service->targets())
{
out += " Port=";
out += QByteArray::number(target.servicePort);
out += RsUtil::NumberToString(target.servicePort);
out += ",";
out += target.targetAddress.toString().toLatin1();
out += target.targetAddress;
out += ":";
out += QByteArray::number(target.targetPort);
out += RsUtil::NumberToString(target.targetPort);
}
out.append("\r\n");
return out;
}
void AddOnionCommand::onReply(int statusCode, const QByteArray &data)
void AddOnionCommand::onReply(int statusCode, const ByteArray &data)
{
TorControlCommand::onReply(statusCode, data);
if (statusCode != 250) {
m_errorMessage = QString::fromLatin1(data);
m_errorMessage = data.toString();
return;
}
const QByteArray keyPrefix("PrivateKey=");
const QByteArray sidPrefix("ServiceID=");
const ByteArray keyPrefix("PrivateKey=");
const ByteArray sidPrefix("ServiceID=");
if(data.startsWith("ServiceID=")){
QByteArray service_id = data.mid(sidPrefix.size());
if(data.startsWith(sidPrefix))
{
ByteArray service_id = data.mid(sidPrefix.size());
m_service->setServiceId(service_id);
}
if (data.startsWith(keyPrefix)) {
QByteArray keyData(data.mid(keyPrefix.size()));
if (data.startsWith(keyPrefix))
{
ByteArray keyData(data.mid(keyPrefix.size()));
CryptoKey key;
if (!key.loadFromTorMessage(keyData)) {
m_errorMessage = QStringLiteral("Key structure check failed");
m_errorMessage = "Key structure check failed";
return;
}
@ -108,9 +110,9 @@ void AddOnionCommand::onFinished(int statusCode)
{
TorControlCommand::onFinished(statusCode);
if (isSuccessful())
emit succeeded();
mSucceeded();
else
emit failed(statusCode);
mFailed(statusCode);
}

View File

@ -30,13 +30,9 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef ADDONIONCOMMAND_H
#define ADDONIONCOMMAND_H
#pragma once
#include "TorControlCommand.h"
#include <QList>
#include <QPair>
#include <QVariant>
namespace Tor
{
@ -45,33 +41,26 @@ class HiddenService;
class AddOnionCommand : public TorControlCommand
{
Q_OBJECT
Q_DISABLE_COPY(AddOnionCommand)
Q_PROPERTY(QString errorMessage READ errorMessage CONSTANT)
Q_PROPERTY(bool successful READ isSuccessful CONSTANT)
public:
AddOnionCommand(HiddenService *service);
QByteArray build();
ByteArray build();
QString errorMessage() const { return m_errorMessage; }
std::string errorMessage() const { return m_errorMessage; }
bool isSuccessful() const;
signals:
void succeeded();
void failed(int code);
void set_succeeded_callback(const std::function<void(void)>& f) { mSucceeded=f;}
void set_failed_callback(const std::function<void(int)>& f) { mFailed=f;}
protected:
HiddenService *m_service;
QString m_errorMessage;
std::string m_errorMessage;
virtual void onReply(int statusCode, const QByteArray &data);
std::function<void(void)> mSucceeded;
std::function<void(int)> mFailed;
virtual void onReply(int statusCode, const ByteArray &data);
virtual void onFinished(int statusCode);
};
}
#endif // ADDONIONCOMMAND_H

View File

@ -38,27 +38,29 @@ AuthenticateCommand::AuthenticateCommand()
{
}
QByteArray AuthenticateCommand::build(const QByteArray &data)
ByteArray AuthenticateCommand::build(const ByteArray& data)
{
if (data.isNull())
return QByteArray("AUTHENTICATE\r\n");
return ByteArray("AUTHENTICATE\r\n");
return QByteArray("AUTHENTICATE ") + data.toHex() + "\r\n";
return ByteArray("AUTHENTICATE ") + data.toHex() + "\r\n";
}
void AuthenticateCommand::onReply(int statusCode, const QByteArray &data)
void AuthenticateCommand::onReply(int statusCode, const ByteArray &data)
{
TorControlCommand::onReply(statusCode, data);
m_statusMessage = QString::fromLatin1(data);
m_statusMessage = data.toString();
}
void AuthenticateCommand::onFinished(int statusCode)
{
if (statusCode == 515) {
m_statusMessage = QStringLiteral("Authentication failed - incorrect password");
} else if (statusCode != 250) {
if (m_statusMessage.isEmpty())
m_statusMessage = QStringLiteral("Authentication failed (error %1").arg(statusCode);
m_statusMessage = "Authentication failed - incorrect password";
}
else if (statusCode != 250)
{
if (m_statusMessage.empty())
m_statusMessage = "Authentication failed (error " + RsUtil::NumberToString(statusCode) + ")";
}
TorControlCommand::onFinished(statusCode);
}

View File

@ -30,9 +30,9 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef AUTHENTICATECOMMAND_H
#define AUTHENTICATECOMMAND_H
#pragma once
#include "bytearray.h"
#include "TorControlCommand.h"
namespace Tor
@ -40,24 +40,20 @@ namespace Tor
class AuthenticateCommand : public TorControlCommand
{
Q_OBJECT
public:
AuthenticateCommand();
QByteArray build(const QByteArray &data = QByteArray());
ByteArray build(const ByteArray& data = ByteArray());
bool isSuccessful() const { return statusCode() == 250; }
QString errorMessage() const { return m_statusMessage; }
std::string errorMessage() const { return m_statusMessage; }
protected:
virtual void onReply(int statusCode, const QByteArray &data);
virtual void onReply(int statusCode, const ByteArray &data);
virtual void onFinished(int statusCode);
private:
QString m_statusMessage;
std::string m_statusMessage;
};
}
#endif // AUTHENTICATECOMMAND_H

View File

@ -0,0 +1,153 @@
/* Ricochet - https://ricochet.im/
* Copyright (C) 2014, John Brooks <john.brooks@dereferenced.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the names of the copyright owners nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include "CryptoKey.h"
#include <openssl/bn.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include "stdio.h"
#include "util/rsdebug.h"
#include "util/rsrandom.h"
#include "util/rsdir.h"
#include "retroshare/rsids.h"
#include "bytearray.h"
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
{
*p = r->p;
*q = r->q;
}
#define RSA_bits(o) (BN_num_bits((o)->n))
#endif
CryptoKey::CryptoKey()
{
}
CryptoKey::~CryptoKey()
{
clear();
}
void CryptoKey::clear()
{
key_data.clear();
}
bool CryptoKey::loadFromFile(const std::string& path)
{
FILE *file = fopen(path.c_str(),"r");
if (!file)
{
RsWarn() << "Failed to open Tor key file " << path << ": errno = " << errno ;
return false;
}
ByteArray data ;
int c;
while(EOF != (c=fgetc(file)))
data.push_back((unsigned char)c);
fclose(file);
if(data.startsWith("-----BEGIN RSA PRIVATE KEY-----"))
{
std::cerr << "Note: Reading/converting Tor v2 key format." << std::endl;
// This to be compliant with old format. New format is oblivious to the type of key so we dont need a header
data = data.replace(ByteArray("-----BEGIN RSA PRIVATE KEY-----"),ByteArray());
data = data.replace(ByteArray("-----END RSA PRIVATE KEY-----"),ByteArray());
data = data.replace(ByteArray("\n"),ByteArray());
data = data.replace(ByteArray("\t"),ByteArray());
data = ByteArray("RSA1024:")+data;
}
std::cerr << "Have read the following key: " << std::endl;
std::cerr << data.toString() << std::endl;
key_data = data;
return true;
}
bool CryptoKey::loadFromTorMessage(const ByteArray& b)
{
// note: We should probably check the structure a bit more, for security.
std::cerr << "Loading new key:" << std::endl;
if(b.startsWith("RSA1024"))
std::cerr << " type: RSA-1024 (Tor v2)" << std::endl;
else if(b.startsWith("ED25519-V3"))
std::cerr << " type: ED25519-V3 (Tor v3)" << std::endl;
else if(b.indexOf(':'))
{
std::cerr << " unknown type, or bad syntax in key: \"" << b.left(b.indexOf(':')).toString() << "\". Not accepted." << std::endl;
return false;
}
key_data = b;
return true;
}
/* Cryptographic hash of a password as expected by Tor's HashedControlPassword */
ByteArray torControlHashedPassword(const ByteArray &password)
{
ByteArray salt(8);
RsRandom::random_bytes(&salt[0],8);
uint32_t count = ((uint32_t)16 + (96 & 15)) << ((96 >> 4) + 6);
SHA_CTX hash;
SHA1_Init(&hash);
ByteArray tmp = salt + password;
while (count)
{
int c = std::min((size_t)count, tmp.size());
SHA1_Update(&hash, reinterpret_cast<const void*>(tmp.data()), c);
count -= c;
}
unsigned char md[20];
SHA1_Final(md, &hash);
/* 60 is the hex-encoded value of 96, which is a constant used by Tor's algorithm. */
return ByteArray("16:") + salt.toHex().toUpper() + ByteArray("60") + ByteArray(md, 20).toHex().toUpper();
}

View File

@ -30,22 +30,35 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SECURERNG_H
#define SECURERNG_H
#pragma once
#include <QByteArray>
#include "bytearray.h"
class SecureRNG
class CryptoKey
{
public:
static bool seed();
enum KeyType {
PrivateKey,
PublicKey
};
static void random(char *buf, int size);
static QByteArray random(int size);
enum KeyFormat {
PEM,
DER
};
static QByteArray randomPrintable(int length);
static unsigned randomInt(unsigned max);
static quint64 randomInt64(quint64 max);
CryptoKey();
~CryptoKey();
bool loadFromFile(const std::string &path);
void clear();
const ByteArray bytes() const { return key_data; }
bool loadFromTorMessage(const ByteArray& b);
bool isLoaded() const { return !key_data.isNull(); }
private:
ByteArray key_data;
};
#endif // SECURERNG_H
ByteArray torControlHashedPassword(const ByteArray &password);

View File

@ -32,7 +32,6 @@
#include "GetConfCommand.h"
#include "StrUtil.h"
#include <QDebug>
using namespace Tor;
@ -41,25 +40,25 @@ GetConfCommand::GetConfCommand(Type t)
{
}
QByteArray GetConfCommand::build(const QByteArray &key)
ByteArray GetConfCommand::build(const std::string &key)
{
return build(QList<QByteArray>() << key);
return build(std::list<std::string> { key } );
}
QByteArray GetConfCommand::build(const QList<QByteArray> &keys)
ByteArray GetConfCommand::build(const std::list<std::string> &keys)
{
QByteArray out;
ByteArray out;
if (type == GetConf) {
out = "GETCONF";
} else if (type == GetInfo) {
out = "GETINFO";
} else {
Q_ASSERT(false);
assert(false);
return out;
}
foreach (const QByteArray &key, keys) {
out.append(' ');
for(const ByteArray &key: keys) {
out.push_back(' ');
out.append(key);
}
@ -67,49 +66,29 @@ QByteArray GetConfCommand::build(const QList<QByteArray> &keys)
return out;
}
void GetConfCommand::onReply(int statusCode, const QByteArray &data)
void GetConfCommand::onReply(int statusCode, const ByteArray &data)
{
TorControlCommand::onReply(statusCode, data);
if (statusCode != 250)
return;
int kep = data.indexOf('=');
QString key = QString::fromLatin1(data.mid(0, kep));
QVariant value;
std::string key = data.mid(0, kep).toString();
std::string value;
if (kep >= 0)
value = QString::fromLatin1(unquotedString(data.mid(kep + 1)));
value = unquotedString(data.mid(kep + 1)).toString();
m_lastKey = key;
QVariantMap::iterator it = m_results.find(key);
if (it != m_results.end()) {
// Make a list of values
QVariantList results = it->toList();
if (results.isEmpty())
results.append(*it);
results.append(value);
*it = QVariant(results);
} else {
m_results.insert(key, value);
}
m_results[key].push_back(value);
}
void GetConfCommand::onDataLine(const QByteArray &data)
void GetConfCommand::onDataLine(const ByteArray &data)
{
if (m_lastKey.isEmpty()) {
qWarning() << "torctrl: Unexpected data line in GetConf command";
if (m_lastKey.empty()) {
RsWarn() << "torctrl: Unexpected data line in GetConf command";
return;
}
QVariantMap::iterator it = m_results.find(m_lastKey);
if (it != m_results.end()) {
QVariantList results = it->toList();
if (results.isEmpty() && !it->toByteArray().isEmpty())
results.append(*it);
results.append(data);
*it = QVariant(results);
} else {
m_results.insert(m_lastKey, QVariantList() << data);
}
m_results[m_lastKey].push_back(data.toString());
}
void GetConfCommand::onDataFinished()
@ -117,8 +96,13 @@ void GetConfCommand::onDataFinished()
m_lastKey.clear();
}
QVariant GetConfCommand::get(const QByteArray &key) const
std::list<std::string> GetConfCommand::get(const std::string& key) const
{
return m_results.value(QString::fromLatin1(key));
auto it = m_results.find(key);
if(it != m_results.end())
return it->second;
else
return std::list<std::string>();
}

View File

@ -30,23 +30,16 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef GETCONFCOMMAND_H
#define GETCONFCOMMAND_H
#pragma once
#include <map>
#include "TorControlCommand.h"
#include <QList>
#include <QVariantMap>
namespace Tor
{
class GetConfCommand : public TorControlCommand
{
Q_OBJECT
Q_DISABLE_COPY(GetConfCommand)
Q_PROPERTY(QVariantMap results READ results CONSTANT)
public:
enum Type {
GetConf,
@ -56,22 +49,20 @@ public:
GetConfCommand(Type type);
QByteArray build(const QByteArray &key);
QByteArray build(const QList<QByteArray> &keys);
ByteArray build(const std::string &key);
ByteArray build(const std::list<std::string> &keys);
const QVariantMap &results() const { return m_results; }
QVariant get(const QByteArray &key) const;
const std::map<std::string,std::list<std::string> > &results() const { return m_results; }
std::list<std::string> get(const std::string &key) const;
protected:
virtual void onReply(int statusCode, const QByteArray &data);
virtual void onDataLine(const QByteArray &data);
virtual void onReply(int statusCode, const ByteArray &data);
virtual void onDataLine(const ByteArray &data);
virtual void onDataFinished();
private:
QVariantMap m_results;
QString m_lastKey;
std::map<std::string,std::list<std::string> > m_results;
std::string m_lastKey;
};
}
#endif // GETCONFCOMMAND_H

View File

@ -32,34 +32,32 @@
#include "HiddenService.h"
#include "TorControl.h"
#include "TorSocket.h"
#include "CryptoKey.h"
#include "Useful.h"
#include <QDir>
#include <QFile>
#include <QTimer>
#include <QDebug>
#include "util/rsdir.h"
#include <fstream>
using namespace Tor;
HiddenService::HiddenService(QObject *parent)
: QObject(parent), m_status(NotCreated)
HiddenService::HiddenService(HiddenServiceClient *client)
: m_status(NotCreated), m_client(client)
{
}
HiddenService::HiddenService(const QString &path, QObject *parent)
: QObject(parent), m_dataPath(path), m_status(NotCreated)
HiddenService::HiddenService(HiddenServiceClient *client,const std::string& path)
: m_dataPath(path), m_status(NotCreated), m_client(client)
{
/* Set the initial status and, if possible, load the hostname */
if (QDir(m_dataPath).exists(QLatin1String("private_key"))) {
if(RsDirUtil::fileExists(m_dataPath + "/private_key"))
{
loadPrivateKey();
if (!m_hostname.isEmpty())
if (!m_hostname.empty())
m_status = Offline;
}
}
HiddenService::HiddenService(const CryptoKey &privateKey, const QString &path, QObject *parent)
: QObject(parent), m_dataPath(path), m_status(NotCreated)
HiddenService::HiddenService(HiddenServiceClient *client,const CryptoKey &privateKey, const std::string &path)
: m_dataPath(path), m_status(NotCreated), m_client(client)
{
setPrivateKey(privateKey);
m_status = Offline;
@ -73,69 +71,89 @@ void HiddenService::setStatus(Status newStatus)
Status old = m_status;
m_status = newStatus;
emit statusChanged(m_status, old);
if(m_client)
m_client->hiddenServiceStatusChanged(m_status,old); //emit statusChanged(m_status, old);
if (m_status == Online)
emit serviceOnline();
if(m_client)
m_client->hiddenServiceOnline(); //emit serviceOnline();
}
void HiddenService::addTarget(const Target &target)
{
m_targets.append(target);
m_targets.push_back(target);
}
void HiddenService::addTarget(quint16 servicePort, QHostAddress targetAddress, quint16 targetPort)
void HiddenService::addTarget(uint16_t servicePort, std::string targetAddress, uint16_t targetPort)
{
Target t = { targetAddress, servicePort, targetPort };
m_targets.append(t);
m_targets.push_back(t);
}
void HiddenService::setServiceId(const QByteArray& sid)
void HiddenService::setServiceId(const ByteArray& sid)
{
m_service_id = sid;
m_hostname = sid + ".onion";
m_hostname = sid.toString() + ".onion";
emit hostnameChanged();
if(m_client)
m_client->hiddenServiceHostnameChanged(); // emit hostnameChanged();
}
void HiddenService::setPrivateKey(const CryptoKey &key)
{
if (m_privateKey.isLoaded()) {
BUG() << "Cannot change the private key on an existing HiddenService";
RsErr() << "Cannot change the private key on an existing HiddenService";
return;
}
#ifdef TO_REMOVE
if (!key.isPrivate()) {
BUG() << "Cannot create a hidden service with a public key";
return;
}
#endif
m_privateKey = key;
emit privateKeyChanged();
if(m_client)
m_client->hiddenServicePrivateKeyChanged(); //emit privateKeyChanged();
}
void HiddenService::loadPrivateKey()
bool HiddenService::loadPrivateKey()
{
if (m_privateKey.isLoaded() || m_dataPath.isEmpty())
return;
if (m_privateKey.isLoaded() || m_dataPath.empty())
return false;
bool ok = m_privateKey.loadFromFile(m_dataPath + QLatin1String("/private_key"));
bool ok = m_privateKey.loadFromFile(m_dataPath + "/private_key");
if (!ok) {
qWarning() << "Failed to load hidden service key";
return;
RsWarn() << "Failed to load hidden service key";
return false;
}
emit privateKeyChanged();
// Also load the onion address stored in "hostname" file. This is not needed, except for early display
// of the onion address, since the onion address will be re-computed by Tor (to the same value) when the
// service is published.
std::ifstream i((m_dataPath + "/hostname").c_str());
if(i)
{
std::string s;
i >> s;
if(ByteArray(s).endsWith(ByteArray(".onion")))
{
m_hostname = s;
m_service_id = s.substr(0,s.length() - std::string(".onion").length());
RsDbg() << "Read existing hostname: " << m_hostname;
}
i.close();
}
if(m_client)
m_client->hiddenServicePrivateKeyChanged(); // emit privateKeyChanged();
return true;
}
void HiddenService::servicePublished()
{
loadPrivateKey();
if (m_hostname.isEmpty()) {
if (m_hostname.empty()) {
std::cerr << "Failed to read hidden service hostname" << std::endl;
return;
}

View File

@ -30,31 +30,35 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef HIDDENSERVICE_H
#define HIDDENSERVICE_H
#pragma once
#include <QObject>
#include <QHostAddress>
#include <QList>
#include "CryptoKey.h"
#include "bytearray.h"
namespace Tor
{
class TorSocket;
// This class is used to receive synchroneous notifications from the hidden service.
// Each client should implement its own notification handling.
class HiddenService : public QObject
class HiddenServiceClient
{
Q_OBJECT
Q_DISABLE_COPY(HiddenService)
public:
virtual void hiddenServiceStatusChanged(int /* newStatus */, int /* oldStatus */) =0;
virtual void hiddenServiceOnline() =0;
virtual void hiddenServicePrivateKeyChanged() =0;
virtual void hiddenServiceHostnameChanged() =0;
};
friend class TorControlPrivate;
class HiddenService
{
friend class TorControl;
public:
struct Target
{
QHostAddress targetAddress;
quint16 servicePort, targetPort;
std::string targetAddress;
uint16_t servicePort, targetPort;
};
enum Status
@ -64,45 +68,42 @@ public:
Online /* Published */
};
HiddenService(QObject *parent = 0);
HiddenService(const QString &dataPath, QObject *parent = 0);
HiddenService(const CryptoKey &privateKey, const QString &dataPath = QString(), QObject *parent = 0);
HiddenService(HiddenServiceClient *client);
HiddenService(HiddenServiceClient *client, const std::string &dataPath);
HiddenService(HiddenServiceClient *client, const CryptoKey &privateKey, const std::string &dataPath = std::string());
Status status() const { return m_status; }
const QString& hostname() const { return m_hostname; }
const QString serviceId() const { return QString(m_service_id); }
const QString& dataPath() const { return m_dataPath; }
const std::string& hostname() const { return m_hostname; }
const std::string serviceId() const { return m_service_id.toString(); }
const std::string& dataPath() const { return m_dataPath; }
CryptoKey privateKey() { return m_privateKey; }
void setPrivateKey(const CryptoKey &privateKey);
void setServiceId(const QByteArray& sid);
void setServiceId(const ByteArray &sid);
const QList<Target> &targets() const { return m_targets; }
const std::list<Target> &targets() const { return m_targets; }
void addTarget(const Target &target);
void addTarget(quint16 servicePort, QHostAddress targetAddress, quint16 targetPort);
void addTarget(uint16_t servicePort, std::string targetAddress, uint16_t targetPort);
signals:
void statusChanged(int newStatus, int oldStatus);
void serviceOnline();
void privateKeyChanged();
void hostnameChanged();
private slots:
void servicePublished();
private:
QString m_dataPath;
QList<Target> m_targets;
QString m_hostname;
std::string m_dataPath;
std::list<Target> m_targets;
std::string m_hostname;
Status m_status;
CryptoKey m_privateKey;
QByteArray m_service_id;
ByteArray m_service_id;
void loadPrivateKey();
bool loadPrivateKey();
void setStatus(Status newStatus);
HiddenServiceClient *m_client;
// make the object non copyable
HiddenService(const HiddenService&) {}
HiddenService& operator=(const HiddenService&) { return *this ; }
};
}
#endif // HIDDENSERVICE_H

View File

@ -30,10 +30,11 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include "PendingOperation.h"
PendingOperation::PendingOperation(QObject *parent)
: QObject(parent), m_finished(false)
PendingOperation::PendingOperation()
: m_finished(false),mFinishedCallback([](){}), mSuccessCallback([](){}),mErrorCallback([](const std::string&){})
{
}
@ -44,41 +45,44 @@ bool PendingOperation::isFinished() const
bool PendingOperation::isSuccess() const
{
return m_finished && m_errorMessage.isNull();
return m_finished && m_errorMessage.empty();
}
bool PendingOperation::isError() const
{
return m_finished && !m_errorMessage.isNull();
return m_finished && !m_errorMessage.empty();
}
QString PendingOperation::errorMessage() const
std::string PendingOperation::errorMessage() const
{
return m_errorMessage;
}
void PendingOperation::finishWithError(const QString &message)
void PendingOperation::finishWithError(const std::string &message)
{
if (message.isEmpty())
m_errorMessage = QStringLiteral("Unknown Error");
if (message.empty())
m_errorMessage = "Unknown Error";
m_errorMessage = message;
if (!m_finished) {
m_finished = true;
emit finished();
emit error(m_errorMessage);
mErrorCallback(m_errorMessage);
mFinishedCallback();
}
}
void PendingOperation::finishWithSuccess()
{
Q_ASSERT(m_errorMessage.isNull());
assert(m_errorMessage.empty());
if (!m_finished) {
m_finished = true;
emit finished();
if (isSuccess())
emit success();
mSuccessCallback();
mFinishedCallback();
}
}

View File

@ -30,10 +30,10 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PENDINGOPERATION_H
#define PENDINGOPERATION_H
#pragma once
#include <QObject>
#include <functional>
#include <string>
/* Represents an asynchronous operation for reporting status
*
@ -48,40 +48,27 @@
* PendingOperation will emit finished() and one of success() or
* error() when completed.
*/
class PendingOperation : public QObject
class PendingOperation
{
Q_OBJECT
Q_PROPERTY(bool isFinished READ isFinished NOTIFY finished FINAL)
Q_PROPERTY(bool isSuccess READ isSuccess NOTIFY success FINAL)
Q_PROPERTY(bool isError READ isError NOTIFY error FINAL)
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY finished FINAL)
public:
PendingOperation(QObject *parent = 0);
PendingOperation();
bool isFinished() const;
bool isSuccess() const;
bool isError() const;
QString errorMessage() const;
std::string errorMessage() const;
signals:
// Always emitted once when finished, regardless of status
void finished();
// One of error() or success() is emitted once
void error(const QString &errorMessage);
void success();
protected slots:
void finishWithError(const QString &errorMessage);
void finishWithError(const std::string &errorMessage);
void finishWithSuccess();
void set_finished_callback(const std::function<void(void)>& f) { mFinishedCallback = f; }
private:
bool m_finished;
QString m_errorMessage;
std::string m_errorMessage;
std::function<void(void)> mFinishedCallback;
std::function<void(void)> mSuccessCallback;
std::function<void(const std::string&)> mErrorCallback;
};
Q_DECLARE_METATYPE(PendingOperation*)
#endif

Some files were not shown because too many files have changed in this diff Show More