From 3a9559be89f5992a6e0b8980f6b40ddce7ffef07 Mon Sep 17 00:00:00 2001 From: ng0 Date: Tue, 26 Nov 2019 15:27:42 +0000 Subject: init gnunet-dbus from SVN snapshot (restore lost data after crash). --- AUTHORS | 2 + COPYING | 674 +++++++++++++++++++ ChangeLog | 0 INSTALL | 370 +++++++++++ Makefile.am | 5 + NEWS | 0 README | 48 ++ bootstrap | 4 + configure.ac | 27 + src/Makefile.am | 3 + src/etc/gnunet-dbus.conf | 20 + src/lib/Makefile.am | 3 + src/lib/block/Makefile.am | 16 + src/lib/block/gnunet_block_dbus_lib.c | 26 + src/lib/block/gnunet_block_dbus_lib_pop.c | 20 + src/lib/block/gnunet_block_dbus_lib_push.c | 18 + src/lib/common/Makefile.am | 31 + src/lib/common/gnunet_dbus_lib_arg.c | 65 ++ src/lib/common/gnunet_dbus_lib_client.c | 119 ++++ src/lib/common/gnunet_dbus_lib_interface.c | 275 ++++++++ src/lib/common/gnunet_dbus_lib_message.c | 62 ++ src/lib/common/gnunet_dbus_lib_method.c | 149 +++++ src/lib/common/gnunet_dbus_lib_method_context.c | 83 +++ src/lib/common/gnunet_dbus_lib_object.c | 192 ++++++ src/lib/common/gnunet_dbus_lib_object_path.c | 162 +++++ src/lib/common/gnunet_dbus_lib_pop.c | 733 +++++++++++++++++++++ src/lib/common/gnunet_dbus_lib_push.c | 510 ++++++++++++++ src/lib/common/gnunet_dbus_lib_service.c | 686 +++++++++++++++++++ src/lib/common/gnunet_dbus_lib_signal.c | 126 ++++ src/lib/common/gnunet_dbus_lib_signature.c | 60 ++ src/lib/common/timeout.c | 131 ++++ src/lib/common/timeout.h | 31 + src/lib/common/watch.c | 235 +++++++ src/lib/common/watch.h | 34 + src/lib/dht/Makefile.am | 16 + src/lib/dht/gnunet_dht_dbus_lib.c | 13 + src/lib/dht/gnunet_dht_dbus_lib_pop.c | 16 + src/lib/dht/gnunet_dht_dbus_lib_push.c | 14 + src/lib/gnsrecord/Makefile.am | 17 + src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib.c | 23 + src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib_pop.c | 85 +++ src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib_push.c | 88 +++ src/lib/include/gnunet_block_dbus_lib.h | 18 + src/lib/include/gnunet_block_dbus_lib_pop.h | 24 + src/lib/include/gnunet_block_dbus_lib_push.h | 16 + src/lib/include/gnunet_block_dbus_lib_signature.h | 7 + src/lib/include/gnunet_crypto_dbus_lib.h | 11 + src/lib/include/gnunet_crypto_dbus_lib_pop.h | 33 + src/lib/include/gnunet_crypto_dbus_lib_push.h | 27 + src/lib/include/gnunet_crypto_dbus_lib_signature.h | 22 + src/lib/include/gnunet_dbus_lib.h | 29 + src/lib/include/gnunet_dbus_lib_arg.h | 75 +++ src/lib/include/gnunet_dbus_lib_client.h | 105 +++ src/lib/include/gnunet_dbus_lib_interface.h | 104 +++ src/lib/include/gnunet_dbus_lib_message.h | 45 ++ src/lib/include/gnunet_dbus_lib_method.h | 110 ++++ src/lib/include/gnunet_dbus_lib_method_context.h | 115 ++++ src/lib/include/gnunet_dbus_lib_object.h | 125 ++++ src/lib/include/gnunet_dbus_lib_object_path.h | 62 ++ src/lib/include/gnunet_dbus_lib_pop.h | 408 ++++++++++++ src/lib/include/gnunet_dbus_lib_push.h | 397 +++++++++++ src/lib/include/gnunet_dbus_lib_service.h | 87 +++ src/lib/include/gnunet_dbus_lib_signal.h | 102 +++ src/lib/include/gnunet_dbus_lib_signature.h | 42 ++ src/lib/include/gnunet_dht_dbus_lib.h | 23 + src/lib/include/gnunet_dht_dbus_lib_pop.h | 19 + src/lib/include/gnunet_dht_dbus_lib_push.h | 16 + src/lib/include/gnunet_dht_dbus_lib_signature.h | 9 + src/lib/include/gnunet_gnsrecord_dbus_lib.h | 17 + src/lib/include/gnunet_gnsrecord_dbus_lib_pop.h | 29 + src/lib/include/gnunet_gnsrecord_dbus_lib_push.h | 38 ++ .../include/gnunet_gnsrecord_dbus_lib_signature.h | 27 + src/lib/include/gnunet_time_dbus_lib.h | 11 + src/lib/include/gnunet_time_dbus_lib_pop.h | 17 + src/lib/include/gnunet_time_dbus_lib_push.h | 23 + src/lib/include/gnunet_time_dbus_lib_signature.h | 10 + src/lib/util/Makefile.am | 27 + src/lib/util/gnunet_crypto_dbus_lib_pop.c | 145 ++++ src/lib/util/gnunet_crypto_dbus_lib_push.c | 54 ++ src/lib/util/gnunet_time_dbus_lib_pop.c | 52 ++ src/lib/util/gnunet_time_dbus_lib_push.c | 61 ++ src/services/Makefile.am | 31 + src/services/gnunet-service-dht-dbus.c | 612 +++++++++++++++++ src/services/gnunet-service-gns-dbus.c | 198 ++++++ src/services/gnunet-service-namecache-dbus.c | 158 +++++ 85 files changed, 8733 insertions(+) create mode 100644 AUTHORS create mode 100644 COPYING create mode 100644 ChangeLog create mode 100644 INSTALL create mode 100644 Makefile.am create mode 100644 NEWS create mode 100644 README create mode 100755 bootstrap create mode 100644 configure.ac create mode 100644 src/Makefile.am create mode 100644 src/etc/gnunet-dbus.conf create mode 100644 src/lib/Makefile.am create mode 100644 src/lib/block/Makefile.am create mode 100644 src/lib/block/gnunet_block_dbus_lib.c create mode 100644 src/lib/block/gnunet_block_dbus_lib_pop.c create mode 100644 src/lib/block/gnunet_block_dbus_lib_push.c create mode 100644 src/lib/common/Makefile.am create mode 100644 src/lib/common/gnunet_dbus_lib_arg.c create mode 100644 src/lib/common/gnunet_dbus_lib_client.c create mode 100644 src/lib/common/gnunet_dbus_lib_interface.c create mode 100644 src/lib/common/gnunet_dbus_lib_message.c create mode 100644 src/lib/common/gnunet_dbus_lib_method.c create mode 100644 src/lib/common/gnunet_dbus_lib_method_context.c create mode 100644 src/lib/common/gnunet_dbus_lib_object.c create mode 100644 src/lib/common/gnunet_dbus_lib_object_path.c create mode 100644 src/lib/common/gnunet_dbus_lib_pop.c create mode 100644 src/lib/common/gnunet_dbus_lib_push.c create mode 100644 src/lib/common/gnunet_dbus_lib_service.c create mode 100644 src/lib/common/gnunet_dbus_lib_signal.c create mode 100644 src/lib/common/gnunet_dbus_lib_signature.c create mode 100644 src/lib/common/timeout.c create mode 100644 src/lib/common/timeout.h create mode 100644 src/lib/common/watch.c create mode 100644 src/lib/common/watch.h create mode 100644 src/lib/dht/Makefile.am create mode 100644 src/lib/dht/gnunet_dht_dbus_lib.c create mode 100644 src/lib/dht/gnunet_dht_dbus_lib_pop.c create mode 100644 src/lib/dht/gnunet_dht_dbus_lib_push.c create mode 100644 src/lib/gnsrecord/Makefile.am create mode 100644 src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib.c create mode 100644 src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib_pop.c create mode 100644 src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib_push.c create mode 100644 src/lib/include/gnunet_block_dbus_lib.h create mode 100644 src/lib/include/gnunet_block_dbus_lib_pop.h create mode 100644 src/lib/include/gnunet_block_dbus_lib_push.h create mode 100644 src/lib/include/gnunet_block_dbus_lib_signature.h create mode 100644 src/lib/include/gnunet_crypto_dbus_lib.h create mode 100644 src/lib/include/gnunet_crypto_dbus_lib_pop.h create mode 100644 src/lib/include/gnunet_crypto_dbus_lib_push.h create mode 100644 src/lib/include/gnunet_crypto_dbus_lib_signature.h create mode 100644 src/lib/include/gnunet_dbus_lib.h create mode 100644 src/lib/include/gnunet_dbus_lib_arg.h create mode 100644 src/lib/include/gnunet_dbus_lib_client.h create mode 100644 src/lib/include/gnunet_dbus_lib_interface.h create mode 100644 src/lib/include/gnunet_dbus_lib_message.h create mode 100644 src/lib/include/gnunet_dbus_lib_method.h create mode 100644 src/lib/include/gnunet_dbus_lib_method_context.h create mode 100644 src/lib/include/gnunet_dbus_lib_object.h create mode 100644 src/lib/include/gnunet_dbus_lib_object_path.h create mode 100644 src/lib/include/gnunet_dbus_lib_pop.h create mode 100644 src/lib/include/gnunet_dbus_lib_push.h create mode 100644 src/lib/include/gnunet_dbus_lib_service.h create mode 100644 src/lib/include/gnunet_dbus_lib_signal.h create mode 100644 src/lib/include/gnunet_dbus_lib_signature.h create mode 100644 src/lib/include/gnunet_dht_dbus_lib.h create mode 100644 src/lib/include/gnunet_dht_dbus_lib_pop.h create mode 100644 src/lib/include/gnunet_dht_dbus_lib_push.h create mode 100644 src/lib/include/gnunet_dht_dbus_lib_signature.h create mode 100644 src/lib/include/gnunet_gnsrecord_dbus_lib.h create mode 100644 src/lib/include/gnunet_gnsrecord_dbus_lib_pop.h create mode 100644 src/lib/include/gnunet_gnsrecord_dbus_lib_push.h create mode 100644 src/lib/include/gnunet_gnsrecord_dbus_lib_signature.h create mode 100644 src/lib/include/gnunet_time_dbus_lib.h create mode 100644 src/lib/include/gnunet_time_dbus_lib_pop.h create mode 100644 src/lib/include/gnunet_time_dbus_lib_push.h create mode 100644 src/lib/include/gnunet_time_dbus_lib_signature.h create mode 100644 src/lib/util/Makefile.am create mode 100644 src/lib/util/gnunet_crypto_dbus_lib_pop.c create mode 100644 src/lib/util/gnunet_crypto_dbus_lib_push.c create mode 100644 src/lib/util/gnunet_time_dbus_lib_pop.c create mode 100644 src/lib/util/gnunet_time_dbus_lib_push.c create mode 100644 src/services/Makefile.am create mode 100644 src/services/gnunet-service-dht-dbus.c create mode 100644 src/services/gnunet-service-gns-dbus.c create mode 100644 src/services/gnunet-service-namecache-dbus.c diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..473113c --- /dev/null +++ b/AUTHORS @@ -0,0 +1,2 @@ +Andrew Cann + diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..94a9ed0 --- /dev/null +++ b/COPYING @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + 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. + + + Copyright (C) + + 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 . + +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: + + Copyright (C) + 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 +. + + 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 +. diff --git a/ChangeLog b/ChangeLog new file mode 100644 index 0000000..e69de29 diff --git a/INSTALL b/INSTALL new file mode 100644 index 0000000..007e939 --- /dev/null +++ b/INSTALL @@ -0,0 +1,370 @@ +Installation Instructions +************************* + +Copyright (C) 1994-1996, 1999-2002, 2004-2013 Free Software Foundation, +Inc. + + Copying and distribution of this file, with or without modification, +are permitted in any medium without royalty provided the copyright +notice and this notice are preserved. This file is offered as-is, +without warranty of any kind. + +Basic Installation +================== + + Briefly, the shell commands `./configure; make; make install' should +configure, build, and install this package. The following +more-detailed instructions are generic; see the `README' file for +instructions specific to this package. Some packages provide this +`INSTALL' file but do not implement all of the features documented +below. The lack of an optional feature in a given package is not +necessarily a bug. More recommendations for GNU packages can be found +in *note Makefile Conventions: (standards)Makefile Conventions. + + The `configure' shell script attempts to guess correct values for +various system-dependent variables used during compilation. It uses +those values to create a `Makefile' in each directory of the package. +It may also create one or more `.h' files containing system-dependent +definitions. Finally, it creates a shell script `config.status' that +you can run in the future to recreate the current configuration, and a +file `config.log' containing compiler output (useful mainly for +debugging `configure'). + + It can also use an optional file (typically called `config.cache' +and enabled with `--cache-file=config.cache' or simply `-C') that saves +the results of its tests to speed up reconfiguring. Caching is +disabled by default to prevent problems with accidental use of stale +cache files. + + If you need to do unusual things to compile the package, please try +to figure out how `configure' could check whether to do them, and mail +diffs or instructions to the address given in the `README' so they can +be considered for the next release. If you are using the cache, and at +some point `config.cache' contains results you don't want to keep, you +may remove or edit it. + + The file `configure.ac' (or `configure.in') is used to create +`configure' by a program called `autoconf'. You need `configure.ac' if +you want to change it or regenerate `configure' using a newer version +of `autoconf'. + + The simplest way to compile this package is: + + 1. `cd' to the directory containing the package's source code and type + `./configure' to configure the package for your system. + + Running `configure' might take a while. While running, it prints + some messages telling which features it is checking for. + + 2. Type `make' to compile the package. + + 3. Optionally, type `make check' to run any self-tests that come with + the package, generally using the just-built uninstalled binaries. + + 4. Type `make install' to install the programs and any data files and + documentation. When installing into a prefix owned by root, it is + recommended that the package be configured and built as a regular + user, and only the `make install' phase executed with root + privileges. + + 5. Optionally, type `make installcheck' to repeat any self-tests, but + this time using the binaries in their final installed location. + This target does not install anything. Running this target as a + regular user, particularly if the prior `make install' required + root privileges, verifies that the installation completed + correctly. + + 6. You can remove the program binaries and object files from the + source code directory by typing `make clean'. To also remove the + files that `configure' created (so you can compile the package for + a different kind of computer), type `make distclean'. There is + also a `make maintainer-clean' target, but that is intended mainly + for the package's developers. If you use it, you may have to get + all sorts of other programs in order to regenerate files that came + with the distribution. + + 7. Often, you can also type `make uninstall' to remove the installed + files again. In practice, not all packages have tested that + uninstallation works correctly, even though it is required by the + GNU Coding Standards. + + 8. Some packages, particularly those that use Automake, provide `make + distcheck', which can by used by developers to test that all other + targets like `make install' and `make uninstall' work correctly. + This target is generally not run by end users. + +Compilers and Options +===================== + + Some systems require unusual options for compilation or linking that +the `configure' script does not know about. Run `./configure --help' +for details on some of the pertinent environment variables. + + You can give `configure' initial values for configuration parameters +by setting variables in the command line or in the environment. Here +is an example: + + ./configure CC=c99 CFLAGS=-g LIBS=-lposix + + *Note Defining Variables::, for more details. + +Compiling For Multiple Architectures +==================================== + + You can compile the package for more than one kind of computer at the +same time, by placing the object files for each architecture in their +own directory. To do this, you can use GNU `make'. `cd' to the +directory where you want the object files and executables to go and run +the `configure' script. `configure' automatically checks for the +source code in the directory that `configure' is in and in `..'. This +is known as a "VPATH" build. + + With a non-GNU `make', it is safer to compile the package for one +architecture at a time in the source code directory. After you have +installed the package for one architecture, use `make distclean' before +reconfiguring for another architecture. + + On MacOS X 10.5 and later systems, you can create libraries and +executables that work on multiple system types--known as "fat" or +"universal" binaries--by specifying multiple `-arch' options to the +compiler but only a single `-arch' option to the preprocessor. Like +this: + + ./configure CC="gcc -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ + CXX="g++ -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ + CPP="gcc -E" CXXCPP="g++ -E" + + This is not guaranteed to produce working output in all cases, you +may have to build one architecture at a time and combine the results +using the `lipo' tool if you have problems. + +Installation Names +================== + + By default, `make install' installs the package's commands under +`/usr/local/bin', include files under `/usr/local/include', etc. You +can specify an installation prefix other than `/usr/local' by giving +`configure' the option `--prefix=PREFIX', where PREFIX must be an +absolute file name. + + You can specify separate installation prefixes for +architecture-specific files and architecture-independent files. If you +pass the option `--exec-prefix=PREFIX' to `configure', the package uses +PREFIX as the prefix for installing programs and libraries. +Documentation and other data files still use the regular prefix. + + In addition, if you use an unusual directory layout you can give +options like `--bindir=DIR' to specify different values for particular +kinds of files. Run `configure --help' for a list of the directories +you can set and what kinds of files go in them. In general, the +default for these options is expressed in terms of `${prefix}', so that +specifying just `--prefix' will affect all of the other directory +specifications that were not explicitly provided. + + The most portable way to affect installation locations is to pass the +correct locations to `configure'; however, many packages provide one or +both of the following shortcuts of passing variable assignments to the +`make install' command line to change installation locations without +having to reconfigure or recompile. + + The first method involves providing an override variable for each +affected directory. For example, `make install +prefix=/alternate/directory' will choose an alternate location for all +directory configuration variables that were expressed in terms of +`${prefix}'. Any directories that were specified during `configure', +but not in terms of `${prefix}', must each be overridden at install +time for the entire installation to be relocated. The approach of +makefile variable overrides for each directory variable is required by +the GNU Coding Standards, and ideally causes no recompilation. +However, some platforms have known limitations with the semantics of +shared libraries that end up requiring recompilation when using this +method, particularly noticeable in packages that use GNU Libtool. + + The second method involves providing the `DESTDIR' variable. For +example, `make install DESTDIR=/alternate/directory' will prepend +`/alternate/directory' before all installation names. The approach of +`DESTDIR' overrides is not required by the GNU Coding Standards, and +does not work on platforms that have drive letters. On the other hand, +it does better at avoiding recompilation issues, and works well even +when some directory options were not specified in terms of `${prefix}' +at `configure' time. + +Optional Features +================= + + If the package supports it, you can cause programs to be installed +with an extra prefix or suffix on their names by giving `configure' the +option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. + + Some packages pay attention to `--enable-FEATURE' options to +`configure', where FEATURE indicates an optional part of the package. +They may also pay attention to `--with-PACKAGE' options, where PACKAGE +is something like `gnu-as' or `x' (for the X Window System). The +`README' should mention any `--enable-' and `--with-' options that the +package recognizes. + + For packages that use the X Window System, `configure' can usually +find the X include and library files automatically, but if it doesn't, +you can use the `configure' options `--x-includes=DIR' and +`--x-libraries=DIR' to specify their locations. + + Some packages offer the ability to configure how verbose the +execution of `make' will be. For these packages, running `./configure +--enable-silent-rules' sets the default to minimal output, which can be +overridden with `make V=1'; while running `./configure +--disable-silent-rules' sets the default to verbose, which can be +overridden with `make V=0'. + +Particular systems +================== + + On HP-UX, the default C compiler is not ANSI C compatible. If GNU +CC is not installed, it is recommended to use the following options in +order to use an ANSI C compiler: + + ./configure CC="cc -Ae -D_XOPEN_SOURCE=500" + +and if that doesn't work, install pre-built binaries of GCC for HP-UX. + + HP-UX `make' updates targets which have the same time stamps as +their prerequisites, which makes it generally unusable when shipped +generated files such as `configure' are involved. Use GNU `make' +instead. + + On OSF/1 a.k.a. Tru64, some versions of the default C compiler cannot +parse its `' header file. The option `-nodtk' can be used as +a workaround. If GNU CC is not installed, it is therefore recommended +to try + + ./configure CC="cc" + +and if that doesn't work, try + + ./configure CC="cc -nodtk" + + On Solaris, don't put `/usr/ucb' early in your `PATH'. This +directory contains several dysfunctional programs; working variants of +these programs are available in `/usr/bin'. So, if you need `/usr/ucb' +in your `PATH', put it _after_ `/usr/bin'. + + On Haiku, software installed for all users goes in `/boot/common', +not `/usr/local'. It is recommended to use the following options: + + ./configure --prefix=/boot/common + +Specifying the System Type +========================== + + There may be some features `configure' cannot figure out +automatically, but needs to determine by the type of machine the package +will run on. Usually, assuming the package is built to be run on the +_same_ architectures, `configure' can figure that out, but if it prints +a message saying it cannot guess the machine type, give it the +`--build=TYPE' option. TYPE can either be a short name for the system +type, such as `sun4', or a canonical name which has the form: + + CPU-COMPANY-SYSTEM + +where SYSTEM can have one of these forms: + + OS + KERNEL-OS + + See the file `config.sub' for the possible values of each field. If +`config.sub' isn't included in this package, then this package doesn't +need to know the machine type. + + If you are _building_ compiler tools for cross-compiling, you should +use the option `--target=TYPE' to select the type of system they will +produce code for. + + If you want to _use_ a cross compiler, that generates code for a +platform different from the build platform, you should specify the +"host" platform (i.e., that on which the generated programs will +eventually be run) with `--host=TYPE'. + +Sharing Defaults +================ + + If you want to set default values for `configure' scripts to share, +you can create a site shell script called `config.site' that gives +default values for variables like `CC', `cache_file', and `prefix'. +`configure' looks for `PREFIX/share/config.site' if it exists, then +`PREFIX/etc/config.site' if it exists. Or, you can set the +`CONFIG_SITE' environment variable to the location of the site script. +A warning: not all `configure' scripts look for a site script. + +Defining Variables +================== + + Variables not defined in a site shell script can be set in the +environment passed to `configure'. However, some packages may run +configure again during the build, and the customized values of these +variables may be lost. In order to avoid this problem, you should set +them in the `configure' command line, using `VAR=value'. For example: + + ./configure CC=/usr/local2/bin/gcc + +causes the specified `gcc' to be used as the C compiler (unless it is +overridden in the site shell script). + +Unfortunately, this technique does not work for `CONFIG_SHELL' due to +an Autoconf limitation. Until the limitation is lifted, you can use +this workaround: + + CONFIG_SHELL=/bin/bash ./configure CONFIG_SHELL=/bin/bash + +`configure' Invocation +====================== + + `configure' recognizes the following options to control how it +operates. + +`--help' +`-h' + Print a summary of all of the options to `configure', and exit. + +`--help=short' +`--help=recursive' + Print a summary of the options unique to this package's + `configure', and exit. The `short' variant lists options used + only in the top level, while the `recursive' variant lists options + also present in any nested packages. + +`--version' +`-V' + Print the version of Autoconf used to generate the `configure' + script, and exit. + +`--cache-file=FILE' + Enable the cache: use and save the results of the tests in FILE, + traditionally `config.cache'. FILE defaults to `/dev/null' to + disable caching. + +`--config-cache' +`-C' + Alias for `--cache-file=config.cache'. + +`--quiet' +`--silent' +`-q' + Do not print messages saying which checks are being made. To + suppress all normal output, redirect it to `/dev/null' (any error + messages will still be shown). + +`--srcdir=DIR' + Look for the package's source code in directory DIR. Usually + `configure' can determine that directory automatically. + +`--prefix=DIR' + Use DIR as the installation prefix. *note Installation Names:: + for more details, including other options available for fine-tuning + the installation locations. + +`--no-create' +`-n' + Run the configure checks, but stop before creating any output + files. + +`configure' also accepts some other, not widely useful, options. Run +`configure --help' for more details. diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..e2b7d09 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,5 @@ +# This Makefile.am is in the public domain +SUBDIRS = src + +ACLOCAL_AMFLAGS = -I m4 + diff --git a/NEWS b/NEWS new file mode 100644 index 0000000..e69de29 diff --git a/README b/README new file mode 100644 index 0000000..e78bfeb --- /dev/null +++ b/README @@ -0,0 +1,48 @@ +## gnunet-dbus: A DBus proxy for GNUnet + +# Purpose + +The aim of this project is to make it easy to control GNUnet via DBus. Any +DBus-enabled piece of software should be able to use this to start/stop GNUnet +services, perform GNS lookups, download files etc. + +# Installation + +$ ./bootstrap +$ ./configure +$ make +$ make install + +After that, you will need to relax the DBus permissions to allow gnunet-dbus to +use the system bus. On most Linux distros, copying src/etc/gnunet-dbus.conf to +/etc/dbus-1/system.d/ should do the trick. Otherwise, you can use that file for +guidance on how to setup permissions. + +# Running + +gnunet-dbus is not currently integrated with gnunet-arm or any init system so +you'll have to run the binaries manually. There is one gnunet-dbus executable +per GNUnet service. They are installed to $PREFIX/bin by default and have names +like gnunet-service-dht-dbus, gnunet-service-gns-dbus etc. + +# Hacking + +Contributions are welcome! + +In the src/ directory you'll find the service/ and lib/ directories. service/ +contains the source for the service executables (gnunet-service-gns-dbus etc.). +lib/ contains all the source and headers for the gnunet-dbus libraries. +lib/common/ is probably the most interesting directory here as it contains the +bulk of the code for binding libdbus with GNUnet's event loop. The other +directories in lib/ mainly consist of code for serialising/deserialising types +from the various parts of GNUnet's API. + +For learning how to use this library to write more service wrappers or extend +the existing ones it's probably easiest to read through existing code. The code +tries to be clear and logical and is very object-oriented. +gnunet-service-gns-dbus.c shows, for example, how to setup a dbus +service+object+interface+method for performing lookups and bind this DBus +method to an internal function which calls GNUNET_GNS_lookup. All types and +functions exposed by the library side of gnunet-dbus are documented in the +header files in src/lib/include + diff --git a/bootstrap b/bootstrap new file mode 100755 index 0000000..a28bc75 --- /dev/null +++ b/bootstrap @@ -0,0 +1,4 @@ +#!/bin/sh + +autoreconf --install + diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..f3381be --- /dev/null +++ b/configure.ac @@ -0,0 +1,27 @@ +AC_INIT([gnunet-dbus],[0.10.0],[shum@canndrew.org]) +AM_INIT_AUTOMAKE([-Wall -Werror]) +AC_CONFIG_MACRO_DIR([m4]) +AC_PROG_CC +AM_PROG_AR +LT_INIT([disable-static dlopen win32-dll]) +AC_CONFIG_HEADERS([config.h]) +AC_CONFIG_FILES([ +Makefile +src/Makefile +src/lib/Makefile +src/lib/common/Makefile +src/lib/block/Makefile +src/lib/dht/Makefile +src/lib/gnsrecord/Makefile +src/lib/util/Makefile +src/services/Makefile +]) + +PKG_CHECK_MODULES([DBUS], [dbus-1 >= 1.6.8]) + +CFLAGS="$CFLAGS -Wall -Werror" +CFLAGS="$CFLAGS $DBUS_CFLAGS" +LDFLAGS="$LDFLAGS $DBUS_LDFLAGS" + +AC_OUTPUT + diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..93829dd --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,3 @@ +# This Makefile.am is in the public domain +SUBDIRS = lib services + diff --git a/src/etc/gnunet-dbus.conf b/src/etc/gnunet-dbus.conf new file mode 100644 index 0000000..5cbc34a --- /dev/null +++ b/src/etc/gnunet-dbus.conf @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am new file mode 100644 index 0000000..594cb3a --- /dev/null +++ b/src/lib/Makefile.am @@ -0,0 +1,3 @@ +# This Makefile.am is in the public domain +SUBDIRS = common block dht util gnsrecord + diff --git a/src/lib/block/Makefile.am b/src/lib/block/Makefile.am new file mode 100644 index 0000000..b3af878 --- /dev/null +++ b/src/lib/block/Makefile.am @@ -0,0 +1,16 @@ +# This Makefile.am is in the public domain +lib_LTLIBRARIES = \ + libgnunetblockdbus.la + +libgnunetblockdbus_la_SOURCES = \ + gnunet_block_dbus_lib.c \ + gnunet_block_dbus_lib_pop.c \ + gnunet_block_dbus_lib_push.c + +libgnunetblockdbus_la_CFLAGS = \ + -I$(top_builddir)/src/lib/include \ + $(DBUS_CFLAGS) + +libgnunetblockdbus_la_LDFLAGS = \ + $(DBUS_LIBS) + diff --git a/src/lib/block/gnunet_block_dbus_lib.c b/src/lib/block/gnunet_block_dbus_lib.c new file mode 100644 index 0000000..c0e52fe --- /dev/null +++ b/src/lib/block/gnunet_block_dbus_lib.c @@ -0,0 +1,26 @@ +#include "config.h" + +#include +#include +#include + +#include "gnunet_block_dbus_lib.h" + +const struct GNUNET_DBUS_StringEnumPair GNUNET_BLOCK_DBUS_type_description[] = { + {"any", GNUNET_BLOCK_TYPE_ANY}, + {"fs_dblock", GNUNET_BLOCK_TYPE_FS_DBLOCK}, + {"fs_iblock", GNUNET_BLOCK_TYPE_FS_IBLOCK}, + {"fs_kblock", GNUNET_BLOCK_TYPE_FS_KBLOCK}, + {"fs_sblock", GNUNET_BLOCK_TYPE_FS_SBLOCK}, + {"fs_nblock", GNUNET_BLOCK_TYPE_FS_NBLOCK}, + {"fs_ondemand", GNUNET_BLOCK_TYPE_FS_ONDEMAND}, + {"dht_hello", GNUNET_BLOCK_TYPE_DHT_HELLO}, + {"test", GNUNET_BLOCK_TYPE_TEST}, + {"fs_ublock", GNUNET_BLOCK_TYPE_FS_UBLOCK}, + {"dns", GNUNET_BLOCK_TYPE_DNS}, + {"gns_namerecord", GNUNET_BLOCK_TYPE_GNS_NAMERECORD}, + {"regex", GNUNET_BLOCK_TYPE_REGEX}, + {"regex_accept", GNUNET_BLOCK_TYPE_REGEX_ACCEPT}, + {NULL, 0} +}; + diff --git a/src/lib/block/gnunet_block_dbus_lib_pop.c b/src/lib/block/gnunet_block_dbus_lib_pop.c new file mode 100644 index 0000000..8323495 --- /dev/null +++ b/src/lib/block/gnunet_block_dbus_lib_pop.c @@ -0,0 +1,20 @@ +#include "config.h" + +#include +#include +#include + +#include "gnunet_dbus_lib.h" +#include "gnunet_block_dbus_lib.h" + +DBusMessage * +GNUNET_BLOCK_DBUS_pop_type ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + enum GNUNET_BLOCK_Type *value) +{ + return GNUNET_DBUS_pop_enum (message, iter, arg_name, (int *)value, GNUNET_BLOCK_DBUS_type_description); +}; + + diff --git a/src/lib/block/gnunet_block_dbus_lib_push.c b/src/lib/block/gnunet_block_dbus_lib_push.c new file mode 100644 index 0000000..1aaca01 --- /dev/null +++ b/src/lib/block/gnunet_block_dbus_lib_push.c @@ -0,0 +1,18 @@ +#include "config.h" + +#include +#include +#include + +#include "gnunet_dbus_lib.h" +#include "gnunet_block_dbus_lib.h" + +void +GNUNET_BLOCK_DBUS_push_type ( + DBusMessage *message, + DBusMessageIter *iter, + const enum GNUNET_BLOCK_Type *value) +{ + return GNUNET_DBUS_push_enum (message, iter, (const int *)value, GNUNET_BLOCK_DBUS_type_description); +}; + diff --git a/src/lib/common/Makefile.am b/src/lib/common/Makefile.am new file mode 100644 index 0000000..04f1489 --- /dev/null +++ b/src/lib/common/Makefile.am @@ -0,0 +1,31 @@ +# This Makefile.am is in the public domain +AM_CFLAGS = \ + -I$(top_builddir)/src/lib/include + +lib_LTLIBRARIES = \ + libgnunetdbus.la + +libgnunetdbus_la_SOURCES = \ + watch.c \ + timeout.c \ + gnunet_dbus_lib_service.c \ + gnunet_dbus_lib_object.c \ + gnunet_dbus_lib_object_path.c \ + gnunet_dbus_lib_interface.c \ + gnunet_dbus_lib_method.c \ + gnunet_dbus_lib_signal.c \ + gnunet_dbus_lib_arg.c \ + gnunet_dbus_lib_message.c \ + gnunet_dbus_lib_method_context.c \ + gnunet_dbus_lib_pop.c \ + gnunet_dbus_lib_push.c \ + gnunet_dbus_lib_client.c \ + gnunet_dbus_lib_signature.c + +libgnunetdbus_la_CFLAGS = \ + -I$(top_builddir)/src/lib/include \ + $(DBUS_CFLAGS) + +libgnunetdbus_la_LDFLAGS = \ + $(DBUS_LIBS) + diff --git a/src/lib/common/gnunet_dbus_lib_arg.c b/src/lib/common/gnunet_dbus_lib_arg.c new file mode 100644 index 0000000..a6be456 --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_arg.c @@ -0,0 +1,65 @@ +#include "config.h" + +#include +#include + +#include "gnunet_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from(kind, "dbus-arg", __VA_ARGS__) + +struct GNUNET_DBUS_Arg +{ + char *name; + const char *signature; + unsigned ref_count; +}; + +struct GNUNET_DBUS_Arg * +GNUNET_DBUS_arg_create ( + const char *name, + const char *signature) +{ + struct GNUNET_DBUS_Arg *arg = GNUNET_new (struct GNUNET_DBUS_Arg); + arg->name = GNUNET_strdup (name); + arg->signature = signature; + arg->ref_count = 1; + return arg; +}; + +void +GNUNET_DBUS_arg_ref ( + struct GNUNET_DBUS_Arg *arg) +{ + arg->ref_count++; +}; + +void +GNUNET_DBUS_arg_unref ( + struct GNUNET_DBUS_Arg *arg) +{ + if (arg->ref_count == 0) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unreference arg with ref count 0\n"); + GNUNET_abort_ (); + }; + if (0 == --(arg->ref_count)) + { + GNUNET_free (arg->name); + GNUNET_free (arg); + }; +}; + +const char * +GNUNET_DBUS_arg_get_name ( + const struct GNUNET_DBUS_Arg *arg) +{ + return arg->name; +}; + +const char * +GNUNET_DBUS_arg_get_signature ( + const struct GNUNET_DBUS_Arg *arg) +{ + return arg->signature; +}; + diff --git a/src/lib/common/gnunet_dbus_lib_client.c b/src/lib/common/gnunet_dbus_lib_client.c new file mode 100644 index 0000000..72b3aef --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_client.c @@ -0,0 +1,119 @@ +#include "config.h" + +#include +#include + +#include "gnunet_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-client", __VA_ARGS__) + +struct GNUNET_DBUS_Client +{ + char *unique_name; + void *data; + bool prefers_pretty_encodings; + unsigned ref_count; +}; + +struct GNUNET_DBUS_Client * +GNUNET_DBUS_client_create ( + const char *unique_name) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating client with unique name %s\n", unique_name); + + struct GNUNET_DBUS_Client *client = GNUNET_new (struct GNUNET_DBUS_Client); + client->unique_name = GNUNET_strdup (unique_name); + client->data = NULL; + client->prefers_pretty_encodings = false; + client->ref_count = 1; + + return client; +}; + +void +GNUNET_DBUS_client_ref ( + struct GNUNET_DBUS_Client *client) +{ + client->ref_count++; +} + +void +GNUNET_DBUS_client_unref ( + struct GNUNET_DBUS_Client *client) +{ + if (client->ref_count == 0) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unref client with ref count 0\n"); + LOG (GNUNET_ERROR_TYPE_ERROR, " unique_name == %s\n", client->unique_name); + GNUNET_abort_ (); + } + + if (0 == --(client->ref_count)) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying client with unique name %s\n", client->unique_name); + + GNUNET_free (client->unique_name); + GNUNET_free (client); + } +}; + +const char * +GNUNET_DBUS_client_get_unique_name ( + const struct GNUNET_DBUS_Client *client) +{ + return client->unique_name; +}; + +void +GNUNET_DBUS_client_set_data ( + struct GNUNET_DBUS_Client *client, + void *data) +{ + client->data = data; +}; + +void * +GNUNET_DBUS_client_get_data ( + const struct GNUNET_DBUS_Client *client) +{ + return client->data; +}; + +void +GNUNET_DBUS_client_set_prefers_pretty_encodings ( + struct GNUNET_DBUS_Client *client, + bool prefers_pretty_encodings) +{ + client->prefers_pretty_encodings = prefers_pretty_encodings; +} + +bool +GNUNET_DBUS_client_get_prefers_pretty_encodings ( + const struct GNUNET_DBUS_Client *client) +{ + return client->prefers_pretty_encodings; +}; + +DBusMessage * +GNUNET_DBUS_client_create_method_call ( + struct GNUNET_DBUS_Client *client, + const char *object_path, + const char *interface, + const char *method, + bool pretty, + void (*return_callback)(DBusMessage *message)) +{ + DBusMessage *ret = dbus_message_new_method_call ( + client->unique_name, + object_path, + interface, + method); + GNUNET_DBUS_message_set_pretty (ret, pretty); + if (return_callback) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Method calls on external objects not implemented yet.\n"); + GNUNET_abort_ (); + }; + return ret; +} + diff --git a/src/lib/common/gnunet_dbus_lib_interface.c b/src/lib/common/gnunet_dbus_lib_interface.c new file mode 100644 index 0000000..bbc3467 --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_interface.c @@ -0,0 +1,275 @@ +#include "config.h" + +#include + +#include +#include +#include + +#include "gnunet_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-interface", __VA_ARGS__) + +struct GNUNET_DBUS_Interface +{ + struct GNUNET_DBUS_MethodIterator *methods_front; + struct GNUNET_DBUS_MethodIterator *methods_back; + + struct GNUNET_DBUS_SignalIterator *signals_front; + struct GNUNET_DBUS_SignalIterator *signals_back; + + char *name; + unsigned ref_count; +}; + +struct GNUNET_DBUS_Interface * +GNUNET_DBUS_interface_create ( + const char *name) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating interface %s\n", name); + + struct GNUNET_DBUS_Interface *interface = GNUNET_new (struct GNUNET_DBUS_Interface); + + interface->methods_front = NULL; + interface->methods_back = NULL; + interface->name = GNUNET_strdup (name); + interface->ref_count = 1; + + return interface; +}; + +void +GNUNET_DBUS_interface_ref ( + struct GNUNET_DBUS_Interface *interface) +{ + interface->ref_count++; +}; + +void +GNUNET_DBUS_interface_unref ( + struct GNUNET_DBUS_Interface *interface) +{ + if (interface->ref_count == 0) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unreference interface with ref count 0\n"); + GNUNET_abort_ (); + }; + if (0 == --(interface->ref_count)) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying interface %s\n", interface->name); + + struct GNUNET_DBUS_MethodIterator *methods_iter = interface->methods_front; + while (methods_iter) + { + struct GNUNET_DBUS_MethodIterator *next = methods_iter->next; + GNUNET_DBUS_method_unref (methods_iter->method); + GNUNET_free (methods_iter); + methods_iter = next; + }; + + GNUNET_free (interface->name); + GNUNET_free (interface); + } +}; + +void +GNUNET_DBUS_interface_add_method ( + struct GNUNET_DBUS_Interface *interface, + struct GNUNET_DBUS_Method *method) +{ + struct GNUNET_DBUS_MethodIterator *meth_it = GNUNET_new (struct GNUNET_DBUS_MethodIterator); + meth_it->method = method; + GNUNET_DBUS_method_ref (method); + + GNUNET_CONTAINER_DLL_insert (interface->methods_front, + interface->methods_back, + meth_it); +}; + +void +GNUNET_DBUS_interface_add_signal ( + struct GNUNET_DBUS_Interface *interface, + struct GNUNET_DBUS_Signal *signal) +{ + struct GNUNET_DBUS_SignalIterator *sig_it = GNUNET_new (struct GNUNET_DBUS_SignalIterator); + sig_it->signal = signal; + GNUNET_DBUS_signal_ref (signal); + + GNUNET_CONTAINER_DLL_insert (interface->signals_front, + interface->signals_back, + sig_it); +}; + +const char * +GNUNET_DBUS_interface_get_name ( + const struct GNUNET_DBUS_Interface *interface) +{ + return interface->name; +}; + +const struct GNUNET_DBUS_MethodIterator * +GNUNET_DBUS_interface_iterate_methods ( + const struct GNUNET_DBUS_Interface *interface) +{ + return interface->methods_front; +}; + +const struct GNUNET_DBUS_SignalIterator * +GNUNET_DBUS_interface_iterate_signals ( + const struct GNUNET_DBUS_Interface *interface) +{ + return interface->signals_front; +} + +static void +introspectable_introspect ( + struct GNUNET_DBUS_MethodContext *mc) +{ + char *data = NULL; + char *new_data = NULL; + + LOG (GNUNET_ERROR_TYPE_DEBUG, "Introspecting\n"); + struct GNUNET_DBUS_Object *object = mc->object; + + GNUNET_asprintf (&data, "%s", DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE); + GNUNET_asprintf (&new_data, "%s\n", data); + GNUNET_free (data); data = new_data; + + const struct GNUNET_DBUS_InterfaceIterator *int_it = GNUNET_DBUS_object_iterate_interfaces (object); + while (int_it) + { + struct GNUNET_DBUS_Interface *interface = int_it->interface; + const char *interface_name = GNUNET_DBUS_interface_get_name (interface); + GNUNET_asprintf (&new_data, "%s \n", data, interface_name); + GNUNET_free (data); data = new_data; + const struct GNUNET_DBUS_MethodIterator *meth_it = GNUNET_DBUS_interface_iterate_methods (interface); + while (meth_it) + { + struct GNUNET_DBUS_Method *method = meth_it->method; + const char *method_name = GNUNET_DBUS_method_get_name (method); + GNUNET_asprintf (&new_data, "%s \n", data, method_name); + GNUNET_free (data); data = new_data; + const struct GNUNET_DBUS_ArgIterator *arg_it = GNUNET_DBUS_method_iterate_args (method); + while (arg_it) + { + struct GNUNET_DBUS_Arg *arg = arg_it->arg; + const char *arg_name = GNUNET_DBUS_arg_get_name (arg); + const char *signature = GNUNET_DBUS_arg_get_signature (arg); + GNUNET_asprintf ( + &new_data, + "%s \n", + data, + arg_name, + signature); + GNUNET_free (data); data = new_data; + arg_it = arg_it->next; + }; + + arg_it = GNUNET_DBUS_method_iterate_return_args (method); + while (arg_it) + { + struct GNUNET_DBUS_Arg *arg = arg_it->arg; + const char *arg_name = GNUNET_DBUS_arg_get_name (arg); + const char *signature = GNUNET_DBUS_arg_get_signature (arg); + GNUNET_asprintf ( + &new_data, + "%s \n", + data, + arg_name, + signature); + GNUNET_free (data); data = new_data; + arg_it = arg_it->next; + }; + GNUNET_asprintf (&new_data, "%s \n", data); + GNUNET_free (data); data = new_data; + meth_it = meth_it->next; + }; + const struct GNUNET_DBUS_SignalIterator *sig_it = GNUNET_DBUS_interface_iterate_signals (interface); + while (sig_it) + { + struct GNUNET_DBUS_Signal *signal = sig_it->signal; + const char *signal_name = GNUNET_DBUS_signal_get_name (signal); + GNUNET_asprintf (&new_data, "%s \n", data, signal_name); + GNUNET_free (data); data = new_data; + const struct GNUNET_DBUS_ArgIterator *arg_it = GNUNET_DBUS_signal_iterate_args (signal); + while (arg_it) + { + struct GNUNET_DBUS_Arg *arg = arg_it->arg; + const char *arg_name = GNUNET_DBUS_arg_get_name (arg); + const char *signature = GNUNET_DBUS_arg_get_signature (arg); + GNUNET_asprintf ( + &new_data, + "%s \n", + data, + arg_name, + signature); + GNUNET_free (data); data = new_data; + arg_it = arg_it->next; + } + GNUNET_asprintf (&new_data, "%s \n", data); + GNUNET_free (data); data = new_data; + sig_it = sig_it->next; + }; + GNUNET_asprintf (&new_data, "%s \n", data); + GNUNET_free (data); data = new_data; + int_it = int_it->next; + }; + const struct GNUNET_DBUS_ObjectIterator *obj_it = GNUNET_DBUS_object_iterate_subobjects (object); + while (obj_it) + { + const char *obj_name = GNUNET_DBUS_object_get_name (obj_it->object); + GNUNET_asprintf (&new_data, "%s \n", data, obj_name); + GNUNET_free (data); data = new_data; + obj_it = obj_it->next; + } + GNUNET_asprintf (&new_data, "%s\n", data); + GNUNET_free (data); data = new_data; + + DBusMessage *reply = GNUNET_DBUS_method_context_create_reply (mc); + DBusMessageIter reply_iter; + dbus_message_iter_init_append (reply, &reply_iter); + GNUNET_DBUS_push_string (reply, &reply_iter, (const char *const *)&data); + GNUNET_DBUS_method_context_send_reply (mc, reply); + GNUNET_free (data); +}; + +struct GNUNET_DBUS_Interface *interface_introspectable; + +static void construct_interface_introspectable () + __attribute__((constructor)); + +static void destruct_interface_introspectable () + __attribute__((destructor)); + +static void construct_interface_introspectable () +{ + interface_introspectable = GNUNET_DBUS_interface_create ("org.freedesktop.DBus.Introspectable"); + struct GNUNET_DBUS_Method *introspect = GNUNET_DBUS_method_create ("Introspect", introspectable_introspect); + GNUNET_DBUS_method_add_return_arg (introspect, "data", GNUNET_DBUS_SIGNATURE_STRING); + GNUNET_DBUS_interface_add_method (interface_introspectable, introspect); +} + +static void destruct_interface_introspectable () +{ + GNUNET_DBUS_interface_unref (interface_introspectable); +} + +struct GNUNET_DBUS_Interface * +GNUNET_DBUS_interface_introspectable () +{ + return interface_introspectable; +}; + +const struct GNUNET_DBUS_InterfaceIterator * +GNUNET_DBUS_interface_find ( + const struct GNUNET_DBUS_InterfaceIterator *int_it, + const char *name) +{ + for (; int_it; int_it = int_it->next) + { + if (! strcmp (name, int_it->interface->name)) + return int_it; + } + return NULL; +} + diff --git a/src/lib/common/gnunet_dbus_lib_message.c b/src/lib/common/gnunet_dbus_lib_message.c new file mode 100644 index 0000000..1d76f84 --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_message.c @@ -0,0 +1,62 @@ +#include "config.h" + +#include +#include + +#include "gnunet_dbus_lib_message.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-message", __VA_ARGS__) + +static dbus_int32_t +pretty_slot_id () +{ + static dbus_int32_t id = -1; + if (-1 == id) + { + dbus_bool_t succ = dbus_message_allocate_data_slot (&id); + if (! succ || -1 == id) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "dbus_message_allocate_data_slot failed. id == %lld\n", (long long)id); + GNUNET_abort_ (); + }; + }; + + return id; +}; + +bool +GNUNET_DBUS_message_get_pretty ( + DBusMessage *message) +{ + return (bool)dbus_message_get_data (message, pretty_slot_id ()); +} + +void +GNUNET_DBUS_message_set_pretty ( + DBusMessage *message, + bool pretty) +{ + dbus_int32_t id = pretty_slot_id (); + bool succ = dbus_message_set_data (message, id, (void *)pretty, NULL); + if (! succ) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "dbus_message_set_data failed. id == %lld\n", (long long)id); + GNUNET_abort_ (); + }; +}; + +void +GNUNET_DBUS_message_set_destination ( + DBusMessage *message, + struct GNUNET_DBUS_Client *client) +{ + dbus_message_set_destination (message, GNUNET_DBUS_client_get_unique_name (client)); +} + +void +GNUNET_DBUS_message_unset_destination ( + DBusMessage *message) +{ + dbus_message_set_destination (message, NULL); +} + diff --git a/src/lib/common/gnunet_dbus_lib_method.c b/src/lib/common/gnunet_dbus_lib_method.c new file mode 100644 index 0000000..ebf631d --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_method.c @@ -0,0 +1,149 @@ +#include "config.h" + +#include +#include +#include + +#include "gnunet_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-method", __VA_ARGS__) + +struct GNUNET_DBUS_Method +{ + struct GNUNET_DBUS_ArgIterator *args_front; + struct GNUNET_DBUS_ArgIterator *args_back; + + struct GNUNET_DBUS_ArgIterator *return_args_front; + struct GNUNET_DBUS_ArgIterator *return_args_back; + + char *name; + + void (*underlying_method)(struct GNUNET_DBUS_MethodContext *mc); + + unsigned ref_count; +}; + +struct GNUNET_DBUS_Method * +GNUNET_DBUS_method_create ( + const char *name, + void (*underlying_method)(struct GNUNET_DBUS_MethodContext *mc)) +{ + struct GNUNET_DBUS_Method *method = GNUNET_new (struct GNUNET_DBUS_Method); + method->name = GNUNET_strdup (name); + method->underlying_method = underlying_method; + method->ref_count = 1; + + return method; +}; + +void +GNUNET_DBUS_method_ref ( + struct GNUNET_DBUS_Method *method) +{ + method->ref_count++; +}; + +void +GNUNET_DBUS_method_unref ( + struct GNUNET_DBUS_Method *method) +{ + if (method->ref_count == 0) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unreference method with ref count 0\n"); + GNUNET_abort_ (); + }; + if (0 == --(method->ref_count)) + { + struct GNUNET_DBUS_ArgIterator *arg_it = method->args_front; + while (arg_it) + { + struct GNUNET_DBUS_ArgIterator *next = arg_it->next; + GNUNET_DBUS_arg_unref (arg_it->arg); + GNUNET_free (arg_it); + arg_it = next; + } + + arg_it = method->return_args_front; + while (arg_it) + { + struct GNUNET_DBUS_ArgIterator *next = arg_it->next; + GNUNET_DBUS_arg_unref (arg_it->arg); + GNUNET_free (arg_it); + arg_it = next; + } + + GNUNET_free (method->name); + GNUNET_free (method); + } +}; + +void +GNUNET_DBUS_method_add_arg ( + struct GNUNET_DBUS_Method *method, + const char *name, + const char *signature) +{ + struct GNUNET_DBUS_Arg *arg = GNUNET_DBUS_arg_create (name, signature); + struct GNUNET_DBUS_ArgIterator *arg_it = GNUNET_new (struct GNUNET_DBUS_ArgIterator); + arg_it->arg = arg; + GNUNET_CONTAINER_DLL_insert_tail (method->args_front, + method->args_back, + arg_it); +}; + +void +GNUNET_DBUS_method_add_return_arg ( + struct GNUNET_DBUS_Method *method, + const char *name, + const char *signature) +{ + struct GNUNET_DBUS_Arg *arg = GNUNET_DBUS_arg_create (name, signature); + struct GNUNET_DBUS_ArgIterator *arg_it = GNUNET_new (struct GNUNET_DBUS_ArgIterator); + arg_it->arg = arg; + GNUNET_CONTAINER_DLL_insert_tail (method->return_args_front, + method->return_args_back, + arg_it); +}; + +const char * +GNUNET_DBUS_method_get_name ( + const struct GNUNET_DBUS_Method *method) +{ + return method->name; +}; + +const struct GNUNET_DBUS_ArgIterator * +GNUNET_DBUS_method_iterate_args ( + const struct GNUNET_DBUS_Method *method) +{ + return method->args_front; +}; + +const struct GNUNET_DBUS_ArgIterator * +GNUNET_DBUS_method_iterate_return_args ( + const struct GNUNET_DBUS_Method *method) +{ + return method->return_args_front; +}; + +void +GNUNET_DBUS_method_call ( + struct GNUNET_DBUS_Method *method, + struct GNUNET_DBUS_MethodContext *mc) +{ + method->underlying_method (mc); +}; + +const struct GNUNET_DBUS_MethodIterator * +GNUNET_DBUS_method_find ( + const struct GNUNET_DBUS_MethodIterator *meth_it, + const char *name) +{ + for (; meth_it; meth_it = meth_it->next) + { + if (! strcmp (name, meth_it->method->name)) + return meth_it; + } + return NULL; +} + diff --git a/src/lib/common/gnunet_dbus_lib_method_context.c b/src/lib/common/gnunet_dbus_lib_method_context.c new file mode 100644 index 0000000..f7a4bac --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_method_context.c @@ -0,0 +1,83 @@ +#include "config.h" + +#include + +#include +#include +#include + +#include "gnunet_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-method-context", __VA_ARGS__) + +struct GNUNET_DBUS_MethodContext * +GNUNET_DBUS_method_context_create ( + struct GNUNET_DBUS_Client *client, + struct GNUNET_DBUS_Service *service, + struct GNUNET_DBUS_Object *object, + struct GNUNET_DBUS_Interface *interface, + struct GNUNET_DBUS_Method *method, + DBusMessage *message) +{ + struct GNUNET_DBUS_MethodContext *ret = GNUNET_new (struct GNUNET_DBUS_MethodContext); + ret->client = client; + ret->service = service; + ret->object = object; + ret->interface = interface; + ret->method = method; + ret->message = message; + ret->expects_reply = ! dbus_message_get_no_reply (message); + + ret->replied = false; + ret->ref_count = 1; + dbus_message_ref (message); + + return ret; +}; + +void +GNUNET_DBUS_method_context_ref ( + struct GNUNET_DBUS_MethodContext *mc) +{ + mc->ref_count++; +}; + +void +GNUNET_DBUS_method_context_unref ( + struct GNUNET_DBUS_MethodContext *mc) +{ + if (0 == mc->ref_count) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unreference method context with ref count 0\n"); + GNUNET_abort_ (); + }; + if (0 == --(mc->ref_count)) + { + dbus_message_unref (mc->message); + GNUNET_free (mc); + } +}; + +void +GNUNET_DBUS_method_context_send_reply ( + struct GNUNET_DBUS_MethodContext *mc, + DBusMessage *reply) +{ + if (! mc->expects_reply) + return; + + GNUNET_DBUS_service_send (mc->service, reply); + dbus_message_unref (reply); +}; + +DBusMessage * +GNUNET_DBUS_method_context_create_reply ( + struct GNUNET_DBUS_MethodContext *mc) +{ + DBusMessage *ret = dbus_message_new_method_return (mc->message); + //GNUNET_DBUS_message_set_pretty (ret, GNUNET_DBUS_client_get_prefers_pretty_encodings (mc->client)); + GNUNET_DBUS_message_set_pretty (ret, GNUNET_DBUS_message_get_pretty (mc->message)); + return ret; +} + + diff --git a/src/lib/common/gnunet_dbus_lib_object.c b/src/lib/common/gnunet_dbus_lib_object.c new file mode 100644 index 0000000..816f587 --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_object.c @@ -0,0 +1,192 @@ +#include "config.h" + +#include + +#include +#include +#include + +#include "gnunet_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-object", __VA_ARGS__) + +struct GNUNET_DBUS_Object +{ + struct GNUNET_DBUS_InterfaceIterator *interfaces_front; + struct GNUNET_DBUS_InterfaceIterator *interfaces_back; + + struct GNUNET_DBUS_ObjectIterator *subobjects_front; + struct GNUNET_DBUS_ObjectIterator *subobjects_back; + + char *name; + void *data; + unsigned ref_count; +}; + +struct GNUNET_DBUS_Object * +GNUNET_DBUS_object_create ( + const char *name, + void *data) +{ + struct GNUNET_DBUS_Object *object = GNUNET_new (struct GNUNET_DBUS_Object); + object->name = GNUNET_strdup (name); + object->data = data; + object->interfaces_front = NULL; + object->interfaces_back = NULL; + object->subobjects_front = NULL; + object->subobjects_back = NULL; + object->ref_count = 1; + + return object; +}; + +void +GNUNET_DBUS_object_ref ( + struct GNUNET_DBUS_Object *object) +{ + object->ref_count++; +}; + +void +GNUNET_DBUS_object_unref ( + struct GNUNET_DBUS_Object *object) +{ + if (object->ref_count == 0) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unreference object with ref count 0\n"); + GNUNET_abort_ (); + }; + if (0 == --(object->ref_count)) + { + struct GNUNET_DBUS_InterfaceIterator *int_it = object->interfaces_front; + while (int_it) + { + struct GNUNET_DBUS_InterfaceIterator *next = int_it->next; + GNUNET_DBUS_interface_unref (int_it->interface); + GNUNET_free (int_it); + int_it = next; + }; + + struct GNUNET_DBUS_ObjectIterator *obj_it = object->subobjects_front; + while (obj_it) + { + struct GNUNET_DBUS_ObjectIterator *next = obj_it->next; + GNUNET_DBUS_object_unref (obj_it->object); + GNUNET_free (obj_it); + obj_it = next; + }; + + GNUNET_free (object->name); + GNUNET_free (object); + } +}; + +const char * +GNUNET_DBUS_object_get_name ( + const struct GNUNET_DBUS_Object *object) +{ + return object->name; +}; + +void +GNUNET_DBUS_object_add_interface ( + struct GNUNET_DBUS_Object *object, + struct GNUNET_DBUS_Interface *interface) +{ + struct GNUNET_DBUS_InterfaceIterator *int_it = GNUNET_new (struct GNUNET_DBUS_InterfaceIterator); + int_it->interface = interface; + GNUNET_DBUS_interface_ref (interface); + + GNUNET_CONTAINER_DLL_insert (object->interfaces_front, + object->interfaces_back, + int_it); +}; + +const struct GNUNET_DBUS_InterfaceIterator * +GNUNET_DBUS_object_iterate_interfaces ( + struct GNUNET_DBUS_Object *object) +{ + return object->interfaces_front; +}; + +void * +GNUNET_DBUS_object_get_data ( + struct GNUNET_DBUS_Object *object) +{ + return object->data; +}; + +struct GNUNET_DBUS_Object * +GNUNET_DBUS_object_create_uniquely_named_subobject ( + struct GNUNET_DBUS_Object *object, + void *data) +{ + while(true) + { + char name[9]; + uint32_t id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 0xffffffff); + GNUNET_snprintf (name, sizeof(name), "%08" PRIx32, id); + const struct GNUNET_DBUS_ObjectIterator *obj_it = GNUNET_DBUS_object_iterate_subobjects (object); + while (true) + { + if (! obj_it) + { + struct GNUNET_DBUS_Object *subobject = GNUNET_DBUS_object_create (name, data); + GNUNET_DBUS_object_add_subobject (object, subobject); + return subobject; + } + + const struct GNUNET_DBUS_Object *test_object = obj_it->object; + if (0 == strcmp (name, GNUNET_DBUS_object_get_name (test_object))) + break; + + obj_it = obj_it->next; + } + } +} + +void +GNUNET_DBUS_object_add_subobject ( + struct GNUNET_DBUS_Object *object, + struct GNUNET_DBUS_Object *subobject) +{ + struct GNUNET_DBUS_ObjectIterator *obj_it = GNUNET_new (struct GNUNET_DBUS_ObjectIterator); + obj_it->object = subobject; + GNUNET_DBUS_object_ref (subobject); + + GNUNET_CONTAINER_DLL_insert (object->subobjects_front, + object->subobjects_back, + obj_it); +}; + +void +GNUNET_DBUS_object_remove_subobject ( + struct GNUNET_DBUS_Object *object, + struct GNUNET_DBUS_ObjectIterator *subobject_it) +{ + GNUNET_DBUS_object_unref (subobject_it->object); + GNUNET_CONTAINER_DLL_remove (object->subobjects_front, + object->subobjects_back, + subobject_it); + GNUNET_free (subobject_it); +}; + +struct GNUNET_DBUS_ObjectIterator * +GNUNET_DBUS_object_iterate_subobjects ( + const struct GNUNET_DBUS_Object *object) +{ + return object->subobjects_front; +} + +const struct GNUNET_DBUS_ObjectIterator * +GNUNET_DBUS_object_find ( + const struct GNUNET_DBUS_ObjectIterator *object_it, + const char *name) +{ + while (object_it && strcmp (name, object_it->object->name)) + object_it = object_it->next; + + return object_it; +} + + diff --git a/src/lib/common/gnunet_dbus_lib_object_path.c b/src/lib/common/gnunet_dbus_lib_object_path.c new file mode 100644 index 0000000..72a155f --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_object_path.c @@ -0,0 +1,162 @@ +#include "config.h" + +#include +#include +#include + +#include "gnunet_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-object-path", __VA_ARGS__) + +struct GNUNET_DBUS_ObjectPath +{ + size_t objects_len; + struct GNUNET_DBUS_Object **objects; + size_t ref_count; +}; + +struct GNUNET_DBUS_ObjectPath * +GNUNET_DBUS_object_path_create ( + struct GNUNET_DBUS_Service *service, + ...) +{ + struct GNUNET_DBUS_ObjectPath *path = GNUNET_new (struct GNUNET_DBUS_ObjectPath); + path->objects_len = 1; + path->ref_count = 1; + + va_list vl; + va_start (vl, service); + while (va_arg (vl, struct GNUNET_DBUS_Object *)) + { + path->objects_len++; + } + va_end (vl); + + path->objects = GNUNET_malloc (sizeof (struct GNUNET_DBUS_Object *) * (path->objects_len + 1)); + va_start (vl, service); + size_t i; + struct GNUNET_DBUS_Object *elem = GNUNET_DBUS_service_get_root_object (service); + path->objects[0] = elem; + GNUNET_DBUS_object_ref (elem); + for (i = 1; i < path->objects_len; i++) + { + elem = va_arg (vl, struct GNUNET_DBUS_Object *); + path->objects[i] = elem; + GNUNET_DBUS_object_ref (elem); + } + path->objects[path->objects_len] = NULL; + va_end (vl); + + return path; +} + +void +GNUNET_DBUS_object_path_unref ( + struct GNUNET_DBUS_ObjectPath *path) +{ + if (path->ref_count == 0) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unreference object path with ref count 0\n"); + + /* This will probably segfault, but oh well */ + char *path_as_str = GNUNET_DBUS_object_path_to_string (path); + LOG (GNUNET_ERROR_TYPE_ERROR, " path == %s\n", path_as_str); + GNUNET_abort_ (); + }; + if (0 == --(path->ref_count)) + { + size_t i; + for (i = 0; i < path->objects_len; i++) + { + GNUNET_DBUS_object_unref (path->objects[i]); + } + + GNUNET_free (path->objects); + GNUNET_free (path); + } +} + +char * +GNUNET_DBUS_object_path_to_string ( + const struct GNUNET_DBUS_ObjectPath *path) +{ + char *data; + char *new_data; + + struct GNUNET_DBUS_Object **obj = path->objects; + + obj++; + if (NULL == *obj) + { + GNUNET_asprintf (&data, "/"); + return data; + } + + GNUNET_asprintf (&data, "%s", ""); + while (*obj) + { + GNUNET_asprintf (&new_data, "%s/%s", data, GNUNET_DBUS_object_get_name (*obj)); + GNUNET_free (data); data = new_data; + obj++; + } + + return data; +} + +#if 0 +struct GNUNET_DBUS_ObjectPath * +GNUNET_DBUS_object_path_create_from_string ( + const struct GNUNET_DBUS_Service *service, + const char *path_str) +{ + char **path_decomposed = NULL; + dbus_bool_t succ = dbus_message_get_path_decomposed (dbus_message, &path_decomposed); + if (! succ) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "dbus_message_get_path_decomposed failed.\n"); + GNUNET_abort_ (); + }; + + struct GNUNET_DBUS_ObjectPath *object_path = GNUNET_new (struct GNUNET_DBUS_ObjectPath); + object_path->objects_len = 0; + const char **path_element = path_decomposed; + while (*path_element) + { + object_path->objects_len++; + path_element++; + } + + object_path->objects = GNUNET_malloc (sizeof (struct GNUNET_DBUS_Object *) * object_path->objects_len); + + const struct GNUNET_DBUS_Object *object = GNUNET_DBUS_service_get_root_object (service); + struct GNUNET_DBUS_Object **pos = object_path->objects; + *pos++ = object; + path_element = path_decomposed; + while (*path_element) + { + const struct GNUNET_DBUS_ObjectIterator *start = GNUNET_DBUS_object_iterate_subobjects (object); + const struct GNUNET_DBUS_ObjectIterator *found = GNUNET_DBUS_object_find (start, *path_element); + if (! found) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, "Invalid object path \"%s\"\n", path_str); + return NULL; + }; + + object = found->object; + *pos++ = object; + path_element++; + } + + object_path->ref_count = 1; + + return object_path; +} +#endif + +struct GNUNET_DBUS_Object * +GNUNET_DBUS_object_path_get_destination ( + const struct GNUNET_DBUS_ObjectPath *path) +{ + return path->objects[path->objects_len - 1]; +} + diff --git a/src/lib/common/gnunet_dbus_lib_pop.c b/src/lib/common/gnunet_dbus_lib_pop.c new file mode 100644 index 0000000..dd945e4 --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_pop.c @@ -0,0 +1,733 @@ +#include "config.h" + +#include +#include +#include + +#include "gnunet_dbus_lib.h" +#include "gnunet_crypto_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-pop", __VA_ARGS__) + +static DBusMessage * +pop_basic ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + int expected_type, + void *value) +{ + int arg_type = dbus_message_iter_get_arg_type (iter); + if (expected_type != arg_type) + { + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Expected %s. Got %s", + arg_name, + GNUNET_DBUS_signature_typecode_to_string (expected_type), + GNUNET_DBUS_signature_typecode_to_string (arg_type)); + }; + dbus_message_iter_get_basic (iter, value); + dbus_message_iter_next (iter); + + return NULL; +}; + +DBusMessage * +GNUNET_DBUS_pop_byte ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + unsigned char *value) +{ + return pop_basic ( + message, + iter, + arg_name, + DBUS_TYPE_BYTE, + (void *)value); +}; + +DBusMessage * +GNUNET_DBUS_pop_boolean ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_bool_t *value) +{ + return pop_basic ( + message, + iter, + arg_name, + DBUS_TYPE_BOOLEAN, + (void *)value); +}; + +DBusMessage * +GNUNET_DBUS_pop_int16 ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_int16_t *value) +{ + return pop_basic ( + message, + iter, + arg_name, + DBUS_TYPE_INT16, + (void *)value); +}; + +DBusMessage * +GNUNET_DBUS_pop_uint16 ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_uint16_t *value) +{ + return pop_basic ( + message, + iter, + arg_name, + DBUS_TYPE_UINT16, + (void *)value); +}; + +DBusMessage * +GNUNET_DBUS_pop_int32 ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_int32_t *value) +{ + return pop_basic ( + message, + iter, + arg_name, + DBUS_TYPE_INT32, + (void *)value); +}; + +DBusMessage * +GNUNET_DBUS_pop_uint32 ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_uint32_t *value) +{ + return pop_basic ( + message, + iter, + arg_name, + DBUS_TYPE_UINT32, + (void *)value); +}; + +DBusMessage * +GNUNET_DBUS_pop_int64 ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_int64_t *value) +{ + return pop_basic ( + message, + iter, + arg_name, + DBUS_TYPE_INT64, + (void *)value); +}; + +DBusMessage * +GNUNET_DBUS_pop_uint64 ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_uint64_t *value) +{ + return pop_basic ( + message, + iter, + arg_name, + DBUS_TYPE_UINT64, + (void *)value); +}; + +DBusMessage * +GNUNET_DBUS_pop_double ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + double *value) +{ + return pop_basic ( + message, + iter, + arg_name, + DBUS_TYPE_DOUBLE, + (void *)value); +}; + +DBusMessage * +GNUNET_DBUS_pop_string ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const char **value) +{ + return pop_basic ( + message, + iter, + arg_name, + DBUS_TYPE_STRING, + (void *)value); +}; + +DBusMessage * +GNUNET_DBUS_pop_object_path ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const char **value) +{ + return pop_basic ( + message, + iter, + arg_name, + DBUS_TYPE_OBJECT_PATH, + (void *)value); +}; + +DBusMessage * +GNUNET_DBUS_pop_signature ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const char **value) +{ + return pop_basic ( + message, + iter, + arg_name, + DBUS_TYPE_SIGNATURE, + (void *)value); +}; + +DBusMessage * +GNUNET_DBUS_pop_unix_fd ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + int *value) +{ + return pop_basic ( + message, + iter, + arg_name, + DBUS_TYPE_UNIX_FD, + (void *)value); +}; + +static DBusMessage * +pop_enter_container ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + int type, + const char *arg_name) +{ + int arg_type = dbus_message_iter_get_arg_type (iter); + if (type != arg_type) + { + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Expected %s. Got %s", + arg_name, + GNUNET_DBUS_signature_typecode_to_string (type), + GNUNET_DBUS_signature_typecode_to_string (arg_type)); + } + dbus_message_iter_recurse (iter, iter_sub); + dbus_message_iter_next (iter); + return NULL; +}; + +DBusMessage * +GNUNET_DBUS_pop_enter_array ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + const char *arg_name, + size_t *len) +{ + if (len && dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY) + { + DBusMessageIter iter_calc_len; + dbus_message_iter_recurse (iter, &iter_calc_len); + *len = 0; + while (dbus_message_iter_get_arg_type (&iter_calc_len) != DBUS_TYPE_INVALID) + { + ++*len; + dbus_message_iter_next (&iter_calc_len); + }; + }; + return pop_enter_container (message, iter, iter_sub, DBUS_TYPE_ARRAY, arg_name); +} + +DBusMessage * +GNUNET_DBUS_pop_enter_struct ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + const char *arg_name) +{ + return pop_enter_container (message, iter, iter_sub, DBUS_TYPE_STRUCT, arg_name); +} + +DBusMessage * +GNUNET_DBUS_pop_enter_variant ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + const char *arg_name) +{ + return pop_enter_container (message, iter, iter_sub, DBUS_TYPE_VARIANT, arg_name); +} + +DBusMessage * +GNUNET_DBUS_pop_enter_dict_entry ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + const char *arg_name) +{ + return pop_enter_container (message, iter, iter_sub, DBUS_TYPE_DICT_ENTRY, arg_name); +} + +static DBusMessage * +pop_fixed_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + int expected_type, + void *value, + int *n_elements) +{ + int arg_type = dbus_message_iter_get_arg_type (iter); + if (DBUS_TYPE_ARRAY != arg_type) + { + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Expected array of %s. Got %s.", + arg_name, + GNUNET_DBUS_signature_typecode_to_string (expected_type), + GNUNET_DBUS_signature_typecode_to_string (arg_type)); + }; + + int element_type = dbus_message_iter_get_element_type (iter); + if (expected_type != element_type) + { + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Expected array of %s. Got array of %s", + arg_name, + GNUNET_DBUS_signature_typecode_to_string (expected_type), + GNUNET_DBUS_signature_typecode_to_string (element_type)); + }; + + DBusMessageIter iter_sub; + dbus_message_iter_recurse (iter, &iter_sub); + dbus_message_iter_get_fixed_array (&iter_sub, value, n_elements); + dbus_message_iter_next (iter); + + return NULL; +}; + +DBusMessage * +GNUNET_DBUS_pop_byte_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const unsigned char **value, + int *n_elements) +{ + return pop_fixed_array ( + message, + iter, + arg_name, + DBUS_TYPE_BYTE, + (void *)value, + n_elements); +}; + +DBusMessage * +GNUNET_DBUS_pop_boolean_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const dbus_bool_t **value, + int *n_elements) +{ + return pop_fixed_array ( + message, + iter, + arg_name, + DBUS_TYPE_BOOLEAN, + (void *)value, + n_elements); +}; + +DBusMessage * +GNUNET_DBUS_pop_int16_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const int16_t **value, + int *n_elements) +{ + return pop_fixed_array ( + message, + iter, + arg_name, + DBUS_TYPE_INT16, + (void *)value, + n_elements); +}; + +DBusMessage * +GNUNET_DBUS_pop_uint16_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const uint16_t **value, + int *n_elements) +{ + return pop_fixed_array ( + message, + iter, + arg_name, + DBUS_TYPE_UINT16, + (void *)value, + n_elements); +}; + +DBusMessage * +GNUNET_DBUS_pop_int32_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const int32_t **value, + int *n_elements) +{ + return pop_fixed_array ( + message, + iter, + arg_name, + DBUS_TYPE_INT32, + (void *)value, + n_elements); +}; + +DBusMessage * +GNUNET_DBUS_pop_uint32_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const uint32_t **value, + int *n_elements) +{ + return pop_fixed_array ( + message, + iter, + arg_name, + DBUS_TYPE_UINT32, + (void *)value, + n_elements); +}; + +DBusMessage * +GNUNET_DBUS_pop_int64_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const int64_t **value, + int *n_elements) +{ + return pop_fixed_array ( + message, + iter, + arg_name, + DBUS_TYPE_INT64, + (void *)value, + n_elements); +}; + +DBusMessage * +GNUNET_DBUS_pop_uint64_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const uint64_t **value, + int *n_elements) +{ + return pop_fixed_array ( + message, + iter, + arg_name, + DBUS_TYPE_UINT64, + (void *)value, + n_elements); +}; + +DBusMessage * +GNUNET_DBUS_pop_double_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const double **value, + int *n_elements) +{ + return pop_fixed_array ( + message, + iter, + arg_name, + DBUS_TYPE_DOUBLE, + (void *)value, + n_elements); +}; + +/* + * bitfield enums can be sent across the wire in either their integer form (as + * an array of bit flags) or as an array of strings. Sending an int is more + * efficient and is what libraries built around the DBus API would use, people + * using the DBus API directly may want to pass human-readable strings for + * convenience. + */ +DBusMessage * +GNUNET_DBUS_pop_bitfield ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + int *value, + const struct GNUNET_DBUS_StringEnumPair *fields) +{ + DBusMessageIter iter_sub; + DBusMessage *ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name); + if (ret) + return ret; + + int arg_type = dbus_message_iter_get_arg_type (&iter_sub); + switch (arg_type) + { + case DBUS_TYPE_ARRAY: { + int element_type = dbus_message_iter_get_element_type (&iter_sub); + if (DBUS_TYPE_STRING != element_type) + { + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Variant should contain a uint32 or an array of strings.", + arg_name); + }; + DBusMessageIter iter_sub_sub; + dbus_message_iter_recurse (&iter_sub, &iter_sub_sub); + *value = 0; + while (dbus_message_iter_get_arg_type (&iter_sub_sub) != DBUS_TYPE_INVALID) + { + char *option; + dbus_message_iter_get_basic (&iter_sub_sub, &option); + const struct GNUNET_DBUS_StringEnumPair *field; + for (field = fields; field->name; field++) + { + if (! strcmp (field->name, option)) + { + *value |= field->value; + break; + }; + }; + if (! field->name) + { + char *errmsg; + GNUNET_asprintf (&errmsg, "Bad argument for '%s'. Unrecognised option '%s'. Valid options are [", arg_name, option); + for (field = fields; field->name; field++) + { + char *errmsg_new; + GNUNET_asprintf (&errmsg_new, "%s'%s'%s", errmsg, field->name, (field + 1)->name ? ", " : "]."); + GNUNET_free (errmsg); + errmsg = errmsg_new; + }; + DBusMessage *ret = dbus_message_new_error ( + message, + DBUS_ERROR_INVALID_ARGS, + errmsg); + GNUNET_free (errmsg); + return ret; + }; + dbus_message_iter_next (&iter_sub_sub); + }; + GNUNET_DBUS_message_set_pretty (message, true); + return NULL; + }; + case DBUS_TYPE_UINT32: { + uint32_t flags; + dbus_message_iter_get_basic (&iter_sub, &flags); + *value = (int)flags; + return NULL; + }; + default: + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Variant contains a %s. Should contain a uint32 or array of strings.", + arg_name); + }; +}; + +DBusMessage * +GNUNET_DBUS_pop_enum ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + int *value, + const struct GNUNET_DBUS_StringEnumPair *names) +{ + DBusMessageIter iter_sub; + DBusMessage *ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name); + if (ret) + return ret; + + int arg_type = dbus_message_iter_get_arg_type (&iter_sub); + switch (arg_type) + { + case DBUS_TYPE_STRING: { + char *option; + dbus_message_iter_get_basic (&iter_sub, &option); + const struct GNUNET_DBUS_StringEnumPair *name; + for (name = names; name->name; name++) + { + if (! strcmp (name->name, option)) + { + *value = name->value; + return NULL; + }; + }; + char *errmsg; + GNUNET_asprintf (&errmsg, "Bad argument for '%s'. Unrecognised option '%s'. Valid options are [", arg_name, option); + for (name = names; name->name; name++) + { + char *errmsg_new; + GNUNET_asprintf (&errmsg_new, "%s'%s'%s", errmsg, name->name, (name + 1)->name ? ", " : "]."); + GNUNET_free (errmsg); + errmsg = errmsg_new; + }; + + DBusMessage *ret = dbus_message_new_error ( + message, + DBUS_ERROR_INVALID_ARGS, + errmsg); + GNUNET_free (errmsg); + return ret; + }; + case DBUS_TYPE_UINT32: { + uint32_t option; + dbus_message_iter_get_basic (&iter_sub, &option); + *value = (int)option; + return NULL; + }; + default: + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Variant contains a %s. Should contain a uint32 or strings.", + arg_name, + GNUNET_DBUS_signature_typecode_to_string (arg_type)); + }; +}; + +DBusMessage * +GNUNET_DBUS_pop_hashcode ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + struct GNUNET_HashCode *value) +{ + DBusMessageIter iter_sub; + DBusMessage *ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name); + if (ret) + return ret; + + int arg_type = dbus_message_iter_get_arg_type (&iter_sub); + switch (arg_type) + { + case DBUS_TYPE_STRING: { + char *encoded; + dbus_message_iter_get_basic (&iter_sub, &encoded); + int success = GNUNET_CRYPTO_hash_from_string2 (encoded, strlen (encoded), value); + if (GNUNET_OK != success) + { + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Malformed hash string. GNUNET_CRYPTO_hash_from_string2 returned %d", + arg_name, + success); + }; + GNUNET_DBUS_message_set_pretty (message, true); + return NULL; + } + case DBUS_TYPE_ARRAY: { + DBusMessageIter iter_sub_sub; + unsigned char *data; + int n_elements; + int element_type = dbus_message_iter_get_element_type (&iter_sub); + if (DBUS_TYPE_BYTE != element_type) + { + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Variant contains an array of %s. Should contain a base32 encoded string or array of 64 bytes.", + arg_name, + GNUNET_DBUS_signature_typecode_to_string (element_type)); + }; + dbus_message_iter_recurse (&iter_sub, &iter_sub_sub); + dbus_message_iter_get_fixed_array (&iter_sub_sub, &data, &n_elements); + if (sizeof (value->bits) != n_elements) + { + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Hash code consists of 64 bytes (512 bits). Array contains %d bytes.", + arg_name, + n_elements); + }; + memcpy (value->bits, data, n_elements); + return NULL; + }; + default: + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Variant contains a %s. Should contain a base32 encoded string or array of 64 bytes.", + arg_name, + GNUNET_DBUS_signature_typecode_to_string (arg_type)); + }; +} + +DBusMessage * +GNUNET_DBUS_pop_peer_identity ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + struct GNUNET_PeerIdentity *value) +{ + DBusMessageIter iter_sub; + DBusMessage *ret = GNUNET_DBUS_pop_enter_struct (message, iter, &iter_sub, arg_name); + if (ret) + return ret; + + return GNUNET_CRYPTO_DBUS_pop_eddsa_public_key (message, &iter_sub, "public_key", &value->public_key); +} + diff --git a/src/lib/common/gnunet_dbus_lib_push.c b/src/lib/common/gnunet_dbus_lib_push.c new file mode 100644 index 0000000..1cb455e --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_push.c @@ -0,0 +1,510 @@ +#include "config.h" + +#include + +#include +#include +#include + +#include "gnunet_dbus_lib.h" +#include "gnunet_crypto_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-push", __VA_ARGS__) + +static void +push_basic ( + DBusMessage *message, + DBusMessageIter *iter, + int type, + const void *value) +{ + (void)message; + bool success = dbus_message_iter_append_basic (iter, type, value); + if (! success) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Ran out of memory in dbus_message_iter_append_basic\n"); + GNUNET_abort_ (); + } +} + +void +GNUNET_DBUS_push_byte ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing byte\n"); + push_basic (message, iter, DBUS_TYPE_BYTE, value); +} + +void +GNUNET_DBUS_push_boolean ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_bool_t *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing boolean\n"); + push_basic (message, iter, DBUS_TYPE_BOOLEAN, value); +} + +void +GNUNET_DBUS_push_int16 ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_int16_t *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing int16\n"); + push_basic (message, iter, DBUS_TYPE_INT16, value); +} + +void +GNUNET_DBUS_push_uint16 ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_uint16_t *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing uint16\n"); + push_basic (message, iter, DBUS_TYPE_UINT16, value); +} + +void +GNUNET_DBUS_push_int32 ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_int32_t *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing int32\n"); + push_basic (message, iter, DBUS_TYPE_INT32, value); +} + +void +GNUNET_DBUS_push_uint32 ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_uint32_t *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing uint32\n"); + push_basic (message, iter, DBUS_TYPE_UINT32, value); +} + +void +GNUNET_DBUS_push_int64 ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_int64_t *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing int64\n"); + push_basic (message, iter, DBUS_TYPE_INT64, value); +} + +void +GNUNET_DBUS_push_uint64 ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_uint64_t *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing uint64\n"); + push_basic (message, iter, DBUS_TYPE_UINT64, value); +} + +void +GNUNET_DBUS_push_double ( + DBusMessage *message, + DBusMessageIter *iter, + const double *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing double\n"); + push_basic (message, iter, DBUS_TYPE_DOUBLE, value); +} + +void +GNUNET_DBUS_push_string ( + DBusMessage *message, + DBusMessageIter *iter, + const char *const *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing string\n"); + push_basic (message, iter, DBUS_TYPE_STRING, value); +} + +void +GNUNET_DBUS_push_object_path ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_DBUS_ObjectPath *path) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing object path\n"); + char *path_as_string = GNUNET_DBUS_object_path_to_string (path); + push_basic (message, iter, DBUS_TYPE_OBJECT_PATH, &path_as_string); + GNUNET_free (path_as_string); +} + +void +GNUNET_DBUS_push_signature ( + DBusMessage *message, + DBusMessageIter *iter, + const char *const *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing signature\n"); + push_basic (message, iter, DBUS_TYPE_SIGNATURE, value); +} + +void +GNUNET_DBUS_push_unix_fd ( + DBusMessage *message, + DBusMessageIter *iter, + const int *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing unix fd\n"); + push_basic (message, iter, DBUS_TYPE_UNIX_FD, value); +} + +static void +push_open_container ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + int type, + const char *signature) +{ + (void)message; + bool success = dbus_message_iter_open_container (iter, type, signature, iter_sub); + if (! success) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Ran out of memory in dbus_message_iter_open_container\n"); + GNUNET_abort_ (); + } +} + +void +GNUNET_DBUS_push_open_array ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + const char *signature) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Opening array\n"); + push_open_container (message, iter, iter_sub, DBUS_TYPE_ARRAY, signature); +} + +void +GNUNET_DBUS_push_open_struct ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Opening struct\n"); + push_open_container (message, iter, iter_sub, DBUS_TYPE_STRUCT, NULL); +} + +void +GNUNET_DBUS_push_open_variant ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + const char *signature) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Opening variant\n"); + push_open_container (message, iter, iter_sub, DBUS_TYPE_VARIANT, signature); +} + +void +GNUNET_DBUS_push_open_dict_entry ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Opening dict entry\n"); + push_open_container (message, iter, iter_sub, DBUS_TYPE_DICT_ENTRY, NULL); +} + +static void +push_close_container ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub) +{ + (void)message; + bool success = dbus_message_iter_close_container (iter, iter_sub); + if (! success) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Ran out of memory in dbus_message_iter_close_container\n"); + GNUNET_abort_ (); + } +} + +void +GNUNET_DBUS_push_close_array ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing array\n"); + push_close_container (message, iter, iter_sub); +} + +void +GNUNET_DBUS_push_close_struct ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing struct\n"); + push_close_container (message, iter, iter_sub); +} + +void +GNUNET_DBUS_push_close_variant ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing variant\n"); + push_close_container (message, iter, iter_sub); +} + +void +GNUNET_DBUS_push_close_dict_entry ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing dict entry\n"); + push_close_container (message, iter, iter_sub); +} + +static void +push_fixed_array ( + DBusMessage *message, + DBusMessageIter *iter, + int type, + const void *value, + size_t value_count) +{ + char sig[2] = { (char)type, '\0' }; + DBusMessageIter iter_sub; + + GNUNET_DBUS_push_open_array (message, iter, &iter_sub, sig); + bool success = dbus_message_iter_append_fixed_array (&iter_sub, type, value, (int)value_count); + if (! success) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Ran out of memory in dbus_message_iter_push_fixed_array\n"); + GNUNET_abort_ (); + } + GNUNET_DBUS_push_close_array (message, iter, &iter_sub); +} + +void +GNUNET_DBUS_push_byte_array ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value, + size_t value_count) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing byte array\n"); + push_fixed_array (message, iter, DBUS_TYPE_BYTE, &value, value_count); +} + +void +GNUNET_DBUS_push_boolean_array ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_bool_t *value, + size_t value_count) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing boolean array\n"); + push_fixed_array (message, iter, DBUS_TYPE_BOOLEAN, &value, value_count); +} + +void +GNUNET_DBUS_push_int16_array ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_int16_t *value, + size_t value_count) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing int16 array\n"); + push_fixed_array (message, iter, DBUS_TYPE_INT16, &value, value_count); +} + +void +GNUNET_DBUS_push_uint16_array ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_uint16_t *value, + size_t value_count) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing uint16 array\n"); + push_fixed_array (message, iter, DBUS_TYPE_UINT16, &value, value_count); +} + +void +GNUNET_DBUS_push_int32_array ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value, + size_t value_count) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing int32 array\n"); + push_fixed_array (message, iter, DBUS_TYPE_INT32, &value, value_count); +} + +void +GNUNET_DBUS_push_uint32_array ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value, + size_t value_count) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing uint32 array\n"); + push_fixed_array (message, iter, DBUS_TYPE_UINT32, &value, value_count); +} + +void +GNUNET_DBUS_push_int64_array ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value, + size_t value_count) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing int64 array\n"); + push_fixed_array (message, iter, DBUS_TYPE_INT64, &value, value_count); +} + +void +GNUNET_DBUS_push_uint64_array ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value, + size_t value_count) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing uint64 array\n"); + push_fixed_array (message, iter, DBUS_TYPE_UINT64, &value, value_count); +} + +void +GNUNET_DBUS_push_double_array ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value, + size_t value_count) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing double array\n"); + push_fixed_array (message, iter, DBUS_TYPE_DOUBLE, &value, value_count); +} + +void +GNUNET_DBUS_push_bitfield ( + DBusMessage *message, + DBusMessageIter *iter, + const int *value, + const struct GNUNET_DBUS_StringEnumPair *fields) +{ + DBusMessageIter iter_sub; + if (GNUNET_DBUS_message_get_pretty (message)) + { + DBusMessageIter iter_sub_sub; + + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_ARRAY (GNUNET_DBUS_SIGNATURE_STRING)); + GNUNET_DBUS_push_open_array (message, &iter_sub, &iter_sub_sub, GNUNET_DBUS_SIGNATURE_STRING); + const struct GNUNET_DBUS_StringEnumPair *field; + for (field = fields; field->name; field++) + { + if ((*value & field->value) == field->value) + GNUNET_DBUS_push_string (message, &iter_sub_sub, &field->name); + }; + GNUNET_DBUS_push_close_array (message, &iter_sub, &iter_sub_sub); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + } + else + { + uint32_t push_value = (uint32_t)*value; + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_UINT32); + GNUNET_DBUS_push_uint32 (message, &iter_sub, &push_value); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + }; +}; + +void +GNUNET_DBUS_push_enum ( + DBusMessage *message, + DBusMessageIter *iter, + const int *value, + const struct GNUNET_DBUS_StringEnumPair *names) +{ + DBusMessageIter iter_sub; + if (GNUNET_DBUS_message_get_pretty (message)) + { + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_STRING); + const struct GNUNET_DBUS_StringEnumPair *name; + for (name = names; name->name; name++) + { + if (name->value == *value) + { + GNUNET_DBUS_push_string (message, &iter_sub, &name->name); + break; + } + } + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + } + else + { + uint32_t push_value = (uint32_t)*value; + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_UINT32); + GNUNET_DBUS_push_uint32 (message, &iter_sub, &push_value); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + }; +}; + +void +GNUNET_DBUS_push_data ( + DBusMessage *message, + DBusMessageIter *iter, + const void *value, + size_t value_size) +{ + DBusMessageIter iter_sub; + if (GNUNET_DBUS_message_get_pretty (message)) + { + size_t string_size = (value_size * 8 + 4) / 5 + 1; + char *string = GNUNET_malloc (string_size); + char *end = GNUNET_STRINGS_data_to_string (value, value_size, string, string_size); + *end = '\0'; + const char *string_const = string; + + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_STRING); + GNUNET_DBUS_push_string (message, &iter_sub, &string_const); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + GNUNET_free (string); + } + else + { + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_ARRAY (GNUNET_DBUS_SIGNATURE_BYTE)); + GNUNET_DBUS_push_byte_array (message, &iter_sub, value, value_size); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + }; +} + +void +GNUNET_DBUS_push_hashcode ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_HashCode *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing GNUNET_HashCode\n"); + GNUNET_DBUS_push_data (message, iter, value->bits, sizeof (value->bits)); +} + +void +GNUNET_DBUS_push_peer_identity ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_PeerIdentity *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing GNUNET_PeerIdentity\n"); + DBusMessageIter iter_sub; + GNUNET_DBUS_push_open_struct (message, iter, &iter_sub); + GNUNET_CRYPTO_DBUS_push_eddsa_public_key (message, &iter_sub, &value->public_key); + GNUNET_DBUS_push_close_struct (message, iter, &iter_sub); +} + diff --git a/src/lib/common/gnunet_dbus_lib_service.c b/src/lib/common/gnunet_dbus_lib_service.c new file mode 100644 index 0000000..2151613 --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_service.c @@ -0,0 +1,686 @@ +#include "config.h" + +#include +#include + +#include +#include +#include +#include + +#include "gnunet_dbus_lib.h" + +#include "watch.h" +#include "timeout.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-service", __VA_ARGS__) + +struct GNUNET_DBUS_Service +{ +#if 0 + struct GNUNET_DBUS_ObjectIterator *objects_front; + struct GNUNET_DBUS_ObjectIterator *objects_back; +#else + struct GNUNET_DBUS_Object *root_object; +#endif + + DBusConnection *dbus_connection; + const struct GNUNET_CONFIGURATION_Handle *cfg; + + char *gnunet_name; + char *well_known_name; + + /* + * Linked list of watches + */ + struct WatchIter *watches_front; + struct WatchIter *watches_back; + + /* + * Linked list of timeouts + */ + struct TimeoutIter *timeouts_front; + struct TimeoutIter *timeouts_back; + + /* + * Linked list of clients + */ + struct GNUNET_DBUS_ClientIterator *clients_front; + struct GNUNET_DBUS_ClientIterator *clients_back; + + GNUNET_DBUS_ClientConnectsHandler client_connects; + GNUNET_DBUS_ClientDisconnectsHandler client_disconnects; + + unsigned ref_count; +}; + +static dbus_int32_t service_slot_id () +{ + static dbus_int32_t id = -1; + if (-1 == id) + { + dbus_bool_t succ = dbus_connection_allocate_data_slot (&id); + if (! succ || -1 == id) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "dbus_connection_allocate_data_slot failed. id == %lld\n", (long long)id); + GNUNET_abort_ (); + }; + }; + + return id; +}; + +/* + * Called by DBus when it has a new watch that it wants us to watch. + * + * @param watch The DBus watch, created and passed to us by DBus. + * @param data the GNUNET_DBUS_Service we passed to dbus_connection_set_watch_functions + * @return true on success, false on an unrecoverable error. + */ +static dbus_bool_t +watch_add ( + DBusWatch *watch, + void *data) +{ + struct GNUNET_DBUS_Service *service = (struct GNUNET_DBUS_Service *)data; + struct Watch *w = watch_create (watch); + + if (dbus_watch_get_enabled (watch)) + watch_schedule (w); + + struct WatchIter *wi = GNUNET_new (struct WatchIter); + wi->w = w; + GNUNET_CONTAINER_DLL_insert (service->watches_front, + service->watches_back, + wi); + return true; +}; + +/* + * Called by DBus when it want to permanently disable and remove a watch + * + * @param watch The DBus watch, passed to us by DBus. + * @param data the GNUNET_DBUS_Service we passed to dbus_connection_set_watch_functions + */ +static void +watch_remove ( + DBusWatch *watch, + void *data) +{ + struct GNUNET_DBUS_Service *service = (struct GNUNET_DBUS_Service *)data; + struct WatchIter *wi = watch_find (service->watches_front, watch); + + if (NULL == wi) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Asked to remove watch that has not been added\n"); + GNUNET_abort_ (); + }; + + struct Watch *w = wi->w; + watch_unschedule (w); + GNUNET_CONTAINER_DLL_remove (service->watches_front, + service->watches_back, + wi); + watch_unref (w); + GNUNET_free (wi); +}; + +/* + * Called by DBus when it wants to enable or disable a watch. + * Schedules or unschedules the scheduler to monitor this watch as appropriate. + * + * @param watch The DBus watch, passed to us by DBus. + * @param data the GNUNET_DBUS_Service we passed to dbus_connection_set_watch_functions + */ +static void +watch_toggle ( + DBusWatch *watch, + void *data) +{ + struct GNUNET_DBUS_Service *service = (struct GNUNET_DBUS_Service *)data; + struct WatchIter *wi = watch_find (service->watches_front, watch); + + if (NULL == wi) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Asked to toggle watch that has not been added\n"); + GNUNET_abort_ (); + }; + + struct Watch *w = wi->w; + if (dbus_watch_get_enabled (watch)) + watch_unschedule (w); + else + watch_schedule (w); +}; + +static dbus_bool_t +timeout_add ( + DBusTimeout *timeout, + void *data) +{ + struct GNUNET_DBUS_Service *service = (struct GNUNET_DBUS_Service *)data; + struct Timeout *t = timeout_create (timeout); + + if (dbus_timeout_get_enabled (timeout)) + timeout_schedule (t); + + struct TimeoutIter *ti = GNUNET_new (struct TimeoutIter); + ti->t = t; + GNUNET_CONTAINER_DLL_insert (service->timeouts_front, + service->timeouts_back, + ti); + return true; +}; + +static void +timeout_remove ( + DBusTimeout *timeout, + void *data) +{ + struct GNUNET_DBUS_Service *service = (struct GNUNET_DBUS_Service *)data; + struct TimeoutIter *ti = timeout_find (service->timeouts_front, timeout); + + if (NULL == ti) + { + LOG (GNUNET_ERROR_TYPE_WARNING, "Asked to remove timeout that has not been added\n"); + return; + }; + + struct Timeout *t = ti->t; + timeout_unschedule (t); + GNUNET_CONTAINER_DLL_remove (service->timeouts_front, + service->timeouts_back, + ti); + timeout_unref (t); + GNUNET_free (ti); +}; + +static void +timeout_toggle ( + DBusTimeout *timeout, + void *data) +{ + struct GNUNET_DBUS_Service *service = (struct GNUNET_DBUS_Service *)data; + struct TimeoutIter *ti = timeout_find (service->timeouts_front, timeout); + + if (NULL == ti) + { + LOG (GNUNET_ERROR_TYPE_WARNING, "asked to toggle timeout that has not been added\n"); + return; + }; + + struct Timeout *t = ti->t; + if (dbus_timeout_get_enabled (timeout)) + timeout_unschedule (t); + else + timeout_schedule (t); +}; + + +static void +dispatch ( + void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc); + + +static void +handle_dispatch ( + DBusConnection *dbus_connection, + DBusDispatchStatus status) +{ + switch (status) + { + case DBUS_DISPATCH_DATA_REMAINS: + GNUNET_SCHEDULER_add_now (dispatch, + dbus_connection); + break; + case DBUS_DISPATCH_COMPLETE: + break; + case DBUS_DISPATCH_NEED_MEMORY: + LOG (GNUNET_ERROR_TYPE_ERROR, "Out of memory!\n"); + GNUNET_abort_ (); + break; + default: + LOG (GNUNET_ERROR_TYPE_ERROR, "Unrecognized dispatch status\n"); + break; + }; +}; + + +static void +dispatch ( + void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct DBusConnection *dbus_connection = (struct DBusConnection *)cls; + dbus_connection_dispatch (dbus_connection); + handle_dispatch (dbus_connection, dbus_connection_get_dispatch_status (dbus_connection)); +}; + + +static void +dispatch_status_changed ( + DBusConnection *dbus_connection, + DBusDispatchStatus new_status, + void *data) +{ + (void)data; + handle_dispatch (dbus_connection, new_status); +}; + +/* + * Called whenever a message arrives from DBus + * + * @param conn The connection it arrived on. + * @param message The message + * @param cls The closure passed to dbus_connection_try_register_object_path + * in our case this is the GNUNET_DBUS_Object that the message was + * sent to. + * + * @return DBUS_HANDLER_RESULT_NEED_MEMORY if anything failed due to lack of + * memory. + * DBUS_HANDLER_RESULT_HANDLED if any method returned a reply. + * DBUS_HANDLER_RESULT_NOT_YET_HANDLED otherwise. + */ +DBusHandlerResult +handle_object_message ( + DBusConnection *connection, + DBusMessage *dbus_message, + void *cls) +{ + //struct GNUNET_DBUS_Object *object = (struct GNUNET_DBUS_Object *)cls; + (void)cls; + + const char *object_path = dbus_message_get_path (dbus_message); + const char *type_string = dbus_message_type_to_string (dbus_message_get_type (dbus_message)); + const char *interface_name = dbus_message_get_interface (dbus_message); + const char *member_name = dbus_message_get_member (dbus_message); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Recieved DBus message for %s\n", object_path); + LOG (GNUNET_ERROR_TYPE_DEBUG, " type == %s\n", type_string ? type_string : "(none)"); + LOG (GNUNET_ERROR_TYPE_DEBUG, " interface == %s\n", interface_name ? interface_name : "(none)"); + LOG (GNUNET_ERROR_TYPE_DEBUG, " member == %s\n", member_name ? member_name : "(none)"); + + struct GNUNET_DBUS_Service *service = dbus_connection_get_data (connection, service_slot_id ()); + + const char *unique_name = dbus_message_get_sender (dbus_message); + struct GNUNET_DBUS_ClientIterator *client_it = service->clients_front; + struct GNUNET_DBUS_Client *client = NULL; + for (; client_it; client_it = client_it->next) + { + client = client_it->client; + const char *this_unique_name = GNUNET_DBUS_client_get_unique_name (client); + if (! strcmp (unique_name, this_unique_name)) + break; + }; + if (! client_it) + { + client = GNUNET_DBUS_client_create (unique_name); + client_it = GNUNET_new (struct GNUNET_DBUS_ClientIterator); + client_it->client = client; + GNUNET_CONTAINER_DLL_insert (service->clients_front, + service->clients_back, + client_it); + if (service->client_connects) + service->client_connects (service, client); + /* + + ** TODO ** + + detect when a client is no longer on the bus and + destroy the client object. At the moment this will slowly + leak memory as clients come and go. + + */ + }; + + /* + * TODO: + * + * The code below could be more efficient. Avoid the allocation with + * dbus_message_get_path_decomposed and use tries to do + * object/method/interface lookups. + */ + char **path_decomposed = NULL; + dbus_bool_t succ = dbus_message_get_path_decomposed (dbus_message, &path_decomposed); + if (! succ) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Ran out of memory in dbus_message_get_path_decomposed.\n"); + GNUNET_abort_ (); + }; + + struct GNUNET_DBUS_Object *object = service->root_object; + char **path_element = path_decomposed; + while (*path_element) + { + const struct GNUNET_DBUS_ObjectIterator *start = GNUNET_DBUS_object_iterate_subobjects (object); + const struct GNUNET_DBUS_ObjectIterator *found = GNUNET_DBUS_object_find (start, *path_element); + if (! found) + { + const char *object_name = GNUNET_DBUS_object_get_name (object); + LOG (GNUNET_ERROR_TYPE_DEBUG, "No such object \"%s\" under \"%s\"\n", *path_element, object_name[0] ? object_name : "(root object)"); + dbus_free_string_array (path_decomposed); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + }; + + object = found->object; + path_element++; + }; + dbus_free_string_array (path_decomposed); + + if (! member_name) + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + if (! interface_name) + interface_name = service->well_known_name; + + int dbus_message_type = dbus_message_get_type (dbus_message); + if (dbus_message_type != DBUS_MESSAGE_TYPE_METHOD_CALL) + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + const struct GNUNET_DBUS_InterfaceIterator *int_it = \ + GNUNET_DBUS_interface_find ( \ + GNUNET_DBUS_object_iterate_interfaces (object), + interface_name); + if (! int_it) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, "No such interface: %s\n", interface_name); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + }; + struct GNUNET_DBUS_Interface *interface = int_it->interface; + + const struct GNUNET_DBUS_MethodIterator *meth_it = \ + GNUNET_DBUS_method_find ( \ + GNUNET_DBUS_interface_iterate_methods (interface), + member_name); + if (! meth_it) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, "No such method: %s\n", member_name); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + }; + struct GNUNET_DBUS_Method *method = meth_it->method; + + LOG (GNUNET_ERROR_TYPE_DEBUG, "Found method.\n"); + + struct GNUNET_DBUS_MethodContext *mc = GNUNET_DBUS_method_context_create ( + client, + service, + object, + interface, + method, + dbus_message + ); + GNUNET_DBUS_method_call (method, mc); + GNUNET_DBUS_method_context_unref (mc); + return DBUS_HANDLER_RESULT_HANDLED; +}; + +struct GNUNET_DBUS_Service * +GNUNET_DBUS_service_create ( + const struct GNUNET_CONFIGURATION_Handle *cfg, + const char *name) +{ + struct GNUNET_DBUS_Service *service = GNUNET_new (struct GNUNET_DBUS_Service); + service->cfg = cfg; + service->gnunet_name = GNUNET_strdup (name); + service->watches_front = NULL; + service->watches_back = NULL; + service->timeouts_front = NULL; + service->timeouts_back = NULL; + service->client_connects = NULL; + service->client_disconnects = NULL; + service->ref_count = 1; + + DBusError err; + dbus_error_init(&err); + + service->root_object = GNUNET_DBUS_object_create ("", NULL); + if (! service->root_object) + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create root object for service.\n"); + else + { + service->dbus_connection = dbus_bus_get_private (DBUS_BUS_SYSTEM, &err); + if (! service->dbus_connection) + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dbus system bus (%s)\n", dbus_error_is_set (&err) ? err.message : "dbus_bus_get_private returned NULL"); + else + { + dbus_connection_set_exit_on_disconnect (service->dbus_connection, + false); + dbus_bool_t succ = dbus_connection_set_data (service->dbus_connection, + service_slot_id (), + service, + NULL); + if (! succ) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "dbus_connection_set_data returned false. Out of memory.\n"); + GNUNET_abort_ (); + }; + + succ = dbus_connection_set_watch_functions (service->dbus_connection, watch_add, watch_remove, watch_toggle, service, NULL); + if(! succ) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "dbus_connection_set_watch_functions returned false. Out of memory.\n"); + GNUNET_abort_ (); + }; + + dbus_connection_set_dispatch_status_function (service->dbus_connection, dispatch_status_changed, service, NULL); + handle_dispatch (service->dbus_connection, dbus_connection_get_dispatch_status (service->dbus_connection)); + + succ = dbus_connection_set_timeout_functions (service->dbus_connection, timeout_add, timeout_remove, timeout_toggle, service, NULL); + if (! succ) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "dbus_connection_set_timeout_functions returned false. Out of memory.\n"); + GNUNET_abort_ (); + }; + + DBusObjectPathVTable vtable; + vtable.message_function = handle_object_message; + vtable.unregister_function = NULL; + succ = dbus_connection_try_register_fallback (service->dbus_connection, "/", &vtable, service, &err); + if (dbus_error_is_set (&err) || ! succ) + LOG (GNUNET_ERROR_TYPE_ERROR, "dbus_connection_try_register_fallback failed when registering root object.\n"); + else + { + service->well_known_name = NULL; + GNUNET_asprintf (&service->well_known_name, "gnu.gnunet.%s", name); + int request_result = dbus_bus_request_name (service->dbus_connection, service->well_known_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, &err); + if (dbus_error_is_set (&err)) + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to request the bus name (%s)\n", err.message); + else + { + switch(request_result) + { + case DBUS_REQUEST_NAME_REPLY_EXISTS: + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to request the bus name \"%s\"; name is already taken.\n", service->well_known_name); + break; + default: + LOG (GNUNET_ERROR_TYPE_ERROR, "Unable to interpret result of dbus_request_name (%d)\n", request_result); + break; + case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER: + + /* Success! */ + return service; + + } + } + dbus_connection_unregister_object_path (service->dbus_connection, "/"); + } + dbus_connection_close (service->dbus_connection); + dbus_connection_unref (service->dbus_connection); + } + GNUNET_DBUS_object_unref (service->root_object); + } + GNUNET_free (service->well_known_name); + GNUNET_free (service->gnunet_name); + GNUNET_free (service); + LOG (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DBUS_service create failed.\n"); + return NULL; +}; + +void +GNUNET_DBUS_service_ref ( + struct GNUNET_DBUS_Service *service) +{ + service->ref_count++; +} + +void +GNUNET_DBUS_service_unref ( + struct GNUNET_DBUS_Service *service) +{ + if (service->ref_count == 0) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unref service with ref count 0\n"); + LOG (GNUNET_ERROR_TYPE_ERROR, " well_known_name == %s\n", service->well_known_name); + GNUNET_abort_ (); + } + + if (0 == --(service->ref_count)) + { + GNUNET_DBUS_object_unref (service->root_object); + + GNUNET_free (service->well_known_name); + GNUNET_free (service->gnunet_name); + + struct WatchIter *wi = service->watches_front; + while (wi) + { + struct WatchIter *next = wi->next; + watch_unref (wi->w); + GNUNET_free (wi); + wi = next; + } + + struct TimeoutIter *ti = service->timeouts_front; + while (ti) + { + struct TimeoutIter *next = ti->next; + timeout_unref (ti->t); + GNUNET_free (ti); + ti = next; + } + + struct GNUNET_DBUS_ClientIterator *ci = service->clients_front; + while (ci) + { + struct GNUNET_DBUS_ClientIterator *next = ci->next; + GNUNET_DBUS_client_unref (ci->client); + GNUNET_free (ci); + ci = next; + } + + GNUNET_free (service); + } +}; + +const struct GNUNET_CONFIGURATION_Handle * +GNUNET_DBUS_service_get_config ( + struct GNUNET_DBUS_Service *service) +{ + return service->cfg; +}; + +#if 0 +int +GNUNET_DBUS_service_add_object ( + struct GNUNET_DBUS_Service *service, + struct GNUNET_DBUS_Object *object) +{ + DBusError err; + dbus_error_init(&err); + + const char *path = GNUNET_DBUS_object_get_path (object); + + DBusObjectPathVTable vtable; + vtable.message_function = handle_object_message; + vtable.unregister_function = NULL; + dbus_bool_t succ = dbus_connection_try_register_object_path ( + service->dbus_connection, + path, + &vtable, + object, + &err); + if (dbus_error_is_set (&err)) + { + LOG ( + GNUNET_ERROR_TYPE_ERROR, + "dbus_connection_try_register failed to register path \"%s\": %s\n", + path, + err.message); + return GNUNET_SYSERR; + }; + if (! succ) + { + LOG ( + GNUNET_ERROR_TYPE_ERROR, + "dbus_connection_try_register returned false when registering path \"%s\"\n", + path); + return GNUNET_SYSERR; + }; + + struct GNUNET_DBUS_ObjectIterator *object_it = GNUNET_new (struct GNUNET_DBUS_ObjectIterator); + object_it->object = object; + GNUNET_DBUS_object_ref (object); + GNUNET_CONTAINER_DLL_insert (service->objects_front, + service->objects_back, + object_it); + + return GNUNET_OK; +}; + +void +GNUNET_DBUS_service_remove_object ( + struct GNUNET_DBUS_Service *service, + struct GNUNET_DBUS_Object *object) +{ + struct GNUNET_DBUS_ObjectIterator *obj_it; + for (obj_it = service->objects_front; obj_it; obj_it = obj_it->next) + { + if (obj_it->object == object) + break; + }; + + if (! obj_it) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to remove object that has not been added.\n"); + GNUNET_abort_ (); + }; + + GNUNET_CONTAINER_DLL_remove ( + service->objects_front, + service->objects_back, + obj_it); + + GNUNET_free (obj_it); + GNUNET_DBUS_object_unref (object); +} +#endif + +void +GNUNET_DBUS_service_send ( + struct GNUNET_DBUS_Service *service, + DBusMessage *dbus_message) +{ + dbus_bool_t succ = dbus_connection_send (service->dbus_connection, dbus_message, NULL); + if (! succ) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "dbus_connection_send failed. Out of memory.\n"); + GNUNET_abort_ (); + }; +}; + +void +GNUNET_DBUS_service_set_client_handlers ( + struct GNUNET_DBUS_Service *service, + GNUNET_DBUS_ClientConnectsHandler client_connects, + GNUNET_DBUS_ClientDisconnectsHandler client_disconnects) +{ + service->client_connects = client_connects; + service->client_disconnects = client_disconnects; +}; + +struct GNUNET_DBUS_Object * +GNUNET_DBUS_service_get_root_object ( + struct GNUNET_DBUS_Service *service) +{ + return service->root_object; +} diff --git a/src/lib/common/gnunet_dbus_lib_signal.c b/src/lib/common/gnunet_dbus_lib_signal.c new file mode 100644 index 0000000..0597200 --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_signal.c @@ -0,0 +1,126 @@ +#include "config.h" + +#include +#include +#include + +#include "gnunet_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-signal", __VA_ARGS__) + +struct GNUNET_DBUS_Signal +{ + struct GNUNET_DBUS_ArgIterator *args_front; + struct GNUNET_DBUS_ArgIterator *args_back; + + struct GNUNET_DBUS_Interface *owner; + + char *name; + + unsigned ref_count; +}; + +struct GNUNET_DBUS_Signal * +GNUNET_DBUS_signal_create ( + const char *name) +{ + struct GNUNET_DBUS_Signal *ret = GNUNET_new (struct GNUNET_DBUS_Signal); + ret->args_front = NULL; + ret->args_back = NULL; + ret->owner = NULL; + ret->name = strdup (name); + ret->ref_count = 1; + + return ret; +} + +void +GNUNET_DBUS_signal_ref ( + struct GNUNET_DBUS_Signal *signal) +{ + signal->ref_count++; +} + +void +GNUNET_DBUS_signal_unref ( + struct GNUNET_DBUS_Signal *signal) +{ + if (signal->ref_count == 0) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unreference signal with ref count 0.\n"); + GNUNET_abort_ (); + } + if (0 == --(signal->ref_count)) + { + struct GNUNET_DBUS_ArgIterator *arg_it = signal->args_front; + while (arg_it) + { + struct GNUNET_DBUS_ArgIterator *next = arg_it->next; + GNUNET_DBUS_arg_unref (arg_it->arg); + GNUNET_free (arg_it); + arg_it = next; + } + + GNUNET_free (signal->name); + GNUNET_free (signal); + } +}; + +void +GNUNET_DBUS_signal_add_arg ( + struct GNUNET_DBUS_Signal *signal, + const char *name, + const char *signature) +{ + struct GNUNET_DBUS_Arg *arg = GNUNET_DBUS_arg_create (name, signature); + struct GNUNET_DBUS_ArgIterator *arg_it = GNUNET_new (struct GNUNET_DBUS_ArgIterator); + arg_it->arg = arg; + GNUNET_CONTAINER_DLL_insert_tail (signal->args_front, + signal->args_back, + arg_it); +}; + +const char * +GNUNET_DBUS_signal_get_name ( + const struct GNUNET_DBUS_Signal *signal) +{ + return signal->name; +} + +const struct GNUNET_DBUS_ArgIterator * +GNUNET_DBUS_signal_iterate_args ( + const struct GNUNET_DBUS_Signal *signal) +{ + return signal->args_front; +} + +DBusMessage * +GNUNET_DBUS_signal_spawn ( + const struct GNUNET_DBUS_Signal *signal, + const struct GNUNET_DBUS_ObjectPath *path, + const struct GNUNET_DBUS_Interface *interface, + bool pretty) +{ + char *path_str = GNUNET_DBUS_object_path_to_string (path); + DBusMessage *ret = dbus_message_new_signal ( + path_str, + GNUNET_DBUS_interface_get_name (interface), + signal->name); + GNUNET_free (path_str); + GNUNET_DBUS_message_set_pretty (ret, pretty); + return ret; +}; + +DBusMessage * +GNUNET_DBUS_signal_spawn_unicast ( + const struct GNUNET_DBUS_Signal *signal, + const struct GNUNET_DBUS_ObjectPath *path, + const struct GNUNET_DBUS_Interface *interface, + const struct GNUNET_DBUS_Client *client, + bool pretty) +{ + DBusMessage *ret = GNUNET_DBUS_signal_spawn (signal, path, interface, pretty); + dbus_message_set_destination (ret, GNUNET_DBUS_client_get_unique_name (client)); + return ret; +} + diff --git a/src/lib/common/gnunet_dbus_lib_signature.c b/src/lib/common/gnunet_dbus_lib_signature.c new file mode 100644 index 0000000..73832fb --- /dev/null +++ b/src/lib/common/gnunet_dbus_lib_signature.c @@ -0,0 +1,60 @@ +#include "config.h" + +#include "gnunet_dbus_lib.h" + +const char * +GNUNET_DBUS_signature_typecode_to_string ( + int typecode) +{ + switch (typecode) + { + case DBUS_TYPE_INVALID: + return "nothing"; + case DBUS_TYPE_BOOLEAN: + return "boolean"; + case DBUS_TYPE_BYTE: + return "byte"; + case DBUS_TYPE_INT16: + return "int16"; + case DBUS_TYPE_UINT16: + return "uint16"; + case DBUS_TYPE_INT32: + return "int32"; + case DBUS_TYPE_UINT32: + return "uint32"; + case DBUS_TYPE_INT64: + return "int64"; + case DBUS_TYPE_UINT64: + return "uint64"; + case DBUS_TYPE_DOUBLE: + return "double"; + case DBUS_TYPE_STRING: + return "string"; + case DBUS_TYPE_OBJECT_PATH: + return "object_path"; + case DBUS_TYPE_SIGNATURE: + return "signature"; + case DBUS_TYPE_STRUCT: + return "struct"; + case DBUS_TYPE_DICT_ENTRY: + return "dict_entry"; + case DBUS_TYPE_ARRAY: + return "array"; + case DBUS_TYPE_VARIANT: + return "variant"; + case DBUS_STRUCT_BEGIN_CHAR: + return "begin_struct"; + case DBUS_STRUCT_END_CHAR: + return "end_struct"; + case DBUS_DICT_ENTRY_BEGIN_CHAR: + return "begin_dict_entry"; + case DBUS_DICT_ENTRY_END_CHAR: + return "end_dict_entry"; + case DBUS_TYPE_UNIX_FD: + return "unix_fd"; + default: + return "unknown"; + } +} + + diff --git a/src/lib/common/timeout.c b/src/lib/common/timeout.c new file mode 100644 index 0000000..b12780b --- /dev/null +++ b/src/lib/common/timeout.c @@ -0,0 +1,131 @@ +#include "timeout.h" + +#include + +#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-timeout", __VA_ARGS__) + +struct Timeout +{ + /* doubly-linked list */ + struct Timeout *next; + struct Timeout *prev; + + DBusTimeout *timeout; + + bool scheduled; + + struct GNUNET_SCHEDULER_Task *task; + + unsigned ref_count; +}; + +struct Timeout * +timeout_create ( + DBusTimeout *timeout) +{ + struct Timeout *t = GNUNET_new (struct Timeout); + + t->timeout = timeout; + t->scheduled = false; + t->ref_count = 1; + + return t; +} + +void +timeout_ref ( + struct Timeout *t) +{ + t->ref_count++; +} + +void +timeout_unref ( + struct Timeout *t) +{ + if (0 == t->ref_count) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unref timeout with ref_count == 0\n"); + GNUNET_abort_ (); + }; + + if (0 == --t->ref_count) + { + GNUNET_free (t); + } +} + +static void +handle_timeout ( + void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct Timeout *t = (struct Timeout *)cls; + + t->scheduled = false; + + if (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) + dbus_timeout_handle (t->timeout); + + if (t->ref_count > 1 && ! (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + timeout_schedule (t); + timeout_unref (t); +}; + +void +timeout_schedule ( + struct Timeout *t) +{ + if (! dbus_timeout_get_enabled (t->timeout)) + { + LOG (GNUNET_ERROR_TYPE_WARNING, "Tried to schedule timeout that is disabled!\n"); + return; + }; + + if (t->scheduled) + return; + + int interval = dbus_timeout_get_interval (t->timeout); + struct GNUNET_TIME_Relative delay; + delay.rel_value_us = interval * 1000; + t->task = GNUNET_SCHEDULER_add_delayed ( + delay, + handle_timeout, + t); + t->scheduled = true; + timeout_ref (t); +} + +void +do_timeout_unschedule ( + void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + (void)tc; + struct Timeout *t = (struct Timeout *)cls; + + if (! t->scheduled) + return; + + void *ret = GNUNET_SCHEDULER_cancel (t->task); + if ((struct Timeout *)ret != t) + LOG (GNUNET_ERROR_TYPE_WARNING, "Weird result unscheduling task. t == %p, GNUNET_SCHEDULER_cancel returned %p\n", t, ret); + + timeout_unref (t); +} + +void +timeout_unschedule ( + struct Timeout *t) +{ + GNUNET_SCHEDULER_add_now (do_timeout_unschedule, t); +} + +struct TimeoutIter * +timeout_find (struct TimeoutIter *ti, DBusTimeout *timeout) +{ + while (ti && ti->t->timeout != timeout) + ti = ti->next; + return ti; +} + diff --git a/src/lib/common/timeout.h b/src/lib/common/timeout.h new file mode 100644 index 0000000..5230057 --- /dev/null +++ b/src/lib/common/timeout.h @@ -0,0 +1,31 @@ +#ifndef TIMEOUT_H +#define TIMEOUT_H + +#include + +#include +#include +#include + +struct Timeout; + +struct TimeoutIter +{ + struct TimeoutIter *next; + struct TimeoutIter *prev; + + struct Timeout *t; +}; + +struct Timeout *timeout_create (DBusTimeout *timeout); +void timeout_ref (struct Timeout *t); +void timeout_unref (struct Timeout *t); + +void timeout_schedule (struct Timeout *t); +void timeout_unschedule (struct Timeout *t); +void do_timeout_unschedule (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); + +struct TimeoutIter *timeout_find (struct TimeoutIter *, DBusTimeout *timeout); + +#endif + diff --git a/src/lib/common/watch.c b/src/lib/common/watch.c new file mode 100644 index 0000000..e6b5317 --- /dev/null +++ b/src/lib/common/watch.c @@ -0,0 +1,235 @@ +#include "watch.h" + +#include + +#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-watch", __VA_ARGS__) + +/* + * Wraps a file descriptor that needs to be watched + * for activity with select() + */ +struct Watch +{ + /* + * DBus watch data. Contains the actual file descritor wrapped by libdbus + */ + DBusWatch *watch; + + /* + * Have we asked the scheduler to watch this? + * Will be false if the associated task has not been + * re-scheduled yet after execution or because dbus has asked + * us to disable this watch. + */ + bool scheduled; + + /* + * The task that is watching our file descriptor. + * Only valid if scheduled is true. + */ + struct GNUNET_SCHEDULER_Task *task; + + struct GNUNET_NETWORK_Handle *net_handle; + struct GNUNET_DISK_FileHandle *file_handle; + + unsigned ref_count; +}; + +struct Watch * +watch_create ( + DBusWatch *watch) +{ + struct Watch *w = GNUNET_new (struct Watch); + + w->watch = watch; + w->scheduled = false; + w->net_handle = NULL; + w->file_handle = NULL; + w->ref_count = 1; + + SOCKTYPE sock = dbus_watch_get_socket (watch); + if (-1 != sock) + { + w->net_handle = GNUNET_NETWORK_socket_box_native (sock); + if (NULL == w->net_handle) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to box network socket passed in from dbus.\n"); + GNUNET_abort_ (); + }; + } + else { + int fd = dbus_watch_get_unix_fd (watch); + if (-1 != fd) + { + w->file_handle = GNUNET_DISK_get_handle_from_int_fd (fd); + if (NULL == w->file_handle) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to box file handle passed in from dbus.\n"); + GNUNET_abort_ (); + }; + }; + }; + + if (! w->net_handle && ! w->file_handle) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create watch. dbus_watch_get_socket returned %d\n", (int)sock); + GNUNET_abort_ (); + }; + + return w; +}; + +void +watch_ref ( + struct Watch *w) +{ + w->ref_count++; +}; + +void +watch_unref ( + struct Watch *w) +{ + if (0 == w->ref_count) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unref watch with ref_count == 0\n"); + GNUNET_abort_ (); + }; + + if (0 == --w->ref_count) + { + if (w->net_handle) + GNUNET_free (w->net_handle); + if (w->file_handle) + GNUNET_free (w->file_handle); + + GNUNET_free (w); + } +}; + +/* + * Callback called by the scheduler to tell libdbus that there is activity on + * one of its file descriptors. + * + * @param cls The watch + * @param tc the context given to us by the scheduler for this execution + */ +void +handle_watch ( + void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct Watch *w = (struct Watch *)cls; + + w->scheduled = false; + + unsigned flags = 0; + if (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) + flags |= DBUS_WATCH_READABLE; + if (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) + flags |= DBUS_WATCH_WRITABLE; + if (flags) + { + dbus_watch_handle(w->watch, flags); + }; + + if(w->ref_count > 1 && ! (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + watch_schedule (w); + watch_unref (w); +}; + +/* + * Ask the scheduler to watch this watch for activity. + * + * @param w The watch + * @return GNUNET_OK or GNUNET_SYSERR + */ +void +watch_schedule ( + struct Watch *w) +{ + unsigned flags = dbus_watch_get_flags (w->watch); + + if (! dbus_watch_get_enabled (w->watch)) + { + LOG (GNUNET_ERROR_TYPE_WARNING, "Tried to schedule watch that is disabled!\n"); + return; + }; + + if (w->scheduled) + return; + + if (w->net_handle) + { + w->task = GNUNET_SCHEDULER_add_net_with_priority( + GNUNET_TIME_UNIT_FOREVER_REL, + GNUNET_SCHEDULER_PRIORITY_DEFAULT, + w->net_handle, + flags & DBUS_WATCH_READABLE, + flags & DBUS_WATCH_WRITABLE, + handle_watch, + w); + w->scheduled = true; + watch_ref (w); + return; + }; + + if (w->file_handle) + { + w->task = GNUNET_SCHEDULER_add_file_with_priority( + GNUNET_TIME_UNIT_FOREVER_REL, + GNUNET_SCHEDULER_PRIORITY_DEFAULT, + w->file_handle, + flags & DBUS_WATCH_READABLE, + flags & DBUS_WATCH_WRITABLE, + handle_watch, + w); + w->scheduled = true; + watch_ref (w); + return; + }; + + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to schedule watch.\n"); + GNUNET_abort_ (); +}; + +/* + * Ask the scheduler to stop monitoring a watch either because we are shutting + * down or dbus has asked us to disable this watch. + * + * @param w The watch + * @return GNUNET_OK or GNUNET_SYSERR + */ +void +do_watch_unschedule ( + void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + (void)tc; + struct Watch *w = (struct Watch *)cls; + + if (! w->scheduled) + return; + + void *ret = GNUNET_SCHEDULER_cancel (w->task); + if ((struct Watch *)ret != w) + LOG (GNUNET_ERROR_TYPE_WARNING, "Weird result unscheduling task. w == %p, GNUNET_SCHEDULER_cancel returned %p\n", w, ret); + + watch_unref (w); +}; + +void +watch_unschedule ( + struct Watch *w) +{ + GNUNET_SCHEDULER_add_now (do_watch_unschedule, w); +}; + +struct WatchIter * +watch_find (struct WatchIter *wi, DBusWatch *watch) +{ + while (wi && wi->w->watch != watch) + wi = wi->next; + return wi; +}; + diff --git a/src/lib/common/watch.h b/src/lib/common/watch.h new file mode 100644 index 0000000..ca60155 --- /dev/null +++ b/src/lib/common/watch.h @@ -0,0 +1,34 @@ +#ifndef WATCH_H +#define WATCH_H + +#include + +#include +#include +#include + +struct Watch; + +struct WatchIter +{ + struct WatchIter *next; + struct WatchIter *prev; + + struct Watch *w; +}; + +struct Watch *watch_create (DBusWatch *watch); +void watch_ref (struct Watch *w); +void watch_unref (struct Watch *w); + +/* + * Schedule or schedule the scheduler to monitor dbus file descriptors. + */ +void watch_schedule (struct Watch *w); +void watch_unschedule (struct Watch *w); +void do_watch_unschedule (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); + +struct WatchIter *watch_find (struct WatchIter *, DBusWatch *watch); + +#endif + diff --git a/src/lib/dht/Makefile.am b/src/lib/dht/Makefile.am new file mode 100644 index 0000000..2adb485 --- /dev/null +++ b/src/lib/dht/Makefile.am @@ -0,0 +1,16 @@ +# This Makefile.am is in the public domain +lib_LTLIBRARIES = \ + libgnunetdhtdbus.la + +libgnunetdhtdbus_la_SOURCES = \ + gnunet_dht_dbus_lib.c \ + gnunet_dht_dbus_lib_pop.c \ + gnunet_dht_dbus_lib_push.c + +libgnunetdhtdbus_la_CFLAGS = \ + -I$(top_builddir)/src/lib/include \ + $(DBUS_CFLAGS) + +libgnunetdhtdbus_la_LDFLAGS = \ + $(DBUS_LIBS) + diff --git a/src/lib/dht/gnunet_dht_dbus_lib.c b/src/lib/dht/gnunet_dht_dbus_lib.c new file mode 100644 index 0000000..f01c9ea --- /dev/null +++ b/src/lib/dht/gnunet_dht_dbus_lib.c @@ -0,0 +1,13 @@ +#include "config.h" + +#include "gnunet_dbus_lib.h" +#include "gnunet_dht_dbus_lib.h" + +const struct GNUNET_DBUS_StringEnumPair GNUNET_DHT_DBUS_route_option_description[] = { + { "demultiplex_everywhere", GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE }, + { "record_route", GNUNET_DHT_RO_RECORD_ROUTE }, + { "find_peer", GNUNET_DHT_RO_FIND_PEER }, + { "bart", GNUNET_DHT_RO_BART }, + { NULL, 0} +}; + diff --git a/src/lib/dht/gnunet_dht_dbus_lib_pop.c b/src/lib/dht/gnunet_dht_dbus_lib_pop.c new file mode 100644 index 0000000..e95fa6f --- /dev/null +++ b/src/lib/dht/gnunet_dht_dbus_lib_pop.c @@ -0,0 +1,16 @@ +#include "config.h" + +#include "gnunet_dbus_lib.h" +#include "gnunet_dht_dbus_lib.h" + +DBusMessage * +GNUNET_DHT_DBUS_pop_route_option ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + enum GNUNET_DHT_RouteOption *value) +{ + return GNUNET_DBUS_pop_bitfield (message, iter, arg_name, (int *)value, GNUNET_DHT_DBUS_route_option_description); +}; + + diff --git a/src/lib/dht/gnunet_dht_dbus_lib_push.c b/src/lib/dht/gnunet_dht_dbus_lib_push.c new file mode 100644 index 0000000..b65314e --- /dev/null +++ b/src/lib/dht/gnunet_dht_dbus_lib_push.c @@ -0,0 +1,14 @@ +#include "config.h" + +#include "gnunet_dbus_lib.h" +#include "gnunet_dht_dbus_lib.h" + +void +GNUNET_DHT_DBUS_push_route_option ( + DBusMessage *message, + DBusMessageIter *iter, + const enum GNUNET_DHT_RouteOption *value) +{ + return GNUNET_DBUS_push_bitfield (message, iter, (const int *)value, GNUNET_DHT_DBUS_route_option_description); +}; + diff --git a/src/lib/gnsrecord/Makefile.am b/src/lib/gnsrecord/Makefile.am new file mode 100644 index 0000000..19add3b --- /dev/null +++ b/src/lib/gnsrecord/Makefile.am @@ -0,0 +1,17 @@ +# This Makefile.am is in the public domain +lib_LTLIBRARIES = \ + libgnunetgnsrecorddbus.la + +libgnunetgnsrecorddbus_la_SOURCES = \ + gnunet_gnsrecord_dbus_lib.c \ + gnunet_gnsrecord_dbus_lib_pop.c \ + gnunet_gnsrecord_dbus_lib_push.c + +libgnunetgnsrecorddbus_la_CFLAGS = \ + -I$(top_builddir)/src/lib/include \ + $(DBUS_CFLAGS) + +libgnunetgnsrecorddbus_la_LDFLAGS = \ + $(top_builddir)/src/lib/util/libgnunettimedbus.la \ + $(DBUS_LIBS) + diff --git a/src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib.c b/src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib.c new file mode 100644 index 0000000..be8ba82 --- /dev/null +++ b/src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib.c @@ -0,0 +1,23 @@ +#include "config.h" + +#include +#include +#include +#include +#include + +#include "gnunet_dbus_lib.h" + +const struct GNUNET_DBUS_StringEnumPair * +GNUNET_GNSRECORD_DBUS_describe_flags() +{ + static const struct GNUNET_DBUS_StringEnumPair options[] = { + { "private", GNUNET_GNSRECORD_RF_PRIVATE }, + { "relative_expiration", GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION }, + { "shadow_record", GNUNET_GNSRECORD_RF_SHADOW_RECORD }, + { NULL, 0 } + }; + return options; +}; + + diff --git a/src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib_pop.c b/src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib_pop.c new file mode 100644 index 0000000..3e09d4b --- /dev/null +++ b/src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib_pop.c @@ -0,0 +1,85 @@ +#include "config.h" + +#include "gnunet_gnsrecord_dbus_lib.h" + +DBusMessage * +GNUNET_GNSRECORD_DBUS_pop_type ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + uint32_t *value) +{ + DBusMessageIter iter_sub; + DBusMessage *ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name); + if (ret) + return ret; + + int arg_type = dbus_message_iter_get_arg_type (&iter_sub); + switch (arg_type) + { + case DBUS_TYPE_STRING: { + const char *string; + dbus_message_iter_get_basic (&iter_sub, &string); + *value = GNUNET_GNSRECORD_typename_to_number (string); + if (*value == UINT32_MAX) + { + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. %s is not a valid GNS record type. (Should be something like 'AAAA' or 'CNAME' etc.)", + arg_name, + string); + }; + return NULL; + }; + case DBUS_TYPE_UINT32: + dbus_message_iter_get_basic (&iter_sub, value); + return NULL; + default: + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Variant should contain a string or uint32. Contains a %s.", + arg_name, + GNUNET_DBUS_signature_typecode_to_string (arg_type)); + }; +}; + +DBusMessage * +GNUNET_GNSRECORD_DBUS_pop_flags ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + enum GNUNET_GNSRECORD_Flags *value) +{ + return GNUNET_DBUS_pop_bitfield (message, iter, arg_name, (int *)value, GNUNET_GNSRECORD_DBUS_describe_flags ()); +}; + +#if 0 +DBusMessage * +GNUNET_GNSRECORD_DBUS_pop_data ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + struct GNUNET_GNSRECORD_Data *value) +{ + DBusMessageIter iter_sub; + DBusMessage *ret = GNUNET_DBUS_pop_enter_struct (message, iter, &iter_sub, arg_name); + if (ret) + return ret; + + DBusMessageIter iter_sub_sub; + GNUNET_GNSRECORD_DBUS_pop_type (message, &iter_sub, "record_type", &value->record_type); + GNUNET_GNSRECORD_DBUS_pop_flags (message, &iter_sub, "flags", &value->flags); + GNUNET_DBUS_pop_enter_variant (message, &iter_sub, &iter_sub_sub); + int arg_type = dbus_message_iter_get_arg_type (&iter_sub_sub); + switch (arg_type) + { + case DBUS_TYPE_STRING: { + const char *stringified; + dbus_message_iter_get_basic (&iter_sub_sub, &stringified); + int success = GNUNET_GNSRECORD_string_to_value (value->record_type, stringified, + +#endif + + diff --git a/src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib_push.c b/src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib_push.c new file mode 100644 index 0000000..00dddc0 --- /dev/null +++ b/src/lib/gnsrecord/gnunet_gnsrecord_dbus_lib_push.c @@ -0,0 +1,88 @@ +#include "config.h" + +#include + +#include "gnunet_gnsrecord_dbus_lib.h" +#include "gnunet_time_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from(kind, "gnsrecord-dbus-push", __VA_ARGS__) + +void +GNUNET_GNSRECORD_DBUS_push_type ( + DBusMessage *message, + DBusMessageIter *iter, + const uint32_t *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing GNSRECORD type\n"); + DBusMessageIter iter_sub; + if (GNUNET_DBUS_message_get_pretty (message)) + { + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_STRING); + const char *stringified = GNUNET_GNSRECORD_number_to_typename (*value); + GNUNET_DBUS_push_string (message, &iter_sub, &stringified); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + } + else + { + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_UINT32); + GNUNET_DBUS_push_uint32 (message, &iter_sub, value); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + } +}; + +void +GNUNET_GNSRECORD_DBUS_push_flags ( + DBusMessage *message, + DBusMessageIter *iter, + const enum GNUNET_GNSRECORD_Flags *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing GNUNET_GNSRECORD_Flags\n"); + GNUNET_DBUS_push_bitfield (message, iter, (const int *)value, GNUNET_GNSRECORD_DBUS_describe_flags ()); +}; + +void +GNUNET_GNSRECORD_DBUS_push_data ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_GNSRECORD_Data *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing GNUNET_GNSRECORD_Data\n"); + DBusMessageIter iter_sub; + DBusMessageIter iter_sub_sub; + + GNUNET_DBUS_push_open_struct (message, iter, &iter_sub); + GNUNET_GNSRECORD_DBUS_push_type (message, &iter_sub, &value->record_type); + GNUNET_GNSRECORD_DBUS_push_flags (message, &iter_sub, &value->flags); + + if (GNUNET_DBUS_message_get_pretty (message)) + { + GNUNET_DBUS_push_open_variant (message, &iter_sub, &iter_sub_sub, GNUNET_DBUS_SIGNATURE_STRING); + char *stringified = GNUNET_GNSRECORD_value_to_string (value->record_type, value->data, value->data_size); + const char *stringified_const = stringified; + GNUNET_DBUS_push_string (message, &iter_sub_sub, &stringified_const); + GNUNET_free (stringified); + GNUNET_DBUS_push_close_variant (message, &iter_sub, &iter_sub_sub); + } + else + { + GNUNET_DBUS_push_open_variant (message, &iter_sub, &iter_sub_sub, GNUNET_DBUS_SIGNATURE_ARRAY (GNUNET_DBUS_SIGNATURE_BYTE)); + GNUNET_DBUS_push_byte_array (message, &iter_sub_sub, (const unsigned char *)value->data, value->data_size); + GNUNET_DBUS_push_close_variant (message, &iter_sub, &iter_sub_sub); + }; + + if (value->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION) + { + struct GNUNET_TIME_Relative expiration_time; + expiration_time.rel_value_us = value->expiration_time; + GNUNET_TIME_DBUS_push_relative (message, &iter_sub, &expiration_time); + } + else + { + struct GNUNET_TIME_Absolute expiration_time; + expiration_time.abs_value_us = value->expiration_time; + GNUNET_TIME_DBUS_push_absolute (message, &iter_sub, &expiration_time); + }; + GNUNET_DBUS_push_close_struct (message, iter, &iter_sub); +} + + diff --git a/src/lib/include/gnunet_block_dbus_lib.h b/src/lib/include/gnunet_block_dbus_lib.h new file mode 100644 index 0000000..acdfc99 --- /dev/null +++ b/src/lib/include/gnunet_block_dbus_lib.h @@ -0,0 +1,18 @@ +#ifndef GNUNET_BLOCK_DBUS_LIB_H +#define GNUNET_BLOCK_DBUS_LIB_H + +#include + +/** + * A {NULL, 0}-terminated list of textual descriptions of `GNUNET_BLOCK_Type` + * enum variants. Used for converting back-and-forth between `GNUNET_DBUS_Type` + * and `const char *` + */ +extern const struct GNUNET_DBUS_StringEnumPair GNUNET_BLOCK_DBUS_type_description[]; + +#include "gnunet_block_dbus_lib_pop.h" +#include "gnunet_block_dbus_lib_push.h" +#include "gnunet_block_dbus_lib_signature.h" + +#endif + diff --git a/src/lib/include/gnunet_block_dbus_lib_pop.h b/src/lib/include/gnunet_block_dbus_lib_pop.h new file mode 100644 index 0000000..ac8ce30 --- /dev/null +++ b/src/lib/include/gnunet_block_dbus_lib_pop.h @@ -0,0 +1,24 @@ +#ifndef GNUNET_BLOCK_DBUS_LIB_POP_H +#define GNUNET_BLOCK_DBUS_LIB_POP_H + +#include +#include +#include +#include + +#include "gnunet_dbus_lib_method_context.h" + +/** + * Pop a GNUNET_DBUS_Type off of a DBusMessage, moving the iterator. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_BLOCK_DBUS_pop_type ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + enum GNUNET_BLOCK_Type *value); + +#endif + diff --git a/src/lib/include/gnunet_block_dbus_lib_push.h b/src/lib/include/gnunet_block_dbus_lib_push.h new file mode 100644 index 0000000..27d78c3 --- /dev/null +++ b/src/lib/include/gnunet_block_dbus_lib_push.h @@ -0,0 +1,16 @@ +#ifndef GNUNET_BLOCK_DBUS_LIB_PUSH_H +#define GNUNET_BLOCK_DBUS_LIB_PUSH_H + +#include "gnunet_block_dbus_lib.h" + +/** + * Push a GNUNET_BLOCK_Type onto a DBusMessage, advancing the iterator. + */ +void +GNUNET_BLOCK_DBUS_push_type ( + DBusMessage *message, + DBusMessageIter *iter, + const enum GNUNET_BLOCK_Type *value); + +#endif + diff --git a/src/lib/include/gnunet_block_dbus_lib_signature.h b/src/lib/include/gnunet_block_dbus_lib_signature.h new file mode 100644 index 0000000..f6970ec --- /dev/null +++ b/src/lib/include/gnunet_block_dbus_lib_signature.h @@ -0,0 +1,7 @@ +#ifndef GNUNET_BLOCK_DBUS_LIB_SIGNATURE_H +#define GNUNET_BLOCK_DBUS_LIB_SIGNATURE_H + +#define GNUNET_BLOCK_DBUS_SIGNATURE_TYPE GNUNET_DBUS_SIGNATURE_VARIANT + +#endif + diff --git a/src/lib/include/gnunet_crypto_dbus_lib.h b/src/lib/include/gnunet_crypto_dbus_lib.h new file mode 100644 index 0000000..ddcc653 --- /dev/null +++ b/src/lib/include/gnunet_crypto_dbus_lib.h @@ -0,0 +1,11 @@ +#ifndef GNUNET_CRYPTO_DBUS_LIB_H +#define GNUNET_CRYPTO_DBUS_LIB_H + +#include + +#include "gnunet_crypto_dbus_lib_push.h" +#include "gnunet_crypto_dbus_lib_pop.h" +#include "gnunet_crypto_dbus_lib_signature.h" + +#endif + diff --git a/src/lib/include/gnunet_crypto_dbus_lib_pop.h b/src/lib/include/gnunet_crypto_dbus_lib_pop.h new file mode 100644 index 0000000..7fe086a --- /dev/null +++ b/src/lib/include/gnunet_crypto_dbus_lib_pop.h @@ -0,0 +1,33 @@ +#ifndef GNUNET_CRYPTO_DBUS_LIB_POP_H +#define GNUNET_CRYPTO_DBUS_LIB_POP_H + +#include +#include +#include + +/** + * Pop a GNUNET_CRYPT_EcdsaPublicKey off of a DBusMessage, moving the iterator. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_CRYPTO_DBUS_pop_ecdsa_public_key ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + struct GNUNET_CRYPTO_EcdsaPublicKey *value); + +/** + * Pop a GNUNET_CRYPTO_EddsaPublicKey off a DBusMessage, moving the iterator. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_CRYPTO_DBUS_pop_eddsa_public_key ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + struct GNUNET_CRYPTO_EddsaPublicKey *value); + +#endif + diff --git a/src/lib/include/gnunet_crypto_dbus_lib_push.h b/src/lib/include/gnunet_crypto_dbus_lib_push.h new file mode 100644 index 0000000..80b15d6 --- /dev/null +++ b/src/lib/include/gnunet_crypto_dbus_lib_push.h @@ -0,0 +1,27 @@ +#ifndef GNUNET_CRYPTO_DBUS_LIB_PUSH_H +#define GNUNET_CRYPTO_DBUS_LIB_PUSH_H + +#include +#include +#include + +/** + * Push a GNUNET_CRYPTO_EcdsaPublicKey onto a DBusMessage, advancing the iterator. + */ +void +GNUNET_CRYPTO_DBUS_push_ecdsa_public_key ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_CRYPTO_EcdsaPublicKey *value); + +/** + * Push a GNUNET_CRYPTO_EddsaPublicKey onto a DBusMessage, advancing the iterator. + */ +void +GNUNET_CRYPTO_DBUS_push_eddsa_public_key ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_CRYPTO_EddsaPublicKey *value); + +#endif + diff --git a/src/lib/include/gnunet_crypto_dbus_lib_signature.h b/src/lib/include/gnunet_crypto_dbus_lib_signature.h new file mode 100644 index 0000000..94fed6e --- /dev/null +++ b/src/lib/include/gnunet_crypto_dbus_lib_signature.h @@ -0,0 +1,22 @@ +#ifndef GNUNET_CRYPTO_DBUS_LIB_SIGNATURE_H +#define GNUNET_CRYPTO_DBUS_LIB_SIGNATURE_H + +#include "gnunet_dbus_lib.h" + +#define GNUNET_CRYPTO_DBUS_SIGNATURE_ECDSA_SIGNATURE \ + GNUNET_DBUS_SIGNATURE_STRUCT ( \ + GNUNET_DBUS_SIGNATURE_VARIANT \ + GNUNET_DBUS_SIGNATURE_VARIANT \ + ) + +#define GNUNET_CRYPTO_DBUS_SIGNATURE_ECC_SIGNATURE_PURPOSE \ + GNUNET_DBUS_SIGNATURE_STRUCT ( \ + GNUNET_DBUS_SIGNATURE_UINT32 \ + GNUNET_DBUS_SIGNATURE_UINT32 \ + ) + +#define GNUNET_CRYPTO_DBUS_SIGNATURE_ECDSA_PUBLIC_KEY GNUNET_DBUS_SIGNATURE_VARIANT +#define GNUNET_CRYPTO_DBUS_SIGNATURE_EDDSA_PUBLIC_KEY GNUNET_DBUS_SIGNATURE_VARIANT + +#endif + diff --git a/src/lib/include/gnunet_dbus_lib.h b/src/lib/include/gnunet_dbus_lib.h new file mode 100644 index 0000000..7cf5143 --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib.h @@ -0,0 +1,29 @@ +#ifndef GNUNET_DBUS_LIB_H +#define GNUNET_DBUS_LIB_H + +/** + * Used to assign a textual description to an enum variant. + * eg. {"dht_hello", GNUNET_BLOCK_TYPE_DHT_HELLO}. + * Generally stored in an array, terminated by {NULL, 0}. + */ +struct GNUNET_DBUS_StringEnumPair { + const char *name; + const int value; +}; + +#include "gnunet_dbus_lib_service.h" +#include "gnunet_dbus_lib_object.h" +#include "gnunet_dbus_lib_object_path.h" +#include "gnunet_dbus_lib_interface.h" +#include "gnunet_dbus_lib_method.h" +#include "gnunet_dbus_lib_signal.h" +#include "gnunet_dbus_lib_arg.h" +#include "gnunet_dbus_lib_message.h" +#include "gnunet_dbus_lib_method_context.h" +#include "gnunet_dbus_lib_client.h" +#include "gnunet_dbus_lib_signature.h" +#include "gnunet_dbus_lib_push.h" +#include "gnunet_dbus_lib_pop.h" + +#endif + diff --git a/src/lib/include/gnunet_dbus_lib_arg.h b/src/lib/include/gnunet_dbus_lib_arg.h new file mode 100644 index 0000000..34a8a68 --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib_arg.h @@ -0,0 +1,75 @@ +#ifndef GNUNET_DBUS_LIB_ARG_H +#define GNUNET_DBUS_LIB_ARG_H + +/** + * Used to represent an argument to a DBus method or signal. An argument + * consists of a name and a DBus type signature. + */ +struct GNUNET_DBUS_Arg; + +/** + * An iterable, doubly-linked-list of GNUNET_DBUS_Arg. + */ +struct GNUNET_DBUS_ArgIterator +{ + /* linked list */ + struct GNUNET_DBUS_ArgIterator *next; + struct GNUNET_DBUS_ArgIterator *prev; + + struct GNUNET_DBUS_Arg *arg; +}; + +/** + * Create a GNUNET_DBUS_Arg with the given name, DBus type signature and a + * reference count of one. + */ +struct GNUNET_DBUS_Arg * +GNUNET_DBUS_arg_create ( + const char *name, + const char *signature); + +/** + * Increase the reference count of this GNUNET_DBUS_Arg by one. + */ +void +GNUNET_DBUS_arg_ref ( + struct GNUNET_DBUS_Arg *arg); + +/** + * Decrease the reference count of this GNUNET_DBUS_Arg by one. Will free the + * GNNET_DBUS_Arg if the reference count reaches zero. + */ +void +GNUNET_DBUS_arg_unref ( + struct GNUNET_DBUS_Arg *arg); + +/** + * Get the name of this GNUNET_DBUS_Arg. + */ +const char * +GNUNET_DBUS_arg_get_name ( + const struct GNUNET_DBUS_Arg *arg); + +/** + * Get the DBus type signature of this GNUNET_DBUS_Arg. + */ +const char * +GNUNET_DBUS_arg_get_signature ( + const struct GNUNET_DBUS_Arg *arg); + +#if 0 +void +GNUNET_DBUS_arg_pop_message ( + const struct GNUNET_DBUS_Arg *arg, + DBusMessageIter *dbus_message_iter, + void *value); + +void +GNUNET_DBUS_arg_push_message ( + const struct GNUNET_DBUS_Arg *arg, + DBusMessageIter *dbus_message_iter, + va_list vl); +#endif + +#endif + diff --git a/src/lib/include/gnunet_dbus_lib_client.h b/src/lib/include/gnunet_dbus_lib_client.h new file mode 100644 index 0000000..413a0c3 --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib_client.h @@ -0,0 +1,105 @@ +#ifndef GNUNET_DBUS_CLIENT_H +#define GNUNET_DBUS_CLIENT_H + +#include + +/** + * Used by services to keep track of connected clients. Can associate an + * arbitrary piece of data with each client. + */ +struct GNUNET_DBUS_Client; + +/** + * An iterable, double-linked-list of GNUNET_DBUS_Client + */ +struct GNUNET_DBUS_ClientIterator +{ + struct GNUNET_DBUS_ClientIterator *next; + struct GNUNET_DBUS_ClientIterator *prev; + + struct GNUNET_DBUS_Client *client; +}; + +/** + * Create a GNUNET_DBUS_Client with the given DBus unique name (eg. "1:23"). + */ +struct GNUNET_DBUS_Client * +GNUNET_DBUS_client_create ( + const char *unique_name); + +/** + * Increase the reference count of this GNUNET_DBUS_Client by one. + */ +void +GNUNET_DBUS_client_ref ( + struct GNUNET_DBUS_Client *client); + +/** + * Decrease the reference count of this GNUNET_DBUS_Client by one. Will free + * the GNUNET_DBUS_Client if the reference count reaches zero. + */ +void +GNUNET_DBUS_client_unref ( + struct GNUNET_DBUS_Client *client); + +/* + * Get the DBus unique name of this client (eg. "1:23"). + */ +const char * +GNUNET_DBUS_client_get_unique_name ( + const struct GNUNET_DBUS_Client *client); + +/** + * Set the arbitrary piece of data associated with the client. + */ +void +GNUNET_DBUS_client_set_data ( + struct GNUNET_DBUS_Client *client, + void *data); + +/** + * Get the arbitrary piece of data associated with the client. + */ +void * +GNUNET_DBUS_client_get_data ( + const struct GNUNET_DBUS_Client *client); +/** + * Set whether this client prefers data to be pretty-encoded for transmission + * over DBus. For example, the enum variant GNUNET_BLOCK_TYPE_DHT_HELLO will be + * sent over-the-wire to this client as a DBus uint32 or as a the DBus string + * "dht_hello" depending on this flag. + */ +void +GNUNET_DBUS_client_set_prefers_pretty_encodings ( + struct GNUNET_DBUS_Client *client, + bool prefers_pretty_encodings); + +/** + * Get whether this client prefers data to be pretty-encoded for transmission + * over DBus. For example, the enum variant GNUNET_BLOCK_TYPE_DHT_HELLO will be + * sent over-the-wire to this client as a DBus uint32 or as a the DBus string + * "dht_hello" depending on this flag. + */ +bool +GNUNET_DBUS_client_get_prefers_pretty_encodings ( + const struct GNUNET_DBUS_Client *client); + +DBusMessage * +GNUNET_DBUS_client_create_method_call ( + struct GNUNET_DBUS_Client *client, + const char *object_path, + const char *interface, + const char *method, + bool pretty, + void (*return_callback)(DBusMessage *message)); + +DBusMessage * +GNUNET_DBUS_client_create_unicast_signal ( + struct GNUNET_DBUS_Client *client, + const char *object_path, + const char *interface, + const char *signal, + bool pretty); + +#endif + diff --git a/src/lib/include/gnunet_dbus_lib_interface.h b/src/lib/include/gnunet_dbus_lib_interface.h new file mode 100644 index 0000000..83cd598 --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib_interface.h @@ -0,0 +1,104 @@ +#ifndef GNUNET_DBUS_LIB_INTERFACE_H +#define GNUNET_DBUS_LIB_INTERFACE_H + +#include + +/** + * Represents a DBus interface. An interface consists of a set of methods and + * signals. + */ +struct GNUNET_DBUS_Interface; + +#include "gnunet_dbus_lib_object.h" +#include "gnunet_dbus_lib_method.h" +#include "gnunet_dbus_lib_method_context.h" +#include "gnunet_dbus_lib_signal.h" + +struct GNUNET_DBUS_InterfaceIterator +{ + /* linked list */ + struct GNUNET_DBUS_InterfaceIterator *next; + struct GNUNET_DBUS_InterfaceIterator *prev; + + struct GNUNET_DBUS_Interface *interface; +}; + +/** + * Create a DBus interface with the given name. After populating this interface + * with methods and signals it can be added to an object with + * GNUNET_DBUS_object_add_interface. + */ +struct GNUNET_DBUS_Interface * +GNUNET_DBUS_interface_create ( + const char *name); + +/** + * Increase the reference count of this GNUNET_DBUS_Interface by one. + */ +void +GNUNET_DBUS_interface_ref ( + struct GNUNET_DBUS_Interface *interface); + +/** + * Decrease the reference count of this GNUNET_DBUS_Interface by one. + */ +void +GNUNET_DBUS_interface_unref ( + struct GNUNET_DBUS_Interface *interface); + +/** + * Add a method to this interface. This will increase the reference count of + * the GNUNET_DBUS_Method by one. + */ +void +GNUNET_DBUS_interface_add_method ( + struct GNUNET_DBUS_Interface *interface, + struct GNUNET_DBUS_Method *method); + +/** + * Add a signal to this interface. This will increase the reference count of + * the GNUNET_DBUS_Signal by one. + */ +void +GNUNET_DBUS_interface_add_signal ( + struct GNUNET_DBUS_Interface *interface, + struct GNUNET_DBUS_Signal *signal); + +/** + * Get the name of this GNUNET_DBUS_Interface. + */ +const char * +GNUNET_DBUS_interface_get_name ( + const struct GNUNET_DBUS_Interface *interface); + +/** + * Iterate over the methods of this GNUNET_DBUS_Interface. + */ +const struct GNUNET_DBUS_MethodIterator * +GNUNET_DBUS_interface_iterate_methods ( + const struct GNUNET_DBUS_Interface *interface); + +/** + * Iterate over the signals of this GNUNET_DBUS_Interface. + */ +const struct GNUNET_DBUS_SignalIterator * +GNUNET_DBUS_interface_iterate_signals ( + const struct GNUNET_DBUS_Interface *interface); + +/** + * Returns the org.freedesktop.DBus.Introspectable interface. + */ +struct GNUNET_DBUS_Interface * +GNUNET_DBUS_interface_introspectable (); + +/** + * Return the position in the given linked-list of the GNUNET_DBUS_Interface + * with the given name. Returns NULL if the interface could not be found. + */ +const struct GNUNET_DBUS_InterfaceIterator * +GNUNET_DBUS_interface_find ( + const struct GNUNET_DBUS_InterfaceIterator *int_it, + const char *name); + +#endif + diff --git a/src/lib/include/gnunet_dbus_lib_message.h b/src/lib/include/gnunet_dbus_lib_message.h new file mode 100644 index 0000000..e575823 --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib_message.h @@ -0,0 +1,45 @@ +#ifndef GNUNET_DBUS_LIB_MESSAGE_H +#define GNUNET_DBUS_LIB_MESSAGE_H + +#include +#include + +#include "gnunet_dbus_lib_client.h" + +/** + * Get the value of this message's pretty-encoding flag. This flag controls + * whether data pushed to the message will be pretty-encoded for transmission + * over DBus. + */ +bool +GNUNET_DBUS_message_get_pretty ( + DBusMessage *message); + +/** + * Set the value of this message's pretty-encoding flag. This flag controls + * whether data pushed to the message will be pretty-encoded for transmission + * over DBus. + */ +void +GNUNET_DBUS_message_set_pretty ( + DBusMessage *message, + bool pretty); + +/** + * Set the destination of this message to the DBus address of the + * GNUNET_DBUS_Client. + */ +void +GNUNET_DBUS_message_set_destination ( + DBusMessage *message, + struct GNUNET_DBUS_Client *client); + +/** + * Set the destination of this message to NULL. + */ +void +GNUNET_DBUS_message_unset_destination ( + DBusMessage *message); + +#endif + diff --git a/src/lib/include/gnunet_dbus_lib_method.h b/src/lib/include/gnunet_dbus_lib_method.h new file mode 100644 index 0000000..9b47fcf --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib_method.h @@ -0,0 +1,110 @@ +#ifndef GNUNET_DBUS_LIB_METHOD_H +#define GNUNET_DBUS_LIB_METHOD_H + +#include + +/** + * Represents a DBus method. Consists of a linked-list of args and return args + * of type GNUNET_DBUS_Arg. + */ +struct GNUNET_DBUS_Method; + +#include "gnunet_dbus_lib_object.h" +#include "gnunet_dbus_lib_method_context.h" + +/** + * An iterable, double-linked list of GNUNET_DBUS_Method. + */ +struct GNUNET_DBUS_MethodIterator +{ + /* linked list */ + struct GNUNET_DBUS_MethodIterator *next; + struct GNUNET_DBUS_MethodIterator *prev; + + struct GNUNET_DBUS_Method *method; +}; + +/** + * Create a new GNUNET_DBUS_Method with the given name. The underlying_method + * function will be called whenever this method is called via DBus. After + * creating a method you need to populate it the args and return args before + * binding it to an interface with GNUNET_DBUS_interface_add_method. + */ +struct GNUNET_DBUS_Method * +GNUNET_DBUS_method_create ( + const char *name, + void (*underlying_method)(struct GNUNET_DBUS_MethodContext *mc)); + +/** + * Increase the reference count of this GNUNET_DBUS_Method by one. + */ +void +GNUNET_DBUS_method_ref ( + struct GNUNET_DBUS_Method *method); + +/** + * Decrease the reference count of this GNUNET_DBUS_Method by one. Will free + * the method if the reference count reaches zero. + */ +void +GNUNET_DBUS_method_unref ( + struct GNUNET_DBUS_Method *method); + +/** + * Add an argument with the given name and type signature to this method. + */ +void +GNUNET_DBUS_method_add_arg ( + struct GNUNET_DBUS_Method *method, + const char *name, + const char *signature); + +/** + * Add a return argument with the given name and type signature to this method. + */ +void +GNUNET_DBUS_method_add_return_arg ( + struct GNUNET_DBUS_Method *method, + const char *name, + const char *signature); + +/** + * Get the name of this method. + */ +const char * +GNUNET_DBUS_method_get_name ( + const struct GNUNET_DBUS_Method *method); + +/** + * Iterate over the arguments of this GNUNET_DBUS_Method. + */ +const struct GNUNET_DBUS_ArgIterator * +GNUNET_DBUS_method_iterate_args ( + const struct GNUNET_DBUS_Method *method); +/* + * Iterate over the return arguments of this GNUNET_DBUS_Method. + */ +const struct GNUNET_DBUS_ArgIterator * +GNUNET_DBUS_method_iterate_return_args ( + const struct GNUNET_DBUS_Method *method); + +/** + * Call this method with the supplied GNUNET_DBUS_MethodContext. + */ +void +GNUNET_DBUS_method_call ( + struct GNUNET_DBUS_Method *method, + struct GNUNET_DBUS_MethodContext *mc); + +/** + * Find a GNUNET_DBUS_Method with the given name in the supplied iterator. + * Returns the position in the iterator of the method or NULL if it could not + * be found. + */ +const struct GNUNET_DBUS_MethodIterator * +GNUNET_DBUS_method_find ( + const struct GNUNET_DBUS_MethodIterator *meth_it, + const char *name); + +#endif + diff --git a/src/lib/include/gnunet_dbus_lib_method_context.h b/src/lib/include/gnunet_dbus_lib_method_context.h new file mode 100644 index 0000000..27b5789 --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib_method_context.h @@ -0,0 +1,115 @@ +#ifndef GNUNET_DBUS_LIB_METHOD_CONTEXT_H +#define GNUNET_DBUS_LIB_METHOD_CONTEXT_H + +#include + +#include + +/** + * An object of this type is passed to every function bound to a + * GNUNET_DBUS_Method when it is called via DBus. + */ +struct GNUNET_DBUS_MethodContext +{ + /** + * The client calling this method. + */ + struct GNUNET_DBUS_Client *client; + + /** + * The service that this method was called on. This is necessary as the + * process might be running several DBus services all advertising the same + * method. + */ + struct GNUNET_DBUS_Service *service; + + /** + * The object that this method was called on. + */ + struct GNUNET_DBUS_Object *object; + + /** + * The interface that this method was called on. + */ + struct GNUNET_DBUS_Interface *interface; + + /** + * The method that was called. Necessary because multiple DBus methods may be + * bound to a single internal function. + */ + struct GNUNET_DBUS_Method *method; + + /** + * The method-call message sent by the client. + */ + DBusMessage *message; + + /** + * Whether the client expects a reply to this message. If the user of this + * API tries to send a reply to the client via this + * GNUNET_DBUS_MethodContext, the reply will not be sent if this flag is + * unset. + */ + bool expects_reply; + + /** + * Whether we have already replied to this DBus method-call message. + */ + bool replied; + + /** + * The reference count of the GNUNET_DBUS_MethodContext. + */ + unsigned ref_count; +}; + +/** + * Create a GNUNET_DBUS_MethodContext from the supplied DBus method-call + * message. The message must have been sent by the given client to the given + * service+object+interface+method. This will increase the reference count of + * the DBusMessage but not the other supplied objects. + */ +struct GNUNET_DBUS_MethodContext * +GNUNET_DBUS_method_context_create ( + struct GNUNET_DBUS_Client *client, + struct GNUNET_DBUS_Service *service, + struct GNUNET_DBUS_Object *object, + struct GNUNET_DBUS_Interface *interface, + struct GNUNET_DBUS_Method *method, + DBusMessage *message); + +/** + * Increase the reference count of this GNUNET_DBUS_MethodContext by one. + */ +void +GNUNET_DBUS_method_context_ref ( + struct GNUNET_DBUS_MethodContext *mc); + +/** + * Decrease the reference count of this GNUNET_DBUS_MethodContext by one. Will + * free the method context if the reference count reaches zero. + */ +void +GNUNET_DBUS_method_context_unref ( + struct GNUNET_DBUS_MethodContext *mc); + +/** + * Send a DBusMessage in reply to the method call associated with this method + * context. The DBusMessage should have been created using + * GNUNET_DBUS_method_context_create_reply. + */ +void +GNUNET_DBUS_method_context_send_reply ( + struct GNUNET_DBUS_MethodContext *mc, + DBusMessage *reply); + +/** + * Create a DBusMessage in reply to this method call. After populating the + * message with data, send it using GNUNET_DBUS_method_context_send_reply. + */ +DBusMessage * +GNUNET_DBUS_method_context_create_reply ( + struct GNUNET_DBUS_MethodContext *mc); + +#endif + diff --git a/src/lib/include/gnunet_dbus_lib_object.h b/src/lib/include/gnunet_dbus_lib_object.h new file mode 100644 index 0000000..3264494 --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib_object.h @@ -0,0 +1,125 @@ +#ifndef GNUNET_DBUS_LIB_OBJECT_H +#define GNUNET_DBUS_LIB_OBJECT_H + +/** + * Represents a DBus object advertised on a DBus service. Objects are named and + * exist in a tree. For example, the object at the path + * /org/freedesktop/PolicyKit1/Authority is named Authority and sits in a + * heirarchy below four other objects including the service's root object. Each + * object has a set of interfaces and can be associated with an arbitrary piece + * of data. + */ +struct GNUNET_DBUS_Object; + +#include "gnunet_dbus_lib_interface.h" + +/** + * An iterable, doubly-linked-list of GNUNET_DBUS_Object. + */ +struct GNUNET_DBUS_ObjectIterator +{ + /* linked list */ + struct GNUNET_DBUS_ObjectIterator *next; + struct GNUNET_DBUS_ObjectIterator *prev; + + struct GNUNET_DBUS_Object *object; +}; + +/** + * Create a new GNUNET_DBUS_Object with the given name and associated data. For + * example, the object that exists at the path + * /org/freedesktop/PolicyKit1/Authority has the name Authority and sits below + * four other objects including the service's root object. + */ +struct GNUNET_DBUS_Object * +GNUNET_DBUS_object_create ( + const char *name, + void *data); + +/** + * Increase the reference count of this GNUNET_DBUS_Object by one. + */ +void +GNUNET_DBUS_object_ref ( + struct GNUNET_DBUS_Object *object); + +/** + * Decrease the reference count of this GNUNET_DBUS_Object by one. Will free + * the GNUNET_DBUS_Object if the reference count reaches zero. + */ +void +GNUNET_DBUS_object_unref ( + struct GNUNET_DBUS_Object *object); + +/** + * Get the name of this GNUNET_DBUS_Object. + */ +const char * +GNUNET_DBUS_object_get_name ( + const struct GNUNET_DBUS_Object *object); + +/** + * Add an interface to this GNUNET_DBUS_Object. + */ +void +GNUNET_DBUS_object_add_interface ( + struct GNUNET_DBUS_Object *object, + struct GNUNET_DBUS_Interface *interface); + +/** + * Iterate over the interfaces of this GNUNET_DBUS_Object. + */ +const struct GNUNET_DBUS_InterfaceIterator * +GNUNET_DBUS_object_iterate_interfaces ( + struct GNUNET_DBUS_Object *object); + +/** + * Get the data associated with this GNUNET_DBUS_Object. + */ +void * +GNUNET_DBUS_object_get_data ( + struct GNUNET_DBUS_Object *object); + +/** + * Create an object with a randomly generated name, associate data with it, and + * add it as a child to the given object. Returns the newly created object. + */ +struct GNUNET_DBUS_Object * +GNUNET_DBUS_object_create_uniquely_named_subobject ( + struct GNUNET_DBUS_Object *object, + void *data); + +/** + * Add subobject as a child of object. + */ +void +GNUNET_DBUS_object_add_subobject ( + struct GNUNET_DBUS_Object *object, + struct GNUNET_DBUS_Object *subobject); + +/** + * Remove the object pointed to by subobject_it as a child from object. + */ +void +GNUNET_DBUS_object_remove_subobject ( + struct GNUNET_DBUS_Object *object, + struct GNUNET_DBUS_ObjectIterator *subobject_it); + +/** + * Iterate over an object's child objects. + */ +struct GNUNET_DBUS_ObjectIterator * +GNUNET_DBUS_object_iterate_subobjects ( + const struct GNUNET_DBUS_Object *object); + +/** + * Search the given linked list of GNUNET_DBUS_Object for an object with the + * given name and return an iterator to it. + */ +const struct GNUNET_DBUS_ObjectIterator * +GNUNET_DBUS_object_find ( + const struct GNUNET_DBUS_ObjectIterator *object_it, + const char *name); + +#endif + diff --git a/src/lib/include/gnunet_dbus_lib_object_path.h b/src/lib/include/gnunet_dbus_lib_object_path.h new file mode 100644 index 0000000..ec93d99 --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib_object_path.h @@ -0,0 +1,62 @@ +#ifndef GNUNET_DBUS_LIB_OBJECT_PATH_H +#define GNUNET_DBUS_LIB_OBJECT_PATH_H + +#include "gnunet_dbus_lib_object.h" + +/** + * Represents a path of DBus objects in service's object namespace. eg. + * /org/freedesktop/PolicyKit1/Authority + */ +struct GNUNET_DBUS_ObjectPath; + +/** + * Create an object path from a GNUNET_DBUS_Service and a list of + * GNUNET_DBUS_Object. The last item in the list must be NULL. + * For example, to create the path to the + * /org/freedesktop/PolicyKit1/Authority1 object you would call as: + * + * GNUNET_DBUS_object_path_create(policy_kit_service_object, + * org_object, + * freedesktop_object, + * PolicyKit1_object, + * Authority_object, + * NULL); + */ +struct GNUNET_DBUS_ObjectPath * +GNUNET_DBUS_object_path_create ( + struct GNUNET_DBUS_Service *service, + ...) + __attribute__((sentinel)); + +/** + * Increase the reference count of this GNUNET_DBUS_ObjectPath by one. + */ +void +GNUNET_DBUS_object_path_ref ( + struct GNUNET_DBUS_ObjectPath *path); + +/** + * Decrease the reference count of this GNUNET_DBUS_ObjectPath by one. Will + * free the GNUNET_DBUS_ObjectPath if the reference count reaches zero. + */ +void +GNUNET_DBUS_object_path_unref ( + struct GNUNET_DBUS_ObjectPath *path); + +/** + * Format the GNUNET_DBUS_ObjectPath as a string. eg. + * "/org/freedesktop/PolicyKit1/Authority1". + */ +char * +GNUNET_DBUS_object_path_to_string ( + const struct GNUNET_DBUS_ObjectPath *path); + +/** + * Get the destination object of this path. ie. the final object in the path. + */ +struct GNUNET_DBUS_Object * +GNUNET_DBUS_object_path_get_destination ( + const struct GNUNET_DBUS_ObjectPath *path); + +#endif + diff --git a/src/lib/include/gnunet_dbus_lib_pop.h b/src/lib/include/gnunet_dbus_lib_pop.h new file mode 100644 index 0000000..6ffde70 --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib_pop.h @@ -0,0 +1,408 @@ +#ifndef GNUNET_DBUS_LIB_POP_H +#define GNUNET_DBUS_LIB_POP_H + +/** + * Pop a byte from a DBus message, advancing the iterator. `arg_name` is used + * for reporting useful diagnostics if the value in the message is missing or + * the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_byte ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + unsigned char *value); + +/** + * Pop a boolean from a DBus message, advancing the iterator. `arg_name` is + * used for reporting useful diagnostics if the value in the message is missing + * or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_boolean ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_bool_t *value); + +/** + * Pop an int16 from a DBus message, advancing the iterator. `arg_name` is used + * for reporting useful diagnostics if the value in the message is missing or + * the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_int16 ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_int16_t *value); + +/** + * Pop a uint16 from a DBus message, advancing the iterator. `arg_name` is used + * for reporting useful diagnostics if the value in the message is missing or + * the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_uint16 ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_uint16_t *value); + +/** + * Pop an int32 from a DBus message, advancing the iterator. `arg_name` is + * used for reporting useful diagnostics if the value in the message is missing + * or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_int32 ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_int32_t *value); + +/** + * Pop a uint32 from a DBus message, advancing the iterator. `arg_name` is + * used for reporting useful diagnostics if the value in the message is missing + * or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_uint32 ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_uint32_t *value); + +/** + * Pop an int64 from a DBus message, advancing the iterator. `arg_name` is + * used for reporting useful diagnostics if the value in the message is missing + * or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_int64 ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_int64_t *value); + +/** + * Pop a uint64 from a DBus message, advancing the iterator. `arg_name` is + * used for reporting useful diagnostics if the value in the message is missing + * or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_uint64 ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + dbus_uint64_t *value); + +/** + * Pop a double from a DBus message, advancing the iterator. `arg_name` is used + * for reporting useful diagnostics if the value in the message is missing or + * the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_double ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + double *value); + +/** + * Pop a string from a DBus message, advancing the iterator. `arg_name` is used + * for reporting useful diagnostics if the value in the message is missing or + * the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_string ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const char **value); + +/** + * Pop a DBus object path from a DBus message, advancing the iterator. The + * object path is returned in *value as a string. `arg_name` is used for + * reporting useful diagnostics if the value in the message is missing or the + * wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_object_path ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const char **value); + +/** + * Pop a DBus type signature from a DBusMessage, advancing the iterator. The + * type signature is returned in *value as a string. `arg_name` is used for + * reporting useful diagnostics if the value in the message is missing or the + * wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_signature ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const char **value); + +/** + * Pop a unix file descriptor from a DBusMessage, advancing the iterator. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_unix_fd ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + int *value); + +/** + * Pop an array from a DBusMessage. Moves iter past the array and initialises + * iter_sub to point to the first element of the array. The length of the array + * is returned in *len. `arg_name` is used for reporting useful diagnostics if + * the value in the message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_enter_array ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + const char *arg_name, + size_t *len); + +/** + * Pop a struct from a DBusMessage. Moves iter past the struct and initialises + * iter_sub to point to the first element of the struct. `arg_name` is used for + * reporting useful diagnostics if the value in the message is missing or the + * wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_enter_struct ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + const char *arg_name); + +/** + * Pop a variant from a DBusMessage. Moves iter past the variant and + * initialises iter_sub to point to the value inside the variant. `arg_name` + * is used for reporting useful diagnostics if the value in the message is + * missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_enter_variant ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + const char *arg_name); + +/** + * Pop a dictionary entry from a DBusMessage. Moves iter past the dictionary + * entry and initialises iter_sub to point to the key. The key and value can + * then be popped in sequence using iter_sub. `arg_name` is used for reporting + * useful diagnostics if the value in the message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_enter_dict_entry ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + const char *arg_name); + +/** + * Pop an array of bytes from a DBusMessage. A pointer to the array is stored + * in *value. The array is not copied so *value will become invalid once the + * message has been freed. The length of the array is stored in *n_elements. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_byte_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const unsigned char **value, + int *n_elements); + +/** + * Pop an array of booleans from a DBusMessage. A pointer to the array is + * stored in *value. The array is not copied so *value will become invalid once + * the message has been freed. The length of the array is stored in + * *n_elements. `arg_name` is used for reporting useful diagnostics if the + * value in the message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_boolean_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const dbus_bool_t **value, + int *n_elements); + +/** + * Pop an array of int16s from a DBusMessage. A pointer to the array is stored + * in *value. The array is not copied so *value will become invalid once the + * message has been freed. The length of the array is stored in *n_elements. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_int16_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const int16_t **value, + int *n_elements); + +/** + * Pop an array of uint16s from a DBusMessage. A pointer to the array is stored + * in *value. The array is not copied so *value will become invalid once the + * message has been freed. The length of the array is stored in *n_elements. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_uint16_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const uint16_t **value, + int *n_elements); + +/** + * Pop an array of int32s from a DBusMessage. A pointer to the array is stored + * in *value. The array is not copied so *value will become invalid once the + * message has been freed. The length of the array is stored in *n_elements. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_int32_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const int32_t **value, + int *n_elements); + +/** + * Pop an array of uint32s from a DBusMessage. A pointer to the array is stored + * in *value. The array is not copied so *value will become invalid once the + * message has been freed. The length of the array is stored in *n_elements. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_uint32_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const uint32_t **value, + int *n_elements); + +/** + * Pop an array of int64s from a DBusMessage. A pointer to the array is stored + * in *value. The array is not copied so *value will become invalid once the + * message has been freed. The length of the array is stored in *n_elements. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_int64_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const int64_t **value, + int *n_elements); + +/** + * Pop an array of uint64s from a DBusMessage. A pointer to the array is stored + * in *value. The array is not copied so *value will become invalid once the + * message has been freed. The length of the array is stored in *n_elements. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_uint64_array ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + const uint64_t **value, + int *n_elements); + +/* + * Pop a bitfield enum from a DBusMessage, advancing the iterator. A bitfield + * enum is an enum where the individual bits represent different flags. See for + * example the definition of GNUNET_GNSRECORD_Flags: + * + * enum GNUNET_GNSRECORD_Flags { + * GNUNET_GNSRECORD_RF_PRIVATE = 2, + * // GNUNET_GNSRECORD_RF_UNUSED = 4, + * GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION = 8, + * GNUNET_GNSRECORD_RF_SHADOW_RECORD = 16 + * } + * + * Bitfield enums can be sent across the wire in either their integer form (as + * a uint32) or as an array of strings. Sending an int is more efficient and is + * what libraries built around the DBus API would use. People using the DBus + * API directly via the command line or a DBus debugger may want to pass + * human-readable strings for convenience. names is a pointer to a + * {NULL, 0}-terminated array of GNUNET_DBUS_StringEnumPair which assigns + * human-readable names to the bitfields. `arg_name` is used for reporting + * useful diagnostics if the value in the message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_bitfield ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + int *value, + const struct GNUNET_DBUS_StringEnumPair *fields); + +/** + * Pop an enum from a DBusMessage, advancing the iterator. enums can be sent + * across the wire as either a uint32 or as a string. Sending a uint32 is more + * efficient and is what libraries built around the DBus API would use. People + * using the DBus API directly via the command line or a DBus debugger may want + * to pass human-readable strings for convenience. names is a pointer to a + * {NULL, 0}-terminated array of GNUNET_DBUS_StringEnumPair which assigns + * human-readable names to the different enum variants. `arg_name` is used for + * reporting useful diagnostics if the value in the message is missing or the + * wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_enum ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + int *value, + const struct GNUNET_DBUS_StringEnumPair *names); + +/** + * Pop a GNUNET_HashCode from a DBusMessage, advancing the iterator. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_hashcode ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + struct GNUNET_HashCode *value); + +/** + * Pop a GNUNET_PeerIdentity from a DBusMessage, advancing the iterator. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DBUS_pop_peer_identity ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + struct GNUNET_PeerIdentity *value); + +#endif + diff --git a/src/lib/include/gnunet_dbus_lib_push.h b/src/lib/include/gnunet_dbus_lib_push.h new file mode 100644 index 0000000..31a9bc9 --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib_push.h @@ -0,0 +1,397 @@ +#ifndef GNUNET_DBUS_LIB_PUSH_H +#define GNUNET_DBUS_LIB_PUSH_H + +#include +#include +#include +#include + +/** + * Push a byte to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_byte ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value); + +/** + * Push a boolean to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_boolean ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_bool_t *value); + +/** + * Push an int16 to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_int16 ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_int16_t *value); + +/** + * Push a uint16 to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_uint16 ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_uint16_t *value); + +/** + * Push an int32 to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_int32 ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_int32_t *value); + +/** + * Push a uint32 to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_uint32 ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_uint32_t *value); + +/** + * Push an int64 to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_int64 ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_int64_t *value); + +/** + * Push a uint64 to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_uint64 ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_uint64_t *value); + +/** + * Push a double to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_double ( + DBusMessage *message, + DBusMessageIter *iter, + const double *value); + +/** + * Push a string to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_string ( + DBusMessage *message, + DBusMessageIter *iter, + const char *const *value); + +/** + * Push an object path to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_object_path ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_DBUS_ObjectPath *path); + +/** + * Push a DBus type signature to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_signature ( + DBusMessage *message, + DBusMessageIter *iter, + const char *const *value); + +/** + * Push a unix file descriptor to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_unix_fd ( + DBusMessage *message, + DBusMessageIter *iter, + const int *value); + +/** + * Push a unix file descriptor to a DBus message, advancing the iterator. + */ +void +GNUNET_DBUS_push_open_array ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + const char *signature); + +/** + * Commence pushing an array to a DBusMessage. iter_sub is initialised to point + * to the start of a new array and can be used to populate the array. + * GNUNET_DBUS_push_close_array must be called after the array has been + * populated. iter is invalid until GNUNET_DBUS_push_close_array has been called. + */ +void +GNUNET_DBUS_push_open_struct ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub); + +/** + * Commence pushing a variant to a DBusMessage. iter_sub is initialised to + * point to an empty variant and must be used to place the value in the + * variant. After the value is placed, GNUNET_DBUS_push_close_variant must be + * called. iter is invalid until GNUNET_DBUS_push_close_array has been called. + */ +void +GNUNET_DBUS_push_open_variant ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub, + const char *signature); + +/** + * Commence pushing a dictionary entry onto a DBusMessage. iter_sub is + * initialised to point to the start of the new dictionary entry. It must be + * used to push the key and value in order after which + * GNUNET_DBUS_push_close_dict_entry must be called. iter is invalid until + * GNUNET_DBUS_push_close_array has been called. + */ +void +GNUNET_DBUS_push_open_dict_entry ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub); + +/** + * Finish pushing an array to a DBusMesage. This must be called after + * GNUNET_DBUS_push_open_array and after the array elements have been pushed to + * the message. After calling this function, iter_sub becomes invalid and iter + * can again be used to push data to the message. + */ +void +GNUNET_DBUS_push_close_array ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub); + +/** + * Finish pushing a struct to a DBusMessage. This must be called after + * GNUNET_DBUS_push_open_struct and after the struct members have been pushed + * to the message. iter is invalid until GNUNET_DBUS_push_close_array has been + * called. After calling this function, iter_sub becomes invalid and iter can + * again be used to push data to the message. + */ +void +GNUNET_DBUS_push_close_struct ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub); + +/** + * Finish pushing a variant to a DBusMessage. This must be called after + * GNUNET_DBUS_push_open_variant and after the variant's value has been pushed + * to the message. After calling this function, iter_sub becomes invalid and + * iter can again be used to push data to the message. + */ +void +GNUNET_DBUS_push_close_variant ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub); + +/** + * Finish pushing a dictionary entry to a DBusMessage. This must be called + * after GNUNET_DBUS_push_open_dict_entry and after the key and value have been + * pushed to the message. After calling this function, iter_sub becomes invalid + * and iter can again be used to push data to the message. + */ +void +GNUNET_DBUS_push_close_dict_entry ( + DBusMessage *message, + DBusMessageIter *iter, + DBusMessageIter *iter_sub); + +/** + * Push an array of bytes to a DBusMessage, advancing the iterator. The values + * are copied to the message. value_count is the number of elements in the array. + */ +void +GNUNET_DBUS_push_byte_array ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value, + size_t value_count); + +/** + * Push an array of booleans to a DBusMessage, advancing the iterator. The values + * are copied to the message. value_count is the number of elements in the array. + */ +void +GNUNET_DBUS_push_boolean_array ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_bool_t *value, + size_t value_count); + +/** + * Push an array of int16s to a DBusMessage, advancing the iterator. The values + * are copied to the message. value_count is the number of elements in the array. + */ +void +GNUNET_DBUS_push_int16_array ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_int16_t *value, + size_t value_count); + +/** + * Push an array of uint16s to a DBusMessage, advancing the iterator. The values + * are copied to the message. value_count is the number of elements in the array. + */ +void +GNUNET_DBUS_push_uint16_array ( + DBusMessage *message, + DBusMessageIter *iter, + const dbus_uint16_t *value, + size_t value_count); + +/** + * Push an array of int32s to a DBusMessage, advancing the iterator. The values + * are copied to the message. value_count is the number of elements in the array. + */ +void +GNUNET_DBUS_push_int32_array ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value, + size_t value_count); + +/** + * Push an array of uint32s to a DBusMessage, advancing the iterator. The values + * are copied to the message. value_count is the number of elements in the array. + */ +void +GNUNET_DBUS_push_uint32_array ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value, + size_t value_count); + +/** + * Push an array of int64s to a DBusMessage, advancing the iterator. The values + * are copied to the message. value_count is the number of elements in the array. + */ +void +GNUNET_DBUS_push_int64_array ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value, + size_t value_count); + +/** + * Push an array of uint64s to a DBusMessage, advancing the iterator. The values + * are copied to the message. value_count is the number of elements in the array. + */ +void +GNUNET_DBUS_push_uint64_array ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value, + size_t value_count); + +/** + * Push an array of doubles to a DBusMessage, advancing the iterator. The values + * are copied to the message. value_count is the number of elements in the array. + */ +void +GNUNET_DBUS_push_double_array ( + DBusMessage *message, + DBusMessageIter *iter, + const unsigned char *value, + size_t value_count); + +/** + * Push binary data to a DBusMessage, advancing the iterator. The data is + * copied to the message. value_count is the number of bytes of data. The data + * may be represented on-the-wire as either an array of bytes or a base32 + * encoded string depending on the pretty_encoding flag of the message. + */ +void +GNUNET_DBUS_push_data ( + DBusMessage *message, + DBusMessageIter *iter, + const void *value, + size_t value_size); + +/* + * Push a bitfield enum to a DBusMessage, advancing the iterator. A bitfield + * enum is an enum where the individual bits represent different flags. See for + * example the definition of GNUNET_GNSRECORD_Flags: + * + * enum GNUNET_GNSRECORD_Flags { GNUNET_GNSRECORD_RF_PRIVATE = 2, // + * GNUNET_GNSRECORD_RF_UNUSED = 4, GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION = + * 8, GNUNET_GNSRECORD_RF_SHADOW_RECORD = 16 } + * + * Bitfield enums can be sent across the wire in either their integer form (as + * a uint32) or as an array of strings depending on the pretty_encoding flag of + * the message. Sending an int is more efficient and is what libraries built + * around the DBus API would use. People using the DBus API directly via the + * command line or a DBus debugger may prefer to receive human-readable strings + * for convenience. names is a pointer to a {NULL, 0}-terminated array of + * GNUNET_DBUS_StringEnumPair which assigns human-readable names to the + * bitfields. + */ +void +GNUNET_DBUS_push_bitfield ( + DBusMessage *message, + DBusMessageIter *iter, + const int *value, + const struct GNUNET_DBUS_StringEnumPair *fields); + +/** + * Push an enum to a DBusMessage, advancing the iterator. enums can be sent + * across the wire as either a uint32 or as a string depending on the + * pretty_encoding flag of the message. Sending a uint32 is more efficient and + * is what libraries built around the DBus API would use. People using the DBus + * API directly via the command line or a DBus debugger may prefer to receive + * human-readable strings for convenience. names is a pointer to a + * {NULL, 0}-terminated array of GNUNET_DBUS_StringEnumPair which assigns + * human-readable names to the different enum variants. + */ +void +GNUNET_DBUS_push_enum ( + DBusMessage *message, + DBusMessageIter *iter, + const int *value, + const struct GNUNET_DBUS_StringEnumPair *names); + +/** + * Push a GNUNET_HashCode to a DBusMessage, advancing the iterator. + */ +void +GNUNET_DBUS_push_hashcode ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_HashCode *value); + +/** + * Push a GNUNET_PeerIdentity to a DBusMessage, advancing the iterator. + */ +void +GNUNET_DBUS_push_peer_identity ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_PeerIdentity *value); + +#endif + + diff --git a/src/lib/include/gnunet_dbus_lib_service.h b/src/lib/include/gnunet_dbus_lib_service.h new file mode 100644 index 0000000..4d88079 --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib_service.h @@ -0,0 +1,87 @@ +#ifndef GNUNET_DBUS_LIB_SERVICE_H +#define GNUNET_DBUS_LIB_SERVICE_H + +#include +#include +#include + +/** + * Represents a DBus service that we are running. + */ +struct GNUNET_DBUS_Service; + +#include "gnunet_dbus_lib_object.h" +#include "gnunet_dbus_lib_client.h" + +/** + * The type of callbacks used to handle newly-connecting clients. + */ +typedef void (*GNUNET_DBUS_ClientConnectsHandler)( + struct GNUNET_DBUS_Service *service, + struct GNUNET_DBUS_Client *client); + +/** + * The type of callbacks used to handle clients disconnecting. + */ +typedef void (*GNUNET_DBUS_ClientDisconnectsHandler)( + struct GNUNET_DBUS_Service *service, + struct GNUNET_DBUS_Client *client); + +/** + * Create a service with the given name and configuration. The service will + * start running immediately in GNUnet's scheduler. + */ +struct GNUNET_DBUS_Service * +GNUNET_DBUS_service_create ( + const struct GNUNET_CONFIGURATION_Handle *cfg, + const char *name); + +/** + * Increase the reference count of this GNUNET_DBUS_Service by one. + */ +void +GNUNET_DBUS_service_ref ( + struct GNUNET_DBUS_Service *service); + +/** + * Decrease the reference count of this GNUNET_DBUS_Service by one. Will free + * the service if the reference count reaches zero. + */ +void +GNUNET_DBUS_service_unref ( + struct GNUNET_DBUS_Service *service); + +/** + * Get a reference to this GNUNET_DBUS_Service's configuration. + */ +const struct GNUNET_CONFIGURATION_Handle * +GNUNET_DBUS_service_get_config ( + struct GNUNET_DBUS_Service *service); + +/** + * Send an arbitrary message from this service. + */ +void +GNUNET_DBUS_service_send ( + struct GNUNET_DBUS_Service *service, + DBusMessage *dbus_message); + +/** + * Set the callbacks used by this service to handle clients connecting and + * disconnecting. + */ +void +GNUNET_DBUS_service_set_client_handlers ( + struct GNUNET_DBUS_Service *service, + GNUNET_DBUS_ClientConnectsHandler client_connects, + GNUNET_DBUS_ClientDisconnectsHandler client_disconnects); + +/** + * Get this GNUNET_DBUS_Service's root object (a.k.a. '/') + */ +struct GNUNET_DBUS_Object * +GNUNET_DBUS_service_get_root_object ( + struct GNUNET_DBUS_Service *service); + +#endif + diff --git a/src/lib/include/gnunet_dbus_lib_signal.h b/src/lib/include/gnunet_dbus_lib_signal.h new file mode 100644 index 0000000..af314fe --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib_signal.h @@ -0,0 +1,102 @@ +#ifndef GNUNET_DBUS_LIB_SIGNAL_H +#define GNUNET_DBUS_LIB_SIGNAL_H + +/** + * Defines a DBus signal that may be sent from an interface. + */ +struct GNUNET_DBUS_Signal; + +#include "gnunet_dbus_lib_object_path.h" +#include "gnunet_dbus_lib_interface.h" + +/** + * An iterable, double-linked-list of GNUNET_DBUS_Signal. + */ +struct GNUNET_DBUS_SignalIterator +{ + /* linked list */ + struct GNUNET_DBUS_SignalIterator *next; + struct GNUNET_DBUS_SignalIterator *prev; + + struct GNUNET_DBUS_Signal *signal; +}; + +/** + * Create a signal with the given name. The signal should be populated with + * argument definitions then bound to an interface with + * GNUNET_DBUS_interface_add_signal. + */ +struct GNUNET_DBUS_Signal * +GNUNET_DBUS_signal_create ( + const char *name); + +/** + * Increase the reference count of this GNUNET_DBUS_Signal by one. + */ +void +GNUNET_DBUS_signal_ref ( + struct GNUNET_DBUS_Signal *signal); + +/** + * Decrease the reference count of this GNUNET_DBUS_Signal by one. Will free + * the GNUNET_DBUS_Signal if the reference count reaches zero. + */ +void +GNUNET_DBUS_signal_unref ( + struct GNUNET_DBUS_Signal *signal); + +/** + * Adds an argument definition to this GNUNET_DBUS_Signal given both it's name + * and it's DBus type signature. + */ +void +GNUNET_DBUS_signal_add_arg ( + struct GNUNET_DBUS_Signal *signal, + const char *name, + const char *signature); + +/** + * Get the name of this GNUNET_DBUS_Signal. + */ +const char * +GNUNET_DBUS_signal_get_name ( + const struct GNUNET_DBUS_Signal *signal); + +/** + * Iterate over the arguments of this GNUNET_DBUS_Signal. + */ +const struct GNUNET_DBUS_ArgIterator * +GNUNET_DBUS_signal_iterate_args ( + const struct GNUNET_DBUS_Signal *signal); + +/** + * Instantiate the signal so that it can be sent to all interested peers on the + * bus. The returned DBusMessage must be populated with the signal's arguments + * and can then be sent to send the signal. The signal will originate from the + * supplied object path and interface. pretty sets whether data pushed to the + * returned DBusMessage will be prety-encoded. + */ +DBusMessage * +GNUNET_DBUS_signal_spawn ( + const struct GNUNET_DBUS_Signal *signal, + const struct GNUNET_DBUS_ObjectPath *path, + const struct GNUNET_DBUS_Interface *interface, + bool pretty); + +/** + * Instantiate the signal so that it can be sent to a single client. The + * returned DBusMessage must be populated with the signal's arguments and can + * then be sent to send the signal. The signal will originate from the supplied + * object path and interface. pretty sets whether data pushed to the returned + * DBusMessage will be prety-encoded. + */ +DBusMessage * +GNUNET_DBUS_signal_spawn_unicast ( + const struct GNUNET_DBUS_Signal *signal, + const struct GNUNET_DBUS_ObjectPath *path, + const struct GNUNET_DBUS_Interface *interface, + const struct GNUNET_DBUS_Client *client, + bool pretty); + +#endif + diff --git a/src/lib/include/gnunet_dbus_lib_signature.h b/src/lib/include/gnunet_dbus_lib_signature.h new file mode 100644 index 0000000..b5ebc84 --- /dev/null +++ b/src/lib/include/gnunet_dbus_lib_signature.h @@ -0,0 +1,42 @@ +#ifndef GNUNET_DBUS_LIB_SIGNATURE_H +#define GNUNET_DBUS_LIB_SIGNATURE_H + +#include +#include "gnunet_crypto_dbus_lib.h" + +#define GNUNET_DBUS_SIGNATURE_BYTE DBUS_TYPE_BYTE_AS_STRING +#define GNUNET_DBUS_SIGNATURE_BOOLEAN DBUS_TYPE_BOOLEAN_AS_STRING +#define GNUNET_DBUS_SIGNATURE_INT16 DBUS_TYPE_INT16_AS_STRING +#define GNUNET_DBUS_SIGNATURE_UINT16 DBUS_TYPE_UINT16_AS_STRING +#define GNUNET_DBUS_SIGNATURE_INT32 DBUS_TYPE_INT32_AS_STRING +#define GNUNET_DBUS_SIGNATURE_UINT32 DBUS_TYPE_UINT32_AS_STRING +#define GNUNET_DBUS_SIGNATURE_INT64 DBUS_TYPE_INT64_AS_STRING +#define GNUNET_DBUS_SIGNATURE_UINT64 DBUS_TYPE_UINT64_AS_STRING +#define GNUNET_DBUS_SIGNATURE_DOUBLE DBUS_TYPE_DOUBLE_AS_STRING +#define GNUNET_DBUS_SIGNATURE_STRING DBUS_TYPE_STRING_AS_STRING +#define GNUNET_DBUS_SIGNATURE_OBJECT_PATH DBUS_TYPE_OBJECT_PATH_AS_STRING +#define GNUNET_DBUS_SIGNATURE_SIGNATURE DBUS_TYPE_SIGNATURE_AS_STRING +#define GNUNET_DBUS_SIGNATURE_UNIX_FD DBUS_TYPE_UNIX_FD_AS_STRING +#define GNUNET_DBUS_SIGNATURE_VARIANT DBUS_TYPE_VARIANT_AS_STRING + +#define GNUNET_DBUS_SIGNATURE_ARRAY(s) DBUS_TYPE_ARRAY_AS_STRING s +#define GNUNET_DBUS_SIGNATURE_DICT(s) DBUS_TYPE_ARRAY_AS_STRING DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING s DBUS_DICT_ENTRY_END_CHAR_AS_STRING +#define GNUNET_DBUS_SIGNATURE_STRUCT(s) DBUS_STRUCT_BEGIN_CHAR_AS_STRING s DBUS_STRUCT_END_CHAR_AS_STRING + +#define GNUNET_DBUS_SIGNATURE_BITFIELD GNUNET_DBUS_SIGNATURE_VARIANT +#define GNUNET_DBUS_SIGNATURE_ENUM GNUNET_DBUS_SIGNATURE_VARIANT + +#define GNUNET_DBUS_SIGNATURE_HASHCODE GNUNET_DBUS_SIGNATURE_VARIANT +#define GNUNET_DBUS_SIGNATURE_PEER_IDENTITY GNUNET_DBUS_SIGNATURE_STRUCT(GNUNET_CRYPTO_DBUS_SIGNATURE_EDDSA_PUBLIC_KEY) + +/** + * Formats the given DBus typecode as a string. For example, + * GNUNET_DBUS_signature_typecode_to_string(DBUS_TYPE_UINT32) will return the + * constant string "u" + */ +const char * +GNUNET_DBUS_signature_typecode_to_string ( + int typecode); + +#endif + diff --git a/src/lib/include/gnunet_dht_dbus_lib.h b/src/lib/include/gnunet_dht_dbus_lib.h new file mode 100644 index 0000000..0281f92 --- /dev/null +++ b/src/lib/include/gnunet_dht_dbus_lib.h @@ -0,0 +1,23 @@ +#ifndef GNUNET_DHT_DBUS_LIB_H +#define GNUNET_DHT_DBUS_LIB_H + +#include +#include +#include +#include + +#include + +/** + * A {NULL, 0}-terminated list of textual descriptions of + * `GNUNET_DHT_RouteOption` enum variants. Used for converting back-and-forth + * between `GNUNET_DHT_RouteOption` and `const char *` + */ +extern const struct GNUNET_DBUS_StringEnumPair GNUNET_DHT_DBUS_route_option_description[]; + +#include "gnunet_dht_dbus_lib_push.h" +#include "gnunet_dht_dbus_lib_pop.h" +#include "gnunet_dht_dbus_lib_signature.h" + +#endif + diff --git a/src/lib/include/gnunet_dht_dbus_lib_pop.h b/src/lib/include/gnunet_dht_dbus_lib_pop.h new file mode 100644 index 0000000..e69587b --- /dev/null +++ b/src/lib/include/gnunet_dht_dbus_lib_pop.h @@ -0,0 +1,19 @@ +#ifndef GNUNET_DHT_DBUS_LIB_POP_H +#define GNUNET_DHT_DBUS_LIB_POP_H + +#include "gnunet_dht_dbus_lib.h" + +/** + * Pop a GNUNET_DHT_RouteOption from a DBusMessage, advancing the iterator. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_DHT_DBUS_pop_route_option ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + enum GNUNET_DHT_RouteOption *value); + +#endif + diff --git a/src/lib/include/gnunet_dht_dbus_lib_push.h b/src/lib/include/gnunet_dht_dbus_lib_push.h new file mode 100644 index 0000000..44e83be --- /dev/null +++ b/src/lib/include/gnunet_dht_dbus_lib_push.h @@ -0,0 +1,16 @@ +#ifndef GNUNET_DHT_DBUS_LIB_PUSH_H +#define GNUNET_DHT_DBUS_LIB_PUSH_H + +#include "gnunet_dht_dbus_lib.h" + +/** + * Push a GNUNET_DHT_RouteOption to the DBusMessage, advancing the iterator. + */ +void +GNUNET_DHT_DBUS_push_route_option ( + DBusMessage *message, + DBusMessageIter *iter, + const enum GNUNET_DHT_RouteOption *value); + +#endif + diff --git a/src/lib/include/gnunet_dht_dbus_lib_signature.h b/src/lib/include/gnunet_dht_dbus_lib_signature.h new file mode 100644 index 0000000..d8f2092 --- /dev/null +++ b/src/lib/include/gnunet_dht_dbus_lib_signature.h @@ -0,0 +1,9 @@ +#ifndef GNUNET_DHT_DBUS_LIB_SIGNATURE_H +#define GNUNET_DHT_DBUS_LIB_SIGNATURE_H + +#include "gnunet_dbus_lib.h" + +#define GNUNET_DHT_DBUS_SIGNATURE_ROUTE_OPTION GNUNET_DBUS_SIGNATURE_VARIANT + +#endif + diff --git a/src/lib/include/gnunet_gnsrecord_dbus_lib.h b/src/lib/include/gnunet_gnsrecord_dbus_lib.h new file mode 100644 index 0000000..6f36c45 --- /dev/null +++ b/src/lib/include/gnunet_gnsrecord_dbus_lib.h @@ -0,0 +1,17 @@ +#ifndef GNUNET_GNSRECORD_DBUS_LIB_H +#define GNUNET_GNSRECORD_DBUS_LIB_H + +#include + +/** + * A {NULL, 0}-terminate array of textual descriptions of the different + * GNUNET_GNSRECORD_Flags flags. + */ +const struct GNUNET_DBUS_StringEnumPair *GNUNET_GNSRECORD_DBUS_describe_flags(); + +#include "gnunet_gnsrecord_dbus_lib_push.h" +#include "gnunet_gnsrecord_dbus_lib_pop.h" +#include "gnunet_gnsrecord_dbus_lib_signature.h" + +#endif + diff --git a/src/lib/include/gnunet_gnsrecord_dbus_lib_pop.h b/src/lib/include/gnunet_gnsrecord_dbus_lib_pop.h new file mode 100644 index 0000000..97eec2e --- /dev/null +++ b/src/lib/include/gnunet_gnsrecord_dbus_lib_pop.h @@ -0,0 +1,29 @@ +#ifndef GNUNET_GNSRECORD_DBUS_LIB_POP_H +#define GNUNET_GNSRECORD_DBUS_LIB_POP_H + +/** + * Pop a GNUNET_GNSRECORD_Type from a DBusMessage, advancing the iterator. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_GNSRECORD_DBUS_pop_type ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + uint32_t *value); + +/** + * Pop a GNUNET_GNSRECORD_Flags from a DBusMessage, advancing the iterator. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_GNSRECORD_DBUS_pop_flags ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + enum GNUNET_GNSRECORD_Flags *value); + +#endif + diff --git a/src/lib/include/gnunet_gnsrecord_dbus_lib_push.h b/src/lib/include/gnunet_gnsrecord_dbus_lib_push.h new file mode 100644 index 0000000..0fdfc6c --- /dev/null +++ b/src/lib/include/gnunet_gnsrecord_dbus_lib_push.h @@ -0,0 +1,38 @@ +#ifndef GNUNET_GNSRECORD_DBUS_LIB_PUSH_H +#define GNUNET_GNSRECORD_DBUS_LIB_PUSH_H + +#include +#include +#include + +#include "gnunet_gnsrecord_dbus_lib.h" + +/** + * Push a GNUNET_GNSRECORD_Type to a DBusMessage, advancing the iterator. + */ +void +GNUNET_GNSRECORD_DBUS_push_type ( + DBusMessage *message, + DBusMessageIter *iter, + const uint32_t *value); + +/** + * Push a GNUNET_GNSRECORD_Flags to a DBusMessage, advancing the iterator. + */ +void +GNUNET_GNSRECORD_DBUS_push_flags ( + DBusMessage *message, + DBusMessageIter *iter, + const enum GNUNET_GNSRECORD_Flags *value); + +/** + * Push a GNUNET_GNSRECORD_Data to a DBusMessage, advancing the iterator. + */ +void +GNUNET_GNSRECORD_DBUS_push_data ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_GNSRECORD_Data *value); + +#endif + diff --git a/src/lib/include/gnunet_gnsrecord_dbus_lib_signature.h b/src/lib/include/gnunet_gnsrecord_dbus_lib_signature.h new file mode 100644 index 0000000..bda353a --- /dev/null +++ b/src/lib/include/gnunet_gnsrecord_dbus_lib_signature.h @@ -0,0 +1,27 @@ +#ifndef GNUNET_GNSRECORD_DBUS_LIB_SIGNATURE_H +#define GNUNET_GNSRECORD_DBUS_LIB_SIGNATURE_H + +#include "gnunet_time_dbus_lib_signature.h" +#include "gnunet_crypto_dbus_lib_signature.h" + +#define GNUNET_GNSRECORD_DBUS_SIGNATURE_TYPE GNUNET_DBUS_SIGNATURE_VARIANT +#define GNUNET_GNSRECORD_DBUS_SIGNATURE_FLAGS GNUNET_DBUS_SIGNATURE_BITFIELD + +#define GNUNET_GNSRECORD_DBUS_SIGNATURE_DATA \ + GNUNET_DBUS_SIGNATURE_STRUCT ( \ + GNUNET_GNSRECORD_DBUS_SIGNATURE_TYPE \ + GNUNET_GNSRECORD_DBUS_SIGNATURE_FLAGS \ + GNUNET_DBUS_SIGNATURE_VARIANT \ + GNUNET_DBUS_SIGNATURE_VARIANT \ + ) + +#define GNUNET_GNSRECORD_DBUS_SIGNATURE_BLOCK \ + GNUNET_DBUS_SIGNATURE_STRUCT ( \ + GNUNET_CRYPTO_DBUS_SIGNATURE_ECDSA_SIGNATURE \ + GNUNET_CRYPTO_DBUS_SIGNATURE_ECDSA_PUBLIC_KEY \ + GNUNET_CRYPTO_DBUS_SIGNATURE_ECC_SIGNATURE_PURPOSE \ + GNUNET_TIME_DBUS_SIGNATURE_ABSOLUTE \ + ) + +#endif + diff --git a/src/lib/include/gnunet_time_dbus_lib.h b/src/lib/include/gnunet_time_dbus_lib.h new file mode 100644 index 0000000..73046e9 --- /dev/null +++ b/src/lib/include/gnunet_time_dbus_lib.h @@ -0,0 +1,11 @@ +#ifndef GNUNET_TIME_DBUS_LIB_H +#define GNUNET_TIME_DBUS_LIB_H + +#include + +#include "gnunet_time_dbus_lib_push.h" +#include "gnunet_time_dbus_lib_pop.h" +#include "gnunet_time_dbus_lib_signature.h" + +#endif + diff --git a/src/lib/include/gnunet_time_dbus_lib_pop.h b/src/lib/include/gnunet_time_dbus_lib_pop.h new file mode 100644 index 0000000..a1f0456 --- /dev/null +++ b/src/lib/include/gnunet_time_dbus_lib_pop.h @@ -0,0 +1,17 @@ +#ifndef GNUNET_TIME_DBUS_LIB_POP_H +#define GNUNET_TIME_DBUS_LIB_POP_H + +/** + * Pop a GNUNET_TIME_Absolute from a DBusMessage, advancing the iterator. + * `arg_name` is used for reporting useful diagnostics if the value in the + * message is missing or the wrong type. + */ +DBusMessage * +GNUNET_TIME_DBUS_pop_absolute ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + struct GNUNET_TIME_Absolute *value); + +#endif + diff --git a/src/lib/include/gnunet_time_dbus_lib_push.h b/src/lib/include/gnunet_time_dbus_lib_push.h new file mode 100644 index 0000000..8469e31 --- /dev/null +++ b/src/lib/include/gnunet_time_dbus_lib_push.h @@ -0,0 +1,23 @@ +#ifndef GNUNET_TIME_DBUS_LIB_PUSH_H +#define GNUNET_TIME_DBUS_LIB_PUSH_H + +/** + * Push a GNUNET_TIME_Absolute to DBusMessage, advancing the iterator. + */ +void +GNUNET_TIME_DBUS_push_absolute ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_TIME_Absolute *value); + +/** + * Push a GNUNET_TIME_Relative to DBusMessage, advancing the iterator. + */ +void +GNUNET_TIME_DBUS_push_relative ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_TIME_Relative *value); + +#endif + diff --git a/src/lib/include/gnunet_time_dbus_lib_signature.h b/src/lib/include/gnunet_time_dbus_lib_signature.h new file mode 100644 index 0000000..d05f4aa --- /dev/null +++ b/src/lib/include/gnunet_time_dbus_lib_signature.h @@ -0,0 +1,10 @@ +#ifndef GNUNET_TIME_DBUS_LIB_SIGNATURE_H +#define GNUNET_TIME_DBUS_LIB_SIGNATURE_H + +#include "gnunet_dbus_lib.h" + +#define GNUNET_TIME_DBUS_SIGNATURE_ABSOLUTE GNUNET_DBUS_SIGNATURE_VARIANT +#define GNUNET_TIME_DBUS_SIGNATURE_RELATIVE GNUNET_DBUS_SIGNATURE_VARIANT + +#endif + diff --git a/src/lib/util/Makefile.am b/src/lib/util/Makefile.am new file mode 100644 index 0000000..f7f6b7c --- /dev/null +++ b/src/lib/util/Makefile.am @@ -0,0 +1,27 @@ +# This Makefile.am is in the public domain +lib_LTLIBRARIES = \ + libgnunettimedbus.la \ + libgnunetcryptodbus.la + +libgnunettimedbus_la_SOURCES = \ + gnunet_time_dbus_lib_pop.c \ + gnunet_time_dbus_lib_push.c + +libgnunettimedbus_la_CFLAGS = \ + -I$(top_builddir)/src/lib/include \ + $(DBUS_CFLAGS) + +libgnunettimedbus_la_LDFLAGS = \ + $(DBUS_LIBS) + +libgnunetcryptodbus_la_SOURCES = \ + gnunet_crypto_dbus_lib_pop.c \ + gnunet_crypto_dbus_lib_push.c + +libgnunetcryptodbus_la_CFLAGS = \ + -I$(top_builddir)/src/lib/include \ + $(DBUS_CFLAGS) + +libgnunetcryptodbus_la_LDFLAGS = \ + $(DBUS_LIBS) + diff --git a/src/lib/util/gnunet_crypto_dbus_lib_pop.c b/src/lib/util/gnunet_crypto_dbus_lib_pop.c new file mode 100644 index 0000000..a598f1d --- /dev/null +++ b/src/lib/util/gnunet_crypto_dbus_lib_pop.c @@ -0,0 +1,145 @@ +#include "config.h" + +#include "gnunet_dbus_lib.h" + +DBusMessage * +GNUNET_CRYPTO_DBUS_pop_ecdsa_public_key ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + struct GNUNET_CRYPTO_EcdsaPublicKey *value) +{ + DBusMessage *ret = NULL; + DBusMessageIter iter_sub; + DBusMessageIter iter_sub_sub; + + ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name); + if (ret) + return ret; + + const char *encoded; + int success; + int arg_type = dbus_message_iter_get_arg_type (&iter_sub); + int element_type; + int n_elements; + unsigned char *marshalled_array; + switch (arg_type) + { + case DBUS_TYPE_STRING: + dbus_message_iter_get_basic (&iter_sub, &encoded); + success = GNUNET_CRYPTO_ecdsa_public_key_from_string (encoded, strlen (encoded), value); + if (GNUNET_OK != success) + { + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "String is not a valid base32 encoded ECDSA public key for argument '%s'", + arg_name); + } + GNUNET_DBUS_message_set_pretty (message, true); + return NULL; + case DBUS_TYPE_ARRAY: + element_type = dbus_message_iter_get_element_type (&iter_sub); + if (DBUS_TYPE_BYTE == element_type) + { + dbus_message_iter_recurse (&iter_sub, &iter_sub_sub); + dbus_message_iter_get_fixed_array (&iter_sub_sub, &marshalled_array, &n_elements); + if (sizeof (value->q_y) == n_elements) + { + memcpy (value->q_y, marshalled_array, n_elements); + return NULL; + }; + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "ECDSA public key consists of 32 bytes (256 bits). Array given for argument '%s' contains %d bytes.", + arg_name, + n_elements); + }; + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Invalid type for argument '%s'. Variant contains an array of '%s'. Should contain an ECDSA public key in the form of a base32 encoded string or an array of 32 bytes (256 bits).", + arg_name, + GNUNET_DBUS_signature_typecode_to_string (element_type)); + default: + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Invalid type in variant for argument '%s'. Should contain an ECDSA public key in the form of a base32 encoded string or an array of 32 bytes (256 bits). Contains '%s'", + arg_name, + GNUNET_DBUS_signature_typecode_to_string (arg_type)); + } +} + +DBusMessage * +GNUNET_CRYPTO_DBUS_pop_eddsa_public_key ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + struct GNUNET_CRYPTO_EddsaPublicKey *value) +{ + DBusMessage *ret = NULL; + DBusMessageIter iter_sub; + DBusMessageIter iter_sub_sub; + + ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name); + if (ret) + return ret; + + const char *encoded; + int success; + int arg_type = dbus_message_iter_get_arg_type (&iter_sub); + int element_type; + int n_elements; + unsigned char *marshalled_array; + switch (arg_type) + { + case DBUS_TYPE_STRING: + dbus_message_iter_get_basic (&iter_sub, &encoded); + success = GNUNET_CRYPTO_eddsa_public_key_from_string (encoded, strlen (encoded), value); + if (GNUNET_OK != success) + { + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "String is not a valid base32 encoded EDDSA public key for argument '%s'", + arg_name); + } + GNUNET_DBUS_message_set_pretty (message, true); + return NULL; + case DBUS_TYPE_ARRAY: + element_type = dbus_message_iter_get_element_type (&iter_sub); + if (DBUS_TYPE_BYTE == element_type) + { + dbus_message_iter_recurse (&iter_sub, &iter_sub_sub); + dbus_message_iter_get_fixed_array (&iter_sub_sub, &marshalled_array, &n_elements); + if (sizeof (value->q_y) == n_elements) + { + memcpy (value->q_y, marshalled_array, n_elements); + return NULL; + }; + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "EDDSA public key consists of 32 bytes (256 bits). Array given for argument '%s' contains %d bytes.", + arg_name, + n_elements); + }; + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Invalid type for argument '%s'. Variant contains an array of '%s'. Should contain an EDDSA public key in the form of a base32 encoded string or an array of 32 bytes (256 bits).", + arg_name, + GNUNET_DBUS_signature_typecode_to_string (element_type)); + default: + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Invalid type in variant for argument '%s'. Should contain an ECDSA public key in the form of a base32 encoded string or an array of 32 bytes (256 bits). Contains '%s'", + arg_name, + GNUNET_DBUS_signature_typecode_to_string (arg_type)); + } +} + + diff --git a/src/lib/util/gnunet_crypto_dbus_lib_push.c b/src/lib/util/gnunet_crypto_dbus_lib_push.c new file mode 100644 index 0000000..6e1cd3a --- /dev/null +++ b/src/lib/util/gnunet_crypto_dbus_lib_push.c @@ -0,0 +1,54 @@ +#include "config.h" + +#include "gnunet_dbus_lib.h" + +void +GNUNET_CRYPTO_DBUS_push_ecdsa_public_key ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_CRYPTO_EcdsaPublicKey *value) +{ + DBusMessageIter iter_sub; + + if (GNUNET_DBUS_message_get_pretty (message)) + { + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_STRING); + char *encoded = GNUNET_CRYPTO_ecdsa_public_key_to_string (value); + const char *encoded_const = encoded; + GNUNET_DBUS_push_string (message, &iter_sub, &encoded_const); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + GNUNET_free (encoded); + } + else + { + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_ARRAY (GNUNET_DBUS_SIGNATURE_BYTE)); + GNUNET_DBUS_push_byte_array (message, &iter_sub, value->q_y, 32); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + }; +}; + +void +GNUNET_CRYPTO_DBUS_push_eddsa_public_key ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_CRYPTO_EddsaPublicKey *value) +{ + DBusMessageIter iter_sub; + + if (GNUNET_DBUS_message_get_pretty (message)) + { + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_STRING); + char *encoded = GNUNET_CRYPTO_eddsa_public_key_to_string (value); + const char *encoded_const = encoded; + GNUNET_DBUS_push_string (message, &iter_sub, &encoded_const); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + GNUNET_free (encoded); + } + else + { + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_ARRAY (GNUNET_DBUS_SIGNATURE_BYTE)); + GNUNET_DBUS_push_byte_array (message, &iter_sub, value->q_y, 32); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + }; +}; + diff --git a/src/lib/util/gnunet_time_dbus_lib_pop.c b/src/lib/util/gnunet_time_dbus_lib_pop.c new file mode 100644 index 0000000..10322c6 --- /dev/null +++ b/src/lib/util/gnunet_time_dbus_lib_pop.c @@ -0,0 +1,52 @@ +#include "config.h" + +#include +#include +#include + +#include "gnunet_time_dbus_lib.h" + +DBusMessage * +GNUNET_TIME_DBUS_pop_absolute ( + DBusMessage *message, + DBusMessageIter *iter, + const char *arg_name, + struct GNUNET_TIME_Absolute *value) +{ + DBusMessageIter iter_sub; + DBusMessage *ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name); + if (ret) + return ret; + + int arg_type = dbus_message_iter_get_arg_type (&iter_sub); + switch (arg_type) + { + case DBUS_TYPE_STRING: { + const char *time; + dbus_message_iter_get_basic (&iter_sub, &time); + int err = GNUNET_STRINGS_fancy_time_to_absolute (time, value); + if (GNUNET_OK != err) + { + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Malformed time string. GNUNET_STRINGS_fancy_time_to_absolute returned %d", + arg_name, + err); + }; + return NULL; + }; + case DBUS_TYPE_UINT64: + dbus_message_iter_get_basic (&iter_sub, &value->abs_value_us); + return NULL; + default: + return dbus_message_new_error_printf ( + message, + DBUS_ERROR_INVALID_ARGS, + "Bad argument for '%s'. Variant must contain a time encoded as a human readable string or a uint64 representing the number of microseconds since epoch. Contains %s.", + arg_name, + GNUNET_DBUS_signature_typecode_to_string (arg_type)); + }; +}; + + diff --git a/src/lib/util/gnunet_time_dbus_lib_push.c b/src/lib/util/gnunet_time_dbus_lib_push.c new file mode 100644 index 0000000..a618278 --- /dev/null +++ b/src/lib/util/gnunet_time_dbus_lib_push.c @@ -0,0 +1,61 @@ +#include "config.h" + +#include +#include +#include + +#include "gnunet_time_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "time-dbus-push", __VA_ARGS__) + +void +GNUNET_TIME_DBUS_push_absolute ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_TIME_Absolute *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing absolute time\n"); + DBusMessageIter iter_sub; + if (GNUNET_DBUS_message_get_pretty (message)) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, "Encoding time as string\n"); + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_STRING); + const char *stringified = GNUNET_STRINGS_absolute_time_to_string (*value); + GNUNET_DBUS_push_string (message, &iter_sub, &stringified); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + } + else + { + LOG (GNUNET_ERROR_TYPE_DEBUG, "Encoding time as uint64\n"); + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_UINT64); + GNUNET_DBUS_push_uint64 (message, &iter_sub, &value->abs_value_us); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + } +} + +void +GNUNET_TIME_DBUS_push_relative ( + DBusMessage *message, + DBusMessageIter *iter, + const struct GNUNET_TIME_Relative *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Pushing relative time\n"); + DBusMessageIter iter_sub; + if (GNUNET_DBUS_message_get_pretty (message)) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, "Encoding time as string\n"); + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_STRING); + const char *stringified = GNUNET_STRINGS_relative_time_to_string (*value, 0); + GNUNET_DBUS_push_string (message, &iter_sub, &stringified); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + } + else + { + LOG (GNUNET_ERROR_TYPE_DEBUG, "Encoding time as uint64\n"); + GNUNET_DBUS_push_open_variant (message, iter, &iter_sub, GNUNET_DBUS_SIGNATURE_UINT64); + GNUNET_DBUS_push_uint64 (message, &iter_sub, &value->rel_value_us); + GNUNET_DBUS_push_close_variant (message, iter, &iter_sub); + } +} + + diff --git a/src/services/Makefile.am b/src/services/Makefile.am new file mode 100644 index 0000000..27c2372 --- /dev/null +++ b/src/services/Makefile.am @@ -0,0 +1,31 @@ +# This Makefile.am is in the public domain +AM_CFLAGS = \ + -I$(top_builddir)/src/lib/include + +bin_PROGRAMS = \ + gnunet-service-gns-dbus \ + gnunet-service-dht-dbus + +gnunet_service_gns_dbus_SOURCES = \ + gnunet-service-gns-dbus.c + +gnunet_service_gns_dbus_LDADD = \ + $(top_builddir)/src/lib/common/libgnunetdbus.la \ + $(top_builddir)/src/lib/util/libgnunettimedbus.la \ + $(top_builddir)/src/lib/util/libgnunetcryptodbus.la \ + $(top_builddir)/src/lib/gnsrecord/libgnunetgnsrecorddbus.la \ + -lgnunetgns \ + -lgnunetutil + +gnunet_service_dht_dbus_SOURCES = \ + gnunet-service-dht-dbus.c + +gnunet_service_dht_dbus_LDADD = \ + $(top_builddir)/src/lib/common/libgnunetdbus.la \ + $(top_builddir)/src/lib/util/libgnunettimedbus.la \ + $(top_builddir)/src/lib/util/libgnunetcryptodbus.la \ + $(top_builddir)/src/lib/dht/libgnunetdhtdbus.la \ + $(top_builddir)/src/lib/block/libgnunetblockdbus.la \ + -lgnunetdht \ + -lgnunetutil + diff --git a/src/services/gnunet-service-dht-dbus.c b/src/services/gnunet-service-dht-dbus.c new file mode 100644 index 0000000..4b47be7 --- /dev/null +++ b/src/services/gnunet-service-dht-dbus.c @@ -0,0 +1,612 @@ +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "gnunet_dbus_lib.h" +#include "gnunet_dht_dbus_lib.h" +#include "gnunet_block_dbus_lib.h" +#include "gnunet_time_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "dht-dbus", __VA_ARGS__) + +struct GetRequest +{ + struct GNUNET_DBUS_Client *client; + struct GNUNET_DHT_GetHandle *handle; + bool pretty; +}; + +struct ClientData +{ + struct GNUNET_DBUS_ObjectIterator *gets_front; + struct GNUNET_DBUS_ObjectIterator *gets_back; + struct GNUNET_DHT_Handle *dht_handle; +}; + +static struct GNUNET_DBUS_Service *gbl_dht_service; +static struct GNUNET_DBUS_Interface *gbl_dht_interface; +static struct GNUNET_DBUS_Method *gbl_dht_put_method; +static struct GNUNET_DBUS_Method *gbl_dht_get_start_method; +static struct GNUNET_DBUS_Object *gbl_dht_get_object; +static struct GNUNET_DBUS_Interface *gbl_dht_get_request_interface; +static struct GNUNET_DBUS_Method + *gbl_dht_get_request_filter_known_results_method; +static struct GNUNET_DBUS_Method *gbl_dht_get_request_stop_method; +static struct GNUNET_DBUS_Signal *gbl_dht_get_request_result_signal; + +static void +put_return ( + void *cls, + int success) +{ + struct GNUNET_DBUS_MethodContext *mc = + (struct GNUNET_DBUS_MethodContext *)cls; + unsigned msg_serial = dbus_message_get_serial (mc->message); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Received reply from DHT. Method call id %u\n", + msg_serial); + DBusMessage *reply; + switch (success) + { + case GNUNET_OK: + reply = GNUNET_DBUS_method_context_create_reply (mc); + break; + case GNUNET_NO: + reply = dbus_message_new_error ( + mc->message, + DBUS_ERROR_FAILED, + "DHT service timed out."); + break; + case GNUNET_SYSERR: + reply = dbus_message_new_error ( + mc->message, + DBUS_ERROR_FAILED, + "DHT DBus proxy was disconnected from DHT service."); + break; + default: + reply = dbus_message_new_error_printf ( + mc->message, + DBUS_ERROR_FAILED, + "Unabled to interpret response from DHT service." + "Service returned code %d.", + success); + }; + + GNUNET_DBUS_method_context_send_reply (mc, reply); + GNUNET_DBUS_method_context_unref (mc); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Received reply from DHT. Method call id %u\n", + msg_serial); +}; + +static void +put ( + struct GNUNET_DBUS_MethodContext *mc) +{ + struct GNUNET_HashCode key; + uint32_t desired_replication_level; + enum GNUNET_DHT_RouteOption options; + enum GNUNET_BLOCK_Type type; + int size; + const void *data; + struct GNUNET_TIME_Absolute expiry; + + unsigned msg_serial = dbus_message_get_serial (mc->message); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Recieved put from dbus. Method call id %u\n", + msg_serial); + + DBusMessage *message = mc->message; + DBusMessageIter iter; + dbus_message_iter_init (message, &iter); + + DBusMessage *reply = NULL; + reply = reply ? reply : GNUNET_DBUS_pop_hashcode ( + message, &iter, + "key", + &key); + reply = reply ? reply : GNUNET_DBUS_pop_uint32 ( + message, &iter, + "desired_replication_level", + &desired_replication_level); + reply = reply ? reply : GNUNET_DHT_DBUS_pop_route_option ( + message, &iter, + "options", + &options); + reply = reply ? reply : GNUNET_BLOCK_DBUS_pop_type ( + message, &iter, + "type", + &type); + reply = reply ? reply : GNUNET_DBUS_pop_byte_array ( + message, &iter, + "data", + (const unsigned char **)&data, &size); + reply = reply ? reply : GNUNET_TIME_DBUS_pop_absolute ( + message, &iter, + "expiry", + &expiry); + if (reply) + { + GNUNET_DBUS_method_context_send_reply (mc, reply); + return; + }; + + GNUNET_DBUS_method_context_ref (mc); + struct ClientData *cd = GNUNET_DBUS_client_get_data (mc->client); + struct GNUNET_DHT_Handle *handle = cd->dht_handle; + + GNUNET_DHT_put ( + handle, + &key, + desired_replication_level, + options, + type, + size, + data, + expiry, + GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100), + put_return, + mc); + + LOG ( + GNUNET_ERROR_TYPE_DEBUG, + "Forwarded put to dht. Method call id %u\n", + msg_serial); +}; + +static void +get_filter_known_results ( + struct GNUNET_DBUS_MethodContext *mc) +{ + struct GNUNET_HashCode *results; + size_t results_len; + + DBusMessage *message = mc->message; + DBusMessageIter iter; + DBusMessageIter iter_sub; + dbus_message_iter_init (message, &iter); + + DBusMessage *reply = NULL; + reply = GNUNET_DBUS_pop_enter_array (message, &iter, &iter_sub, + "results", + &results_len); + if (reply) + { + GNUNET_DBUS_method_context_send_reply (mc, reply); + return ; + }; + + results = GNUNET_malloc (sizeof (struct GNUNET_HashCode) * results_len); + size_t i; + for (i = 0; i < results_len && ! results ; i++) + reply = GNUNET_DBUS_pop_hashcode (message, &iter_sub, + "result", + results + i); + if(reply) + { + GNUNET_free (results); + GNUNET_DBUS_method_context_send_reply (mc, reply); + return; + }; + + struct GetRequest *get_request = GNUNET_DBUS_object_get_data (mc->object); + struct GNUNET_DHT_GetHandle *handle = get_request->handle; + + GNUNET_DHT_get_filter_known_results (handle, (unsigned)results_len, results); + GNUNET_free (results); + + reply = GNUNET_DBUS_method_context_create_reply (mc); + GNUNET_DBUS_method_context_send_reply (mc, reply); +} + +static void +get_stop ( + struct GNUNET_DBUS_MethodContext *mc) +{ + struct GNUNET_DBUS_Object *object = mc->object; + struct GetRequest *get_request = GNUNET_DBUS_object_get_data (object); + struct GNUNET_DHT_GetHandle *handle = get_request->handle; + + GNUNET_DHT_get_stop (handle); + + struct GNUNET_DBUS_ObjectIterator *obj_it = + GNUNET_DBUS_object_iterate_subobjects (gbl_dht_get_object); + while (obj_it) + { + if (obj_it->object == object) + { + GNUNET_DBUS_object_remove_subobject (gbl_dht_get_object, obj_it); + break; + } + obj_it = obj_it->next; + }; + if (obj_it) + { + GNUNET_DBUS_client_unref (get_request->client); + GNUNET_free (get_request); + } + else + { + LOG (GNUNET_ERROR_TYPE_WARNING, + "Tried to stop request that does not exist\n"); + LOG (GNUNET_ERROR_TYPE_WARNING, + " name == %s\n", + GNUNET_DBUS_object_get_name (object)); + } + + DBusMessage *reply = GNUNET_DBUS_method_context_create_reply (mc); + GNUNET_DBUS_method_context_send_reply (mc, reply); +} + +static void +get_iter_return ( + void *cls, + struct GNUNET_TIME_Absolute expiry, + const struct GNUNET_HashCode *key, + const struct GNUNET_PeerIdentity *get_path, + unsigned get_path_len, + const struct GNUNET_PeerIdentity *put_path, + unsigned put_path_len, + enum GNUNET_BLOCK_Type type, + size_t size, + const void *data) +{ + unsigned i; + struct GNUNET_DBUS_Object *request_object = (struct GNUNET_DBUS_Object *)cls; + struct GetRequest *get_request = + (struct GetRequest *)GNUNET_DBUS_object_get_data (request_object); + struct GNUNET_DBUS_Client *client = get_request->client; + struct GNUNET_DBUS_ObjectPath *path = GNUNET_DBUS_object_path_create ( + gbl_dht_service, + gbl_dht_get_object, + request_object, + NULL); + + DBusMessage *message = GNUNET_DBUS_signal_spawn_unicast ( + gbl_dht_get_request_result_signal, + path, + gbl_dht_get_request_interface, + client, + get_request->pretty); + + GNUNET_DBUS_object_path_unref (path); + + DBusMessageIter message_iter; + DBusMessageIter message_iter_sub; + dbus_message_iter_init_append (message, &message_iter); + + GNUNET_TIME_DBUS_push_absolute (message, &message_iter, &expiry); + GNUNET_DBUS_push_hashcode (message, &message_iter, key); + GNUNET_DBUS_push_open_array (message, &message_iter, &message_iter_sub, + GNUNET_DBUS_SIGNATURE_PEER_IDENTITY); + for (i = 0; i < get_path_len; i++) + { + GNUNET_DBUS_push_peer_identity (message, &message_iter_sub, get_path + i); + } + GNUNET_DBUS_push_close_array (message, &message_iter, &message_iter_sub); + GNUNET_DBUS_push_open_array (message, &message_iter, &message_iter_sub, + GNUNET_DBUS_SIGNATURE_PEER_IDENTITY); + for (i = 0; i < put_path_len; i++) + { + GNUNET_DBUS_push_peer_identity (message, &message_iter_sub, put_path + i); + } + GNUNET_DBUS_push_close_array (message, &message_iter, &message_iter_sub); + GNUNET_BLOCK_DBUS_push_type (message, &message_iter, &type); + GNUNET_DBUS_push_byte_array (message, &message_iter, data, size); + + GNUNET_DBUS_service_send (gbl_dht_service, message); +} + +static void +get_start ( + struct GNUNET_DBUS_MethodContext *mc) +{ + enum GNUNET_BLOCK_Type type; + struct GNUNET_HashCode key; + uint32_t desired_replication_level; + enum GNUNET_DHT_RouteOption options; + + unsigned msg_serial = dbus_message_get_serial (mc->message); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Recieved get_start from dbus. Method call id %u\n", msg_serial); + + DBusMessage *message = mc->message; + DBusMessageIter iter; + dbus_message_iter_init (message, &iter); + + DBusMessage *reply = NULL; + reply = reply ? reply : GNUNET_BLOCK_DBUS_pop_type ( + message, &iter, + "type", + &type); + reply = reply ? reply : GNUNET_DBUS_pop_hashcode ( + message, &iter, + "key", + &key); + reply = reply ? reply : GNUNET_DBUS_pop_uint32 ( + message, &iter, + "desired_replication_level", + &desired_replication_level); + reply = reply ? reply : GNUNET_DHT_DBUS_pop_route_option ( + message, &iter, + "options", + &options); + if (reply) + { + GNUNET_DBUS_method_context_send_reply (mc, reply); + return; + }; + //if (GNUNET_DBUS_message_get_pretty_encoded (message)) + //GNUNET_DBUS_client_set_prefers_pretty_encodings (mc->client, true); + + GNUNET_DBUS_method_context_ref (mc); + struct ClientData *cd = GNUNET_DBUS_client_get_data (mc->client); + struct GNUNET_DHT_Handle *handle = cd->dht_handle; + + struct GetRequest *get_request = GNUNET_new (struct GetRequest); + get_request->client = mc->client; + GNUNET_DBUS_client_ref (mc->client); + get_request->pretty = GNUNET_DBUS_message_get_pretty (message); + + struct GNUNET_DBUS_Object *request_object = + GNUNET_DBUS_object_create_uniquely_named_subobject (gbl_dht_get_object, + get_request); + GNUNET_DBUS_object_add_interface (request_object, + GNUNET_DBUS_interface_introspectable ()); + GNUNET_DBUS_object_add_interface (request_object, + gbl_dht_get_request_interface); + + struct GNUNET_DBUS_ObjectPath *path = + GNUNET_DBUS_object_path_create (gbl_dht_service, + gbl_dht_get_object, + request_object, + NULL); + + reply = GNUNET_DBUS_method_context_create_reply (mc); + DBusMessageIter reply_iter; + dbus_message_iter_init_append (reply, &reply_iter); + GNUNET_DBUS_push_object_path (reply, &reply_iter, path); + GNUNET_DBUS_method_context_send_reply (mc, reply); + + struct GNUNET_DBUS_ObjectIterator *obj_it = + GNUNET_new (struct GNUNET_DBUS_ObjectIterator); + obj_it->object = request_object; + GNUNET_CONTAINER_DLL_insert (cd->gets_front, + cd->gets_back, + obj_it); + + get_request->handle = GNUNET_DHT_get_start ( + handle, + type, + &key, + desired_replication_level, + options, + NULL, + 0, + get_iter_return, + request_object); +}; + +static void +client_connects ( + struct GNUNET_DBUS_Service *service, + struct GNUNET_DBUS_Client *client) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Creating dht client for %s\n", + GNUNET_DBUS_client_get_unique_name (client)); + const struct GNUNET_CONFIGURATION_Handle *cfg = + GNUNET_DBUS_service_get_config (service); + struct ClientData *cd = GNUNET_new (struct ClientData); + cd->dht_handle = GNUNET_DHT_connect (cfg, 32); + cd->gets_front = NULL; + cd->gets_back = NULL; + GNUNET_DBUS_client_set_data (client, cd); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Finished creating DHT client for %s\n", + GNUNET_DBUS_client_get_unique_name (client)); +}; + +static void +client_disconnects ( + struct GNUNET_DBUS_Service *service, + struct GNUNET_DBUS_Client *client) +{ + struct ClientData *cd = GNUNET_DBUS_client_get_data (client); + struct GNUNET_DBUS_ObjectIterator *obj_it = cd->gets_front; + while (obj_it) + { + struct GNUNET_DBUS_ObjectIterator *next = obj_it->next; + struct GNUNET_DBUS_Object *object = obj_it->object; + struct GetRequest *request = GNUNET_DBUS_object_get_data (object); + + GNUNET_DHT_get_stop (request->handle); + GNUNET_free (request); + GNUNET_DBUS_object_remove_subobject (gbl_dht_get_object, obj_it); + GNUNET_free (obj_it); + obj_it = next; + }; + GNUNET_DHT_disconnect (cd->dht_handle); + GNUNET_free (cd); +}; + +static void +shutdown_task ( + void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + (void)cls; + (void)tc; + + GNUNET_DBUS_service_unref (gbl_dht_service); + + LOG (GNUNET_ERROR_TYPE_INFO, "Exiting.\n"); +}; + +static void +run ( + void *cls, + char *const *args, + const char *configfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Running.\n"); + + gbl_dht_service = GNUNET_DBUS_service_create (cfg, "dht"); + if (! gbl_dht_service) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create dht service.\n"); + GNUNET_abort_ (); + }; + GNUNET_DBUS_service_set_client_handlers (gbl_dht_service, + client_connects, + client_disconnects); + + struct GNUNET_DBUS_Object *root_object = + GNUNET_DBUS_service_get_root_object (gbl_dht_service); + + gbl_dht_interface = GNUNET_DBUS_interface_create ("gnu.gnunet.dht"); + GNUNET_DBUS_object_add_interface (root_object, + GNUNET_DBUS_interface_introspectable ()); + GNUNET_DBUS_object_add_interface (root_object, + gbl_dht_interface); + + gbl_dht_put_method = GNUNET_DBUS_method_create ("put", put); + GNUNET_DBUS_interface_add_method (gbl_dht_interface, gbl_dht_put_method); + GNUNET_DBUS_method_add_arg (gbl_dht_put_method, + "key", + GNUNET_DBUS_SIGNATURE_HASHCODE); + GNUNET_DBUS_method_add_arg (gbl_dht_put_method, + "desired_replication_level", + GNUNET_DBUS_SIGNATURE_UINT32); + GNUNET_DBUS_method_add_arg (gbl_dht_put_method, + "options", + GNUNET_DHT_DBUS_SIGNATURE_ROUTE_OPTION); + GNUNET_DBUS_method_add_arg (gbl_dht_put_method, + "type", + GNUNET_BLOCK_DBUS_SIGNATURE_TYPE); + GNUNET_DBUS_method_add_arg (gbl_dht_put_method, + "data", + GNUNET_DBUS_SIGNATURE_ARRAY ( + GNUNET_DBUS_SIGNATURE_BYTE)); + GNUNET_DBUS_method_add_arg (gbl_dht_put_method, + "expiry", + GNUNET_TIME_DBUS_SIGNATURE_ABSOLUTE); + + gbl_dht_get_start_method = GNUNET_DBUS_method_create ("get_start", + get_start); + GNUNET_DBUS_interface_add_method (gbl_dht_interface, + gbl_dht_get_start_method); + GNUNET_DBUS_method_add_arg (gbl_dht_get_start_method, + "type", + GNUNET_BLOCK_DBUS_SIGNATURE_TYPE); + GNUNET_DBUS_method_add_arg (gbl_dht_get_start_method, + "key", + GNUNET_DBUS_SIGNATURE_HASHCODE); + GNUNET_DBUS_method_add_arg (gbl_dht_get_start_method, + "desired_replication_level", + GNUNET_DBUS_SIGNATURE_UINT32); + GNUNET_DBUS_method_add_arg (gbl_dht_get_start_method, + "options", + GNUNET_DHT_DBUS_SIGNATURE_ROUTE_OPTION); + GNUNET_DBUS_method_add_return_arg (gbl_dht_get_start_method, + "request_object", + GNUNET_DBUS_SIGNATURE_OBJECT_PATH); + + gbl_dht_get_object = GNUNET_DBUS_object_create ("get", NULL); + GNUNET_DBUS_object_add_subobject (root_object, gbl_dht_get_object); + GNUNET_DBUS_object_add_interface (gbl_dht_get_object, + GNUNET_DBUS_interface_introspectable ()); + + gbl_dht_get_request_interface = + GNUNET_DBUS_interface_create ("gnu.gnunet.dht.get"); + + gbl_dht_get_request_filter_known_results_method = + GNUNET_DBUS_method_create ("filter_known_results", + get_filter_known_results); + GNUNET_DBUS_interface_add_method ( + gbl_dht_get_request_interface, + gbl_dht_get_request_filter_known_results_method); + GNUNET_DBUS_method_add_arg (gbl_dht_get_request_filter_known_results_method, + "results", + GNUNET_DBUS_SIGNATURE_ARRAY ( + GNUNET_DBUS_SIGNATURE_HASHCODE)); + + gbl_dht_get_request_stop_method = GNUNET_DBUS_method_create ("stop", + get_stop); + GNUNET_DBUS_interface_add_method (gbl_dht_get_request_interface, + gbl_dht_get_request_stop_method); + + gbl_dht_get_request_result_signal = GNUNET_DBUS_signal_create ("result"); + GNUNET_DBUS_interface_add_signal (gbl_dht_get_request_interface, + gbl_dht_get_request_result_signal); + GNUNET_DBUS_signal_add_arg (gbl_dht_get_request_result_signal, + "expiry", + GNUNET_TIME_DBUS_SIGNATURE_ABSOLUTE); + GNUNET_DBUS_signal_add_arg (gbl_dht_get_request_result_signal, + "key", + GNUNET_DBUS_SIGNATURE_HASHCODE); + GNUNET_DBUS_signal_add_arg (gbl_dht_get_request_result_signal, + "get_path", + GNUNET_DBUS_SIGNATURE_ARRAY ( + GNUNET_DBUS_SIGNATURE_PEER_IDENTITY)); + GNUNET_DBUS_signal_add_arg (gbl_dht_get_request_result_signal, + "put_path", + GNUNET_DBUS_SIGNATURE_ARRAY ( + GNUNET_DBUS_SIGNATURE_PEER_IDENTITY)); + GNUNET_DBUS_signal_add_arg (gbl_dht_get_request_result_signal, + "type", + GNUNET_BLOCK_DBUS_SIGNATURE_TYPE); + GNUNET_DBUS_signal_add_arg (gbl_dht_get_request_result_signal, + "data", + GNUNET_DBUS_SIGNATURE_ARRAY ( + GNUNET_DBUS_SIGNATURE_BYTE)); + + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, + shutdown_task, NULL); +}; + +int +main ( + int argc, + char *const *argv) +{ + int ret; + + static const struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_OPTION_END + }; + static const char bin_name[] = "gnunet-service-dht-dbus [OPTIONS]"; + static const char bin_help[] = gettext_noop ("DBus proxy for gnunet-service-dht"); + + ret = GNUNET_log_setup ("gnunet-service-dht-dbus", "DEBUG", NULL); + if (GNUNET_OK != ret) + { + fprintf (stderr, "ERROR: Failed to setup logging. GNUNET_log_setup returned %d\n", ret); + return 1; + } + + ret = GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv); + if (GNUNET_OK != ret) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to parse command line options. GNUNET_STRINGS_get_utf8_args returned %d\n", ret); + return 1; + }; + + ret = GNUNET_PROGRAM_run (argc, argv, bin_name, bin_help, options, run, NULL); + if (GNUNET_OK != ret) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to run program. GNUNET_PROGRAM_run returned %d\n", ret); + return 1; + }; + + GNUNET_free ((void *)argv); + return 0; +}; + + diff --git a/src/services/gnunet-service-gns-dbus.c b/src/services/gnunet-service-gns-dbus.c new file mode 100644 index 0000000..29ac2f8 --- /dev/null +++ b/src/services/gnunet-service-gns-dbus.c @@ -0,0 +1,198 @@ +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "gnunet_dbus_lib.h" +#include "gnunet_crypto_dbus_lib.h" +#include "gnunet_gnsrecord_dbus_lib.h" + +#define LOG(kind, ...) GNUNET_log_from (kind, "gns-dbus", __VA_ARGS__) + +static void +lookup_return ( + void *cls, + uint32_t rd_count, + const struct GNUNET_GNSRECORD_Data *rd) +{ + struct GNUNET_DBUS_MethodContext *mc = (struct GNUNET_DBUS_MethodContext *)cls; + unsigned msg_serial = dbus_message_get_serial (mc->message); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Received reply from GNS. Method call id %u\n", msg_serial); + DBusMessage *reply = GNUNET_DBUS_method_context_create_reply (mc); + DBusMessageIter reply_iter; + dbus_message_iter_init_append (reply, &reply_iter); + + DBusMessageIter reply_iter_sub; + GNUNET_DBUS_push_open_array (reply, &reply_iter, &reply_iter_sub, GNUNET_GNSRECORD_DBUS_SIGNATURE_DATA); + uint32_t i; + for (i = 0; i < rd_count; i++) + GNUNET_GNSRECORD_DBUS_push_data (reply, &reply_iter_sub, rd + i); + GNUNET_DBUS_push_close_array (reply, &reply_iter, &reply_iter_sub); + + GNUNET_DBUS_method_context_send_reply (mc, reply); + GNUNET_DBUS_method_context_unref (mc); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Forwarded reply to dbus. Method call id %u\n", msg_serial); +}; + +#if 0 +static void +lookup_timeout ( + void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct GNUNET_GNS_LookupRequest *lr = (struct GNUNET_GNS_LookupRequest *)cls; + GNUNET_G +} +#endif + +static void +lookup ( + struct GNUNET_DBUS_MethodContext *mc) +{ + const char *name; + struct GNUNET_CRYPTO_EcdsaPublicKey zone; + uint32_t type; + dbus_bool_t only_cached; + + LOG (GNUNET_ERROR_TYPE_DEBUG, "Received lookup request from dbus. Method call id %u\n", dbus_message_get_serial (mc->message)); + + DBusMessage *message = mc->message; + DBusMessageIter message_iter; + dbus_message_iter_init (message, &message_iter); + + DBusMessage *reply = NULL; + reply = reply ? reply : GNUNET_DBUS_pop_string (message, &message_iter, "name", &name); + reply = reply ? reply : GNUNET_CRYPTO_DBUS_pop_ecdsa_public_key (message, &message_iter, "zone", &zone); + reply = reply ? reply : GNUNET_GNSRECORD_DBUS_pop_type (message, &message_iter, "type", &type); + reply = reply ? reply : GNUNET_DBUS_pop_boolean (message, &message_iter, "only_cached", &only_cached); + if (reply) + { + GNUNET_DBUS_method_context_send_reply (mc, reply); + return; + }; + + GNUNET_DBUS_method_context_ref (mc); + struct GNUNET_GNS_Handle *handle = GNUNET_DBUS_client_get_data (mc->client); + struct GNUNET_GNS_LookupRequest *lr = GNUNET_GNS_lookup ( + handle, + name, &zone, (int)type, (int)only_cached, NULL, + lookup_return, mc); + (void)lr; + + //GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (500, GNUNET_TIME_UNIT_MILLISECONDS), + LOG (GNUNET_ERROR_TYPE_DEBUG, "Forwarded lookup to GNS. Method call id %u\n", dbus_message_get_serial (mc->message)); +}; + +static void +client_connects ( + struct GNUNET_DBUS_Service *service, + struct GNUNET_DBUS_Client *client) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating GNS client for %s\n", GNUNET_DBUS_client_get_unique_name (client)); + const struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_DBUS_service_get_config (service); + struct GNUNET_GNS_Handle *handle = GNUNET_GNS_connect (cfg); + GNUNET_DBUS_client_set_data (client, handle); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished creating GNS client for %s\n", GNUNET_DBUS_client_get_unique_name (client)); +}; + +static void +client_disconnects ( + struct GNUNET_DBUS_Service *service, + struct GNUNET_DBUS_Client *client) +{ + (void)service; + GNUNET_GNS_disconnect (GNUNET_DBUS_client_get_data (client)); +}; + +static void +shutdown_task ( + void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + (void)tc; + + struct GNUNET_DBUS_Service *gns_service = (struct GNUNET_DBUS_Service *)cls; + GNUNET_DBUS_service_unref (gns_service); + + LOG (GNUNET_ERROR_TYPE_INFO, "Exiting.\n"); +}; + +static void +run ( + void *cls, + char *const *args, + const char *configfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Running.\n"); + + struct GNUNET_DBUS_Service *gns_service = GNUNET_DBUS_service_create (cfg, "gns"); + if (! gns_service) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create gns service.\n"); + GNUNET_abort_ (); + }; + GNUNET_DBUS_service_set_client_handlers (gns_service, client_connects, client_disconnects); + + struct GNUNET_DBUS_Object *gns_object = GNUNET_DBUS_service_get_root_object (gns_service); + + struct GNUNET_DBUS_Interface *gns_interface = GNUNET_DBUS_interface_create ("gnu.gnunet.gns"); + GNUNET_DBUS_object_add_interface (gns_object, GNUNET_DBUS_interface_introspectable ()); + GNUNET_DBUS_object_add_interface (gns_object, gns_interface); + + struct GNUNET_DBUS_Method *gns_method_lookup = GNUNET_DBUS_method_create ("lookup", lookup); + GNUNET_DBUS_interface_add_method (gns_interface, gns_method_lookup); + GNUNET_DBUS_method_add_arg (gns_method_lookup, "name", GNUNET_DBUS_SIGNATURE_STRING); + GNUNET_DBUS_method_add_arg (gns_method_lookup, "zone", GNUNET_CRYPTO_DBUS_SIGNATURE_ECDSA_PUBLIC_KEY); + GNUNET_DBUS_method_add_arg (gns_method_lookup, "type", GNUNET_GNSRECORD_DBUS_SIGNATURE_TYPE); + GNUNET_DBUS_method_add_arg (gns_method_lookup, "only_cached", GNUNET_DBUS_SIGNATURE_BOOLEAN); + + GNUNET_DBUS_method_add_return_arg (gns_method_lookup, "records", GNUNET_DBUS_SIGNATURE_ARRAY (GNUNET_GNSRECORD_DBUS_SIGNATURE_DATA)); + + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, shutdown_task, gns_service); +}; + +int +main ( + int argc, + char *const *argv) +{ + int ret; + + static const struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_OPTION_END + }; + static const char bin_name[] = "gnunet-service-gns-dbus [OPTIONS]"; + static const char bin_help[] = gettext_noop ("DBus proxy for gnunet-service-gns"); + + ret = GNUNET_log_setup ("gnunet-service-gns-dbus", "DEBUG", NULL); + if (GNUNET_OK != ret) + { + fprintf (stderr, "ERROR: Failed to setup logging. GNUNET_log_setup returned %d\n", ret); + return 1; + } + + ret = GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv); + if (GNUNET_OK != ret) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to parse command line options. GNUNET_STRINGS_get_utf8_args returned %d\n", ret); + return 1; + }; + + ret = GNUNET_PROGRAM_run (argc, argv, bin_name, bin_help, options, run, NULL); + if (GNUNET_OK != ret) + { + LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to run program. GNUNET_PROGRAM_run returned %d\n", ret); + return 1; + }; + + GNUNET_free ((void *)argv); + return 0; +}; + + diff --git a/src/services/gnunet-service-namecache-dbus.c b/src/services/gnunet-service-namecache-dbus.c new file mode 100644 index 0000000..7eded44 --- /dev/null +++ b/src/services/gnunet-service-namecache-dbus.c @@ -0,0 +1,158 @@ +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "gnunet_dbus_lib.h" + +static void +block_cache_return ( + void *cls, + int32_t success, + const char *emsg) +{ + struct GNUNET_DBUS_MethodContext *mc = (struct GNUNET_DBUS_MethodContext *)cls; + + DBusMessage *message = mc->message; + DBusMessage *reply = dbus_message_new_method_return (message); + DBusMessageIter reply_iter; + dbus_message_iter_init_append (reply, &reply_iter); + GNUNET_DBUS_message_iter_push_int32 (&reply_iter, success); + GNUNET_DBUS_message_iter_push_string (&reply_iter, emsg); + + GNUNET_DBUS_method_context_send_reply (mc, reply); + GNUNET_DBUS_method_context_unref (mc); +}; + +static void +block_cache ( + struct GNUNET_DBUS_MethodContext *mc) +{ + struct GNUNET_GNSRECORD_Block block; + + DBusMessage *dbus_message = mc->message; + DBusMessageIter iter; + dbus_message_iter_init (dbus_message, &iter); + DBusMessage *reply = GNUNET_DBUS_message_iter_pop_gnsrecord_block (dbus_message, &iter, "block", &block); + if (reply) + { + GNUNET_DBUS_method_context_send_reply (mc, reply); + return; + }; + + GNUNET_DBUS_method_context_ref (mc); + struct GNUNET_NAMECACHE_Handle *handle = GNUNET_DBUS_client_get_data (mc->client); + GNUNET_NAMECACHE_block_cache (handle, &block, block_cache_return, mc); +}; + +static void +client_connects ( + struct GNUNET_DBUS_Service *service, + struct GNUNET_DBUS_Client *client) +{ + const struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_DBUS_service_get_config (service); + struct GNUNET_NAMECACHE_Handle *handle = GNUNET_NAMECACHE_connect (cfg); + GNUNET_DBUS_client_set_data (client, handle); +}; + +static void +client_disconnects ( + struct GNUNET_DBUS_Service *service, + struct GNUNET_DBUS_Client *client) +{ + (void)service; + GNUNET_NAMECACHE_disconnect (GNUNET_DBUS_client_get_data (client)); +}; + +static void +shutdown_task ( + void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + (void)tc; + + struct GNUNET_DBUS_Service *gns_service = (struct GNUNET_DBUS_Service *)cls; + GNUNET_DBUS_service_destroy (gns_service); + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Exiting.\n"); +}; + +static void +run ( + void *cls, + char *const *args, + const char *configfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running.\n"); + + struct GNUNET_DBUS_Service *namecache_service = GNUNET_DBUS_service_create (cfg, "namecache"); + GNUNET_DBUS_service_set_client_handlers (namecache_service, client_connects, client_disconnects); + + struct GNUNET_DBUS_Object *namecache_object = GNUNET_DBUS_object_create ("/namecache", NULL); + int err = GNUNET_DBUS_service_add_object (namecache_service, namecache_object); + if (GNUNET_OK != err) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to add /namecache object. GNUNET_DBUS_service_add_object returned %d\n", err); + GNUNET_abort_ (); + }; + + struct GNUNET_DBUS_Interface *namecache_interface = GNUNET_DBUS_interface_create ("gnu.gnunet.namecache"); + GNUNET_DBUS_object_add_interface (namecache_object, GNUNET_DBUS_interface_introspectable ()); + GNUNET_DBUS_object_add_interface (namecache_object, namecache_interface); + + struct GNUNET_DBUS_Method *namecache_method_block_cache = GNUNET_DBUS_method_create ("block_cache", block_cache); + GNUNET_DBUS_interface_add_method (namecache_interface, namecache_method_block_cache); + GNUNET_DBUS_method_add_arg (namecache_method_block_cache, "block", GNUNET_DBUS_SIGNATURE_GNSRECORD_BLOCK); + + GNUNET_DBUS_method_add_return_arg (namecache_method_block_cache, "success", GNUNET_DBUS_SIGNATURE_INT32); + GNUNET_DBUS_method_add_return_arg (namecache_method_block_cache, "error_msg", GNUNET_DBUS_SIGNATURE_STRING); + + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, shutdown_task, namecache_service); +}; + +int +main ( + int argc, + char *const *argv) +{ + int ret; + + static const struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_OPTION_END + }; + + static const char bin_name[] = "gnunet-service-namecache-dbus [OPTIONS]"; + static const char bin_help[] = "DBus proxy for gnunet-service-namecache"; + + ret = GNUNET_log_setup ("gnunet-service-namecache-dbus", "DEBUG", NULL); + if (GNUNET_OK != ret) + { + fprintf (stderr, "ERROR: Failed to setup logging. GNUNET_log_setup returned %d\n", ret); + return 1; + }; + + ret = GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv); + if (GNUNET_OK != ret) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to parse command line options. GNUNET_STRINGS_get_utf8_args returned %d\n", ret); + return 1; + }; + + ret = GNUNET_PROGRAM_run (argc, argv, bin_name, bin_help, options, run, NULL); + if (GNUNET_OK != ret) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to run program. GNUNET_PROGRAM_run returned %d\n", ret); + return 1; + }; + + printf ("leaving main()\n"); + GNUNET_free ((void *)argv); + return 0; +} + -- cgit v1.2.3