Switch project license from GPL-3.0-or-later to MIT

This commit is contained in:
Adrien Béraud committed 2026-01-17 22:25:43 -05:00
1 parent 4fd4745647
commit f9cba71d51
123 files changed
+5944 -7638

No files matched your search

-674
View File
@@ -1,674 +0,0 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
-674
View File
@@ -1,674 +0,0 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
+7
View File
@@ -0,0 +1,7 @@
Copyright (c) 2014-2026 Savoir-faire Linux Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+1 -5
View File
@@ -157,11 +157,7 @@ IRC: join us on Libera.chat at [`#opendht`](https://web.libera.chat/#opendht).
## License
Copyright (c) 2014-2026 Savoir-faire Linux Inc.
OpenDHT is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
See COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html for the full GPLv3 license.
OpenDHT is released under the MIT License. See [LICENSE](LICENSE) for details.
## Acknowledgements
This project was originally based on https://github.com/jech/dht by Juliusz Chroboczek.
+317 -159
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "opendht_c.h"
@@ -31,67 +17,94 @@ extern "C" {
#include <errno.h>
const char* dht_version()
const char*
dht_version()
{
return dht::version();
}
// dht::InfoHash
inline dht_infohash dht_infohash_to_c(const dht::InfoHash& h) {
inline dht_infohash
dht_infohash_to_c(const dht::InfoHash& h)
{
dht_infohash ret;
*reinterpret_cast<dht::InfoHash*>(&ret) = h;
return ret;
}
inline dht_pkid dht_pkid_to_c(const dht::PkId& h) {
inline dht_pkid
dht_pkid_to_c(const dht::PkId& h)
{
dht_pkid ret;
*reinterpret_cast<dht::PkId*>(&ret) = h;
return ret;
}
const char* dht_infohash_print(const dht_infohash* h) {
const char*
dht_infohash_print(const dht_infohash* h)
{
return reinterpret_cast<const dht::InfoHash*>(h)->to_c_str();
}
void dht_infohash_zero(dht_infohash* h) {
*reinterpret_cast<dht::InfoHash*>(h) = dht::InfoHash{};
void
dht_infohash_zero(dht_infohash* h)
{
*reinterpret_cast<dht::InfoHash*>(h) = dht::InfoHash {};
}
void dht_infohash_random(dht_infohash* h) {
void
dht_infohash_random(dht_infohash* h)
{
*reinterpret_cast<dht::InfoHash*>(h) = dht::InfoHash::getRandom();
}
void dht_infohash_get(dht_infohash* h, const uint8_t* dat, size_t dat_size) {
void
dht_infohash_get(dht_infohash* h, const uint8_t* dat, size_t dat_size)
{
*reinterpret_cast<dht::InfoHash*>(h) = dht::InfoHash::get(dat, dat_size);
}
void dht_infohash_get_from_string(dht_infohash* h, const char* dat) {
*reinterpret_cast<dht::InfoHash*>(h) = dht::InfoHash::get((const uint8_t*)dat, (size_t)strlen(dat));
void
dht_infohash_get_from_string(dht_infohash* h, const char* dat)
{
*reinterpret_cast<dht::InfoHash*>(h) = dht::InfoHash::get((const uint8_t*) dat, (size_t) strlen(dat));
}
bool dht_infohash_is_zero(const dht_infohash* h) {
bool
dht_infohash_is_zero(const dht_infohash* h)
{
return !static_cast<bool>(*reinterpret_cast<const dht::InfoHash*>(h));
}
void dht_infohash_from_hex(dht_infohash* h, const char* dat) {
*h = dht_infohash_to_c(dht::InfoHash(std::string_view(dat, HASH_LEN*2)));
void
dht_infohash_from_hex(dht_infohash* h, const char* dat)
{
*h = dht_infohash_to_c(dht::InfoHash(std::string_view(dat, HASH_LEN * 2)));
}
void dht_infohash_from_hex_null(dht_infohash* h, const char* dat) {
void
dht_infohash_from_hex_null(dht_infohash* h, const char* dat)
{
*h = dht_infohash_to_c(dht::InfoHash(std::string_view(dat)));
}
const char* dht_pkid_print(const dht_pkid* h) {
const char*
dht_pkid_print(const dht_pkid* h)
{
return reinterpret_cast<const dht::PkId*>(h)->to_c_str();
}
// dht::Blob
void dht_blob_delete(dht_blob* data) {
void
dht_blob_delete(dht_blob* data)
{
delete reinterpret_cast<dht::Blob*>(data);
}
dht_data_view dht_blob_get_data(const dht_blob* data) {
dht_data_view
dht_blob_get_data(const dht_blob* data)
{
dht_data_view view;
view.data = reinterpret_cast<const dht::Blob*>(data)->data();
view.size = reinterpret_cast<const dht::Blob*>(data)->size();
@@ -99,7 +112,9 @@ dht_data_view dht_blob_get_data(const dht_blob* data) {
}
// dht::Value
dht_data_view dht_value_get_data(const dht_value* data) {
dht_data_view
dht_value_get_data(const dht_value* data)
{
const ValueSp& vsp(*reinterpret_cast<const ValueSp*>(data));
dht_data_view view;
view.data = vsp->data.data();
@@ -107,140 +122,193 @@ dht_data_view dht_value_get_data(const dht_value* data) {
return view;
}
dht_value_id dht_value_get_id(const dht_value* data) {
dht_value_id
dht_value_get_id(const dht_value* data)
{
const ValueSp& vsp(*reinterpret_cast<const ValueSp*>(data));
return vsp->id;
}
dht_publickey* dht_value_get_owner(const dht_value* data) {
dht_publickey*
dht_value_get_owner(const dht_value* data)
{
const ValueSp& vsp(*reinterpret_cast<const ValueSp*>(data));
return vsp->owner ? reinterpret_cast<dht_publickey*>(new PubkeySp(vsp->owner)) : nullptr;
}
dht_infohash dht_value_get_recipient(const dht_value* data) {
dht_infohash
dht_value_get_recipient(const dht_value* data)
{
const ValueSp& vsp(*reinterpret_cast<const ValueSp*>(data));
return dht_infohash_to_c(vsp->recipient);
}
const char* dht_value_get_user_type(const dht_value* data) {
const char*
dht_value_get_user_type(const dht_value* data)
{
const ValueSp& vsp(*reinterpret_cast<const ValueSp*>(data));
return vsp->user_type.c_str();
}
void dht_value_set_user_type(dht_value* data, const char* user_type) {
void
dht_value_set_user_type(dht_value* data, const char* user_type)
{
(*reinterpret_cast<ValueSp*>(data))->user_type = user_type;
}
dht_value* dht_value_with_id_new(const uint8_t* data, size_t size, uint64_t value_id) {
return reinterpret_cast<dht_value*>(new ValueSp(std::make_shared<dht::Value>(dht::ValueType::USER_DATA.id, data, size, value_id)));
dht_value*
dht_value_with_id_new(const uint8_t* data, size_t size, uint64_t value_id)
{
return reinterpret_cast<dht_value*>(
new ValueSp(std::make_shared<dht::Value>(dht::ValueType::USER_DATA.id, data, size, value_id)));
}
dht_value* dht_value_new(const uint8_t* data, size_t size) {
dht_value*
dht_value_new(const uint8_t* data, size_t size)
{
return dht_value_with_id_new(data, size, 0);
}
dht_value* dht_value_new_from_string(const char* str) {
ValueSp value = std::make_shared<dht::Value>((const uint8_t*)str, strlen(str));
dht_value*
dht_value_new_from_string(const char* str)
{
ValueSp value = std::make_shared<dht::Value>((const uint8_t*) str, strlen(str));
value->user_type = "text/plain";
return reinterpret_cast<dht_value*>(new ValueSp(std::move(value)));
}
dht_value* dht_value_ref(const dht_value* v) {
dht_value*
dht_value_ref(const dht_value* v)
{
return reinterpret_cast<dht_value*>(new ValueSp(*reinterpret_cast<const ValueSp*>(v)));
}
void dht_value_unref(dht_value* v) {
void
dht_value_unref(dht_value* v)
{
delete reinterpret_cast<ValueSp*>(v);
}
// dht::crypto::PublicKey
dht_publickey* dht_publickey_import(const uint8_t* dat, size_t dat_size) {
dht_publickey*
dht_publickey_import(const uint8_t* dat, size_t dat_size)
{
try {
return reinterpret_cast<dht_publickey*>(new PubkeySp(std::make_shared<const dht::crypto::PublicKey>(dat, dat_size)));
return reinterpret_cast<dht_publickey*>(
new PubkeySp(std::make_shared<const dht::crypto::PublicKey>(dat, dat_size)));
} catch (const dht::crypto::CryptoException& e) {
return nullptr;
}
}
void dht_publickey_delete(dht_publickey* pk) {
void
dht_publickey_delete(dht_publickey* pk)
{
delete reinterpret_cast<PubkeySp*>(pk);
}
int dht_publickey_export(const dht_publickey* pk, char* out, size_t* outlen) {
int
dht_publickey_export(const dht_publickey* pk, char* out, size_t* outlen)
{
const auto& pkey = *reinterpret_cast<const PubkeySp*>(pk);
return pkey->pack((uint8_t*)out, outlen);
return pkey->pack((uint8_t*) out, outlen);
}
dht_infohash dht_publickey_get_id(const dht_publickey* pk) {
dht_infohash
dht_publickey_get_id(const dht_publickey* pk)
{
const auto& pkey = *reinterpret_cast<const PubkeySp*>(pk);
return dht_infohash_to_c(pkey->getId());
}
dht_pkid dht_publickey_get_long_id(const dht_publickey* pk) {
dht_pkid
dht_publickey_get_long_id(const dht_publickey* pk)
{
const auto& pkey = *reinterpret_cast<const PubkeySp*>(pk);
return dht_pkid_to_c(pkey->getLongId());
}
bool dht_publickey_check_signature(const dht_publickey* pk, const char* data, size_t data_size, const char* signature, size_t signature_size) {
bool
dht_publickey_check_signature(
const dht_publickey* pk, const char* data, size_t data_size, const char* signature, size_t signature_size)
{
const auto& pkey = *reinterpret_cast<const PubkeySp*>(pk);
return pkey->checkSignature((const uint8_t*)data, data_size, (const uint8_t*)signature, signature_size);
return pkey->checkSignature((const uint8_t*) data, data_size, (const uint8_t*) signature, signature_size);
}
dht_blob* dht_publickey_encrypt(const dht_publickey* pk, const char* data, size_t data_size) {
dht_blob*
dht_publickey_encrypt(const dht_publickey* pk, const char* data, size_t data_size)
{
const auto& pkey = *reinterpret_cast<const PubkeySp*>(pk);
try {
auto rdata = std::make_unique<dht::Blob>();
*rdata = pkey->encrypt((const uint8_t*)data, data_size);
return (dht_blob*)rdata.release();
*rdata = pkey->encrypt((const uint8_t*) data, data_size);
return (dht_blob*) rdata.release();
} catch (...) {
return nullptr;
}
}
// dht::crypto::PrivateKey
dht_privatekey* dht_privatekey_generate(unsigned key_length_bits) {
dht_privatekey*
dht_privatekey_generate(unsigned key_length_bits)
{
if (key_length_bits == 0)
key_length_bits = 4096;
return reinterpret_cast<dht_privatekey*>(new PrivkeySp(std::make_shared<dht::crypto::PrivateKey>(dht::crypto::PrivateKey::generate(key_length_bits))));
return reinterpret_cast<dht_privatekey*>(
new PrivkeySp(std::make_shared<dht::crypto::PrivateKey>(dht::crypto::PrivateKey::generate(key_length_bits))));
}
dht_privatekey* dht_privatekey_import(const uint8_t* dat, size_t dat_size, const char* password) {
dht_privatekey*
dht_privatekey_import(const uint8_t* dat, size_t dat_size, const char* password)
{
try {
return reinterpret_cast<dht_privatekey*>(new PrivkeySp(std::make_shared<dht::crypto::PrivateKey>(dat, dat_size, password)));
return reinterpret_cast<dht_privatekey*>(
new PrivkeySp(std::make_shared<dht::crypto::PrivateKey>(dat, dat_size, password)));
} catch (const dht::crypto::CryptoException& e) {
return nullptr;
}
}
int dht_privatekey_export(const dht_privatekey* k, char* out, size_t* out_size, const char* password) {
int
dht_privatekey_export(const dht_privatekey* k, char* out, size_t* out_size, const char* password)
{
if (!out or !out_size or !*out_size)
return -1;
const auto& key = *reinterpret_cast<const PrivkeySp*>(k);
return key->serialize((uint8_t*)out, out_size, password);
return key->serialize((uint8_t*) out, out_size, password);
}
dht_publickey* dht_privatekey_get_publickey(const dht_privatekey* k) {
dht_publickey*
dht_privatekey_get_publickey(const dht_privatekey* k)
{
const auto& key = *reinterpret_cast<const PrivkeySp*>(k);
return reinterpret_cast<dht_publickey*>(new PubkeySp(key->getSharedPublicKey()));
}
dht_blob* dht_privatekey_decrypt(const dht_privatekey* k, const char* data, size_t data_size) {
dht_blob*
dht_privatekey_decrypt(const dht_privatekey* k, const char* data, size_t data_size)
{
const auto& key = *reinterpret_cast<const PrivkeySp*>(k);
try {
auto rdata = std::make_unique<dht::Blob>();
*rdata = key->decrypt((const uint8_t*)data, data_size);
return (dht_blob*)rdata.release();
*rdata = key->decrypt((const uint8_t*) data, data_size);
return (dht_blob*) rdata.release();
} catch (...) {
return nullptr;
}
}
void dht_privatekey_delete(dht_privatekey* pk) {
void
dht_privatekey_delete(dht_privatekey* pk)
{
delete reinterpret_cast<PrivkeySp*>(pk);
}
// dht::crypto::Certificate
dht_certificate* dht_certificate_import(const uint8_t* dat, size_t dat_size) {
dht_certificate*
dht_certificate_import(const uint8_t* dat, size_t dat_size)
{
try {
return reinterpret_cast<dht_certificate*>(new CertSp(std::make_shared<dht::crypto::Certificate>(dat, dat_size)));
} catch (const dht::crypto::CryptoException& e) {
@@ -248,21 +316,29 @@ dht_certificate* dht_certificate_import(const uint8_t* dat, size_t dat_size) {
}
}
void dht_certificate_delete(dht_certificate* c) {
void
dht_certificate_delete(dht_certificate* c)
{
delete reinterpret_cast<CertSp*>(c);
}
dht_infohash dht_certificate_get_id(const dht_certificate* c) {
dht_infohash
dht_certificate_get_id(const dht_certificate* c)
{
const auto& cert = *reinterpret_cast<const CertSp*>(c);
return dht_infohash_to_c(cert->getId());
}
dht_pkid dht_certificate_get_long_id(const dht_certificate* c) {
dht_pkid
dht_certificate_get_long_id(const dht_certificate* c)
{
const auto& cert = *reinterpret_cast<const CertSp*>(c);
return dht_pkid_to_c(cert->getLongId());
}
dht_publickey* dht_certificate_get_publickey(const dht_certificate* c) {
dht_publickey*
dht_certificate_get_publickey(const dht_certificate* c)
{
const auto& cert = *reinterpret_cast<const CertSp*>(c);
return reinterpret_cast<dht_publickey*>(new PubkeySp(cert->getSharedPublicKey()));
}
@@ -273,7 +349,9 @@ dht_publickey* dht_certificate_get_publickey(const dht_certificate* c) {
#endif
// dht::crypto::Identity
inline dht::crypto::Identity dht_identity_from_c(const dht_identity* cid) {
inline dht::crypto::Identity
dht_identity_from_c(const dht_identity* cid)
{
dht::crypto::Identity id {};
if (cid and cid->privatekey)
id.first = *reinterpret_cast<const PrivkeySp*>(cid->privatekey);
@@ -282,7 +360,9 @@ inline dht::crypto::Identity dht_identity_from_c(const dht_identity* cid) {
return id;
}
inline dht_identity dht_identity_to_c(const dht::crypto::Identity& id) {
inline dht_identity
dht_identity_to_c(const dht::crypto::Identity& id)
{
dht_identity cid {};
cid.privatekey = id.first ? reinterpret_cast<dht_privatekey*>(new PrivkeySp(id.first)) : NULL;
cid.certificate = id.second ? reinterpret_cast<dht_certificate*>(new CertSp(id.second)) : NULL;
@@ -293,11 +373,15 @@ inline dht_identity dht_identity_to_c(const dht::crypto::Identity& id) {
extern "C" {
#endif
OPENDHT_C_PUBLIC dht_identity dht_identity_generate(const char* common_name, const dht_identity* ca) {
OPENDHT_C_PUBLIC dht_identity
dht_identity_generate(const char* common_name, const dht_identity* ca)
{
return dht_identity_to_c(dht::crypto::generateIdentity(common_name, dht_identity_from_c(ca)));
}
OPENDHT_C_PUBLIC void dht_identity_delete(dht_identity* id) {
OPENDHT_C_PUBLIC void
dht_identity_delete(dht_identity* id)
{
if (id->certificate) {
dht_certificate_delete(id->certificate);
id->certificate = NULL;
@@ -309,41 +393,52 @@ OPENDHT_C_PUBLIC void dht_identity_delete(dht_identity* id) {
}
// config
void dht_runner_config_default(dht_runner_config* config) {
void
dht_runner_config_default(dht_runner_config* config)
{
memset(config, 0, sizeof(dht_runner_config));
config->threaded = true;
}
// dht::DhtRunner
dht_runner* dht_runner_new(void) {
dht_runner*
dht_runner_new(void)
{
return reinterpret_cast<dht_runner*>(new dht::DhtRunner);
}
void dht_runner_delete(dht_runner* runner) {
void
dht_runner_delete(dht_runner* runner)
{
delete reinterpret_cast<dht::DhtRunner*>(runner);
}
int dht_runner_run(dht_runner* r, in_port_t port) {
int
dht_runner_run(dht_runner* r, in_port_t port)
{
auto runner = reinterpret_cast<dht::DhtRunner*>(r);
try {
runner->run(port, {}, true);
} catch(...) {
} catch (...) {
return ENOTCONN;
}
return 0;
}
int dht_runner_run_config(dht_runner* r, in_port_t port, const dht_runner_config* conf) {
int
dht_runner_run_config(dht_runner* r, in_port_t port, const dht_runner_config* conf)
{
auto runner = reinterpret_cast<dht::DhtRunner*>(r);
try {
dht::DhtRunner::Config config;
config.dht_config.node_config.is_bootstrap = conf->dht_config.node_config.is_bootstrap;
config.dht_config.node_config.is_bootstrap = conf->dht_config.node_config.is_bootstrap;
config.dht_config.node_config.maintain_storage = conf->dht_config.node_config.maintain_storage;
config.dht_config.node_config.node_id = *reinterpret_cast<const dht::InfoHash*>(&conf->dht_config.node_config.node_id);
config.dht_config.node_config.network = conf->dht_config.node_config.network;
config.dht_config.node_config.persist_path = conf->dht_config.node_config.persist_path
? std::string(conf->dht_config.node_config.persist_path)
: std::string{};
config.dht_config.node_config.node_id = *reinterpret_cast<const dht::InfoHash*>(
&conf->dht_config.node_config.node_id);
config.dht_config.node_config.network = conf->dht_config.node_config.network;
config.dht_config.node_config.persist_path = conf->dht_config.node_config.persist_path
? std::string(conf->dht_config.node_config.persist_path)
: std::string {};
if (conf->dht_config.id.privatekey)
config.dht_config.id.first = *reinterpret_cast<const PrivkeySp*>(conf->dht_config.id.privatekey);
@@ -351,42 +446,45 @@ int dht_runner_run_config(dht_runner* r, in_port_t port, const dht_runner_config
if (conf->dht_config.id.certificate)
config.dht_config.id.second = *reinterpret_cast<const CertSp*>(conf->dht_config.id.certificate);
config.threaded = conf->threaded;
config.proxy_server = conf->proxy_server ? std::string(conf->proxy_server) : std::string{};
config.push_node_id = conf->push_node_id ? std::string(conf->push_node_id) : std::string{};
config.push_token = conf->push_token ? std::string(conf->push_token) : std::string{};
config.push_topic = conf->push_topic ? std::string(conf->push_topic) : std::string{};
config.push_platform = conf->push_platform ? std::string(conf->push_platform) : std::string{};
config.proxy_user_agent = conf->proxy_user_agent ? std::string(conf->proxy_user_agent) : std::string{};
config.peer_discovery = conf->peer_discovery;
config.peer_publish = conf->peer_publish;
config.threaded = conf->threaded;
config.proxy_server = conf->proxy_server ? std::string(conf->proxy_server) : std::string {};
config.push_node_id = conf->push_node_id ? std::string(conf->push_node_id) : std::string {};
config.push_token = conf->push_token ? std::string(conf->push_token) : std::string {};
config.push_topic = conf->push_topic ? std::string(conf->push_topic) : std::string {};
config.push_platform = conf->push_platform ? std::string(conf->push_platform) : std::string {};
config.proxy_user_agent = conf->proxy_user_agent ? std::string(conf->proxy_user_agent) : std::string {};
config.peer_discovery = conf->peer_discovery;
config.peer_publish = conf->peer_publish;
if (conf->server_ca)
config.server_ca = *reinterpret_cast<const CertSp*>(conf->server_ca);
config.client_identity = dht_identity_from_c(&conf->client_identity);
config.server_ca = *reinterpret_cast<const CertSp*>(conf->server_ca);
config.client_identity = dht_identity_from_c(&conf->client_identity);
dht::DhtRunner::Context context;
if (conf->log) {
context.logger = dht::log::getStdLogger();
}
runner->run(port, config, std::move(context));
} catch(...) {
} catch (...) {
return ENOTCONN;
}
return 0;
}
void dht_runner_ping(dht_runner* r, struct sockaddr* addr, socklen_t addr_len, dht_done_cb done_cb, void* cb_user_data) {
void
dht_runner_ping(dht_runner* r, struct sockaddr* addr, socklen_t addr_len, dht_done_cb done_cb, void* cb_user_data)
{
auto runner = reinterpret_cast<dht::DhtRunner*>(r);
if (done_cb) {
runner->bootstrap(dht::SockAddr(addr, addr_len), [done_cb, cb_user_data](bool ok){
done_cb(ok, cb_user_data);
});
runner->bootstrap(dht::SockAddr(addr, addr_len),
[done_cb, cb_user_data](bool ok) { done_cb(ok, cb_user_data); });
} else {
runner->bootstrap(dht::SockAddr(addr, addr_len));
}
}
void dht_runner_bootstrap(dht_runner* r, const char* host, const char* service) {
void
dht_runner_bootstrap(dht_runner* r, const char* host, const char* service)
{
auto runner = reinterpret_cast<dht::DhtRunner*>(r);
if (service)
runner->bootstrap(host, service);
@@ -394,136 +492,196 @@ void dht_runner_bootstrap(dht_runner* r, const char* host, const char* service)
runner->bootstrap(host);
}
void dht_runner_get(dht_runner* r, const dht_infohash* h, dht_get_cb cb, dht_done_cb done_cb, void* cb_user_data) {
void
dht_runner_get(dht_runner* r, const dht_infohash* h, dht_get_cb cb, dht_done_cb done_cb, void* cb_user_data)
{
auto runner = reinterpret_cast<dht::DhtRunner*>(r);
auto hash = reinterpret_cast<const dht::InfoHash*>(h);
runner->get(*hash, [cb,cb_user_data](std::shared_ptr<dht::Value> value){
return cb(reinterpret_cast<const dht_value*>(&value), cb_user_data);
}, [done_cb, cb_user_data](bool ok){
if (done_cb)
done_cb(ok, cb_user_data);
});
runner->get(
*hash,
[cb, cb_user_data](std::shared_ptr<dht::Value> value) {
return cb(reinterpret_cast<const dht_value*>(&value), cb_user_data);
},
[done_cb, cb_user_data](bool ok) {
if (done_cb)
done_cb(ok, cb_user_data);
});
}
struct ScopeGuardCb {
struct ScopeGuardCb
{
ScopeGuardCb(dht_shutdown_cb cb, void* data)
: onDestroy(cb), userData(data) {}
: onDestroy(cb)
, userData(data)
{}
~ScopeGuardCb() {
~ScopeGuardCb()
{
if (onDestroy)
onDestroy((void*)userData);
onDestroy((void*) userData);
}
private:
const dht_shutdown_cb onDestroy;
void const* userData;
};
dht_op_token* dht_runner_listen(dht_runner* r, const dht_infohash* h, dht_value_cb cb, dht_shutdown_cb done_cb, void* cb_user_data) {
dht_op_token*
dht_runner_listen(dht_runner* r, const dht_infohash* h, dht_value_cb cb, dht_shutdown_cb done_cb, void* cb_user_data)
{
auto runner = reinterpret_cast<dht::DhtRunner*>(r);
auto hash = reinterpret_cast<const dht::InfoHash*>(h);
auto fret = new std::future<size_t>;
*fret = runner->listen(*hash, [
cb,
cb_user_data,
guard = done_cb ? std::make_shared<ScopeGuardCb>(done_cb, cb_user_data) : std::shared_ptr<ScopeGuardCb>{}
](const std::vector<std::shared_ptr<dht::Value>>& values, bool expired) {
for (const auto& value : values) {
if (not cb(reinterpret_cast<const dht_value*>(&value), expired, cb_user_data))
return false;
}
return true;
});
return (dht_op_token*)fret;
*fret = runner->listen(*hash,
[cb,
cb_user_data,
guard = done_cb ? std::make_shared<ScopeGuardCb>(done_cb, cb_user_data)
: std::shared_ptr<ScopeGuardCb> {}](
const std::vector<std::shared_ptr<dht::Value>>& values, bool expired) {
for (const auto& value : values) {
if (not cb(reinterpret_cast<const dht_value*>(&value), expired, cb_user_data))
return false;
}
return true;
});
return (dht_op_token*) fret;
}
void dht_runner_cancel_listen(dht_runner* r, const dht_infohash* h, dht_op_token* t) {
void
dht_runner_cancel_listen(dht_runner* r, const dht_infohash* h, dht_op_token* t)
{
auto runner = reinterpret_cast<dht::DhtRunner*>(r);
auto hash = reinterpret_cast<const dht::InfoHash*>(h);
auto token = reinterpret_cast<std::future<size_t>*>(t);
runner->cancelListen(*hash, std::move(*token));
}
void dht_op_token_delete(dht_op_token* token) {
void
dht_op_token_delete(dht_op_token* token)
{
delete reinterpret_cast<std::future<size_t>*>(token);
}
void dht_runner_put(dht_runner* r, const dht_infohash* h, const dht_value* v, dht_done_cb done_cb, void* cb_user_data, bool permanent) {
void
dht_runner_put(
dht_runner* r, const dht_infohash* h, const dht_value* v, dht_done_cb done_cb, void* cb_user_data, bool permanent)
{
auto runner = reinterpret_cast<dht::DhtRunner*>(r);
auto hash = reinterpret_cast<const dht::InfoHash*>(h);
auto value = reinterpret_cast<const ValueSp*>(v);
runner->put(*hash, *value, [done_cb, cb_user_data](bool ok){
if (done_cb)
done_cb(ok, cb_user_data);
}, dht::time_point::max(), permanent);
runner->put(
*hash,
*value,
[done_cb, cb_user_data](bool ok) {
if (done_cb)
done_cb(ok, cb_user_data);
},
dht::time_point::max(),
permanent);
}
void dht_runner_put_signed(dht_runner* r, const dht_infohash* h, const dht_value* v, dht_done_cb done_cb, void* cb_user_data, bool permanent) {
void
dht_runner_put_signed(
dht_runner* r, const dht_infohash* h, const dht_value* v, dht_done_cb done_cb, void* cb_user_data, bool permanent)
{
auto runner = reinterpret_cast<dht::DhtRunner*>(r);
auto hash = reinterpret_cast<const dht::InfoHash*>(h);
auto value = reinterpret_cast<const ValueSp*>(v);
runner->putSigned(*hash, *value, [done_cb, cb_user_data](bool ok){
if (done_cb)
done_cb(ok, cb_user_data);
}, permanent);
runner->putSigned(
*hash,
*value,
[done_cb, cb_user_data](bool ok) {
if (done_cb)
done_cb(ok, cb_user_data);
},
permanent);
}
void dht_runner_put_encrypted(dht_runner* r, const dht_infohash* h, const dht_infohash* to, const dht_value* v, dht_done_cb done_cb, void* cb_user_data, bool permanent) {
void
dht_runner_put_encrypted(dht_runner* r,
const dht_infohash* h,
const dht_infohash* to,
const dht_value* v,
dht_done_cb done_cb,
void* cb_user_data,
bool permanent)
{
auto runner = reinterpret_cast<dht::DhtRunner*>(r);
auto hash = reinterpret_cast<const dht::InfoHash*>(h);
auto toHash = reinterpret_cast<const dht::InfoHash*>(to);
auto value = reinterpret_cast<const ValueSp*>(v);
runner->putEncrypted(*hash, *toHash, *value, [done_cb, cb_user_data](bool ok){
if (done_cb)
done_cb(ok, cb_user_data);
}, permanent);
runner->putEncrypted(
*hash,
*toHash,
*value,
[done_cb, cb_user_data](bool ok) {
if (done_cb)
done_cb(ok, cb_user_data);
},
permanent);
}
void dht_runner_cancel_put(dht_runner* r, const dht_infohash* h, dht_value_id value_id) {
void
dht_runner_cancel_put(dht_runner* r, const dht_infohash* h, dht_value_id value_id)
{
auto runner = reinterpret_cast<dht::DhtRunner*>(r);
auto hash = reinterpret_cast<const dht::InfoHash*>(h);
runner->cancelPut(*hash, value_id);
}
void dht_runner_shutdown(dht_runner* r, dht_shutdown_cb done_cb, void* cb_user_data) {
void
dht_runner_shutdown(dht_runner* r, dht_shutdown_cb done_cb, void* cb_user_data)
{
auto runner = reinterpret_cast<dht::DhtRunner*>(r);
runner->shutdown([done_cb, cb_user_data](){
runner->shutdown([done_cb, cb_user_data]() {
if (done_cb)
done_cb(cb_user_data);
});
}
bool dht_runner_is_running(const dht_runner* r) {
if (not r) return false;
bool
dht_runner_is_running(const dht_runner* r)
{
if (not r)
return false;
auto runner = reinterpret_cast<const dht::DhtRunner*>(r);
return runner->isRunning();
}
in_port_t dht_runner_get_bound_port(const dht_runner* r, sa_family_t af) {
in_port_t
dht_runner_get_bound_port(const dht_runner* r, sa_family_t af)
{
auto runner = reinterpret_cast<const dht::DhtRunner*>(r);
return runner->getBoundPort(af);
}
dht_infohash dht_runner_get_node_id(const dht_runner* r) {
dht_infohash
dht_runner_get_node_id(const dht_runner* r)
{
auto runner = reinterpret_cast<const dht::DhtRunner*>(r);
dht_infohash ret;
*reinterpret_cast<dht::InfoHash*>(&ret) = runner->getNodeId();
return ret;
}
dht_infohash dht_runner_get_id(const dht_runner* r) {
dht_infohash
dht_runner_get_id(const dht_runner* r)
{
auto runner = reinterpret_cast<const dht::DhtRunner*>(r);
dht_infohash ret;
*reinterpret_cast<dht::InfoHash*>(&ret) = runner->getId();
return ret;
}
struct sockaddr** dht_runner_get_public_address(const dht_runner* r) {
struct sockaddr**
dht_runner_get_public_address(const dht_runner* r)
{
auto runner = reinterpret_cast<const dht::DhtRunner*>(r);
auto addrs = const_cast<dht::DhtRunner*>(runner)->getPublicAddress();
if (addrs.empty())
return nullptr;
auto ret = (struct sockaddr**)malloc(sizeof(struct sockaddr*) * (addrs.size() + 1));
for (size_t i=0; i<addrs.size(); i++)
auto ret = (struct sockaddr**) malloc(sizeof(struct sockaddr*) * (addrs.size() + 1));
for (size_t i = 0; i < addrs.size(); i++)
ret[i] = addrs[i].release();
ret[addrs.size()] = nullptr;
return ret;
+47 -31
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#ifdef __cplusplus
@@ -38,7 +23,8 @@ typedef uint16_t in_port_t;
#endif
// Non-owning data view
struct OPENDHT_C_PUBLIC dht_data_view {
struct OPENDHT_C_PUBLIC dht_data_view
{
const uint8_t* data;
size_t size;
};
@@ -53,7 +39,10 @@ OPENDHT_C_PUBLIC dht_data_view dht_blob_get_data(const dht_blob* data);
OPENDHT_C_PUBLIC void dht_blob_delete(dht_blob* data);
// dht::InfoHash
struct OPENDHT_C_PUBLIC dht_infohash { uint8_t d[HASH_LEN]; };
struct OPENDHT_C_PUBLIC dht_infohash
{
uint8_t d[HASH_LEN];
};
typedef struct dht_infohash dht_infohash;
OPENDHT_C_PUBLIC void dht_infohash_zero(dht_infohash* h);
OPENDHT_C_PUBLIC void dht_infohash_random(dht_infohash* h);
@@ -65,7 +54,10 @@ OPENDHT_C_PUBLIC const char* dht_infohash_print(const dht_infohash* h);
OPENDHT_C_PUBLIC bool dht_infohash_is_zero(const dht_infohash* h);
// dht::PkId
struct OPENDHT_C_PUBLIC dht_pkid { uint8_t d[32]; };
struct OPENDHT_C_PUBLIC dht_pkid
{
uint8_t d[32];
};
typedef struct dht_pkid dht_pkid;
OPENDHT_C_PUBLIC const char* dht_pkid_print(const dht_pkid* h);
@@ -77,7 +69,8 @@ OPENDHT_C_PUBLIC void dht_publickey_delete(dht_publickey* pk);
OPENDHT_C_PUBLIC int dht_publickey_export(const dht_publickey* pk, char* out, size_t* out_size);
OPENDHT_C_PUBLIC dht_infohash dht_publickey_get_id(const dht_publickey* pk);
OPENDHT_C_PUBLIC dht_pkid dht_publickey_get_long_id(const dht_publickey* pk);
OPENDHT_C_PUBLIC bool dht_publickey_check_signature(const dht_publickey* pk, const char* data, size_t data_size, const char* signature, size_t signature_size);
OPENDHT_C_PUBLIC bool dht_publickey_check_signature(
const dht_publickey* pk, const char* data, size_t data_size, const char* signature, size_t signature_size);
OPENDHT_C_PUBLIC dht_blob* dht_publickey_encrypt(const dht_publickey* pk, const char* data, size_t data_size);
// dht::crypto::PrivateKey
@@ -99,7 +92,8 @@ OPENDHT_C_PUBLIC dht_pkid dht_certificate_get_long_id(const dht_certificate*);
OPENDHT_C_PUBLIC dht_publickey* dht_certificate_get_publickey(const dht_certificate*);
OPENDHT_C_PUBLIC void dht_certificate_delete(dht_certificate*);
struct OPENDHT_C_PUBLIC dht_identity {
struct OPENDHT_C_PUBLIC dht_identity
{
dht_privatekey* privatekey;
dht_certificate* certificate;
};
@@ -134,7 +128,8 @@ typedef struct dht_op_token dht_op_token;
OPENDHT_C_PUBLIC void dht_op_token_delete(dht_op_token* token);
// config
struct OPENDHT_C_PUBLIC dht_node_config {
struct OPENDHT_C_PUBLIC dht_node_config
{
dht_infohash node_id;
uint32_t network;
bool is_bootstrap;
@@ -143,13 +138,15 @@ struct OPENDHT_C_PUBLIC dht_node_config {
};
typedef struct dht_node_config dht_node_config;
struct OPENDHT_C_PUBLIC dht_secure_config {
struct OPENDHT_C_PUBLIC dht_secure_config
{
dht_node_config node_config;
dht_identity id;
};
typedef struct dht_secure_config dht_secure_config;
struct OPENDHT_C_PUBLIC dht_runner_config {
struct OPENDHT_C_PUBLIC dht_runner_config
{
dht_secure_config dht_config;
bool threaded;
const char* proxy_server;
@@ -176,14 +173,33 @@ OPENDHT_C_PUBLIC void dht_runner_delete(dht_runner* runner);
OPENDHT_C_PUBLIC int dht_runner_run(dht_runner* runner, in_port_t port);
/* Returns 0 on success, standard error code on failure */
OPENDHT_C_PUBLIC int dht_runner_run_config(dht_runner* runner, in_port_t port, const dht_runner_config* config);
OPENDHT_C_PUBLIC void dht_runner_ping(dht_runner* runner, struct sockaddr* addr, socklen_t addr_len, dht_done_cb done_cb, void* cb_user_data);
OPENDHT_C_PUBLIC void dht_runner_ping(
dht_runner* runner, struct sockaddr* addr, socklen_t addr_len, dht_done_cb done_cb, void* cb_user_data);
OPENDHT_C_PUBLIC void dht_runner_bootstrap(dht_runner* runner, const char* host, const char* service);
OPENDHT_C_PUBLIC void dht_runner_get(dht_runner* runner, const dht_infohash* hash, dht_get_cb cb, dht_done_cb done_cb, void* cb_user_data);
OPENDHT_C_PUBLIC dht_op_token* dht_runner_listen(dht_runner* runner, const dht_infohash* hash, dht_value_cb cb, dht_shutdown_cb done_cb, void* cb_user_data);
OPENDHT_C_PUBLIC void dht_runner_get(
dht_runner* runner, const dht_infohash* hash, dht_get_cb cb, dht_done_cb done_cb, void* cb_user_data);
OPENDHT_C_PUBLIC dht_op_token* dht_runner_listen(
dht_runner* runner, const dht_infohash* hash, dht_value_cb cb, dht_shutdown_cb done_cb, void* cb_user_data);
OPENDHT_C_PUBLIC void dht_runner_cancel_listen(dht_runner* runner, const dht_infohash* hash, dht_op_token* token);
OPENDHT_C_PUBLIC void dht_runner_put(dht_runner* runner, const dht_infohash* hash, const dht_value* value, dht_done_cb done_cb, void* cb_user_data, bool permanent);
OPENDHT_C_PUBLIC void dht_runner_put_signed(dht_runner* runner, const dht_infohash* hash, const dht_value* value, dht_done_cb done_cb, void* cb_user_data, bool permanent);
OPENDHT_C_PUBLIC void dht_runner_put_encrypted(dht_runner* runner, const dht_infohash* hash, const dht_infohash* to, const dht_value* value, dht_done_cb done_cb, void* cb_user_data, bool permanent);
OPENDHT_C_PUBLIC void dht_runner_put(dht_runner* runner,
const dht_infohash* hash,
const dht_value* value,
dht_done_cb done_cb,
void* cb_user_data,
bool permanent);
OPENDHT_C_PUBLIC void dht_runner_put_signed(dht_runner* runner,
const dht_infohash* hash,
const dht_value* value,
dht_done_cb done_cb,
void* cb_user_data,
bool permanent);
OPENDHT_C_PUBLIC void dht_runner_put_encrypted(dht_runner* runner,
const dht_infohash* hash,
const dht_infohash* to,
const dht_value* value,
dht_done_cb done_cb,
void* cb_user_data,
bool permanent);
OPENDHT_C_PUBLIC void dht_runner_cancel_put(dht_runner* runner, const dht_infohash* hash, dht_value_id value_id);
OPENDHT_C_PUBLIC void dht_runner_shutdown(dht_runner* runner, dht_shutdown_cb done_cb, void* cb_user_data);
OPENDHT_C_PUBLIC dht_infohash dht_runner_get_node_id(const dht_runner* runner);
+2 -17
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "opendht/dhtrunner.h"
+23 -38
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "infohash.h"
@@ -43,17 +28,15 @@ enum class NodeStatus {
};
inline constexpr const char*
statusToStr(NodeStatus status) {
return status == NodeStatus::Connected ? "connected" : (
status == NodeStatus::Connecting ? "connecting" :
"disconnected");
statusToStr(NodeStatus status)
{
return status == NodeStatus::Connected ? "connected"
: (status == NodeStatus::Connecting ? "connecting" : "disconnected");
}
struct OPENDHT_PUBLIC NodeStats {
unsigned good_nodes {0},
dubious_nodes {0},
cached_nodes {0},
incoming_nodes {0};
struct OPENDHT_PUBLIC NodeStats
{
unsigned good_nodes {0}, dubious_nodes {0}, cached_nodes {0}, incoming_nodes {0};
unsigned table_depth {0};
unsigned searches {0};
unsigned node_cache_size {0};
@@ -73,7 +56,8 @@ struct OPENDHT_PUBLIC NodeStats {
MSGPACK_DEFINE_MAP(good_nodes, dubious_nodes, cached_nodes, incoming_nodes, table_depth, searches, node_cache_size)
};
struct OPENDHT_PUBLIC NodeInfo {
struct OPENDHT_PUBLIC NodeInfo
{
InfoHash id;
InfoHash node_id;
NodeStats ipv4 {};
@@ -99,7 +83,8 @@ struct OPENDHT_PUBLIC NodeInfo {
/**
* Dht configuration.
*/
struct OPENDHT_PUBLIC Config {
struct OPENDHT_PUBLIC Config
{
/** DHT node ID */
InfoHash node_id {};
@@ -160,7 +145,7 @@ struct OPENDHT_PUBLIC SecureDhtConfig
bool cert_cache_all {false};
};
enum class OPENDHT_PUBLIC PushNotificationResult: uint8_t {
enum class OPENDHT_PUBLIC PushNotificationResult : uint8_t {
/* success codes */
PutRefresh,
ListenRefresh,
@@ -186,19 +171,19 @@ using ShutdownCallback = std::function<void()>;
using IdentityAnnouncedCb = std::function<void(bool)>;
using PublicAddressChangedCb = std::function<void(std::vector<SockAddr>)>;
using CertificateStoreQueryLegacy = std::function<std::vector<std::shared_ptr<crypto::Certificate>>(const InfoHash& pk_id)>;
using CertificateStoreQueryLegacy
= std::function<std::vector<std::shared_ptr<crypto::Certificate>>(const InfoHash& pk_id)>;
using CertificateStoreQuery = std::function<std::vector<std::shared_ptr<crypto::Certificate>>(const PkId& pk_id)>;
using DoneCallback = std::function<void(bool success, const std::vector<std::shared_ptr<Node>>& nodes)>;
using DoneCallbackSimple = std::function<void(bool success)>;
typedef bool (*GetCallbackRaw)(std::shared_ptr<Value>, void *user_data);
typedef bool (*ValueCallbackRaw)(std::shared_ptr<Value>, bool expired, void *user_data);
typedef void (*DoneCallbackRaw)(bool, std::vector<std::shared_ptr<Node>>*, void *user_data);
typedef void (*ShutdownCallbackRaw)(void *user_data);
typedef void (*DoneCallbackSimpleRaw)(bool, void *user_data);
typedef bool (*FilterRaw)(const Value&, void *user_data);
typedef bool (*GetCallbackRaw)(std::shared_ptr<Value>, void* user_data);
typedef bool (*ValueCallbackRaw)(std::shared_ptr<Value>, bool expired, void* user_data);
typedef void (*DoneCallbackRaw)(bool, std::vector<std::shared_ptr<Node>>*, void* user_data);
typedef void (*ShutdownCallbackRaw)(void* user_data);
typedef void (*DoneCallbackSimpleRaw)(bool, void* user_data);
typedef bool (*FilterRaw)(const Value&, void* user_data);
OPENDHT_PUBLIC GetCallbackSimple bindGetCb(GetCallbackRaw raw_cb, void* user_data);
OPENDHT_PUBLIC GetCallback bindGetCb(GetCallbackSimple cb);
@@ -209,4 +194,4 @@ OPENDHT_PUBLIC DoneCallback bindDoneCb(DoneCallbackRaw raw_cb, void* user_data);
OPENDHT_PUBLIC DoneCallbackSimple bindDoneCbSimple(DoneCallbackSimpleRaw raw_cb, void* user_data);
OPENDHT_PUBLIC Value::Filter bindFilterRaw(FilterRaw raw_filter, void* user_data);
}
} // namespace dht
+2 -17
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "infohash.h"
+48 -41
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include <vector>
@@ -22,71 +8,91 @@
namespace dht {
namespace crypto {
template <class T>
template<class T>
class secure_vector
{
public:
secure_vector() {}
secure_vector(secure_vector<T> const&) = default;
secure_vector(secure_vector<T> &&) = default;
explicit secure_vector(unsigned size): data_(size) {}
explicit secure_vector(unsigned size, T _item): data_(size, _item) {}
explicit secure_vector(const std::vector<T>& c): data_(c) {}
secure_vector(std::vector<T>&& c): data_(std::move(c)) {}
secure_vector(secure_vector<T>&&) = default;
explicit secure_vector(unsigned size)
: data_(size)
{}
explicit secure_vector(unsigned size, T _item)
: data_(size, _item)
{}
explicit secure_vector(const std::vector<T>& c)
: data_(c)
{}
secure_vector(std::vector<T>&& c)
: data_(std::move(c))
{}
~secure_vector() { clean(); }
static secure_vector<T> getRandom(size_t size) {
static secure_vector<T> getRandom(size_t size)
{
secure_vector<T> ret(size);
std::random_device rdev;
#ifdef _WIN32
std::uniform_int_distribution<int> rand_byte{ 0, std::numeric_limits<uint8_t>::max() };
std::uniform_int_distribution<int> rand_byte {0, std::numeric_limits<uint8_t>::max()};
#else
std::uniform_int_distribution<uint8_t> rand_byte;
#endif
std::generate_n((uint8_t*)ret.data_.data(), ret.size()*sizeof(T), std::bind(rand_byte, std::ref(rdev)));
std::generate_n((uint8_t*) ret.data_.data(), ret.size() * sizeof(T), std::bind(rand_byte, std::ref(rdev)));
return ret;
}
secure_vector<T>& operator=(const secure_vector<T>& c) {
secure_vector<T>& operator=(const secure_vector<T>& c)
{
if (&c == this)
return *this;
clean();
data_ = c.data_;
return *this;
}
secure_vector<T>& operator=(secure_vector<T>&& c) {
secure_vector<T>& operator=(secure_vector<T>&& c)
{
if (&c == this)
return *this;
clean();
data_ = std::move(c.data_);
return *this;
}
secure_vector<T>& operator=(std::vector<T>&& c) {
secure_vector<T>& operator=(std::vector<T>&& c)
{
clean();
data_ = std::move(c);
return *this;
}
std::vector<T>& writable() { clean(); return data_; }
std::vector<T>& writable()
{
clean();
return data_;
}
const std::vector<T>& makeInsecure() const { return data_; }
const uint8_t* data() const { return data_.data(); }
void clean() {
clean(data_.begin(), data_.end());
}
void clean() { clean(data_.begin(), data_.end()); }
void clear() { clean(); data_.clear(); }
void clear()
{
clean();
data_.clear();
}
size_t size() const { return data_.size(); }
bool empty() const { return data_.empty(); }
void swap(secure_vector<T>& other) { data_.swap(other.data_); }
void resize(size_t s) {
if (s == data_.size()) return;
void resize(size_t s)
{
if (s == data_.size())
return;
if (s < data_.size()) {
//shrink
clean(data_.begin()+s, data_.end());
// shrink
clean(data_.begin() + s, data_.end());
data_.resize(s);
} else {
//grow
// grow
auto data = std::move(data_); // move protected data
clear();
data_.resize(s);
@@ -99,7 +105,8 @@ private:
/**
* Securely wipe memory
*/
static void clean(const typename std::vector<T>::iterator& i, const typename std::vector<T>::iterator& j) {
static void clean(const typename std::vector<T>::iterator& i, const typename std::vector<T>::iterator& j)
{
volatile uint8_t* b = reinterpret_cast<uint8_t*>(&*i);
volatile uint8_t* e = reinterpret_cast<uint8_t*>(&*j);
std::fill(b, e, 0);
@@ -110,5 +117,5 @@ private:
using SecureBlob = secure_vector<uint8_t>;
}
}
} // namespace crypto
} // namespace dht
+80 -84
View File
@@ -1,32 +1,12 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "value.h"
#include "sockaddr.h"
namespace dht {
enum class ImStatus : uint8_t {
NONE = 0,
TYPING,
RECEIVED,
READ
};
enum class ImStatus : uint8_t { NONE = 0, TYPING, RECEIVED, READ };
}
MSGPACK_ADD_ENUM(dht::ImStatus)
@@ -37,11 +17,12 @@ class OPENDHT_PUBLIC DhtMessage : public Value::Serializable<DhtMessage>
public:
static const ValueType TYPE;
DhtMessage(const std::string& s = {}, const Blob& msg = {}) : service(s), data(msg) {}
DhtMessage(const std::string& s = {}, const Blob& msg = {})
: service(s)
, data(msg)
{}
std::string getService() const {
return service;
}
std::string getService() const { return service; }
static Value::Filter getFilter() { return {}; }
@@ -50,21 +31,22 @@ public:
static Value::Filter ServiceFilter(const std::string& s);
/** print value for debugging */
friend std::ostream& operator<< (std::ostream&, const DhtMessage&);
friend std::ostream& operator<<(std::ostream&, const DhtMessage&);
std::string service;
Blob data;
MSGPACK_DEFINE(service, data)
};
template <typename T>
template<typename T>
class SignedValue : public Value::Serializable<T>
{
private:
using BaseClass = Value::Serializable<T>;
public:
virtual void unpackValue(const Value& v) override {
virtual void unpackValue(const Value& v) override
{
if (v.owner) {
owner = v.owner;
from = v.owner->getId();
@@ -74,22 +56,26 @@ public:
BaseClass::unpackValue(v);
}
static Value::Filter getFilter() {
return [](const Value& v){ return v.isSigned(); };
static Value::Filter getFilter()
{
return [](const Value& v) {
return v.isSigned();
};
}
Sp<crypto::PublicKey> owner;
dht::InfoHash from;
};
template <typename T>
template<typename T>
class EncryptedValue : public SignedValue<T>
{
public:
using BaseClass = SignedValue<T>;
public:
virtual void unpackValue(const Value& v) override {
virtual void unpackValue(const Value& v) override
{
if (!v.recipient) {
throw std::runtime_error("Value has no recipient");
}
@@ -97,11 +83,10 @@ public:
BaseClass::unpackValue(v);
}
static Value::Filter getFilter() {
return Value::Filter::chain(
BaseClass::getFilter(),
[](const Value& v) { return static_cast<bool>(v.recipient); }
);
static Value::Filter getFilter()
{
return Value::Filter::chain(BaseClass::getFilter(),
[](const Value& v) { return static_cast<bool>(v.recipient); });
}
dht::InfoHash to;
@@ -117,20 +102,31 @@ public:
ImMessage() {}
ImMessage(dht::Value::Id id, std::string&& m, long d = 0)
: id(id), msg(std::move(m)), date(d) {}
ImMessage(dht::Value::Id id, std::string &&dt, std::string &&m, long d = 0)
: id(id), msg(std::move(m)), datatype(std::move(dt)), date(d) {}
ImMessage(dht::Value::Id id, std::string &&dt, std::string &&m, std::map<std::string, std::string> &&md, long d = 0)
: id(id), msg(std::move(m)), datatype(std::move(dt)), metadatas(std::move(md)), date(d) {}
: id(id)
, msg(std::move(m))
, date(d)
{}
ImMessage(dht::Value::Id id, std::string&& dt, std::string&& m, long d = 0)
: id(id)
, msg(std::move(m))
, datatype(std::move(dt))
, date(d)
{}
ImMessage(dht::Value::Id id, std::string&& dt, std::string&& m, std::map<std::string, std::string>&& md, long d = 0)
: id(id)
, msg(std::move(m))
, datatype(std::move(dt))
, metadatas(std::move(md))
, date(d)
{}
virtual void unpackValue(const Value& v) override {
virtual void unpackValue(const Value& v) override
{
to = v.recipient;
SignedValue::unpackValue(v);
}
static Value::Filter getFilter() {
return SignedValue::getFilter();
}
static Value::Filter getFilter() { return SignedValue::getFilter(); }
dht::InfoHash to;
dht::Value::Id id {0};
@@ -152,12 +148,17 @@ public:
static const ValueType TYPE;
TrustRequest() {}
TrustRequest(std::string s, std::string ci = {}) : service(s), conversationId(ci) {}
TrustRequest(std::string s, std::string ci, const Blob& d) : service(s), conversationId(ci), payload(d) {}
TrustRequest(std::string s, std::string ci = {})
: service(s)
, conversationId(ci)
{}
TrustRequest(std::string s, std::string ci, const Blob& d)
: service(s)
, conversationId(ci)
, payload(d)
{}
static Value::Filter getFilter() {
return EncryptedValue::getFilter();
}
static Value::Filter getFilter() { return EncryptedValue::getFilter(); }
std::string service;
std::string conversationId;
@@ -175,20 +176,21 @@ public:
static const ValueType TYPE;
IceCandidates() {}
IceCandidates(Value::Id msg_id, Blob ice) : id(msg_id), ice_data(ice) {}
IceCandidates(Value::Id msg_id, Blob ice)
: id(msg_id)
, ice_data(ice)
{}
static Value::Filter getFilter() {
return EncryptedValue::getFilter();
}
static Value::Filter getFilter() { return EncryptedValue::getFilter(); }
template <typename Packer>
template<typename Packer>
void msgpack_pack(Packer& pk) const
{
pk.pack_array(2);
pk.pack(id);
#if 1
pk.pack_bin(ice_data.size());
pk.pack_bin_body((const char*)ice_data.data(), ice_data.size());
pk.pack_bin_body((const char*) ice_data.data(), ice_data.size());
#else
// hack for backward compatibility with old opendht compiled with msgpack 1.0
// remove when enough people have moved to new versions
@@ -200,8 +202,10 @@ public:
virtual void msgpack_unpack(const msgpack::object& o)
{
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
if (o.via.array.size < 2) throw msgpack::type_error();
if (o.type != msgpack::type::ARRAY)
throw msgpack::type_error();
if (o.via.array.size < 2)
throw msgpack::type_error();
id = o.via.array.ptr[0].as<Value::Id>();
ice_data = unpackBlob(o.via.array.ptr[1]);
}
@@ -220,59 +224,51 @@ private:
public:
static const ValueType TYPE;
IpServiceAnnouncement(sa_family_t family = AF_UNSPEC, in_port_t p = 0) {
IpServiceAnnouncement(sa_family_t family = AF_UNSPEC, in_port_t p = 0)
{
addr.setFamily(family);
addr.setPort(p);
}
IpServiceAnnouncement(const SockAddr& sa) : addr(sa) {}
IpServiceAnnouncement(const SockAddr& sa)
: addr(sa)
{}
IpServiceAnnouncement(const Blob& b) {
msgpack_unpack(unpackMsg(b).get());
}
IpServiceAnnouncement(const Blob& b) { msgpack_unpack(unpackMsg(b).get()); }
template <typename Packer>
template<typename Packer>
void msgpack_pack(Packer& pk) const
{
pk.pack_bin(addr.getLength());
pk.pack_bin_body((const char*)addr.get(), addr.getLength());
pk.pack_bin_body((const char*) addr.get(), addr.getLength());
}
virtual void msgpack_unpack(const msgpack::object& o)
{
if (o.type == msgpack::type::BIN)
addr = {(sockaddr*)o.via.bin.ptr, (socklen_t)o.via.bin.size};
addr = {(sockaddr*) o.via.bin.ptr, (socklen_t) o.via.bin.size};
else
throw msgpack::type_error();
}
in_port_t getPort() const {
return addr.getPort();
}
void setPort(in_port_t p) {
addr.setPort(p);
}
in_port_t getPort() const { return addr.getPort(); }
void setPort(in_port_t p) { addr.setPort(p); }
const SockAddr& getPeerAddr() const {
return addr;
}
const SockAddr& getPeerAddr() const { return addr; }
virtual const ValueType& getType() const {
return TYPE;
}
virtual const ValueType& getType() const { return TYPE; }
static bool storePolicy(InfoHash, std::shared_ptr<Value>&, const InfoHash&, const SockAddr&);
/** print value for debugging */
friend std::ostream& operator<< (std::ostream&, const IpServiceAnnouncement&);
friend std::ostream& operator<<(std::ostream&, const IpServiceAnnouncement&);
private:
SockAddr addr;
};
OPENDHT_PUBLIC extern const std::array<std::reference_wrapper<const ValueType>, 5> DEFAULT_TYPES;
OPENDHT_PUBLIC extern const std::array<std::reference_wrapper<const ValueType>, 1> DEFAULT_INSECURE_TYPES;
}
} // namespace dht
+128 -132
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "infohash.h"
@@ -56,13 +41,17 @@ struct LocalListener;
* Must be given open UDP sockets and ::periodic must be
* called regularly.
*/
class OPENDHT_PUBLIC Dht final : public DhtInterface {
class OPENDHT_PUBLIC Dht final : public DhtInterface
{
public:
/**
* Initialise the Dht with two open sockets (for IPv4 and IP6)
* and an ID for the node.
*/
Dht(std::unique_ptr<net::DatagramSocket>&& sock, const Config& config, const Sp<Logger>& l = {}, std::unique_ptr<std::mt19937_64>&& rd = {});
Dht(std::unique_ptr<net::DatagramSocket>&& sock,
const Config& config,
const Sp<Logger>& l = {},
std::unique_ptr<std::mt19937_64>&& rd = {});
virtual ~Dht();
@@ -70,22 +59,16 @@ public:
* Get the ID of the node.
*/
inline const InfoHash& getNodeId() const override { return myid; }
void setOnPublicAddressChanged(PublicAddressChangedCb cb) override {
publicAddressChangedCb_ = std::move(cb);
}
void setOnPublicAddressChanged(PublicAddressChangedCb cb) override { publicAddressChangedCb_ = std::move(cb); }
NodeStatus updateStatus(sa_family_t af) override;
/**
* Get the current status of the node for the given family.
*/
NodeStatus getStatus(sa_family_t af) const override {
return dht(af).status;
}
NodeStatus getStatus(sa_family_t af) const override { return dht(af).status; }
NodeStatus getStatus() const override {
return std::max(getStatus(AF_INET), getStatus(AF_INET6));
}
NodeStatus getStatus() const override { return std::max(getStatus(AF_INET), getStatus(AF_INET6)); }
net::DatagramSocket* getSocket() const override { return network_engine.getSocket(); };
@@ -102,21 +85,16 @@ public:
*/
bool isRunning(sa_family_t af = 0) const override;
virtual void registerType(const ValueType& type) override {
types.registerType(type);
}
const ValueType& getType(ValueType::Id type_id) const override {
return types.getType(type_id);
}
virtual void registerType(const ValueType& type) override { types.registerType(type); }
const ValueType& getType(ValueType::Id type_id) const override { return types.getType(type_id); }
void addBootstrap(const std::string& host, const std::string& service) override {
void addBootstrap(const std::string& host, const std::string& service) override
{
bootstrap_nodes.emplace_back(host, service);
startBootstrap();
}
void clearBootstrap() override {
bootstrap_nodes.clear();
}
void clearBootstrap() override { bootstrap_nodes.clear(); }
/**
* Insert a node in the main routing table.
@@ -124,14 +102,14 @@ public:
* used to bootstrap efficiently from previously known nodes.
*/
void insertNode(const InfoHash& id, const SockAddr&) override;
void insertNode(const NodeExport& n) override {
insertNode(n.id, n.addr);
}
void insertNode(const NodeExport& n) override { insertNode(n.id, n.addr); }
void pingNode(SockAddr, DoneCallbackSimple&& cb={}) override;
void pingNode(SockAddr, DoneCallbackSimple&& cb = {}) override;
time_point periodic(const uint8_t *buf, size_t buflen, SockAddr, const time_point& now) override;
time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now) override {
time_point periodic(const uint8_t* buf, size_t buflen, SockAddr, const time_point& now) override;
time_point periodic(
const uint8_t* buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now) override
{
return periodic(buf, buflen, SockAddr(from, fromlen), now);
}
@@ -145,14 +123,30 @@ public:
cb and donecb won't be called again afterward.
* @param f a filter function used to prefilter values.
*/
virtual void get(const InfoHash& key, GetCallback cb, DoneCallback donecb={}, Value::Filter&& f={}, Where&& w = {}) override;
virtual void get(const InfoHash& key, GetCallback cb, DoneCallbackSimple donecb={}, Value::Filter&& f={}, Where&& w = {}) override {
virtual void get(
const InfoHash& key, GetCallback cb, DoneCallback donecb = {}, Value::Filter&& f = {}, Where&& w = {}) override;
virtual void get(const InfoHash& key,
GetCallback cb,
DoneCallbackSimple donecb = {},
Value::Filter&& f = {},
Where&& w = {}) override
{
get(key, cb, bindDoneCb(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
}
virtual void get(const InfoHash& key, GetCallbackSimple cb, DoneCallback donecb={}, Value::Filter&& f={}, Where&& w = {}) override {
virtual void get(const InfoHash& key,
GetCallbackSimple cb,
DoneCallback donecb = {},
Value::Filter&& f = {},
Where&& w = {}) override
{
get(key, bindGetCb(cb), donecb, std::forward<Value::Filter>(f), std::forward<Where>(w));
}
virtual void get(const InfoHash& key, GetCallbackSimple cb, DoneCallbackSimple donecb, Value::Filter&& f={}, Where&& w = {}) override {
virtual void get(const InfoHash& key,
GetCallbackSimple cb,
DoneCallbackSimple donecb,
Value::Filter&& f = {},
Where&& w = {}) override
{
get(key, bindGetCb(cb), bindDoneCb(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
}
/**
@@ -166,7 +160,8 @@ public:
* response.
*/
virtual void query(const InfoHash& key, QueryCallback cb, DoneCallback done_cb = {}, Query&& q = {}) override;
virtual void query(const InfoHash& key, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query&& q = {}) override {
virtual void query(const InfoHash& key, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query&& q = {}) override
{
query(key, cb, bindDoneCb(done_cb), std::forward<Query>(q));
}
@@ -187,32 +182,32 @@ public:
* The done callback will be called once, when the first announce succeeds, or fails.
*/
void put(const InfoHash& key,
Sp<Value>,
DoneCallback cb=nullptr,
time_point created=time_point::max(),
bool permanent = false) override;
Sp<Value>,
DoneCallback cb = nullptr,
time_point created = time_point::max(),
bool permanent = false) override;
void put(const InfoHash& key,
const Sp<Value>& v,
DoneCallbackSimple cb,
time_point created=time_point::max(),
bool permanent = false) override
const Sp<Value>& v,
DoneCallbackSimple cb,
time_point created = time_point::max(),
bool permanent = false) override
{
put(key, v, bindDoneCb(cb), created, permanent);
}
void put(const InfoHash& key,
Value&& v,
DoneCallback cb=nullptr,
time_point created=time_point::max(),
bool permanent = false) override
Value&& v,
DoneCallback cb = nullptr,
time_point created = time_point::max(),
bool permanent = false) override
{
put(key, std::make_shared<Value>(std::move(v)), cb, created, permanent);
}
void put(const InfoHash& key,
Value&& v,
DoneCallbackSimple cb,
time_point created=time_point::max(),
bool permanent = false) override
Value&& v,
DoneCallbackSimple cb,
time_point created = time_point::max(),
bool permanent = false) override
{
put(key, std::forward<Value>(v), bindDoneCb(cb), created, permanent);
}
@@ -240,16 +235,22 @@ public:
*
* @return a token to cancel the listener later.
*/
size_t listen(const InfoHash&, ValueCallback, Value::Filter={}, Where={}) override;
size_t listen(const InfoHash&, ValueCallback, Value::Filter = {}, Where = {}) override;
size_t listen(const InfoHash& key, GetCallback cb, Value::Filter f={}, Where w={}) override {
return listen(key, [cb](const std::vector<Sp<Value>>& vals, bool expired){
if (not expired)
return cb(vals);
return true;
}, std::forward<Value::Filter>(f), std::forward<Where>(w));
size_t listen(const InfoHash& key, GetCallback cb, Value::Filter f = {}, Where w = {}) override
{
return listen(
key,
[cb](const std::vector<Sp<Value>>& vals, bool expired) {
if (not expired)
return cb(vals);
return true;
},
std::forward<Value::Filter>(f),
std::forward<Where>(w));
}
size_t listen(const InfoHash& key, GetCallbackSimple cb, Value::Filter f={}, Where w={}) override {
size_t listen(const InfoHash& key, GetCallbackSimple cb, Value::Filter f = {}, Where w = {}) override
{
return listen(key, bindGetCb(cb), std::forward<Value::Filter>(f), std::forward<Where>(w));
}
@@ -261,7 +262,8 @@ public:
* The DHT will recontact neighbor nodes, re-register for listen ops etc.
*/
void connectivityChanged(sa_family_t) override;
void connectivityChanged() override {
void connectivityChanged() override
{
connectivityChanged(AF_INET);
connectivityChanged(AF_INET6);
}
@@ -288,37 +290,32 @@ public:
std::string getSearchLog(const InfoHash&, sa_family_t af = AF_UNSPEC) const override;
void dumpTables() const override;
std::vector<unsigned> getNodeMessageStats(bool in = false) override {
std::vector<unsigned> getNodeMessageStats(bool in = false) override
{
return network_engine.getNodeMessageStats(in);
}
/**
* Set the in-memory storage limit in bytes
*/
void setStorageLimit(size_t limit = DEFAULT_STORAGE_LIMIT) override {
max_store_size = limit;
}
size_t getStorageLimit() const override {
return max_store_size;
}
void setStorageLimit(size_t limit = DEFAULT_STORAGE_LIMIT) override { max_store_size = limit; }
size_t getStorageLimit() const override { return max_store_size; }
/**
* Returns the total memory usage of stored values and the number
* of stored values.
*/
std::pair<size_t, size_t> getStoreSize() const override {
return {total_store_size, total_values};
}
std::pair<size_t, size_t> getStoreSize() const override { return {total_store_size, total_values}; }
std::vector<SockAddr> getPublicAddress(sa_family_t family = 0) override;
PushNotificationResult pushNotificationReceived(const std::map<std::string, std::string>&) override {
PushNotificationResult pushNotificationReceived(const std::map<std::string, std::string>&) override
{
return PushNotificationResult::IgnoredDisabled;
}
void resubscribe(unsigned) {}
private:
/* When performing a search, we search for up to SEARCH_NODES closest nodes
to the destination, and use the additional ones to backtrack if any of
the target 8 turn out to be dead. */
@@ -384,7 +381,8 @@ private:
using SearchMap = std::map<InfoHash, Sp<Search>>;
using ReportedAddr = std::pair<unsigned, SockAddr>;
struct Kad {
struct Kad
{
RoutingTable buckets {};
SearchMap searches {};
unsigned pending_pings {0};
@@ -399,7 +397,7 @@ private:
Kad dht6 {};
PublicAddressChangedCb publicAddressChangedCb_ {};
std::vector<std::pair<std::string,std::string>> bootstrap_nodes {};
std::vector<std::pair<std::string, std::string>> bootstrap_nodes {};
std::chrono::steady_clock::duration bootstrap_period {BOOTSTRAP_PERIOD};
Sp<Scheduler::Job> bootstrapJob {};
@@ -418,7 +416,6 @@ private:
std::map<size_t, std::tuple<size_t, size_t, size_t>> listeners {};
size_t listener_token {1};
// timing
Scheduler scheduler;
Sp<Scheduler::Job> nextNodesConfirmation {};
@@ -436,7 +433,8 @@ private:
const bool maintain_storage {false};
const bool public_stable {false};
inline const duration& getListenExpiration() const {
inline const duration& getListenExpiration() const
{
return public_stable ? LISTEN_EXPIRE_TIME_PUBLIC : LISTEN_EXPIRE_TIME;
}
@@ -449,7 +447,11 @@ private:
// Storage
void storageAddListener(const InfoHash& id, const Sp<Node>& node, size_t tid, Query&& = {}, int version = 0);
bool storageStore(const InfoHash& id, const Sp<Value>& value, time_point created, const SockAddr& sa = {}, bool permanent = false);
bool storageStore(const InfoHash& id,
const Sp<Value>& value,
time_point created,
const SockAddr& sa = {},
bool permanent = false);
bool storageRefresh(const InfoHash& id, Value::Id vid);
void expireStore();
void expireStorage(InfoHash h);
@@ -465,19 +467,21 @@ private:
* nodes.
*/
void dataPersistence(InfoHash id);
size_t maintainStorage(decltype(store)::value_type&, bool force=false, const DoneCallback& donecb={});
size_t maintainStorage(decltype(store)::value_type&, bool force = false, const DoneCallback& donecb = {});
// Buckets
Kad& dht(sa_family_t af) { return af == AF_INET ? dht4 : dht6; }
const Kad& dht(sa_family_t af) const { return af == AF_INET ? dht4 : dht6; }
RoutingTable& buckets(sa_family_t af) { return dht(af).buckets; }
const RoutingTable& buckets(sa_family_t af) const { return dht(af).buckets; }
Bucket* findBucket(const InfoHash& id, sa_family_t af) {
Bucket* findBucket(const InfoHash& id, sa_family_t af)
{
auto& b = buckets(af);
auto it = b.findBucket(id);
return it == b.end() ? nullptr : &(*it);
}
const Bucket* findBucket(const InfoHash& id, sa_family_t af) const {
const Bucket* findBucket(const InfoHash& id, sa_family_t af) const
{
return const_cast<Dht*>(this)->findBucket(id, af);
}
@@ -502,9 +506,20 @@ private:
* Low-level method that will perform a search on the DHT for the specified
* infohash (id), using the specified IP version (IPv4 or IPv6).
*/
Sp<Search> search(const InfoHash& id, sa_family_t af, GetCallback = {}, QueryCallback = {}, DoneCallback = {}, Value::Filter = {}, const Sp<Query>& q = {});
Sp<Search> search(const InfoHash& id,
sa_family_t af,
GetCallback = {},
QueryCallback = {},
DoneCallback = {},
Value::Filter = {},
const Sp<Query>& q = {});
void announce(const InfoHash& id, sa_family_t af, Sp<Value> value, DoneCallback callback, time_point created=time_point::max(), bool permanent = false);
void announce(const InfoHash& id,
sa_family_t af,
Sp<Value> value,
DoneCallback callback,
time_point created = time_point::max(),
bool permanent = false);
size_t listenTo(const InfoHash& id, sa_family_t af, ValueCallback cb, Value::Filter f = {}, const Sp<Query>& q = {});
/**
@@ -532,9 +547,9 @@ private:
* @param query The query sent to the node.
*/
void searchNodeGetDone(const net::Request& status,
net::RequestAnswer&& answer,
std::weak_ptr<Search> ws,
Sp<Query> query);
net::RequestAnswer&& answer,
std::weak_ptr<Search> ws,
Sp<Query> query);
/**
* Generic function to execute when a 'get' request expires.
@@ -559,7 +574,7 @@ private:
/**
* If update is true, this method will also send message to synced but non-updated search nodes.
*/
SearchNode* searchSendGetValues(Sp<Search> sr, SearchNode *n = nullptr, bool update = true);
SearchNode* searchSendGetValues(Sp<Search> sr, SearchNode* n = nullptr, bool update = true);
/**
* Forwards an 'announce' request for a list of nodes to the network engine.
@@ -591,41 +606,22 @@ private:
net::RequestAnswer onPing(Sp<Node> node);
/* when we receive a "find node" request */
net::RequestAnswer onFindNode(Sp<Node> node, const InfoHash& hash, want_t want);
void onFindNodeDone(const Sp<Node>& status,
net::RequestAnswer& a,
Sp<Search> sr);
void onFindNodeDone(const Sp<Node>& status, net::RequestAnswer& a, Sp<Search> sr);
/* when we receive a "get values" request */
net::RequestAnswer onGetValues(Sp<Node> node,
const InfoHash& hash,
want_t want,
const Query& q);
void onGetValuesDone(const Sp<Node>& status,
net::RequestAnswer& a,
Sp<Search>& sr,
const Sp<Query>& orig_query);
net::RequestAnswer onGetValues(Sp<Node> node, const InfoHash& hash, want_t want, const Query& q);
void onGetValuesDone(const Sp<Node>& status, net::RequestAnswer& a, Sp<Search>& sr, const Sp<Query>& orig_query);
/* when we receive a listen request */
net::RequestAnswer onListen(Sp<Node> node,
const InfoHash& hash,
const Blob& token,
size_t socket_id,
const Query& query,
int version = 0);
void onListenDone(const Sp<Node>& status,
net::RequestAnswer& a,
Sp<Search>& sr);
net::RequestAnswer onListen(
Sp<Node> node, const InfoHash& hash, const Blob& token, size_t socket_id, const Query& query, int version = 0);
void onListenDone(const Sp<Node>& status, net::RequestAnswer& a, Sp<Search>& sr);
/* when we receive an announce request */
net::RequestAnswer onAnnounce(Sp<Node> node,
const InfoHash& hash,
const Blob& token,
const std::vector<Sp<Value>>& v,
const time_point& created);
net::RequestAnswer onRefresh(Sp<Node> node,
const InfoHash& hash,
const Blob& token,
const Value::Id& vid);
void onAnnounceDone(const Sp<Node>& status,
net::RequestAnswer& a,
Sp<Search>& sr);
const InfoHash& hash,
const Blob& token,
const std::vector<Sp<Value>>& v,
const time_point& created);
net::RequestAnswer onRefresh(Sp<Node> node, const InfoHash& hash, const Blob& token, const Value::Id& vid);
void onAnnounceDone(const Sp<Node>& status, net::RequestAnswer& a, Sp<Search>& sr);
};
}
} // namespace dht
+56 -55
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "infohash.h"
@@ -26,14 +11,19 @@
namespace dht {
namespace net {
class DatagramSocket;
class DatagramSocket;
}
class OPENDHT_PUBLIC DhtInterface {
class OPENDHT_PUBLIC DhtInterface
{
public:
DhtInterface() = default;
DhtInterface(const Logger& l) : logger_(std::make_shared<Logger>(l)) {}
DhtInterface(const std::shared_ptr<Logger>& l) : logger_(l) {}
DhtInterface(const Logger& l)
: logger_(std::make_shared<Logger>(l))
{}
DhtInterface(const std::shared_ptr<Logger>& l)
: logger_(l)
{}
virtual ~DhtInterface() = default;
// [[deprecated]]
@@ -48,9 +38,7 @@ public:
virtual NodeStatus getStatus(sa_family_t af) const = 0;
virtual NodeStatus getStatus() const = 0;
void addOnConnectedCallback(std::function<void()> cb) {
onConnectCallbacks_.emplace(std::move(cb));
}
void addOnConnectedCallback(std::function<void()> cb) { onConnectCallbacks_.emplace(std::move(cb)); }
virtual void setOnPublicAddressChanged(PublicAddressChangedCb) {}
virtual net::DatagramSocket* getSocket() const { return {}; }
@@ -90,10 +78,12 @@ public:
virtual void insertNode(const InfoHash& id, const SockAddr&) = 0;
virtual void insertNode(const NodeExport& n) = 0;
virtual void pingNode(SockAddr, DoneCallbackSimple&& cb={}) = 0;
virtual void pingNode(SockAddr, DoneCallbackSimple&& cb = {}) = 0;
virtual time_point periodic(const uint8_t *buf, size_t buflen, SockAddr, const time_point& now) = 0;
virtual time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now) = 0;
virtual time_point periodic(const uint8_t* buf, size_t buflen, SockAddr, const time_point& now) = 0;
virtual time_point periodic(
const uint8_t* buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now)
= 0;
/**
* Get a value by searching on all available protocols (IPv4, IPv6),
@@ -105,10 +95,18 @@ public:
cb and donecb won't be called again afterward.
* @param f a filter function used to prefilter values.
*/
virtual void get(const InfoHash& key, GetCallback cb, DoneCallback donecb={}, Value::Filter&& f={}, Where&& w = {}) = 0;
virtual void get(const InfoHash& key, GetCallback cb, DoneCallbackSimple donecb={}, Value::Filter&& f={}, Where&& w = {}) = 0;
virtual void get(const InfoHash& key, GetCallbackSimple cb, DoneCallback donecb={}, Value::Filter&& f={}, Where&& w = {}) = 0;
virtual void get(const InfoHash& key, GetCallbackSimple cb, DoneCallbackSimple donecb, Value::Filter&& f={}, Where&& w = {}) = 0;
virtual void get(
const InfoHash& key, GetCallback cb, DoneCallback donecb = {}, Value::Filter&& f = {}, Where&& w = {})
= 0;
virtual void get(
const InfoHash& key, GetCallback cb, DoneCallbackSimple donecb = {}, Value::Filter&& f = {}, Where&& w = {})
= 0;
virtual void get(
const InfoHash& key, GetCallbackSimple cb, DoneCallback donecb = {}, Value::Filter&& f = {}, Where&& w = {})
= 0;
virtual void get(
const InfoHash& key, GetCallbackSimple cb, DoneCallbackSimple donecb, Value::Filter&& f = {}, Where&& w = {})
= 0;
/**
* Similar to Dht::get, but sends a Query to filter data remotely.
@@ -140,25 +138,29 @@ public:
* The done callback will be called once, when the first announce succeeds, or fails.
*/
virtual void put(const InfoHash& key,
Sp<Value>,
DoneCallback cb=nullptr,
time_point created=time_point::max(),
bool permanent = false) = 0;
Sp<Value>,
DoneCallback cb = nullptr,
time_point created = time_point::max(),
bool permanent = false)
= 0;
virtual void put(const InfoHash& key,
const Sp<Value>& v,
DoneCallbackSimple cb,
time_point created=time_point::max(),
bool permanent = false) = 0;
const Sp<Value>& v,
DoneCallbackSimple cb,
time_point created = time_point::max(),
bool permanent = false)
= 0;
virtual void put(const InfoHash& key,
Value&& v,
DoneCallback cb=nullptr,
time_point created=time_point::max(),
bool permanent = false) = 0;
Value&& v,
DoneCallback cb = nullptr,
time_point created = time_point::max(),
bool permanent = false)
= 0;
virtual void put(const InfoHash& key,
Value&& v,
DoneCallbackSimple cb,
time_point created=time_point::max(),
bool permanent = false) = 0;
Value&& v,
DoneCallbackSimple cb,
time_point created = time_point::max(),
bool permanent = false)
= 0;
/**
* Get data currently being put at the given hash.
@@ -183,9 +185,9 @@ public:
*
* @return a token to cancel the listener later.
*/
virtual size_t listen(const InfoHash&, GetCallback, Value::Filter={}, Where w = {}) = 0;
virtual size_t listen(const InfoHash& key, GetCallbackSimple cb, Value::Filter f={}, Where w = {}) = 0;
virtual size_t listen(const InfoHash&, ValueCallback, Value::Filter={}, Where w = {}) = 0;
virtual size_t listen(const InfoHash&, GetCallback, Value::Filter = {}, Where w = {}) = 0;
virtual size_t listen(const InfoHash& key, GetCallbackSimple cb, Value::Filter f = {}, Where w = {}) = 0;
virtual size_t listen(const InfoHash&, ValueCallback, Value::Filter = {}, Where w = {}) = 0;
virtual bool cancelListen(const InfoHash&, size_t token) = 0;
@@ -232,16 +234,15 @@ public:
virtual std::vector<SockAddr> getPublicAddress(sa_family_t family = 0) = 0;
virtual void setLogger(const Logger& l) {
virtual void setLogger(const Logger& l)
{
if (logger_)
*logger_ = l;
else
logger_= std::make_shared<Logger>(l);
logger_ = std::make_shared<Logger>(l);
}
virtual void setLogger(const std::shared_ptr<Logger>& l) {
logger_ = l;
}
virtual void setLogger(const std::shared_ptr<Logger>& l) { logger_ = l; }
/**
* Only print logs related to the given InfoHash (if given), or disable filter (if zeroes).
+111 -102
View File
@@ -1,22 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
* Adrien Béraud <adrien.beraud@savoirfairelinux.com>
* Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include <functional>
@@ -42,33 +25,32 @@ class Value;
namespace http {
class Resolver;
class Request;
}
} // namespace http
namespace dht {
class OPENDHT_PUBLIC DhtProxyClient final : public DhtInterface {
class OPENDHT_PUBLIC DhtProxyClient final : public DhtInterface
{
public:
DhtProxyClient();
explicit DhtProxyClient(
std::shared_ptr<crypto::Certificate> serverCA,
crypto::Identity clientIdentity,
std::function<void()> loopSignal,
const std::string& serverHost,
const std::string& userAgent = "",
const std::string& pushClientId = "",
const std::string& pushToken = "",
const std::string& pushTopic = "",
const std::string& pushPlatform = "",
std::shared_ptr<Logger> logger = {}
);
explicit DhtProxyClient(std::shared_ptr<crypto::Certificate> serverCA,
crypto::Identity clientIdentity,
std::function<void()> loopSignal,
const std::string& serverHost,
const std::string& userAgent = "",
const std::string& pushClientId = "",
const std::string& pushToken = "",
const std::string& pushTopic = "",
const std::string& pushPlatform = "",
std::shared_ptr<Logger> logger = {});
void setHeaderFields(http::Request& request);
virtual void setPushNotificationToken(const std::string& token) override;
virtual void setPushNotificationTopic(const std::string& topic) override {
virtual void setPushNotificationTopic(const std::string& topic) override
{
#ifdef OPENDHT_PUSH_NOTIFICATIONS
notificationTopic_ = topic;
#else
@@ -76,7 +58,8 @@ public:
#endif
}
virtual void setPushNotificationPlatform(const std::string& platform) override {
virtual void setPushNotificationPlatform(const std::string& platform) override
{
#ifdef OPENDHT_PUSH_NOTIFICATIONS
platform_ = platform;
#else
@@ -90,17 +73,13 @@ public:
* Get the ID of the node.
*/
inline const InfoHash& getNodeId() const override { return myid; }
void setOnPublicAddressChanged(PublicAddressChangedCb cb) override {
publicAddressChangedCb_ = std::move(cb);
}
void setOnPublicAddressChanged(PublicAddressChangedCb cb) override { publicAddressChangedCb_ = std::move(cb); }
/**
* Get the current status of the node for the given family.
*/
NodeStatus getStatus(sa_family_t af) const override;
NodeStatus getStatus() const override {
return std::max(getStatus(AF_INET), getStatus(AF_INET6));
}
NodeStatus getStatus() const override { return std::max(getStatus(AF_INET), getStatus(AF_INET6)); }
/**
* Performs final operations before quitting.
@@ -125,14 +104,30 @@ public:
cb and donecb won't be called again afterward.
* @param f a filter function used to prefilter values.
*/
virtual void get(const InfoHash& key, GetCallback cb, DoneCallback donecb={}, Value::Filter&& f={}, Where&& w = {}) override;
virtual void get(const InfoHash& key, GetCallback cb, DoneCallbackSimple donecb={}, Value::Filter&& f={}, Where&& w = {}) override {
virtual void get(
const InfoHash& key, GetCallback cb, DoneCallback donecb = {}, Value::Filter&& f = {}, Where&& w = {}) override;
virtual void get(const InfoHash& key,
GetCallback cb,
DoneCallbackSimple donecb = {},
Value::Filter&& f = {},
Where&& w = {}) override
{
get(key, cb, bindDoneCb(std::move(donecb)), std::forward<Value::Filter>(f), std::forward<Where>(w));
}
virtual void get(const InfoHash& key, GetCallbackSimple cb, DoneCallback donecb={}, Value::Filter&& f={}, Where&& w = {}) override {
virtual void get(const InfoHash& key,
GetCallbackSimple cb,
DoneCallback donecb = {},
Value::Filter&& f = {},
Where&& w = {}) override
{
get(key, bindGetCb(cb), std::move(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
}
virtual void get(const InfoHash& key, GetCallbackSimple cb, DoneCallbackSimple donecb, Value::Filter&& f={}, Where&& w = {}) override {
virtual void get(const InfoHash& key,
GetCallbackSimple cb,
DoneCallbackSimple donecb,
Value::Filter&& f = {},
Where&& w = {}) override
{
get(key, bindGetCb(cb), bindDoneCb(std::move(donecb)), std::forward<Value::Filter>(f), std::forward<Where>(w));
}
@@ -144,32 +139,32 @@ public:
* NOTE: For now, created parameter is ignored.
*/
void put(const InfoHash& key,
Sp<Value>,
DoneCallback cb=nullptr,
time_point created=time_point::max(),
bool permanent = false) override;
Sp<Value>,
DoneCallback cb = nullptr,
time_point created = time_point::max(),
bool permanent = false) override;
void put(const InfoHash& key,
const Sp<Value>& v,
DoneCallbackSimple cb,
time_point created=time_point::max(),
bool permanent = false) override
const Sp<Value>& v,
DoneCallbackSimple cb,
time_point created = time_point::max(),
bool permanent = false) override
{
put(key, v, bindDoneCb(std::move(cb)), created, permanent);
}
void put(const InfoHash& key,
Value&& v,
DoneCallback cb=nullptr,
time_point created=time_point::max(),
bool permanent = false) override
Value&& v,
DoneCallback cb = nullptr,
time_point created = time_point::max(),
bool permanent = false) override
{
put(key, std::make_shared<Value>(std::move(v)), std::move(cb), created, permanent);
}
void put(const InfoHash& key,
Value&& v,
DoneCallbackSimple cb,
time_point created=time_point::max(),
bool permanent = false) override
Value&& v,
DoneCallbackSimple cb,
time_point created = time_point::max(),
bool permanent = false) override
{
put(key, std::forward<Value>(v), bindDoneCb(std::move(cb)), created, permanent);
}
@@ -193,16 +188,22 @@ public:
*
* @return a token to cancel the listener later.
*/
virtual size_t listen(const InfoHash&, ValueCallback, Value::Filter={}, Where={}) override;
virtual size_t listen(const InfoHash&, ValueCallback, Value::Filter = {}, Where = {}) override;
virtual size_t listen(const InfoHash& key, GetCallback cb, Value::Filter f={}, Where w={}) override {
return listen(key, [cb=std::move(cb)](const std::vector<Sp<Value>>& vals, bool expired){
if (not expired)
return cb(vals);
return true;
}, std::forward<Value::Filter>(f), std::forward<Where>(w));
virtual size_t listen(const InfoHash& key, GetCallback cb, Value::Filter f = {}, Where w = {}) override
{
return listen(
key,
[cb = std::move(cb)](const std::vector<Sp<Value>>& vals, bool expired) {
if (not expired)
return cb(vals);
return true;
},
std::forward<Value::Filter>(f),
std::forward<Where>(w));
}
virtual size_t listen(const InfoHash& key, GetCallbackSimple cb, Value::Filter f={}, Where w={}) override {
virtual size_t listen(const InfoHash& key, GetCallbackSimple cb, Value::Filter f = {}, Where w = {}) override
{
return listen(key, bindGetCb(std::move(cb)), std::forward<Value::Filter>(f), std::forward<Where>(w));
}
/*
@@ -218,7 +219,9 @@ public:
PushNotificationResult pushNotificationReceived(const std::map<std::string, std::string>& notification) override;
time_point periodic(const uint8_t*, size_t, SockAddr, const time_point& now) override;
time_point periodic(const uint8_t* buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now) override {
time_point periodic(
const uint8_t* buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now) override
{
return periodic(buf, buflen, SockAddr(from, fromlen), now);
}
@@ -232,8 +235,13 @@ public:
* @param q a query used to filter values on the remotes before they send a
* response.
*/
virtual void query(const InfoHash& /*key*/, QueryCallback /*cb*/, DoneCallback /*done_cb*/ = {}, Query&& /*q*/ = {}) override { }
virtual void query(const InfoHash& key, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query&& q = {}) override {
virtual void query(const InfoHash& /*key*/,
QueryCallback /*cb*/,
DoneCallback /*done_cb*/ = {},
Query&& /*q*/ = {}) override
{}
virtual void query(const InfoHash& key, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query&& q = {}) override
{
query(key, cb, bindDoneCb(std::move(done_cb)), std::forward<Query>(q));
}
@@ -253,14 +261,10 @@ public:
*/
bool cancelPut(const InfoHash&, const Value::Id&) override;
void pingNode(SockAddr, DoneCallbackSimple&& /*cb*/={}) override { }
void pingNode(SockAddr, DoneCallbackSimple&& /*cb*/ = {}) override {}
virtual void registerType(const ValueType& type) override {
types.registerType(type);
}
const ValueType& getType(ValueType::Id type_id) const override {
return types.getType(type_id);
}
virtual void registerType(const ValueType& type) override { types.registerType(type); }
const ValueType& getType(ValueType::Id type_id) const override { return types.getType(type_id); }
std::vector<Sp<Value>> getLocal(const InfoHash& k, const Value::Filter& filter) const override;
Sp<Value> getLocalById(const InfoHash& k, Value::Id id) const override;
@@ -269,8 +273,8 @@ public:
* NOTE: The following methods will not be implemented because the
* DhtProxyClient doesn't have any storage nor synchronization process
*/
void insertNode(const InfoHash&, const SockAddr&) override { }
void insertNode(const NodeExport&) override { }
void insertNode(const InfoHash&, const SockAddr&) override {}
void insertNode(const NodeExport&) override {}
std::pair<size_t, size_t> getStoreSize() const override { return {}; }
std::vector<NodeExport> exportNodes() const override { return {}; }
std::vector<ValuesExport> exportValues() const override { return {}; }
@@ -284,17 +288,14 @@ public:
std::vector<unsigned> getNodeMessageStats(bool) override { return {}; }
void setStorageLimit(size_t) override {}
virtual size_t getStorageLimit() const override { return 0; }
void connectivityChanged(sa_family_t) override {
getProxyInfos();
}
void connectivityChanged() override {
void connectivityChanged(sa_family_t) override { getProxyInfos(); }
void connectivityChanged() override
{
getProxyInfos();
loopSignal_();
}
void listenKeepIdle(uint32_t seconds) {
listenKeepIdle_ = seconds;
}
void listenKeepIdle(uint32_t seconds) { listenKeepIdle_ = seconds; }
inline uint32_t listenKeepIdle() { return listenKeepIdle_; }
private:
@@ -310,13 +311,15 @@ private:
*/
struct InfoState;
void getProxyInfos();
void queryProxyInfo(const std::shared_ptr<InfoState>& infoState, const std::shared_ptr<http::Resolver>& resolver, sa_family_t family);
void queryProxyInfo(const std::shared_ptr<InfoState>& infoState,
const std::shared_ptr<http::Resolver>& resolver,
sa_family_t family);
void onProxyInfos(const Json::Value& val, const sa_family_t family);
SockAddr parsePublicAddress(const Json::Value& val);
void opFailed();
void handleExpireListener(const asio::error_code &ec, const InfoHash& key);
void handleExpireListener(const asio::error_code& ec, const InfoHash& key);
struct Listener;
struct OperationState;
@@ -325,15 +328,21 @@ private:
SUBSCRIBE,
RESUBSCRIBE,
};
using CacheValueCallback = std::function<bool(const std::vector<std::shared_ptr<Value>>& values, bool expired, system_clock::time_point)>;
using CacheValueCallback
= std::function<bool(const std::vector<std::shared_ptr<Value>>& values, bool expired, system_clock::time_point)>;
/**
* Send Listen with httpClient_
*/
void sendListen(const restinio::http_request_header_t& header, const CacheValueCallback& cb,
const Sp<OperationState>& opstate, Listener& listener, ListenMethod method = ListenMethod::LISTEN);
void handleResubscribe(const asio::error_code& ec, const InfoHash& key,
const size_t token, std::shared_ptr<OperationState> opstate);
void sendListen(const restinio::http_request_header_t& header,
const CacheValueCallback& cb,
const Sp<OperationState>& opstate,
Listener& listener,
ListenMethod method = ListenMethod::LISTEN);
void handleResubscribe(const asio::error_code& ec,
const InfoHash& key,
const size_t token,
std::shared_ptr<OperationState> opstate);
void doPut(const InfoHash&, Sp<Value>, DoneCallbackSimple, time_point created, bool permanent);
void handleRefreshPut(const asio::error_code& ec, InfoHash key, Value::Id id);
@@ -413,14 +422,14 @@ private:
/**
* Retrieve if we can connect to the proxy (update statusIpvX_)
*/
void handleProxyConfirm(const asio::error_code &ec);
void handleProxyConfirm(const asio::error_code& ec);
std::unique_ptr<asio::steady_timer> nextProxyConfirmationTimer_;
std::unique_ptr<asio::steady_timer> listenerRestartTimer_;
/**
* Relaunch LISTEN requests if the client disconnect/reconnect.
*/
void restartListeners(const asio::error_code &ec);
void restartListeners(const asio::error_code& ec);
/**
* Refresh a listen via a token
@@ -444,12 +453,12 @@ private:
*/
std::string platform_
#ifdef __ANDROID__
{"android"};
{"android"};
#else
#ifdef __APPLE__
{"ios"};
{"ios"};
#else
{};
{};
#endif
#endif
@@ -466,4 +475,4 @@ private:
std::shared_ptr<http::Request> buildRequest(const std::string& target = {});
};
}
} // namespace dht
+101 -100
View File
@@ -1,22 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
* Adrien Béraud <adrien.beraud@savoirfairelinux.com>
* Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "callbacks.h"
@@ -36,12 +19,7 @@
#include <mutex>
namespace dht {
enum class PushType {
None = 0,
Android,
iOS,
UnifiedPush
};
enum class PushType { None = 0, Android, iOS, UnifiedPush };
}
MSGPACK_ADD_ENUM(dht::PushType)
@@ -54,14 +32,15 @@ namespace dht {
namespace http {
class Request;
struct ListenerSession;
}
} // namespace http
class DhtRunner;
using RestRouter = restinio::router::express_router_t<>;
using RequestStatus = restinio::request_handling_status_t;
struct OPENDHT_PUBLIC ProxyServerConfig {
struct OPENDHT_PUBLIC ProxyServerConfig
{
std::string address {};
in_port_t port {8000};
std::string pushServer {};
@@ -85,8 +64,8 @@ public:
* it will fails silently
*/
DhtProxyServer(const std::shared_ptr<DhtRunner>& dht,
const ProxyServerConfig& config = {},
const std::shared_ptr<log::Logger>& logger = {});
const ProxyServerConfig& config = {},
const std::shared_ptr<log::Logger>& logger = {});
virtual ~DhtProxyServer();
@@ -100,30 +79,35 @@ public:
using clock = std::chrono::steady_clock;
using time_point = clock::time_point;
struct PushStats {
struct PushStats
{
uint64_t highPriorityCount {0};
uint64_t normalPriorityCount {0};
void increment(bool highPriority) {
void increment(bool highPriority)
{
if (highPriority)
highPriorityCount++;
else
normalPriorityCount++;
}
Json::Value toJson() const {
Json::Value toJson() const
{
Json::Value val;
val["highPriorityCount"] = static_cast<Json::UInt64>(highPriorityCount);
val["normalPriorityCount"] = static_cast<Json::UInt64>(normalPriorityCount);
return val;
}
std::string toString() const {
std::string toString() const
{
return fmt::format("{} high priority, {} normal priority", highPriorityCount, normalPriorityCount);
}
};
struct OPENDHT_PUBLIC ServerStats {
struct OPENDHT_PUBLIC ServerStats
{
/** Current number of listen operations */
size_t listenCount {0};
/** Current number of permanent put operations (hash used) */
@@ -138,7 +122,7 @@ public:
/** Last time at which the stats were updated */
time_point lastUpdated;
/** Total number of push notification requests that the server attempted to
* send since being started, broken down by type and priority level */
* send since being started, broken down by type and priority level */
PushStats androidPush;
PushStats iosPush;
PushStats unifiedPush;
@@ -167,13 +151,12 @@ private:
struct RestRouterTraitsTls;
struct RestRouterTraits;
template <typename HttpResponse>
template<typename HttpResponse>
static HttpResponse initHttpResponse(HttpResponse response);
static restinio::request_handling_status_t serverError(restinio::request_t& request);
template< typename ServerSettings >
void addServerSettings(ServerSettings& serverSettings,
const unsigned int max_pipelined_requests = 16);
template<typename ServerSettings>
void addServerSettings(ServerSettings& serverSettings, const unsigned int max_pipelined_requests = 16);
std::unique_ptr<RestRouter> createRestRouter();
@@ -186,8 +169,7 @@ private:
* On error: HTTP 503, body: {"err":"xxxx"}
* @param session
*/
RequestStatus getNodeInfo(restinio::request_handle_t request,
restinio::router::route_params_t params) const;
RequestStatus getNodeInfo(restinio::request_handle_t request, restinio::router::route_params_t params) const;
/**
* Return ServerStats in JSON format
@@ -195,8 +177,7 @@ private:
* Result: HTTP 200, body: Node infos in JSON format
* @param session
*/
RequestStatus getStats(restinio::request_handle_t request,
restinio::router::route_params_t params);
RequestStatus getStats(restinio::request_handle_t request, restinio::router::route_params_t params);
/**
* Return Values of an infoHash
@@ -208,8 +189,7 @@ private:
* On error: HTTP 503, body: {"err":"xxxx"}
* @param session
*/
RequestStatus get(restinio::request_handle_t request,
restinio::router::route_params_t params);
RequestStatus get(restinio::request_handle_t request, restinio::router::route_params_t params);
/**
* Listen incoming Values of an infoHash.
@@ -221,8 +201,7 @@ private:
* On error: HTTP 503, body: {"err":"xxxx"}
* @param session
*/
RequestStatus listen(restinio::request_handle_t request,
restinio::router::route_params_t params);
RequestStatus listen(restinio::request_handle_t request, restinio::router::route_params_t params);
/**
* Put a value on the DHT
@@ -233,10 +212,9 @@ private:
* HTTP 400, body: {"err":"xxxx"} if bad json or HTTP 502 if put fails
* @param session
*/
RequestStatus put(restinio::request_handle_t request,
restinio::router::route_params_t params);
RequestStatus put(restinio::request_handle_t request, restinio::router::route_params_t params);
void handleCancelPermamentPut(const asio::error_code &ec, const InfoHash& key, Value::Id vid);
void handleCancelPermamentPut(const asio::error_code& ec, const InfoHash& key, Value::Id vid);
#ifdef OPENDHT_PROXY_SERVER_IDENTITY
/**
@@ -248,8 +226,7 @@ private:
* HTTP 400, body: {"err":"xxxx"} if bad json
* @param session
*/
RequestStatus putSigned(restinio::request_handle_t request,
restinio::router::route_params_t params) const;
RequestStatus putSigned(restinio::request_handle_t request, restinio::router::route_params_t params) const;
/**
* Put a value to encrypt by the proxy on the DHT
@@ -260,8 +237,7 @@ private:
* HTTP 400, body: {"err":"xxxx"} if bad json
* @param session
*/
RequestStatus putEncrypted(restinio::request_handle_t request,
restinio::router::route_params_t params);
RequestStatus putEncrypted(restinio::request_handle_t request, restinio::router::route_params_t params);
#endif // OPENDHT_PROXY_SERVER_IDENTITY
@@ -275,8 +251,7 @@ private:
* On error: HTTP 503, body: {"err":"xxxx"}
* @param session
*/
RequestStatus getFiltered(restinio::request_handle_t request,
restinio::router::route_params_t params);
RequestStatus getFiltered(restinio::request_handle_t request, restinio::router::route_params_t params);
/**
* Respond allowed Methods
@@ -285,21 +260,22 @@ private:
* See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
* @param session
*/
RequestStatus options(restinio::request_handle_t request,
restinio::router::route_params_t params);
RequestStatus options(restinio::request_handle_t request, restinio::router::route_params_t params);
struct PushSessionContext {
struct PushSessionContext
{
std::mutex lock;
std::string sessionId;
PushSessionContext(const std::string& id) : sessionId(id) {}
PushSessionContext(const std::string& id)
: sessionId(id)
{}
};
#ifdef OPENDHT_PUSH_NOTIFICATIONS
PushType getTypeFromString(const std::string& type);
std::string getDefaultTopic(PushType type);
RequestStatus pingPush(restinio::request_handle_t request,
restinio::router::route_params_t /*params*/);
RequestStatus pingPush(restinio::request_handle_t request, restinio::router::route_params_t /*params*/);
/**
* Subscribe to push notifications for an iOS or Android device.
* Method: SUBSCRIBE "/{InfoHash: .*}"
@@ -309,8 +285,7 @@ private:
* so you need to refresh the operation each six hours.
* @param session
*/
RequestStatus subscribe(restinio::request_handle_t request,
restinio::router::route_params_t params);
RequestStatus subscribe(restinio::request_handle_t request, restinio::router::route_params_t params);
/**
* Unsubscribe to push notifications for an iOS or Android device.
@@ -319,15 +294,15 @@ private:
* Return: nothing
* @param session
*/
RequestStatus unsubscribe(restinio::request_handle_t request,
restinio::router::route_params_t params);
RequestStatus unsubscribe(restinio::request_handle_t request, restinio::router::route_params_t params);
/**
* Send a push notification via a gorush push gateway
* @param key of the device
* @param json, the content to send
*/
void sendPushNotification(const std::string& key, Json::Value&& json, PushType type, bool highPriority, const std::string& topic);
void sendPushNotification(
const std::string& key, Json::Value&& json, PushType type, bool highPriority, const std::string& topic);
/**
* Send push notification with an expire timeout.
@@ -337,8 +312,11 @@ private:
* @param type
* @param topic
*/
void handleNotifyPushListenExpire(const asio::error_code &ec, const std::string pushToken,
std::function<Json::Value()> json, PushType type, const std::string& topic);
void handleNotifyPushListenExpire(const asio::error_code& ec,
const std::string pushToken,
std::function<Json::Value()> json,
PushType type,
const std::string& topic);
/**
* Remove a push listener between a client and a hash
@@ -347,8 +325,10 @@ private:
* @param key
* @param clientId
*/
void handleCancelPushListen(const asio::error_code &ec, const std::string pushToken,
const InfoHash key, const std::string clientId);
void handleCancelPushListen(const asio::error_code& ec,
const std::string pushToken,
const InfoHash key,
const std::string clientId);
/**
* Handles a push listen request.
@@ -363,20 +343,24 @@ private:
* @param expired A boolean indicating whether the push listen request has expired.
* @return true.
*/
bool handlePushListen(const InfoHash& infoHash, const std::string& pushToken,
PushType type, const std::string& clientId,
const std::shared_ptr<DhtProxyServer::PushSessionContext>& sessionCtx, const std::string& topic,
const std::vector<std::shared_ptr<Value>>& values, bool expired);
bool handlePushListen(const InfoHash& infoHash,
const std::string& pushToken,
PushType type,
const std::string& clientId,
const std::shared_ptr<DhtProxyServer::PushSessionContext>& sessionCtx,
const std::string& topic,
const std::vector<std::shared_ptr<Value>>& values,
bool expired);
#endif //OPENDHT_PUSH_NOTIFICATIONS
#endif // OPENDHT_PUSH_NOTIFICATIONS
void handlePrintStats(const asio::error_code &ec);
void handlePrintStats(const asio::error_code& ec);
void updateStats();
template <typename Os>
template<typename Os>
void saveState(Os& stream);
template <typename Is>
template<typename Is>
void loadState(Is& is, size_t size);
std::shared_ptr<asio::io_context> ioContext_;
@@ -415,7 +399,8 @@ private:
std::map<restinio::connection_id_t, http::ListenerSession> listeners_;
// Connection Listener observing conn state changes.
std::shared_ptr<ConnectionListener> connListener_;
struct PermanentPut {
struct PermanentPut
{
time_point expiration;
std::string pushToken;
std::string clientId;
@@ -426,31 +411,40 @@ private:
PushType type;
std::string topic;
template <typename Packer>
template<typename Packer>
void msgpack_pack(Packer& p) const
{
p.pack_map(2 + (sessionCtx ? 1 : 0) + (clientId.empty() ? 0 : 1) + (type == PushType::None ? 0 : 2) + (topic.empty() ? 0 : 1));
p.pack("value"); p.pack(value);
p.pack("exp"); p.pack(to_time_t(expiration));
p.pack_map(2 + (sessionCtx ? 1 : 0) + (clientId.empty() ? 0 : 1) + (type == PushType::None ? 0 : 2)
+ (topic.empty() ? 0 : 1));
p.pack("value");
p.pack(value);
p.pack("exp");
p.pack(to_time_t(expiration));
if (not clientId.empty()) {
p.pack("cid"); p.pack(clientId);
p.pack("cid");
p.pack(clientId);
}
if (sessionCtx) {
std::lock_guard<std::mutex> l(sessionCtx->lock);
p.pack("sid"); p.pack(sessionCtx->sessionId);
p.pack("sid");
p.pack(sessionCtx->sessionId);
}
if (type != PushType::None) {
p.pack("t"); p.pack(type);
p.pack("token"); p.pack(pushToken);
p.pack("t");
p.pack(type);
p.pack("token");
p.pack(pushToken);
}
if (not topic.empty()) {
p.pack("top"); p.pack(topic);
p.pack("top");
p.pack(topic);
}
}
void msgpack_unpack(const msgpack::object& o);
};
struct SearchPuts {
struct SearchPuts
{
std::map<dht::Value::Id, PermanentPut> puts;
MSGPACK_DEFINE_ARRAY(puts)
};
@@ -464,7 +458,8 @@ private:
std::string bundleId_;
#ifdef OPENDHT_PUSH_NOTIFICATIONS
struct Listener {
struct Listener
{
time_point expiration;
std::string clientId;
std::shared_ptr<PushSessionContext> sessionCtx;
@@ -474,30 +469,36 @@ private:
PushType type;
std::string topic;
template <typename Packer>
template<typename Packer>
void msgpack_pack(Packer& p) const
{
p.pack_map(3 + (sessionCtx ? 1 : 0) + (topic.empty() ? 0 : 1));
p.pack("cid"); p.pack(clientId);
p.pack("exp"); p.pack(to_time_t(expiration));
p.pack("cid");
p.pack(clientId);
p.pack("exp");
p.pack(to_time_t(expiration));
if (sessionCtx) {
std::lock_guard<std::mutex> l(sessionCtx->lock);
p.pack("sid"); p.pack(sessionCtx->sessionId);
p.pack("sid");
p.pack(sessionCtx->sessionId);
}
p.pack("t"); p.pack(type);
p.pack("t");
p.pack(type);
if (!topic.empty()) {
p.pack("top"); p.pack(topic);
p.pack("top");
p.pack(topic);
}
}
void msgpack_unpack(const msgpack::object& o);
};
struct PushListener {
struct PushListener
{
std::map<InfoHash, std::vector<Listener>> listeners;
MSGPACK_DEFINE_ARRAY(listeners)
};
std::map<std::string, PushListener> pushListeners_;
#endif //OPENDHT_PUSH_NOTIFICATIONS
#endif // OPENDHT_PUSH_NOTIFICATIONS
};
}
} // namespace dht
+215 -154
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "def.h"
@@ -48,12 +33,13 @@ struct SecureDhtConfig;
* either wait for (expectedly frequent) calls to ::loop() or start an internal
* thread that will update the DHT when appropriate.
*/
class OPENDHT_PUBLIC DhtRunner {
class OPENDHT_PUBLIC DhtRunner
{
public:
using StatusCallback = std::function<void(NodeStatus, NodeStatus)>;
struct Config {
struct Config
{
SecureDhtConfig dht_config {};
bool threaded {true};
std::string proxy_server {};
@@ -69,7 +55,8 @@ public:
SockAddr bind4 {}, bind6 {};
};
struct Context {
struct Context
{
std::shared_ptr<Logger> logger {};
std::unique_ptr<net::DatagramSocket> sock;
std::shared_ptr<PeerDiscovery> peerDiscovery {};
@@ -85,196 +72,270 @@ public:
DhtRunner();
virtual ~DhtRunner();
void get(InfoHash id, GetCallbackSimple cb, DoneCallback donecb={}, Value::Filter f = {}, Where w = {}) {
void get(InfoHash id, GetCallbackSimple cb, DoneCallback donecb = {}, Value::Filter f = {}, Where w = {})
{
get(id, bindGetCb(cb), donecb, f, w);
}
void get(InfoHash id, GetCallbackSimple cb, DoneCallbackSimple donecb={}, Value::Filter f = {}, Where w = {}) {
void get(InfoHash id, GetCallbackSimple cb, DoneCallbackSimple donecb = {}, Value::Filter f = {}, Where w = {})
{
get(id, bindGetCb(cb), donecb, f, w);
}
void get(InfoHash hash, GetCallback vcb, DoneCallback dcb, Value::Filter f={}, Where w = {});
void get(InfoHash hash, GetCallback vcb, DoneCallback dcb, Value::Filter f = {}, Where w = {});
void get(InfoHash id, GetCallback cb, DoneCallbackSimple donecb={}, Value::Filter f = {}, Where w = {}) {
void get(InfoHash id, GetCallback cb, DoneCallbackSimple donecb = {}, Value::Filter f = {}, Where w = {})
{
get(id, cb, bindDoneCb(donecb), f, w);
}
void get(const std::string& key, GetCallback vcb, DoneCallbackSimple dcb={}, Value::Filter f = {}, Where w = {});
void get(const std::string& key, GetCallback vcb, DoneCallbackSimple dcb = {}, Value::Filter f = {}, Where w = {});
template <class T>
void get(InfoHash hash, std::function<bool(std::vector<T>&&)> cb, DoneCallbackSimple dcb={})
template<class T>
void get(InfoHash hash, std::function<bool(std::vector<T>&&)> cb, DoneCallbackSimple dcb = {})
{
get(hash, [cb=std::move(cb)](const std::vector<std::shared_ptr<Value>>& vals) {
return cb(unpackVector<T>(vals));
},
dcb,
getFilterSet<T>());
get(
hash,
[cb = std::move(cb)](const std::vector<std::shared_ptr<Value>>& vals) { return cb(unpackVector<T>(vals)); },
dcb,
getFilterSet<T>());
}
template <class T>
void get(InfoHash hash, std::function<bool(T&&)> cb, DoneCallbackSimple dcb={})
template<class T>
void get(InfoHash hash, std::function<bool(T&&)> cb, DoneCallbackSimple dcb = {})
{
get(hash, [cb=std::move(cb)](const std::vector<std::shared_ptr<Value>>& vals) {
for (const auto& v : vals) {
try {
if (not cb(Value::unpack<T>(*v)))
return false;
} catch (const std::exception&) {
continue;
get(
hash,
[cb = std::move(cb)](const std::vector<std::shared_ptr<Value>>& vals) {
for (const auto& v : vals) {
try {
if (not cb(Value::unpack<T>(*v)))
return false;
} catch (const std::exception&) {
continue;
}
}
}
return true;
},
dcb,
getFilterSet<T>());
return true;
},
dcb,
getFilterSet<T>());
}
std::future<std::vector<std::shared_ptr<dht::Value>>> get(InfoHash key, Value::Filter f = {}, Where w = {}) {
auto p = std::make_shared<std::promise<std::vector<std::shared_ptr< dht::Value >>>>();
auto values = std::make_shared<std::vector<std::shared_ptr< dht::Value >>>();
get(key, [=](const std::vector<std::shared_ptr<dht::Value>>& vlist) {
values->insert(values->end(), vlist.begin(), vlist.end());
return true;
}, [=](bool) {
p->set_value(std::move(*values));
},
f, w);
std::future<std::vector<std::shared_ptr<dht::Value>>> get(InfoHash key, Value::Filter f = {}, Where w = {})
{
auto p = std::make_shared<std::promise<std::vector<std::shared_ptr<dht::Value>>>>();
auto values = std::make_shared<std::vector<std::shared_ptr<dht::Value>>>();
get(
key,
[=](const std::vector<std::shared_ptr<dht::Value>>& vlist) {
values->insert(values->end(), vlist.begin(), vlist.end());
return true;
},
[=](bool) { p->set_value(std::move(*values)); },
f,
w);
return p->get_future();
}
template <class T>
std::future<std::vector<T>> get(InfoHash key) {
template<class T>
std::future<std::vector<T>> get(InfoHash key)
{
auto p = std::make_shared<std::promise<std::vector<T>>>();
auto values = std::make_shared<std::vector<T>>();
get<T>(key, [=](T&& v) {
values->emplace_back(std::move(v));
return true;
}, [=](bool) {
p->set_value(std::move(*values));
});
get<T>(
key,
[=](T&& v) {
values->emplace_back(std::move(v));
return true;
},
[=](bool) { p->set_value(std::move(*values)); });
return p->get_future();
}
void query(const InfoHash& hash, QueryCallback cb, DoneCallback done_cb = {}, Query q = {});
void query(const InfoHash& hash, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query q = {}) {
void query(const InfoHash& hash, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query q = {})
{
query(hash, cb, bindDoneCb(done_cb), q);
}
std::future<size_t> listen(InfoHash key, ValueCallback vcb, Value::Filter f = {}, Where w = {});
std::future<size_t> listen(InfoHash key, GetCallback cb, Value::Filter f={}, Where w={}) {
return listen(key, [cb=std::move(cb)](const std::vector<Sp<Value>>& vals, bool expired){
if (not expired)
return cb(vals);
return true;
}, std::forward<Value::Filter>(f), std::forward<Where>(w));
std::future<size_t> listen(InfoHash key, GetCallback cb, Value::Filter f = {}, Where w = {})
{
return listen(
key,
[cb = std::move(cb)](const std::vector<Sp<Value>>& vals, bool expired) {
if (not expired)
return cb(vals);
return true;
},
std::forward<Value::Filter>(f),
std::forward<Where>(w));
}
std::future<size_t> listen(const std::string& key, GetCallback vcb, Value::Filter f = {}, Where w = {});
std::future<size_t> listen(InfoHash key, GetCallbackSimple cb, Value::Filter f = {}, Where w = {}) {
std::future<size_t> listen(InfoHash key, GetCallbackSimple cb, Value::Filter f = {}, Where w = {})
{
return listen(key, bindGetCb(cb), f, w);
}
template <class T>
template<class T>
std::future<size_t> listen(InfoHash hash, std::function<bool(std::vector<T>&&)> cb)
{
return listen(hash, [cb=std::move(cb)](const std::vector<std::shared_ptr<Value>>& vals) {
return cb(unpackVector<T>(vals));
},
getFilterSet<T>());
return listen(
hash,
[cb = std::move(cb)](const std::vector<std::shared_ptr<Value>>& vals) { return cb(unpackVector<T>(vals)); },
getFilterSet<T>());
}
template <class T>
template<class T>
std::future<size_t> listen(InfoHash hash, std::function<bool(std::vector<T>&&, bool)> cb)
{
return listen(hash, [cb=std::move(cb)](const std::vector<std::shared_ptr<Value>>& vals, bool expired) {
return cb(unpackVector<T>(vals), expired);
},
getFilterSet<T>());
return listen(
hash,
[cb = std::move(cb)](const std::vector<std::shared_ptr<Value>>& vals, bool expired) {
return cb(unpackVector<T>(vals), expired);
},
getFilterSet<T>());
}
template <typename T>
template<typename T>
std::future<size_t> listen(InfoHash hash, std::function<bool(T&&)> cb, Value::Filter f = {}, Where w = {})
{
return listen(hash, [cb=std::move(cb)](const std::vector<std::shared_ptr<Value>>& vals) {
for (const auto& v : vals) {
try {
if (not cb(Value::unpack<T>(*v)))
return false;
} catch (const std::exception&) {
continue;
return listen(
hash,
[cb = std::move(cb)](const std::vector<std::shared_ptr<Value>>& vals) {
for (const auto& v : vals) {
try {
if (not cb(Value::unpack<T>(*v)))
return false;
} catch (const std::exception&) {
continue;
}
}
}
return true;
},
getFilterSet<T>(f), w);
return true;
},
getFilterSet<T>(f),
w);
}
template <typename T>
template<typename T>
std::future<size_t> listen(InfoHash hash, std::function<bool(T&&, bool)> cb, Value::Filter f = {}, Where w = {})
{
return listen(hash, [cb=std::move(cb)](const std::vector<std::shared_ptr<Value>>& vals, bool expired) {
for (const auto& v : vals) {
try {
if (not cb(Value::unpack<T>(*v), expired))
return false;
} catch (const std::exception&) {
continue;
return listen(
hash,
[cb = std::move(cb)](const std::vector<std::shared_ptr<Value>>& vals, bool expired) {
for (const auto& v : vals) {
try {
if (not cb(Value::unpack<T>(*v), expired))
return false;
} catch (const std::exception&) {
continue;
}
}
}
return true;
},
getFilterSet<T>(f), w);
return true;
},
getFilterSet<T>(f),
w);
}
void cancelListen(InfoHash h, size_t token);
void cancelListen(InfoHash h, std::shared_future<size_t> token);
void put(InfoHash hash, std::shared_ptr<Value> value, DoneCallback cb={}, time_point created=time_point::max(), bool permanent = false);
void put(InfoHash hash, std::shared_ptr<Value> value, DoneCallbackSimple cb, time_point created=time_point::max(), bool permanent = false) {
void put(InfoHash hash,
std::shared_ptr<Value> value,
DoneCallback cb = {},
time_point created = time_point::max(),
bool permanent = false);
void put(InfoHash hash,
std::shared_ptr<Value> value,
DoneCallbackSimple cb,
time_point created = time_point::max(),
bool permanent = false)
{
put(hash, value, bindDoneCb(cb), created, permanent);
}
void put(InfoHash hash, Value&& value, DoneCallback cb={}, time_point created=time_point::max(), bool permanent = false);
void put(InfoHash hash, Value&& value, DoneCallbackSimple cb, time_point created=time_point::max(), bool permanent = false) {
void put(InfoHash hash,
Value&& value,
DoneCallback cb = {},
time_point created = time_point::max(),
bool permanent = false);
void put(InfoHash hash,
Value&& value,
DoneCallbackSimple cb,
time_point created = time_point::max(),
bool permanent = false)
{
put(hash, std::forward<Value>(value), bindDoneCb(cb), created, permanent);
}
void put(const std::string& key, Value&& value, DoneCallbackSimple cb={}, time_point created=time_point::max(), bool permanent = false);
void put(const std::string& key,
Value&& value,
DoneCallbackSimple cb = {},
time_point created = time_point::max(),
bool permanent = false);
void cancelPut(const InfoHash& h, Value::Id id);
void cancelPut(const InfoHash& h, const std::shared_ptr<Value>& value);
void putSigned(InfoHash hash, std::shared_ptr<Value> value, DoneCallback cb={}, bool permanent = false);
void putSigned(InfoHash hash, std::shared_ptr<Value> value, DoneCallbackSimple cb, bool permanent = false) {
void putSigned(InfoHash hash, std::shared_ptr<Value> value, DoneCallback cb = {}, bool permanent = false);
void putSigned(InfoHash hash, std::shared_ptr<Value> value, DoneCallbackSimple cb, bool permanent = false)
{
putSigned(hash, value, bindDoneCb(cb), permanent);
}
void putSigned(InfoHash hash, Value&& value, DoneCallback cb={}, bool permanent = false);
void putSigned(InfoHash hash, Value&& value, DoneCallbackSimple cb, bool permanent = false) {
void putSigned(InfoHash hash, Value&& value, DoneCallback cb = {}, bool permanent = false);
void putSigned(InfoHash hash, Value&& value, DoneCallbackSimple cb, bool permanent = false)
{
putSigned(hash, std::forward<Value>(value), bindDoneCb(cb), permanent);
}
void putSigned(const std::string& key, Value&& value, DoneCallbackSimple cb={}, bool permanent = false);
void putSigned(const std::string& key, Value&& value, DoneCallbackSimple cb = {}, bool permanent = false);
void putEncrypted(InfoHash hash, InfoHash to, std::shared_ptr<Value> value, DoneCallback cb={}, bool permanent = false);
void putEncrypted(InfoHash hash, InfoHash to, std::shared_ptr<Value> value, DoneCallbackSimple cb, bool permanent = false) {
void putEncrypted(
InfoHash hash, InfoHash to, std::shared_ptr<Value> value, DoneCallback cb = {}, bool permanent = false);
void putEncrypted(
InfoHash hash, InfoHash to, std::shared_ptr<Value> value, DoneCallbackSimple cb, bool permanent = false)
{
putEncrypted(hash, to, value, bindDoneCb(cb), permanent);
}
void putEncrypted(InfoHash hash, InfoHash to, Value&& value, DoneCallback cb={}, bool permanent = false);
void putEncrypted(InfoHash hash, InfoHash to, Value&& value, DoneCallbackSimple cb, bool permanent = false) {
void putEncrypted(InfoHash hash, InfoHash to, Value&& value, DoneCallback cb = {}, bool permanent = false);
void putEncrypted(InfoHash hash, InfoHash to, Value&& value, DoneCallbackSimple cb, bool permanent = false)
{
putEncrypted(hash, to, std::forward<Value>(value), bindDoneCb(cb), permanent);
}
void putEncrypted(const std::string& key, InfoHash to, Value&& value, DoneCallback cb={}, bool permanent = false);
void putEncrypted(const std::string& key, InfoHash to, Value&& value, DoneCallback cb = {}, bool permanent = false);
void putEncrypted(InfoHash hash, const std::shared_ptr<crypto::PublicKey>& to, std::shared_ptr<Value> value, DoneCallback cb={}, bool permanent = false);
void putEncrypted(InfoHash hash, const std::shared_ptr<crypto::PublicKey>& to, std::shared_ptr<Value> value, DoneCallbackSimple cb, bool permanent = false) {
void putEncrypted(InfoHash hash,
const std::shared_ptr<crypto::PublicKey>& to,
std::shared_ptr<Value> value,
DoneCallback cb = {},
bool permanent = false);
void putEncrypted(InfoHash hash,
const std::shared_ptr<crypto::PublicKey>& to,
std::shared_ptr<Value> value,
DoneCallbackSimple cb,
bool permanent = false)
{
putEncrypted(hash, to, value, bindDoneCb(cb), permanent);
}
[[deprecated("Use the shared_ptr version instead")]]
void putEncrypted(InfoHash hash, const std::shared_ptr<crypto::PublicKey>& to, Value&& value, DoneCallback cb={}, bool permanent = false);
void putEncrypted(InfoHash hash,
const std::shared_ptr<crypto::PublicKey>& to,
Value&& value,
DoneCallback cb = {},
bool permanent = false);
[[deprecated("Use the shared_ptr version instead")]]
void putEncrypted(InfoHash hash, const std::shared_ptr<crypto::PublicKey>& to, Value&& value, DoneCallbackSimple cb, bool permanent = false) {
void putEncrypted(InfoHash hash,
const std::shared_ptr<crypto::PublicKey>& to,
Value&& value,
DoneCallbackSimple cb,
bool permanent = false)
{
putEncrypted(hash, to, std::forward<Value>(value), bindDoneCb(cb), permanent);
}
void putEncrypted(InfoHash hash, const PkId& to, std::shared_ptr<Value> value, DoneCallback cb={}, bool permanent = false);
void putEncrypted(InfoHash hash, const PkId& to, std::shared_ptr<Value> value, DoneCallbackSimple cb, bool permanent = false) {
void putEncrypted(
InfoHash hash, const PkId& to, std::shared_ptr<Value> value, DoneCallback cb = {}, bool permanent = false);
void putEncrypted(
InfoHash hash, const PkId& to, std::shared_ptr<Value> value, DoneCallbackSimple cb, bool permanent = false)
{
putEncrypted(hash, to, value, bindDoneCb(cb), permanent);
}
@@ -282,8 +343,8 @@ public:
* Insert known nodes to the routing table, without necessarly ping them.
* Usefull to restart a node and get things running fast without putting load on the network.
*/
void bootstrap(std::vector<SockAddr> nodes, DoneCallbackSimple cb={});
void bootstrap(SockAddr addr, DoneCallbackSimple cb={});
void bootstrap(std::vector<SockAddr> nodes, DoneCallbackSimple cb = {});
void bootstrap(SockAddr addr, DoneCallbackSimple cb = {});
/**
* Insert known nodes to the routing table, without necessarly ping them.
@@ -353,9 +414,7 @@ public:
std::vector<ValuesExport> exportValues() const;
void setLogger(const Sp<Logger>& logger = {});
void setLogger(const Logger& logger) {
setLogger(std::make_shared<Logger>(logger));
}
void setLogger(const Logger& logger) { setLogger(std::make_shared<Logger>(logger)); }
/**
* Only print logs related to the given InfoHash (if given), or disable filter (if zeroes).
@@ -367,12 +426,14 @@ public:
void importValues(const std::vector<ValuesExport>& values);
bool isRunning() const {
return running != State::Idle;
}
bool isRunning() const { return running != State::Idle; }
NodeStats getNodesStats(sa_family_t af) const;
unsigned getNodesStats(sa_family_t af, unsigned *good_return, unsigned *dubious_return, unsigned *cached_return, unsigned *incoming_return) const;
unsigned getNodesStats(sa_family_t af,
unsigned* good_return,
unsigned* dubious_return,
unsigned* cached_return,
unsigned* incoming_return) const;
NodeInfo getNodeInfo() const;
void getNodeInfo(std::function<void(std::shared_ptr<NodeInfo>)>);
@@ -401,7 +462,11 @@ public:
* @param threaded: If false, ::loop() must be called periodically. Otherwise a thread is launched.
* @param cb: Optional callback to receive general state information.
*/
void run(in_port_t port = dht::net::DHT_DEFAULT_PORT, const crypto::Identity& identity = {}, bool threaded = true, NetId network = 0) {
void run(in_port_t port = dht::net::DHT_DEFAULT_PORT,
const crypto::Identity& identity = {},
bool threaded = true,
NetId network = 0)
{
Config config;
config.dht_config.node_config.network = network;
config.dht_config.id = identity;
@@ -411,13 +476,15 @@ public:
void run(in_port_t port, Config& config, Context&& context = {});
/**
* Same as @run(sockaddr_in, sockaddr_in6, Identity, bool, StatusCallback), but with string IP addresses and service (port).
* Same as @run(sockaddr_in, sockaddr_in6, Identity, bool, StatusCallback), but with string IP addresses and service
* (port).
*/
void run(const char* ip4, const char* ip6, const char* service, Config& config, Context&& context = {});
void run(const Config& config, Context&& context);
void setOnStatusChanged(StatusCallback&& cb) {
void setOnStatusChanged(StatusCallback&& cb)
{
if (cb)
statusCbs.emplace_back(std::move(cb));
}
@@ -427,7 +494,8 @@ public:
* regularly and everytime a new packet is received.
* @return the next op
*/
time_point loop() {
time_point loop()
{
std::lock_guard<std::mutex> lck(dht_mtx);
return loop_();
}
@@ -463,12 +531,12 @@ public:
*/
void setPushNotificationToken(const std::string& token);
/**
/**
* Sets the push notification topic
*/
void setPushNotificationTopic(const std::string& topic);
/**
/**
* Sets the push notification platform
*/
void setPushNotificationPlatform(const std::string& platform);
@@ -482,17 +550,11 @@ public:
void forwardAllMessages(bool forward);
private:
enum class State {
Idle,
Running,
Stopping
};
enum class State { Idle, Running, Stopping };
time_point loop_();
NodeStatus getStatus() const {
return std::max(status4, status6);
}
NodeStatus getStatus() const { return std::max(status4, status6); }
bool checkShutdown();
void opEnded();
@@ -529,8 +591,7 @@ private:
std::atomic_size_t ongoing_ops {0};
std::vector<ShutdownCallback> shutdownCallbacks_;
NodeStatus status4 {NodeStatus::Disconnected},
status6 {NodeStatus::Disconnected};
NodeStatus status4 {NodeStatus::Disconnected}, status6 {NodeStatus::Disconnected};
std::vector<StatusCallback> statusCbs {};
@@ -544,4 +605,4 @@ private:
std::shared_ptr<dht::Logger> logger_;
};
}
} // namespace dht
+2 -17
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "def.h"
+129 -113
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include <string>
@@ -43,25 +28,32 @@ namespace indexation {
* the node in question is a leaf, *the label is a prefix of all the keys
* contained in the leaf*.
*/
struct OPENDHT_PUBLIC Prefix {
struct OPENDHT_PUBLIC Prefix
{
Prefix() {}
Prefix(InfoHash h) : size_(h.size() * 8), content_(h.begin(), h.end()) { }
Prefix(const Blob& d, const Blob& f={}) : size_(d.size()*8), flags_(f), content_(d) { }
Prefix(InfoHash h)
: size_(h.size() * 8)
, content_(h.begin(), h.end())
{}
Prefix(const Blob& d, const Blob& f = {})
: size_(d.size() * 8)
, flags_(f)
, content_(d)
{}
Prefix(const Prefix& p, size_t first) :
size_(std::min(first, p.content_.size()*8)),
content_(Blob(p.content_.begin(), p.content_.begin()+size_/8))
Prefix(const Prefix& p, size_t first)
: size_(std::min(first, p.content_.size() * 8))
, content_(Blob(p.content_.begin(), p.content_.begin() + size_ / 8))
{
auto rem = size_ % 8;
if ( not p.flags_.empty() ) {
flags_ = Blob(p.flags_.begin(), p.flags_.begin()+size_/8);
if (not p.flags_.empty()) {
flags_ = Blob(p.flags_.begin(), p.flags_.begin() + size_ / 8);
if (rem)
flags_.push_back(p.flags_[size_/8] & (0xFF << (8 - rem)));
flags_.push_back(p.flags_[size_ / 8] & (0xFF << (8 - rem)));
}
if (rem)
content_.push_back(p.content_[size_/8] & (0xFF << (8 - rem)));
content_.push_back(p.content_[size_ / 8] & (0xFF << (8 - rem)));
}
/**
@@ -76,8 +68,9 @@ struct OPENDHT_PUBLIC Prefix {
*
* @throw out_of_range if len is larger than size of the content
*/
Prefix getPrefix(ssize_t len) const {
if ((size_t)std::abs(len) >= content_.size() * 8)
Prefix getPrefix(ssize_t len) const
{
if ((size_t) std::abs(len) >= content_.size() * 8)
throw std::out_of_range("len larger than prefix size.");
if (len < 0)
len += size_;
@@ -91,33 +84,31 @@ struct OPENDHT_PUBLIC Prefix {
ee *
* @see isActiveBit in private function
*/
bool isFlagActive(size_t pos) const {
return flags_.empty() or isActiveBit(flags_, pos);
}
bool isFlagActive(size_t pos) const { return flags_.empty() or isActiveBit(flags_, pos); }
/**
* @see isActiveBit in private function
*/
bool isContentBitActive(size_t pos) const {
return isActiveBit(content_, pos);
}
bool isContentBitActive(size_t pos) const { return isActiveBit(content_, pos); }
Prefix getFullSize() { return Prefix(*this, content_.size()*8); }
Prefix getFullSize() { return Prefix(*this, content_.size() * 8); }
/**
* This methods gets the prefix of its sibling in the PHT structure.
*
* @return The prefix of this sibling.
*/
Prefix getSibling() const {
Prefix getSibling() const
{
Prefix copy = *this;
if ( size_ )
if (size_)
copy.swapContentBit(size_ - 1);
return copy;
}
InfoHash hash() const {
InfoHash hash() const
{
Blob copy(content_);
copy.push_back(size_);
return InfoHash::get(copy);
@@ -130,22 +121,20 @@ ee *
* @param p2 second prefix to compared
* @return Lenght of the larger common prefix between both
*/
static inline unsigned commonBits(const Prefix& p1, const Prefix& p2) {
static inline unsigned commonBits(const Prefix& p1, const Prefix& p2)
{
unsigned i, j;
uint8_t x;
auto longest_prefix_size = std::min(p1.size_, p2.size_);
for (i = 0; i < longest_prefix_size; i++) {
if (p1.content_.data()[i] != p2.content_.data()[i]
or not p1.isFlagActive(i)
or not p2.isFlagActive(i) ) {
break;
if (p1.content_.data()[i] != p2.content_.data()[i] or not p1.isFlagActive(i) or not p2.isFlagActive(i)) {
break;
}
}
if (i == longest_prefix_size)
return 8*longest_prefix_size;
return 8 * longest_prefix_size;
x = p1.content_.data()[i] ^ p2.content_.data()[i];
@@ -161,38 +150,33 @@ ee *
/**
* @see doc of swap private function
*/
void swapContentBit(size_t bit) {
swapBit(content_, bit);
}
void swapContentBit(size_t bit) { swapBit(content_, bit); }
/**
* @see doc of swap private function
*/
void swapFlagBit(size_t bit) {
swapBit(flags_, bit);
}
void swapFlagBit(size_t bit) { swapBit(flags_, bit); }
/**
* @see doc of addPadding private function
*/
void addPaddingContent(size_t size) {
content_ = addPadding(content_, size);
}
void addPaddingContent(size_t size) { content_ = addPadding(content_, size); }
void updateFlags() {
void updateFlags()
{
/* Fill first known bit */
auto csize = size_ - flags_.size() * 8;
while(csize >= 8) {
while (csize >= 8) {
flags_.push_back(0xFF);
csize -= 8;
}
/* if needed fill remaining bit */
if ( csize )
if (csize)
flags_.push_back(0xFF << (8 - csize));
/* Complet vector space missing */
for ( auto i = flags_.size(); i < content_.size(); i++ )
for (auto i = flags_.size(); i < content_.size(); i++)
flags_.push_back(0xFF);
}
@@ -207,7 +191,6 @@ ee *
Blob content_ {};
private:
/**
* Add a padding to the input blob
*
@@ -216,9 +199,10 @@ private:
*
* @return Copy of the input Blob but with a padding
*/
Blob addPadding(Blob toP, size_t size) {
Blob addPadding(Blob toP, size_t size)
{
Blob copy = toP;
for ( auto i = copy.size(); i < size; i++ )
for (auto i = copy.size(); i < size; i++)
copy.push_back(0);
swapBit(copy, size_ + 1);
@@ -235,11 +219,12 @@ private:
*
* @throw out_of_range if bit is superior to blob size * 8
*/
bool isActiveBit(const Blob &b, size_t pos) const {
if ( pos >= content_.size() * 8 )
bool isActiveBit(const Blob& b, size_t pos) const
{
if (pos >= content_.size() * 8)
throw std::out_of_range("Can't detect active bit at pos, pos larger than prefix size or empty prefix");
return ((b[pos / 8] >> (7 - (pos % 8)) ) & 1) == 1;
return ((b[pos / 8] >> (7 - (pos % 8))) & 1) == 1;
}
/**
@@ -252,8 +237,9 @@ private:
*
* @throw out_of_range if bit is superior to blob size * 8
*/
void swapBit(Blob &b, size_t bit) {
if ( bit >= b.size() * 8 )
void swapBit(Blob& b, size_t bit)
{
if (bit >= b.size() * 8)
throw std::out_of_range("bit larger than prefix size.");
size_t offset_bit = (8 - bit) % 8;
@@ -262,15 +248,18 @@ private:
};
using Value = std::pair<InfoHash, dht::Value::Id>;
struct OPENDHT_PUBLIC IndexEntry : public dht::Value::Serializable<IndexEntry> {
struct OPENDHT_PUBLIC IndexEntry : public dht::Value::Serializable<IndexEntry>
{
static const ValueType& TYPE;
virtual void unpackValue(const dht::Value& v) {
virtual void unpackValue(const dht::Value& v)
{
Serializable<IndexEntry>::unpackValue(v);
name = v.user_type;
}
virtual dht::Value packValue() const {
virtual dht::Value packValue() const
{
auto pack = Serializable<IndexEntry>::packValue();
pack.user_type = name;
return pack;
@@ -282,14 +271,14 @@ struct OPENDHT_PUBLIC IndexEntry : public dht::Value::Serializable<IndexEntry> {
MSGPACK_DEFINE_MAP(prefix, value)
};
class OPENDHT_PUBLIC Pht {
class OPENDHT_PUBLIC Pht
{
static constexpr const char* INVALID_KEY = "Key does not match the PHT key spec.";
/* Prefixes the user_type for all dht values put on the DHT */
static constexpr const char* INDEX_PREFIX = "index.pht.";
public:
/* This is the maximum number of entries per node. This parameter is
* critical and influences the traffic a lot during a lookup operation.
*/
@@ -303,28 +292,34 @@ public:
using KeySpec = std::map<std::string, size_t>;
using LookupCallback = std::function<void(std::vector<std::shared_ptr<Value>>& values, const Prefix& p)>;
typedef void (*LookupCallbackRaw)(std::vector<std::shared_ptr<Value>>* values, Prefix* p, void *user_data);
static LookupCallback
bindLookupCb(LookupCallbackRaw raw_cb, void* user_data) {
if (not raw_cb) return {};
typedef void (*LookupCallbackRaw)(std::vector<std::shared_ptr<Value>>* values, Prefix* p, void* user_data);
static LookupCallback bindLookupCb(LookupCallbackRaw raw_cb, void* user_data)
{
if (not raw_cb)
return {};
return [=](std::vector<std::shared_ptr<Value>>& values, const Prefix& p) {
raw_cb((std::vector<std::shared_ptr<Value>>*) &values, (Prefix*) &p, user_data);
};
}
using LookupCallbackSimple = std::function<void(std::vector<std::shared_ptr<Value>>& values)>;
typedef void (*LookupCallbackSimpleRaw)(std::vector<std::shared_ptr<Value>>* values, void *user_data);
static LookupCallbackSimple
bindLookupCbSimple(LookupCallbackSimpleRaw raw_cb, void* user_data) {
if (not raw_cb) return {};
typedef void (*LookupCallbackSimpleRaw)(std::vector<std::shared_ptr<Value>>* values, void* user_data);
static LookupCallbackSimple bindLookupCbSimple(LookupCallbackSimpleRaw raw_cb, void* user_data)
{
if (not raw_cb)
return {};
return [=](std::vector<std::shared_ptr<Value>>& values) {
raw_cb((std::vector<std::shared_ptr<Value>>*) &values, user_data);
};
}
Pht(std::string name, KeySpec k_spec, std::shared_ptr<DhtRunner> dht)
: name_(INDEX_PREFIX + name), canary_(name_ + ".canary"), keySpec_(k_spec), dht_(dht) {}
: name_(INDEX_PREFIX + name)
, canary_(name_ + ".canary")
, keySpec_(k_spec)
, dht_(dht)
{}
virtual ~Pht () { }
virtual ~Pht() {}
/**
* Lookup a key for a value.
@@ -332,7 +327,11 @@ public:
void lookup(Key k, LookupCallback cb = {}, DoneCallbackSimple done_cb = {}, bool exact_match = true);
void lookup(Key k, LookupCallbackSimple cb = {}, DoneCallbackSimple done_cb = {}, bool exact_match = true)
{
lookup(k, [cb=std::move(cb)](std::vector<std::shared_ptr<Value>>& values, Prefix) { cb(values); }, done_cb, exact_match);
lookup(
k,
[cb = std::move(cb)](std::vector<std::shared_ptr<Value>>& values, Prefix) { cb(values); },
done_cb,
exact_match);
}
/**
@@ -342,7 +341,8 @@ public:
* @param v : Value to insert
* @param done_cb : Callbakc which going to be call when all the insert is done
*/
void insert(Key k, Value v, DoneCallbackSimple done_cb = {}) {
void insert(Key k, Value v, DoneCallbackSimple done_cb = {})
{
Prefix p = linearize(k);
auto lo = std::make_shared<int>(0);
@@ -357,7 +357,6 @@ public:
}
private:
/**
* Insert function which really insert onto the pht
*
@@ -370,10 +369,16 @@ private:
* @param done_cb : Callback to call when the insert is done
*/
void insert(const Prefix& kp, IndexEntry entry, std::shared_ptr<int> lo, std::shared_ptr<int> hi, time_point time_p,
bool check_split, DoneCallbackSimple done_cb = {});
void insert(const Prefix& kp,
IndexEntry entry,
std::shared_ptr<int> lo,
std::shared_ptr<int> hi,
time_point time_p,
bool check_split,
DoneCallbackSimple done_cb = {});
class Cache {
class Cache
{
public:
/**
* Insert all needed node into the tree according to a prefix
@@ -395,14 +400,15 @@ private:
static constexpr const size_t MAX_ELEMENT {1024};
static constexpr const std::chrono::minutes NODE_EXPIRE_TIME {5};
struct Node {
struct Node
{
time_point last_reply; /* Made the assocation between leaves and leaves multimap */
std::shared_ptr<Node> parent; /* Share_ptr to the parent, it allow the self destruction of tree */
std::weak_ptr<Node> left_child; /* Left child, for bit equal to 1 */
std::weak_ptr<Node> right_child; /* Right child, for bit equal to 0 */
};
std::weak_ptr<Node> root_; /* Root of the tree */
std::weak_ptr<Node> root_; /* Root of the tree */
/**
* This mutlimap contains all prefix insert in the tree in time order
@@ -427,14 +433,19 @@ private:
* @param cb : Callback to use at the end of the lookupStep (call on the value of vals)
* @param done_cb : Callback at the end of the lookupStep
* @param max_common_prefix_len: used in the inexacte lookup match case, indicate the longest common prefix found
* @param start : If start is set then lo and hi will be ignore for the first step, if the step fail lo and hi will be used
* @param start : If start is set then lo and hi will be ignore for the first step, if the step fail lo and hi
* will be used
* @param all_values : If all value is true then all value met during the lookupstep will be in the vector vals
*/
void lookupStep(Prefix k, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
std::shared_ptr<std::vector<std::shared_ptr<IndexEntry>>> vals,
LookupCallbackWrapper cb, DoneCallbackSimple done_cb,
std::shared_ptr<unsigned> max_common_prefix_len,
int start = -1, bool all_values = false);
void lookupStep(Prefix k,
std::shared_ptr<int> lo,
std::shared_ptr<int> hi,
std::shared_ptr<std::vector<std::shared_ptr<IndexEntry>>> vals,
LookupCallbackWrapper cb,
DoneCallbackSimple done_cb,
std::shared_ptr<unsigned> max_common_prefix_len,
int start = -1,
bool all_values = false);
/**
* Apply the zcurve algorithm on the list of input prefix
@@ -463,7 +474,7 @@ private:
* @param entry The entry to put at the prefix p
* @param end_cb Callback to use at the end of counting
*/
void getRealPrefix(const std::shared_ptr<Prefix>& p, IndexEntry entry, RealInsertCallback end_cb );
void getRealPrefix(const std::shared_ptr<Prefix>& p, IndexEntry entry, RealInsertCallback end_cb);
/**
* Looking where to put the data cause if there is free space on the node
@@ -482,10 +493,11 @@ private:
* @param vals : The vector of values to compare with comapred
* @return position compared diverge from all others
*/
static size_t findSplitLocation(const Prefix& compared, const std::vector<std::shared_ptr<IndexEntry>>& vals) {
for ( size_t i = 0; i < compared.content_.size() * 8 - 1; i++ )
for ( auto const& v : vals)
if ( Prefix(v->prefix).isContentBitActive(i) != compared.isContentBitActive(i) )
static size_t findSplitLocation(const Prefix& compared, const std::vector<std::shared_ptr<IndexEntry>>& vals)
{
for (size_t i = 0; i < compared.content_.size() * 8 - 1; i++)
for (auto const& v : vals)
if (Prefix(v->prefix).isContentBitActive(i) != compared.isContentBitActive(i))
return i + 1;
return compared.content_.size() * 8 - 1;
}
@@ -498,18 +510,23 @@ private:
* @param entry : Entry to put on the pht
* @param end_cb : Callback to apply to the insert prefi (here does the insert)
*/
void split(const Prefix& insert, const std::vector<std::shared_ptr<IndexEntry>>& vals, IndexEntry entry, RealInsertCallback end_cb);
void split(const Prefix& insert,
const std::vector<std::shared_ptr<IndexEntry>>& vals,
IndexEntry entry,
RealInsertCallback end_cb);
/**
* Tells if the key is valid according to the key spec.
*/
bool validKey(const Key& k) const {
return k.size() == keySpec_.size() and
std::equal(k.begin(), k.end(), keySpec_.begin(),
[&](const Key::value_type& key, const KeySpec::value_type& key_spec) {
return key.first == key_spec.first and key.second.size() <= key_spec.second;
}
);
bool validKey(const Key& k) const
{
return k.size() == keySpec_.size()
and std::equal(k.begin(),
k.end(),
keySpec_.begin(),
[&](const Key::value_type& key, const KeySpec::value_type& key_spec) {
return key.first == key_spec.first and key.second.size() <= key_spec.second;
});
}
/**
@@ -525,6 +542,5 @@ private:
std::shared_ptr<DhtRunner> dht_;
};
} /* indexation */
} /* dht */
} // namespace indexation
} // namespace dht
+116 -118
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "def.h"
@@ -53,7 +38,7 @@ namespace dht {
using byte = uint8_t;
namespace crypto {
OPENDHT_PUBLIC void hash(const uint8_t* data, size_t data_length, uint8_t* hash, size_t hash_length);
OPENDHT_PUBLIC void hash(const uint8_t* data, size_t data_length, uint8_t* hash, size_t hash_length);
}
/**
@@ -61,17 +46,17 @@ namespace crypto {
* a byte array of N bytes.
* Hashes identify nodes and values in the Dht.
*/
template <size_t N>
class Hash {
template<size_t N>
class Hash
{
public:
using T = std::array<uint8_t, N>;
typedef typename T::iterator iterator;
typedef typename T::const_iterator const_iterator;
Hash() noexcept {
data_.fill(0);
}
Hash(const uint8_t* h, size_t data_len) {
Hash() noexcept { data_.fill(0); }
Hash(const uint8_t* h, size_t data_len)
{
if (data_len < N)
data_.fill(0);
else
@@ -82,16 +67,15 @@ public:
* hex must be at least 2.HASH_LEN characters long.
* If too long, only the first 2.HASH_LEN characters are read.
*/
explicit Hash(std::string_view hex) {
if (hex.size() < 2*N)
explicit Hash(std::string_view hex)
{
if (hex.size() < 2 * N)
data_.fill(0);
else
fromString(hex.data());
}
Hash(const msgpack::object& o) {
msgpack_unpack(o);
}
Hash(const msgpack::object& o) { msgpack_unpack(o); }
static constexpr size_t size() noexcept { return N; }
const uint8_t* data() const { return data_.data(); }
@@ -101,22 +85,22 @@ public:
iterator end() { return data_.end(); }
const_iterator cend() const { return data_.cend(); }
static constexpr inline Hash zero() noexcept { return Hash{}; }
static constexpr inline Hash zero() noexcept { return Hash {}; }
bool operator==(const Hash& h) const {
return data_ == h.data_;
}
bool operator==(const Hash& h) const { return data_ == h.data_; }
bool operator!=(const Hash& h) const { return !(*this == h); }
bool operator<(const Hash& o) const {
bool operator<(const Hash& o) const
{
for (unsigned i = 0; i < N; i++) {
if(data_[i] != o.data_[i])
if (data_[i] != o.data_[i])
return data_[i] < o.data_[i];
}
return false;
}
Hash operator^(const Hash& o) const {
Hash operator^(const Hash& o) const
{
Hash result;
for (auto i = 0u; i < N; i++) {
result[i] = data_[i] ^ o.data_[i];
@@ -124,7 +108,8 @@ public:
return result;
}
explicit operator bool() const {
explicit operator bool() const
{
auto a = reinterpret_cast<const uint32_t*>(data_.data());
auto b = reinterpret_cast<const uint32_t*>(data_.data() + N);
for (; a != b; a++) {
@@ -141,41 +126,42 @@ public:
* Find the lowest 1 bit in an id.
* Result will allways be lower than 8*N
*/
inline int lowbit() const {
inline int lowbit() const
{
int i, j;
for(i = N-1; i >= 0; i--)
if(data_[i] != 0)
for (i = N - 1; i >= 0; i--)
if (data_[i] != 0)
break;
if(i < 0)
if (i < 0)
return -1;
for(j = 7; j >= 0; j--)
if((data_[i] & (0x80 >> j)) != 0)
for (j = 7; j >= 0; j--)
if ((data_[i] & (0x80 >> j)) != 0)
break;
return 8 * i + j;
}
static inline int cmp(const Hash& id1, const Hash& id2) {
static inline int cmp(const Hash& id1, const Hash& id2)
{
return std::memcmp(id1.data_.data(), id2.data_.data(), N);
}
/** Find how many bits two ids have in common. */
static inline unsigned
commonBits(const Hash& id1, const Hash& id2)
static inline unsigned commonBits(const Hash& id1, const Hash& id2)
{
unsigned i, j;
uint8_t x;
for(i = 0; i < N; i++) {
if(id1.data_[i] != id2.data_[i])
for (i = 0; i < N; i++) {
if (id1.data_[i] != id2.data_[i])
break;
}
if(i == N)
return 8*N;
if (i == N)
return 8 * N;
x = id1.data_[i] ^ id2.data_[i];
j = 0;
while((x & 0x80) == 0) {
while ((x & 0x80) == 0) {
x <<= 1;
j++;
}
@@ -184,11 +170,10 @@ public:
}
/** Determine whether id1 or id2 is closer to this */
int
xorCmp(const Hash& id1, const Hash& id2) const
int xorCmp(const Hash& id1, const Hash& id2) const
{
for (unsigned i = 0; i < N; i++) {
if(id1.data_[i] == id2.data_[i])
if (id1.data_[i] == id2.data_[i])
continue;
uint8_t xor1 = id1.data_[i] ^ data_[i];
uint8_t xor2 = id2.data_[i] ^ data_[i];
@@ -197,40 +182,36 @@ public:
return 0;
}
bool
getBit(unsigned nbit) const
bool getBit(unsigned nbit) const
{
auto& num = *(data_.cbegin()+(nbit/8));
auto& num = *(data_.cbegin() + (nbit / 8));
unsigned bit = 7 - (nbit % 8);
return (num >> bit) & 1;
}
void
setBit(unsigned nbit, bool b)
void setBit(unsigned nbit, bool b)
{
auto& num = data_[nbit/8];
auto& num = data_[nbit / 8];
unsigned bit = 7 - (nbit % 8);
num ^= (-b ^ num) & (1 << bit);
}
double toFloat() const {
double toFloat() const
{
using D = size_t;
double v = 0.;
for (size_t i = 0; i < std::min<size_t>(N, sizeof(D)-1); i++)
v += *(data_.cbegin()+i) / (double)((D)1 << 8*(i+1));
for (size_t i = 0; i < std::min<size_t>(N, sizeof(D) - 1); i++)
v += *(data_.cbegin() + i) / (double) ((D) 1 << 8 * (i + 1));
return v;
}
static inline Hash get(std::string_view data) {
return get((const uint8_t*)data.data(), data.size());
}
static inline Hash get(std::string_view data) { return get((const uint8_t*) data.data(), data.size()); }
static inline Hash get(const std::vector<uint8_t>& data) {
return get(data.data(), data.size());
}
static inline Hash get(const std::vector<uint8_t>& data) { return get(data.data(), data.size()); }
template <size_t H>
static Hash get(const Hash<H>& o) {
template<size_t H>
static Hash get(const Hash<H>& o)
{
return get(o.data(), o.size());
}
@@ -246,33 +227,35 @@ public:
static Hash getRandom();
template <typename Rd>
template<typename Rd>
static Hash getRandom(Rd&);
template <size_t M>
friend std::ostream& operator<< (std::ostream& s, const Hash<M>& h);
template<size_t M>
friend std::ostream& operator<<(std::ostream& s, const Hash<M>& h);
template <size_t M>
friend std::istream& operator>> (std::istream& s, Hash<M>& h);
template<size_t M>
friend std::istream& operator>>(std::istream& s, Hash<M>& h);
/** Returns view to thread-allocated memory, only valid until the next call to this function. */
std::string_view to_view() const { return std::string_view(to_c_str(), N*2); }
std::string_view to_view() const { return std::string_view(to_c_str(), N * 2); }
const char* to_c_str() const;
std::string toString() const;
template <typename Packer>
template<typename Packer>
void msgpack_pack(Packer& pk) const
{
pk.pack_bin(N);
pk.pack_bin_body((char*)data_.data(), N);
pk.pack_bin_body((char*) data_.data(), N);
}
void msgpack_unpack(msgpack::object o) {
void msgpack_unpack(msgpack::object o)
{
if (o.type != msgpack::type::BIN or o.via.bin.size != N)
throw msgpack::type_error();
std::copy_n(o.via.bin.ptr, N, data_.data());
}
private:
T data_;
void fromString(const char*);
@@ -283,41 +266,48 @@ using InfoHash = Hash<HASH_LEN>;
using h256 = Hash<32>;
using PkId = h256;
template <size_t N>
std::ostream& operator<< (std::ostream& s, const Hash<N>& h)
template<size_t N>
std::ostream&
operator<<(std::ostream& s, const Hash<N>& h)
{
s.write(h.to_c_str(), N*2);
s.write(h.to_c_str(), N * 2);
return s;
}
template <size_t N>
std::istream& operator>> (std::istream& s, Hash<N>& h)
template<size_t N>
std::istream&
operator>>(std::istream& s, Hash<N>& h)
{
std::array<char, h.size()*2> dat;
std::array<char, h.size() * 2> dat;
s.exceptions(std::istream::eofbit | std::istream::failbit);
s.read(&(*dat.begin()), dat.size());
fromString(dat.data());
return s;
}
template <size_t N>
template<size_t N>
void
Hash<N>::fromString(const char* in) {
Hash<N>::fromString(const char* in)
{
auto hex2bin = [](char c) -> uint8_t {
if (c >= 'a' and c <= 'f') return 10 + c - 'a';
else if (c >= 'A' and c <= 'F') return 10 + c - 'A';
else if (c >= '0' and c <= '9') return c - '0';
else throw std::domain_error("not an hex character");
if (c >= 'a' and c <= 'f')
return 10 + c - 'a';
else if (c >= 'A' and c <= 'F')
return 10 + c - 'A';
else if (c >= '0' and c <= '9')
return c - '0';
else
throw std::domain_error("not an hex character");
};
try {
for (size_t i=0; i<N; i++)
data_[i] = (hex2bin(in[2*i]) << 4) | hex2bin(in[2*i+1]);
for (size_t i = 0; i < N; i++)
data_[i] = (hex2bin(in[2 * i]) << 4) | hex2bin(in[2 * i + 1]);
} catch (const std::domain_error&) {
data_.fill(0);
}
}
template <size_t N>
template<size_t N>
Hash<N>
Hash<N>::getRandom()
{
@@ -330,8 +320,8 @@ Hash<N>::getRandom()
return h;
}
template <size_t N>
template <typename Rd>
template<size_t N>
template<typename Rd>
Hash<N>
Hash<N>::getRandom(Rd& rdev)
{
@@ -343,14 +333,18 @@ Hash<N>::getRandom(Rd& rdev)
return h;
}
struct alignas(std::max_align_t) HexMap : public std::array<std::array<char, 2>, 256> {
constexpr HexMap(): std::array<std::array<char, 2>, 256>() {
struct alignas(std::max_align_t) HexMap : public std::array<std::array<char, 2>, 256>
{
constexpr HexMap()
: std::array<std::array<char, 2>, 256>()
{
for (size_t i = 0; i < size(); i++) {
auto& e = (*this)[i];
e[0] = hex_digits[(i >> 4) & 0x0F];
e[1] = hex_digits[i & 0x0F];
}
}
private:
static constexpr const char* hex_digits = "0123456789abcdef";
};
@@ -358,47 +352,51 @@ private:
inline constexpr HexMap hex_map {};
inline std::string
toHex(const uint8_t* data, size_t size) {
toHex(const uint8_t* data, size_t size)
{
std::string ret(size * 2, '\0');
for (size_t i=0; i<size; i++) {
auto b = ret.data()+i*2;
for (size_t i = 0; i < size; i++) {
auto b = ret.data() + i * 2;
const auto& m = hex_map[data[i]];
*((uint16_t*)b) = *((uint16_t*)&m);
*((uint16_t*) b) = *((uint16_t*) &m);
}
return ret;
}
inline std::string
toHex(const std::vector<uint8_t>& data) {
toHex(const std::vector<uint8_t>& data)
{
return toHex(data.data(), data.size());
}
template <size_t N>
template<size_t N>
const char*
Hash<N>::to_c_str() const
{
alignas(std::max_align_t) thread_local std::array<char, N*2+1> buf;
for (size_t i=0; i<N; i++) {
auto b = buf.data() + i*2;
alignas(std::max_align_t) thread_local std::array<char, N * 2 + 1> buf;
for (size_t i = 0; i < N; i++) {
auto b = buf.data() + i * 2;
const auto& m = hex_map[data_[i]];
*((uint16_t*)b) = *((uint16_t*)&m);
*((uint16_t*) b) = *((uint16_t*) &m);
}
buf[N*2] = '\0';
buf[N * 2] = '\0';
return buf.data();
}
template <size_t N>
template<size_t N>
std::string
Hash<N>::toString() const
{
return std::string(to_c_str(), N*2);
return std::string(to_c_str(), N * 2);
}
}
} // namespace dht
template <size_t N>
struct fmt::formatter<dht::Hash<N>>: formatter<string_view> {
constexpr auto format(const dht::Hash<N>& c, format_context& ctx) const {
return formatter<string_view>::format(c.to_view(), ctx);
}
template<size_t N>
struct fmt::formatter<dht::Hash<N>> : formatter<string_view>
{
constexpr auto format(const dht::Hash<N>& c, format_context& ctx) const
{
return formatter<string_view>::format(c.to_view(), ctx);
}
};
+9 -28
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "def.h"
@@ -36,22 +21,18 @@ OPENDHT_PUBLIC
std::shared_ptr<Logger> getStdLogger();
OPENDHT_PUBLIC
std::shared_ptr<Logger> getFileLogger(const std::string &path);
std::shared_ptr<Logger> getFileLogger(const std::string& path);
OPENDHT_PUBLIC
std::shared_ptr<Logger> getSyslogLogger(const char* name);
OPENDHT_PUBLIC void
enableLogging(dht::DhtRunner &dht);
OPENDHT_PUBLIC void enableLogging(dht::DhtRunner& dht);
OPENDHT_PUBLIC void
enableFileLogging(dht::DhtRunner &dht, const std::string &path);
OPENDHT_PUBLIC void enableFileLogging(dht::DhtRunner& dht, const std::string& path);
OPENDHT_PUBLIC void
disableLogging(dht::DhtRunner &dht);
OPENDHT_PUBLIC void disableLogging(dht::DhtRunner& dht);
OPENDHT_PUBLIC void
enableSyslog(dht::DhtRunner &dht, const char* name);
OPENDHT_PUBLIC void enableSyslog(dht::DhtRunner& dht, const char* name);
} /* log */
} /* dht */
} // namespace log
} // namespace dht
+2 -17
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "infohash.h"
+105 -119
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "node_cache.h"
@@ -46,23 +31,25 @@ struct TransId;
#define MSG_CONFIRM 0
#endif
struct NetworkConfig {
struct NetworkConfig
{
NetId network {0};
ssize_t max_req_per_sec {0};
ssize_t max_peer_req_per_sec {0};
bool is_client {false};
};
class DhtProtocolException : public DhtException {
class DhtProtocolException : public DhtException
{
public:
// sent to another peer (http-like).
static const constexpr uint16_t NON_AUTHORITATIVE_INFORMATION {203}; /* incomplete request packet. */
static const constexpr uint16_t UNAUTHORIZED {401}; /* incorrect tokens. */
static const constexpr uint16_t NOT_FOUND {404}; /* storage not found */
// for internal use (custom).
static const constexpr uint16_t INVALID_TID_SIZE {421}; /* id was truncated. */
static const constexpr uint16_t UNKNOWN_TID {422}; /* unknown tid */
static const constexpr uint16_t WRONG_NODE_INFO_BUF_LEN {423}; /* node info length is incorrect */
static const constexpr uint16_t INVALID_TID_SIZE {421}; /* id was truncated. */
static const constexpr uint16_t UNKNOWN_TID {422}; /* unknown tid */
static const constexpr uint16_t WRONG_NODE_INFO_BUF_LEN {423}; /* node info length is incorrect */
static const std::string GET_NO_INFOHASH; /* received "get" request with no infohash */
static const std::string LISTEN_NO_INFOHASH; /* got "listen" request without infohash */
@@ -72,8 +59,12 @@ public:
static const std::string STORAGE_NOT_FOUND; /* got access request for an unknown storage */
static const std::string PUT_INVALID_ID; /* invalid id in "put" request */
DhtProtocolException(uint16_t code, const std::string& msg="", InfoHash failing_node_id={})
: DhtException(msg), msg(msg), code(code), failing_node_id(failing_node_id) {}
DhtProtocolException(uint16_t code, const std::string& msg = "", InfoHash failing_node_id = {})
: DhtException(msg)
, msg(msg)
, code(code)
, failing_node_id(failing_node_id)
{}
const std::string& getMsg() const { return msg; }
uint16_t getCode() const { return code; }
@@ -90,7 +81,8 @@ struct ParsedMessage;
/**
* Answer for a request.
*/
struct RequestAnswer {
struct RequestAnswer
{
Blob ntoken {};
Value::Id vid {};
std::vector<Sp<Value>> values {};
@@ -175,12 +167,7 @@ private:
* @param token (type: Blob) security token.
* @param rid (type: uint16_t) request id.
*/
std::function<RequestAnswer(Sp<Node>,
const InfoHash&,
const Blob&,
Tid,
const Query&,
int)> onListen {};
std::function<RequestAnswer(Sp<Node>, const InfoHash&, const Blob&, Tid, const Query&, int)> onListen {};
/**
* Called on announce request.
*
@@ -190,11 +177,8 @@ private:
* @param values (type: std::vector<Sp<Value>>) values to store.
* @param created (type: time_point) time when the value was created.
*/
std::function<RequestAnswer(Sp<Node>,
const InfoHash&,
const Blob&,
const std::vector<Sp<Value>>&,
const time_point&)> onAnnounce {};
std::function<RequestAnswer(Sp<Node>, const InfoHash&, const Blob&, const std::vector<Sp<Value>>&, const time_point&)>
onAnnounce {};
/**
* Called on refresh request.
*
@@ -203,32 +187,28 @@ private:
* @param token (type: Blob) security token.
* @param vid (type: Value::id) the value id.
*/
std::function<RequestAnswer(Sp<Node>,
const InfoHash&,
const Blob&,
const Value::Id&)> onRefresh {};
std::function<RequestAnswer(Sp<Node>, const InfoHash&, const Blob&, const Value::Id&)> onRefresh {};
public:
using RequestCb = std::function<void(const Request&, RequestAnswer&&)>;
using RequestErrorCb = std::function<bool(const Request&, DhtProtocolException&&)>;
using RequestExpiredCb = std::function<void(const Request&, bool)>;
NetworkEngine(
InfoHash& myid,
NetworkConfig config,
std::unique_ptr<DatagramSocket>&& sock,
const Sp<Logger>& log,
std::mt19937_64& rd,
Scheduler& scheduler,
decltype(NetworkEngine::onError)&& onError,
decltype(NetworkEngine::onNewNode)&& onNewNode,
decltype(NetworkEngine::onReportedAddr)&& onReportedAddr,
decltype(NetworkEngine::onPing)&& onPing,
decltype(NetworkEngine::onFindNode)&& onFindNode,
decltype(NetworkEngine::onGetValues)&& onGetValues,
decltype(NetworkEngine::onListen)&& onListen,
decltype(NetworkEngine::onAnnounce)&& onAnnounce,
decltype(NetworkEngine::onRefresh)&& onRefresh);
NetworkEngine(InfoHash& myid,
NetworkConfig config,
std::unique_ptr<DatagramSocket>&& sock,
const Sp<Logger>& log,
std::mt19937_64& rd,
Scheduler& scheduler,
decltype(NetworkEngine::onError)&& onError,
decltype(NetworkEngine::onNewNode)&& onNewNode,
decltype(NetworkEngine::onReportedAddr)&& onReportedAddr,
decltype(NetworkEngine::onPing)&& onPing,
decltype(NetworkEngine::onFindNode)&& onFindNode,
decltype(NetworkEngine::onGetValues)&& onGetValues,
decltype(NetworkEngine::onListen)&& onListen,
decltype(NetworkEngine::onAnnounce)&& onAnnounce,
decltype(NetworkEngine::onRefresh)&& onRefresh);
~NetworkEngine();
@@ -251,15 +231,32 @@ public:
* @param values The values to send.
* @param version If version = 1, a request will be used to answer to the listener
*/
void tellListener(const Sp<Node>& n, Tid socket_id, const InfoHash& hash, want_t want, const Blob& ntoken,
std::vector<Sp<Node>>&& nodes, std::vector<Sp<Node>>&& nodes6,
std::vector<Sp<Value>>&& values, const Query& q, int version);
void tellListener(const Sp<Node>& n,
Tid socket_id,
const InfoHash& hash,
want_t want,
const Blob& ntoken,
std::vector<Sp<Node>>&& nodes,
std::vector<Sp<Node>>&& nodes6,
std::vector<Sp<Value>>&& values,
const Query& q,
int version);
void tellListenerRefreshed(const Sp<Node>& n, Tid socket_id, const InfoHash& hash, const Blob& ntoken, const std::vector<Value::Id>& values, int version);
void tellListenerExpired(const Sp<Node>& n, Tid socket_id, const InfoHash& hash, const Blob& ntoken, const std::vector<Value::Id>& values, int version);
void tellListenerRefreshed(const Sp<Node>& n,
Tid socket_id,
const InfoHash& hash,
const Blob& ntoken,
const std::vector<Value::Id>& values,
int version);
void tellListenerExpired(const Sp<Node>& n,
Tid socket_id,
const InfoHash& hash,
const Blob& ntoken,
const std::vector<Value::Id>& values,
int version);
bool isRunning(sa_family_t af) const;
inline want_t want () const { return dht_socket->hasIPv4() and dht_socket->hasIPv6() ? (WANT4 | WANT6) : -1; }
inline want_t want() const { return dht_socket->hasIPv4() and dht_socket->hasIPv6() ? (WANT4 | WANT6) : -1; }
void connectivityChanged(sa_family_t);
@@ -276,8 +273,7 @@ public:
*
* @return the request with information concerning its success.
*/
Sp<Request>
sendPing(const Sp<Node>& n, RequestCb&& on_done, RequestExpiredCb&& on_expired);
Sp<Request> sendPing(const Sp<Node>& n, RequestCb&& on_done, RequestExpiredCb&& on_expired);
/**
* Send a "ping" request to a given node.
@@ -289,11 +285,11 @@ public:
*
* @return the request with information concerning its success.
*/
Sp<Request>
sendPing(SockAddr&& sa, RequestCb&& on_done, RequestExpiredCb&& on_expired) {
Sp<Request> sendPing(SockAddr&& sa, RequestCb&& on_done, RequestExpiredCb&& on_expired)
{
return sendPing(std::make_shared<Node>(InfoHash::zero(), std::move(sa), rd),
std::forward<RequestCb>(on_done),
std::forward<RequestExpiredCb>(on_expired));
std::forward<RequestCb>(on_done),
std::forward<RequestExpiredCb>(on_expired));
}
/**
@@ -416,11 +412,11 @@ public:
* @return the request with information concerning its success.
*/
void sendUpdateValues(const Sp<Node>& n,
const InfoHash& infohash,
std::vector<Sp<Value>>&& values,
time_point created,
const Blob& token,
size_t sid);
const InfoHash& infohash,
std::vector<Sp<Value>>&& values,
time_point created,
const Blob& token,
size_t sid);
Sp<Request> sendUpdateValues(const Sp<Node>& n,
const InfoHash& infohash,
std::vector<Sp<Value>>::iterator begin,
@@ -438,44 +434,38 @@ public:
* @param fromlen The length of the corresponding sockaddr structure.
* @param now The time to adjust the clock in the network engine.
*/
void processMessage(const uint8_t *buf, size_t buflen, SockAddr addr);
void processMessage(const uint8_t* buf, size_t buflen, SockAddr addr);
Sp<Node> insertNode(const InfoHash& id, const SockAddr& addr) {
Sp<Node> insertNode(const InfoHash& id, const SockAddr& addr)
{
auto n = cache.getNode(id, addr, scheduler.time(), 0);
onNewNode(n, 0);
return n;
}
std::vector<unsigned> getNodeMessageStats(bool in) {
std::vector<unsigned> getNodeMessageStats(bool in)
{
auto& st = in ? in_stats : out_stats;
std::vector<unsigned> stats {st.ping, st.find, st.get, st.listen, st.put};
std::vector<unsigned> stats {st.ping, st.find, st.get, st.listen, st.put};
st = {};
return stats;
}
void blacklistNode(const Sp<Node>& n);
std::vector<Sp<Node>> getCachedNodes(const InfoHash& id, sa_family_t sa_f, size_t count) {
std::vector<Sp<Node>> getCachedNodes(const InfoHash& id, sa_family_t sa_f, size_t count)
{
return cache.getCachedNodes(id, sa_f, count);
}
size_t getNodeCacheSize() const {
return cache.size();
}
size_t getNodeCacheSize(sa_family_t af) const {
return cache.size(af);
}
size_t getNodeCacheSize() const { return cache.size(); }
size_t getNodeCacheSize(sa_family_t af) const { return cache.size(af); }
size_t getRateLimiterSize() const {
return address_rate_limiter.size();
}
size_t getRateLimiterSize() const { return address_rate_limiter.size(); }
size_t getPartialCount() const {
return partial_messages.size();
}
size_t getPartialCount() const { return partial_messages.size(); }
private:
struct PartialMessage;
/***************
@@ -515,23 +505,26 @@ private:
*/
void sendRequest(const Sp<Request>& request);
struct MessageStats {
unsigned ping {0};
unsigned find {0};
unsigned get {0};
unsigned put {0};
unsigned listen {0};
unsigned refresh {0};
unsigned updateValue {0};
struct MessageStats
{
unsigned ping {0};
unsigned find {0};
unsigned get {0};
unsigned put {0};
unsigned listen {0};
unsigned refresh {0};
unsigned updateValue {0};
};
// basic wrapper for socket sendto function
int send(const SockAddr& addr, const char *buf, size_t len, bool confirmed = false);
int send(const SockAddr& addr, const char* buf, size_t len, bool confirmed = false);
void sendValueParts(Tid tid, const std::vector<Blob>& svals, const SockAddr& addr);
std::vector<Blob> packValueHeader(msgpack::sbuffer&, std::vector<Sp<Value>>::const_iterator, std::vector<Sp<Value>>::const_iterator) const;
std::vector<Blob> packValueHeader(msgpack::sbuffer& buf, const std::vector<Sp<Value>>& values) const {
std::vector<Blob> packValueHeader(msgpack::sbuffer&,
std::vector<Sp<Value>>::const_iterator,
std::vector<Sp<Value>>::const_iterator) const;
std::vector<Blob> packValueHeader(msgpack::sbuffer& buf, const std::vector<Sp<Value>>& values) const
{
return packValueHeader(buf, values.begin(), values.end());
}
void maintainRxBuffer(Tid tid);
@@ -543,29 +536,22 @@ private:
void sendPong(const SockAddr& addr, Tid tid);
/* answer to findnodes/getvalues request */
void sendNodesValues(const SockAddr& addr,
Tid tid,
const Blob& nodes,
const Blob& nodes6,
const std::vector<Sp<Value>>& st,
const Query& query,
const Blob& token);
Tid tid,
const Blob& nodes,
const Blob& nodes6,
const std::vector<Sp<Value>>& st,
const Query& query,
const Blob& token);
Blob bufferNodes(sa_family_t af, const InfoHash& id, std::vector<Sp<Node>>& nodes);
std::pair<Blob, Blob> bufferNodes(sa_family_t af,
const InfoHash& id,
want_t want,
std::vector<Sp<Node>>& nodes,
std::vector<Sp<Node>>& nodes6);
std::pair<Blob, Blob> bufferNodes(
sa_family_t af, const InfoHash& id, want_t want, std::vector<Sp<Node>>& nodes, std::vector<Sp<Node>>& nodes6);
/* answer to a listen request */
void sendListenConfirmation(const SockAddr& addr, Tid tid);
/* answer to put request */
void sendValueAnnounced(const SockAddr& addr, Tid, Value::Id);
/* answer in case of error */
void sendError(const SockAddr& addr,
Tid tid,
uint16_t code,
const std::string& message,
bool include_id=false);
void sendError(const SockAddr& addr, Tid tid, uint16_t code, const std::string& message, bool include_id = false);
void deserializeNodes(ParsedMessage& msg, const SockAddr& from);
+2 -16
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "def.h"
+32 -44
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "infohash.h" // includes socket structures
@@ -36,28 +21,28 @@ struct RequestAnswer;
using Tid = uint32_t;
using SocketCb = std::function<void(const Sp<Node>&, net::RequestAnswer&&)>;
struct Socket {
struct Socket
{
Socket() {}
Socket(SocketCb&& on_receive) :
on_receive(std::move(on_receive)) {}
Socket(SocketCb&& on_receive)
: on_receive(std::move(on_receive))
{}
SocketCb on_receive {};
};
struct Node {
struct Node
{
const InfoHash id;
Node(const InfoHash& id, const SockAddr& addr, std::mt19937_64& rd, bool client=false);
Node(const InfoHash& id, SockAddr&& addr, std::mt19937_64& rd, bool client=false);
Node(const InfoHash& id, const SockAddr& addr, std::mt19937_64& rd, bool client = false);
Node(const InfoHash& id, SockAddr&& addr, std::mt19937_64& rd, bool client = false);
Node(const InfoHash& id, const sockaddr* sa, socklen_t salen, std::mt19937_64& rd)
: Node(id, SockAddr(sa, salen), rd) {}
: Node(id, SockAddr(sa, salen), rd)
{}
const InfoHash& getId() const {
return id;
}
const InfoHash& getId() const { return id; }
const SockAddr& getAddr() const { return addr; }
std::string getAddrStr() const {
return addr.toString();
}
std::string getAddrStr() const { return addr.toString(); }
bool isClient() const { return is_client; }
bool isIncoming() { return time > reply_time; }
@@ -70,7 +55,8 @@ struct Node {
* to MAX_AUTH_ERRORS errors are accepted in order to let the node recover.
* Upon this limit, the node expires.
*/
void authError() {
void authError()
{
if (++auth_errors > MAX_AUTH_ERRORS)
setExpired();
}
@@ -81,14 +67,11 @@ struct Node {
bool isPendingMessage() const;
size_t getPendingMessageCount() const;
bool isOld(const time_point& now) const {
return time + NODE_EXPIRE_TIME < now;
}
bool isRemovable(const time_point& now) const {
return isExpired() and isOld(now);
}
bool isOld(const time_point& now) const { return time + NODE_EXPIRE_TIME < now; }
bool isRemovable(const time_point& now) const { return isExpired() and isOld(now); }
NodeExport exportNode() const {
NodeExport exportNode() const
{
NodeExport ne;
ne.id = id;
ne.addr = addr;
@@ -128,21 +111,26 @@ struct Node {
/**
* Resets the state of the node so it's not expired anymore.
*/
void reset() { expired_ = false; reply_time = time_point::min(); }
void reset()
{
expired_ = false;
reply_time = time_point::min();
}
/**
* Generates a new request id, skipping the invalid id.
*
* @return the new id.
*/
Tid getNewTid() {
Tid getNewTid()
{
++transaction_id;
return transaction_id ? transaction_id : ++transaction_id;
}
std::string toString() const;
OPENDHT_PUBLIC friend std::ostream& operator<< (std::ostream& s, const Node& h);
OPENDHT_PUBLIC friend std::ostream& operator<<(std::ostream& s, const Node& h);
static constexpr const std::chrono::minutes NODE_GOOD_TIME {120};
@@ -158,8 +146,8 @@ private:
SockAddr addr;
bool is_client {false};
time_point time {time_point::min()}; /* last time eared about */
time_point reply_time {time_point::min()}; /* time of last correct reply received */
time_point time {time_point::min()}; /* last time eared about */
time_point reply_time {time_point::min()}; /* time of last correct reply received */
unsigned auth_errors {0};
bool expired_ {false};
Tid transaction_id;
@@ -169,4 +157,4 @@ private:
std::map<Tid, Sp<Socket>> sockets_;
};
}
} // namespace dht
+15 -29
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "node.h"
@@ -24,16 +9,13 @@
namespace dht {
struct NodeCache {
size_t size(sa_family_t family) const {
return cache(family).count();
}
size_t size() const {
return size(AF_INET) + size(AF_INET6);
}
struct NodeCache
{
size_t size(sa_family_t family) const { return cache(family).count(); }
size_t size() const { return size(AF_INET) + size(AF_INET6); }
Sp<Node> getNode(const InfoHash& id, sa_family_t family);
Sp<Node> getNode(const InfoHash& id, const SockAddr&, time_point now, bool confirmed, bool client=false);
Sp<Node> getNode(const InfoHash& id, const SockAddr&, time_point now, bool confirmed, bool client = false);
std::vector<Sp<Node>> getCachedNodes(const InfoHash& id, sa_family_t sa_f, size_t count) const;
/**
@@ -43,19 +25,23 @@ struct NodeCache {
*/
void clearBadNodes(sa_family_t family = 0);
NodeCache(std::mt19937_64& r) : rd(r) {};
NodeCache(std::mt19937_64& r)
: rd(r) {};
~NodeCache();
private:
class NodeMap : private std::map<InfoHash, std::weak_ptr<Node>> {
class NodeMap : private std::map<InfoHash, std::weak_ptr<Node>>
{
public:
Sp<Node> getNode(const InfoHash& id);
Sp<Node> getNode(const InfoHash& id, const SockAddr&, time_point now, bool confirmed, bool client, std::mt19937_64& rd);
Sp<Node> getNode(
const InfoHash& id, const SockAddr&, time_point now, bool confirmed, bool client, std::mt19937_64& rd);
std::vector<Sp<Node>> getCachedNodes(const InfoHash& id, size_t count) const;
void clearBadNodes();
void setExpired();
void cleanup();
size_t count() const { return size(); }
private:
size_t cleanup_counter {0};
};
@@ -67,4 +53,4 @@ private:
std::mt19937_64& rd;
};
}
} // namespace dht
+8 -21
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "def.h"
@@ -25,11 +11,12 @@
namespace dht {
using namespace std::literals;
struct OPENDHT_PUBLIC NodeExport {
struct OPENDHT_PUBLIC NodeExport
{
InfoHash id;
SockAddr addr;
template <typename Packer>
template<typename Packer>
void msgpack_pack(Packer& pk) const
{
pk.pack_map(2);
@@ -37,12 +24,12 @@ struct OPENDHT_PUBLIC NodeExport {
pk.pack(id);
pk.pack("addr"sv);
pk.pack_bin(addr.getLength());
pk.pack_bin_body((const char*)addr.get(), (size_t)addr.getLength());
pk.pack_bin_body((const char*) addr.get(), (size_t) addr.getLength());
}
void msgpack_unpack(msgpack::object o);
OPENDHT_PUBLIC friend std::ostream& operator<< (std::ostream& s, const NodeExport& h);
OPENDHT_PUBLIC friend std::ostream& operator<<(std::ostream& s, const NodeExport& h);
};
}
} // namespace dht
+14 -30
View File
@@ -1,21 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
* Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "def.h"
@@ -37,11 +21,11 @@ class OPENDHT_PUBLIC PeerDiscovery
{
public:
static constexpr in_port_t DEFAULT_PORT = 8888;
using ServiceDiscoveredCallback = std::function<void(msgpack::object &&, SockAddr &&)>;
using ServiceDiscoveredCallback = std::function<void(msgpack::object&&, SockAddr&&)>;
PeerDiscovery(in_port_t port = DEFAULT_PORT,
std::shared_ptr<asio::io_context> ioContext = {},
std::shared_ptr<Logger> logger = {});
std::shared_ptr<asio::io_context> ioContext = {},
std::shared_ptr<Logger> logger = {});
~PeerDiscovery();
/**
@@ -50,21 +34,21 @@ public:
*/
void startDiscovery(std::string_view type, ServiceDiscoveredCallback callback);
template <typename T>
void startDiscovery(std::string_view type, std::function<void(T &&, SockAddr &&)> cb) {
startDiscovery(type, [cb](msgpack::object &&ob, SockAddr &&addr) {
cb(ob.as<T>(), std::move(addr));
});
template<typename T>
void startDiscovery(std::string_view type, std::function<void(T&&, SockAddr&&)> cb)
{
startDiscovery(type, [cb](msgpack::object&& ob, SockAddr&& addr) { cb(ob.as<T>(), std::move(addr)); });
}
/**
* startPublish - Keeping sending data until node is joinned or stop is called
*/
void startPublish(std::string_view type, const msgpack::sbuffer &pack_buf);
void startPublish(sa_family_t domain, std::string_view type, const msgpack::sbuffer &pack_buf);
void startPublish(std::string_view type, const msgpack::sbuffer& pack_buf);
void startPublish(sa_family_t domain, std::string_view type, const msgpack::sbuffer& pack_buf);
template <typename T>
void startPublish(std::string_view type, const T &object) {
template<typename T>
void startPublish(std::string_view type, const T& object)
{
msgpack::sbuffer buf;
msgpack::pack(buf, object);
startPublish(type, buf);
+5 -19
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include <chrono>
@@ -22,9 +8,9 @@ namespace dht {
namespace proxy {
constexpr const std::chrono::hours OP_TIMEOUT {24}; // one day
constexpr const std::chrono::hours OP_MARGIN {2}; // two hours
constexpr const std::chrono::hours OP_MARGIN {2}; // two hours
using ListenToken = uint64_t;
}
}
} // namespace proxy
} // namespace dht
+14 -25
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "utils.h"
@@ -22,20 +7,25 @@
namespace dht {
class RateLimiter {
class RateLimiter
{
public:
RateLimiter(size_t quota, const duration& period = std::chrono::seconds(1))
: quota_(quota), period_(period) {}
: quota_(quota)
, period_(period)
{}
/** Clear outdated records and return current quota usage */
size_t maintain(const time_point& now) {
size_t maintain(const time_point& now)
{
auto limit = now - period_;
while (not records.empty() and records.front() < limit)
records.pop();
return records.size();
}
/** Return false if quota is reached, insert record and return true otherwise. */
bool limit(const time_point& now) {
bool limit(const time_point& now)
{
if (quota_ == std::numeric_limits<size_t>::max())
return true;
if (maintain(now) >= quota_)
@@ -43,13 +33,12 @@ public:
records.emplace(now);
return true;
}
bool empty() const {
return records.empty();
}
bool empty() const { return records.empty(); }
private:
const size_t quota_;
const duration period_;
std::queue<time_point> records {};
};
}
} // namespace dht
+19 -31
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include <random>
@@ -26,14 +11,16 @@
namespace dht {
namespace crypto {
/**
/**
* Generate a seeded random engine.
*/
template<class T = std::mt19937, std::size_t N = T::state_size+1>
auto getSeededRandomEngine() -> typename std::enable_if<!!N, T>::type {
template<class T = std::mt19937, std::size_t N = T::state_size + 1>
auto
getSeededRandomEngine() -> typename std::enable_if<!!N, T>::type
{
std::array<typename T::result_type, N> random_data;
constexpr auto gen = [](std::random_device& source) -> typename T::result_type {
for (unsigned j=0; j<64; j++) {
for (unsigned j = 0; j < 64; j++) {
try {
return source();
} catch (...) {
@@ -42,14 +29,13 @@ auto getSeededRandomEngine() -> typename std::enable_if<!!N, T>::type {
}
throw std::runtime_error("Can't generate random number");
};
for (unsigned i=0; i<8; i++) {
for (unsigned i = 0; i < 8; i++) {
try {
std::random_device source;
for (auto& r : random_data)
r = gen(source);
std::seed_seq seed(
(std::seed_seq::result_type*)random_data.data(),
(std::seed_seq::result_type*)(random_data.data() + random_data.size()));
std::seed_seq seed((std::seed_seq::result_type*) random_data.data(),
(std::seed_seq::result_type*) (random_data.data() + random_data.size()));
return T(seed);
} catch (...) {
std::this_thread::sleep_for(std::chrono::microseconds(500));
@@ -61,14 +47,16 @@ auto getSeededRandomEngine() -> typename std::enable_if<!!N, T>::type {
/**
* Generate a random engine from another source.
*/
template<class T = std::mt19937, std::size_t N = T::state_size+1>
auto getDerivedRandomEngine(T& source) -> typename std::enable_if<!!N, T>::type {
template<class T = std::mt19937, std::size_t N = T::state_size + 1>
auto
getDerivedRandomEngine(T& source) -> typename std::enable_if<!!N, T>::type
{
std::array<typename T::result_type, N> random_data;
std::generate(random_data.begin(), random_data.end(), std::ref(source));
std::seed_seq seed(
(std::seed_seq::result_type*)random_data.data(),
(std::seed_seq::result_type*)(random_data.data() + random_data.size()));
std::seed_seq seed((std::seed_seq::result_type*) random_data.data(),
(std::seed_seq::result_type*) (random_data.data() + random_data.size()));
return T(seed);
}
}} // dht::crypto
} // namespace crypto
} // namespace dht
+26 -31
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "node.h"
@@ -26,28 +11,37 @@ namespace net {
class NetworkEngine;
}
struct Bucket {
Bucket() : cached() {}
struct Bucket
{
Bucket()
: cached()
{}
Bucket(sa_family_t af, const InfoHash& f = {}, time_point t = time_point::min())
: af(af), first(f), time(t), cached() {}
: af(af)
, first(f)
, time(t)
, cached()
{}
sa_family_t af {0};
InfoHash first {};
time_point time {time_point::min()}; /* time of last reply in this bucket */
std::list<Sp<Node>> nodes {};
Sp<Node> cached; /* the address of a likely candidate */
Sp<Node> cached; /* the address of a likely candidate */
/** Return a random node in a bucket. */
Sp<Node> randomNode(std::mt19937_64& rd);
void sendCachedPing(net::NetworkEngine& ne);
void connectivityChanged() {
void connectivityChanged()
{
time = time_point::min();
for (auto& node : nodes)
node->setTime(time_point::min());
}
};
class RoutingTable : public std::list<Bucket> {
class RoutingTable : public std::list<Bucket>
{
public:
using std::list<Bucket>::list;
@@ -64,25 +58,26 @@ public:
/**
* Return true if the id is in the bucket's range.
*/
inline bool contains(const RoutingTable::const_iterator& bucket, const InfoHash& id) const {
inline bool contains(const RoutingTable::const_iterator& bucket, const InfoHash& id) const
{
return InfoHash::cmp(bucket->first, id) <= 0
&& (std::next(bucket) == end() || InfoHash::cmp(id, std::next(bucket)->first) < 0);
&& (std::next(bucket) == end() || InfoHash::cmp(id, std::next(bucket)->first) < 0);
}
/**
* Return true if the table has no bucket ore one empty buket.
*/
inline bool isEmpty() const {
return empty() || (size() == 1 && front().nodes.empty());
}
inline bool isEmpty() const { return empty() || (size() == 1 && front().nodes.empty()); }
void connectivityChanged(const time_point& now) {
void connectivityChanged(const time_point& now)
{
grow_time = now;
for (auto& b : *this)
b.connectivityChanged();
}
bool onNewNode(const Sp<Node>& node, int comfirm, const time_point& now, const InfoHash& myid, net::NetworkEngine& ne);
bool onNewNode(
const Sp<Node>& node, int comfirm, const time_point& now, const InfoHash& myid, net::NetworkEngine& ne);
/**
* Return a random id in the bucket's range.
@@ -97,4 +92,4 @@ public:
bool split(const RoutingTable::iterator& b);
};
}
} // namespace dht
+20 -29
View File
@@ -1,21 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "utils.h"
@@ -31,10 +15,15 @@ namespace dht {
* @details
* Maintains the timings upon which to execute a job.
*/
class Scheduler {
class Scheduler
{
public:
struct Job {
Job(std::function<void()>&& f, time_point t) : do_(std::move(f)), t_(t) {}
struct Job
{
Job(std::function<void()>&& f, time_point t)
: do_(std::move(f))
, t_(t)
{}
std::function<void()> do_;
const time_point t_;
void cancel() { do_ = {}; }
@@ -48,7 +37,8 @@ public:
*
* @return pointer to the newly scheduled job.
*/
Sp<Scheduler::Job> add(time_point t, std::function<void()>&& job_func) {
Sp<Scheduler::Job> add(time_point t, std::function<void()>&& job_func)
{
auto job = std::make_shared<Job>(std::move(job_func), t);
if (t != time_point::max())
timers.emplace(std::move(t), job);
@@ -61,7 +51,8 @@ public:
* @param job The job to edit.
* @param t The time at which the job shall be rescheduled.
*/
void edit(Sp<Scheduler::Job>& job, time_point t) {
void edit(Sp<Scheduler::Job>& job, time_point t)
{
if (not job)
return;
// std::function move doesn't garantee to leave the object empty.
@@ -71,7 +62,8 @@ public:
job = add(t, std::move(task));
}
bool cancel(Sp<Scheduler::Job>& job) {
bool cancel(Sp<Scheduler::Job>& job)
{
if (job) {
job->cancel();
for (auto r = timers.equal_range(job->t_); r.first != r.second; ++r.first) {
@@ -90,7 +82,8 @@ public:
*
* @return The time for the next job to run.
*/
time_point run() {
time_point run()
{
while (not timers.empty()) {
auto timer = timers.begin();
/*
@@ -110,9 +103,7 @@ public:
return getNextJobTime();
}
inline time_point getNextJobTime() const {
return timers.empty() ? time_point::max() : timers.begin()->first;
}
inline time_point getNextJobTime() const { return timers.empty() ? time_point::max() : timers.begin()->first; }
/**
* Accessors for the common time reference used for synchronizing
@@ -127,4 +118,4 @@ private:
std::multimap<time_point, Sp<Job>> timers {}; /* the jobs ordered by time */
};
}
} // namespace dht
+156 -200
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "dht.h"
@@ -27,9 +12,9 @@
namespace dht {
class OPENDHT_PUBLIC SecureDht final : public DhtInterface {
class OPENDHT_PUBLIC SecureDht final : public DhtInterface
{
public:
typedef std::function<void(bool)> SignatureCheckCallback;
using Config = SecureDhtConfig;
@@ -38,7 +23,7 @@ public:
{
auto c = conf.node_config;
if (not c.node_id and conf.id.second)
c.node_id = InfoHash::get("node:"+conf.id.second->getId().toString());
c.node_id = InfoHash::get("node:" + conf.id.second->getId().toString());
return c;
}
@@ -48,36 +33,37 @@ public:
* id: the identity to use for the crypto layer and to compute
* our own hash on the Dht.
*/
SecureDht(std::unique_ptr<DhtInterface> dht, Config config, IdentityAnnouncedCb iacb = {}, const std::shared_ptr<Logger>& l = {});
SecureDht(std::unique_ptr<DhtInterface> dht,
Config config,
IdentityAnnouncedCb iacb = {},
const std::shared_ptr<Logger>& l = {});
virtual ~SecureDht();
InfoHash getId() const {
return key_ ? key_->getPublicKey().getId() : InfoHash();
}
PkId getLongId() const {
return key_ ? key_->getPublicKey().getLongId() : PkId();
}
Sp<crypto::PublicKey> getPublicKey() const {
return key_ ? key_->getSharedPublicKey() : Sp<crypto::PublicKey>{};
}
InfoHash getId() const { return key_ ? key_->getPublicKey().getId() : InfoHash(); }
PkId getLongId() const { return key_ ? key_->getPublicKey().getLongId() : PkId(); }
Sp<crypto::PublicKey> getPublicKey() const { return key_ ? key_->getSharedPublicKey() : Sp<crypto::PublicKey> {}; }
ValueType secureType(ValueType&& type);
ValueType secureType(const ValueType& type) {
ValueType secureType(const ValueType& type)
{
ValueType tmp_type = type;
return secureType(std::move(tmp_type));
}
void registerType(const ValueType& type) override {
void registerType(const ValueType& type) override
{
if (dht_)
dht_->registerType(secureType(type));
}
void registerType(ValueType&& type) {
void registerType(ValueType&& type)
{
if (dht_)
dht_->registerType(secureType(std::forward<ValueType>(type)));
}
void registerInsecureType(const ValueType& type) {
void registerInsecureType(const ValueType& type)
{
if (dht_)
dht_->registerType(type);
}
@@ -87,14 +73,29 @@ public:
* If the signature is unable to be checked, or if the data is unable to be decrypted, it is not returned.
* Public, non-signed & non-encrypted data is retransmitted as-is.
*/
void get(const InfoHash& id, GetCallback cb, DoneCallback donecb={}, Value::Filter&& = {}, Where&& w = {}) override;
void get(const InfoHash& id, GetCallback cb, DoneCallbackSimple donecb={}, Value::Filter&& f = {}, Where&& w = {}) override {
void get(const InfoHash& id, GetCallback cb, DoneCallback donecb = {}, Value::Filter&& = {}, Where&& w = {}) override;
void get(const InfoHash& id,
GetCallback cb,
DoneCallbackSimple donecb = {},
Value::Filter&& f = {},
Where&& w = {}) override
{
get(id, cb, bindDoneCb(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
}
void get(const InfoHash& key, GetCallbackSimple cb, DoneCallback donecb={}, Value::Filter&& f={}, Where&& w = {}) override {
void get(const InfoHash& key,
GetCallbackSimple cb,
DoneCallback donecb = {},
Value::Filter&& f = {},
Where&& w = {}) override
{
get(key, bindGetCb(cb), donecb, std::forward<Value::Filter>(f), std::forward<Where>(w));
}
void get(const InfoHash& key, GetCallbackSimple cb, DoneCallbackSimple donecb, Value::Filter&& f={}, Where&& w = {}) override {
void get(const InfoHash& key,
GetCallbackSimple cb,
DoneCallbackSimple donecb,
Value::Filter&& f = {},
Where&& w = {}) override
{
get(key, bindGetCb(cb), bindDoneCb(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
}
@@ -102,7 +103,8 @@ public:
* Will take ownership of the value, sign it using our private key and put it in the DHT.
*/
void putSigned(const InfoHash& hash, Sp<Value> val, DoneCallback callback, bool permanent = false);
void putSigned(const InfoHash& hash, Value&& v, DoneCallback callback, bool permanent = false) {
void putSigned(const InfoHash& hash, Value&& v, DoneCallback callback, bool permanent = false)
{
putSigned(hash, std::make_shared<Value>(std::move(v)), callback, permanent);
}
@@ -111,16 +113,24 @@ public:
* and put it in the DHT.
* The operation will be immediate if the recipient' public key is known (otherwise it will be retrived first).
*/
void putEncrypted(const InfoHash& hash, const InfoHash& to, Sp<Value> val, DoneCallback callback, bool permanent = false);
void putEncrypted(
const InfoHash& hash, const InfoHash& to, Sp<Value> val, DoneCallback callback, bool permanent = false);
[[deprecated("Use the shared_ptr version instead")]]
void putEncrypted(const InfoHash& hash, const InfoHash& to, Value&& v, DoneCallback callback, bool permanent = false) {
void putEncrypted(const InfoHash& hash, const InfoHash& to, Value&& v, DoneCallback callback, bool permanent = false)
{
putEncrypted(hash, to, std::make_shared<Value>(std::move(v)), callback, permanent);
}
void putEncrypted(const InfoHash& hash, const crypto::PublicKey& to, Sp<Value> val, DoneCallback callback, bool permanent = false);
void putEncrypted(const InfoHash& hash,
const crypto::PublicKey& to,
Sp<Value> val,
DoneCallback callback,
bool permanent = false);
[[deprecated("Use the shared_ptr version instead")]]
void putEncrypted(const InfoHash& hash, const crypto::PublicKey& to, Value&& v, DoneCallback callback, bool permanent = false) {
void putEncrypted(
const InfoHash& hash, const crypto::PublicKey& to, Value&& v, DoneCallback callback, bool permanent = false)
{
putEncrypted(hash, to, std::make_shared<Value>(std::move(v)), callback, permanent);
}
@@ -154,201 +164,142 @@ public:
* The search key used is the public key ID, so there may be multiple certificates retured, signed with
* the same private key.
*/
void setLocalCertificateStore(CertificateStoreQueryLegacy&& query_method) {
void setLocalCertificateStore(CertificateStoreQueryLegacy&& query_method)
{
localQueryMethodLegacy_ = std::move(query_method);
}
void setLocalCertificateStore(CertificateStoreQuery&& query_method) {
localQueryMethod_ = std::move(query_method);
}
void setOnPublicAddressChanged(PublicAddressChangedCb cb) override {
dht_->setOnPublicAddressChanged(cb);
}
void setLocalCertificateStore(CertificateStoreQuery&& query_method) { localQueryMethod_ = std::move(query_method); }
void setOnPublicAddressChanged(PublicAddressChangedCb cb) override { dht_->setOnPublicAddressChanged(cb); }
/**
* SecureDht to Dht proxy
*/
void shutdown(ShutdownCallback cb, bool stop = false) override {
dht_->shutdown(cb, stop);
}
void dumpTables() const override {
dht_->dumpTables();
}
void shutdown(ShutdownCallback cb, bool stop = false) override { dht_->shutdown(cb, stop); }
void dumpTables() const override { dht_->dumpTables(); }
inline const InfoHash& getNodeId() const override { return dht_->getNodeId(); }
std::pair<size_t, size_t> getStoreSize() const override {
return dht_->getStoreSize();
}
std::string getStorageLog() const override {
return dht_->getStorageLog();
}
std::string getStorageLog(const InfoHash& h) const override {
return dht_->getStorageLog(h);
}
void setStorageLimit(size_t limit = DEFAULT_STORAGE_LIMIT) override {
dht_->setStorageLimit(limit);
}
size_t getStorageLimit() const override {
return dht_->getStorageLimit();
}
std::pair<size_t, size_t> getStoreSize() const override { return dht_->getStoreSize(); }
std::string getStorageLog() const override { return dht_->getStorageLog(); }
std::string getStorageLog(const InfoHash& h) const override { return dht_->getStorageLog(h); }
void setStorageLimit(size_t limit = DEFAULT_STORAGE_LIMIT) override { dht_->setStorageLimit(limit); }
size_t getStorageLimit() const override { return dht_->getStorageLimit(); }
std::vector<NodeExport> exportNodes() const override {
return dht_->exportNodes();
}
std::vector<ValuesExport> exportValues() const override {
return dht_->exportValues();
}
void importValues(const std::vector<ValuesExport>& v) override {
dht_->importValues(v);
}
NodeStats getNodesStats(sa_family_t af) const override {
return dht_->getNodesStats(af);
}
std::vector<unsigned> getNodeMessageStats(bool in = false) override {
return dht_->getNodeMessageStats(in);
}
std::string getRoutingTablesLog(sa_family_t af) const override {
return dht_->getRoutingTablesLog(af);
}
std::string getSearchesLog(sa_family_t af) const override {
return dht_->getSearchesLog(af);
}
std::string getSearchLog(const InfoHash& h, sa_family_t af = AF_UNSPEC) const override {
std::vector<NodeExport> exportNodes() const override { return dht_->exportNodes(); }
std::vector<ValuesExport> exportValues() const override { return dht_->exportValues(); }
void importValues(const std::vector<ValuesExport>& v) override { dht_->importValues(v); }
NodeStats getNodesStats(sa_family_t af) const override { return dht_->getNodesStats(af); }
std::vector<unsigned> getNodeMessageStats(bool in = false) override { return dht_->getNodeMessageStats(in); }
std::string getRoutingTablesLog(sa_family_t af) const override { return dht_->getRoutingTablesLog(af); }
std::string getSearchesLog(sa_family_t af) const override { return dht_->getSearchesLog(af); }
std::string getSearchLog(const InfoHash& h, sa_family_t af = AF_UNSPEC) const override
{
return dht_->getSearchLog(h, af);
}
std::vector<SockAddr> getPublicAddress(sa_family_t family = 0) override {
return dht_->getPublicAddress(family);
}
time_point periodic(const uint8_t *buf, size_t buflen, SockAddr sa, const time_point& now) override {
std::vector<SockAddr> getPublicAddress(sa_family_t family = 0) override { return dht_->getPublicAddress(family); }
time_point periodic(const uint8_t* buf, size_t buflen, SockAddr sa, const time_point& now) override
{
return dht_->periodic(buf, buflen, std::move(sa), now);
}
time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now) override {
time_point periodic(
const uint8_t* buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now) override
{
return dht_->periodic(buf, buflen, from, fromlen, now);
}
NodeStatus updateStatus(sa_family_t af) override {
return dht_->updateStatus(af);
}
NodeStatus getStatus(sa_family_t af) const override {
return dht_->getStatus(af);
}
NodeStatus getStatus() const override {
return dht_->getStatus();
}
net::DatagramSocket* getSocket() const override {
return dht_->getSocket();
};
bool isRunning(sa_family_t af = 0) const override {
return dht_->isRunning(af);
}
const ValueType& getType(ValueType::Id type_id) const override {
return dht_->getType(type_id);
}
void addBootstrap(const std::string& host, const std::string& service) override {
NodeStatus updateStatus(sa_family_t af) override { return dht_->updateStatus(af); }
NodeStatus getStatus(sa_family_t af) const override { return dht_->getStatus(af); }
NodeStatus getStatus() const override { return dht_->getStatus(); }
net::DatagramSocket* getSocket() const override { return dht_->getSocket(); };
bool isRunning(sa_family_t af = 0) const override { return dht_->isRunning(af); }
const ValueType& getType(ValueType::Id type_id) const override { return dht_->getType(type_id); }
void addBootstrap(const std::string& host, const std::string& service) override
{
dht_->addBootstrap(host, service);
}
void clearBootstrap() override {
dht_->clearBootstrap();
}
void insertNode(const InfoHash& id, const SockAddr& sa) override {
dht_->insertNode(id, sa);
}
void insertNode(const NodeExport& n) override {
dht_->insertNode(n);
}
void pingNode(SockAddr sa, DoneCallbackSimple&& cb={}) override {
dht_->pingNode(std::move(sa), std::move(cb));
}
void query(const InfoHash& key, QueryCallback cb, DoneCallback done_cb = {}, Query&& q = {}) override {
void clearBootstrap() override { dht_->clearBootstrap(); }
void insertNode(const InfoHash& id, const SockAddr& sa) override { dht_->insertNode(id, sa); }
void insertNode(const NodeExport& n) override { dht_->insertNode(n); }
void pingNode(SockAddr sa, DoneCallbackSimple&& cb = {}) override { dht_->pingNode(std::move(sa), std::move(cb)); }
void query(const InfoHash& key, QueryCallback cb, DoneCallback done_cb = {}, Query&& q = {}) override
{
dht_->query(key, cb, done_cb, std::move(q));
}
void query(const InfoHash& key, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query&& q = {}) override {
void query(const InfoHash& key, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query&& q = {}) override
{
dht_->query(key, cb, done_cb, std::move(q));
}
std::vector<Sp<Value>> getLocal(const InfoHash& key, const Value::Filter& f = {}) const override {
std::vector<Sp<Value>> getLocal(const InfoHash& key, const Value::Filter& f = {}) const override
{
return dht_->getLocal(key, f);
}
Sp<Value> getLocalById(const InfoHash& key, Value::Id vid) const override {
return dht_->getLocalById(key, vid);
}
Sp<Value> getLocalById(const InfoHash& key, Value::Id vid) const override { return dht_->getLocalById(key, vid); }
void put(const InfoHash& key,
Sp<Value> v,
DoneCallback cb=nullptr,
time_point created=time_point::max(),
bool permanent = false) override
Sp<Value> v,
DoneCallback cb = nullptr,
time_point created = time_point::max(),
bool permanent = false) override
{
dht_->put(key, v, cb, created, permanent);
}
void put(const InfoHash& key,
const Sp<Value>& v,
DoneCallbackSimple cb,
time_point created=time_point::max(),
bool permanent = false) override
const Sp<Value>& v,
DoneCallbackSimple cb,
time_point created = time_point::max(),
bool permanent = false) override
{
dht_->put(key, v, cb, created, permanent);
}
void put(const InfoHash& key,
Value&& v,
DoneCallback cb=nullptr,
time_point created=time_point::max(),
bool permanent = false) override
Value&& v,
DoneCallback cb = nullptr,
time_point created = time_point::max(),
bool permanent = false) override
{
dht_->put(key, std::move(v), cb, created, permanent);
}
void put(const InfoHash& key,
Value&& v,
DoneCallbackSimple cb,
time_point created=time_point::max(),
bool permanent = false) override
Value&& v,
DoneCallbackSimple cb,
time_point created = time_point::max(),
bool permanent = false) override
{
dht_->put(key, std::move(v), cb, created, permanent);
}
std::vector<Sp<Value>> getPut(const InfoHash& h) const override {
return dht_->getPut(h);
}
Sp<Value> getPut(const InfoHash& h, const Value::Id& vid) const override {
return dht_->getPut(h, vid);
}
bool cancelPut(const InfoHash& h, const Value::Id& vid) override {
return dht_->cancelPut(h, vid);
}
std::vector<Sp<Value>> getPut(const InfoHash& h) const override { return dht_->getPut(h); }
Sp<Value> getPut(const InfoHash& h, const Value::Id& vid) const override { return dht_->getPut(h, vid); }
bool cancelPut(const InfoHash& h, const Value::Id& vid) override { return dht_->cancelPut(h, vid); }
size_t listen(const InfoHash& key, ValueCallback, Value::Filter={}, Where={}) override;
size_t listen(const InfoHash& key, ValueCallback, Value::Filter = {}, Where = {}) override;
size_t listen(const InfoHash& key, GetCallback cb, Value::Filter = {}, Where w = {}) override;
size_t listen(const InfoHash& key, GetCallbackSimple cb, Value::Filter f={}, Where w = {}) override {
size_t listen(const InfoHash& key, GetCallbackSimple cb, Value::Filter f = {}, Where w = {}) override
{
return listen(key, bindGetCb(cb), f, w);
}
bool cancelListen(const InfoHash& h, size_t token) override {
return dht_->cancelListen(h, token);
}
void connectivityChanged(sa_family_t af) override {
dht_->connectivityChanged(af);
}
void connectivityChanged() override {
dht_->connectivityChanged();
}
bool cancelListen(const InfoHash& h, size_t token) override { return dht_->cancelListen(h, token); }
void connectivityChanged(sa_family_t af) override { dht_->connectivityChanged(af); }
void connectivityChanged() override { dht_->connectivityChanged(); }
void forwardAllMessages(bool forward) {
forward_all_ = forward;
}
void forwardAllMessages(bool forward) { forward_all_ = forward; }
void setPushNotificationToken(const std::string& token = "") override {
dht_->setPushNotificationToken(token);
}
void setPushNotificationToken(const std::string& token = "") override { dht_->setPushNotificationToken(token); }
/**
* Call linked callback with push_notification
* @param notification to process
*/
PushNotificationResult pushNotificationReceived(const std::map<std::string, std::string>& notification) override {
PushNotificationResult pushNotificationReceived(const std::map<std::string, std::string>& notification) override
{
return dht_->pushNotificationReceived(notification);
}
void setLogger(const Logger& logger) override {
void setLogger(const Logger& logger) override
{
DhtInterface::setLogger(logger);
dht_->setLogger(logger);
}
void setLogger(const std::shared_ptr<Logger>& logger) override {
void setLogger(const std::shared_ptr<Logger>& logger) override
{
DhtInterface::setLogger(logger);
dht_->setLogger(logger);
}
@@ -356,7 +307,8 @@ public:
/**
* Only print logs related to the given InfoHash (if given), or disable filter (if zeroes).
*/
void setLogFilter(const InfoHash& f) override {
void setLogFilter(const InfoHash& f) override
{
DhtInterface::setLogFilter(f);
dht_->setLogFilter(f);
}
@@ -391,23 +343,27 @@ private:
bool enableCache_ {false};
};
const ValueType CERTIFICATE_TYPE = {
8, "Certificate", std::chrono::hours(24 * 7),
// A certificate can only be stored at its public key ID.
[](InfoHash id, Sp<Value>& v, const InfoHash&, const SockAddr&) {
try {
crypto::Certificate crt(v->data);
// TODO check certificate signature
return crt.getPublicKey().getId() == id || InfoHash::get(crt.getPublicKey().getLongId()) == id;
} catch (const std::exception& e) {}
return false;
},
[](InfoHash, const Sp<Value>& o, Sp<Value>& n, const InfoHash&, const SockAddr&) {
try {
return crypto::Certificate(o->data).getPublicKey().getLongId() == crypto::Certificate(n->data).getPublicKey().getLongId();
} catch (const std::exception& e) {}
return false;
}
};
const ValueType CERTIFICATE_TYPE = {8,
"Certificate",
std::chrono::hours(24 * 7),
// A certificate can only be stored at its public key ID.
[](InfoHash id, Sp<Value>& v, const InfoHash&, const SockAddr&) {
try {
crypto::Certificate crt(v->data);
// TODO check certificate signature
return crt.getPublicKey().getId() == id
|| InfoHash::get(crt.getPublicKey().getLongId()) == id;
} catch (const std::exception& e) {
}
return false;
},
[](InfoHash, const Sp<Value>& o, Sp<Value>& n, const InfoHash&, const SockAddr&) {
try {
return crypto::Certificate(o->data).getPublicKey().getLongId()
== crypto::Certificate(n->data).getPublicKey().getLongId();
} catch (const std::exception& e) {
}
return false;
}};
}
} // namespace dht
+91 -92
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "def.h"
@@ -58,30 +43,34 @@ OPENDHT_PUBLIC std::string print_addr(const sockaddr_storage& ss, socklen_t ssle
/**
* A Socket Address (sockaddr*), with abstraction for IPv4, IPv6 address families.
*/
class OPENDHT_PUBLIC SockAddr {
class OPENDHT_PUBLIC SockAddr
{
public:
SockAddr() {}
SockAddr(const SockAddr& o) {
set(o.get(), o.getLength());
}
SockAddr(SockAddr&& o) noexcept : addr(std::move(o.addr)), len(o.len) {
SockAddr(const SockAddr& o) { set(o.get(), o.getLength()); }
SockAddr(SockAddr&& o) noexcept
: addr(std::move(o.addr))
, len(o.len)
{
o.len = 0;
}
/**
* Build from existing address.
*/
SockAddr(const sockaddr* sa, socklen_t length) {
SockAddr(const sockaddr* sa, socklen_t length)
{
if (length > static_cast<socklen_t>(sizeof(sockaddr_storage)))
throw std::runtime_error("Socket address length is too large");
set(sa, length);
}
SockAddr(const sockaddr* sa) {
SockAddr(const sockaddr* sa)
{
socklen_t len = 0;
if (sa) {
if (sa->sa_family == AF_INET)
len = sizeof(sockaddr_in);
else if(sa->sa_family == AF_INET6)
else if (sa->sa_family == AF_INET6)
len = sizeof(sockaddr_in6);
else
throw std::runtime_error("Unknown address family");
@@ -92,34 +81,37 @@ public:
/**
* Build from an existing sockaddr_storage structure.
*/
SockAddr(const sockaddr_storage& ss, socklen_t len) : SockAddr((const sockaddr*)&ss, len) {}
SockAddr(const sockaddr_storage& ss, socklen_t len)
: SockAddr((const sockaddr*) &ss, len)
{}
static std::vector<SockAddr> resolve(const std::string& host, const std::string& service = {});
bool operator<(const SockAddr& o) const {
bool operator<(const SockAddr& o) const
{
if (len != o.len)
return len < o.len;
return std::memcmp((const uint8_t*)get(), (const uint8_t*)o.get(), len) < 0;
return std::memcmp((const uint8_t*) get(), (const uint8_t*) o.get(), len) < 0;
}
bool equals(const SockAddr& o) const {
return len == o.len
&& std::memcmp((const uint8_t*)get(), (const uint8_t*)o.get(), len) == 0;
bool equals(const SockAddr& o) const
{
return len == o.len && std::memcmp((const uint8_t*) get(), (const uint8_t*) o.get(), len) == 0;
}
SockAddr& operator=(const SockAddr& o) {
SockAddr& operator=(const SockAddr& o)
{
set(o.get(), o.getLength());
return *this;
}
SockAddr& operator=(SockAddr&& o) {
SockAddr& operator=(SockAddr&& o)
{
len = o.len;
o.len = 0;
addr = std::move(o.addr);
return *this;
}
std::string toString() const {
return print_addr(get(), getLength());
}
std::string toString() const { return print_addr(get(), getLength()); }
/**
* Returns the address family or AF_UNSPEC if the address is not set.
@@ -131,9 +123,10 @@ public:
* in which case the sockaddr structure is cleared to zero,
* and set the address family field (sa_family).
*/
void setFamily(sa_family_t af) {
void setFamily(sa_family_t af)
{
socklen_t new_length;
switch(af) {
switch (af) {
case AF_INET:
new_length = sizeof(sockaddr_in);
break;
@@ -145,8 +138,10 @@ public:
}
if (new_length != len) {
len = new_length;
if (len) addr.reset((sockaddr*)::calloc(len, 1));
else addr.reset();
if (len)
addr.reset((sockaddr*) ::calloc(len, 1));
else
addr.reset();
}
if (len)
addr->sa_family = af;
@@ -155,9 +150,10 @@ public:
/**
* Set address to any
*/
void setAny() {
void setAny()
{
auto family = getFamily();
switch(family) {
switch (family) {
case AF_INET:
getIPv4().sin_addr.s_addr = htonl(INADDR_ANY);
break;
@@ -170,9 +166,10 @@ public:
/**
* Set address to loopback.
*/
void setLoopback() {
void setLoopback()
{
auto family = getFamily();
switch(family) {
switch (family) {
case AF_INET:
getIPv4().sin_addr.s_addr = htonl(INADDR_LOOPBACK);
break;
@@ -186,8 +183,9 @@ public:
* Retreive the port (in host byte order) or 0 if the address is not
* of a supported family.
*/
in_port_t getPort() const {
switch(getFamily()) {
in_port_t getPort() const
{
switch (getFamily()) {
case AF_INET:
return ntohs(getIPv4().sin_port);
case AF_INET6:
@@ -200,8 +198,9 @@ public:
* Set the port. The address must be of a supported family.
* @param p The port in host byte order.
*/
void setPort(in_port_t p) {
switch(getFamily()) {
void setPort(in_port_t p)
{
switch (getFamily()) {
case AF_INET:
getIPv4().sin_port = htons(p);
break;
@@ -226,9 +225,7 @@ public:
/**
* An address is defined to be true if its length is not zero.
*/
explicit operator bool() const noexcept {
return len;
}
explicit operator bool() const noexcept { return len; }
/**
* Returns the address to the managed sockaddr structure.
@@ -242,24 +239,17 @@ public:
*/
sockaddr* get() { return addr.get(); }
inline const sockaddr_in& getIPv4() const {
return *reinterpret_cast<const sockaddr_in*>(get());
}
inline const sockaddr_in6& getIPv6() const {
return *reinterpret_cast<const sockaddr_in6*>(get());
}
inline sockaddr_in& getIPv4() {
return *reinterpret_cast<sockaddr_in*>(get());
}
inline sockaddr_in6& getIPv6() {
return *reinterpret_cast<sockaddr_in6*>(get());
}
inline const sockaddr_in& getIPv4() const { return *reinterpret_cast<const sockaddr_in*>(get()); }
inline const sockaddr_in6& getIPv6() const { return *reinterpret_cast<const sockaddr_in6*>(get()); }
inline sockaddr_in& getIPv4() { return *reinterpret_cast<sockaddr_in*>(get()); }
inline sockaddr_in6& getIPv6() { return *reinterpret_cast<sockaddr_in6*>(get()); }
/**
/**
* Releases the ownership of the managed object, if any.
* The caller is responsible for deleting the object with free().
*/
inline sockaddr* release() {
inline sockaddr* release()
{
len = 0;
return addr.release();
}
@@ -284,56 +274,65 @@ public:
* A comparator to classify IP addresses, only considering the
* first 64 bits in IPv6.
*/
struct ipCmp {
bool operator()(const SockAddr& a, const SockAddr& b) const {
struct ipCmp
{
bool operator()(const SockAddr& a, const SockAddr& b) const
{
if (a.len != b.len)
return a.len < b.len;
socklen_t start, len;
switch(a.getFamily()) {
case AF_INET:
start = offsetof(sockaddr_in, sin_addr);
len = sizeof(in_addr);
break;
case AF_INET6:
start = offsetof(sockaddr_in6, sin6_addr);
// don't consider more than 64 bits (IPv6)
len = 8;
break;
default:
start = 0;
len = a.len;
break;
switch (a.getFamily()) {
case AF_INET:
start = offsetof(sockaddr_in, sin_addr);
len = sizeof(in_addr);
break;
case AF_INET6:
start = offsetof(sockaddr_in6, sin6_addr);
// don't consider more than 64 bits (IPv6)
len = 8;
break;
default:
start = 0;
len = a.len;
break;
}
return std::memcmp((uint8_t*)a.get()+start,
(uint8_t*)b.get()+start, len) < 0;
return std::memcmp((uint8_t*) a.get() + start, (uint8_t*) b.get() + start, len) < 0;
}
};
friend std::ostream& operator<< (std::ostream& s, const SockAddr& h) {
friend std::ostream& operator<<(std::ostream& s, const SockAddr& h)
{
print_addr(s, h.get(), h.getLength());
return s;
}
private:
struct free_delete { void operator()(void* p) { ::free(p); } };
struct free_delete
{
void operator()(void* p) { ::free(p); }
};
std::unique_ptr<sockaddr, free_delete> addr {};
socklen_t len {0};
void set(const sockaddr* sa, socklen_t length) {
void set(const sockaddr* sa, socklen_t length)
{
if (len != length) {
len = length;
if (len) addr.reset((sockaddr*)::malloc(len));
else addr.reset();
if (len)
addr.reset((sockaddr*) ::malloc(len));
else
addr.reset();
}
if (len)
std::memcpy((uint8_t*)get(), (const uint8_t*)sa, len);
std::memcpy((uint8_t*) get(), (const uint8_t*) sa, len);
}
};
OPENDHT_PUBLIC bool operator==(const SockAddr& a, const SockAddr& b);
}
} // namespace dht
#if FMT_VERSION >= 90000
template <> struct fmt::formatter<dht::SockAddr> : ostream_formatter {};
template<>
struct fmt::formatter<dht::SockAddr> : ostream_formatter
{};
#endif
+37 -43
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "def.h"
@@ -29,7 +14,8 @@
namespace dht {
class OPENDHT_PUBLIC ThreadPool {
class OPENDHT_PUBLIC ThreadPool
{
public:
static ThreadPool& computation();
static ThreadPool& io();
@@ -41,7 +27,8 @@ public:
void run(std::function<void()>&& cb);
template<class T>
std::future<T> get(std::function<T()>&& cb) {
std::future<T> get(std::function<T()>&& cb)
{
auto ret = std::make_shared<std::promise<T>>();
run([cb = std::move(cb), ret]() mutable {
try {
@@ -49,13 +36,15 @@ public:
} catch (...) {
try {
ret->set_exception(std::current_exception());
} catch(...) {}
} catch (...) {
}
}
});
return ret->get_future();
}
template<class T>
std::shared_future<T> getShared(std::function<T()>&& cb) {
std::shared_future<T> getShared(std::function<T()>&& cb)
{
return get(std::move(cb));
}
@@ -79,10 +68,12 @@ private:
void threadEnded(std::thread&);
};
class OPENDHT_PUBLIC Executor : public std::enable_shared_from_this<Executor> {
class OPENDHT_PUBLIC Executor : public std::enable_shared_from_this<Executor>
{
public:
Executor(ThreadPool& pool, unsigned maxConcurrent = 1)
: threadPool_(pool), maxConcurrent_(maxConcurrent)
: threadPool_(pool)
, maxConcurrent_(maxConcurrent)
{}
void run(std::function<void()>&& task);
@@ -98,32 +89,31 @@ private:
void schedule();
};
class OPENDHT_PUBLIC ExecutionContext {
class OPENDHT_PUBLIC ExecutionContext
{
public:
ExecutionContext(ThreadPool& pool)
: threadPool_(pool), state_(std::make_shared<SharedState>())
: threadPool_(pool)
, state_(std::make_shared<SharedState>())
{}
~ExecutionContext() {
state_->destroy();
}
~ExecutionContext() { state_->destroy(); }
/** Wait for ongoing tasks to complete execution and drop other pending tasks */
void stop() {
state_->destroy(false);
}
void stop() { state_->destroy(false); }
void run(std::function<void()>&& task) {
void run(std::function<void()>&& task)
{
std::lock_guard<std::mutex> lock(state_->mtx);
if (state_->shutdown_) return;
if (state_->shutdown_)
return;
state_->pendingTasks++;
threadPool_.get().run([task = std::move(task), state = state_] {
state->run(task);
});
threadPool_.get().run([task = std::move(task), state = state_] { state->run(task); });
}
private:
struct SharedState {
struct SharedState
{
std::mutex mtx {};
std::condition_variable cv {};
unsigned pendingTasks {0};
@@ -133,9 +123,11 @@ private:
/** When true, prevents scheduled tasks to be executed */
std::atomic_bool destroyed {false};
void destroy(bool wait = true) {
void destroy(bool wait = true)
{
std::unique_lock<std::mutex> lock(mtx);
if (destroyed) return;
if (destroyed)
return;
if (wait) {
cv.wait(lock, [this] { return pendingTasks == 0 && ongoingTasks == 0; });
}
@@ -146,14 +138,16 @@ private:
destroyed = true;
}
void run(const std::function<void()>& task) {
void run(const std::function<void()>& task)
{
{
std::lock_guard<std::mutex> lock(mtx);
pendingTasks--;
ongoingTasks++;
}
if (destroyed) return;
task();
if (destroyed)
return;
task();
{
std::lock_guard<std::mutex> lock(mtx);
ongoingTasks--;
@@ -165,4 +159,4 @@ private:
std::shared_ptr<SharedState> state_;
};
}
} // namespace dht
+3 -19
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "def.h"
@@ -138,8 +123,7 @@ print_time_relative(TimePoint now, TimePoint d)
return "never"s;
if (d == now)
return "now"s;
return (d > now) ? concat("in "sv, print_duration(d - now))
: concat(print_duration(now - d), " ago"sv);
return (d > now) ? concat("in "sv, print_duration(d - now)) : concat(print_duration(now - d), " ago"sv);
}
template<typename Duration = duration>
+354 -287
View File
File diff suppressed because it is too large. Load diff
+4 -9
View File
@@ -3,6 +3,8 @@ project(
'c',
'cpp',
'cython',
license: 'MIT',
license_files: 'LICENSE',
version: '3.6.2',
meson_version: '>=1.1.0',
default_options: ['cpp_std=c++17', 'warning_level=3'],
@@ -117,11 +119,7 @@ opendht = library(
)
# Install library headers.
install_subdir(
'include',
install_dir: get_option('includedir'),
strip_directory: true
)
install_subdir('include', install_dir: get_option('includedir'), strip_directory: true)
conf_data.set('VERSION', meson.project_version())
conf_data.set('prefix', get_option('prefix'))
@@ -152,10 +150,7 @@ if get_option('c').enabled()
install: true,
)
install_headers(
'c/opendht_c.h',
subdir: 'opendht'
)
install_headers('c/opendht_c.h', subdir: 'opendht')
configure_file(
input: 'opendht-c.pc.in',
+1 -1
View File
@@ -86,4 +86,4 @@ For more information, examples and documentation, or to repport issues, visit: h
Copyright (c) 2014-2026 Savoir-faire Linux Inc.
OpenDHT is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
OpenDHT is released under the MIT License. See [LICENSE](LICENSE) for details.
+1 -13
View File
@@ -6,19 +6,7 @@
# cython: language_level=3
#
# Copyright (c) 2014-2026 Savoir-faire Linux Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# SPDX-License-Identifier: MIT
from libcpp.map cimport map as map
from libcpp cimport bool
+1 -13
View File
@@ -1,17 +1,5 @@
# Copyright (c) 2014-2026 Savoir-faire Linux Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# SPDX-License-Identifier: MIT
from __future__ import annotations
import asyncio
+1 -13
View File
@@ -1,17 +1,5 @@
# Copyright (c) 2014-2026 Savoir-faire Linux Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# SPDX-License-Identifier: MIT
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t
from libcpp cimport bool, nullptr_t, nullptr
+2 -14
View File
@@ -1,17 +1,5 @@
# Copyright (c) 2014-2026 Savoir-faire Linux Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# SPDX-License-Identifier: MIT
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext as _build_ext
@@ -52,7 +40,7 @@ setup(name="opendht",
long_description_content_type="text/markdown",
url='https://github.com/savoirfairelinux/opendht',
author="Adrien Beraud <adrien.beraud@savoirfairelinux.com>",
license="GPL-3.0-or-later",
license="MIT",
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
+1 -13
View File
@@ -1,19 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2014-2026 Savoir-faire Linux Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.
# SPDX-License-Identifier: MIT
import os
import sys
+2 -16
View File
@@ -1,19 +1,5 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2015-2026 Savoir-faire Linux Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.
# Copyright (c) 2014-2026 Savoir-faire Linux Inc.
# SPDX-License-Identifier: MIT
import os
import sys
+2 -16
View File
@@ -1,19 +1,5 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2015-2026 Savoir-faire Linux Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.
# Copyright (c) 2014-2026 Savoir-faire Linux Inc.
# SPDX-License-Identifier: MIT
import argparse
import os
import subprocess
+1 -14
View File
@@ -1,20 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2014-2026 Savoir-faire Linux Inc.
# Author(s): Adrien Béraud <adrien.beraud@savoirfairelinux.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.
# SPDX-License-Identifier: MIT
import os, sys, time, cmd
import signal
+2 -14
View File
@@ -1,18 +1,6 @@
#!/usr/bin/env python3
# Copyright (c) 2016-2026 Savoir-faire Linux Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.
# Copyright (c) 2014-2026 Savoir-faire Linux Inc.
# SPDX-License-Identifier: MIT
from twisted.web import server, resource
from twisted.internet import reactor, endpoints
+2 -14
View File
@@ -1,18 +1,6 @@
#!/usr/bin/env python3
# Copyright (c) 2015-2026 Savoir-faire Linux Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.
# Copyright (c) 2014-2026 Savoir-faire Linux Inc.
# SPDX-License-Identifier: MIT
import time
import argparse
+2 -14
View File
@@ -1,18 +1,6 @@
#!/usr/bin/env python3
# Copyright (c) 2015-2026 Savoir-faire Linux Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <https://www.gnu.org/licenses/>.
# Copyright (c) 2014-2026 Savoir-faire Linux Inc.
# SPDX-License-Identifier: MIT
import asyncio
import time
+1 -13
View File
@@ -1,18 +1,6 @@
#!/usr/bin/env python3
# Copyright (c) 2014-2026 Savoir-faire Linux Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# SPDX-License-Identifier: MIT
import time, sys, os
from pprint import pprint
+7
View File
@@ -0,0 +1,7 @@
Copyright (c) 2014-2026 Savoir-faire Linux Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+2 -16
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
extern crate opendht;
use std::{ thread, time };
+5 -23
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
use crate::ffi::*;
@@ -21,16 +7,12 @@ pub use crate::ffi::Blob;
impl Blob {
pub fn data(&self) -> DataView {
unsafe {
dht_blob_get_data(self)
}
unsafe { dht_blob_get_data(self) }
}
}
impl Drop for Blob {
fn drop(&mut self) {
unsafe {
dht_blob_delete(&mut *self)
}
unsafe { dht_blob_delete(&mut *self) }
}
}
}
+39 -82
View File
@@ -1,95 +1,72 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#![allow(dead_code)]
pub use crate::ffi::*;
use std::ffi::CString;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::fs::File;
use std::ptr;
impl PublicKey {
pub fn new(data: &str) -> Box<PublicKey> {
unsafe {
Box::from_raw(dht_publickey_import(data.as_ptr(), data.len()))
}
unsafe { Box::from_raw(dht_publickey_import(data.as_ptr(), data.len())) }
}
pub fn unpack(&mut self, data: Vec<u8>) -> i32 {
unsafe {
dht_publickey_unpack(&mut *self, data.as_ptr(), data.len())
}
unsafe { dht_publickey_unpack(&mut *self, data.as_ptr(), data.len()) }
}
pub fn pack(&mut self, data: &str) -> i32 {
let data = CString::new(data).unwrap();
unsafe {
dht_publickey_pack(&mut *self,
data.as_ptr(),
data.as_bytes().len())
}
unsafe { dht_publickey_pack(&mut *self, data.as_ptr(), data.as_bytes().len()) }
}
pub fn id(&self) -> InfoHash {
unsafe {
dht_publickey_get_id(self)
}
unsafe { dht_publickey_get_id(self) }
}
pub fn long_id(&self) -> PkId {
unsafe {
dht_publickey_get_long_id(self)
}
unsafe { dht_publickey_get_long_id(self) }
}
pub fn check_signature(&self, data: &str, signature: &str) -> bool {
let data = CString::new(data).unwrap();
let signature = CString::new(signature).unwrap();
unsafe {
dht_publickey_check_signature(self,
data.as_ptr(), data.as_bytes().len(),
signature.as_ptr(), signature.as_bytes().len())
dht_publickey_check_signature(
self,
data.as_ptr(),
data.as_bytes().len(),
signature.as_ptr(),
signature.as_bytes().len(),
)
}
}
pub fn encrypt(&self, data: &str) -> Box<Blob> {
let data = CString::new(data).unwrap();
unsafe {
Box::from_raw(dht_publickey_encrypt(self,
data.as_ptr(), data.as_bytes().len()))
Box::from_raw(dht_publickey_encrypt(
self,
data.as_ptr(),
data.as_bytes().len(),
))
}
}
}
impl Drop for PublicKey {
fn drop(&mut self) {
unsafe {
dht_publickey_delete(&mut *self)
}
unsafe { dht_publickey_delete(&mut *self) }
}
}
impl PrivateKey {
pub fn new(key_length_bits: u32) -> Box<PrivateKey> {
unsafe {
Box::from_raw(dht_privatekey_generate(key_length_bits))
}
unsafe { Box::from_raw(dht_privatekey_generate(key_length_bits)) }
}
pub fn import(file: &str, password: &str) -> io::Result<Box<PrivateKey>> {
@@ -101,24 +78,22 @@ impl PrivateKey {
pub fn from_bytes(buffer: &Vec<u8>, password: &str) -> Box<PrivateKey> {
unsafe {
Box::from_raw(dht_privatekey_import((&*buffer).as_ptr(), buffer.len(),
password.as_ptr() as *const i8))
Box::from_raw(dht_privatekey_import(
(&*buffer).as_ptr(),
buffer.len(),
password.as_ptr() as *const i8,
))
}
}
pub fn public_key(&self) -> Box<PublicKey> {
unsafe {
Box::from_raw(dht_privatekey_get_publickey(self))
}
unsafe { Box::from_raw(dht_privatekey_get_publickey(self)) }
}
}
impl Drop for PrivateKey {
fn drop(&mut self) {
unsafe {
dht_privatekey_delete(&mut *self)
}
unsafe { dht_privatekey_delete(&mut *self) }
}
}
@@ -131,63 +106,45 @@ impl DhtCertificate {
}
pub fn from_bytes(buffer: &Vec<u8>) -> Box<DhtCertificate> {
unsafe {
Box::from_raw(dht_certificate_import((&*buffer).as_ptr(), buffer.len()))
}
unsafe { Box::from_raw(dht_certificate_import((&*buffer).as_ptr(), buffer.len())) }
}
pub fn from_slice(buffer: &str) -> Box<DhtCertificate> {
unsafe {
Box::from_raw(dht_certificate_import((&*buffer).as_ptr(), buffer.len()))
}
unsafe { Box::from_raw(dht_certificate_import((&*buffer).as_ptr(), buffer.len())) }
}
pub fn id(&self) -> InfoHash {
unsafe {
dht_certificate_get_id(&*self)
}
unsafe { dht_certificate_get_id(&*self) }
}
pub fn long_id(&self) -> PkId {
unsafe {
dht_certificate_get_long_id(&*self)
}
unsafe { dht_certificate_get_long_id(&*self) }
}
pub fn publickey(&self) -> Box<PublicKey> {
unsafe {
Box::from_raw(dht_certificate_get_publickey(&*self))
}
unsafe { Box::from_raw(dht_certificate_get_publickey(&*self)) }
}
}
impl Drop for DhtCertificate {
fn drop(&mut self) {
unsafe {
dht_certificate_delete(&mut *self)
}
unsafe { dht_certificate_delete(&mut *self) }
}
}
impl DhtIdentity {
pub fn new(common_name: &str) -> DhtIdentity {
unsafe {
DhtIdentity::generate(common_name, Box::from_raw(ptr::null_mut()))
}
unsafe { DhtIdentity::generate(common_name, Box::from_raw(ptr::null_mut())) }
}
pub fn generate(common_name: &str, ca: Box<DhtIdentity>) -> DhtIdentity {
let common_name = CString::new(common_name).unwrap();
unsafe {
dht_identity_generate(common_name.as_ptr(), &*ca)
}
unsafe { dht_identity_generate(common_name.as_ptr(), &*ca) }
}
}
impl Drop for DhtIdentity {
fn drop(&mut self) {
unsafe {
dht_identity_delete(&mut *self)
}
unsafe { dht_identity_delete(&mut *self) }
}
}
}
+90 -118
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#![allow(dead_code)]
@@ -22,11 +8,10 @@ use std::ffi::CString;
use std::ptr;
pub use crate::ffi::*;
use std::net::SocketAddr;
use os_socketaddr::OsSocketAddr;
use std::net::SocketAddr;
impl DhtRunnerConfig {
pub fn new() -> Box<DhtRunnerConfig> {
let mut config: Box<DhtRunnerConfig> = Box::new(DhtRunnerConfig {
dht_config: DhtSecureConfig {
@@ -92,25 +77,21 @@ impl DhtRunnerConfig {
self.dht_config.id.privatekey = Box::into_raw(privatekey);
self.dht_config.id.certificate = Box::into_raw(certificate);
}
}
impl DhtNodeConfig
{
impl DhtNodeConfig {
pub fn set_persist_path(&mut self, persist_path: &str) {
self.persist_path = CString::new(persist_path).unwrap().into_raw();
}
}
struct GetHandler<'a>
{
get_cb: &'a mut(dyn FnMut(Box<Value>) -> bool),
done_cb: &'a mut(dyn FnMut(bool))
struct GetHandler<'a> {
get_cb: &'a mut (dyn FnMut(Box<Value>) -> bool),
done_cb: &'a mut (dyn FnMut(bool)),
}
impl<'a> GetHandler<'a>
{
fn get_cb(&mut self, v: Box<Value>) -> bool{
impl<'a> GetHandler<'a> {
fn get_cb(&mut self, v: Box<Value>) -> bool {
(self.get_cb)(v)
}
@@ -119,7 +100,7 @@ impl<'a> GetHandler<'a>
}
}
extern fn get_handler_cb(v: *mut Value, ptr: *mut c_void) -> bool {
extern "C" fn get_handler_cb(v: *mut Value, ptr: *mut c_void) -> bool {
if ptr.is_null() {
return true;
}
@@ -129,52 +110,48 @@ extern fn get_handler_cb(v: *mut Value, ptr: *mut c_void) -> bool {
}
}
extern fn done_handler_cb(ok: bool, ptr: *mut c_void) {
extern "C" fn done_handler_cb(ok: bool, ptr: *mut c_void) {
unsafe {
let handler = Box::from_raw(ptr as *mut GetHandler);
(*handler.done_cb)(ok)
}
}
struct PutHandler<'a>
{
done_cb: &'a mut(dyn FnMut(bool))
struct PutHandler<'a> {
done_cb: &'a mut (dyn FnMut(bool)),
}
impl<'a> PutHandler<'a>
{
impl<'a> PutHandler<'a> {
fn done_cb(&mut self, ok: bool) {
(self.done_cb)(ok)
}
}
extern fn put_handler_done(ok: bool, ptr: *mut c_void) {
extern "C" fn put_handler_done(ok: bool, ptr: *mut c_void) {
unsafe {
let handler = Box::from_raw(ptr as *mut PutHandler);
(*handler.done_cb)(ok)
}
}
struct ListenHandler<'a>
{
cb: &'a mut(dyn FnMut(Box<Value>, bool) -> bool)
struct ListenHandler<'a> {
cb: &'a mut (dyn FnMut(Box<Value>, bool) -> bool),
}
impl<'a> ListenHandler<'a>
{
impl<'a> ListenHandler<'a> {
fn cb(&mut self, v: Box<Value>, expired: bool) -> bool {
(self.cb)(v, expired)
}
}
extern fn listen_handler(v: *mut Value, expired: bool, ptr: *mut c_void) -> bool {
extern "C" fn listen_handler(v: *mut Value, expired: bool, ptr: *mut c_void) -> bool {
unsafe {
let handler = ptr as *mut ListenHandler;
(*handler).cb((*v).boxed(), expired)
}
}
extern fn listen_handler_done(ptr: *mut c_void) {
extern "C" fn listen_handler_done(ptr: *mut c_void) {
unsafe {
Box::from_raw(ptr as *mut ListenHandler);
}
@@ -182,83 +159,79 @@ extern fn listen_handler_done(ptr: *mut c_void) {
impl DhtRunner {
pub fn new() -> Box<DhtRunner> {
unsafe {
Box::from_raw(dht_runner_new())
}
unsafe { Box::from_raw(dht_runner_new()) }
}
pub fn run(&mut self, port: u16) {
unsafe {
dht_runner_run(&mut *self, port)
}
unsafe { dht_runner_run(&mut *self, port) }
}
pub fn run_config(&mut self, port: u16, config: Box<DhtRunnerConfig>) {
unsafe {
dht_runner_run_config(&mut *self, port, &*config)
}
unsafe { dht_runner_run_config(&mut *self, port, &*config) }
}
pub fn bootstrap(&mut self, host: &str, service: u16) {
unsafe {
dht_runner_bootstrap(&mut *self,
dht_runner_bootstrap(
&mut *self,
CString::new(host).unwrap().into_raw(),
CString::new(service.to_string()).unwrap().into_raw())
CString::new(service.to_string()).unwrap().into_raw(),
)
}
}
pub fn node_id(&self) -> InfoHash {
unsafe {
dht_runner_get_node_id(&*self)
}
unsafe { dht_runner_get_node_id(&*self) }
}
pub fn id(&self) -> InfoHash {
unsafe {
dht_runner_get_id(&*self)
}
unsafe { dht_runner_get_id(&*self) }
}
pub fn get<'a>(&mut self, h: &InfoHash,
get_cb: &'a mut(dyn FnMut(Box<Value>) -> bool),
done_cb: &'a mut(dyn FnMut(bool))) {
let handler = Box::new(GetHandler {
get_cb,
done_cb,
});
pub fn get<'a>(
&mut self,
h: &InfoHash,
get_cb: &'a mut (dyn FnMut(Box<Value>) -> bool),
done_cb: &'a mut (dyn FnMut(bool)),
) {
let handler = Box::new(GetHandler { get_cb, done_cb });
let handler = Box::into_raw(handler) as *mut c_void;
unsafe {
dht_runner_get(&mut *self, h, get_handler_cb, done_handler_cb, handler)
}
unsafe { dht_runner_get(&mut *self, h, get_handler_cb, done_handler_cb, handler) }
}
pub fn put<'a>(&mut self, h: &InfoHash, v: Box<Value>,
done_cb: &'a mut(dyn FnMut(bool)), permanent: bool) {
let handler = Box::new(PutHandler {
done_cb,
});
pub fn put<'a>(
&mut self,
h: &InfoHash,
v: Box<Value>,
done_cb: &'a mut (dyn FnMut(bool)),
permanent: bool,
) {
let handler = Box::new(PutHandler { done_cb });
let handler = Box::into_raw(handler) as *mut c_void;
unsafe {
dht_runner_put(&mut *self, h, &*v, put_handler_done, handler, permanent)
}
unsafe { dht_runner_put(&mut *self, h, &*v, put_handler_done, handler, permanent) }
}
pub fn put_signed<'a>(&mut self, h: &InfoHash, v: Box<Value>,
done_cb: &'a mut(dyn FnMut(bool)), permanent: bool) {
let handler = Box::new(PutHandler {
done_cb,
});
pub fn put_signed<'a>(
&mut self,
h: &InfoHash,
v: Box<Value>,
done_cb: &'a mut (dyn FnMut(bool)),
permanent: bool,
) {
let handler = Box::new(PutHandler { done_cb });
let handler = Box::into_raw(handler) as *mut c_void;
unsafe {
dht_runner_put_signed(&mut *self, h, &*v, put_handler_done, handler, permanent)
}
unsafe { dht_runner_put_signed(&mut *self, h, &*v, put_handler_done, handler, permanent) }
}
pub fn put_encrypted<'a>(&mut self, h: &InfoHash, to: &InfoHash, v: Box<Value>,
done_cb: &'a mut(dyn FnMut(bool)), permanent: bool) {
let handler = Box::new(PutHandler {
done_cb,
});
pub fn put_encrypted<'a>(
&mut self,
h: &InfoHash,
to: &InfoHash,
v: Box<Value>,
done_cb: &'a mut (dyn FnMut(bool)),
permanent: bool,
) {
let handler = Box::new(PutHandler { done_cb });
let handler = Box::into_raw(handler) as *mut c_void;
unsafe {
dht_runner_put_encrypted(&mut *self, h, to, &*v, put_handler_done, handler, permanent)
@@ -266,35 +239,37 @@ impl DhtRunner {
}
pub fn cancel_put<'a>(&mut self, h: &InfoHash, vid: u64) {
unsafe {
dht_runner_cancel_put(&mut *self, h, vid)
}
unsafe { dht_runner_cancel_put(&mut *self, h, vid) }
}
pub fn listen<'a>(&mut self, h: &InfoHash,
cb: &'a mut(dyn FnMut(Box<Value>, bool) -> bool)) -> Box<OpToken> {
let handler = Box::new(ListenHandler {
cb,
});
pub fn listen<'a>(
&mut self,
h: &InfoHash,
cb: &'a mut (dyn FnMut(Box<Value>, bool) -> bool),
) -> Box<OpToken> {
let handler = Box::new(ListenHandler { cb });
let handler = Box::into_raw(handler) as *mut c_void;
unsafe {
Box::from_raw(dht_runner_listen(&mut *self, h, listen_handler, listen_handler_done, handler))
Box::from_raw(dht_runner_listen(
&mut *self,
h,
listen_handler,
listen_handler_done,
handler,
))
}
}
pub fn cancel_listen(&mut self, h: &InfoHash, token: Box<OpToken>) {
unsafe {
dht_runner_cancel_listen(&mut *self, h, &*token)
}
unsafe { dht_runner_cancel_listen(&mut *self, h, &*token) }
}
pub fn shutdown(&mut self,
done_cb: extern fn(bool, *mut c_void),
cb_user_data: *mut c_void)
{
unsafe {
dht_runner_shutdown(&mut *self, done_cb, cb_user_data)
}
pub fn shutdown(
&mut self,
done_cb: extern "C" fn(bool, *mut c_void),
cb_user_data: *mut c_void,
) {
unsafe { dht_runner_shutdown(&mut *self, done_cb, cb_user_data) }
}
pub fn public_addresses(&self) -> Vec<SocketAddr> {
@@ -306,7 +281,8 @@ impl DhtRunner {
if sock.is_some() {
result.push(sock.unwrap());
}
addresses = (addresses as usize + std::mem::size_of::<*mut OsSocketAddr>()) as *mut *mut OsSocketAddr;
addresses = (addresses as usize + std::mem::size_of::<*mut OsSocketAddr>())
as *mut *mut OsSocketAddr;
}
}
result
@@ -315,16 +291,12 @@ impl DhtRunner {
impl Drop for DhtRunner {
fn drop(&mut self) {
unsafe {
dht_runner_delete(&mut *self)
}
unsafe { dht_runner_delete(&mut *self) }
}
}
impl Drop for OpToken {
fn drop(&mut self) {
unsafe {
dht_op_token_delete(&mut *self)
}
unsafe { dht_op_token_delete(&mut *self) }
}
}
}
+2 -16
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#![allow(dead_code)]
+8 -26
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
use crate::ffi::*;
use std::ffi::CStr;
@@ -24,9 +10,7 @@ pub use crate::ffi::InfoHash;
impl InfoHash {
pub fn new() -> InfoHash {
InfoHash {
d: [0; 20]
}
InfoHash { d: [0; 20] }
}
pub fn random() -> InfoHash {
@@ -64,19 +48,17 @@ impl InfoHash {
}
pub fn is_zero(&self) -> bool {
unsafe {
dht_infohash_is_zero(self)
}
unsafe { dht_infohash_is_zero(self) }
}
}
impl fmt::Display for InfoHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
unsafe {
let self_str = CStr::from_ptr(
dht_infohash_print(self)
).to_str().unwrap_or("");
let self_str = CStr::from_ptr(dht_infohash_print(self))
.to_str()
.unwrap_or("");
write!(f, "{}", self_str)
}
}
}
}
+2 -16
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
extern crate libc;
extern crate os_socketaddr;
+2 -16
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
use crate::ffi::*;
use std::ffi::CStr;
+2 -16
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#![allow(dead_code)]
use crate::ffi::*;
+2 -16
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "base64.h"
#include "utils.h"
+2 -17
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include <string>
+24 -30
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -23,11 +9,11 @@
namespace dht {
GetCallbackSimple
bindGetCb(GetCallbackRaw raw_cb, void* user_data)
{
if (not raw_cb) return {};
if (not raw_cb)
return {};
return [=](const std::shared_ptr<Value>& value) {
return raw_cb(value, user_data);
};
@@ -36,8 +22,9 @@ bindGetCb(GetCallbackRaw raw_cb, void* user_data)
GetCallback
bindGetCb(GetCallbackSimple cb)
{
if (not cb) return {};
return [cb=std::move(cb)](const std::vector<std::shared_ptr<Value>>& values) {
if (not cb)
return {};
return [cb = std::move(cb)](const std::vector<std::shared_ptr<Value>>& values) {
for (const auto& v : values)
if (not cb(v))
return false;
@@ -48,7 +35,8 @@ bindGetCb(GetCallbackSimple cb)
ValueCallback
bindValueCb(ValueCallbackRaw raw_cb, void* user_data)
{
if (not raw_cb) return {};
if (not raw_cb)
return {};
return [=](const std::vector<std::shared_ptr<Value>>& values, bool expired) {
for (const auto& v : values)
if (not raw_cb(v, expired, user_data))
@@ -60,13 +48,16 @@ bindValueCb(ValueCallbackRaw raw_cb, void* user_data)
ShutdownCallback
bindShutdownCb(ShutdownCallbackRaw shutdown_cb_raw, void* user_data)
{
return [=]() { shutdown_cb_raw(user_data); };
return [=]() {
shutdown_cb_raw(user_data);
};
}
DoneCallback
bindDoneCb(DoneCallbackSimple donecb)
{
if (not donecb) return {};
if (not donecb)
return {};
using namespace std::placeholders;
return std::bind(std::move(donecb), _1);
}
@@ -74,15 +65,18 @@ bindDoneCb(DoneCallbackSimple donecb)
DoneCallback
bindDoneCb(DoneCallbackRaw raw_cb, void* user_data)
{
if (not raw_cb) return {};
if (not raw_cb)
return {};
return [=](bool success, const std::vector<std::shared_ptr<Node>>& nodes) {
raw_cb(success, (std::vector<std::shared_ptr<Node>>*)&nodes, user_data);
raw_cb(success, (std::vector<std::shared_ptr<Node>>*) &nodes, user_data);
};
}
DoneCallbackSimple
bindDoneCbSimple(DoneCallbackSimpleRaw raw_cb, void* user_data) {
if (not raw_cb) return {};
bindDoneCbSimple(DoneCallbackSimpleRaw raw_cb, void* user_data)
{
if (not raw_cb)
return {};
return [=](bool success) {
raw_cb(success, user_data);
};
@@ -92,7 +86,8 @@ std::string
NodeStats::toString() const
{
std::ostringstream ss;
ss << "Known nodes: " << good_nodes << " good, " << dubious_nodes << " dubious, " << incoming_nodes << " incoming." << std::endl;
ss << "Known nodes: " << good_nodes << " good, " << dubious_nodes << " dubious, " << incoming_nodes << " incoming."
<< std::endl;
ss << searches << " searches, " << node_cache_size << " total cached nodes" << std::endl;
if (table_depth > 1) {
ss << "Routing table depth: " << table_depth << std::endl;
@@ -159,5 +154,4 @@ NodeInfo::NodeInfo(const Json::Value& v)
#endif
}
} // namespace dht
+6 -22
View File
@@ -1,21 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#ifndef __STRICT_ANSI__
@@ -26,9 +10,9 @@
#include <fcntl.h>
#include <synchapi.h>
#define R_OK 4
#define W_OK 2
#define F_OK 0
#define R_OK 4
#define W_OK 2
#define F_OK 0
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
@@ -43,7 +27,7 @@
#endif
#define srandom srand
#define random rand
#define random rand
#define inline __inline
typedef int mode_t;
+4 -19
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "os_cert.h"
#include "logger.h"
@@ -34,7 +20,7 @@ namespace dht {
namespace http {
PEMCache::PEMCache(const std::shared_ptr<Logger>& l)
: logger(l)
: logger(l)
{
#ifdef _WIN32
PCCERT_CONTEXT pContext = NULL;
@@ -59,8 +45,7 @@ PEMCache::PEMCache(const std::shared_ptr<Logger>& l)
if ((osStatus = SecTrustCopyAnchorCertificates(&result)) != 0) {
if (logger) {
CFStringRef statusString = SecCopyErrorMessageString(osStatus, NULL);
logger->d("Error enumerating certificates: %s",
CFStringGetCStringPtr(statusString, kCFStringEncodingASCII));
logger->d("Error enumerating certificates: %s", CFStringGetCStringPtr(statusString, kCFStringEncodingASCII));
CFRelease(statusString);
}
if (result != NULL) {
+4 -19
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "logger.h"
@@ -33,7 +18,7 @@ class PEMCache
{
PEMCache(const std::shared_ptr<Logger>& l);
using X509Ptr = std::unique_ptr<X509, void(*)(X509*)>;
using X509Ptr = std::unique_ptr<X509, void (*)(X509*)>;
std::vector<X509Ptr> pems_;
std::shared_ptr<Logger> logger;
@@ -46,5 +31,5 @@ public:
void fillX509Store(SSL_CTX* ctx);
};
}
} // namespace http
} // namespace dht
+2 -17
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
* Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "crypto.h"
#include "rng.h"
+26 -46
View File
@@ -1,25 +1,12 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "default_types.h"
namespace dht {
std::ostream& operator<< (std::ostream& s, const DhtMessage& v)
std::ostream&
operator<<(std::ostream& s, const DhtMessage& v)
{
s << "DhtMessage: service " << v.service << std::endl;
return s;
@@ -32,26 +19,25 @@ DhtMessage::storePolicy(InfoHash h, std::shared_ptr<Value>& v, const InfoHash& f
auto msg = unpackMsg<DhtMessage>(v->data);
if (msg.service.empty())
return false;
} catch (const std::exception& e) {}
} catch (const std::exception& e) {
}
return ValueType::DEFAULT_STORE_POLICY(h, v, f, sa);
}
Value::Filter
DhtMessage::ServiceFilter(const std::string& s)
{
return Value::Filter::chain(
Value::TypeFilter(TYPE),
[s](const Value& v) {
try {
return unpackMsg<DhtMessage>(v.data).service == s;
} catch (const std::exception& e) {
return false;
}
return Value::Filter::chain(Value::TypeFilter(TYPE), [s](const Value& v) {
try {
return unpackMsg<DhtMessage>(v.data).service == s;
} catch (const std::exception& e) {
return false;
}
);
});
}
std::ostream& operator<< (std::ostream& s, const IpServiceAnnouncement& v)
std::ostream&
operator<<(std::ostream& s, const IpServiceAnnouncement& v)
{
if (v.addr) {
s << "Peer: ";
@@ -76,30 +62,24 @@ IpServiceAnnouncement::storePolicy(InfoHash h, std::shared_ptr<Value>& v, const
// argument v is modified (not the value).
v = std::make_shared<Value>(IpServiceAnnouncement::TYPE, sa_addr, v->id);
return ValueType::DEFAULT_STORE_POLICY(h, v, f, sa);
} catch (const std::exception& e) {}
} catch (const std::exception& e) {
}
return false;
}
const ValueType DhtMessage::TYPE(1, "DHT message", std::chrono::minutes(5), DhtMessage::storePolicy);
const ValueType IpServiceAnnouncement::TYPE(2, "Internet Service Announcement", std::chrono::minutes(15), IpServiceAnnouncement::storePolicy);
const ValueType IpServiceAnnouncement::TYPE(2,
"Internet Service Announcement",
std::chrono::minutes(15),
IpServiceAnnouncement::storePolicy);
const ValueType ImMessage::TYPE = {3, "IM message", std::chrono::minutes(5)};
const ValueType TrustRequest::TYPE = {4, "Certificate trust request", std::chrono::hours(24*7)};
const ValueType TrustRequest::TYPE = {4, "Certificate trust request", std::chrono::hours(24 * 7)};
const ValueType IceCandidates::TYPE = {5, "ICE candidates", std::chrono::minutes(1)};
const std::array<std::reference_wrapper<const ValueType>, 5>
DEFAULT_TYPES
{{
ValueType::USER_DATA,
DhtMessage::TYPE,
ImMessage::TYPE,
IceCandidates::TYPE,
TrustRequest::TYPE
}};
const std::array<std::reference_wrapper<const ValueType>, 5> DEFAULT_TYPES {
{ValueType::USER_DATA, DhtMessage::TYPE, ImMessage::TYPE, IceCandidates::TYPE, TrustRequest::TYPE}
};
const std::array<std::reference_wrapper<const ValueType>, 1>
DEFAULT_INSECURE_TYPES
{{
IpServiceAnnouncement::TYPE
}};
const std::array<std::reference_wrapper<const ValueType>, 1> DEFAULT_INSECURE_TYPES {{IpServiceAnnouncement::TYPE}};
}
} // namespace dht
+764 -527
View File
File diff suppressed because it is too large. Load diff
+2 -18
View File
@@ -1,21 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
* Adrien Béraud <adrien.beraud@savoirfairelinux.com>
* Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "dht_proxy_client.h"
#include "dhtrunner.h"
+2 -18
View File
@@ -1,21 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
* Adrien Béraud <adrien.beraud@savoirfairelinux.com>
* Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#ifdef HAVE_CONFIG_H
#include "config.h"
+2 -16
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#ifdef HAVE_CONFIG_H
#include "config.h"
+2 -16
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "http.h"
#include "logger.h"
+193 -173
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "indexation/pht.h"
#include "rng.h"
@@ -28,7 +13,9 @@ namespace indexation {
*
* @return string that represent the blob into a readable way
*/
static std::string blobToString(const Blob &bl) {
static std::string
blobToString(const Blob& bl)
{
std::ostringstream ss;
auto bn = bl.size() % 8;
auto n = bl.size() / 8;
@@ -36,13 +23,15 @@ static std::string blobToString(const Blob &bl) {
for (size_t i = 0; i < bl.size(); i++)
ss << std::bitset<8>(bl[i]) << " ";
if (bn)
for (unsigned b=0; b < bn; b++)
ss << (char)((bl[n] & (1 << (7 - b))) ? '1':'0');
for (unsigned b = 0; b < bn; b++)
ss << (char) ((bl[n] & (1 << (7 - b))) ? '1' : '0');
return ss.str();
}
std::string Prefix::toString() const {
std::string
Prefix::toString() const
{
std::ostringstream ss;
ss << "Prefix : " << std::endl << "\tContent_ : \"";
@@ -56,20 +45,19 @@ std::string Prefix::toString() const {
return ss.str();
}
void Pht::Cache::insert(const Prefix& p) {
void
Pht::Cache::insert(const Prefix& p)
{
size_t i = 0;
auto now = clock::now();
std::shared_ptr<Node> curr_node;
while ((leaves_.size() > 0
and leaves_.begin()->first + NODE_EXPIRE_TIME < now)
or leaves_.size() > MAX_ELEMENT) {
while ((leaves_.size() > 0 and leaves_.begin()->first + NODE_EXPIRE_TIME < now) or leaves_.size() > MAX_ELEMENT) {
leaves_.erase(leaves_.begin());
}
if (not (curr_node = root_.lock()) ) {
if (not(curr_node = root_.lock())) {
/* Root does not exist, need to create one*/
curr_node = std::make_shared<Node>();
root_ = curr_node;
@@ -78,10 +66,9 @@ void Pht::Cache::insert(const Prefix& p) {
curr_node->last_reply = now;
/* Iterate through all bit of the Blob */
for ( i = 0; i < p.size_; i++ ) {
for (i = 0; i < p.size_; i++) {
/* According to the bit define which node is the next one */
auto& next = ( p.isContentBitActive(i) ) ? curr_node->right_child : curr_node->left_child;
auto& next = (p.isContentBitActive(i)) ? curr_node->right_child : curr_node->left_child;
/**
* If lock, node exists
@@ -101,42 +88,43 @@ void Pht::Cache::insert(const Prefix& p) {
}
/* Insert the leaf (curr_node) into the multimap */
leaves_.emplace(std::move(now), std::move(curr_node) );
leaves_.emplace(std::move(now), std::move(curr_node));
}
int Pht::Cache::lookup(const Prefix& p) {
int
Pht::Cache::lookup(const Prefix& p)
{
int pos = -1;
auto now = clock::now(), last_node_time = now;
/* Before lookup remove the useless one [i.e. too old] */
while ( leaves_.size() > 0
and leaves_.begin()->first + NODE_EXPIRE_TIME < now ) {
while (leaves_.size() > 0 and leaves_.begin()->first + NODE_EXPIRE_TIME < now) {
leaves_.erase(leaves_.begin());
}
auto next = root_;
std::shared_ptr<Node> curr_node;
while ( auto n = next.lock() ) {
while (auto n = next.lock()) {
++pos;
/* Safe since pos is equal to 0 until here */
if ( (unsigned) pos >= p.content_.size() * 8) break;
if ((unsigned) pos >= p.content_.size() * 8)
break;
curr_node = n;
last_node_time = curr_node->last_reply;
curr_node->last_reply = now;
/* Get the Prefix bit by bit, starting from left */
next = ( p.isContentBitActive(pos) ) ? curr_node->right_child : curr_node->left_child;
next = (p.isContentBitActive(pos)) ? curr_node->right_child : curr_node->left_child;
}
if ( pos >= 0 ) {
if (pos >= 0) {
auto to_erase = leaves_.find(last_node_time);
if ( to_erase != leaves_.end() )
leaves_.erase( to_erase );
if (to_erase != leaves_.end())
leaves_.erase(to_erase);
leaves_.emplace( std::move(now), std::move(curr_node) );
leaves_.emplace(std::move(now), std::move(curr_node));
}
return pos;
@@ -145,19 +133,25 @@ int Pht::Cache::lookup(const Prefix& p) {
const ValueType& IndexEntry::TYPE = ValueType::USER_DATA;
constexpr std::chrono::minutes Pht::Cache::NODE_EXPIRE_TIME;
void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
std::shared_ptr<std::vector<std::shared_ptr<IndexEntry>>> vals,
LookupCallbackWrapper cb, DoneCallbackSimple done_cb,
std::shared_ptr<unsigned> max_common_prefix_len,
int start, bool all_values)
void
Pht::lookupStep(Prefix p,
std::shared_ptr<int> lo,
std::shared_ptr<int> hi,
std::shared_ptr<std::vector<std::shared_ptr<IndexEntry>>> vals,
LookupCallbackWrapper cb,
DoneCallbackSimple done_cb,
std::shared_ptr<unsigned> max_common_prefix_len,
int start,
bool all_values)
{
struct node_lookup_result {
struct node_lookup_result
{
bool done {false};
bool is_pht {false};
};
/* start could be under 0 but after the compare it to 0 it always will be unsigned, so we can cast it*/
auto mid = (start >= 0) ? (unsigned) start : (*lo + *hi)/2;
auto mid = (start >= 0) ? (unsigned) start : (*lo + *hi) / 2;
auto first_res = std::make_shared<node_lookup_result>();
auto second_res = std::make_shared<node_lookup_result>();
@@ -167,8 +161,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
if (not ok) {
if (done_cb)
done_cb(false);
}
else if (is_leaf or *lo > *hi) {
} else if (is_leaf or *lo > *hi) {
// leaf node
Prefix to_insert = p.getPrefix(mid);
cache_.insert(to_insert);
@@ -188,7 +181,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
done_cb(true);
} else if (first_res->is_pht) {
// internal node
*lo = mid+1;
*lo = mid + 1;
lookupStep(p, lo, hi, vals, cb, done_cb, max_common_prefix_len, -1, all_values);
} else {
// first get failed before second.
@@ -205,8 +198,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
auto on_get = [=](const std::shared_ptr<dht::Value>& value, std::shared_ptr<node_lookup_result> res) {
if (value->user_type == canary_) {
res->is_pht = true;
}
else {
} else {
IndexEntry entry;
entry.unpackValue(*value);
@@ -224,8 +216,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
if (vals->empty()) {
vals->emplace_back(std::make_shared<IndexEntry>(entry));
*max_common_prefix_len = common_bits;
}
else {
} else {
if (common_bits == *max_common_prefix_len) /* this is the max so far */
vals->emplace_back(std::make_shared<IndexEntry>(entry));
else if (common_bits > *max_common_prefix_len) { /* new max found! */
@@ -241,50 +232,54 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
return true;
};
dht_->get(p.getPrefix(mid).hash(),
std::bind(on_get, std::placeholders::_1, first_res),
dht_->get(
p.getPrefix(mid).hash(),
std::bind(on_get, std::placeholders::_1, first_res),
[=](bool ok) {
if (not ok) {
// DHT failed
first_res->done = true;
if (done_cb and second_res->done)
on_done(false);
} else {
if (not first_res->is_pht) {
// Not a PHT node.
*hi = mid - 1;
lookupStep(p, lo, hi, vals, cb, done_cb, max_common_prefix_len, -1, all_values);
} else {
first_res->done = true;
if (second_res->done or mid >= p.size_ - 1)
on_done(true);
}
}
},
pht_filter);
if (mid < p.size_ - 1)
dht_->get(
p.getPrefix(mid + 1).hash(),
std::bind(on_get, std::placeholders::_1, second_res),
[=](bool ok) {
if (not ok) {
// DHT failed
first_res->done = true;
if (done_cb and second_res->done)
second_res->done = true;
if (done_cb and first_res->done)
on_done(false);
} else {
second_res->done = true;
if (first_res->done)
on_done(true);
}
else {
if (not first_res->is_pht) {
// Not a PHT node.
*hi = mid-1;
lookupStep(p, lo, hi, vals, cb, done_cb, max_common_prefix_len, -1, all_values);
} else {
first_res->done = true;
if (second_res->done or mid >= p.size_ - 1)
on_done(true);
}
}
}, pht_filter);
if (mid < p.size_ - 1)
dht_->get(p.getPrefix(mid+1).hash(),
std::bind(on_get, std::placeholders::_1, second_res),
[=](bool ok) {
if (not ok) {
// DHT failed
second_res->done = true;
if (done_cb and first_res->done)
on_done(false);
}
else {
second_res->done = true;
if (first_res->done)
on_done(true);
}
}, pht_filter);
},
pht_filter);
} else {
on_done(true);
}
}
void Pht::lookup(Key k, Pht::LookupCallback cb, DoneCallbackSimple done_cb, bool exact_match) {
void
Pht::lookup(Key k, Pht::LookupCallback cb, DoneCallbackSimple done_cb, bool exact_match)
{
auto prefix = linearize(k);
auto values = std::make_shared<std::vector<std::shared_ptr<IndexEntry>>>();
@@ -292,32 +287,38 @@ void Pht::lookup(Key k, Pht::LookupCallback cb, DoneCallbackSimple done_cb, bool
auto hi = std::make_shared<int>(prefix.size_);
std::shared_ptr<unsigned> max_common_prefix_len = not exact_match ? std::make_shared<unsigned>(0) : nullptr;
lookupStep(prefix, lo, hi, values,
lookupStep(
prefix,
lo,
hi,
values,
[=](std::vector<std::shared_ptr<IndexEntry>>& entries, const Prefix& p) {
std::vector<std::shared_ptr<Value>> vals(entries.size());
std::transform(entries.begin(), entries.end(), vals.begin(),
[](const std::shared_ptr<IndexEntry>& ie) {
return std::make_shared<Value>(ie->value);
std::transform(entries.begin(), entries.end(), vals.begin(), [](const std::shared_ptr<IndexEntry>& ie) {
return std::make_shared<Value>(ie->value);
});
cb(vals, p);
}, done_cb, max_common_prefix_len, cache_.lookup(prefix));
},
done_cb,
max_common_prefix_len,
cache_.lookup(prefix));
}
void Pht::updateCanary(Prefix p) {
void
Pht::updateCanary(Prefix p)
{
// TODO: change this... copy value
dht::Value canary_value;
canary_value.user_type = canary_;
dht_->put(p.hash(), std::move(canary_value),
[=](bool){
static std::bernoulli_distribution d(0.5);
std::random_device rd;
if (p.size_ and d(rd))
updateCanary(p.getPrefix(-1));
}
);
dht_->put(p.hash(), std::move(canary_value), [=](bool) {
static std::bernoulli_distribution d(0.5);
std::random_device rd;
if (p.size_ and d(rd))
updateCanary(p.getPrefix(-1));
});
if (p.size_) {
dht::Value canary_second_value;
@@ -326,67 +327,76 @@ void Pht::updateCanary(Prefix p) {
}
}
void Pht::insert(const Prefix& kp, IndexEntry entry, std::shared_ptr<int> lo, std::shared_ptr<int> hi, time_point time_p,
bool check_split, DoneCallbackSimple done_cb) {
if (time_p + ValueType::USER_DATA.expiration < clock::now()) return;
void
Pht::insert(const Prefix& kp,
IndexEntry entry,
std::shared_ptr<int> lo,
std::shared_ptr<int> hi,
time_point time_p,
bool check_split,
DoneCallbackSimple done_cb)
{
if (time_p + ValueType::USER_DATA.expiration < clock::now())
return;
auto vals = std::make_shared<std::vector<std::shared_ptr<IndexEntry>>>();
auto final_prefix = std::make_shared<Prefix>();
lookupStep(kp, lo, hi, vals,
[=](std::vector<std::shared_ptr<IndexEntry>>&, Prefix p) {
*final_prefix = Prefix(p);
},
[=](bool ok){
lookupStep(
kp,
lo,
hi,
vals,
[=](std::vector<std::shared_ptr<IndexEntry>>&, Prefix p) { *final_prefix = Prefix(p); },
[=](bool ok) {
if (not ok) {
if (done_cb)
done_cb(false);
} else {
RealInsertCallback real_insert = [=](const Prefix& p, IndexEntry entry) {
updateCanary(p);
checkPhtUpdate(p, entry, time_p);
cache_.insert(p);
dht_->put(p.hash(), std::move(entry), done_cb , time_p);
dht_->put(p.hash(), std::move(entry), done_cb, time_p);
};
if ( not check_split or final_prefix->size_ == kp.size_ ) {
if (not check_split or final_prefix->size_ == kp.size_) {
real_insert(*final_prefix, std::move(entry));
} else {
if ( vals->size() < MAX_NODE_ENTRY_COUNT ) {
if (vals->size() < MAX_NODE_ENTRY_COUNT) {
getRealPrefix(final_prefix, std::move(entry), real_insert);
}
else {
} else {
split(*final_prefix, *vals, entry, real_insert);
}
}
}
}, nullptr, cache_.lookup(kp), true);
},
nullptr,
cache_.lookup(kp),
true);
}
Prefix Pht::zcurve(const std::vector<Prefix>& all_prefix) const {
Prefix
Pht::zcurve(const std::vector<Prefix>& all_prefix) const
{
Prefix p;
if ( all_prefix.size() == 1 )
if (all_prefix.size() == 1)
return all_prefix[0];
/* All prefix got the same size (thanks to padding) */
size_t prefix_size = all_prefix[0].content_.size();
/* Loop on all uint8_t of the input prefix */
for ( size_t j = 0, bit = 0; j < prefix_size; j++) {
for (size_t j = 0, bit = 0; j < prefix_size; j++) {
uint8_t mask = 0x80;
/* For each of the 8 bits of the input uint8_t */
for ( int i = 0; i < 8; ) {
for (int i = 0; i < 8;) {
uint8_t flags = 0;
uint8_t content = 0;
/* For each bit of the output uint8_t */
for ( int k = 0 ; k < 8; k++ ) {
for (int k = 0; k < 8; k++) {
auto diff = k - i;
/*get the content 'c', and the flag 'f' of the input prefix */
@@ -395,12 +405,16 @@ Prefix Pht::zcurve(const std::vector<Prefix>& all_prefix) const {
/* Move this bit at the right position according to the diff
and merge it into content and flags in the same way */
content |= ( diff >= 0 ) ? c >> diff : c << std::abs(diff);
flags |= ( diff >= 0 ) ? f >> diff : f << std::abs(diff);
content |= (diff >= 0) ? c >> diff : c << std::abs(diff);
flags |= (diff >= 0) ? f >> diff : f << std::abs(diff);
/* If we are on the last prefix of the vector get back to the first and
,move the mask in order to get the n + 1nth bit */
if ( ++bit == all_prefix.size() ) { bit = 0; ++i; mask >>= 1; }
if (++bit == all_prefix.size()) {
bit = 0;
++i;
mask >>= 1;
}
}
/* Add the next flags + content to the output prefix */
@@ -413,19 +427,26 @@ Prefix Pht::zcurve(const std::vector<Prefix>& all_prefix) const {
return p;
}
Prefix Pht::linearize(Key k) const {
if (not validKey(k)) { throw std::invalid_argument(INVALID_KEY); }
Prefix
Pht::linearize(Key k) const
{
if (not validKey(k)) {
throw std::invalid_argument(INVALID_KEY);
}
std::vector<Prefix> all_prefix;
all_prefix.reserve(k.size());
/* Get the max size of the keyspec and take it for size limit (for padding) */
auto max = std::max_element(keySpec_.begin(), keySpec_.end(),
[](const std::pair<std::string, size_t>& a, const std::pair<std::string, size_t>& b) {
return a.second < b.second;
})->second + 1;
auto max = std::max_element(keySpec_.begin(),
keySpec_.end(),
[](const std::pair<std::string, size_t>& a, const std::pair<std::string, size_t>& b) {
return a.second < b.second;
})
->second
+ 1;
for ( auto const& it : k ) {
for (auto const& it : k) {
Prefix p = Blob {it.second.begin(), it.second.end()};
p.addPaddingContent(max);
p.updateFlags();
@@ -436,18 +457,22 @@ Prefix Pht::linearize(Key k) const {
return zcurve(all_prefix);
}
void Pht::getRealPrefix(const std::shared_ptr<Prefix>& p, IndexEntry entry, RealInsertCallback end_cb )
void
Pht::getRealPrefix(const std::shared_ptr<Prefix>& p, IndexEntry entry, RealInsertCallback end_cb)
{
if ( p->size_ == 0 ) {
if (p->size_ == 0) {
end_cb(*p, std::move(entry));
return;
}
struct OpState {
struct OpState
{
unsigned entry_count {0}; /* Total number of data on 3 nodes */
unsigned ended {0}; /* How many ops have ended */
unsigned ended {0}; /* How many ops have ended */
Prefix parent;
OpState(Prefix p) : parent(p) {}
OpState(Prefix p)
: parent(p)
{}
};
auto op_state = std::make_shared<OpState>(p->getPrefix(-1));
@@ -456,16 +481,16 @@ void Pht::getRealPrefix(const std::shared_ptr<Prefix>& p, IndexEntry entry, Real
};
/* Lambda will count total number of data node */
auto count = [=]( const std::shared_ptr<dht::Value>& value ) {
auto count = [=](const std::shared_ptr<dht::Value>& value) {
if (value->user_type != canary_)
op_state->entry_count++;
return true;
};
auto on_done = [=] ( bool ) {
auto on_done = [=](bool) {
op_state->ended++;
/* Only the last one do the CallBack*/
if (op_state->ended == 3) {
if (op_state->ended == 3) {
if (op_state->entry_count < MAX_NODE_ENTRY_COUNT)
end_cb(op_state->parent, std::move(entry));
else
@@ -473,34 +498,25 @@ void Pht::getRealPrefix(const std::shared_ptr<Prefix>& p, IndexEntry entry, Real
}
};
dht_->get(op_state->parent.hash(),
count,
on_done,
pht_filter
);
dht_->get(op_state->parent.hash(), count, on_done, pht_filter);
dht_->get(p->hash(),
count,
on_done,
pht_filter
);
dht_->get(p->hash(), count, on_done, pht_filter);
dht_->get(p->getSibling().hash(),
count,
on_done,
pht_filter
);
dht_->get(p->getSibling().hash(), count, on_done, pht_filter);
}
void Pht::checkPhtUpdate(Prefix p, IndexEntry entry, time_point time_p) {
void
Pht::checkPhtUpdate(Prefix p, IndexEntry entry, time_point time_p)
{
Prefix full = entry.prefix;
if ( p.content_.size() * 8 >= full.content_.size() * 8 ) return;
if (p.content_.size() * 8 >= full.content_.size() * 8)
return;
auto next_prefix = full.getPrefix( p.size_ + 1 );
auto next_prefix = full.getPrefix(p.size_ + 1);
dht_->listen(next_prefix.hash(),
[=](const std::shared_ptr<dht::Value> &value) {
dht_->listen(
next_prefix.hash(),
[=](const std::shared_ptr<dht::Value>& value) {
if (value->user_type == canary_) {
insert(full, entry, std::make_shared<int>(0), std::make_shared<int>(full.size_), time_p, false, nullptr);
@@ -513,23 +529,27 @@ void Pht::checkPhtUpdate(Prefix p, IndexEntry entry, time_point time_p) {
[=](const dht::Value& v) {
/* Filter value v thats start with the same name as ours */
return v.user_type.compare(0, name_.size(), name_) == 0;
}
);
});
}
void Pht::split(const Prefix& insert, const std::vector<std::shared_ptr<IndexEntry>>& vals, IndexEntry entry, RealInsertCallback end_cb ) {
void
Pht::split(const Prefix& insert,
const std::vector<std::shared_ptr<IndexEntry>>& vals,
IndexEntry entry,
RealInsertCallback end_cb)
{
const auto full = Prefix(entry.prefix);
auto loc = findSplitLocation(full, vals);
const auto prefix_to_insert = full.getPrefix(loc);
for(;loc != insert.size_ - 1; loc--) {
for (; loc != insert.size_ - 1; loc--) {
updateCanary(full.getPrefix(loc));
}
end_cb(prefix_to_insert, entry);
}
} /* indexation */
} // namespace indexation
} /* dht */
} // namespace dht
+14 -22
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "value.h"
@@ -26,14 +11,20 @@ namespace dht {
/**
* Foreign nodes asking for updates about an InfoHash.
*/
struct Listener {
struct Listener
{
time_point time;
Query query;
int version;
Listener(time_point t, Query&& q, int version = 0) : time(t), query(std::move(q)), version(version) {}
Listener(time_point t, Query&& q, int version = 0)
: time(t)
, query(std::move(q))
, version(version)
{}
void refresh(time_point t, Query&& q) {
void refresh(time_point t, Query&& q)
{
time = t;
query = std::move(q);
}
@@ -42,10 +33,11 @@ struct Listener {
/**
* A single "listen" operation data
*/
struct LocalListener {
struct LocalListener
{
Sp<Query> query;
Value::Filter filter;
ValueCallback get_cb;
};
}
} // namespace dht
+61 -74
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "log.h"
#include "dhtrunner.h"
@@ -36,27 +22,28 @@ namespace log {
* Terminal colors for logging
*/
namespace Color {
enum Code {
FG_RED = 31,
FG_GREEN = 32,
FG_YELLOW = 33,
FG_BLUE = 34,
FG_DEFAULT = 39,
BG_RED = 41,
BG_GREEN = 42,
BG_BLUE = 44,
BG_DEFAULT = 49
};
class Modifier {
const Code code;
public:
constexpr Modifier(Code pCode) : code(pCode) {}
friend std::ostream&
operator<<(std::ostream& os, const Modifier& mod) {
return os << "\033[" << mod.code << 'm';
}
};
}
enum Code {
FG_RED = 31,
FG_GREEN = 32,
FG_YELLOW = 33,
FG_BLUE = 34,
FG_DEFAULT = 39,
BG_RED = 41,
BG_GREEN = 42,
BG_BLUE = 44,
BG_DEFAULT = 49
};
class Modifier
{
const Code code;
public:
constexpr Modifier(Code pCode)
: code(pCode)
{}
friend std::ostream& operator<<(std::ostream& os, const Modifier& mod) { return os << "\033[" << mod.code << 'm'; }
};
} // namespace Color
constexpr const Color::Modifier def(Color::FG_DEFAULT);
constexpr const Color::Modifier red(Color::FG_RED);
@@ -66,7 +53,8 @@ constexpr const Color::Modifier yellow(Color::FG_YELLOW);
* Print va_list to std::ostream (used for logging).
*/
void
printfLog(std::ostream& s, const std::string& message) {
printfLog(std::ostream& s, const std::string& message)
{
using namespace std::chrono;
using log_precision = microseconds;
auto num = duration_cast<log_precision>(steady_clock::now().time_since_epoch()).count();
@@ -75,7 +63,8 @@ printfLog(std::ostream& s, const std::string& message) {
s << message << std::endl;
}
void
printLog(std::ostream& s, fmt::string_view format, fmt::format_args args) {
printLog(std::ostream& s, fmt::string_view format, fmt::format_args args)
{
using namespace std::chrono;
using log_precision = microseconds;
auto num = duration_cast<log_precision>(steady_clock::now().time_since_epoch()).count();
@@ -86,33 +75,31 @@ printLog(std::ostream& s, fmt::string_view format, fmt::format_args args) {
}
std::shared_ptr<Logger>
getStdLogger() {
return std::make_shared<Logger>(
[](LogLevel level, std::string&& message) {
if (level == LogLevel::error)
std::cerr << red;
else if (level == LogLevel::warning)
std::cerr << yellow;
printfLog(std::cerr, message);
std::cerr << def;
}
);
getStdLogger()
{
return std::make_shared<Logger>([](LogLevel level, std::string&& message) {
if (level == LogLevel::error)
std::cerr << red;
else if (level == LogLevel::warning)
std::cerr << yellow;
printfLog(std::cerr, message);
std::cerr << def;
});
}
std::shared_ptr<Logger>
getFileLogger(const std::string &path) {
getFileLogger(const std::string& path)
{
auto logfile = std::make_shared<std::ofstream>();
logfile->open(path, std::ios::out);
return std::make_shared<Logger>(
[logfile](LogLevel /*level*/, std::string&& message) {
printfLog(*logfile, message);
}
);
[logfile](LogLevel /*level*/, std::string&& message) { printfLog(*logfile, message); });
}
#ifndef _WIN32
constexpr
int syslogLevel(LogLevel level) {
constexpr int
syslogLevel(LogLevel level)
{
switch (level) {
case LogLevel::error:
return LOG_ERR;
@@ -126,15 +113,13 @@ int syslogLevel(LogLevel level) {
#endif
std::shared_ptr<Logger>
getSyslogLogger(const char* name) {
getSyslogLogger(const char* name)
{
#ifndef _WIN32
struct Syslog {
explicit Syslog(const char* n) {
openlog(n, LOG_NDELAY, LOG_USER);
}
~Syslog() {
closelog();
}
struct Syslog
{
explicit Syslog(const char* n) { openlog(n, LOG_NDELAY, LOG_USER); }
~Syslog() { closelog(); }
};
// syslog is global. Existing instance must be reused.
static std::weak_ptr<Syslog> opened_logfile;
@@ -144,33 +129,35 @@ getSyslogLogger(const char* name) {
opened_logfile = logfile;
}
return std::make_shared<Logger>(
[logfile](LogLevel level, std::string&& message) {
syslog(syslogLevel(level), "%s", message.c_str());
});
[logfile](LogLevel level, std::string&& message) { syslog(syslogLevel(level), "%s", message.c_str()); });
#else
return getStdLogger();
#endif
}
void
enableLogging(dht::DhtRunner &dht) {
enableLogging(dht::DhtRunner& dht)
{
dht.setLogger(getStdLogger());
}
void
enableFileLogging(dht::DhtRunner &dht, const std::string &path) {
enableFileLogging(dht::DhtRunner& dht, const std::string& path)
{
dht.setLogger(getFileLogger(path));
}
OPENDHT_PUBLIC void
enableSyslog(dht::DhtRunner &dht, const char* name) {
enableSyslog(dht::DhtRunner& dht, const char* name)
{
dht.setLogger(getSyslogLogger(name));
}
void
disableLogging(dht::DhtRunner &dht) {
disableLogging(dht::DhtRunner& dht)
{
dht.setLogger();
}
}
}
} // namespace log
} // namespace dht
+3 -18
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
namespace dht {
@@ -35,4 +20,4 @@ enum class MessageType {
};
} /* namespace net */
} /* dht */
} // namespace dht
+650 -442
View File
File diff suppressed because it is too large. Load diff
+2 -16
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "network_utils.h"
+20 -29
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "node.h"
#include "request.h"
@@ -29,24 +14,28 @@ constexpr std::chrono::minutes Node::NODE_GOOD_TIME;
constexpr std::chrono::seconds Node::MAX_RESPONSE_TIME;
Node::Node(const InfoHash& id, const SockAddr& addr, std::mt19937_64& rd, bool client)
: id(id), addr(addr), is_client(client), sockets_()
: id(id)
, addr(addr)
, is_client(client)
, sockets_()
{
transaction_id = std::uniform_int_distribution<Tid>{1}(rd);
transaction_id = std::uniform_int_distribution<Tid> {1}(rd);
}
Node::Node(const InfoHash& id, SockAddr&& addr, std::mt19937_64& rd, bool client)
: id(id), addr(std::move(addr)), is_client(client), sockets_()
: id(id)
, addr(std::move(addr))
, is_client(client)
, sockets_()
{
transaction_id = std::uniform_int_distribution<Tid>{1}(rd);
transaction_id = std::uniform_int_distribution<Tid> {1}(rd);
}
/* This is our definition of a known-good node. */
bool
Node::isGood(time_point now) const
{
return not expired_ &&
reply_time >= now - NODE_GOOD_TIME &&
time >= now - NODE_EXPIRE_TIME;
return not expired_ && reply_time >= now - NODE_GOOD_TIME && time >= now - NODE_EXPIRE_TIME;
}
bool
@@ -151,7 +140,7 @@ Node::closeSocket(Tid id)
{
if (id) {
sockets_.erase(id);
//DHT_LOG.w("Closing socket (tid: %d), %lu remaining", socket->id, sockets_.size());
// DHT_LOG.w("Closing socket (tid: %d), %lu remaining", socket->id, sockets_.size());
}
}
@@ -163,7 +152,8 @@ Node::toString() const
return ss.str();
}
std::ostream& operator<< (std::ostream& s, const Node& h)
std::ostream&
operator<<(std::ostream& s, const Node& h)
{
s << h.id << " " << h.addr.toString();
return s;
@@ -186,13 +176,14 @@ NodeExport::msgpack_unpack(msgpack::object o)
if (maddr.via.bin.size > sizeof(sockaddr_storage))
throw msgpack::type_error();
id.msgpack_unpack(o.via.map.ptr[0].val);
addr = {(const sockaddr*)maddr.via.bin.ptr, (socklen_t)maddr.via.bin.size};
addr = {(const sockaddr*) maddr.via.bin.ptr, (socklen_t) maddr.via.bin.size};
}
std::ostream& operator<< (std::ostream& s, const NodeExport& h)
std::ostream&
operator<<(std::ostream& s, const NodeExport& h)
{
msgpack::pack(s, h);
return s;
}
}
} // namespace dht
+21 -28
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "node_cache.h"
@@ -29,12 +15,14 @@ NodeCache::~NodeCache()
}
Sp<Node>
NodeCache::getNode(const InfoHash& id, sa_family_t family) {
NodeCache::getNode(const InfoHash& id, sa_family_t family)
{
return cache(family).getNode(id);
}
Sp<Node>
NodeCache::getNode(const InfoHash& id, const SockAddr& addr, time_point now, bool confirm, bool client) {
NodeCache::getNode(const InfoHash& id, const SockAddr& addr, time_point now, bool confirm, bool client)
{
if (not id)
return std::make_shared<Node>(id, addr, rd, client);
return cache(addr.getFamily()).getNode(id, addr, now, confirm, client, rd);
@@ -58,8 +46,7 @@ NodeCache::NodeMap::getCachedNodes(const InfoHash& id, size_t count) const
return ret;
};
auto it_p = lower_bound(id),
it_n = it_p;
auto it_p = lower_bound(id), it_n = it_p;
if (not empty())
dec_it(it_p); /* Create 2 separate iterator if we could */
@@ -67,12 +54,15 @@ NodeCache::NodeMap::getCachedNodes(const InfoHash& id, size_t count) const
/* If one of the iterator is at the end, then take the other one
If they are both in middle of somewhere comapre both and take
the closest to the id. */
if (it_p == cend()) it = it_n++;
else if (it_n == cend()) it = dec_it(it_p);
else it = id.xorCmp(it_p->first, it_n->first) < 0 ? dec_it(it_p) : it_n++;
if (it_p == cend())
it = it_n++;
else if (it_n == cend())
it = dec_it(it_p);
else
it = id.xorCmp(it_p->first, it_n->first) < 0 ? dec_it(it_p) : it_n++;
if (auto n = it->second.lock())
if ( not n->isExpired() and not n->isClient() )
if (not n->isExpired() and not n->isClient())
nodes.emplace_back(std::move(n));
}
@@ -103,7 +93,8 @@ NodeCache::NodeMap::getNode(const InfoHash& id)
}
Sp<Node>
NodeCache::NodeMap::getNode(const InfoHash& id, const SockAddr& addr, time_point now, bool confirm, bool client, std::mt19937_64& rd)
NodeCache::NodeMap::getNode(
const InfoHash& id, const SockAddr& addr, time_point now, bool confirm, bool client, std::mt19937_64& rd)
{
auto& nref = (*this)[id];
auto node = nref.lock();
@@ -121,7 +112,8 @@ NodeCache::NodeMap::getNode(const InfoHash& id, const SockAddr& addr, time_point
}
void
NodeCache::NodeMap::clearBadNodes() {
NodeCache::NodeMap::clearBadNodes()
{
for (auto it = cbegin(); it != cend();) {
if (auto n = it->second.lock()) {
n->reset();
@@ -134,7 +126,8 @@ NodeCache::NodeMap::clearBadNodes() {
}
void
NodeCache::NodeMap::setExpired() {
NodeCache::NodeMap::setExpired()
{
for (auto& wn : *this)
if (auto n = wn.second.lock())
n->setExpired();
@@ -156,4 +149,4 @@ NodeCache::NodeMap::cleanup()
}
}
}
} // namespace dht
+35 -37
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "op_cache.h"
namespace dht {
@@ -21,7 +7,8 @@ namespace dht {
constexpr const std::chrono::seconds OpCache::EXPIRATION;
bool
OpValueCache::onValuesAdded(const std::vector<Sp<Value>>& vals, const system_clock::time_point& t) {
OpValueCache::onValuesAdded(const std::vector<Sp<Value>>& vals, const system_clock::time_point& t)
{
std::vector<Sp<Value>> newValues;
for (const auto& v : vals) {
auto viop = values.emplace(v->id, v);
@@ -42,14 +29,15 @@ OpValueCache::onValuesAdded(const std::vector<Sp<Value>>& vals, const system_clo
}
bool
OpValueCache::onValuesExpired(const std::vector<Sp<Value>>& vals, const system_clock::time_point& t) {
OpValueCache::onValuesExpired(const std::vector<Sp<Value>>& vals, const system_clock::time_point& t)
{
std::vector<Sp<Value>> expiredValues;
for (const auto& v : vals) {
auto vit = values.find(v->id);
if (vit != values.end()) {
if (vit->second.updated > t)
continue;
vit->second.updated = t;
vit->second.refCount--;
if (not vit->second.refCount) {
@@ -70,7 +58,7 @@ OpValueCache::onValuesExpired(const std::vector<Value::Id>& vids, const system_c
if (vit != values.end()) {
if (vit->second.updated > t)
continue;
vit->second.updated = t;
vit->second.refCount--;
if (not vit->second.refCount) {
@@ -83,7 +71,8 @@ OpValueCache::onValuesExpired(const std::vector<Value::Id>& vids, const system_c
}
std::vector<Sp<Value>>
OpValueCache::get(const Value::Filter& filter) const {
OpValueCache::get(const Value::Filter& filter) const
{
std::vector<Sp<Value>> ret;
if (not filter)
ret.reserve(values.size());
@@ -94,7 +83,8 @@ OpValueCache::get(const Value::Filter& filter) const {
}
Sp<Value>
OpValueCache::get(Value::Id id) const {
OpValueCache::get(Value::Id id) const
{
auto v = values.find(id);
if (v == values.end())
return {};
@@ -102,7 +92,8 @@ OpValueCache::get(Value::Id id) const {
}
std::vector<Sp<Value>>
OpValueCache::getValues() const {
OpValueCache::getValues() const
{
std::vector<Sp<Value>> ret;
ret.reserve(values.size());
for (const auto& v : values)
@@ -111,7 +102,8 @@ OpValueCache::getValues() const {
}
void
OpCache::onValuesAdded(const std::vector<Sp<Value>>& vals) {
OpCache::onValuesAdded(const std::vector<Sp<Value>>& vals)
{
if (not listeners.empty()) {
std::vector<LocalListener> list;
list.reserve(listeners.size());
@@ -123,7 +115,8 @@ OpCache::onValuesAdded(const std::vector<Sp<Value>>& vals) {
}
void
OpCache::onValuesExpired(const std::vector<Sp<Value>>& vals) {
OpCache::onValuesExpired(const std::vector<Sp<Value>>& vals)
{
if (not listeners.empty()) {
std::vector<LocalListener> list;
list.reserve(listeners.size());
@@ -135,7 +128,8 @@ OpCache::onValuesExpired(const std::vector<Sp<Value>>& vals) {
}
time_point
OpCache::getExpiration() const {
OpCache::getExpiration() const
{
if (not listeners.empty())
return time_point::max();
return lastRemoved + EXPIRATION;
@@ -182,11 +176,10 @@ SearchCache::listen(const ValueCallback& get_cb, const Sp<Query>& q, Value::Filt
// New query
op = ops.emplace(q, std::make_unique<OpCache>()).first;
auto& cache = *op->second;
cache.searchToken = onListen(q, [&](const std::vector<Sp<Value>>& values, bool expired){
return cache.onValue(values, expired);
}, [&](ListenSyncStatus status) {
cache.onNodeChanged(status);
});
cache.searchToken = onListen(
q,
[&](const std::vector<Sp<Value>>& values, bool expired) { return cache.onValue(values, expired); },
[&](ListenSyncStatus status) { cache.onNodeChanged(status); });
}
auto token = nextToken_++;
if (nextToken_ == 0)
@@ -195,7 +188,8 @@ SearchCache::listen(const ValueCallback& get_cb, const Sp<Query>& q, Value::Filt
}
bool
SearchCache::cancelListen(size_t gtoken, const time_point& now) {
SearchCache::cancelListen(size_t gtoken, const time_point& now)
{
for (auto& op : ops) {
if (op.second->removeListener(gtoken, now)) {
nextExpiration_ = std::min(nextExpiration_, op.second->getExpiration());
@@ -206,7 +200,8 @@ SearchCache::cancelListen(size_t gtoken, const time_point& now) {
}
void
SearchCache::cancelAll(const std::function<void(size_t)>& onCancel) {
SearchCache::cancelAll(const std::function<void(size_t)>& onCancel)
{
for (auto& op : ops) {
auto cache = std::move(op.second);
cache->removeAll();
@@ -216,7 +211,8 @@ SearchCache::cancelAll(const std::function<void(size_t)>& onCancel) {
}
time_point
SearchCache::expire(const time_point& now, const std::function<void(size_t)>& onCancel) {
SearchCache::expire(const time_point& now, const std::function<void(size_t)>& onCancel)
{
nextExpiration_ = time_point::max();
auto ret = nextExpiration_;
for (auto it = ops.begin(); it != ops.end();) {
@@ -249,7 +245,8 @@ SearchCache::get(const Value::Filter& f, const Sp<Query>& q, const GetCallback&
}
std::vector<Sp<Value>>
SearchCache::get(const Value::Filter& filter) const {
SearchCache::get(const Value::Filter& filter) const
{
if (ops.size() == 1)
return ops.begin()->second->get(filter);
std::map<Value::Id, Sp<Value>> c;
@@ -265,11 +262,12 @@ SearchCache::get(const Value::Filter& filter) const {
}
Sp<Value>
SearchCache::get(Value::Id id) const {
SearchCache::get(Value::Id id) const
{
for (const auto& op : ops)
if (auto v = op.second->get(id))
return v;
return {};
}
}
} // namespace dht
+75 -81
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "value.h"
@@ -27,21 +13,28 @@ struct OpCacheValueStorage
Sp<Value> data {};
unsigned refCount {1};
system_clock::time_point updated {system_clock::time_point::min()};
explicit OpCacheValueStorage(Sp<Value> val) : data(std::move(val)) {}
explicit OpCacheValueStorage(Sp<Value> val)
: data(std::move(val))
{}
};
class OpValueCache {
class OpValueCache
{
public:
OpValueCache(ValueCallback&& cb) noexcept : callback(std::forward<ValueCallback>(cb)) {}
explicit OpValueCache(OpValueCache&& o) noexcept : values(std::move(o.values)), callback(std::move(o.callback)) {
OpValueCache(ValueCallback&& cb) noexcept
: callback(std::forward<ValueCallback>(cb))
{}
explicit OpValueCache(OpValueCache&& o) noexcept
: values(std::move(o.values))
, callback(std::move(o.callback))
{
o.callback = {};
}
static ValueCallback cacheCallback(ValueCallback&& cb, std::function<void()>&& onCancel) {
return [
cache = std::make_shared<OpValueCache>(std::forward<ValueCallback>(cb)),
onCancel = std::move(onCancel)
](const std::vector<Sp<Value>>& vals, bool expired){
static ValueCallback cacheCallback(ValueCallback&& cb, std::function<void()>&& onCancel)
{
return [cache = std::make_shared<OpValueCache>(std::forward<ValueCallback>(cb)),
onCancel = std::move(onCancel)](const std::vector<Sp<Value>>& vals, bool expired) {
auto ret = cache->onValue(vals, expired);
if (not ret)
onCancel();
@@ -49,22 +42,35 @@ public:
};
}
bool onValue(const std::vector<Sp<Value>>& vals, bool expired, const system_clock::time_point& t = system_clock::time_point::min()) {
return expired
? onValuesExpired(vals, t)
: onValuesAdded(vals, t);
bool onValue(const std::vector<Sp<Value>>& vals,
bool expired,
const system_clock::time_point& t = system_clock::time_point::min())
{
return expired ? onValuesExpired(vals, t) : onValuesAdded(vals, t);
}
bool onValuesAdded(const std::vector<Sp<Value>>& vals, const system_clock::time_point& t = system_clock::time_point::min());
bool onValuesExpired(const std::vector<Sp<Value>>& vals, const system_clock::time_point& t = system_clock::time_point::min());
bool onValuesExpired(const std::vector<Value::Id>& vals, const system_clock::time_point& t = system_clock::time_point::min());
bool onValuesAdded(const std::vector<Sp<Value>>& vals,
const system_clock::time_point& t = system_clock::time_point::min());
bool onValuesExpired(const std::vector<Sp<Value>>& vals,
const system_clock::time_point& t = system_clock::time_point::min());
bool onValuesExpired(const std::vector<Value::Id>& vals,
const system_clock::time_point& t = system_clock::time_point::min());
void onNodeChanged(ListenSyncStatus status) {
void onNodeChanged(ListenSyncStatus status)
{
switch (status) {
case ListenSyncStatus::ADDED: nodes++; break;
case ListenSyncStatus::REMOVED: nodes--; break;
case ListenSyncStatus::SYNCED : syncedNodes++; break;
case ListenSyncStatus::UNSYNCED: syncedNodes--; break;
case ListenSyncStatus::ADDED:
nodes++;
break;
case ListenSyncStatus::REMOVED:
nodes--;
break;
case ListenSyncStatus::SYNCED:
syncedNodes++;
break;
case ListenSyncStatus::UNSYNCED:
syncedNodes--;
break;
}
}
@@ -85,70 +91,58 @@ private:
ValueCallback callback;
};
class OpCache {
class OpCache
{
public:
OpCache() : cache([this](const std::vector<Sp<Value>>& vals, bool expired){
if (expired)
onValuesExpired(vals);
else
onValuesAdded(vals);
return true;
}) {}
OpCache()
: cache([this](const std::vector<Sp<Value>>& vals, bool expired) {
if (expired)
onValuesExpired(vals);
else
onValuesAdded(vals);
return true;
})
{}
bool onValue(const std::vector<Sp<Value>>& vals, bool expired) {
return cache.onValue(vals, expired);
}
void onNodeChanged(ListenSyncStatus status) {
cache.onNodeChanged(status);
}
bool onValue(const std::vector<Sp<Value>>& vals, bool expired) { return cache.onValue(vals, expired); }
void onNodeChanged(ListenSyncStatus status) { cache.onNodeChanged(status); }
void onValuesAdded(const std::vector<Sp<Value>>& vals);
void onValuesExpired(const std::vector<Sp<Value>>& vals);
bool addListener(size_t token, const ValueCallback& cb, const Sp<Query>& q, Value::Filter&& filter) {
bool addListener(size_t token, const ValueCallback& cb, const Sp<Query>& q, Value::Filter&& filter)
{
auto cached = cache.get(filter);
if (not cached.empty() and not cb(cached, false)) {
return false;
}
listeners.emplace(token, LocalListener{q, std::move(filter), cb});
listeners.emplace(token, LocalListener {q, std::move(filter), cb});
return true;
}
bool removeListener(size_t token, const time_point& now) {
bool removeListener(size_t token, const time_point& now)
{
lastRemoved = now;
return listeners.erase(token) > 0;
}
void removeAll() {
listeners.clear();
}
void removeAll() { listeners.clear(); }
bool isDone() {
return listeners.empty();
}
bool isDone() { return listeners.empty(); }
std::vector<Sp<Value>> get(const Value::Filter& filter) const {
return cache.get(filter);
}
std::vector<Sp<Value>> get(const Value::Filter& filter) const { return cache.get(filter); }
Sp<Value> get(Value::Id id) const {
return cache.get(id);
}
Sp<Value> get(Value::Id id) const { return cache.get(id); }
bool isSynced() const {
return cache.isSynced();
}
bool isSynced() const { return cache.isSynced(); }
bool isExpired(const time_point& now) const {
return listeners.empty() and (lastRemoved + EXPIRATION < now);
}
bool isExpired(const time_point& now) const { return listeners.empty() and (lastRemoved + EXPIRATION < now); }
time_point getExpiration() const;
size_t size() const {
return cache.size();
}
size_t size() const { return cache.size(); }
size_t searchToken {0};
private:
constexpr static const std::chrono::seconds EXPIRATION {60};
OpCache(const OpCache&) = delete;
@@ -159,7 +153,8 @@ private:
time_point lastRemoved {clock::now()};
};
class SearchCache {
class SearchCache
{
public:
SearchCache() {}
SearchCache(SearchCache&&) = default;
@@ -171,15 +166,14 @@ public:
void cancelAll(const std::function<void(size_t)>& onCancel);
time_point expire(const time_point& now, const std::function<void(size_t)>& onCancel);
time_point getExpiration() const {
return nextExpiration_;
}
time_point getExpiration() const { return nextExpiration_; }
bool get(const Value::Filter& f, const Sp<Query>& q, const GetCallback& gcb, const DoneCallback& dcb) const;
std::vector<Sp<Value>> get(const Value::Filter& filter) const;
Sp<Value> get(Value::Id id) const;
std::pair<size_t, size_t> size() const {
std::pair<size_t, size_t> size() const
{
size_t tot = 0;
for (const auto& c : ops)
tot += c.second->size();
@@ -199,4 +193,4 @@ private:
time_point nextExpiration_ {time_point::max()};
};
}
} // namespace dht
+32 -40
View File
@@ -1,20 +1,6 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "infohash.h"
#include "sockaddr.h"
@@ -66,7 +52,9 @@ static constexpr auto QUERY_PUT = "put"sv;
static constexpr auto QUERY_LISTEN = "listen"sv;
static constexpr auto QUERY_REFRESH = "refresh"sv;
Tid unpackTid(const msgpack::object& o) {
Tid
unpackTid(const msgpack::object& o)
{
switch (o.type) {
case msgpack::type::POSITIVE_INTEGER:
return o.as<Tid>();
@@ -75,7 +63,8 @@ Tid unpackTid(const msgpack::object& o) {
}
}
struct ParsedMessage {
struct ParsedMessage
{
MessageType type;
/* Node ID of the sender */
InfoHash id;
@@ -96,7 +85,7 @@ struct ParsedMessage {
/* the value id (announce confirmation) */
Value::Id value_id {0};
/* time when value was first created */
time_point created { time_point::max() };
time_point created {time_point::max()};
/* IPv4 nodes in response to a 'find' request */
Blob nodes4_raw, nodes6_raw;
std::vector<Sp<Node>> nodes4, nodes6;
@@ -131,18 +120,15 @@ ParsedMessage::append(const ParsedMessage& block)
bool ret(false);
for (const auto& ve : block.value_parts) {
auto part_val = value_parts.find(ve.first);
if (part_val == value_parts.end()
|| part_val->second.second.size() >= part_val->second.first)
if (part_val == value_parts.end() || part_val->second.second.size() >= part_val->second.first)
continue;
// TODO: handle out-of-order packets
if (ve.second.first != part_val->second.second.size()) {
//std::cout << "skipping out-of-order packet" << std::endl;
// std::cout << "skipping out-of-order packet" << std::endl;
continue;
}
ret = true;
part_val->second.second.insert(part_val->second.second.end(),
ve.second.second.begin(),
ve.second.second.end());
part_val->second.second.insert(part_val->second.second.end(), ve.second.second.begin(), ve.second.second.end());
}
return ret;
}
@@ -152,13 +138,14 @@ ParsedMessage::complete()
{
for (auto& e : value_parts) {
if (e.second.first > e.second.second.size()) {
//std::cout << "uncomplete part " << e.first << ": " << e.second.second.size() << "/" << e.second.first << std::endl;
// std::cout << "uncomplete part " << e.first << ": " << e.second.second.size() << "/" << e.second.first <<
// std::endl;
return false;
}
}
for (auto& e : value_parts) {
msgpack::unpacked msg;
msgpack::unpack(msg, (const char*)e.second.second.data(), e.second.second.size());
msgpack::unpack(msg, (const char*) e.second.second.data(), e.second.second.size());
values.emplace_back(std::make_shared<Value>(msg.get()));
}
return true;
@@ -167,9 +154,11 @@ ParsedMessage::complete()
void
ParsedMessage::msgpack_unpack(const msgpack::object& msg)
{
if (msg.type != msgpack::type::MAP) throw msgpack::type_error();
if (msg.type != msgpack::type::MAP)
throw msgpack::type_error();
struct ParsedMsg {
struct ParsedMsg
{
msgpack::object* y;
msgpack::object* r;
msgpack::object* u;
@@ -259,7 +248,8 @@ ParsedMessage::msgpack_unpack(const msgpack::object& msg)
error_code = parsed.e->via.array.ptr[0].as<uint16_t>();
}
struct ParsedReq {
struct ParsedReq
{
msgpack::object* values;
msgpack::object* fields;
msgpack::object* sa;
@@ -315,12 +305,12 @@ ParsedMessage::msgpack_unpack(const msgpack::object& msg)
addr.setFamily(AF_INET);
auto& a = addr.getIPv4();
a.sin_port = 0;
std::copy_n(parsedReq.sa->via.bin.ptr, l, (char*)&a.sin_addr);
std::copy_n(parsedReq.sa->via.bin.ptr, l, (char*) &a.sin_addr);
} else if (l == sizeof(in6_addr)) {
addr.setFamily(AF_INET6);
auto& a = addr.getIPv6();
a.sin6_port = 0;
std::copy_n(parsedReq.sa->via.bin.ptr, l, (char*)&a.sin6_addr);
std::copy_n(parsedReq.sa->via.bin.ptr, l, (char*) &a.sin6_addr);
}
} else
addr = {};
@@ -334,12 +324,12 @@ ParsedMessage::msgpack_unpack(const msgpack::object& msg)
// Skip oversize values with a small margin for header overhead
if (packed_v.via.u64 > MAX_VALUE_SIZE + 32)
continue;
value_parts.emplace(i, std::make_pair(packed_v.via.u64, Blob{}));
value_parts.emplace(i, std::make_pair(packed_v.via.u64, Blob {}));
} else {
try {
values.emplace_back(std::make_shared<Value>(parsedReq.values->via.array.ptr[i]));
} catch (const std::exception& e) {
//DHT_LOG_WARN("Error reading value: %s", e.what());
// DHT_LOG_WARN("Error reading value: %s", e.what());
}
}
}
@@ -353,9 +343,10 @@ ParsedMessage::msgpack_unpack(const msgpack::object& msg)
for (size_t i = 0; i < val_num; ++i) {
try {
auto v = std::make_shared<FieldValueIndex>();
v->msgpack_unpack_fields(vfields, *rvalues, i*vfields.size());
v->msgpack_unpack_fields(vfields, *rvalues, i * vfields.size());
fields.emplace_back(std::move(v));
} catch (const std::exception& e) { }
} catch (const std::exception& e) {
}
}
}
} else {
@@ -367,15 +358,16 @@ ParsedMessage::msgpack_unpack(const msgpack::object& msg)
if (parsedReq.want->type != msgpack::type::ARRAY)
throw msgpack::type_error();
want = 0;
for (unsigned i=0; i<parsedReq.want->via.array.size; i++) {
for (unsigned i = 0; i < parsedReq.want->via.array.size; i++) {
auto& val = parsedReq.want->via.array.ptr[i];
try {
auto w = val.as<sa_family_t>();
if (w == AF_INET)
want |= WANT4;
else if(w == AF_INET6)
else if (w == AF_INET6)
want |= WANT6;
} catch (const std::exception& e) {};
} catch (const std::exception& e) {
};
}
} else {
want = -1;
+125 -127
View File
@@ -1,21 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
* Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
* Adrien Béraud <adrien.beraud@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "peer_discovery.h"
#include "network_utils.h"
@@ -38,11 +22,14 @@ constexpr char MULTICAST_ADDRESS_IPV6[] = "ff08::101";
class PeerDiscovery::DomainPeerDiscovery
{
public:
DomainPeerDiscovery(asio::ip::udp domain, in_port_t port, Sp<asio::io_context> ioContext = {}, Sp<Logger> logger = {});
DomainPeerDiscovery(asio::ip::udp domain,
in_port_t port,
Sp<asio::io_context> ioContext = {},
Sp<Logger> logger = {});
~DomainPeerDiscovery();
void startDiscovery(std::string_view type, ServiceDiscoveredCallback callback);
void startPublish(std::string_view type, const msgpack::sbuffer &pack_buf);
void startPublish(std::string_view type, const msgpack::sbuffer& pack_buf);
void stop();
@@ -52,20 +39,19 @@ public:
void connectivityChanged();
void stopConnectivityChanged();
private:
Sp<Logger> logger_;
//dmtx_ for callbackmap_ and drunning_ (write)
// dmtx_ for callbackmap_ and drunning_ (write)
std::mutex dmtx_;
//mtx_ for messages_ and lrunning (listen)
// mtx_ for messages_ and lrunning (listen)
std::mutex mtx_;
std::shared_ptr<asio::io_context> ioContext_;
static constexpr dht::duration PeerDiscovery_PERIOD_MAX{
std::chrono::minutes(1)};
static constexpr std::chrono::seconds PeerDiscovery_PERIOD{10};
static constexpr dht::duration PeerDiscovery_PERIOD_MAX {std::chrono::minutes(1)};
static constexpr std::chrono::seconds PeerDiscovery_PERIOD {10};
asio::steady_timer peerDiscoveryTimer;
std::chrono::steady_clock::duration peerDiscovery_period{
PeerDiscovery_PERIOD};
std::chrono::steady_clock::duration peerDiscovery_period {PeerDiscovery_PERIOD};
asio::ip::udp::socket sockFd_;
asio::ip::udp::endpoint sockAddrSend_;
@@ -91,13 +77,16 @@ private:
void reDiscover();
};
PeerDiscovery::DomainPeerDiscovery::DomainPeerDiscovery(asio::ip::udp domain, in_port_t port, Sp<asio::io_context> ioContext, Sp<Logger> logger)
PeerDiscovery::DomainPeerDiscovery::DomainPeerDiscovery(asio::ip::udp domain,
in_port_t port,
Sp<asio::io_context> ioContext,
Sp<Logger> logger)
: logger_(logger)
, ioContext_(ioContext)
, peerDiscoveryTimer(*ioContext_)
, sockFd_(*ioContext_, domain)
, sockAddrSend_(asio::ip::make_address(domain.family() == AF_INET ? MULTICAST_ADDRESS_IPV4
: MULTICAST_ADDRESS_IPV6), port)
, sockAddrSend_(asio::ip::make_address(domain.family() == AF_INET ? MULTICAST_ADDRESS_IPV4 : MULTICAST_ADDRESS_IPV6),
port)
{
try {
sockFd_.set_option(asio::ip::multicast::join_group(sockAddrSend_.address()));
@@ -105,23 +94,23 @@ PeerDiscovery::DomainPeerDiscovery::DomainPeerDiscovery(asio::ip::udp domain, in
sockFd_.bind({domain, port});
} catch (const std::exception& e) {
#ifdef __ANDROID__
if(domain.family() == AF_INET && strcmp(e.what(), "set_option: No such device") == 0) {
try{
if (domain.family() == AF_INET && strcmp(e.what(), "set_option: No such device") == 0) {
try {
sockFd_.set_option(asio::ip::udp::socket::reuse_address(true));
auto mc_interface = workaround::get_interface();
sockFd_.set_option(asio::ip::multicast::outbound_interface(mc_interface));
sockFd_.set_option(asio::ip::multicast::join_group(sockAddrSend_.address().to_v4(), mc_interface));
sockFd_.bind({domain, port});
} catch(const std::exception& e){
} catch (const std::exception& e) {
if (logger_)
logger_->error("Unable to start peer discovery using android workaround: {}", e.what());
}
}
else
} else
#endif
if (logger_)
if (logger_)
logger_->error("Unable to start peer discovery for {}: {}",
domain.family() == AF_INET ? "IPv4" : "IPv6", e.what());
domain.family() == AF_INET ? "IPv4" : "IPv6",
e.what());
}
}
@@ -138,10 +127,10 @@ PeerDiscovery::DomainPeerDiscovery::startDiscovery(std::string_view type, Servic
callbackmap_[std::string(type)] = callback;
if (not drunning_) {
drunning_ = true;
asio::post(*ioContext_, [this] () {
loopListener();
query(sockAddrSend_);
});
asio::post(*ioContext_, [this]() {
loopListener();
query(sockAddrSend_);
});
}
}
@@ -151,47 +140,50 @@ PeerDiscovery::DomainPeerDiscovery::loopListener()
std::lock_guard<std::mutex> lck(dmtx_);
if (not drunning_)
return;
sockFd_.async_receive_from(asio::buffer(receiveBuf_), receiveFrom_, [this](const asio::error_code& error, size_t bytes) {
if (error == asio::error::operation_aborted)
return;
if (error) {
if (logger_)
logger_->e("Error receiving message: %s", error.message().c_str());
}
try {
auto rcv = msgpack::unpack(receiveBuf_.data(), bytes);
msgpack::object obj = rcv.get();
sockFd_.async_receive_from(asio::buffer(receiveBuf_),
receiveFrom_,
[this](const asio::error_code& error, size_t bytes) {
if (error == asio::error::operation_aborted)
return;
if (error) {
if (logger_)
logger_->e("Error receiving message: %s", error.message().c_str());
}
try {
auto rcv = msgpack::unpack(receiveBuf_.data(), bytes);
msgpack::object obj = rcv.get();
if (obj.type == msgpack::type::STR) {
if (lrunning_ and obj.as<std::string_view>() == "q"sv)
publish(receiveFrom_);
} else if (obj.type == msgpack::type::MAP) {
for (unsigned i = 0; i < obj.via.map.size; i++) {
auto& o = obj.via.map.ptr[i];
if (o.key.type != msgpack::type::STR)
continue;
ServiceDiscoveredCallback cb;
{
std::lock_guard<std::mutex> lck(dmtx_);
if (drunning_) {
auto callback = callbackmap_.find(o.key.as<std::string_view>());
if (callback != callbackmap_.end())
cb = callback->second;
} else
return;
}
if (cb)
cb(std::move(o.val), SockAddr{ receiveFrom_.data(), (socklen_t)receiveFrom_.size() });
}
} else {
throw msgpack::type_error{};
}
} catch (const std::exception& e) {
if (logger_)
logger_->e("Error receiving packet: %s", e.what());
}
loopListener();
});
if (obj.type == msgpack::type::STR) {
if (lrunning_ and obj.as<std::string_view>() == "q"sv)
publish(receiveFrom_);
} else if (obj.type == msgpack::type::MAP) {
for (unsigned i = 0; i < obj.via.map.size; i++) {
auto& o = obj.via.map.ptr[i];
if (o.key.type != msgpack::type::STR)
continue;
ServiceDiscoveredCallback cb;
{
std::lock_guard<std::mutex> lck(dmtx_);
if (drunning_) {
auto callback = callbackmap_.find(o.key.as<std::string_view>());
if (callback != callbackmap_.end())
cb = callback->second;
} else
return;
}
if (cb)
cb(std::move(o.val),
SockAddr {receiveFrom_.data(), (socklen_t) receiveFrom_.size()});
}
} else {
throw msgpack::type_error {};
}
} catch (const std::exception& e) {
if (logger_)
logger_->e("Error receiving packet: %s", e.what());
}
loopListener();
});
}
void
@@ -204,15 +196,14 @@ PeerDiscovery::DomainPeerDiscovery::query(const asio::ip::udp::endpoint& peer)
msgpack::sbuffer pbuf_request;
msgpack::pack(pbuf_request, "q");
sockFd_.async_send_to(asio::buffer(pbuf_request.data(), pbuf_request.size()), peer,
[logger=logger_, peer] (const asio::error_code& ec, size_t)
{
if (ec and (ec != asio::error::operation_aborted) and logger)
logger->w("Error sending packet to: %s with err: %s",
peer.address().to_string().c_str(),
ec.message().c_str());
}
);
sockFd_.async_send_to(asio::buffer(pbuf_request.data(), pbuf_request.size()),
peer,
[logger = logger_, peer](const asio::error_code& ec, size_t) {
if (ec and (ec != asio::error::operation_aborted) and logger)
logger->w("Error sending packet to: %s with err: %s",
peer.address().to_string().c_str(),
ec.message().c_str());
});
}
void
@@ -222,20 +213,18 @@ PeerDiscovery::DomainPeerDiscovery::publish(const asio::ip::udp::endpoint& peer)
if (not lrunning_)
return;
sockFd_.async_send_to(asio::buffer((const void*)sbuf_.data(), sbuf_.size()), peer,
[logger=logger_, peer] (const asio::error_code& ec, size_t)
{
if (ec and (ec != asio::error::operation_aborted) and logger)
logger->w("Error sending packet to: %s with err: %s",
peer.address().to_string().c_str(),
ec.message().c_str());
}
);
sockFd_.async_send_to(asio::buffer((const void*) sbuf_.data(), sbuf_.size()),
peer,
[logger = logger_, peer](const asio::error_code& ec, size_t) {
if (ec and (ec != asio::error::operation_aborted) and logger)
logger->w("Error sending packet to: %s with err: %s",
peer.address().to_string().c_str(),
ec.message().c_str());
});
}
void
PeerDiscovery::DomainPeerDiscovery::startPublish(std::string_view type, const msgpack::sbuffer &pack_buf)
PeerDiscovery::DomainPeerDiscovery::startPublish(std::string_view type, const msgpack::sbuffer& pack_buf)
{
msgpack::sbuffer pack_buf_c(pack_buf.size());
pack_buf_c.write(pack_buf.data(), pack_buf.size());
@@ -244,7 +233,7 @@ PeerDiscovery::DomainPeerDiscovery::startPublish(std::string_view type, const ms
messages_[std::string(type)] = std::move(pack_buf_c);
reloadMessages();
lrunning_ = true;
asio::post(*ioContext_, [this] () { publish(sockAddrSend_); });
asio::post(*ioContext_, [this]() { publish(sockAddrSend_); });
}
bool
@@ -321,26 +310,23 @@ PeerDiscovery::DomainPeerDiscovery::reDiscover()
sockFd_.set_option(asio::ip::multicast::join_group(sockAddrSend_.address()), ec);
if (ec and logger_)
logger_->w("Unable to multicast on %s: %s",
sockAddrSend_.address().to_string().c_str(),
ec.message().c_str());
logger_->w("Unable to multicast on %s: %s", sockAddrSend_.address().to_string().c_str(), ec.message().c_str());
query(sockAddrSend_);
}
void
PeerDiscovery::DomainPeerDiscovery::connectivityChanged()
{
asio::post(*ioContext_, [this] () {
asio::post(*ioContext_, [this]() {
reDiscover();
publish(sockAddrSend_);
});
if (logger_)
logger_->d("PeerDiscovery: connectivity changed");
if (peerDiscovery_period == PeerDiscovery_PERIOD_MAX ){
if (peerDiscovery_period == PeerDiscovery_PERIOD_MAX) {
peerDiscovery_period = PeerDiscovery_PERIOD;
}
else{
} else {
peerDiscoveryTimer.expires_after(peerDiscovery_period);
peerDiscoveryTimer.async_wait([this](const asio::error_code& ec) {
if (ec == asio::error::operation_aborted)
@@ -351,7 +337,9 @@ PeerDiscovery::DomainPeerDiscovery::connectivityChanged()
}
}
void PeerDiscovery::DomainPeerDiscovery::stopConnectivityChanged() {
void
PeerDiscovery::DomainPeerDiscovery::stopConnectivityChanged()
{
peerDiscoveryTimer.cancel();
peerDiscovery_period = PeerDiscovery_PERIOD;
}
@@ -369,8 +357,7 @@ PeerDiscovery::PeerDiscovery(in_port_t port, Sp<asio::io_context> ioContext, Sp<
ioContext->run();
if (logger)
logger->d("[peerdiscovery] io_context stopped");
}
catch (const std::exception& ex){
} catch (const std::exception& ex) {
if (logger)
logger->e("[peerdiscovery] run error: %s", ex.what());
}
@@ -379,19 +366,20 @@ PeerDiscovery::PeerDiscovery(in_port_t port, Sp<asio::io_context> ioContext, Sp<
try {
peerDiscovery4_.reset(new DomainPeerDiscovery(asio::ip::udp::v4(), port, ioContext, logger));
} catch(const std::exception& e){
} catch (const std::exception& e) {
if (logger)
logger->e("[peerdiscovery] Unable to start IPv4: %s", e.what());
}
try {
peerDiscovery6_.reset(new DomainPeerDiscovery(asio::ip::udp::v6(), port, ioContext, logger));
} catch(const std::exception& e) {
} catch (const std::exception& e) {
if (logger)
logger->e("[peerdiscovery] Unable to start IPv6: %s", e.what());
}
}
PeerDiscovery::~PeerDiscovery() {
PeerDiscovery::~PeerDiscovery()
{
stop();
if (ioContext_)
ioContext_->stop();
@@ -402,32 +390,40 @@ PeerDiscovery::~PeerDiscovery() {
void
PeerDiscovery::startDiscovery(std::string_view type, ServiceDiscoveredCallback callback)
{
if (peerDiscovery4_) peerDiscovery4_->startDiscovery(type, callback);
if (peerDiscovery6_) peerDiscovery6_->startDiscovery(type, callback);
if (peerDiscovery4_)
peerDiscovery4_->startDiscovery(type, callback);
if (peerDiscovery6_)
peerDiscovery6_->startDiscovery(type, callback);
}
void
PeerDiscovery::startPublish(std::string_view type, const msgpack::sbuffer& pack_buf)
{
if (peerDiscovery4_) peerDiscovery4_->startPublish(type, pack_buf);
if (peerDiscovery6_) peerDiscovery6_->startPublish(type, pack_buf);
if (peerDiscovery4_)
peerDiscovery4_->startPublish(type, pack_buf);
if (peerDiscovery6_)
peerDiscovery6_->startPublish(type, pack_buf);
}
void
PeerDiscovery::startPublish(sa_family_t domain, std::string_view type, const msgpack::sbuffer& pack_buf)
{
if (domain == AF_INET) {
if (peerDiscovery4_) peerDiscovery4_->startPublish(type, pack_buf);
if (peerDiscovery4_)
peerDiscovery4_->startPublish(type, pack_buf);
} else if (domain == AF_INET6) {
if (peerDiscovery6_) peerDiscovery6_->startPublish(type, pack_buf);
if (peerDiscovery6_)
peerDiscovery6_->startPublish(type, pack_buf);
}
}
void
PeerDiscovery::stop()
{
if (peerDiscovery4_) peerDiscovery4_->stop();
if (peerDiscovery6_) peerDiscovery6_->stop();
if (peerDiscovery4_)
peerDiscovery4_->stop();
if (peerDiscovery6_)
peerDiscovery6_->stop();
}
bool
@@ -466,9 +462,11 @@ PeerDiscovery::connectivityChanged()
peerDiscovery6_->connectivityChanged();
}
void PeerDiscovery::stopConnectivityChanged() {
if (peerDiscovery4_)
peerDiscovery4_->stopConnectivityChanged();
void
PeerDiscovery::stopConnectivityChanged()
{
if (peerDiscovery4_)
peerDiscovery4_->stopConnectivityChanged();
if (peerDiscovery6_)
peerDiscovery6_->stopConnectivityChanged();
}
+60 -51
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "net.h"
@@ -36,19 +21,14 @@ struct ParsedMessage;
* desitned to specific node and std::function callbacks to execute when the
* request is done.
*/
struct Request {
struct Request
{
friend class dht::net::NetworkEngine;
Sp<Node> node {}; /* the node to whom the request is destined. */
Sp<Node> node {}; /* the node to whom the request is destined. */
time_point reply_time {time_point::min()}; /* time when we received the response to the request. */
enum class State
{
PENDING,
CANCELLED,
EXPIRED,
COMPLETED
};
enum class State { PENDING, CANCELLED, EXPIRED, COMPLETED };
bool expired() const { return state_ == State::EXPIRED; }
bool completed() const { return state_ == State::COMPLETED; }
@@ -56,35 +36,59 @@ struct Request {
bool pending() const { return state_ == State::PENDING; }
bool over() const { return not pending(); }
State getState() const { return state_; }
char getStateChar() const {
char getStateChar() const
{
switch (state_) {
case State::PENDING: return 'f';
case State::CANCELLED: return 'c';
case State::EXPIRED: return 'e';
case State::COMPLETED: return 'a';
default: return '?';
case State::PENDING:
return 'f';
case State::CANCELLED:
return 'c';
case State::EXPIRED:
return 'e';
case State::COMPLETED:
return 'a';
default:
return '?';
}
}
Request(State state = State::PENDING) : state_(state) {}
Request(MessageType type, Tid tid,
Request(State state = State::PENDING)
: state_(state)
{}
Request(MessageType type,
Tid tid,
Sp<Node> node,
Blob&& msg,
std::function<void(const Request&, ParsedMessage&&)> on_done,
std::function<void(const Request&, bool)> on_expired) :
node(std::move(node)), tid(tid), type(type), on_done(std::move(on_done)), on_expired(std::move(on_expired)), msg(std::move(msg)) { }
Request(MessageType type, Tid tid,
std::function<void(const Request&, bool)> on_expired)
: node(std::move(node))
, tid(tid)
, type(type)
, on_done(std::move(on_done))
, on_expired(std::move(on_expired))
, msg(std::move(msg))
{}
Request(MessageType type,
Tid tid,
Sp<Node> node,
Blob&& msg,
std::function<void(const Request&, ParsedMessage&&)> on_done,
std::function<bool(const Request&, DhtProtocolException&&)> on_error,
std::function<void(const Request&, bool)> on_expired) :
node(std::move(node)), tid(tid), type(type), on_done(std::move(on_done)), on_error(std::move(on_error)), on_expired(std::move(on_expired)), msg(std::move(msg)) { }
std::function<void(const Request&, bool)> on_expired)
: node(std::move(node))
, tid(tid)
, type(type)
, on_done(std::move(on_done))
, on_error(std::move(on_error))
, on_expired(std::move(on_expired))
, msg(std::move(msg))
{}
Tid getTid() const { return tid; }
MessageType getType() const { return type; }
void setExpired() {
void setExpired()
{
if (pending()) {
state_ = Request::State::EXPIRED;
if (on_expired)
@@ -92,14 +96,16 @@ struct Request {
clear();
}
}
void setDone(ParsedMessage&& msg) {
void setDone(ParsedMessage&& msg)
{
if (pending()) {
state_ = Request::State::COMPLETED;
on_done(*this, std::forward<ParsedMessage>(msg));
clear();
}
}
bool setError(DhtProtocolException&& e) {
bool setError(DhtProtocolException&& e)
{
if (pending()) {
state_ = Request::State::EXPIRED;
bool handled = on_error and on_error(*this, std::forward<DhtProtocolException>(e));
@@ -109,7 +115,8 @@ struct Request {
return true;
}
void cancel() {
void cancel()
{
if (pending()) {
state_ = State::CANCELLED;
clear();
@@ -119,11 +126,13 @@ struct Request {
private:
static const constexpr size_t MAX_ATTEMPT_COUNT {3};
bool isExpired(time_point now) const {
bool isExpired(time_point now) const
{
return pending() and now > last_try + attempt_duration and attempt_count >= Request::MAX_ATTEMPT_COUNT;
}
void clear() {
void clear()
{
on_done = {};
on_error = {};
on_expired = {};
@@ -135,18 +144,18 @@ private:
const MessageType type {};
State state_ {State::PENDING};
unsigned attempt_count {0}; /* number of attempt to process the request. */
duration attempt_duration {((duration)Node::MAX_RESPONSE_TIME)/2};
time_point start {time_point::min()}; /* time when the request is created. */
time_point last_try {time_point::min()}; /* time of the last attempt to process the request. */
unsigned attempt_count {0}; /* number of attempt to process the request. */
duration attempt_duration {((duration) Node::MAX_RESPONSE_TIME) / 2};
time_point start {time_point::min()}; /* time when the request is created. */
time_point last_try {time_point::min()}; /* time of the last attempt to process the request. */
std::function<void(const Request&, ParsedMessage&&)> on_done {};
std::function<bool(const Request&, DhtProtocolException&&)> on_error {};
std::function<void(const Request&, bool)> on_expired {};
Blob msg {}; /* the serialized message. */
Blob msg {}; /* the serialized message. */
std::vector<Blob> parts;
};
} /* namespace net */
}
} // namespace dht
+35 -45
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "routing_table.h"
@@ -29,17 +15,19 @@ Bucket::randomNode(std::mt19937_64& rd)
{
if (nodes.empty())
return nullptr;
unsigned expired_node_count = std::count_if(nodes.cbegin(), nodes.cend(), [](const decltype(nodes)::value_type& node) {
return node->isExpired();
});
unsigned expired_node_count = std::count_if(nodes.cbegin(),
nodes.cend(),
[](const decltype(nodes)::value_type& node) {
return node->isExpired();
});
auto prioritize_not_expired = expired_node_count < nodes.size();
std::uniform_int_distribution<unsigned> rand_node(0, prioritize_not_expired
? nodes.size() - expired_node_count - 1
: nodes.size()-1);
std::uniform_int_distribution<unsigned> rand_node(0,
prioritize_not_expired ? nodes.size() - expired_node_count - 1
: nodes.size() - 1);
unsigned nn = rand_node(rd);
for (auto& n : nodes) {
if (not (prioritize_not_expired and n->isExpired())) {
if (not(prioritize_not_expired and n->isExpired())) {
if (not nn--)
return n;
}
@@ -47,11 +35,12 @@ Bucket::randomNode(std::mt19937_64& rd)
return nodes.back();
}
void Bucket::sendCachedPing(net::NetworkEngine& ne)
void
Bucket::sendCachedPing(net::NetworkEngine& ne)
{
if (not cached)
return;
//DHT_LOG.d(b.cached->id, "[node %s] sending ping to cached node", cached->toString().c_str());
// DHT_LOG.d(b.cached->id, "[node %s] sending ping to cached node", cached->toString().c_str());
ne.sendPing(cached, nullptr, nullptr);
cached = {};
}
@@ -63,16 +52,16 @@ RoutingTable::randomId(const RoutingTable::const_iterator& it, std::mt19937_64&
int bit2 = std::next(it) != end() ? std::next(it)->first.lowbit() : -1;
int bit = std::max(bit1, bit2) + 1;
if (bit >= 8*(int)HASH_LEN)
if (bit >= 8 * (int) HASH_LEN)
return it->first;
#ifdef _WIN32
std::uniform_int_distribution<int> rand_byte{ 0, std::numeric_limits<uint8_t>::max() };
std::uniform_int_distribution<int> rand_byte {0, std::numeric_limits<uint8_t>::max()};
#else
std::uniform_int_distribution<uint8_t> rand_byte;
#endif
int b = bit/8;
int b = bit / 8;
InfoHash id_return;
std::copy_n(it->first.cbegin(), b, id_return.begin());
id_return[b] = it->first[b] & (0xFF00 >> (bit % 8));
@@ -86,7 +75,7 @@ InfoHash
RoutingTable::middle(const RoutingTable::const_iterator& it) const
{
unsigned bit = depth(it);
if (bit >= 8*HASH_LEN)
if (bit >= 8 * HASH_LEN)
throw std::out_of_range("End of table");
InfoHash id = it->first;
@@ -101,7 +90,7 @@ RoutingTable::depth(const RoutingTable::const_iterator& it) const
return 0;
int bit1 = it->first.lowbit();
int bit2 = std::next(it) != end() ? std::next(it)->first.lowbit() : -1;
return std::max(bit1, bit2)+1;
return std::max(bit1, bit2) + 1;
}
std::vector<Sp<Node>>
@@ -111,18 +100,18 @@ RoutingTable::findClosestNodes(const InfoHash id, time_point now, size_t count)
nodes.reserve(count);
auto bucket = findBucket(id);
if (bucket == end()) { return nodes; }
if (bucket == end()) {
return nodes;
}
auto sortedBucketInsert = [&](const Bucket &b) {
auto sortedBucketInsert = [&](const Bucket& b) {
for (auto n : b.nodes) {
if (not n->isGood(now))
continue;
auto here = std::find_if(nodes.begin(), nodes.end(),
[&id,&n](Sp<Node> &node) {
return id.xorCmp(n->id, node->id) < 0;
}
);
auto here = std::find_if(nodes.begin(), nodes.end(), [&id, &n](Sp<Node>& node) {
return id.xorCmp(n->id, node->id) < 0;
});
nodes.insert(here, n);
}
};
@@ -200,9 +189,12 @@ RoutingTable::split(const RoutingTable::iterator& b)
}
bool
RoutingTable::onNewNode(const Sp<Node>& node, int confirm, const time_point& now, const InfoHash& myid, net::NetworkEngine& ne) {
RoutingTable::onNewNode(
const Sp<Node>& node, int confirm, const time_point& now, const InfoHash& myid, net::NetworkEngine& ne)
{
auto b = findBucket(node->id);
if (b == end()) return false;
if (b == end())
return false;
if (confirm == 2)
b->time = now;
@@ -215,7 +207,7 @@ RoutingTable::onNewNode(const Sp<Node>& node, int confirm, const time_point& now
bool mybucket = contains(b, myid);
if (mybucket) {
grow_time = now;
//scheduler.edit(nextNodesConfirmation, now);
// scheduler.edit(nextNodesConfirmation, now);
}
if (b->nodes.size() >= TARGET_NODES) {
@@ -235,7 +227,7 @@ RoutingTable::onNewNode(const Sp<Node>& node, int confirm, const time_point& now
if (not n->isGood(now)) {
dubious = true;
if (not n->isPendingMessage()) {
//DHT_LOG.d(n->id, "[node %s] sending ping to dubious node", n->toString().c_str());
// DHT_LOG.d(n->id, "[node %s] sending ping to dubious node", n->toString().c_str());
ne.sendPing(n, nullptr, nullptr);
break;
}
@@ -243,7 +235,7 @@ RoutingTable::onNewNode(const Sp<Node>& node, int confirm, const time_point& now
}
if ((mybucket || (is_client and depth(b) < 6)) && (!dubious || size() == 1)) {
//DHT_LOG.d("Splitting from depth %u", depth(b));
// DHT_LOG.d("Splitting from depth %u", depth(b));
b->sendCachedPing(ne);
split(b);
return onNewNode(node, confirm, now, myid, ne);
@@ -259,6 +251,4 @@ RoutingTable::onNewNode(const Sp<Node>& node, int confirm, const time_point& now
return true;
}
}
} // namespace dht
+214 -163
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "value.h"
@@ -28,7 +13,8 @@ namespace dht {
/**
* A single "get" operation data
*/
struct Dht::Get {
struct Dht::Get
{
time_point start;
Value::Filter filter;
Sp<Query> query;
@@ -40,22 +26,26 @@ struct Dht::Get {
/**
* A single "put" operation data
*/
struct Dht::Announce {
struct Dht::Announce
{
bool permanent;
Sp<Value> value;
time_point created;
std::vector<DoneCallback> callbacks;
};
struct Dht::SearchNode {
struct AnnounceStatus {
struct Dht::SearchNode
{
struct AnnounceStatus
{
Sp<net::Request> req {};
Sp<Scheduler::Job> refresh {};
time_point refresh_time;
AnnounceStatus(){};
AnnounceStatus(Sp<net::Request> r, time_point t): req(std::move(r)), refresh_time(t)
{}
AnnounceStatus() {};
AnnounceStatus(Sp<net::Request> r, time_point t)
: req(std::move(r))
, refresh_time(t)
{}
};
/**
* Foreach value id, we keep track of a pair (net::Request, time_point) where the
@@ -69,17 +59,21 @@ struct Dht::SearchNode {
*/
using SyncStatus = std::map<Sp<Query>, Sp<net::Request>>;
struct CachedListenStatus {
struct CachedListenStatus
{
ValueCache cache;
Sp<Scheduler::Job> refresh {};
Sp<Scheduler::Job> cacheExpirationJob {};
Sp<net::Request> req {};
Tid socketId {0};
CachedListenStatus(ValueStateCallback&& cb, SyncCallback scb, Tid sid)
: cache(std::forward<ValueStateCallback>(cb), std::forward<SyncCallback>(scb)), socketId(sid) {}
: cache(std::forward<ValueStateCallback>(cb), std::forward<SyncCallback>(scb))
, socketId(sid)
{}
CachedListenStatus(CachedListenStatus&&) = delete;
CachedListenStatus(const CachedListenStatus&) = delete;
~CachedListenStatus() {
~CachedListenStatus()
{
if (socketId and req and req->node) {
req->node->closeSocket(socketId);
}
@@ -87,31 +81,36 @@ struct Dht::SearchNode {
};
using NodeListenerStatus = std::map<Sp<Query>, CachedListenStatus>;
Sp<Node> node {}; /* the node info */
Sp<Node> node {}; /* the node info */
/* queries sent for finding out values hosted by the node */
Sp<Query> probe_query {};
/* queries substituting formal 'get' requests */
std::map<Sp<Query>, std::vector<Sp<Query>>> pagination_queries {};
SyncStatus getStatus {}; /* get/sync status */
SyncStatus getStatus {}; /* get/sync status */
NodeListenerStatus listenStatus {}; /* listen status */
AnnounceStatusMap acked {}; /* announcement status for a given value id */
AnnounceStatusMap acked {}; /* announcement status for a given value id */
Blob token {}; /* last token the node sent to us after a get request */
time_point last_get_reply {time_point::min()}; /* last time received valid token */
Sp<Scheduler::Job> syncJob {};
bool candidate {false}; /* A search node is candidate if the search is/was synced and this
node is a new candidate for inclusion. */
bool candidate {false}; /* A search node is candidate if the search is/was synced and this
node is a new candidate for inclusion. */
SearchNode() : node() {}
SearchNode()
: node()
{}
SearchNode(const SearchNode&) = delete;
SearchNode(SearchNode&&) = delete;
SearchNode& operator=(const SearchNode&) = delete;
SearchNode& operator=(SearchNode&&) = delete;
SearchNode(const Sp<Node>& node) : node(node) {}
~SearchNode() {
SearchNode(const Sp<Node>& node)
: node(node)
{}
~SearchNode()
{
if (node) {
cancelGet();
cancelListen();
@@ -122,22 +121,23 @@ struct Dht::SearchNode {
/**
* Can we use this node to listen/announce now ?
*/
bool isSynced(const time_point& now) const {
return not node->isExpired() and
not token.empty() and last_get_reply >= now - Node::NODE_EXPIRE_TIME;
bool isSynced(const time_point& now) const
{
return not node->isExpired() and not token.empty() and last_get_reply >= now - Node::NODE_EXPIRE_TIME;
}
time_point getSyncTime(const time_point& now) const {
time_point getSyncTime(const time_point& now) const
{
if (node->isExpired() or token.empty())
return now;
return last_get_reply + Node::NODE_EXPIRE_TIME;
}
friend std::ostream& operator<< (std::ostream& s, const SearchNode& node) {
s << "getStatus:" << node.getStatus.size()
<< " listenStatus:" << node.listenStatus.size()
<< " acked:" << node.acked.size()
<< " cache:" << (node.listenStatus.empty() ? 0 : node.listenStatus.begin()->second.cache.size()) << std::endl;
friend std::ostream& operator<<(std::ostream& s, const SearchNode& node)
{
s << "getStatus:" << node.getStatus.size() << " listenStatus:" << node.listenStatus.size()
<< " acked:" << node.acked.size()
<< " cache:" << (node.listenStatus.empty() ? 0 : node.listenStatus.begin()->second.cache.size()) << std::endl;
return s;
}
@@ -159,28 +159,27 @@ struct Dht::SearchNode {
*
* @return true if we can send get, else false.
*/
bool canGet(time_point now, time_point update, const Sp<Query>& q) const {
bool canGet(time_point now, time_point update, const Sp<Query>& q) const
{
if (node->isExpired())
return false;
bool is_pending {false},
completed_sq_status {false},
pending_sq_status {false};
bool is_pending {false}, completed_sq_status {false}, pending_sq_status {false};
for (const auto& s : getStatus) {
if (s.second and s.second->pending())
is_pending = true;
if (s.first and q and q->isSatisfiedBy(*s.first) and s.second) {
if (s.second->pending())
pending_sq_status = true;
else if (s.second->completed() and not (update > s.second->reply_time))
else if (s.second->completed() and not(update > s.second->reply_time))
completed_sq_status = true;
if (completed_sq_status and pending_sq_status)
break;
}
}
return (not is_pending and now > last_get_reply + Node::NODE_EXPIRE_TIME) or
not (completed_sq_status or pending_sq_status or hasStartedPagination(q));
return (not is_pending and now > last_get_reply + Node::NODE_EXPIRE_TIME)
or not(completed_sq_status or pending_sq_status or hasStartedPagination(q));
}
/**
@@ -190,18 +189,20 @@ struct Dht::SearchNode {
*
* @return true if pagination process has started, else false.
*/
bool hasStartedPagination(const Sp<Query>& q) const {
bool hasStartedPagination(const Sp<Query>& q) const
{
const auto& pqs = pagination_queries.find(q);
if (pqs == pagination_queries.cend() or pqs->second.empty())
return false;
return std::find_if(pqs->second.cbegin(), pqs->second.cend(),
[this](const Sp<Query>& query) {
const auto& req = getStatus.find(query);
return req != getStatus.cend() and req->second;
}) != pqs->second.cend();
return std::find_if(pqs->second.cbegin(),
pqs->second.cend(),
[this](const Sp<Query>& query) {
const auto& req = getStatus.find(query);
return req != getStatus.cend() and req->second;
})
!= pqs->second.cend();
};
/**
* Tell if the node has finished responding to a given 'get' request.
*
@@ -213,14 +214,18 @@ struct Dht::SearchNode {
*
* @return true if it has finished, else false.
*/
bool isDone(const Get& get) const {
bool isDone(const Get& get) const
{
if (hasStartedPagination(get.query)) {
const auto& pqs = pagination_queries.find(get.query);
auto paginationPending = std::find_if(pqs->second.cbegin(), pqs->second.cend(),
[this](const Sp<Query>& query) {
const auto& req = getStatus.find(query);
return req != getStatus.cend() and req->second and req->second->pending();
}) != pqs->second.cend();
auto paginationPending = std::find_if(pqs->second.cbegin(),
pqs->second.cend(),
[this](const Sp<Query>& query) {
const auto& req = getStatus.find(query);
return req != getStatus.cend() and req->second
and req->second->pending();
})
!= pqs->second.cend();
return not paginationPending;
} else { /* no pagination yet */
const auto& gs = get.query ? getStatus.find(get.query) : getStatus.cend();
@@ -228,7 +233,8 @@ struct Dht::SearchNode {
}
}
void cancelGet() {
void cancelGet()
{
for (const auto& status : getStatus) {
if (status.second->pending()) {
node->cancelRequest(status.second);
@@ -242,13 +248,16 @@ struct Dht::SearchNode {
auto l = listenStatus.find(q);
if (l != listenStatus.end()) {
auto next = l->second.cache.onValues(answer.values,
answer.refreshed_values,
answer.expired_values, types, scheduler.time());
answer.refreshed_values,
answer.expired_values,
types,
scheduler.time());
scheduler.edit(l->second.cacheExpirationJob, next);
}
}
void onListenSynced(const Sp<Query>& q, bool synced = true, Sp<Scheduler::Job> refreshJob = {}) {
void onListenSynced(const Sp<Query>& q, bool synced = true, Sp<Scheduler::Job> refreshJob = {})
{
auto l = listenStatus.find(q);
if (l != listenStatus.end()) {
if (l->second.refresh)
@@ -258,7 +267,8 @@ struct Dht::SearchNode {
}
}
void expireValues(const Sp<Query>& q, Scheduler& scheduler) {
void expireValues(const Sp<Query>& q, Scheduler& scheduler)
{
auto l = listenStatus.find(q);
if (l != listenStatus.end()) {
auto next = l->second.cache.expireValues(scheduler.time());
@@ -287,28 +297,34 @@ struct Dht::SearchNode {
*
* @return true if there exists an expired request, else false.
*/
static bool pending(const SyncStatus& status) {
return std::find_if(status.cbegin(), status.cend(),
[](const SyncStatus::value_type& r){
return r.second and r.second->pending();
}) != status.cend();
static bool pending(const SyncStatus& status)
{
return std::find_if(status.cbegin(),
status.cend(),
[](const SyncStatus::value_type& r) { return r.second and r.second->pending(); })
!= status.cend();
}
static bool pending(const NodeListenerStatus& status) {
return std::find_if(status.begin(), status.end(),
[](const NodeListenerStatus::value_type& r){
return r.second.req and r.second.req->pending();
}) != status.end();
static bool pending(const NodeListenerStatus& status)
{
return std::find_if(status.begin(),
status.end(),
[](const NodeListenerStatus::value_type& r) {
return r.second.req and r.second.req->pending();
})
!= status.end();
}
bool pendingGet() const { return pending(getStatus); }
bool isAnnounced(Value::Id vid) const {
bool isAnnounced(Value::Id vid) const
{
auto ack = acked.find(vid);
if (ack == acked.end() or not ack->second.req)
return false;
return ack->second.req->completed();
}
void cancelAnnounce() {
void cancelAnnounce()
{
for (const auto& status : acked) {
const auto& req = status.second.req;
if (req and req->pending()) {
@@ -320,28 +336,34 @@ struct Dht::SearchNode {
acked.clear();
}
inline bool isListening(time_point now, duration listen_expire) const {
inline bool isListening(time_point now, duration listen_expire) const
{
auto ls = listenStatus.begin();
for ( ; ls != listenStatus.end() ; ++ls) {
for (; ls != listenStatus.end(); ++ls) {
if (isListening(now, ls, listen_expire)) {
break;
}
}
return ls != listenStatus.end();
}
inline bool isListening(time_point now, const Sp<Query>& q, duration listen_expire) const {
inline bool isListening(time_point now, const Sp<Query>& q, duration listen_expire) const
{
const auto& ls = listenStatus.find(q);
if (ls == listenStatus.end())
return false;
else
return isListening(now, ls, listen_expire);
}
inline bool isListening(time_point now, NodeListenerStatus::const_iterator listen_status, duration listen_expire) const {
inline bool isListening(time_point now,
NodeListenerStatus::const_iterator listen_status,
duration listen_expire) const
{
if (listen_status == listenStatus.end() or not listen_status->second.req)
return false;
return listen_status->second.req->reply_time + listen_expire > now;
}
void cancelListen() {
void cancelListen()
{
for (const auto& status : listenStatus) {
node->cancelRequest(status.second.req);
if (status.second.refresh)
@@ -349,9 +371,10 @@ struct Dht::SearchNode {
if (status.second.cacheExpirationJob)
status.second.cacheExpirationJob->cancel();
}
listenStatus.clear();
listenStatus.clear();
}
void cancelListen(const Sp<Query>& query) {
void cancelListen(const Sp<Query>& query)
{
auto it = listenStatus.find(query);
if (it != listenStatus.end()) {
node->cancelRequest(it->second.req);
@@ -364,7 +387,8 @@ struct Dht::SearchNode {
/**
* Assuming the node is synced, should a "put" request be sent to this node now ?
*/
time_point getAnnounceTime(Value::Id vid) const {
time_point getAnnounceTime(Value::Id vid) const
{
const auto& ack = acked.find(vid);
if (ack == acked.cend() or not ack->second.req) {
return time_point::min();
@@ -379,39 +403,41 @@ struct Dht::SearchNode {
* Assuming the node is synced, should the "listen" request with Query q be
* sent to this node now ?
*/
time_point getListenTime(const decltype(listenStatus)::const_iterator listen_status, duration listen_expire) const {
time_point getListenTime(const decltype(listenStatus)::const_iterator listen_status, duration listen_expire) const
{
if (listen_status == listenStatus.cend() or not listen_status->second.req)
return time_point::min();
return listen_status->second.req->pending() ? time_point::max() :
listen_status->second.req->reply_time + listen_expire - REANNOUNCE_MARGIN;
return listen_status->second.req->pending()
? time_point::max()
: listen_status->second.req->reply_time + listen_expire - REANNOUNCE_MARGIN;
}
time_point getListenTime(const Sp<Query>& q, duration listen_expire) const {
time_point getListenTime(const Sp<Query>& q, duration listen_expire) const
{
return getListenTime(listenStatus.find(q), listen_expire);
}
/**
* Is this node expired or candidate
*/
bool isBad() const {
return not node or node->isExpired() or candidate;
}
bool isBad() const { return not node or node->isExpired() or candidate; }
};
/**
* A search is a list of the nodes we think are responsible
* for storing values for a given hash.
*/
struct Dht::Search {
struct Dht::Search
{
InfoHash id {};
sa_family_t af;
uint16_t tid;
time_point refill_time {time_point::min()};
time_point step_time {time_point::min()}; /* the time of the last search step */
time_point step_time {time_point::min()}; /* the time of the last search step */
Sp<Scheduler::Job> nextSearchStep {};
bool expired {false}; /* no node, or all nodes expired */
bool done {false}; /* search is over, cached for later */
bool expired {false}; /* no node, or all nodes expired */
bool done {false}; /* search is over, cached for later */
std::vector<std::unique_ptr<SearchNode>> nodes {};
/* pending puts */
@@ -421,7 +447,8 @@ struct Dht::Search {
std::multimap<time_point, Get> callbacks {};
/* listeners */
struct SearchListener {
struct SearchListener
{
Sp<Query> query;
ValueCallback get_cb;
SyncCallback sync_cb;
@@ -433,7 +460,8 @@ struct Dht::Search {
SearchCache cache;
Sp<Scheduler::Job> opExpirationJob;
~Search() {
~Search()
{
if (opExpirationJob)
opExpirationJob->cancel();
for (auto& g : callbacks) {
@@ -447,12 +475,11 @@ struct Dht::Search {
}
}
friend std::ostream& operator<< (std::ostream& s, const Search& sr) {
friend std::ostream& operator<<(std::ostream& s, const Search& sr)
{
auto csize = sr.cache.size();
s << "announce:" << sr.announce.size()
<< " gets:" << sr.callbacks.size()
<< " listeners:" << sr.listeners.size()
<< " cache:" << csize.first << ',' << csize.second << std::endl;
s << "announce:" << sr.announce.size() << " gets:" << sr.callbacks.size()
<< " listeners:" << sr.listeners.size() << " cache:" << csize.first << ',' << csize.second << std::endl;
s << "nodes:" << std::endl;
for (const auto& n : sr.nodes)
s << *n;
@@ -462,9 +489,10 @@ struct Dht::Search {
/**
* @returns true if the node was not present and added to the search
*/
bool insertNode(const Sp<Node>& n, time_point now, const Blob& token={});
bool insertNode(const Sp<Node>& n, time_point now, const Blob& token = {});
SearchNode* getNode(const Sp<Node>& n) {
SearchNode* getNode(const Sp<Node>& n)
{
auto srn = std::find_if(nodes.begin(), nodes.end(), [&](const std::unique_ptr<SearchNode>& sn) {
return n == sn->node;
});
@@ -472,7 +500,8 @@ struct Dht::Search {
}
/* number of concurrent sync requests */
unsigned currentlySolicitedNodeCount() const {
unsigned currentlySolicitedNodeCount() const
{
unsigned count = 0;
for (const auto& n : nodes)
if (not n->isBad() and n->pendingGet())
@@ -509,7 +538,8 @@ struct Dht::Search {
*
* @param get The 'get' operation which is now over.
*/
void setDone(const Get& get) {
void setDone(const Get& get)
{
for (auto& n : nodes) {
auto pqs = n->pagination_queries.find(get.query);
if (pqs != n->pagination_queries.cend()) {
@@ -527,7 +557,8 @@ struct Dht::Search {
* the opportunity we have to clear some entries in the SearchNodes status
* maps.
*/
void setDone() {
void setDone()
{
for (auto& n : nodes) {
n->getStatus.clear();
n->listenStatus.clear();
@@ -539,40 +570,50 @@ struct Dht::Search {
bool isAnnounced(Value::Id id) const;
bool isListening(time_point now, duration exp) const;
void get(const Value::Filter& f, const Sp<Query>& q, const QueryCallback& qcb, const GetCallback& gcb, const DoneCallback& dcb, Scheduler& scheduler) {
void get(const Value::Filter& f,
const Sp<Query>& q,
const QueryCallback& qcb,
const GetCallback& gcb,
const DoneCallback& dcb,
Scheduler& scheduler)
{
if (gcb or qcb) {
if (not cache.get(f, q, gcb, dcb)) {
const auto& now = scheduler.time();
callbacks.emplace(now, Get { now, f, q, qcb, gcb, dcb });
callbacks.emplace(now, Get {now, f, q, qcb, gcb, dcb});
scheduler.edit(nextSearchStep, now);
}
}
}
size_t listen(const ValueCallback& cb, Value::Filter&& f, const Sp<Query>& q, Scheduler& scheduler) {
//DHT_LOG.e(id, "[search %s IPv%c] listen", id.toString().c_str(), (af == AF_INET) ? '4' : '6');
return cache.listen(cb, q, std::move(f), [&](const Sp<Query>& q, ValueCallback vcb, SyncCallback scb){
size_t listen(const ValueCallback& cb, Value::Filter&& f, const Sp<Query>& q, Scheduler& scheduler)
{
// DHT_LOG.e(id, "[search %s IPv%c] listen", id.toString().c_str(), (af == AF_INET) ? '4' : '6');
return cache.listen(cb, q, std::move(f), [&](const Sp<Query>& q, ValueCallback vcb, SyncCallback scb) {
done = false;
auto token = ++listener_token;
listeners.emplace(token, SearchListener{q, vcb, scb});
listeners.emplace(token, SearchListener {q, vcb, scb});
scheduler.edit(nextSearchStep, scheduler.time());
return token;
});
}
void cancelListen(size_t token, Scheduler& scheduler) {
void cancelListen(size_t token, Scheduler& scheduler)
{
cache.cancelListen(token, scheduler.time());
if (not opExpirationJob)
opExpirationJob = scheduler.add(time_point::max(), [this,&scheduler]{
auto nextExpire = cache.expire(scheduler.time(), [&](size_t t){
opExpirationJob = scheduler.add(time_point::max(), [this, &scheduler] {
auto nextExpire = cache.expire(scheduler.time(), [&](size_t t) {
const auto& ll = listeners.find(t);
if (ll != listeners.cend()) {
auto query = ll->second.query;
listeners.erase(ll);
if (listeners.empty()) {
for (auto& sn : nodes) sn->cancelListen();
for (auto& sn : nodes)
sn->cancelListen();
} else if (query) {
for (auto& sn : nodes) sn->cancelListen(query);
for (auto& sn : nodes)
sn->cancelListen(query);
}
}
});
@@ -581,7 +622,8 @@ struct Dht::Search {
scheduler.edit(opExpirationJob, cache.getExpiration());
}
std::vector<Sp<Value>> getPut() const {
std::vector<Sp<Value>> getPut() const
{
std::vector<Sp<Value>> ret;
ret.reserve(announce.size());
for (const auto& a : announce)
@@ -589,7 +631,8 @@ struct Dht::Search {
return ret;
}
Sp<Value> getPut(Value::Id vid) const {
Sp<Value> getPut(Value::Id vid) const
{
for (auto& a : announce) {
if (a.value->id == vid)
return a.value;
@@ -597,14 +640,14 @@ struct Dht::Search {
return {};
}
bool cancelPut(Value::Id vid) {
bool cancelPut(Value::Id vid)
{
bool canceled {false};
for (auto it = announce.begin(); it != announce.end();) {
if (it->value->id == vid) {
canceled = true;
it = announce.erase(it);
}
else
} else
++it;
}
for (auto& n : nodes) {
@@ -620,14 +663,15 @@ struct Dht::Search {
return canceled;
}
void put(const Sp<Value>& value, DoneCallback callback, time_point created, bool permanent) {
void put(const Sp<Value>& value, DoneCallback callback, time_point created, bool permanent)
{
done = false;
expired = false;
auto a_sr = std::find_if(announce.begin(), announce.end(), [&](const Announce& a){
auto a_sr = std::find_if(announce.begin(), announce.end(), [&](const Announce& a) {
return a.value->id == value->id;
});
if (a_sr == announce.end()) {
auto& a = announce.emplace_back(Announce {permanent, value, created, {}} );
auto& a = announce.emplace_back(Announce {permanent, value, created, {}});
if (callback)
a.callbacks.emplace_back(std::move(callback));
for (auto& n : nodes) {
@@ -644,7 +688,7 @@ struct Dht::Search {
n->acked[value->id].req.reset();
n->probe_query.reset();
}
for (auto& cb: a_sr->callbacks)
for (auto& cb : a_sr->callbacks)
cb(false, {});
a_sr->callbacks.clear();
if (callback)
@@ -664,12 +708,14 @@ struct Dht::Search {
/**
* @return The number of non-good search nodes.
*/
unsigned getNumberOfBadNodes() const {
unsigned getNumberOfBadNodes() const
{
return std::count_if(nodes.begin(), nodes.end(), [](const std::unique_ptr<SearchNode>& sn) {
return sn->isBad();
});
}
unsigned getNumberOfConsecutiveBadNodes() const {
unsigned getNumberOfConsecutiveBadNodes() const
{
unsigned count = 0;
for (const auto& sn : nodes) {
if (not sn->node->isExpired())
@@ -688,11 +734,13 @@ struct Dht::Search {
*
* @return true if a node has been removed, else false.
*/
bool removeExpiredNode(const time_point& now) {
bool removeExpiredNode(const time_point& now)
{
for (auto e = nodes.cend(); e != nodes.cbegin();) {
const Node& n = *(*(--e))->node;
if (n.isRemovable(now)) {
//std::cout << "Removing expired node " << n.id << " from IPv" << (af==AF_INET?'4':'6') << " search " << id << std::endl;
// std::cout << "Removing expired node " << n.id << " from IPv" << (af==AF_INET?'4':'6') << " search "
// << id << std::endl;
nodes.erase(e);
return true;
}
@@ -707,7 +755,8 @@ struct Dht::Search {
* - remove all nodes from the search;
* - clear (non-permanent) callbacks;
*/
void expire() {
void expire()
{
// no nodes or all expired nodes. This is most likely a connectivity change event.
expired = true;
@@ -725,8 +774,10 @@ struct Dht::Search {
{
std::vector<DoneCallback> a_cbs;
a_cbs.reserve(announce.size());
for (auto ait = announce.begin() ; ait != announce.end(); ) {
a_cbs.insert(a_cbs.end(), std::make_move_iterator(ait->callbacks.begin()), std::make_move_iterator(ait->callbacks.end()));
for (auto ait = announce.begin(); ait != announce.end();) {
a_cbs.insert(a_cbs.end(),
std::make_move_iterator(ait->callbacks.begin()),
std::make_move_iterator(ait->callbacks.end()));
ait->callbacks.clear();
if (not ait->permanent)
ait = announce.erase(ait);
@@ -745,22 +796,22 @@ struct Dht::Search {
* @param types The sequence of existing types.
* @param now The time reference to now.
*/
void checkAnnounced(Value::Id vid = Value::INVALID_ID) {
auto announced = std::partition(announce.begin(), announce.end(),
[this,&vid](Announce& a) {
if (vid != Value::INVALID_ID and (!a.value || a.value->id != vid))
return true;
if (isAnnounced(a.value->id)) {
if (!a.callbacks.empty()) {
const auto& nodes = getNodes();
for (auto& cb : a.callbacks)
cb(true, nodes);
a.callbacks.clear();
}
if (not a.permanent)
return false;
}
void checkAnnounced(Value::Id vid = Value::INVALID_ID)
{
auto announced = std::partition(announce.begin(), announce.end(), [this, &vid](Announce& a) {
if (vid != Value::INVALID_ID and (!a.value || a.value->id != vid))
return true;
if (isAnnounced(a.value->id)) {
if (!a.callbacks.empty()) {
const auto& nodes = getNodes();
for (auto& cb : a.callbacks)
cb(true, nodes);
a.callbacks.clear();
}
if (not a.permanent)
return false;
}
return true;
});
// remove acked for cleared annouces
for (auto it = announced; it != announce.end(); ++it) {
@@ -778,7 +829,8 @@ struct Dht::Search {
std::vector<Sp<Node>> getNodes() const;
void clear() {
void clear()
{
announce.clear();
callbacks.clear();
listeners.clear();
@@ -787,7 +839,6 @@ struct Dht::Search {
}
};
/* A search contains a list of nodes, sorted by decreasing distance to the
target. We just got a new candidate, insert it at the right spot or
discard it. */
@@ -819,8 +870,8 @@ Dht::Search::insertNode(const Sp<Node>& snode, time_point now, const Blob& token
if (not found) {
// find if and where to trim excessive nodes
auto t = nodes.cend();
size_t bad = 0; // number of bad nodes (if search is not expired)
bool full {false}; // is the search full (has the maximum nodes)
size_t bad = 0; // number of bad nodes (if search is not expired)
bool full {false}; // is the search full (has the maximum nodes)
if (expired) {
// if the search is expired, trim to SEARCH_NODES nodes
if (nodes.size() >= SEARCH_NODES) {
@@ -830,8 +881,8 @@ Dht::Search::insertNode(const Sp<Node>& snode, time_point now, const Blob& token
} else {
// otherwise, trim to SEARCH_NODES nodes, not counting bad nodes
bad = getNumberOfBadNodes();
full = nodes.size() - bad >= SEARCH_NODES;
while (std::distance(nodes.cbegin(), t) - bad > SEARCH_NODES) {
full = nodes.size() - bad >= SEARCH_NODES;
while (std::distance(nodes.cbegin(), t) - bad > SEARCH_NODES) {
--t;
if ((*t)->isBad())
bad--;
@@ -983,7 +1034,7 @@ Dht::Search::isListening(time_point now, duration listen_expire) const
if (n->isBad())
continue;
SearchNode::NodeListenerStatus::const_iterator ls {};
for (ls = n->listenStatus.begin(); ls != n->listenStatus.end() ; ++ls) {
for (ls = n->listenStatus.begin(); ls != n->listenStatus.end(); ++ls) {
if (n->isListening(now, ls, listen_expire))
break;
}
@@ -995,4 +1046,4 @@ Dht::Search::isListening(time_point now, duration listen_expire) const
return i;
}
}
} // namespace dht
+127 -121
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@@ -33,10 +19,18 @@ extern "C" {
namespace dht {
SecureDht::SecureDht(std::unique_ptr<DhtInterface> dht, SecureDht::Config conf, IdentityAnnouncedCb iacb, const std::shared_ptr<Logger>& l)
: DhtInterface(l), dht_(std::move(dht)), key_(conf.id.first), certificate_(conf.id.second), enableCache_(conf.cert_cache_all)
SecureDht::SecureDht(std::unique_ptr<DhtInterface> dht,
SecureDht::Config conf,
IdentityAnnouncedCb iacb,
const std::shared_ptr<Logger>& l)
: DhtInterface(l)
, dht_(std::move(dht))
, key_(conf.id.first)
, certificate_(conf.id.second)
, enableCache_(conf.cert_cache_all)
{
if (!dht_) return;
if (!dht_)
return;
for (const auto& type : DEFAULT_TYPES)
registerType(type);
@@ -63,34 +57,32 @@ SecureDht::SecureDht(std::unique_ptr<DhtInterface> dht, SecureDht::Config conf,
iacb(completedState->second);
} : iacb;
dht_->addOnConnectedCallback([this, certId, certLongId, cb=std::move(cb)]{
dht_->put(certId, Value {
CERTIFICATE_TYPE,
*certificate_,
1
}, cb, {}, true);
dht_->put(InfoHash::get(certLongId), Value {
CERTIFICATE_TYPE,
*certificate_,
1
}, cb, {}, true);
dht_->addOnConnectedCallback([this, certId, certLongId, cb = std::move(cb)] {
dht_->put(certId, Value {CERTIFICATE_TYPE, *certificate_, 1}, cb, {}, true);
dht_->put(InfoHash::get(certLongId), Value {CERTIFICATE_TYPE, *certificate_, 1}, cb, {}, true);
});
}
}
SecureDht::~SecureDht(){
SecureDht::~SecureDht()
{
dht_.reset();
}
ValueType
SecureDht::secureType(ValueType&& type)
{
type.storePolicy = [sp = std::move(type.storePolicy)](InfoHash id, Sp<Value>& v, const InfoHash& nid, const SockAddr& a) {
if (v->isSigned())
return v->checkSignature();
return sp(id, v, nid, a);
};
type.editPolicy = [l = logger_, ep = std::move(type.editPolicy)](InfoHash id, const Sp<Value>& o, Sp<Value>& n, const InfoHash& nid, const SockAddr& a) {
type.storePolicy =
[sp = std::move(type.storePolicy)](InfoHash id, Sp<Value>& v, const InfoHash& nid, const SockAddr& a) {
if (v->isSigned())
return v->checkSignature();
return sp(id, v, nid, a);
};
type.editPolicy = [l = logger_, ep = std::move(type.editPolicy)](InfoHash id,
const Sp<Value>& o,
Sp<Value>& n,
const InfoHash& nid,
const SockAddr& a) {
if (not o->isSigned())
return ep(id, o, n, nid, a);
if (*o->owner != *n->owner or not n->isSigned()) {
@@ -111,8 +103,7 @@ SecureDht::secureType(ValueType&& type)
l->w("Edition forbidden: sequence number must be increasing.");
return false;
}
}
else if (n->seq < o->seq)
} else if (n->seq < o->seq)
return false;
return true;
};
@@ -244,22 +235,26 @@ SecureDht::findCertificate(const InfoHash& node, const std::function<void(const
}
auto found = std::make_shared<bool>(false);
dht_->get(node, [cb,node,found,this](const std::vector<Sp<Value>>& vals) {
for (const auto& v : vals) {
if (auto cert = registerCertificate(node, v->data)) {
*found = true;
if (logger_)
logger_->d(node, "Found certificate for %s", node.to_c_str());
if (cb)
cb(cert);
return false;
dht_->get(
node,
[cb, node, found, this](const std::vector<Sp<Value>>& vals) {
for (const auto& v : vals) {
if (auto cert = registerCertificate(node, v->data)) {
*found = true;
if (logger_)
logger_->d(node, "Found certificate for %s", node.to_c_str());
if (cb)
cb(cert);
return false;
}
}
}
return !*found;
}, [cb,found](bool) {
if (!*found and cb)
cb(nullptr);
}, Value::TypeFilter(CERTIFICATE_TYPE));
return !*found;
},
[cb, found](bool) {
if (!*found and cb)
cb(nullptr);
},
Value::TypeFilter(CERTIFICATE_TYPE));
}
void
@@ -279,11 +274,13 @@ SecureDht::findPublicKey(const InfoHash& node, const std::function<void(const Sp
if (*pk) {
nodesPubKeys_[pk->getId()] = pk;
nodesPubKeysLong_[pk->getLongId()] = pk;
if (cb) cb(pk);
if (cb)
cb(pk);
return;
}
}
if (cb) cb(nullptr);
if (cb)
cb(nullptr);
});
}
@@ -313,22 +310,26 @@ SecureDht::findCertificate(const PkId& node, const std::function<void(const Sp<c
}
auto found = std::make_shared<bool>(false);
dht_->get(InfoHash::get(node), [cb,node,found,this](const std::vector<Sp<Value>>& vals) {
for (const auto& v : vals) {
if (auto cert = registerCertificate(node, v->data)) {
*found = true;
if (logger_)
logger_->debug("Found certificate for {}", node.to_c_str());
if (cb)
cb(cert);
return false;
dht_->get(
InfoHash::get(node),
[cb, node, found, this](const std::vector<Sp<Value>>& vals) {
for (const auto& v : vals) {
if (auto cert = registerCertificate(node, v->data)) {
*found = true;
if (logger_)
logger_->debug("Found certificate for {}", node.to_c_str());
if (cb)
cb(cert);
return false;
}
}
}
return !*found;
}, [cb,found](bool) {
if (!*found and cb)
cb(nullptr);
}, Value::TypeFilter(CERTIFICATE_TYPE));
return !*found;
},
[cb, found](bool) {
if (!*found and cb)
cb(nullptr);
},
Value::TypeFilter(CERTIFICATE_TYPE));
}
void
@@ -348,11 +349,13 @@ SecureDht::findPublicKey(const PkId& node, const std::function<void(const Sp<cry
if (*pk) {
nodesPubKeys_[pk->getId()] = pk;
nodesPubKeysLong_[pk->getLongId()] = pk;
if (cb) cb(pk);
if (cb)
cb(pk);
return;
}
}
if (cb) cb(nullptr);
if (cb)
cb(nullptr);
});
}
@@ -420,7 +423,6 @@ SecureDht::getCallbackFilter(const ValueCallback& cb, Value::Filter&& filter)
};
}
GetCallback
SecureDht::getCallbackFilter(const GetCallback& cb, Value::Filter&& filter)
{
@@ -451,7 +453,6 @@ SecureDht::listen(const InfoHash& id, ValueCallback cb, Value::Filter f, Where w
return dht_->listen(id, getCallbackFilter(cb, std::forward<Value::Filter>(f)), {}, std::forward<Where>(w));
}
size_t
SecureDht::listen(const InfoHash& id, GetCallback cb, Value::Filter f, Where w)
{
@@ -461,7 +462,7 @@ SecureDht::listen(const InfoHash& id, GetCallback cb, Value::Filter f, Where w)
void
SecureDht::putSigned(const InfoHash& hash, Sp<Value> val, DoneCallback callback, bool permanent)
{
if (not key_ or not hash or not val) {
if (not key_ or not hash or not val) {
if (callback)
callback(false, {});
return;
@@ -479,8 +480,9 @@ SecureDht::putSigned(const InfoHash& hash, Sp<Value> val, DoneCallback callback,
}
// Check if data already exists on the dht
get(hash,
[val,this] (const std::vector<Sp<Value>>& vals) {
get(
hash,
[val, this](const std::vector<Sp<Value>>& vals) {
if (logger_)
logger_->d("Found online previous value being announced.");
for (const auto& v : vals) {
@@ -495,73 +497,77 @@ SecureDht::putSigned(const InfoHash& hash, Sp<Value> val, DoneCallback callback,
}
return true;
},
[hash,val,this,callback,permanent] (bool /* ok */) {
[hash, val, this, callback, permanent](bool /* ok */) {
sign(*val);
dht_->put(hash, val, callback, time_point::max(), permanent);
},
Value::IdFilter(val->id),
Where().id(val->id)
);
Where().id(val->id));
}
void
SecureDht::putEncrypted(const InfoHash& hash, const InfoHash& to, Sp<Value> val, DoneCallback callback, bool permanent)
{
if (not key_) {
if (not key_) {
if (callback)
callback(false, {});
return;
}
findPublicKey(to, [this, hash, val = std::move(val), callback = std::move(callback), permanent](const Sp<crypto::PublicKey>& pk) {
if(!pk || !*pk) {
if (callback)
callback(false, {});
return;
}
if (logger_)
logger_->w("Encrypting data for PK: %s", pk->getId().toString().c_str());
try {
dht_->put(hash, encrypt(*val, *pk), callback, time_point::max(), permanent);
} catch (const std::exception& e) {
if (logger_)
logger_->e("Error putting encrypted data: %s", e.what());
if (callback)
callback(false, {});
}
});
findPublicKey(to,
[this, hash, val = std::move(val), callback = std::move(callback), permanent](
const Sp<crypto::PublicKey>& pk) {
if (!pk || !*pk) {
if (callback)
callback(false, {});
return;
}
if (logger_)
logger_->w("Encrypting data for PK: %s", pk->getId().toString().c_str());
try {
dht_->put(hash, encrypt(*val, *pk), callback, time_point::max(), permanent);
} catch (const std::exception& e) {
if (logger_)
logger_->e("Error putting encrypted data: %s", e.what());
if (callback)
callback(false, {});
}
});
}
void
SecureDht::putEncrypted(const InfoHash& hash, const PkId& to, Sp<Value> val, DoneCallback callback, bool permanent)
{
if (not key_) {
if (not key_) {
if (callback)
callback(false, {});
return;
}
findPublicKey(to, [this, hash, val = std::move(val), callback = std::move(callback), permanent](const Sp<crypto::PublicKey>& pk) {
if(!pk || !*pk) {
if (callback)
callback(false, {});
return;
}
if (logger_)
logger_->warn("Encrypting data for PK: {}", pk->getLongId());
try {
dht_->put(hash, encrypt(*val, *pk), callback, time_point::max(), permanent);
} catch (const std::exception& e) {
if (logger_)
logger_->e("Error putting encrypted data: %s", e.what());
if (callback)
callback(false, {});
}
});
findPublicKey(to,
[this, hash, val = std::move(val), callback = std::move(callback), permanent](
const Sp<crypto::PublicKey>& pk) {
if (!pk || !*pk) {
if (callback)
callback(false, {});
return;
}
if (logger_)
logger_->warn("Encrypting data for PK: {}", pk->getLongId());
try {
dht_->put(hash, encrypt(*val, *pk), callback, time_point::max(), permanent);
} catch (const std::exception& e) {
if (logger_)
logger_->e("Error putting encrypted data: %s", e.what());
if (callback)
callback(false, {});
}
});
}
void
SecureDht::putEncrypted(const InfoHash& hash, const crypto::PublicKey& pk, Sp<Value> val, DoneCallback callback, bool permanent)
SecureDht::putEncrypted(
const InfoHash& hash, const crypto::PublicKey& pk, Sp<Value> val, DoneCallback callback, bool permanent)
{
if (not key_) {
if (not key_) {
if (callback)
callback(false, {});
return;
@@ -599,7 +605,7 @@ SecureDht::decrypt(const Value& v)
auto decrypted = key_->decrypt(v.cypher);
Value ret {v.id};
auto msg = msgpack::unpack((const char*)decrypted.data(), decrypted.size());
auto msg = msgpack::unpack((const char*) decrypted.data(), decrypted.size());
ret.msgpack_unpack_body(msg.get());
if (ret.recipient != getId())
@@ -610,4 +616,4 @@ SecureDht::decrypt(const Value& v)
return ret;
}
}
} // namespace dht
+60 -68
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "infohash.h"
@@ -29,13 +14,16 @@ namespace dht {
/**
* Tracks storage usage per IP or IP range
*/
class StorageBucket {
class StorageBucket
{
public:
void insert(const InfoHash& id, const Value& value, time_point expiration) {
void insert(const InfoHash& id, const Value& value, time_point expiration)
{
totalSize_ += value.size();
storedValues_.emplace(expiration, std::pair<InfoHash, Value::Id>(id, value.id));
}
void erase(const InfoHash& id, const Value& value, time_point expiration) {
void erase(const InfoHash& id, const Value& value, time_point expiration)
{
auto range = storedValues_.equal_range(expiration);
for (auto rit = range.first; rit != range.second;) {
if (rit->second.first == id && rit->second.second == value.id) {
@@ -47,7 +35,8 @@ public:
}
// printf("StorageBucket::erase unable to find value %s %016" PRIx64 "\n", id.to_c_str(), value.id);
}
void refresh(const InfoHash& id, const Value& value, time_point old_expiration, time_point expiration) {
void refresh(const InfoHash& id, const Value& value, time_point old_expiration, time_point expiration)
{
auto range = storedValues_.equal_range(old_expiration);
for (auto rit = range.first; rit != range.second;) {
if (rit->second.first == id && rit->second.second == value.id) {
@@ -61,13 +50,18 @@ public:
insert(id, value, expiration);
}
size_t size() const { return totalSize_; }
std::pair<InfoHash, Value::Id> getOldest() const { return storedValues_.empty() ? std::pair<InfoHash, Value::Id>{} : storedValues_.begin()->second; }
std::pair<InfoHash, Value::Id> getOldest() const
{
return storedValues_.empty() ? std::pair<InfoHash, Value::Id> {} : storedValues_.begin()->second;
}
private:
std::multimap<time_point, std::pair<InfoHash, Value::Id>> storedValues_;
size_t totalSize_ {0};
};
struct ValueStorage {
struct ValueStorage
{
Sp<Value> data {};
time_point created {};
time_point expiration {};
@@ -76,11 +70,14 @@ struct ValueStorage {
ValueStorage() {}
ValueStorage(const Sp<Value>& v, time_point t, time_point e)
: data(v), created(t), expiration(e) {}
: data(v)
, created(t)
, expiration(e)
{}
};
struct Storage {
struct Storage
{
time_point maintenance_time {};
std::map<Sp<Node>, std::map<size_t, Listener>> listeners;
std::map<size_t, LocalListener> local_listeners {};
@@ -92,7 +89,8 @@ struct Storage {
/**
* Changes caused by an operation on the storage.
*/
struct StoreDiff {
struct StoreDiff
{
/** Difference in stored size caused by the op */
ssize_t size_diff;
/** Difference in number of values */
@@ -104,35 +102,35 @@ struct Storage {
};
Storage() {}
Storage(time_point t) : maintenance_time(t) {}
Storage(time_point t)
: maintenance_time(t)
{}
Storage(Storage&& o) noexcept = default;
Storage& operator=(Storage&& o) = default;
bool empty() const {
return values.empty();
}
bool empty() const { return values.empty(); }
StoreDiff clear();
size_t valueCount() const {
return values.size();
}
size_t valueCount() const { return values.size(); }
size_t totalSize() const {
return total_size;
}
size_t totalSize() const { return total_size; }
const std::vector<ValueStorage>& getValues() const { return values; }
Sp<Value> getById(Value::Id vid) const {
Sp<Value> getById(Value::Id vid) const
{
for (auto& v : values)
if (v.data->id == vid) return v.data;
if (v.data->id == vid)
return v.data;
return {};
}
std::vector<Sp<Value>> get(const Value::Filter& f = {}) const {
std::vector<Sp<Value>> get(const Value::Filter& f = {}) const
{
std::vector<Sp<Value>> newvals {};
if (not f) newvals.reserve(values.size());
if (not f)
newvals.reserve(values.size());
for (auto& v : values) {
if (not f || f(*v.data))
newvals.push_back(v.data);
@@ -148,8 +146,8 @@ struct Storage {
* change_size: size difference
* change_value_num: change of value number (0 or 1)
*/
std::pair<ValueStorage*, StoreDiff>
store(const InfoHash& id, const Sp<Value>&, time_point created, time_point expiration, StorageBucket*);
std::pair<ValueStorage*, StoreDiff> store(
const InfoHash& id, const Sp<Value>&, time_point created, time_point expiration, StorageBucket*);
/**
* Refreshes the time point of the value's lifetime begining.
@@ -158,8 +156,11 @@ struct Storage {
* @param vid The value id
* @return time of the next expiration, time_point::max() if no expiration
*/
std::pair<ValueStorage*, time_point>
refresh(const InfoHash& id, const time_point& now, const Value::Id& vid, const TypeStore& types) {
std::pair<ValueStorage*, time_point> refresh(const InfoHash& id,
const time_point& now,
const Value::Id& vid,
const TypeStore& types)
{
for (auto& vs : values)
if (vs.data->id == vid) {
vs.created = now;
@@ -174,9 +175,7 @@ struct Storage {
size_t listen(ValueCallback& cb, Value::Filter& f, const Sp<Query>& q);
void cancelListen(size_t token) {
local_listeners.erase(token);
}
void cancelListen(size_t token) { local_listeners.erase(token); }
Sp<Value> remove(const InfoHash& id, Value::Id);
@@ -190,7 +189,6 @@ private:
size_t total_size {};
};
size_t
Storage::listen(ValueCallback& gcb, Value::Filter& filter, const Sp<Query>& query)
{
@@ -202,15 +200,14 @@ Storage::listen(ValueCallback& gcb, Value::Filter& filter, const Sp<Query>& quer
}
}
auto tokenlocal = ++listener_token;
local_listeners.emplace(tokenlocal, LocalListener{query, filter, gcb});
local_listeners.emplace(tokenlocal, LocalListener {query, filter, gcb});
return tokenlocal;
}
std::pair<ValueStorage*, Storage::StoreDiff>
Storage::store(const InfoHash& id, const Sp<Value>& value, time_point created, time_point expiration, StorageBucket* sb)
{
auto it = std::find_if (values.begin(), values.end(), [&](const ValueStorage& vr) {
auto it = std::find_if(values.begin(), values.end(), [&](const ValueStorage& vr) {
return vr.data == value || vr.data->id == value->id;
});
ssize_t size_new = value->size();
@@ -219,9 +216,9 @@ Storage::store(const InfoHash& id, const Sp<Value>& value, time_point created, t
it->created = created;
if (it->data != value) {
size_t size_old = it->data->size();
ssize_t size_diff = size_new - (ssize_t)size_old;
//DHT_LOG.DEBUG("Updating %s -> %s", id.toString().c_str(), value->toString().c_str());
// clear quota for previous value
ssize_t size_diff = size_new - (ssize_t) size_old;
// DHT_LOG.DEBUG("Updating %s -> %s", id.toString().c_str(), value->toString().c_str());
// clear quota for previous value
if (it->store_bucket)
it->store_bucket->erase(id, *it->data, it->expiration);
it->expiration = expiration;
@@ -231,28 +228,26 @@ Storage::store(const InfoHash& id, const Sp<Value>& value, time_point created, t
sb->insert(id, *value, expiration);
it->data = value;
total_size += size_diff;
return std::make_pair(&(*it), StoreDiff{size_diff, 0, 0, 1});
return std::make_pair(&(*it), StoreDiff {size_diff, 0, 0, 1});
}
} else {
//DHT_LOG.DEBUG("Storing %s -> %s", id.toString().c_str(), value->toString().c_str());
// DHT_LOG.DEBUG("Storing %s -> %s", id.toString().c_str(), value->toString().c_str());
if (values.size() < MAX_VALUES) {
total_size += size_new;
values.emplace_back(value, created, expiration);
values.back().store_bucket = sb;
if (sb)
sb->insert(id, *value, expiration);
return std::make_pair(&values.back(), StoreDiff{size_new, 1, 0, 0});
return std::make_pair(&values.back(), StoreDiff {size_new, 1, 0, 0});
}
}
return std::make_pair(nullptr, StoreDiff{});
return std::make_pair(nullptr, StoreDiff {});
}
Sp<Value>
Storage::remove(const InfoHash& id, Value::Id vid)
{
auto it = std::find_if (values.begin(), values.end(), [&](const ValueStorage& vr) {
return vr.data->id == vid;
});
auto it = std::find_if(values.begin(), values.end(), [&](const ValueStorage& vr) { return vr.data->id == vid; });
if (it == values.end())
return {};
ssize_t size = it->data->size();
@@ -291,15 +286,12 @@ Storage::expire(const InfoHash& id, time_point now)
}
if (node_listeners.empty()) {
nl_it = listeners.erase(nl_it);
}
else
} else
++nl_it;
}
// expire values
auto r = std::partition(values.begin(), values.end(), [&](const ValueStorage& v) {
return v.expiration > now;
});
auto r = std::partition(values.begin(), values.end(), [&](const ValueStorage& v) { return v.expiration > now; });
std::vector<Sp<Value>> ret;
ret.reserve(std::distance(r, values.end()));
ssize_t size_diff {0};
@@ -316,4 +308,4 @@ Storage::expire(const InfoHash& id, time_point now)
return {size_diff, std::move(ret)};
}
}
} // namespace dht
+18 -30
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "thread_pool.h"
@@ -21,7 +7,7 @@
#include <thread>
#include <iostream>
#include <ciso646> // fix windows compiler bug
#include <cmath> // std::pow
#include <cmath> // std::pow
namespace dht {
@@ -41,10 +27,9 @@ ThreadPool::io()
return pool;
}
ThreadPool::ThreadPool(unsigned minThreads, unsigned maxThreads)
: minThreads_(std::max(minThreads, 1u))
, maxThreads_(maxThreads ? std::max(minThreads_, maxThreads) : minThreads_)
: minThreads_(std::max(minThreads, 1u))
, maxThreads_(maxThreads ? std::max(minThreads_, maxThreads) : minThreads_)
{
threads_.reserve(maxThreads_);
if (minThreads_ != maxThreads_) {
@@ -53,7 +38,7 @@ ThreadPool::ThreadPool(unsigned minThreads, unsigned maxThreads)
}
ThreadPool::ThreadPool()
: ThreadPool(std::max(std::thread::hardware_concurrency(), 4u))
: ThreadPool(std::max(std::thread::hardware_concurrency(), 4u))
{}
ThreadPool::~ThreadPool()
@@ -65,14 +50,15 @@ void
ThreadPool::run(std::function<void()>&& cb)
{
std::unique_lock l(lock_);
if (not cb or not running_) return;
if (not cb or not running_)
return;
// launch new thread if necessary
if (not readyThreads_ && threads_.size() < maxThreads_) {
try {
bool permanent_thread = threads_.size() < minThreads_;
auto& thread = *threads_.emplace_back(std::make_unique<std::thread>());
thread = std::thread([this, permanent_thread, e=threadExpirationDelay, &thread]() {
thread = std::thread([this, permanent_thread, e = threadExpirationDelay, &thread]() {
while (true) {
std::function<void()> task;
@@ -80,7 +66,9 @@ ThreadPool::run(std::function<void()>&& cb)
{
std::unique_lock l(lock_);
readyThreads_++;
auto waitCond = [&](){ return not running_ or not tasks_.empty(); };
auto waitCond = [&]() {
return not running_ or not tasks_.empty();
};
if (permanent_thread)
cv_.wait(l, waitCond);
else
@@ -103,7 +91,7 @@ ThreadPool::run(std::function<void()>&& cb)
if (not permanent_thread)
threadEnded(thread);
});
} catch(const std::exception& e) {
} catch (const std::exception& e) {
std::cerr << "Exception starting thread: " << e.what() << std::endl;
if (threads_.empty())
throw;
@@ -121,7 +109,7 @@ void
ThreadPool::threadEnded(std::thread& thread)
{
std::lock_guard l(lock_);
tasks_.emplace([this,t=std::reference_wrapper<std::thread>(thread)]{
tasks_.emplace([this, t = std::reference_wrapper<std::thread>(thread)] {
std::lock_guard l(lock_);
for (auto it = threads_.begin(); it != threads_.end(); ++it) {
if (&*(*it) == &t.get()) {
@@ -135,7 +123,7 @@ ThreadPool::threadEnded(std::thread& thread)
// If new threads start later, increase the expiration delay.
if (threadExpirationDelay > std::chrono::hours(24 * 7)) {
// If we reach 7 days, assume the thread is regularly used at full capacity
minThreads_ = std::min(minThreads_+1, maxThreads_);
minThreads_ = std::min(minThreads_ + 1, maxThreads_);
} else {
threadExpirationDelay *= threadDelayRatio_;
}
@@ -147,7 +135,7 @@ ThreadPool::stop(bool wait)
{
std::unique_lock l(lock_);
if (wait) {
cv_.wait(l, [&](){ return tasks_.empty(); });
cv_.wait(l, [&]() { return tasks_.empty(); });
}
running_ = false;
tasks_ = {};
@@ -190,7 +178,7 @@ Executor::run_(std::function<void()>&& task)
{
current_++;
std::weak_ptr<Executor> w = shared_from_this();
threadPool_.get().run([w,task = std::move(task)] {
threadPool_.get().run([w, task = std::move(task)] {
try {
task();
} catch (const std::exception& e) {
@@ -214,4 +202,4 @@ Executor::schedule()
}
}
}
} // namespace dht
+2 -16
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#ifdef HAVE_CONFIG_H
#include "config.h"
+2 -16
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#ifdef HAVE_CONFIG_H
#include "config.h"
+47 -50
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "value.h"
@@ -25,22 +11,29 @@ enum class ListenSyncStatus { ADDED, SYNCED, UNSYNCED, REMOVED };
using SyncCallback = std::function<void(ListenSyncStatus)>;
using CallbackQueue = std::list<std::function<void()>>;
class ValueCache {
class ValueCache
{
public:
explicit ValueCache(ValueStateCallback&& cb, SyncCallback&& scb = {})
: callback(std::forward<ValueStateCallback>(cb)), syncCallback(std::move(scb))
: callback(std::forward<ValueStateCallback>(cb))
, syncCallback(std::move(scb))
{
if (syncCallback)
syncCallback(ListenSyncStatus::ADDED);
}
ValueCache(ValueCache&& o) noexcept : values(std::move(o.values)), callback(std::move(o.callback)), syncCallback(std::move(o.syncCallback)) {
ValueCache(ValueCache&& o) noexcept
: values(std::move(o.values))
, callback(std::move(o.callback))
, syncCallback(std::move(o.syncCallback))
{
o.callback = {};
o.syncCallback = {};
}
~ValueCache() {
~ValueCache()
{
auto q = clear();
for (auto& cb: q)
for (auto& cb : q)
cb();
if (syncCallback) {
if (status == ListenSyncStatus::SYNCED)
@@ -49,7 +42,8 @@ public:
}
}
CallbackQueue clear() {
CallbackQueue clear()
{
std::vector<Sp<Value>> expired_values;
expired_values.reserve(values.size());
for (auto& v : values)
@@ -57,14 +51,13 @@ public:
values.clear();
CallbackQueue ret;
if (not expired_values.empty() and callback) {
ret.emplace_back([expired_values = std::move(expired_values), cb = callback]{
cb(expired_values, true);
});
ret.emplace_back([expired_values = std::move(expired_values), cb = callback] { cb(expired_values, true); });
}
return ret;
}
time_point expireValues(const time_point& now) {
time_point expireValues(const time_point& now)
{
time_point ret = time_point::max();
auto cbs = expireValues(now, ret);
while (not cbs.empty()) {
@@ -74,7 +67,8 @@ public:
return ret;
}
CallbackQueue expireValues(const time_point& now, time_point& next) {
CallbackQueue expireValues(const time_point& now, time_point& next)
{
std::vector<Sp<Value>> expired_values;
for (auto it = values.begin(); it != values.end();) {
if (it->second.expiration <= now) {
@@ -101,18 +95,16 @@ public:
}
CallbackQueue ret;
if (not expired_values.empty() and callback) {
ret.emplace_back([cb = callback, expired_values = std::move(expired_values)]{
cb(expired_values, true);
});
ret.emplace_back([cb = callback, expired_values = std::move(expired_values)] { cb(expired_values, true); });
}
return ret;
}
time_point onValues
(const std::vector<Sp<Value>>& new_values,
const std::vector<Value::Id>& refreshed_values,
const std::vector<Value::Id>& expired_values,
const TypeStore& types, const time_point& now)
time_point onValues(const std::vector<Sp<Value>>& new_values,
const std::vector<Value::Id>& refreshed_values,
const std::vector<Value::Id>& expired_values,
const TypeStore& types,
const time_point& now)
{
CallbackQueue cbs;
time_point ret = time_point::max();
@@ -130,7 +122,8 @@ public:
return ret;
}
void onSynced(bool synced) {
void onSynced(bool synced)
{
auto newStatus = synced ? ListenSyncStatus::SYNCED : ListenSyncStatus::UNSYNCED;
if (status != newStatus) {
status = newStatus;
@@ -139,9 +132,7 @@ public:
}
}
size_t size() const {
return values.size();
}
size_t size() const { return values.size(); }
private:
// prevent copy
@@ -152,14 +143,18 @@ private:
/* The maximum number of values we store in the cache. */
static constexpr unsigned MAX_VALUES {4096};
struct CacheValueStorage {
struct CacheValueStorage
{
Sp<Value> data {};
time_point created {};
time_point expiration {};
CacheValueStorage() {}
CacheValueStorage(const Sp<Value>& v, time_point t, time_point e)
: data(v), created(t), expiration(e) {}
: data(v)
, created(t)
, expiration(e)
{}
};
std::map<Value::Id, CacheValueStorage> values;
@@ -167,7 +162,8 @@ private:
SyncCallback syncCallback;
ListenSyncStatus status {ListenSyncStatus::UNSYNCED};
CallbackQueue addValues(const std::vector<Sp<Value>>& new_values, const TypeStore& types, const time_point& now) {
CallbackQueue addValues(const std::vector<Sp<Value>>& new_values, const TypeStore& types, const time_point& now)
{
std::vector<Sp<Value>> nvals;
for (const auto& value : new_values) {
auto v = values.find(value->id);
@@ -183,24 +179,25 @@ private:
}
CallbackQueue ret;
if (callback and not nvals.empty())
ret.emplace_back([cb = callback, nvals = std::move(nvals)]{
cb(nvals, false);
});
ret.emplace_back([cb = callback, nvals = std::move(nvals)] { cb(nvals, false); });
return ret;
}
CallbackQueue expireValue(Value::Id vid) {
CallbackQueue expireValue(Value::Id vid)
{
auto v = values.find(vid);
if (v == values.end())
return {};
std::vector<Sp<Value>> val {std::move(v->second.data)};
values.erase(v);
CallbackQueue ret;
ret.emplace_back([cb = callback, val = std::move(val)]{
if (cb) cb(val, true);
ret.emplace_back([cb = callback, val = std::move(val)] {
if (cb)
cb(val, true);
});
return ret;
}
void refreshValue(Value::Id vid, const TypeStore& types, const time_point& now) {
void refreshValue(Value::Id vid, const TypeStore& types, const time_point& now)
{
auto v = values.find(vid);
if (v == values.end())
return;
@@ -209,4 +206,4 @@ private:
}
};
}
} // namespace dht
+2 -18
View File
@@ -1,21 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "cryptotester.h"
+2 -19
View File
@@ -1,22 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
// cppunit
+16 -27
View File
@@ -1,19 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "dhtproxy_stress_tester.h"
@@ -28,7 +14,8 @@ namespace test {
CPPUNIT_TEST_SUITE_REGISTRATION(DhtProxyStressTester);
void
DhtProxyStressTester::setUp() {
DhtProxyStressTester::setUp()
{
logger = dht::log::getStdLogger();
dht::DhtRunner::Context ctx;
@@ -55,26 +42,28 @@ DhtProxyStressTester::setUp() {
}
void
DhtProxyStressTester::tearDown() {
DhtProxyStressTester::tearDown()
{
nodePeer.join();
nodeClient.join();
bool done = false;
std::condition_variable cv;
std::mutex cv_m;
nodeProxy->shutdown([&]{
nodeProxy->shutdown([&] {
std::lock_guard<std::mutex> lk(cv_m);
done = true;
cv.notify_all();
});
std::unique_lock<std::mutex> lk(cv_m);
CPPUNIT_ASSERT(cv.wait_for(lk, 15s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 15s, [&] { return done; }));
serverProxy.reset();
nodeProxy.reset();
}
void
DhtProxyStressTester::testRepeatValues() {
DhtProxyStressTester::testRepeatValues()
{
dht::DhtRunner::Context ctx;
ctx.logger = logger;
nodeClient.run(0, clientConfig, std::move(ctx));
@@ -95,7 +84,7 @@ DhtProxyStressTester::testRepeatValues() {
done = true;
cv.notify_all();
});
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return done; }));
done = false;
std::vector<dht::Blob> values;
@@ -109,7 +98,7 @@ DhtProxyStressTester::testRepeatValues() {
}
return true;
});
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return done; }));
done = false;
// Here values should contains 1 values
CPPUNIT_ASSERT_EQUAL(static_cast<int>(values.size()), 1);
@@ -119,7 +108,7 @@ DhtProxyStressTester::testRepeatValues() {
dht::Value secondVal {"You're a monster"};
auto secondVal_data = secondVal.data;
nodePeer.put(key, std::move(secondVal));
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return done; }));
nodeClient.cancelListen(key, std::move(token));
CPPUNIT_ASSERT_EQUAL(static_cast<int>(values.size()), 2);
CPPUNIT_ASSERT(values.back() == secondVal_data);
@@ -147,7 +136,7 @@ DhtProxyStressTester::testRepeatValues() {
done = true;
cv.notify_all();
});
CPPUNIT_ASSERT(cv.wait_for(lk, 20s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 20s, [&] { return done; }));
done = false;
// temporary node to make sure the value is retrieved
auto tmpValues = tmpNode->get(key);
@@ -165,7 +154,7 @@ DhtProxyStressTester::testRepeatValues() {
}
return true;
});
CPPUNIT_ASSERT(cv.wait_for(lk, 25s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 25s, [&] { return done; }));
done = false;
bool found_value = false;
@@ -182,4 +171,4 @@ DhtProxyStressTester::testRepeatValues() {
}
}
} // namespace test
} // namespace test
+10 -24
View File
@@ -1,20 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#pragma once
// cppunit
@@ -27,27 +12,28 @@
namespace test {
class DhtProxyStressTester : public CppUnit::TestFixture {
class DhtProxyStressTester : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(DhtProxyStressTester);
CPPUNIT_TEST(testRepeatValues);
CPPUNIT_TEST_SUITE_END();
public:
public:
/**
* Method automatically called before each test by CppUnit
* Init nodes
*/
void setUp();
void setUp();
/**
* Method automatically called after each test CppUnit
*/
void tearDown();
void tearDown();
/**
* Test get and put methods
*/
void testRepeatValues();
void testRepeatValues();
private:
private:
std::shared_ptr<dht::log::Logger> logger;
dht::DhtRunner::Config clientConfig {};
dht::DhtRunner nodePeer;
@@ -56,4 +42,4 @@ class DhtProxyStressTester : public CppUnit::TestFixture {
std::unique_ptr<dht::DhtProxyServer> serverProxy;
};
} // namespace test
} // namespace test
+67 -77
View File
@@ -1,21 +1,5 @@
/*
* Copyright (c) 2014-2026 Savoir-faire Linux Inc.
*
* Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
// SPDX-License-Identifier: MIT
#include "dhtproxytester.h"
@@ -32,7 +16,8 @@ namespace test {
CPPUNIT_TEST_SUITE_REGISTRATION(DhtProxyTester);
void
DhtProxyTester::setUp() {
DhtProxyTester::setUp()
{
clientConfig.dht_config.node_config.max_peer_req_per_sec = -1;
clientConfig.dht_config.node_config.max_req_per_sec = -1;
@@ -46,12 +31,12 @@ DhtProxyTester::setUp() {
bound.setLoopback();
nodeProxy->bootstrap(bound);
//auto serverCAIdentity = dht::crypto::generateEcIdentity("DHT Node CA");
// auto serverCAIdentity = dht::crypto::generateEcIdentity("DHT Node CA");
uint16_t port = 1024 + (std::rand() % (65535 - 1024));
dht::ProxyServerConfig serverConfig;
//serverConfig.identity = dht::crypto::generateIdentity("DHT Node", serverCAIdentity);
// serverConfig.identity = dht::crypto::generateIdentity("DHT Node", serverCAIdentity);
serverConfig.port = port;
// serverConfig.pushServer = "127.0.0.1:8090";
serverProxy = std::make_unique<dht::DhtProxyServer>(nodeProxy, serverConfig);
@@ -59,30 +44,32 @@ DhtProxyTester::setUp() {
/*clientConfig.server_ca = serverCAIdentity.second;
clientConfig.client_identity = dht::crypto::generateIdentity("DhtProxyTester");
clientConfig.push_node_id = "dhtnode";*/
clientConfig.proxy_server = "http://127.0.0.1:" + std::to_string(port);//"https://127.0.0.1:8080";
clientConfig.proxy_server = "http://127.0.0.1:" + std::to_string(port); //"https://127.0.0.1:8080";
}
void
DhtProxyTester::tearDown() {
DhtProxyTester::tearDown()
{
nodePeer.join();
nodeClient.join();
bool done = false;
std::condition_variable cv;
std::mutex cv_m;
nodeProxy->shutdown([&]{
nodeProxy->shutdown([&] {
std::lock_guard<std::mutex> lk(cv_m);
done = true;
cv.notify_all();
});
std::unique_lock<std::mutex> lk(cv_m);
CPPUNIT_ASSERT(cv.wait_for(lk, 15s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 15s, [&] { return done; }));
serverProxy.reset();
nodeProxy.reset();
}
void
DhtProxyTester::testGetPut() {
DhtProxyTester::testGetPut()
{
nodeClient.run(0, clientConfig);
bool done = false;
@@ -99,7 +86,7 @@ DhtProxyTester::testGetPut() {
cv.notify_all();
});
std::unique_lock<std::mutex> lk(cv_m);
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return done; }));
}
auto vals = nodeClient.get(key).get();
@@ -108,7 +95,8 @@ DhtProxyTester::testGetPut() {
}
void
DhtProxyTester::testListen() {
DhtProxyTester::testListen()
{
nodeClient.run(0, clientConfig);
std::condition_variable cv;
@@ -127,7 +115,7 @@ DhtProxyTester::testListen() {
done = true;
cv.notify_all();
});
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return done; }));
done = false;
std::vector<dht::Blob> values;
@@ -141,7 +129,7 @@ DhtProxyTester::testListen() {
}
return true;
});
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return done; }));
done = false;
// Here values should contains 1 values
CPPUNIT_ASSERT_EQUAL(static_cast<int>(values.size()), 1);
@@ -152,7 +140,7 @@ DhtProxyTester::testListen() {
dht::Value secondVal {"You're a monster"};
auto secondVal_data = secondVal.data;
nodePeer.put(key, std::move(secondVal));
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return done; }));
nodeClient.cancelListen(key, std::move(token));
// Here values should contains 2 values
CPPUNIT_ASSERT_EQUAL(static_cast<int>(values.size()), 2);
@@ -160,7 +148,8 @@ DhtProxyTester::testListen() {
}
void
DhtProxyTester::testResubscribeGetValues() {
DhtProxyTester::testResubscribeGetValues()
{
clientConfig.push_token = "atlas";
nodeClient.run(0, clientConfig);
@@ -180,14 +169,12 @@ DhtProxyTester::testResubscribeGetValues() {
done = true;
cv.notify_all();
});
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return done; }));
done = false;
// Send a first subscribe, the value is sent via a push notification
// So ignore values here.
nodeClient.listen(key, [&](const std::vector<std::shared_ptr<dht::Value>>&, bool) {
return true;
});
nodeClient.listen(key, [&](const std::vector<std::shared_ptr<dht::Value>>&, bool) { return true; });
cv.wait_for(lk, std::chrono::seconds(1));
// Reboot node (to avoid cache)
@@ -207,12 +194,12 @@ DhtProxyTester::testResubscribeGetValues() {
return true;
});
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return done; }));
auto token = ftoken.get();
CPPUNIT_ASSERT(token);
nodeClient.cancelListen(key, token);
// Here values should still contains 1 values
CPPUNIT_ASSERT_EQUAL((size_t)1u, values.size());
CPPUNIT_ASSERT_EQUAL((size_t) 1u, values.size());
CPPUNIT_ASSERT(firstVal_data == values.front()->data);
}
@@ -242,21 +229,24 @@ DhtProxyTester::testPutGet40KChars()
done_put = ok;
cv.notify_all();
});
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&]{ return done_put; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return done_put; }));
nodeClient.get(key, [&](const std::vector<std::shared_ptr<dht::Value>>& vals){
values.insert(values.end(), vals.begin(), vals.end());
return true;
},[&](bool ok){
std::lock_guard<std::mutex> lk(cv_m);
done_get = ok;
cv.notify_all();
});
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&]{ return done_get; }));
nodeClient.get(
key,
[&](const std::vector<std::shared_ptr<dht::Value>>& vals) {
values.insert(values.end(), vals.begin(), vals.end());
return true;
},
[&](bool ok) {
std::lock_guard<std::mutex> lk(cv_m);
done_get = ok;
cv.notify_all();
});
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return done_get; }));
// Assert
CPPUNIT_ASSERT_EQUAL((size_t)1u, values.size());
for (const auto &value: values)
CPPUNIT_ASSERT_EQUAL((size_t) 1u, values.size());
for (const auto& value : values)
CPPUNIT_ASSERT(value->data == mtu);
}
@@ -277,20 +267,19 @@ DhtProxyTester::testFuzzy()
for (size_t i = 0; i < 100; i++) {
auto nodeTest = std::make_shared<dht::DhtRunner>();
nodeTest->run(0, clientConfig);
nodeTest->put(key, dht::Value(mtu), [&](bool ok) {
CPPUNIT_ASSERT(ok);
});
nodeTest->get(key, [&](const std::vector<std::shared_ptr<dht::Value>>& vals){
values.insert(values.end(), vals.begin(), vals.end());
return true;
},[&](bool ok){
CPPUNIT_ASSERT(ok);
});
nodeTest->put(key, dht::Value(mtu), [&](bool ok) { CPPUNIT_ASSERT(ok); });
nodeTest->get(
key,
[&](const std::vector<std::shared_ptr<dht::Value>>& vals) {
values.insert(values.end(), vals.begin(), vals.end());
return true;
},
[&](bool ok) { CPPUNIT_ASSERT(ok); });
std::this_thread::sleep_for(5ms);
}
// Assert
for (const auto &value: values)
for (const auto& value : values)
CPPUNIT_ASSERT(value->data == mtu);
}
@@ -314,27 +303,28 @@ DhtProxyTester::testShutdownStop()
for (size_t i = 0; i < C; i++) {
auto nodeTest = std::make_shared<dht::DhtRunner>();
nodeTest->run(0, clientConfig);
nodeTest->put(key, dht::Value(mtu), [&](bool /*ok*/) {
callback_count++;
});
nodeTest->get(key, [&](const std::vector<std::shared_ptr<dht::Value>>& vals){
values.insert(values.end(), vals.begin(), vals.end());
return true;
},[&](bool /*ok*/){
callback_count++;
});
nodeTest->put(key, dht::Value(mtu), [&](bool /*ok*/) { callback_count++; });
nodeTest->get(
key,
[&](const std::vector<std::shared_ptr<dht::Value>>& vals) {
values.insert(values.end(), vals.begin(), vals.end());
return true;
},
[&](bool /*ok*/) { callback_count++; });
bool done = false;
std::condition_variable cv;
std::mutex cv_m;
nodeTest->shutdown([&]{
std::lock_guard<std::mutex> lk(cv_m);
done = true;
cv.notify_all();
}, true);
nodeTest->shutdown(
[&] {
std::lock_guard<std::mutex> lk(cv_m);
done = true;
cv.notify_all();
},
true);
std::unique_lock<std::mutex> lk(cv_m);
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&]{ return done; }));
CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return done; }));
}
CPPUNIT_ASSERT_EQUAL(2*C, callback_count.load());
CPPUNIT_ASSERT_EQUAL(2 * C, callback_count.load());
}
} // namespace test
} // namespace test
Loaded 100 of 123 files, more files were not shown because too many files have changed in this diff. Show more