diff --git a/libdap-chain-net-srv b/libdap-chain-net-srv deleted file mode 160000 index efddc6a76a3682a04d7f7d9c4b153de5a602bea0..0000000000000000000000000000000000000000 --- a/libdap-chain-net-srv +++ /dev/null @@ -1 +0,0 @@ -Subproject commit efddc6a76a3682a04d7f7d9c4b153de5a602bea0 diff --git a/libdap-chain-net-srv/.gitignore b/libdap-chain-net-srv/.gitignore new file mode 100755 index 0000000000000000000000000000000000000000..4bf974a8994c2ee687383ddb4194759098412bb5 --- /dev/null +++ b/libdap-chain-net-srv/.gitignore @@ -0,0 +1,52 @@ +# Prerequisites +*.d +*.autosave +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf diff --git a/libdap-chain-net-srv/CMakeLists.txt b/libdap-chain-net-srv/CMakeLists.txt new file mode 100755 index 0000000000000000000000000000000000000000..442dcdb31f6944ffb1ee10a402723514e4638382 --- /dev/null +++ b/libdap-chain-net-srv/CMakeLists.txt @@ -0,0 +1,30 @@ +cmake_minimum_required(VERSION 2.8) +project (dap_chain_net_srv) + +set(DAP_CHAIN_NET_SRV_SRCS + dap_chain_net_srv.c + dap_chain_net_srv_order.c + dap_chain_net_srv_client.c + dap_chain_net_srv_common.c + dap_chain_net_srv_stream_session.c + ) + +set(DAP_CHAIN_NET_SRV_HEADERS + dap_chain_net_srv.h + dap_chain_net_srv_client.h + dap_chain_net_srv_order.h + dap_chain_net_srv_common.h + dap_chain_net_srv_stream_session.h + ) + + + +add_library(${PROJECT_NAME} STATIC ${DAP_CHAIN_NET_SRV_SRCS} ${DAP_CHAIN_NET_SRV_HEADERS}) + +target_link_libraries(dap_chain_net_srv dap_core dap_crypto dap_chain dap_chain_crypto dap_chain_net dap_chain_wallet) + +target_include_directories(dap_chain_net_srv INTERFACE .) + +set(${PROJECT_NAME}_DEFINITIONS CACHE INTERNAL "${PROJECT_NAME}: Definitions" FORCE) + +set(${PROJECT_NAME}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR} CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE) diff --git a/libdap-chain-net-srv/LICENSE b/libdap-chain-net-srv/LICENSE new file mode 100755 index 0000000000000000000000000000000000000000..94a9ed024d3859793618152ea559a168bbcbb5e2 --- /dev/null +++ b/libdap-chain-net-srv/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + <program> Copyright (C) <year> <name of author> + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +<http://www.gnu.org/licenses/>. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +<http://www.gnu.org/philosophy/why-not-lgpl.html>. diff --git a/libdap-chain-net-srv/README.md b/libdap-chain-net-srv/README.md new file mode 100755 index 0000000000000000000000000000000000000000..2b53896b89a020d902e793cc229b0d9b63d13b73 --- /dev/null +++ b/libdap-chain-net-srv/README.md @@ -0,0 +1,2 @@ +# libdap-chain-net-srv +DapChain Network Service API diff --git a/libdap-chain-net-srv/dap_chain_net_srv.c b/libdap-chain-net-srv/dap_chain_net_srv.c new file mode 100755 index 0000000000000000000000000000000000000000..8d7b8217c9442c427872ceb16fcfc04e4e792e05 --- /dev/null +++ b/libdap-chain-net-srv/dap_chain_net_srv.c @@ -0,0 +1,725 @@ +/* + * Authors: + * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> + * Aleksandr Lysikov <alexander.lysikov@demlabs.net> + * DeM Labs Inc. https://demlabs.net + * DeM Labs Open source community https://github.com/demlabsinc + * Copyright (c) 2017-2018 + * All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) 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. + + DAP 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 any DAP based project. If not, see <http://www.gnu.org/licenses/>. + */ +#include <stdlib.h> +#include <stdio.h> +#include <time.h> +#include <stdlib.h> +#include <stddef.h> +#include <stdint.h> + +#ifdef WIN32 +#include <winsock2.h> +#include <windows.h> +#include <mswsock.h> +#include <ws2tcpip.h> +#include <io.h> +#endif + +#include <pthread.h> +#include <dirent.h> + +#include "uthash.h" +#include "utlist.h" +#include "dap_list.h" +#include "dap_string.h" + +#include "dap_chain.h" +#include "dap_chain_common.h" +#include "dap_chain_net.h" +#include "dap_chain_net_srv.h" +#include "dap_chain_net_srv_order.h" +#include "dap_chain_net_srv_stream_session.h" + +#include "dap_chain_node_cli_cmd.h" + +#define LOG_TAG "chain_net_srv" + +static size_t m_uid_count; +static dap_chain_net_srv_uid_t * m_uid; + + +typedef struct service_list { + dap_chain_net_srv_uid_t uid; + dap_chain_net_srv_t * srv; + UT_hash_handle hh; +} service_list_t; + +// list of active services +static service_list_t *s_srv_list = NULL; +// for separate access to s_srv_list +static pthread_mutex_t s_srv_list_mutex = PTHREAD_MUTEX_INITIALIZER; +static int s_cli_net_srv( int argc, char **argv, void *arg_func, char **a_str_reply); +static void s_load(const char * a_path); +static void s_load_all(void); + +/** + * @brief dap_chain_net_srv_init + * @return + */ +int dap_chain_net_srv_init(dap_config_t * a_cfg) +{ + m_uid = NULL; + m_uid_count = 0; + if( dap_chain_net_srv_order_init() != 0 ) + return -1; + + dap_chain_node_cli_cmd_item_create ("net_srv", s_cli_net_srv, NULL, "Network services managment", + "net_srv -net <chain net name> order find [-direction <sell|buy>][-srv_uid <Service UID>] [-price_unit <price unit>]\\\n" + " [-price_token <Token ticker>] [-price_min <Price minimum>] [-price_max <Price maximum>]\n" + "\tOrders list, all or by UID and/or class\n" + "net_srv -net <chain net name> order delete -hash <Order hash>\n" + "\tOrder delete\n" + "net_srv -net <chain net name> order dump -hash <Order hash>\n" + "\tOrder dump info\n" + "net_srv -net <chain net name> order create -direction <sell|buy> -srv_uid <Service UID> -price <Price>\\\n" + " -price_unit <Price Unit> -price_token <Token ticker> [-node_addr <Node Address>] [-tx_cond <TX Cond Hash>] \\\n" + " [-expires <Unix time when expires>] [-ext <Extension with params>]\\\n" + "\tOrder create\n" ); + + s_load_all(); + return 0; +} + +/** + * @brief s_load_all + */ +void s_load_all(void) +{ + char * l_net_dir_str = dap_strdup_printf("%s/service.d", dap_config_path()); + DIR * l_net_dir = opendir( l_net_dir_str); + if ( l_net_dir ){ + struct dirent * l_dir_entry; + while ( (l_dir_entry = readdir(l_net_dir) )!= NULL ){ + if (l_dir_entry->d_name[0]=='\0' || l_dir_entry->d_name[0]=='.') + continue; + // don't search in directories + char * l_full_path = dap_strdup_printf("%s/%s", l_net_dir_str, l_dir_entry->d_name); + if(dap_dir_test(l_full_path)) { + DAP_DELETE(l_full_path); + continue; + } + // search only ".cfg" files + if(strlen(l_dir_entry->d_name) > 4) { // It has non zero name excluding file extension + if(strncmp(l_dir_entry->d_name + strlen(l_dir_entry->d_name) - 4, ".cfg", 4) != 0) { + // its not .cfg file + continue; + } + } + log_it(L_DEBUG,"Service config %s try to load", l_dir_entry->d_name); + //char* l_dot_pos = rindex(l_dir_entry->d_name,'.'); + char* l_dot_pos = strchr(l_dir_entry->d_name,'.'); + if ( l_dot_pos ) + *l_dot_pos = '\0'; + s_load(l_full_path ); + DAP_DELETE(l_full_path); + } + closedir(l_net_dir); + } + DAP_DELETE (l_net_dir_str); +} + +/** + * @brief s_load + * @param a_name + */ +static void s_load(const char * a_path) +{ + log_it ( L_INFO, "Service config %s", a_path); + // TODO open service +} + + +/** + * @brief dap_chain_net_srv_deinit + */ +void dap_chain_net_srv_deinit(void) +{ + // TODO Stop all services + + dap_chain_net_srv_del_all(); +} + + +/** + * @brief s_cli_net_srv + * @param argc + * @param argv + * @param a_str_reply + * @return + */ +static int s_cli_net_srv( int argc, char **argv, void *arg_func, char **a_str_reply) +{ + int arg_index = 1; + dap_chain_net_t * l_net = NULL; + + int ret = dap_chain_node_cli_cmd_values_parse_net_chain( &arg_index, argc, argv, a_str_reply, NULL, &l_net ); + if ( l_net ) { + char * l_orders_group = dap_chain_net_srv_order_get_gdb_group( l_net ); + + dap_string_t *l_string_ret = dap_string_new(""); + const char *l_order_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "order", &l_order_str); + + // Order direction + const char *l_direction_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-direction", &l_direction_str); + + const char* l_srv_uid_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-srv_uid", &l_srv_uid_str); + + const char* l_srv_class_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-srv_class", &l_srv_class_str); + + const char* l_node_addr_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-node_addr", &l_node_addr_str); + + const char* l_tx_cond_hash_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-tx_cond", &l_tx_cond_hash_str); + + const char* l_price_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-price", &l_price_str); + + const char* l_expires_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-expires", &l_expires_str); + + const char* l_price_unit_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-price_unit", &l_price_unit_str); + + const char* l_price_token_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-price_token", &l_price_token_str); + + const char* l_ext = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-ext", &l_ext); + + const char *l_order_hash_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-hash", &l_order_hash_str); + + const char* l_region_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-region", &l_region_str); + const char* l_continent_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-continent", &l_continent_str); + + int8_t l_continent_num = dap_chain_net_srv_order_continent_to_num(l_continent_str); + + if(l_continent_str && l_continent_num <= 0) { + dap_string_t *l_string_err = dap_string_new("Unrecognized \"-continent\" option="); + dap_string_append_printf(l_string_err, "\"%s\". Variants: ", l_continent_str); + int i = 0; + while(1) { + const char *l_continent = dap_chain_net_srv_order_continent_to_str(i); + if(!l_continent) + break; + if(!i) + dap_string_append_printf(l_string_err, "\"%s\"", l_continent); + else + dap_string_append_printf(l_string_err, ", \"%s\"", l_continent); + i++; + } + dap_string_append_printf(l_string_ret, "%s\n", l_string_err->str); + dap_string_free(l_string_err, true); + ret = -1; + } + // Update order + else if(dap_strcmp(l_order_str, "update") == 0) { + + if(!l_order_hash_str) { + ret = -1; + dap_string_append(l_string_ret, "Can't find option '-hash'\n"); + } + else { + dap_chain_net_srv_order_t * l_order = dap_chain_net_srv_order_find_by_hash_str(l_net, l_order_hash_str); + if(!l_order) { + ret = -2; + dap_string_append_printf(l_string_ret, "Can't find order with hash %s\n", l_order_hash_str); + } + else { + if(l_ext) { + l_order->ext_size = strlen(l_ext) + 1; + l_order = DAP_REALLOC(l_order, sizeof(dap_chain_net_srv_order_t) + l_order->ext_size); + strncpy(l_order->ext, l_ext, l_order->ext_size); + } + else + dap_chain_net_srv_order_set_continent_region(&l_order, l_continent_num, l_region_str); + /*if(l_region_str) { + strncpy(l_order->region, l_region_str, min(sizeof(l_order->region) - 1, strlen(l_region_str) + 1)); + } + if(l_continent_num>=0) + l_order->continent = l_continent_num;*/ + + + ret = dap_chain_net_srv_order_save(l_net, l_order); + if(!ret) + ret = 0; + if(!ret) { + dap_chain_hash_fast_t l_new_order_hash; + size_t l_new_order_size = dap_chain_net_srv_order_get_size(l_order); + dap_hash_fast(l_order, l_new_order_size, &l_new_order_hash); + char * l_new_order_hash_str = dap_chain_hash_fast_to_str_new(&l_new_order_hash); + // delete prev order + if(dap_strcmp(l_new_order_hash_str, l_order_hash_str)) + dap_chain_net_srv_order_delete_by_hash_str(l_net, l_order_hash_str); + DAP_DELETE(l_new_order_hash_str); + dap_string_append_printf(l_string_ret, "order updated\n"); + } + else + dap_string_append_printf(l_string_ret, "order did not updated\n"); + + DAP_DELETE(l_order); + } + } + + } + else if ( dap_strcmp( l_order_str, "find" ) == 0 ){ + + // Order direction + const char *l_direction_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-direction", &l_direction_str); + + // Select with specified service uid + const char *l_srv_uid_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-srv_uid", &l_srv_uid_str); + + + // Select with specified price units + const char* l_price_unit_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-price_unit", &l_price_unit_str); + + // Token ticker + const char* l_price_token_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-price_token", &l_price_token_str); + + // Select with price not more than price_min + const char* l_price_min_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-price_min", &l_price_min_str); + + // Select with price not more than price_max + const char* l_price_max_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-price_max", &l_price_max_str); + + dap_chain_net_srv_order_direction_t l_direction = SERV_DIR_UNDEFINED; + dap_chain_net_srv_uid_t l_srv_uid={{0}}; + uint64_t l_price_min=0, l_price_max =0 ; + dap_chain_net_srv_price_unit_uid_t l_price_unit={{0}}; + + if ( l_direction_str ){ + if ( strcmp(l_direction_str, "sell")==0) + l_direction = SERV_DIR_SELL; + else if ( strcmp(l_direction_str, "buy")==0) + l_direction = SERV_DIR_BUY; + } + + if ( l_srv_uid_str) + l_srv_uid.uint64 = (uint128_t) atoll( l_srv_uid_str); + + if ( l_price_min_str ) + l_price_min = (uint64_t) atoll ( l_price_min_str ); + + if ( l_price_max_str ) + l_price_max = (uint64_t) atoll ( l_price_max_str ); + if ( l_price_unit_str) + l_price_unit.uint32 = (uint32_t) atol ( l_price_unit_str ); + + dap_chain_net_srv_order_t * l_orders; + size_t l_orders_num = 0; + if( dap_chain_net_srv_order_find_all_by( l_net, l_direction,l_srv_uid,l_price_unit,l_price_token_str,l_price_min, l_price_max,&l_orders,&l_orders_num) == 0 ){ + dap_string_append_printf(l_string_ret,"Found %u orders:\n",l_orders_num); + size_t l_orders_size = 0; + for (size_t i = 0; i< l_orders_num; i++){ + dap_chain_net_srv_order_t *l_order =(dap_chain_net_srv_order_t *) (((byte_t*) l_orders) + l_orders_size); + dap_chain_net_srv_order_dump_to_string(l_order, l_string_ret); + l_orders_size += dap_chain_net_srv_order_get_size(l_order); + dap_string_append(l_string_ret,"\n"); + } + ret = 0; + }else{ + ret = -5 ; + dap_string_append(l_string_ret,"Can't get orders: some internal error or wrong params\n"); + } + DAP_DELETE(l_orders); + + }else if( dap_strcmp( l_order_str, "dump" ) == 0 ){ + // Select with specified service uid + if ( l_order_hash_str ){ + dap_chain_net_srv_order_t * l_order = dap_chain_net_srv_order_find_by_hash_str( l_net, l_order_hash_str ); + if (l_order){ + dap_chain_net_srv_order_dump_to_string(l_order,l_string_ret); + ret = 0; + }else{ + ret = -7 ; + dap_string_append_printf(l_string_ret,"Can't find order with hash %s\n", l_order_hash_str ); + } + } else{ + + dap_chain_net_srv_order_t * l_orders = NULL; + size_t l_orders_num =0; + dap_chain_net_srv_uid_t l_srv_uid={{0}}; + uint64_t l_price_min=0, l_price_max =0 ; + dap_chain_net_srv_price_unit_uid_t l_price_unit={{0}}; + dap_chain_net_srv_order_direction_t l_direction = SERV_DIR_UNDEFINED; + + if( dap_chain_net_srv_order_find_all_by( l_net,l_direction,l_srv_uid,l_price_unit, NULL, l_price_min, l_price_max,&l_orders,&l_orders_num) == 0 ){ + dap_string_append_printf(l_string_ret,"Found %zd orders:\n",l_orders_num); + size_t l_orders_size = 0; + for(size_t i = 0; i < l_orders_num; i++) { + dap_chain_net_srv_order_t *l_order = (char*) l_orders + l_orders_size; + dap_chain_net_srv_order_dump_to_string(l_order, l_string_ret); + l_orders_size += dap_chain_net_srv_order_get_size(l_order); + dap_string_append(l_string_ret, "\n"); + } + ret = 0; + }else{ + ret = -5 ; + dap_string_append(l_string_ret,"Can't get orders: some internal error or wrong params\n"); + } + DAP_DELETE(l_orders); + } + }else if( dap_strcmp( l_order_str, "delete" ) == 0 ){ + // Select with specified service uid + const char *l_order_hash_str = NULL; + dap_chain_node_cli_find_option_val(argv, arg_index, argc, "-hash", &l_order_hash_str); + if ( l_order_hash_str ){ + dap_chain_net_srv_order_t * l_order = dap_chain_net_srv_order_find_by_hash_str( l_net, l_order_hash_str ); + if (l_order && dap_chain_net_srv_order_delete_by_hash_str(l_net,l_order_hash_str) == 0){ + ret = 0 ; + dap_string_append_printf(l_string_ret,"Deleted order %s\n", l_order_hash_str ); + }else{ + ret = -8 ; + dap_string_append_printf(l_string_ret,"Can't find order with hash %s\n", l_order_hash_str ); + } + DAP_DELETE(l_order); + } else{ + ret = -9 ; + dap_string_append(l_string_ret,"need -hash param to obtain what the order we need to dump\n"); + } + }else if( dap_strcmp( l_order_str, "create" ) == 0 ){ + + if ( l_srv_uid_str && l_price_str && l_price_token_str && l_price_unit_str) { + dap_chain_net_srv_uid_t l_srv_uid={{0}}; + dap_chain_node_addr_t l_node_addr={0}; + dap_chain_hash_fast_t l_tx_cond_hash={{0}}; + dap_chain_time_t l_expires=0; // TS when the service expires + uint64_t l_price=0; + char l_price_token[DAP_CHAIN_TICKER_SIZE_MAX]={0}; + dap_chain_net_srv_price_unit_uid_t l_price_unit={{0}}; + dap_chain_net_srv_order_direction_t l_direction = SERV_DIR_UNDEFINED; + if ( l_direction_str ){ + if ( strcmp(l_direction_str, "sell") == 0 ){ + l_direction = SERV_DIR_SELL; + log_it(L_DEBUG, "Created order to sell"); + }else if ( strcmp(l_direction_str, "buy")==0){ + l_direction = SERV_DIR_BUY; + log_it(L_DEBUG, "Created order to buy"); + }else + log_it(L_WARNING, "Undefined order direction"); + } + + + if (l_expires_str) + l_expires = (dap_chain_time_t ) atoll( l_expires_str); + l_srv_uid.uint64 = (uint64_t) atoll( l_srv_uid_str); + if (l_node_addr_str){ + + if (dap_chain_node_addr_from_str( &l_node_addr, l_node_addr_str ) == 0 ) + log_it( L_DEBUG, "node addr " NODE_ADDR_FP_STR, NODE_ADDR_FP_ARGS_S(l_node_addr) ); + else + log_it( L_ERROR, "Can't parse \"%s\" as node addr"); + } + if (l_tx_cond_hash_str) + dap_chain_str_to_hash_fast (l_tx_cond_hash_str, &l_tx_cond_hash); + l_price = (uint64_t) atoll ( l_price_str ); + l_price_unit.uint32 = (uint32_t) atol ( l_price_unit_str ); + + char * l_order_new_hash_str = dap_chain_net_srv_order_create( + l_net,l_direction, l_srv_uid, l_node_addr,l_tx_cond_hash, l_price, l_price_unit, + l_price_token, l_expires,l_ext, l_region_str, l_continent_num); + if (l_order_new_hash_str) + dap_string_append_printf( l_string_ret, "Created order %s\n", l_order_new_hash_str); + else{ + dap_string_append_printf( l_string_ret, "Error! Can't created order\n"); + ret = -4; + } + } else { + dap_string_append_printf( l_string_ret, "Missed some required params\n"); + ret=-5; + } + } else { + dap_string_append_printf( l_string_ret, "Unknown subcommand \n"); + ret=-3; + } + dap_chain_node_cli_set_reply_text(a_str_reply, l_string_ret->str); + dap_string_free(l_string_ret, true); + } + + return ret; +} + +/** + * @brief dap_chain_net_srv_add + * @param a_uid + * @param a_callback_request + * @param a_callback_response_success + * @param a_callback_response_error + * @return + */ +dap_chain_net_srv_t* dap_chain_net_srv_add(dap_chain_net_srv_uid_t a_uid,dap_chain_net_srv_callback_data_t a_callback_request, + dap_chain_net_srv_callback_data_t a_callback_response_success, + dap_chain_net_srv_callback_data_t a_callback_response_error, + dap_chain_net_srv_callback_data_t a_callback_receipt_next_success) +{ + service_list_t *l_sdata = NULL; + dap_chain_net_srv_t * l_srv = NULL; + dap_chain_net_srv_uid_t l_uid = {.uint64 = a_uid.uint64 }; // Copy to let then compiler to pass args via registers not stack + pthread_mutex_lock(&s_srv_list_mutex); + HASH_FIND(hh, s_srv_list, &l_uid, sizeof(l_uid), l_sdata); + if(l_sdata == NULL) { + l_srv = DAP_NEW_Z(dap_chain_net_srv_t); + l_srv->uid.uint64 = a_uid.uint64; + l_srv->callback_requested = a_callback_request; + l_srv->callback_receipt_first_success = a_callback_response_success; + l_srv->callback_response_error = a_callback_response_error; + l_srv->callback_receipt_next_success = a_callback_receipt_next_success; + l_sdata = DAP_NEW_Z(service_list_t); + memcpy(&l_sdata->uid, &l_uid, sizeof(l_uid)); + l_sdata->srv = l_srv;//DAP_NEW(dap_chain_net_srv_t); + //memcpy(l_sdata->srv, l_srv, sizeof(dap_chain_net_srv_t)); + HASH_ADD(hh, s_srv_list, uid, sizeof(l_srv->uid), l_sdata); + }else{ + log_it(L_ERROR, "Already present service with 0x%016llX ", a_uid.uint64); + } + pthread_mutex_unlock(&s_srv_list_mutex); + return l_srv; +} + +/** + * @brief dap_chain_net_srv_set_ch_callbacks + * @param a_uid + * @param a_callback_stream_ch_opened + * @param a_callback_stream_ch_read + * @param a_callback_stream_ch_write + * @param a_callback_stream_ch_closed + * @return + */ +int dap_chain_net_srv_set_ch_callbacks(dap_chain_net_srv_uid_t a_uid, + dap_chain_net_srv_callback_ch_t a_callback_stream_ch_opened, + dap_chain_net_srv_callback_data_t a_callback_stream_ch_read, + dap_chain_net_srv_callback_ch_t a_callback_stream_ch_write, + dap_chain_net_srv_callback_ch_t a_callback_stream_ch_closed + ) +{ + service_list_t *l_sdata = NULL; + int l_ret =0; + dap_chain_net_srv_t * l_srv = NULL; + dap_chain_net_srv_uid_t l_uid = {.uint64 = a_uid.uint64 }; // Copy to let then compiler to pass args via registers not stack + + pthread_mutex_lock(&s_srv_list_mutex); + HASH_FIND(hh, s_srv_list, &l_uid, sizeof(l_uid), l_sdata); + if( l_sdata ) { + l_srv = l_sdata->srv; + l_srv->callback_stream_ch_opened = a_callback_stream_ch_opened; + l_srv->callback_stream_ch_read = a_callback_stream_ch_read; + l_srv->callback_stream_ch_write = a_callback_stream_ch_write; + l_srv->callback_stream_ch_closed = a_callback_stream_ch_closed; + }else{ + log_it(L_ERROR, "Can't find service with 0x%016llX", a_uid.uint64); + l_ret= -1; + } + pthread_mutex_unlock(&s_srv_list_mutex); + return l_ret; +} + + +/** + * @brief dap_chain_net_srv_del + * @param a_srv + */ +void dap_chain_net_srv_del(dap_chain_net_srv_t * a_srv) +{ + service_list_t *l_sdata; + pthread_mutex_lock(&s_srv_list_mutex); + HASH_FIND(hh, s_srv_list, a_srv, sizeof(dap_chain_net_srv_uid_t), l_sdata); + if(l_sdata) { + DAP_DELETE(l_sdata); + HASH_DEL(s_srv_list, l_sdata); + } + pthread_mutex_unlock(&s_srv_list_mutex); +} + +/** + * @brief dap_chain_net_srv_call_write_all + * @param a_client + */ +void dap_chain_net_srv_call_write_all(dap_stream_ch_t * a_client) +{ + service_list_t *l_sdata, *l_sdata_tmp; + pthread_mutex_lock(&s_srv_list_mutex); + HASH_ITER(hh, s_srv_list , l_sdata, l_sdata_tmp) + { + if ( l_sdata->srv->callback_stream_ch_write) + l_sdata->srv->callback_stream_ch_write(l_sdata->srv, a_client); + } + pthread_mutex_unlock(&s_srv_list_mutex); +} + +/** + * @brief dap_chain_net_srv_call_opened_all + * @param a_client + */ +void dap_chain_net_srv_call_opened_all(dap_stream_ch_t * a_client) +{ + service_list_t *l_sdata, *l_sdata_tmp; + pthread_mutex_lock(&s_srv_list_mutex); + HASH_ITER(hh, s_srv_list , l_sdata, l_sdata_tmp) + { + if ( l_sdata->srv->callback_stream_ch_opened) + l_sdata->srv->callback_stream_ch_opened(l_sdata->srv, a_client); + } + pthread_mutex_unlock(&s_srv_list_mutex); +} + +void dap_chain_net_srv_call_closed_all(dap_stream_ch_t * a_client) +{ + service_list_t *l_sdata, *l_sdata_tmp; + pthread_mutex_lock(&s_srv_list_mutex); + HASH_ITER(hh, s_srv_list , l_sdata, l_sdata_tmp) + { + if ( l_sdata->srv->callback_stream_ch_closed) + l_sdata->srv->callback_stream_ch_closed(l_sdata->srv, a_client); + } + pthread_mutex_unlock(&s_srv_list_mutex); +} + + + +/** + * @brief dap_chain_net_srv_del_all + * @param a_srv + */ +void dap_chain_net_srv_del_all(void) +{ + service_list_t *l_sdata, *l_sdata_tmp; + pthread_mutex_lock(&s_srv_list_mutex); + HASH_ITER(hh, s_srv_list , l_sdata, l_sdata_tmp) + { + DAP_DELETE(l_sdata); + HASH_DEL(s_srv_list, l_sdata); + } + pthread_mutex_unlock(&s_srv_list_mutex); +} + +/** + * @brief dap_chain_net_srv_get + * @param a_uid + * @return + */ +dap_chain_net_srv_t * dap_chain_net_srv_get(dap_chain_net_srv_uid_t a_uid) +{ + service_list_t *l_sdata = NULL; + pthread_mutex_lock(&s_srv_list_mutex); + HASH_FIND(hh, s_srv_list, &a_uid, sizeof(dap_chain_net_srv_uid_t), l_sdata); + pthread_mutex_unlock(&s_srv_list_mutex); + return (l_sdata) ? l_sdata->srv : NULL; +} + +/** + * @brief dap_chain_net_srv_count + * @return + */ + size_t dap_chain_net_srv_count(void) +{ + size_t l_count = 0; + service_list_t *l_sdata, *l_sdata_tmp; + pthread_mutex_lock(&s_srv_list_mutex); + HASH_ITER(hh, s_srv_list , l_sdata, l_sdata_tmp) + { + l_count++; + } + pthread_mutex_unlock(&s_srv_list_mutex); + return l_count; +} + +/** + * @brief dap_chain_net_srv_list + * @return + */ +const dap_chain_net_srv_uid_t * dap_chain_net_srv_list(void) +{ + static dap_chain_net_srv_uid_t *l_srv_uids = NULL; + static size_t l_count_last = 0; + size_t l_count_cur = 0; + dap_list_t *l_list = NULL; + service_list_t *l_sdata, *l_sdata_tmp; + pthread_mutex_lock(&s_srv_list_mutex); + // count the number of services and save them in list + HASH_ITER(hh, s_srv_list , l_sdata, l_sdata_tmp) + { + l_list = dap_list_append(l_list, l_sdata); + l_count_cur++; + } + // fill the output array + if(l_count_cur > 0) { + if(l_count_cur != l_count_last) { + DAP_DELETE(l_srv_uids); + l_srv_uids = DAP_NEW_SIZE(dap_chain_net_srv_uid_t, sizeof(dap_chain_net_srv_uid_t) * l_count_cur); + } + for(size_t i = 0; i < l_count_cur; i++) { + service_list_t *l_sdata = l_list->data; + memcpy(l_srv_uids + i, &l_sdata->uid, sizeof(dap_chain_net_srv_uid_t)); + } + } + // save new number of services + l_count_last = l_count_cur; + pthread_mutex_unlock(&s_srv_list_mutex); + dap_list_free(l_list); + return l_srv_uids; +} + +/** + * @brief dap_chain_net_srv_issue_receipt + * @param a_srv + * @param a_usage + * @param a_price + * @return + */ +dap_chain_datum_tx_receipt_t * dap_chain_net_srv_issue_receipt(dap_chain_net_srv_t *a_srv, + dap_chain_net_srv_usage_t * a_usage, + dap_chain_net_srv_price_t * a_price, + const void * a_ext, size_t a_ext_size + ) +{ + dap_chain_datum_tx_receipt_t * l_receipt = dap_chain_datum_tx_receipt_create( + a_srv->uid, a_price->units_uid, a_price->units, a_price->value_datoshi, a_ext, a_ext_size); + size_t l_receipt_size = l_receipt->size; // nested receipt plus 8 bits for type + + // Sign with our wallet + l_receipt_size = dap_chain_datum_tx_receipt_sign_add(&l_receipt,l_receipt_size , dap_chain_wallet_get_key( a_price->wallet,0) ); + + a_usage->receipt = l_receipt; + a_usage->receipt_size = l_receipt_size; + a_usage->wallet = a_price->wallet; + + return l_receipt; +} + + diff --git a/libdap-chain-net-srv/dap_chain_net_srv.h b/libdap-chain-net-srv/dap_chain_net_srv.h new file mode 100755 index 0000000000000000000000000000000000000000..f12364a226a910d30a82712af5df9da7eaac2d4f --- /dev/null +++ b/libdap-chain-net-srv/dap_chain_net_srv.h @@ -0,0 +1,98 @@ +/* +* Authors: +* Dmitriy Gerasimov <naeper@demlabs.net> +* Aleksandr Lysikov <alexander.lysikov@demlabs.net> +* Cellframe https://cellframe.net +* DeM Labs Inc. https://demlabs.net +* Copyright (c) 2017-2019 +* All rights reserved. + +This file is part of CellFrame SDK the open source project + +CellFrame SDK 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. + +CellFrame SDK 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 any CellFrame SDK based project. If not, see <http://www.gnu.org/licenses/>. +*/ +#pragma once + +#include "dap_config.h" +#include "dap_chain_net_srv_common.h" +#include "dap_chain_net_srv_client.h" +#include "dap_chain_net_srv_stream_session.h" + +typedef struct dap_chain_net_srv dap_chain_net_srv_t; + +typedef void (*dap_chain_net_srv_callback_t)(dap_chain_net_srv_t *, dap_chain_net_srv_client_t *); +typedef int (*dap_chain_net_srv_callback_data_t)(dap_chain_net_srv_t *, uint32_t, dap_chain_net_srv_client_t *, const void *, size_t ); +typedef void (*dap_chain_net_srv_callback_ch_t)(dap_chain_net_srv_t *, dap_stream_ch_t *); + +typedef struct dap_chain_net_srv +{ + dap_chain_net_srv_uid_t uid; // Unique ID for service. + dap_chain_net_srv_abstract_t srv_common; + dap_chain_net_srv_price_t *pricelist; + dap_chain_callback_trafic_t callback_trafic; + + // Request for usage + dap_chain_net_srv_callback_data_t callback_requested; + + // Receipt first sign successfull + dap_chain_net_srv_callback_data_t callback_receipt_first_success; + + // Response error + dap_chain_net_srv_callback_data_t callback_response_error; + + // Receipt next sign succesfull + dap_chain_net_srv_callback_data_t callback_receipt_next_success; + + // Stream CH callbacks - channed opened,ready for read, ready for write and closed + dap_chain_net_srv_callback_ch_t callback_stream_ch_opened; + dap_chain_net_srv_callback_data_t callback_stream_ch_read; + dap_chain_net_srv_callback_ch_t callback_stream_ch_write; + dap_chain_net_srv_callback_ch_t callback_stream_ch_closed; + + // Pointer to inheritor object + void * _inhertor; +} dap_chain_net_srv_t; +typedef void (*dap_chain_net_srv_callback_new_t)(dap_chain_net_srv_t *, dap_config_t *); + + +int dap_chain_net_srv_init(dap_config_t * a_cfg); +void dap_chain_net_srv_deinit(void); +dap_chain_net_srv_t* dap_chain_net_srv_add(dap_chain_net_srv_uid_t a_uid,dap_chain_net_srv_callback_data_t a_callback_requested, + dap_chain_net_srv_callback_data_t a_callback_response_success, + dap_chain_net_srv_callback_data_t a_callback_response_error, + dap_chain_net_srv_callback_data_t a_callback_receipt_next_success + ); + +int dap_chain_net_srv_set_ch_callbacks(dap_chain_net_srv_uid_t a_uid, + dap_chain_net_srv_callback_ch_t a_callback_stream_ch_opened, + dap_chain_net_srv_callback_data_t a_callback_stream_ch_read, + dap_chain_net_srv_callback_ch_t a_callback_stream_ch_write, + dap_chain_net_srv_callback_ch_t a_callback_stream_ch_closed + ); + +void dap_chain_net_srv_del(dap_chain_net_srv_t * a_srv); +void dap_chain_net_srv_del_all(void); + +void dap_chain_net_srv_call_write_all(dap_stream_ch_t * a_client); +void dap_chain_net_srv_call_closed_all(dap_stream_ch_t * a_client); +void dap_chain_net_srv_call_opened_all(dap_stream_ch_t * a_client); + +dap_chain_net_srv_t * dap_chain_net_srv_get(dap_chain_net_srv_uid_t a_uid); +size_t dap_chain_net_srv_count(void); +const dap_chain_net_srv_uid_t * dap_chain_net_srv_list(void); +dap_chain_datum_tx_receipt_t * dap_chain_net_srv_issue_receipt(dap_chain_net_srv_t *a_srv, + dap_chain_net_srv_usage_t * a_usage, + dap_chain_net_srv_price_t * a_price, const void * a_ext, size_t a_ext_size + ); + diff --git a/libdap-chain-net-srv/dap_chain_net_srv_client.c b/libdap-chain-net-srv/dap_chain_net_srv_client.c new file mode 100644 index 0000000000000000000000000000000000000000..35c69cbf8dcc240f7984acbde84ed96794debc5a --- /dev/null +++ b/libdap-chain-net-srv/dap_chain_net_srv_client.c @@ -0,0 +1,30 @@ +/* +* Authors: +* Dmitriy Gerasimov <naeper@demlabs.net> +* Cellframe https://cellframe.net +* DeM Labs Inc. https://demlabs.net +* Copyright (c) 2017-2019 +* All rights reserved. + +This file is part of CellFrame SDK the open source project + +CellFrame SDK 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. + +CellFrame SDK 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 any CellFrame SDK based project. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "dap_common.h" + +#include "dap_chain_net_srv_client.h" + + +#define LOG_TAG "dap_chain_net_srv_client" diff --git a/libdap-chain-net-srv/dap_chain_net_srv_client.h b/libdap-chain-net-srv/dap_chain_net_srv_client.h new file mode 100644 index 0000000000000000000000000000000000000000..d385f29c3d01aa4465bbb7448956a6667af758b2 --- /dev/null +++ b/libdap-chain-net-srv/dap_chain_net_srv_client.h @@ -0,0 +1,38 @@ +/* +* Authors: +* Dmitriy Gerasimov <naeper@demlabs.net> +* Cellframe https://cellframe.net +* DeM Labs Inc. https://demlabs.net +* Copyright (c) 2017-2019 +* All rights reserved. + +This file is part of CellFrame SDK the open source project + +CellFrame SDK 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. + +CellFrame SDK 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 any CellFrame SDK based project. If not, see <http://www.gnu.org/licenses/>. +*/ +#pragma once + +#include "dap_chain_net_srv_common.h" +#include "dap_chain_net_remote.h" + +typedef struct dap_chain_net_srv_client +{ + time_t ts_created; + dap_stream_ch_t *ch; // For direct connections + dap_chain_net_remote_t *net_remote; // For remotes + uint64_t bytes_received; + uint64_t bytes_sent; + struct dap_chain_net_srv_client *prev; + struct dap_chain_net_srv_client *next; +} dap_chain_net_srv_client_t; diff --git a/libdap-chain-net-srv/dap_chain_net_srv_common.c b/libdap-chain-net-srv/dap_chain_net_srv_common.c new file mode 100755 index 0000000000000000000000000000000000000000..494923184e111d6fe4ed0b24387fdac6b108f073 --- /dev/null +++ b/libdap-chain-net-srv/dap_chain_net_srv_common.c @@ -0,0 +1,48 @@ +/* + * Authors: + * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> + * Aleksandr Lysikov <alexander.lysikov@demlabs.net> + * DeM Labs Inc. https://demlabs.net + * Kelvin Project https://github.com/kelvinblockchain + * Copyright (c) 2019 + * All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) 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. + + DAP 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 any DAP based project. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <stdio.h> +#include <time.h> +#include <stdlib.h> +#include <stddef.h> +#include <stdint.h> + +#ifdef WIN32 +#include <winsock2.h> +#include <windows.h> +#include <mswsock.h> +#include <ws2tcpip.h> +#include <io.h> +#endif + +#include <pthread.h> + +#include "dap_strfuncs.h" +#include "rand/dap_rand.h" +#include "dap_chain_datum_tx_items.h" +#include "dap_stream.h" +#include "dap_chain_net_srv_common.h" + diff --git a/libdap-chain-net-srv/dap_chain_net_srv_common.h b/libdap-chain-net-srv/dap_chain_net_srv_common.h new file mode 100755 index 0000000000000000000000000000000000000000..94886a340cb6faf161b449ece43fca0587aa2319 --- /dev/null +++ b/libdap-chain-net-srv/dap_chain_net_srv_common.h @@ -0,0 +1,178 @@ +/* + * Authors: + * Aleksandr Lysikov <alexander.lysikov@demlabs.net> + * DeM Labs Inc. https://demlabs.net + * Kelvin Project https://github.com/kelvinblockchain + * Copyright (c) 2019 + * All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) 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. + + DAP 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 any DAP based project. If not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once +#include <stdint.h> +#include <stdbool.h> +#include "dap_common.h" +#include "dap_math_ops.h" +#include "dap_server.h" +#include "dap_stream_ch.h" +#include "dap_chain_common.h" +#include "dap_chain_ledger.h" +#include "dap_chain_net.h" +#include "dap_chain_wallet.h" + + + +//Units of service + + + +//Service direction +typedef enum dap_chain_net_srv_order_direction{ + SERV_DIR_BUY = 1, + SERV_DIR_SELL = 2, + SERV_DIR_UNDEFINED = 0 +} dap_chain_net_srv_order_direction_t; + + + + +typedef struct dap_chain_net_srv_abstract +{ + uint8_t class; //Class of service (once or permanent) + dap_chain_net_srv_uid_t type_id; //Type of service + union { + struct { + int bandwith; + int abuse_resistant; + size_t limit_bytes; + } vpn; + /*struct { + int value; + } another_srv;*/ + } proposal_params; + + //size_t pub_key_data_size; + //void * pub_key_data; + + uint64_t price; // service price, for SERV_CLASS_ONCE ONCE for the whole service, for SERV_CLASS_PERMANENT for one unit. + uint8_t price_units; // Unit of service (seconds, megabytes, etc.) Only for SERV_CLASS_PERMANENT + char decription[128]; +}DAP_ALIGN_PACKED dap_chain_net_srv_abstract_t; + +typedef void (*dap_chain_callback_trafic_t)(dap_client_remote_t *, dap_stream_ch_t *); + +typedef struct dap_chain_net_srv_price +{ + dap_chain_wallet_t * wallet; + char * net_name; + dap_chain_net_t * net; + uint64_t value_datoshi; + double value_coins; + char token[DAP_CHAIN_TICKER_SIZE_MAX]; + uint64_t units; + dap_chain_net_srv_price_unit_uid_t units_uid; + struct dap_chain_net_srv_price * next; + struct dap_chain_net_srv_price * prev; +} dap_chain_net_srv_price_t; + + +// Ch pkt types +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_REQUEST 0x01 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_SIGN_REQUEST 0x10 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_SIGN_RESPONSE 0x11 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_NOTIFY_STOPPED 0x20 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_DATA 0x30 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_SUCCESS 0xf0 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR 0xff + +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_UNDEFINED 0x00000000 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_SERVICE_NOT_FOUND 0x00000100 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_SERVICE_CH_NOT_FOUND 0x00000101 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_NETWORK_NOT_FOUND 0x00000200 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_NETWORK_NO_LEDGER 0x00000201 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_USAGE_CANT_ADD 0x00000300 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_TX_COND_NOT_FOUND 0x00000400 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_TX_COND_NO_COND_OUT 0x00000401 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_TX_COND_NOT_ENOUGH 0x00000402 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_TX_COND_NOT_ACCEPT_TOKEN 0x00000403 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_TX_COND_WRONG_SRV_UID 0x00000404 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_TX_COND_WRONG_SIZE 0x00000405 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_RECEIPT_CANT_FIND 0x00000500 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_RECEIPT_NO_SIGN 0x00000501 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_RECEIPT_WRONG_PKEY_HASH 0x00000502 +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_PRICE_NOT_FOUND 0x00000600 + +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_UNKNOWN 0xffffffff +// TYPE_REQUEST +typedef struct dap_stream_ch_chain_net_srv_pkt_request_hdr{ + dap_chain_net_id_t net_id;// Network id wheither to request + dap_chain_hash_fast_t tx_cond; // Conditioned transaction with paymemt for + dap_chain_net_srv_uid_t srv_uid; + char token[DAP_CHAIN_TICKER_SIZE_MAX]; +} DAP_ALIGN_PACKED dap_stream_ch_chain_net_srv_pkt_request_hdr_t; + +typedef struct dap_stream_ch_chain_net_srv_pkt_request{ + dap_stream_ch_chain_net_srv_pkt_request_hdr_t hdr; + uint8_t data[]; +} DAP_ALIGN_PACKED dap_stream_ch_chain_net_srv_pkt_request_t; + +// Custom service data packet +typedef struct dap_stream_ch_chain_net_srv_pkt_data_hdr{ + uint8_t version; + uint8_t offset[3]; + uint32_t usage_id; + dap_chain_net_srv_uid_t srv_uid; +} DAP_ALIGN_PACKED dap_stream_ch_chain_net_srv_pkt_data_hdr_t; + +typedef struct dap_stream_ch_chain_net_srv_pkt_data{ + dap_stream_ch_chain_net_srv_pkt_data_hdr_t hdr; + uint8_t data[]; +} DAP_ALIGN_PACKED dap_stream_ch_chain_net_srv_pkt_data_t; + + +typedef struct dap_stream_ch_chain_net_srv_pkt_success_hdr{ + uint32_t usage_id; + dap_chain_net_id_t net_id; + dap_chain_net_srv_uid_t srv_uid; +} DAP_ALIGN_PACKED dap_stream_ch_chain_net_srv_pkt_success_hdr_t; + +typedef struct dap_stream_ch_chain_net_srv_pkt_success{ + dap_stream_ch_chain_net_srv_pkt_success_hdr_t hdr; + uint8_t custom_data[]; +} DAP_ALIGN_PACKED dap_stream_ch_chain_net_srv_pkt_success_t; + +// TYPE_RESPONSE_ERROR +typedef struct dap_stream_ch_chain_net_srv_pkt_error{ + dap_chain_net_id_t net_id; + dap_chain_net_srv_uid_t srv_uid; + uint32_t usage_id; + uint32_t code; // error code +} DAP_ALIGN_PACKED dap_stream_ch_chain_net_srv_pkt_error_t; + + + + +DAP_STATIC_INLINE const char * dap_chain_net_srv_price_unit_uid_to_str( dap_chain_net_srv_price_unit_uid_t a_uid ) +{ + switch ( a_uid.enm) { + case SERV_UNIT_UNDEFINED: return "BYTE"; + case SERV_UNIT_MB: return "MEGABYTE"; + case SERV_UNIT_SEC: return "SECOND"; + case SERV_UNIT_DAY: return "DAY"; + default: return "UNKNOWN"; + } +} diff --git a/libdap-chain-net-srv/dap_chain_net_srv_order.c b/libdap-chain-net-srv/dap_chain_net_srv_order.c new file mode 100644 index 0000000000000000000000000000000000000000..104424e56a1842fd67c42f38aa0cd2ed3c938951 --- /dev/null +++ b/libdap-chain-net-srv/dap_chain_net_srv_order.c @@ -0,0 +1,425 @@ +/* + * Authors: + * Dmitrii Gerasimov <naeper@demlabs.net> + * DeM Labs Inc. https://demlabs.net + * Cellframe https://cellframe.net + * Copyright (c) 2017-2019 + * All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) 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. + + DAP 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 any DAP based project. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "dap_chain_net_srv_order.h" + +#include "dap_hash.h" +#include "dap_chain_global_db.h" + +#define LOG_TAG "dap_chain_net_srv_order" + +char *s_server_continents[]={ + "None", + "Africa", + "Europe", + "North America", + "South America", + "Southeast Asia", + "Near East", + }; + +/** + * @brief dap_chain_net_srv_order_init + * @return + */ +int dap_chain_net_srv_order_init(void) +{ + + return 0; +} + +/** + * @brief dap_chain_net_srv_order_deinit + */ +void dap_chain_net_srv_order_deinit() +{ + +} + +size_t dap_chain_net_srv_order_get_size(dap_chain_net_srv_order_t *a_order) +{ + return a_order ? sizeof(dap_chain_net_srv_order_t) + a_order->ext_size : 0; +} + +/** + * @brief dap_chain_net_srv_order_get_region_continent + * @param a_continent_num [in] + * @param a_region [in] + */ +bool dap_chain_net_srv_order_set_continent_region(dap_chain_net_srv_order_t **a_order, uint8_t a_continent_num, const char *a_region) +{ + dap_chain_net_srv_order_t *l_order = *a_order; + if(!l_order || (!a_continent_num && !a_region)) + return false; + uint8_t l_continent_num_prev = 0; + char *l_region_prev = NULL; + dap_chain_net_srv_order_get_continent_region(*a_order, &l_continent_num_prev, &l_region_prev); + uint32_t l_ext_size = 1 + sizeof(uint8_t); + if(a_region) + l_ext_size += strlen(a_region) + 1; + else if(l_region_prev) + l_ext_size += strlen(l_region_prev) + 1; + + l_order = DAP_REALLOC(l_order, sizeof(dap_chain_net_srv_order_t) + l_ext_size); + l_order->ext[0] =0x52; + if(a_continent_num > 0) + memcpy(l_order->ext + 1, &a_continent_num, sizeof(uint8_t)); + else + memcpy(l_order->ext + 1, &l_continent_num_prev, sizeof(uint8_t)); + if(a_region) + memcpy(l_order->ext + 1 + sizeof(uint8_t), a_region, strlen(a_region) + 1); + else if(l_region_prev) + memcpy(l_order->ext + 1 + sizeof(uint8_t), l_region_prev, strlen(l_region_prev) + 1); + //dap_sprintf(l_order->ext, "\52%d-%s", a_continent_num, a_region); + l_order->ext_size = l_ext_size; + *a_order = l_order; + DAP_DELETE(l_region_prev); + return true; +} + +/** + * @brief dap_chain_net_srv_order_get_region_continent + * @param a_continent_num [out] + * @param a_region [out] + */ +bool dap_chain_net_srv_order_get_continent_region(dap_chain_net_srv_order_t *a_order, uint8_t *a_continent_num, char **a_region) +{ + if(!a_order || !a_order->ext_size || !a_order->ext || a_order->ext[0]!=0x52) + return false; + if(a_continent_num) { + if((uint8_t)a_order->ext[1]!=0xff) + memcpy(a_continent_num, a_order->ext + 1, sizeof(uint8_t)); + else + a_continent_num = 0; + }else + a_continent_num = 0; + if(a_region) { + size_t l_size = a_order->ext_size - sizeof(uint8_t) - 1; + if(l_size > 0) { + *a_region = DAP_NEW_SIZE(char, l_size); + memcpy(*a_region, a_order->ext + 1 + sizeof(uint8_t), l_size); + } + else + *a_region = NULL; + } + return true; +} + + +/** + * @brief dap_chain_net_srv_order_continents_count + */ +size_t dap_chain_net_srv_order_continents_count(void) +{ + size_t l_count = sizeof(s_server_continents) / sizeof(char*); + return l_count; +} + +/** + * @brief dap_chain_net_srv_order_get_continent_str + */ +const char* dap_chain_net_srv_order_continent_to_str(int8_t a_num) +{ + int8_t l_count = dap_chain_net_srv_order_continents_count(); + if(a_num >= l_count) + return NULL; + return s_server_continents[a_num]; +} + +/** + * @brief dap_chain_net_srv_order_get_continent_num + */ +int8_t dap_chain_net_srv_order_continent_to_num(const char *a_continent_str) +{ + int8_t l_count = dap_chain_net_srv_order_continents_count(); + char *l_continent_str = dap_strup(a_continent_str, -1); + for(int8_t i = 1; i < l_count; i++) { + char *l_server_continents = dap_strup(s_server_continents[i], -1); + if(!dap_strcmp(l_continent_str, l_server_continents)) { + DAP_DELETE(l_server_continents); + DAP_DELETE(l_continent_str); + return i; + } + DAP_DELETE(l_server_continents); + } + DAP_DELETE(l_continent_str); + return -1; +} + +char* dap_chain_net_srv_order_create( + dap_chain_net_t * a_net, + dap_chain_net_srv_order_direction_t a_direction, + dap_chain_net_srv_uid_t a_srv_uid, // Service UID + dap_chain_node_addr_t a_node_addr, // Node address that servs the order (if present) + dap_chain_hash_fast_t a_tx_cond_hash, // Hash index of conditioned transaction attached with order + uint64_t a_price, // service price in datoshi, for SERV_CLASS_ONCE ONCE for the whole service, for SERV_CLASS_PERMANENT for one unit. + dap_chain_net_srv_price_unit_uid_t a_price_unit, // Unit of service (seconds, megabytes, etc.) Only for SERV_CLASS_PERMANENT + char a_price_ticker[DAP_CHAIN_TICKER_SIZE_MAX], + dap_chain_time_t a_expires, // TS when the service expires + const char *a_ext, + const char *a_region, + int8_t a_continent_num + ) +{ + if (a_net) { + dap_chain_net_srv_order_t *l_order = DAP_NEW_Z(dap_chain_net_srv_order_t); + dap_chain_hash_fast_t* l_order_hash = DAP_NEW_Z(dap_chain_hash_fast_t); + l_order->version = 1; + l_order->srv_uid = a_srv_uid; + l_order->direction = a_direction; + l_order->ts_created = (dap_chain_time_t) time(NULL); + + if(a_ext) + strncpy(l_order->ext, a_ext, strlen(l_order->ext) + 1); + else + dap_chain_net_srv_order_set_continent_region(&l_order, a_continent_num, a_region); + + if ( a_node_addr.uint64) + l_order->node_addr.uint64 = a_node_addr.uint64; + + memcpy(&l_order->tx_cond_hash, &a_tx_cond_hash, DAP_CHAIN_HASH_FAST_SIZE); + l_order->price = a_price; + l_order->price_unit.uint32 = a_price_unit.uint32; + + if ( a_price_ticker) + strncpy(l_order->price_ticker, a_price_ticker,sizeof(l_order->price_ticker)-1); + + size_t l_order_size = dap_chain_net_srv_order_get_size(l_order); + dap_hash_fast( l_order, l_order_size, l_order_hash ); + char * l_order_hash_str = dap_chain_hash_fast_to_str_new( l_order_hash ); + char * l_gdb_group_str = dap_chain_net_srv_order_get_gdb_group( a_net); + if ( !dap_chain_global_db_gr_set( dap_strdup(l_order_hash_str), l_order, l_order_size, l_gdb_group_str ) ){ + DAP_DELETE( l_order ); + DAP_DELETE( l_order_hash ); + DAP_DELETE( l_order_hash_str ); + DAP_DELETE( l_gdb_group_str ); + return NULL; + } + DAP_DELETE( l_order_hash ); + //DAP_DELETE(l_order_hash_str ); + DAP_DELETE( l_gdb_group_str ); + return l_order_hash_str; + }else + return NULL; +} + +/** + * @brief dap_chain_net_srv_order_update + * @param a_net + * @param a_order + * @return + */ +int dap_chain_net_srv_order_save(dap_chain_net_t * a_net, dap_chain_net_srv_order_t *a_order) +{ + if (!a_net || !a_order) + return -1; + + dap_chain_hash_fast_t l_order_hash; + size_t l_order_size = dap_chain_net_srv_order_get_size(a_order); + dap_hash_fast( a_order, l_order_size, &l_order_hash ); + char * l_order_hash_str = dap_chain_hash_fast_to_str_new(&l_order_hash); + char * l_gdb_group_str = dap_chain_net_srv_order_get_gdb_group( a_net); + if ( !dap_chain_global_db_gr_set(l_order_hash_str, a_order, l_order_size, l_gdb_group_str ) ){ + DAP_DELETE( l_gdb_group_str ); + return -1; + } + DAP_DELETE( l_gdb_group_str ); + return 0; +} + +/** + * @brief dap_chain_net_srv_order_find_by_hash_str + * @param a_net + * @param a_hash_str + * @return + */ +dap_chain_net_srv_order_t * dap_chain_net_srv_order_find_by_hash_str(dap_chain_net_t * a_net, const char * a_hash_str) +{ + dap_chain_net_srv_order_t * l_order = NULL; + if ( a_net && a_hash_str ){ + char * l_gdb_group_str = dap_chain_net_srv_order_get_gdb_group( a_net); + size_t l_order_size =0; + l_order = (dap_chain_net_srv_order_t *) dap_chain_global_db_gr_get(a_hash_str, &l_order_size, l_gdb_group_str ); + // check order size + if(l_order_size != dap_chain_net_srv_order_get_size(l_order)) { + log_it( L_ERROR, "Found wrong size order"); + DAP_DELETE( l_order ); + DAP_DELETE( l_gdb_group_str ); + return NULL; + } + DAP_DELETE( l_gdb_group_str ); + } + return l_order; +} + +/** + * @brief dap_chain_net_srv_order_find_all_by + * @param a_net + * @param a_srv_uid + * @param a_srv_class + * @param a_price_unit + * @param a_price_min + * @param a_price_max + * @param a_output_orders + * @param a_output_orders_count + * @return + */ +int dap_chain_net_srv_order_find_all_by(dap_chain_net_t * a_net,const dap_chain_net_srv_order_direction_t a_direction, + const dap_chain_net_srv_uid_t a_srv_uid, + const dap_chain_net_srv_price_unit_uid_t a_price_unit,const char a_price_ticker[DAP_CHAIN_TICKER_SIZE_MAX], + const uint64_t a_price_min, const uint64_t a_price_max, + dap_chain_net_srv_order_t ** a_output_orders, size_t * a_output_orders_count) +{ + if ( a_net && a_output_orders && a_output_orders_count ){ + char * l_gdb_group_str = dap_chain_net_srv_order_get_gdb_group( a_net); + size_t l_orders_count = 0; + dap_global_db_obj_t * l_orders = dap_chain_global_db_gr_load(l_gdb_group_str,&l_orders_count); + log_it( L_DEBUG ,"Loaded %zd orders", l_orders_count); + bool l_order_pass_first=true; + size_t l_order_passed_index = 0; + size_t l_orders_size = 0; +lb_order_pass: + l_order_passed_index = 0; + l_orders_size = 0; + for (size_t i=0; i< l_orders_count; i++){ + dap_chain_net_srv_order_t * l_order = (dap_chain_net_srv_order_t *) l_orders[i].value; + // Check direction + if (a_direction != SERV_DIR_UNDEFINED ) + if ( l_order->direction != a_direction ) + continue; + + // Check srv uid + if ( a_srv_uid.uint64 ) + if ( l_order->srv_uid.uint64 != a_srv_uid.uint64 ) + continue; + // check price unit + if ( a_price_unit.uint32 ) + if ( a_price_unit.uint32 != l_order->price_unit.uint32 ) + continue; + // Check price minimum + if ( a_price_min ) + if ( l_order->price < a_price_min ) + continue; + // Check price maximum + if ( a_price_max ) + if ( l_order->price > a_price_max ) + continue; + // Check ticker + if ( a_price_ticker ) + if ( strcmp( l_order->price_ticker, a_price_ticker) != 0 ) + continue; + + if( !l_order_pass_first ){ + size_t l_order_size = dap_chain_net_srv_order_get_size(l_order); + memcpy((char*)*a_output_orders + l_orders_size, l_order, l_order_size); + l_orders_size += l_order_size; + } + else + // calc size of all orders + l_orders_size += dap_chain_net_srv_order_get_size(l_order); + l_order_passed_index++; + + } + // Dirty goto usage ho ho ho + if (l_order_pass_first) { + l_order_pass_first = false; + *a_output_orders_count = l_order_passed_index; + *a_output_orders = DAP_NEW_Z_SIZE(dap_chain_net_srv_order_t, l_orders_size); + goto lb_order_pass; + } + // If we here - its the second pass through + + dap_chain_global_db_objs_delete(l_orders, l_orders_count); + DAP_DELETE( l_gdb_group_str); + return 0; + } + return -1; +} + +/** + * @brief dap_chain_net_srv_order_delete_by_hash_str + * @param a_net + * @param a_hash_str + * @return + */ +int dap_chain_net_srv_order_delete_by_hash_str(dap_chain_net_t * a_net, const char * a_hash_str ) +{ + int ret = -2; + if ( a_net && a_hash_str ){ + char * l_gdb_group_str = dap_chain_net_srv_order_get_gdb_group( a_net); + char * l_hash_str = strdup( a_hash_str ); + ret = dap_chain_global_db_gr_del( l_hash_str, l_gdb_group_str ) ? 0 : -1; + DAP_DELETE( l_gdb_group_str ); + } + return ret; +} + +/** + * @brief dap_chain_net_srv_order_dump_to_string + * @param a_orders + * @param a_str_out + */ +void dap_chain_net_srv_order_dump_to_string(dap_chain_net_srv_order_t *a_order,dap_string_t * a_str_out) +{ + if (a_order && a_str_out ){ + dap_chain_hash_fast_t l_hash; + char l_hash_str[DAP_CHAIN_HASH_FAST_SIZE * 2 + 4]; + dap_hash_fast(a_order, dap_chain_net_srv_order_get_size(a_order), &l_hash); + dap_chain_hash_fast_to_str(&l_hash,l_hash_str,sizeof(l_hash_str)-1); + dap_string_append_printf(a_str_out, "== Order %s ==\n", l_hash_str); + dap_string_append_printf(a_str_out, " version: %u\n", a_order->version ); + + switch ( a_order->direction) { + case SERV_DIR_UNDEFINED: dap_string_append_printf(a_str_out, " direction: SERV_DIR_UNDEFINED\n" ); break; + case SERV_DIR_SELL: dap_string_append_printf(a_str_out, " direction: SERV_DIR_SELL\n" ); break; + case SERV_DIR_BUY: dap_string_append_printf(a_str_out, " direction: SERV_DIR_BUY\n" ); break; + } + + dap_string_append_printf(a_str_out, " srv_uid: 0x%016llX\n", a_order->srv_uid.uint64 ); + dap_string_append_printf(a_str_out, " price: \u00a0%.7Lf (%llu)\n", dap_chain_balance_to_coins(a_order->price) , a_order->price); + if( a_order->price_unit.uint32 ) + dap_string_append_printf(a_str_out, " price_unit: %s\n", dap_chain_net_srv_price_unit_uid_to_str(a_order->price_unit) ); + if ( a_order->node_addr.uint64) + dap_string_append_printf(a_str_out, " node_addr: "NODE_ADDR_FP_STR"\n", NODE_ADDR_FP_ARGS_S(a_order->node_addr) ); + + char *l_region = NULL; + uint8_t l_continent_num = 0; + const char *l_continent_str = NULL; + if(dap_chain_net_srv_order_get_continent_region(a_order, &l_continent_num, &l_region)) + l_continent_str = dap_chain_net_srv_order_continent_to_str(l_continent_num); + dap_string_append_printf(a_str_out, " node_location: %s - %s\n", l_continent_str ? l_continent_str : "None" , l_region ? l_region : "None"); + DAP_DELETE(l_region); + + dap_chain_hash_fast_to_str(&a_order->tx_cond_hash,l_hash_str, sizeof(l_hash_str)-1); + dap_string_append_printf(a_str_out, " tx_cond_hash: %s\n", l_hash_str ); + char *l_ext_out = a_order->ext_size ? DAP_NEW_Z_SIZE(char, a_order->ext_size * 2 + 1) : NULL; + dap_bin2hex(l_ext_out, a_order->ext, a_order->ext_size); + if(l_ext_out) + dap_string_append_printf(a_str_out, " ext: 0x%s\n", l_ext_out); + else + dap_string_append_printf(a_str_out, " ext: 0x0\n"); + + DAP_DELETE(l_ext_out); + } +} diff --git a/libdap-chain-net-srv/dap_chain_net_srv_order.h b/libdap-chain-net-srv/dap_chain_net_srv_order.h new file mode 100644 index 0000000000000000000000000000000000000000..3aa88fd18145045a68c158a1edd97bd760588f22 --- /dev/null +++ b/libdap-chain-net-srv/dap_chain_net_srv_order.h @@ -0,0 +1,123 @@ +/* +* Authors: +* Dmitriy Gerasimov <naeper@demlabs.net> +* Cellframe https://cellframe.net +* DeM Labs Inc. https://demlabs.net +* Copyright (c) 2017-2019 +* All rights reserved. + +This file is part of CellFrame SDK the open source project + +CellFrame SDK 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. + +CellFrame SDK 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 any CellFrame SDK based project. If not, see <http://www.gnu.org/licenses/>. +*/ + +#pragma once +#include "dap_common.h" +#include "dap_string.h" +#include "dap_chain_common.h" +#include "dap_chain_net.h" +#include "dap_chain_net_srv_common.h" + +typedef struct dap_chain_net_srv_order +{ + uint16_t version; + dap_chain_net_srv_uid_t srv_uid; // Service UID + byte_t padding; // some padding + dap_chain_net_srv_order_direction_t direction:8; // Order direction - SELL or PURCHASE + dap_chain_node_addr_t node_addr; // Node address that servs the order (if present) + dap_chain_hash_fast_t tx_cond_hash; // Hash index of conditioned transaction attached with order + dap_chain_net_srv_price_unit_uid_t price_unit; // Unit of service (seconds, megabytes, etc.) Only for SERV_CLASS_PERMANENT + dap_chain_time_t ts_created; + dap_chain_time_t ts_expires; + uint64_t price; // service price in datoshi, for SERV_CLASS_ONCE ONCE for the whole service, for SERV_CLASS_PERMANENT for one unit. + char price_ticker[DAP_CHAIN_TICKER_SIZE_MAX]; // Token ticker to pay for service + //uint8_t continent; + //char region[32]; + uint32_t ext_size; + char ext[]; +} DAP_ALIGN_PACKED dap_chain_net_srv_order_t; + +// Init/deinit should be call only if private +int dap_chain_net_srv_order_init(void); +void dap_chain_net_srv_order_deinit(void); + +size_t dap_chain_net_srv_order_get_size(dap_chain_net_srv_order_t *a_order); + +bool dap_chain_net_srv_order_set_continent_region(dap_chain_net_srv_order_t **a_order, uint8_t a_continent_num, const char *a_region); +bool dap_chain_net_srv_order_get_continent_region(dap_chain_net_srv_order_t *a_order, uint8_t *a_continent_num, char **a_region); + +size_t dap_chain_net_srv_order_continents_count(void); +const char* dap_chain_net_srv_order_continent_to_str(int8_t a_num); +int8_t dap_chain_net_srv_order_continent_to_num(const char *l_continent_str); + +dap_chain_net_srv_order_t * dap_chain_net_srv_order_find_by_hash_str(dap_chain_net_t * a_net, const char * a_hash_str); + +DAP_STATIC_INLINE dap_chain_net_srv_order_t * dap_chain_net_srv_order_find_by_hash(dap_chain_net_t * a_net, dap_chain_hash_fast_t * a_hash) +{ + if ( a_net && a_hash ){ + char l_hash_str[DAP_CHAIN_HASH_FAST_SIZE * 2 + 4]; + dap_chain_hash_fast_to_str(a_hash,l_hash_str,sizeof(l_hash_str)-1); + return dap_chain_net_srv_order_find_by_hash_str(a_net, l_hash_str ); + } +} + +int dap_chain_net_srv_order_find_all_by(dap_chain_net_t * a_net,const dap_chain_net_srv_order_direction_t a_direction, const dap_chain_net_srv_uid_t a_srv_uid, + const dap_chain_net_srv_price_unit_uid_t a_price_unit, const char a_price_ticker[DAP_CHAIN_TICKER_SIZE_MAX],const uint64_t a_price_min, const uint64_t a_price_max, + dap_chain_net_srv_order_t ** a_output_orders, size_t * a_output_orders_count); +int dap_chain_net_srv_order_delete_by_hash_str( dap_chain_net_t * a_net,const char * a_hash_str ); + +/** + * @brief dap_chain_net_srv_order_delete_by_hash + * @param a_net + * @param a_hash + * @return + */ +DAP_STATIC_INLINE int dap_chain_net_srv_order_delete_by_hash(dap_chain_net_t * a_net, dap_chain_hash_fast_t * a_hash ) +{ + char l_hash_str[DAP_CHAIN_HASH_FAST_SIZE * 2 + 4]; + dap_chain_hash_fast_to_str(a_hash,l_hash_str,sizeof(l_hash_str)-1); + return dap_chain_net_srv_order_delete_by_hash_str ( a_net, l_hash_str); +} + +char* dap_chain_net_srv_order_create( + dap_chain_net_t * a_net, + dap_chain_net_srv_order_direction_t a_direction, + dap_chain_net_srv_uid_t a_srv_uid, // Service UID + dap_chain_node_addr_t a_node_addr, // Node address that servs the order (if present) + dap_chain_hash_fast_t a_tx_cond_hash, // Hash index of conditioned transaction attached with order + uint64_t a_price, // service price in datoshi, for SERV_CLASS_ONCE ONCE for the whole service, for SERV_CLASS_PERMANENT for one unit. + dap_chain_net_srv_price_unit_uid_t a_price_unit, // Unit of service (seconds, megabytes, etc.) Only for SERV_CLASS_PERMANENT + char a_price_ticker[DAP_CHAIN_TICKER_SIZE_MAX], + dap_chain_time_t a_expires, // TS when the service expires + const char *a_ext, + const char *a_region, + int8_t a_continent_num + ); + +int dap_chain_net_srv_order_save(dap_chain_net_t * a_net, dap_chain_net_srv_order_t *a_order); +void dap_chain_net_srv_order_dump_to_string(dap_chain_net_srv_order_t *a_order,dap_string_t * a_str_out); + +/** +* @brief dap_chain_net_srv_order_get_gdb_group_mempool +* @param l_chain +* @return +*/ +DAP_STATIC_INLINE char * dap_chain_net_srv_order_get_gdb_group(dap_chain_net_t * a_net) +{ + if ( a_net ) { + const char c_srv_order_group_str[]="service.orders"; + return dap_strdup_printf("%s.%s",a_net->pub.gdb_groups_prefix,c_srv_order_group_str); + } + return NULL; +} diff --git a/libdap-chain-net-srv/dap_chain_net_srv_stream_session.c b/libdap-chain-net-srv/dap_chain_net_srv_stream_session.c new file mode 100644 index 0000000000000000000000000000000000000000..f5f1517b0e884fc9ccb8dd4f6cc3e5c927a08d7b --- /dev/null +++ b/libdap-chain-net-srv/dap_chain_net_srv_stream_session.c @@ -0,0 +1,118 @@ +/* +* Authors: +* Dmitriy Gerasimov <naeper@demlabs.net> +* Cellframe https://cellframe.net +* DeM Labs Inc. https://demlabs.net +* Copyright (c) 2017-2019 +* All rights reserved. + +This file is part of CellFrame SDK the open source project + +CellFrame SDK 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. + +CellFrame SDK 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 any CellFrame SDK based project. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "dap_common.h" +#include "rand/dap_rand.h" +#include "dap_chain_net_srv.h" +#include "dap_chain_net_srv_stream_session.h" + +#define LOG_TAG "dap_stream_ch_chain_net_srv_session" + +/** + * @brief dap_chain_net_srv_stream_session_create + * @param a_session + * @return + */ +dap_chain_net_srv_stream_session_t * dap_chain_net_srv_stream_session_create( dap_stream_session_t * a_session) +{ + if (!a_session){ + log_it (L_ERROR, "Session is NULL!"); + return NULL; + } + dap_chain_net_srv_stream_session_t * l_session_srv= DAP_NEW_Z(dap_chain_net_srv_stream_session_t); + a_session->_inheritor = l_session_srv; + l_session_srv->parent = a_session; + log_it(L_NOTICE, "created service session"); + return l_session_srv; +} + +/** + * @brief dap_chain_net_srv_usage_add + * @param a_srv_session + * @param a_net + * @param a_srv + * @return + */ +dap_chain_net_srv_usage_t* dap_chain_net_srv_usage_add (dap_chain_net_srv_stream_session_t * a_srv_session, + dap_chain_net_t * a_net, dap_chain_net_srv_t * a_srv) +{ + if ( a_srv_session && a_net && a_srv ){ + dap_chain_net_srv_usage_t * l_ret = DAP_NEW_Z(dap_chain_net_srv_usage_t); + //l_ret->id = 666; + randombytes(&l_ret->id, sizeof(l_ret->id)); + l_ret->net = a_net; + l_ret->service = a_srv; + pthread_rwlock_init(&l_ret->rwlock,NULL); + pthread_mutex_lock(&a_srv_session->parent->mutex); + HASH_ADD_INT( a_srv_session->usages, id,l_ret ); + pthread_mutex_unlock(&a_srv_session->parent->mutex); + log_it( L_NOTICE, "Added service %s:0x%016llX , usage id: %d", l_ret->net->pub.name, a_srv->uid.uint64, l_ret->id); + return l_ret; + }else{ + log_it( L_ERROR, "Some NULLs was in input"); + return NULL; + } +} + +/** + * @brief dap_chain_net_srv_usage_delete + * @param a_srv_session + * @param a_usage + * @return + */ +void dap_chain_net_srv_usage_delete (dap_chain_net_srv_stream_session_t * a_srv_session, + dap_chain_net_srv_usage_t* a_usage) +{ + if ( a_usage->receipt ) + DAP_DELETE( a_usage->receipt ); + if ( a_usage->clients ){ + for (dap_chain_net_srv_client_t * l_srv_client = a_usage->clients, * tmp = NULL; l_srv_client; ){ + tmp = l_srv_client; + l_srv_client = l_srv_client->next; + DAP_DELETE( tmp); + } + + + } + pthread_mutex_lock(&a_srv_session->parent->mutex); + HASH_DEL(a_srv_session->usages, a_usage); + pthread_mutex_unlock(&a_srv_session->parent->mutex); + DAP_DELETE( a_usage ); +} + +/** + * @brief dap_chain_net_srv_usage_find + * @param a_srv_session + * @param a_usage_id + * @return + */ +dap_chain_net_srv_usage_t* dap_chain_net_srv_usage_find (dap_chain_net_srv_stream_session_t * a_srv_session, + uint32_t a_usage_id) +{ + dap_chain_net_srv_usage_t * l_ret = NULL; + pthread_mutex_lock(&a_srv_session->parent->mutex); + HASH_FIND_INT(a_srv_session->usages, &a_usage_id, l_ret); + pthread_mutex_unlock(&a_srv_session->parent->mutex); + return l_ret; +} diff --git a/libdap-chain-net-srv/dap_chain_net_srv_stream_session.h b/libdap-chain-net-srv/dap_chain_net_srv_stream_session.h new file mode 100644 index 0000000000000000000000000000000000000000..29c506e8ebf04197184a34d2e42f0b2edf74be42 --- /dev/null +++ b/libdap-chain-net-srv/dap_chain_net_srv_stream_session.h @@ -0,0 +1,89 @@ +/* +* Authors: +* Dmitriy Gerasimov <naeper@demlabs.net> +* Cellframe https://cellframe.net +* DeM Labs Inc. https://demlabs.net +* Copyright (c) 2017-2019 +* All rights reserved. + +This file is part of CellFrame SDK the open source project + +CellFrame SDK 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. + +CellFrame SDK 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 any CellFrame SDK based project. If not, see <http://www.gnu.org/licenses/>. +*/ +#pragma once + +#include "pthread.h" +#include "uthash.h" +#include "dap_stream_session.h" +#include "dap_hash.h" +#include "dap_chain.h" +#include "dap_sign.h" +#include "dap_chain_datum_tx.h" +#include "dap_chain_datum_tx_receipt.h" +//#include "dap_chain_net_srv.h" +#include "dap_chain_net_srv_order.h" +#include "dap_chain_net_srv_client.h" +#include "dap_chain_wallet.h" + +typedef struct dap_chain_net_srv dap_chain_net_srv_t; +typedef struct dap_chain_net_srv_usage{ + uint32_t id; // Usage id + pthread_rwlock_t rwlock; + time_t ts_created; // Created timpestamp + dap_chain_net_t * net; // Chain network where everything happens + dap_chain_wallet_t * wallet; + dap_chain_net_srv_t * service; // Service that used + + dap_chain_datum_tx_receipt_t* receipt; + dap_chain_datum_tx_receipt_t* receipt_next; // Receipt on the next units amount + dap_chain_net_srv_price_t * price; // Price for issue next receipt + size_t receipt_size; + size_t receipt_next_size; + dap_chain_net_srv_client_t * clients; + dap_chain_datum_tx_t * tx_cond; + dap_chain_hash_fast_t tx_cond_hash; + char token_ticker[DAP_CHAIN_TICKER_SIZE_MAX]; + bool is_active; + bool is_free; + UT_hash_handle hh; // +} dap_chain_net_srv_usage_t; + +typedef void (*dap_response_success_callback_t) (dap_stream_ch_chain_net_srv_pkt_success_t*, void*); + +typedef struct dap_chain_net_srv_stream_session { + dap_stream_session_t * parent; + dap_chain_net_srv_usage_t * usages; + dap_chain_net_srv_usage_t * usage_active; + + uint128_t limits_bytes; // Bytes left + time_t limits_ts; // Timestamp until its activte + dap_chain_net_srv_price_unit_uid_t limits_units_type; + + time_t ts_activated; + dap_sign_t* user_sign; // User's signature for auth if reconnect + + dap_response_success_callback_t response_success_callback; + void *response_success_callback_data; + +} dap_chain_net_srv_stream_session_t; + +#define DAP_CHAIN_NET_SRV_STREAM_SESSION(a) ((dap_chain_net_srv_stream_session_t *) (a)->_inheritor ) + +dap_chain_net_srv_stream_session_t * dap_chain_net_srv_stream_session_create( dap_stream_session_t * a_session); +dap_chain_net_srv_usage_t* dap_chain_net_srv_usage_add (dap_chain_net_srv_stream_session_t * a_srv_session, + dap_chain_net_t * a_net, dap_chain_net_srv_t * a_srv); +void dap_chain_net_srv_usage_delete (dap_chain_net_srv_stream_session_t * a_srv_session, + dap_chain_net_srv_usage_t* a_usage); +dap_chain_net_srv_usage_t* dap_chain_net_srv_usage_find (dap_chain_net_srv_stream_session_t * a_srv_session, + uint32_t a_usage_id);