diff --git a/COPYING.applications b/COPYING.applications deleted file mode 100644 index d511905c..00000000 --- a/COPYING.applications +++ /dev/null @@ -1,339 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 - - Copyright (C) 1989, 1991 Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -License is intended to guarantee your freedom to share and change free -software--to make sure the software is free for all its users. This -General Public License applies to most of the Free Software -Foundation's software and to any other program whose authors commit to -using it. (Some other Free Software Foundation software is covered by -the GNU Lesser General Public License instead.) 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 -this service 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 make restrictions that forbid -anyone to deny you these rights or to ask you to surrender the rights. -These restrictions translate to certain responsibilities for you if you -distribute copies of the software, or if you modify it. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must give the recipients all the rights that -you have. 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. - - We protect your rights with two steps: (1) copyright the software, and -(2) offer you this license which gives you legal permission to copy, -distribute and/or modify the software. - - Also, for each author's protection and ours, we want to make certain -that everyone understands that there is no warranty for this free -software. If the software is modified by someone else and passed on, we -want its recipients to know that what they have is not the original, so -that any problems introduced by others will not reflect on the original -authors' reputations. - - Finally, any free program is threatened constantly by software -patents. We wish to avoid the danger that redistributors of a free -program will individually obtain patent licenses, in effect making the -program proprietary. To prevent this, we have made it clear that any -patent must be licensed for everyone's free use or not licensed at all. - - The precise terms and conditions for copying, distribution and -modification follow. - - GNU GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License applies to any program or other work which contains -a notice placed by the copyright holder saying it may be distributed -under the terms of this General Public License. The "Program", below, -refers to any such program or work, and a "work based on the Program" -means either the Program or any derivative work under copyright law: -that is to say, a work containing the Program or a portion of it, -either verbatim or with modifications and/or translated into another -language. (Hereinafter, translation is included without limitation in -the term "modification".) Each licensee is addressed as "you". - -Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running the Program is not restricted, and the output from the Program -is covered only if its contents constitute a work based on the -Program (independent of having been made by running the Program). -Whether that is true depends on what the Program does. - - 1. You may copy and distribute 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 and disclaimer of warranty; keep intact all the -notices that refer to this License and to the absence of any warranty; -and give any other recipients of the Program a copy of this License -along with the Program. - -You may charge a fee for the physical act of transferring a copy, and -you may at your option offer warranty protection in exchange for a fee. - - 2. You may modify your copy or copies of the Program or any portion -of it, thus forming a work based on the Program, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) You must cause the modified files to carry prominent notices - stating that you changed the files and the date of any change. - - b) You must cause any work that you distribute or publish, that in - whole or in part contains or is derived from the Program or any - part thereof, to be licensed as a whole at no charge to all third - parties under the terms of this License. - - c) If the modified program normally reads commands interactively - when run, you must cause it, when started running for such - interactive use in the most ordinary way, to print or display an - announcement including an appropriate copyright notice and a - notice that there is no warranty (or else, saying that you provide - a warranty) and that users may redistribute the program under - these conditions, and telling the user how to view a copy of this - License. (Exception: if the Program itself is interactive but - does not normally print such an announcement, your work based on - the Program is not required to print an announcement.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Program, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Program, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Program. - -In addition, mere aggregation of another work not based on the Program -with the Program (or with a work based on the Program) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may copy and distribute the Program (or a work based on it, -under Section 2) in object code or executable form under the terms of -Sections 1 and 2 above provided that you also do one of the following: - - a) Accompany it with the complete corresponding machine-readable - source code, which must be distributed under the terms of Sections - 1 and 2 above on a medium customarily used for software interchange; or, - - b) Accompany it with a written offer, valid for at least three - years, to give any third party, for a charge no more than your - cost of physically performing source distribution, a complete - machine-readable copy of the corresponding source code, to be - distributed under the terms of Sections 1 and 2 above on a medium - customarily used for software interchange; or, - - c) Accompany it with the information you received as to the offer - to distribute corresponding source code. (This alternative is - allowed only for noncommercial distribution and only if you - received the program in object code or executable form with such - an offer, in accord with Subsection b above.) - -The source code for a work means the preferred form of the work for -making modifications to it. For an executable work, complete source -code means all the source code for all modules it contains, plus any -associated interface definition files, plus the scripts used to -control compilation and installation of the executable. However, as a -special exception, the source code distributed need not include -anything that is normally distributed (in either source or binary -form) with the major components (compiler, kernel, and so on) of the -operating system on which the executable runs, unless that component -itself accompanies the executable. - -If distribution of executable or object code is made by offering -access to copy from a designated place, then offering equivalent -access to copy the source code from the same place counts as -distribution of the source code, even though third parties are not -compelled to copy the source along with the object code. - - 4. You may not copy, modify, sublicense, or distribute the Program -except as expressly provided under this License. Any attempt -otherwise to copy, modify, sublicense or distribute the Program is -void, and will automatically terminate your rights under this License. -However, parties who have received copies, or rights, from you under -this License will not have their licenses terminated so long as such -parties remain in full compliance. - - 5. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Program or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Program (or any work based on the -Program), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Program or works based on it. - - 6. Each time you redistribute the Program (or any work based on the -Program), the recipient automatically receives a license from the -original licensor to copy, distribute or modify the Program subject to -these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties to -this License. - - 7. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -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 -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Program at all. For example, if a patent -license would not permit royalty-free redistribution of the Program by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Program. - -If any portion of this section is held invalid or unenforceable under -any particular circumstance, the balance of the section is intended to -apply and the section as a whole is intended to apply in other -circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system, which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 8. If the distribution and/or use of the Program is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Program under this License -may add an explicit geographical distribution limitation excluding -those countries, so that distribution is permitted only in or among -countries not thus excluded. In such case, this License incorporates -the limitation as if written in the body of this License. - - 9. The Free Software Foundation may publish revised and/or new versions -of the 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 a version number of this License which applies to it and "any -later version", you have the option of following the terms and conditions -either of that version or of any later version published by the Free -Software Foundation. If the Program does not specify a version number of -this License, you may choose any version ever published by the Free Software -Foundation. - - 10. If you wish to incorporate parts of the Program into other free -programs whose distribution conditions are different, write to the author -to ask for permission. For software which is copyrighted by the Free -Software Foundation, write to the Free Software Foundation; we sometimes -make exceptions for this. Our decision will be guided by the two goals -of preserving the free status of all derivatives of our free software and -of promoting the sharing and reuse of software generally. - - NO WARRANTY - - 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, 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. - - 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR -REDISTRIBUTE 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. - - 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 -convey the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -Also add information on how to contact you by electronic and paper mail. - -If the program is interactive, make it output a short notice like this -when it starts in an interactive mode: - - Gnomovision version 69, Copyright (C) year name of author - Gnomovision 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, the commands you use may -be called something other than `show w' and `show c'; they could even be -mouse-clicks or menu items--whatever suits your program. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the program - `Gnomovision' (which makes passes at compilers) written by James Hacker. - - , 1 April 1989 - Ty Coon, President of Vice - -This 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. diff --git a/COPYING.libraries b/COPYING.libraries deleted file mode 100644 index 2d2d780e..00000000 --- a/COPYING.libraries +++ /dev/null @@ -1,510 +0,0 @@ - - GNU LESSER GENERAL PUBLIC LICENSE - Version 2.1, February 1999 - - Copyright (C) 1991, 1999 Free Software Foundation, Inc. - 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - -[This is the first released version of the Lesser GPL. It also counts - as the successor of the GNU Library Public License, version 2, hence - the version number 2.1.] - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -Licenses are intended to guarantee your freedom to share and change -free software--to make sure the software is free for all its users. - - This license, the Lesser General Public License, applies to some -specially designated software packages--typically libraries--of the -Free Software Foundation and other authors who decide to use it. You -can use it too, but we suggest you first think carefully about whether -this license or the ordinary General Public License is the better -strategy to use in any particular case, based on the explanations -below. - - When we speak of free software, we are referring to freedom of use, -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 this service if you wish); that you receive source code or can get -it if you want it; that you can change the software and use pieces of -it in new free programs; and that you are informed that you can do -these things. - - To protect your rights, we need to make restrictions that forbid -distributors to deny you these rights or to ask you to surrender these -rights. These restrictions translate to certain responsibilities for -you if you distribute copies of the library or if you modify it. - - For example, if you distribute copies of the library, whether gratis -or for a fee, you must give the recipients all the rights that we gave -you. You must make sure that they, too, receive or can get the source -code. If you link other code with the library, you must provide -complete object files to the recipients, so that they can relink them -with the library after making changes to the library and recompiling -it. And you must show them these terms so they know their rights. - - We protect your rights with a two-step method: (1) we copyright the -library, and (2) we offer you this license, which gives you legal -permission to copy, distribute and/or modify the library. - - To protect each distributor, we want to make it very clear that -there is no warranty for the free library. Also, if the library is -modified by someone else and passed on, the recipients should know -that what they have is not the original version, so that the original -author's reputation will not be affected by problems that might be -introduced by others. - - Finally, software patents pose a constant threat to the existence of -any free program. We wish to make sure that a company cannot -effectively restrict the users of a free program by obtaining a -restrictive license from a patent holder. Therefore, we insist that -any patent license obtained for a version of the library must be -consistent with the full freedom of use specified in this license. - - Most GNU software, including some libraries, is covered by the -ordinary GNU General Public License. This license, the GNU Lesser -General Public License, applies to certain designated libraries, and -is quite different from the ordinary General Public License. We use -this license for certain libraries in order to permit linking those -libraries into non-free programs. - - When a program is linked with a library, whether statically or using -a shared library, the combination of the two is legally speaking a -combined work, a derivative of the original library. The ordinary -General Public License therefore permits such linking only if the -entire combination fits its criteria of freedom. The Lesser General -Public License permits more lax criteria for linking other code with -the library. - - We call this license the "Lesser" General Public License because it -does Less to protect the user's freedom than the ordinary General -Public License. It also provides other free software developers Less -of an advantage over competing non-free programs. These disadvantages -are the reason we use the ordinary General Public License for many -libraries. However, the Lesser license provides advantages in certain -special circumstances. - - For example, on rare occasions, there may be a special need to -encourage the widest possible use of a certain library, so that it -becomes a de-facto standard. To achieve this, non-free programs must -be allowed to use the library. A more frequent case is that a free -library does the same job as widely used non-free libraries. In this -case, there is little to gain by limiting the free library to free -software only, so we use the Lesser General Public License. - - In other cases, permission to use a particular library in non-free -programs enables a greater number of people to use a large body of -free software. For example, permission to use the GNU C Library in -non-free programs enables many more people to use the whole GNU -operating system, as well as its variant, the GNU/Linux operating -system. - - Although the Lesser General Public License is Less protective of the -users' freedom, it does ensure that the user of a program that is -linked with the Library has the freedom and the wherewithal to run -that program using a modified version of the Library. - - The precise terms and conditions for copying, distribution and -modification follow. Pay close attention to the difference between a -"work based on the library" and a "work that uses the library". The -former contains code derived from the library, whereas the latter must -be combined with the library in order to run. - - GNU LESSER GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License Agreement applies to any software library or other -program which contains a notice placed by the copyright holder or -other authorized party saying it may be distributed under the terms of -this Lesser General Public License (also called "this License"). -Each licensee is addressed as "you". - - A "library" means a collection of software functions and/or data -prepared so as to be conveniently linked with application programs -(which use some of those functions and data) to form executables. - - The "Library", below, refers to any such software library or work -which has been distributed under these terms. A "work based on the -Library" means either the Library or any derivative work under -copyright law: that is to say, a work containing the Library or a -portion of it, either verbatim or with modifications and/or translated -straightforwardly into another language. (Hereinafter, translation is -included without limitation in the term "modification".) - - "Source code" for a work means the preferred form of the work for -making modifications to it. For a library, complete source code means -all the source code for all modules it contains, plus any associated -interface definition files, plus the scripts used to control -compilation and installation of the library. - - Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running a program using the Library is not restricted, and output from -such a program is covered only if its contents constitute a work based -on the Library (independent of the use of the Library in a tool for -writing it). Whether that is true depends on what the Library does -and what the program that uses the Library does. - - 1. You may copy and distribute verbatim copies of the Library's -complete source code as you receive it, in any medium, provided that -you conspicuously and appropriately publish on each copy an -appropriate copyright notice and disclaimer of warranty; keep intact -all the notices that refer to this License and to the absence of any -warranty; and distribute a copy of this License along with the -Library. - - You may charge a fee for the physical act of transferring a copy, -and you may at your option offer warranty protection in exchange for a -fee. - - 2. You may modify your copy or copies of the Library or any portion -of it, thus forming a work based on the Library, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) The modified work must itself be a software library. - - b) You must cause the files modified to carry prominent notices - stating that you changed the files and the date of any change. - - c) You must cause the whole of the work to be licensed at no - charge to all third parties under the terms of this License. - - d) If a facility in the modified Library refers to a function or a - table of data to be supplied by an application program that uses - the facility, other than as an argument passed when the facility - is invoked, then you must make a good faith effort to ensure that, - in the event an application does not supply such function or - table, the facility still operates, and performs whatever part of - its purpose remains meaningful. - - (For example, a function in a library to compute square roots has - a purpose that is entirely well-defined independent of the - application. Therefore, Subsection 2d requires that any - application-supplied function or table used by this function must - be optional: if the application does not supply it, the square - root function must still compute square roots.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Library, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Library, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote -it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Library. - -In addition, mere aggregation of another work not based on the Library -with the Library (or with a work based on the Library) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may opt to apply the terms of the ordinary GNU General Public -License instead of this License to a given copy of the Library. To do -this, you must alter all the notices that refer to this License, so -that they refer to the ordinary GNU General Public License, version 2, -instead of to this License. (If a newer version than version 2 of the -ordinary GNU General Public License has appeared, then you can specify -that version instead if you wish.) Do not make any other change in -these notices. - - Once this change is made in a given copy, it is irreversible for -that copy, so the ordinary GNU General Public License applies to all -subsequent copies and derivative works made from that copy. - - This option is useful when you wish to copy part of the code of -the Library into a program that is not a library. - - 4. You may copy and distribute the Library (or a portion or -derivative of it, under Section 2) in object code or executable form -under the terms of Sections 1 and 2 above provided that you accompany -it with the complete corresponding machine-readable source code, which -must be distributed under the terms of Sections 1 and 2 above on a -medium customarily used for software interchange. - - If distribution of object code is made by offering access to copy -from a designated place, then offering equivalent access to copy the -source code from the same place satisfies the requirement to -distribute the source code, even though third parties are not -compelled to copy the source along with the object code. - - 5. A program that contains no derivative of any portion of the -Library, but is designed to work with the Library by being compiled or -linked with it, is called a "work that uses the Library". Such a -work, in isolation, is not a derivative work of the Library, and -therefore falls outside the scope of this License. - - However, linking a "work that uses the Library" with the Library -creates an executable that is a derivative of the Library (because it -contains portions of the Library), rather than a "work that uses the -library". The executable is therefore covered by this License. -Section 6 states terms for distribution of such executables. - - When a "work that uses the Library" uses material from a header file -that is part of the Library, the object code for the work may be a -derivative work of the Library even though the source code is not. -Whether this is true is especially significant if the work can be -linked without the Library, or if the work is itself a library. The -threshold for this to be true is not precisely defined by law. - - If such an object file uses only numerical parameters, data -structure layouts and accessors, and small macros and small inline -functions (ten lines or less in length), then the use of the object -file is unrestricted, regardless of whether it is legally a derivative -work. (Executables containing this object code plus portions of the -Library will still fall under Section 6.) - - Otherwise, if the work is a derivative of the Library, you may -distribute the object code for the work under the terms of Section 6. -Any executables containing that work also fall under Section 6, -whether or not they are linked directly with the Library itself. - - 6. As an exception to the Sections above, you may also combine or -link a "work that uses the Library" with the Library to produce a -work containing portions of the Library, and distribute that work -under terms of your choice, provided that the terms permit -modification of the work for the customer's own use and reverse -engineering for debugging such modifications. - - You must give prominent notice with each copy of the work that the -Library is used in it and that the Library and its use are covered by -this License. You must supply a copy of this License. If the work -during execution displays copyright notices, you must include the -copyright notice for the Library among them, as well as a reference -directing the user to the copy of this License. Also, you must do one -of these things: - - a) Accompany the work with the complete corresponding - machine-readable source code for the Library including whatever - changes were used in the work (which must be distributed under - Sections 1 and 2 above); and, if the work is an executable linked - with the Library, with the complete machine-readable "work that - uses the Library", as object code and/or source code, so that the - user can modify the Library and then relink to produce a modified - executable containing the modified Library. (It is understood - that the user who changes the contents of definitions files in the - Library will not necessarily be able to recompile the application - to use the modified definitions.) - - b) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (1) uses at run time a - copy of the library already present on the user's computer system, - rather than copying library functions into the executable, and (2) - will operate properly with a modified version of the library, if - the user installs one, as long as the modified version is - interface-compatible with the version that the work was made with. - - c) Accompany the work with a written offer, valid for at least - three years, to give the same user the materials specified in - Subsection 6a, above, for a charge no more than the cost of - performing this distribution. - - d) If distribution of the work is made by offering access to copy - from a designated place, offer equivalent access to copy the above - specified materials from the same place. - - e) Verify that the user has already received a copy of these - materials or that you have already sent this user a copy. - - For an executable, the required form of the "work that uses the -Library" must include any data and utility programs needed for -reproducing the executable from it. However, as a special exception, -the materials to be distributed need not include anything that is -normally distributed (in either source or binary form) with the major -components (compiler, kernel, and so on) of the operating system on -which the executable runs, unless that component itself accompanies -the executable. - - It may happen that this requirement contradicts the license -restrictions of other proprietary libraries that do not normally -accompany the operating system. Such a contradiction means you cannot -use both them and the Library together in an executable that you -distribute. - - 7. You may place library facilities that are a work based on the -Library side-by-side in a single library together with other library -facilities not covered by this License, and distribute such a combined -library, provided that the separate distribution of the work based on -the Library and of the other library facilities is otherwise -permitted, and provided that you do these two things: - - a) Accompany the combined library with a copy of the same work - based on the Library, uncombined with any other library - facilities. This must be distributed under the terms of the - Sections above. - - b) Give prominent notice with the combined library of the fact - that part of it is a work based on the Library, and explaining - where to find the accompanying uncombined form of the same work. - - 8. You may not copy, modify, sublicense, link with, or distribute -the Library except as expressly provided under this License. Any -attempt otherwise to copy, modify, sublicense, link with, or -distribute the Library is void, and will automatically terminate your -rights under this License. However, parties who have received copies, -or rights, from you under this License will not have their licenses -terminated so long as such parties remain in full compliance. - - 9. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Library or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Library (or any work based on the -Library), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Library or works based on it. - - 10. Each time you redistribute the Library (or any work based on the -Library), the recipient automatically receives a license from the -original licensor to copy, distribute, link with or modify the Library -subject to these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties with -this License. - - 11. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -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 -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Library at all. For example, if a patent -license would not permit royalty-free redistribution of the Library by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Library. - -If any portion of this section is held invalid or unenforceable under -any particular circumstance, the balance of the section is intended to -apply, and the section as a whole is intended to apply in other -circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 12. If the distribution and/or use of the Library is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Library under this License -may add an explicit geographical distribution limitation excluding those -countries, so that distribution is permitted only in or among -countries not thus excluded. In such case, this License incorporates -the limitation as if written in the body of this License. - - 13. The Free Software Foundation may publish revised and/or new -versions of the Lesser 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 Library -specifies a version number of this License which applies to it and -"any later version", you have the option of following the terms and -conditions either of that version or of any later version published by -the Free Software Foundation. If the Library does not specify a -license version number, you may choose any version ever published by -the Free Software Foundation. - - 14. If you wish to incorporate parts of the Library into other free -programs whose distribution conditions are incompatible with these, -write to the author to ask for permission. For software which is -copyrighted by the Free Software Foundation, write to the Free -Software Foundation; we sometimes make exceptions for this. Our -decision will be guided by the two goals of preserving the free status -of all derivatives of our free software and of promoting the sharing -and reuse of software generally. - - NO WARRANTY - - 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO -WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. -EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR -OTHER PARTIES PROVIDE THE LIBRARY "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 -LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME -THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN -WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY -AND/OR REDISTRIBUTE THE LIBRARY 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 -LIBRARY (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 LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF -SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH -DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Libraries - - If you develop a new library, and you want it to be of the greatest -possible use to the public, we recommend making it free software that -everyone can redistribute and change. You can do so by permitting -redistribution under these terms (or, alternatively, under the terms -of the ordinary General Public License). - - To apply these terms, attach the following notices to the library. -It is safest to attach them to the start of each source file to most -effectively convey the exclusion of warranty; and each file should -have at least the "copyright" line and a pointer to where the full -notice is found. - - - - Copyright (C) - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - -Also add information on how to contact you by electronic and paper mail. - -You should also get your employer (if you work as a programmer) or -your school, if any, to sign a "copyright disclaimer" for the library, -if necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the - library `Frob' (a library for tweaking knobs) written by James - Random Hacker. - - , 1 April 1990 - Ty Coon, President of Vice - -That's all there is to it! - - diff --git a/COPYRIGHT b/COPYRIGHT deleted file mode 100644 index ebfcd3ad..00000000 --- a/COPYRIGHT +++ /dev/null @@ -1,4 +0,0 @@ -Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - -Authors: Fabio M. Di Nitto - Federico Simoncelli diff --git a/Makefile.am b/Makefile.am deleted file mode 100644 index fc94e10b..00000000 --- a/Makefile.am +++ /dev/null @@ -1,213 +0,0 @@ -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -MAINTAINERCLEANFILES = Makefile.in aclocal.m4 configure depcomp \ - config.guess config.sub missing install-sh \ - ltmain.sh compile config.h.in config.h.in~ \ - autoscan.log configure.scan test-driver - -include $(top_srcdir)/build-aux/check.mk - -AUTOMAKE_OPTIONS = foreign - -AM_DISTCHECK_CONFIGURE_FLAGS = --enable-kronosnetd - -ACLOCAL_AMFLAGS = -I m4 - -SPEC = $(PACKAGE_NAME).spec - -DEBCHANGELOG = debian/changelog -DEBPOSTINST = debian/kronosnetd.postinst - -TARGZFILE = $(PACKAGE_NAME)-$(VERSION).tar.gz - -EXTRA_DIST = build-aux autogen.sh .version \ - debian $(DEBCHANGELOG) $(DEBPOSTINST) \ - $(SPEC) $(SPEC).in - -SUBDIRS = init libtap libknet kronosnetd - -if BUILD_DOCS -SUBDIRS += docs -endif - -if BUILD_POC -SUBDIRS += poc-code -endif - -dist_doc_DATA = \ - COPYING.applications \ - COPYING.libraries \ - COPYRIGHT \ - README.licence \ - README \ - TODO - -all-local: $(SPEC) $(DEBCHANGELOG) $(DEBPOSTINST) - -clean-local: - rm -f $(SPEC) - rm -f $(DEBPOSTINST) - -distclean-local: - rm -f $(PACKAGE_NAME)-*.tar.* - -maintainer-clean-local: - make -f debian/rules clean || true - rm -rf m4 $(DEBCHANGELOG) $(DEBPOSTINST) debian/patches - -## make rpm/srpm section. - -$(SPEC): $(SPEC).in .version config.status - rm -f $@-t $@ - date="$(shell LC_ALL=C date "+%a %b %d %Y")" && \ - if [ -f .tarball-version ]; then \ - gitver="$(shell cat .tarball-version)" && \ - rpmver=$$gitver && \ - alphatag="" && \ - dirty="" && \ - numcomm="0"; \ - else \ - gitver="$(shell GIT_DIR=$(abs_srcdir)/.git git describe --abbrev=4 --match='v*' HEAD 2>/dev/null)" && \ - rpmver=`echo $$gitver | sed -e "s/^v//" -e "s/-.*//g"` && \ - alphatag=`echo $$gitver | sed -e "s/.*-//" -e "s/^g//"` && \ - vtag=`echo $$gitver | sed -e "s/-.*//g"` && \ - numcomm=`GIT_DIR=$(abs_srcdir)/.git git rev-list $$vtag..HEAD | wc -l` && \ - GIT_DIR=$(abs_srcdir)/.git git update-index --refresh > /dev/null 2>&1 || true && \ - dirty=`GIT_DIR=$(abs_srcdir)/.git git diff-index --name-only HEAD 2>/dev/null`; \ - fi && \ - if [ -n "$$dirty" ]; then dirty="dirty"; else dirty=""; fi && \ - if [ "$$numcomm" = "0" ]; then \ - sed \ - -e "s#@version@#$$rpmver#g" \ - -e "s#%glo.*alpha.*##g" \ - -e "s#%glo.*numcomm.*##g" \ - -e "s#@dirty@#$$dirty#g" \ - -e "s#@date@#$$date#g" \ - $< > $@-t; \ - else \ - sed \ - -e "s#@version@#$$rpmver#g" \ - -e "s#@alphatag@#$$alphatag#g" \ - -e "s#@numcomm@#$$numcomm#g" \ - -e "s#@dirty@#$$dirty#g" \ - -e "s#@date@#$$date#g" \ - $< > $@-t; \ - fi; \ - if [ -z "$$dirty" ]; then sed -i -e "s#%glo.*dirty.*##g" $@-t; fi -if BUILD_KRONOSNETD - sed -i -e "s#@kronosnetd@#bcond_without#g" $@-t -else - sed -i -e "s#@kronosnetd@#bcond_with#g" $@-t -endif -if BUILD_LIBTAP - sed -i -e "s#@libtap@#bcond_without#g" $@-t -else - sed -i -e "s#@libtap@#bcond_with#g" $@-t -endif - sed -i -e "s#@defaultadmgroup@#$(DEFAULTADMGROUP)#g" $@-t - chmod a-w $@-t - mv $@-t $@ - -$(TARGZFILE): - $(MAKE) dist - -RPMBUILDOPTS = --define "_sourcedir $(abs_builddir)" \ - --define "_specdir $(abs_builddir)" \ - --define "_builddir $(abs_builddir)" \ - --define "_srcrpmdir $(abs_builddir)" \ - --define "_rpmdir $(abs_builddir)" - -srpm: clean - $(MAKE) $(SPEC) $(TARGZFILE) - rpmbuild $(RPMBUILDOPTS) --nodeps -bs $(SPEC) - -rpm: clean - $(MAKE) $(SPEC) $(TARGZFILE) - rpmbuild $(RPMBUILDOPTS) -ba $(SPEC) - -## make deb - -$(DEBPOSTINST): $(DEBPOSTINST).in config.h - mkdir -p debian - rm -f $@-t $@ - sed -e "s#@defaultadmgroup@#$(DEFAULTADMGROUP)#g" $< > $@-t - chmod a-w $@-t - mv $@-t $@ - -$(DEBCHANGELOG): $(DEBCHANGELOG).in .version - mkdir -p debian - rm -f $@-t $@ - date="$(shell LC_ALL=C date -R)" && \ - if [ -f .tarball-version ]; then \ - gitver="$(shell cat .tarball-version)" && \ - debver=$$gitver && \ - alphatag="" && \ - dirty="" && \ - numcomm="0"; \ - else \ - gitver="$(shell GIT_DIR=$(abs_srcdir)/.git git describe --abbrev=4 --match='v*' HEAD 2>/dev/null)" && \ - debver=`echo $$gitver | sed -e "s/^v//" -e "s/-.*//g"` && \ - alphatag=`echo $$gitver | sed -e "s/.*-//" -e "s/^g//"` && \ - vtag=`echo $$gitver | sed -e "s/-.*//g"` && \ - numcomm=`GIT_DIR=$(abs_srcdir)/.git git rev-list $$vtag..HEAD | wc -l` && \ - GIT_DIR=$(abs_srcdir)/.git git update-index --refresh > /dev/null 2>&1 || true && \ - dirty=`GIT_DIR=$(abs_srcdir)/.git git diff-index --name-only HEAD 2>/dev/null`; \ - fi && \ - if [ -n "$$dirty" ]; then dirty="-dirty"; else dirty=""; fi && \ - if [ "$$numcomm" = "0" ]; then \ - sed \ - -e "s#@version@#$$debver#g" \ - -e "s#-@alphatag@##g" \ - -e "s#.@numcomm@##g" \ - -e "s#@dirty@#$$dirty#g" \ - -e "s#@date@#$$date#g" \ - $< > $@-t; \ - else \ - sed \ - -e "s#@version@#$$debver#g" \ - -e "s#@alphatag@#$$alphatag#g" \ - -e "s#@numcomm@#$$numcomm#g" \ - -e "s#@dirty@#$$dirty#g" \ - -e "s#@date@#$$date#g" \ - $< > $@-t; \ - fi; - chmod a-w $@-t - mv $@-t $@ - -deb-clean: - make -f debian/rules clean - ./configure - -debs: deb-clean dist - if [ -f .tarball-version ]; then \ - gitver="$(shell cat .tarball-version)"; \ - else \ - gitver="$(shell git describe --abbrev=4 --match='v*' HEAD 2>/dev/null)" && \ - gitver=`echo $$gitver | sed -e "s/^v//" -e "s/-g/-/" -e "s/-/./"`; \ - fi && \ - cp $(TARGZFILE) ../$(PACKAGE_NAME)_$$gitver.orig.tar.gz - dpkg-buildpackage -uc -us - -# release/versioning -BUILT_SOURCES = .version -.version: - echo $(VERSION) > $@-t && mv $@-t $@ - -dist-hook: gen-ChangeLog - echo $(VERSION) > $(distdir)/.tarball-version - -gen_start_date = 2000-01-01 -.PHONY: gen-ChangeLog -gen-ChangeLog: - if test -d .git; then \ - LC_ALL=C $(top_srcdir)/build-aux/gitlog-to-changelog \ - --since=$(gen_start_date) > $(distdir)/cl-t; \ - rm -f $(distdir)/ChangeLog; \ - mv $(distdir)/cl-t $(distdir)/ChangeLog; \ - fi diff --git a/NOTES_TO_PACKAGE_MAINTAINERS b/NOTES_TO_PACKAGE_MAINTAINERS deleted file mode 100644 index b20b00a9..00000000 --- a/NOTES_TO_PACKAGE_MAINTAINERS +++ /dev/null @@ -1,27 +0,0 @@ -To: distribution package maintainers - -Those are a few things about this project that you should know. - -Both spec file and debian/ are up for adoption. The spec file -is most often up to date to handle current HEAD of the tree. -The debian/ directory is probably obsoleted vs current -Debian packaging guidelines. - -I surely welcome patches to support both in a better way. - -DO NOT ship kronosnetd. It's an experimental piece of super -crappy code. IF you decide to ship it anyway, you get to -be also the upstream maintainer :-) you have been warned. - -libtap is a simple commodity library used only by kronosnetd. -I don't mind to support it, but don't ship just for the fun of it. -It has no users, that I know of, outside of kronosnetd. - -libknet is still under heavy development. There might be more -onwire network changes that could make current version -incompatible with newer versions. Make sure to warn your users -about it. Plan is to release a stable version (1.0) when -we are confident that the current implementation is stable enough. - -Thanks -Your upstream maintainers diff --git a/README b/README deleted file mode 100644 index ca7bca38..00000000 --- a/README +++ /dev/null @@ -1,33 +0,0 @@ -kronosnet ---------- - -Architecture ------------- - -Please refer to -http://kronosnet.org/docs/kronosnet_architecture.odp -for a few fancy drawings and schemas - -WARNING -------- - -kronosnet is currently in very early stages of its development. -on-wire and configuration compatibility between versions will not -be guaranteed till 1.0 release. - -QUICK SETUP ------------ - -* build/install -* create /etc/pam.d/kronosnetd - - the users that can login with those credentials will gain access - to kronosnetd read only view of the setup -* add kronosnetadm system group - - user in this group, after login to kronosnetd (auth via pam), will - be allowed to perform configuration tasks within kronosnetd -* start kronosnetd - - there is no requirement for kronosnetd to have a configuration file - to start. Configuration can be created at runtime by: - "telnet localhost 50000" -* always refer to http://kronosnet.org/ user manual for latest - information on how to perform runtime configuration tasks diff --git a/README.licence b/README.licence deleted file mode 100644 index 237176f7..00000000 --- a/README.licence +++ /dev/null @@ -1,30 +0,0 @@ -For a detailed list of authors and copyright holders, please check the -included COPYRIGHT file. - -Libraries: - -You can redistribute them and/or modify them under the terms of the GNU Lesser -General Public License as published by the Free Software Foundation; either -version 2.1 of the License, or (at your option) any later version. - -The libraries are distributed in the hope that they will be useful, but WITHOUT -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more -details. - -Applications: - -You can redistribute them and/or modify them under the terms of the GNU General -Public License as published by the Free Software Foundation; either version -2 of the License, or (at your option) any later version. - -The applications are distributed in the hope that they 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. - -A copy of each license is included for your convenience in COPYING.applications -and COPYING.libraries. - -If missing, write to the Free Software Foundation, Inc., 51 Franklin St, -Fifth Floor, Boston, MA 02110-1301 USA. diff --git a/TODO b/TODO deleted file mode 100644 index 2fbd3dff..00000000 --- a/TODO +++ /dev/null @@ -1,4 +0,0 @@ -The TODO list has moved to: https://trello.com/b/Aqq9xJfF/todo - -Please read https://trello.com/c/JQIRSPE2/1-how-to-use-this-board - diff --git a/autogen.sh b/autogen.sh deleted file mode 100755 index 3c5e1d93..00000000 --- a/autogen.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -# Run this to generate all the initial makefiles, etc. -mkdir -p m4 -autoreconf -i -v && echo Now run ./configure and make diff --git a/build-aux/check.mk b/build-aux/check.mk deleted file mode 100644 index b2ff9ff7..00000000 --- a/build-aux/check.mk +++ /dev/null @@ -1,12 +0,0 @@ -VALGRIND = valgrind -q --error-exitcode=127 - -MEMCHECK = $(VALGRIND) --track-fds=yes --leak-check=full --suppressions=$(abs_top_srcdir)/build-aux/knet_valgrind_memcheck.supp -HELGRIND = $(VALGRIND) --tool=helgrind --suppressions=$(abs_top_srcdir)/build-aux/knet_valgrind_helgrind.supp - -check-memcheck: $(check_PROGRAMS) - export KNETMEMCHECK=yes && \ - $(MAKE) check LOG_COMPILE="libtool --mode=execute $(MEMCHECK)" - -check-helgrind: $(check_PROGRAMS) - export KNETHELGRIND=yes && \ - $(MAKE) check LOG_COMPILE="libtool --mode=execute $(HELGRIND)" diff --git a/build-aux/git-version-gen b/build-aux/git-version-gen deleted file mode 100755 index 795a98b3..00000000 --- a/build-aux/git-version-gen +++ /dev/null @@ -1,161 +0,0 @@ -#!/bin/sh -# Print a version string. -scriptversion=2010-10-13.20; # UTC - -# Copyright (C) 2007-2010 Free Software Foundation, Inc. -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -# This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/. -# It may be run two ways: -# - from a git repository in which the "git describe" command below -# produces useful output (thus requiring at least one signed tag) -# - from a non-git-repo directory containing a .tarball-version file, which -# presumes this script is invoked like "./git-version-gen .tarball-version". - -# In order to use intra-version strings in your project, you will need two -# separate generated version string files: -# -# .tarball-version - present only in a distribution tarball, and not in -# a checked-out repository. Created with contents that were learned at -# the last time autoconf was run, and used by git-version-gen. Must not -# be present in either $(srcdir) or $(builddir) for git-version-gen to -# give accurate answers during normal development with a checked out tree, -# but must be present in a tarball when there is no version control system. -# Therefore, it cannot be used in any dependencies. GNUmakefile has -# hooks to force a reconfigure at distribution time to get the value -# correct, without penalizing normal development with extra reconfigures. -# -# .version - present in a checked-out repository and in a distribution -# tarball. Usable in dependencies, particularly for files that don't -# want to depend on config.h but do want to track version changes. -# Delete this file prior to any autoconf run where you want to rebuild -# files to pick up a version string change; and leave it stale to -# minimize rebuild time after unrelated changes to configure sources. -# -# It is probably wise to add these two files to .gitignore, so that you -# don't accidentally commit either generated file. -# -# Use the following line in your configure.ac, so that $(VERSION) will -# automatically be up-to-date each time configure is run (and note that -# since configure.ac no longer includes a version string, Makefile rules -# should not depend on configure.ac for version updates). -# -# AC_INIT([GNU project], -# m4_esyscmd([build-aux/git-version-gen .tarball-version]), -# [bug-project@example]) -# -# Then use the following lines in your Makefile.am, so that .version -# will be present for dependencies, and so that .tarball-version will -# exist in distribution tarballs. -# -# BUILT_SOURCES = $(top_srcdir)/.version -# $(top_srcdir)/.version: -# echo $(VERSION) > $@-t && mv $@-t $@ -# dist-hook: -# echo $(VERSION) > $(distdir)/.tarball-version - -case $# in - 1|2) ;; - *) echo 1>&2 "Usage: $0 \$srcdir/.tarball-version" \ - '[TAG-NORMALIZATION-SED-SCRIPT]' - exit 1;; -esac - -tarball_version_file=$1 -tag_sed_script="${2:-s/x/x/}" -nl=' -' - -# Avoid meddling by environment variable of the same name. -v= - -# First see if there is a tarball-only version file. -# then try "git describe", then default. -if test -f $tarball_version_file -then - v=`cat $tarball_version_file` || exit 1 - case $v in - *$nl*) v= ;; # reject multi-line output - [0-9]*) ;; - *) v= ;; - esac - test -z "$v" \ - && echo "$0: WARNING: $tarball_version_file seems to be damaged" 1>&2 -fi - -if test -n "$v" -then - : # use $v -# Otherwise, if there is at least one git commit involving the working -# directory, and "git describe" output looks sensible, use that to -# derive a version string. -elif test "`git log -1 --pretty=format:x . 2>&1`" = x \ - && v=`git describe --abbrev=4 --match='v*' HEAD 2>/dev/null \ - || git describe --abbrev=4 HEAD 2>/dev/null` \ - && v=`printf '%s\n' "$v" | sed "$tag_sed_script"` \ - && case $v in - v[0-9]*) ;; - *) (exit 1) ;; - esac -then - # Is this a new git that lists number of commits since the last - # tag or the previous older version that did not? - # Newer: v6.10-77-g0f8faeb - # Older: v6.10-g0f8faeb - case $v in - *-*-*) : git describe is okay three part flavor ;; - *-*) - : git describe is older two part flavor - # Recreate the number of commits and rewrite such that the - # result is the same as if we were using the newer version - # of git describe. - vtag=`echo "$v" | sed 's/-.*//'` - numcommits=`git rev-list "$vtag"..HEAD | wc -l` - v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`; - ;; - esac - - # Change the first '-' to a '.', so version-comparing tools work properly. - # Remove the "g" in git describe's output string, to save a byte. - v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`; -else - v=UNKNOWN -fi - -v=`echo "$v" |sed 's/^v//'` - -# Don't declare a version "dirty" merely because a time stamp has changed. -git update-index --refresh > /dev/null 2>&1 - -dirty=`sh -c 'git diff-index --name-only HEAD' 2>/dev/null` || dirty= -case "$dirty" in - '') ;; - *) # Append the suffix only if there isn't one already. - case $v in - *-dirty) ;; - *) v="$v-dirty" ;; - esac ;; -esac - -# Omit the trailing newline, so that m4_esyscmd can use the result directly. -echo "$v" | tr -d "$nl" - -# Local variables: -# eval: (add-hook 'write-file-hooks 'time-stamp) -# time-stamp-start: "scriptversion=" -# time-stamp-format: "%:y-%02m-%02d.%02H" -# time-stamp-time-zone: "UTC" -# time-stamp-end: "; # UTC" -# End: diff --git a/build-aux/gitlog-to-changelog b/build-aux/gitlog-to-changelog deleted file mode 100755 index 7660af51..00000000 --- a/build-aux/gitlog-to-changelog +++ /dev/null @@ -1,191 +0,0 @@ -eval '(exit $?0)' && eval 'exec perl -wS "$0" ${1+"$@"}' - & eval 'exec perl -wS "$0" $argv:q' - if 0; -# Convert git log output to ChangeLog format. - -my $VERSION = '2009-10-30 13:46'; # UTC -# The definition above must lie within the first 8 lines in order -# for the Emacs time-stamp write hook (at end) to update it. -# If you change this file with Emacs, please let the write hook -# do its job. Otherwise, update this string manually. - -# Copyright (C) 2008-2010 Free Software Foundation, Inc. - -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -# Written by Jim Meyering - -use strict; -use warnings; -use Getopt::Long; -use POSIX qw(strftime); - -(my $ME = $0) =~ s|.*/||; - -# use File::Coda; # http://meyering.net/code/Coda/ -END { - defined fileno STDOUT or return; - close STDOUT and return; - warn "$ME: failed to close standard output: $!\n"; - $? ||= 1; -} - -sub usage ($) -{ - my ($exit_code) = @_; - my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR); - if ($exit_code != 0) - { - print $STREAM "Try `$ME --help' for more information.\n"; - } - else - { - print $STREAM < ChangeLog - $ME -- -n 5 foo > last-5-commits-to-branch-foo - -EOF - } - exit $exit_code; -} - -# If the string $S is a well-behaved file name, simply return it. -# If it contains white space, quotes, etc., quote it, and return the new string. -sub shell_quote($) -{ - my ($s) = @_; - if ($s =~ m![^\w+/.,-]!) - { - # Convert each single quote to '\'' - $s =~ s/\'/\'\\\'\'/g; - # Then single quote the string. - $s = "'$s'"; - } - return $s; -} - -sub quoted_cmd(@) -{ - return join (' ', map {shell_quote $_} @_); -} - -{ - my $since_date = '1970-01-01 UTC'; - my $format_string = '%s%n%b%n'; - GetOptions - ( - help => sub { usage 0 }, - version => sub { print "$ME version $VERSION\n"; exit }, - 'since=s' => \$since_date, - 'format=s' => \$format_string, - ) or usage 1; - - my @cmd = (qw (git log --log-size), "--since=$since_date", - '--pretty=format:%ct %an <%ae>%n%n'.$format_string, @ARGV); - open PIPE, '-|', @cmd - or die ("$ME: failed to run `". quoted_cmd (@cmd) ."': $!\n" - . "(Is your Git too old? Version 1.5.1 or later is required.)\n"); - - my $prev_date_line = ''; - while (1) - { - defined (my $in = ) - or last; - $in =~ /^log size (\d+)$/ - or die "$ME:$.: Invalid line (expected log size):\n$in"; - my $log_nbytes = $1; - - my $log; - my $n_read = read PIPE, $log, $log_nbytes; - $n_read == $log_nbytes - or die "$ME:$.: unexpected EOF\n"; - - my @line = split "\n", $log; - my $author_line = shift @line; - defined $author_line - or die "$ME:$.: unexpected EOF\n"; - $author_line =~ /^(\d+) (.*>)$/ - or die "$ME:$.: Invalid line " - . "(expected date/author/email):\n$author_line\n"; - - my $date_line = sprintf "%s $2\n", strftime ("%F", localtime ($1)); - # If this line would be the same as the previous date/name/email - # line, then arrange not to print it. - if ($date_line ne $prev_date_line) - { - $prev_date_line eq '' - or print "\n"; - print $date_line; - } - $prev_date_line = $date_line; - - # Omit "Signed-off-by..." lines. - @line = grep !/^Signed-off-by: .*>$/, @line; - - # If there were any lines - if (@line == 0) - { - warn "$ME: warning: empty commit message:\n $date_line\n"; - } - else - { - # Remove leading and trailing blank lines. - while ($line[0] =~ /^\s*$/) { shift @line; } - while ($line[$#line] =~ /^\s*$/) { pop @line; } - - # Prefix each non-empty line with a TAB. - @line = map { length $_ ? "\t$_" : '' } @line; - - print "\n", join ("\n", @line), "\n"; - } - - defined ($in = ) - or last; - $in ne "\n" - and die "$ME:$.: unexpected line:\n$in"; - } - - close PIPE - or die "$ME: error closing pipe from " . quoted_cmd (@cmd) . "\n"; - # FIXME-someday: include $PROCESS_STATUS in the diagnostic -} - -# Local Variables: -# mode: perl -# indent-tabs-mode: nil -# eval: (add-hook 'write-file-hooks 'time-stamp) -# time-stamp-start: "my $VERSION = '" -# time-stamp-format: "%:y-%02m-%02d %02H:%02M" -# time-stamp-time-zone: "UTC" -# time-stamp-end: "'; # UTC" -# End: diff --git a/build-aux/knet_valgrind_helgrind.supp b/build-aux/knet_valgrind_helgrind.supp deleted file mode 100644 index 116ee4cf..00000000 --- a/build-aux/knet_valgrind_helgrind.supp +++ /dev/null @@ -1,43 +0,0 @@ -{ - link enable/disable known race (safe to ignore) - Helgrind:Race - fun:_link_updown - fun:knet_link_set_enable - fun:test - fun:main -} -{ - link enable/disable known race (safe to ignore) - Helgrind:Race - fun:_handle_heartbt_thread - obj:/usr/lib64/valgrind/vgpreload_helgrind-amd64-linux.so - fun:start_thread - fun:clone -} -{ - helgrind glitch in parsing the heartbeat code - Helgrind:Race - fun:_handle_check_each - fun:_handle_heartbt_thread - obj:/usr/lib64/valgrind/vgpreload_helgrind-amd64-linux.so - fun:start_thread - fun:clone -} -{ - helgrind glitch in parsing the recv from links code - Helgrind:Race - fun:_parse_recv_from_links - fun:_handle_recv_from_links - fun:_handle_recv_from_links_thread - obj:/usr/lib64/valgrind/vgpreload_helgrind-amd64-linux.so - fun:start_thread - fun:clone -} -{ - helgrind glitch in parsing the PMTUd code - Helgrind:Race - fun:_handle_pmtud_link_thread - obj:/usr/lib64/valgrind/vgpreload_helgrind-amd64-linux.so - fun:start_thread - fun:clone -} diff --git a/build-aux/knet_valgrind_memcheck.supp b/build-aux/knet_valgrind_memcheck.supp deleted file mode 100644 index e69de29b..00000000 diff --git a/build-aux/release.mk b/build-aux/release.mk deleted file mode 100644 index c35ddc06..00000000 --- a/build-aux/release.mk +++ /dev/null @@ -1,76 +0,0 @@ -# to build official release tarballs, handle tagging and publish. - -# signing key -gpgsignkey= - -project=kronosnet - -all: checks setup tag tarballs sha256 sign - -checks: -ifeq (,$(version)) - @echo ERROR: need to define version= - @exit 1 -endif - @if [ ! -d .git ]; then \ - echo This script needs to be executed from top level cluster git tree; \ - exit 1; \ - fi - -setup: checks - ./autogen.sh - ./configure - make maintainer-clean - -tag: setup ./tag-$(version) - -tag-$(version): -ifeq (,$(release)) - @echo Building test release $(version), no tagging -else - git tag -a -m "v$(version) release" v$(version) HEAD - @touch $@ -endif - -tarballs: tag - ./autogen.sh - ./configure - #make distcheck (disabled.. needs root) - make dist - -sha256: tarballs $(project)-$(version).sha256 - -$(project)-$(version).sha256: -ifeq (,$(release)) - @echo Building test release $(version), no sha256 -else - sha256sum $(project)-$(version)*tar* | sort -k2 > $@ -endif - -sign: sha256 $(project)-$(version).sha256.asc - -$(project)-$(version).sha256.asc: $(project)-$(version).sha256 -ifeq (,$(gpgsignkey)) - @echo No GPG signing key defined -else -ifeq (,$(release)) - @echo Building test release $(version), no sign -else - gpg --default-key $(gpgsignkey) \ - --detach-sign \ - --armor \ - $< -endif -endif - -publish: -ifeq (,$(release)) - @echo Building test release $(version), no publishing! -else - @echo CHANGEME git push --tags origin - @echo CHANGEME scp $(project)-$(version).* \ - fedorahosted.org:$(project) -endif - -clean: - rm -rf $(project)-* tag-* diff --git a/configure.ac b/configure.ac deleted file mode 100644 index a4c73ccc..00000000 --- a/configure.ac +++ /dev/null @@ -1,401 +0,0 @@ -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Authors: Fabio M. Di Nitto -# Federico Simoncelli -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -# -*- Autoconf -*- -# Process this file with autoconf to produce a configure script. -# - -AC_PREREQ([2.63]) -AC_INIT([kronosnet], - m4_esyscmd([build-aux/git-version-gen .tarball-version]), - [devel@lists.kronosnet.org]) -AC_USE_SYSTEM_EXTENSIONS -AM_INIT_AUTOMAKE([1.11.1 dist-bzip2 dist-xz color-tests -Wno-portability subdir-objects]) -LT_PREREQ([2.2.6]) -LT_INIT - -AC_CONFIG_MACRO_DIR([m4]) -AC_CONFIG_SRCDIR([kronosnetd/main.c]) -AC_CONFIG_HEADERS([config.h]) - -AC_CANONICAL_HOST -AC_PROG_LIBTOOL - -AC_LANG([C]) - -systemddir=${prefix}/lib/systemd/system - -if test "$prefix" = "NONE"; then - prefix="/usr" - if test "$localstatedir" = "\${prefix}/var"; then - localstatedir="/var" - fi - if test "$sysconfdir" = "\${prefix}/etc"; then - sysconfdir="/etc" - fi - if test "$systemddir" = "NONE/lib/systemd/system"; then - systemddir=/lib/systemd/system - fi - if test "$libdir" = "\${exec_prefix}/lib"; then - if test -e /usr/lib64; then - libdir="/usr/lib64" - else - libdir="/usr/lib" - fi - fi -fi - -# Checks for programs. -if ! ${MAKE-make} --version /cannot/make/this >/dev/null 2>&1; then - AC_MSG_ERROR(["you don't seem to have GNU make; it is required"]) -fi - -AC_PROG_AWK -AC_PROG_GREP -AC_PROG_SED -AC_PROG_CPP -AC_PROG_CC -AC_PROG_CC_C99 -if test "x$ac_cv_prog_cc_c99" = "xno"; then - AC_MSG_ERROR(["C99 support is required"]) -fi -AC_PROG_LN_S -AC_PROG_INSTALL -AC_PROG_MAKE_SET -AC_PROG_CXX -AC_PROG_RANLIB -AC_CHECK_PROGS([PUBLICAN], [publican], [:]) -AC_CHECK_PROGS([PKGCONFIG], [pkg-config]) - -AC_ARG_ENABLE([poc], - [ --enable-poc : build poc code ],, - [ enable_poc="yes" ]) -AM_CONDITIONAL([BUILD_POC], test x$enable_poc = xyes) - -AC_ARG_ENABLE([kronosnetd], - [ --enable-kronosnetd : Kronosnetd support ],, - [ enable_kronosnetd="no" ]) -AM_CONDITIONAL([BUILD_KRONOSNETD], test x$enable_kronosnetd = xyes) - -AC_ARG_ENABLE([libtap], - [ --enable-libtap : libtap support ],, - [ enable_libtap="no" ]) - -if test "x$enable_kronosnetd" = xyes; then - enable_libtap=yes -fi -AM_CONDITIONAL([BUILD_LIBTAP], test x$enable_libtap = xyes) - -AC_ARG_ENABLE([libknet-sctp], - [ --enable-libknet-sctp : libknet SCTP support ],, - [ enable_libknet_sctp="yes" ]) - -## local helper functions -# this function checks if CC support options passed as -# args. Global CFLAGS are ignored during this test. -cc_supports_flag() { - saveCPPFLAGS="$CPPFLAGS" - CPPFLAGS="$@" - if echo $CC | grep -q clang; then - CPPFLAGS="-Werror $CPPFLAGS" - fi - AC_MSG_CHECKING([whether $CC supports "$@"]) - AC_PREPROC_IFELSE([AC_LANG_PROGRAM([])], - [RC=0; AC_MSG_RESULT([yes])], - [RC=1; AC_MSG_RESULT([no])]) - CPPFLAGS="$saveCPPFLAGS" - return $RC -} - -# helper macro to check libs without adding them to LIBS -check_lib_no_libs() { - lib_no_libs_arg1=$1 - shift - lib_no_libs_arg2=$1 - shift - lib_no_libs_args=$@ - AC_CHECK_LIB([$lib_no_libs_arg1], - [$lib_no_libs_arg2],,, - [$lib_no_libs_args]) - LIBS=$ac_check_lib_save_LIBS -} - -# Checks for C features -AC_C_INLINE - -# Checks for libraries. -AC_CHECK_LIB([pthread], [pthread_create]) -AC_CHECK_LIB([m], [ceil]) -AC_CHECK_LIB([rt], [clock_gettime]) - -PKG_CHECK_MODULES([nss],[nss]) - -# Checks for header files. -AC_CHECK_HEADERS([fcntl.h]) -AC_CHECK_HEADERS([stdlib.h]) -AC_CHECK_HEADERS([string.h]) -AC_CHECK_HEADERS([strings.h]) -AC_CHECK_HEADERS([sys/ioctl.h]) -AC_CHECK_HEADERS([syslog.h]) -AC_CHECK_HEADERS([unistd.h]) -AC_CHECK_HEADERS([netinet/in.h]) -AC_CHECK_HEADERS([sys/socket.h]) -AC_CHECK_HEADERS([arpa/inet.h]) -AC_CHECK_HEADERS([netdb.h]) -AC_CHECK_HEADERS([limits.h]) -AC_CHECK_HEADERS([stdint.h]) -AC_CHECK_HEADERS([sys/epoll.h]) - -if test "x$enable_libknet_sctp" = xyes; then -AC_CHECK_HEADERS([netinet/sctp.h],, AC_MSG_ERROR(["missing required SCTP headers"])) -fi - -# Checks for typedefs, structures, and compiler characteristics. -AC_C_INLINE -AC_TYPE_SIZE_T -AC_TYPE_PID_T -AC_TYPE_SSIZE_T -AC_TYPE_UINT8_T -AC_TYPE_UINT16_T -AC_TYPE_UINT32_T -AC_TYPE_UINT64_T -AC_TYPE_INT32_T - -# Checks for library functions. -AC_FUNC_ALLOCA -AC_FUNC_FORK -AC_FUNC_MALLOC -AC_FUNC_REALLOC -AC_CHECK_FUNCS([memset]) -AC_CHECK_FUNCS([strdup]) -AC_CHECK_FUNCS([strerror]) -AC_CHECK_FUNCS([dup2]) -AC_CHECK_FUNCS([select]) -AC_CHECK_FUNCS([socket]) -AC_CHECK_FUNCS([inet_ntoa]) -AC_CHECK_FUNCS([memmove]) -AC_CHECK_FUNCS([strchr]) -AC_CHECK_FUNCS([atexit]) -AC_CHECK_FUNCS([ftruncate]) -AC_CHECK_FUNCS([strrchr]) -AC_CHECK_FUNCS([strstr]) -AC_CHECK_FUNCS([clock_gettime]) -AC_CHECK_FUNCS([strcasecmp]) -AC_CHECK_FUNCS([kevent]) -# if neither sys/epoll.h nor kevent are present, we should fail. - -if test "x$ac_cv_header_sys_epoll_h" = xno && test "x$ac_cv_func_kevent" = xno; then - AC_MSG_ERROR([Both epoll and kevent unavailable on this OS]) -fi - -if test "x$ac_cv_header_sys_epoll_h" = xyes && test "x$ac_cv_func_kevent" = xyes; then - AC_MSG_ERROR([Both epoll and kevent available on this OS, please contact the maintainers to fix the code]) -fi - -# checks (for kronosnetd) -if test "x$enable_kronosnetd" = xyes; then - -AC_CHECK_HEADERS([security/pam_appl.h], - [AC_CHECK_LIB([pam], [pam_start])], - [AC_MSG_ERROR([Unable to find LinuxPAM devel files])]) - -AC_CHECK_HEADERS([security/pam_misc.h], - [AC_CHECK_LIB([pam_misc], [misc_conv])], - [AC_MSG_ERROR([Unable to find LinuxPAM MISC devel files])]) - -PKG_CHECK_MODULES([libqb], [libqb]) - -AC_CHECK_LIB([qb], [qb_log_thread_priority_set], - [have_qb_log_thread_priority_set="yes"], - [have_qb_log_thread_priority_set="no"]) - -if test "x${have_qb_log_thread_priority_set}" = xyes; then - AC_DEFINE_UNQUOTED([HAVE_QB_LOG_THREAD_PRIORITY_SET], 1, [have qb_log_thread_priority_set]) -fi - -fi - -# local options -AC_ARG_ENABLE([debug], - [ --enable-debug enable debug build. ], - [ default="no" ]) - -AC_ARG_ENABLE([publicandocs], - [ --enable-publicandocs enable docs build. ], - [ default="no" ]) - -AC_ARG_WITH([initdefaultdir], - [ --with-initdefaultdir : path to /etc/sysconfig/.. or /etc/default dir. ], - [ INITDEFAULTDIR="$withval" ], - [ INITDEFAULTDIR="$sysconfdir/default" ]) - -AC_ARG_WITH([initddir], - [ --with-initddir=DIR : path to init script directory. ], - [ INITDDIR="$withval" ], - [ INITDDIR="$sysconfdir/init.d" ]) - -AC_ARG_WITH([systemddir], - [ --with-systemddir=DIR : path to systemd unit files directory. ], - [ SYSTEMDDIR="$withval" ], - [ SYSTEMDDIR="$systemddir" ]) - -AC_ARG_WITH([syslogfacility], - [ --with-syslogfacility=FACILITY - default syslog facility. ], - [ SYSLOGFACILITY="$withval" ], - [ SYSLOGFACILITY="LOG_DAEMON" ]) - -AC_ARG_WITH([sysloglevel], - [ --with-sysloglevel=LEVEL - default syslog level. ], - [ SYSLOGLEVEL="$withval" ], - [ SYSLOGLEVEL="LOG_INFO" ]) - -AC_ARG_WITH([defaultadmgroup], - [ --with-defaultadmgroup=GROUP - define PAM group. Users part of this group will be - allowed to configure kronosnet. Others will only - receive read-only rights. ], - [ DEFAULTADMGROUP="$withval" ], - [ DEFAULTADMGROUP="kronosnetadm" ]) - -## random vars -LOGDIR=${localstatedir}/log/ -RUNDIR=${localstatedir}/run/ -DEFAULT_CONFIG_DIR=${sysconfdir}/kronosnet - -## do subst - -AM_CONDITIONAL([BUILD_DOCS], [test "x${enable_publicandocs}" = xyes]) -AM_CONDITIONAL([DEBUG], [test "x${enable_debug}" = xyes]) - -AC_SUBST([DEFAULT_CONFIG_DIR]) -AC_SUBST([INITDEFAULTDIR]) -AC_SUBST([INITDDIR]) -AC_SUBST([SYSTEMDDIR]) -AC_SUBST([LOGDIR]) -AC_SUBST([DEFAULTADMGROUP]) - -AC_DEFINE_UNQUOTED([DEFAULT_CONFIG_DIR], - ["$(eval echo ${DEFAULT_CONFIG_DIR})"], - [Default config directory]) - -AC_DEFINE_UNQUOTED([DEFAULT_CONFIG_FILE], - ["$(eval echo ${DEFAULT_CONFIG_DIR}/kronosnetd.conf)"], - [Default config file]) - -AC_DEFINE_UNQUOTED([LOGDIR], - ["$(eval echo ${LOGDIR})"], - [Default logging directory]) - -AC_DEFINE_UNQUOTED([DEFAULT_LOG_FILE], - ["$(eval echo ${LOGDIR}/kronosnetd.log)"], - [Default log file]) - -AC_DEFINE_UNQUOTED([RUNDIR], - ["$(eval echo ${RUNDIR})"], - [Default run directory]) - -AC_DEFINE_UNQUOTED([SYSLOGFACILITY], - [$(eval echo ${SYSLOGFACILITY})], - [Default syslog facility]) - -AC_DEFINE_UNQUOTED([SYSLOGLEVEL], - [$(eval echo ${SYSLOGLEVEL})], - [Default syslog level]) - -AC_DEFINE_UNQUOTED([DEFAULTADMGROUP], - ["$(eval echo ${DEFAULTADMGROUP})"], - [Default admin group]) - -## *FLAGS handling -ENV_CFLAGS="$CFLAGS" -ENV_CPPFLAGS="$CPPFLAGS" -ENV_LDFLAGS="$LDFLAGS" - -# debug build stuff -if test "x${enable_debug}" = xyes; then - AC_DEFINE_UNQUOTED([DEBUG], [1], [Compiling Debugging code]) - OPT_CFLAGS="-O0" -else - OPT_CFLAGS="-O3" -fi - -# gdb flags -if test "x${GCC}" = xyes; then - GDB_FLAGS="-ggdb3" -else - GDB_FLAGS="-g" -fi - -# extra warnings -EXTRA_WARNINGS="" - -WARNLIST=" - all - shadow - missing-prototypes - missing-declarations - strict-prototypes - declaration-after-statement - pointer-arith - write-strings - cast-align - bad-function-cast - missing-format-attribute - format=2 - format-security - format-nonliteral - no-long-long - unsigned-char - gnu89-inline - no-strict-aliasing - error - address - cpp - overflow - parentheses - sequence-point - switch - uninitialized - unused-but-set-variable - unused-function - unused-result - unused-value - unused-variable - " - -for j in $WARNLIST; do - if cc_supports_flag -W$j; then - EXTRA_WARNINGS="$EXTRA_WARNINGS -W$j"; - fi -done - -CFLAGS="$ENV_CFLAGS $lt_prog_compiler_pic $OPT_CFLAGS $GDB_FLAGS \ - $EXTRA_WARNINGS $WERROR_CFLAGS" -CPPFLAGS="$ENV_CPPFLAGS" -LDFLAGS="$ENV_LDFLAGS $lt_prog_compiler_pic -Wl,--as-needed" - -AC_CONFIG_FILES([ - Makefile - init/Makefile - libtap/Makefile - libtap/libtap.pc - kronosnetd/Makefile - kronosnetd/kronosnetd.logrotate - libknet/Makefile - libknet/libknet.pc - libknet/tests/Makefile - docs/Makefile - poc-code/Makefile - poc-code/iov-hash/Makefile - poc-code/access-list/Makefile - ]) - -AC_OUTPUT diff --git a/debian/changelog.in b/debian/changelog.in deleted file mode 100644 index 74ce9a00..00000000 --- a/debian/changelog.in +++ /dev/null @@ -1,5 +0,0 @@ -kronosnet (@version@.@numcomm@-@alphatag@@dirty@-1) unstable; urgency=low - - * These aren't the droids you're looking for. - - -- Autotools generated version @date@ diff --git a/debian/compat b/debian/compat deleted file mode 100644 index 7f8f011e..00000000 --- a/debian/compat +++ /dev/null @@ -1 +0,0 @@ -7 diff --git a/debian/control b/debian/control deleted file mode 100644 index b0c21da5..00000000 --- a/debian/control +++ /dev/null @@ -1,67 +0,0 @@ -Source: kronosnet -Priority: extra -Maintainer: Fabio M. Di Nitto -Build-Depends: debhelper (>= 7.0.50~), autotools-dev, libqb-dev (>= 0.14.3), libnss3-dev, libnspr4-dev, pkg-config, libpam-dev -Standards-Version: 3.8.4 -Section: admin - -Package: kronosnetd -Section: admin -Architecture: any -Depends: ${shlibs:Depends}, ${misc:Depends} -Description: Multipoint-to-Multipoint VPN daemon - The kronosnet daemon is a bridge between kronosnet switching engine - and kernel network tap devices, to create and administer a - distributed LAN over multipoint-to-multipoint VPNs. - The daemon does a poor attempt to provide a configure UI similar - to other known network devices/tools (Cisco, quagga). - Beside looking horrific, it allows runtime changes and - reconfiguration of the kronosnet(s) without daemon reload - or service disruption. - . - This package contains the kronosnet daemon and utils. - -Package: libtap0 -Section: libs -Architecture: any -Depends: ${shlibs:Depends}, ${misc:Depends} -Description: Simple userland wrapper around kernel tap devices - This is an over-engineered commodity library to manage a pool - of tap devices and provides the basic - pre-up.d/up.d/down.d/post-down.d infrastructure. - . - This package contains libraries that should be used by libtap clients. - -Package: libtap-dev -Section: libdevel -Architecture: any -Depends: libtap0 (= ${binary:Version}), ${misc:Depends} -Description: Simple userland wrapper around kernel tap devices (developer files) - This is an over-engineered commodity library to manage a pool - of tap devices and provides the basic - pre-up.d/up.d/down.d/post-down.d infrastructure. - . - This package contains header files required to build clients for libtap clients. - -Package: libknet0 -Section: libs -Architecture: any -Depends: ${shlibs:Depends}, ${misc:Depends} -Description: kronosnet core switching implementation - The whole kronosnet core is implemented in this library. - Please refer to the not-yet-existing documentation for further - information. - . - This package contains libraries that should be used by libknet clients. - -Package: libknet-dev -Section: libdevel -Architecture: any -Depends: libknet0 (= ${binary:Version}), ${misc:Depends} -Description: kronosnet core switching implementation (developer files) - The whole kronosnet core is implemented in this library. - Please refer to the not-yet-existing documentation for further - information. - . - This package contains header files required to build clients for - libknet clients. diff --git a/debian/copyright b/debian/copyright deleted file mode 100644 index 41603a8e..00000000 --- a/debian/copyright +++ /dev/null @@ -1,24 +0,0 @@ -Format: http://dep.debian.net/deps/dep5 -Upstream-Name: kronosnetd -Source: https://github.com/fabbione/kronosnet - -Files: * -Copyright: 2010-2015 Red Hat, Inc. All rights reserved. -License: GPL-2.0+, LGPL-2.0+ - -License: GPL-2.0+, LGPL-2.0+ - This package is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - . - This package is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - . - You should have received a copy of the GNU General Public License - along with this program. If not, see . - . - On Debian systems, the complete text of the GNU Lesser General - Public License can be found in "/usr/share/common-licenses/LGPL-2". diff --git a/debian/kronosnetd.install b/debian/kronosnetd.install deleted file mode 100644 index 48b46d6a..00000000 --- a/debian/kronosnetd.install +++ /dev/null @@ -1,7 +0,0 @@ -etc/kronosnet/* -etc/default/kronosnetd -etc/init.d/kronosnetd -etc/logrotate.d/kronosnetd -etc/pam.d/kronosnetd -usr/sbin/* -usr/share/man/man8/* diff --git a/debian/kronosnetd.postinst.in b/debian/kronosnetd.postinst.in deleted file mode 100644 index 3070c27e..00000000 --- a/debian/kronosnetd.postinst.in +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh - -set -e - -case "$1" in - configure) - groupadd --force --system @defaultadmgroup@ > /dev/null 2>&1 - ;; - purge|abort-upgrade|abort-remove|abort-deconfigure) - ;; - *) - echo "postinst called with unknown argument \`$1'" >&2 - exit 0 - ;; -esac - -#DEBHELPER# - -exit 0 diff --git a/debian/libknet-dev.install b/debian/libknet-dev.install deleted file mode 100644 index f97a6779..00000000 --- a/debian/libknet-dev.install +++ /dev/null @@ -1,4 +0,0 @@ -usr/include/libknet.h -usr/lib/libknet.a -usr/lib/libknet.so -usr/lib/pkgconfig/libknet.pc diff --git a/debian/libknet0.install b/debian/libknet0.install deleted file mode 100644 index 23db98c3..00000000 --- a/debian/libknet0.install +++ /dev/null @@ -1 +0,0 @@ -usr/lib/libknet.so.* diff --git a/debian/libtap-dev.install b/debian/libtap-dev.install deleted file mode 100644 index 7fc27158..00000000 --- a/debian/libtap-dev.install +++ /dev/null @@ -1,4 +0,0 @@ -usr/include/libtap.h -usr/lib/libtap.a -usr/lib/libtap.so -usr/lib/pkgconfig/libtap.pc diff --git a/debian/libtap0.install b/debian/libtap0.install deleted file mode 100644 index 68050e27..00000000 --- a/debian/libtap0.install +++ /dev/null @@ -1 +0,0 @@ -usr/lib/libtap.so.* diff --git a/debian/rules b/debian/rules deleted file mode 100755 index 6b22c651..00000000 --- a/debian/rules +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/make -f -# -*- makefile -*- -# Sample debian/rules that uses debhelper. -# This file was originally written by Joey Hess and Craig Small. -# As a special exception, when this file is copied by dh-make into a -# dh-make output file, you may use that output file without restriction. -# This special exception was added by Craig Small in version 0.37 of dh-make. - -# Uncomment this to turn on verbose mode. -#export DH_VERBOSE=1 - -# package check requires root and does nasty things -export DEB_BUILD_OPTIONS=nocheck - -%: - dh $@ - -override_dh_installinit: - dh_installinit \ - --package kronosnetd \ - --onlyscripts \ - --no-restart-on-upgrade \ - --init-script=kronosnetd diff --git a/debian/source/format b/debian/source/format deleted file mode 100644 index 163aaf8d..00000000 --- a/debian/source/format +++ /dev/null @@ -1 +0,0 @@ -3.0 (quilt) diff --git a/init/Makefile.am b/init/Makefile.am deleted file mode 100644 index 64b392c0..00000000 --- a/init/Makefile.am +++ /dev/null @@ -1,47 +0,0 @@ -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -MAINTAINERCLEANFILES = Makefile.in - -include $(top_srcdir)/build-aux/check.mk - -EXTRA_DIST = kronosnetd.in kronosnetd.service.in \ - kronosnetd.default - -if BUILD_KRONOSNETD - -systemdconfdir = $(SYSTEMDDIR) -systemdconf_DATA = kronosnetd.service - -initscriptdir = $(INITDDIR) -initscript_SCRIPTS = kronosnetd - -%: %.in Makefile - rm -f $@-t $@ - cat $< | sed \ - -e 's#@''SBINDIR@#$(sbindir)#g' \ - -e 's#@''SYSCONFDIR@#$(sysconfdir)#g' \ - -e 's#@''INITDDIR@#$(INITDDIR)#g' \ - -e 's#@''LOCALSTATEDIR@#$(localstatedir)#g' \ - > $@-t - mv $@-t $@ - -install-exec-local: - $(INSTALL) -d $(DESTDIR)/$(INITDEFAULTDIR) - $(INSTALL) -m 644 $(srcdir)/kronosnetd.default $(DESTDIR)/$(INITDEFAULTDIR)/kronosnetd - -uninstall-local: - rm -f $(DESTDIR)/$(INITDEFAULTDIR)/kronosnetd - rmdir $(DESTDIR)/$(INITDEFAULTDIR) || :; - -all-local: $(initscript_SCRIPTS) $(systemdconf_DATA) - -clean-local: - rm -rf $(initscript_SCRIPTS) $(systemdconf_DATA) - -endif diff --git a/init/kronosnetd.default b/init/kronosnetd.default deleted file mode 100644 index 04de6604..00000000 --- a/init/kronosnetd.default +++ /dev/null @@ -1,3 +0,0 @@ -# kronosnetd startup options (see man kronosnetd.8) - -KNETD_OPTS="" diff --git a/init/kronosnetd.in b/init/kronosnetd.in deleted file mode 100644 index 20ed36a7..00000000 --- a/init/kronosnetd.in +++ /dev/null @@ -1,152 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -# chkconfig: - 20 80 -# description: kronosnetd vpn daemon -# processname: kronosnetd -# -### BEGIN INIT INFO -# Provides: kronosnetd -# Required-Start: $network $remote_fs $syslog -# Required-Stop: $network $remote_fs $syslog -# Default-Start: 2 3 4 5 -# Default-Stop: -# Short-Description: Starts and stops kronosnetd vpn daemon. -# Description: Starts and stops kronosnetd vpn daemon. -### END INIT INFO - -desc="kronosnetd" -prog="kronosnetd" - -# set secure PATH -PATH="/sbin:/bin:/usr/sbin:/usr/bin:@SBINDIR@" - -success() -{ - echo -ne "[ OK ]\r" -} - -failure() -{ - echo -ne "[FAILED]\r" -} - -status() -{ - pid=$(pidof $1 2>/dev/null) - rtrn=$? - if [ $rtrn -ne 0 ]; then - echo "$1 is stopped" - else - echo "$1 (pid $pid) is running..." - fi - return $rtrn -} - -# rpm based distros -if [ -d @SYSCONFDIR@/sysconfig ]; then - [ -f @INITDDIR@/functions ] && . @INITDDIR@/functions - [ -f @SYSCONFDIR@/sysconfig/$prog ] && . @SYSCONFDIR@/sysconfig/$prog - [ -z "$LOCK_FILE" ] && LOCK_FILE="@LOCALSTATEDIR@/lock/subsys/$prog" -fi - -# deb based distros -if [ -d @SYSCONFDIR@/default ]; then - [ -f @SYSCONFDIR@/default/$prog ] && . @SYSCONFDIR@/default/$prog - [ -z "$LOCK_FILE" ] && LOCK_FILE="@LOCALSTATEDIR@/lock/$prog" -fi - -# The version of __pids_pidof in /etc/init.d/functions calls pidof with -x -# This means it matches scripts, including this one. -# Redefine it here so that status (from the same file) works. -# Otherwise simultaneous calls to stop() will loop forever -__pids_pidof() { - pidof -c -o $$ -o $PPID -o %PPID "$1" || \ - pidof -c -o $$ -o $PPID -o %PPID "${1##*/}" -} - -start() -{ - echo -n "Starting $desc ($prog): " - - # most recent distributions use tmpfs for @LOCALSTATEDIR@/run - # to avoid to clean it up on every boot. - # they also assume that init scripts will create - # required subdirectories for proper operations - mkdir -p @LOCALSTATEDIR@/run - - if status $prog > /dev/null 2>&1; then - success - else - $prog $KNETD_OPTS > /dev/null 2>&1 - touch $LOCK_FILE - success - fi - echo -} - -stop() -{ - ! status $prog > /dev/null 2>&1 && return - - echo -n "Signaling $desc ($prog) to terminate: " - kill -TERM $(pidof $prog) > /dev/null 2>&1 - success - echo - - echo -n "Waiting for $prog to unload:" - while status $prog > /dev/null 2>&1; do - sleep 1 - echo -n "." - done - - rm -f $LOCK_FILE - success - echo -} - -restart() -{ - stop - start -} - -rtrn=0 - -case "$1" in -start) - start - rtrn=$? -;; -restart|reload|force-reload) - restart - rtrn=$? -;; -condrestart|try-restart) - if status $prog > /dev/null 2>&1; then - restart - rtrn=$? - fi -;; -status) - status $prog - rtrn=$? -;; -stop) - stop - rtrn=$? -;; -*) - echo "usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}" - rtrn=2 -;; -esac - -exit $rtrn diff --git a/init/kronosnetd.service.in b/init/kronosnetd.service.in deleted file mode 100644 index 69f6c4d8..00000000 --- a/init/kronosnetd.service.in +++ /dev/null @@ -1,21 +0,0 @@ -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -[Unit] -Description=kronosnetd -Requires=network.target -After=network.target syslog.target - -[Service] -Type=forking -ControlGroup=cpu:/ -EnvironmentFile=/etc/sysconfig/kronosnetd -ExecStart=@SBINDIR@/kronosnetd $KNETD_OPTS - -[Install] -WantedBy=multi-user.target diff --git a/kronosnet.spec.in b/kronosnet.spec.in deleted file mode 100644 index 782c6611..00000000 --- a/kronosnet.spec.in +++ /dev/null @@ -1,253 +0,0 @@ -############################################################################### -############################################################################### -## -## Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -## -## This copyrighted material is made available to anyone wishing to use, -## modify, copy, or redistribute it subject to the terms and conditions -## of the GNU General Public License v.2 or higher -## -############################################################################### -############################################################################### - -# keep around ready for later user -%global alphatag @alphatag@ -%global numcomm @numcomm@ -%global dirty @dirty@ - -# set defaults from ./configure invokation -%@kronosnetd@ kronosnetd -%@libtap@ libtap - -%if %{with libtap} -%global buildlibtap 1 -%endif -%if %{with kronosnetd} -%global buildlibtap 1 -%global buildkronosnetd 1 -%endif - -# main (empty) package -# http://www.rpm.org/max-rpm/s1-rpm-subpack-spec-file-changes.html - -Name: kronosnet -Summary: Multipoint-to-Multipoint VPN daemon -Version: @version@ -Release: 1%{?numcomm:.%{numcomm}}%{?alphatag:.%{alphatag}}%{?dirty:.%{dirty}}%{?dist} -License: GPLv2+ and LGPLv2+ -Group: System Environment/Base -URL: https://github.com/fabbione/kronosnet/ -Source0: https://github.com/fabbione/kronosnet/archive/%{name}-%{version}%{?numcomm:.%{numcomm}}%{?alphatag:-%{alphatag}}%{?dirty:-%{dirty}}.tar.gz - -## Setup/build bits - -BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX) - -# Build dependencies -BuildRequires: nss-devel lksctp-tools-devel -%if %{defined buildkronosnetd} -BuildRequires: libqb-devel pam-devel -%endif - -%prep -%setup -q -n %{name}-%{version}%{?numcomm:.%{numcomm}}%{?alphatag:-%{alphatag}}%{?dirty:-%{dirty}} - -%build -%{configure} --disable-poc \ -%if %{defined buildkronosnetd} - --enable-kronosnetd \ -%endif -%if %{defined buildlibtap} - --enable-libtap \ -%endif - --with-initdefaultdir=%{_sysconfdir}/sysconfig/ \ -%if %{defined _unitdir} - --with-systemddir=%{_unitdir} -%else - --with-initddir=%{_sysconfdir}/rc.d/init.d/ -%endif - -make %{_smp_mflags} - -%install -rm -rf %{buildroot} -make install DESTDIR=%{buildroot} - -# tree cleanup -# remove static libraries -find %{buildroot} -name "*.a" -exec rm {} \; -# remove libtools leftovers -find %{buildroot} -name "*.la" -exec rm {} \; - -# handle systemd vs init script -%if %{defined _unitdir} -# remove init scripts -rm -rf %{buildroot}/etc/init.d -%else -# remove systemd specific bits -find %{buildroot} -name "*.service" -exec rm {} \; -%endif - -# remove docs -rm -rf %{buildroot}/usr/share/doc/kronosnet - -%clean -rm -rf %{buildroot} - -# main empty package -%description -kronosnet source - -%if %{defined buildkronosnetd} -## Runtime and subpackages section -%package -n kronosnetd -Group: System Environment/Base -Summary: Multipoint-to-Multipoint VPN daemon -%if %{defined _unitdir} -# Needed for systemd unit -Requires(post): systemd-sysv -Requires(post): systemd-units -Requires(preun): systemd-units -Requires(postun): systemd-units -%else -Requires(post): chkconfig -Requires(preun): chkconfig, initscripts -%endif -Requires(post): shadow-utils -Requires(preun): shadow-utils -Requires: pam, /etc/pam.d/passwd - -%description -n kronosnetd - The kronosnet daemon is a bridge between kronosnet switching engine - and kernel network tap devices, to create and administer a - distributed LAN over multipoint-to-multipoint VPNs. - The daemon does a poor attempt to provide a configure UI similar - to other known network devices/tools (Cisco, quagga). - Beside looking horrific, it allows runtime changes and - reconfiguration of the kronosnet(s) without daemon reload - or service disruption. - -%post -n kronosnetd -%if %{defined _unitdir} - %if 0%{?systemd_post:1} - %systemd_post kronosnetd.service - %else - /bin/systemctl daemon-reload >/dev/null 2>&1 || : - %endif -%else -/sbin/chkconfig --add kronosnetd -%endif -/usr/sbin/groupadd --force --system @defaultadmgroup@ - -%preun -n kronosnetd -%if %{defined _unitdir} - %if 0%{?systemd_preun:1} - %systemd_preun kronosnetd.service - %else -if [ "$1" -eq 0 ]; then - /bin/systemctl --no-reload disable kronosnetd.service - /bin/systemctl stop kronosnetd.service >/dev/null 2>&1 -fi -%endif -%else -if [ "$1" = 0 ]; then - /sbin/service kronosnetd stop >/dev/null 2>&1 - /sbin/chkconfig --del kronosnetd -fi -%endif - -%files -n kronosnetd -%defattr(-,root,root,-) -%doc COPYING.* COPYRIGHT -%dir %{_sysconfdir}/kronosnet -%dir %{_sysconfdir}/kronosnet/* -%config(noreplace) %{_sysconfdir}/sysconfig/kronosnetd -%config(noreplace) %{_sysconfdir}/pam.d/kronosnetd -%config(noreplace) %{_sysconfdir}/logrotate.d/kronosnetd -%if %{defined _unitdir} -%{_unitdir}/kronosnetd.service -%else -%config(noreplace) %{_sysconfdir}/rc.d/init.d/kronosnetd -%endif -%{_sbindir}/* -%{_mandir}/man8/* -%endif - -%if %{defined buildlibtap} -%package -n libtap0 -Group: System Environment/Libraries -Summary: Simple userland wrapper around kernel tap devices - -%description -n libtap0 - This is an over-engineered commodity library to manage a pool - of tap devices and provides the basic - pre-up.d/up.d/down.d/post-down.d infrastructure. - -%files -n libtap0 -%defattr(-,root,root,-) -%doc COPYING.* COPYRIGHT -%{_libdir}/libtap.so.* - -%post -n libtap0 -p /sbin/ldconfig - -%postun -n libtap0 -p /sbin/ldconfig - -%package -n libtap0-devel -Group: Development/Libraries -Summary: Simple userland wrapper around kernel tap devices (developer files) -Requires: libtap0 = %{version}-%{release} -Requires: pkgconfig - -%description -n libtap0-devel - This is an over-engineered commodity library to manage a pool - of tap devices and provides the basic - pre-up.d/up.d/down.d/post-down.d infrastructure. - -%files -n libtap0-devel -%defattr(-,root,root,-) -%doc COPYING.* COPYRIGHT -%{_libdir}/libtap.so -%{_includedir}/libtap.h -%{_libdir}/pkgconfig/libtap.pc -%endif - -%package -n libknet0 -Group: System Environment/Libraries -Summary: Kronosnet core switching implementation - -%description -n libknet0 - The whole kronosnet core is implemented in this library. - Please refer to the not-yet-existing documentation for further - information. - -%files -n libknet0 -%defattr(-,root,root,-) -%doc COPYING.* COPYRIGHT -%{_libdir}/libknet.so.* - -%post -n libknet0 -p /sbin/ldconfig - -%postun -n libknet0 -p /sbin/ldconfig - -%package -n libknet0-devel -Group: Development/Libraries -Summary: Kronosnet core switching implementation (developer files) -Requires: libknet0 = %{version}-%{release} -Requires: pkgconfig - -%description -n libknet0-devel - The whole kronosnet core is implemented in this library. - Please refer to the not-yet-existing documentation for further - information. - -%files -n libknet0-devel -%defattr(-,root,root,-) -%doc COPYING.* COPYRIGHT -%{_libdir}/libknet.so -%{_includedir}/libknet.h -%{_libdir}/pkgconfig/libknet.pc - -%changelog -* @date@ Autotools generated version - @version@-1-@numcomm@.@alphatag@.@dirty@ -- These aren't the droids you're looking for. - diff --git a/kronosnetd/Makefile.am b/kronosnetd/Makefile.am deleted file mode 100644 index 0503e726..00000000 --- a/kronosnetd/Makefile.am +++ /dev/null @@ -1,90 +0,0 @@ -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -MAINTAINERCLEANFILES = Makefile.in kronostnetd.logrotate - -include $(top_srcdir)/build-aux/check.mk - -EXTRA_DIST = kronosnetd.logrotate.in kronosnetd.8 knet-keygen.8 - -noinst_HEADERS = \ - cfg.h \ - etherfilter.h \ - logging.h \ - vty.h \ - vty_auth.h \ - vty_cli.h \ - vty_cli_cmds.h \ - vty_utils.h - -kronosnetd_SOURCES = \ - cfg.c \ - etherfilter.c \ - main.c \ - logging.c \ - vty.c \ - vty_auth.c \ - vty_cli.c \ - vty_cli_cmds.c \ - vty_utils.c - -kronosnetd_CPPFLAGS = \ - -I$(top_srcdir)/libtap \ - -I$(top_srcdir)/libknet - -kronosnetd_CFLAGS = $(libqb_CFLAGS) - -kronosnetd_LDADD = \ - $(top_builddir)/libknet/libknet.la \ - $(top_builddir)/libtap/libtap.la \ - $(libqb_LIBS) - -knet_keygen_SOURCES = keygen.c - -knet_keygen_CPPFLAGS = -I$(top_srcdir)/libknet - -if BUILD_KRONOSNETD - -sbin_PROGRAMS = kronosnetd \ - knet-keygen - -dist_man_MANS = kronosnetd.8 knet-keygen.8 - -install-exec-local: - $(INSTALL) -d $(DESTDIR)/$(DEFAULT_CONFIG_DIR) - $(INSTALL) -d $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/down.d - $(INSTALL) -d $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/post-down.d - $(INSTALL) -d $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/pre-up.d - $(INSTALL) -d $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/up.d - $(INSTALL) -d $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/cryptokeys.d - $(INSTALL) -d -m 0755 $(DESTDIR)/$(sysconfdir)/logrotate.d - $(INSTALL) -m 644 kronosnetd.logrotate $(DESTDIR)/$(sysconfdir)/logrotate.d/kronosnetd - $(INSTALL) -d -m 0755 $(DESTDIR)/$(sysconfdir)/pam.d - if [ -a $(sysconfdir)/pam.d/password-auth ]; then \ - cd $(DESTDIR)/$(sysconfdir)/pam.d && \ - rm -f kronosnetd && \ - $(LN_S) password-auth kronosnetd; \ - else \ - cd $(DESTDIR)/$(sysconfdir)/pam.d && \ - rm -f kronosnetd && \ - $(LN_S) passwd kronosnetd; \ - fi - -uninstall-local: - rmdir $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/cryptokeys.d || :; - rmdir $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/down.d || :; - rmdir $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/post-down.d || :; - rmdir $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/pre-up.d || :; - rmdir $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/up.d || :; - rmdir $(DESTDIR)/$(DEFAULT_CONFIG_DIR) || :; - rm -f $(DESTDIR)/$(sysconfdir)/logrotate.d/kronosnetd - rmdir $(DESTDIR)/$(sysconfdir)/logrotate.d || :; - rm -f $(DESTDIR)/$(sysconfdir)/pam.d/kronosnetd || :; - rmdir $(DESTDIR)/$(sysconfdir)/pam.d || :; - -endif diff --git a/kronosnetd/cfg.c b/kronosnetd/cfg.c deleted file mode 100644 index 2b76c5d6..00000000 --- a/kronosnetd/cfg.c +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include - -#include "cfg.h" -#include "libtap.h" - -struct knet_cfg *knet_get_iface(const char *name, int create) -{ - struct knet_cfg *knet_iface = knet_cfg_head.knet_cfg; - int found = 0; - - while (knet_iface != NULL) { - if (!strcmp(tap_get_name(knet_iface->cfg_eth.tap), name)) { - found = 1; - break; - } - knet_iface = knet_iface->next; - } - - if ((!found) && (create)) { - knet_iface = malloc(sizeof(struct knet_cfg)); - if (!knet_iface) - goto out_clean; - - memset(knet_iface, 0, sizeof(struct knet_cfg)); - - knet_iface->cfg_ring.base_port = KNET_RING_DEFPORT; - - strncpy(knet_iface->knet_handle_crypto_cfg.crypto_model, - "none", - sizeof(knet_iface->knet_handle_crypto_cfg.crypto_model) - 1); - - strncpy(knet_iface->knet_handle_crypto_cfg.crypto_cipher_type, - "none", - sizeof(knet_iface->knet_handle_crypto_cfg.crypto_cipher_type) - 1); - - strncpy(knet_iface->knet_handle_crypto_cfg.crypto_hash_type, - "none", - sizeof(knet_iface->knet_handle_crypto_cfg.crypto_hash_type) - 1); - - if (knet_cfg_head.knet_cfg) { - struct knet_cfg *knet_iface_last = knet_cfg_head.knet_cfg; - - while (knet_iface_last->next != NULL) { - knet_iface_last = knet_iface_last->next; - } - knet_iface_last->next = knet_iface; - } else { - knet_cfg_head.knet_cfg = knet_iface; - } - } - -out_clean: - - return knet_iface; -} - -void knet_destroy_iface(struct knet_cfg *knet_iface) -{ - struct knet_cfg *knet_iface_tmp = knet_cfg_head.knet_cfg; - struct knet_cfg *knet_iface_prev = knet_cfg_head.knet_cfg; - - while (knet_iface_tmp != knet_iface) { - knet_iface_prev = knet_iface_tmp; - knet_iface_tmp = knet_iface_tmp->next; - } - - if (knet_iface_tmp == knet_iface) { - if (knet_iface_tmp == knet_iface_prev) { - knet_cfg_head.knet_cfg = knet_iface_tmp->next; - } else { - knet_iface_prev->next = knet_iface_tmp->next; - } - free(knet_iface); - } -} diff --git a/kronosnetd/cfg.h b/kronosnetd/cfg.h deleted file mode 100644 index 83d803c7..00000000 --- a/kronosnetd/cfg.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNETD_CFG_H__ -#define __KNETD_CFG_H__ - -#include -#include - -#include "libtap.h" -#include "libknet.h" - -#define KNET_RING_DEFPORT 50000 - -struct knet_cfg_eth { - tap_t tap; - int auto_mtu; - knet_node_id_t node_id; -}; - -struct knet_cfg_ring { - knet_handle_t knet_h; - int data_mtu; - int base_port; -}; - -struct knet_cfg { - struct knet_cfg_eth cfg_eth; - struct knet_cfg_ring cfg_ring; - int active; - struct knet_handle_crypto_cfg knet_handle_crypto_cfg; - struct knet_cfg *next; -}; - -struct knet_cfg_top { - char *conffile; - char *logfile; - char *vty_ipv4; - char *vty_ipv6; - char *vty_port; - struct knet_cfg *knet_cfg; -}; - -struct knet_cfg *knet_get_iface(const char *name, const int create); -void knet_destroy_iface(struct knet_cfg *knet_iface); - -extern struct knet_cfg_top knet_cfg_head; - -#endif diff --git a/kronosnetd/etherfilter.c b/kronosnetd/etherfilter.c deleted file mode 100644 index db91a801..00000000 --- a/kronosnetd/etherfilter.c +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include - -#include "etherfilter.h" - -/* - * stole from linux kernel/include/linux/etherdevice.h - */ - -static inline int is_zero_ether_addr(const uint8_t *addr) -{ - return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]); -} - -static inline int is_multicast_ether_addr(const uint8_t *addr) -{ - return 0x01 & addr[0]; -} - -static inline int is_broadcast_ether_addr(const uint8_t *addr) -{ - return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff; -} - -int ether_host_filter_fn (void *private_data, - const unsigned char *outdata, - ssize_t outdata_len, - uint8_t tx_rx, - knet_node_id_t this_host_id, - knet_node_id_t src_host_id, - int8_t *channel, - knet_node_id_t *dst_host_ids, - size_t *dst_host_ids_entries) -{ - struct ether_header *eth_h = (struct ether_header *)outdata; - uint8_t *dst_mac = (uint8_t *)eth_h->ether_dhost; - uint16_t dst_host_id; - - if (is_zero_ether_addr(dst_mac)) - return -1; - - if (is_multicast_ether_addr(dst_mac) || - is_broadcast_ether_addr(dst_mac)) { - return 1; - } - - memmove(&dst_host_id, &dst_mac[4], 2); - - dst_host_ids[0] = ntohs(dst_host_id); - *dst_host_ids_entries = 1; - - return 0; -} diff --git a/kronosnetd/etherfilter.h b/kronosnetd/etherfilter.h deleted file mode 100644 index f7358f3e..00000000 --- a/kronosnetd/etherfilter.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNETD_ETHERFILTER_H__ -#define __KNETD_ETHERFILTER_H__ - -#include -#include "libknet.h" - -int ether_host_filter_fn (void *private_data, - const unsigned char *outdata, - ssize_t outdata_len, - uint8_t tx_rx, - knet_node_id_t this_host_id, - knet_node_id_t src_host_id, - int8_t *channel, - knet_node_id_t *dst_host_ids, - size_t *dst_host_ids_entries); - -#endif diff --git a/kronosnetd/keygen.c b/kronosnetd/keygen.c deleted file mode 100644 index acb3221d..00000000 --- a/kronosnetd/keygen.c +++ /dev/null @@ -1,180 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -static char *output_file = NULL; -static ssize_t keylen = KNET_MAX_KEY_LEN; - -static void print_usage(void) -{ - printf("\nUsage:\n\n"); - printf("knet-keygen -o [-s ]\n\n"); -}; - -#define OPTION_STRING "ho:s:" - -static int read_arguments(int argc, char **argv) -{ - int cont = 1; - int optchar; - - while (cont) { - optchar = getopt(argc, argv, OPTION_STRING); - - switch (optchar) { - - case 'o': - output_file = strdup(optarg); - if (!output_file) { - fprintf(stderr, "Error: Unable to allocate memory\n"); - return -1; - } - if (strlen(output_file) > PATH_MAX) { - fprintf(stderr, "Seriously? WTF\n"); - return -1; - } - break; - - case 's': - keylen = atoi(optarg); - if ((keylen < KNET_MIN_KEY_LEN) || (keylen > KNET_MAX_KEY_LEN)) { - fprintf(stderr, "Error: Key size should be a value between %u and %u (default) included\n", - KNET_MIN_KEY_LEN, KNET_MAX_KEY_LEN); - return -1; - } - break; - - case 'h': - print_usage(); - exit(EXIT_SUCCESS); - break; - - case EOF: - cont = 0; - break; - - default: - fprintf(stderr, "Error: unknown option: %c\n", optchar); - print_usage(); - return -1; - break; - - } - } - if (!output_file) { - fprintf(stderr, "Error: no output file specified\n"); - print_usage(); - return -1; - } - return 0; -} - -int main (int argc, char *argv[]) -{ - int ret = 0; - int fd = -1; - ssize_t res; - ssize_t bytes_read = 0; - char *keybuf = NULL; - - printf (PACKAGE " key generator.\n"); - - if (read_arguments(argc, argv) < 0) { - goto exit_error; - } - - if (geteuid() != 0) { - fprintf(stderr, "Error: Authorization key must be generated as root user.\n"); - goto exit_error; - } - - fd = open ("/dev/random", O_RDONLY); - if (fd < 0) { - fprintf(stderr, "Error: Unable to open /dev/random\n"); - goto exit_error; - } - - keybuf = malloc(keylen); - if (!keybuf) { - fprintf(stderr, "Error: Unable to allocate memory for key\n"); - goto exit_error; - } - - printf("Gathering %zu bytes for key from /dev/random.\n", keylen); - printf("This process might take a long time due the amount on entropy required\n"); - printf("Press keys on your keyboard, perform any kind of disk I/O and/or network to generate entropy faster.\n"); - -keep_reading: - res = read(fd, &keybuf[bytes_read], keylen - bytes_read); - if (res == -1) { - fprintf(stderr, "Error: Unable to read from /dev/random.\n"); - goto exit_error; - } - bytes_read += res; - if (bytes_read != keylen) { - printf("bytes read = %zu, missing = %zu.\n", bytes_read, keylen - bytes_read); - goto keep_reading; - } - close (fd); - fd = -1; - - fd = open (output_file, O_CREAT|O_WRONLY, 600); - if (fd == -1) { - fprintf(stderr, "Error: Could not create %s\n", output_file); - goto exit_error; - } - - /* - * Make sure file is owned by root and mode 0400 - */ - if (fchown(fd, 0, 0)) { - fprintf(stderr, "Error: Could not set uid 0 (root) and gid 0 (root) on keyfile %s\n", output_file); - goto exit_error; - } - if (fchmod(fd, 0400)) { - fprintf(stderr, "Error: Could not set read-only permissions on keyfile %s\n", output_file); - goto exit_error; - } - - printf("Writing private key to %s\n", output_file); - - if (write(fd, keybuf, keylen) != keylen) { - fprintf(stderr, "Error: Could not write key to file %s\n", output_file); - goto exit_error; - } - - printf("Done.\n"); - printf("Please copy this file in " DEFAULT_CONFIG_DIR "/cryptokeys.d/\n"); - printf("on all nodes participating in the same kronosnet instance\n"); - -exit_clean: - if (output_file) - free(output_file); - if (keybuf) - free(keybuf); - if (fd > -1) - close(fd); - - return ret; - -exit_error: - ret = -1; - goto exit_clean; -} diff --git a/kronosnetd/knet-keygen.8 b/kronosnetd/knet-keygen.8 deleted file mode 100644 index 6f51bd77..00000000 --- a/kronosnetd/knet-keygen.8 +++ /dev/null @@ -1,28 +0,0 @@ -.\"/* -.\" * Copyright (C) 2010-2015 Red Hat, Inc. -.\" * -.\" * All rights reserved. -.\" * -.\" * Author: Fabio M. Di Nitto -.\" * -.\" * This software licensed under GPL-2.0+, LGPL-2.0+ -.\" */ -.TH "KRONOSNETD" "8" "November 2012" "kronosnetd key generator." "System Administration Utilities" - -.SH "NAME" -knet\-keygen \- Tool to generate keys for kronosnetd. - -.SH "SYNOPSIS" -.B knet\-keygen -\fB\-o\fR [\-s ] - -.SH "OPTIONS" -.TP -\fB\-o\fR -Output file -.TP -\fB\-s\fR -Key size (value between 1024 and 4096 included) - -.SH "SEE ALSO" -kronosnetd(8) diff --git a/kronosnetd/kronosnetd.8 b/kronosnetd/kronosnetd.8 deleted file mode 100644 index 7d0f18c1..00000000 --- a/kronosnetd/kronosnetd.8 +++ /dev/null @@ -1,51 +0,0 @@ -.\"/* -.\" * Copyright (C) 2010-2015 Red Hat, Inc. -.\" * -.\" * All rights reserved. -.\" * -.\" * Author: Fabio M. Di Nitto -.\" * -.\" * This software licensed under GPL-2.0+, LGPL-2.0+ -.\" */ -.TH "KRONOSNETD" "8" "November 2012" "kronosnetd Usage:" "System Administration Utilities" - -.SH "NAME" -kronosnetd \- libknet management daemon - -.SH "DESCRIPTION" -Usage: -.PP -kronosnetd [options] - -.SH "OPTIONS" - -.TP -\fB\-a\fR -Bind management VTY to ipv6_addr (default: localhost) -.TP -\fB\-b\fR -Bind management VTY to ipv4_addr (default: localhost) -.TP -\fB\-p\fR -Bind management VTY to port (default 50000) -.TP -\fB\-c\fR -Use config file (default /etc/kronosnet/kronosnetd.conf) -.TP -\fB\-l\fR -Use log file (default /var/log//kronosnetd.log) -.TP -\fB\-f\fR -Do not fork in background -.TP -\fB\-d\fR -Enable debugging output -.TP -\fB\-h\fR -This help -.TP -\fB\-V\fR -Print program version information - -.SH "SEE ALSO" -knet-keygen(8) diff --git a/kronosnetd/kronosnetd.logrotate.in b/kronosnetd/kronosnetd.logrotate.in deleted file mode 100644 index 870e2dac..00000000 --- a/kronosnetd/kronosnetd.logrotate.in +++ /dev/null @@ -1,17 +0,0 @@ -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -@LOGDIR@kronosnetd.log { - missingok - compress - copytruncate - daily - rotate 31 - minsize 2048 - notifempty -} diff --git a/kronosnetd/logging.c b/kronosnetd/logging.c deleted file mode 100644 index 7f36a770..00000000 --- a/kronosnetd/logging.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include "logging.h" - -void logging_init_defaults(int debug, int daemonize, const char *logfile) -{ - int level = SYSLOGLEVEL; - int32_t filetarget; - - if (debug) { - level = LOG_DEBUG; - } - - qb_log_init(PACKAGE "d", SYSLOGFACILITY, level); - - qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_TRUE); - if (debug) { - qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_PRIORITY_BUMP, - LOG_INFO - LOG_DEBUG); - } - - /* - * initialize stderr output only if we are not forking in background - */ - if (!daemonize) { - qb_log_format_set(QB_LOG_STDERR, "%t %N [%p]: %b"); - qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE); - qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD, - QB_LOG_FILTER_FUNCTION, "*", level); - } - - filetarget = qb_log_file_open(logfile); - qb_log_ctl(filetarget, QB_LOG_CONF_ENABLED, QB_TRUE); - qb_log_format_set(filetarget, "%t %N [%p]: %b"); - qb_log_filter_ctl(filetarget, QB_LOG_FILTER_ADD, - QB_LOG_FILTER_FUNCTION, "*", level); - - qb_log_thread_start(); - qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_THREADED, QB_TRUE); - qb_log_ctl(filetarget, QB_LOG_CONF_THREADED, QB_TRUE); -} - -void logging_fini(void) -{ - qb_log_fini(); -} diff --git a/kronosnetd/logging.h b/kronosnetd/logging.h deleted file mode 100644 index 4e979c41..00000000 --- a/kronosnetd/logging.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNETD_LOGGING_H__ -#define __KNETD_LOGGING_H__ - -#include - -#define log_debug(fmt, args...) qb_log(LOG_DEBUG, "(%s:%i|%s): " fmt, __FILE__, __LINE__, __FUNCTION__, ##args); - -#define log_kdebug(fmt, args...) qb_log(LOG_DEBUG, fmt, ##args); - -#define log_info(fmt, args...) qb_log(LOG_INFO, fmt, ##args); - -#define log_warn(fmt, args...) qb_log(LOG_WARNING, fmt, ##args); - -#define log_error(fmt, args...) qb_log(LOG_ERR, fmt, ##args); - -void logging_init_defaults(int debug, int daemonize, const char *logfile); - -void logging_fini(void); - -#endif diff --git a/kronosnetd/main.c b/kronosnetd/main.c deleted file mode 100644 index aaf2710d..00000000 --- a/kronosnetd/main.c +++ /dev/null @@ -1,347 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "cfg.h" -#include "vty.h" -#include "logging.h" - -#define LOCKFILE_NAME RUNDIR PACKAGE "d.pid" - -#define OPTION_STRING "hdfVc:l:a:b:p:" - -static int debug = 0; -static int daemonize = 1; - -struct knet_cfg_top knet_cfg_head; - -static void print_usage(void) -{ - printf("Usage:\n\n"); - printf(PACKAGE "d [options]\n\n"); - printf("Options:\n\n"); - printf(" -a Bind management VTY to ipv6_addr (default: localhost)\n"); - printf(" -b Bind management VTY to ipv4_addr (default: localhost)\n"); - printf(" -p Bind management VTY to port (default %d)\n", - KNET_VTY_DEFAULT_PORT); - printf(" -c Use config file (default "DEFAULT_CONFIG_FILE")\n"); - printf(" -l Use log file (default "DEFAULT_LOG_FILE")\n"); - printf(" -f Do not fork in background\n"); - printf(" -d Enable debugging output\n"); - printf(" -h This help\n"); - printf(" -V Print program version information\n"); - return; -} - -static int read_arguments(int argc, char **argv) -{ - int cont = 1; - int optchar; - int int_port; - - while (cont) { - optchar = getopt(argc, argv, OPTION_STRING); - - switch (optchar) { - - case 'a': - knet_cfg_head.vty_ipv6 = strdup(optarg); - if (!knet_cfg_head.vty_ipv6) - return -1; - break; - - case 'b': - knet_cfg_head.vty_ipv4 = strdup(optarg); - if (!knet_cfg_head.vty_ipv4) - return -1; - break; - - case 'p': - int_port = atoi(optarg); - if ((int_port < 0) || (int_port > 65535)) { - errno = EINVAL; - return -1; - } - knet_cfg_head.vty_port = strdup(optarg); - if (!knet_cfg_head.vty_port) - return -1; - break; - - case 'c': - knet_cfg_head.conffile = strdup(optarg); - if (!knet_cfg_head.conffile) - return -1; - break; - - case 'l': - knet_cfg_head.logfile = strdup(optarg); - if (!knet_cfg_head.logfile) - return -1; - break; - - case 'd': - debug = 1; - break; - - case 'f': - daemonize = 0; - break; - - case 'h': - print_usage(); - exit(EXIT_SUCCESS); - break; - - case 'V': - printf(PACKAGE "d " PACKAGE_VERSION " (built " __DATE__ - " " __TIME__ ")\n"); - exit(EXIT_SUCCESS); - break; - - case EOF: - cont = 0; - break; - - default: - fprintf(stderr, "unknown option: %c\n", optchar); - print_usage(); - exit(EXIT_FAILURE); - break; - - } - } - return 0; -} - -static int set_scheduler(void) -{ - struct sched_param sched_param; - int err; - - err = sched_get_priority_max(SCHED_RR); - if (err < 0) { - log_warn("Could not get maximum scheduler priority"); - return err; - } - - sched_param.sched_priority = err; - err = sched_setscheduler(0, SCHED_RR, &sched_param); - if (err < 0) - log_warn("could not set SCHED_RR priority %d", - sched_param.sched_priority); - - return err; -} - -static void remove_lockfile(void) -{ - unlink(LOCKFILE_NAME); -} - -static int create_lockfile(const char *lockfile) -{ - int fd, value; - size_t bufferlen; - ssize_t write_out; - struct flock lock; - char buffer[50]; - - if ((fd = open(lockfile, O_CREAT | O_WRONLY, - (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) < 0) { - fprintf(stderr, "Cannot open lockfile [%s], error was [%s]\n", - lockfile, strerror(errno)); - return -1; - } - - lock.l_type = F_WRLCK; - lock.l_start = 0; - lock.l_whence = SEEK_SET; - lock.l_len = 0; - -retry_fcntl: - - if (fcntl(fd, F_SETLK, &lock) < 0) { - switch (errno) { - case EINTR: - goto retry_fcntl; - break; - case EACCES: - case EAGAIN: - fprintf(stderr, "Cannot lock lockfile [%s], error was [%s]\n", - lockfile, strerror(errno)); - break; - default: - fprintf(stderr, "process is already running\n"); - } - - goto fail_close; - } - - if (ftruncate(fd, 0) < 0) { - fprintf(stderr, "Cannot truncate pidfile [%s], error was [%s]\n", - lockfile, strerror(errno)); - - goto fail_close_unlink; - } - - memset(buffer, 0, sizeof(buffer)); - snprintf(buffer, sizeof(buffer)-1, "%u\n", getpid()); - - bufferlen = strlen(buffer); - write_out = write(fd, buffer, bufferlen); - - if ((write_out < 0) || (write_out == 0 && errno)) { - fprintf(stderr, "Cannot write pid to pidfile [%s], error was [%s]\n", - lockfile, strerror(errno)); - - goto fail_close_unlink; - } - - if ((write_out == 0) || (write_out < bufferlen)) { - fprintf(stderr, "Cannot write pid to pidfile [%s], shortwrite of" - "[%zu] bytes, expected [%zu]\n", - lockfile, write_out, bufferlen); - - goto fail_close_unlink; - } - - if ((value = fcntl(fd, F_GETFD, 0)) < 0) { - fprintf(stderr, "Cannot get close-on-exec flag from pidfile [%s], " - "error was [%s]\n", lockfile, strerror(errno)); - - goto fail_close_unlink; - } - value |= FD_CLOEXEC; - if (fcntl(fd, F_SETFD, value) < 0) { - fprintf(stderr, "Cannot set close-on-exec flag from pidfile [%s], " - "error was [%s]\n", lockfile, strerror(errno)); - - goto fail_close_unlink; - } - - atexit(remove_lockfile); - - return 0; - -fail_close_unlink: - if (unlink(lockfile)) - fprintf(stderr, "Unable to unlink %s\n", lockfile); - -fail_close: - if (close(fd)) - fprintf(stderr, "Unable to close %s file descriptor\n", lockfile); - return -1; -} - -static void set_cfg_defaults(void) -{ - if (!knet_cfg_head.conffile) - knet_cfg_head.conffile = strdup(DEFAULT_CONFIG_FILE); - if (!knet_cfg_head.conffile) { - fprintf(stderr, "Unable to allocate memory for config file\n"); - exit(EXIT_FAILURE); - } - - if (!knet_cfg_head.logfile) - knet_cfg_head.logfile = strdup(DEFAULT_LOG_FILE); - if (!knet_cfg_head.conffile) { - fprintf(stderr, "Unable to allocate memory for log file\n"); - exit(EXIT_FAILURE); - } - - if (!knet_cfg_head.vty_ipv6) - knet_cfg_head.vty_ipv6 = strdup("::1"); - if (!knet_cfg_head.vty_ipv6) { - fprintf(stderr, "Unable to allocate memory for default ip address\n"); - exit(EXIT_FAILURE); - } - - if (!knet_cfg_head.vty_ipv4) - knet_cfg_head.vty_ipv4 = strdup("127.0.0.1"); - if (!knet_cfg_head.vty_ipv4) { - fprintf(stderr, "Unable to allocate memory for default ip address\n"); - exit(EXIT_FAILURE); - } - - if (!knet_cfg_head.vty_port) { - char portbuf[8]; - memset(&portbuf, 0, sizeof(portbuf)); - snprintf(portbuf, sizeof(portbuf), "%d", KNET_VTY_DEFAULT_PORT); - knet_cfg_head.vty_port = strdup(portbuf); - } - if (!knet_cfg_head.vty_port) { - fprintf(stderr, "Unable to allocate memory for default port address\n"); - exit(EXIT_FAILURE); - } -} - -int main(int argc, char **argv) -{ - int err; - - memset(&knet_cfg_head, 0, sizeof(struct knet_cfg_top)); - - if (read_arguments(argc, argv) < 0) { - fprintf(stderr, "Unable to parse options\n"); - exit(EXIT_FAILURE); - } - - set_cfg_defaults(); - - if (create_lockfile(LOCKFILE_NAME) < 0) { - fprintf(stderr, "Unable to create lockfile\n"); - exit(EXIT_FAILURE); - } - - if (daemonize) { - if (daemon(0, 0) < 0) { - perror("Unable to daemonize"); - exit(EXIT_FAILURE); - } - } - - logging_init_defaults(debug, daemonize, knet_cfg_head.logfile); - log_info(PACKAGE "d version " VERSION); - - /* - * don't fail if scheduler is not RR because systemd is - * an utter piece of shit that refuses us to set RR via init script - */ - set_scheduler(); - - err = knet_vty_main_loop(debug); - if (err < 0) - log_error("Detected fatal error in main loop"); - - if (knet_cfg_head.logfile) - free(knet_cfg_head.logfile); - if (knet_cfg_head.conffile) - free(knet_cfg_head.conffile); - if (knet_cfg_head.vty_ipv6) - free(knet_cfg_head.vty_ipv6); - if (knet_cfg_head.vty_ipv4) - free(knet_cfg_head.vty_ipv4); - if (knet_cfg_head.vty_port) - free(knet_cfg_head.vty_port); - - logging_fini(); - - return err; -} diff --git a/kronosnetd/vty.c b/kronosnetd/vty.c deleted file mode 100644 index 5491f622..00000000 --- a/kronosnetd/vty.c +++ /dev/null @@ -1,504 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "cfg.h" -#include "logging.h" -#include "netutils.h" -#include "vty.h" -#include "vty_auth.h" -#include "vty_cli.h" -#include "vty_cli_cmds.h" -#include "vty_utils.h" - -static int vty_max_connections = KNET_VTY_DEFAULT_MAX_CONN; -static int vty_current_connections = 0; -static int daemon_quit = 0; - -pthread_mutex_t knet_vty_mutex = PTHREAD_MUTEX_INITIALIZER; -int knet_vty_config = -1; -struct knet_vty knet_vtys[KNET_VTY_TOTAL_MAX_CONN]; -struct knet_vty_global_conf vty_global_conf; -pthread_t logging_thread; - -static int _fdset_cloexec(int fd) -{ - int fdflags; - - fdflags = fcntl(fd, F_GETFD, 0); - if (fdflags < 0) - return -1; - - fdflags |= FD_CLOEXEC; - - if (fcntl(fd, F_SETFD, fdflags) < 0) - return -1; - - return 0; -} - -static int _fdset_nonblock(int fd) -{ - int fdflags; - - fdflags = fcntl(fd, F_GETFL, 0); - if (fdflags < 0) - return -1; - - fdflags |= O_NONBLOCK; - - if (fcntl(fd, F_SETFL, fdflags) < 0) - return -1; - - return 0; -} - -static void *_handle_logging_thread(void *data) -{ - int logfd; - int se_result = 0; - fd_set rfds; - struct timeval tv; - - memmove(&logfd, data, sizeof(int)); - - while (se_result >= 0 && !daemon_quit){ - FD_ZERO (&rfds); - FD_SET (logfd, &rfds); - - tv.tv_sec = 1; - tv.tv_usec = 0; - - se_result = select(FD_SETSIZE, &rfds, 0, 0, &tv); - - if (se_result == -1) - goto out; - - if (se_result == 0) - continue; - - if (FD_ISSET(logfd, &rfds)) { - struct knet_log_msg msg; - size_t bytes_read = 0; - size_t len; - - while (bytes_read < sizeof(struct knet_log_msg)) { - len = read(logfd, &msg + bytes_read, - sizeof(struct knet_log_msg) - bytes_read); - if (len <= 0) { - break; - } - bytes_read += len; - } - - if (bytes_read != sizeof(struct knet_log_msg)) - continue; - - switch(msg.msglevel) { - case KNET_LOG_WARN: - log_warn("(%s) %s", knet_log_get_subsystem_name(msg.subsystem), msg.msg); - break; - case KNET_LOG_INFO: - log_info("(%s) %s", knet_log_get_subsystem_name(msg.subsystem), msg.msg); - break; - case KNET_LOG_DEBUG: - log_kdebug("(%s) %s", knet_log_get_subsystem_name(msg.subsystem), msg.msg); - break; - case KNET_LOG_ERR: - default: - log_error("(%s) %s", knet_log_get_subsystem_name(msg.subsystem), msg.msg); - } - } - } - -out: - return NULL; -} - -static int knet_vty_init_listener(const char *ip_addr, const char *port) -{ - int sockfd = -1, sockopt = 1; - int socktype = SOCK_STREAM; - int err = 0; - struct sockaddr_storage ss; - - memset(&ss, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr(ip_addr, port, &ss, sizeof(struct sockaddr_storage)) != 0) - return -1; - - pthread_mutex_lock(&knet_vty_mutex); - - /* handle sigpipe if we decide to use KEEPALIVE */ - - sockfd = socket(ss.ss_family, socktype, 0); - if (sockfd < 0) { - err = sockfd; - goto out_clean; - } - - if (ss.ss_family == AF_INET6) { - err = setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, - (void *)&sockopt, sizeof(sockopt)); - if (err) - goto out_clean; - } - - err = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, - (void *)&sockopt, sizeof(sockopt)); - if (err) - goto out_clean; - - if (_fdset_cloexec(sockfd)) { - err = -1; - goto out_clean; - } - - err = bind(sockfd, (struct sockaddr *)&ss, sizeof(struct sockaddr_storage)); - if (err) - goto out_clean; - - err = listen(sockfd, 0); - if (err) - goto out_clean; - - pthread_mutex_unlock(&knet_vty_mutex); - - return sockfd; - -out_clean: - if (sockfd >= 0) - close(sockfd); - - pthread_mutex_unlock(&knet_vty_mutex); - - return err; -} - -static void knet_vty_close_listener(int listener_fd) -{ - pthread_mutex_lock(&knet_vty_mutex); - - if (listener_fd <= 0) - goto out_clean; - - close(listener_fd); - listener_fd = 0; - -out_clean: - - pthread_mutex_unlock(&knet_vty_mutex); - - return; -} - -static void sigterm_handler(int sig) -{ - daemon_quit = 1; -} - -static void sigpipe_handler(int sig) -{ - return; -} - -static void knet_vty_close(struct knet_vty *vty) -{ - if (knet_vty_config == vty->conn_num) - knet_vty_config = -1; - - knet_vty_free_history(vty); - vty->active = 0; - close(vty->vty_sock); - vty_current_connections--; -} - -static void *vty_accept_thread(void *arg) -{ - struct knet_vty *vty = (struct knet_vty *)&knet_vtys[*(int *)arg]; - char addr_str[KNET_MAX_HOST_LEN]; - char port_str[KNET_MAX_PORT_LEN]; - int err; - - knet_vty_print_banner(vty); - if (vty->got_epipe) - goto out_clean; - - err = knet_addrtostr(&vty->src_sa, vty->src_sa_len, - addr_str, KNET_MAX_HOST_LEN, - port_str, KNET_MAX_PORT_LEN); - - if (!err) { - strncpy(vty->ip, addr_str, sizeof(vty->ip)); - } else { - strcpy(vty->ip, "unknown"); - } - - if ((knet_vty_auth_user(vty, NULL) < 0) && (!vty->got_epipe)) { - log_info("User failed to authenticate (ip: %s)", vty->ip); - goto out_clean; - } - if (vty->got_epipe) - goto out_clean; - - log_info("User %s connected from %s", vty->username, vty->ip); - knet_vty_write(vty, "Welcome %s (%s) on vty(%d)\n\n", vty->username, vty->ip, vty->conn_num); - if (vty->got_epipe) - goto out_clean; - - if (knet_vty_set_iacs(vty) < 0) { - knet_vty_write(vty, "Unable to set telnet session preferences"); - goto out_clean; - } - if (vty->got_epipe) - goto out_clean; - - knet_vty_cli_bind(vty); - -out_clean: - pthread_mutex_lock(&knet_vty_mutex); - knet_vty_close(vty); - pthread_mutex_unlock(&knet_vty_mutex); - - return NULL; -} - -/* - * mainloop is not thread safe as there should only be one - */ -int knet_vty_main_loop(int debug) -{ - int logfd[2]; - int vty_listener6_fd; - int vty_listener4_fd; - int vty_listener_fd; - int vty_accept_fd; - struct sockaddr_storage incoming_sa; - socklen_t salen; - fd_set rfds; - int se_result = 0; - struct timeval tv; - int err = 0; - int conn_index, found; - - signal(SIGTERM, sigterm_handler); - signal(SIGINT, sigterm_handler); - signal(SIGPIPE, sigpipe_handler); - - if (pipe(logfd)) { - log_error("Unable to create logging pipe"); - return -1; - } - - if ((_fdset_cloexec(logfd[0])) || - (_fdset_nonblock(logfd[0])) || - (_fdset_cloexec(logfd[1])) || - (_fdset_nonblock(logfd[1]))) { - log_error("Unable to set FD_CLOEXEX / O_NONBLOCK on logfd pipe"); - return -1; - } - - err = pthread_create(&logging_thread, - NULL, _handle_logging_thread, - (void *)&logfd[0]); - if (err) { - log_error("Unable to create logging thread"); - return -1; - } - - memset(&knet_vtys, 0, sizeof(knet_vtys)); - memset(&vty_global_conf, 0, sizeof(struct knet_vty_global_conf)); - vty_global_conf.idle_timeout = KNET_VTY_CLI_TIMEOUT; - - for(conn_index = 0; conn_index < KNET_VTY_TOTAL_MAX_CONN; conn_index++) { - knet_vtys[conn_index].logfd = logfd[1]; - knet_vtys[conn_index].vty_global_conf = &vty_global_conf; - if (debug) { - knet_vtys[conn_index].loglevel = KNET_LOG_DEBUG; - } else { - knet_vtys[conn_index].loglevel = KNET_LOG_INFO; - } - } - - if (knet_read_conf() < 0) { - log_error("Unable to read config file %s", knet_cfg_head.conffile); - return -1; - } - - vty_listener6_fd = knet_vty_init_listener(knet_cfg_head.vty_ipv6, - knet_cfg_head.vty_port); - if (vty_listener6_fd < 0) { - log_error("Unable to setup vty listener for ipv6"); - return -1; - } - - vty_listener4_fd = knet_vty_init_listener(knet_cfg_head.vty_ipv4, - knet_cfg_head.vty_port); - - if (vty_listener4_fd < 0) { - log_error("Unable to setup vty listener for ipv4"); - goto out; - } - - while (se_result >= 0 && !daemon_quit) { - FD_ZERO (&rfds); - FD_SET (vty_listener6_fd, &rfds); - FD_SET (vty_listener4_fd, &rfds); - - tv.tv_sec = 1; - tv.tv_usec = 0; - - se_result = select(FD_SETSIZE, &rfds, 0, 0, &tv); - - if ((se_result == -1) && (daemon_quit)) { - log_info("Got a SIGTERM, requesting CLI threads to exit"); - for(conn_index = 0; conn_index < KNET_VTY_TOTAL_MAX_CONN; conn_index++) { - if (knet_vtys[conn_index].active) { - knet_vty_write(&knet_vtys[conn_index], "%s%sServer is going down..%s%s", - telnet_newline, telnet_newline, telnet_newline, telnet_newline); - knet_vty_close(&knet_vtys[conn_index]); - knet_vtys[conn_index].got_epipe = 1; - } - } - sleep(2); /* give time to all vty to exit */ - knet_close_down(); - log_info("Have a nice day! Goodbye"); - goto out; - } - - if (se_result == -1) { - err = se_result; - log_error("Unable to select on vty listener socket!"); - goto out; - } - - if (se_result == 0) { - pthread_mutex_lock(&knet_vty_mutex); - for(conn_index = 0; conn_index < KNET_VTY_TOTAL_MAX_CONN; conn_index++) { - if ((knet_vtys[conn_index].active) && - (knet_vtys[conn_index].idle_timeout)) { - knet_vtys[conn_index].idle++; - if (knet_vtys[conn_index].idle > knet_vtys[conn_index].idle_timeout) { - knet_vty_close(&knet_vtys[conn_index]); - knet_vtys[conn_index].got_epipe = 1; - } - } - } - pthread_mutex_unlock(&knet_vty_mutex); - continue; - } - - if (FD_ISSET(vty_listener6_fd, &rfds)) { - vty_listener_fd = vty_listener6_fd; - } else if (FD_ISSET(vty_listener4_fd, &rfds)) { - vty_listener_fd = vty_listener4_fd; - } else { - continue; - } - - memset(&incoming_sa, 0, sizeof(struct sockaddr_storage)); - salen = sizeof(struct sockaddr_storage); - - vty_accept_fd = accept(vty_listener_fd, (struct sockaddr *)&incoming_sa, &salen); - if (vty_accept_fd < 0) { - log_error("Unable to accept connection to vty"); - continue; - } - - // check for ip address access list here against incoming_sa - - pthread_mutex_lock(&knet_vty_mutex); - - found = 0; - for(conn_index = 0; conn_index <= vty_max_connections; conn_index++) { - if (knet_vtys[conn_index].active == 0) { - found = 1; - break; - } - } - - if ((vty_current_connections == vty_max_connections) || (!found)) { - errno = ECONNREFUSED; - log_error("Too many connections to VTY or no available slots"); - close(vty_accept_fd); - pthread_mutex_unlock(&knet_vty_mutex); - continue; - } - - vty_current_connections++; - - memset(&knet_vtys[conn_index], 0, - sizeof(struct knet_vty)); - - knet_vtys[conn_index].vty_sock = vty_accept_fd; - knet_vtys[conn_index].conn_num = conn_index; - memmove(&knet_vtys[conn_index].src_sa, &incoming_sa, salen); - knet_vtys[conn_index].src_sa_len = salen; - knet_vtys[conn_index].active = 1; - knet_vtys[conn_index].logfd = logfd[1]; - knet_vtys[conn_index].vty_global_conf = &vty_global_conf; - knet_vtys[conn_index].idle_timeout = vty_global_conf.idle_timeout; - if (debug) { - knet_vtys[conn_index].loglevel = KNET_LOG_DEBUG; - } else { - knet_vtys[conn_index].loglevel = KNET_LOG_INFO; - } - - err = pthread_create(&knet_vtys[conn_index].vty_thread, - NULL, vty_accept_thread, - (void *)&conn_index); - if (err < 0) { - log_error("Unable to spawn vty thread"); - memset(&knet_vtys[conn_index], 0, - sizeof(struct knet_vty)); - vty_current_connections--; - } - - pthread_mutex_unlock(&knet_vty_mutex); - } - -out: - pthread_cancel(logging_thread); - knet_vty_close_listener(vty_listener6_fd); - knet_vty_close_listener(vty_listener4_fd); - close(logfd[0]); - close(logfd[1]); - - return err; -} - -/* -int knet_vty_set_max_connections(const int max_connections) -{ - int err = 0; - - pthread_mutex_lock(&knet_vty_mutex); - if ((max_connections > KNET_VTY_TOTAL_MAX_CONN) || - (max_connections < 1)) { - errno = EINVAL; - err = -1; - } else { - vty_max_connections = max_connections; - } - pthread_mutex_unlock(&knet_vty_mutex); - return err; -} -*/ diff --git a/kronosnetd/vty.h b/kronosnetd/vty.h deleted file mode 100644 index 87cfa198..00000000 --- a/kronosnetd/vty.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNETD_VTY_H__ -#define __KNETD_VTY_H__ - -#include -#include -#include -#include "libknet.h" - -#define KNET_VTY_DEFAULT_PORT 50000 - -#define KNET_VTY_DEFAULT_MAX_CONN 4 -#define KNET_VTY_TOTAL_MAX_CONN 16 -#define KNET_VTY_CLI_TIMEOUT 300 - -#define KNET_VTY_MAX_LINE 512 - -#define KNET_VTY_MAX_HIST 50 - -struct knet_vty_global_conf { - int idle_timeout; -}; - -struct knet_vty { - pthread_t vty_thread; /* thread struct for this vty */ - struct sockaddr_storage src_sa; /* source IP */ - socklen_t src_sa_len; /* sa len */ - char ip[128]; /* ip addr of source */ - char username[64]; /* username */ - char line[KNET_VTY_MAX_LINE]; /* input line */ - char *history[KNET_VTY_MAX_HIST]; /* history */ - int history_idx; /* index to history */ - int history_pos; /* position in the history */ - int insert_mode; /* add or insert */ - int line_idx; /* index on the input line */ - int cursor_pos; /* position of the cursor in the line */ - int escape; /* escape status */ - int escape_code; /* escape code buffer */ - int user_can_enable;/* user is in group kronosnetadm */ - int vty_sock; /* tcp socket for this vty */ - int conn_num; /* vty number */ - int active; /* vty is active */ - int got_epipe; /* vty_sock has been closed */ - int idle; /* idle time */ - int idle_timeout; /* in seconds or 0 to disable automatic logout */ - int node; /* node number of the menus */ - int prevnode; /* node number of the menus (used by VTY node) */ - void *param; /* pointer to cmd param */ - int paramoffset; /* required if param is set */ - int logfd; /* fd to pass to iface create */ - int loglevel; /* loglevel (debug, etc) */ - void *iface; /* pointer to iface we are working on */ - knet_node_id_t host_id; /* peer/host we are working on */ - uint8_t link_id; /* link id we are working on */ - int filemode; /* tell print_conf to add or not carriage return */ - struct knet_vty_global_conf *vty_global_conf; /* pointer to vty global config */ -}; - -extern pthread_mutex_t knet_vty_mutex; -extern int knet_vty_config; - -extern struct knet_vty knet_vtys[KNET_VTY_TOTAL_MAX_CONN]; - -int knet_vty_main_loop(int debug); - -#endif diff --git a/kronosnetd/vty_auth.c b/kronosnetd/vty_auth.c deleted file mode 100644 index 634a535b..00000000 --- a/kronosnetd/vty_auth.c +++ /dev/null @@ -1,305 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "logging.h" -#include "vty_auth.h" -#include "vty_utils.h" - -static int knet_pam_misc_conv(int num_msg, const struct pam_message **msgm, - struct pam_response **response, void *appdata_ptr) -{ - int count = 0; - struct pam_response *reply; - struct knet_vty *vty = (struct knet_vty *)appdata_ptr; - - if (num_msg <= 0) - return PAM_CONV_ERR; - - reply = (struct pam_response *) calloc(num_msg, sizeof(struct pam_response)); - - if (reply == NULL) - return PAM_CONV_ERR; - - for (count=0; count < num_msg; ++count) { - unsigned char readbuf[VTY_MAX_BUFFER_SIZE]; - char *string=NULL; - int nc; - - memset(readbuf, 0, sizeof(readbuf)); - - switch (msgm[count]->msg_style) { - case PAM_PROMPT_ECHO_OFF: - if (knet_vty_set_echo(vty, 0) < 0) { - knet_vty_write(vty, "Unable to turn off terminal/telnet echo"); - goto failed_conversation; - } - knet_vty_write(vty, "%s", msgm[count]->msg); - nc = knet_vty_read(vty, readbuf, sizeof(readbuf)); - if (nc < 0) - goto failed_conversation; - if (knet_vty_set_echo(vty, 1) < 0) { - /* doesn't really make a lot of sense tho.... */ - knet_vty_write(vty, "Unable to turn on terminal/telnet echo"); - goto failed_conversation; - } - knet_vty_write(vty, "\n"); - readbuf[nc-2] = 0; - string = strdup((const char*)readbuf); - if (!string) - goto failed_conversation; - break; - case PAM_PROMPT_ECHO_ON: - knet_vty_write(vty, "\n%s", msgm[count]->msg); - nc = knet_vty_read(vty, readbuf, sizeof(readbuf)); - if (nc < 0) - goto failed_conversation; - readbuf[nc-2] = 0; - string = strdup((const char*)readbuf); - if (!string) - goto failed_conversation; - break; - case PAM_ERROR_MSG: - log_error("Received PAM error message %s", msgm[count]->msg); - knet_vty_write(vty, "%s", msgm[count]->msg); - break; - case PAM_TEXT_INFO: - log_error("Received PAM text info: %s", msgm[count]->msg); - knet_vty_write(vty, "%s", msgm[count]->msg); - break; - default: - if (!vty->got_epipe) { - log_error("Unknown PAM conversation message"); - knet_vty_write(vty, "Unknown PAM conversation message"); - } - goto failed_conversation; - } - - if (string) { - reply[count].resp_retcode = 0; - reply[count].resp = string; - string = NULL; - } - } - - *response = reply; - reply = NULL; - - return PAM_SUCCESS; - -failed_conversation: - if (!vty->got_epipe) { - log_error("PAM conversation error"); - knet_vty_write(vty, "PAM conversation error"); - } - if (reply) { - for (count=0; count < num_msg; ++count) { - if (reply[count].resp == NULL) - continue; - switch (msgm[count]->msg_style) { - case PAM_PROMPT_ECHO_ON: - case PAM_PROMPT_ECHO_OFF: - _pam_overwrite(reply[count].resp); - free(reply[count].resp); - break; - case PAM_BINARY_PROMPT: - { - void *bt_ptr = reply[count].resp; - pam_binary_handler_free(appdata_ptr, bt_ptr); - break; - } - case PAM_ERROR_MSG: - case PAM_TEXT_INFO: - free(reply[count].resp); - } - } - free(reply); - reply = NULL; - } - - return PAM_CONV_ERR; -} - -static int knet_vty_get_pam_user(struct knet_vty *vty, pam_handle_t *pamh) -{ - const void *value; - int err; - - memset(vty->username, 0, sizeof(vty->username)); - - err = pam_get_item(pamh, PAM_USER, &value); - - if (err != PAM_SUCCESS) - return err; - - strncpy(vty->username, (const char*)value, 32); - - return 0; -} - -static int knet_vty_pam_auth_user(struct knet_vty *vty, const char *user) -{ - pam_handle_t *pamh=NULL; - struct pam_conv conv; - int err; - int retry = 1; - - conv.conv = knet_pam_misc_conv; - conv.appdata_ptr = (void *)vty; - -retry_auth: - err = pam_start("kronosnetd", user, &conv, &pamh); - if (err != PAM_SUCCESS) { - errno = EINVAL; - log_error("PAM fatal error: %s", pam_strerror(pamh, err)); - knet_vty_write(vty, "PAM fatal error: %s", - pam_strerror(pamh, err)); - goto out_fatal; - } - - if (pam_set_item(pamh, PAM_USER_PROMPT, (const void *)"login: ") != PAM_SUCCESS) { - log_error("PAM fatal error: %s", pam_strerror(pamh, err)); - knet_vty_write(vty, "PAM fatal error: %s", - pam_strerror(pamh, err)); - goto out_fatal; - } - - err = pam_authenticate(pamh, 0); - if (err != PAM_SUCCESS) { - if (vty->got_epipe) { - errno = EPIPE; - goto out_fatal; - } else { - errno = EINVAL; - goto out_clean; - } - } - - if (knet_vty_get_pam_user(vty, pamh) != PAM_SUCCESS) { - log_error("PAM: unable to get PAM_USER: %s", - pam_strerror(pamh, err)); - knet_vty_write(vty, "PAM: unable to get PAM_USER: %s", - pam_strerror(pamh, err)); - goto out_clean; - } - - err = pam_acct_mgmt(pamh, 0); - if (err != PAM_SUCCESS) { - log_info("User: %s failed to authenticate on vty(%d) attempt %d", - vty->username, vty->conn_num, retry); - goto out_clean; - } - -out_clean: - if (pamh) { - pam_end(pamh, err); - pamh = NULL; - } - - if ((err != PAM_SUCCESS) && (retry < AUTH_MAX_RETRY)) { - retry++; - goto retry_auth; - } - -out_fatal: - if (pamh) { - pam_end(pamh, err); - pamh = NULL; - } - - knet_vty_write(vty, "\n"); - - return err; -} - -static int knet_vty_group_check(struct knet_vty *vty) -{ - struct group grp; - char *buf; - size_t buflen; - long int initlen; - struct group *result; - char *gr_mem; - int err, i; - - errno = 0; - initlen = sysconf(_SC_GETGR_R_SIZE_MAX); - if ((initlen < 0) && (errno == EINVAL)) - return -1; - - if (initlen < 0) - initlen = 1024; - - buflen = (size_t) initlen; - - buf = malloc(buflen); - if (!buf) - return -1; - - while ((err = getgrnam_r(DEFAULTADMGROUP, &grp, buf, buflen, &result)) == ERANGE) { - size_t newlen = 2 * buflen; - char *newbuf; - - newbuf = realloc(buf, newlen); - if (!newbuf) { - err = -1; - goto out_clean; - } - buf = newbuf; - } - if (err) - goto out_clean; - - if (result == NULL) { - errno = EACCES; - log_error("No " DEFAULTADMGROUP " group found on the system"); - knet_vty_write(vty, "No " DEFAULTADMGROUP " group found on the system\n"); - err = -1; - goto out_clean; - } - - gr_mem = *grp.gr_mem; - - i = 0; - while(gr_mem != NULL) { - if (!strcmp(vty->username, gr_mem)) { - vty->user_can_enable = 1; - break; - } - gr_mem = *(grp.gr_mem + i); - i++; - } - -out_clean: - free(buf); - - return err; -} - -int knet_vty_auth_user(struct knet_vty *vty, const char *user) -{ - int err; - - err = knet_vty_pam_auth_user(vty, user); - if (err != PAM_SUCCESS) - return -1; - - return knet_vty_group_check(vty); -} diff --git a/kronosnetd/vty_auth.h b/kronosnetd/vty_auth.h deleted file mode 100644 index 79c75118..00000000 --- a/kronosnetd/vty_auth.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNETD_VTY_AUTH_H__ -#define __KNETD_VTY_AUTH_H__ - -#include "vty.h" - -#define AUTH_MAX_RETRY 3 - -int knet_vty_auth_user(struct knet_vty *vty, const char *user); - -#endif diff --git a/kronosnetd/vty_cli.c b/kronosnetd/vty_cli.c deleted file mode 100644 index 36065c7f..00000000 --- a/kronosnetd/vty_cli.c +++ /dev/null @@ -1,531 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include - -#include "logging.h" -#include "vty.h" -#include "vty_cli.h" -#include "vty_cli_cmds.h" -#include "vty_utils.h" - -/* if this code looks like quagga lib/vty.c it is because we stole it in good part */ - -#define CONTROL(X) ((X) - '@') -#define VTY_NORMAL 0 -#define VTY_PRE_ESCAPE 1 -#define VTY_ESCAPE 2 -#define VTY_EXT_ESCAPE 3 - -static void knet_vty_reset_buf(struct knet_vty *vty) -{ - memset(vty->line, 0, sizeof(vty->line)); - vty->line_idx = 0; - vty->cursor_pos = 0; - vty->history_pos = vty->history_idx; -} - -static void knet_vty_add_to_buf(struct knet_vty *vty, unsigned char *buf, int pos) -{ - char outbuf[2]; - int i; - - if (vty->cursor_pos == vty->line_idx) { - vty->line[vty->line_idx] = buf[pos]; - vty->line_idx++; - vty->cursor_pos++; - } else { - if (!vty->insert_mode) { - memmove(&vty->line[vty->cursor_pos+1], &vty->line[vty->cursor_pos], - vty->line_idx - vty->cursor_pos); - vty->line_idx++; - } - vty->line[vty->cursor_pos] = buf[pos]; - vty->cursor_pos++; - } - - outbuf[0] = buf[pos]; - outbuf[1] = 0; - knet_vty_write(vty, "%s%s", outbuf, &vty->line[vty->cursor_pos]); - for (i = 0; i < (vty->line_idx - vty->cursor_pos); i++) - knet_vty_write(vty, "%s", telnet_backward_char); -} - -static void knet_vty_forward_char(struct knet_vty *vty) -{ - char buf[2]; - - if (vty->cursor_pos < vty->line_idx) { - buf[0] = vty->line[vty->cursor_pos]; - buf[1] = 0; - knet_vty_write(vty, "%s", buf); - vty->cursor_pos++; - } -} - -static void knet_vty_backward_char(struct knet_vty *vty) -{ - if (vty->cursor_pos > 0) { - knet_vty_write(vty, "%s", telnet_backward_char); - vty->cursor_pos--; - } -} - -static void knet_vty_kill_line(struct knet_vty *vty) -{ - int size, i; - - size = vty->line_idx - vty->cursor_pos; - - if (size == 0) - return; - - for (i = 0; i < size; i++) - knet_vty_write(vty, " "); - - for (i = 0; i < size; i++) - knet_vty_write(vty, "%s", telnet_backward_char); - - memset(&vty->line[vty->cursor_pos], 0, size); - vty->line_idx = vty->cursor_pos; -} - -static void knet_vty_newline(struct knet_vty *vty) -{ - knet_vty_write(vty, "%s", telnet_newline); - knet_vty_reset_buf(vty); - knet_vty_prompt(vty); -} - -static void knet_vty_delete_char(struct knet_vty *vty) -{ - int size, i; - - if (vty->line_idx == 0) { - knet_vty_exit_node(vty); - if (!vty->got_epipe) { - knet_vty_newline(vty); - } - return; - } - - if (vty->line_idx == vty->cursor_pos) - return; - - size = vty->line_idx - vty->cursor_pos; - - vty->line_idx--; - memmove(&vty->line[vty->cursor_pos], &vty->line[vty->cursor_pos+1], - size - 1); - vty->line[vty->line_idx] = '\0'; - - knet_vty_write(vty, "%s ", &vty->line[vty->cursor_pos]); - for (i = 0; i < size; i++) - knet_vty_write(vty, "%s", telnet_backward_char); -} - -static void knet_vty_delete_backward_char(struct knet_vty *vty) -{ - if (vty->cursor_pos == 0) - return; - - knet_vty_backward_char(vty); - knet_vty_delete_char(vty); -} - -static void knet_vty_beginning_of_line(struct knet_vty *vty) -{ - while (vty->cursor_pos != 0) - knet_vty_backward_char(vty); -} - -static void knet_vty_end_of_line(struct knet_vty *vty) -{ - while (vty->cursor_pos != vty->line_idx) - knet_vty_forward_char(vty); -} - -static void knet_vty_kill_line_from_beginning(struct knet_vty *vty) -{ - knet_vty_beginning_of_line(vty); - knet_vty_kill_line(vty); -} - -static void knet_vty_backward_word(struct knet_vty *vty) -{ - while(vty->cursor_pos > 0 && vty->line[vty->cursor_pos - 1] == ' ') - knet_vty_backward_char(vty); - - while(vty->cursor_pos > 0 && vty->line[vty->cursor_pos - 1] != ' ') - knet_vty_backward_char(vty); -} - -static void knet_vty_forward_word(struct knet_vty *vty) -{ - while(vty->cursor_pos != vty->line_idx && vty->line[vty->cursor_pos] == ' ') - knet_vty_forward_char(vty); - - while(vty->cursor_pos != vty->line_idx && vty->line[vty->cursor_pos] != ' ') - knet_vty_forward_char(vty); -} - -static void knet_vty_backward_kill_word(struct knet_vty *vty) -{ - while(vty->cursor_pos > 0 && vty->line[vty->cursor_pos - 1] == ' ') - knet_vty_delete_backward_char(vty); - - while(vty->cursor_pos > 0 && vty->line[vty->cursor_pos - 1] != ' ') - knet_vty_delete_backward_char(vty); -} - -static void knet_vty_forward_kill_word(struct knet_vty *vty) -{ - while(vty->cursor_pos != vty->line_idx && vty->line[vty->cursor_pos] == ' ') - knet_vty_delete_char(vty); - - while(vty->cursor_pos != vty->line_idx && vty->line[vty->cursor_pos] != ' ') - knet_vty_delete_backward_char(vty); -} - -static void knet_vty_transpose_chars(struct knet_vty *vty) -{ - unsigned char swap[2]; - - if (vty->line_idx < 2 || vty->cursor_pos < 2) - return; - - swap[0] = vty->line[vty->cursor_pos - 1]; - swap[1] = vty->line[vty->cursor_pos - 2]; - knet_vty_delete_backward_char(vty); - knet_vty_delete_backward_char(vty); - knet_vty_add_to_buf(vty, swap, 0); - knet_vty_add_to_buf(vty, swap, 1); -} - -static void knet_vty_history_add(struct knet_vty *vty) -{ - int idx; - - if (knet_vty_is_line_empty(vty)) - return; - - idx = vty->history_idx % KNET_VTY_MAX_HIST; - - if (vty->history[idx]) { - free(vty->history[idx]); - vty->history[idx] = NULL; - } - - vty->history[idx] = strdup(vty->line); - if (vty->history[idx] == NULL) { - log_error("Not enough memory to add history lines!"); - knet_vty_write(vty, "Not enough memory to add history lines!"); - } - - vty->history_idx++; - - if (vty->history_idx == KNET_VTY_MAX_HIST) - vty->history_idx = 0; - - vty->history_pos = vty->history_idx; -} - -static void knet_vty_history_print(struct knet_vty *vty) -{ - int len; - - knet_vty_kill_line_from_beginning(vty); - - len = strlen(vty->history[vty->history_pos]); - memmove(vty->line, vty->history[vty->history_pos], len); - vty->cursor_pos = vty->line_idx = len; - - knet_vty_write(vty, "%s", vty->line); -} - -static void knet_vty_history_prev(struct knet_vty *vty) -{ - int idx; - - idx = vty->history_pos; - - if (idx == 0) { - idx = KNET_VTY_MAX_HIST - 1; - } else { - idx--; - } - - if (vty->history[idx] == NULL) - return; - - vty->history_pos = idx; - - knet_vty_history_print(vty); -} - -static void knet_vty_history_next(struct knet_vty *vty) -{ - int idx; - - if (vty->history_pos == vty->history_idx) - return; - - idx = vty->history_pos; - - if (idx == (KNET_VTY_MAX_HIST - 1)) { - idx = 0; - } else { - idx++; - } - - if (vty->history[idx] == NULL) { - knet_vty_kill_line_from_beginning(vty); - vty->history_pos = vty->history_idx; - return; - } - - vty->history_pos = idx; - - knet_vty_history_print(vty); -} - -static int knet_vty_process_buf(struct knet_vty *vty, unsigned char *buf, int buflen) -{ - int i; - - if (vty->line_idx >= KNET_VTY_MAX_LINE) - return -1; - - for (i = 0; i < buflen; i++) { - if (vty->escape == VTY_EXT_ESCAPE) { - if (buf[i] != '~') - goto vty_ext_escape_out; - - switch (vty->escape_code) { - case ('1'): - knet_vty_beginning_of_line(vty); - break; - case ('2'): - if (!vty->insert_mode) { - vty->insert_mode = 1; - } else { - vty->insert_mode = 0; - } - break; - case ('3'): - knet_vty_delete_char(vty); - break; - case ('4'): - knet_vty_end_of_line(vty); - break; - case ('5'): - knet_vty_history_prev(vty); - break; - case ('6'): - knet_vty_history_next(vty); - break; - } - -vty_ext_escape_out: - vty->escape = VTY_NORMAL; - continue; - } - - if (vty->escape == VTY_ESCAPE) { - switch (buf[i]) { - case ('A'): - knet_vty_history_prev(vty); - break; - case ('B'): - knet_vty_history_next(vty); - break; - case ('C'): - knet_vty_forward_char(vty); - break; - case ('D'): - knet_vty_backward_char(vty); - break; - case ('H'): - knet_vty_beginning_of_line(vty); - break; - case ('F'): - knet_vty_end_of_line(vty); - break; - case ('1'): - case ('2'): - case ('3'): - case ('4'): - case ('5'): - case ('6'): - vty->escape = VTY_EXT_ESCAPE; - vty->escape_code = buf[i]; - break; - default: - break; - } - - if (vty->escape == VTY_ESCAPE) - vty->escape = VTY_NORMAL; - - continue; - } - - if (vty->escape == VTY_PRE_ESCAPE) { - switch (buf[i]) { - case 'O': - case '[': - vty->escape = VTY_ESCAPE; - break; - case 'b': - vty->escape = VTY_NORMAL; - knet_vty_backward_word(vty); - break; - case 'f': - vty->escape = VTY_NORMAL; - knet_vty_forward_word(vty); - break; - case 'd': - vty->escape = VTY_NORMAL; - knet_vty_forward_kill_word(vty); - break; - case CONTROL('H'): - case 0x7f: - vty->escape = VTY_NORMAL; - knet_vty_backward_kill_word(vty); - break; - default: - break; - } - continue; - } - - switch (buf[i]) { - case CONTROL('A'): - knet_vty_beginning_of_line(vty); - break; - case CONTROL('B'): - knet_vty_backward_char(vty); - break; - case CONTROL('C'): - knet_vty_newline(vty); - break; - case CONTROL('D'): - knet_vty_delete_char(vty); - break; - case CONTROL('E'): - knet_vty_end_of_line(vty); - break; - case CONTROL('F'): - knet_vty_forward_char(vty); - break; - case CONTROL('H'): - case 0x7f: - knet_vty_delete_backward_char(vty); - break; - case CONTROL('K'): - knet_vty_kill_line(vty); - break; - case CONTROL('N'): - knet_vty_history_next(vty); - break; - case CONTROL('P'): - knet_vty_history_prev(vty); - break; - case CONTROL('T'): - knet_vty_transpose_chars(vty); - break; - case CONTROL('U'): - knet_vty_kill_line_from_beginning(vty); - break; - case CONTROL('W'): - knet_vty_backward_kill_word(vty); - break; - case CONTROL('Z'): - vty->node = NODE_CONFIG; - knet_vty_exit_node(vty); - knet_vty_newline(vty); - break; - case '\n': - case '\r': - knet_vty_end_of_line(vty); - knet_vty_write(vty, "%s", telnet_newline); - knet_vty_history_add(vty); - knet_vty_execute_cmd(vty); - knet_vty_reset_buf(vty); - knet_vty_prompt(vty); - break; - case '\t': - knet_vty_end_of_line(vty); - knet_vty_tab_completion(vty); - break; - case '?': - knet_vty_end_of_line(vty); - knet_vty_write(vty, "%s", telnet_newline); - knet_vty_help(vty); - knet_vty_prompt(vty); - knet_vty_write(vty, "%s", vty->line); - break; - case '\033': - vty->escape = VTY_PRE_ESCAPE; - break; - default: - if (buf[i] > 31 && buf[i] < 127) - knet_vty_add_to_buf(vty, buf, i); - break; - } - } - - return 0; -} - -void knet_vty_cli_bind(struct knet_vty *vty) -{ - int se_result = 0; - fd_set rfds; - struct timeval tv; - unsigned char buf[VTY_MAX_BUFFER_SIZE]; - int readlen; - - knet_vty_prompt(vty); - - while (se_result >= 0 && !vty->got_epipe) { - FD_ZERO (&rfds); - FD_SET (vty->vty_sock, &rfds); - - tv.tv_sec = 1; - tv.tv_usec = 0; - - se_result = select((vty->vty_sock + 1), &rfds, 0, 0, &tv); - - if ((se_result == -1) || (vty->got_epipe)) - goto out_clean; - - if ((se_result == 0) || (!FD_ISSET(vty->vty_sock, &rfds))) - continue; - - memset(buf, 0 , sizeof(buf)); - readlen = knet_vty_read(vty, buf, sizeof(buf)); - if (readlen <= 0) - goto out_clean; - - if (knet_vty_process_buf(vty, buf, readlen) < 0) { - knet_vty_write(vty, "\nError processing command: command too long\n"); - knet_vty_reset_buf(vty); - } - } - -out_clean: - - return; -} diff --git a/kronosnetd/vty_cli.h b/kronosnetd/vty_cli.h deleted file mode 100644 index 0509c381..00000000 --- a/kronosnetd/vty_cli.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNETD_VTY_CLI_H__ -#define __KNETD_VTY_CLI_H__ - -#include "vty.h" - -static const char telnet_backward_char[] = { 0x08, 0x0 }; -static const char telnet_newline[] = { '\n', '\r', 0x0 }; -static const char file_newline[] = { '\n', 0x0 }; - -void knet_vty_cli_bind(struct knet_vty *vty); - -#endif diff --git a/kronosnetd/vty_cli_cmds.c b/kronosnetd/vty_cli_cmds.c deleted file mode 100644 index 36b6b6a1..00000000 --- a/kronosnetd/vty_cli_cmds.c +++ /dev/null @@ -1,2185 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include - -#include "cfg.h" -#include "etherfilter.h" -#include "logging.h" -#include "libtap.h" -#include "netutils.h" -#include "vty.h" -#include "vty_cli.h" -#include "vty_cli_cmds.h" -#include "vty_utils.h" - -#define KNET_VTY_MAX_MATCHES 64 -#define KNET_VTY_MATCH_HELP 0 -#define KNET_VTY_MATCH_EXEC 1 -#define KNET_VTY_MATCH_EXPAND 2 - -#define CMDS_PARAM_NOMORE 0 -#define CMDS_PARAM_KNET 1 -#define CMDS_PARAM_IP 2 -#define CMDS_PARAM_IP_PREFIX 3 -#define CMDS_PARAM_IP_PORT 4 -#define CMDS_PARAM_BOOL 5 -#define CMDS_PARAM_INT 6 -#define CMDS_PARAM_NODEID 7 -#define CMDS_PARAM_NAME 8 -#define CMDS_PARAM_MTU 9 -#define CMDS_PARAM_CRYPTO_MODEL 10 -#define CMDS_PARAM_CRYPTO_TYPE 11 -#define CMDS_PARAM_HASH_TYPE 12 -#define CMDS_PARAM_POLICY 13 -#define CMDS_PARAM_LINK_ID 14 -#define CMDS_PARAM_LINK_PRI 15 -#define CMDS_PARAM_LINK_KEEPAL 16 -#define CMDS_PARAM_LINK_HOLDTI 17 -#define CMDS_PARAM_LINK_PONG 18 -#define CMDS_PARAM_VTY_TIMEOUT 19 -#define CMDS_PARAM_PMTU_FREQ 20 -#define CMDS_PARAM_LINK_TRANSP 21 - -/* - * CLI helper functions - menu/node stuff starts below - */ - - -/* - * return 0 if we find a command in vty->line and cmd/len/no are set - * return -1 if we cannot find a command. no can be trusted. cmd/len would be empty - */ - -static int get_command(struct knet_vty *vty, char **cmd, int *cmdlen, int *cmdoffset, int *no) -{ - int start = 0, idx; - - for (idx = 0; idx < vty->line_idx; idx++) { - if (vty->line[idx] != ' ') - break; - } - - if (!strncmp(&vty->line[idx], "no ", 3)) { - *no = 1; - idx = idx + 3; - - for (; idx < vty->line_idx; idx++) { - if (vty->line[idx] != ' ') - break; - } - } else { - *no = 0; - } - - start = idx; - if (start == vty->line_idx) - return -1; - - *cmd = &vty->line[start]; - *cmdoffset = start; - - for (idx = start; idx < vty->line_idx; idx++) { - if (vty->line[idx] == ' ') - break; - } - - *cmdlen = idx - start; - - return 0; -} - -/* - * still not sure why I need to count backwards... - */ -static void get_n_word_from_end(struct knet_vty *vty, int n, - char **word, int *wlen, int *woffset) -{ - int widx; - int idx, end, start; - - start = end = vty->line_idx; - - for (widx = 0; widx < n; widx++) { - for (idx = start - 1; idx > 0; idx--) { - if (vty->line[idx] != ' ') - break; - } - end = idx; - for (idx = end; idx > 0; idx--) { - if (vty->line[idx-1] == ' ') - break; - } - start = idx; - } - - *wlen = (end - start) + 1; - *word = &vty->line[start]; - *woffset = start; -} - -static int expected_params(const vty_param_t *params) -{ - int idx = 0; - - while(params[idx].param != CMDS_PARAM_NOMORE) - idx++; - - return idx; -} - -static int count_words(struct knet_vty *vty, - int offset) -{ - int idx, widx = 0; - int status = 0; - - for (idx = offset; idx < vty->line_idx; idx++) { - if (vty->line[idx] == ' ') { - status = 0; - continue; - } - if ((vty->line[idx] != ' ') && (!status)) { - widx++; - status = 1; - continue; - } - } - return widx; -} - -static int param_to_int(const char *param, int paramlen) -{ - char buf[KNET_VTY_MAX_LINE]; - - memset(buf, 0, sizeof(buf)); - memmove(buf, param, paramlen); - return atoi(buf); -} - -static int param_to_str(char *buf, int bufsize, const char *param, int paramlen) -{ - if (bufsize < paramlen) - return -1; - - memset(buf, 0, bufsize); - memmove(buf, param, paramlen); - return paramlen; -} - -static const vty_node_cmds_t *get_cmds(struct knet_vty *vty, char **cmd, int *cmdlen, int *cmdoffset) -{ - int no; - const vty_node_cmds_t *cmds = knet_vty_nodes[vty->node].cmds; - - get_command(vty, cmd, cmdlen, cmdoffset, &no); - - if (no) - cmds = knet_vty_nodes[vty->node].no_cmds; - - return cmds; -} - -static int check_param(struct knet_vty *vty, const int paramtype, char *param, int paramlen) -{ - int err = 0; - char buf[KNET_VTY_MAX_LINE]; - int tmp; - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - - memset(buf, 0, sizeof(buf)); - - switch(paramtype) { - case CMDS_PARAM_NOMORE: - break; - case CMDS_PARAM_KNET: - if (paramlen >= IFNAMSIZ) { - knet_vty_write(vty, "interface name too long%s", telnet_newline); - err = -1; - } - break; - case CMDS_PARAM_IP: - break; - case CMDS_PARAM_IP_PREFIX: - break; - case CMDS_PARAM_IP_PORT: - tmp = param_to_int(param, paramlen); - if ((tmp < 0) || (tmp > 65279)) { - knet_vty_write(vty, "port number must be a value between 0 and 65279%s", telnet_newline); - err = -1; - } - break; - case CMDS_PARAM_BOOL: - break; - case CMDS_PARAM_INT: - break; - case CMDS_PARAM_NODEID: - tmp = param_to_int(param, paramlen); - if ((tmp < 0) || (tmp > 255)) { - knet_vty_write(vty, "node id must be a value between 0 and 255%s", telnet_newline); - err = -1; - } - break; - case CMDS_PARAM_NAME: - if (paramlen >= KNET_MAX_HOST_LEN) { - knet_vty_write(vty, "name cannot exceed %d char in len%s", KNET_MAX_HOST_LEN - 1, telnet_newline); - } - break; - case CMDS_PARAM_MTU: - tmp = param_to_int(param, paramlen); - if ((tmp < 576) || (tmp > 65536)) { - knet_vty_write(vty, "mtu should be a value between 576 and 65536 (note: max value depends on the media)%s", telnet_newline); - err = -1; - } - break; - case CMDS_PARAM_PMTU_FREQ: - tmp = param_to_int(param, paramlen); - if ((tmp < 5) || (tmp > 600)) { - knet_vty_write(vty, "PMTUd frequency should be a value between 5 and 600%s", telnet_newline); - err = -1; - } - break; - case CMDS_PARAM_CRYPTO_MODEL: - param_to_str(buf, KNET_VTY_MAX_LINE, param, paramlen); - if (!strncmp("none", buf, 4)) - break; - if (!strncmp("nss", buf, 3)) - break; - knet_vty_write(vty, "unknown encryption model: %s. Supported: none/nss%s", param, telnet_newline); - err = -1; - break; - case CMDS_PARAM_CRYPTO_TYPE: - param_to_str(buf, KNET_VTY_MAX_LINE, param, paramlen); - if (!strncmp("none", buf, 4)) - break; - if (!strncmp("aes256", buf, 6)) - break; - if (!strncmp("aes192", buf, 6)) - break; - if (!strncmp("aes128", buf, 6)) - break; - if (!strncmp("3des", buf, 4)) - break; - knet_vty_write(vty, "unknown encryption method: %s. Supported: none/aes256/aes192/aes128/3des%s", param, telnet_newline); - err = -1; - break; - case CMDS_PARAM_HASH_TYPE: - param_to_str(buf, KNET_VTY_MAX_LINE, param, paramlen); - if (!strncmp("none", buf, 4)) - break; - if (!strncmp("md5", buf, 3)) - break; - if (!strncmp("sha1", buf, 4)) - break; - if (!strncmp("sha256", buf, 6)) - break; - if (!strncmp("sha384", buf, 6)) - break; - if (!strncmp("sha512", buf, 6)) - break; - knet_vty_write(vty, "unknown hash method: %s. Supported none/md5/sha1/sha256/sha384/sha512%s", param, telnet_newline); - err = -1; - break; - case CMDS_PARAM_POLICY: - param_to_str(buf, KNET_VTY_MAX_LINE, param, paramlen); - if (!strncmp("passive", buf, 7)) - break; - if (!strncmp("active", buf, 6)) - break; - if (!strncmp("round-robin", buf, 11)) - break; - knet_vty_write(vty, "unknown switching policy: %s. Supported passive/active/round-robin%s", param, telnet_newline); - err = -1; - break; - case CMDS_PARAM_LINK_ID: - tmp = param_to_int(param, paramlen); - if ((tmp < 0) || (tmp > 7)) { - knet_vty_write(vty, "link id should be a value between 0 and 7%s", telnet_newline); - err = -1; - } - break; - case CMDS_PARAM_LINK_TRANSP: - param_to_str(buf, KNET_VTY_MAX_LINE, param, paramlen); - if (knet_handle_get_transport_id_by_name(knet_iface->cfg_ring.knet_h, buf) == KNET_MAX_TRANSPORTS) { - knet_vty_write(vty, "link transport is invalid%s", telnet_newline); - err = -1; - } - break; - case CMDS_PARAM_LINK_PRI: - tmp = param_to_int(param, paramlen); - if ((tmp < 0) || (tmp > 255)) { - knet_vty_write(vty, "link priority should be a value between 0 and 256%s", telnet_newline); - err = -1; - } - break; - case CMDS_PARAM_LINK_KEEPAL: - tmp = param_to_int(param, paramlen); - if ((tmp <= 0) || (tmp > 60000)) { - knet_vty_write(vty, "link keepalive should be a value between 0 and 60000 (milliseconds). Default: 1000%s", telnet_newline); - err = -1; - } - break; - case CMDS_PARAM_LINK_HOLDTI: - tmp = param_to_int(param, paramlen); - if ((tmp <= 0) || (tmp > 60000)) { - knet_vty_write(vty, "link holdtimer should be a value between 0 and 60000 (milliseconds). Default: 5000%s", telnet_newline); - err = -1; - } - break; - case CMDS_PARAM_LINK_PONG: - tmp = param_to_int(param, paramlen); - if (tmp < 1) { - knet_vty_write(vty, "pong_count must be a value between 0 and 255%s", telnet_newline); - err = -1; - } - break; - case CMDS_PARAM_VTY_TIMEOUT: - tmp = param_to_int(param, paramlen); - if ((tmp < 0) || (tmp > 3600)) { - knet_vty_write(vty, "vty logout timeout should be a value between 0 (disabled) and 3600 seconds. Default: %d%s", KNET_VTY_CLI_TIMEOUT, telnet_newline); - } - break; - default: - knet_vty_write(vty, "CLI ERROR: unknown parameter type%s", telnet_newline); - err = -1; - break; - } - return err; -} - -static void describe_param(struct knet_vty *vty, const int paramtype) -{ - switch(paramtype) { - case CMDS_PARAM_NOMORE: - knet_vty_write(vty, "no more parameters%s", telnet_newline); - break; - case CMDS_PARAM_KNET: - knet_vty_write(vty, "KNET_IFACE_NAME - interface name (max %d chars) eg: kronosnet0%s", IFNAMSIZ, telnet_newline); - break; - case CMDS_PARAM_IP: - knet_vty_write(vty, "IP address - ipv4 or ipv6 address to add/remove%s", telnet_newline); - break; - case CMDS_PARAM_IP_PREFIX: - knet_vty_write(vty, "IP prefix len (eg. 24, 64)%s", telnet_newline); - break; - case CMDS_PARAM_IP_PORT: - knet_vty_write(vty, "base port (eg: %d) %s", KNET_RING_DEFPORT, telnet_newline); - case CMDS_PARAM_BOOL: - break; - case CMDS_PARAM_INT: - break; - case CMDS_PARAM_NODEID: - knet_vty_write(vty, "NODEID - unique identifier for this interface in this kronos network (value between 0 and 255)%s", telnet_newline); - break; - case CMDS_PARAM_NAME: - knet_vty_write(vty, "NAME - unique name identifier for this entity (max %d chars)%s", KNET_MAX_HOST_LEN - 1, telnet_newline); - break; - case CMDS_PARAM_MTU: - knet_vty_write(vty, "MTU - a value between 576 and 65536 (note: max value depends on the media)%s", telnet_newline); - break; - case CMDS_PARAM_PMTU_FREQ: - knet_vty_write(vty, "PMTUd frequency - a value in seconds between 5 and 600 (default: 5)%s", telnet_newline); - break; - case CMDS_PARAM_CRYPTO_MODEL: - knet_vty_write(vty, "MODEL - define encryption backend: none or nss%s", telnet_newline); - break; - case CMDS_PARAM_CRYPTO_TYPE: - knet_vty_write(vty, "CRYPTO - define packets encryption method: none or aes256%s", telnet_newline); - break; - case CMDS_PARAM_HASH_TYPE: - knet_vty_write(vty, "HASH - define packets hashing method: none/md5/sha1/sha256/sha384/sha512%s", telnet_newline); - break; - case CMDS_PARAM_POLICY: - knet_vty_write(vty, "POLICY - define packets switching policy: passive/active/round-robin%s", telnet_newline); - break; - case CMDS_PARAM_LINK_ID: - knet_vty_write(vty, "LINKID - specify the link identification number (0-7)%s", telnet_newline); - break; - case CMDS_PARAM_LINK_TRANSP: - knet_vty_write(vty, "TRANSPORT - specify the link transport protocol (UDP/SCTP/..)%s", telnet_newline); - break; - case CMDS_PARAM_LINK_PRI: - knet_vty_write(vty, "PRIORITY - specify the link priority for passive switching (0 to 255, default is 0). The higher value is preferred over lower value%s", telnet_newline); - break; - case CMDS_PARAM_LINK_KEEPAL: - knet_vty_write(vty, "KEEPALIVE - specify the keepalive interval for this link (0 to 60000 milliseconds, default is 1000).%s", telnet_newline); - break; - case CMDS_PARAM_LINK_HOLDTI: - knet_vty_write(vty, "HOLDTIME - specify how much time has to pass without connection before a link is considered dead (0 to 60000 milliseconds, default is 5000).%s", telnet_newline); - break; - case CMDS_PARAM_VTY_TIMEOUT: - knet_vty_write(vty, "VTY_TIMEOUT - specify the number of seconds before a session is automatically closed.%s", telnet_newline); - break; - default: /* this should never happen */ - knet_vty_write(vty, "CLI ERROR: unknown parameter type%s", telnet_newline); - break; - } -} - -static void print_help(struct knet_vty *vty, const vty_node_cmds_t *cmds, int idx) -{ - if ((idx < 0) || (cmds == NULL) || (cmds[idx].cmd == NULL)) - return; - - if (cmds[idx].help != NULL) { - knet_vty_write(vty, "%s\t%s%s", - cmds[idx].cmd, - cmds[idx].help, - telnet_newline); - } else { - knet_vty_write(vty, "%s\tNo help available for this command%s", - cmds[idx].cmd, - telnet_newline); - } -} - -static int get_param(struct knet_vty *vty, int wanted_paranum, - char **param, int *paramlen, int *paramoffset) -{ - int eparams, tparams; - const vty_param_t *params = (const vty_param_t *)vty->param; - int paramstart = vty->paramoffset; - - eparams = expected_params(params); - tparams = count_words(vty, paramstart); - - if (tparams > eparams) - return -1; - - if (wanted_paranum == -1) { - get_n_word_from_end(vty, 1, param, paramlen, paramoffset); - return tparams; - } - - if (tparams < wanted_paranum) - return -1; - - get_n_word_from_end(vty, (tparams - wanted_paranum) + 1, param, paramlen, paramoffset); - return tparams - wanted_paranum; -} - - -static int match_command(struct knet_vty *vty, const vty_node_cmds_t *cmds, - char *cmd, int cmdlen, int cmdoffset, int mode) -{ - int idx = 0, found = -1, paramoffset = 0, paramlen = 0, last_param = 0; - char *param = NULL; - int paramstart = cmdlen + cmdoffset; - int matches[KNET_VTY_MAX_MATCHES]; - - memset(&matches, -1, sizeof(matches)); - - while ((cmds[idx].cmd != NULL) && (idx < KNET_VTY_MAX_MATCHES)) { - if (!strncmp(cmds[idx].cmd, cmd, cmdlen)) { - found++; - matches[found] = idx; - } - idx++; - } - - if (idx >= KNET_VTY_MAX_MATCHES) { - knet_vty_write(vty, "Too many matches for this command%s", telnet_newline); - return -1; - } - - if (found < 0) { - knet_vty_write(vty, "There is no such command%s", telnet_newline); - return -1; - } - - switch(mode) { - case KNET_VTY_MATCH_HELP: - if (found == 0) { - if ((cmdoffset <= vty->cursor_pos) && (vty->cursor_pos <= paramstart)) { - print_help(vty, cmds, matches[0]); - break; - } - if (cmds[matches[0]].params != NULL) { - vty->param = (void *)cmds[matches[0]].params; - vty->paramoffset = paramstart; - last_param = get_param(vty, -1, ¶m, ¶mlen, ¶moffset); - - if ((paramoffset <= vty->cursor_pos) && (vty->cursor_pos <= (paramoffset + paramlen))) - last_param--; - - if (last_param >= CMDS_PARAM_NOMORE) { - describe_param(vty, cmds[matches[0]].params[last_param].param); - if (paramoffset > 0) - check_param(vty, cmds[matches[0]].params[last_param].param, param, paramlen); - } - break; - } - } - if (found >= 0) { - idx = 0; - while (matches[idx] >= 0) { - print_help(vty, cmds, matches[idx]); - idx++; - } - } - break; - case KNET_VTY_MATCH_EXEC: - if (found == 0) { - int exec = 0; - if (cmds[matches[0]].params != NULL) { - int eparams, tparams; - - eparams = expected_params(cmds[matches[0]].params); - tparams = count_words(vty, paramstart); - - if (eparams != tparams) { - exec = -1; - idx = 0; - - knet_vty_write(vty, "Parameter required for this command:%s", telnet_newline); - - while(cmds[matches[0]].params[idx].param != CMDS_PARAM_NOMORE) { - describe_param(vty, cmds[matches[0]].params[idx].param); - idx++; - } - break; - } - - idx = 0; - vty->param = (void *)cmds[matches[0]].params; - vty->paramoffset = paramstart; - while(cmds[matches[0]].params[idx].param != CMDS_PARAM_NOMORE) { - get_param(vty, idx + 1, ¶m, ¶mlen, ¶moffset); - if (check_param(vty, cmds[matches[0]].params[idx].param, - param, paramlen) < 0) { - exec = -1; - if (vty->filemode) - return -1; - } - - idx++; - } - } - if (!exec) { - if (cmds[matches[0]].params != NULL) { - vty->param = (void *)cmds[matches[0]].params; - vty->paramoffset = paramstart; - } - if (cmds[matches[0]].func != NULL) { - return cmds[matches[0]].func(vty); - } else { /* this will eventually disappear */ - knet_vty_write(vty, "no fn associated to this command%s", telnet_newline); - } - } - } - if (found > 0) { - knet_vty_write(vty, "Ambiguous command.%s", telnet_newline); - } - break; - case KNET_VTY_MATCH_EXPAND: - if (found == 0) { - int cmdreallen; - - if (vty->cursor_pos > cmdoffset+cmdlen) /* complete param? */ - break; - - cmdreallen = strlen(cmds[matches[0]].cmd); - memset(vty->line + cmdoffset, 0, cmdlen); - memmove(vty->line + cmdoffset, cmds[matches[0]].cmd, cmdreallen); - vty->line[cmdreallen + cmdoffset] = ' '; - vty->line_idx = cmdreallen + cmdoffset + 1; - vty->cursor_pos = cmdreallen + cmdoffset + 1; - } - if (found > 0) { /* add completion to string base root */ - int count = 0; - idx = 0; - while (matches[idx] >= 0) { - knet_vty_write(vty, "%s\t\t", cmds[matches[idx]].cmd); - idx++; - count++; - if (count == 4) { - knet_vty_write(vty, "%s",telnet_newline); - count = 0; - } - } - knet_vty_write(vty, "%s",telnet_newline); - } - break; - default: /* this should never really happen */ - log_info("Unknown match mode"); - break; - } - return found; -} - -/* forward declarations */ - -/* common to almost all nodes */ -static int knet_cmd_logout(struct knet_vty *vty); -static int knet_cmd_who(struct knet_vty *vty); -static int knet_cmd_exit_node(struct knet_vty *vty); -static int knet_cmd_help(struct knet_vty *vty); - -/* root node */ -static int knet_cmd_config(struct knet_vty *vty); - -/* config node */ -static int knet_cmd_interface(struct knet_vty *vty); -static int knet_cmd_no_interface(struct knet_vty *vty); -static int knet_cmd_status(struct knet_vty *vty); -static int knet_cmd_show_conf(struct knet_vty *vty); -static int knet_cmd_write_conf(struct knet_vty *vty); - -/* interface node */ -static int knet_cmd_mtu(struct knet_vty *vty); -static int knet_cmd_no_mtu(struct knet_vty *vty); -static int knet_cmd_ip(struct knet_vty *vty); -static int knet_cmd_no_ip(struct knet_vty *vty); -static int knet_cmd_peer(struct knet_vty *vty); -static int knet_cmd_no_peer(struct knet_vty *vty); -static int knet_cmd_start(struct knet_vty *vty); -static int knet_cmd_stop(struct knet_vty *vty); -static int knet_cmd_crypto(struct knet_vty *vty); -static int knet_cmd_pmtufreq(struct knet_vty *vty); -static int knet_cmd_no_pmtufreq(struct knet_vty *vty); - - -/* peer node */ -static int knet_cmd_link(struct knet_vty *vty); -static int knet_cmd_no_link(struct knet_vty *vty); -static int knet_cmd_switch_policy(struct knet_vty *vty); - -/* link node */ -static int knet_cmd_link_pri(struct knet_vty *vty); -static int knet_cmd_link_pong(struct knet_vty *vty); -static int knet_cmd_link_timer(struct knet_vty *vty); - -/* vty node */ -static int knet_cmd_vty(struct knet_vty *vty); -static int knet_cmd_vty_timeout(struct knet_vty *vty); - -/* root node description */ -vty_node_cmds_t root_cmds[] = { - { "configure", "enter configuration mode", NULL, knet_cmd_config }, - { "exit", "exit from CLI", NULL, knet_cmd_logout }, - { "help", "display basic help", NULL, knet_cmd_help }, - { "logout", "exit from CLI", NULL, knet_cmd_logout }, - { "status", "display current network status", NULL, knet_cmd_status }, - { "vty", "enter vty configuration mode", NULL, knet_cmd_vty }, - { "who", "display users connected to CLI", NULL, knet_cmd_who }, - { NULL, NULL, NULL, NULL }, -}; - -/* config node description */ -vty_param_t no_int_params[] = { - { CMDS_PARAM_KNET }, - { CMDS_PARAM_NOMORE }, -}; - -vty_node_cmds_t no_config_cmds[] = { - { "interface", "destroy kronosnet interface", no_int_params, knet_cmd_no_interface }, - { NULL, NULL, NULL, NULL }, -}; - -vty_param_t int_params[] = { - { CMDS_PARAM_KNET }, - { CMDS_PARAM_NODEID }, - { CMDS_PARAM_IP_PORT }, - { CMDS_PARAM_NOMORE }, -}; - -vty_node_cmds_t config_cmds[] = { - { "exit", "exit configuration mode", NULL, knet_cmd_exit_node }, - { "interface", "configure kronosnet interface", int_params, knet_cmd_interface }, - { "show", "show running config", NULL, knet_cmd_show_conf }, - { "help", "display basic help", NULL, knet_cmd_help }, - { "logout", "exit from CLI", NULL, knet_cmd_logout }, - { "no", "revert command", NULL, NULL }, - { "status", "display current network status", NULL, knet_cmd_status }, - { "vty", "enter vty configuration mode", NULL, knet_cmd_vty }, - { "who", "display users connected to CLI", NULL, knet_cmd_who }, - { "write", "write current config to file", NULL, knet_cmd_write_conf }, - { NULL, NULL, NULL, NULL }, -}; - -/* interface node description */ - -vty_param_t ip_params[] = { - { CMDS_PARAM_IP }, - { CMDS_PARAM_IP_PREFIX }, - { CMDS_PARAM_NOMORE }, -}; - -vty_param_t peer_params[] = { - { CMDS_PARAM_NAME }, - { CMDS_PARAM_NODEID }, - { CMDS_PARAM_NOMORE }, -}; - -vty_node_cmds_t no_interface_cmds[] = { - { "ip", "remove ip address", ip_params, knet_cmd_no_ip }, - { "mtu", "revert to default MTU", NULL, knet_cmd_no_mtu }, - { "pmtudfreq", "revert to default PMTUd frequency (default: 5)", NULL, knet_cmd_no_pmtufreq }, - { "peer", "remove peer from this interface", peer_params, knet_cmd_no_peer }, - { NULL, NULL, NULL, NULL }, -}; - -vty_param_t mtu_params[] = { - { CMDS_PARAM_MTU }, - { CMDS_PARAM_NOMORE }, -}; - -vty_param_t pmtu_params[] = { - { CMDS_PARAM_PMTU_FREQ }, - { CMDS_PARAM_NOMORE }, -}; - -vty_param_t crypto_params[] = { - { CMDS_PARAM_CRYPTO_MODEL }, - { CMDS_PARAM_CRYPTO_TYPE }, - { CMDS_PARAM_HASH_TYPE }, - { CMDS_PARAM_NOMORE }, -}; - -vty_node_cmds_t interface_cmds[] = { - { "crypto", "enable crypto/hmac", crypto_params, knet_cmd_crypto }, - { "exit", "exit configuration mode", NULL, knet_cmd_exit_node }, - { "help", "display basic help", NULL, knet_cmd_help }, - { "ip", "add ip address", ip_params, knet_cmd_ip }, - { "logout", "exit from CLI", NULL, knet_cmd_logout }, - { "mtu", "set mtu (default: auto)", mtu_params, knet_cmd_mtu }, - { "pmtudfreq", "PMTUd frequency (default: 5)", pmtu_params, knet_cmd_pmtufreq }, - { "no", "revert command", NULL, NULL }, - { "peer", "add peer endpoint", peer_params, knet_cmd_peer }, - { "show", "show running config", NULL, knet_cmd_show_conf }, - { "start", "start forwarding engine", NULL, knet_cmd_start }, - { "status", "display current network status", NULL, knet_cmd_status }, - { "stop", "stop forwarding engine", NULL, knet_cmd_stop }, - { "who", "display users connected to CLI", NULL, knet_cmd_who }, - { "write", "write current config to file", NULL, knet_cmd_write_conf }, - { NULL, NULL, NULL, NULL }, -}; - -/* peer node description */ - -vty_param_t nolink_params[] = { - { CMDS_PARAM_LINK_ID }, - { CMDS_PARAM_NOMORE }, -}; - -vty_param_t link_params[] = { - { CMDS_PARAM_LINK_ID }, - { CMDS_PARAM_IP }, - { CMDS_PARAM_IP }, - { CMDS_PARAM_LINK_TRANSP }, - { CMDS_PARAM_NOMORE }, -}; - -vty_param_t switch_params[] = { - { CMDS_PARAM_POLICY }, - { CMDS_PARAM_NOMORE }, -}; - -vty_node_cmds_t no_peer_cmds[] = { - { "link", "remove peer endpoint", nolink_params, knet_cmd_no_link}, - { NULL, NULL, NULL, NULL }, -}; - -vty_node_cmds_t peer_cmds[] = { - { "exit", "exit configuration mode", NULL, knet_cmd_exit_node }, - { "help", "display basic help", NULL, knet_cmd_help }, - { "link", "add peer endpoint", link_params, knet_cmd_link }, - { "logout", "exit from CLI", NULL, knet_cmd_logout }, - { "no", "revert command", NULL, NULL }, - { "show", "show running config", NULL, knet_cmd_show_conf }, - { "status", "display current network status", NULL, knet_cmd_status }, - { "switch-policy", "configure switching policy engine", switch_params, knet_cmd_switch_policy }, - { "who", "display users connected to CLI", NULL, knet_cmd_who }, - { "write", "write current config to file", NULL, knet_cmd_write_conf }, - { NULL, NULL, NULL, NULL }, -}; - -/* link node description */ - -vty_param_t link_pri_params[] = { - { CMDS_PARAM_LINK_PRI }, - { CMDS_PARAM_NOMORE }, -}; - -vty_param_t link_timer_params[] = { - { CMDS_PARAM_LINK_KEEPAL }, - { CMDS_PARAM_LINK_HOLDTI }, - { CMDS_PARAM_NOMORE }, -}; - -vty_param_t pong_count_params[] = { - { CMDS_PARAM_LINK_PONG }, - { CMDS_PARAM_NOMORE }, -}; - -vty_node_cmds_t link_cmds[] = { - { "exit", "exit configuration mode", NULL, knet_cmd_exit_node }, - { "help", "display basic help", NULL, knet_cmd_help }, - { "logout", "exit from CLI", NULL, knet_cmd_logout }, - { "no", "revert command", NULL, NULL }, - { "pong_count", "set number of pongs to be received before a link is considered alive", pong_count_params, knet_cmd_link_pong }, - { "priority", "set priority of this link for passive switching", link_pri_params, knet_cmd_link_pri }, - { "show", "show running config", NULL, knet_cmd_show_conf }, - { "status", "display current network status", NULL, knet_cmd_status }, - { "timers", "set link keepalive and holdtime", link_timer_params, knet_cmd_link_timer }, - { "who", "display users connected to CLI", NULL, knet_cmd_who }, - { "write", "write current config to file", NULL, knet_cmd_write_conf }, - { NULL, NULL, NULL, NULL }, -}; - -vty_param_t vty_timeout_params[] = { - { CMDS_PARAM_VTY_TIMEOUT }, - { CMDS_PARAM_NOMORE }, -}; - -vty_node_cmds_t vty_cmds[] = { - { "exit", "exit configuration mode", NULL, knet_cmd_exit_node }, - { "help", "display basic help", NULL, knet_cmd_help }, - { "logout", "exit from CLI", NULL, knet_cmd_logout }, - { "show", "show running config", NULL, knet_cmd_show_conf }, - { "status", "display current network status", NULL, knet_cmd_status }, - { "timeout", "set number of seconds before session is automatically closed", vty_timeout_params, knet_cmd_vty_timeout }, - { "who", "display users connected to CLI", NULL, knet_cmd_who }, - { "write", "write current config to file", NULL, knet_cmd_write_conf }, - { NULL, NULL, NULL, NULL }, -}; - -/* nodes */ -vty_nodes_t knet_vty_nodes[] = { - { NODE_ROOT, "knet", root_cmds, NULL }, - { NODE_CONFIG, "config", config_cmds, no_config_cmds }, - { NODE_INTERFACE, "iface", interface_cmds, no_interface_cmds }, - { NODE_PEER, "peer", peer_cmds, no_peer_cmds }, - { NODE_LINK, "link", link_cmds, NULL }, - { NODE_VTY, "vty", vty_cmds, NULL }, - { -1, NULL, NULL }, -}; - -/* command execution */ - -/* vty */ -static int knet_cmd_vty_timeout(struct knet_vty *vty) -{ - int paramlen = 0, paramoffset = 0, timeout; - char *param = NULL; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - timeout = param_to_int(param, paramlen); - - if ((vty->filemode) || (vty->prevnode == NODE_CONFIG)) { - vty->vty_global_conf->idle_timeout = timeout; - } - vty->idle_timeout = timeout; - - return 0; -} - -static int knet_cmd_vty(struct knet_vty *vty) -{ - vty->prevnode = vty->node; - vty->node = NODE_VTY; - return 0; -} - -/* links */ - -static int knet_cmd_link_pong(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - int paramlen = 0, paramoffset = 0; - char *param = NULL; - uint8_t pong_count; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - pong_count = param_to_int(param, paramlen); - - knet_link_set_pong_count(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, pong_count); - - return 0; -} - -static int knet_cmd_link_timer(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - int paramlen = 0, paramoffset = 0; - char *param = NULL; - time_t keepalive, holdtime; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - keepalive = param_to_int(param, paramlen); - - get_param(vty, 2, ¶m, ¶mlen, ¶moffset); - holdtime = param_to_int(param, paramlen); - - knet_link_set_ping_timers(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, keepalive, holdtime, 2048); - - return 0; -} - -static int knet_cmd_link_pri(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - int paramlen = 0, paramoffset = 0; - char *param = NULL; - uint8_t priority; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - priority = param_to_int(param, paramlen); - - if (knet_link_set_priority(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, priority)) { - knet_vty_write(vty, "Error: unable to update link priority%s", telnet_newline); - return -1; - } - - return 0; -} - -static int knet_cmd_no_link(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - struct knet_link_status status; - int paramlen = 0, paramoffset = 0; - char *param = NULL; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - vty->link_id = param_to_int(param, paramlen); - - knet_link_get_status(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, &status); - - if (status.enabled) { - if (knet_link_set_enable(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, 0)) { - knet_vty_write(vty, "Error: unable to update switching cache%s", telnet_newline); - return -1; - } - knet_link_clear_config(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id); - } - - return 0; -} - -static int knet_cmd_link(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - struct knet_link_status status; - int paramlen = 0, paramoffset = 0, err = 0; - char *param = NULL; - char src_ipaddr[KNET_MAX_HOST_LEN], src_port[KNET_MAX_PORT_LEN], dst_ipaddr[KNET_MAX_HOST_LEN], dst_port[KNET_MAX_PORT_LEN]; - struct sockaddr_storage src_addr; - struct sockaddr_storage dst_addr; - struct sockaddr_storage *dst = NULL; - char transport[10]; - uint8_t transport_id; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - vty->link_id = param_to_int(param, paramlen); - - get_param(vty, 2, ¶m, ¶mlen, ¶moffset); - param_to_str(src_ipaddr, KNET_MAX_HOST_LEN, param, paramlen); - - memset(src_port, 0, sizeof(src_port)); - snprintf(src_port, KNET_MAX_PORT_LEN, "%d", knet_iface->cfg_ring.base_port + vty->host_id); - - get_param(vty, 3, ¶m, ¶mlen, ¶moffset); - param_to_str(dst_ipaddr, KNET_MAX_HOST_LEN, param, paramlen); - - memset(dst_port, 0, sizeof(dst_port)); - snprintf(dst_port, KNET_MAX_PORT_LEN, "%d", knet_iface->cfg_ring.base_port + knet_iface->cfg_eth.node_id); - - get_param(vty, 4, ¶m, ¶mlen, ¶moffset); - param_to_str(transport, sizeof(transport), param, paramlen); - - transport_id = knet_handle_get_transport_id_by_name(knet_iface->cfg_ring.knet_h, transport); - - knet_link_get_status(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, &status); - if (!status.enabled) { - if (knet_strtoaddr(src_ipaddr, src_port, &src_addr, sizeof(struct sockaddr_storage)) != 0) { - knet_vty_write(vty, "Error: unable to convert source ip addr to sockaddr!%s", telnet_newline); - err = -1; - goto out_clean; - } - - if (!strncmp(dst_ipaddr, "dynamic", 7)) { - dst = NULL; - } else { - if (knet_strtoaddr(dst_ipaddr, dst_port, &dst_addr, sizeof(struct sockaddr_storage)) != 0) { - knet_vty_write(vty, "Error: unable to convert destination ip addr to sockaddr!%s", telnet_newline); - err = -1; - goto out_clean; - } - dst = &dst_addr; - } - - knet_link_set_config(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, transport_id, &src_addr, dst); - - knet_link_set_ping_timers(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, 1000, 5000, 2048); - - knet_link_set_enable(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, 1); - } - - vty->node = NODE_LINK; - -out_clean: - return err; -} - -static int knet_cmd_switch_policy(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - int paramlen = 0, paramoffset = 0, err = 0; - char *param = NULL; - char policystr[16]; - int policy = -1; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - param_to_str(policystr, sizeof(policystr), param, paramlen); - - if (!strncmp("passive", policystr, 7)) - policy = KNET_LINK_POLICY_PASSIVE; - if (!strncmp("active", policystr, 6)) - policy = KNET_LINK_POLICY_ACTIVE; - if (!strncmp("round-robin", policystr, 11)) - policy = KNET_LINK_POLICY_RR; - - if (policy < 0) { - knet_vty_write(vty, "Error: unknown switching policy method%s", telnet_newline); - return -1; - } - - err = knet_host_set_policy(knet_iface->cfg_ring.knet_h, vty->host_id, policy); - if (err) - knet_vty_write(vty, "Error: unable to set switching policy to %s%s", policystr, telnet_newline); - - return err; -} - -/* - * -1 on internal error - * 0 host does not exist - * 1 host exists - */ -static int knet_find_host(struct knet_vty *vty, const char *nodename, const knet_node_id_t requested_node_id) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - int have_nodeid, have_name; - knet_node_id_t node_id; - char name[KNET_MAX_HOST_LEN]; - - have_nodeid = knet_host_get_id_by_host_name(knet_iface->cfg_ring.knet_h, nodename, &node_id); - have_name = knet_host_get_name_by_host_id(knet_iface->cfg_ring.knet_h, requested_node_id, name); - - /* - * host does not exist without a name - */ - if (have_name < 0) { - return 0; - } - - /* - * internal error.. get out - */ - if (have_nodeid < 0) { - knet_vty_write(vty, "Error: unable to query libknet for nodeid info%s", telnet_newline); - return -1; - } - - if ((!have_name) && (!have_nodeid)) { - if (!strcmp(name, nodename) && (node_id == requested_node_id)) - return 1; - } - - knet_vty_write(vty, "Error: requested nodename or id already exists in libknet%s", telnet_newline); - - return -1; -} - -static int knet_cmd_no_peer(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - int paramlen = 0, paramoffset = 0, requested_node_id = 0, err = 0; - char *param = NULL; - char nodename[KNET_MAX_HOST_LEN]; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - param_to_str(nodename, sizeof(nodename), param, paramlen); - - get_param(vty, 2, ¶m, ¶mlen, ¶moffset); - requested_node_id = param_to_int(param, paramlen); - - if (requested_node_id == knet_iface->cfg_eth.node_id) { - knet_vty_write(vty, "Error: remote peer id cannot be the same as local id%s", telnet_newline); - return -1; - } - - err = knet_find_host(vty, nodename, requested_node_id); - if (err < 0) - goto out_clean; - - if (err != 1) { - knet_vty_write(vty, "Error: peer not found in list%s", telnet_newline); - goto out_clean; - } - - err = knet_host_remove(knet_iface->cfg_ring.knet_h, requested_node_id); - if (err < 0) { - knet_vty_write(vty, "Error: unable to remove peer from current config%s", telnet_newline); - goto out_clean; - } - -out_clean: - return err; -} - -static int knet_cmd_peer(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - int paramlen = 0, paramoffset = 0, requested_node_id = 0, err = 0, host = 0; - char *param = NULL; - char nodename[KNET_MAX_HOST_LEN]; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - param_to_str(nodename, sizeof(nodename), param, paramlen); - - get_param(vty, 2, ¶m, ¶mlen, ¶moffset); - requested_node_id = param_to_int(param, paramlen); - - if (requested_node_id == knet_iface->cfg_eth.node_id) { - knet_vty_write(vty, "Error: remote peer id cannot be the same as local id%s", telnet_newline); - return -1; - } - - err = knet_find_host(vty, nodename, requested_node_id); - if (err < 0) - goto out_clean; - - if (err == 0) { - err = knet_host_add(knet_iface->cfg_ring.knet_h, requested_node_id); - if (err < 0) { - knet_vty_write(vty, "Error: unable to allocate memory for host struct!%s", telnet_newline); - goto out_clean; - } - host = 1; - knet_host_set_name(knet_iface->cfg_ring.knet_h, requested_node_id, nodename); - knet_host_set_policy(knet_iface->cfg_ring.knet_h, requested_node_id, KNET_LINK_POLICY_PASSIVE); - } - - vty->host_id = requested_node_id; - vty->node = NODE_PEER; - -out_clean: - if (err < 0) { - if (host) - knet_host_remove(knet_iface->cfg_ring.knet_h, requested_node_id); - } - - return err; -} - -static int knet_cmd_no_ip(struct knet_vty *vty) -{ - int paramlen = 0, paramoffset = 0; - char *param = NULL; - char ipaddr[KNET_MAX_HOST_LEN], prefix[4]; - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - char *error_string = NULL; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - param_to_str(ipaddr, sizeof(ipaddr), param, paramlen); - - get_param(vty, 2, ¶m, ¶mlen, ¶moffset); - param_to_str(prefix, sizeof(prefix), param, paramlen); - - if (tap_del_ip(knet_iface->cfg_eth.tap, ipaddr, prefix, &error_string) < 0) { - knet_vty_write(vty, "Error: Unable to del ip addr %s/%s on device %s%s", - ipaddr, prefix, tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); - if (error_string) { - knet_vty_write(vty, "(%s)%s", error_string, telnet_newline); - free(error_string); - } - return -1; - } - - return 0; -} - -static int knet_cmd_ip(struct knet_vty *vty) -{ - int paramlen = 0, paramoffset = 0; - char *param = NULL; - char ipaddr[512], prefix[4]; - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - char *error_string = NULL; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - param_to_str(ipaddr, sizeof(ipaddr), param, paramlen); - - get_param(vty, 2, ¶m, ¶mlen, ¶moffset); - param_to_str(prefix, sizeof(prefix), param, paramlen); - - if (tap_add_ip(knet_iface->cfg_eth.tap, ipaddr, prefix, &error_string) < 0) { - knet_vty_write(vty, "Error: Unable to set ip addr %s/%s on device %s%s", - ipaddr, prefix, tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); - if (error_string) { - knet_vty_write(vty, "(%s)%s", error_string, telnet_newline); - free(error_string); - } - return -1; - } - - return 0; -} - -static void knet_cmd_auto_mtu_notify(void *private_data, - unsigned int data_mtu) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)private_data; - - /* - * 48 is the magic number! yes it is.. it's the magic number... - */ - knet_iface->cfg_ring.data_mtu = data_mtu - 48; - - if (!knet_iface->cfg_eth.auto_mtu) { - int mtu = 0; - - mtu = tap_get_mtu(knet_iface->cfg_eth.tap); - if (mtu < 0) { - log_debug("Unable to get current MTU?"); - } else { - if (data_mtu < mtu) { - log_debug("Manually configured MTU (%d) is higher than automatically detected MTU (%d)", - mtu, data_mtu); - } - } - - return; - } - - if (tap_set_mtu(knet_iface->cfg_eth.tap, knet_iface->cfg_ring.data_mtu) < 0) { - log_warn("Error: Unable to set requested mtu %d on device %s via mtu notify", - knet_iface->cfg_ring.data_mtu, tap_get_name(knet_iface->cfg_eth.tap)); - } else { - log_info("Device %s new mtu: %d (via mtu notify)", - tap_get_name(knet_iface->cfg_eth.tap), knet_iface->cfg_ring.data_mtu); - } -} - -static int knet_cmd_no_pmtufreq(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - - if (knet_handle_pmtud_setfreq(knet_iface->cfg_ring.knet_h, 5) < 0) { - knet_vty_write(vty, "Error: Unable to reset PMTUd frequency to 5 seconds on device %s%s", - tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); - return -1; - } - - return 0; -} - -static int knet_cmd_pmtufreq(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - int paramlen = 0, paramoffset = 0, pmtufreq = 5; - char *param = NULL; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - pmtufreq = param_to_int(param, paramlen); - - if (knet_handle_pmtud_setfreq(knet_iface->cfg_ring.knet_h, pmtufreq) < 0) { - knet_vty_write(vty, "Error: Unable to set PMTUd frequency to %d seconds on device %s%s", - pmtufreq, tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); - return -1; - } - - return 0; -} - -static int knet_cmd_no_mtu(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - - /* allow automatic updates of mtu */ - knet_iface->cfg_eth.auto_mtu = 1; - - if (knet_iface->cfg_ring.data_mtu > 0) { - if (tap_set_mtu(knet_iface->cfg_eth.tap, knet_iface->cfg_ring.data_mtu) < 0) { - knet_iface->cfg_eth.auto_mtu = 0; - knet_vty_write(vty, "Error: Unable to set auto detected mtu on device %s%s", - tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); - return -1; - } - } else { - if (tap_reset_mtu(knet_iface->cfg_eth.tap) < 0) { - knet_iface->cfg_eth.auto_mtu = 0; - knet_vty_write(vty, "Error: Unable to reset mtu on device %s%s", - tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); - return -1; - } - } - - return 0; -} - -static int knet_cmd_mtu(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - int paramlen = 0, paramoffset = 0, expected_mtu = 0; - char *param = NULL; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - expected_mtu = param_to_int(param, paramlen); - - /* disable mtu auto updates */ - knet_iface->cfg_eth.auto_mtu = 0; - - if ((knet_iface->cfg_ring.data_mtu) && - (expected_mtu > knet_iface->cfg_ring.data_mtu)) { - knet_vty_write(vty, "WARNING: Manually configured MTU (%d) is higher than automatically detected MTU (%d)%s", - expected_mtu, knet_iface->cfg_ring.data_mtu, telnet_newline); - } - - if (tap_set_mtu(knet_iface->cfg_eth.tap, expected_mtu) < 0) { - knet_iface->cfg_eth.auto_mtu = 1; - knet_vty_write(vty, "Error: Unable to set requested mtu %d on device %s%s", - expected_mtu, tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); - return -1; - } - - return 0; -} - -static int knet_cmd_stop(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - char *error_down = NULL, *error_postdown = NULL; - int err = 0; - - err = tap_set_down(knet_iface->cfg_eth.tap, &error_down, &error_postdown); - if (err < 0) { - knet_vty_write(vty, "Error: Unable to set interface %s down!%s", tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); - } else { - if (knet_iface->cfg_ring.knet_h) - knet_handle_setfwd(knet_iface->cfg_ring.knet_h, 0); - knet_iface->active = 0; - } - if (error_down) { - knet_vty_write(vty, "down script output:%s(%s)%s", telnet_newline, error_down, telnet_newline); - free(error_down); - } - if (error_postdown) { - knet_vty_write(vty, "post-down script output:%s(%s)%s", telnet_newline, error_postdown, telnet_newline); - free(error_postdown); - } - - return err; -} - -static int knet_cmd_crypto(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - int paramlen = 0, paramoffset = 0; - char *param = NULL; - int err = 0; - struct knet_handle_crypto_cfg knet_handle_crypto_cfg_new; - int fd = -1; - char keyfile[PATH_MAX]; - struct stat sb; - - if (knet_iface->active) { - knet_vty_write(vty, "Error: Unable to activate encryption while interface is active%s", telnet_newline); - return -1; - } - - memset(&knet_handle_crypto_cfg_new, 0, sizeof(struct knet_handle_crypto_cfg)); - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - param_to_str(knet_handle_crypto_cfg_new.crypto_model, - sizeof(knet_handle_crypto_cfg_new.crypto_model), param, paramlen); - - get_param(vty, 2, ¶m, ¶mlen, ¶moffset); - param_to_str(knet_handle_crypto_cfg_new.crypto_cipher_type, - sizeof(knet_handle_crypto_cfg_new.crypto_cipher_type), param, paramlen); - - get_param(vty, 3, ¶m, ¶mlen, ¶moffset); - param_to_str(knet_handle_crypto_cfg_new.crypto_hash_type, - sizeof(knet_handle_crypto_cfg_new.crypto_hash_type), param, paramlen); - - if ((!strncmp("none", knet_handle_crypto_cfg_new.crypto_model, 4)) || - ((!strncmp("none", knet_handle_crypto_cfg_new.crypto_cipher_type, 4)) && - ((!strncmp("none", knet_handle_crypto_cfg_new.crypto_hash_type, 4))))) - goto no_key; - - memset(keyfile, 0, PATH_MAX); - snprintf(keyfile, PATH_MAX - 1, DEFAULT_CONFIG_DIR "/cryptokeys.d/%s", tap_get_name(knet_iface->cfg_eth.tap)); - - fd = open(keyfile, O_RDONLY); - if (fd < 0) { - knet_vty_write(vty, "Error: Unable to open security key: %s%s", keyfile, telnet_newline); - err = -1; - return -1; - } - - if (fstat(fd, &sb)) { - knet_vty_write(vty, "Error: Unable to verify security key: %s%s", keyfile, telnet_newline); - goto key_error; - } - - if (!S_ISREG(sb.st_mode)) { - knet_vty_write(vty, "Error: Key %s does not appear to be a regular file%s", - keyfile, telnet_newline); - goto key_error; - } - - knet_handle_crypto_cfg_new.private_key_len = (unsigned int)sb.st_size; - if ((knet_handle_crypto_cfg_new.private_key_len < KNET_MIN_KEY_LEN) || - (knet_handle_crypto_cfg_new.private_key_len > KNET_MAX_KEY_LEN)) { - knet_vty_write(vty, "Error: Key %s is %u long. Must be %u <= key_len <= %u%s", - keyfile, knet_handle_crypto_cfg_new.private_key_len, - KNET_MIN_KEY_LEN, KNET_MAX_KEY_LEN, telnet_newline); - goto key_error; - } - - if (((sb.st_mode & S_IRWXU) != S_IRUSR) || - (sb.st_mode & S_IRWXG) || - (sb.st_mode & S_IRWXO)) { - knet_vty_write(vty, "Error: Key %s does not have the correct permission (must be user read-only)%s", - keyfile, telnet_newline); - goto key_error; - } - - if (read(fd, - &knet_handle_crypto_cfg_new.private_key, - knet_handle_crypto_cfg_new.private_key_len) != knet_handle_crypto_cfg_new.private_key_len) { - knet_vty_write(vty, "Error: Unable to read key %s%s", keyfile, telnet_newline); - goto key_error; - } - - close(fd); - -no_key: - err = knet_handle_crypto(knet_iface->cfg_ring.knet_h, - &knet_handle_crypto_cfg_new); - - if (!err) { - memmove(&knet_iface->knet_handle_crypto_cfg, &knet_handle_crypto_cfg_new, sizeof(struct knet_handle_crypto_cfg)); - } else { - knet_vty_write(vty, "Error: Unable to initialize crypto module%s", telnet_newline); - } - - return err; - -key_error: - close(fd); - return -1; -} - -static int knet_cmd_start(struct knet_vty *vty) -{ - struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; - char *error_preup = NULL, *error_up = NULL; - int err = 0; - - err = tap_set_up(knet_iface->cfg_eth.tap, &error_preup, &error_up); - if (err < 0) { - knet_vty_write(vty, "Error: Unable to set interface %s up!%s", tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); - knet_handle_setfwd(knet_iface->cfg_ring.knet_h, 0); - } else { - knet_handle_setfwd(knet_iface->cfg_ring.knet_h, 1); - knet_iface->active = 1; - } - if (error_preup) { - knet_vty_write(vty, "pre-up script output:%s(%s)%s", telnet_newline, error_preup, telnet_newline); - free(error_preup); - } - if (error_up) { - knet_vty_write(vty, "up script output:%s(%s)%s", telnet_newline, error_up, telnet_newline); - free(error_up); - } - - return err; -} - -static int knet_cmd_no_interface(struct knet_vty *vty) -{ - int err = 0, paramlen = 0, paramoffset = 0; - char *param = NULL; - char device[IFNAMSIZ]; - struct knet_cfg *knet_iface = NULL; - char *ip_list = NULL; - int ip_list_entries = 0, j, i, offset = 0; - char *error_string = NULL; - knet_node_id_t host_ids[KNET_MAX_HOST]; - uint8_t link_ids[KNET_MAX_LINK]; - size_t host_ids_entries = 0, link_ids_entries = 0; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - param_to_str(device, IFNAMSIZ, param, paramlen); - - knet_iface = knet_get_iface(device, 0); - if (!knet_iface) { - knet_vty_write(vty, "Error: Unable to find requested interface%s", telnet_newline); - return -1; - } - - vty->iface = (void *)knet_iface; - - /* - * disable PTMUd notification before shutting down the tap device - */ - knet_handle_enable_pmtud_notify(knet_iface->cfg_ring.knet_h, NULL, NULL); - - tap_get_ips(knet_iface->cfg_eth.tap, &ip_list, &ip_list_entries); - if ((ip_list) && (ip_list_entries > 0)) { - for (i = 1; i <= ip_list_entries; i++) { - tap_del_ip(knet_iface->cfg_eth.tap, - ip_list + offset, - ip_list + offset + strlen(ip_list + offset) + 1, &error_string); - if (error_string) { - free(error_string); - error_string = NULL; - } - offset = offset + strlen(ip_list) + 1; - offset = offset + strlen(ip_list + offset) + 1; - } - free(ip_list); - ip_list = NULL; - ip_list_entries = 0; - } - - knet_host_get_host_list(knet_iface->cfg_ring.knet_h, host_ids, &host_ids_entries); - for (j = 0; j < host_ids_entries; j++) { - knet_link_get_link_list(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids, &link_ids_entries); - for (i = 0; i < link_ids_entries; i++) { - knet_link_set_enable(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], 0); - knet_link_clear_config(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i]); - } - knet_host_remove(knet_iface->cfg_ring.knet_h, host_ids[j]); - } - - knet_cmd_stop(vty); - - if (knet_iface->cfg_ring.knet_h) { - knet_handle_free(knet_iface->cfg_ring.knet_h); - knet_iface->cfg_ring.knet_h = NULL; - } - - if (knet_iface->cfg_eth.tap) - tap_close(knet_iface->cfg_eth.tap); - - if (knet_iface) - knet_destroy_iface(knet_iface); - - return err; -} - -static void sock_notify_fn(void *private_data, int datafd, int8_t chan, uint8_t tx_rx, int error, int errorno) -{ - struct knet_vty *vty = (struct knet_vty *)private_data; - - knet_vty_write(vty, "Error: received sock notify, datafd: %d channel: %d direction: %u error: %d errno: %d (%s)%s", - datafd, chan, tx_rx, error, errorno, strerror(errorno), telnet_newline); -} - -static int knet_cmd_interface(struct knet_vty *vty) -{ - int err = 0, paramlen = 0, paramoffset = 0, found = 0, requested_id, tapfd; - uint16_t baseport; - uint8_t *bport = (uint8_t *)&baseport; - char *param = NULL; - char device[IFNAMSIZ]; - char mac[18]; - struct knet_cfg *knet_iface = NULL; - int8_t channel = 0; - - get_param(vty, 1, ¶m, ¶mlen, ¶moffset); - param_to_str(device, IFNAMSIZ, param, paramlen); - - get_param(vty, 2, ¶m, ¶mlen, ¶moffset); - requested_id = param_to_int(param, paramlen); - - get_param(vty, 3, ¶m, ¶mlen, ¶moffset); - baseport = param_to_int(param, paramlen); - - knet_iface = knet_get_iface(device, 1); - if (!knet_iface) { - knet_vty_write(vty, "Error: Unable to allocate memory for config structures%s", - telnet_newline); - return -1; - } - - if (knet_iface->cfg_eth.tap) { - found = 1; - goto tap_found; - } - - if (!knet_iface->cfg_eth.tap) - knet_iface->cfg_eth.tap = tap_open(device, IFNAMSIZ, DEFAULT_CONFIG_DIR); - - if ((!knet_iface->cfg_eth.tap) && (errno == EBUSY)) { - knet_vty_write(vty, "Error: interface %s seems to exist in the system%s", - device, telnet_newline); - err = -1; - goto out_clean; - } - - if (!knet_iface->cfg_eth.tap) { - knet_vty_write(vty, "Error: Unable to create %s system tap device%s", - device, telnet_newline); - err = -1; - goto out_clean; - } -tap_found: - - if (knet_iface->cfg_ring.knet_h) - goto knet_found; - - knet_iface->cfg_ring.base_port = baseport; - - tapfd = tap_get_fd(knet_iface->cfg_eth.tap); - - knet_iface->cfg_ring.knet_h = knet_handle_new(requested_id, vty->logfd, vty->loglevel); - if (!knet_iface->cfg_ring.knet_h) { - knet_vty_write(vty, "Error: Unable to create ring handle for device %s%s", - device, telnet_newline); - err = -1; - goto out_clean; - } - - if (knet_handle_enable_sock_notify(knet_iface->cfg_ring.knet_h, &vty, sock_notify_fn)) { - knet_vty_write(vty, "Error: Unable to add sock notify callback to to knet_handle %s%s", - strerror(errno), telnet_newline); - err = -1; - goto out_clean; - } - - if (knet_handle_add_datafd(knet_iface->cfg_ring.knet_h, &tapfd, &channel) < 0) { - knet_vty_write(vty, "Error: Unable to add tapfd to knet_handle %s%s", - strerror(errno), telnet_newline); - err = -1; - goto out_clean; - } - - knet_handle_enable_filter(knet_iface->cfg_ring.knet_h, NULL, ether_host_filter_fn); - - if (knet_handle_enable_pmtud_notify(knet_iface->cfg_ring.knet_h, - knet_iface, - knet_cmd_auto_mtu_notify) < 0) { - knet_vty_write(vty, "Error: Unable to configure auto mtu notification for device %s%s", - device, telnet_newline); - err = -1; - goto out_clean; - } - knet_iface->cfg_eth.auto_mtu = 1; - - /* - * make this configurable - */ - knet_handle_pmtud_setfreq(knet_iface->cfg_ring.knet_h, 5); - -knet_found: - if (found) { - if (requested_id == knet_iface->cfg_eth.node_id) - goto out_found; - - knet_vty_write(vty, "Error: no interface %s with nodeid %d found%s", - device, requested_id, telnet_newline); - goto out_clean; - - } else { - knet_iface->cfg_eth.node_id = requested_id; - } - - baseport = htons(baseport); - memset(&mac, 0, sizeof(mac)); - snprintf(mac, sizeof(mac) - 1, "54:54:%x:%x:0:%x", bport[0], bport[1], knet_iface->cfg_eth.node_id); - if (tap_set_mac(knet_iface->cfg_eth.tap, mac) < 0) { - knet_vty_write(vty, "Error: Unable to set mac address %s on device %s%s", - mac, device, telnet_newline); - err = -1; - goto out_clean; - } - -out_found: - - vty->node = NODE_INTERFACE; - vty->iface = (void *)knet_iface; - -out_clean: - if (err) { - if (knet_iface->cfg_ring.knet_h) - knet_handle_free(knet_iface->cfg_ring.knet_h); - - if (knet_iface->cfg_eth.tap) - tap_close(knet_iface->cfg_eth.tap); - - knet_destroy_iface(knet_iface); - } - return err; -} - -static int knet_cmd_exit_node(struct knet_vty *vty) -{ - knet_vty_exit_node(vty); - return 0; -} - -static int knet_cmd_status(struct knet_vty *vty) -{ - int i, j; - struct knet_cfg *knet_iface = knet_cfg_head.knet_cfg; - struct knet_link_status status; - const char *nl = telnet_newline; - struct timespec now; - char nodename[KNET_MAX_HOST_LEN]; - knet_node_id_t host_ids[KNET_MAX_HOST]; - uint8_t link_ids[KNET_MAX_LINK]; - size_t host_ids_entries = 0, link_ids_entries = 0; - uint8_t policy; - - clock_gettime(CLOCK_MONOTONIC, &now); - - knet_vty_write(vty, "Current knet status%s", nl); - knet_vty_write(vty, "-------------------%s", nl); - - while (knet_iface != NULL) { - knet_vty_write(vty, "interface %s (active: %d)%s", tap_get_name(knet_iface->cfg_eth.tap), knet_iface->active, nl); - - knet_host_get_host_list(knet_iface->cfg_ring.knet_h, host_ids, &host_ids_entries); - - for (j = 0; j < host_ids_entries; j++) { - knet_host_get_name_by_host_id(knet_iface->cfg_ring.knet_h, host_ids[j], nodename); - knet_vty_write(vty, " peer %s ", nodename); - - knet_host_get_policy(knet_iface->cfg_ring.knet_h, host_ids[j], &policy); - switch (policy) { - case KNET_LINK_POLICY_PASSIVE: - knet_vty_write(vty, "(passive)%s", nl); - break; - case KNET_LINK_POLICY_ACTIVE: - knet_vty_write(vty, "(active)%s", nl); - break; - case KNET_LINK_POLICY_RR: - knet_vty_write(vty, "(round-robin)%s", nl); - break; - } - - knet_link_get_link_list(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids, &link_ids_entries); - for (i = 0; i < link_ids_entries; i++) { - uint8_t dynamic, transport; - const char *transport_name; - struct sockaddr_storage src_addr; - struct sockaddr_storage dst_addr; - - if (!knet_link_get_config(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &transport, &src_addr, &dst_addr, &dynamic)) { - transport_name = knet_handle_get_transport_name_by_id(knet_iface->cfg_ring.knet_h, transport); - knet_link_get_status(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &status); - if (status.enabled == 1) { - if (dynamic) { - knet_vty_write(vty, " link %s dynamic (%s/connected: %d)%s", status.src_ipaddr, transport_name, status.connected, nl); - } else { - knet_vty_write(vty, " link %s %s (%s/connected: %d)%s", status.src_ipaddr, status.dst_ipaddr, transport_name, status.connected, nl); - } - if (status.connected) { - knet_vty_write(vty, " average latency: %llu us%s", status.latency, nl); - if ((dynamic) && (status.dynconnected)) { - knet_vty_write(vty, " source ip: %s%s", status.dst_ipaddr, nl); - } - } else { - knet_vty_write(vty, " last heard: "); - if (status.pong_last.tv_sec) { - knet_vty_write(vty, "%lu s ago%s", - (long unsigned int)now.tv_sec - status.pong_last.tv_sec, nl); - } else { - knet_vty_write(vty, "never%s", nl); - } - } - } - } - } - } - - knet_iface = knet_iface->next; - } - - return 0; -} - -static int knet_cmd_print_conf(struct knet_vty *vty) -{ - int i, j; - struct knet_cfg *knet_iface = knet_cfg_head.knet_cfg; - struct knet_link_status status; - const char *nl = telnet_newline; - char *ip_list = NULL; - int ip_list_entries = 0; - knet_node_id_t host_ids[KNET_MAX_HOST]; - uint8_t link_ids[KNET_MAX_LINK]; - size_t host_ids_entries = 0, link_ids_entries = 0; - char nodename[KNET_MAX_HOST_LEN]; - uint8_t policy; - unsigned int pmtudfreq = 0; - - if (vty->filemode) - nl = file_newline; - - knet_vty_write(vty, "configure%s", nl); - - knet_vty_write(vty, " vty%s", nl); - knet_vty_write(vty, " timeout %d%s", vty->idle_timeout, nl); - knet_vty_write(vty, " exit%s", nl); - - while (knet_iface != NULL) { - knet_vty_write(vty, " interface %s %u %u%s", tap_get_name(knet_iface->cfg_eth.tap), - knet_iface->cfg_eth.node_id, - knet_iface->cfg_ring.base_port, nl); - - if (!knet_iface->cfg_eth.auto_mtu) - knet_vty_write(vty, " mtu %d%s", tap_get_mtu(knet_iface->cfg_eth.tap), nl); - - knet_handle_pmtud_getfreq(knet_iface->cfg_ring.knet_h, &pmtudfreq); - if ((pmtudfreq > 0) && (pmtudfreq != 5)) - knet_vty_write(vty, " pmtudfreq %d%s", pmtudfreq, nl); - - tap_get_ips(knet_iface->cfg_eth.tap, &ip_list, &ip_list_entries); - if ((ip_list) && (ip_list_entries > 0)) { - char *ipaddr = NULL, *prefix = NULL, *next = ip_list; - for (i = 1; i <= ip_list_entries; i++) { - ipaddr = next; - prefix = ipaddr + strlen(ipaddr) + 1; - next = prefix + strlen(prefix) + 1; - knet_vty_write(vty, " ip %s %s%s", ipaddr, prefix, nl); - } - free(ip_list); - ip_list = NULL; - ip_list_entries = 0; - } - - knet_vty_write(vty, " crypto %s %s %s%s", - knet_iface->knet_handle_crypto_cfg.crypto_model, - knet_iface->knet_handle_crypto_cfg.crypto_cipher_type, - knet_iface->knet_handle_crypto_cfg.crypto_hash_type, nl); - - knet_host_get_host_list(knet_iface->cfg_ring.knet_h, host_ids, &host_ids_entries); - for (j = 0; j < host_ids_entries; j++) { - knet_host_get_name_by_host_id(knet_iface->cfg_ring.knet_h, host_ids[j], nodename); - knet_vty_write(vty, " peer %s %u%s", nodename, host_ids[j], nl); - - knet_host_get_policy(knet_iface->cfg_ring.knet_h, host_ids[j], &policy); - switch (policy) { - case KNET_LINK_POLICY_PASSIVE: - knet_vty_write(vty, " switch-policy passive%s", nl); - break; - case KNET_LINK_POLICY_ACTIVE: - knet_vty_write(vty, " switch-policy active%s", nl); - break; - case KNET_LINK_POLICY_RR: - knet_vty_write(vty, " switch-policy round-robin%s", nl); - break; - } - - knet_link_get_link_list(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids, &link_ids_entries); - for (i = 0; i < link_ids_entries; i++) { - uint8_t dynamic, transport; - const char *transport_name; - struct sockaddr_storage src_addr; - struct sockaddr_storage dst_addr; - - if (!knet_link_get_config(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &transport, &src_addr, &dst_addr, &dynamic)) { - transport_name = knet_handle_get_transport_name_by_id(knet_iface->cfg_ring.knet_h, transport); - knet_link_get_status(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &status); - if (status.enabled == 1) { - uint8_t priority, pong_count; - unsigned int precision; - time_t interval, timeout; - - if (dynamic) { - knet_vty_write(vty, " link %d %s dynamic %s%s", link_ids[i], status.src_ipaddr, transport_name, nl); - } else { - knet_vty_write(vty, " link %d %s %s %s%s", link_ids[i], status.src_ipaddr, status.dst_ipaddr, transport_name, nl); - } - knet_link_get_pong_count(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &pong_count); - knet_vty_write(vty, " pong_count %u%s", pong_count, nl); - knet_link_get_ping_timers(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &interval, &timeout, &precision); - knet_vty_write(vty, " timers %llu %llu%s", (unsigned long long)interval, (unsigned long long)timeout, nl); - knet_link_get_priority(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &priority); - knet_vty_write(vty, " priority %u%s", priority, nl); - /* print link properties */ - knet_vty_write(vty, " exit%s", nl); - } - } - } - knet_vty_write(vty, " exit%s", nl); - } - - if (knet_iface->active) - knet_vty_write(vty, " start%s", nl); - - knet_vty_write(vty, " exit%s", nl); - knet_iface = knet_iface->next; - } - - knet_vty_write(vty, " exit%sexit%s", nl, nl); - - return 0; -} - -static int knet_cmd_show_conf(struct knet_vty *vty) -{ - return knet_cmd_print_conf(vty); -} - -static int knet_cmd_write_conf(struct knet_vty *vty) -{ - int fd = 1, vty_sock, err = 0, backup = 1; - char tempfile[PATH_MAX]; - - memset(tempfile, 0, sizeof(tempfile)); - - snprintf(tempfile, sizeof(tempfile), "%s.sav", knet_cfg_head.conffile); - err = rename(knet_cfg_head.conffile, tempfile); - if ((err < 0) && (errno != ENOENT)) { - knet_vty_write(vty, "Unable to create backup config file %s %s", tempfile, telnet_newline); - return -1; - } - if ((err < 0) && (errno == ENOENT)) - backup = 0; - - fd = open(knet_cfg_head.conffile, - O_RDWR | O_CREAT | O_EXCL | O_TRUNC, - S_IRUSR | S_IWUSR); - if (fd < 0) { - knet_vty_write(vty, "Error unable to open file%s", telnet_newline); - return -1; - } - - vty_sock = vty->vty_sock; - vty->vty_sock = fd; - vty->filemode = 1; - knet_cmd_print_conf(vty); - vty->vty_sock = vty_sock; - vty->filemode = 0; - - close(fd); - - knet_vty_write(vty, "Configuration saved to %s%s", knet_cfg_head.conffile, telnet_newline); - if (backup) - knet_vty_write(vty, "Old configuration file has been stored in %s%s", - tempfile, telnet_newline); - - return err; -} - -static int knet_cmd_config(struct knet_vty *vty) -{ - int err = 0; - - if (!vty->user_can_enable) { - knet_vty_write(vty, "Error: user %s does not have enough privileges to perform config operations%s", vty->username, telnet_newline); - return -1; - } - - pthread_mutex_lock(&knet_vty_mutex); - if (knet_vty_config >= 0) { - knet_vty_write(vty, "Error: configuration is currently locked by user %s on vty(%d). Try again later%s", vty->username, knet_vty_config, telnet_newline); - err = -1; - goto out_clean; - } - vty->node = NODE_CONFIG; - knet_vty_config = vty->conn_num; -out_clean: - pthread_mutex_unlock(&knet_vty_mutex); - return err; -} - -static int knet_cmd_logout(struct knet_vty *vty) -{ - vty->got_epipe = 1; - return 0; -} - -static int knet_cmd_who(struct knet_vty *vty) -{ - int conn_index; - - pthread_mutex_lock(&knet_vty_mutex); - - for(conn_index = 0; conn_index < KNET_VTY_TOTAL_MAX_CONN; conn_index++) { - if (knet_vtys[conn_index].active) { - knet_vty_write(vty, "User %s connected on vty(%d) from %s%s", - knet_vtys[conn_index].username, - knet_vtys[conn_index].conn_num, - knet_vtys[conn_index].ip, - telnet_newline); - } - } - - pthread_mutex_unlock(&knet_vty_mutex); - - return 0; -} - -static int knet_cmd_help(struct knet_vty *vty) -{ - knet_vty_write(vty, PACKAGE "d VTY provides advanced help feature.%s%s" - "When you need help, anytime at the command line please press '?'.%s%s" - "If nothing matches, the help list will be empty and you must backup%s" - " until entering a '?' shows the available options.%s", - telnet_newline, telnet_newline, telnet_newline, telnet_newline, - telnet_newline, telnet_newline); - return 0; -} - -/* exported API to vty_cli.c */ - -int knet_vty_execute_cmd(struct knet_vty *vty) -{ - const vty_node_cmds_t *cmds = NULL; - char *cmd = NULL; - int cmdlen = 0; - int cmdoffset = 0; - - if (knet_vty_is_line_empty(vty)) - return 0; - - cmds = get_cmds(vty, &cmd, &cmdlen, &cmdoffset); - - /* this will eventually disappear. keep it as safeguard for now */ - if (cmds == NULL) { - knet_vty_write(vty, "No commands associated to this node%s", telnet_newline); - return 0; - } - - return match_command(vty, cmds, cmd, cmdlen, cmdoffset, KNET_VTY_MATCH_EXEC); -} - -void knet_close_down(void) -{ - struct knet_vty *vty = &knet_vtys[0]; - int err, loop = 0; - - vty->node = NODE_CONFIG; - vty->vty_sock = 1; - vty->user_can_enable = 1; - vty->filemode = 1; - vty->got_epipe = 0; - - while ((knet_cfg_head.knet_cfg) && (loop < 10)) { - memset(vty->line, 0, sizeof(vty->line)); - snprintf(vty->line, sizeof(vty->line) - 1, "no interface %s", tap_get_name(knet_cfg_head.knet_cfg->cfg_eth.tap)); - vty->line_idx = strlen(vty->line); - err = knet_vty_execute_cmd(vty); - if (err != 0) { - log_error("error shutting down: %s", vty->line); - break; - } - loop++; - } -} - -int knet_read_conf(void) -{ - int err = 0, len = 0, line = 0; - struct knet_vty *vty = &knet_vtys[0]; - FILE *file = NULL; - - file = fopen(knet_cfg_head.conffile, "r"); - - if ((file == NULL) && (errno != ENOENT)) { - log_error("Unable to open config file for reading %s", knet_cfg_head.conffile); - return -1; - } - - if ((file == NULL) && (errno == ENOENT)) { - log_info("Configuration file %s not found, starting with default empty config", knet_cfg_head.conffile); - return 0; - } - - vty->vty_sock = 1; - vty->user_can_enable = 1; - vty->filemode = 1; - - while(fgets(vty->line, sizeof(vty->line), file) != NULL) { - line++; - len = strlen(vty->line) - 1; - memset(&vty->line[len], 0, 1); - vty->line_idx = len; - err = knet_vty_execute_cmd(vty); - if (err != 0) { - log_error("line[%d]: %s", line, vty->line); - break; - } - } - - fclose(file); - - memset(vty, 0, sizeof(*vty)); - - return err; -} - -void knet_vty_help(struct knet_vty *vty) -{ - int idx = 0; - const vty_node_cmds_t *cmds = NULL; - char *cmd = NULL; - int cmdlen = 0; - int cmdoffset = 0; - - cmds = get_cmds(vty, &cmd, &cmdlen, &cmdoffset); - - /* this will eventually disappear. keep it as safeguard for now */ - if (cmds == NULL) { - knet_vty_write(vty, "No commands associated to this node%s", telnet_newline); - return; - } - - if (knet_vty_is_line_empty(vty) || cmd == NULL) { - while (cmds[idx].cmd != NULL) { - print_help(vty, cmds, idx); - idx++; - } - return; - } - - match_command(vty, cmds, cmd, cmdlen, cmdoffset, KNET_VTY_MATCH_HELP); -} - -void knet_vty_tab_completion(struct knet_vty *vty) -{ - const vty_node_cmds_t *cmds = NULL; - char *cmd = NULL; - int cmdlen = 0; - int cmdoffset = 0; - - if (knet_vty_is_line_empty(vty)) - return; - - knet_vty_write(vty, "%s", telnet_newline); - - cmds = get_cmds(vty, &cmd, &cmdlen, &cmdoffset); - - /* this will eventually disappear. keep it as safeguard for now */ - if (cmds == NULL) { - knet_vty_write(vty, "No commands associated to this node%s", telnet_newline); - return; - } - - match_command(vty, cmds, cmd, cmdlen, cmdoffset, KNET_VTY_MATCH_EXPAND); - - knet_vty_prompt(vty); - knet_vty_write(vty, "%s", vty->line); -} diff --git a/kronosnetd/vty_cli_cmds.h b/kronosnetd/vty_cli_cmds.h deleted file mode 100644 index c6d091d5..00000000 --- a/kronosnetd/vty_cli_cmds.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNETD_VTY_CLI_CMDS_H__ -#define __KNETD_VTY_CLI_CMDS_H__ - -#include "vty.h" - -typedef struct { - const int param; -} vty_param_t; - -typedef struct { - const char *cmd; - const char *help; - const vty_param_t *params; - int (*func) (struct knet_vty *vty); -} vty_node_cmds_t; - -typedef struct { - const int node_num; - const char *prompt; - const vty_node_cmds_t *cmds; - const vty_node_cmds_t *no_cmds; -} vty_nodes_t; - -enum vty_nodes { - NODE_ROOT = 0, - NODE_CONFIG, - NODE_INTERFACE, - NODE_PEER, - NODE_LINK, - NODE_VTY -}; - -int knet_vty_execute_cmd(struct knet_vty *vty); -void knet_vty_help(struct knet_vty *vty); -void knet_vty_tab_completion(struct knet_vty *vty); - -int knet_read_conf(void); -void knet_close_down(void); - -extern vty_nodes_t knet_vty_nodes[]; - -#endif diff --git a/kronosnetd/vty_utils.c b/kronosnetd/vty_utils.c deleted file mode 100644 index e6feb91e..00000000 --- a/kronosnetd/vty_utils.c +++ /dev/null @@ -1,269 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "logging.h" -#include "vty_cli.h" -#include "vty_cli_cmds.h" -#include "vty_utils.h" - -static int check_vty(struct knet_vty *vty) -{ - if (!vty) { - errno = EINVAL; - return -1; - } - if (vty->got_epipe) { - errno = EPIPE; - return -1; - } - return 0; -} - -/* - * TODO: implement loopy_write here - * should sock be non-blocking? - */ -static int knet_vty_loopy_write(struct knet_vty *vty, const char *buf, size_t bufsize) -{ - ssize_t writelen; - - writelen = write(vty->vty_sock, buf, bufsize); - if (writelen < 0) - vty->got_epipe = 1; - - return writelen; -} - -int knet_vty_write(struct knet_vty *vty, const char *format, ...) -{ - va_list args; - int len = 0; - char buf[VTY_MAX_BUFFER_SIZE]; - - if (check_vty(vty)) - return -1; - - va_start (args, format); - len = vsnprintf (buf, VTY_MAX_BUFFER_SIZE, format, args); - va_end (args); - - if ((len < 0) || (len > VTY_MAX_BUFFER_SIZE)) - return -1; - - return knet_vty_loopy_write(vty, buf, len); -} - -static int knet_vty_read_real(struct knet_vty *vty, unsigned char *buf, size_t bufsize, - int ignore_iac) -{ - ssize_t readlen; - -iac_retry: - readlen = recv(vty->vty_sock, buf, bufsize, 0); - if (readlen == 0) { - vty->got_epipe = 1; - goto out_clean; - } - if (readlen < 0) - goto out_clean; - - vty->idle = 0; - - /* at somepoint we have to add IAC parsing */ - if ((buf[0] == IAC) && (ignore_iac)) - goto iac_retry; - -out_clean: - return readlen; -} - -int knet_vty_read(struct knet_vty *vty, unsigned char *buf, size_t bufsize) -{ - if (check_vty(vty)) - return -1; - - if ((!buf) || (bufsize == 0)) { - errno = EINVAL; - return -1; - } - return knet_vty_read_real(vty, buf, bufsize, 1); -} - -static int knet_vty_set_echooff(struct knet_vty *vty) -{ - unsigned char cmdreply[VTY_MAX_BUFFER_SIZE]; - unsigned char cmdechooff[] = { IAC, WILL, TELOPT_ECHO, '\0' }; - unsigned char cmdechooffreply[] = { IAC, DO, TELOPT_ECHO, '\0' }; - ssize_t readlen; - - if (knet_vty_write(vty, "%s", cmdechooff) < 0) - return -1; - - readlen = knet_vty_read_real(vty, cmdreply, VTY_MAX_BUFFER_SIZE, 0); - if (readlen < 0) - return readlen; - - if (memcmp(&cmdreply, &cmdechooffreply, readlen)) - return -1; - - return 0; -} - -static int knet_vty_set_echoon(struct knet_vty *vty) -{ - unsigned char cmdreply[VTY_MAX_BUFFER_SIZE]; - unsigned char cmdechoon[] = { IAC, WONT, TELOPT_ECHO, '\0' }; - unsigned char cmdechoonreply[] = { IAC, DONT, TELOPT_ECHO, '\0' }; - ssize_t readlen; - - if (knet_vty_write(vty, "%s", cmdechoon) < 0) - return -1; - - readlen = knet_vty_read_real(vty, cmdreply, VTY_MAX_BUFFER_SIZE, 0); - if (readlen < 0) - return readlen; - - if (memcmp(&cmdreply, &cmdechoonreply, readlen)) - return -1; - - return 0; -} - -int knet_vty_set_echo(struct knet_vty *vty, int on) -{ - if (check_vty(vty)) - return -1; - - if (on) - return knet_vty_set_echoon(vty); - - return knet_vty_set_echooff(vty); -} - -void knet_vty_print_banner(struct knet_vty *vty) -{ - if (check_vty(vty)) - return; - - knet_vty_write(vty, - "Welcome to " PACKAGE "d " PACKAGE_VERSION " (built " __DATE__ - " " __TIME__ ")\n"); -} - -int knet_vty_set_iacs(struct knet_vty *vty) -{ - unsigned char cmdreply[VTY_MAX_BUFFER_SIZE]; - unsigned char cmdsga[] = { IAC, WILL, TELOPT_SGA, '\0' }; - unsigned char cmdsgareply[] = { IAC, DO, TELOPT_SGA, '\0' }; - unsigned char cmdlm[] = { IAC, DONT, TELOPT_LINEMODE, '\0' }; - ssize_t readlen; - - if (check_vty(vty)) - return -1; - - if (knet_vty_set_echo(vty, 0) < 0) - return -1; - - if (knet_vty_write(vty, "%s", cmdsga) < 0) - return -1; - - readlen = knet_vty_read_real(vty, cmdreply, VTY_MAX_BUFFER_SIZE, 0); - if (readlen < 0) - return readlen; - - if (memcmp(&cmdreply, &cmdsgareply, readlen)) - return -1; - - if (knet_vty_write(vty, "%s", cmdlm) < 0) - return -1; - - return 0; -} - -void knet_vty_free_history(struct knet_vty *vty) -{ - int i; - - if (check_vty(vty)) - return; - - for (i = 0; i < KNET_VTY_MAX_HIST; i++) { - if (vty->history[i]) { - free(vty->history[i]); - vty->history[i] = NULL; - } - } -} - -void knet_vty_exit_node(struct knet_vty *vty) -{ - switch(vty->node) { - case NODE_VTY: - vty->node = vty->prevnode; - break; - case NODE_LINK: - vty->node = NODE_PEER; - break; - case NODE_PEER: - vty->node = NODE_INTERFACE; - break; - case NODE_INTERFACE: - vty->node = NODE_CONFIG; - break; - case NODE_CONFIG: - pthread_mutex_lock(&knet_vty_mutex); - knet_vty_config = -1; - pthread_mutex_unlock(&knet_vty_mutex); - vty->node = NODE_ROOT; - break; - case NODE_ROOT: - vty->got_epipe = 1; - break; - default: - knet_vty_write(vty, "No idea where to go..%s", telnet_newline); - break; - } -} - -int knet_vty_is_line_empty(struct knet_vty *vty) -{ - int idx; - - for (idx = 0; idx < vty->line_idx; idx++) { - if (vty->line[idx] != ' ') - return 0; - } - - return 1; -} - -void knet_vty_prompt(struct knet_vty *vty) -{ - char buf[3]; - - if (vty->user_can_enable) { - buf[0] = '#'; - } else { - buf[0] = '>'; - } - buf[1] = ' '; - buf[2] = 0; - knet_vty_write(vty, "%s%s", knet_vty_nodes[vty->node].prompt, buf); -} diff --git a/kronosnetd/vty_utils.h b/kronosnetd/vty_utils.h deleted file mode 100644 index 8dda8853..00000000 --- a/kronosnetd/vty_utils.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNETD_VTY_UTILS_H__ -#define __KNETD_VTY_UTILS_H__ - -#include "vty.h" - -#define VTY_MAX_BUFFER_SIZE 4096 - -int knet_vty_write(struct knet_vty *vty, const char *format, ...) - __attribute__ ((__format__ (__printf__, 2, 3))); - -int knet_vty_read(struct knet_vty *vty, unsigned char *buf, size_t bufsize); - -int knet_vty_set_echo(struct knet_vty *vty, int on); - -void knet_vty_print_banner(struct knet_vty *vty); - -int knet_vty_set_iacs(struct knet_vty *vty); - -void knet_vty_free_history(struct knet_vty *vty); - -void knet_vty_exit_node(struct knet_vty *vty); - -int knet_vty_is_line_empty(struct knet_vty *vty); - -void knet_vty_prompt(struct knet_vty *vty); - -#endif diff --git a/libknet/Makefile.am b/libknet/Makefile.am deleted file mode 100644 index 6fd9306e..00000000 --- a/libknet/Makefile.am +++ /dev/null @@ -1,82 +0,0 @@ -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Authors: Fabio M. Di Nitto -# Federico Simoncelli -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -MAINTAINERCLEANFILES = Makefile.in - -include $(top_srcdir)/build-aux/check.mk - -SYMFILE = libknet_exported_syms - -EXTRA_DIST = $(SYMFILE) - -SUBDIRS = . tests - -libversion = 0:0:0 - -# override global LIBS that pulls in lots of craft we don't need here -LIBS = - -sources = \ - common.c \ - compat.c \ - crypto.c \ - handle.c \ - host.c \ - link.c \ - logging.c \ - netutils.c \ - nsscrypto.c \ - threads_common.c \ - threads_dsthandler.c \ - threads_heartbeat.c \ - threads_pmtud.c \ - threads_rx.c \ - threads_tx.c \ - transport_udp.c \ - transport_sctp.c \ - transport_common.c - -include_HEADERS = libknet.h - -pkgconfigdir = $(libdir)/pkgconfig - -pkgconfig_DATA = libknet.pc - -noinst_HEADERS = \ - common.h \ - compat.h \ - crypto.h \ - host.h \ - internals.h \ - link.h \ - logging.h \ - netutils.h \ - nsscrypto.h \ - onwire.h \ - threads_common.h \ - threads_dsthandler.h \ - threads_heartbeat.h \ - threads_pmtud.h \ - threads_rx.h \ - threads_tx.h \ - transports.h - -lib_LTLIBRARIES = libknet.la - -libknet_la_SOURCES = $(sources) - -libknet_la_CFLAGS = $(nss_CFLAGS) - -EXTRA_libknet_la_DEPENDENCIES = $(SYMFILE) - -libknet_la_LDFLAGS = -Wl,--version-script=$(srcdir)/$(SYMFILE) \ - --export-dynamic \ - -version-number $(libversion) - -libknet_la_LIBADD = $(nss_LIBS) -lrt -lpthread -lm diff --git a/libknet/common.c b/libknet/common.c deleted file mode 100644 index fb905b45..00000000 --- a/libknet/common.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include - -#include "common.h" - -int _fdset_cloexec(int fd) -{ - int fdflags; - - fdflags = fcntl(fd, F_GETFD, 0); - if (fdflags < 0) - return -1; - - fdflags |= FD_CLOEXEC; - - if (fcntl(fd, F_SETFD, fdflags) < 0) - return -1; - - return 0; -} - -int _fdset_nonblock(int fd) -{ - int fdflags; - - fdflags = fcntl(fd, F_GETFL, 0); - if (fdflags < 0) - return -1; - - fdflags |= O_NONBLOCK; - - if (fcntl(fd, F_SETFL, fdflags) < 0) - return -1; - - return 0; -} diff --git a/libknet/common.h b/libknet/common.h deleted file mode 100644 index e00861e9..00000000 --- a/libknet/common.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_COMMON_H__ -#define __KNET_COMMON_H__ - -int _fdset_cloexec(int fd); -int _fdset_nonblock(int fd); - -#endif diff --git a/libknet/compat.c b/libknet/compat.c deleted file mode 100644 index 479db2e7..00000000 --- a/libknet/compat.c +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Author: Jan Friesse - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include - -#include "compat.h" - -#ifndef HAVE_SYS_EPOLL_H -#ifdef HAVE_KEVENT - -/* for FreeBSD which has kevent instead of epoll */ - -#include -#include -#include -#include - -static int32_t -_poll_to_filter_(int32_t event) -{ - int32_t out = 0; - if (event & POLLIN) - out |= EVFILT_READ; - if (event & POLLOUT) - out |= EVFILT_WRITE; - return out; -} - -int epoll_create(int size) -{ - return kqueue(); -} - - -int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) -{ - int ret = 0; - struct kevent ke; - short filters = _poll_to_filter_(event->events); - - switch (op) { - /* The kevent man page says that EV_ADD also does MOD */ - case EPOLL_CTL_ADD: - case EPOLL_CTL_MOD: - EV_SET(&ke, fd, filters, EV_ADD | EV_ENABLE, 0, 0, event->data.ptr); - break; - case EPOLL_CTL_DEL: - EV_SET(&ke, fd, filters, EV_DELETE, 0, 0, event->data.ptr); - break; - default: - errno = EINVAL; - return -1; - } - ret = kevent(epfd, &ke, 1, NULL, 0, NULL); - return ret; -} - -int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout_ms) -{ - struct kevent kevents[maxevents]; - struct timespec timeout = { 0, 0 }; - struct timespec *timeout_ptr = &timeout; - uint32_t revents; - int event_count; - int i; - int returned_events; - - if (timeout_ms != -1) { - timeout.tv_sec = timeout_ms/1000; - timeout.tv_nsec += (timeout_ms % 1000) * 1000000ULL; - } - else { - timeout_ptr = NULL; - } - - event_count = kevent(epfd, NULL, 0, kevents, maxevents, timeout_ptr); - if (event_count == -1) { - return -1; - } - - returned_events = 0; - for (i = 0; i < event_count; i++) { - revents = 0; - - if (kevents[i].flags & EV_ERROR) { - revents |= POLLERR; - } - if (kevents[i].flags & EV_EOF) { - revents |= POLLHUP; - } - if (kevents[i].filter == EVFILT_READ) { - revents |= POLLIN; - } - if (kevents[i].filter == EVFILT_WRITE) { - revents |= POLLOUT; - } - events[returned_events].events = revents; - events[returned_events].data.ptr = kevents[i].udata; - returned_events++; - } - - return returned_events; -} -#endif /* HAVE_KEVENT */ -#endif /* HAVE_SYS_EPOLL_H */ diff --git a/libknet/compat.h b/libknet/compat.h deleted file mode 100644 index 8a00c14f..00000000 --- a/libknet/compat.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Jan Friesse - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_COMPAT_H__ -#define __KNET_COMPAT_H__ - -#include "config.h" -#include -#include - -#ifndef ETIME -#define ETIME ETIMEDOUT -#endif - -#ifdef HAVE_SYS_EPOLL_H -#include -#else -#ifdef HAVE_KEVENT -#include -#define EPOLL_CTL_ADD 1 -#define EPOLL_CTL_MOD 2 -#define EPOLL_CTL_DEL 3 - -#define EPOLLIN POLLIN -#define EPOLLOUT POLLOUT - -typedef union epoll_data { - void *ptr; - int fd; - uint32_t u32; - uint64_t u64; -} epoll_data_t; - -struct epoll_event { - uint32_t events; /* Epoll events */ - epoll_data_t data; /* User data variable */ -}; - -int epoll_create(int size); -int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); -int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout_ms); - -#endif /* HAVE_KEVENT */ -#endif /* HAVE_SYS_EPOLL_H */ -#endif /* __KNET_COMPAT_H__ */ diff --git a/libknet/crypto.c b/libknet/crypto.c deleted file mode 100644 index 4c4cc021..00000000 --- a/libknet/crypto.c +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include - -#include "crypto.h" -#include "nsscrypto.h" -#include "internals.h" -#include "logging.h" - -/* - * internal module switch data - */ - -crypto_model_t modules_cmds[] = { - { "nss", nsscrypto_init, nsscrypto_fini, nsscrypto_encrypt_and_sign, nsscrypto_encrypt_and_signv, nsscrypto_authenticate_and_decrypt }, - { NULL, NULL, NULL, NULL, NULL, NULL }, -}; - -static int get_model(const char *model) -{ - int idx = 0; - - while (modules_cmds[idx].model_name != NULL) { - if (!strcmp(modules_cmds[idx].model_name, model)) - return idx; - idx++; - } - return -1; -} - -/* - * exported API - */ - -int crypto_encrypt_and_sign ( - knet_handle_t knet_h, - const unsigned char *buf_in, - const ssize_t buf_in_len, - unsigned char *buf_out, - ssize_t *buf_out_len) -{ - return modules_cmds[knet_h->crypto_instance->model].crypt(knet_h, buf_in, buf_in_len, buf_out, buf_out_len); -} - -int crypto_encrypt_and_signv ( - knet_handle_t knet_h, - const struct iovec *iov_in, - int iovcnt_in, - unsigned char *buf_out, - ssize_t *buf_out_len) -{ - return modules_cmds[knet_h->crypto_instance->model].cryptv(knet_h, iov_in, iovcnt_in, buf_out, buf_out_len); -} - -int crypto_authenticate_and_decrypt ( - knet_handle_t knet_h, - const unsigned char *buf_in, - const ssize_t buf_in_len, - unsigned char *buf_out, - ssize_t *buf_out_len) -{ - return modules_cmds[knet_h->crypto_instance->model].decrypt(knet_h, buf_in, buf_in_len, buf_out, buf_out_len); -} - -int crypto_init( - knet_handle_t knet_h, - struct knet_handle_crypto_cfg *knet_handle_crypto_cfg) -{ - log_debug(knet_h, KNET_SUB_CRYPTO, - "Initizializing crypto module [%s/%s/%s]", - knet_handle_crypto_cfg->crypto_model, - knet_handle_crypto_cfg->crypto_cipher_type, - knet_handle_crypto_cfg->crypto_hash_type); - - knet_h->crypto_instance = malloc(sizeof(struct crypto_instance)); - - if (!knet_h->crypto_instance) { - log_err(knet_h, KNET_SUB_CRYPTO, "Unable to allocate memory for crypto instance"); - return -1; - } - - knet_h->crypto_instance->model = get_model(knet_handle_crypto_cfg->crypto_model); - if (knet_h->crypto_instance->model < 0) { - log_err(knet_h, KNET_SUB_CRYPTO, "model %s not supported", knet_handle_crypto_cfg->crypto_model); - goto out_err; - } - - if (modules_cmds[knet_h->crypto_instance->model].init(knet_h, knet_handle_crypto_cfg)) - goto out_err; - - log_debug(knet_h, KNET_SUB_CRYPTO, "security network overhead: %u", knet_h->sec_header_size); - - return 0; - -out_err: - free(knet_h->crypto_instance); - knet_h->crypto_instance = NULL; - return -1; -} - -void crypto_fini( - knet_handle_t knet_h) -{ - if (knet_h->crypto_instance) { - modules_cmds[knet_h->crypto_instance->model].fini(knet_h); - free(knet_h->crypto_instance); - knet_h->crypto_instance = NULL; - } - - return; -} diff --git a/libknet/crypto.h b/libknet/crypto.h deleted file mode 100644 index bac3359c..00000000 --- a/libknet/crypto.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_CRYPTO_H__ -#define __KNET_CRYPTO_H__ - -#include "internals.h" - -struct crypto_instance { - int model; - void *model_instance; -}; - -typedef struct { - const char *model_name; - int (*init) (knet_handle_t knet_h, - struct knet_handle_crypto_cfg *knet_handle_crypto_cfg); - void (*fini) (knet_handle_t knet_h); - int (*crypt) (knet_handle_t knet_h, - const unsigned char *buf_in, - const ssize_t buf_in_len, - unsigned char *buf_out, - ssize_t *buf_out_len); - int (*cryptv) (knet_handle_t knet_h, - const struct iovec *iov_in, - int iovcnt_in, - unsigned char *buf_out, - ssize_t *buf_out_len); - int (*decrypt) (knet_handle_t knet_h, - const unsigned char *buf_in, - const ssize_t buf_in_len, - unsigned char *buf_out, - ssize_t *buf_out_len); -} crypto_model_t; - -int crypto_authenticate_and_decrypt ( - knet_handle_t knet_h, - const unsigned char *buf_in, - const ssize_t buf_in_len, - unsigned char *buf_out, - ssize_t *buf_out_len); - -int crypto_encrypt_and_sign ( - knet_handle_t knet_h, - const unsigned char *buf_in, - const ssize_t buf_in_len, - unsigned char *buf_out, - ssize_t *buf_out_len); - -int crypto_encrypt_and_signv ( - knet_handle_t knet_h, - const struct iovec *iov_in, - int iovcnt_in, - unsigned char *buf_out, - ssize_t *buf_out_len); - -int crypto_init( - knet_handle_t knet_h, - struct knet_handle_crypto_cfg *knet_handle_crypto_cfg); - -void crypto_fini( - knet_handle_t knet_h); - -#endif diff --git a/libknet/handle.c b/libknet/handle.c deleted file mode 100644 index 8f7aa132..00000000 --- a/libknet/handle.c +++ /dev/null @@ -1,1414 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "internals.h" -#include "crypto.h" -#include "compat.h" -#include "common.h" -#include "threads_common.h" -#include "threads_heartbeat.h" -#include "threads_pmtud.h" -#include "threads_dsthandler.h" -#include "threads_rx.h" -#include "threads_tx.h" -#include "transports.h" -#include "logging.h" - -static pthread_mutex_t handle_config_mutex = PTHREAD_MUTEX_INITIALIZER; - -static int _init_locks(knet_handle_t knet_h) -{ - int savederrno = 0; - - savederrno = pthread_rwlock_init(&knet_h->global_rwlock, NULL); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize list rwlock: %s", - strerror(savederrno)); - goto exit_fail; - } - - knet_h->lock_init_done = 1; - - savederrno = pthread_mutex_init(&knet_h->pmtud_mutex, NULL); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize pmtud mutex: %s", - strerror(savederrno)); - goto exit_fail; - } - - savederrno = pthread_cond_init(&knet_h->pmtud_cond, NULL); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize pmtud conditional mutex: %s", - strerror(savederrno)); - goto exit_fail; - } - - savederrno = pthread_mutex_init(&knet_h->hb_mutex, NULL); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize hb_thread mutex: %s", - strerror(savederrno)); - goto exit_fail; - } - - savederrno = pthread_mutex_init(&knet_h->tx_mutex, NULL); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize tx_thread mutex: %s", - strerror(savederrno)); - goto exit_fail; - } - - savederrno = pthread_mutex_init(&knet_h->tx_seq_num_mutex, NULL); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize tx_seq_num_mutex mutex: %s", - strerror(savederrno)); - goto exit_fail; - } - - return 0; - -exit_fail: - errno = savederrno; - return -1; -} - -static void _destroy_locks(knet_handle_t knet_h) -{ - knet_h->lock_init_done = 0; - pthread_rwlock_destroy(&knet_h->global_rwlock); - pthread_mutex_destroy(&knet_h->pmtud_mutex); - pthread_cond_destroy(&knet_h->pmtud_cond); - pthread_mutex_destroy(&knet_h->hb_mutex); - pthread_mutex_destroy(&knet_h->tx_mutex); - pthread_mutex_destroy(&knet_h->tx_seq_num_mutex); -} - -static int _init_socks(knet_handle_t knet_h) -{ - int savederrno = 0; - - if (_init_socketpair(knet_h, knet_h->hostsockfd)) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize internal hostsockpair: %s", - strerror(savederrno)); - goto exit_fail; - } - - if (_init_socketpair(knet_h, knet_h->dstsockfd)) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize internal dstsockpair: %s", - strerror(savederrno)); - goto exit_fail; - } - - return 0; - -exit_fail: - errno = savederrno; - return -1; -} - -static void _close_socks(knet_handle_t knet_h) -{ - _close_socketpair(knet_h, knet_h->dstsockfd); - _close_socketpair(knet_h, knet_h->hostsockfd); -} - -static int _init_buffers(knet_handle_t knet_h) -{ - int savederrno = 0; - int i; - size_t bufsize; - - for (i = 0; i < PCKT_FRAG_MAX; i++) { - bufsize = ceil((float)KNET_MAX_PACKET_SIZE / (i + 1)) + KNET_HEADER_ALL_SIZE; - knet_h->send_to_links_buf[i] = malloc(bufsize); - if (!knet_h->send_to_links_buf[i]) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory datafd to link buffer: %s", - strerror(savederrno)); - goto exit_fail; - } - memset(knet_h->send_to_links_buf[i], 0, bufsize); - - knet_h->recv_from_sock_buf[i] = malloc(KNET_DATABUFSIZE); - if (!knet_h->recv_from_sock_buf[i]) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for app to datafd buffer: %s", - strerror(savederrno)); - goto exit_fail; - } - memset(knet_h->recv_from_sock_buf[i], 0, KNET_DATABUFSIZE); - - knet_h->recv_from_links_buf[i] = malloc(KNET_DATABUFSIZE); - if (!knet_h->recv_from_links_buf[i]) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for link to datafd buffer: %s", - strerror(savederrno)); - goto exit_fail; - } - memset(knet_h->recv_from_links_buf[i], 0, KNET_DATABUFSIZE); - } - - knet_h->pingbuf = malloc(KNET_HEADER_PING_SIZE); - if (!knet_h->pingbuf) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for hearbeat buffer: %s", - strerror(savederrno)); - goto exit_fail; - } - memset(knet_h->pingbuf, 0, KNET_HEADER_PING_SIZE); - - knet_h->pmtudbuf = malloc(KNET_PMTUD_SIZE_V6); - if (!knet_h->pmtudbuf) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for pmtud buffer: %s", - strerror(savederrno)); - goto exit_fail; - } - memset(knet_h->pmtudbuf, 0, KNET_PMTUD_SIZE_V6); - - for (i = 0; i < PCKT_FRAG_MAX; i++) { - bufsize = ceil((float)KNET_MAX_PACKET_SIZE / (i + 1)) + KNET_HEADER_ALL_SIZE + KNET_DATABUFSIZE_CRYPT_PAD; - knet_h->send_to_links_buf_crypt[i] = malloc(bufsize); - if (!knet_h->send_to_links_buf_crypt[i]) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for crypto datafd to link buffer: %s", - strerror(savederrno)); - goto exit_fail; - } - memset(knet_h->send_to_links_buf_crypt[i], 0, bufsize); - } - - knet_h->recv_from_links_buf_decrypt = malloc(KNET_DATABUFSIZE_CRYPT); - if (!knet_h->recv_from_links_buf_decrypt) { - savederrno = errno; - log_err(knet_h, KNET_SUB_CRYPTO, "Unable to allocate memory for crypto link to datafd buffer: %s", - strerror(savederrno)); - goto exit_fail; - } - memset(knet_h->recv_from_links_buf_decrypt, 0, KNET_DATABUFSIZE_CRYPT); - - knet_h->recv_from_links_buf_crypt = malloc(KNET_DATABUFSIZE_CRYPT); - if (!knet_h->recv_from_links_buf_crypt) { - savederrno = errno; - log_err(knet_h, KNET_SUB_CRYPTO, "Unable to allocate memory for crypto link to datafd buffer: %s", - strerror(savederrno)); - goto exit_fail; - } - memset(knet_h->recv_from_links_buf_crypt, 0, KNET_DATABUFSIZE_CRYPT); - - knet_h->pingbuf_crypt = malloc(KNET_DATABUFSIZE_CRYPT); - if (!knet_h->pingbuf_crypt) { - savederrno = errno; - log_err(knet_h, KNET_SUB_CRYPTO, "Unable to allocate memory for crypto hearbeat buffer: %s", - strerror(savederrno)); - goto exit_fail; - } - memset(knet_h->pingbuf_crypt, 0, KNET_DATABUFSIZE_CRYPT); - - knet_h->pmtudbuf_crypt = malloc(KNET_DATABUFSIZE_CRYPT); - if (!knet_h->pmtudbuf_crypt) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for crypto pmtud buffer: %s", - strerror(savederrno)); - goto exit_fail; - } - memset(knet_h->pmtudbuf_crypt, 0, KNET_DATABUFSIZE_CRYPT); - - memset(knet_h->knet_transport_fd_tracker, KNET_MAX_TRANSPORTS, sizeof(knet_h->knet_transport_fd_tracker)); - - return 0; - -exit_fail: - errno = savederrno; - return -1; -} - -static void _destroy_buffers(knet_handle_t knet_h) -{ - int i; - - for (i = 0; i < PCKT_FRAG_MAX; i++) { - free(knet_h->send_to_links_buf[i]); - free(knet_h->recv_from_sock_buf[i]); - free(knet_h->send_to_links_buf_crypt[i]); - free(knet_h->recv_from_links_buf[i]); - } - free(knet_h->recv_from_links_buf_decrypt); - free(knet_h->recv_from_links_buf_crypt); - free(knet_h->pingbuf); - free(knet_h->pingbuf_crypt); - free(knet_h->pmtudbuf); - free(knet_h->pmtudbuf_crypt); -} - -static int _init_epolls(knet_handle_t knet_h) -{ - struct epoll_event ev; - int savederrno = 0; - - /* - * even if the kernel does dynamic allocation with epoll_ctl - * we need to reserve one extra for host to host communication - */ - knet_h->send_to_links_epollfd = epoll_create(KNET_EPOLL_MAX_EVENTS + 1); - if (knet_h->send_to_links_epollfd < 0) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to create epoll datafd to link fd: %s", - strerror(savederrno)); - goto exit_fail; - } - - knet_h->recv_from_links_epollfd = epoll_create(KNET_EPOLL_MAX_EVENTS); - if (knet_h->recv_from_links_epollfd < 0) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to create epoll link to datafd fd: %s", - strerror(savederrno)); - goto exit_fail; - } - - knet_h->dst_link_handler_epollfd = epoll_create(KNET_EPOLL_MAX_EVENTS); - if (knet_h->dst_link_handler_epollfd < 0) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to create epoll dst cache fd: %s", - strerror(savederrno)); - goto exit_fail; - } - - if (_fdset_cloexec(knet_h->send_to_links_epollfd)) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to set CLOEXEC on datafd to link epoll fd: %s", - strerror(savederrno)); - goto exit_fail; - } - - if (_fdset_cloexec(knet_h->recv_from_links_epollfd)) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to set CLOEXEC on link to datafd epoll fd: %s", - strerror(savederrno)); - goto exit_fail; - } - - if (_fdset_cloexec(knet_h->dst_link_handler_epollfd)) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to set CLOEXEC on dst cache epoll fd: %s", - strerror(savederrno)); - goto exit_fail; - } - - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = knet_h->hostsockfd[0]; - - if (epoll_ctl(knet_h->send_to_links_epollfd, - EPOLL_CTL_ADD, knet_h->hostsockfd[0], &ev)) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to add hostsockfd[0] to epoll pool: %s", - strerror(savederrno)); - goto exit_fail; - } - - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = knet_h->dstsockfd[0]; - - if (epoll_ctl(knet_h->dst_link_handler_epollfd, - EPOLL_CTL_ADD, knet_h->dstsockfd[0], &ev)) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to add dstsockfd[0] to epoll pool: %s", - strerror(savederrno)); - goto exit_fail; - } - - return 0; - -exit_fail: - errno = savederrno; - return -1; -} - -static void _close_epolls(knet_handle_t knet_h) -{ - struct epoll_event ev; - int i; - - memset(&ev, 0, sizeof(struct epoll_event)); - - for (i = 0; i < KNET_DATAFD_MAX; i++) { - if (knet_h->sockfd[i].in_use) { - epoll_ctl(knet_h->send_to_links_epollfd, EPOLL_CTL_DEL, knet_h->sockfd[i].sockfd[knet_h->sockfd[i].is_created], &ev); - if (knet_h->sockfd[i].sockfd[knet_h->sockfd[i].is_created]) { - _close_socketpair(knet_h, knet_h->sockfd[i].sockfd); - } - } - } - - epoll_ctl(knet_h->send_to_links_epollfd, EPOLL_CTL_DEL, knet_h->hostsockfd[0], &ev); - epoll_ctl(knet_h->dst_link_handler_epollfd, EPOLL_CTL_DEL, knet_h->dstsockfd[0], &ev); - close(knet_h->send_to_links_epollfd); - close(knet_h->recv_from_links_epollfd); - close(knet_h->dst_link_handler_epollfd); -} - -static int _start_transports(knet_handle_t knet_h) -{ - int i, savederrno = 0, err = 0; - - for (i=0; itransport_ops[i] = get_udp_transport(); - break; - case KNET_TRANSPORT_SCTP: - knet_h->transport_ops[i] = get_sctp_transport(); - break; - } - - if (knet_h->transport_ops[i]) { - if (knet_h->transport_ops[i]->transport_init(knet_h) < 0) { - savederrno = errno; - log_err(knet_h, KNET_SUB_HANDLE, "Failed to allocate transport handle for %s: %s", - knet_h->transport_ops[i]->transport_name, - strerror(savederrno)); - err = -1; - goto out; - } - } - } - -out: - errno = savederrno; - return err; -} - -static void _stop_transports(knet_handle_t knet_h) -{ - int i; - - for (i=0; itransport_ops[i]) { - knet_h->transport_ops[i]->transport_free(knet_h); - } - } -} - -static int _start_threads(knet_handle_t knet_h) -{ - int savederrno = 0; - - savederrno = pthread_create(&knet_h->pmtud_link_handler_thread, 0, - _handle_pmtud_link_thread, (void *) knet_h); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to start pmtud link thread: %s", - strerror(savederrno)); - goto exit_fail; - } - - savederrno = pthread_create(&knet_h->dst_link_handler_thread, 0, - _handle_dst_link_handler_thread, (void *) knet_h); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to start dst cache thread: %s", - strerror(savederrno)); - goto exit_fail; - } - - savederrno = pthread_create(&knet_h->send_to_links_thread, 0, - _handle_send_to_links_thread, (void *) knet_h); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to start datafd to link thread: %s", - strerror(savederrno)); - goto exit_fail; - } - - savederrno = pthread_create(&knet_h->recv_from_links_thread, 0, - _handle_recv_from_links_thread, (void *) knet_h); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to start link to datafd thread: %s", - strerror(savederrno)); - goto exit_fail; - } - - savederrno = pthread_create(&knet_h->heartbt_thread, 0, - _handle_heartbt_thread, (void *) knet_h); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to start heartbeat thread: %s", - strerror(savederrno)); - goto exit_fail; - } - return 0; - -exit_fail: - errno = savederrno; - return -1; -} - -static void _stop_threads(knet_handle_t knet_h) -{ - void *retval; - - /* - * allow threads to catch on shutdown request - * and release locks before we stop them. - * this isn't the most efficent way to handle it - * but it works good enough for now - */ - - sleep(1); - - if (knet_h->heartbt_thread) { - pthread_cancel(knet_h->heartbt_thread); - pthread_join(knet_h->heartbt_thread, &retval); - } - - if (knet_h->send_to_links_thread) { - pthread_cancel(knet_h->send_to_links_thread); - pthread_join(knet_h->send_to_links_thread, &retval); - } - - if (knet_h->recv_from_links_thread) { - pthread_cancel(knet_h->recv_from_links_thread); - pthread_join(knet_h->recv_from_links_thread, &retval); - } - - if (knet_h->dst_link_handler_thread) { - pthread_cancel(knet_h->dst_link_handler_thread); - pthread_join(knet_h->dst_link_handler_thread, &retval); - } - - pthread_mutex_lock(&knet_h->pmtud_mutex); - pthread_cond_signal(&knet_h->pmtud_cond); - pthread_mutex_unlock(&knet_h->pmtud_mutex); - - sleep(1); - - if (knet_h->pmtud_link_handler_thread) { - pthread_cancel(knet_h->pmtud_link_handler_thread); - pthread_join(knet_h->pmtud_link_handler_thread, &retval); - } -} - -knet_handle_t knet_handle_new(knet_node_id_t host_id, - int log_fd, - uint8_t default_log_level) -{ - knet_handle_t knet_h; - int savederrno = 0; - struct rlimit cur; - - if (getrlimit(RLIMIT_NOFILE, &cur) < 0) { - return NULL; - } - - if ((log_fd < 0) || (log_fd >= cur.rlim_max)) { - errno = EINVAL; - return NULL; - } - - /* - * validate incoming request - */ - - if ((log_fd) && (default_log_level > KNET_LOG_DEBUG)) { - errno = EINVAL; - return NULL; - } - - /* - * allocate handle - */ - - knet_h = malloc(sizeof(struct knet_handle)); - if (!knet_h) { - errno = ENOMEM; - return NULL; - } - memset(knet_h, 0, sizeof(struct knet_handle)); - - savederrno = pthread_mutex_lock(&handle_config_mutex); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get handle mutex lock: %s", - strerror(savederrno)); - errno = savederrno; - goto exit_fail; - } - - /* - * copy config in place - */ - - knet_h->host_id = host_id; - knet_h->logfd = log_fd; - if (knet_h->logfd > 0) { - memset(&knet_h->log_levels, default_log_level, KNET_MAX_SUBSYSTEMS); - } - - /* - * set pmtud default timers - */ - knet_h->pmtud_interval = KNET_PMTUD_DEFAULT_INTERVAL; - - /* - * init main locking structures - */ - - if (_init_locks(knet_h)) { - savederrno = errno; - goto exit_fail; - } - - /* - * init sockets - */ - - if (_init_socks(knet_h)) { - savederrno = errno; - goto exit_fail; - } - - /* - * allocate packet buffers - */ - - if (_init_buffers(knet_h)) { - savederrno = errno; - goto exit_fail; - } - - /* - * create epoll fds - */ - - if (_init_epolls(knet_h)) { - savederrno = errno; - goto exit_fail; - } - - /* - * start transports - */ - - if (_start_transports(knet_h)) { - savederrno = errno; - goto exit_fail; - } - - /* - * start internal threads - */ - - if (_start_threads(knet_h)) { - savederrno = errno; - goto exit_fail; - } - - pthread_mutex_unlock(&handle_config_mutex); - return knet_h; - -exit_fail: - pthread_mutex_unlock(&handle_config_mutex); - knet_handle_free(knet_h); - errno = savederrno; - return NULL; -} - -int knet_handle_free(knet_handle_t knet_h) -{ - int savederrno = 0; - - savederrno = pthread_mutex_lock(&handle_config_mutex); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get handle mutex lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - if (!knet_h) { - pthread_mutex_unlock(&handle_config_mutex); - errno = EINVAL; - return -1; - } - - if (!knet_h->lock_init_done) { - goto exit_nolock; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", - strerror(savederrno)); - pthread_mutex_unlock(&handle_config_mutex); - errno = savederrno; - return -1; - } - - if (knet_h->host_head != NULL) { - savederrno = EBUSY; - log_err(knet_h, KNET_SUB_HANDLE, - "Unable to free handle: host(s) or listener(s) are still active: %s", - strerror(savederrno)); - pthread_rwlock_unlock(&knet_h->global_rwlock); - pthread_mutex_unlock(&handle_config_mutex); - errno = savederrno; - return -1; - } - - knet_h->fini_in_progress = 1; - - pthread_rwlock_unlock(&knet_h->global_rwlock); - - _stop_threads(knet_h); - _stop_transports(knet_h); - _close_epolls(knet_h); - _destroy_buffers(knet_h); - _close_socks(knet_h); - crypto_fini(knet_h); - - _destroy_locks(knet_h); - -exit_nolock: - free(knet_h); - knet_h = NULL; - pthread_mutex_unlock(&handle_config_mutex); - return 0; -} - -int knet_handle_enable_sock_notify(knet_handle_t knet_h, - void *sock_notify_fn_private_data, - void (*sock_notify_fn) ( - void *private_data, - int datafd, - int8_t channel, - uint8_t tx_rx, - int error, - int errorno)) -{ - int savederrno = 0, err = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (!sock_notify_fn) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - knet_h->sock_notify_fn_private_data = sock_notify_fn_private_data; - knet_h->sock_notify_fn = sock_notify_fn; - log_debug(knet_h, KNET_SUB_HANDLE, "sock_notify_fn enabled"); - - pthread_rwlock_unlock(&knet_h->global_rwlock); - - return err; -} - -int knet_handle_add_datafd(knet_handle_t knet_h, int *datafd, int8_t *channel) -{ - int err = 0, savederrno = 0; - int i; - struct epoll_event ev; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (datafd == NULL) { - errno = EINVAL; - return -1; - } - - if (channel == NULL) { - errno = EINVAL; - return -1; - } - - if (*channel >= KNET_DATAFD_MAX) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - if (!knet_h->sock_notify_fn) { - log_err(knet_h, KNET_SUB_HANDLE, "Adding datafd requires sock notify callback enabled!"); - savederrno = EINVAL; - err = -1; - goto out_unlock; - } - - if (*datafd > 0) { - for (i = 0; i < KNET_DATAFD_MAX; i++) { - if ((knet_h->sockfd[i].in_use) && (knet_h->sockfd[i].sockfd[0] == *datafd)) { - log_err(knet_h, KNET_SUB_HANDLE, "requested datafd: %d already exist in index: %d", *datafd, i); - savederrno = EEXIST; - err = -1; - goto out_unlock; - } - } - } - - /* - * auto allocate a channel - */ - if (*channel < 0) { - for (i = 0; i < KNET_DATAFD_MAX; i++) { - if (!knet_h->sockfd[i].in_use) { - *channel = i; - break; - } - } - if (*channel < 0) { - savederrno = EBUSY; - err = -1; - goto out_unlock; - } - } else { - if (knet_h->sockfd[*channel].in_use) { - savederrno = EBUSY; - err = -1; - goto out_unlock; - } - } - - knet_h->sockfd[*channel].is_created = 0; - knet_h->sockfd[*channel].is_socket = 0; - knet_h->sockfd[*channel].has_error = 0; - - if (*datafd > 0) { - int sockopt; - socklen_t sockoptlen = sizeof(sockopt); - - if (_fdset_cloexec(*datafd)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to set CLOEXEC on datafd: %s", - strerror(savederrno)); - goto out_unlock; - } - - if (_fdset_nonblock(*datafd)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to set NONBLOCK on datafd: %s", - strerror(savederrno)); - goto out_unlock; - } - - knet_h->sockfd[*channel].sockfd[0] = *datafd; - knet_h->sockfd[*channel].sockfd[1] = 0; - - if (!getsockopt(knet_h->sockfd[*channel].sockfd[0], SOL_SOCKET, SO_TYPE, &sockopt, &sockoptlen)) { - knet_h->sockfd[*channel].is_socket = 1; - } - } else { - if (_init_socketpair(knet_h, knet_h->sockfd[*channel].sockfd)) { - savederrno = errno; - err = -1; - goto out_unlock; - } - - knet_h->sockfd[*channel].is_created = 1; - knet_h->sockfd[*channel].is_socket = 1; - *datafd = knet_h->sockfd[*channel].sockfd[0]; - } - - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = knet_h->sockfd[*channel].sockfd[knet_h->sockfd[*channel].is_created]; - - if (epoll_ctl(knet_h->send_to_links_epollfd, - EPOLL_CTL_ADD, knet_h->sockfd[*channel].sockfd[knet_h->sockfd[*channel].is_created], &ev)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to add datafd %d to linkfd epoll pool: %s", - knet_h->sockfd[*channel].sockfd[knet_h->sockfd[*channel].is_created], strerror(savederrno)); - if (knet_h->sockfd[*channel].is_created) { - _close_socketpair(knet_h, knet_h->sockfd[*channel].sockfd); - } - goto out_unlock; - } - - knet_h->sockfd[*channel].in_use = 1; - -out_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_handle_remove_datafd(knet_handle_t knet_h, int datafd) -{ - int err = 0, savederrno = 0; - int8_t channel = -1; - int i; - struct epoll_event ev; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (datafd <= 0) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - for (i = 0; i < KNET_DATAFD_MAX; i++) { - if ((knet_h->sockfd[i].in_use) && - (knet_h->sockfd[i].sockfd[0] == datafd)) { - channel = i; - break; - } - } - - if (channel < 0) { - savederrno = EINVAL; - err = -1; - goto out_unlock; - } - - if (!knet_h->sockfd[channel].has_error) { - memset(&ev, 0, sizeof(struct epoll_event)); - - if (epoll_ctl(knet_h->send_to_links_epollfd, - EPOLL_CTL_DEL, knet_h->sockfd[channel].sockfd[knet_h->sockfd[channel].is_created], &ev)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to del datafd %d from linkfd epoll pool: %s", - knet_h->sockfd[channel].sockfd[0], strerror(savederrno)); - goto out_unlock; - } - } - - if (knet_h->sockfd[channel].is_created) { - _close_socketpair(knet_h, knet_h->sockfd[channel].sockfd); - } - - memset(&knet_h->sockfd[channel], 0, sizeof(struct knet_sock)); - -out_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_handle_get_datafd(knet_handle_t knet_h, const int8_t channel, int *datafd) -{ - int err = 0, savederrno = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if ((channel < 0) || (channel >= KNET_DATAFD_MAX)) { - errno = EINVAL; - return -1; - } - - if (datafd == NULL) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - if (!knet_h->sockfd[channel].in_use) { - savederrno = EINVAL; - err = -1; - goto out_unlock; - } - - *datafd = knet_h->sockfd[channel].sockfd[0]; - -out_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_handle_get_channel(knet_handle_t knet_h, const int datafd, int8_t *channel) -{ - int err = 0, savederrno = 0; - int i; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (datafd <= 0) { - errno = EINVAL; - return -1; - } - - if (channel == NULL) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - *channel = -1; - - for (i = 0; i < KNET_DATAFD_MAX; i++) { - if ((knet_h->sockfd[i].in_use) && - (knet_h->sockfd[i].sockfd[0] == datafd)) { - *channel = i; - break; - } - } - - if (*channel < 0) { - savederrno = EINVAL; - err = -1; - goto out_unlock; - } - -out_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_handle_enable_filter(knet_handle_t knet_h, - void *dst_host_filter_fn_private_data, - int (*dst_host_filter_fn) ( - void *private_data, - const unsigned char *outdata, - ssize_t outdata_len, - uint8_t tx_rx, - knet_node_id_t this_host_id, - knet_node_id_t src_node_id, - int8_t *channel, - knet_node_id_t *dst_host_ids, - size_t *dst_host_ids_entries)) -{ - int savederrno = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - knet_h->dst_host_filter_fn_private_data = dst_host_filter_fn_private_data; - knet_h->dst_host_filter_fn = dst_host_filter_fn; - if (knet_h->dst_host_filter_fn) { - log_debug(knet_h, KNET_SUB_HANDLE, "dst_host_filter_fn enabled"); - } else { - log_debug(knet_h, KNET_SUB_HANDLE, "dst_host_filter_fn disabled"); - } - - pthread_rwlock_unlock(&knet_h->global_rwlock); - - return 0; -} - -int knet_handle_setfwd(knet_handle_t knet_h, unsigned int enabled) -{ - int savederrno = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if ((enabled < 0) || (enabled > 1)) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - knet_h->enabled = enabled; - - if (enabled) { - log_debug(knet_h, KNET_SUB_HANDLE, "Data forwarding is enabled"); - } else { - log_debug(knet_h, KNET_SUB_HANDLE, "Data forwarding is disabled"); - } - - pthread_rwlock_unlock(&knet_h->global_rwlock); - - return 0; -} - -int knet_handle_pmtud_getfreq(knet_handle_t knet_h, unsigned int *interval) -{ - int savederrno = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (!interval) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - *interval = knet_h->pmtud_interval; - - pthread_rwlock_unlock(&knet_h->global_rwlock); - - return 0; -} - -int knet_handle_pmtud_setfreq(knet_handle_t knet_h, unsigned int interval) -{ - int savederrno = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if ((!interval) || (interval > 86400)) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - knet_h->pmtud_interval = interval; - log_debug(knet_h, KNET_SUB_HANDLE, "PMTUd interval set to: %u seconds", interval); - - pthread_rwlock_unlock(&knet_h->global_rwlock); - - return 0; -} - -int knet_handle_enable_pmtud_notify(knet_handle_t knet_h, - void *pmtud_notify_fn_private_data, - void (*pmtud_notify_fn) ( - void *private_data, - unsigned int data_mtu)) -{ - int savederrno = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - knet_h->pmtud_notify_fn_private_data = pmtud_notify_fn_private_data; - knet_h->pmtud_notify_fn = pmtud_notify_fn; - if (knet_h->pmtud_notify_fn) { - log_debug(knet_h, KNET_SUB_HANDLE, "pmtud_notify_fn enabled"); - } else { - log_debug(knet_h, KNET_SUB_HANDLE, "pmtud_notify_fn disabled"); - } - - pthread_rwlock_unlock(&knet_h->global_rwlock); - - return 0; -} - -int knet_handle_pmtud_get(knet_handle_t knet_h, - unsigned int *data_mtu) -{ - int savederrno = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (!data_mtu) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - *data_mtu = knet_h->data_mtu; - - pthread_rwlock_unlock(&knet_h->global_rwlock); - - return 0; -} - -int knet_handle_crypto(knet_handle_t knet_h, struct knet_handle_crypto_cfg *knet_handle_crypto_cfg) -{ - int savederrno = 0; - int err = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (!knet_handle_crypto_cfg) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - crypto_fini(knet_h); - - if ((!strncmp("none", knet_handle_crypto_cfg->crypto_model, 4)) || - ((!strncmp("none", knet_handle_crypto_cfg->crypto_cipher_type, 4)) && - (!strncmp("none", knet_handle_crypto_cfg->crypto_hash_type, 4)))) { - log_debug(knet_h, KNET_SUB_CRYPTO, "crypto is not enabled"); - err = 0; - goto exit_unlock; - } - - if (knet_handle_crypto_cfg->private_key_len < KNET_MIN_KEY_LEN) { - log_debug(knet_h, KNET_SUB_CRYPTO, "private key len too short (min %u): %u", - KNET_MIN_KEY_LEN, knet_handle_crypto_cfg->private_key_len); - savederrno = EINVAL; - err = -1; - goto exit_unlock; - } - - if (knet_handle_crypto_cfg->private_key_len > KNET_MAX_KEY_LEN) { - log_debug(knet_h, KNET_SUB_CRYPTO, "private key len too long (max %u): %u", - KNET_MAX_KEY_LEN, knet_handle_crypto_cfg->private_key_len); - savederrno = EINVAL; - err = -1; - goto exit_unlock; - } - - err = crypto_init(knet_h, knet_handle_crypto_cfg); - - if (err) { - err = -2; - } - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -ssize_t knet_recv(knet_handle_t knet_h, char *buff, const size_t buff_len, const int8_t channel) -{ - int savederrno = 0; - ssize_t err = 0; - struct iovec iov_in; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (buff == NULL) { - errno = EINVAL; - return -1; - } - - if (buff_len <= 0) { - errno = EINVAL; - return -1; - } - - if (buff_len > KNET_MAX_PACKET_SIZE) { - errno = EINVAL; - return -1; - } - - if (channel < 0) { - errno = EINVAL; - return -1; - } - - if (channel >= KNET_DATAFD_MAX) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - if (!knet_h->sockfd[channel].in_use) { - savederrno = EINVAL; - err = -1; - goto out_unlock; - } - - memset(&iov_in, 0, sizeof(iov_in)); - iov_in.iov_base = (void *)buff; - iov_in.iov_len = buff_len; - - err = readv(knet_h->sockfd[channel].sockfd[0], &iov_in, 1); - savederrno = errno; - -out_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -ssize_t knet_send(knet_handle_t knet_h, const char *buff, const size_t buff_len, const int8_t channel) -{ - int savederrno = 0; - ssize_t err = 0; - struct iovec iov_out[1]; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (buff == NULL) { - errno = EINVAL; - return -1; - } - - if (buff_len <= 0) { - errno = EINVAL; - return -1; - } - - if (buff_len > KNET_MAX_PACKET_SIZE) { - errno = EINVAL; - return -1; - } - - if (channel < 0) { - errno = EINVAL; - return -1; - } - - if (channel >= KNET_DATAFD_MAX) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - if (!knet_h->sockfd[channel].in_use) { - savederrno = EINVAL; - err = -1; - goto out_unlock; - } - - memset(iov_out, 0, sizeof(iov_out)); - - iov_out[0].iov_base = (void *)buff; - iov_out[0].iov_len = buff_len; - - err = writev(knet_h->sockfd[channel].sockfd[0], iov_out, 1); - savederrno = errno; - -out_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} diff --git a/libknet/host.c b/libknet/host.c deleted file mode 100644 index 29408ec2..00000000 --- a/libknet/host.c +++ /dev/null @@ -1,698 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "host.h" -#include "internals.h" -#include "logging.h" - -static void _host_list_update(knet_handle_t knet_h) -{ - struct knet_host *host; - knet_h->host_ids_entries = 0; - - for (host = knet_h->host_head; host != NULL; host = host->next) { - knet_h->host_ids[knet_h->host_ids_entries] = host->host_id; - knet_h->host_ids_entries++; - } -} - -int knet_host_add(knet_handle_t knet_h, knet_node_id_t host_id) -{ - int savederrno = 0, err = 0; - struct knet_host *host = NULL; - uint8_t link_idx; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HOST, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - if (knet_h->host_index[host_id]) { - err = -1; - savederrno = EEXIST; - log_err(knet_h, KNET_SUB_HOST, "Unable to add host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - host = malloc(sizeof(struct knet_host)); - if (!host) { - err = -1; - savederrno = errno; - log_err(knet_h, KNET_SUB_HOST, "Unable to allocate memory for host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - memset(host, 0, sizeof(struct knet_host)); - - /* - * set host_id - */ - host->host_id = host_id; - - /* - * set default host->name to host_id for logging - */ - snprintf(host->name, KNET_MAX_HOST_LEN - 1, "%u", host_id); - - /* - * initialize links internal data - */ - for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { - host->link[link_idx].link_id = link_idx; - } - - /* - * add new host to the index - */ - knet_h->host_index[host_id] = host; - - /* - * add new host to host list - */ - if (knet_h->host_head) { - host->next = knet_h->host_head; - } - knet_h->host_head = host; - - _host_list_update(knet_h); - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - if (err < 0) { - free(host); - } - errno = savederrno; - return err; -} - -int knet_host_remove(knet_handle_t knet_h, knet_node_id_t host_id) -{ - int savederrno = 0, err = 0; - struct knet_host *host, *removed; - uint8_t link_idx; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HOST, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_HOST, "Unable to remove host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - /* - * if links are configured we cannot release the host - */ - - for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { - if (host->link[link_idx].configured) { - err = -1; - savederrno = EBUSY; - log_err(knet_h, KNET_SUB_HOST, "Unable to remove host %u, links are still configured: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - } - - removed = NULL; - - /* - * removing host from list - */ - if (knet_h->host_head->host_id == host_id) { - removed = knet_h->host_head; - knet_h->host_head = removed->next; - } else { - for (host = knet_h->host_head; host->next != NULL; host = host->next) { - if (host->next->host_id == host_id) { - removed = host->next; - host->next = removed->next; - break; - } - } - } - - knet_h->host_index[host_id] = NULL; - free(removed); - - _host_list_update(knet_h); - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_host_set_name(knet_handle_t knet_h, knet_node_id_t host_id, const char *name) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HOST, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - if (!knet_h->host_index[host_id]) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_HOST, "Unable to find host %u to set name: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - if (!name) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_HOST, "Unable to set name for host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - if (strlen(name) >= KNET_MAX_HOST_LEN) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_HOST, "Requested name for host %u is too long: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - for (host = knet_h->host_head; host != NULL; host = host->next) { - if (!strncmp(host->name, name, KNET_MAX_HOST_LEN - 1)) { - err = -1; - savederrno = EEXIST; - log_err(knet_h, KNET_SUB_HOST, "Duplicated name found on host_id %u", - host->host_id); - goto exit_unlock; - } - } - - snprintf(knet_h->host_index[host_id]->name, KNET_MAX_HOST_LEN - 1, "%s", name); - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_host_get_name_by_host_id(knet_handle_t knet_h, knet_node_id_t host_id, - char *name) -{ - int savederrno = 0, err = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (!name) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HOST, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - if (!knet_h->host_index[host_id]) { - savederrno = EINVAL; - err = -1; - log_debug(knet_h, KNET_SUB_HOST, "Host %u not found", host_id); - goto exit_unlock; - } - - snprintf(name, KNET_MAX_HOST_LEN - 1, "%s", knet_h->host_index[host_id]->name); - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_host_get_id_by_host_name(knet_handle_t knet_h, const char *name, - knet_node_id_t *host_id) -{ - int savederrno = 0, err = 0, found = 0; - struct knet_host *host; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (!name) { - errno = EINVAL; - return -1; - } - - if (!host_id) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HOST, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - for (host = knet_h->host_head; host != NULL; host = host->next) { - if (!strncmp(name, host->name, KNET_MAX_HOST_LEN)) { - found = 1; - *host_id = host->host_id; - break; - } - } - - if (!found) { - savederrno = ENOENT; - err = -1; - } - - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_host_get_host_list(knet_handle_t knet_h, - knet_node_id_t *host_ids, size_t *host_ids_entries) -{ - int savederrno = 0, err = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if ((!host_ids) || (!host_ids_entries)) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HOST, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - memmove(host_ids, knet_h->host_ids, sizeof(knet_h->host_ids)); - *host_ids_entries = knet_h->host_ids_entries; - - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_host_set_policy(knet_handle_t knet_h, knet_node_id_t host_id, - uint8_t policy) -{ - int savederrno = 0, err = 0; - uint8_t old_policy; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (policy > KNET_LINK_POLICY_RR) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HOST, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - if (!knet_h->host_index[host_id]) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_HOST, "Unable to set name for host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - old_policy = knet_h->host_index[host_id]->link_handler_policy; - knet_h->host_index[host_id]->link_handler_policy = policy; - - if (_host_dstcache_update_async(knet_h, knet_h->host_index[host_id])) { - savederrno = errno; - err = -1; - knet_h->host_index[host_id]->link_handler_policy = old_policy; - log_debug(knet_h, KNET_SUB_HOST, "Unable to update switch cache for host %u: %s", - host_id, strerror(savederrno)); - } - - log_debug(knet_h, KNET_SUB_HOST, "Host %u has new switching policy: %u", host_id, policy); - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_host_get_policy(knet_handle_t knet_h, knet_node_id_t host_id, - uint8_t *policy) -{ - int savederrno = 0, err = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (!policy) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HOST, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - if (!knet_h->host_index[host_id]) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_HOST, "Unable to get name for host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - *policy = knet_h->host_index[host_id]->link_handler_policy; - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_host_get_status(knet_handle_t knet_h, knet_node_id_t host_id, - struct knet_host_status *status) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (!status) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HOST, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_HOST, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - memmove(status, &host->status, sizeof(struct knet_host_status)); - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_host_enable_status_change_notify(knet_handle_t knet_h, - void *host_status_change_notify_fn_private_data, - void (*host_status_change_notify_fn) ( - void *private_data, - knet_node_id_t host_id, - uint8_t reachable, - uint8_t remote, - uint8_t external)) -{ - int savederrno = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HOST, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - knet_h->host_status_change_notify_fn_private_data = host_status_change_notify_fn_private_data; - knet_h->host_status_change_notify_fn = host_status_change_notify_fn; - if (knet_h->host_status_change_notify_fn) { - log_debug(knet_h, KNET_SUB_HOST, "host_status_change_notify_fn enabled"); - } else { - log_debug(knet_h, KNET_SUB_HOST, "host_status_change_notify_fn disabled"); - } - - pthread_rwlock_unlock(&knet_h->global_rwlock); - - return 0; -} - -int _send_host_info(knet_handle_t knet_h, const void *data, const size_t datalen) -{ - if (knet_h->fini_in_progress) { - return 0; - } - - if (sendto(knet_h->hostsockfd[1], data, datalen, MSG_DONTWAIT | MSG_NOSIGNAL, NULL, 0) != datalen) { - log_debug(knet_h, KNET_SUB_HOST, "Unable to write data to hostpipe"); - return -1; - } - - return 0; -} - -static void _clear_cbuffers(struct knet_host *host, seq_num_t rx_seq_num) -{ - int i; - - memset(host->circular_buffer, 0, KNET_CBUFFER_SIZE); - host->rx_seq_num = rx_seq_num; - - memset(host->circular_buffer_defrag, 0, KNET_CBUFFER_SIZE); - - for (i = 0; i < KNET_MAX_LINK; i++) { - memset(&host->defrag_buf[i], 0, sizeof(struct knet_host_defrag_buf)); - } -} - -/* - * check if a given packet seq num is in the circular buffers - * defrag_buf = 0 -> use normal cbuf 1 -> use the defrag buffer lookup - */ - -int _seq_num_lookup(struct knet_host *host, seq_num_t seq_num, int defrag_buf, int clear_buf) -{ - size_t i, j; /* circular buffer indexes */ - seq_num_t seq_dist; - char *dst_cbuf = host->circular_buffer; - char *dst_cbuf_defrag = host->circular_buffer_defrag; - seq_num_t *dst_seq_num = &host->rx_seq_num; - - if (clear_buf) { - _clear_cbuffers(host, seq_num); - } - - if (seq_num < *dst_seq_num) { - seq_dist = (SEQ_MAX - seq_num) + *dst_seq_num; - } else { - seq_dist = *dst_seq_num - seq_num; - } - - j = seq_num % KNET_CBUFFER_SIZE; - - if (seq_dist < KNET_CBUFFER_SIZE) { /* seq num is in ring buffer */ - if (!defrag_buf) { - return (dst_cbuf[j] == 0) ? 1 : 0; - } else { - return (dst_cbuf_defrag[j] == 0) ? 1 : 0; - } - } else if (seq_dist <= SEQ_MAX - KNET_CBUFFER_SIZE) { - memset(dst_cbuf, 0, KNET_CBUFFER_SIZE); - memset(dst_cbuf_defrag, 0, KNET_CBUFFER_SIZE); - *dst_seq_num = seq_num; - } - - /* cleaning up circular buffer */ - i = (*dst_seq_num + 1) % KNET_CBUFFER_SIZE; - - if (i > j) { - memset(dst_cbuf + i, 0, KNET_CBUFFER_SIZE - i); - memset(dst_cbuf, 0, j + 1); - memset(dst_cbuf_defrag + i, 0, KNET_CBUFFER_SIZE - i); - memset(dst_cbuf_defrag, 0, j + 1); - } else { - memset(dst_cbuf + i, 0, j - i + 1); - memset(dst_cbuf_defrag + i, 0, j - i + 1); - } - - *dst_seq_num = seq_num; - - return 1; -} - -void _seq_num_set(struct knet_host *host, seq_num_t seq_num, int defrag_buf) -{ - if (!defrag_buf) { - host->circular_buffer[seq_num % KNET_CBUFFER_SIZE] = 1; - } else { - host->circular_buffer_defrag[seq_num % KNET_CBUFFER_SIZE] = 1; - } - - return; -} - -int _host_dstcache_update_async(knet_handle_t knet_h, struct knet_host *host) -{ - int savederrno = 0; - knet_node_id_t host_id = host->host_id; - - if (sendto(knet_h->dstsockfd[1], &host_id, sizeof(host_id), MSG_DONTWAIT | MSG_NOSIGNAL, NULL, 0) != sizeof(host_id)) { - savederrno = errno; - log_debug(knet_h, KNET_SUB_HOST, "Unable to write to dstpipefd[1]: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - return 0; -} - -int _host_dstcache_update_sync(knet_handle_t knet_h, struct knet_host *host) -{ - int link_idx; - int best_priority = -1; - int reachable = 0; - - host->active_link_entries = 0; - - for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { - if (host->link[link_idx].status.enabled != 1) /* link is not enabled */ - continue; - if (host->link[link_idx].status.connected != 1) /* link is not enabled */ - continue; - if (host->link[link_idx].has_valid_mtu != 1) /* link does not have valid MTU */ - continue; - - if (host->link_handler_policy == KNET_LINK_POLICY_PASSIVE) { - /* for passive we look for the only active link with higher priority */ - if (host->link[link_idx].priority > best_priority) { - host->active_links[0] = link_idx; - best_priority = host->link[link_idx].priority; - } - host->active_link_entries = 1; - } else { - /* for RR and ACTIVE we need to copy all available links */ - host->active_links[host->active_link_entries] = link_idx; - host->active_link_entries++; - } - } - - if (host->link_handler_policy == KNET_LINK_POLICY_PASSIVE) { - log_debug(knet_h, KNET_SUB_HOST, "host: %u (passive) best link: %u (pri: %u)", - host->host_id, host->link[host->active_links[0]].link_id, - host->link[host->active_links[0]].priority); - } else { - log_debug(knet_h, KNET_SUB_HOST, "host: %u has %u active links", - host->host_id, host->active_link_entries); - } - - /* no active links, we can clean the circular buffers and indexes */ - if (!host->active_link_entries) { - log_warn(knet_h, KNET_SUB_HOST, "host: %u has no active links", host->host_id); - _clear_cbuffers(host, 0); - } else { - reachable = 1; - } - - if (host->status.reachable != reachable) { - host->status.reachable = reachable; - if (knet_h->host_status_change_notify_fn) { - knet_h->host_status_change_notify_fn( - knet_h->host_status_change_notify_fn_private_data, - host->host_id, - host->status.reachable, - host->status.remote, - host->status.external); - } - } - - return 0; -} diff --git a/libknet/host.h b/libknet/host.h deleted file mode 100644 index 1b36c882..00000000 --- a/libknet/host.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_HOST_H__ -#define __KNET_HOST_H__ - -#include "internals.h" - -int _seq_num_lookup(struct knet_host *host, seq_num_t seq_num, int defrag_buf, int clear_buf); -void _seq_num_set(struct knet_host *host, seq_num_t seq_num, int defrag_buf); - -int _send_host_info(knet_handle_t knet_h, const void *data, const size_t datalen); -int _host_dstcache_update_async(knet_handle_t knet_h, struct knet_host *host); -int _host_dstcache_update_sync(knet_handle_t knet_h, struct knet_host *host); - -#endif diff --git a/libknet/internals.h b/libknet/internals.h deleted file mode 100644 index 20bae927..00000000 --- a/libknet/internals.h +++ /dev/null @@ -1,467 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_INTERNALS_H__ -#define __KNET_INTERNALS_H__ - -/* - * NOTE: you shouldn't need to include this header normally - */ - -#include "libknet.h" -#include "onwire.h" -#include "compat.h" - -#define KNET_DATABUFSIZE KNET_MAX_PACKET_SIZE + KNET_HEADER_ALL_SIZE -#define KNET_DATABUFSIZE_CRYPT_PAD 1024 -#define KNET_DATABUFSIZE_CRYPT KNET_DATABUFSIZE + KNET_DATABUFSIZE_CRYPT_PAD - -#define KNET_RING_RCVBUFF 8388608 - -#define PCKT_FRAG_MAX UINT8_MAX - -#define KNET_EPOLL_MAX_EVENTS KNET_DATAFD_MAX - -typedef void *knet_transport_link_t; /* per link transport handle */ -typedef void *knet_transport_t; /* per knet_h transport handle */ -struct knet_transport_ops; /* Forward because of circular dependancy */ - -struct knet_mmsghdr { - struct msghdr msg_hdr; /* Message header */ - unsigned int msg_len; /* Number of bytes transmitted */ -}; - -struct knet_link { - /* required */ - struct sockaddr_storage src_addr; - struct sockaddr_storage dst_addr; - /* configurable */ - unsigned int dynamic; /* see KNET_LINK_DYN_ define above */ - uint8_t priority; /* higher priority == preferred for A/P */ - unsigned long long ping_interval; /* interval */ - unsigned long long pong_timeout; /* timeout */ - unsigned int latency_fix; /* precision */ - uint8_t pong_count; /* how many ping/pong to send/receive before link is up */ - /* status */ - struct knet_link_status status; - /* internals */ - uint8_t link_id; - uint8_t transport_type; /* #defined constant from API */ - knet_transport_link_t transport_link; /* link_info_t from transport */ - int outsock; - unsigned int configured:1; /* set to 1 if src/dst have been configured transport initialized on this link*/ - unsigned int transport_connected:1; /* set to 1 if lower level transport is connected */ - unsigned int latency_exp; - uint8_t received_pong; - struct timespec ping_last; - /* used by PMTUD thread as temp per-link variables and should always contain the onwire_len value! */ - uint32_t proto_overhead; - struct timespec pmtud_last; - uint32_t last_ping_size; - uint32_t last_good_mtu; - uint32_t last_bad_mtu; - uint32_t last_sent_mtu; - uint32_t last_recv_mtu; - uint8_t has_valid_mtu; -}; - -#define KNET_CBUFFER_SIZE 4096 - -struct knet_host_defrag_buf { - char buf[KNET_DATABUFSIZE]; - uint8_t in_use; /* 0 buffer is free, 1 is in use */ - seq_num_t pckt_seq; /* identify the pckt we are receiving */ - uint8_t frag_recv; /* how many frags did we receive */ - uint8_t frag_map[PCKT_FRAG_MAX];/* bitmap of what we received? */ - uint8_t last_first; /* special case if we receive the last fragment first */ - uint16_t frag_size; /* normal frag size (not the last one) */ - uint16_t last_frag_size; /* the last fragment might not be aligned with MTU size */ - struct timespec last_update; /* keep time of the last pckt */ -}; - -struct knet_host { - /* required */ - knet_node_id_t host_id; - /* configurable */ - uint8_t link_handler_policy; - char name[KNET_MAX_HOST_LEN]; - /* status */ - struct knet_host_status status; - /* internals */ - char circular_buffer[KNET_CBUFFER_SIZE]; - seq_num_t rx_seq_num; - seq_num_t untimed_rx_seq_num; - seq_num_t timed_rx_seq_num; - uint8_t got_data; - /* defrag/reassembly buffers */ - struct knet_host_defrag_buf defrag_buf[KNET_MAX_LINK]; - char circular_buffer_defrag[KNET_CBUFFER_SIZE]; - /* link stuff */ - struct knet_link link[KNET_MAX_LINK]; - uint8_t active_link_entries; - uint8_t active_links[KNET_MAX_LINK]; - struct knet_host *next; -}; - -struct knet_sock { - int sockfd[2]; /* sockfd[0] will always be application facing - * and sockfd[1] internal if sockpair has been created by knet */ - int is_socket; /* check if it's a socket for recvmmsg usage */ - int is_created; /* knet created this socket and has to clean up on exit/del */ - int in_use; /* set to 1 if it's use, 0 if free */ - int has_error; /* set to 1 if there were errors reading from the sock - * and socket has been removed from epoll */ -}; - -struct knet_fd_trackers { - uint8_t transport; /* transport type (UDP/SCTP...) */ - uint8_t data_type; /* internal use for transport to define what data are associated - * to this fd */ - void *data; /* pointer to the data */ -}; - -#define KNET_MAX_FDS KNET_MAX_HOST * KNET_MAX_LINK * 4 - -struct knet_handle { - knet_node_id_t host_id; - unsigned int enabled:1; - struct knet_sock sockfd[KNET_DATAFD_MAX]; - int logfd; - uint8_t log_levels[KNET_MAX_SUBSYSTEMS]; - int hostsockfd[2]; - int dstsockfd[2]; - int send_to_links_epollfd; - int recv_from_links_epollfd; - int dst_link_handler_epollfd; - unsigned int pmtud_interval; - unsigned int data_mtu; /* contains the max data size that we can send onwire - * without frags */ - struct knet_host *host_head; - struct knet_host *host_index[KNET_MAX_HOST]; - knet_transport_t transports[KNET_MAX_TRANSPORTS+1]; - struct knet_transport_ops *transport_ops[KNET_MAX_TRANSPORTS+1]; - struct knet_fd_trackers knet_transport_fd_tracker[KNET_MAX_FDS]; /* track status for each fd handled by transports */ - knet_node_id_t host_ids[KNET_MAX_HOST]; - size_t host_ids_entries; - struct knet_header *recv_from_sock_buf[PCKT_FRAG_MAX]; - struct knet_header *send_to_links_buf[PCKT_FRAG_MAX]; - struct knet_header *recv_from_links_buf[PCKT_FRAG_MAX]; - struct knet_header *pingbuf; - struct knet_header *pmtudbuf; - pthread_t send_to_links_thread; - pthread_t recv_from_links_thread; - pthread_t heartbt_thread; - pthread_t dst_link_handler_thread; - pthread_t pmtud_link_handler_thread; - int lock_init_done; - pthread_rwlock_t global_rwlock; /* global config lock */ - pthread_mutex_t pmtud_mutex; /* pmtud mutex to handle conditional send/recv + timeout */ - pthread_cond_t pmtud_cond; /* conditional for above */ - pthread_mutex_t tx_mutex; /* used to protect knet_send_sync and TX thread */ - pthread_mutex_t hb_mutex; /* used to protect heartbeat thread and seq_num broadcasting */ - struct crypto_instance *crypto_instance; - uint16_t sec_header_size; - uint16_t sec_block_size; - uint16_t sec_hash_size; - uint16_t sec_salt_size; - unsigned char *send_to_links_buf_crypt[PCKT_FRAG_MAX]; - unsigned char *recv_from_links_buf_crypt; - unsigned char *recv_from_links_buf_decrypt; - unsigned char *pingbuf_crypt; - unsigned char *pmtudbuf_crypt; - seq_num_t tx_seq_num; - pthread_mutex_t tx_seq_num_mutex; - void *dst_host_filter_fn_private_data; - int (*dst_host_filter_fn) ( - void *private_data, - const unsigned char *outdata, - ssize_t outdata_len, - uint8_t tx_rx, - knet_node_id_t this_host_id, - knet_node_id_t src_node_id, - int8_t *channel, - knet_node_id_t *dst_host_ids, - size_t *dst_host_ids_entries); - void *pmtud_notify_fn_private_data; - void (*pmtud_notify_fn) ( - void *private_data, - unsigned int data_mtu); - void *host_status_change_notify_fn_private_data; - void (*host_status_change_notify_fn) ( - void *private_data, - knet_node_id_t host_id, - uint8_t reachable, - uint8_t remote, - uint8_t external); - void *sock_notify_fn_private_data; - void (*sock_notify_fn) ( - void *private_data, - int datafd, - int8_t channel, - uint8_t tx_rx, - int error, - int errorno); - int fini_in_progress; -}; - -/* - * NOTE: every single operation must be implementend - * for every protocol. - */ - -typedef struct knet_transport_ops { -/* - * transport generic information - */ - const char *transport_name; - const uint8_t transport_id; - uint32_t transport_mtu_overhead; -/* - * transport init must allocate the new transport - * and perform all internal initializations - * (threads, lists, etc). - */ - int (*transport_init)(knet_handle_t knet_h); -/* - * transport free must releases _all_ resources - * allocated by tranport_init - */ - int (*transport_free)(knet_handle_t knet_h); - -/* - * link operations should take care of all the - * sockets and epoll management for a given link/transport set - * transport_link_disable should return err = -1 and errno = EBUSY - * if listener is still in use, and any other errno in case - * the link cannot be disabled. - * - * set_config/clear_config are invoked in global write lock context - */ - int (*transport_link_set_config)(knet_handle_t knet_h, struct knet_link *link); - int (*transport_link_clear_config)(knet_handle_t knet_h, struct knet_link *link); - -/* - * transport callback for incoming dynamic connections - * this is called in global read lock context - */ - int (*transport_link_dyn_connect)(knet_handle_t knet_h, int sockfd, struct knet_link *link); - -/* - * per transport error handling of recvmmsg - * (see _handle_recv_from_links comments for details) - */ - -/* - * transport_rx_sock_error is invoked when recvmmsg returns <= 0 - * - * transport_rx_sock_error is invoked with both global_rdlock - */ - - int (*transport_rx_sock_error)(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno); - -/* - * transport_tx_sock_error is invoked with global_rwlock and - * it's invoked when sendto or sendmmsg returns =< 0 - * - * it should return: - * -1 on internal error - * 0 ignore error and continue - * 1 retry - * any sleep or wait action should happen inside the transport code - */ - int (*transport_tx_sock_error)(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno); - -/* - * this function is called on _every_ received packet - * to verify if the packet is data or internal protocol error handling - * - * it should return: - * -1 on error - * 0 packet is not data and we should continue the packet process loop - * 1 packet is not data and we should STOP the packet process loop - * 2 packet is data and should be parsed as such - * - * transport_rx_is_data is invoked with both global_rwlock - * and fd_tracker read lock (from RX thread) - */ - int (*transport_rx_is_data)(knet_handle_t knet_h, int sockfd, struct knet_mmsghdr *msg); -} knet_transport_ops_t; - -socklen_t sockaddr_len(const struct sockaddr_storage *ss); - -/** - * This is a kernel style list implementation. - * - * @author Steven Dake - */ - -struct knet_list_head { - struct knet_list_head *next; - struct knet_list_head *prev; -}; - -/** - * @def KNET_LIST_DECLARE() - * Declare and initialize a list head. - */ -#define KNET_LIST_DECLARE(name) \ - struct knet_list_head name = { &(name), &(name) } - -#define KNET_INIT_LIST_HEAD(ptr) do { \ - (ptr)->next = (ptr); (ptr)->prev = (ptr); \ -} while (0) - -/** - * Initialize the list entry. - * - * Points next and prev pointers to head. - * @param head pointer to the list head - */ -static inline void knet_list_init(struct knet_list_head *head) -{ - head->next = head; - head->prev = head; -} - -/** - * Add this element to the list. - * - * @param element the new element to insert. - * @param head pointer to the list head - */ -static inline void knet_list_add(struct knet_list_head *element, - struct knet_list_head *head) -{ - head->next->prev = element; - element->next = head->next; - element->prev = head; - head->next = element; -} - -/** - * Add to the list (but at the end of the list). - * - * @param element pointer to the element to add - * @param head pointer to the list head - * @see knet_list_add() - */ -static inline void knet_list_add_tail(struct knet_list_head *element, - struct knet_list_head *head) -{ - head->prev->next = element; - element->next = head; - element->prev = head->prev; - head->prev = element; -} - -/** - * Delete an entry from the list. - * - * @param _remove the list item to remove - */ -static inline void knet_list_del(struct knet_list_head *_remove) -{ - _remove->next->prev = _remove->prev; - _remove->prev->next = _remove->next; -} - -/** - * Replace old entry by new one - * @param old: the element to be replaced - * @param new: the new element to insert - */ -static inline void knet_list_replace(struct knet_list_head *old, - struct knet_list_head *new) -{ - new->next = old->next; - new->next->prev = new; - new->prev = old->prev; - new->prev->next = new; -} - -/** - * Tests whether list is the last entry in list head - * @param list: the entry to test - * @param head: the head of the list - * @return boolean true/false - */ -static inline int knet_list_is_last(const struct knet_list_head *list, - const struct knet_list_head *head) -{ - return list->next == head; -} - -/** - * A quick test to see if the list is empty (pointing to it's self). - * @param head pointer to the list head - * @return boolean true/false - */ -static inline int32_t knet_list_empty(const struct knet_list_head *head) -{ - return head->next == head; -} - - -/** - * Get the struct for this entry - * @param ptr: the &struct list_head pointer. - * @param type: the type of the struct this is embedded in. - * @param member: the name of the list_struct within the struct. - */ -#define knet_list_entry(ptr,type,member)\ - ((type *)((char *)(ptr)-(char*)(&((type *)0)->member))) - -/** - * Get the first element from a list - * @param ptr: the &struct list_head pointer. - * @param type: the type of the struct this is embedded in. - * @param member: the name of the list_struct within the struct. - */ -#define knet_list_first_entry(ptr, type, member) \ - knet_list_entry((ptr)->next, type, member) - -/** - * Iterate over a list - * @param pos: the &struct list_head to use as a loop counter. - * @param head: the head for your list. - */ -#define knet_list_for_each(pos, head) \ - for (pos = (head)->next; pos != (head); pos = pos->next) - -/** - * Iterate over a list backwards - * @param pos: the &struct list_head to use as a loop counter. - * @param head: the head for your list. - */ -#define knet_list_for_each_reverse(pos, head) \ - for (pos = (head)->prev; pos != (head); pos = pos->prev) - -/** - * Iterate over a list safe against removal of list entry - * @param pos: the &struct list_head to use as a loop counter. - * @param n: another &struct list_head to use as temporary storage - * @param head: the head for your list. - */ -#define knet_list_for_each_safe(pos, n, head) \ - for (pos = (head)->next, n = pos->next; pos != (head); \ - pos = n, n = pos->next) - -/** - * Iterate over list of given type - * @param pos: the type * to use as a loop counter. - * @param head: the head for your list. - * @param member: the name of the list_struct within the struct. - */ -#define knet_list_for_each_entry(pos, head, member) \ - for (pos = knet_list_entry((head)->next, typeof(*pos), member); \ - &pos->member != (head); \ - pos = knet_list_entry(pos->member.next, typeof(*pos), member)) - - -#endif diff --git a/libknet/libknet.h b/libknet/libknet.h deleted file mode 100644 index 70ab674f..00000000 --- a/libknet/libknet.h +++ /dev/null @@ -1,1452 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __LIBKNET_H__ -#define __LIBKNET_H__ - -#include -#include - -/* - * libknet limits - */ - -/* - * Maximum number of hosts - */ - -typedef uint16_t knet_node_id_t; -#define KNET_MAX_HOST 65536 - -/* - * Maximum number of links between 2 hosts - */ - -#define KNET_MAX_LINK 8 - -/* - * Maximum packet size that should be written to datafd - * see knet_handle_new for details - */ - -#define KNET_MAX_PACKET_SIZE 65536 - -/* - * Buffers used for pretty logging - * host is used to store both ip addresses and hostnames - */ - -#define KNET_MAX_HOST_LEN 256 -#define KNET_MAX_PORT_LEN 6 - -/* - * Some notifications can be generated either on TX or RX - */ - -#define KNET_NOTIFY_TX 0 -#define KNET_NOTIFY_RX 1 - -typedef struct knet_handle *knet_handle_t; - -/* - * Handle structs/API calls - */ - -/* - * knet_handle_new - * - * host_id - Each host in a knet is identified with a unique - * ID. when creating a new handle local host_id - * must be specified (0 to UINT16T_MAX are all valid). - * It is the user's responsibility to check that the value - * is unique, or bad things might happen. - * - * log_fd - Write file descriptor. If set to a value > 0, it will be used - * to write log packets (see below) from libknet to the application. - * Setting to 0 will disable logging from libknet. - * It is possible to enable logging at any given time (see logging API - * below). - * Make sure to either read from this filedescriptor properly and/or - * mark it O_NONBLOCK, otherwise if the fd becomes full, libknet could - * block. - * - * default_log_level - - * If logfd is specified, it will initialize all subsystems to log - * at default_log_level value. (see logging API below) - * - * on success, a new knet_handle_t is returned. - * on failure, NULL is returned and errno is set. - */ - -knet_handle_t knet_handle_new(knet_node_id_t host_id, - int log_fd, - uint8_t default_log_level); - -/* - * knet_handle_free - * - * knet_h - pointer to knet_handle_t - * - * Destroy a knet handle, free all resources - * - * knet_handle_free returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_handle_free(knet_handle_t knet_h); - -/* - * knet_handle_enable_sock_notify - * - * knet_h - pointer to knet_handle_t - * - * sock_notify_fn_private_data - * void pointer to data that can be used to identify - * the callback. - * - * sock_notify_fn - * A callback function that is invoked every time - * a socket in the datafd pool will report an error (-1) - * or an end of read (0) (see socket.7). - * This function MUST NEVER block or add substantial delays. - * The callback is invoked in an internal unlocked area - * to allow calls to knet_handle_add_datafd/knet_handle_remove_datafd - * to swap/replace the bad fd. - * if both err and errno are 0, it means that the socket - * has received a 0 byte packet (EOF?). - * The callback function must either remove the fd from knet - * (by calling knet_handle_remove_fd()) or dup a new fd in its place. - * Failure to do this can cause problems. - * - * knet_handle_enable_sock_notify returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_handle_enable_sock_notify(knet_handle_t knet_h, - void *sock_notify_fn_private_data, - void (*sock_notify_fn) ( - void *private_data, - int datafd, - int8_t channel, - uint8_t tx_rx, - int error, - int errorno)); /* sorry! can't call it errno ;) */ - -/* - * knet_handle_add_datafd - * - * IMPORTANT: In order to add datafd to knet, knet_handle_enable_sock_notify - * _MUST_ be set and be able to handle both errors (-1) and - * 0 bytes read / write from the provided datafd. - * On read error (< 0) from datafd, the socket is automatically - * removed from polling to avoid spinning on dead sockets. - * It is safe to call knet_handle_remove_datafd even on sockets - * that have been removed. - * - * knet_h - pointer to knet_handle_t - * - * *datafd - read/write file descriptor. - * knet will read data here to send to the other hosts - * and will write data received from the network. - * Each data packet can be of max size KNET_MAX_PACKET_SIZE! - * Applications using knet_send/knet_recv will receive a - * proper error if the packet size is not within boundaries. - * Applications using their own functions to write to the - * datafd should NOT write more than KNET_MAX_PACKET_SIZE. - * - * Please refer to handle.c on how to set up a socketpair. - * - * datafd can be 0, and knet_handle_add_datafd will create a properly - * populated socket pair the same way as ping_test, or a value - * higher than 0. A negative number will return an error. - * On exit knet_handle_free will take care to cleanup the - * socketpair only if they have been created by knet_handle_add_datafd. - * - * It is possible to pass either sockets or normal fds. - * User provided datafd will be marked as non-blocking and close-on-exit. - * - * *channel - This value has the same effect of VLAN tagging. - * A negative value will auto-allocate a channel. - * Setting a value between 0 and 31 will try to allocate that - * specific channel (unless already in use). - * - * It is possible to add up to 32 datafds but be aware that each - * one of them must have a receiving end on the other host. - * - * Example: - * hostA channel 0 will be delivered to datafd on hostB channel 0 - * hostA channel 1 to hostB channel 1. - * - * Each channel must have a unique file descriptor. - * - * If your application could have 2 channels on one host and one - * channel on another host, then you can use dst_host_filter - * to manipulate channel values on TX and RX. - * - * knet_handle_add_datafd returns: - * - * 0 on success - * *datafd will be populated with a socket if the original value was 0 - * or if a specific fd was set, the value is untouched. - * *channel will be populated with a channel number if the original value - * was negative or the value is untouched if a specific channel - * was requested. - * - * -1 on error and errno is set. - * *datafd and *channel are untouched or empty. - */ - -#define KNET_DATAFD_MAX 32 - -int knet_handle_add_datafd(knet_handle_t knet_h, int *datafd, int8_t *channel); - -/* - * knet_handle_remove_datafd - * - * knet_h - pointer to knet_handle_t - * - * datafd - file descriptor to remove. - * NOTE that if the socket/fd was created by knet_handle_add_datafd, - * the socket will be closed by libknet. - * - * knet_handle_remove_datafd returns: - * - * 0 on success - * - * -1 on error and errno is set. - */ - -int knet_handle_remove_datafd(knet_handle_t knet_h, int datafd); - -/* - * knet_handle_get_channel - * - * knet_h - pointer to knet_handle_t - * - * datafd - get the channel associated to this datafd - * - * *channel - will contain the result - * - * knet_handle_get_channel returns: - * - * 0 on success - * and *channel will contain the result - * - * -1 on error and errno is set. - * and *channel content is meaningless - */ - -int knet_handle_get_channel(knet_handle_t knet_h, const int datafd, int8_t *channel); - -/* - * knet_handle_get_datafd - * - * knet_h - pointer to knet_handle_t - * - * channel - get the datafd associated to this channel - * - * *datafd - will contain the result - * - * knet_handle_get_datafd returns: - * - * 0 on success - * and *datafd will contain the results - * - * -1 on error and errno is set. - * and *datafd content is meaningless - */ - -int knet_handle_get_datafd(knet_handle_t knet_h, const int8_t channel, int *datafd); - -/* - * knet_recv - * - * knet_h - pointer to knet_handle_t - * - * buff - pointer to buffer to store the received data - * - * buff_len - buffer lenght - * - * knet_recv is a commodity function to wrap iovec operations - * around a socket. It returns a call to readv(2). - */ - -ssize_t knet_recv(knet_handle_t knet_h, - char *buff, - const size_t buff_len, - const int8_t channel); - -/* - * knet_send - * - * knet_h - pointer to knet_handle_t - * - * buff - pointer to the buffer of data to send - * - * buff_len - length of data to send - * - * knet_send is a commodity function to wrap iovec operations - * around a socket. It returns a call to writev(2). - */ - -ssize_t knet_send(knet_handle_t knet_h, - const char *buff, - const size_t buff_len, - const int8_t channel); - -/* - * knet_send_sync - * - * knet_h - pointer to knet_handle_t - * - * buff - pointer to the buffer of data to send - * - * buff_len - length of data to send - * - * channel - data channel to use (see knet_handle_add_datafd) - * - * All knet RX/TX operations are async for performance reasons. - * There are applications that might need a sync version of data - * transmission and receive errors in case of failure to deliver - * to another host. - * knet_send_sync bypasses the whole TX async layer and delivers - * data directly to the link layer, and returns errors accordingly. - * knet_send_sync allows to send only one packet to one host at - * a time. It does NOT support multiple destinations or multicast - * packets. Decision is still based on dst_host_filter_fn. - * - * knet_send_sync returns 0 on success and -1 on error. - * - * In addition to normal sendmmsg errors, knet_send_sync can fail - * due to: - * - * ECANCELED - data forward is disabled - * EFAULT - dst_host_filter fatal error - * EINVAL - dst_host_filter did not provide - * dst_host_ids_entries on unicast pckts - * E2BIG - dst_host_filter did return more than one - * dst_host_ids_entries on unicast pckts - * ENOMSG - received unknown message type - * EHOSTDOWN - unicast pckt cannot be delivered because - * dest host is not connected yet - * ECHILD - crypto failed - * EAGAIN - sendmmsg was unable to send all messages and - * there was no progress during retry - */ - -int knet_send_sync(knet_handle_t knet_h, - const char *buff, - const size_t buff_len, - const int8_t channel); - -/* - * knet_handle_enable_filter - * - * knet_h - pointer to knet_handle_t - * - * dst_host_filter_fn_private_data - * void pointer to data that can be used to identify - * the callback. - * - * dst_host_filter_fn - - * is a callback function that is invoked every time - * a packet hits datafd (see knet_handle_new). - * the function allows users to tell libknet where the - * packet has to be delivered. - * - * const unsigned char *outdata - is a pointer to the - * current packet - * ssize_t outdata_len - lenght of the above data - * uint8_t tx_rx - filter is called on tx or rx - * (see defines below) - * knet_node_id_t this_host_id - host_id processing the packet - * knet_node_id_t src_host_id - host_id that generated the - * packet - * knet_node_id_t *dst_host_ids - array of KNET_MAX_HOST knet_node_id_t - * where to store the destinations - * size_t *dst_host_ids_entries - number of hosts to send the message - * - * dst_host_filter_fn should return - * -1 on error, packet is discarded. - * 0 packet is unicast and should be sent to dst_host_ids and there are - * dst_host_ids_entries in the buffer. - * 1 packet is broadcast/multicast and is sent all hosts. - * contents of dst_host_ids and dst_host_ids_entries are ignored. - * (see also kronosnetd/etherfilter.* for an example that filters based - * on ether protocol) - * - * knet_handle_enable_filter returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_handle_enable_filter(knet_handle_t knet_h, - void *dst_host_filter_fn_private_data, - int (*dst_host_filter_fn) ( - void *private_data, - const unsigned char *outdata, - ssize_t outdata_len, - uint8_t tx_rx, - knet_node_id_t this_host_id, - knet_node_id_t src_host_id, - int8_t *channel, - knet_node_id_t *dst_host_ids, - size_t *dst_host_ids_entries)); - -/* - * knet_handle_setfwd - * - * knet_h - pointer to knet_handle_t - * - * enable - set to 1 to allow data forwarding, 0 to disable data forwarding. - * - * knet_handle_setfwd returns: - * - * 0 on success - * -1 on error and errno is set. - * - * By default data forwarding is off and no traffic will pass through knet until - * it is set on. - */ - -int knet_handle_setfwd(knet_handle_t knet_h, unsigned int enabled); - -/* - * knet_handle_pmtud_setfreq - * - * knet_h - pointer to knet_handle_t - * - * interval - define the interval in seconds between PMTUd scans - * range from 1 to 86400 (24h) - * - * knet_handle_pmtud_setfreq returns: - * - * 0 on success - * -1 on error and errno is set. - * - * default interval is 60. - */ - -#define KNET_PMTUD_DEFAULT_INTERVAL 60 - -int knet_handle_pmtud_setfreq(knet_handle_t knet_h, unsigned int interval); - -/* - * knet_handle_pmtud_getfreq - * - * knet_h - pointer to knet_handle_t - * - * interval - pointer where to store the current interval value - * - * knet_handle_pmtud_setfreq returns: - * - * 0 on success - * -1 on error and errno is set. - */ - - -int knet_handle_pmtud_getfreq(knet_handle_t knet_h, unsigned int *interval); - -/* - * knet_handle_enable_pmtud_notify - * - * knet_h - pointer to knet_handle_t - * - * pmtud_notify_fn_private_data - * void pointer to data that can be used to identify - * the callback. - * - * pmtud_notify_fn - * is a callback function that is invoked every time - * a path MTU size change is detected. - * The function allows libknet to notify the user - * of data MTU, that's the max value that can be send - * onwire without fragmentation. The data MTU will always - * be lower than real link MTU because it accounts for - * protocol overhead, knet packet header and (if configured) - * crypto overhead, - * This function MUST NEVER block or add substantial delays. - * - * knet_handle_enable_pmtud_notify returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_handle_enable_pmtud_notify(knet_handle_t knet_h, - void *pmtud_notify_fn_private_data, - void (*pmtud_notify_fn) ( - void *private_data, - unsigned int data_mtu)); - -/* - * knet_handle_pmtud_get - * - * knet_h - pointer to knet_handle_t - * - * data_mtu - pointer where to store data_mtu (see above) - * - * knet_handle_pmtud_get returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_handle_pmtud_get(knet_handle_t knet_h, - unsigned int *data_mtu); - -/* - * knet_handle_crypto - * - * knet_h - pointer to knet_handle_t - * - * knet_handle_crypto_cfg - - * pointer to a knet_handle_crypto_cfg structure - * - * crypto_model should contain the model name. - * Currently only "nss" is supported. - * Setting to "none" will disable crypto. - * - * crypto_cipher_type - * should contain the cipher algo name. - * It can be set to "none" to disable - * encryption. - * Currently supported by "nss" model: - * "3des", "aes128", "aes192" and "aes256". - * - * crypto_hash_type - * should contain the hashing algo name. - * It can be set to "none" to disable - * hashing. - * Currently supported by "nss" model: - * "md5", "sha1", "sha256", "sha384" and "sha512". - * - * private_key will contain the private shared key. - * It has to be at least KNET_MIN_KEY_LEN long. - * - * private_key_len - * length of the provided private_key. - * - * Implementation notes/current limitations: - * - enabling crypto, will increase latency as packets have - * to processed. - * - enabling crypto might reduce the overall throughtput - * due to crypto data overhead. - * - re-keying is not implemented yet. - * - private/public key encryption/hashing is not currently - * planned. - * - crypto key must be the same for all hosts in the same - * knet instance. - * - it is safe to call knet_handle_crypto multiple times at runtime. - * The last config will be used. - * IMPORTANT: a call to knet_handle_crypto can fail due to: - * 1) failure to obtain locking - * 2) errors to initializing the crypto level. - * This can happen even in subsequent calls to knet_handle_crypto. - * A failure in crypto init, might leave your traffic unencrypted! - * It's best to stop data forwarding (see above), change crypto config, - * start forward again. - * - * knet_handle_crypto returns: - * - * 0 on success - * -1 on error and errno is set. - * -2 on crypto subsystem initialization error. No errno is provided at the moment (yet). - */ - -#define KNET_MIN_KEY_LEN 1024 -#define KNET_MAX_KEY_LEN 4096 - -struct knet_handle_crypto_cfg { - char crypto_model[16]; - char crypto_cipher_type[16]; - char crypto_hash_type[16]; - unsigned char private_key[KNET_MAX_KEY_LEN]; - unsigned int private_key_len; -}; - -int knet_handle_crypto(knet_handle_t knet_h, - struct knet_handle_crypto_cfg *knet_handle_crypto_cfg); - -/* - * host structs/API calls - */ - -/* - * knet_host_add - * - * knet_h - pointer to knet_handle_t - * - * host_id - each host in a knet is identified with a unique ID - * (see also knet_handle_new documentation above) - * - * knet_host_add returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_host_add(knet_handle_t knet_h, knet_node_id_t host_id); - -/* - * knet_host_remove - * - * knet_h - pointer to knet_handle_t - * - * host_id - each host in a knet is identified with a unique ID - * (see also knet_handle_new documentation above) - * - * knet_host_remove returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_host_remove(knet_handle_t knet_h, knet_node_id_t host_id); - -/* - * knet_host_set_name - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * name - this name will be used for pretty logging and eventually - * search for hosts (see also get_name and get_id below). - * Only up to KNET_MAX_HOST_LEN - 1 bytes will be accepted and - * name has to be unique for each host. - * - * knet_host_set_name returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_host_set_name(knet_handle_t knet_h, knet_node_id_t host_id, - const char *name); - -/* - * knet_host_get_name_by_host_id - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * name - pointer to a preallocated buffer of at least size KNET_MAX_HOST_LEN - * where the current host name will be stored - * (as set by knet_host_set_name or default by knet_host_add) - * - * knet_host_get_name_by_host_id returns: - * - * 0 on success - * -1 on error and errno is set (name is left untouched) - */ - -int knet_host_get_name_by_host_id(knet_handle_t knet_h, knet_node_id_t host_id, - char *name); - -/* - * knet_host_get_id_by_host_name - * - * knet_h - pointer to knet_handle_t - * - * name - name to lookup, max len KNET_MAX_HOST_LEN - * - * host_id - where to store the result - * - * knet_host_get_id_by_host_name returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_host_get_id_by_host_name(knet_handle_t knet_h, const char *name, - knet_node_id_t *host_id); - -/* - * knet_host_get_host_list - * - * knet_h - pointer to knet_handle_t - * - * host_ids - array of at lest KNET_MAX_HOST size - * - * host_ids_entries - - * number of entries writted in host_ids - * - * knet_host_get_host_list returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_host_get_host_list(knet_handle_t knet_h, - knet_node_id_t *host_ids, size_t *host_ids_entries); - -/* - * define switching policies - */ - -#define KNET_LINK_POLICY_PASSIVE 0 -#define KNET_LINK_POLICY_ACTIVE 1 -#define KNET_LINK_POLICY_RR 2 - -/* - * knet_host_set_policy - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * policy - there are currently 3 kind of simple switching policies - * as defined above, based on link configuration. - * KNET_LINK_POLICY_PASSIVE - the active link with the lowest - * priority will be used. - * if one or more active links share - * the same priority, the one with - * lowest link_id will be used. - * - * KNET_LINK_POLICY_ACTIVE - all active links will be used - * simultaneously to send traffic. - * link priority is ignored. - * - * KNET_LINK_POLICY_RR - round-robin policy, every packet - * will be send on a different active - * link. - * - * knet_host_set_policy returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_host_set_policy(knet_handle_t knet_h, knet_node_id_t host_id, - uint8_t policy); - -/* - * knet_host_get_policy - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * policy - will contain the current configured switching policy. - * Default is passive when creating a new host. - * - * knet_host_get_policy returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_host_get_policy(knet_handle_t knet_h, knet_node_id_t host_id, - uint8_t *policy); - -/* - * knet_host_enable_status_change_notify - * - * knet_h - pointer to knet_handle_t - * - * host_status_change_notify_fn_private_data - * void pointer to data that can be used to identify - * the callback. - * - * host_status_change_notify_fn - * is a callback function that is invoked every time - * there is a change in the host status. - * host status is identified by: - * - reachable, this host can send/receive data to/from host_id - * - remote, 0 if the host_id is connected locally or 1 if - * the there is one or more knet host(s) in between. - * NOTE: re-switching is NOT currently implemented, - * but this is ready for future and can avoid - * an API/ABI breakage later on. - * - external, 0 if the host_id is configured locally or 1 if - * it has been added from remote nodes config. - * NOTE: dynamic topology is NOT currently implemented, - * but this is ready for future and can avoid - * an API/ABI breakage later on. - * This function MUST NEVER block or add substantial delays. - * - * knet_host_status_change_notify returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_host_enable_status_change_notify(knet_handle_t knet_h, - void *host_status_change_notify_fn_private_data, - void (*host_status_change_notify_fn) ( - void *private_data, - knet_node_id_t host_id, - uint8_t reachable, - uint8_t remote, - uint8_t external)); - -/* - * define host status structure for quick lookup - * struct is in flux as more stats will be added soon - * - * reachable host_id can be seen either directly connected - * or via another host_id - * - * remote 0 = node is connected locally, 1 is visible via - * via another host_id - * - * external 0 = node is configured/known locally, - * 1 host_id has been received via another host_id - */ - -struct knet_host_status { - uint8_t reachable; - uint8_t remote; - uint8_t external; - /* add host statistics */ -}; - -/* - * knet_host_status_get - * - * knet_h - pointer to knet_handle_t - * - * status - pointer to knet_host_status struct (see above) - * - * knet_handle_pmtud_get returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_host_get_status(knet_handle_t knet_h, knet_node_id_t host_id, - struct knet_host_status *status); - -/* - * link structs/API calls - * - * every host allocated/managed by knet_host_* has - * KNET_MAX_LINK structures to define the network - * paths that connect 2 hosts. - * - * Each link is identified by a link_id that has a - * values between 0 and KNET_MAX_LINK - 1. - * - * KNOWN LIMITATIONS: - * - * - let's assume the scenario where two hosts are connected - * with any number of links. link_id must match on both sides. - * If host_id 0 link_id 0 is configured to connect IP1 to IP2 and - * host_id 0 link_id 1 is configured to connect IP3 to IP4, - * host_id 1 link_id 0 _must_ connect IP2 to IP1 and likewise - * host_id 1 link_id 1 _must_ connect IP4 to IP3. - * We might be able to lift this restriction in future, by using - * other data to determine src/dst link_id, but for now, deal with it. - * - * - - */ - -/* - * commodity functions to convert strings to sockaddr and viceversa - */ - -/* - * knet_strtoaddr - * - * host - IPaddr/hostname to convert - * be aware only the first IP address will be returned - * in case a hostname resolves to multiple IP - * - * port - port to connect to - * - * ss - sockaddr_storage where to store the converted data - * - * sslen - len of the sockaddr_storage - * - * knet_strtoaddr returns same error codes as getaddrinfo - * - */ - -int knet_strtoaddr(const char *host, const char *port, - struct sockaddr_storage *ss, socklen_t sslen); - -/* - * knet_addrtostr - * - * ss - sockaddr_storage to convert - * - * sslen - len of the sockaddr_storage - * - * host - IPaddr/hostname where to store data - * (recommended size: KNET_MAX_HOST_LEN) - * - * port - port buffer where to store data - * (recommended size: KNET_MAX_PORT_LEN) - * - * knet_strtoaddr returns same error codes as getnameinfo - * - */ - -int knet_addrtostr(const struct sockaddr_storage *ss, socklen_t sslen, - char *addr_buf, size_t addr_buf_size, - char *port_buf, size_t port_buf_size); - -/* - * knet_handle_get_transport_list - * - * knet_h - pointer to knet_handle_t - * - * transport_list - an array of struct transport_info that must be - * at least of size struct transport_info * KNET_MAX_TRANSPORTS - * - * transport_list_entries - pointer to a size_t where to store how many transports - * are available in this build of libknet. - * - * knet_handle_get_transport_list returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -#define KNET_TRANSPORT_UDP 0 -#define KNET_TRANSPORT_SCTP 1 -#define KNET_MAX_TRANSPORTS 2 - -struct transport_info { - const char *name; /* UDP/SCTP/etc... */ - uint8_t id; /* value that can be used for link_set_config */ - uint8_t properties; /* currently unused */ -}; - -int knet_handle_get_transport_list(knet_handle_t knet_h, - struct transport_info *transport_list, size_t *transport_list_entries); - -/* - * knet_handle_get_transport_name_by_id - * - * knet_h - pointer to knet_handle_t - * - * transport - one of the above KNET_TRANSPORT_xxx constants - * - * knet_handle_get_transport_name_by_id returns: - * - * pointer to the name on success or - * NULL on error and errno is set. - */ - -const char *knet_handle_get_transport_name_by_id(knet_handle_t knet_h, uint8_t transport); - -/* - * knet_handle_get_transport_id_by_name - * - * knet_h - pointer to knet_handle_t - * - * name - transport name (UDP/SCTP/etc) - * - * knet_handle_get_transport_name_by_id returns: - * - * KNET_MAX_TRANSPORTS on error and errno is set accordingly - * KNET_TRANSPORT_xxx on success. - */ - -uint8_t knet_handle_get_transport_id_by_name(knet_handle_t knet_h, const char *name); - -/* - * knet_link_set_config - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * link_id - see above - * - * transport - one of the above KNET_TRANSPORT_xxx constants - * - * src_addr - sockaddr_storage that can be either IPv4 or IPv6 - * - * dst_addr - sockaddr_storage that can be either IPv4 or IPv6 - * this can be null if we don't know the incoming - * IP address/port and the link will remain quiet - * till the node on the other end will initiate a - * connection - * - * knet_link_set_config returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_link_set_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - uint8_t transport, - struct sockaddr_storage *src_addr, - struct sockaddr_storage *dst_addr); - -/* - * knet_link_get_config - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * link_id - see above - * - * transport - see above - * - * src_addr - sockaddr_storage that can be either IPv4 or IPv6 - * - * dst_addr - sockaddr_storage that can be either IPv4 or IPv6 - * - * dynamic - 0 if dst_addr is static or 1 if dst_addr is dynamic. - * In case of 1, dst_addr can be NULL and it will be left - * untouched. - * - * knet_link_get_config returns: - * - * 0 on success. - * -1 on error and errno is set. - */ - -int knet_link_get_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - uint8_t *transport, - struct sockaddr_storage *src_addr, - struct sockaddr_storage *dst_addr, - uint8_t *dynamic); - -/* - * knet_link_clear_config - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * link_id - see above - * - * knet_link_clear_config returns: - * - * 0 on success. - * -1 on error and errno is set. - */ - -int knet_link_clear_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id); - -/* - * knet_link_set_enable - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * link_id - see above - * - * enabled - 0 disable the link, 1 enable the link - * - * knet_link_set_enable returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_link_set_enable(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - unsigned int enabled); - -/* - * knet_link_get_enable - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * link_id - see above - * - * enabled - 0 disable the link, 1 enable the link - * - * knet_link_get_enable returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_link_get_enable(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - unsigned int *enabled); - -/* - * knet_link_set_ping_timers - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * link_id - see above - * - * interval - specify the ping interval - * - * timeout - if no pong is received within this time, - * the link is declared dead - * - * precision - how many values of latency are used to calculate - * the average link latency (see also get_status below) - * - * knet_link_set_ping_timers returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -#define KNET_LINK_DEFAULT_PING_INTERVAL 1000 /* 1 second */ -#define KNET_LINK_DEFAULT_PING_TIMEOUT 2000 /* 2 seconds */ -#define KNET_LINK_DEFAULT_PING_PRECISION 2048 /* samples */ - -int knet_link_set_ping_timers(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - time_t interval, time_t timeout, unsigned int precision); - -/* - * knet_link_get_ping_timers - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * link_id - see above - * - * interval - ping intervall - * - * timeout - if no pong is received within this time, - * the link is declared dead - * - * precision - how many values of latency are used to calculate - * the average link latency (see also get_status below) - * - * knet_link_get_ping_timers returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_link_get_ping_timers(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - time_t *interval, time_t *timeout, unsigned int *precision); - -/* - * knet_link_set_pong_count - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * link_id - see above - * - * pong_count - how many valid ping/pongs before a link is marked UP. - * default: 5, value should be > 0 - * - * knet_link_set_pong_count returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -#define KNET_LINK_DEFAULT_PONG_COUNT 5 - -int knet_link_set_pong_count(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - uint8_t pong_count); - -/* - * knet_link_get_pong_count - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * link_id - see above - * - * pong_count - see above - * - * knet_link_get_pong_count returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_link_get_pong_count(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - uint8_t *pong_count); - -/* - * knet_link_set_priority - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * link_id - see above - * - * priority - specify the switching priority for this link - * see also knet_host_set_policy - * - * knet_link_set_priority returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_link_set_priority(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - uint8_t priority); - -/* - * knet_link_get_priority - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * link_id - see above - * - * priority - gather the switching priority for this link - * see also knet_host_set_policy - * - * knet_link_get_priority returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_link_get_priority(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - uint8_t *priority); - -/* - * knet_link_get_link_list - * - * knet_h - pointer to knet_handle_t - * - * link_ids - array of at lest KNET_MAX_LINK size - * with the list of configured links for a certain host. - * - * link_ids_entries - - * number of entries contained in link_ids - * - * knet_link_get_link_list returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_link_get_link_list(knet_handle_t knet_h, knet_node_id_t host_id, - uint8_t *link_ids, size_t *link_ids_entries); - -/* - * define link status structure for quick lookup - * struct is in flux as more stats will be added soon - * - * src/dst_{ipaddr,port} strings are filled by - * getnameinfo(3) when configuring the link. - * if the link is dynamic (see knet_link_set_config) - * dst_ipaddr/port will contain ipaddr/port of the currently - * connected peer or "Unknown" if it was not possible - * to determine the ipaddr/port at runtime. - * - * enabled see also knet_link_set/get_enable. - * - * connected the link is connected to a peer and ping/pong traffic - * is flowing. - * - * dynconnected the link has dynamic ip on the other end, and - * we can see the other host is sending pings to us. - * - * latency average latency of this link - * see also knet_link_set/get_timeout. - * - * pong_last if the link is down, this value tells us how long - * ago this link was active. A value of 0 means that the link - * has never been active. - */ - -struct knet_link_status { - char src_ipaddr[KNET_MAX_HOST_LEN]; - char src_port[KNET_MAX_PORT_LEN]; - char dst_ipaddr[KNET_MAX_HOST_LEN]; - char dst_port[KNET_MAX_PORT_LEN]; - unsigned int enabled:1; /* link is configured and admin enabled for traffic */ - unsigned int connected:1; /* link is connected for data (local view) */ - unsigned int dynconnected:1; /* link has been activated by remote dynip */ - unsigned long long latency; /* average latency computed by fix/exp */ - struct timespec pong_last; - unsigned int mtu; /* current detected MTU on this link */ - unsigned int proto_overhead; /* contains the size of the IP protocol, knet headers and - * crypto headers (if configured). This value is filled in - * ONLY after the first PMTUd run on that given link, - * and can change if link configuration or crypto configuration - * changes at runtime. - * WARNING: in general mtu + proto_overhead might or might - * not match the output of ifconfig mtu due to crypto - * requirements to pad packets to some specific boundaries. */ - /* add link statistics */ -}; - -/* - * knet_link_get_status - * - * knet_h - pointer to knet_handle_t - * - * host_id - see above - * - * link_id - see above - * - * status - pointer to knet_link_status struct (see above) - * - * knet_link_get_status returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_link_get_status(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - struct knet_link_status *status); - -/* - * logging structs/API calls - */ - -/* - * libknet is composed of several subsystems. In order - * to easily distinguish log messages coming from different - * places, each subsystem has its own ID. - * - * 0-19 config/management - * 20-39 internal threads - * 40-59 transports - * 60-69 crypto implementations - */ - -#define KNET_SUB_COMMON 0 /* common.c */ -#define KNET_SUB_HANDLE 1 /* handle.c alloc/dealloc config changes */ -#define KNET_SUB_HOST 2 /* host add/del/modify */ -#define KNET_SUB_LISTENER 3 /* listeners add/del/modify... */ -#define KNET_SUB_LINK 4 /* link add/del/modify */ -#define KNET_SUB_TRANSPORT 5 /* Transport common */ -#define KNET_SUB_CRYPTO 6 /* crypto.c config generic layer */ - -#define KNET_SUB_FILTER 19 /* allocated for users to log from dst_filter */ - -#define KNET_SUB_DSTCACHE 20 /* switching thread (destination cache handling) */ -#define KNET_SUB_HEARTBEAT 21 /* heartbeat thread */ -#define KNET_SUB_PMTUD 22 /* Path MTU Discovery thread */ -#define KNET_SUB_TX 23 /* send to link thread */ -#define KNET_SUB_RX 24 /* recv from link thread */ - -#define KNET_SUB_TRANSP_UDP 40 /* UDP Transport */ -#define KNET_SUB_TRANSP_SCTP 41 /* SCTP Transport */ - -#define KNET_SUB_NSSCRYPTO 60 /* nsscrypto.c */ - -#define KNET_SUB_UNKNOWN 254 -#define KNET_MAX_SUBSYSTEMS KNET_SUB_UNKNOWN + 1 - -/* - * Convert between subsystem IDs and names - */ - -/* - * knet_log_get_subsystem_name - * - * return internal name of the subsystem or "common" - */ - -const char *knet_log_get_subsystem_name(uint8_t subsystem); - -/* - * knet_log_get_subsystem_id - * - * return internal ID of the subsystem or KNET_SUB_COMMON - */ - -uint8_t knet_log_get_subsystem_id(const char *name); - -/* - * 4 log levels are enough for everybody - */ - -#define KNET_LOG_ERR 0 /* unrecoverable errors/conditions */ -#define KNET_LOG_WARN 1 /* recoverable errors/conditions */ -#define KNET_LOG_INFO 2 /* info, link up/down, config changes.. */ -#define KNET_LOG_DEBUG 3 - -/* - * Convert between log level values and names - */ - -/* - * knet_log_get_loglevel_name - * - * return internal name of the log level or "ERROR" for unknown values - */ - -const char *knet_log_get_loglevel_name(uint8_t level); - -/* - * knet_log_get_loglevel_id - * - * return internal log level ID or KNET_LOG_ERR for invalid names - */ - -uint8_t knet_log_get_loglevel_id(const char *name); - -/* - * every log message is composed by a text message (including a trailing \n) - * and message level/subsystem IDs. - * In order to make debugging easier it is possible to send those packets - * straight to stdout/stderr (see knet_bench.c stdout option). - */ - -#define KNET_MAX_LOG_MSG_SIZE 256 - -struct knet_log_msg { - char msg[KNET_MAX_LOG_MSG_SIZE - (sizeof(uint8_t)*2)]; - uint8_t subsystem; /* KNET_SUB_* */ - uint8_t msglevel; /* KNET_LOG_* */ -}; - -/* - * knet_log_set_log_level - * - * knet_h - same as above - * - * subsystem - same as above - * - * level - same as above - * - * knet_log_set_loglevel allows fine control of log levels by subsystem. - * See also knet_handle_new for defaults. - * - * knet_log_set_loglevel returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_log_set_loglevel(knet_handle_t knet_h, uint8_t subsystem, - uint8_t level); - -/* - * knet_log_get_log_level - * - * knet_h - same as above - * - * subsystem - same as above - * - * level - same as above - * - * knet_log_get_loglevel returns: - * - * 0 on success - * -1 on error and errno is set. - */ - -int knet_log_get_loglevel(knet_handle_t knet_h, uint8_t subsystem, - uint8_t *level); - -#endif diff --git a/libknet/libknet.pc.in b/libknet/libknet.pc.in deleted file mode 100644 index e3c2e1c1..00000000 --- a/libknet/libknet.pc.in +++ /dev/null @@ -1,19 +0,0 @@ -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Federico Simoncelli -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -prefix=@prefix@ -exec_prefix=${prefix} -libdir=@libdir@ -includedir=${prefix}/include - -Name: libknet -Version: @VERSION@ -Description: kronosnet library -Requires: -Libs: -L${libdir} -lknet -lpthread -Cflags: -I${includedir} diff --git a/libknet/libknet_exported_syms b/libknet/libknet_exported_syms deleted file mode 100644 index 9db144b9..00000000 --- a/libknet/libknet_exported_syms +++ /dev/null @@ -1,8 +0,0 @@ -# Version and symbol export for libknet.so - -LIBKNET_0.0 { - global: - knet_*; - local: - *; -}; diff --git a/libknet/link.c b/libknet/link.c deleted file mode 100644 index 141055ff..00000000 --- a/libknet/link.c +++ /dev/null @@ -1,973 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include - -#include "internals.h" -#include "logging.h" -#include "link.h" -#include "transports.h" -#include "host.h" - -int _link_updown(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - unsigned int enabled, unsigned int connected) -{ - struct knet_link *link = &knet_h->host_index[host_id]->link[link_id]; - - if ((link->status.enabled == enabled) && - (link->status.connected == connected)) - return 0; - - link->status.enabled = enabled; - link->status.connected = connected; - - _host_dstcache_update_async(knet_h, knet_h->host_index[host_id]); - - if ((link->status.dynconnected) && - (!link->status.connected)) - link->status.dynconnected = 0; - - return 0; -} - -int knet_link_set_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - uint8_t transport, - struct sockaddr_storage *src_addr, - struct sockaddr_storage *dst_addr) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - struct knet_link *link; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (link_id >= KNET_MAX_LINK) { - errno = EINVAL; - return -1; - } - - if (!src_addr) { - errno = EINVAL; - return -1; - } - - if (transport >= KNET_MAX_TRANSPORTS) { - errno = EINVAL; - return -1; - } - - if (!knet_h->transport_ops[transport]) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_LINK, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - link = &host->link[link_id]; - - if (link->configured != 0) { - err =-1; - savederrno = EBUSY; - log_err(knet_h, KNET_SUB_LINK, "Host %u link %u is currently configured: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - if (link->status.enabled != 0) { - err =-1; - savederrno = EBUSY; - log_err(knet_h, KNET_SUB_LINK, "Host %u link %u is currently in use: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - memmove(&link->src_addr, src_addr, sizeof(struct sockaddr_storage)); - - err = knet_addrtostr(src_addr, sizeof(struct sockaddr_storage), - link->status.src_ipaddr, KNET_MAX_HOST_LEN, - link->status.src_port, KNET_MAX_PORT_LEN); - if (err) { - if (err == EAI_SYSTEM) { - savederrno = errno; - log_warn(knet_h, KNET_SUB_LINK, - "Unable to resolve host: %u link: %u source addr/port: %s", - host_id, link_id, strerror(savederrno)); - } else { - savederrno = EINVAL; - log_warn(knet_h, KNET_SUB_LINK, - "Unable to resolve host: %u link: %u source addr/port: %s", - host_id, link_id, gai_strerror(err)); - } - err = -1; - goto exit_unlock; - } - - if (!dst_addr) { - link->dynamic = KNET_LINK_DYNIP; - } else { - - link->dynamic = KNET_LINK_STATIC; - - memmove(&link->dst_addr, dst_addr, sizeof(struct sockaddr_storage)); - err = knet_addrtostr(dst_addr, sizeof(struct sockaddr_storage), - link->status.dst_ipaddr, KNET_MAX_HOST_LEN, - link->status.dst_port, KNET_MAX_PORT_LEN); - if (err) { - if (err == EAI_SYSTEM) { - savederrno = errno; - log_warn(knet_h, KNET_SUB_LINK, - "Unable to resolve host: %u link: %u destination addr/port: %s", - host_id, link_id, strerror(savederrno)); - } else { - savederrno = EINVAL; - log_warn(knet_h, KNET_SUB_LINK, - "Unable to resolve host: %u link: %u destination addr/port: %s", - host_id, link_id, gai_strerror(err)); - } - err = -1; - goto exit_unlock; - } - } - - link->transport_type = transport; - link->transport_connected = 0; - link->proto_overhead = knet_h->transport_ops[link->transport_type]->transport_mtu_overhead; - link->configured = 1; - link->pong_count = KNET_LINK_DEFAULT_PONG_COUNT; - link->has_valid_mtu = 0; - link->ping_interval = KNET_LINK_DEFAULT_PING_INTERVAL * 1000; /* microseconds */ - link->pong_timeout = KNET_LINK_DEFAULT_PING_TIMEOUT * 1000; /* microseconds */ - link->latency_fix = KNET_LINK_DEFAULT_PING_PRECISION; - link->latency_exp = KNET_LINK_DEFAULT_PING_PRECISION - \ - ((link->ping_interval * KNET_LINK_DEFAULT_PING_PRECISION) / 8000000); - - if (knet_h->transport_ops[link->transport_type]->transport_link_set_config(knet_h, link) < 0) { - savederrno = errno; - err = -1; - goto exit_unlock; - } - log_debug(knet_h, KNET_SUB_LINK, "host: %u link: %u is configured", - host_id, link_id); - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_link_get_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - uint8_t *transport, - struct sockaddr_storage *src_addr, - struct sockaddr_storage *dst_addr, - uint8_t *dynamic) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - struct knet_link *link; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (link_id >= KNET_MAX_LINK) { - errno = EINVAL; - return -1; - } - - if (!src_addr) { - errno = EINVAL; - return -1; - } - - if (!dynamic) { - errno = EINVAL; - return -1; - } - - if (!transport) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_LINK, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - link = &host->link[link_id]; - - if (!link->configured) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "host %u link %u is not configured: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - if ((link->dynamic == KNET_LINK_STATIC) && (!dst_addr)) { - savederrno = EINVAL; - err = -1; - goto exit_unlock; - } - - memmove(src_addr, &link->src_addr, sizeof(struct sockaddr_storage)); - - *transport = link->transport_type; - - if (link->dynamic == KNET_LINK_STATIC) { - *dynamic = 0; - memmove(dst_addr, &link->dst_addr, sizeof(struct sockaddr_storage)); - } else { - *dynamic = 1; - } - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_link_clear_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - struct knet_link *link; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (link_id >= KNET_MAX_LINK) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_LINK, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - link = &host->link[link_id]; - - if (link->configured != 1) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Host %u link %u is not configured: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - if (link->status.enabled != 0) { - err = -1; - savederrno = EBUSY; - log_err(knet_h, KNET_SUB_LINK, "Host %u link %u is currently in use: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - if ((knet_h->transport_ops[link->transport_type]->transport_link_clear_config(knet_h, link) < 0) && - (errno != EBUSY)) { - savederrno = errno; - err = -1; - goto exit_unlock; - } - - memset(link, 0, sizeof(struct knet_link)); - link->link_id = link_id; - - log_debug(knet_h, KNET_SUB_LINK, "host: %u link: %u config has been wiped", - host_id, link_id); - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_link_set_enable(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - unsigned int enabled) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - struct knet_link *link; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (link_id >= KNET_MAX_LINK) { - errno = EINVAL; - return -1; - } - - if (enabled > 1) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_LINK, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - link = &host->link[link_id]; - - if (!link->configured) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "host %u link %u is not configured: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - if (link->status.enabled == enabled) { - err = 0; - goto exit_unlock; - } - - err = _link_updown(knet_h, host_id, link_id, enabled, link->status.connected); - savederrno = errno; - - if (enabled) { - goto exit_unlock; - } - - log_debug(knet_h, KNET_SUB_LINK, "host: %u link: %u is disabled", - host_id, link_id); - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_link_get_enable(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - unsigned int *enabled) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - struct knet_link *link; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (link_id >= KNET_MAX_LINK) { - errno = EINVAL; - return -1; - } - - if (!enabled) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_LINK, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - link = &host->link[link_id]; - - if (!link->configured) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "host %u link %u is not configured: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - *enabled = link->status.enabled; - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_link_set_pong_count(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - uint8_t pong_count) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - struct knet_link *link; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (link_id >= KNET_MAX_LINK) { - errno = EINVAL; - return -1; - } - - if (pong_count < 1) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_LINK, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - link = &host->link[link_id]; - - if (!link->configured) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "host %u link %u is not configured: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - link->pong_count = pong_count; - - log_debug(knet_h, KNET_SUB_LINK, - "host: %u link: %u pong count update: %u", - host_id, link_id, link->pong_count); - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_link_get_pong_count(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - uint8_t *pong_count) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - struct knet_link *link; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (link_id >= KNET_MAX_LINK) { - errno = EINVAL; - return -1; - } - - if (!pong_count) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_LINK, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - link = &host->link[link_id]; - - if (!link->configured) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "host %u link %u is not configured: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - *pong_count = link->pong_count; - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_link_set_ping_timers(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - time_t interval, time_t timeout, unsigned int precision) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - struct knet_link *link; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (link_id >= KNET_MAX_LINK) { - errno = EINVAL; - return -1; - } - - if (!interval) { - errno = EINVAL; - return -1; - } - - if (!timeout) { - errno = EINVAL; - return -1; - } - - if (!precision) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_LINK, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - link = &host->link[link_id]; - - if (!link->configured) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "host %u link %u is not configured: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - link->ping_interval = interval * 1000; /* microseconds */ - link->pong_timeout = timeout * 1000; /* microseconds */ - link->latency_fix = precision; - link->latency_exp = precision - \ - ((link->ping_interval * precision) / 8000000); - - log_debug(knet_h, KNET_SUB_LINK, - "host: %u link: %u timeout update - interval: %llu timeout: %llu precision: %d", - host_id, link_id, link->ping_interval, link->pong_timeout, precision); - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_link_get_ping_timers(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - time_t *interval, time_t *timeout, unsigned int *precision) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - struct knet_link *link; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (link_id >= KNET_MAX_LINK) { - errno = EINVAL; - return -1; - } - - if (!interval) { - errno = EINVAL; - return -1; - } - - if (!timeout) { - errno = EINVAL; - return -1; - } - - if (!precision) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_LINK, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - link = &host->link[link_id]; - - if (!link->configured) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "host %u link %u is not configured: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - *interval = link->ping_interval / 1000; /* microseconds */ - *timeout = link->pong_timeout / 1000; - *precision = link->latency_fix; - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_link_set_priority(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - uint8_t priority) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - struct knet_link *link; - uint8_t old_priority; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (link_id >= KNET_MAX_LINK) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_LINK, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - link = &host->link[link_id]; - - if (!link->configured) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "host %u link %u is not configured: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - old_priority = link->priority; - - if (link->priority == priority) { - err = 0; - goto exit_unlock; - } - - link->priority = priority; - - if (_host_dstcache_update_sync(knet_h, host)) { - savederrno = errno; - log_debug(knet_h, KNET_SUB_LINK, - "Unable to update link priority (host: %u link: %u priority: %u): %s", - host_id, link_id, link->priority, strerror(savederrno)); - link->priority = old_priority; - err = -1; - goto exit_unlock; - } - - log_debug(knet_h, KNET_SUB_LINK, - "host: %u link: %u priority set to: %u", - host_id, link_id, link->priority); - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_link_get_priority(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - uint8_t *priority) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - struct knet_link *link; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (link_id >= KNET_MAX_LINK) { - errno = EINVAL; - return -1; - } - - if (!priority) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_LINK, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - link = &host->link[link_id]; - - if (!link->configured) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "host %u link %u is not configured: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - *priority = link->priority; - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_link_get_link_list(knet_handle_t knet_h, knet_node_id_t host_id, - uint8_t *link_ids, size_t *link_ids_entries) -{ - int savederrno = 0, err = 0, i, count = 0; - struct knet_host *host; - struct knet_link *link; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (!link_ids) { - errno = EINVAL; - return -1; - } - - if (!link_ids_entries) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_LINK, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - for (i = 0; i < KNET_MAX_LINK; i++) { - link = &host->link[i]; - if (!link->configured) { - continue; - } - link_ids[count] = i; - count++; - } - - *link_ids_entries = count; - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} - -int knet_link_get_status(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, - struct knet_link_status *status) -{ - int savederrno = 0, err = 0; - struct knet_host *host; - struct knet_link *link; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (link_id >= KNET_MAX_LINK) { - errno = EINVAL; - return -1; - } - - if (!status) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_LINK, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - host = knet_h->host_index[host_id]; - if (!host) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s", - host_id, strerror(savederrno)); - goto exit_unlock; - } - - link = &host->link[link_id]; - - if (!link->configured) { - err = -1; - savederrno = EINVAL; - log_err(knet_h, KNET_SUB_LINK, "host %u link %u is not configured: %s", - host_id, link_id, strerror(savederrno)); - goto exit_unlock; - } - - memmove(status, &link->status, sizeof(struct knet_link_status)); - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} diff --git a/libknet/link.h b/libknet/link.h deleted file mode 100644 index 5f2c27b3..00000000 --- a/libknet/link.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_LINK_H__ -#define __KNET_LINK_H__ - -#include "internals.h" - -#define KNET_LINK_STATIC 0 /* link has static ip on both ends */ -#define KNET_LINK_DYNIP 1 /* link has dynamic destination ip */ - -int _link_updown(knet_handle_t knet_h, knet_node_id_t node_id, uint8_t link_id, - unsigned int enabled, unsigned int connected); - -#endif diff --git a/libknet/logging.c b/libknet/logging.c deleted file mode 100644 index 70341d8c..00000000 --- a/libknet/logging.c +++ /dev/null @@ -1,246 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include - -#include "internals.h" -#include "logging.h" - -struct pretty_names { - const char *name; - uint8_t val; -}; - -static struct pretty_names subsystem_names[] = -{ - { "common", KNET_SUB_COMMON }, - { "handle", KNET_SUB_HANDLE }, - { "host", KNET_SUB_HOST }, - { "listener", KNET_SUB_LISTENER }, - { "link", KNET_SUB_LINK }, - { "transport", KNET_SUB_TRANSPORT }, - { "crypto", KNET_SUB_CRYPTO }, - { "filter", KNET_SUB_FILTER }, - { "dstcache", KNET_SUB_DSTCACHE }, - { "heartbeat", KNET_SUB_HEARTBEAT }, - { "pmtud", KNET_SUB_PMTUD }, - { "tx", KNET_SUB_TX }, - { "rx", KNET_SUB_RX }, - { "udp", KNET_SUB_TRANSP_UDP }, - { "sctp", KNET_SUB_TRANSP_SCTP }, - { "nsscrypto", KNET_SUB_NSSCRYPTO }, - { "unknown", KNET_SUB_UNKNOWN } /* unknown MUST always be last in this array */ -}; - -const char *knet_log_get_subsystem_name(uint8_t subsystem) -{ - unsigned int i; - - for (i = 0; i < KNET_MAX_SUBSYSTEMS; i++) { - if (subsystem_names[i].val == KNET_SUB_UNKNOWN) { - break; - } - if (subsystem_names[i].val == subsystem) { - return subsystem_names[i].name; - } - } - return "unknown"; -} - -uint8_t knet_log_get_subsystem_id(const char *name) -{ - unsigned int i; - - for (i = 0; i < KNET_MAX_SUBSYSTEMS; i++) { - if (subsystem_names[i].val == KNET_SUB_UNKNOWN) { - break; - } - if (strcasecmp(name, subsystem_names[i].name) == 0) { - return subsystem_names[i].val; - } - } - return KNET_SUB_UNKNOWN; -} - -static int is_valid_subsystem(uint8_t subsystem) -{ - unsigned int i; - - for (i = 0; i < KNET_MAX_SUBSYSTEMS; i++) { - if ((subsystem != KNET_SUB_UNKNOWN) && - (subsystem_names[i].val == KNET_SUB_UNKNOWN)) { - break; - } - if (subsystem_names[i].val == subsystem) { - return 0; - } - } - return -1; -} - -static struct pretty_names loglevel_names[] = -{ - { "ERROR", KNET_LOG_ERR }, - { "WARNING", KNET_LOG_WARN }, - { "info", KNET_LOG_INFO }, - { "debug", KNET_LOG_DEBUG } -}; - -const char *knet_log_get_loglevel_name(uint8_t level) -{ - unsigned int i; - - for (i = 0; i <= KNET_LOG_DEBUG; i++) { - if (loglevel_names[i].val == level) { - return loglevel_names[i].name; - } - } - return "ERROR"; -} - -uint8_t knet_log_get_loglevel_id(const char *name) -{ - unsigned int i; - - for (i = 0; i <= KNET_LOG_DEBUG; i++) { - if (strcasecmp(name, loglevel_names[i].name) == 0) { - return loglevel_names[i].val; - } - } - return KNET_LOG_ERR; -} - -int knet_log_set_loglevel(knet_handle_t knet_h, uint8_t subsystem, - uint8_t level) -{ - int savederrno = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (is_valid_subsystem(subsystem) < 0) { - errno = EINVAL; - return -1; - } - - if (level > KNET_LOG_DEBUG) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, subsystem, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - knet_h->log_levels[subsystem] = level; - - pthread_rwlock_unlock(&knet_h->global_rwlock); - return 0; -} - -int knet_log_get_loglevel(knet_handle_t knet_h, uint8_t subsystem, - uint8_t *level) -{ - int savederrno = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (is_valid_subsystem(subsystem) < 0) { - errno = EINVAL; - return -1; - } - - if (!level) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, subsystem, "Unable to get write lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - *level = knet_h->log_levels[subsystem]; - - pthread_rwlock_unlock(&knet_h->global_rwlock); - return 0; -} - -void log_msg(knet_handle_t knet_h, uint8_t subsystem, uint8_t msglevel, - const char *fmt, ...) -{ - va_list ap; - struct knet_log_msg msg; - size_t byte_cnt = 0; - int len, err; - - if ((!knet_h) || - (subsystem == KNET_MAX_SUBSYSTEMS) || - (msglevel > knet_h->log_levels[subsystem])) - return; - - /* - * most logging calls will take place with locking in place. - * if we get an EINVAL and locking is initialized, then - * we are getting a real error and we need to stop - */ - err = pthread_rwlock_tryrdlock(&knet_h->global_rwlock); - if ((err == EAGAIN) && (knet_h->lock_init_done)) - return; - - if (knet_h->logfd <= 0) - goto out_unlock; - - memset(&msg, 0, sizeof(struct knet_log_msg)); - msg.subsystem = subsystem; - msg.msglevel = msglevel; - - va_start(ap, fmt); - vsnprintf(msg.msg, sizeof(msg.msg) - 2, fmt, ap); - va_end(ap); - - len = strlen(msg.msg); - msg.msg[len+1] = '\n'; - - while (byte_cnt < sizeof(struct knet_log_msg)) { - len = write(knet_h->logfd, &msg, sizeof(struct knet_log_msg) - byte_cnt); - if (len <= 0) - return; - - byte_cnt += len; - } - -out_unlock: - /* - * unlock only if we are holding the lock - */ - if (!err) - pthread_rwlock_unlock(&knet_h->global_rwlock); - - return; -} diff --git a/libknet/logging.h b/libknet/logging.h deleted file mode 100644 index c5cd99e2..00000000 --- a/libknet/logging.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_LOGGING_H__ -#define __KNET_LOGGING_H__ - -#include "internals.h" - -void log_msg(knet_handle_t knet_h, uint8_t subsystem, uint8_t msglevel, - const char *fmt, ...) __attribute__((format(printf, 4, 5)));; - -#define log_err(knet_h, subsys, fmt, args...) \ - log_msg(knet_h, subsys, KNET_LOG_ERR, fmt, ##args) - -#define log_warn(knet_h, subsys, fmt, args...) \ - log_msg(knet_h, subsys, KNET_LOG_WARN, fmt, ##args) - -#define log_info(knet_h, subsys, fmt, args...) \ - log_msg(knet_h, subsys, KNET_LOG_INFO, fmt, ##args) - -#define log_debug(knet_h, subsys, fmt, args...) \ - log_msg(knet_h, subsys, KNET_LOG_DEBUG, fmt, ##args) - -#endif diff --git a/libknet/netutils.c b/libknet/netutils.c deleted file mode 100644 index ae9a3b10..00000000 --- a/libknet/netutils.c +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include - -#include "internals.h" -#include "netutils.h" - -static int is_v4_mapped(const struct sockaddr_storage *ss, socklen_t salen) -{ - char map[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff }; - struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) ss; - return memcmp(&addr6->sin6_addr, map, 12); -} - -int cmpaddr(const struct sockaddr_storage *ss1, socklen_t sslen1, - const struct sockaddr_storage *ss2, socklen_t sslen2) -{ - int ss1_offset = 0, ss2_offset = 0; - struct sockaddr_in6 *ss1_addr6 = (struct sockaddr_in6 *)ss1; - struct sockaddr_in6 *ss2_addr6 = (struct sockaddr_in6 *)ss2; - struct sockaddr_in *ss1_addr = (struct sockaddr_in *)ss1; - struct sockaddr_in *ss2_addr = (struct sockaddr_in *)ss2; - char *addr1, *addr2; - - if (ss1->ss_family == ss2->ss_family) { - return memcmp(ss1, ss2, sslen1); - } - - if (ss1->ss_family == AF_INET6) { - if (is_v4_mapped(ss1, sslen1)) { - return 1; - } - addr1 = (char *)&ss1_addr6->sin6_addr; - ss1_offset = 12; - } else { - addr1 = (char *)&ss1_addr->sin_addr; - } - - if (ss2->ss_family == AF_INET6) { - if (is_v4_mapped(ss2, sslen2)) { - return 1; - } - addr2 = (char *)&ss2_addr6->sin6_addr; - ss2_offset = 12; - } else { - addr2 = (char *)&ss2_addr->sin_addr; - } - - return memcmp(addr1+ss1_offset, addr2+ss2_offset, 4); -} - -int cpyaddrport(struct sockaddr_storage *dst, const struct sockaddr_storage *src) -{ - struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *)dst; - struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *)src; - - memset(dst, 0, sizeof(struct sockaddr_storage)); - - if (src->ss_family == AF_INET6) { - dst->ss_family = src->ss_family; - memmove(&dst_addr6->sin6_port, &src_addr6->sin6_port, sizeof(in_port_t)); - memmove(&dst_addr6->sin6_addr, &src_addr6->sin6_addr, sizeof(struct in6_addr)); - } else { - memmove(dst, src, sizeof(struct sockaddr_in)); - } - return 0; -} - -socklen_t sockaddr_len(const struct sockaddr_storage *ss) -{ - if (ss->ss_family == AF_INET) { - return sizeof(struct sockaddr_in); - } else { - return sizeof(struct sockaddr_in6); - } -} - -/* - * exported APIs - */ - -int knet_strtoaddr(const char *host, const char *port, struct sockaddr_storage *ss, socklen_t sslen) -{ - int err; - struct addrinfo hints; - struct addrinfo *result = NULL; - - if (!host) { - errno = EINVAL; - return -1; - } - - if (!port) { - errno = EINVAL; - return -1; - } - - if (!ss) { - errno = EINVAL; - return -1; - } - - if (!sslen) { - errno = EINVAL; - return -1; - } - - memset(&hints, 0, sizeof(struct addrinfo)); - - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_DGRAM; - hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV; - - err = getaddrinfo(host, port, &hints, &result); - - if (!err) { - memmove(ss, result->ai_addr, - (sslen < result->ai_addrlen) ? sslen : result->ai_addrlen); - - freeaddrinfo(result); - } - - return err; -} - -int knet_addrtostr(const struct sockaddr_storage *ss, socklen_t sslen, - char *addr_buf, size_t addr_buf_size, - char *port_buf, size_t port_buf_size) -{ - if (!ss) { - errno = EINVAL; - return -1; - } - - if (!sslen) { - errno = EINVAL; - return -1; - } - - if (!addr_buf) { - errno = EINVAL; - return -1; - } - - if (!port_buf) { - errno = EINVAL; - return -1; - } - - return getnameinfo((struct sockaddr *)ss, sockaddr_len(ss), addr_buf, addr_buf_size, - port_buf, port_buf_size, - NI_NUMERICHOST | NI_NUMERICSERV); -} diff --git a/libknet/netutils.h b/libknet/netutils.h deleted file mode 100644 index 7879d0b9..00000000 --- a/libknet/netutils.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_NETUTILS_H__ -#define __KNET_NETUTILS_H__ - -#include - -int cmpaddr(const struct sockaddr_storage *ss1, socklen_t sslen1, const struct sockaddr_storage *ss2, socklen_t sslen2); -int cpyaddrport(struct sockaddr_storage *dst, const struct sockaddr_storage *src); - -socklen_t knet_sockaddr_len(const struct sockaddr_storage *ss); -#endif diff --git a/libknet/nsscrypto.c b/libknet/nsscrypto.c deleted file mode 100644 index 35326327..00000000 --- a/libknet/nsscrypto.c +++ /dev/null @@ -1,714 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "crypto.h" -#include "nsscrypto.h" -#include "logging.h" - -static pthread_mutex_t nssdbinit_mutex = PTHREAD_MUTEX_INITIALIZER; -static int nssdbinit_done = 0; - -/* - * crypto definitions and conversion tables - */ - -#define SALT_SIZE 16 - -/* - * This are defined in new NSS. For older one, we will define our own - */ -#ifndef AES_256_KEY_LENGTH -#define AES_256_KEY_LENGTH 32 -#endif - -#ifndef AES_192_KEY_LENGTH -#define AES_192_KEY_LENGTH 24 -#endif - -#ifndef AES_128_KEY_LENGTH -#define AES_128_KEY_LENGTH 16 -#endif - -enum crypto_crypt_t { - CRYPTO_CIPHER_TYPE_NONE = 0, - CRYPTO_CIPHER_TYPE_AES256 = 1, - CRYPTO_CIPHER_TYPE_AES192 = 2, - CRYPTO_CIPHER_TYPE_AES128 = 3, - CRYPTO_CIPHER_TYPE_3DES = 4 -}; - -CK_MECHANISM_TYPE cipher_to_nss[] = { - 0, /* CRYPTO_CIPHER_TYPE_NONE */ - CKM_AES_CBC_PAD, /* CRYPTO_CIPHER_TYPE_AES256 */ - CKM_AES_CBC_PAD, /* CRYPTO_CIPHER_TYPE_AES192 */ - CKM_AES_CBC_PAD, /* CRYPTO_CIPHER_TYPE_AES128 */ - CKM_DES3_CBC_PAD /* CRYPTO_CIPHER_TYPE_3DES */ -}; - -size_t cipher_key_len[] = { - 0, /* CRYPTO_CIPHER_TYPE_NONE */ - AES_256_KEY_LENGTH, /* CRYPTO_CIPHER_TYPE_AES256 */ - AES_192_KEY_LENGTH, /* CRYPTO_CIPHER_TYPE_AES192 */ - AES_128_KEY_LENGTH, /* CRYPTO_CIPHER_TYPE_AES128 */ - 24 /* CRYPTO_CIPHER_TYPE_3DES */ -}; - -size_t cypher_block_len[] = { - 0, /* CRYPTO_CIPHER_TYPE_NONE */ - AES_BLOCK_SIZE, /* CRYPTO_CIPHER_TYPE_AES256 */ - AES_BLOCK_SIZE, /* CRYPTO_CIPHER_TYPE_AES192 */ - AES_BLOCK_SIZE, /* CRYPTO_CIPHER_TYPE_AES128 */ - 0 /* CRYPTO_CIPHER_TYPE_3DES */ -}; - -/* - * hash definitions and conversion tables - */ - -enum crypto_hash_t { - CRYPTO_HASH_TYPE_NONE = 0, - CRYPTO_HASH_TYPE_MD5 = 1, - CRYPTO_HASH_TYPE_SHA1 = 2, - CRYPTO_HASH_TYPE_SHA256 = 3, - CRYPTO_HASH_TYPE_SHA384 = 4, - CRYPTO_HASH_TYPE_SHA512 = 5 -}; - -CK_MECHANISM_TYPE hash_to_nss[] = { - 0, /* CRYPTO_HASH_TYPE_NONE */ - CKM_MD5_HMAC, /* CRYPTO_HASH_TYPE_MD5 */ - CKM_SHA_1_HMAC, /* CRYPTO_HASH_TYPE_SHA1 */ - CKM_SHA256_HMAC, /* CRYPTO_HASH_TYPE_SHA256 */ - CKM_SHA384_HMAC, /* CRYPTO_HASH_TYPE_SHA384 */ - CKM_SHA512_HMAC /* CRYPTO_HASH_TYPE_SHA512 */ -}; - -size_t hash_len[] = { - 0, /* CRYPTO_HASH_TYPE_NONE */ - MD5_LENGTH, /* CRYPTO_HASH_TYPE_MD5 */ - SHA1_LENGTH, /* CRYPTO_HASH_TYPE_SHA1 */ - SHA256_LENGTH, /* CRYPTO_HASH_TYPE_SHA256 */ - SHA384_LENGTH, /* CRYPTO_HASH_TYPE_SHA384 */ - SHA512_LENGTH /* CRYPTO_HASH_TYPE_SHA512 */ -}; - -struct nsscrypto_instance { - PK11SymKey *nss_sym_key; - PK11SymKey *nss_sym_key_sign; - - unsigned char *private_key; - - unsigned int private_key_len; - - int crypto_cipher_type; - - int crypto_hash_type; -}; - -/* - * crypt/decrypt functions - */ - -static int string_to_crypto_cipher_type(const char* crypto_cipher_type) -{ - if (strcmp(crypto_cipher_type, "none") == 0) { - return CRYPTO_CIPHER_TYPE_NONE; - } else if (strcmp(crypto_cipher_type, "aes256") == 0) { - return CRYPTO_CIPHER_TYPE_AES256; - } else if (strcmp(crypto_cipher_type, "aes192") == 0) { - return CRYPTO_CIPHER_TYPE_AES192; - } else if (strcmp(crypto_cipher_type, "aes128") == 0) { - return CRYPTO_CIPHER_TYPE_AES128; - } else if (strcmp(crypto_cipher_type, "3des") == 0) { - return CRYPTO_CIPHER_TYPE_3DES; - } - return -1; -} - -static int init_nss_crypto(knet_handle_t knet_h) -{ - PK11SlotInfo* crypt_slot = NULL; - SECItem crypt_param; - struct nsscrypto_instance *instance = knet_h->crypto_instance->model_instance; - - if (!cipher_to_nss[instance->crypto_cipher_type]) { - return 0; - } - - crypt_param.type = siBuffer; - crypt_param.data = instance->private_key; - crypt_param.len = cipher_key_len[instance->crypto_cipher_type]; - - crypt_slot = PK11_GetBestSlot(cipher_to_nss[instance->crypto_cipher_type], NULL); - if (crypt_slot == NULL) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "Unable to find security slot (err %d)", - PR_GetError()); - return -1; - } - - instance->nss_sym_key = PK11_ImportSymKey(crypt_slot, - cipher_to_nss[instance->crypto_cipher_type], - PK11_OriginUnwrap, CKA_ENCRYPT|CKA_DECRYPT, - &crypt_param, NULL); - if (instance->nss_sym_key == NULL) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "Failure to import key into NSS (err %d)", - PR_GetError()); - return -1; - } - - PK11_FreeSlot(crypt_slot); - - return 0; -} - -static int encrypt_nss( - knet_handle_t knet_h, - const struct iovec *iov, - int iovcnt, - unsigned char *buf_out, - ssize_t *buf_out_len) -{ - struct nsscrypto_instance *instance = knet_h->crypto_instance->model_instance; - PK11Context* crypt_context = NULL; - SECItem crypt_param; - SECItem *nss_sec_param = NULL; - int tmp_outlen = 0, tmp1_outlen = 0; - unsigned int tmp2_outlen = 0; - unsigned char *salt = buf_out; - unsigned char *data = buf_out + SALT_SIZE; - int err = -1; - int i; - - if (PK11_GenerateRandom (salt, SALT_SIZE) != SECSuccess) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "Failure to generate a random number %d", - PR_GetError()); - goto out; - } - - crypt_param.type = siBuffer; - crypt_param.data = salt; - crypt_param.len = SALT_SIZE; - - nss_sec_param = PK11_ParamFromIV (cipher_to_nss[instance->crypto_cipher_type], - &crypt_param); - if (nss_sec_param == NULL) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "Failure to set up PKCS11 param (err %d)", - PR_GetError()); - goto out; - } - - /* - * Create cipher context for encryption - */ - crypt_context = PK11_CreateContextBySymKey (cipher_to_nss[instance->crypto_cipher_type], - CKA_ENCRYPT, - instance->nss_sym_key, - nss_sec_param); - if (!crypt_context) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d)", - (int)cipher_to_nss[instance->crypto_cipher_type], - PR_GetError()); - goto out; - } - - for (i=0; icrypto_cipher_type], - PR_GetError()); - goto out; - } - tmp1_outlen = tmp1_outlen + tmp_outlen; - } - - if (PK11_DigestFinal(crypt_context, data + tmp1_outlen, - &tmp2_outlen, KNET_DATABUFSIZE_CRYPT - tmp1_outlen) != SECSuccess) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_DigestFinal failed (encrypt) crypt_type=%d (err %d)", - (int)cipher_to_nss[instance->crypto_cipher_type], - PR_GetError()); - goto out; - - } - - *buf_out_len = tmp1_outlen + tmp2_outlen + SALT_SIZE; - - err = 0; - -out: - if (crypt_context) { - PK11_DestroyContext(crypt_context, PR_TRUE); - } - if (nss_sec_param) { - SECITEM_FreeItem(nss_sec_param, PR_TRUE); - } - return err; -} - -static int decrypt_nss ( - knet_handle_t knet_h, - const unsigned char *buf_in, - const ssize_t buf_in_len, - unsigned char *buf_out, - ssize_t *buf_out_len) -{ - struct nsscrypto_instance *instance = knet_h->crypto_instance->model_instance; - PK11Context* decrypt_context = NULL; - SECItem decrypt_param; - int tmp1_outlen = 0; - unsigned int tmp2_outlen = 0; - unsigned char *salt = (unsigned char *)buf_in; - unsigned char *data = salt + SALT_SIZE; - int datalen = buf_in_len - SALT_SIZE; - int err = -1; - - /* Create cipher context for decryption */ - decrypt_param.type = siBuffer; - decrypt_param.data = salt; - decrypt_param.len = SALT_SIZE; - - decrypt_context = PK11_CreateContextBySymKey(cipher_to_nss[instance->crypto_cipher_type], - CKA_DECRYPT, - instance->nss_sym_key, &decrypt_param); - if (!decrypt_context) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_CreateContext (decrypt) failed (err %d)", - PR_GetError()); - goto out; - } - - if (PK11_CipherOp(decrypt_context, buf_out, &tmp1_outlen, - KNET_DATABUFSIZE_CRYPT, data, datalen) != SECSuccess) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_CipherOp (decrypt) failed (err %d)", - PR_GetError()); - goto out; - } - - if (PK11_DigestFinal(decrypt_context, buf_out + tmp1_outlen, &tmp2_outlen, - KNET_DATABUFSIZE_CRYPT - tmp1_outlen) != SECSuccess) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_DigestFinal (decrypt) failed (err %d)", - PR_GetError()); - goto out; - } - - *buf_out_len = tmp1_outlen + tmp2_outlen; - - err = 0; - -out: - if (decrypt_context) { - PK11_DestroyContext(decrypt_context, PR_TRUE); - } - - return err; -} - - -/* - * hash/hmac/digest functions - */ - -static int string_to_crypto_hash_type(const char* crypto_hash_type) -{ - if (strcmp(crypto_hash_type, "none") == 0) { - return CRYPTO_HASH_TYPE_NONE; - } else if (strcmp(crypto_hash_type, "md5") == 0) { - return CRYPTO_HASH_TYPE_MD5; - } else if (strcmp(crypto_hash_type, "sha1") == 0) { - return CRYPTO_HASH_TYPE_SHA1; - } else if (strcmp(crypto_hash_type, "sha256") == 0) { - return CRYPTO_HASH_TYPE_SHA256; - } else if (strcmp(crypto_hash_type, "sha384") == 0) { - return CRYPTO_HASH_TYPE_SHA384; - } else if (strcmp(crypto_hash_type, "sha512") == 0) { - return CRYPTO_HASH_TYPE_SHA512; - } - - return -1; -} - -static int init_nss_hash(knet_handle_t knet_h) -{ - PK11SlotInfo* hash_slot = NULL; - SECItem hash_param; - struct nsscrypto_instance *instance = knet_h->crypto_instance->model_instance; - - if (!hash_to_nss[instance->crypto_hash_type]) { - return 0; - } - - hash_param.type = siBuffer; - hash_param.data = instance->private_key; - hash_param.len = instance->private_key_len; - - hash_slot = PK11_GetBestSlot(hash_to_nss[instance->crypto_hash_type], NULL); - if (hash_slot == NULL) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "Unable to find security slot (err %d)", - PR_GetError()); - return -1; - } - - instance->nss_sym_key_sign = PK11_ImportSymKey(hash_slot, - hash_to_nss[instance->crypto_hash_type], - PK11_OriginUnwrap, CKA_SIGN, - &hash_param, NULL); - if (instance->nss_sym_key_sign == NULL) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "Failure to import key into NSS (err %d)", - PR_GetError()); - return -1; - } - - PK11_FreeSlot(hash_slot); - - return 0; -} - -static int calculate_nss_hash( - knet_handle_t knet_h, - const unsigned char *buf, - const size_t buf_len, - unsigned char *hash) -{ - struct nsscrypto_instance *instance = knet_h->crypto_instance->model_instance; - PK11Context* hash_context = NULL; - SECItem hash_param; - unsigned int hash_tmp_outlen = 0; - int err = -1; - - /* Now do the digest */ - hash_param.type = siBuffer; - hash_param.data = 0; - hash_param.len = 0; - - hash_context = PK11_CreateContextBySymKey(hash_to_nss[instance->crypto_hash_type], - CKA_SIGN, - instance->nss_sym_key_sign, - &hash_param); - - if (!hash_context) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_CreateContext failed (hash) hash_type=%d (err %d)", - (int)hash_to_nss[instance->crypto_hash_type], - PR_GetError()); - goto out; - } - - if (PK11_DigestBegin(hash_context) != SECSuccess) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_DigestBegin failed (hash) hash_type=%d (err %d)", - (int)hash_to_nss[instance->crypto_hash_type], - PR_GetError()); - goto out; - } - - if (PK11_DigestOp(hash_context, - buf, - buf_len) != SECSuccess) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_DigestOp failed (hash) hash_type=%d (err %d)", - (int)hash_to_nss[instance->crypto_hash_type], - PR_GetError()); - goto out; - } - - if (PK11_DigestFinal(hash_context, - hash, - &hash_tmp_outlen, - hash_len[instance->crypto_hash_type]) != SECSuccess) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_DigestFinale failed (hash) hash_type=%d (err %d)", - (int)hash_to_nss[instance->crypto_hash_type], - PR_GetError()); - goto out; - } - - err = 0; - -out: - if (hash_context) { - PK11_DestroyContext(hash_context, PR_TRUE); - } - - return err; -} - -/* - * global/glue nss functions - */ - -static void nss_atexit_handler(void) -{ - NSS_Shutdown(); - PL_ArenaFinish(); - PR_Cleanup(); -} - -static int init_nss_db(knet_handle_t knet_h) -{ - struct nsscrypto_instance *instance = knet_h->crypto_instance->model_instance; - int err = 0; - - if ((!cipher_to_nss[instance->crypto_cipher_type]) && - (!hash_to_nss[instance->crypto_hash_type])) { - return 0; - } - - err = pthread_mutex_lock(&nssdbinit_mutex); - if (err) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "NSS DB unable to get mutex lock (%d)", err); - return -1; - } - - if (nssdbinit_done) { - err = 0; - goto out_unlock; - } - - PR_Init(PR_USER_THREAD, PR_PRIORITY_URGENT, 0); - - if (NSS_NoDB_Init(".") != SECSuccess) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "NSS DB initialization failed (err %d)", - PR_GetError()); - err = -1; - goto out_unlock; - } - - if (atexit(&nss_atexit_handler) != 0) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "NSS DB unable to register atexit handler"); - err = -1; - goto out_unlock; - } - - nssdbinit_done = 1; - -out_unlock: - pthread_mutex_unlock(&nssdbinit_mutex); - return err; -} - -static int init_nss(knet_handle_t knet_h) -{ - if (init_nss_db(knet_h) < 0) { - return -1; - } - - if (init_nss_crypto(knet_h) < 0) { - return -1; - } - - if (init_nss_hash(knet_h) < 0) { - return -1; - } - - return 0; -} - -/* - * exported API - */ - -int nsscrypto_encrypt_and_sign ( - knet_handle_t knet_h, - const unsigned char *buf_in, - const ssize_t buf_in_len, - unsigned char *buf_out, - ssize_t *buf_out_len) -{ - struct iovec iov_in; - - memset(&iov_in, 0, sizeof(iov_in)); - iov_in.iov_base = (unsigned char *)buf_in; - iov_in.iov_len = buf_in_len; - - return nsscrypto_encrypt_and_signv(knet_h, &iov_in, 1, buf_out, buf_out_len); -} - -int nsscrypto_encrypt_and_signv ( - knet_handle_t knet_h, - const struct iovec *iov_in, - int iovcnt_in, - unsigned char *buf_out, - ssize_t *buf_out_len) -{ - struct nsscrypto_instance *instance = knet_h->crypto_instance->model_instance; - int i; - - if (cipher_to_nss[instance->crypto_cipher_type]) { - if (encrypt_nss(knet_h, iov_in, iovcnt_in, buf_out, buf_out_len) < 0) { - return -1; - } - } else { - *buf_out_len = 0; - for (i=0; icrypto_hash_type]) { - if (calculate_nss_hash(knet_h, buf_out, *buf_out_len, buf_out + *buf_out_len) < 0) { - return -1; - } - *buf_out_len = *buf_out_len + hash_len[instance->crypto_hash_type]; - } - - return 0; -} - -int nsscrypto_authenticate_and_decrypt ( - knet_handle_t knet_h, - const unsigned char *buf_in, - const ssize_t buf_in_len, - unsigned char *buf_out, - ssize_t *buf_out_len) -{ - struct nsscrypto_instance *instance = knet_h->crypto_instance->model_instance; - ssize_t temp_len = buf_in_len; - - if (hash_to_nss[instance->crypto_hash_type]) { - unsigned char tmp_hash[hash_len[instance->crypto_hash_type]]; - ssize_t temp_buf_len = buf_in_len - hash_len[instance->crypto_hash_type]; - - if ((temp_buf_len < 0) || (temp_buf_len > KNET_MAX_PACKET_SIZE)) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "Incorrect packet size."); - return -1; - } - - if (calculate_nss_hash(knet_h, buf_in, temp_buf_len, tmp_hash) < 0) { - return -1; - } - - if (memcmp(tmp_hash, buf_in + temp_buf_len, hash_len[instance->crypto_hash_type]) != 0) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "Digest does not match"); - return -1; - } - - temp_len = temp_len - hash_len[instance->crypto_hash_type]; - *buf_out_len = temp_len; - } - - if (cipher_to_nss[instance->crypto_cipher_type]) { - if (decrypt_nss(knet_h, buf_in, temp_len, buf_out, buf_out_len) < 0) { - return -1; - } - } else { - memmove(buf_out, buf_in, temp_len); - *buf_out_len = temp_len; - } - - return 0; -} - -int nsscrypto_init( - knet_handle_t knet_h, - struct knet_handle_crypto_cfg *knet_handle_crypto_cfg) -{ - struct nsscrypto_instance *nsscrypto_instance = NULL; - - log_debug(knet_h, KNET_SUB_NSSCRYPTO, - "Initizializing nss crypto module [%s/%s]", - knet_handle_crypto_cfg->crypto_cipher_type, - knet_handle_crypto_cfg->crypto_hash_type); - - knet_h->crypto_instance->model_instance = malloc(sizeof(struct nsscrypto_instance)); - if (!knet_h->crypto_instance->model_instance) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "Unable to allocate memory for nss model instance"); - return -1; - } - - nsscrypto_instance = knet_h->crypto_instance->model_instance; - - memset(nsscrypto_instance, 0, sizeof(struct nsscrypto_instance)); - - nsscrypto_instance->crypto_cipher_type = string_to_crypto_cipher_type(knet_handle_crypto_cfg->crypto_cipher_type); - if (nsscrypto_instance->crypto_cipher_type < 0) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "unknown crypto cipher type requested"); - goto out_err; - } - - nsscrypto_instance->crypto_hash_type = string_to_crypto_hash_type(knet_handle_crypto_cfg->crypto_hash_type); - if (nsscrypto_instance->crypto_hash_type < 0) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "unknown crypto hash type requested"); - goto out_err; - } - - if ((nsscrypto_instance->crypto_cipher_type > 0) && - (nsscrypto_instance->crypto_hash_type == 0)) { - log_err(knet_h, KNET_SUB_NSSCRYPTO, "crypto communication requires hash specified"); - goto out_err; - } - - nsscrypto_instance->private_key = knet_handle_crypto_cfg->private_key; - nsscrypto_instance->private_key_len = knet_handle_crypto_cfg->private_key_len; - - if (init_nss(knet_h) < 0) { - goto out_err; - } - - knet_h->sec_header_size = 0; - - if (nsscrypto_instance->crypto_hash_type > 0) { - knet_h->sec_header_size += hash_len[nsscrypto_instance->crypto_hash_type]; - knet_h->sec_hash_size = hash_len[nsscrypto_instance->crypto_hash_type]; - } - - if (nsscrypto_instance->crypto_cipher_type > 0) { - int block_size; - - if (cypher_block_len[nsscrypto_instance->crypto_cipher_type]) { - block_size = cypher_block_len[nsscrypto_instance->crypto_cipher_type]; - } else { - block_size = PK11_GetBlockSize(nsscrypto_instance->crypto_cipher_type, NULL); - if (block_size < 0) { - goto out_err; - } - } - - knet_h->sec_header_size += (block_size * 2); - knet_h->sec_header_size += SALT_SIZE; - knet_h->sec_salt_size = SALT_SIZE; - knet_h->sec_block_size = block_size; - } - - return 0; - -out_err: - nsscrypto_fini(knet_h); - return -1; -} - -void nsscrypto_fini( - knet_handle_t knet_h) -{ - struct nsscrypto_instance *nsscrypto_instance = knet_h->crypto_instance->model_instance; - - if (nsscrypto_instance) { - if (nsscrypto_instance->nss_sym_key) { - PK11_FreeSymKey(nsscrypto_instance->nss_sym_key); - nsscrypto_instance->nss_sym_key = NULL; - } - if (nsscrypto_instance->nss_sym_key_sign) { - PK11_FreeSymKey(nsscrypto_instance->nss_sym_key_sign); - nsscrypto_instance->nss_sym_key_sign = NULL; - } - free(nsscrypto_instance); - knet_h->crypto_instance->model_instance = NULL; - knet_h->sec_header_size = 0; - } - - return; -} diff --git a/libknet/nsscrypto.h b/libknet/nsscrypto.h deleted file mode 100644 index d839f891..00000000 --- a/libknet/nsscrypto.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_NSSCRYPTO_H__ -#define __KNET_NSSCRYPTO_H__ - -#include "internals.h" - -struct nsscrypto_instance; - -int nsscrypto_authenticate_and_decrypt ( - knet_handle_t knet_h, - const unsigned char *buf_in, - const ssize_t buf_in_len, - unsigned char *buf_out, - ssize_t *buf_out_len); - -int nsscrypto_encrypt_and_sign ( - knet_handle_t knet_h, - const unsigned char *buf_in, - const ssize_t buf_in_len, - unsigned char *buf_out, - ssize_t *buf_out_len); - -int nsscrypto_encrypt_and_signv ( - knet_handle_t knet_h, - const struct iovec *iov_in, - int iovcnt_in, - unsigned char *buf_out, - ssize_t *buf_out_len); - -int nsscrypto_init( - knet_handle_t knet_h, - struct knet_handle_crypto_cfg *knet_handle_crypto_cfg); - -void nsscrypto_fini( - knet_handle_t knet_h); - -#endif diff --git a/libknet/onwire.h b/libknet/onwire.h deleted file mode 100644 index 5d51701c..00000000 --- a/libknet/onwire.h +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_ONWIRE_H__ -#define __KNET_ONWIRE_H__ - -/* - * data structures to define network packets. - * Start from knet_header at the bottom - */ - -#include - -#include "libknet.h" - -#if 0 - -/* - * for future protocol extension (re-switching table calculation) - */ - -struct knet_hinfo_link { - uint8_t khl_link_id; - uint8_t khl_link_dynamic; - uint8_t khl_link_priority; - uint64_t khl_link_latency; - char khl_link_dst_ipaddr[KNET_MAX_HOST_LEN]; - char khl_link_dst_port[KNET_MAX_PORT_LEN]; -} __attribute__((packed)); - -struct knet_hinfo_link_table { - knet_node_id_t khlt_node_id; - uint8_t khlt_local; /* we have this node connected locally */ - struct knet_hinfo_link khlt_link[KNET_MAX_LINK]; /* info we send about each link in the node */ -} __attribute__((packed)); - -struct link_table { - knet_node_id_t khdt_host_entries; - uint8_t khdt_host_maps[0]; /* array of knet_hinfo_link_table[khdt_host_entries] */ -} __attribute__((packed)); -#endif - -#define KNET_HOSTINFO_LINK_STATUS_DOWN 0 -#define KNET_HOSTINFO_LINK_STATUS_UP 1 - -struct knet_hostinfo_payload_link_status { - uint8_t khip_link_status_link_id; /* link id */ - uint8_t khip_link_status_status; /* up/down status */ -} __attribute__((packed)); - -/* - * union to reference possible individual payloads - */ - -union knet_hostinfo_payload { - struct knet_hostinfo_payload_link_status knet_hostinfo_payload_link_status; -} __attribute__((packed)); - -/* - * due to the nature of knet_hostinfo, we are currently - * sending those data as part of knet_header_payload_data.khp_data_userdata - * and avoid a union that increses knet_header_payload_data size - * unnecessarely. - * This might change later on depending on how we implement - * host info exchange - */ - -#define KNET_HOSTINFO_TYPE_LINK_UP_DOWN 0 // UNUSED -#define KNET_HOSTINFO_TYPE_LINK_TABLE 1 // NOT IMPLEMENTED - -#define KNET_HOSTINFO_UCAST 0 /* send info to a specific host */ -#define KNET_HOSTINFO_BCAST 1 /* send info to all known / connected hosts */ - -struct knet_hostinfo { - uint8_t khi_type; /* type of hostinfo we are sending */ - uint8_t khi_bcast; /* hostinfo destination bcast/ucast */ - knet_node_id_t khi_dst_node_id;/* used only if in ucast mode */ - union knet_hostinfo_payload khi_payload; -} __attribute__((packed)); - -#define KNET_HOSTINFO_ALL_SIZE sizeof(struct knet_hostinfo) -#define KNET_HOSTINFO_SIZE (KNET_HOSTINFO_ALL_SIZE - sizeof(union knet_hostinfo_payload)) -#define KNET_HOSTINFO_LINK_STATUS_SIZE (KNET_HOSTINFO_SIZE + sizeof(struct knet_hostinfo_payload_link_status)) - -#define khip_link_status_status khi_payload.knet_hostinfo_payload_link_status.khip_link_status_status -#define khip_link_status_link_id khi_payload.knet_hostinfo_payload_link_status.khip_link_status_link_id - -/* - * typedef uint64_t seq_num_t; - * #define SEQ_MAX UINT64_MAX - */ -typedef uint16_t seq_num_t; -#define SEQ_MAX UINT16_MAX - -struct knet_header_payload_data { - seq_num_t khp_data_seq_num; /* pckt seq number used to deduplicate pkcts */ - uint8_t khp_data_pad1; /* make sure to have space in the header to grow features */ - uint8_t khp_data_pad2; - uint8_t khp_data_bcast; /* data destination bcast/ucast */ - uint8_t khp_data_frag_num; /* number of fragments of this pckt. 1 is not fragmented */ - uint8_t khp_data_frag_seq; /* as above, indicates the frag sequence number */ - int8_t khp_data_channel; /* transport channel data for localsock <-> knet <-> localsock mapping */ - uint8_t khp_data_userdata[0]; /* pointer to the real user data */ -} __attribute__((packed)); - -struct knet_header_payload_ping { - uint8_t khp_ping_link; /* source link id */ - uint32_t khp_ping_time[4]; /* ping timestamp */ - seq_num_t khp_ping_seq_num; /* transport host seq_num */ - uint8_t khp_ping_timed; /* timed pinged (1) or forced by seq_num (0) */ -} __attribute__((packed)); - -/* taken from tracepath6 */ -#define KNET_PMTUD_SIZE_V4 65535 -#define KNET_PMTUD_SIZE_V6 KNET_PMTUD_SIZE_V4 - -/* These two get the protocol-specific overheads added to them */ -#define KNET_PMTUD_OVERHEAD_V4 20 -#define KNET_PMTUD_OVERHEAD_V6 40 - -#define KNET_PMTUD_MIN_MTU_V4 576 -#define KNET_PMTUD_MIN_MTU_V6 1280 - -struct knet_header_payload_pmtud { - uint8_t khp_pmtud_link; /* source link id */ - uint16_t khp_pmtud_size; /* size of the current packet */ - uint8_t khp_pmtud_data[0]; /* pointer to empty/random data/fill buffer */ -} __attribute__((packed)); - -/* - * union to reference possible individual payloads - */ - -union knet_header_payload { - struct knet_header_payload_data khp_data; /* pure data packet struct */ - struct knet_header_payload_ping khp_ping; /* heartbeat packet struct */ - struct knet_header_payload_pmtud khp_pmtud; /* Path MTU discovery packet struct */ -} __attribute__((packed)); - -/* - * starting point - */ - -#define KNET_HEADER_VERSION 0x01 /* we currently support only one version */ - -#define KNET_HEADER_TYPE_DATA 0x00 /* pure data packet */ -#define KNET_HEADER_TYPE_HOST_INFO 0x01 /* host status information pckt */ - -#define KNET_HEADER_TYPE_PMSK 0x80 /* packet mask */ -#define KNET_HEADER_TYPE_PING 0x81 /* heartbeat */ -#define KNET_HEADER_TYPE_PONG 0x82 /* reply to heartbeat */ -#define KNET_HEADER_TYPE_PMTUD 0x83 /* Used to determine Path MTU */ -#define KNET_HEADER_TYPE_PMTUD_REPLY 0x84 /* reply from remote host */ - -struct knet_header { - uint8_t kh_version; /* pckt format/version */ - uint8_t kh_type; /* from above defines. Tells what kind of pckt it is */ - knet_node_id_t kh_node; /* host id of the source host for this pckt */ - uint8_t kh_pad1; /* make sure to have space in the header to grow features */ - uint8_t kh_pad2; - union knet_header_payload kh_payload; /* union of potential data struct based on kh_type */ -} __attribute__((packed)); - -/* - * commodoty defines to hide structure nesting - * (needs review and cleanup) - */ - -#define khp_data_seq_num kh_payload.khp_data.khp_data_seq_num -#define khp_data_frag_num kh_payload.khp_data.khp_data_frag_num -#define khp_data_frag_seq kh_payload.khp_data.khp_data_frag_seq -#define khp_data_userdata kh_payload.khp_data.khp_data_userdata -#define khp_data_bcast kh_payload.khp_data.khp_data_bcast -#define khp_data_channel kh_payload.khp_data.khp_data_channel - -#define khp_ping_link kh_payload.khp_ping.khp_ping_link -#define khp_ping_time kh_payload.khp_ping.khp_ping_time -#define khp_ping_seq_num kh_payload.khp_ping.khp_ping_seq_num -#define khp_ping_timed kh_payload.khp_ping.khp_ping_timed - -#define khp_pmtud_link kh_payload.khp_pmtud.khp_pmtud_link -#define khp_pmtud_size kh_payload.khp_pmtud.khp_pmtud_size -#define khp_pmtud_data kh_payload.khp_pmtud.khp_pmtud_data - -/* - * extra defines to avoid mingling with sizeof() too much - */ - -#define KNET_HEADER_ALL_SIZE sizeof(struct knet_header) -#define KNET_HEADER_SIZE (KNET_HEADER_ALL_SIZE - sizeof(union knet_header_payload)) -#define KNET_HEADER_PING_SIZE (KNET_HEADER_SIZE + sizeof(struct knet_header_payload_ping)) -#define KNET_HEADER_PMTUD_SIZE (KNET_HEADER_SIZE + sizeof(struct knet_header_payload_pmtud)) -#define KNET_HEADER_DATA_SIZE (KNET_HEADER_SIZE + sizeof(struct knet_header_payload_data)) - -#endif diff --git a/libknet/tests/Makefile.am b/libknet/tests/Makefile.am deleted file mode 100644 index 906d7b5e..00000000 --- a/libknet/tests/Makefile.am +++ /dev/null @@ -1,89 +0,0 @@ -# -# Copyright (C) 2016 Red Hat, Inc. All rights reserved. -# -# Authors: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -MAINTAINERCLEANFILES = Makefile.in - -include $(top_srcdir)/build-aux/check.mk -include $(top_srcdir)/libknet/tests/api-check.mk - -EXTRA_DIST = \ - api-test-coverage \ - api-check.mk - -AM_CPPFLAGS = -I$(top_srcdir)/libknet - -# override global LIBS that pulls in lots of craft we don't need here -LIBS = $(top_builddir)/libknet/libknet.la - -noinst_HEADERS = \ - test-common.h - -# the order of those tests is NOT random. -# some functions can only be tested properly after some dependents -# API have been validated upfront. - -check_PROGRAMS = \ - $(api_checks) \ - $(int_checks) \ - $(fun_checks) - -int_checks = \ - int_crypto_test \ - int_timediff_test - -fun_checks = - -benchmarks = \ - crypto_bench_test \ - knet_bench_test - -noinst_PROGRAMS = \ - pckt_test \ - $(benchmarks) \ - $(check_PROGRAMS) - -noinst_SCRIPTS = \ - api-test-coverage - -TESTS = $(check_PROGRAMS) - -check-local: check-api-test-coverage - -check-api-test-coverage: - chmod u+x $(top_srcdir)/libknet/tests/api-test-coverage - $(top_srcdir)/libknet/tests/api-test-coverage $(top_srcdir) $(top_builddir) - -pckt_test_SOURCES = pckt_test.c - -int_crypto_test_SOURCES = int_crypto.c \ - ../crypto.c \ - ../nsscrypto.c \ - ../logging.c \ - test-common.c - -int_crypto_test_CFLAGS = $(nss_CFLAGS) - -int_crypto_test_LDFLAGS = $(nss_LIBS) - -int_timediff_test_SOURCES = int_timediff.c - -crypto_bench_test_SOURCES = crypto_bench.c \ - ../crypto.c \ - ../nsscrypto.c \ - ../logging.c \ - test-common.c - -crypto_bench_test_CFLAGS = $(nss_CFLAGS) - -crypto_bench_test_LDFLAGS = $(nss_LIBS) - -knet_bench_test_SOURCES = knet_bench.c \ - test-common.c \ - ../common.c \ - ../logging.c \ - ../transport_common.c diff --git a/libknet/tests/api-check.mk b/libknet/tests/api-check.mk deleted file mode 100644 index 1f064cc2..00000000 --- a/libknet/tests/api-check.mk +++ /dev/null @@ -1,211 +0,0 @@ -# -# Copyright (C) 2016 Red Hat, Inc. All rights reserved. -# -# Authors: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -api_checks = \ - api_knet_handle_new_test \ - api_knet_handle_free_test \ - api_knet_handle_crypto_test \ - api_knet_handle_setfwd_test \ - api_knet_handle_enable_filter_test \ - api_knet_handle_enable_sock_notify_test \ - api_knet_handle_add_datafd_test \ - api_knet_handle_remove_datafd_test \ - api_knet_handle_get_channel_test \ - api_knet_handle_get_datafd_test \ - api_knet_handle_get_transport_list_test \ - api_knet_handle_get_transport_name_by_id_test \ - api_knet_handle_get_transport_id_by_name_test \ - api_knet_recv_test \ - api_knet_send_test \ - api_knet_send_sync_test \ - api_knet_handle_pmtud_setfreq_test \ - api_knet_handle_pmtud_getfreq_test \ - api_knet_handle_enable_pmtud_notify_test \ - api_knet_handle_pmtud_get_test \ - api_knet_host_add_test \ - api_knet_host_remove_test \ - api_knet_host_set_name_test \ - api_knet_host_get_name_by_host_id_test \ - api_knet_host_get_id_by_host_name_test \ - api_knet_host_get_host_list_test \ - api_knet_host_set_policy_test \ - api_knet_host_get_policy_test \ - api_knet_host_get_status_test \ - api_knet_host_enable_status_change_notify_test \ - api_knet_log_get_subsystem_name_test \ - api_knet_log_get_subsystem_id_test \ - api_knet_log_get_loglevel_name_test \ - api_knet_log_get_loglevel_id_test \ - api_knet_log_set_loglevel_test \ - api_knet_log_get_loglevel_test \ - api_knet_strtoaddr_test \ - api_knet_addrtostr_test \ - api_knet_link_set_config_test \ - api_knet_link_clear_config_test \ - api_knet_link_get_config_test \ - api_knet_link_set_ping_timers_test \ - api_knet_link_get_ping_timers_test \ - api_knet_link_set_pong_count_test \ - api_knet_link_get_pong_count_test \ - api_knet_link_set_priority_test \ - api_knet_link_get_priority_test \ - api_knet_link_set_enable_test \ - api_knet_link_get_enable_test \ - api_knet_link_get_link_list_test \ - api_knet_link_get_status_test - -api_knet_handle_new_test_SOURCES = api_knet_handle_new.c \ - test-common.c - -api_knet_handle_free_test_SOURCES = api_knet_handle_free.c \ - test-common.c - -api_knet_handle_crypto_test_SOURCES = api_knet_handle_crypto.c \ - test-common.c - -api_knet_handle_setfwd_test_SOURCES = api_knet_handle_setfwd.c \ - test-common.c - -api_knet_handle_enable_filter_test_SOURCES = api_knet_handle_enable_filter.c \ - test-common.c - -api_knet_handle_enable_sock_notify_test_SOURCES = api_knet_handle_enable_sock_notify.c \ - test-common.c - -api_knet_handle_add_datafd_test_SOURCES = api_knet_handle_add_datafd.c \ - test-common.c - -api_knet_handle_remove_datafd_test_SOURCES = api_knet_handle_remove_datafd.c \ - test-common.c - -api_knet_handle_get_channel_test_SOURCES = api_knet_handle_get_channel.c \ - test-common.c - -api_knet_handle_get_datafd_test_SOURCES = api_knet_handle_get_datafd.c \ - test-common.c - -api_knet_handle_get_transport_list_test_SOURCES = api_knet_handle_get_transport_list.c \ - test-common.c - -api_knet_handle_get_transport_name_by_id_test_SOURCES = api_knet_handle_get_transport_name_by_id.c \ - test-common.c - -api_knet_handle_get_transport_id_by_name_test_SOURCES = api_knet_handle_get_transport_id_by_name.c \ - test-common.c - -api_knet_recv_test_SOURCES = api_knet_recv.c \ - test-common.c - -api_knet_send_test_SOURCES = api_knet_send.c \ - test-common.c - -api_knet_send_sync_test_SOURCES = api_knet_send_sync.c \ - test-common.c - -api_knet_handle_pmtud_setfreq_test_SOURCES = api_knet_handle_pmtud_setfreq.c \ - test-common.c - -api_knet_handle_pmtud_getfreq_test_SOURCES = api_knet_handle_pmtud_getfreq.c \ - test-common.c - -api_knet_handle_enable_pmtud_notify_test_SOURCES = api_knet_handle_enable_pmtud_notify.c \ - test-common.c - -api_knet_handle_pmtud_get_test_SOURCES = api_knet_handle_pmtud_get.c \ - test-common.c - -api_knet_host_add_test_SOURCES = api_knet_host_add.c \ - test-common.c - -api_knet_host_remove_test_SOURCES = api_knet_host_remove.c \ - test-common.c - -api_knet_host_set_name_test_SOURCES = api_knet_host_set_name.c \ - test-common.c - -api_knet_host_get_name_by_host_id_test_SOURCES = api_knet_host_get_name_by_host_id.c \ - test-common.c - -api_knet_host_get_id_by_host_name_test_SOURCES = api_knet_host_get_id_by_host_name.c \ - test-common.c - -api_knet_host_get_host_list_test_SOURCES = api_knet_host_get_host_list.c \ - test-common.c - -api_knet_host_set_policy_test_SOURCES = api_knet_host_set_policy.c \ - test-common.c - -api_knet_host_get_policy_test_SOURCES = api_knet_host_get_policy.c \ - test-common.c - -api_knet_host_get_status_test_SOURCES = api_knet_host_get_status.c \ - test-common.c - -api_knet_host_enable_status_change_notify_test_SOURCES = api_knet_host_enable_status_change_notify.c \ - test-common.c - -api_knet_log_get_subsystem_name_test_SOURCES = api_knet_log_get_subsystem_name.c \ - test-common.c - -api_knet_log_get_subsystem_id_test_SOURCES = api_knet_log_get_subsystem_id.c \ - test-common.c - -api_knet_log_get_loglevel_name_test_SOURCES = api_knet_log_get_loglevel_name.c \ - test-common.c - -api_knet_log_get_loglevel_id_test_SOURCES = api_knet_log_get_loglevel_id.c \ - test-common.c - -api_knet_log_set_loglevel_test_SOURCES = api_knet_log_set_loglevel.c \ - test-common.c - -api_knet_log_get_loglevel_test_SOURCES = api_knet_log_get_loglevel.c \ - test-common.c - -api_knet_strtoaddr_test_SOURCES = api_knet_strtoaddr.c - -api_knet_addrtostr_test_SOURCES = api_knet_addrtostr.c - -api_knet_link_set_config_test_SOURCES = api_knet_link_set_config.c \ - test-common.c - -api_knet_link_clear_config_test_SOURCES = api_knet_link_clear_config.c \ - test-common.c - -api_knet_link_get_config_test_SOURCES = api_knet_link_get_config.c \ - test-common.c - -api_knet_link_set_ping_timers_test_SOURCES = api_knet_link_set_ping_timers.c \ - test-common.c - -api_knet_link_get_ping_timers_test_SOURCES = api_knet_link_get_ping_timers.c \ - test-common.c - -api_knet_link_set_pong_count_test_SOURCES = api_knet_link_set_pong_count.c \ - test-common.c - -api_knet_link_get_pong_count_test_SOURCES = api_knet_link_get_pong_count.c \ - test-common.c - -api_knet_link_set_priority_test_SOURCES = api_knet_link_set_priority.c \ - test-common.c - -api_knet_link_get_priority_test_SOURCES = api_knet_link_get_priority.c \ - test-common.c - -api_knet_link_set_enable_test_SOURCES = api_knet_link_set_enable.c \ - test-common.c - -api_knet_link_get_enable_test_SOURCES = api_knet_link_get_enable.c \ - test-common.c - -api_knet_link_get_link_list_test_SOURCES = api_knet_link_get_link_list.c \ - test-common.c - -api_knet_link_get_status_test_SOURCES = api_knet_link_get_status.c \ - test-common.c diff --git a/libknet/tests/api-test-coverage b/libknet/tests/api-test-coverage deleted file mode 100755 index 9287e7a7..00000000 --- a/libknet/tests/api-test-coverage +++ /dev/null @@ -1,79 +0,0 @@ -#!/bin/bash - -srcdir="$1"/libknet/tests -builddir="$2"/libknet/tests - -headerapicalls="$(grep knet_ "$srcdir"/../libknet.h | grep -v "^ \*" | grep -v ^struct | grep -v "^[[:space:]]" | grep -v typedef | sed -e 's/(.*//g' -e 's/^const //g' -e 's/\*//g' | awk '{print $2}')" - -exportedapicalls="$(nm -B -D "$builddir"/../.libs/libknet.so | grep ' T ' | awk '{print $3}')" - -echo "Checking for exported symbols NOT available in header file" - -for i in $exportedapicalls; do - found=0 - for x in $headerapicalls; do - if [ "$x" = "$i" ]; then - found=1 - break; - fi - done - if [ "$found" = 0 ]; then - echo "Symbol $i not found in header file" - exit 1 - fi -done - -echo "Checking for symbols in header file NOT exported by binary lib" - -for i in $headerapicalls; do - found=0 - for x in $exportedapicalls; do - if [ "$x" = "$i" ]; then - found=1 - break; - fi - done - if [ "$found" = 0 ]; then - echo "Symbol $i not found in binary lib" - exit 1 - fi -done - -echo "Checking for tests with memcheck exceptions" - -for i in $(grep -l is_memcheck "$srcdir"/*.c | grep -v test-common); do - echo "WARNING: $(basename $i) - has memcheck exception enabled" -done - -echo "Checking for tests with helgrind exceptions" - -for i in $(grep -l is_helgrind "$srcdir"/*.c | grep -v test-common); do - echo "WARNING: $(basename $i) has helgrind exception enabled" -done - -echo "Checking for api test coverage" - -numapicalls=0 -found=0 -missing=0 - -for i in $headerapicalls; do - numapicalls=$((numapicalls + 1)) - if [ -f $srcdir/api_${i}.c ]; then - found=$((found + 1)) - else - missing=$((missing + 1)) - echo "MISSING: $i" - fi -done - -echo "Summary" -echo "-------" -echo "Found : $found" -echo "Missing : $missing" -echo "Total : $numapicalls" -which bc > /dev/null 2>&1 && { - coverage=$(echo "scale=3; $found / $numapicalls * 100" | bc -l) - echo "Coverage: $coverage%" -} -exit 0 diff --git a/libknet/tests/api_knet_addrtostr.c b/libknet/tests/api_knet_addrtostr.c deleted file mode 100644 index 3865be18..00000000 --- a/libknet/tests/api_knet_addrtostr.c +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include - -#include "libknet.h" -#include "test-common.h" - -static void test(void) -{ - struct sockaddr_storage addr; - struct sockaddr_in *addrv4; - struct sockaddr_in6 *addrv6; - char addr_str[KNET_MAX_HOST_LEN]; - char port_str[KNET_MAX_PORT_LEN]; - - memset(&addr, 0, sizeof(struct sockaddr_storage)); - - printf("Checking knet_addrtostr with invalid ss\n"); - - if (!knet_addrtostr(NULL, sizeof(struct sockaddr_storage), - addr_str, KNET_MAX_HOST_LEN, - port_str, KNET_MAX_PORT_LEN) && (errno != EINVAL)) { - printf("knet_addrtostr accepted invalid ss\n"); - exit(FAIL); - } - - printf("Checking knet_addrtostr with invalid sslen\n"); - - if (!knet_addrtostr(&addr, 0, - addr_str, KNET_MAX_HOST_LEN, - port_str, KNET_MAX_PORT_LEN) && (errno != EINVAL)) { - printf("knet_addrtostr accepted invalid sslen\n"); - exit(FAIL); - } - - printf("Checking knet_addrtostr with invalid addr_str\n"); - - if (!knet_addrtostr(&addr, sizeof(struct sockaddr_storage), - NULL, KNET_MAX_HOST_LEN, - port_str, KNET_MAX_PORT_LEN) && (errno != EINVAL)) { - printf("knet_addrtostr accepted invalid addr_str\n"); - exit(FAIL); - } - - printf("Checking knet_addrtostr with invalid port_str\n"); - - if (!knet_addrtostr(&addr, sizeof(struct sockaddr_storage), - addr_str, KNET_MAX_HOST_LEN, - NULL, KNET_MAX_PORT_LEN) && (errno != EINVAL)) { - printf("knet_addrtostr accepted invalid addr_str\n"); - exit(FAIL); - } - - addrv4 = (struct sockaddr_in *)&addr; - addrv4->sin_family = AF_INET; - addrv4->sin_addr.s_addr = htonl(0xc0a80001); /* 192.168.0.1 */ - addrv4->sin_port = htons(50000); - - printf("Checking knet_addrtostr with valid data (192.168.0.1:50000)\n"); - - if (knet_addrtostr(&addr, sizeof(struct sockaddr_storage), - addr_str, KNET_MAX_HOST_LEN, - port_str, KNET_MAX_PORT_LEN) < 0) { - printf("Unable to convert 192.168.0.1:50000\n"); - exit(FAIL); - } - - if (strcmp(addr_str, "192.168.0.1") != 0) { - printf("Wrong address conversion: %s\n", addr_str); - exit(EXIT_FAILURE); - } - - if (strcmp(port_str, "50000") != 0) { - printf("Wrong port conversion: %s\n", port_str); - exit(EXIT_FAILURE); - } - - printf("Checking knet_addrtostr with valid data ([fd00::1]:50000)\n"); - - memset(&addr, 0, sizeof(struct sockaddr_storage)); - - addrv6 = (struct sockaddr_in6 *)&addr; - addrv6->sin6_family = AF_INET6; - addrv6->sin6_addr.s6_addr16[0] = htons(0xfd00); /* fd00::1 */ - addrv6->sin6_addr.s6_addr16[7] = htons(0x0001); - addrv6->sin6_port = htons(50000); - - if (knet_addrtostr(&addr, sizeof(struct sockaddr_storage), - addr_str, KNET_MAX_HOST_LEN, - port_str, KNET_MAX_PORT_LEN) < 0) { - printf("Unable to convert [fd00::1]:50000\n"); - exit(FAIL); - } - - if (strcmp(addr_str, "fd00::1") != 0) { - printf("Wrong address conversion: %s\n", addr_str); - exit(FAIL); - } - - if (strcmp(port_str, "50000") != 0) { - printf("Wrong port conversion: %s\n", port_str); - exit(EXIT_FAILURE); - } -} - -int main(int argc, char *argv[]) -{ - - test(); - - exit(PASS); -} diff --git a/libknet/tests/api_knet_handle_add_datafd.c b/libknet/tests/api_knet_handle_add_datafd.c deleted file mode 100644 index 14dd89ab..00000000 --- a/libknet/tests/api_knet_handle_add_datafd.c +++ /dev/null @@ -1,224 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static int private_data; - -static void sock_notify(void *pvt_data, - int datafd, - int8_t channel, - uint8_t tx_rx, - int error, - int errorno) -{ - return; -} - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - int datafd = 0, i; - int8_t channel = 0; - int datafdmax[KNET_DATAFD_MAX]; - int8_t channels[KNET_DATAFD_MAX]; - - printf("Test knet_handle_add_datafd incorrect knet_h\n"); - - if ((!knet_handle_add_datafd(NULL, &datafd, &channel)) || (errno != EINVAL)) { - printf("knet_handle_add_datafd accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_handle_add_datafd with no datafd\n"); - - if ((!knet_handle_add_datafd(knet_h, NULL, &channel)) || (errno != EINVAL)) { - printf("knet_handle_add_datafd accepted invalid datafd or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_add_datafd with no channel\n"); - - if ((!knet_handle_add_datafd(knet_h, &datafd, NULL)) || (errno != EINVAL)) { - printf("knet_handle_add_datafd accepted invalid channel or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_add_datafd with invalid channel\n"); - - channel = KNET_DATAFD_MAX; - - if ((!knet_handle_add_datafd(knet_h, &datafd, &channel)) || (errno != EINVAL)) { - printf("knet_handle_add_datafd accepted invalid channel or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_add_datafd with no socknotify\n"); - - datafd = 0; - channel = -1; - - if ((!knet_handle_add_datafd(knet_h, &datafd, &channel)) || (errno != EINVAL)) { - printf("knet_handle_add_datafd accepted invalid channel or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_add_datafd with automatic config values\n"); - - if (knet_handle_enable_sock_notify(knet_h, &private_data, sock_notify) < 0) { - printf("knet_handle_enable_sock_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - datafd = 0; - channel = -1; - - if (knet_handle_add_datafd(knet_h, &datafd, &channel) < 0) { - printf("knet_handle_add_datafd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("got datafd: %d channel: %d\n", datafd, channel); - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_add_datafd with duplicated datafd\n"); - - if ((!knet_handle_add_datafd(knet_h, &datafd, &channel)) || (errno != EEXIST)) { - printf("knet_handle_add_datafd accepted duplicated datafd or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_add_datafd with busy channel\n"); - - datafd = datafd + 1; - - if ((!knet_handle_add_datafd(knet_h, &datafd, &channel)) || (errno != EBUSY)) { - printf("knet_handle_add_datafd accepted duplicated datafd or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - datafd = datafd - 1; - - if (knet_handle_remove_datafd(knet_h, datafd) < 0) { - printf("knet_handle_remove_datafd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_add_datafd with no available channels\n"); - - for (i = 0; i < KNET_DATAFD_MAX; i++) { - datafdmax[i] = 0; - channels[i] = -1; - if (knet_handle_add_datafd(knet_h, &datafdmax[i], &channels[i]) < 0) { - printf("knet_handle_add_datafd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - } - - datafd = 0; - channel = -1; - - if ((!knet_handle_add_datafd(knet_h, &datafd, &channel)) || (errno != EBUSY)) { - printf("knet_handle_add_datafd accepted entry with no available channels or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - for (i = 0; i < KNET_DATAFD_MAX; i++) { - if (knet_handle_remove_datafd(knet_h, datafdmax[i]) < 0) { - printf("knet_handle_remove_datafd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - } - - flush_logs(logfds[0], stdout); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_crypto.c b/libknet/tests/api_knet_handle_crypto.c deleted file mode 100644 index d725368b..00000000 --- a/libknet/tests/api_knet_handle_crypto.c +++ /dev/null @@ -1,210 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct knet_handle_crypto_cfg knet_handle_crypto_cfg; - - memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); - - printf("Test knet_handle_crypto incorrect knet_h\n"); - - if ((!knet_handle_crypto(NULL, &knet_handle_crypto_cfg)) || (errno != EINVAL)) { - printf("knet_handle_crypto accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_crypto with invalid cfg\n"); - - if ((!knet_handle_crypto(knet_h, NULL)) || (errno != EINVAL)) { - printf("knet_handle_crypto accepted invalid cfg or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_crypto with un-initialized cfg\n"); - - if ((!knet_handle_crypto(knet_h, &knet_handle_crypto_cfg)) || (errno != EINVAL)) { - printf("knet_handle_crypto accepted invalid un-initialized cfg\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_crypto with none crypto model (disable crypto)\n"); - - memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); - strncpy(knet_handle_crypto_cfg.crypto_model, "none", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); - strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); - strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); - - if (knet_handle_crypto(knet_h, &knet_handle_crypto_cfg) != 0) { - printf("knet_handle_crypto did not accept none crypto mode cfg\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_crypto with none crypto cipher and hash (disable crypto)\n"); - - memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); - strncpy(knet_handle_crypto_cfg.crypto_model, "nss", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); - strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "none", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); - strncpy(knet_handle_crypto_cfg.crypto_hash_type, "none", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); - - if (knet_handle_crypto(knet_h, &knet_handle_crypto_cfg) != 0) { - printf("knet_handle_crypto did not accept none crypto cipher and hash cfg\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_crypto with nss/aes128/sha1 and too short key\n"); - - memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); - strncpy(knet_handle_crypto_cfg.crypto_model, "nss", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); - strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); - strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); - knet_handle_crypto_cfg.private_key_len = 10; - - if ((!knet_handle_crypto(knet_h, &knet_handle_crypto_cfg)) || (errno != EINVAL)) { - printf("knet_handle_crypto accepted too short private key\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_crypto with nss/aes128/sha1 and too long key\n"); - - memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); - strncpy(knet_handle_crypto_cfg.crypto_model, "nss", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); - strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); - strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); - knet_handle_crypto_cfg.private_key_len = 10000; - - if ((!knet_handle_crypto(knet_h, &knet_handle_crypto_cfg)) || (errno != EINVAL)) { - printf("knet_handle_crypto accepted too long private key\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_crypto with nss/aes128/sha1 and normal key\n"); - - memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); - strncpy(knet_handle_crypto_cfg.crypto_model, "nss", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); - strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); - strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); - knet_handle_crypto_cfg.private_key_len = 2000; - - if (knet_handle_crypto(knet_h, &knet_handle_crypto_cfg)) { - printf("knet_handle_crypto failed with correct config: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_crypto (nss specific test) with nss/aes128/none and normal key\n"); - - memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); - strncpy(knet_handle_crypto_cfg.crypto_model, "nss", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); - strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); - strncpy(knet_handle_crypto_cfg.crypto_hash_type, "none", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); - knet_handle_crypto_cfg.private_key_len = 2000; - - if (!knet_handle_crypto(knet_h, &knet_handle_crypto_cfg)) { - printf("knet_handle_crypto (nss) accepted crypto without hashing\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Shutdown crypto\n"); - - memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); - strncpy(knet_handle_crypto_cfg.crypto_model, "none", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); - strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "none", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); - strncpy(knet_handle_crypto_cfg.crypto_hash_type, "none", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); - knet_handle_crypto_cfg.private_key_len = 2000; - - if (knet_handle_crypto(knet_h, &knet_handle_crypto_cfg) < 0) { - printf("Unable to shutdown crypto: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_enable_filter.c b/libknet/tests/api_knet_handle_enable_filter.c deleted file mode 100644 index 9f715037..00000000 --- a/libknet/tests/api_knet_handle_enable_filter.c +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static int private_data; - -static int dhost_filter(void *pvt_data, - const unsigned char *outdata, - ssize_t outdata_len, - uint8_t tx_rx, - knet_node_id_t this_host_id, - knet_node_id_t src_host_id, - int8_t *dst_channel, - knet_node_id_t *dst_host_ids, - size_t *dst_host_ids_entries) -{ - return 0; -} - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - - printf("Test knet_handle_enable_filter incorrect knet_h\n"); - - if ((!knet_handle_enable_filter(NULL, NULL, dhost_filter)) || (errno != EINVAL)) { - printf("knet_handle_enable_filter accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_handle_enable_filter with no private_data\n"); - - if (knet_handle_enable_filter(knet_h, NULL, dhost_filter) < 0) { - printf("knet_handle_enable_filter failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->dst_host_filter_fn_private_data != NULL) { - printf("knet_handle_enable_filter failed to unset private_data"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_enable_filter with private_data\n"); - - if (knet_handle_enable_filter(knet_h, &private_data, NULL) < 0) { - printf("knet_handle_enable_filter failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->dst_host_filter_fn_private_data != &private_data) { - printf("knet_handle_enable_filter failed to set private_data"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_enable_filter with no dhost_filter fn\n"); - - if (knet_handle_enable_filter(knet_h, NULL, NULL) < 0) { - printf("knet_handle_enable_filter failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->dst_host_filter_fn != NULL) { - printf("knet_handle_enable_filter failed to unset dhost_filter fn"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_enable_filter with dhost_filter fn\n"); - - if (knet_handle_enable_filter(knet_h, NULL, dhost_filter) < 0) { - printf("knet_handle_enable_filter failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->dst_host_filter_fn != &dhost_filter) { - printf("knet_handle_enable_filter failed to set dhost_filter fn"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_enable_pmtud_notify.c b/libknet/tests/api_knet_handle_enable_pmtud_notify.c deleted file mode 100644 index 1d458b7f..00000000 --- a/libknet/tests/api_knet_handle_enable_pmtud_notify.c +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static int private_data; - -static void pmtud_notify(void *priv_data, - unsigned int data_mtu) -{ - return; -} - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - - printf("Test knet_handle_enable_pmtud_notify incorrect knet_h\n"); - - if ((!knet_handle_enable_pmtud_notify(NULL, NULL, pmtud_notify)) || (errno != EINVAL)) { - printf("knet_handle_enable_pmtud_notify accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_handle_enable_pmtud_notify with no private_data\n"); - - if (knet_handle_enable_pmtud_notify(knet_h, NULL, pmtud_notify) < 0) { - printf("knet_handle_enable_pmtud_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->pmtud_notify_fn_private_data != NULL) { - printf("knet_handle_enable_pmtud_notify failed to unset private_data"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_enable_pmtud_notify with private_data\n"); - - if (knet_handle_enable_pmtud_notify(knet_h, &private_data, NULL) < 0) { - printf("knet_handle_enable_pmtud_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->pmtud_notify_fn_private_data != &private_data) { - printf("knet_handle_enable_pmtud_notify failed to set private_data"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_enable_pmtud_notify with no pmtud_notify fn\n"); - - if (knet_handle_enable_pmtud_notify(knet_h, NULL, NULL) < 0) { - printf("knet_handle_enable_pmtud_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->pmtud_notify_fn != NULL) { - printf("knet_handle_enable_pmtud_notify failed to unset pmtud_notify fn"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_enable_pmtud_notify with pmtud_notify fn\n"); - - if (knet_handle_enable_pmtud_notify(knet_h, NULL, pmtud_notify) < 0) { - printf("knet_handle_enable_pmtud_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->pmtud_notify_fn != &pmtud_notify) { - printf("knet_handle_enable_pmtud_notify failed to set pmtud_notify fn"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_enable_sock_notify.c b/libknet/tests/api_knet_handle_enable_sock_notify.c deleted file mode 100644 index 0c0b2efd..00000000 --- a/libknet/tests/api_knet_handle_enable_sock_notify.c +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static int private_data; - -static void sock_notify(void *pvt_data, - int datafd, - int8_t channel, - uint8_t tx_rx, - int error, - int errorno) -{ - return; -} - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - - printf("Test knet_handle_enable_sock_notify incorrect knet_h\n"); - - if ((!knet_handle_enable_sock_notify(NULL, NULL, sock_notify)) || (errno != EINVAL)) { - printf("knet_handle_enable_sock_notify accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_handle_enable_sock_notify with no private_data\n"); - - if (knet_handle_enable_sock_notify(knet_h, NULL, sock_notify) < 0) { - printf("knet_handle_enable_sock_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->sock_notify_fn_private_data != NULL) { - printf("knet_handle_enable_sock_notify failed to unset private_data"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_enable_sock_notify with private_data\n"); - - if (knet_handle_enable_sock_notify(knet_h, &private_data, sock_notify) < 0) { - printf("knet_handle_enable_sock_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->sock_notify_fn_private_data != &private_data) { - printf("knet_handle_enable_sock_notify failed to set private_data"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_enable_sock_notify with no sock_notify fn\n"); - - if ((!knet_handle_enable_sock_notify(knet_h, NULL, NULL)) || (errno != EINVAL)) { - printf("knet_handle_enable_sock_notify accepted invalid sock_notify or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_enable_sock_notify with sock_notify fn\n"); - - if (knet_handle_enable_sock_notify(knet_h, NULL, sock_notify) < 0) { - printf("knet_handle_enable_sock_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->sock_notify_fn != &sock_notify) { - printf("knet_handle_enable_sock_notify failed to set sock_notify fn"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_free.c b/libknet/tests/api_knet_handle_free.c deleted file mode 100644 index 907dbb9a..00000000 --- a/libknet/tests/api_knet_handle_free.c +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" -#include "internals.h" - -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - - setup_logpipes(logfds); - - printf("Test knet_handle_free with invalid knet_h\n"); - - if ((!knet_handle_free(NULL)) || (errno != EINVAL)) { - printf("knet_handle_free failed to detect invalid parameter\n"); - exit(FAIL); - } - - printf("Test knet_handle_free with one host configured\n"); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add new knet_host: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_handle_free(knet_h)) || (errno != EBUSY)) { - printf("knet_handle_free didn't return error or correct errno with one host configured: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - if (knet_host_remove(knet_h, 1) < 0) { - printf("Unable to remove knet_host: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_handle_free(knet_h) < 0) { - printf("knet_handle_free failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_get_channel.c b/libknet/tests/api_knet_handle_get_channel.c deleted file mode 100644 index 07770d21..00000000 --- a/libknet/tests/api_knet_handle_get_channel.c +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static int private_data; - -static void sock_notify(void *pvt_data, - int datafd, - int8_t channel, - uint8_t tx_rx, - int error, - int errorno) -{ - return; -} - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - int datafd = 0; - int8_t channel = 0, old_channel = 0; - - printf("Test knet_handle_get_channel incorrect knet_h\n"); - - if ((!knet_handle_get_channel(NULL, datafd, &channel)) || (errno != EINVAL)) { - printf("knet_handle_get_channel accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_handle_get_channel with invalid datafd\n"); - - datafd = 0; - - if ((!knet_handle_get_channel(knet_h, datafd, &channel)) || (errno != EINVAL)) { - printf("knet_handle_get_channel accepted invalid datafd or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_get_channel with invalid channel\n"); - - datafd = 10; - - if ((!knet_handle_get_channel(knet_h, datafd, NULL)) || (errno != EINVAL)) { - printf("knet_handle_get_channel accepted invalid channel or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_get_channel with unconfigured datafd/channel\n"); - - datafd = 10; - - if ((!knet_handle_get_channel(knet_h, datafd, &channel)) || (errno != EINVAL)) { - printf("knet_handle_get_channel accepted invalid channel or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_get_channel with valid datafd\n"); - - if (knet_handle_enable_sock_notify(knet_h, &private_data, sock_notify) < 0) { - printf("knet_handle_enable_sock_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - datafd = 0; - old_channel = -1; - - if (knet_handle_add_datafd(knet_h, &datafd, &old_channel) < 0) { - printf("knet_handle_add_datafd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_handle_get_channel(knet_h, datafd, &channel) < 0) { - printf("knet_handle_get_channel failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (old_channel != channel) { - printf("knet_handle_get_channel got incorrect channel\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_get_datafd.c b/libknet/tests/api_knet_handle_get_datafd.c deleted file mode 100644 index 3e536269..00000000 --- a/libknet/tests/api_knet_handle_get_datafd.c +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static int private_data; - -static void sock_notify(void *pvt_data, - int datafd, - int8_t channel, - uint8_t tx_rx, - int error, - int errorno) -{ - return; -} - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - int datafd = 0, old_datafd; - int8_t channel = 0; - - printf("Test knet_handle_get_datafd incorrect knet_h\n"); - - if ((!knet_handle_get_datafd(NULL, channel, &datafd)) || (errno != EINVAL)) { - printf("knet_handle_get_datafd accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_handle_get_datafd with invalid channel (< 0)\n"); - - channel = 0; - - if ((!knet_handle_get_datafd(knet_h, channel, &datafd)) || (errno != EINVAL)) { - printf("knet_handle_get_datafd accepted invalid channel or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_get_datafd with invalid channel (KNET_DATAFD_MAX)\n"); - - channel = KNET_DATAFD_MAX; - - if ((!knet_handle_get_datafd(knet_h, channel, &datafd)) || (errno != EINVAL)) { - printf("knet_handle_get_datafd accepted invalid channel or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_get_datafd with unconfigured datafd/channel\n"); - - channel = 10; - - if ((!knet_handle_get_datafd(knet_h, channel, &datafd)) || (errno != EINVAL)) { - printf("knet_handle_get_datafd accepted unconfigured channel or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_get_datafd with valid datafd\n"); - - if (knet_handle_enable_sock_notify(knet_h, &private_data, sock_notify) < 0) { - printf("knet_handle_enable_sock_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - old_datafd = 0; - channel = -1; - - if (knet_handle_add_datafd(knet_h, &old_datafd, &channel) < 0) { - printf("knet_handle_add_datafd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_handle_get_datafd(knet_h, channel, &datafd) < 0) { - printf("knet_handle_get_datafd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (old_datafd != datafd) { - printf("knet_handle_get_datafd got incorrect channel\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_get_transport_id_by_name.c b/libknet/tests/api_knet_handle_get_transport_id_by_name.c deleted file mode 100644 index 9893fdb6..00000000 --- a/libknet/tests/api_knet_handle_get_transport_id_by_name.c +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - uint8_t id; - - printf("Test knet_handle_get_transport_id_by_name with incorrect knet_h\n"); - - if ((knet_handle_get_transport_id_by_name(NULL, "UDP") != KNET_MAX_TRANSPORTS) || (errno != EINVAL)) { - printf("knet_handle_get_transport_id_by_name accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_handle_get_transport_id_by_name with no name\n"); - - if ((knet_handle_get_transport_id_by_name(knet_h, NULL) != KNET_MAX_TRANSPORTS) || (errno != EINVAL)) { - printf("knet_handle_get_transport_id_by_name accepted invalid transport or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_get_transport_id_by_name with incorrect name\n"); - - if ((knet_handle_get_transport_id_by_name(knet_h, "ARP") != KNET_MAX_TRANSPORTS) || (errno != EINVAL)) { - printf("knet_handle_get_transport_id_by_name accepted invalid transport or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_get_transport_id_by_name with correct values\n"); - - id = knet_handle_get_transport_id_by_name(knet_h, "UDP"); - if (id != KNET_TRANSPORT_UDP) { - printf("knet_handle_get_transport_id_by_name failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_get_transport_list.c b/libknet/tests/api_knet_handle_get_transport_list.c deleted file mode 100644 index 0c95e560..00000000 --- a/libknet/tests/api_knet_handle_get_transport_list.c +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct transport_info transport_list[KNET_MAX_TRANSPORTS]; - size_t transport_list_entries; - int i, expected_count; - - memset(transport_list, 0, sizeof(transport_list)); - - printf("Test knet_handle_get_transport_list with incorrect knet_h\n"); - - if (!knet_handle_get_transport_list(NULL, transport_list, &transport_list_entries) || (errno != EINVAL)) { - printf("knet_handle_get_transport_list accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_handle_get_transport_list with no transport_list\n"); - - if ((!knet_handle_get_transport_list(knet_h, NULL, &transport_list_entries)) || (errno != EINVAL)) { - printf("knet_handle_get_transport_list accepted invalid datafd or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_get_transport_list with no channel\n"); - - if ((!knet_handle_get_transport_list(knet_h, transport_list, NULL)) || (errno != EINVAL)) { - printf("knet_handle_get_transport_list accepted invalid transport_list_entries or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_get_transport_list with valid data\n"); - - if (knet_handle_get_transport_list(knet_h, transport_list, &transport_list_entries) < 0) { - printf("knet_handle_get_transport_list failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - for (i=0; i - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - const char *name = NULL; - - printf("Test knet_handle_get_transport_name_by_id with incorrect knet_h\n"); - - if ((knet_handle_get_transport_name_by_id(NULL, KNET_TRANSPORT_UDP) != NULL) || (errno != EINVAL)) { - printf("knet_handle_get_transport_name_by_id accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_handle_get_transport_name_by_id with incorrect transport\n"); - - if ((knet_handle_get_transport_name_by_id(knet_h, KNET_MAX_TRANSPORTS) != NULL) || (errno != EINVAL)) { - printf("knet_handle_get_transport_name_by_id accepted invalid transport or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_get_transport_name_by_id with correct values\n"); - - name = knet_handle_get_transport_name_by_id(knet_h, KNET_TRANSPORT_UDP); - if (!name) { - printf("knet_handle_get_transport_name_by_id failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - if (strcmp(name, "UDP")) { - printf("knet_handle_get_transport_name_by_id failed to get UDP transport name\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_new.c b/libknet/tests/api_knet_handle_new.c deleted file mode 100644 index e691eb5f..00000000 --- a/libknet/tests/api_knet_handle_new.c +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include - -#include "libknet.h" -#include "internals.h" - -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - struct rlimit cur; - int logfds[2]; - - printf("Test knet_handle_new hostid 1, no logging\n"); - - knet_h = knet_handle_new(1, 0, 0); - if (!knet_h) { - printf("Unable to init knet_handle! err: %s\n", strerror(errno)); - exit(FAIL); - } - - if (knet_handle_free(knet_h) != 0) { - printf("Unable to free knet_handle\n"); - exit(FAIL); - } - - printf("Test knet_handle_new hostid -1, no logging\n"); - - knet_h = knet_handle_new(-1, 0, 0); - if (!knet_h) { - printf("Unable to init knet_handle! err: %s\n", strerror(errno)); - exit(FAIL); - } - - /* - * -1 == knet_node_id_t 65535 - */ - - if (knet_h->host_id != 65535) { - printf("host_id size might have changed!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - if (knet_handle_free(knet_h) != 0) { - printf("Unable to free knet_handle\n"); - exit(FAIL); - } - - if (getrlimit(RLIMIT_NOFILE, &cur) < 0) { - printf("Unable to get current fd limit: %s\n", strerror(errno)); - exit(SKIP); - } - - /* - * passing a bad fd and it should fail - */ - printf("Test knet_handle_new hostid 1, incorrect log_fd (-1)\n"); - - knet_h = knet_handle_new(1, -1, 0); - - if ((!knet_h) && (errno != EINVAL)) { - printf("knet_handle_new returned incorrect errno on incorrect log_fd\n"); - exit(FAIL); - } - - if (knet_h) { - printf("knet_handle_new accepted an incorrect (-1) log_fd\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - /* - * passing a bad fd and it should fail - */ - printf("Test knet_handle_new hostid 1, incorrect log_fd (max_fd + 1)\n"); - - knet_h = knet_handle_new(1, (int) cur.rlim_max, 0); - - if ((knet_h) || (errno != EINVAL)) { - printf("knet_handle_new accepted an incorrect (max_fd + 1) log_fd or returned incorrect errno on incorrect log_fd: %s\n", strerror(errno)); - knet_handle_free(knet_h); - exit(FAIL); - } - - setup_logpipes(logfds); - - printf("Test knet_handle_new hostid 1, proper log_fd, invalid log level (DEBUG + 1)\n"); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG + 1); - - if ((knet_h) || (errno != EINVAL)) { - printf("knet_handle_new accepted an incorrect log level or returned incorrect errno on incorrect log level: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_handle_new hostid 1, proper log_fd, proper log level (DEBUG)\n"); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_pmtud_get.c b/libknet/tests/api_knet_handle_pmtud_get.c deleted file mode 100644 index b2172400..00000000 --- a/libknet/tests/api_knet_handle_pmtud_get.c +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - unsigned int data_mtu; - - printf("Test knet_handle_pmtud_get incorrect knet_h\n"); - - if ((!knet_handle_pmtud_get(NULL, &data_mtu)) || (errno != EINVAL)) { - printf("knet_handle_pmtud_get accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_pmtud_get with no data_mtu\n"); - if ((!knet_handle_pmtud_get(knet_h, NULL)) || (errno != EINVAL)) { - printf("knet_handle_pmtud_get accepted invalid data_mtu or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - if (knet_handle_pmtud_get(knet_h, &data_mtu) < 0) { - printf("knet_handle_pmtud_get failed error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->data_mtu != data_mtu) { - printf("knet_handle_pmtud_get failed to set the value\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_pmtud_getfreq.c b/libknet/tests/api_knet_handle_pmtud_getfreq.c deleted file mode 100644 index 7c018eb9..00000000 --- a/libknet/tests/api_knet_handle_pmtud_getfreq.c +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - unsigned int interval; - - printf("Test knet_handle_pmtud_getfreq incorrect knet_h\n"); - - if ((!knet_handle_pmtud_getfreq(NULL, &interval)) || (errno != EINVAL)) { - printf("knet_handle_pmtud_getfreq accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_pmtud_getfreq with no interval\n"); - if ((!knet_handle_pmtud_getfreq(knet_h, NULL)) || (errno != EINVAL)) { - printf("knet_handle_pmtud_getfreq accepted invalid interval or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - if (knet_handle_pmtud_getfreq(knet_h, &interval) < 0) { - printf("knet_handle_pmtud_getfreq failed error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->pmtud_interval != interval) { - printf("knet_handle_pmtud_getfreq failed to set the value\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_pmtud_setfreq.c b/libknet/tests/api_knet_handle_pmtud_setfreq.c deleted file mode 100644 index ac3fc86d..00000000 --- a/libknet/tests/api_knet_handle_pmtud_setfreq.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - - printf("Test knet_handle_pmtud_setfreq incorrect knet_h\n"); - - if ((!knet_handle_pmtud_setfreq(NULL, 1)) || (errno != EINVAL)) { - printf("knet_handle_pmtud_setfreq accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_pmtud_setfreq with 0 (incorrect)\n"); - if ((!knet_handle_pmtud_setfreq(NULL, 0)) || (errno != EINVAL)) { - printf("knet_handle_pmtud_setfreq accepted invalid PTMUd freq or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_pmtud_setfreq with 86401 (incorrect)\n"); - if ((!knet_handle_pmtud_setfreq(NULL, 86401)) || (errno != EINVAL)) { - printf("knet_handle_pmtud_setfreq accepted invalid PTMUd freq or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_pmtud_setfreq with 1 (correct)\n"); - if (knet_handle_pmtud_setfreq(knet_h, 1) < 0) { - printf("knet_handle_pmtud_setfreq failed error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->pmtud_interval != 1) { - printf("knet_handle_pmtud_setfreq failed to set the value\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_remove_datafd.c b/libknet/tests/api_knet_handle_remove_datafd.c deleted file mode 100644 index 1461852c..00000000 --- a/libknet/tests/api_knet_handle_remove_datafd.c +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static int private_data; - -static void sock_notify(void *pvt_data, - int datafd, - int8_t channel, - uint8_t tx_rx, - int error, - int errorno) -{ - return; -} - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - int datafd = 0; - int8_t channel = 0; - - printf("Test knet_handle_remove_datafd incorrect knet_h\n"); - - if ((!knet_handle_remove_datafd(NULL, datafd)) || (errno != EINVAL)) { - printf("knet_handle_remove_datafd accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_handle_remove_datafd with no datafd\n"); - - datafd = 0; - - if ((!knet_handle_remove_datafd(knet_h, datafd)) || (errno != EINVAL)) { - printf("knet_handle_remove_datafd accepted invalid datafd or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_remove_datafd with invalid datafd\n"); - - datafd = 10; - - if ((!knet_handle_remove_datafd(knet_h, datafd)) || (errno != EINVAL)) { - printf("knet_handle_remove_datafd accepted invalid channel or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_remove_datafd with valid datafd\n"); - - if (knet_handle_enable_sock_notify(knet_h, &private_data, sock_notify) < 0) { - printf("knet_handle_enable_sock_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - datafd = 0; - channel = -1; - - if (knet_handle_add_datafd(knet_h, &datafd, &channel) < 0) { - printf("knet_handle_add_datafd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_handle_remove_datafd(knet_h, datafd) < 0) { - printf("knet_handle_remove_datafd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_handle_setfwd.c b/libknet/tests/api_knet_handle_setfwd.c deleted file mode 100644 index 438461cc..00000000 --- a/libknet/tests/api_knet_handle_setfwd.c +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" -#include "internals.h" - -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - - printf("Test knet_handle_setfwd with invalid knet_h\n"); - - if ((!knet_handle_setfwd(NULL, 0)) || (errno != EINVAL)) { - printf("knet_handle_setfwd accepted invalid knet_h parameter\n"); - exit(FAIL); - } - - setup_logpipes(logfds); - - printf("Test knet_handle_setfwd with invalid param (2) \n"); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_handle_setfwd(knet_h, 2)) || (errno != EINVAL)) { - printf("knet_handle_setfwd accepted invalid param for enabled: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_setfwd with valid param (1) \n"); - - if (knet_handle_setfwd(knet_h, 1) < 0) { - printf("knet_handle_setfwd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->enabled != 1) { - printf("knet_handle_setfwd failed to set correct value"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_setfwd with valid param (0) \n"); - - if (knet_handle_setfwd(knet_h, 0) < 0) { - printf("knet_handle_setfwd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->enabled != 0) { - printf("knet_handle_setfwd failed to set correct value"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_host_add.c b/libknet/tests/api_knet_host_add.c deleted file mode 100644 index b135ab40..00000000 --- a/libknet/tests/api_knet_host_add.c +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - knet_node_id_t host_ids[KNET_MAX_HOST]; - size_t host_ids_entries; - - printf("Test knet_host_add incorrect knet_h\n"); - - if ((!knet_host_add(NULL, 1)) || (errno != EINVAL)) { - printf("knet_host_add accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_add with hostid 1\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("knet_host_add failed error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test verify host_id 1 is in the host list\n"); - - if (knet_host_get_host_list(knet_h, host_ids, &host_ids_entries) < 0) { - printf("Unable to get host list: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (host_ids_entries != 1) { - printf("Too many hosts?\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (host_ids[0] != 1) { - printf("Unable to find host id 1 in host list\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_add adding host 1 again\n"); - - if ((!knet_host_add(knet_h, 1)) || (errno != EEXIST)) { - printf("knet_host_add accepted duplicated host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_host_remove(knet_h, 1); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_host_enable_status_change_notify.c b/libknet/tests/api_knet_host_enable_status_change_notify.c deleted file mode 100644 index dc0b1be1..00000000 --- a/libknet/tests/api_knet_host_enable_status_change_notify.c +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static int private_data; - -static void host_notify(void *priv_data, - knet_node_id_t host_id, - uint8_t reachable, - uint8_t remote, - uint8_t external) -{ - return; -} - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - - printf("Test knet_host_enable_status_change_notify incorrect knet_h\n"); - - if ((!knet_host_enable_status_change_notify(NULL, NULL, host_notify)) || (errno != EINVAL)) { - printf("knet_host_enable_status_change_notify accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_host_enable_status_change_notify with no private_data\n"); - - if (knet_host_enable_status_change_notify(knet_h, NULL, host_notify) < 0) { - printf("knet_host_enable_status_change_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->host_status_change_notify_fn_private_data != NULL) { - printf("knet_host_enable_status_change_notify failed to unset private_data"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_enable_status_change_notify with private_data\n"); - - if (knet_host_enable_status_change_notify(knet_h, &private_data, NULL) < 0) { - printf("knet_host_enable_status_change_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->host_status_change_notify_fn_private_data != &private_data) { - printf("knet_host_enable_status_change_notify failed to set private_data"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_enable_status_change_notify with no host_notify fn\n"); - - if (knet_host_enable_status_change_notify(knet_h, NULL, NULL) < 0) { - printf("knet_host_enable_status_change_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->host_status_change_notify_fn != NULL) { - printf("knet_host_enable_status_change_notify failed to unset host_notify fn"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_enable_status_change_notify with host_notify fn\n"); - - if (knet_host_enable_status_change_notify(knet_h, NULL, host_notify) < 0) { - printf("knet_host_enable_status_change_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->host_status_change_notify_fn != &host_notify) { - printf("knet_host_enable_status_change_notify failed to set host_notify fn"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_host_get_host_list.c b/libknet/tests/api_knet_host_get_host_list.c deleted file mode 100644 index b644bade..00000000 --- a/libknet/tests/api_knet_host_get_host_list.c +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - knet_node_id_t host_ids[KNET_MAX_HOST]; - size_t host_ids_entries; - - printf("Test knet_host_get_host_list incorrect knet_h\n"); - - if ((!knet_host_get_host_list(NULL, host_ids, &host_ids_entries)) || (errno != EINVAL)) { - printf("knet_host_get_host_list accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_host_list incorrect host_ids\n"); - - if ((!knet_host_get_host_list(knet_h, NULL, &host_ids_entries)) || (errno != EINVAL)) { - printf("knet_host_get_host_list accepted invalid host_ids or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_host_list incorrect host_ids_entries\n"); - - if ((!knet_host_get_host_list(knet_h, host_ids, NULL)) || (errno != EINVAL)) { - printf("knet_host_get_host_list accepted invalid host_ids or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_host_list with one host\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("knet_host_add failed error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_host_get_host_list(knet_h, host_ids, &host_ids_entries) < 0) { - printf("Unable to get host list: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (host_ids_entries != 1) { - printf("Too many hosts?\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (host_ids[0] != 1) { - printf("Unable to find host id 1 in host list\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_host_list with zero hosts\n"); - - if (knet_host_remove(knet_h, 1) < 0) { - printf("knet_host_remove failed error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_host_get_host_list(knet_h, host_ids, &host_ids_entries) < 0) { - printf("Unable to get host list: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (host_ids_entries != 0) { - printf("Too many hosts?\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_host_get_id_by_host_name.c b/libknet/tests/api_knet_host_get_id_by_host_name.c deleted file mode 100644 index fcb88a4a..00000000 --- a/libknet/tests/api_knet_host_get_id_by_host_name.c +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - knet_node_id_t host_id; - - printf("Test knet_host_get_id_by_host_name incorrect knet_h\n"); - - if ((!knet_host_get_id_by_host_name(NULL, "1", &host_id)) || (errno != EINVAL)) { - printf("knet_host_get_id_by_host_name accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_id_by_host_name with incorrect name 1\n"); - - if ((!knet_host_get_id_by_host_name(knet_h, NULL, &host_id)) || (errno != EINVAL)) { - printf("knet_host_get_id_by_host_name accepted invalid name or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_id_by_host_name with incorrect host_id\n"); - - if ((!knet_host_get_id_by_host_name(knet_h, "1", NULL)) || (errno != EINVAL)) { - printf("knet_host_get_id_by_host_name accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_id_by_host_name with incorrect values for name\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("knet_host_add failed error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_host_get_id_by_host_name(knet_h, "test", &host_id)) || (errno != ENOENT)) { - printf("knet_host_get_id_by_host_name returned invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_id_by_host_name with correct values\n"); - - if (knet_host_get_id_by_host_name(knet_h, "1", &host_id) < 0) { - printf("knet_host_get_id_by_host_name could not get id for known name: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_host_remove(knet_h, 1); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_host_get_name_by_host_id.c b/libknet/tests/api_knet_host_get_name_by_host_id.c deleted file mode 100644 index 03c93fa2..00000000 --- a/libknet/tests/api_knet_host_get_name_by_host_id.c +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - char name[KNET_MAX_HOST_LEN]; - - printf("Test knet_host_get_name_by_host_id incorrect knet_h\n"); - - if ((!knet_host_get_name_by_host_id(NULL, 1, name)) || (errno != EINVAL)) { - printf("knet_host_get_name_by_host_id accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_name_by_host_id with incorrect hostid 1\n"); - - if ((!knet_host_get_name_by_host_id(knet_h, 1, name)) || (errno != EINVAL)) { - printf("knet_host_get_name_by_host_id accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_name_by_host_id with incorrect name\n"); - - if ((!knet_host_get_name_by_host_id(knet_h, 1, NULL)) || (errno != EINVAL)) { - printf("knet_host_get_name_by_host_id accepted invalid name or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_name_by_host_id with correct values for hostid 1: "); - - if (knet_host_add(knet_h, 1) < 0) { - printf("knet_host_add failed error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_host_get_name_by_host_id(knet_h, 1, name) < 0) { - printf("knet_host_get_name_by_host_id faild to retrive name: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("%s\n", name); - - flush_logs(logfds[0], stdout); - - knet_host_remove(knet_h, 1); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_host_get_policy.c b/libknet/tests/api_knet_host_get_policy.c deleted file mode 100644 index 6a7f5cda..00000000 --- a/libknet/tests/api_knet_host_get_policy.c +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - uint8_t policy; - - printf("Test knet_host_get_policy incorrect knet_h\n"); - - if ((!knet_host_get_policy(NULL, 1, &policy)) || (errno != EINVAL)) { - printf("knet_host_get_policy accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_policy incorrect host_id\n"); - - if ((!knet_host_get_policy(knet_h, 1, &policy)) || (errno != EINVAL)) { - printf("knet_host_get_policy accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_policy incorrect policy\n"); - - if ((!knet_host_get_policy(knet_h, 1, NULL)) || (errno != EINVAL)) { - printf("knet_host_get_policy accepted invalid policy or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_policy correct policy\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("knet_host_add failed error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_host_set_policy(knet_h, 1, KNET_LINK_POLICY_RR) < 0) { - printf("knet_host_set_policy failed to set RR policy for host 1: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_host_get_policy(knet_h, 1, &policy) < 0) { - printf("knet_host_get_policy failed for host 1: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (policy != KNET_LINK_POLICY_RR) { - printf("knet_host_get_policy policy for host 1 does not appear to be correct\n"); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_host_remove(knet_h, 1); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_host_get_status.c b/libknet/tests/api_knet_host_get_status.c deleted file mode 100644 index f9b8ddd5..00000000 --- a/libknet/tests/api_knet_host_get_status.c +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "host.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct knet_host_status status; - - printf("Test knet_host_get_status incorrect knet_h\n"); - - memset(&status, 0, sizeof(struct knet_host_status)); - - if ((!knet_host_get_status(NULL, 1, &status)) || (errno != EINVAL)) { - printf("knet_host_get_status accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_host_get_status with unconfigured host_id\n"); - - if ((!knet_host_get_status(knet_h, 1, &status)) || (errno != EINVAL)) { - printf("knet_host_get_status accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_status with incorrect status\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("knet_host_add failed error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_host_get_status(knet_h, 1, NULL)) || (errno != EINVAL)) { - printf("knet_host_get_status accepted invalid status or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_get_status with correct values\n"); - - if (knet_host_get_status(knet_h, 1, &status) < 0) { - printf("knet_host_get_status failed: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_host_remove.c b/libknet/tests/api_knet_host_remove.c deleted file mode 100644 index e5798eff..00000000 --- a/libknet/tests/api_knet_host_remove.c +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "netutils.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - knet_node_id_t host_ids[KNET_MAX_HOST]; - size_t host_ids_entries; - struct sockaddr_storage ss; - - printf("Test knet_host_add incorrect knet_h\n"); - - if ((!knet_host_remove(NULL, 1)) || (errno != EINVAL)) { - printf("knet_host_remove accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_remove with unconfigured host_id\n"); - - if ((!knet_host_remove(knet_h, 1)) || (errno != EINVAL)) { - printf("knet_host_remove accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_remove with configured host_id and links\n"); - - memset(&ss, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.0", "50000", &ss, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert str to sockaddr: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &ss, NULL) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_enable(knet_h, 1, 0, 1) < 0) { - printf("Unable to enable link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_host_remove(knet_h, 1)) || (errno != EBUSY)) { - printf("knet_host_remove accepted invalid request to remove host with link enabled or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_enable(knet_h, 1, 0, 0) < 0) { - printf("Unable to disable link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_clear_config(knet_h, 1, 0) < 0) { - printf("Unable to clear link config: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_remove with configured host_id (no links)\n"); - - if (knet_host_remove(knet_h, 1) < 0) { - printf("knet_host_remove didn't remove host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_host_get_host_list(knet_h, host_ids, &host_ids_entries) < 0) { - printf("Unable to get host list: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (host_ids_entries) { - printf("Too many hosts?\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_host_set_name.c b/libknet/tests/api_knet_host_set_name.c deleted file mode 100644 index ffbde350..00000000 --- a/libknet/tests/api_knet_host_set_name.c +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - char longhostname[KNET_MAX_HOST_LEN+2]; - - printf("Test knet_host_set_name incorrect knet_h\n"); - - if ((!knet_host_set_name(NULL, 1, "test")) || (errno != EINVAL)) { - printf("knet_host_set_name accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_set_name with incorrect hostid 1\n"); - - if ((!knet_host_set_name(knet_h, 2, "test")) || (errno != EINVAL)) { - printf("knet_host_set_name accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_set_name with correct values\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("knet_host_add failed error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_host_set_name(knet_h, 1, "test") < 0) { - printf("knet_host_set_name failed: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (strcmp("test", knet_h->host_index[1]->name)) { - printf("knet_host_set_name failed to copy name\n"); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_set_name with correct values (name change)\n"); - - if (knet_host_set_name(knet_h, 1, "tes") < 0) { - printf("knet_host_set_name failed: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (strcmp("tes", knet_h->host_index[1]->name)) { - printf("knet_host_set_name failed to change name\n"); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_set_name with NULL name\n"); - - if ((!knet_host_set_name(knet_h, 1, NULL)) || (errno != EINVAL)) { - printf("knet_host_set_name accepted invalid name or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_set_name with duplicate name\n"); - - if (knet_host_add(knet_h, 2) < 0) { - printf("knet_host_add failed error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_host_set_name(knet_h, 2, "tes")) || (errno != EEXIST)) { - printf("knet_host_set_name accepted duplicated name or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 2); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_host_remove(knet_h, 2); - flush_logs(logfds[0], stdout); - - printf("Test knet_host_set_name with (too) long name\n"); - - memset(longhostname, 'a', sizeof(longhostname)); - longhostname[KNET_MAX_HOST_LEN] = '\0'; - - if ((!knet_host_set_name(knet_h, 1, longhostname)) || (errno != EINVAL)) { - printf("knet_host_set_name accepted invalid (too long) name or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_host_remove(knet_h, 1); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_host_set_policy.c b/libknet/tests/api_knet_host_set_policy.c deleted file mode 100644 index f1d0ccf7..00000000 --- a/libknet/tests/api_knet_host_set_policy.c +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - - printf("Test knet_host_set_policy incorrect knet_h\n"); - - if ((!knet_host_set_policy(NULL, 1, KNET_LINK_POLICY_PASSIVE)) || (errno != EINVAL)) { - printf("knet_host_set_policy accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_set_policy incorrect host_id\n"); - - if ((!knet_host_set_policy(knet_h, 1, KNET_LINK_POLICY_PASSIVE)) || (errno != EINVAL)) { - printf("knet_host_set_policy accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_set_policy incorrect policy\n"); - - if ((!knet_host_set_policy(knet_h, 1, KNET_LINK_POLICY_RR + 1)) || (errno != EINVAL)) { - printf("knet_host_set_policy accepted invalid policy or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_host_set_policy correct policy\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("knet_host_add failed error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_host_set_policy(knet_h, 1, KNET_LINK_POLICY_RR) < 0) { - printf("knet_host_set_policy failed to set RR policy for host 1: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->host_index[1]->link_handler_policy != KNET_LINK_POLICY_RR) { - printf("knet_host_set_policy failed to set RR policy for host 1: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_host_remove(knet_h, 1); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_link_clear_config.c b/libknet/tests/api_knet_link_clear_config.c deleted file mode 100644 index 2e282d3b..00000000 --- a/libknet/tests/api_knet_link_clear_config.c +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "link.h" -#include "netutils.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50001", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_link_clear_config incorrect knet_h\n"); - - if ((!knet_link_clear_config(NULL, 1, 0)) || (errno != EINVAL)) { - printf("knet_link_clear_config accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_clear_config with unconfigured host_id\n"); - - if ((!knet_link_clear_config(knet_h, 1, 0)) || (errno != EINVAL)) { - printf("knet_link_clear_config accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_clear_config with incorrect linkid\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_clear_config(knet_h, 1, KNET_MAX_LINK)) || (errno != EINVAL)) { - printf("knet_link_clear_config accepted invalid linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_clear_config with unconfigured linkid\n"); - - if ((!knet_link_clear_config(knet_h, 1, 0)) || (errno != EINVAL)) { - printf("knet_link_clear_config accepted unconfigured linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_clear_config with enabled linkid\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_enable(knet_h, 1, 0, 1) < 0) { - printf("Unable to enable link: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_clear_config(knet_h, 1, 0) || (errno != EBUSY))) { - printf("knet_link_clear_config accepted invalid enabled link or returned incorrect error: %s\n", strerror(errno)); - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_clear_config with correct data\n"); - - if (knet_link_set_enable(knet_h, 1, 0, 0) < 0) { - printf("Unable to disable link: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_clear_config(knet_h, 1, 0) < 0) { - printf("Unable to clear link config: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_link_get_config.c b/libknet/tests/api_knet_link_get_config.c deleted file mode 100644 index de054970..00000000 --- a/libknet/tests/api_knet_link_get_config.c +++ /dev/null @@ -1,296 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "link.h" -#include "netutils.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst, get_src, get_dst; - uint8_t dynamic = 0, transport = 0; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50001", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_link_get_config incorrect knet_h\n"); - - memset(&get_src, 0, sizeof(struct sockaddr_storage)); - memset(&get_dst, 0, sizeof(struct sockaddr_storage)); - - if ((!knet_link_get_config(NULL, 1, 0, &transport, &get_src, &get_dst, &dynamic)) || (errno != EINVAL)) { - printf("knet_link_get_config accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_get_config with unconfigured host_id\n"); - - memset(&get_src, 0, sizeof(struct sockaddr_storage)); - memset(&get_dst, 0, sizeof(struct sockaddr_storage)); - - if ((!knet_link_get_config(knet_h, 1, 0, &transport, &get_src, &get_dst, &dynamic)) || (errno != EINVAL)) { - printf("knet_link_get_config accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_config with incorrect linkid\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - memset(&get_src, 0, sizeof(struct sockaddr_storage)); - memset(&get_dst, 0, sizeof(struct sockaddr_storage)); - - if ((!knet_link_get_config(knet_h, 1, KNET_MAX_LINK, &transport, &get_src, &get_dst, &dynamic)) || (errno != EINVAL)) { - printf("knet_link_get_config accepted invalid linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_config with incorrect src_addr\n"); - - memset(&get_src, 0, sizeof(struct sockaddr_storage)); - memset(&get_dst, 0, sizeof(struct sockaddr_storage)); - - if ((!knet_link_get_config(knet_h, 1, 0, &transport, NULL, &get_dst, &dynamic)) || (errno != EINVAL)) { - printf("knet_link_get_config accepted invalid src_addr or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_config with incorrect dynamic\n"); - - memset(&get_src, 0, sizeof(struct sockaddr_storage)); - memset(&get_dst, 0, sizeof(struct sockaddr_storage)); - - if ((!knet_link_get_config(knet_h, 1, 0, &transport, &get_src, &get_dst, NULL)) || (errno != EINVAL)) { - printf("knet_link_get_config accepted invalid dynamic or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_config with unconfigured link\n"); - - memset(&get_src, 0, sizeof(struct sockaddr_storage)); - memset(&get_dst, 0, sizeof(struct sockaddr_storage)); - - if ((!knet_link_get_config(knet_h, 1, 0, &transport, &get_src, &get_dst, &dynamic)) || (errno != EINVAL)) { - printf("knet_link_get_config accepted unconfigured link or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_config with incorrect dst_addr\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - memset(&get_src, 0, sizeof(struct sockaddr_storage)); - memset(&get_dst, 0, sizeof(struct sockaddr_storage)); - - if ((!knet_link_get_config(knet_h, 1, 0, &transport, &get_src, NULL, &dynamic)) || (errno != EINVAL)) { - printf("knet_link_get_config accepted invalid dst_addr or returned incorrect error: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (dynamic) { - printf("knet_link_get_config returned invalid dynamic status\n"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_config with correct parameters for static link\n"); - - memset(&get_src, 0, sizeof(struct sockaddr_storage)); - memset(&get_dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_link_get_config(knet_h, 1, 0, &transport, &get_src, &get_dst, &dynamic) < 0) { - printf("knet_link_get_config failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (transport != KNET_TRANSPORT_UDP) { - printf("knet_link_get_config returned incorrect transport: %d\n", transport); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((dynamic) || - (memcmp(&src, &get_src, sizeof(struct sockaddr_storage))) || - (memcmp(&dst, &get_dst, sizeof(struct sockaddr_storage)))) { - printf("knet_link_get_config returned invalid data\n"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_config with correct parameters for dynamic link\n"); - - if (knet_link_clear_config(knet_h, 1, 0) < 0) { - printf("Unable to deconfigure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, NULL) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - memset(&get_src, 0, sizeof(struct sockaddr_storage)); - memset(&get_dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_link_get_config(knet_h, 1, 0, &transport, &get_src, &get_dst, &dynamic) < 0) { - printf("knet_link_get_config failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!dynamic) || - (memcmp(&src, &get_src, sizeof(struct sockaddr_storage)))) { - printf("knet_link_get_config returned invalid data\n"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_get_config NULL transport ptr\n"); - - if ((!knet_link_get_config(knet_h, 1, 0, NULL, &get_src, &get_dst, &dynamic)) || (errno != EINVAL)) { - printf("knet_link_get_config accepted NULL &transport or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_link_get_enable.c b/libknet/tests/api_knet_link_get_enable.c deleted file mode 100644 index 7f40f947..00000000 --- a/libknet/tests/api_knet_link_get_enable.c +++ /dev/null @@ -1,205 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "link.h" -#include "netutils.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst; - unsigned int enabled; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50001", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_link_get_enable incorrect knet_h\n"); - - if ((!knet_link_get_enable(NULL, 1, 0, &enabled)) || (errno != EINVAL)) { - printf("knet_link_get_enable accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_get_enable with unconfigured host_id\n"); - - if ((!knet_link_get_enable(knet_h, 1, 0, &enabled)) || (errno != EINVAL)) { - printf("knet_link_get_enable accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_enable with incorrect linkid\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_get_enable(knet_h, 1, KNET_MAX_LINK, &enabled)) || (errno != EINVAL)) { - printf("knet_link_get_enable accepted invalid linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_enable with unconfigured link\n"); - - if ((!knet_link_get_enable(knet_h, 1, 0, &enabled)) || (errno != EINVAL)) { - printf("knet_link_get_enable accepted unconfigured link or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_enable without enabled\n"); - - if ((!knet_link_get_enable(knet_h, 1, 0, NULL)) || (errno != EINVAL)) { - printf("knet_link_get_enable accepted NULL enabled or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_enable with disabled link\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_get_enable(knet_h, 1, 0, &enabled) < 0) { - printf("knet_link_get_enable failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (enabled) { - printf("knet_link_get_enable returned incorrect value"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_enable with enabled link\n"); - - if (knet_link_set_enable(knet_h, 1, 0, 1) < 0) { - printf("knet_link_get_enable failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_get_enable(knet_h, 1, 0, &enabled) < 0) { - printf("knet_link_get_enable failed: %s\n", strerror(errno)); - knet_link_get_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (!enabled) { - printf("knet_link_get_enable returned incorrect value"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_link_get_link_list.c b/libknet/tests/api_knet_link_get_link_list.c deleted file mode 100644 index f7ec527c..00000000 --- a/libknet/tests/api_knet_link_get_link_list.c +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "link.h" -#include "netutils.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst; - uint8_t link_ids[KNET_MAX_LINK]; - size_t link_ids_entries = 0; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50001", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&link_ids, 1, sizeof(link_ids)); - - printf("Test knet_link_get_link_list incorrect knet_h\n"); - - if ((!knet_link_get_link_list(NULL, 1, link_ids, &link_ids_entries)) || (errno != EINVAL)) { - printf("knet_link_get_link_list accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_get_link_list with unconfigured host_id\n"); - - if ((!knet_link_get_link_list(knet_h, 1, link_ids, &link_ids_entries)) || (errno != EINVAL)) { - printf("knet_link_get_link_list accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_link_list with incorrect link_id\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_get_link_list(knet_h, 1, NULL, &link_ids_entries)) || (errno != EINVAL)) { - printf("knet_link_get_link_list accepted invalid link_ids or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_link_list with incorrect link_ids_entries\n"); - - if ((!knet_link_get_link_list(knet_h, 1, link_ids, NULL)) || (errno != EINVAL)) { - printf("knet_link_get_link_list accepted invalid link_ids_entries or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_link_list with no links\n"); - - if (knet_link_get_link_list(knet_h, 1, link_ids, &link_ids_entries) < 0) { - printf("knet_link_get_link_list failed: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (link_ids_entries != 0) { - printf("knet_link_get_link_list returned incorrect number of links"); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_link_list with 1 link\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_get_link_list(knet_h, 1, link_ids, &link_ids_entries) < 0) { - printf("knet_link_get_link_list failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((link_ids_entries != 1) || (link_ids[0] != 0)) { - printf("knet_link_get_link_list returned incorrect values"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_link_get_ping_timers.c b/libknet/tests/api_knet_link_get_ping_timers.c deleted file mode 100644 index 25f48802..00000000 --- a/libknet/tests/api_knet_link_get_ping_timers.c +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "link.h" -#include "netutils.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst; - time_t interval = 0, timeout = 0; - unsigned int precision = 0; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50001", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_link_get_ping_timers incorrect knet_h\n"); - - if ((!knet_link_get_ping_timers(NULL, 1, 0, &interval, &timeout, &precision)) || (errno != EINVAL)) { - printf("knet_link_get_ping_timers accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_get_ping_timers with unconfigured host_id\n"); - - if ((!knet_link_get_ping_timers(knet_h, 1, 0, &interval, &timeout, &precision)) || (errno != EINVAL)) { - printf("knet_link_get_ping_timers accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_ping_timers with incorrect linkid\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_get_ping_timers(knet_h, 1, KNET_MAX_LINK, &interval, &timeout, &precision)) || (errno != EINVAL)) { - printf("knet_link_get_ping_timers accepted invalid linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_ping_timers with incorrect interval\n"); - - if ((!knet_link_get_ping_timers(knet_h, 1, 0, NULL, &timeout, &precision)) || (errno != EINVAL)) { - printf("knet_link_get_ping_timers accepted invalid interval or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_ping_timers with incorrect timeout\n"); - - if ((!knet_link_get_ping_timers(knet_h, 1, 0, &interval, NULL, &precision)) || (errno != EINVAL)) { - printf("knet_link_get_ping_timers accepted invalid timeout or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_ping_timers with incorrect interval\n"); - - if ((!knet_link_get_ping_timers(knet_h, 1, 0, &interval, &timeout, NULL)) || (errno != EINVAL)) { - printf("knet_link_get_ping_timers accepted invalid interval or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_ping_timers with unconfigured link\n"); - - if ((!knet_link_get_ping_timers(knet_h, 1, 0, &interval, &timeout, &precision)) || (errno != EINVAL)) { - printf("knet_link_get_ping_timers accepted unconfigured link or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_ping_timers with correct values\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_get_ping_timers(knet_h, 1, 0, &interval, &timeout, &precision) < 0) { - printf("knet_link_get_ping_timers failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("DEFAULT: int: %zu timeout: %zu prec: %u\n", interval, timeout, precision); - - if ((interval != KNET_LINK_DEFAULT_PING_INTERVAL) || - (timeout != KNET_LINK_DEFAULT_PING_TIMEOUT) || - (precision != KNET_LINK_DEFAULT_PING_PRECISION)) { - printf("knet_link_get_ping_timers failed to set values\n"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_link_get_pong_count.c b/libknet/tests/api_knet_link_get_pong_count.c deleted file mode 100644 index e19f8ff4..00000000 --- a/libknet/tests/api_knet_link_get_pong_count.c +++ /dev/null @@ -1,177 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "link.h" -#include "netutils.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst; - uint8_t pong_count = 0; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50001", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_link_get_pong_count incorrect knet_h\n"); - - if ((!knet_link_get_pong_count(NULL, 1, 0, &pong_count)) || (errno != EINVAL)) { - printf("knet_link_get_pong_count accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_get_pong_count with unconfigured host_id\n"); - - if ((!knet_link_get_pong_count(knet_h, 1, 0, &pong_count)) || (errno != EINVAL)) { - printf("knet_link_get_pong_count accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_pong_count with incorrect linkid\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_get_pong_count(knet_h, 1, KNET_MAX_LINK, &pong_count)) || (errno != EINVAL)) { - printf("knet_link_get_pong_count accepted invalid linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_pong_count with incorrect pong count\n"); - - if ((!knet_link_get_pong_count(knet_h, 1, 0, NULL)) || (errno != EINVAL)) { - printf("knet_link_get_pong_count accepted invalid pong count or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_pong_count with unconfigured link\n"); - - if ((!knet_link_get_pong_count(knet_h, 1, 0, &pong_count)) || (errno != EINVAL)) { - printf("knet_link_get_pong_count accepted unconfigured link or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_pong_count with correct values\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_pong_count(knet_h, 1, 0, 3) < 0) { - printf("knet_link_set_pong_count failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_get_pong_count(knet_h, 1, 0, &pong_count) < 0) { - printf("knet_link_get_pong_count failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (pong_count != 3) { - printf("knet_link_get_pong_count failed to get correct values\n"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_link_get_priority.c b/libknet/tests/api_knet_link_get_priority.c deleted file mode 100644 index b15e1f87..00000000 --- a/libknet/tests/api_knet_link_get_priority.c +++ /dev/null @@ -1,177 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "link.h" -#include "netutils.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst; - uint8_t priority = 0; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50001", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_link_get_priority incorrect knet_h\n"); - - if ((!knet_link_get_priority(NULL, 1, 0, &priority)) || (errno != EINVAL)) { - printf("knet_link_get_priority accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_get_priority with unconfigured host_id\n"); - - if ((!knet_link_get_priority(knet_h, 1, 0, &priority)) || (errno != EINVAL)) { - printf("knet_link_get_priority accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_priority with incorrect linkid\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_get_priority(knet_h, 1, KNET_MAX_LINK, &priority)) || (errno != EINVAL)) { - printf("knet_link_get_priority accepted invalid linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_priority with unconfigured link\n"); - - if ((!knet_link_get_priority(knet_h, 1, 0, &priority)) || (errno != EINVAL)) { - printf("knet_link_get_priority accepted unconfigured link or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_priority with incorrect priority\n"); - - if ((!knet_link_get_priority(knet_h, 1, 0, NULL)) || (errno != EINVAL)) { - printf("knet_link_get_priority accepted incorrect priority or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_priority with correct values\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_priority(knet_h, 1, 0, 1) < 0) { - printf("knet_link_set_priority failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_get_priority(knet_h, 1, 0, &priority) < 0) { - printf("knet_link_get_priority failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (priority != 1) { - printf("knet_link_get_priority failed to get correct values\n"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_link_get_status.c b/libknet/tests/api_knet_link_get_status.c deleted file mode 100644 index 9d2ff44c..00000000 --- a/libknet/tests/api_knet_link_get_status.c +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "link.h" -#include "netutils.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst; - struct knet_link_status status; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50001", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_link_get_status incorrect knet_h\n"); - - memset(&status, 0, sizeof(struct knet_link_status)); - - if ((!knet_link_get_status(NULL, 1, 0, &status)) || (errno != EINVAL)) { - printf("knet_link_get_status accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_get_status with unconfigured host_id\n"); - - if ((!knet_link_get_status(knet_h, 1, 0, &status)) || (errno != EINVAL)) { - printf("knet_link_get_status accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_status with incorrect linkid\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_get_status(knet_h, 1, KNET_MAX_LINK, &status)) || (errno != EINVAL)) { - printf("knet_link_get_status accepted invalid linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_status with incorrect status\n"); - - if ((!knet_link_get_status(knet_h, 1, 0, NULL)) || (errno != EINVAL)) { - printf("knet_link_get_status accepted invalid status or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_status with unconfigured link\n"); - - if ((!knet_link_get_status(knet_h, 1, 0, &status)) || (errno != EINVAL)) { - printf("knet_link_get_status accepted unconfigured link or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_get_status with correct values\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_get_status(knet_h, 1, 0, &status) < 0) { - printf("knet_link_get_status failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_link_set_config.c b/libknet/tests/api_knet_link_set_config.c deleted file mode 100644 index cf6a393a..00000000 --- a/libknet/tests/api_knet_link_set_config.c +++ /dev/null @@ -1,271 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "link.h" -#include "netutils.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst; - struct knet_link_status link_status; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50001", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_link_set_config incorrect knet_h\n"); - - if ((!knet_link_set_config(NULL, 1, 0, KNET_TRANSPORT_UDP, &src, &dst)) || (errno != EINVAL)) { - printf("knet_link_set_config accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_set_config with unconfigured host_id\n"); - - if ((!knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst)) || (errno != EINVAL)) { - printf("knet_link_set_config accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_set_config with bad transport type\n"); - - if ((!knet_link_set_config(knet_h, 1, 0, KNET_MAX_TRANSPORTS+1, &src, &dst)) || (errno != EINVAL)) { - printf("knet_link_set_config accepted invalid transport or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_config with incorrect linkid\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_set_config(knet_h, 1, KNET_MAX_LINK, KNET_TRANSPORT_UDP, &src, &dst)) || (errno != EINVAL)) { - printf("knet_link_set_config accepted invalid linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_config with incorrect src_addr\n"); - - if ((!knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, NULL, &dst)) || (errno != EINVAL)) { - printf("knet_link_set_config accepted invalid src_addr or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_config with dynamic dst_addr\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, NULL) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_get_status(knet_h, 1, 0, &link_status) < 0) { - printf("Unable to get link status: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((link_status.enabled != 0) || - (strcmp(link_status.src_ipaddr, "127.0.0.1")) || - (strcmp(link_status.src_port, "50000")) || - (knet_h->host_index[1]->link[0].dynamic != KNET_LINK_DYNIP)) { - printf("knet_link_set_config failed to set configuration. enabled: %u src_addr %s src_port %s dynamic %u\n", - link_status.enabled, link_status.src_ipaddr, link_status.src_port, knet_h->host_index[1]->link[0].dynamic); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_config with already configured link\n"); - if ((!knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, NULL) || (errno != EBUSY))) { - printf("knet_link_set_config accepted request while link configured or returned incorrect error: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_set_config with link enabled\n"); - - if (knet_link_set_enable(knet_h, 1, 0, 1) < 0) { - printf("Unable to enable link: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_get_status(knet_h, 1, 0, &link_status) < 0) { - printf("Unable to get link status: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, NULL)) || (errno != EBUSY)) { - printf("knet_link_set_config accepted request while link enabled or returned incorrect error: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_enable(knet_h, 1, 0, 0) < 0) { - printf("Unable to disable link: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_clear_config(knet_h, 1, 0) < 0) { - printf("Unable to clear link config: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_set_config with static dst_addr\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_get_status(knet_h, 1, 0, &link_status) < 0) { - printf("Unable to get link status: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((link_status.enabled != 0) || - (strcmp(link_status.src_ipaddr, "127.0.0.1")) || - (strcmp(link_status.src_port, "50000")) || - (strcmp(link_status.dst_ipaddr, "127.0.0.1")) || - (strcmp(link_status.dst_port, "50001")) || - (knet_h->host_index[1]->link[0].dynamic != KNET_LINK_STATIC)) { - printf("knet_link_set_config failed to set configuration. enabled: %u src_addr %s src_port %s dst_addr %s dst_port %s dynamic %u\n", - link_status.enabled, link_status.src_ipaddr, link_status.src_port, link_status.dst_ipaddr, link_status.dst_port, knet_h->host_index[1]->link[0].dynamic); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_link_set_enable.c b/libknet/tests/api_knet_link_set_enable.c deleted file mode 100644 index 7858f39c..00000000 --- a/libknet/tests/api_knet_link_set_enable.c +++ /dev/null @@ -1,368 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "link.h" -#include "netutils.h" -#include "test-common.h" - -static void test_udp(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50001", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_link_set_enable incorrect knet_h\n"); - - if ((!knet_link_set_enable(NULL, 1, 0, 1)) || (errno != EINVAL)) { - printf("knet_link_set_enable accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_set_enable with unconfigured host_id\n"); - - if ((!knet_link_set_enable(knet_h, 1, 0, 1)) || (errno != EINVAL)) { - printf("knet_link_set_enable accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_enable with incorrect linkid\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_set_enable(knet_h, 1, KNET_MAX_LINK, 1)) || (errno != EINVAL)) { - printf("knet_link_set_enable accepted invalid linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_enable with unconfigured link\n"); - - if ((!knet_link_set_enable(knet_h, 1, 0, 1)) || (errno != EINVAL)) { - printf("knet_link_set_enable accepted unconfigured link or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_enable with incorrect values\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_set_enable(knet_h, 1, 0, 2)) || (errno != EINVAL)) { - printf("knet_link_set_enable accepted incorrect value for enabled or returned incorrect error: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_enable with correct values (1)\n"); - - if (knet_link_set_enable(knet_h, 1, 0, 1) < 0) { - printf("knet_link_set_enable failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->host_index[1]->link[0].status.enabled != 1) { - printf("knet_link_set_enable failed to set correct values\n"); - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_set_enable with correct values (0)\n"); - - if (knet_link_set_enable(knet_h, 1, 0, 0) < 0) { - printf("knet_link_set_enable failed: %s\n", strerror(errno)); - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->host_index[1]->link[0].status.enabled != 0) { - printf("knet_link_set_enable failed to set correct values\n"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -#ifdef HAVE_NETINET_SCTP_H -static void test_sctp(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_link_set_enable incorrect knet_h\n"); - - if ((!knet_link_set_enable(NULL, 1, 0, 1)) || (errno != EINVAL)) { - printf("knet_link_set_enable accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_set_enable with unconfigured host_id\n"); - - if ((!knet_link_set_enable(knet_h, 1, 0, 1)) || (errno != EINVAL)) { - printf("knet_link_set_enable accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_enable with incorrect linkid\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_set_enable(knet_h, 1, KNET_MAX_LINK, 1)) || (errno != EINVAL)) { - printf("knet_link_set_enable accepted invalid linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_enable with unconfigured link\n"); - - if ((!knet_link_set_enable(knet_h, 1, 0, 1)) || (errno != EINVAL)) { - printf("knet_link_set_enable accepted unconfigured link or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_enable with incorrect values\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_SCTP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_set_enable(knet_h, 1, 0, 2)) || (errno != EINVAL)) { - printf("knet_link_set_enable accepted incorrect value for enabled or returned incorrect error: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_enable with correct values (1)\n"); - - if (knet_link_set_enable(knet_h, 1, 0, 1) < 0) { - printf("knet_link_set_enable failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->host_index[1]->link[0].status.enabled != 1) { - printf("knet_link_set_enable failed to set correct values\n"); - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Wait 2 seconds for sockets to connect\n"); - sleep(2); - - printf("Test knet_link_set_enable with correct values (0)\n"); - - if (knet_link_set_enable(knet_h, 1, 0, 0) < 0) { - printf("knet_link_set_enable failed: %s\n", strerror(errno)); - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->host_index[1]->link[0].status.enabled != 0) { - printf("knet_link_set_enable failed to set correct values\n"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} -#endif - -int main(int argc, char *argv[]) -{ - need_root(); - - printf("Testing with UDP\n"); - - test_udp(); - -#ifdef HAVE_NETINET_SCTP_H - printf("Testing with SCTP\n"); - - test_sctp(); -#else - printf("Skipping SCTP test. Protocol not supported in this build\n"); -#endif - - return PASS; -} diff --git a/libknet/tests/api_knet_link_set_ping_timers.c b/libknet/tests/api_knet_link_set_ping_timers.c deleted file mode 100644 index db911ac5..00000000 --- a/libknet/tests/api_knet_link_set_ping_timers.c +++ /dev/null @@ -1,196 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "link.h" -#include "netutils.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50001", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_link_set_ping_timers incorrect knet_h\n"); - - if ((!knet_link_set_ping_timers(NULL, 1, 0, 1000, 2000, 2048)) || (errno != EINVAL)) { - printf("knet_link_set_ping_timers accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_set_ping_timers with unconfigured host_id\n"); - - if ((!knet_link_set_ping_timers(knet_h, 1, 0, 1000, 2000, 2048)) || (errno != EINVAL)) { - printf("knet_link_set_ping_timers accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_ping_timers with incorrect linkid\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_set_ping_timers(knet_h, 1, KNET_MAX_LINK, 1000, 2000, 2048)) || (errno != EINVAL)) { - printf("knet_link_set_ping_timers accepted invalid linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_ping_timers with incorrect interval\n"); - - if ((!knet_link_set_ping_timers(knet_h, 1, 0, 0, 2000, 2048)) || (errno != EINVAL)) { - printf("knet_link_set_ping_timers accepted invalid interval or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_ping_timers with incorrect timeout\n"); - - if ((!knet_link_set_ping_timers(knet_h, 1, 0, 1000, 0, 2048)) || (errno != EINVAL)) { - printf("knet_link_set_ping_timers accepted invalid timeout or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_ping_timers with incorrect interval\n"); - - if ((!knet_link_set_ping_timers(knet_h, 1, 0, 1000, 2000, 0)) || (errno != EINVAL)) { - printf("knet_link_set_ping_timers accepted invalid interval or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_ping_timers with unconfigured link\n"); - - if ((!knet_link_set_ping_timers(knet_h, 1, 0, 1000, 2000, 2048)) || (errno != EINVAL)) { - printf("knet_link_set_ping_timers accepted unconfigured link or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_ping_timers with correct values\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_ping_timers(knet_h, 1, 0, 1000, 2000, 2048) < 0) { - printf("knet_link_set_ping_timers failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((knet_h->host_index[1]->link[0].ping_interval != 1000000) || - (knet_h->host_index[1]->link[0].pong_timeout != 2000000) || - (knet_h->host_index[1]->link[0].latency_fix != 2048)) { - printf("knet_link_set_ping_timers failed to set values\n"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_link_set_pong_count.c b/libknet/tests/api_knet_link_set_pong_count.c deleted file mode 100644 index 7b1f8f75..00000000 --- a/libknet/tests/api_knet_link_set_pong_count.c +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "link.h" -#include "netutils.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50001", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_link_set_pong_count incorrect knet_h\n"); - - if ((!knet_link_set_pong_count(NULL, 1, 0, 2)) || (errno != EINVAL)) { - printf("knet_link_set_pong_count accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_set_pong_count with unconfigured host_id\n"); - - if ((!knet_link_set_pong_count(knet_h, 1, 0, 2)) || (errno != EINVAL)) { - printf("knet_link_set_pong_count accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_pong_count with incorrect linkid\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_set_pong_count(knet_h, 1, KNET_MAX_LINK, 2)) || (errno != EINVAL)) { - printf("knet_link_set_pong_count accepted invalid linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_pong_count with incorrect pong count\n"); - - if ((!knet_link_set_pong_count(knet_h, 1, 0, 0)) || (errno != EINVAL)) { - printf("knet_link_set_pong_count accepted invalid pong count or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_pong_count with unconfigured link\n"); - - if ((!knet_link_set_pong_count(knet_h, 1, 0, 2)) || (errno != EINVAL)) { - printf("knet_link_set_pong_count accepted unconfigured link or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_pong_count with correct values\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_pong_count(knet_h, 1, 0, 3) < 0) { - printf("knet_link_set_pong_count failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->host_index[1]->link[0].pong_count != 3) { - printf("knet_link_set_pong_count failed to set correct values\n"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_link_set_priority.c b/libknet/tests/api_knet_link_set_priority.c deleted file mode 100644 index 2096cf79..00000000 --- a/libknet/tests/api_knet_link_set_priority.c +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "link.h" -#include "netutils.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct sockaddr_storage src, dst; - - memset(&src, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &src, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(&dst, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50001", &dst, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_link_set_priority incorrect knet_h\n"); - - if ((!knet_link_set_priority(NULL, 1, 0, 1)) || (errno != EINVAL)) { - printf("knet_link_set_priority accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_link_set_priority with unconfigured host_id\n"); - - if ((!knet_link_set_priority(knet_h, 1, 0, 1)) || (errno != EINVAL)) { - printf("knet_link_set_priority accepted invalid host_id or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_priority with incorrect linkid\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("Unable to add host_id 1: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_link_set_priority(knet_h, 1, KNET_MAX_LINK, 2)) || (errno != EINVAL)) { - printf("knet_link_set_priority accepted invalid linkid or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_priority with unconfigured link\n"); - - if ((!knet_link_set_priority(knet_h, 1, 0, 1)) || (errno != EINVAL)) { - printf("knet_link_set_priority accepted unconfigured link or returned incorrect error: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_link_set_priority with correct values\n"); - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &src, &dst) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_priority(knet_h, 1, 0, 3) < 0) { - printf("knet_link_set_priority failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->host_index[1]->link[0].priority != 3) { - printf("knet_link_set_priority failed to set correct values\n"); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_log_get_loglevel.c b/libknet/tests/api_knet_log_get_loglevel.c deleted file mode 100644 index 60023a39..00000000 --- a/libknet/tests/api_knet_log_get_loglevel.c +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include - -#include "libknet.h" -#include "internals.h" - -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - uint8_t level; - int logfds[2]; - - printf("Test knet_log_get_loglevel incorrect knet_h\n"); - - if ((!knet_log_get_loglevel(NULL, KNET_SUB_UNKNOWN, &level)) || (errno != EINVAL)) { - printf("knet_log_get_loglevel accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - printf("Test knet_log_get_loglevel incorrect subsystem\n"); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_INFO); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_log_get_loglevel(knet_h, KNET_SUB_UNKNOWN - 1, &level)) || (errno != EINVAL)) { - printf("knet_log_get_loglevel accepted invalid subsystem or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_log_get_loglevel incorrect log level\n"); - - if ((!knet_log_get_loglevel(knet_h, KNET_SUB_UNKNOWN, NULL)) || (errno != EINVAL)) { - printf("knet_log_get_loglevel accepted invalid log level or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_log_get_loglevel with valid parameters\n"); - - if (knet_log_get_loglevel(knet_h, KNET_SUB_UNKNOWN, &level ) < 0) { - printf("knet_log_get_loglevel failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->log_levels[KNET_SUB_UNKNOWN] != level) { - printf("knet_log_get_loglevel failed to get the right value\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_log_get_loglevel_id.c b/libknet/tests/api_knet_log_get_loglevel_id.c deleted file mode 100644 index 3cfd73ff..00000000 --- a/libknet/tests/api_knet_log_get_loglevel_id.c +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "test-common.h" - -static void test(void) -{ - uint8_t res; - - printf("Testing knet_log_get_loglevel_id normal lookup\n"); - res = knet_log_get_loglevel_id("debug"); - if (res != KNET_LOG_DEBUG) { - printf("knet_log_get_loglevel_id failed to get correct log level id. got: %u expected: %u\n", - res, KNET_LOG_DEBUG); - exit(FAIL); - } - - printf("Testing knet_log_get_loglevel_id bad lookup\n"); - res = knet_log_get_loglevel_id("whatever"); - if (res != KNET_LOG_ERR) { - printf("knet_log_get_loglevel_id failed to get correct log level id. got: %u expected: %u\n", - res, KNET_LOG_ERR); - exit(FAIL); - } -} - -int main(int argc, char *argv[]) -{ - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_log_get_loglevel_name.c b/libknet/tests/api_knet_log_get_loglevel_name.c deleted file mode 100644 index 6fe2a3d4..00000000 --- a/libknet/tests/api_knet_log_get_loglevel_name.c +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "test-common.h" - -static void test(void) -{ - const char *res; - - printf("Testing knet_log_get_loglevel_name normal lookup\n"); - res = knet_log_get_loglevel_name(KNET_LOG_DEBUG); - if (strcmp(res, "debug")) { - printf("knet_log_get_loglevel_name failed to get correct log level name. got: %s expected: debug\n", - res); - exit(FAIL); - } - - printf("Testing knet_log_get_loglevel_name bad lookup\n"); - res = knet_log_get_loglevel_name(KNET_LOG_DEBUG+1); - if (strcmp(res, "ERROR")) { - printf("knet_log_get_loglevel_name failed to get correct log level name. got: %s expected: ERROR\n", - res); - exit(FAIL); - } -} - -int main(int argc, char *argv[]) -{ - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_log_get_subsystem_id.c b/libknet/tests/api_knet_log_get_subsystem_id.c deleted file mode 100644 index c194497b..00000000 --- a/libknet/tests/api_knet_log_get_subsystem_id.c +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "test-common.h" - -static void test(void) -{ - uint8_t res; - - printf("Testing knet_log_get_subsystem_id normal lookup\n"); - res = knet_log_get_subsystem_id("nsscrypto"); - if (res != KNET_SUB_NSSCRYPTO) { - printf("knet_log_get_subsystem_id failed to get correct log subsystem id. got: %u expected: %u\n", - res, KNET_SUB_NSSCRYPTO); - exit(FAIL); - } - - printf("Testing knet_log_get_subsystem_id bad lookup\n"); - res = knet_log_get_subsystem_id("whatever"); - if (res != KNET_SUB_UNKNOWN) { - printf("knet_log_get_subsystem_id failed to get correct log subsystem id. got: %u expected: %u\n", - res, KNET_SUB_UNKNOWN); - exit(FAIL); - } -} - -int main(int argc, char *argv[]) -{ - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_log_get_subsystem_name.c b/libknet/tests/api_knet_log_get_subsystem_name.c deleted file mode 100644 index 307feadf..00000000 --- a/libknet/tests/api_knet_log_get_subsystem_name.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "test-common.h" - -static void test(void) -{ - const char *res; - - printf("Testing knet_log_get_subsystem_name normal lookup\n"); - res = knet_log_get_subsystem_name(KNET_SUB_NSSCRYPTO); - if (strcmp(res, "nsscrypto")) { - printf("knet_log_get_subsystem_name failed to get correct log subsystem name. got: %s expected: nsscrypto\n", - res); - exit(FAIL); - } - - printf("Testing knet_log_get_subsystem_name bad lookup (within boundaries)\n"); - res = knet_log_get_subsystem_name(KNET_SUB_UNKNOWN - 1); - if (strcmp(res, "unknown")) { - printf("knet_log_get_subsystem_name failed to get correct log subsystem name. got: %s expected: common\n", - res); - exit(FAIL); - } - - printf("Testing knet_log_get_subsystem_name bad lookup (outside boundaries)\n"); - res = knet_log_get_subsystem_name(KNET_MAX_SUBSYSTEMS); - if (strcmp(res, "unknown")) { - printf("knet_log_get_subsystem_name failed to get correct log subsystem name. got: %s expected: common\n", - res); - exit(FAIL); - } -} - -int main(int argc, char *argv[]) -{ - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_log_set_loglevel.c b/libknet/tests/api_knet_log_set_loglevel.c deleted file mode 100644 index cb5970d6..00000000 --- a/libknet/tests/api_knet_log_set_loglevel.c +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include - -#include "libknet.h" -#include "internals.h" - -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - - printf("Test knet_log_set_loglevel incorrect knet_h\n"); - - if ((!knet_log_set_loglevel(NULL, KNET_SUB_COMMON, KNET_LOG_DEBUG)) || (errno != EINVAL)) { - printf("knet_log_set_loglevel accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - printf("Test knet_log_set_loglevel incorrect subsystem\n"); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_INFO); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((!knet_log_set_loglevel(knet_h, KNET_SUB_UNKNOWN - 1, KNET_LOG_DEBUG)) || (errno != EINVAL)) { - printf("knet_log_set_loglevel accepted invalid subsystem or returned incorrect error: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - knet_handle_free(knet_h); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_log_set_loglevel incorrect log level\n"); - - if ((!knet_log_set_loglevel(knet_h, KNET_SUB_UNKNOWN, KNET_LOG_DEBUG + 1)) || (errno != EINVAL)) { - printf("knet_log_set_loglevel accepted invalid log level or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_log_set_loglevel with valid parameters\n"); - - if (knet_h->log_levels[KNET_SUB_UNKNOWN] != KNET_LOG_INFO) { - printf("knet_handle_new did not init log_levels correctly?\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_log_set_loglevel(knet_h, KNET_SUB_UNKNOWN, KNET_LOG_DEBUG) < 0) { - printf("knet_log_set_loglevel failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_h->log_levels[KNET_SUB_UNKNOWN] != KNET_LOG_DEBUG) { - printf("knet_log_set_loglevel did not set log level correctly\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_recv.c b/libknet/tests/api_knet_recv.c deleted file mode 100644 index ca391fb9..00000000 --- a/libknet/tests/api_knet_recv.c +++ /dev/null @@ -1,216 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "test-common.h" - -static int private_data; - -static void sock_notify(void *pvt_data, - int datafd, - int8_t channel, - uint8_t tx_rx, - int error, - int errorno) -{ - return; -} - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - int datafd = 0; - int8_t channel = 0; - char recv_buff[KNET_MAX_PACKET_SIZE]; - char send_buff[KNET_MAX_PACKET_SIZE]; - ssize_t recv_len = 0; - struct iovec iov_out[1]; - - printf("Test knet_recv incorrect knet_h\n"); - - if ((!knet_recv(NULL, recv_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_recv accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_recv with no recv_buff\n"); - - if ((!knet_recv(knet_h, NULL, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_recv accepted invalid recv_buff or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_recv with invalid recv_buff len (0)\n"); - - if ((!knet_recv(knet_h, recv_buff, 0, channel)) || (errno != EINVAL)) { - printf("knet_recv accepted invalid recv_buff len (0) or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_recv with invalid recv_buff len (> KNET_MAX_PACKET_SIZE)\n"); - - if ((!knet_recv(knet_h, recv_buff, KNET_MAX_PACKET_SIZE + 1, channel)) || (errno != EINVAL)) { - printf("knet_recv accepted invalid recv_buff len (> KNET_MAX_PACKET_SIZE) or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_recv with invalid channel (-1)\n"); - - channel = -1; - - if ((!knet_recv(knet_h, recv_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_recv accepted invalid channel (-1) or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_recv with invalid channel (KNET_DATAFD_MAX)\n"); - - channel = KNET_DATAFD_MAX; - - if ((!knet_recv(knet_h, recv_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_recv accepted invalid channel (KNET_DATAFD_MAX) or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_recv with unconfigured channel\n"); - - channel = 0; - - if ((!knet_recv(knet_h, recv_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_recv accepted invalid unconfigured channel or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_recv with valid data\n"); - - if (knet_handle_enable_sock_notify(knet_h, &private_data, sock_notify) < 0) { - printf("knet_handle_enable_sock_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - datafd = 0; - channel = -1; - - if (knet_handle_add_datafd(knet_h, &datafd, &channel) < 0) { - printf("knet_handle_add_datafd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - memset(recv_buff, 0, KNET_MAX_PACKET_SIZE); - memset(send_buff, 1, sizeof(send_buff)); - - iov_out[0].iov_base = (void *)send_buff; - iov_out[0].iov_len = sizeof(send_buff); - - if (writev(knet_h->sockfd[channel].sockfd[1], iov_out, 1) != sizeof(send_buff)) { - printf("Unable to write data: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - recv_len = knet_recv(knet_h, recv_buff, KNET_MAX_PACKET_SIZE, channel); - if (recv_len <= 0) { - printf("knet_recv failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (recv_len != sizeof(send_buff)) { - printf("knet_recv received only %zu bytes: %s\n", recv_len, strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (memcmp(recv_buff, send_buff, KNET_MAX_PACKET_SIZE)) { - printf("knet_recv received bad data\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_send.c b/libknet/tests/api_knet_send.c deleted file mode 100644 index ed2d0ef6..00000000 --- a/libknet/tests/api_knet_send.c +++ /dev/null @@ -1,289 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "netutils.h" -#include "test-common.h" - -static int private_data; - -static void sock_notify(void *pvt_data, - int datafd, - int8_t channel, - uint8_t tx_rx, - int error, - int errorno) -{ - return; -} - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - int datafd = 0; - int8_t channel = 0; - char send_buff[KNET_MAX_PACKET_SIZE]; - char recv_buff[KNET_MAX_PACKET_SIZE]; - ssize_t send_len = 0; - int recv_len = 0; - int savederrno; - struct sockaddr_storage lo; - - memset(&lo, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &lo, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert loopback to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(send_buff, 0, sizeof(send_buff)); - - printf("Test knet_send incorrect knet_h\n"); - - if ((!knet_send(NULL, send_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_send accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_send with no send_buff\n"); - - if ((!knet_send(knet_h, NULL, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_send accepted invalid send_buff or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send with invalid send_buff len (0)\n"); - - if ((!knet_send(knet_h, send_buff, 0, channel)) || (errno != EINVAL)) { - printf("knet_send accepted invalid send_buff len (0) or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send with invalid send_buff len (> KNET_MAX_PACKET_SIZE)\n"); - - if ((!knet_send(knet_h, send_buff, KNET_MAX_PACKET_SIZE + 1, channel)) || (errno != EINVAL)) { - printf("knet_send accepted invalid send_buff len (> KNET_MAX_PACKET_SIZE) or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send with invalid channel (-1)\n"); - - channel = -1; - - if ((!knet_send(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_send accepted invalid channel (-1) or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send with invalid channel (KNET_DATAFD_MAX)\n"); - - channel = KNET_DATAFD_MAX; - - if ((!knet_send(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_send accepted invalid channel (KNET_DATAFD_MAX) or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send with unconfigured channel\n"); - - channel = 0; - - if ((!knet_send(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_send accepted invalid unconfigured channel or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send with valid data\n"); - - if (knet_handle_enable_sock_notify(knet_h, &private_data, sock_notify) < 0) { - printf("knet_handle_enable_sock_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - datafd = 0; - channel = -1; - - if (knet_handle_add_datafd(knet_h, &datafd, &channel) < 0) { - printf("knet_handle_add_datafd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_host_add(knet_h, 1) < 0) { - printf("knet_host_add failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &lo, &lo) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_enable(knet_h, 1, 0, 1) < 0) { - printf("knet_link_set_enable failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_handle_setfwd(knet_h, 1) < 0) { - printf("knet_handle_setfwd failed: %s\n", strerror(errno)); - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - while(knet_h->host_index[1]->status.reachable != 1) { - printf("waiting host to be reachable\n"); - sleep(1); - } - - send_len = knet_send(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel); - if (send_len <= 0) { - printf("knet_send failed: %s\n", strerror(errno)); - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (send_len != sizeof(send_buff)) { - printf("knet_send sent only %zu bytes: %s\n", send_len, strerror(errno)); - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - sleep(1); - - recv_len = knet_recv(knet_h, recv_buff, KNET_MAX_PACKET_SIZE, channel); - savederrno = errno; - if (recv_len != send_len) { - printf("knet_recv received only %d bytes: %s (errno: %d)\n", recv_len, strerror(errno), errno); - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - if ((is_helgrind()) && (recv_len == -1) && (savederrno == EAGAIN)) { - printf("helgrind exception. this is normal due to possible timeouts\n"); - exit(PASS); - } - exit(FAIL); - } - - if (memcmp(recv_buff, send_buff, KNET_MAX_PACKET_SIZE)) { - printf("recv and send buffers are different!\n"); - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_send_sync.c b/libknet/tests/api_knet_send_sync.c deleted file mode 100644 index 904d4c29..00000000 --- a/libknet/tests/api_knet_send_sync.c +++ /dev/null @@ -1,396 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "netutils.h" -#include "test-common.h" - -static int private_data; - -static void sock_notify(void *pvt_data, - int datafd, - int8_t channel, - uint8_t tx_rx, - int error, - int errorno) -{ - return; -} - -static int dhost_filter_ret = 0; - -static int dhost_filter(void *pvt_data, - const unsigned char *outdata, - ssize_t outdata_len, - uint8_t tx_rx, - knet_node_id_t this_host_id, - knet_node_id_t src_host_id, - int8_t *dst_channel, - knet_node_id_t *dst_host_ids, - size_t *dst_host_ids_entries) -{ - dst_host_ids[0] = 0; - - /* - * fatal fault - */ - if (dhost_filter_ret < 0) { - return -1; - } - - /* - * trigger EINVAL - * no ids found - */ - if (dhost_filter_ret == 0) { - *dst_host_ids_entries = 0; - return 0; - } - - /* - * send correct info back - */ - - if (dhost_filter_ret == 1) { - dst_host_ids[0] = 1; - *dst_host_ids_entries = 1; - return 0; - } - - /* - * trigger E2BIG - * mcast destinations - */ - if (dhost_filter_ret == 2) { - dst_host_ids[0] = 1; - *dst_host_ids_entries = 2; - return 0; - } - - /* - * return mcast - */ - if (dhost_filter_ret == 3) { - return 1; - } - - return dhost_filter_ret; -} - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - int datafd = 0; - int8_t channel = 0; - char send_buff[KNET_MAX_PACKET_SIZE]; - struct sockaddr_storage lo; - - memset(&lo, 0, sizeof(struct sockaddr_storage)); - - if (knet_strtoaddr("127.0.0.1", "50000", &lo, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert loopback to sockaddr: %s\n", strerror(errno)); - exit(FAIL); - } - - memset(send_buff, 0, sizeof(send_buff)); - - printf("Test knet_send_sync incorrect knet_h\n"); - - if ((!knet_send_sync(NULL, send_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_send_sync accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); - exit(FAIL); - } - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Test knet_send_sync with no send_buff\n"); - - if ((!knet_send_sync(knet_h, NULL, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_send_sync accepted invalid send_buff or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send_sync with invalid send_buff len (0)\n"); - - if ((!knet_send_sync(knet_h, send_buff, 0, channel)) || (errno != EINVAL)) { - printf("knet_send_sync accepted invalid send_buff len (0) or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send_sync with invalid send_buff len (> KNET_MAX_PACKET_SIZE)\n"); - - if ((!knet_send_sync(knet_h, send_buff, KNET_MAX_PACKET_SIZE + 1, channel)) || (errno != EINVAL)) { - printf("knet_send_sync accepted invalid send_buff len (> KNET_MAX_PACKET_SIZE) or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send_sync with invalid channel (-1)\n"); - - channel = -1; - - if ((!knet_send_sync(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_send_sync accepted invalid channel (-1) or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send_sync with invalid channel (KNET_DATAFD_MAX)\n"); - - channel = KNET_DATAFD_MAX; - - if ((!knet_send_sync(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_send_sync accepted invalid channel (KNET_DATAFD_MAX) or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send_sync with unconfigured channel\n"); - - channel = 0; - - if ((!knet_send_sync(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { - printf("knet_send_sync accepted invalid unconfigured channel or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send_sync with data forwarding disabled\n"); - - if (knet_handle_enable_sock_notify(knet_h, &private_data, sock_notify) < 0) { - printf("knet_handle_enable_sock_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - datafd = 0; - channel = -1; - - if (knet_handle_add_datafd(knet_h, &datafd, &channel) < 0) { - printf("knet_handle_add_datafd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if ((knet_send_sync(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel) == sizeof(send_buff)) || (errno != ECANCELED)) { - printf("knet_send_sync didn't detect datafwd disabled or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send_sync with broken dst_host_filter\n"); - - if (knet_handle_setfwd(knet_h, 1) < 0) { - printf("knet_handle_setfwd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_handle_enable_filter(knet_h, NULL, dhost_filter) < 0) { - printf("knet_handle_enable_filter failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - dhost_filter_ret = -1; - - if ((knet_send_sync(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel) == sizeof(send_buff)) || (errno != EFAULT)) { - printf("knet_send_sync didn't detect fatal error from dst_host_filter or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send_sync with dst_host_filter returning no host_ids_entries\n"); - - dhost_filter_ret = 0; - - if ((knet_send_sync(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel) == sizeof(send_buff)) || (errno != EINVAL)) { - printf("knet_send_sync didn't detect 0 host_ids from dst_host_filter or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send_sync with host down\n"); - - dhost_filter_ret = 1; - - if ((knet_send_sync(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel) == sizeof(send_buff)) || (errno != EHOSTDOWN)) { - printf("knet_send_sync didn't detect hostdown or returned incorrect error: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send_sync with dst_host_filter returning too many host_ids_entries\n"); - - if (knet_host_add(knet_h, 1) < 0) { - printf("knet_host_add failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &lo, &lo) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - if (knet_link_set_enable(knet_h, 1, 0, 1) < 0) { - printf("knet_link_set_enable failed: %s\n", strerror(errno)); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - while(knet_h->host_index[1]->status.reachable != 1) { - printf("waiting host to be reachable\n"); - sleep(1); - } - - dhost_filter_ret = 2; - - if ((knet_send_sync(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel) == sizeof(send_buff)) || (errno != E2BIG)) { - printf("knet_send_sync didn't detect 2+ host_ids from dst_host_filter or returned incorrect error: %s\n", strerror(errno)); - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send_sync with dst_host_filter returning mcast packets\n"); - - dhost_filter_ret = 3; - - if ((knet_send_sync(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel) == sizeof(send_buff)) || (errno != E2BIG)) { - printf("knet_send_sync didn't detect mcast packet from dst_host_filter or returned incorrect error: %s\n", strerror(errno)); - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_send_sync with valid data\n"); - - dhost_filter_ret = 1; - - if (knet_send_sync(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel) < 0) { - printf("knet_send_sync failed: %d %s\n", errno, strerror(errno)); - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - knet_link_set_enable(knet_h, 1, 0, 0); - knet_link_clear_config(knet_h, 1, 0); - knet_host_remove(knet_h, 1); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - test(); - - return PASS; -} diff --git a/libknet/tests/api_knet_strtoaddr.c b/libknet/tests/api_knet_strtoaddr.c deleted file mode 100644 index 1a9fbb8b..00000000 --- a/libknet/tests/api_knet_strtoaddr.c +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include - -#include "libknet.h" -#include "test-common.h" - -static void test(void) -{ - struct sockaddr_storage out_addr; - struct sockaddr_in *out_addrv4 = (struct sockaddr_in *)&out_addr; - struct sockaddr_in6 *out_addrv6 = (struct sockaddr_in6 *)&out_addr; - struct sockaddr_in addrv4; - struct sockaddr_in6 addrv6; - - memset(&out_addr, 0, sizeof(struct sockaddr_storage)); - memset(&addrv4, 0, sizeof(struct sockaddr_in)); - memset(&addrv6, 0, sizeof(struct sockaddr_in6)); - - printf("Checking knet_strtoaddr with invalid host\n"); - - if (!knet_strtoaddr(NULL, "50000", &out_addr, sizeof(struct sockaddr_storage)) && - (errno != EINVAL)) { - printf("knet_strtoaddr accepted invalid host\n"); - exit(FAIL); - } - - printf("Checking knet_strtoaddr with invalid port\n"); - - if (!knet_strtoaddr("127.0.0.1", NULL, &out_addr, sizeof(struct sockaddr_storage)) && - (errno != EINVAL)) { - printf("knet_strtoaddr accepted invalid port\n"); - exit(FAIL); - } - - printf("Checking knet_strtoaddr with invalid addr\n"); - - if (!knet_strtoaddr("127.0.0.1", "50000", NULL, sizeof(struct sockaddr_storage)) && - (errno != EINVAL)) { - printf("knet_strtoaddr accepted invalid addr\n"); - exit(FAIL); - } - - printf("Checking knet_strtoaddr with invalid size\n"); - - if (!knet_strtoaddr("127.0.0.1", "50000", &out_addr, 0) && - (errno != EINVAL)) { - printf("knet_strtoaddr accepted invalid size\n"); - exit(FAIL); - } - - addrv4.sin_family = AF_INET; - addrv4.sin_addr.s_addr = htonl(0xc0a80001); /* 192.168.0.1 */ - addrv4.sin_port = htons(50000); - - printf("Checking knet_strtoaddr with valid data (192.168.0.1:50000)\n"); - - if (knet_strtoaddr("192.168.0.1", "50000", &out_addr, sizeof(struct sockaddr_storage))) { - printf("Unable to convert 192.168.0.1:50000\n"); - exit(FAIL); - } - - if (out_addrv4->sin_family != addrv4.sin_family || - out_addrv4->sin_port != addrv4.sin_port || - out_addrv4->sin_addr.s_addr != addrv4.sin_addr.s_addr) { - printf("Check on 192.168.0.1:50000 failed\n"); - exit(FAIL); - } - - printf("Checking knet_strtoaddr with valid data ([fd00::1]:50000)\n"); - - memset(&out_addr, 0, sizeof(struct sockaddr_storage)); - - addrv6.sin6_family = AF_INET6; - addrv6.sin6_addr.s6_addr16[0] = htons(0xfd00); /* fd00::1 */ - addrv6.sin6_addr.s6_addr16[7] = htons(0x0001); - addrv6.sin6_port = htons(50000); - - if (knet_strtoaddr("fd00::1", "50000", &out_addr, sizeof(struct sockaddr_storage))) { - printf("Unable to convert fd00::1:50000\n"); - exit(FAIL); - } - - if (out_addrv6->sin6_family != addrv6.sin6_family || - out_addrv6->sin6_port != addrv6.sin6_port || - memcmp(&out_addrv6->sin6_addr, &addrv6.sin6_addr, sizeof(struct in6_addr))) { - - printf("Check on fd00::1:50000 failed\n"); - exit(FAIL); - } - -} - -int main(int argc, char *argv[]) -{ - - test(); - - exit(PASS); -} diff --git a/libknet/tests/crypto_bench.c b/libknet/tests/crypto_bench.c deleted file mode 100644 index 35a0c8c7..00000000 --- a/libknet/tests/crypto_bench.c +++ /dev/null @@ -1,268 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "crypto.h" -#include "threads_common.h" - -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfd; - struct knet_handle_crypto_cfg knet_handle_crypto_cfg; - char *buf1, *buf2, *buf3; - const char *input = "Encrypt me!\x0"; - ssize_t input_len = strlen(input) + 1; - ssize_t outbuf_len; - int loops; - struct timespec clock_start, clock_end; - unsigned long long time_diff; - struct iovec iov_in; - struct iovec iov_multi[4]; - - memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); - - logfd = start_logging(stdout); - - knet_h = knet_handle_new(1, logfd, KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - exit(FAIL); - } - - printf("Test knet_handle_crypto with nss/aes128/sha1 and normal key\n"); - - memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); - strncpy(knet_handle_crypto_cfg.crypto_model, "nss", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); - strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); - strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); - knet_handle_crypto_cfg.private_key_len = 2000; - - if (knet_handle_crypto(knet_h, &knet_handle_crypto_cfg)) { - printf("knet_handle_crypto failed with correct config: %s\n", strerror(errno)); - knet_handle_free(knet_h); - exit(FAIL); - } - - buf1=malloc(input_len); - buf2=malloc(input_len + knet_h->sec_header_size); - buf3=malloc(input_len + knet_h->sec_header_size); - - memset(buf1, 0, input_len); - memset(buf2, 0, input_len + knet_h->sec_header_size); - memset(buf3, 0, input_len + knet_h->sec_header_size); - - /* - * setup source buffer - */ - - if (clock_gettime(CLOCK_MONOTONIC, &clock_start) != 0) { - printf("Unable to get start time!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - for (loops=0; loops<1000000; loops++) { - memset(buf1, 0, input_len); - memset(buf2, 0, input_len + knet_h->sec_header_size); - memset(buf3, 0, input_len + knet_h->sec_header_size); - - strncpy(buf1, input, input_len); - - if (crypto_encrypt_and_sign(knet_h, (unsigned char *)buf1, strlen(buf1)+1, (unsigned char *)buf2, &outbuf_len) < 0) { - printf("Unable to crypt and sign!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - if (crypto_authenticate_and_decrypt(knet_h, (unsigned char *)buf2, outbuf_len, (unsigned char *)buf3, &outbuf_len) < 0) { - printf("Unable to auth and decrypt!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - - if (memcmp(buf1, buf3, outbuf_len)) { - printf("Crypt / Descrypt produced two different data set!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - } - - if (clock_gettime(CLOCK_MONOTONIC, &clock_end) != 0) { - printf("Unable to get end time!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - timespec_diff(clock_start, clock_end, &time_diff); - - printf("Execution of 1000000 loops (buf_in api): %llu/ns\n", time_diff); - - if (clock_gettime(CLOCK_MONOTONIC, &clock_start) != 0) { - printf("Unable to get start time!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - memset(buf1, 0, input_len); - strncpy(buf1, input, input_len); - - for (loops=0; loops<1000000; loops++) { - memset(buf2, 0, input_len + knet_h->sec_header_size); - memset(buf3, 0, input_len + knet_h->sec_header_size); - memset(&iov_in, 0, sizeof(iov_in)); - - iov_in.iov_base = (unsigned char *)buf1; - iov_in.iov_len = strlen(buf1)+1; - - if (crypto_encrypt_and_signv(knet_h, &iov_in, 1, (unsigned char *)buf2, &outbuf_len) < 0) { - printf("Unable to crypt and sign!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - if (crypto_authenticate_and_decrypt(knet_h, (unsigned char *)buf2, outbuf_len, (unsigned char *)buf3, &outbuf_len) < 0) { - printf("Unable to auth and decrypt!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - - if (memcmp(buf1, buf3, outbuf_len)) { - printf("Crypt / Descrypt produced two different data set!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - } - - if (clock_gettime(CLOCK_MONOTONIC, &clock_end) != 0) { - printf("Unable to get end time!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - timespec_diff(clock_start, clock_end, &time_diff); - - printf("Execution of 1000000 loops (iov_in api): %llu/ns\n", time_diff); - - if (clock_gettime(CLOCK_MONOTONIC, &clock_start) != 0) { - printf("Unable to get start time!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - memset(buf1, 0, input_len); - strncpy(buf1, input, input_len); - - for (loops=0; loops<1000000; loops++) { - memset(buf2, 0, input_len + knet_h->sec_header_size); - memset(buf3, 0, input_len + knet_h->sec_header_size); - memset(&iov_multi, 0, sizeof(iov_multi)); - - /* - * "Encrypt me!\n" = 12 bytes - */ - - iov_multi[0].iov_base = (unsigned char *)buf1; - iov_multi[0].iov_len = 3; - iov_multi[1].iov_base = (unsigned char *)buf1 + 3; - iov_multi[1].iov_len = 3; - iov_multi[2].iov_base = (unsigned char *)buf1 + 6; - iov_multi[2].iov_len = 3; - iov_multi[3].iov_base = (unsigned char *)buf1 + 9; - iov_multi[3].iov_len = 3; - - if (crypto_encrypt_and_signv(knet_h, iov_multi, 4, (unsigned char *)buf2, &outbuf_len) < 0) { - printf("Unable to crypt and sign!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - if (crypto_authenticate_and_decrypt(knet_h, (unsigned char *)buf2, outbuf_len, (unsigned char *)buf3, &outbuf_len) < 0) { - printf("Unable to auth and decrypt!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - - if (memcmp(buf1, buf3, outbuf_len)) { - printf("Crypt / Descrypt produced two different data set!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - } - - if (clock_gettime(CLOCK_MONOTONIC, &clock_end) != 0) { - printf("Unable to get end time!\n"); - knet_handle_free(knet_h); - exit(FAIL); - } - - timespec_diff(clock_start, clock_end, &time_diff); - - printf("Execution of 1000000 loops (iov_in multi api): %llu/ns\n", time_diff); - - printf("Shutdown crypto\n"); - - memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); - strncpy(knet_handle_crypto_cfg.crypto_model, "none", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); - strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "none", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); - strncpy(knet_handle_crypto_cfg.crypto_hash_type, "none", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); - knet_handle_crypto_cfg.private_key_len = 2000; - - if (knet_handle_crypto(knet_h, &knet_handle_crypto_cfg) < 0) { - printf("Unable to shutdown crypto: %s\n", strerror(errno)); - knet_handle_free(knet_h); - exit(FAIL); - } - - knet_handle_free(knet_h); - free(buf1); - free(buf2); - free(buf3); -} - -int main(int argc, char *argv[]) -{ - need_root(); - - printf("Testing with default scheduler\n"); - - set_scheduler(SCHED_OTHER); - - test(); - - printf("Testing with SCHED_RR scheduler\n"); - - set_scheduler(SCHED_RR); - - test(); - - printf("Testing with SCHED_FIFO scheduler\n"); - - set_scheduler(SCHED_FIFO); - - test(); - - return PASS; -} diff --git a/libknet/tests/int_crypto.c b/libknet/tests/int_crypto.c deleted file mode 100644 index 8cdf8fbe..00000000 --- a/libknet/tests/int_crypto.c +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "crypto.h" -#include "test-common.h" - -static void test(void) -{ - knet_handle_t knet_h; - int logfds[2]; - struct knet_handle_crypto_cfg knet_handle_crypto_cfg; - char *buf1, *buf2, *buf3; - const char *input = "Encrypt me!\x0"; - ssize_t input_len = strlen(input) + 1; - ssize_t outbuf_len; - int i; - - memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); - - setup_logpipes(logfds); - - knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); - - if (!knet_h) { - printf("knet_handle_new failed: %s\n", strerror(errno)); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - printf("Test knet_handle_crypto with nss/aes128/sha1 and normal key\n"); - - memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); - strncpy(knet_handle_crypto_cfg.crypto_model, "nss", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); - strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); - strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); - knet_handle_crypto_cfg.private_key_len = 2000; - - if (knet_handle_crypto(knet_h, &knet_handle_crypto_cfg)) { - printf("knet_handle_crypto failed with correct config: %s\n", strerror(errno)); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - flush_logs(logfds[0], stdout); - - buf1=malloc(input_len); - buf2=malloc(input_len + knet_h->sec_header_size); - buf3=malloc(input_len + knet_h->sec_header_size); - - memset(buf1, 0, input_len); - memset(buf2, 0, input_len + knet_h->sec_header_size); - memset(buf3, 0, input_len + knet_h->sec_header_size); - - /* - * setup source buffer - */ - - strncpy(buf1, input, input_len); - printf("Source Data: %s\n", buf1); - - if (crypto_encrypt_and_sign(knet_h, (unsigned char *)buf1, strlen(buf1)+1, (unsigned char *)buf2, &outbuf_len) < 0) { - printf("Unable to crypt and sign!\n"); - knet_handle_free(knet_h); - flush_logs(logfds[0], stdout); - close_logpipes(logfds); - exit(FAIL); - } - - printf("Encrypted Data: "); - for (i=0; i - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include - -#include "threads_common.h" - -#include "test-common.h" - -#define timespec_set(x, sec, nsec) \ -do { \ - x.tv_sec = sec; \ - x.tv_nsec = nsec; \ -} while (0); - -static void check_timespec_diff(void) -{ - unsigned long long diff; - struct timespec start, end; - - timespec_set(start, 1, 30000); - - timespec_set(end, start.tv_sec, start.tv_nsec + 10000); - timespec_diff(start, end, &diff); - - printf("Checking 10000 == %llu\n", diff); - - if (diff != 10000) { - printf("Failure!\n"); - exit(FAIL); - } - - timespec_set(end, start.tv_sec + 5, start.tv_nsec - 5000); - timespec_diff(start, end, &diff); - - printf("Checking 4999995000 == %llu\n", diff); - - if (diff != 4999995000llu) { - printf("Failure!\n"); - exit(FAIL); - } -} - -int main(int argc, char *argv[]) -{ - check_timespec_diff(); - - return PASS; -} diff --git a/libknet/tests/knet_bench.c b/libknet/tests/knet_bench.c deleted file mode 100644 index a5e10808..00000000 --- a/libknet/tests/knet_bench.c +++ /dev/null @@ -1,664 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include - -#include "libknet.h" - -#include "internals.h" -#include "netutils.h" -#include "transports.h" -#include "test-common.h" - -#define MAX_NODES 128 - -static int senderid = -1; -static knet_handle_t knet_h; -static int datafd = 0; -static int8_t channel = 0; -static int globallistener = 0; -static int continous = 0; -static struct sockaddr_storage allv4; -static struct sockaddr_storage allv6; -static int broadcast_test = 1; -static pthread_t rx_thread = (pthread_t)NULL; -static char *rx_buf[PCKT_FRAG_MAX]; -static int shutdown_in_progress = 0; -static pthread_mutex_t shutdown_mutex = PTHREAD_MUTEX_INITIALIZER; - -#define TEST_PING 0 -#define TEST_PING_AND_DATA 1 -#define TEST_PERF 2 - -static int test_type = TEST_PING; - -struct node { - int nodeid; - int links; - struct sockaddr_storage address[KNET_MAX_LINK]; -}; - -static void print_help(void) -{ - printf("knet_bench usage:\n"); - printf(" -h print this help (no really)\n"); - printf(" -d enable debug logs (default INFO)\n"); - printf(" -c [implementation]:[crypto]:[hashing] crypto configuration. (default disabled)\n"); - printf(" Example: -c nss:aes128:sha1\n"); - printf(" -p [active|passive|rr] (default: passive)\n"); - printf(" -P [udp|sctp] (default: udp) protocol (transport) to use\n"); - printf(" -t [nodeid] This nodeid (required)\n"); - printf(" -n [nodeid],[link1_ip_addr],[link2_..] Other nodes information (at least one required)\n"); - printf(" Example: -t 1,192.168.8.1,3ffe::8:1,..\n"); - printf(" can be repeated up to %d and should contain also the localnode info\n", MAX_NODES); - printf(" -b [port] baseport (default: 50000)\n"); - printf(" -l enable global listener on 0.0.0.0/:: (default: off, incompatible with -o)\n"); - printf(" -o enable baseport offset per nodeid\n"); - printf(" -w dont wait for all nodes to be up before starting the test (default: wait)\n"); - printf(" -T [ping|ping_data|perf] test type (default: ping)\n"); - printf(" ping: will wait for all hosts to join the knet network, sleep 5 seconds and quit\n"); - printf(" ping_data: will wait for all hosts to join the knet network, sends some data to all nodes and quit\n"); - printf(" perf: will wait for all hosts to join the knet network, perform a series of benchmarks and quit\n"); - printf(" -s nodeid that will generate traffic for benchmarks\n"); - printf(" -C repeat the test continously (default: off)\n"); -} - -static void parse_nodes(char *nodesinfo[MAX_NODES], int onidx, int port, struct node nodes[MAX_NODES], int thisnodeid, int *thisidx) -{ - int i; - char *temp = NULL; - char port_str[10]; - - memset(port_str, 0, sizeof(port_str)); - sprintf(port_str, "%d", port); - - for (i = 0; i < onidx; i++) { - nodes[i].nodeid = atoi(strtok(nodesinfo[i], ",")); - if ((nodes[i].nodeid < 0) || (nodes[i].nodeid > KNET_MAX_HOST)) { - printf("Invalid nodeid: %d (0 - %d)\n", nodes[i].nodeid, KNET_MAX_HOST); - exit(FAIL); - } - if (thisnodeid == nodes[i].nodeid) { - *thisidx = i; - } - while((temp = strtok(NULL, ","))) { - if (nodes[i].links == KNET_MAX_LINK) { - printf("Too many links configured. Max %d\n", KNET_MAX_LINK); - exit(FAIL); - } - if (knet_strtoaddr(temp, port_str, - &nodes[i].address[nodes[i].links], - sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert %s to sockaddress\n", temp); - exit(FAIL); - } - nodes[i].links++; - } - } - - if (knet_strtoaddr("0.0.0.0", port_str, &allv4, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert 0.0.0.0 to sockaddress\n"); - exit(FAIL); - } - - if (knet_strtoaddr("::", port_str, &allv6, sizeof(struct sockaddr_storage)) < 0) { - printf("Unable to convert :: to sockaddress\n"); - exit(FAIL); - } - - for (i = 1; i < onidx; i++) { - if (nodes[0].links != nodes[i].links) { - printf("knet_bench does not support unbalanced link configuration\n"); - exit(FAIL); - } - } - - return; -} - -static int private_data; - -static void sock_notify(void *pvt_data, - int local_datafd, - int8_t local_channel, - uint8_t tx_rx, - int error, - int errorno) -{ - printf("Error (%d - %d - %s) from socket: %d\n", error, errorno, strerror(errno), local_datafd); - return; -} - -static void setup_knet(int argc, char *argv[]) -{ - int logfd; - int rv; - char *cryptocfg = NULL, *policystr = NULL, *protostr = NULL; - char *othernodeinfo[MAX_NODES]; - struct node nodes[MAX_NODES]; - int thisnodeid = -1; - int thisidx = -1; - int onidx = 0; - int debug = KNET_LOG_INFO; - int port = 50000, portoffset = 0; - int thisport = 0, otherport = 0; - int thisnewport = 0, othernewport = 0; - struct sockaddr_in *so_in; - struct sockaddr_in6 *so_in6; - struct sockaddr_storage *src; - int i, link_idx, allnodesup = 0; - int policy = KNET_LINK_POLICY_PASSIVE, policyfound = 0; - int protocol = KNET_TRANSPORT_UDP, protofound = 0; - int wait = 1; - struct knet_handle_crypto_cfg knet_handle_crypto_cfg; - char *cryptomodel = NULL, *cryptotype = NULL, *cryptohash = NULL; - - memset(nodes, 0, sizeof(nodes)); - - while ((rv = getopt(argc, argv, "CT:s:ldowb:t:n:c:p:P:h")) != EOF) { - switch(rv) { - case 'h': - print_help(); - exit(PASS); - break; - case 'd': - debug = KNET_LOG_DEBUG; - break; - case 'c': - if (cryptocfg) { - printf("Error: -c can only be specified once\n"); - exit(FAIL); - } - cryptocfg = optarg; - break; - case 'p': - if (policystr) { - printf("Error: -p can only be specified once\n"); - exit(FAIL); - } - policystr = optarg; - if (!strcmp(policystr, "active")) { - policy = KNET_LINK_POLICY_ACTIVE; - policyfound = 1; - } - if (!strcmp(policystr, "rr")) { - policy = KNET_LINK_POLICY_RR; - policyfound = 1; - } - if (!strcmp(policystr, "passive")) { - policy = KNET_LINK_POLICY_PASSIVE; - policyfound = 1; - } - if (!policyfound) { - printf("Error: invalid policy %s specified. -p accepts active|passive|rr\n", policystr); - exit(FAIL); - } - break; - case 'P': - if (protostr) { - printf("Error: -P can only be specified once\n"); - exit(FAIL); - } - protostr = optarg; - if (!strcmp(protostr, "udp")) { - protocol = KNET_TRANSPORT_UDP; - protofound = 1; - } - if (!strcmp(protostr, "sctp")) { - protocol = KNET_TRANSPORT_SCTP; - protofound = 1; - } - if (!protofound) { - printf("Error: invalid protocol %s specified. -P accepts udp|sctp\n", policystr); - exit(FAIL); - } - break; - case 't': - if (thisnodeid >= 0) { - printf("Error: -t can only be specified once\n"); - exit(FAIL); - } - thisnodeid = atoi(optarg); - if ((thisnodeid < 0) || (thisnodeid > 65536)) { - printf("Error: -t nodeid out of range %d (1 - 65536)\n", thisnodeid); - exit(FAIL); - } - break; - case 'n': - if (onidx == MAX_NODES) { - printf("Error: too many other nodes. Max %d\n", MAX_NODES); - exit(FAIL); - } - othernodeinfo[onidx] = optarg; - onidx++; - break; - case 'b': - port = atoi(optarg); - if ((port < 1) || (port > 65536)) { - printf("Error: port %d out of range (1 - 65536)\n", port); - exit(FAIL); - } - case 'o': - if (globallistener) { - printf("Error: -l cannot be used with -o\n"); - exit(FAIL); - } - portoffset = 1; - break; - case 'l': - if (portoffset) { - printf("Error: -o cannot be used with -l\n"); - exit(FAIL); - } - globallistener = 1; - break; - case 'w': - wait = 0; - break; - case 's': - if (senderid >= 0) { - printf("Error: -s can only be specified once\n"); - exit(FAIL); - } - senderid = atoi(optarg); - if ((senderid < 0) || (senderid > 65536)) { - printf("Error: -s nodeid out of range %d (1 - 65536)\n", senderid); - exit(FAIL); - } - break; - case 'T': - if (!strcmp("ping", optarg)) { - test_type = TEST_PING; - } - if (!strcmp("ping_data", optarg)) { - test_type = TEST_PING_AND_DATA; - } - if (!strcmp("perf", optarg)) { - test_type = TEST_PERF; - } - break; - case 'C': - continous = 1; - break; - default: - break; - } - } - - if (thisnodeid < 0) { - printf("Who am I?!? missing -t from command line?\n"); - exit(FAIL); - } - - if (onidx < 1) { - printf("no other nodes configured?!? missing -n from command line\n"); - exit(FAIL); - } - - parse_nodes(othernodeinfo, onidx, port, nodes, thisnodeid, &thisidx); - - if (thisidx < 0) { - printf("no config for this node found\n"); - exit(FAIL); - } - - if (senderid >= 0) { - for (i=0; i < onidx; i++) { - if (senderid == nodes[i].nodeid) { - break; - } - } - if (i == onidx) { - printf("Unable to find senderid in nodelist\n"); - exit(FAIL); - } - } - - if ((test_type == TEST_PERF) && (senderid < 0)) { - printf("Error: performance test requires -s to be set (for now)\n"); - exit(FAIL); - } - - logfd = start_logging(stdout); - - knet_h = knet_handle_new(thisnodeid, logfd, debug); - if (!knet_h) { - printf("Unable to knet_handle_new: %s\n", strerror(errno)); - exit(FAIL); - } - - if (cryptocfg) { - memset(&knet_handle_crypto_cfg, 0, sizeof(knet_handle_crypto_cfg)); - cryptomodel = strtok(cryptocfg, ":"); - cryptotype = strtok(NULL, ":"); - cryptohash = strtok(NULL, ":"); - if (cryptomodel) { - strncpy(knet_handle_crypto_cfg.crypto_model, cryptomodel, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); - } - if (cryptotype) { - strncpy(knet_handle_crypto_cfg.crypto_cipher_type, cryptotype, sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); - } - if (cryptohash) { - strncpy(knet_handle_crypto_cfg.crypto_hash_type, cryptohash, sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); - } - knet_handle_crypto_cfg.private_key_len = KNET_MAX_KEY_LEN; - if (knet_handle_crypto(knet_h, &knet_handle_crypto_cfg)) { - printf("Unable to init crypto\n"); - exit(FAIL); - } - } - - if (knet_handle_enable_sock_notify(knet_h, &private_data, sock_notify) < 0) { - printf("knet_handle_enable_sock_notify failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - exit(FAIL); - } - - datafd = 0; - channel = -1; - - if (knet_handle_add_datafd(knet_h, &datafd, &channel) < 0) { - printf("knet_handle_add_datafd failed: %s\n", strerror(errno)); - knet_handle_free(knet_h); - exit(FAIL); - } - - for (i=0; i < onidx; i++) { - if (i == thisidx) { - continue; - } - - if (knet_host_add(knet_h, nodes[i].nodeid) < 0) { - printf("knet_host_add failed: %s\n", strerror(errno)); - exit(FAIL); - } - - if (knet_host_set_policy(knet_h, nodes[i].nodeid, policy) < 0) { - printf("knet_host_set_policy failed: %s\n", strerror(errno)); - exit(FAIL); - } - - for (link_idx = 0; link_idx < nodes[i].links; link_idx++) { - if (portoffset) { - if (nodes[thisidx].address[link_idx].ss_family == AF_INET) { - so_in = (struct sockaddr_in *)&nodes[thisidx].address[link_idx]; - thisport = ntohs(so_in->sin_port); - thisnewport = thisport + nodes[i].nodeid; - so_in->sin_port = (htons(thisnewport)); - so_in = (struct sockaddr_in *)&nodes[i].address[link_idx]; - otherport = ntohs(so_in->sin_port); - othernewport = otherport + nodes[thisidx].nodeid; - so_in->sin_port = (htons(othernewport)); - } else { - so_in6 = (struct sockaddr_in6 *)&nodes[thisidx].address[link_idx]; - thisport = ntohs(so_in6->sin6_port); - thisnewport = thisport + nodes[i].nodeid; - so_in6->sin6_port = (htons(thisnewport)); - so_in6 = (struct sockaddr_in6 *)&nodes[i].address[link_idx]; - otherport = ntohs(so_in6->sin6_port); - othernewport = otherport + nodes[thisidx].nodeid; - so_in6->sin6_port = (htons(othernewport)); - } - } - if (!globallistener) { - src = &nodes[thisidx].address[link_idx]; - } else { - if (nodes[thisidx].address[link_idx].ss_family == AF_INET) { - src = &allv4; - } else { - src = &allv6; - } - } - if (knet_link_set_config(knet_h, nodes[i].nodeid, link_idx, - protocol, src, - &nodes[i].address[link_idx]) < 0) { - printf("Unable to configure link: %s\n", strerror(errno)); - exit(FAIL); - } - if (portoffset) { - if (nodes[thisidx].address[link_idx].ss_family == AF_INET) { - so_in = (struct sockaddr_in *)&nodes[thisidx].address[link_idx]; - so_in->sin_port = (htons(thisport)); - so_in = (struct sockaddr_in *)&nodes[i].address[link_idx]; - so_in->sin_port = (htons(otherport)); - } else { - so_in6 = (struct sockaddr_in6 *)&nodes[thisidx].address[link_idx]; - so_in6->sin6_port = (htons(thisport)); - so_in6 = (struct sockaddr_in6 *)&nodes[i].address[link_idx]; - so_in6->sin6_port = (htons(otherport)); - } - } - if (knet_link_set_enable(knet_h, nodes[i].nodeid, link_idx, 1) < 0) { - printf("knet_link_set_enable failed: %s\n", strerror(errno)); - exit(FAIL); - } - } - } - - if (knet_handle_setfwd(knet_h, 1) < 0) { - printf("knet_handle_setfwd failed: %s\n", strerror(errno)); - exit(FAIL); - } - - if (wait) { - while(!allnodesup) { - allnodesup = 1; - for (i=0; i < onidx; i++) { - if (i == thisidx) { - continue; - } - if(knet_h->host_index[nodes[i].nodeid]->status.reachable != 1) { - printf("waiting host %d to be reachable\n", nodes[i].nodeid); - allnodesup = 0; - } - } - if (!allnodesup) { - sleep(1); - } - } - sleep(1); - } -} - -static int ping_dst_host_filter(void *pvt_data, - const unsigned char *outdata, - ssize_t outdata_len, - uint8_t tx_rx, - knet_node_id_t this_host_id, - knet_node_id_t src_host_id, - int8_t *dst_channel, - knet_node_id_t *dst_host_ids, - size_t *dst_host_ids_entries) -{ - if (broadcast_test) { - return 1; - } - - if (tx_rx == KNET_NOTIFY_TX) { - memmove(&dst_host_ids[0], outdata, 2); - } else { - dst_host_ids[0] = this_host_id; - } - *dst_host_ids_entries = 1; - return 0; -} - -static void *_rx_thread(void *args) -{ - fd_set rfds; - ssize_t len; - struct timeval tv; - struct sockaddr_storage address[PCKT_FRAG_MAX]; - struct mmsghdr msg[PCKT_FRAG_MAX]; - struct iovec iov_in[PCKT_FRAG_MAX]; - int i, msg_recv; - - for (i = 0; i < PCKT_FRAG_MAX; i++) { - rx_buf[i] = malloc(KNET_MAX_PACKET_SIZE); - if (!rx_buf[i]) { - printf("RXT: Unable to malloc!\n"); - return NULL; - } - memset(rx_buf[i], 0, KNET_MAX_PACKET_SIZE); - iov_in[i].iov_base = (void *)rx_buf[i]; - iov_in[i].iov_len = KNET_MAX_PACKET_SIZE; - memset(&msg[i].msg_hdr, 0, sizeof(struct msghdr)); - msg[i].msg_hdr.msg_name = &address[i]; - msg[i].msg_hdr.msg_namelen = sizeof(struct sockaddr_storage); - msg[i].msg_hdr.msg_iov = &iov_in[i]; - msg[i].msg_hdr.msg_iovlen = 1; - } - -select_loop: - tv.tv_sec = 5; - tv.tv_usec = 0; - - FD_ZERO(&rfds); - FD_SET(datafd, &rfds); - - len = select(FD_SETSIZE, &rfds, NULL, NULL, &tv); - if (len < 0) { - printf("RXT: Unable select over datafd\nHALTING RX THREAD!\n"); - return NULL; - } - if (!len) { - printf("RXT: No data for the past 5 seconds\n"); - } - if (FD_ISSET(datafd, &rfds)) { - msg_recv = _recvmmsg(datafd, &msg[0], PCKT_FRAG_MAX, MSG_DONTWAIT | MSG_NOSIGNAL); - if (msg_recv < 0) { - printf("RXT: error from recvmmsg: %s\n", strerror(errno)); - } - for (i = 0; i < msg_recv; i++) { - if (msg[i].msg_len == 0) { - printf("RXT: received 0 bytes message?\n"); - } - if (test_type == TEST_PING_AND_DATA) { - printf("received %lu bytes message: %s\n", (long)msg[i].msg_len, (char *)msg[i].msg_hdr.msg_iov->iov_base); - } - /* - * do stats here - */ - } - } - - goto select_loop; - - return NULL; -} - -static void setup_data_txrx_common(void) -{ - if (!rx_thread) { - if (knet_handle_enable_filter(knet_h, NULL, ping_dst_host_filter)) { - printf("Unable to enable dst_host_filter: %s\n", strerror(errno)); - exit(FAIL); - } - printf("Setting up rx thread\n"); - if (pthread_create(&rx_thread, 0, _rx_thread, NULL)) { - printf("Unable to start rx thread\n"); - exit(FAIL); - } - } -} - -static void stop_rx_thread(void) -{ - void *retval; - int i; - - if (rx_thread) { - printf("Shutting down rx thread\n"); - pthread_cancel(rx_thread); - pthread_join(rx_thread, &retval); - for (i = 0; i < PCKT_FRAG_MAX; i ++) { - free(rx_buf[i]); - } - } -} - -static void send_ping_data(void) -{ - const char *buf = "Hello world!\x0"; - ssize_t len = strlen(buf); - - if (knet_send(knet_h, buf, len, channel) != len) { - printf("Error sending hello world: %s\n", strerror(errno)); - } - sleep(1); -} - -static void cleanup_all(void) -{ - if (pthread_mutex_lock(&shutdown_mutex)) { - return; - } - - if (shutdown_in_progress) { - pthread_mutex_unlock(&shutdown_mutex); - return; - } - - shutdown_in_progress = 1; - - pthread_mutex_unlock(&shutdown_mutex); - - if (rx_thread) { - stop_rx_thread(); - } - knet_handle_stop(knet_h); -} - -static void sigint_handler(int signum) -{ - printf("Cleaning up... got signal: %d\n", signum); - cleanup_all(); - exit(PASS); -} - -int main(int argc, char *argv[]) -{ - if (signal(SIGINT, sigint_handler) == SIG_ERR) { - printf("Unable to configure SIGINT handler\n"); - exit(FAIL); - } - - need_root(); - - setup_knet(argc, argv); - -restart: - switch(test_type) { - default: - case TEST_PING: /* basic ping, no data */ - sleep(5); - break; - case TEST_PING_AND_DATA: - setup_data_txrx_common(); - send_ping_data(); - break; - case TEST_PERF: - setup_data_txrx_common(); - break; - } - if (continous) { - goto restart; - } - - cleanup_all(); - - return PASS; -} diff --git a/libknet/tests/pckt_test.c b/libknet/tests/pckt_test.c deleted file mode 100644 index 0dc32d99..00000000 --- a/libknet/tests/pckt_test.c +++ /dev/null @@ -1,19 +0,0 @@ -#include - -#include "onwire.h" - -int main(void) -{ - printf("\nKronosnet network header size printout:\n\n"); - printf("KNET_HEADER_ALL_SIZE: %zu\n", KNET_HEADER_ALL_SIZE); - printf("KNET_HEADER_SIZE: %zu\n", KNET_HEADER_SIZE); - printf("KNET_HEADER_PING_SIZE: %zu (%zu)\n", KNET_HEADER_PING_SIZE, sizeof(struct knet_header_payload_ping)); - printf("KNET_HEADER_PMTUD_SIZE: %zu (%zu)\n", KNET_HEADER_PMTUD_SIZE, sizeof(struct knet_header_payload_pmtud)); - printf("KNET_HEADER_DATA_SIZE: %zu (%zu)\n", KNET_HEADER_DATA_SIZE, sizeof(struct knet_header_payload_data)); - printf("\n"); - printf("KNET_HOSTINFO_ALL_SIZE: %zu\n", KNET_HOSTINFO_ALL_SIZE); - printf("KNET_HOSTINFO_SIZE: %zu\n", KNET_HOSTINFO_SIZE); - printf("KNET_HOSTINFO_LINK_STATUS_SIZE: %zu (%zu)\n", KNET_HOSTINFO_LINK_STATUS_SIZE, sizeof(struct knet_hostinfo_payload_link_status)); - - return 0; -} diff --git a/libknet/tests/test-common.c b/libknet/tests/test-common.c deleted file mode 100644 index 3e51d8f3..00000000 --- a/libknet/tests/test-common.c +++ /dev/null @@ -1,434 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "libknet.h" -#include "test-common.h" - -static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; -static int log_init = 0; -static pthread_mutex_t log_thread_mutex = PTHREAD_MUTEX_INITIALIZER; -static pthread_t log_thread; -static int log_thread_init = 0; -static int log_fds[2]; -struct log_thread_data { - int logfd; - FILE *std; -}; -static struct log_thread_data data; -static pthread_mutex_t shutdown_mutex = PTHREAD_MUTEX_INITIALIZER; -static int shutdown_in_progress = 0; - -static int _read_pipe(int fd, char **file, size_t *length) -{ - char buf[4096]; - int n; - int done = 0; - - *file = NULL; - *length = 0; - - memset(buf, 0, sizeof(buf)); - - while (!done) { - - n = read(fd, buf, sizeof(buf)); - - if (n < 0) { - if (errno == EINTR) - continue; - - if (*file) - free(*file); - - return n; - } - - if (n == 0 && (!*length)) - return 0; - - if (n == 0) - done = 1; - - if (*file) - *file = realloc(*file, (*length) + n + done); - else - *file = malloc(n + done); - - if (!*file) - return -1; - - memcpy((*file) + (*length), buf, n); - *length += (done + n); - } - - /* Null terminator */ - (*file)[(*length) - 1] = 0; - - return 0; -} - -int execute_shell(const char *command, char **error_string) -{ - pid_t pid; - int status, err = 0; - int fd[2]; - size_t size = 0; - - if ((command == NULL) || (!error_string)) { - errno = EINVAL; - return FAIL; - } - - *error_string = NULL; - - err = pipe(fd); - if (err) - goto out_clean; - - pid = fork(); - if (pid < 0) { - err = pid; - goto out_clean; - } - - if (pid) { /* parent */ - - close(fd[1]); - err = _read_pipe(fd[0], error_string, &size); - if (err) - goto out_clean0; - - waitpid(pid, &status, 0); - if (!WIFEXITED(status)) { - err = -1; - goto out_clean0; - } - if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { - err = WEXITSTATUS(status); - goto out_clean0; - } - goto out_clean0; - } else { /* child */ - close(0); - close(1); - close(2); - - close(fd[0]); - dup2(fd[1], 1); - dup2(fd[1], 2); - close(fd[1]); - - execlp("/bin/sh", "/bin/sh", "-c", command, NULL); - exit(FAIL); - } - -out_clean: - close(fd[1]); -out_clean0: - close(fd[0]); - - return err; -} - -int is_memcheck(void) -{ - char *val; - - val = getenv("KNETMEMCHECK"); - - if (val) { - if (!strncmp(val, "yes", 3)) { - return 1; - } - } - - return 0; -} - -int is_helgrind(void) -{ - char *val; - - val = getenv("KNETHELGRIND"); - - if (val) { - if (!strncmp(val, "yes", 3)) { - return 1; - } - } - - return 0; -} - -int need_root(void) -{ - if (geteuid() != 0) { - printf("This test requires root privileges\n"); - exit(SKIP); - } - - return PASS; -} - -void set_scheduler(int policy) -{ - struct sched_param sched_param; - int err; - - err = sched_get_priority_max(policy); - if (err < 0) { - printf("Could not get maximum scheduler priority\n"); - exit(FAIL); - } - sched_param.sched_priority = err; - err = sched_setscheduler(0, policy, &sched_param); - if (err < 0) { - printf("Could not set priority\n"); - exit(FAIL); - } - return; -} - -int setup_logpipes(int *logfds) -{ - if (pipe2(logfds, O_CLOEXEC | O_NONBLOCK) < 0) { - printf("Unable to setup logging pipe\n"); - exit(FAIL); - } - - return PASS; -} - -void close_logpipes(int *logfds) -{ - close(logfds[0]); - logfds[0] = 0; - close(logfds[1]); - logfds[1] = 0; -} - -void flush_logs(int logfd, FILE *std) -{ - struct knet_log_msg msg; - size_t bytes_read; - int len; - -next: - len = 0; - bytes_read = 0; - memset(&msg, 0, sizeof(struct knet_log_msg)); - - while (bytes_read < sizeof(struct knet_log_msg)) { - len = read(logfd, &msg + bytes_read, - sizeof(struct knet_log_msg) - bytes_read); - if (len <= 0) { - return; - } - bytes_read += len; - } - - if (len > 0) { - fprintf(std, "knet logs: [%s] %s: %s\n", - knet_log_get_loglevel_name(msg.msglevel), - knet_log_get_subsystem_name(msg.subsystem), - msg.msg); - goto next; - } -} - -static void *_logthread(void *args) -{ - fd_set rfds; - ssize_t len; - struct timeval tv; - -select_loop: - tv.tv_sec = 5; - tv.tv_usec = 0; - - FD_ZERO(&rfds); - FD_SET(data.logfd, &rfds); - - len = select(FD_SETSIZE, &rfds, NULL, NULL, &tv); - if (len < 0) { - fprintf(data.std, "Unable select over logfd!\nHALTING LOGTHREAD!\n"); - return NULL; - } - if (!len) { - fprintf(data.std, "knet logs: No logs in the last 5 seconds\n"); - } - if (FD_ISSET(data.logfd, &rfds)) { - flush_logs(data.logfd, data.std); - } - goto select_loop; - - return NULL; -} - -int start_logthread(int logfd, FILE *std) -{ - int savederrno = 0; - - savederrno = pthread_mutex_lock(&log_thread_mutex); - if (savederrno) { - printf("Unable to get log_thread mutex lock\n"); - return -1; - } - - if (!log_thread_init) { - data.logfd = logfd; - data.std = std; - - savederrno = pthread_create(&log_thread, 0, _logthread, NULL); - if (savederrno) { - printf("Unable to start logging thread: %s\n", strerror(savederrno)); - pthread_mutex_unlock(&log_thread_mutex); - return -1; - } - log_thread_init = 1; - } - - pthread_mutex_unlock(&log_thread_mutex); - return 0; -} - -int stop_logthread(void) -{ - int savederrno = 0; - void *retval; - - savederrno = pthread_mutex_lock(&log_thread_mutex); - if (savederrno) { - printf("Unable to get log_thread mutex lock\n"); - return -1; - } - - if (log_thread_init) { - pthread_cancel(log_thread); - pthread_join(log_thread, &retval); - log_thread_init = 0; - } - - pthread_mutex_unlock(&log_thread_mutex); - return 0; -} - -static void stop_logging(void) -{ - stop_logthread(); - flush_logs(log_fds[0], stdout); - close_logpipes(log_fds); -} - -int start_logging(FILE *std) -{ - int savederrno = 0; - - savederrno = pthread_mutex_lock(&log_mutex); - if (savederrno) { - printf("Unable to get log_mutex lock\n"); - return -1; - } - - if (!log_init) { - setup_logpipes(log_fds); - - if (atexit(&stop_logging) != 0) { - printf("Unable to register atexit handler to stop logging: %s\n", - strerror(errno)); - exit(FAIL); - } - - if (start_logthread(log_fds[0], std) < 0) { - exit(FAIL); - } - - log_init = 1; - } - - pthread_mutex_unlock(&log_mutex); - - return log_fds[1]; -} - -int knet_handle_stop(knet_handle_t knet_h) -{ - int i, j, savederrno; - knet_node_id_t host_ids[KNET_MAX_HOST]; - uint8_t link_ids[KNET_MAX_LINK]; - size_t host_ids_entries = 0, link_ids_entries = 0; - struct knet_link_status status; - - savederrno = pthread_mutex_lock(&shutdown_mutex); - if (savederrno) { - printf("Unable to get shutdown mutex lock\n"); - return -1; - } - - if (shutdown_in_progress) { - pthread_mutex_unlock(&shutdown_mutex); - errno = EINVAL; - return -1; - } - - shutdown_in_progress = 1; - - pthread_mutex_unlock(&shutdown_mutex); - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (knet_host_get_host_list(knet_h, host_ids, &host_ids_entries) < 0) { - printf("knet_host_get_host_list failed: %s\n", strerror(errno)); - return -1; - } - - for (i = 0; i < host_ids_entries; i++) { - if (knet_link_get_link_list(knet_h, host_ids[i], link_ids, &link_ids_entries)) { - printf("knet_link_get_link_list failed: %s\n", strerror(errno)); - return -1; - } - for (j = 0; j < link_ids_entries; j++) { - if (knet_link_get_status(knet_h, host_ids[i], link_ids[j], &status)) { - printf("knet_link_get_status failed: %s\n", strerror(errno)); - return -1; - } - if (status.enabled) { - if (knet_link_set_enable(knet_h, host_ids[i], j, 0)) { - printf("knet_link_set_enable failed: %s\n", strerror(errno)); - return -1; - } - } - knet_link_clear_config(knet_h, host_ids[i], j); - } - if (knet_host_remove(knet_h, host_ids[i]) < 0) { - printf("knet_host_remove failed: %s\n", strerror(errno)); - return -1; - } - } - - if (knet_handle_free(knet_h)) { - printf("knet_handle_free failed: %s\n", strerror(errno)); - return -1; - } - return 0; -} diff --git a/libknet/tests/test-common.h b/libknet/tests/test-common.h deleted file mode 100644 index ce532c6f..00000000 --- a/libknet/tests/test-common.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_TEST_COMMON_H__ -#define __KNET_TEST_COMMON_H__ - -#include "internals.h" -#include - -/* - * error codes from automake test-driver - */ - -#define PASS 0 -#define SKIP 77 -#define ERROR 99 -#define FAIL -1 - -/* For *BSD compatibility */ -#ifndef s6_addr16 -#define s6_addr8 __u6_addr.__u6_addr8 -#define s6_addr16 __u6_addr.__u6_addr16 -#define s6_addr32 __u6_addr.__u6_addr32 -#endif - -/* - * common facilities - */ - -int execute_shell(const char *command, char **error_string); - -int is_memcheck(void); -int is_helgrind(void); - -int need_root(void); - -void set_scheduler(int policy); - -/* - * consider moving this one as official API - */ -int knet_handle_stop(knet_handle_t knet_h); - -/* - * high level logging function. - * automatically setup logpipes and start/stop logging thread. - * - * start_logging exit(FAIL) on error or fd to pass to knet_handle_new - * and it will install an atexit handle to close logging properly - * - * WARNING: DO NOT use start_logging for api_ or int_ testing. - * while start_logging would work just fine, the output - * of the logs is more complex to read because of the way - * the thread would interleave the output of printf from api_/int_ testing - * with knet logs. Functionally speaking you get the exact same logs, - * but a lot harder to read due to the thread latency in printing logs. - */ -int start_logging(FILE *std); - -int setup_logpipes(int *logfds); -void close_logpipes(int *logfds); -void flush_logs(int logfd, FILE *std); -int start_logthread(int logfd, FILE *std); -int stop_logthread(void); - -#endif diff --git a/libknet/threads_common.c b/libknet/threads_common.c deleted file mode 100644 index 8afe1aa2..00000000 --- a/libknet/threads_common.c +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include - -#include "internals.h" -#include "logging.h" -#include "threads_common.h" - -int shutdown_in_progress(knet_handle_t knet_h) -{ - int savederrno = 0; - int ret; - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_COMMON, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - ret = knet_h->fini_in_progress; - - pthread_rwlock_unlock(&knet_h->global_rwlock); - - return ret; -} diff --git a/libknet/threads_common.h b/libknet/threads_common.h deleted file mode 100644 index d849e6c7..00000000 --- a/libknet/threads_common.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_THREADS_COMMON_H__ -#define __KNET_THREADS_COMMON_H__ - -#include "internals.h" - -#define KNET_THREADS_TIMERES 200000 - -#define timespec_diff(start, end, diff) \ -do { \ - if (end.tv_sec > start.tv_sec) \ - *(diff) = ((end.tv_sec - start.tv_sec) * 1000000000llu) \ - + end.tv_nsec - start.tv_nsec; \ - else \ - *(diff) = end.tv_nsec - start.tv_nsec; \ -} while (0); - -int shutdown_in_progress(knet_handle_t knet_h); - -#endif diff --git a/libknet/threads_dsthandler.c b/libknet/threads_dsthandler.c deleted file mode 100644 index 3b1612cc..00000000 --- a/libknet/threads_dsthandler.c +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include - -#include "host.h" -#include "compat.h" -#include "logging.h" -#include "threads_common.h" -#include "threads_dsthandler.h" - -static void _handle_dst_link_updates(knet_handle_t knet_h) -{ - knet_node_id_t host_id; - struct knet_host *host; - - if (recv(knet_h->dstsockfd[0], &host_id, sizeof(host_id), MSG_DONTWAIT | MSG_NOSIGNAL) != sizeof(host_id)) { - log_debug(knet_h, KNET_SUB_DSTCACHE, "Short read on dstsockfd"); - return; - } - - if (pthread_rwlock_wrlock(&knet_h->global_rwlock) != 0) { - log_debug(knet_h, KNET_SUB_DSTCACHE, "Unable to get read lock"); - return; - } - - host = knet_h->host_index[host_id]; - if (!host) { - log_debug(knet_h, KNET_SUB_DSTCACHE, "Unable to find host: %u", host_id); - goto out_unlock; - } - - _host_dstcache_update_sync(knet_h, host); - -out_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); - - return; -} - -void *_handle_dst_link_handler_thread(void *data) -{ - knet_handle_t knet_h = (knet_handle_t) data; - struct epoll_event events[KNET_EPOLL_MAX_EVENTS]; - - while (!shutdown_in_progress(knet_h)) { - if (epoll_wait(knet_h->dst_link_handler_epollfd, events, KNET_EPOLL_MAX_EVENTS, -1) >= 1) - _handle_dst_link_updates(knet_h); - } - - return NULL; -} diff --git a/libknet/threads_dsthandler.h b/libknet/threads_dsthandler.h deleted file mode 100644 index 155a547a..00000000 --- a/libknet/threads_dsthandler.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_THREADS_DSTHANDLER_H__ -#define __KNET_THREADS_DSTHANDLER_H__ - -void *_handle_dst_link_handler_thread(void *data); - -#endif diff --git a/libknet/threads_heartbeat.c b/libknet/threads_heartbeat.c deleted file mode 100644 index 77727245..00000000 --- a/libknet/threads_heartbeat.c +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "crypto.h" -#include "link.h" -#include "logging.h" -#include "threads_common.h" -#include "threads_heartbeat.h" - -static void _link_down(knet_handle_t knet_h, struct knet_host *dst_host, struct knet_link *dst_link) -{ - memset(&dst_link->pmtud_last, 0, sizeof(struct timespec)); - dst_link->received_pong = 0; - dst_link->status.pong_last.tv_nsec = 0; - if (dst_link->status.connected == 1) { - log_info(knet_h, KNET_SUB_LINK, "host: %u link: %u is down", - dst_host->host_id, dst_link->link_id); - _link_updown(knet_h, dst_host->host_id, dst_link->link_id, dst_link->status.enabled, 0); - } -} - -static void _handle_check_each(knet_handle_t knet_h, struct knet_host *dst_host, struct knet_link *dst_link, int timed) -{ - int err = 0, savederrno = 0; - int len; - ssize_t outlen = KNET_HEADER_PING_SIZE; - struct timespec clock_now, pong_last; - unsigned long long diff_ping; - unsigned char *outbuf = (unsigned char *)knet_h->pingbuf; - - if (dst_link->transport_connected == 0) { - _link_down(knet_h, dst_host, dst_link); - return; - } - - /* caching last pong to avoid race conditions */ - pong_last = dst_link->status.pong_last; - - if (clock_gettime(CLOCK_MONOTONIC, &clock_now) != 0) { - log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to get monotonic clock"); - return; - } - - timespec_diff(dst_link->ping_last, clock_now, &diff_ping); - - if ((diff_ping >= (dst_link->ping_interval * 1000llu)) || (!timed)) { - memmove(&knet_h->pingbuf->khp_ping_time[0], &clock_now, sizeof(struct timespec)); - knet_h->pingbuf->khp_ping_link = dst_link->link_id; - if (pthread_mutex_lock(&knet_h->tx_seq_num_mutex)) { - log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to get seq mutex lock"); - return; - } - knet_h->pingbuf->khp_ping_seq_num = htons(knet_h->tx_seq_num); - pthread_mutex_unlock(&knet_h->tx_seq_num_mutex); - knet_h->pingbuf->khp_ping_timed = timed; - - if (knet_h->crypto_instance) { - if (crypto_encrypt_and_sign(knet_h, - (const unsigned char *)knet_h->pingbuf, - outlen, - knet_h->pingbuf_crypt, - &outlen) < 0) { - log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to crypto ping packet"); - return; - } - - outbuf = knet_h->pingbuf_crypt; - } - -retry: - len = sendto(dst_link->outsock, outbuf, outlen, - MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) &dst_link->dst_addr, - sizeof(struct sockaddr_storage)); - - dst_link->ping_last = clock_now; - - if (len != outlen) { - err = knet_h->transport_ops[dst_link->transport_type]->transport_tx_sock_error(knet_h, dst_link->outsock, len, savederrno); - switch(err) { - case -1: /* unrecoverable error */ - log_debug(knet_h, KNET_SUB_HEARTBEAT, - "Unable to send ping (sock: %d) packet (sendto): %d %s. recorded src ip: %s src port: %s dst ip: %s dst port: %s", - dst_link->outsock, errno, strerror(errno), - dst_link->status.src_ipaddr, dst_link->status.src_port, - dst_link->status.dst_ipaddr, dst_link->status.dst_port); - break; - case 0: - break; - case 1: - goto retry; - break; - } - } else { - dst_link->last_ping_size = outlen; - } - } - - timespec_diff(pong_last, clock_now, &diff_ping); - if ((pong_last.tv_nsec) && - (diff_ping >= (dst_link->pong_timeout * 1000llu))) { - _link_down(knet_h, dst_host, dst_link); - } -} - -void _send_pings(knet_handle_t knet_h, int timed) -{ - struct knet_host *dst_host; - int link_idx; - - if (pthread_mutex_lock(&knet_h->hb_mutex)) { - log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to get hb mutex lock"); - return; - } - - for (dst_host = knet_h->host_head; dst_host != NULL; dst_host = dst_host->next) { - for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { - if ((dst_host->link[link_idx].status.enabled != 1) || - ((dst_host->link[link_idx].dynamic == KNET_LINK_DYNIP) && - (dst_host->link[link_idx].status.dynconnected != 1))) - continue; - - _handle_check_each(knet_h, dst_host, &dst_host->link[link_idx], timed); - } - } - - pthread_mutex_unlock(&knet_h->hb_mutex); -} - -void *_handle_heartbt_thread(void *data) -{ - knet_handle_t knet_h = (knet_handle_t) data; - - /* preparing ping buffer */ - knet_h->pingbuf->kh_version = KNET_HEADER_VERSION; - knet_h->pingbuf->kh_type = KNET_HEADER_TYPE_PING; - knet_h->pingbuf->kh_node = htons(knet_h->host_id); - - while (!shutdown_in_progress(knet_h)) { - usleep(KNET_THREADS_TIMERES); - - if (pthread_rwlock_rdlock(&knet_h->global_rwlock) != 0) { - log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to get read lock"); - continue; - } - - _send_pings(knet_h, 1); - - pthread_rwlock_unlock(&knet_h->global_rwlock); - } - - return NULL; -} - diff --git a/libknet/threads_heartbeat.h b/libknet/threads_heartbeat.h deleted file mode 100644 index a966586f..00000000 --- a/libknet/threads_heartbeat.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_THREADS_HEARTBEAT_H__ -#define __KNET_THREADS_HEARTBEAT_H__ - -void _send_pings(knet_handle_t knet_h, int timed); -void *_handle_heartbt_thread(void *data); - -#endif diff --git a/libknet/threads_pmtud.c b/libknet/threads_pmtud.c deleted file mode 100644 index 0df8b4fe..00000000 --- a/libknet/threads_pmtud.c +++ /dev/null @@ -1,404 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include - -#include "crypto.h" -#include "link.h" -#include "host.h" -#include "logging.h" -#include "threads_common.h" -#include "threads_pmtud.h" - -static int _handle_check_link_pmtud(knet_handle_t knet_h, struct knet_host *dst_host, struct knet_link *dst_link) -{ - int err, ret, savederrno, mutex_retry_limit, failsafe; - ssize_t onwire_len; /* current packet onwire size */ - ssize_t overhead_len; /* onwire packet overhead (protocol based) */ - ssize_t max_mtu_len; /* max mtu for protocol */ - ssize_t data_len; /* how much data we can send in the packet - * generally would be onwire_len - overhead_len - * needs to be adjusted for crypto - */ - ssize_t pad_len; /* crypto packet pad size, needs to move into crypto.c callbacks */ - int len; /* len of what we were able to sendto onwire */ - - struct timespec ts; - unsigned char *outbuf = (unsigned char *)knet_h->pmtudbuf; - - mutex_retry_limit = 0; - failsafe = 0; - pad_len = 0; - - dst_link->last_bad_mtu = 0; - - knet_h->pmtudbuf->khp_pmtud_link = dst_link->link_id; - - switch (dst_link->dst_addr.ss_family) { - case AF_INET6: - max_mtu_len = KNET_PMTUD_SIZE_V6; - overhead_len = KNET_PMTUD_OVERHEAD_V6 + dst_link->proto_overhead; - dst_link->last_good_mtu = dst_link->last_ping_size + overhead_len; - break; - case AF_INET: - max_mtu_len = KNET_PMTUD_SIZE_V4; - overhead_len = KNET_PMTUD_OVERHEAD_V4 + dst_link->proto_overhead; - dst_link->last_good_mtu = dst_link->last_ping_size + overhead_len; - break; - default: - log_debug(knet_h, KNET_SUB_PMTUD, "PMTUD aborted, unknown protocol"); - return -1; - break; - } - - /* - * discovery starts from the top because kernel will - * refuse to send packets > current iface mtu. - * this saves us some time and network bw. - */ - onwire_len = max_mtu_len; - -restart: - - /* - * prevent a race when interface mtu is changed _exactly_ during - * the discovery process and it's complex to detect. Easier - * to wait the next loop. - * 30 is not an arbitrary value. To bisect from 576 to 128000 doesn't - * take more than 18/19 steps. - */ - - if (failsafe == 30) { - log_err(knet_h, KNET_SUB_PMTUD, - "Aborting PMTUD process: Too many attempts. MTU might have changed during discovery."); - return -1; - } else { - failsafe++; - } - - data_len = onwire_len - overhead_len; - - if (knet_h->crypto_instance) { - - if (knet_h->sec_block_size) { - pad_len = knet_h->sec_block_size - (data_len % knet_h->sec_block_size); - if (pad_len == knet_h->sec_block_size) { - pad_len = 0; - } - data_len = data_len + pad_len; - } - - data_len = data_len + (knet_h->sec_hash_size + knet_h->sec_salt_size + knet_h->sec_block_size); - - if (knet_h->sec_block_size) { - while (data_len + overhead_len >= max_mtu_len) { - data_len = data_len - knet_h->sec_block_size; - } - } - - if (dst_link->last_bad_mtu) { - while (data_len + overhead_len >= dst_link->last_bad_mtu) { - data_len = data_len - (knet_h->sec_hash_size + knet_h->sec_salt_size + knet_h->sec_block_size); - } - } - - if (data_len < (knet_h->sec_hash_size + knet_h->sec_salt_size + knet_h->sec_block_size) + 1) { - log_debug(knet_h, KNET_SUB_PMTUD, "Aborting PMTUD process: link mtu smaller than crypto header detected (link might have been disconnected)"); - return -1; - } - - onwire_len = data_len + overhead_len; - knet_h->pmtudbuf->khp_pmtud_size = onwire_len; - - if (crypto_encrypt_and_sign(knet_h, - (const unsigned char *)knet_h->pmtudbuf, - data_len - (knet_h->sec_hash_size + knet_h->sec_salt_size + knet_h->sec_block_size), - knet_h->pmtudbuf_crypt, - &data_len) < 0) { - log_debug(knet_h, KNET_SUB_PMTUD, "Unable to crypto pmtud packet"); - return -1; - } - - outbuf = knet_h->pmtudbuf_crypt; - - } else { - - knet_h->pmtudbuf->khp_pmtud_size = onwire_len; - - } - - /* link has gone down, aborting pmtud */ - if (dst_link->status.connected != 1) { - log_debug(knet_h, KNET_SUB_PMTUD, "PMTUD detected host (%u) link (%u) has been disconnected", dst_host->host_id, dst_link->link_id); - return -1; - } - - if (dst_link->transport_connected != 1) { - log_debug(knet_h, KNET_SUB_PMTUD, "PMTUD detected host (%u) link (%u) has been disconnected", dst_host->host_id, dst_link->link_id); - return -1; - } - - if (pthread_mutex_lock(&knet_h->pmtud_mutex) != 0) { - log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get mutex lock"); - return -1; - } - -retry: - len = sendto(dst_link->outsock, outbuf, data_len, - MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) &dst_link->dst_addr, - sizeof(struct sockaddr_storage)); - savederrno = errno; - - err = knet_h->transport_ops[dst_link->transport_type]->transport_tx_sock_error(knet_h, dst_link->outsock, len, savederrno); - switch(err) { - case -1: /* unrecoverable error */ - log_debug(knet_h, KNET_SUB_PMTUD, "Unable to send pmtu packet (sendto): %d %s", savederrno, strerror(savederrno)); - pthread_mutex_unlock(&knet_h->pmtud_mutex); - return -1; - case 0: /* ignore error and continue */ - break; - case 1: /* retry to send those same data */ - goto retry; - break; - } - - if (len != data_len) { - if (savederrno == EMSGSIZE) { - dst_link->last_bad_mtu = onwire_len; - } else { - log_debug(knet_h, KNET_SUB_PMTUD, "Unable to send pmtu packet len: %zu err: %s", onwire_len, strerror(savederrno)); - } - - } else { - dst_link->last_sent_mtu = onwire_len; - dst_link->last_recv_mtu = 0; - - if (clock_gettime(CLOCK_REALTIME, &ts) < 0) { - log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get current time: %s", strerror(errno)); - pthread_mutex_unlock(&knet_h->pmtud_mutex); - return -1; - } - - /* - * Set an artibrary 2 seconds timeout to receive a PMTUd reply - * perhaps this should be configurable but: - * 1) too short timeout can cause instability since MTU value - * influeces link status - * 2) too high timeout slows down the MTU detection process for - * small MTU - * - * Another option is to make the PMTUd process less influent - * in link status detection but that could cause data packet loss - * without link up/down changes - */ - ts.tv_sec += 2; - ret = pthread_cond_timedwait(&knet_h->pmtud_cond, &knet_h->pmtud_mutex, &ts); - - if (shutdown_in_progress(knet_h)) { - pthread_mutex_unlock(&knet_h->pmtud_mutex); - log_debug(knet_h, KNET_SUB_PMTUD, "PMTUD aborted. shutdown in progress"); - return -1; - } - - if ((ret != 0) && (ret != ETIMEDOUT)) { - pthread_mutex_unlock(&knet_h->pmtud_mutex); - if (mutex_retry_limit == 3) { - log_debug(knet_h, KNET_SUB_PMTUD, "PMTUD aborted, unable to get mutex lock"); - return -1; - } - mutex_retry_limit++; - goto restart; - } - - if ((dst_link->last_recv_mtu != onwire_len) || (ret)) { - dst_link->last_bad_mtu = onwire_len; - } else { - int found_mtu = 0; - - if (knet_h->sec_block_size) { - if ((onwire_len + knet_h->sec_block_size >= max_mtu_len) || - ((dst_link->last_bad_mtu) && (dst_link->last_bad_mtu <= (onwire_len + knet_h->sec_block_size)))) { - found_mtu = 1; - } - } else { - if ((onwire_len == max_mtu_len) || - ((dst_link->last_bad_mtu) && (dst_link->last_bad_mtu == (onwire_len + 1)))) { - found_mtu = 1; - } - } - - if (found_mtu) { - /* - * account for IP overhead, knet headers and crypto in PMTU calculation - */ - dst_link->status.mtu = onwire_len - dst_link->status.proto_overhead; - pthread_mutex_unlock(&knet_h->pmtud_mutex); - return 0; - } - - dst_link->last_good_mtu = onwire_len; - } - } - - onwire_len = (dst_link->last_good_mtu + dst_link->last_bad_mtu) / 2; - pthread_mutex_unlock(&knet_h->pmtud_mutex); - - goto restart; -} - -static int _handle_check_pmtud(knet_handle_t knet_h, struct knet_host *dst_host, struct knet_link *dst_link, unsigned int *min_mtu) -{ - uint8_t saved_valid_pmtud; - unsigned int saved_pmtud; - struct timespec clock_now; - unsigned long long diff_pmtud, interval; - - interval = knet_h->pmtud_interval * 1000000000llu; /* nanoseconds */ - - if (clock_gettime(CLOCK_MONOTONIC, &clock_now) != 0) { - log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get monotonic clock"); - return 0; - } - - timespec_diff(dst_link->pmtud_last, clock_now, &diff_pmtud); - - if (diff_pmtud < interval) { - *min_mtu = dst_link->status.mtu; - return dst_link->has_valid_mtu; - } - - switch (dst_link->dst_addr.ss_family) { - case AF_INET6: - dst_link->status.proto_overhead = KNET_PMTUD_OVERHEAD_V6 + dst_link->proto_overhead + KNET_HEADER_ALL_SIZE + knet_h->sec_header_size; - break; - case AF_INET: - dst_link->status.proto_overhead = KNET_PMTUD_OVERHEAD_V4 + dst_link->proto_overhead + KNET_HEADER_ALL_SIZE + knet_h->sec_header_size; - break; - } - - saved_pmtud = dst_link->status.mtu; - saved_valid_pmtud = dst_link->has_valid_mtu; - - log_debug(knet_h, KNET_SUB_PMTUD, "Starting PMTUD for host: %u link: %u", dst_host->host_id, dst_link->link_id); - - if (_handle_check_link_pmtud(knet_h, dst_host, dst_link) < 0) { - dst_link->has_valid_mtu = 0; - } else { - dst_link->has_valid_mtu = 1; - switch (dst_link->dst_addr.ss_family) { - case AF_INET6: - if (((dst_link->status.mtu + dst_link->status.proto_overhead) < KNET_PMTUD_MIN_MTU_V6) || - ((dst_link->status.mtu + dst_link->status.proto_overhead) > KNET_PMTUD_SIZE_V6)) { - log_debug(knet_h, KNET_SUB_PMTUD, - "PMTUD detected an IPv6 MTU out of bound value (%u) for host: %u link: %u.", - dst_link->status.mtu + dst_link->status.proto_overhead, dst_host->host_id, dst_link->link_id); - dst_link->has_valid_mtu = 0; - } - break; - case AF_INET: - if (((dst_link->status.mtu + dst_link->status.proto_overhead) < KNET_PMTUD_MIN_MTU_V4) || - ((dst_link->status.mtu + dst_link->status.proto_overhead) > KNET_PMTUD_SIZE_V4)) { - log_debug(knet_h, KNET_SUB_PMTUD, - "PMTUD detected an IPv4 MTU out of bound value (%u) for host: %u link: %u.", - dst_link->status.mtu + dst_link->status.proto_overhead, dst_host->host_id, dst_link->link_id); - dst_link->has_valid_mtu = 0; - } - break; - } - if (dst_link->has_valid_mtu) { - if ((saved_pmtud) && (saved_pmtud != dst_link->status.mtu)) { - log_info(knet_h, KNET_SUB_PMTUD, "PMTUD link change for host: %u link: %u from %u to %u", - dst_host->host_id, dst_link->link_id, saved_pmtud, dst_link->status.mtu); - } - log_debug(knet_h, KNET_SUB_PMTUD, "PMTUD completed for host: %u link: %u current link mtu: %u", - dst_host->host_id, dst_link->link_id, dst_link->status.mtu); - if (dst_link->status.mtu < *min_mtu) { - *min_mtu = dst_link->status.mtu; - } - dst_link->pmtud_last = clock_now; - } - } - - if (saved_valid_pmtud != dst_link->has_valid_mtu) { - _host_dstcache_update_sync(knet_h, dst_host); - } - - return dst_link->has_valid_mtu; -} - -void *_handle_pmtud_link_thread(void *data) -{ - knet_handle_t knet_h = (knet_handle_t) data; - struct knet_host *dst_host; - struct knet_link *dst_link; - int link_idx; - unsigned int min_mtu, have_mtu; - unsigned int lower_mtu; - - knet_h->data_mtu = KNET_PMTUD_MIN_MTU_V4 - KNET_HEADER_ALL_SIZE - knet_h->sec_header_size; - - /* preparing pmtu buffer */ - knet_h->pmtudbuf->kh_version = KNET_HEADER_VERSION; - knet_h->pmtudbuf->kh_type = KNET_HEADER_TYPE_PMTUD; - knet_h->pmtudbuf->kh_node = htons(knet_h->host_id); - - while (!shutdown_in_progress(knet_h)) { - usleep(KNET_THREADS_TIMERES); - - if (pthread_rwlock_rdlock(&knet_h->global_rwlock) != 0) { - log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get read lock"); - continue; - } - - lower_mtu = KNET_PMTUD_SIZE_V4; - min_mtu = KNET_PMTUD_SIZE_V4 - KNET_HEADER_ALL_SIZE - knet_h->sec_header_size; - have_mtu = 0; - - for (dst_host = knet_h->host_head; dst_host != NULL; dst_host = dst_host->next) { - for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { - dst_link = &dst_host->link[link_idx]; - - if ((dst_link->status.enabled != 1) || - (dst_link->status.connected != 1) || - (!dst_link->last_ping_size) || - ((dst_link->dynamic == KNET_LINK_DYNIP) && - (dst_link->status.dynconnected != 1))) - continue; - - if (_handle_check_pmtud(knet_h, dst_host, dst_link, &min_mtu)) { - have_mtu = 1; - if (min_mtu < lower_mtu) { - lower_mtu = min_mtu; - } - } - } - } - - if (have_mtu) { - if (knet_h->data_mtu != lower_mtu) { - knet_h->data_mtu = lower_mtu; - log_info(knet_h, KNET_SUB_PMTUD, "Global data MTU changed to: %u", knet_h->data_mtu); - - if (knet_h->pmtud_notify_fn) { - knet_h->pmtud_notify_fn(knet_h->pmtud_notify_fn_private_data, - knet_h->data_mtu); - } - } - } - - pthread_rwlock_unlock(&knet_h->global_rwlock); - } - - return NULL; -} diff --git a/libknet/threads_pmtud.h b/libknet/threads_pmtud.h deleted file mode 100644 index ed386435..00000000 --- a/libknet/threads_pmtud.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_THREADS_PMTUD_H__ -#define __KNET_THREADS_PMTUD_H__ - -void *_handle_pmtud_link_thread(void *data); - -#endif diff --git a/libknet/threads_rx.c b/libknet/threads_rx.c deleted file mode 100644 index 7dce1e2e..00000000 --- a/libknet/threads_rx.c +++ /dev/null @@ -1,727 +0,0 @@ -/* - * Copyright (C) 2010-2017 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include - -#include "compat.h" -#include "crypto.h" -#include "host.h" -#include "link.h" -#include "logging.h" -#include "transports.h" -#include "threads_common.h" -#include "threads_heartbeat.h" -#include "threads_rx.h" -#include "netutils.h" - -/* - * RECV - */ - -/* - * return 1 if a > b - * return -1 if b > a - * return 0 if they are equal - */ -static inline int timecmp(struct timespec a, struct timespec b) -{ - if (a.tv_sec != b.tv_sec) { - if (a.tv_sec > b.tv_sec) { - return 1; - } else { - return -1; - } - } else { - if (a.tv_nsec > b.tv_nsec) { - return 1; - } else if (a.tv_nsec < b.tv_nsec) { - return -1; - } else { - return 0; - } - } -} - -/* - * this functions needs to return an index (0 to 7) - * to a knet_host_defrag_buf. (-1 on errors) - */ - -static int find_pckt_defrag_buf(knet_handle_t knet_h, struct knet_header *inbuf) -{ - struct knet_host *src_host = knet_h->host_index[inbuf->kh_node]; - int i, oldest; - - /* - * check if there is a buffer already in use handling the same seq_num - */ - for (i = 0; i < KNET_MAX_LINK; i++) { - if (src_host->defrag_buf[i].in_use) { - if (src_host->defrag_buf[i].pckt_seq == inbuf->khp_data_seq_num) { - return i; - } - } - } - - /* - * If there is no buffer that's handling the current seq_num - * either it's new or it's been reclaimed already. - * check if it's been reclaimed/seen before using the defrag circular - * buffer. If the pckt has been seen before, the buffer expired (ETIME) - * and there is no point to try to defrag it again. - */ - if (!_seq_num_lookup(src_host, inbuf->khp_data_seq_num, 1, 0)) { - errno = ETIME; - return -1; - } - - /* - * register the pckt as seen - */ - _seq_num_set(src_host, inbuf->khp_data_seq_num, 1); - - /* - * see if there is a free buffer - */ - for (i = 0; i < KNET_MAX_LINK; i++) { - if (!src_host->defrag_buf[i].in_use) { - return i; - } - } - - /* - * at this point, there are no free buffers, the pckt is new - * and we need to reclaim a buffer, and we will take the one - * with the oldest timestamp. It's as good as any. - */ - - oldest = 0; - - for (i = 0; i < KNET_MAX_LINK; i++) { - if (timecmp(src_host->defrag_buf[i].last_update, src_host->defrag_buf[oldest].last_update) < 0) { - oldest = i; - } - } - src_host->defrag_buf[oldest].in_use = 0; - return oldest; -} - -static int pckt_defrag(knet_handle_t knet_h, struct knet_header *inbuf, ssize_t *len) -{ - struct knet_host_defrag_buf *defrag_buf; - int defrag_buf_idx; - - defrag_buf_idx = find_pckt_defrag_buf(knet_h, inbuf); - if (defrag_buf_idx < 0) { - if (errno == ETIME) { - log_debug(knet_h, KNET_SUB_RX, "Defrag buffer expired"); - } - return 1; - } - - defrag_buf = &knet_h->host_index[inbuf->kh_node]->defrag_buf[defrag_buf_idx]; - - /* - * if the buf is not is use, then make sure it's clean - */ - if (!defrag_buf->in_use) { - memset(defrag_buf, 0, sizeof(struct knet_host_defrag_buf)); - defrag_buf->in_use = 1; - defrag_buf->pckt_seq = inbuf->khp_data_seq_num; - } - - /* - * update timestamp on the buffer - */ - clock_gettime(CLOCK_MONOTONIC, &defrag_buf->last_update); - - /* - * check if we already received this fragment - */ - if (defrag_buf->frag_map[inbuf->khp_data_frag_seq]) { - /* - * if we have received this fragment and we didn't clear the buffer - * it means that we don't have all fragments yet - */ - return 1; - } - - /* - * we need to handle the last packet with gloves due to its different size - */ - - if (inbuf->khp_data_frag_seq == inbuf->khp_data_frag_num) { - defrag_buf->last_frag_size = *len; - - /* - * in the event when the last packet arrives first, - * we still don't know the offset vs the other fragments (based on MTU), - * so we store the fragment at the end of the buffer where it's safe - * and take a copy of the len so that we can restore its offset later. - * remember we can't use the local MTU for this calculation because pMTU - * can be asymettric between the same hosts. - */ - if (!defrag_buf->frag_size) { - defrag_buf->last_first = 1; - memmove(defrag_buf->buf + (KNET_MAX_PACKET_SIZE - *len), - inbuf->khp_data_userdata, - *len); - } - } else { - defrag_buf->frag_size = *len; - } - - memmove(defrag_buf->buf + ((inbuf->khp_data_frag_seq - 1) * defrag_buf->frag_size), - inbuf->khp_data_userdata, *len); - - defrag_buf->frag_recv++; - defrag_buf->frag_map[inbuf->khp_data_frag_seq] = 1; - - /* - * check if we received all the fragments - */ - if (defrag_buf->frag_recv == inbuf->khp_data_frag_num) { - /* - * special case the last pckt - */ - - if (defrag_buf->last_first) { - memmove(defrag_buf->buf + ((inbuf->khp_data_frag_num - 1) * defrag_buf->frag_size), - defrag_buf->buf + (KNET_MAX_PACKET_SIZE - defrag_buf->last_frag_size), - defrag_buf->last_frag_size); - } - - /* - * recalculate packet lenght - */ - - *len = ((inbuf->khp_data_frag_num - 1) * defrag_buf->frag_size) + defrag_buf->last_frag_size; - - /* - * copy the pckt back in the user data - */ - memmove(inbuf->khp_data_userdata, defrag_buf->buf, *len); - - /* - * free this buffer - */ - defrag_buf->in_use = 0; - return 0; - } - - return 1; -} - -static void _parse_recv_from_links(knet_handle_t knet_h, int sockfd, const struct knet_mmsghdr *msg) -{ - int err = 0, savederrno = 0; - ssize_t outlen; - struct knet_host *src_host; - struct knet_link *src_link; - unsigned long long latency_last; - knet_node_id_t dst_host_ids[KNET_MAX_HOST]; - size_t dst_host_ids_entries = 0; - int bcast = 1; - struct timespec recvtime; - struct knet_header *inbuf = msg->msg_hdr.msg_iov->iov_base; - unsigned char *outbuf = (unsigned char *)msg->msg_hdr.msg_iov->iov_base; - ssize_t len = msg->msg_len; - struct knet_hostinfo *knet_hostinfo; - struct iovec iov_out[1]; - int8_t channel; - struct sockaddr_storage pckt_src; - seq_num_t recv_seq_num; - int wipe_bufs = 0; - - if (knet_h->crypto_instance) { - if (crypto_authenticate_and_decrypt(knet_h, - (unsigned char *)inbuf, - len, - knet_h->recv_from_links_buf_decrypt, - &outlen) < 0) { - log_debug(knet_h, KNET_SUB_RX, "Unable to decrypt/auth packet"); - return; - } - len = outlen; - inbuf = (struct knet_header *)knet_h->recv_from_links_buf_decrypt; - } - - if (len < (KNET_HEADER_SIZE + 1)) { - log_debug(knet_h, KNET_SUB_RX, "Packet is too short: %ld", len); - return; - } - - if (inbuf->kh_version != KNET_HEADER_VERSION) { - log_debug(knet_h, KNET_SUB_RX, "Packet version does not match"); - return; - } - - inbuf->kh_node = ntohs(inbuf->kh_node); - src_host = knet_h->host_index[inbuf->kh_node]; - if (src_host == NULL) { /* host not found */ - log_debug(knet_h, KNET_SUB_RX, "Unable to find source host for this packet"); - return; - } - - src_link = NULL; - - if ((inbuf->kh_type & KNET_HEADER_TYPE_PMSK) != 0) { - src_link = src_host->link + - (inbuf->khp_ping_link % KNET_MAX_LINK); - if (src_link->dynamic == KNET_LINK_DYNIP) { - /* - * cpyaddrport will only copy address and port of the incoming - * packet and strip extra bits such as flow and scopeid - */ - cpyaddrport(&pckt_src, msg->msg_hdr.msg_name); - - if (cmpaddr(&src_link->dst_addr, sockaddr_len(&src_link->dst_addr), - &pckt_src, sockaddr_len(&pckt_src)) != 0) { - log_debug(knet_h, KNET_SUB_RX, "host: %u link: %u appears to have changed ip address", - src_host->host_id, src_link->link_id); - memmove(&src_link->dst_addr, &pckt_src, sizeof(struct sockaddr_storage)); - if (knet_addrtostr(&src_link->dst_addr, sockaddr_len(msg->msg_hdr.msg_name), - src_link->status.dst_ipaddr, KNET_MAX_HOST_LEN, - src_link->status.dst_port, KNET_MAX_PORT_LEN) != 0) { - log_debug(knet_h, KNET_SUB_RX, "Unable to resolve ???"); - snprintf(src_link->status.dst_ipaddr, KNET_MAX_HOST_LEN - 1, "Unknown!!!"); - snprintf(src_link->status.dst_port, KNET_MAX_PORT_LEN - 1, "??"); - } else { - log_info(knet_h, KNET_SUB_RX, - "host: %u link: %u new connection established from: %s %s", - src_host->host_id, src_link->link_id, - src_link->status.dst_ipaddr, src_link->status.dst_port); - } - } - /* - * transport has already accepted the connection here - * otherwise we would not be receiving packets - */ - knet_h->transport_ops[src_link->transport_type]->transport_link_dyn_connect(knet_h, sockfd, src_link); - } - } - - switch (inbuf->kh_type) { - case KNET_HEADER_TYPE_HOST_INFO: - case KNET_HEADER_TYPE_DATA: - /* - * TODO: should we accept data even if we can't reply to the other node? - * how would that work with SCTP and guaranteed delivery? - */ - - if (!src_host->status.reachable) { - log_debug(knet_h, KNET_SUB_RX, "Source host %u not reachable yet", src_host->host_id); - //return; - } - inbuf->khp_data_seq_num = ntohs(inbuf->khp_data_seq_num); - channel = inbuf->khp_data_channel; - src_host->got_data = 1; - - if (!_seq_num_lookup(src_host, inbuf->khp_data_seq_num, 0, 0)) { - if (src_host->link_handler_policy != KNET_LINK_POLICY_ACTIVE) { - log_debug(knet_h, KNET_SUB_RX, "Packet has already been delivered"); - } - return; - } - - if (inbuf->khp_data_frag_num > 1) { - /* - * len as received from the socket also includes extra stuff - * that the defrag code doesn't care about. So strip it - * here and readd only for repadding once we are done - * defragging - */ - len = len - KNET_HEADER_DATA_SIZE; - if (pckt_defrag(knet_h, inbuf, &len)) { - return; - } - len = len + KNET_HEADER_DATA_SIZE; - } - - if (inbuf->kh_type == KNET_HEADER_TYPE_DATA) { - if (knet_h->enabled != 1) /* data forward is disabled */ - break; - - if (knet_h->dst_host_filter_fn) { - int host_idx; - int found = 0; - - bcast = knet_h->dst_host_filter_fn( - knet_h->dst_host_filter_fn_private_data, - (const unsigned char *)inbuf->khp_data_userdata, - len - KNET_HEADER_DATA_SIZE, - KNET_NOTIFY_RX, - knet_h->host_id, - inbuf->kh_node, - &channel, - dst_host_ids, - &dst_host_ids_entries); - if (bcast < 0) { - log_debug(knet_h, KNET_SUB_RX, "Error from dst_host_filter_fn: %d", bcast); - return; - } - - if ((!bcast) && (!dst_host_ids_entries)) { - log_debug(knet_h, KNET_SUB_RX, "Message is unicast but no dst_host_ids_entries"); - return; - } - - /* check if we are dst for this packet */ - if (!bcast) { - for (host_idx = 0; host_idx < dst_host_ids_entries; host_idx++) { - if (dst_host_ids[host_idx] == knet_h->host_id) { - found = 1; - break; - } - } - if (!found) { - log_debug(knet_h, KNET_SUB_RX, "Packet is not for us"); - return; - } - } - } - } - - if (inbuf->kh_type == KNET_HEADER_TYPE_DATA) { - if (!knet_h->sockfd[channel].in_use) { - log_debug(knet_h, KNET_SUB_RX, - "received packet for channel %d but there is no local sock connected", - channel); - return; - } - - memset(iov_out, 0, sizeof(iov_out)); - iov_out[0].iov_base = (void *) inbuf->khp_data_userdata; - iov_out[0].iov_len = len - KNET_HEADER_DATA_SIZE; - - outlen = writev(knet_h->sockfd[channel].sockfd[knet_h->sockfd[channel].is_created], iov_out, 1); - if (outlen <= 0) { - knet_h->sock_notify_fn(knet_h->sock_notify_fn_private_data, - knet_h->sockfd[channel].sockfd[0], - channel, - KNET_NOTIFY_RX, - outlen, - errno); - return; - } - if (outlen == iov_out[0].iov_len) { - _seq_num_set(src_host, inbuf->khp_data_seq_num, 0); - } - } else { /* HOSTINFO */ - knet_hostinfo = (struct knet_hostinfo *)inbuf->khp_data_userdata; - if (knet_hostinfo->khi_bcast == KNET_HOSTINFO_UCAST) { - bcast = 0; - knet_hostinfo->khi_dst_node_id = ntohs(knet_hostinfo->khi_dst_node_id); - } - if (!_seq_num_lookup(src_host, inbuf->khp_data_seq_num, 0, 0)) { - return; - } - _seq_num_set(src_host, inbuf->khp_data_seq_num, 0); - switch(knet_hostinfo->khi_type) { - case KNET_HOSTINFO_TYPE_LINK_UP_DOWN: - break; - case KNET_HOSTINFO_TYPE_LINK_TABLE: - break; - default: - log_warn(knet_h, KNET_SUB_RX, "Receiving unknown host info message from host %u", src_host->host_id); - break; - } - } - break; - case KNET_HEADER_TYPE_PING: - outlen = KNET_HEADER_PING_SIZE; - inbuf->kh_type = KNET_HEADER_TYPE_PONG; - inbuf->kh_node = htons(knet_h->host_id); - recv_seq_num = ntohs(inbuf->khp_ping_seq_num); - - wipe_bufs = 0; - - if (!inbuf->khp_ping_timed) { - /* - * we might be receiving this message from all links, but we want - * to process it only the first time - */ - if (recv_seq_num != src_host->untimed_rx_seq_num) { - /* - * cache the untimed seq num - */ - src_host->untimed_rx_seq_num = recv_seq_num; - /* - * if the host has received data in between - * untimed ping, then we don't need to wipe the bufs - */ - if (src_host->got_data) { - src_host->got_data = 0; - wipe_bufs = 0; - } else { - wipe_bufs = 1; - } - } - _seq_num_lookup(src_host, recv_seq_num, 0, wipe_bufs); - } else { - /* - * pings always arrives in bursts over all the link - * catch the first of them to cache the seq num and - * avoid duplicate processing - */ - if (recv_seq_num != src_host->timed_rx_seq_num) { - src_host->timed_rx_seq_num = recv_seq_num; - - if (recv_seq_num == 0) { - _seq_num_lookup(src_host, recv_seq_num, 0, 1); - } - } - } - - if (knet_h->crypto_instance) { - if (crypto_encrypt_and_sign(knet_h, - (const unsigned char *)inbuf, - len, - knet_h->recv_from_links_buf_crypt, - &outlen) < 0) { - log_debug(knet_h, KNET_SUB_RX, "Unable to encrypt pong packet"); - break; - } - outbuf = knet_h->recv_from_links_buf_crypt; - } - -retry_pong: - len = sendto(src_link->outsock, outbuf, outlen, MSG_DONTWAIT | MSG_NOSIGNAL, - (struct sockaddr *) &src_link->dst_addr, - sizeof(struct sockaddr_storage)); - savederrno = errno; - if (len != outlen) { - err = knet_h->transport_ops[src_link->transport_type]->transport_tx_sock_error(knet_h, src_link->outsock, len, savederrno); - switch(err) { - case -1: /* unrecoverable error */ - log_debug(knet_h, KNET_SUB_RX, - "Unable to send pong reply (sock: %d) packet (sendto): %d %s. recorded src ip: %s src port: %s dst ip: %s dst port: %s", - src_link->outsock, errno, strerror(errno), - src_link->status.src_ipaddr, src_link->status.src_port, - src_link->status.dst_ipaddr, src_link->status.dst_port); - break; - case 0: /* ignore error and continue */ - break; - case 1: /* retry to send those same data */ - goto retry_pong; - break; - } - } - break; - case KNET_HEADER_TYPE_PONG: - clock_gettime(CLOCK_MONOTONIC, &src_link->status.pong_last); - - memmove(&recvtime, &inbuf->khp_ping_time[0], sizeof(struct timespec)); - timespec_diff(recvtime, - src_link->status.pong_last, &latency_last); - - src_link->status.latency = - ((src_link->status.latency * src_link->latency_exp) + - ((latency_last / 1000llu) * - (src_link->latency_fix - src_link->latency_exp))) / - src_link->latency_fix; - - if (src_link->status.latency < src_link->pong_timeout) { - if (!src_link->status.connected) { - if (src_link->received_pong >= src_link->pong_count) { - log_info(knet_h, KNET_SUB_RX, "host: %u link: %u is up", - src_host->host_id, src_link->link_id); - _link_updown(knet_h, src_host->host_id, src_link->link_id, src_link->status.enabled, 1); - } else { - src_link->received_pong++; - log_debug(knet_h, KNET_SUB_RX, "host: %u link: %u received pong: %u", - src_host->host_id, src_link->link_id, src_link->received_pong); - } - } - } - - break; - case KNET_HEADER_TYPE_PMTUD: - outlen = KNET_HEADER_PMTUD_SIZE; - inbuf->kh_type = KNET_HEADER_TYPE_PMTUD_REPLY; - inbuf->kh_node = htons(knet_h->host_id); - - if (knet_h->crypto_instance) { - if (crypto_encrypt_and_sign(knet_h, - (const unsigned char *)inbuf, - len, - knet_h->recv_from_links_buf_crypt, - &outlen) < 0) { - log_debug(knet_h, KNET_SUB_RX, "Unable to encrypt PMTUd reply packet"); - break; - } - outbuf = knet_h->recv_from_links_buf_crypt; - } - -retry_pmtud: - len = sendto(src_link->outsock, outbuf, outlen, MSG_DONTWAIT | MSG_NOSIGNAL, - (struct sockaddr *) &src_link->dst_addr, - sizeof(struct sockaddr_storage)); - if (len != outlen) { - err = knet_h->transport_ops[src_link->transport_type]->transport_tx_sock_error(knet_h, src_link->outsock, len, savederrno); - switch(err) { - case -1: /* unrecoverable error */ - log_debug(knet_h, KNET_SUB_RX, - "Unable to send PMTUd reply (sock: %d) packet (sendto): %d %s. recorded src ip: %s src port: %s dst ip: %s dst port: %s", - src_link->outsock, errno, strerror(errno), - src_link->status.src_ipaddr, src_link->status.src_port, - src_link->status.dst_ipaddr, src_link->status.dst_port); - break; - case 0: /* ignore error and continue */ - break; - case 1: /* retry to send those same data */ - goto retry_pmtud; - break; - } - } - - break; - case KNET_HEADER_TYPE_PMTUD_REPLY: - if (pthread_mutex_lock(&knet_h->pmtud_mutex) != 0) { - log_debug(knet_h, KNET_SUB_RX, "Unable to get mutex lock"); - break; - } - src_link->last_recv_mtu = inbuf->khp_pmtud_size; - pthread_cond_signal(&knet_h->pmtud_cond); - pthread_mutex_unlock(&knet_h->pmtud_mutex); - break; - default: - return; - } -} - -static void _handle_recv_from_links(knet_handle_t knet_h, int sockfd, struct knet_mmsghdr *msg) -{ - int err, savederrno; - int i, msg_recv, transport; - - if (pthread_rwlock_rdlock(&knet_h->global_rwlock) != 0) { - log_debug(knet_h, KNET_SUB_RX, "Unable to get global read lock"); - return; - } - - if (_is_valid_fd(knet_h, sockfd) < 1) { - /* - * this is normal if a fd got an event and before we grab the read lock - * and the link is removed by another thread - */ - goto exit_unlock; - } - - transport = knet_h->knet_transport_fd_tracker[sockfd].transport; - - /* - * reset msg_namelen to buffer size because after recvmmsg - * each msg_namelen will contain sizeof sockaddr_in or sockaddr_in6 - */ - - for (i = 0; i < PCKT_FRAG_MAX; i++) { - msg[i].msg_hdr.msg_namelen = sizeof(struct sockaddr_storage); - } - - msg_recv = _recvmmsg(sockfd, (struct mmsghdr *)&msg[0], PCKT_FRAG_MAX, MSG_DONTWAIT | MSG_NOSIGNAL); - savederrno = errno; - - /* - * WARNING: man page for recvmmsg is wrong. Kernel implementation here: - * recvmmsg can return: - * -1 on error - * 0 if the previous run of recvmmsg recorded an error on the socket - * N number of messages (see exception below). - * - * If there is an error from recvmsg after receiving a frame or more, the recvmmsg - * loop is interrupted, error recorded in the socket (getsockopt(SO_ERROR) and - * it will be visibile in the next run. - * - * Need to be careful how we handle errors at this stage. - * - * error messages need to be handled on a per transport/protocol base - * at this point we have different layers of error handling - * - msg_recv < 0 -> error from this run - * msg_recv = 0 -> error from previous run and error on socket needs to be cleared - * - per-transport message data - * example: msg[i].msg_hdr.msg_flags & MSG_NOTIFICATION or msg_len for SCTP == EOF, - * but for UDP it is perfectly legal to receive a 0 bytes message.. go figure - * - NOTE: on SCTP MSG_NOTIFICATION we get msg_recv == PCKT_FRAG_MAX messages and no - * errno set. That means the error api needs to be able to abort the loop below. - */ - - if (msg_recv <= 0) { - knet_h->transport_ops[transport]->transport_rx_sock_error(knet_h, sockfd, msg_recv, savederrno); - goto exit_unlock; - } - - for (i = 0; i < msg_recv; i++) { - err = knet_h->transport_ops[transport]->transport_rx_is_data(knet_h, sockfd, &msg[i]); - - /* - * TODO: make this section silent once we are confident - * all protocols packet handlers are good - */ - - switch(err) { - case -1: /* on error */ - log_debug(knet_h, KNET_SUB_RX, "Transport reported error parsing packet"); - goto exit_unlock; - break; - case 0: /* packet is not data and we should continue the packet process loop */ - log_debug(knet_h, KNET_SUB_RX, "Transport reported no data, continue"); - break; - case 1: /* packet is not data and we should STOP the packet process loop */ - log_debug(knet_h, KNET_SUB_RX, "Transport reported no data, stop"); - goto exit_unlock; - break; - case 2: /* packet is data and should be parsed as such */ - _parse_recv_from_links(knet_h, sockfd, &msg[i]); - break; - } - } - -exit_unlock: - pthread_rwlock_unlock(&knet_h->global_rwlock); -} - -void *_handle_recv_from_links_thread(void *data) -{ - int i, nev; - knet_handle_t knet_h = (knet_handle_t) data; - struct epoll_event events[KNET_EPOLL_MAX_EVENTS]; - struct sockaddr_storage address[PCKT_FRAG_MAX]; - struct knet_mmsghdr msg[PCKT_FRAG_MAX]; - struct iovec iov_in[PCKT_FRAG_MAX]; - - memset(&msg, 0, sizeof(msg)); - - for (i = 0; i < PCKT_FRAG_MAX; i++) { - iov_in[i].iov_base = (void *)knet_h->recv_from_links_buf[i]; - iov_in[i].iov_len = KNET_DATABUFSIZE; - - memset(&msg[i].msg_hdr, 0, sizeof(struct msghdr)); - - msg[i].msg_hdr.msg_name = &address[i]; - msg[i].msg_hdr.msg_namelen = sizeof(struct sockaddr_storage); - msg[i].msg_hdr.msg_iov = &iov_in[i]; - msg[i].msg_hdr.msg_iovlen = 1; - } - - while (!shutdown_in_progress(knet_h)) { - nev = epoll_wait(knet_h->recv_from_links_epollfd, events, KNET_EPOLL_MAX_EVENTS, -1); - - for (i = 0; i < nev; i++) { - _handle_recv_from_links(knet_h, events[i].data.fd, msg); - } - } - - return NULL; -} diff --git a/libknet/threads_rx.h b/libknet/threads_rx.h deleted file mode 100644 index bca43027..00000000 --- a/libknet/threads_rx.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (C) 2010-2017 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_THREADS_RX_H__ -#define __KNET_THREADS_RX_H__ - -void *_handle_recv_from_links_thread(void *data); - -#endif diff --git a/libknet/threads_tx.c b/libknet/threads_tx.c deleted file mode 100644 index 6c2bca8d..00000000 --- a/libknet/threads_tx.c +++ /dev/null @@ -1,609 +0,0 @@ -/* - * Copyright (C) 2010-2017 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include - -#include "compat.h" -#include "crypto.h" -#include "host.h" -#include "link.h" -#include "logging.h" -#include "transports.h" -#include "threads_common.h" -#include "threads_heartbeat.h" -#include "threads_tx.h" -#include "netutils.h" - -/* - * SEND - */ - -static int _dispatch_to_links(knet_handle_t knet_h, struct knet_host *dst_host, struct knet_mmsghdr *msg, int msgs_to_send) -{ - int link_idx, msg_idx, sent_msgs, prev_sent, progress; - int err = 0, savederrno = 0; - struct knet_mmsghdr *cur; - - for (link_idx = 0; link_idx < dst_host->active_link_entries; link_idx++) { - sent_msgs = 0; - prev_sent = 0; - progress = 1; - - msg_idx = 0; - while (msg_idx < msgs_to_send) { - msg[msg_idx].msg_hdr.msg_name = &dst_host->link[dst_host->active_links[link_idx]].dst_addr; - msg_idx++; - } - -retry: - cur = &msg[prev_sent]; - - sent_msgs = _sendmmsg(dst_host->link[dst_host->active_links[link_idx]].outsock, - &cur[0], msgs_to_send - prev_sent, MSG_DONTWAIT | MSG_NOSIGNAL); - savederrno = errno; - - err = knet_h->transport_ops[dst_host->link[dst_host->active_links[link_idx]].transport_type]->transport_tx_sock_error(knet_h, dst_host->link[dst_host->active_links[link_idx]].outsock, sent_msgs, savederrno); - switch(err) { - case -1: /* unrecoverable error */ - goto out_unlock; - break; - case 0: /* ignore error and continue */ - break; - case 1: /* retry to send those same data */ - goto retry; - break; - } - - prev_sent = prev_sent + sent_msgs; - - if ((sent_msgs >= 0) && (prev_sent < msgs_to_send)) { - if ((sent_msgs) || (progress)) { - if (sent_msgs) { - progress = 1; - } else { - progress = 0; - } -#ifdef DEBUG - log_debug(knet_h, KNET_SUB_TX, "Unable to send all (%d/%d) data packets to host %s (%u) link %s:%s (%u)", - sent_msgs, msg_idx, - dst_host->name, dst_host->host_id, - dst_host->link[dst_host->active_links[link_idx]].status.dst_ipaddr, - dst_host->link[dst_host->active_links[link_idx]].status.dst_port, - dst_host->link[dst_host->active_links[link_idx]].link_id); -#endif - goto retry; - } - if (!progress) { - savederrno = EAGAIN; - err = -1; - goto out_unlock; - } - } - - if ((dst_host->link_handler_policy == KNET_LINK_POLICY_RR) && - (dst_host->active_link_entries > 1)) { - uint8_t cur_link_id = dst_host->active_links[0]; - - memmove(&dst_host->active_links[0], &dst_host->active_links[1], KNET_MAX_LINK - 1); - dst_host->active_links[dst_host->active_link_entries - 1] = cur_link_id; - - break; - } - } - -out_unlock: - errno = savederrno; - return err; -} - -static int _parse_recv_from_sock(knet_handle_t knet_h, int buf_idx, ssize_t inlen, int8_t channel, int is_sync) -{ - ssize_t outlen, frag_len; - struct knet_host *dst_host; - knet_node_id_t dst_host_ids_temp[KNET_MAX_HOST]; - size_t dst_host_ids_entries_temp = 0; - knet_node_id_t dst_host_ids[KNET_MAX_HOST]; - size_t dst_host_ids_entries = 0; - int bcast = 1; - struct knet_hostinfo *knet_hostinfo; - struct iovec iov_out[PCKT_FRAG_MAX]; - uint8_t frag_idx; - unsigned int temp_data_mtu; - int host_idx; - int send_mcast = 0; - struct knet_header *inbuf; - int savederrno = 0; - int err = 0; - seq_num_t tx_seq_num; - struct knet_mmsghdr msg[PCKT_FRAG_MAX]; - int msgs_to_send, msg_idx; - - inbuf = knet_h->recv_from_sock_buf[buf_idx]; - - if ((knet_h->enabled != 1) && - (inbuf->kh_type != KNET_HEADER_TYPE_HOST_INFO)) { /* data forward is disabled */ - log_debug(knet_h, KNET_SUB_TX, "Received data packet but forwarding is disabled"); - savederrno = ECANCELED; - err = -1; - goto out_unlock; - } - - /* - * move this into a separate function to expand on - * extra switching rules - */ - switch(inbuf->kh_type) { - case KNET_HEADER_TYPE_DATA: - if (knet_h->dst_host_filter_fn) { - bcast = knet_h->dst_host_filter_fn( - knet_h->dst_host_filter_fn_private_data, - (const unsigned char *)inbuf->khp_data_userdata, - inlen, - KNET_NOTIFY_TX, - knet_h->host_id, - knet_h->host_id, - &channel, - dst_host_ids_temp, - &dst_host_ids_entries_temp); - if (bcast < 0) { - log_debug(knet_h, KNET_SUB_TX, "Error from dst_host_filter_fn: %d", bcast); - savederrno = EFAULT; - err = -1; - goto out_unlock; - } - - if ((!bcast) && (!dst_host_ids_entries_temp)) { - log_debug(knet_h, KNET_SUB_TX, "Message is unicast but no dst_host_ids_entries"); - savederrno = EINVAL; - err = -1; - goto out_unlock; - } - } - break; - case KNET_HEADER_TYPE_HOST_INFO: - knet_hostinfo = (struct knet_hostinfo *)inbuf->khp_data_userdata; - if (knet_hostinfo->khi_bcast == KNET_HOSTINFO_UCAST) { - bcast = 0; - dst_host_ids_temp[0] = knet_hostinfo->khi_dst_node_id; - dst_host_ids_entries_temp = 1; - knet_hostinfo->khi_dst_node_id = htons(knet_hostinfo->khi_dst_node_id); - } - break; - default: - log_warn(knet_h, KNET_SUB_TX, "Receiving unknown messages from socket"); - savederrno = ENOMSG; - err = -1; - goto out_unlock; - break; - } - - if (is_sync) { - if ((bcast) || - ((!bcast) && (dst_host_ids_entries_temp > 1))) { - log_debug(knet_h, KNET_SUB_TX, "knet_send_sync is only supported with unicast packets for one destination"); - savederrno = E2BIG; - err = -1; - goto out_unlock; - } - } - - /* - * check destinations hosts before spending time - * in fragmenting/encrypting packets to save - * time processing data for unrechable hosts. - * for unicast, also remap the destination data - * to skip unreachable hosts. - */ - - if (!bcast) { - dst_host_ids_entries = 0; - for (host_idx = 0; host_idx < dst_host_ids_entries_temp; host_idx++) { - dst_host = knet_h->host_index[dst_host_ids_temp[host_idx]]; - if (!dst_host) { - continue; - } - if (dst_host->status.reachable) { - dst_host_ids[dst_host_ids_entries] = dst_host_ids_temp[host_idx]; - dst_host_ids_entries++; - } - } - if (!dst_host_ids_entries) { - savederrno = EHOSTDOWN; - err = -1; - goto out_unlock; - } - } else { - send_mcast = 0; - for (dst_host = knet_h->host_head; dst_host != NULL; dst_host = dst_host->next) { - if (dst_host->status.reachable) { - send_mcast = 1; - break; - } - } - if (!send_mcast) { - savederrno = EHOSTDOWN; - err = -1; - goto out_unlock; - } - } - - if (!knet_h->data_mtu) { - /* - * using MIN_MTU_V4 for data mtu is not completely accurate but safe enough - */ - log_debug(knet_h, KNET_SUB_TX, - "Received data packet but data MTU is still unknown." - " Packet might not be delivered." - " Assuming mininum IPv4 mtu (%d)", - KNET_PMTUD_MIN_MTU_V4); - temp_data_mtu = KNET_PMTUD_MIN_MTU_V4; - } else { - /* - * take a copy of the mtu to avoid value changing under - * our feet while we are sending a fragmented pckt - */ - temp_data_mtu = knet_h->data_mtu; - } - - /* - * prepare the outgoing buffers - */ - - frag_len = inlen; - frag_idx = 0; - - inbuf->khp_data_bcast = bcast; - inbuf->khp_data_frag_num = ceil((float)inlen / temp_data_mtu); - inbuf->khp_data_channel = channel; - - if (pthread_mutex_lock(&knet_h->tx_seq_num_mutex)) { - log_debug(knet_h, KNET_SUB_TX, "Unable to get seq mutex lock"); - goto out_unlock; - } - knet_h->tx_seq_num++; - /* - * force seq_num 0 to detect a node that has crashed and rejoining - * the knet instance. seq_num 0 will clear the buffers in the RX - * thread - */ - if (knet_h->tx_seq_num == 0) { - knet_h->tx_seq_num++; - } - /* - * cache the value in locked context - */ - tx_seq_num = knet_h->tx_seq_num; - inbuf->khp_data_seq_num = htons(knet_h->tx_seq_num); - pthread_mutex_unlock(&knet_h->tx_seq_num_mutex); - - /* - * forcefully broadcast a ping to all nodes every SEQ_MAX / 8 - * pckts. - * this solves 2 problems: - * 1) on TX socket overloads we generate extra pings to keep links alive - * 2) in 3+ nodes setup, where all the traffic is flowing between node 1 and 2, - * node 3+ will be able to keep in sync on the TX seq_num even without - * receiving traffic or pings in betweens. This avoids issues with - * rollover of the circular buffer - */ - - if (tx_seq_num % (SEQ_MAX / 8) == 0) { - _send_pings(knet_h, 0); - } - - if (inbuf->khp_data_frag_num > 1) { - while (frag_idx < inbuf->khp_data_frag_num) { - /* - * set the iov_base - */ - iov_out[frag_idx].iov_base = (void *)knet_h->send_to_links_buf[frag_idx]; - - /* - * set the len - */ - if (frag_len > temp_data_mtu) { - iov_out[frag_idx].iov_len = temp_data_mtu + KNET_HEADER_DATA_SIZE; - } else { - iov_out[frag_idx].iov_len = frag_len + KNET_HEADER_DATA_SIZE; - } - - /* - * copy the frag info on all buffers - */ - knet_h->send_to_links_buf[frag_idx]->kh_type = inbuf->kh_type; - knet_h->send_to_links_buf[frag_idx]->khp_data_seq_num = inbuf->khp_data_seq_num; - knet_h->send_to_links_buf[frag_idx]->khp_data_frag_num = inbuf->khp_data_frag_num; - knet_h->send_to_links_buf[frag_idx]->khp_data_bcast = inbuf->khp_data_bcast; - knet_h->send_to_links_buf[frag_idx]->khp_data_channel = inbuf->khp_data_channel; - - memmove(knet_h->send_to_links_buf[frag_idx]->khp_data_userdata, - inbuf->khp_data_userdata + (temp_data_mtu * frag_idx), - iov_out[frag_idx].iov_len - KNET_HEADER_DATA_SIZE); - - frag_len = frag_len - temp_data_mtu; - frag_idx++; - } - } else { - iov_out[frag_idx].iov_base = (void *)inbuf; - iov_out[frag_idx].iov_len = frag_len + KNET_HEADER_DATA_SIZE; - } - - if (knet_h->crypto_instance) { - frag_idx = 0; - while (frag_idx < inbuf->khp_data_frag_num) { - if (crypto_encrypt_and_sign( - knet_h, - (const unsigned char *)iov_out[frag_idx].iov_base, - iov_out[frag_idx].iov_len, - knet_h->send_to_links_buf_crypt[frag_idx], - &outlen) < 0) { - log_debug(knet_h, KNET_SUB_TX, "Unable to encrypt packet"); - savederrno = ECHILD; - err = -1; - goto out_unlock; - } - iov_out[frag_idx].iov_base = knet_h->send_to_links_buf_crypt[frag_idx]; - iov_out[frag_idx].iov_len = outlen; - frag_idx++; - } - } - - memset(&msg, 0, sizeof(msg)); - - msgs_to_send = inbuf->khp_data_frag_num; - - msg_idx = 0; - - while (msg_idx < msgs_to_send) { - msg[msg_idx].msg_hdr.msg_namelen = sizeof(struct sockaddr_storage); - msg[msg_idx].msg_hdr.msg_iov = &iov_out[msg_idx]; - msg[msg_idx].msg_hdr.msg_iovlen = 1; - msg_idx++; - } - - if (!bcast) { - for (host_idx = 0; host_idx < dst_host_ids_entries; host_idx++) { - dst_host = knet_h->host_index[dst_host_ids[host_idx]]; - - err = _dispatch_to_links(knet_h, dst_host, &msg[0], msgs_to_send); - savederrno = errno; - if (err) { - goto out_unlock; - } - } - } else { - for (dst_host = knet_h->host_head; dst_host != NULL; dst_host = dst_host->next) { - if (dst_host->status.reachable) { - err = _dispatch_to_links(knet_h, dst_host, &msg[0], msgs_to_send); - savederrno = errno; - if (err) { - goto out_unlock; - } - } - } - } - -out_unlock: - errno = savederrno; - return err; -} - -int knet_send_sync(knet_handle_t knet_h, const char *buff, const size_t buff_len, const int8_t channel) -{ - int savederrno = 0, err = 0; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (buff == NULL) { - errno = EINVAL; - return -1; - } - - if (buff_len <= 0) { - errno = EINVAL; - return -1; - } - - if (buff_len > KNET_MAX_PACKET_SIZE) { - errno = EINVAL; - return -1; - } - - if (channel < 0) { - errno = EINVAL; - return -1; - } - - if (channel >= KNET_DATAFD_MAX) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_TX, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - if (!knet_h->sockfd[channel].in_use) { - savederrno = EINVAL; - err = -1; - goto out; - } - - savederrno = pthread_mutex_lock(&knet_h->tx_mutex); - if (savederrno) { - log_err(knet_h, KNET_SUB_TX, "Unable to get TX mutex lock: %s", - strerror(savederrno)); - err = -1; - goto out; - } - - knet_h->recv_from_sock_buf[0]->kh_type = KNET_HEADER_TYPE_DATA; - memmove(knet_h->recv_from_sock_buf[0]->khp_data_userdata, buff, buff_len); - err = _parse_recv_from_sock(knet_h, 0, buff_len, channel, 1); - savederrno = errno; - - pthread_mutex_unlock(&knet_h->tx_mutex); - -out: - pthread_rwlock_unlock(&knet_h->global_rwlock); - - errno = savederrno; - return err; -} - -static void _handle_send_to_links(knet_handle_t knet_h, int sockfd, int8_t channel, struct knet_mmsghdr *msg, int type) -{ - ssize_t inlen = 0; - struct iovec iov_in; - int msg_recv, i; - int savederrno = 0, docallback = 0; - - if ((channel >= 0) && - (channel < KNET_DATAFD_MAX) && - (!knet_h->sockfd[channel].is_socket)) { - memset(&iov_in, 0, sizeof(iov_in)); - iov_in.iov_base = (void *)knet_h->recv_from_sock_buf[0]->khp_data_userdata; - iov_in.iov_len = KNET_MAX_PACKET_SIZE; - - inlen = readv(sockfd, &iov_in, 1); - - if (inlen <= 0) { - savederrno = errno; - docallback = 1; - goto out; - } - - msg_recv = 1; - knet_h->recv_from_sock_buf[0]->kh_type = type; - _parse_recv_from_sock(knet_h, 0, inlen, channel, 0); - } else { - msg_recv = _recvmmsg(sockfd, (struct mmsghdr *)&msg[0], PCKT_FRAG_MAX, MSG_DONTWAIT | MSG_NOSIGNAL); - if (msg_recv < 0) { - inlen = msg_recv; - savederrno = errno; - docallback = 1; - goto out; - } - for (i = 0; i < msg_recv; i++) { - inlen = msg[i].msg_len; - if (inlen == 0) { - savederrno = 0; - docallback = 1; - goto out; - break; - } - knet_h->recv_from_sock_buf[i]->kh_type = type; - _parse_recv_from_sock(knet_h, i, inlen, channel, 0); - } - } - -out: - if (inlen < 0) { - struct epoll_event ev; - - memset(&ev, 0, sizeof(struct epoll_event)); - - if (epoll_ctl(knet_h->send_to_links_epollfd, - EPOLL_CTL_DEL, knet_h->sockfd[channel].sockfd[knet_h->sockfd[channel].is_created], &ev)) { - log_err(knet_h, KNET_SUB_TX, "Unable to del datafd %d from linkfd epoll pool: %s", - knet_h->sockfd[channel].sockfd[0], strerror(savederrno)); - } else { - knet_h->sockfd[channel].has_error = 1; - } - - } - - if (docallback) { - knet_h->sock_notify_fn(knet_h->sock_notify_fn_private_data, - knet_h->sockfd[channel].sockfd[0], - channel, - KNET_NOTIFY_TX, - inlen, - savederrno); - } -} - -void *_handle_send_to_links_thread(void *data) -{ - knet_handle_t knet_h = (knet_handle_t) data; - struct epoll_event events[KNET_EPOLL_MAX_EVENTS]; - struct sockaddr_storage address[PCKT_FRAG_MAX]; - struct knet_mmsghdr msg[PCKT_FRAG_MAX]; - struct iovec iov_in[PCKT_FRAG_MAX]; - int i, nev, type; - int8_t channel; - - memset(&msg, 0, sizeof(msg)); - - /* preparing data buffer */ - for (i = 0; i < PCKT_FRAG_MAX; i++) { - iov_in[i].iov_base = (void *)knet_h->recv_from_sock_buf[i]->khp_data_userdata; - iov_in[i].iov_len = KNET_MAX_PACKET_SIZE; - - memset(&msg[i].msg_hdr, 0, sizeof(struct msghdr)); - - msg[i].msg_hdr.msg_name = &address[i]; - msg[i].msg_hdr.msg_namelen = sizeof(struct sockaddr_storage); - msg[i].msg_hdr.msg_iov = &iov_in[i]; - msg[i].msg_hdr.msg_iovlen = 1; - - knet_h->recv_from_sock_buf[i]->kh_version = KNET_HEADER_VERSION; - knet_h->recv_from_sock_buf[i]->khp_data_frag_seq = 0; - knet_h->recv_from_sock_buf[i]->kh_node = htons(knet_h->host_id); - - knet_h->send_to_links_buf[i]->kh_version = KNET_HEADER_VERSION; - knet_h->send_to_links_buf[i]->khp_data_frag_seq = i + 1; - knet_h->send_to_links_buf[i]->kh_node = htons(knet_h->host_id); - } - - while (!shutdown_in_progress(knet_h)) { - nev = epoll_wait(knet_h->send_to_links_epollfd, events, KNET_EPOLL_MAX_EVENTS + 1, -1); - - if (pthread_rwlock_rdlock(&knet_h->global_rwlock) != 0) { - log_debug(knet_h, KNET_SUB_TX, "Unable to get read lock"); - continue; - } - - for (i = 0; i < nev; i++) { - if (events[i].data.fd == knet_h->hostsockfd[0]) { - type = KNET_HEADER_TYPE_HOST_INFO; - channel = -1; - } else { - type = KNET_HEADER_TYPE_DATA; - for (channel = 0; channel < KNET_DATAFD_MAX; channel++) { - if ((knet_h->sockfd[channel].in_use) && - (knet_h->sockfd[channel].sockfd[knet_h->sockfd[channel].is_created] == events[i].data.fd)) { - break; - } - } - } - if (pthread_mutex_lock(&knet_h->tx_mutex) != 0) { - log_debug(knet_h, KNET_SUB_TX, "Unable to get mutex lock"); - continue; - } - _handle_send_to_links(knet_h, events[i].data.fd, channel, &msg[0], type); - pthread_mutex_unlock(&knet_h->tx_mutex); - } - pthread_rwlock_unlock(&knet_h->global_rwlock); - } - - return NULL; -} diff --git a/libknet/threads_tx.h b/libknet/threads_tx.h deleted file mode 100644 index 46563e17..00000000 --- a/libknet/threads_tx.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (C) 2010-2017 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * Federico Simoncelli - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_THREADS_TX_H__ -#define __KNET_THREADS_TX_H__ - -void *_handle_send_to_links_thread(void *data); - -#endif diff --git a/libknet/transport_common.c b/libknet/transport_common.c deleted file mode 100644 index 2b7187d7..00000000 --- a/libknet/transport_common.c +++ /dev/null @@ -1,450 +0,0 @@ -#include "config.h" - -#include -#include -#include -#include -#include -#include - -#include "libknet.h" -#include "compat.h" -#include "host.h" -#include "link.h" -#include "logging.h" -#include "common.h" -#include "transports.h" - -/* - * reuse Jan Friesse's compat layer as wrapper to drop usage of sendmmsg - * - * TODO: kill those wrappers once we work on packet delivery guaranteed - */ - -int _recvmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags) -{ - int savederrno = 0, err = 0; - unsigned int i; - - for (i = 0; i < vlen; i++) { - err = recvmsg(sockfd, &msgvec[i].msg_hdr, flags); - savederrno = errno; - if (err >= 0) { - msgvec[i].msg_len = err; - } else { - if (err == -1 && errno == EAGAIN) { - err = 0; - } - break; - } - } - - errno = savederrno; - return ((err >= 0) ? i : err); -} - -int _sendmmsg(int sockfd, struct knet_mmsghdr *msgvec, unsigned int vlen, unsigned int flags) -{ - int savederrno = 0, err = 0; - unsigned int i; - - for (i = 0; i < vlen; i++) { - err = sendmsg(sockfd, &msgvec[i].msg_hdr, flags); - savederrno = errno; - if (err >= 0) { - msgvec[i].msg_len = err; - } else { - break; - } - } - - errno = savederrno; - return ((err >= 0) ? vlen : err); -} - -int _configure_common_socket(knet_handle_t knet_h, int sock, const char *type) -{ - int err = 0, savederrno = 0; - int value; - - if (_fdset_cloexec(sock)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s CLOEXEC socket opts: %s", - type, strerror(savederrno)); - goto exit_error; - } - - if (_fdset_nonblock(sock)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s NONBLOCK socket opts: %s", - type, strerror(savederrno)); - goto exit_error; - } - - value = KNET_RING_RCVBUFF; -#ifdef SO_RCVBUFFORCE - if (setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE, &value, sizeof(value)) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s receive buffer: %s", - type, strerror(savederrno)); - goto exit_error; - } -#else - if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s SO_RECVBUF: %s", - type, strerror(savederrno)); - goto exit_error; - } -#endif - - value = KNET_RING_RCVBUFF; -#ifdef SO_SNDBUFFORCE - if (setsockopt(sock, SOL_SOCKET, SO_SNDBUFFORCE, &value, sizeof(value)) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s send buffer: %s", - type, strerror(savederrno)); - goto exit_error; - } -#else - if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s SO_SNDBUF: %s", - type, strerror(savederrno)); - goto exit_error; - } -#endif - -exit_error: - errno = savederrno; - return err; -} - -int _configure_transport_socket(knet_handle_t knet_h, int sock, struct sockaddr_storage *address, const char *type) -{ - int err = 0, savederrno = 0; - int value; - - if (_configure_common_socket(knet_h, sock, type) < 0) { - savederrno = errno; - err = -1; - goto exit_error; - } - -#ifdef IP_FREEBIND - value = 1; - if (setsockopt(sock, SOL_IP, IP_FREEBIND, &value, sizeof(value)) <0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set FREEBIND on %s socket: %s", - type, strerror(savederrno)); - goto exit_error; - } -#endif -#ifdef IP_BINDANY /* BSD */ - value = 1; - if (setsockopt(sock, IPPROTO_IP, IP_BINDANY, &value, sizeof(value)) <0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set BINDANY on %s socket: %s", - type, strerror(savederrno)); - goto exit_error; - } -#endif - - if (address->ss_family == AF_INET6) { - value = 1; - if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, - &value, sizeof(value)) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s IPv6 only: %s", - type, strerror(savederrno)); - goto exit_error; - - } -#ifdef IPV6_MTU_DISCOVER - value = IPV6_PMTUDISC_PROBE; - if (setsockopt(sock, SOL_IPV6, IPV6_MTU_DISCOVER, &value, sizeof(value)) <0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set PMTUDISC on %s socket: %s", - type, strerror(savederrno)); - goto exit_error; - } -#else - value = 1; - if (setsockopt(sock, IPPROTO_IPV6, IPV6_DONTFRAG, &value, sizeof(value)) <0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set DONTFRAG on %s socket: %s", - type, strerror(savederrno)); - goto exit_error; - } -#endif - } else { -#ifdef IP_MTU_DISCOVER - value = IP_PMTUDISC_PROBE; - if (setsockopt(sock, SOL_IP, IP_MTU_DISCOVER, &value, sizeof(value)) <0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set PMTUDISC on %s socket: %s", - type, strerror(savederrno)); - goto exit_error; - } -#else - value = 1; - if (setsockopt(sock, IPPROTO_IP, IP_DONTFRAG, &value, sizeof(value)) <0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set DONTFRAG on %s socket: %s", - type, strerror(savederrno)); - goto exit_error; - } -#endif - } - - value = 1; - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value)) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s reuseaddr: %s", - type, strerror(savederrno)); - goto exit_error; - } - -exit_error: - errno = savederrno; - return err; -} - -int _init_socketpair(knet_handle_t knet_h, int *sock) -{ - int err = 0, savederrno = 0; - int i; - - if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sock) != 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize socketpair: %s", - strerror(savederrno)); - goto exit_fail; - } - - for (i = 0; i < 2; i++) { - if (_configure_common_socket(knet_h, sock[i], "local socketpair") < 0) { - savederrno = errno; - err = -1; - goto exit_fail; - } - } - -exit_fail: - errno = savederrno; - return err; -} - -void _close_socketpair(knet_handle_t knet_h, int *sock) -{ - int i; - - for (i = 0; i < 2; i++) { - if (sock[i]) { - close(sock[i]); - sock[i] = 0; - } - } -} - -/* - * must be called with global read lock - * - * return -1 on error - * return 0 if fd is invalid - * return 1 if fd is valid - */ -int _is_valid_fd(knet_handle_t knet_h, int sockfd) -{ - int ret = 0; - - if (sockfd < 0) { - errno = EINVAL; - return -1; - } - - if (sockfd > KNET_MAX_FDS) { - errno = EINVAL; - return -1; - } - - if (knet_h->knet_transport_fd_tracker[sockfd].transport >= KNET_MAX_TRANSPORTS) { - ret = 0; - } else { - ret = 1; - } - - return ret; -} - -/* - * must be called with global write lock - */ - -int _set_fd_tracker(knet_handle_t knet_h, int sockfd, uint8_t transport, uint8_t data_type, void *data) -{ - if (sockfd < 0) { - errno = EINVAL; - return -1; - } - - if (sockfd > KNET_MAX_FDS) { - errno = EINVAL; - return -1; - } - - knet_h->knet_transport_fd_tracker[sockfd].transport = transport; - knet_h->knet_transport_fd_tracker[sockfd].data_type = data_type; - knet_h->knet_transport_fd_tracker[sockfd].data = data; - - return 0; -} - -/* - * public api - */ - -int knet_handle_get_transport_list(knet_handle_t knet_h, - struct transport_info *transport_list, size_t *transport_list_entries) -{ - int err = 0, savederrno = 0; - int i, count; - - if (!knet_h) { - errno = EINVAL; - return -1; - } - - if (!transport_list) { - errno = EINVAL; - return -1; - } - - if (!transport_list_entries) { - errno = EINVAL; - return -1; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return -1; - } - - count = 0; - - /* - * we could potentially build this struct - * at knet_handle_new init time, but - * let's keep it dynamic in case at somepoint - * we need to init transports dynamically - * at runtime vs init time. - */ - - for (i=0; itransport_ops[i]) { - transport_list[count].name = knet_h->transport_ops[i]->transport_name; - transport_list[count].id = knet_h->transport_ops[i]->transport_id; - count++; - } - } - - *transport_list_entries = count; - - pthread_rwlock_unlock(&knet_h->global_rwlock); - - return err; -} - -const char *knet_handle_get_transport_name_by_id(knet_handle_t knet_h, uint8_t transport) -{ - int savederrno = 0; - const char *name = NULL; - - if (!knet_h) { - errno = EINVAL; - return name; - } - - if (transport >= KNET_MAX_TRANSPORTS) { - errno = EINVAL; - return name; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return name; - } - - if (knet_h->transport_ops[transport]) { - name = knet_h->transport_ops[transport]->transport_name; - } else { - savederrno = ENOENT; - } - - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return name; -} - -uint8_t knet_handle_get_transport_id_by_name(knet_handle_t knet_h, const char *name) -{ - int savederrno = 0; - uint8_t err = KNET_MAX_TRANSPORTS; - int i; - - if (!knet_h) { - errno = EINVAL; - return err; - } - - if (!name) { - errno = EINVAL; - return err; - } - - savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", - strerror(savederrno)); - errno = savederrno; - return err; - } - - for (i=0; itransport_ops[i]) { - if (!strcmp(knet_h->transport_ops[i]->transport_name, name)) { - err = knet_h->transport_ops[i]->transport_id; - break; - } - } - } - - if (err == KNET_MAX_TRANSPORTS) { - savederrno = EINVAL; - } - - pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; - return err; -} diff --git a/libknet/transport_sctp.c b/libknet/transport_sctp.c deleted file mode 100644 index e07bc383..00000000 --- a/libknet/transport_sctp.c +++ /dev/null @@ -1,1438 +0,0 @@ -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include - -#include "compat.h" -#include "host.h" -#include "link.h" -#include "logging.h" -#include "common.h" -#include "transports.h" -#include "threads_common.h" - -#ifdef HAVE_NETINET_SCTP_H -#include - -/* - * https://en.wikipedia.org/wiki/SCTP_packet_structure - */ -#define KNET_PMTUD_SCTP_OVERHEAD_COMMON 12 -#define KNET_PMTUD_SCTP_OVERHEAD_DATA_CHUNK 16 -#define KNET_PMTUD_SCTP_OVERHEAD KNET_PMTUD_SCTP_OVERHEAD_COMMON + KNET_PMTUD_SCTP_OVERHEAD_DATA_CHUNK - -/* - * Time to sleep before reconnection attempts. in microseconds - */ -#define KNET_SCTP_SLEEP_TIME 1000000 - -typedef struct sctp_handle_info { - struct knet_list_head listen_links_list; - struct knet_list_head connect_links_list; - int connect_epollfd; - int connectsockfd[2]; - int listen_epollfd; - int listensockfd[2]; - pthread_t connect_thread; - pthread_t listen_thread; -} sctp_handle_info_t; - -/* - * use by fd_tracker data type - */ -#define SCTP_NO_LINK_INFO 0 -#define SCTP_LISTENER_LINK_INFO 1 -#define SCTP_ACCEPTED_LINK_INFO 2 -#define SCTP_CONNECT_LINK_INFO 3 - -/* - * this value is per listener - */ -#define MAX_ACCEPTED_SOCKS 256 - -typedef struct sctp_listen_link_info { - struct knet_list_head list; - int listen_sock; - int accepted_socks[MAX_ACCEPTED_SOCKS]; - struct sockaddr_storage src_address; - int on_listener_epoll; - int on_rx_epoll; -} sctp_listen_link_info_t; - -typedef struct sctp_accepted_link_info { - char mread_buf[KNET_DATABUFSIZE]; - ssize_t mread_len; - sctp_listen_link_info_t *link_info; -} sctp_accepted_link_info_t ; - -typedef struct sctp_connect_link_info { - struct knet_list_head list; - sctp_listen_link_info_t *listener; - struct knet_link *link; - struct sockaddr_storage dst_address; - int connect_sock; - int on_connected_epoll; - int on_rx_epoll; - int close_sock; -} sctp_connect_link_info_t; - -/* - * socket handling functions - * - * those functions do NOT perform locking. locking - * should be handled in the right context from callers - */ - -/* - * sockets are removed from rx_epoll from callers - * see also error handling functions - */ -static int _close_connect_socket(knet_handle_t knet_h, struct knet_link *kn_link) -{ - int err = 0, savederrno = 0; - sctp_connect_link_info_t *info = kn_link->transport_link; - sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; - struct epoll_event ev; - - if (info->on_connected_epoll) { - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLOUT; - ev.data.fd = info->connect_sock; - if (epoll_ctl(handle_info->connect_epollfd, EPOLL_CTL_DEL, info->connect_sock, &ev)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove connected socket from the epoll pool: %s", - strerror(errno)); - goto exit_error; - } - info->on_connected_epoll = 0; - } - -exit_error: - if (info->connect_sock != -1) { - if (_set_fd_tracker(knet_h, info->connect_sock, KNET_MAX_TRANSPORTS, SCTP_NO_LINK_INFO, NULL) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set fd tracker: %s", - strerror(savederrno)); - goto exit_error; - } - close(info->connect_sock); - info->connect_sock = -1; - } - - errno = savederrno; - return err; -} - -static int _enable_sctp_notifications(knet_handle_t knet_h, int sock, const char *type) -{ - int err = 0, savederrno = 0; - struct sctp_event_subscribe events; - - memset(&events, 0, sizeof (events)); - events.sctp_data_io_event = 1; - events.sctp_association_event = 1; - events.sctp_send_failure_event = 1; - events.sctp_address_event = 1; - events.sctp_peer_error_event = 1; - events.sctp_shutdown_event = 1; - if (setsockopt(sock, IPPROTO_SCTP, SCTP_EVENTS, &events, sizeof (events)) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to enable %s events: %s", - type, strerror(savederrno)); - } - - errno = savederrno; - return err; -} - -static int _configure_sctp_socket(knet_handle_t knet_h, int sock, struct sockaddr_storage *address, const char *type) -{ - int err = 0, savederrno = 0; - int value; - int level; - -#ifdef SOL_SCTP - level = SOL_SCTP; -#else - level = IPPROTO_SCTP; -#endif - - if (_configure_transport_socket(knet_h, sock, address, type) < 0) { - savederrno = errno; - err = -1; - goto exit_error; - } - - value = 1; - if (setsockopt(sock, level, SCTP_NODELAY, &value, sizeof(value)) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set sctp nodelay: %s", - strerror(savederrno)); - goto exit_error; - } - - if (_enable_sctp_notifications(knet_h, sock, type) < 0) { - savederrno = errno; - err = -1; - } - -exit_error: - errno = savederrno; - return err; -} - -static int _reconnect_socket(knet_handle_t knet_h, struct knet_link *kn_link) -{ - int err = 0, savederrno = 0; - sctp_connect_link_info_t *info = kn_link->transport_link; - sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; - struct epoll_event ev; - - if (connect(info->connect_sock, (struct sockaddr *)&kn_link->dst_addr, sockaddr_len(&kn_link->dst_addr)) < 0) { - if ((errno != EALREADY) && (errno != EINPROGRESS) && (errno != EISCONN)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to connect SCTP socket %d: %s", - info->connect_sock, strerror(savederrno)); - goto exit_error; - } - } - - if (!info->on_connected_epoll) { - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLOUT; - ev.data.fd = info->connect_sock; - if (epoll_ctl(handle_info->connect_epollfd, EPOLL_CTL_ADD, info->connect_sock, &ev)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to add send/recv to epoll pool: %s", - strerror(savederrno)); - goto exit_error; - } - info->on_connected_epoll = 1; - } - -exit_error: - errno = savederrno; - return err; -} - -static int _create_connect_socket(knet_handle_t knet_h, struct knet_link *kn_link) -{ - int err = 0, savederrno = 0; - sctp_connect_link_info_t *info = kn_link->transport_link; - sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; - struct epoll_event ev; - int connect_sock; - - connect_sock = socket(kn_link->dst_addr.ss_family, SOCK_STREAM, IPPROTO_SCTP); - if (connect_sock < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to create send/recv socket: %s", - strerror(savederrno)); - goto exit_error; - } - - if (_configure_sctp_socket(knet_h, connect_sock, &kn_link->dst_addr, "SCTP connect") < 0) { - savederrno = errno; - err = -1; - goto exit_error; - } - - if (_set_fd_tracker(knet_h, connect_sock, KNET_TRANSPORT_SCTP, SCTP_CONNECT_LINK_INFO, info) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set fd tracker: %s", - strerror(savederrno)); - goto exit_error; - } - - info->connect_sock = connect_sock; - info->close_sock = 0; - if (_reconnect_socket(knet_h, kn_link) < 0) { - savederrno = errno; - err = -1; - goto exit_error; - } - -exit_error: - if (err) { - if (info->on_connected_epoll) { - epoll_ctl(handle_info->connect_epollfd, EPOLL_CTL_DEL, connect_sock, &ev); - } - if (connect_sock >= 0) { - close(connect_sock); - } - } - errno = savederrno; - return err; -} - -static int sctp_transport_tx_sock_error(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno) -{ - sctp_connect_link_info_t *connect_info = knet_h->knet_transport_fd_tracker[sockfd].data; - sctp_accepted_link_info_t *accepted_info = knet_h->knet_transport_fd_tracker[sockfd].data; - sctp_listen_link_info_t *listen_info; - - if (recv_err < 0) { - switch (knet_h->knet_transport_fd_tracker[sockfd].data_type) { - case SCTP_CONNECT_LINK_INFO: - if (connect_info->link->transport_connected == 0) { - return -1; - } - break; - case SCTP_ACCEPTED_LINK_INFO: - listen_info = accepted_info->link_info; - if (listen_info->listen_sock != sockfd) { - if (listen_info->on_rx_epoll == 0) { - return -1; - } - } - break; - } - if (recv_errno == EAGAIN) { -#ifdef DEBUG - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Sock: %d is overloaded. Slowing TX down", sockfd); -#endif - usleep(KNET_THREADS_TIMERES / 16); - return 1; - } - return -1; - } - return 0; -} - -/* - * socket error management functions - * - * both called with global read lock. - * - * NOTE: we need to remove the fd from the epoll as soon as possible - * even before we notify the respective thread to take care of it - * because scheduling can make it so that this thread will overload - * and the threads supposed to take care of the error will never - * be able to take action. - * we CANNOT handle FDs here diretly (close/reconnect/etc) due - * to locking context. We need to delegate that to their respective - * management threads within global write lock. - * - * this function is called from: - * - RX thread with recv_err <= 0 directly on recvmmsg error - * - transport_rx_is_data when msg_len == 0 (recv_err = 1) - * - transport_rx_is_data on notification (recv_err = 2) - * - * basically this small abouse of recv_err is to detect notifications - * generated by sockets created by listen(). - */ -static int sctp_transport_rx_sock_error(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno) -{ - struct epoll_event ev; - sctp_connect_link_info_t *connect_info = knet_h->knet_transport_fd_tracker[sockfd].data; - sctp_accepted_link_info_t *accepted_info = knet_h->knet_transport_fd_tracker[sockfd].data; - sctp_listen_link_info_t *listen_info; - sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; - - switch (knet_h->knet_transport_fd_tracker[sockfd].data_type) { - case SCTP_CONNECT_LINK_INFO: - /* - * all connect link have notifications enabled - * and we accept only data from notification and - * generic recvmmsg errors. - * - * Errors generated by msg_len 0 can be ignored because - * they follow a notification (double notification) - */ - if (recv_err != 1) { - connect_info->link->transport_connected = 0; - if (connect_info->on_rx_epoll) { - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = sockfd; - if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_DEL, sockfd, &ev)) { - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove EOFed socket from epoll pool: %s", - strerror(errno)); - return -1; - } - connect_info->on_rx_epoll = 0; - } - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Notifying connect thread that sockfd %d received an error", sockfd); - if (sendto(handle_info->connectsockfd[1], &sockfd, sizeof(int), MSG_DONTWAIT | MSG_NOSIGNAL, NULL, 0) != sizeof(int)) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to notify connect thread: %s", strerror(errno)); - } - } - break; - case SCTP_ACCEPTED_LINK_INFO: - listen_info = accepted_info->link_info; - if (listen_info->listen_sock != sockfd) { - if (recv_err != 1) { - if (listen_info->on_rx_epoll) { - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = sockfd; - if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_DEL, sockfd, &ev)) { - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove EOFed socket from epoll pool: %s", - strerror(errno)); - return -1; - } - listen_info->on_rx_epoll = 0; - } - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Notifying listen thread that sockfd %d received an error", sockfd); - if (sendto(handle_info->listensockfd[1], &sockfd, sizeof(int), MSG_DONTWAIT | MSG_NOSIGNAL, NULL, 0) != sizeof(int)) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to notify listen thread: %s", strerror(errno)); - } - } - } else { - /* - * this means the listen() socket has generated - * a notification. now what? :-) - */ - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received stray notification for listen() socket %d", sockfd); - } - break; - default: - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received unknown notification? %d", sockfd); - break; - } - /* - * Under RX pressure we need to give time to IPC to pick up the message - */ - usleep(KNET_THREADS_TIMERES / 2); - return 0; -} - -/* - * NOTE: sctp_transport_rx_is_data is called with global rdlock - * delegate any FD error management to sctp_transport_rx_sock_error - * and keep this code to parsing incoming data only - */ -static int sctp_transport_rx_is_data(knet_handle_t knet_h, int sockfd, struct knet_mmsghdr *msg) -{ - int i; - struct iovec *iov = msg->msg_hdr.msg_iov; - size_t iovlen = msg->msg_hdr.msg_iovlen; - struct sctp_assoc_change *sac; - union sctp_notification *snp; - sctp_accepted_link_info_t *info = knet_h->knet_transport_fd_tracker[sockfd].data; - - if (!(msg->msg_hdr.msg_flags & MSG_NOTIFICATION)) { - if (msg->msg_len == 0) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "received 0 bytes len packet: %d", sockfd); - /* - * NOTE: with event notification enabled, we receive error twice: - * 1) from the event notification - * 2) followed by a 0 byte msg_len - * - * This is generally not a problem if not for causing extra - * handling for the same issue. Should we drop notifications - * and keep the code generic (handle all errors via msg_len = 0) - * or keep the duplication as safety measure, or drop msg_len = 0 - * handling (what about sockets without events enabled?) - */ - sctp_transport_rx_sock_error(knet_h, sockfd, 1, 0); - return 1; - } - /* - * missing MSG_EOR has to be treated as a short read - * from the socket and we need to fill in the mread buf - * while we wait for MSG_EOR - */ - if (!(msg->msg_hdr.msg_flags & MSG_EOR)) { - /* - * copy the incoming data into mread_buf + mread_len (incremental) - * and increase mread_len - */ - memmove(info->mread_buf + info->mread_len, iov->iov_base, msg->msg_len); - info->mread_len = info->mread_len + msg->msg_len; - return 0; - } - /* - * got EOR. - * if mread_len is > 0 we are completing a packet from short reads - * complete reassembling the packet in mread_buf, copy it back in the iov - * and set the iov/msg len numbers (size) correctly - */ - if (info->mread_len) { - /* - * add last fragment to mread_buf - */ - memmove(info->mread_buf + info->mread_len, iov->iov_base, msg->msg_len); - info->mread_len = info->mread_len + msg->msg_len; - /* - * move all back into the iovec - */ - memmove(iov->iov_base, info->mread_buf, info->mread_len); - msg->msg_len = info->mread_len; - info->mread_len = 0; - } - return 2; - } - - if (!(msg->msg_hdr.msg_flags & MSG_EOR)) { - return 1; - } - - for (i=0; i< iovlen; i++) { - snp = iov[i].iov_base; - - switch (snp->sn_header.sn_type) { - case SCTP_ASSOC_CHANGE: - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp assoc change"); - sac = &snp->sn_assoc_change; - if (sac->sac_state == SCTP_COMM_LOST) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp assoc change: comm_lost"); - sctp_transport_rx_sock_error(knet_h, sockfd, 2, 0); - } - break; - case SCTP_SHUTDOWN_EVENT: - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp shutdown event"); - sctp_transport_rx_sock_error(knet_h, sockfd, 2, 0); - break; - case SCTP_SEND_FAILED: - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp send failed"); - break; - case SCTP_PEER_ADDR_CHANGE: - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp peer addr change"); - break; - case SCTP_REMOTE_ERROR: - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp remote error"); - break; - default: - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] unknown sctp event type: %hu\n", snp->sn_header.sn_type); - break; - } - } - return 0; -} - -/* - * connect / outgoing socket management thread - */ - -/* - * _handle_connected_sctp* are called with a global write lock - * from the connect_thread - */ -static void _handle_connected_sctp(knet_handle_t knet_h, int connect_sock) -{ - int err; - struct epoll_event ev; - unsigned int status, len = sizeof(status); - sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; - sctp_connect_link_info_t *info = knet_h->knet_transport_fd_tracker[connect_sock].data; - struct knet_link *kn_link = info->link; - - err = getsockopt(connect_sock, SOL_SOCKET, SO_ERROR, &status, &len); - if (err) { - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP getsockopt() on connecting socket %d failed: %s", - connect_sock, strerror(errno)); - return; - } - - if (info->close_sock) { - if (_close_connect_socket(knet_h, kn_link) < 0) { - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to close sock %d from _handle_connected_sctp: %s", connect_sock, strerror(errno)); - return; - } - info->close_sock = 0; - if (_create_connect_socket(knet_h, kn_link) < 0) { - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to recreate connecting sock! %s", strerror(errno)); - return; - } - } - - if (status) { - log_info(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP connect on %d to %s port %s failed: %s", - connect_sock, kn_link->status.dst_ipaddr, kn_link->status.dst_port, - strerror(status)); - - /* - * No need to create a new socket if connect failed, - * just retry connect - */ - _reconnect_socket(knet_h, info->link); - return; - } - - /* - * Connected - Remove us from the connect epoll - */ - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLOUT; - ev.data.fd = connect_sock; - if (epoll_ctl(handle_info->connect_epollfd, EPOLL_CTL_DEL, connect_sock, &ev)) { - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove connected socket %d from epoll pool: %s", - connect_sock, strerror(errno)); - } - info->on_connected_epoll = 0; - - kn_link->transport_connected = 1; - kn_link->outsock = info->connect_sock; - - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = connect_sock; - if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_ADD, connect_sock, &ev)) { - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to add connected socket to epoll pool: %s", - strerror(errno)); - } - info->on_rx_epoll = 1; - - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP handler fd %d now connected to %s port %s", - connect_sock, - kn_link->status.dst_ipaddr, kn_link->status.dst_port); -} - -static void _handle_connected_sctp_errors(knet_handle_t knet_h) -{ - int sockfd = -1; - sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; - sctp_connect_link_info_t *info; - - if (recv(handle_info->connectsockfd[0], &sockfd, sizeof(int), MSG_DONTWAIT | MSG_NOSIGNAL) != sizeof(int)) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Short read on connectsockfd"); - return; - } - - if (_is_valid_fd(knet_h, sockfd) < 1) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received stray notification for connected socket fd error"); - return; - } - - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Processing connected error on socket: %d", sockfd); - - info = knet_h->knet_transport_fd_tracker[sockfd].data; - - info->close_sock = 1; - info->link->transport_connected = 0; - _reconnect_socket(knet_h, info->link); -} - -static void *_sctp_connect_thread(void *data) -{ - int savederrno; - int i, nev; - knet_handle_t knet_h = (knet_handle_t) data; - sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; - struct epoll_event events[KNET_EPOLL_MAX_EVENTS]; - - while (!shutdown_in_progress(knet_h)) { - nev = epoll_wait(handle_info->connect_epollfd, events, KNET_EPOLL_MAX_EVENTS, -1); - - if (nev < 0) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP connect handler EPOLL ERROR: %s", - strerror(errno)); - continue; - } - - /* - * Sort out which FD has a connection - */ - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to get write lock: %s", - strerror(savederrno)); - continue; - } - - /* - * minor optimization: deduplicate events - * - * in some cases we can receive multiple notifcations - * of the same FD having issues or need handling. - * It's enough to process it once even tho it's safe - * to handle them multiple times. - */ - for (i = 0; i < nev; i++) { - if (events[i].data.fd == handle_info->connectsockfd[0]) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received notification from rx_error for connected socket"); - _handle_connected_sctp_errors(knet_h); - } else { - if (_is_valid_fd(knet_h, events[i].data.fd) == 1) { - _handle_connected_sctp(knet_h, events[i].data.fd); - } else { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received stray notification for dead fd %d\n", events[i].data.fd); - } - } - } - pthread_rwlock_unlock(&knet_h->global_rwlock); - /* - * this thread can generate events for itself. - * we need to sleep in between loops to allow other threads - * to be scheduled - */ - usleep(KNET_SCTP_SLEEP_TIME); - } - return NULL; -} - -/* - * listen/incoming connections management thread - */ - -/* - * Listener received a new connection - * called with a write lock from main thread - */ -static void _handle_incoming_sctp(knet_handle_t knet_h, int listen_sock) -{ - int err = 0, savederrno = 0; - int new_fd; - int i = -1; - sctp_listen_link_info_t *info = knet_h->knet_transport_fd_tracker[listen_sock].data; - struct epoll_event ev; - struct sockaddr_storage ss; - socklen_t sock_len = sizeof(ss); - char addr_str[KNET_MAX_HOST_LEN]; - char port_str[KNET_MAX_PORT_LEN]; - sctp_accepted_link_info_t *accept_info = NULL; - - new_fd = accept(listen_sock, (struct sockaddr *)&ss, &sock_len); - if (new_fd < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Incoming: accept error: %s", strerror(errno)); - goto exit_error; - } - - if (knet_addrtostr(&ss, sizeof(ss), - addr_str, KNET_MAX_HOST_LEN, - port_str, KNET_MAX_PORT_LEN) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Incoming: unable to gather socket info"); - goto exit_error; - } - - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Incoming: received connection from: %s port: %s", - addr_str, port_str); - - /* - * Keep a track of all accepted FDs - */ - for (i=0; iaccepted_socks[i] == -1) { - info->accepted_socks[i] = new_fd; - break; - } - } - - if (i == MAX_ACCEPTED_SOCKS) { - errno = EBUSY; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Incoming: too many connections!"); - goto exit_error; - } - - if (_configure_common_socket(knet_h, new_fd, "SCTP incoming") < 0) { - savederrno = errno; - err = -1; - goto exit_error; - } - - if (_enable_sctp_notifications(knet_h, new_fd, "Incoming connection") < 0) { - savederrno = errno; - err = -1; - goto exit_error; - } - - accept_info = malloc(sizeof(sctp_accepted_link_info_t)); - if (!accept_info) { - savederrno = errno; - err = -1; - goto exit_error; - } - memset(accept_info, 0, sizeof(sctp_accepted_link_info_t)); - - accept_info->link_info = info; - - if (_set_fd_tracker(knet_h, new_fd, KNET_TRANSPORT_SCTP, SCTP_ACCEPTED_LINK_INFO, accept_info) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set fd tracker: %s", - strerror(errno)); - goto exit_error; - } - - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = new_fd; - if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_ADD, new_fd, &ev)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Incoming: unable to add accepted socket %d to epoll pool: %s", - new_fd, strerror(errno)); - goto exit_error; - } - info->on_rx_epoll = 1; - - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Incoming: accepted new fd %d for %s/%s (listen fd: %d). index: %d", - new_fd, addr_str, port_str, info->listen_sock, i); - -exit_error: - if (err) { - if ((i >= 0) || (i < MAX_ACCEPTED_SOCKS)) { - info->accepted_socks[i] = -1; - } - _set_fd_tracker(knet_h, new_fd, KNET_MAX_TRANSPORTS, SCTP_NO_LINK_INFO, NULL); - free(accept_info); - close(new_fd); - } - errno = savederrno; - return; -} - -/* - * Listen thread received a notification of a bad socket that needs closing - * called with a write lock from main thread - */ -static void _handle_listen_sctp_errors(knet_handle_t knet_h) -{ - int sockfd = -1; - sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; - sctp_accepted_link_info_t *accept_info; - sctp_listen_link_info_t *info; - struct knet_host *host; - int link_idx; - int i; - - if (recv(handle_info->listensockfd[0], &sockfd, sizeof(int), MSG_DONTWAIT | MSG_NOSIGNAL) != sizeof(int)) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Short read on listensockfd"); - return; - } - - if (_is_valid_fd(knet_h, sockfd) < 1) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received stray notification for listen socket fd error"); - return; - } - - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Processing listen error on socket: %d", sockfd); - - accept_info = knet_h->knet_transport_fd_tracker[sockfd].data; - info = accept_info->link_info; - - /* - * clear all links using this accepted socket as - * outbound dynamically connected socket - */ - - for (host = knet_h->host_head; host != NULL; host = host->next) { - for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { - if ((host->link[link_idx].dynamic == KNET_LINK_DYNIP) && - (host->link[link_idx].outsock == sockfd)) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Found dynamic connection on host %d link %d (%d)", - host->host_id, link_idx, sockfd); - host->link[link_idx].status.dynconnected = 0; - host->link[link_idx].transport_connected = 0; - host->link[link_idx].outsock = 0; - memset(&host->link[link_idx].dst_addr, 0, sizeof(struct sockaddr_storage)); - } - } - } - - for (i=0; iaccepted_socks[i]) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Closing accepted socket %d", sockfd); - _set_fd_tracker(knet_h, sockfd, KNET_MAX_TRANSPORTS, SCTP_NO_LINK_INFO, NULL); - info->accepted_socks[i] = -1; - free(accept_info); - close(sockfd); - } - } -} - -static void *_sctp_listen_thread(void *data) -{ - int savederrno; - int i, nev; - knet_handle_t knet_h = (knet_handle_t) data; - sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; - struct epoll_event events[KNET_EPOLL_MAX_EVENTS]; - - while (!shutdown_in_progress(knet_h)) { - nev = epoll_wait(handle_info->listen_epollfd, events, KNET_EPOLL_MAX_EVENTS, -1); - - if (nev < 0) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP listen handler EPOLL ERROR: %s", - strerror(errno)); - continue; - } - - savederrno = pthread_rwlock_wrlock(&knet_h->global_rwlock); - if (savederrno) { - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to get write lock: %s", - strerror(savederrno)); - continue; - } - /* - * Sort out which FD has an incoming connection - */ - for (i = 0; i < nev; i++) { - if (events[i].data.fd == handle_info->listensockfd[0]) { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received notification from rx_error for listener/accepted socket"); - _handle_listen_sctp_errors(knet_h); - } else { - if (_is_valid_fd(knet_h, events[i].data.fd) == 1) { - _handle_incoming_sctp(knet_h, events[i].data.fd); - } else { - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received listen notification from invalid socket"); - } - } - - } - pthread_rwlock_unlock(&knet_h->global_rwlock); - } - return NULL; -} - -/* - * sctp_link_listener_start/stop are called in global write lock - * context from set_config and clear_config. - */ -static sctp_listen_link_info_t *sctp_link_listener_start(knet_handle_t knet_h, struct knet_link *kn_link) -{ - int err = 0, savederrno = 0; - int listen_sock = -1; - struct epoll_event ev; - sctp_listen_link_info_t *info = NULL; - sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; - - /* - * Only allocate a new listener if src address is different - */ - knet_list_for_each_entry(info, &handle_info->listen_links_list, list) { - if (memcmp(&info->src_address, &kn_link->src_addr, sizeof(struct sockaddr_storage)) == 0) { - return info; - } - } - - info = malloc(sizeof(sctp_listen_link_info_t)); - if (!info) { - err = -1; - goto exit_error; - } - - memset(info, 0, sizeof(sctp_listen_link_info_t)); - - memset(info->accepted_socks, -1, sizeof(info->accepted_socks)); - memcpy(&info->src_address, &kn_link->src_addr, sizeof(struct sockaddr_storage)); - - listen_sock = socket(kn_link->src_addr.ss_family, SOCK_STREAM, IPPROTO_SCTP); - if (listen_sock < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to create listener socket: %s", - strerror(savederrno)); - goto exit_error; - } - - if (_configure_sctp_socket(knet_h, listen_sock, &kn_link->src_addr, "SCTP listener") < 0) { - savederrno = errno; - err = -1; - goto exit_error; - } - - if (bind(listen_sock, (struct sockaddr *)&kn_link->src_addr, sockaddr_len(&kn_link->src_addr)) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to bind listener socket: %s", - strerror(savederrno)); - goto exit_error; - } - - if (listen(listen_sock, 5) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to listen on listener socket: %s", - strerror(savederrno)); - goto exit_error; - } - - if (_set_fd_tracker(knet_h, listen_sock, KNET_TRANSPORT_SCTP, SCTP_LISTENER_LINK_INFO, info) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set fd tracker: %s", - strerror(savederrno)); - goto exit_error; - } - - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = listen_sock; - if (epoll_ctl(handle_info->listen_epollfd, EPOLL_CTL_ADD, listen_sock, &ev)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to add listener to epoll pool: %s", - strerror(savederrno)); - goto exit_error; - } - info->on_listener_epoll = 1; - - info->listen_sock = listen_sock; - knet_list_add(&info->list, &handle_info->listen_links_list); - - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Listening on fd %d for %s:%s", listen_sock, kn_link->status.src_ipaddr, kn_link->status.src_port); - -exit_error: - if (err) { - if (info->on_listener_epoll) { - epoll_ctl(handle_info->listen_epollfd, EPOLL_CTL_DEL, listen_sock, &ev); - } - if (listen_sock >= 0) { - close(listen_sock); - } - if (info) { - free(info); - info = NULL; - } - } - errno = savederrno; - return info; -} - -static int sctp_link_listener_stop(knet_handle_t knet_h, struct knet_link *kn_link) -{ - int err = 0, savederrno = 0; - int found = 0, i; - struct knet_host *host; - int link_idx; - sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; - sctp_connect_link_info_t *this_link_info = kn_link->transport_link; - sctp_listen_link_info_t *info = this_link_info->listener; - sctp_connect_link_info_t *link_info; - struct epoll_event ev; - - for (host = knet_h->host_head; host != NULL; host = host->next) { - for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { - if (&host->link[link_idx] == kn_link) - continue; - - link_info = host->link[link_idx].transport_link; - if ((link_info) && - (link_info->listener == info) && - (host->link[link_idx].status.enabled == 1)) { - found = 1; - break; - } - } - } - - if (found) { - this_link_info->listener = NULL; - log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP listener socket %d still in use", info->listen_sock); - savederrno = EBUSY; - err = -1; - goto exit_error; - } - - if (info->on_listener_epoll) { - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = info->listen_sock; - if (epoll_ctl(handle_info->listen_epollfd, EPOLL_CTL_DEL, info->listen_sock, &ev)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove listener to epoll pool: %s", - strerror(savederrno)); - goto exit_error; - } - info->on_listener_epoll = 0; - } - - if (_set_fd_tracker(knet_h, info->listen_sock, KNET_MAX_TRANSPORTS, SCTP_NO_LINK_INFO, NULL) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set fd tracker: %s", - strerror(savederrno)); - goto exit_error; - } - - close(info->listen_sock); - - for (i=0; i< MAX_ACCEPTED_SOCKS; i++) { - if (info->accepted_socks[i] > -1) { - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = info->accepted_socks[i]; - if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_DEL, info->accepted_socks[i], &ev)) { - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove EOFed socket from epoll pool: %s", - strerror(errno)); - } - info->on_rx_epoll = 0; - free(knet_h->knet_transport_fd_tracker[info->accepted_socks[i]].data); - close(info->accepted_socks[i]); - if (_set_fd_tracker(knet_h, info->accepted_socks[i], KNET_MAX_TRANSPORTS, SCTP_NO_LINK_INFO, NULL) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set fd tracker: %s", - strerror(savederrno)); - goto exit_error; - } - info->accepted_socks[i] = -1; - } - } - - knet_list_del(&info->list); - free(info); - this_link_info->listener = NULL; - -exit_error: - errno = savederrno; - return err; -} - -/* - * Links config/clear. Both called with global wrlock from link_set_config/clear_config - */ -static int sctp_transport_link_set_config(knet_handle_t knet_h, struct knet_link *kn_link) -{ - int savederrno = 0, err = 0; - sctp_connect_link_info_t *info; - sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; - - info = malloc(sizeof(sctp_connect_link_info_t)); - if (!info) { - goto exit_error; - } - - memset(info, 0, sizeof(sctp_connect_link_info_t)); - - kn_link->transport_link = info; - info->link = kn_link; - - memcpy(&info->dst_address, &kn_link->dst_addr, sizeof(struct sockaddr_storage)); - info->on_connected_epoll = 0; - info->connect_sock = -1; - - info->listener = sctp_link_listener_start(knet_h, kn_link); - if (!info->listener) { - savederrno = errno; - err = -1; - goto exit_error; - } - - if (kn_link->dynamic == KNET_LINK_STATIC) { - if (_create_connect_socket(knet_h, kn_link) < 0) { - savederrno = errno; - err = -1; - goto exit_error; - } - kn_link->outsock = info->connect_sock; - } - - knet_list_add(&info->list, &handle_info->connect_links_list); - -exit_error: - if (err) { - if (info) { - if (info->connect_sock) { - close(info->connect_sock); - } - if (info->listener) { - sctp_link_listener_stop(knet_h, kn_link); - } - kn_link->transport_link = NULL; - free(info); - } - } - errno = savederrno; - return err; -} - -/* - * called with global wrlock - */ -static int sctp_transport_link_clear_config(knet_handle_t knet_h, struct knet_link *kn_link) -{ - int err = 0, savederrno = 0; - sctp_connect_link_info_t *info; - struct epoll_event ev; - - if (!kn_link) { - errno = EINVAL; - return -1; - } - - info = kn_link->transport_link; - - if (!info) { - errno = EINVAL; - return -1; - } - - if ((sctp_link_listener_stop(knet_h, kn_link) <0) && (errno != EBUSY)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove listener trasport: %s", - strerror(savederrno)); - goto exit_error; - } - - if (info->on_rx_epoll) { - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = info->connect_sock; - if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_DEL, info->connect_sock, &ev)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove connected socket from epoll pool: %s", - strerror(savederrno)); - goto exit_error; - } - info->on_rx_epoll = 0; - } - - if (_close_connect_socket(knet_h, kn_link) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to close connected socket: %s", - strerror(savederrno)); - goto exit_error; - } - - knet_list_del(&info->list); - - free(info); - kn_link->transport_link = NULL; - -exit_error: - errno = savederrno; - return err; -} - -/* - * transport_free and transport_init are - * called only from knet_handle_new and knet_handle_free. - * all resources (hosts/links) should have been already freed at this point - * and they are called in a write locked context, hence they - * don't need their own locking. - */ - -static int sctp_transport_free(knet_handle_t knet_h) -{ - sctp_handle_info_t *handle_info; - void *thread_status; - struct epoll_event ev; - - if (!knet_h->transports[KNET_TRANSPORT_SCTP]) { - errno = EINVAL; - return -1; - } - - handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; - - /* - * keep it here while we debug list usage and such - */ - if (!knet_list_empty(&handle_info->listen_links_list)) { - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Internal error. listen links list is not empty"); - } - if (!knet_list_empty(&handle_info->connect_links_list)) { - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Internal error. connect links list is not empty"); - } - - if (handle_info->listen_thread) { - pthread_cancel(handle_info->listen_thread); - pthread_join(handle_info->listen_thread, &thread_status); - } - - if (handle_info->connect_thread) { - pthread_cancel(handle_info->connect_thread); - pthread_join(handle_info->connect_thread, &thread_status); - } - - if (handle_info->listensockfd[0] >= 0) { - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = handle_info->listensockfd[0]; - epoll_ctl(handle_info->listen_epollfd, EPOLL_CTL_DEL, handle_info->listensockfd[0], &ev); - } - - if (handle_info->connectsockfd[0] >= 0) { - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = handle_info->connectsockfd[0]; - epoll_ctl(handle_info->connect_epollfd, EPOLL_CTL_DEL, handle_info->connectsockfd[0], &ev); - } - - _close_socketpair(knet_h, handle_info->connectsockfd); - _close_socketpair(knet_h, handle_info->listensockfd); - - if (handle_info->listen_epollfd >= 0) { - close(handle_info->listen_epollfd); - } - - if (handle_info->connect_epollfd >= 0) { - close(handle_info->connect_epollfd); - } - - free(handle_info); - knet_h->transports[KNET_TRANSPORT_SCTP] = NULL; - - return 0; -} - -static int sctp_transport_init(knet_handle_t knet_h) -{ - int err = 0, savederrno = 0; - sctp_handle_info_t *handle_info; - struct epoll_event ev; - - if (knet_h->transports[KNET_TRANSPORT_SCTP]) { - errno = EEXIST; - return -1; - } - - handle_info = malloc(sizeof(sctp_handle_info_t)); - if (!handle_info) { - return -1; - } - - memset(handle_info, 0,sizeof(sctp_handle_info_t)); - - knet_h->transports[KNET_TRANSPORT_SCTP] = handle_info; - - knet_list_init(&handle_info->listen_links_list); - knet_list_init(&handle_info->connect_links_list); - - handle_info->listen_epollfd = epoll_create(KNET_EPOLL_MAX_EVENTS + 1); - if (handle_info->listen_epollfd < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to create epoll listen fd: %s", - strerror(savederrno)); - goto exit_fail; - } - - if (_fdset_cloexec(handle_info->listen_epollfd)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set CLOEXEC on listen_epollfd: %s", - strerror(savederrno)); - goto exit_fail; - } - - handle_info->connect_epollfd = epoll_create(KNET_EPOLL_MAX_EVENTS + 1); - if (handle_info->connect_epollfd < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to create epoll connect fd: %s", - strerror(savederrno)); - goto exit_fail; - } - - if (_fdset_cloexec(handle_info->connect_epollfd)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set CLOEXEC on connect_epollfd: %s", - strerror(savederrno)); - goto exit_fail; - } - - if (_init_socketpair(knet_h, handle_info->connectsockfd) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to init connect socketpair: %s", - strerror(savederrno)); - goto exit_fail; - } - - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = handle_info->connectsockfd[0]; - if (epoll_ctl(handle_info->connect_epollfd, EPOLL_CTL_ADD, handle_info->connectsockfd[0], &ev)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to add connectsockfd[0] to connect epoll pool: %s", - strerror(savederrno)); - goto exit_fail; - } - - if (_init_socketpair(knet_h, handle_info->listensockfd) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to init listen socketpair: %s", - strerror(savederrno)); - goto exit_fail; - } - - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = handle_info->listensockfd[0]; - if (epoll_ctl(handle_info->listen_epollfd, EPOLL_CTL_ADD, handle_info->listensockfd[0], &ev)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to add listensockfd[0] to listen epoll pool: %s", - strerror(savederrno)); - goto exit_fail; - } - - /* - * Start connect & listener threads - */ - savederrno = pthread_create(&handle_info->listen_thread, 0, _sctp_listen_thread, (void *) knet_h); - if (savederrno) { - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to start sctp listen thread: %s", - strerror(savederrno)); - goto exit_fail; - } - - savederrno = pthread_create(&handle_info->connect_thread, 0, _sctp_connect_thread, (void *) knet_h); - if (savederrno) { - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to start sctp connect thread: %s", - strerror(savederrno)); - goto exit_fail; - } - -exit_fail: - if (err < 0) { - sctp_transport_free(knet_h); - } - errno = savederrno; - return err; -} - -static int sctp_transport_link_dyn_connect(knet_handle_t knet_h, int sockfd, struct knet_link *kn_link) -{ - kn_link->outsock = sockfd; - kn_link->status.dynconnected = 1; - kn_link->transport_connected = 1; - return 0; -} - -static knet_transport_ops_t sctp_transport_ops = { - .transport_name = "SCTP", - .transport_id = KNET_TRANSPORT_SCTP, - .transport_mtu_overhead = KNET_PMTUD_SCTP_OVERHEAD, - .transport_init = sctp_transport_init, - .transport_free = sctp_transport_free, - .transport_link_set_config = sctp_transport_link_set_config, - .transport_link_clear_config = sctp_transport_link_clear_config, - .transport_link_dyn_connect = sctp_transport_link_dyn_connect, - .transport_rx_sock_error = sctp_transport_rx_sock_error, - .transport_tx_sock_error = sctp_transport_tx_sock_error, - .transport_rx_is_data = sctp_transport_rx_is_data, -}; - -knet_transport_ops_t *get_sctp_transport() -{ - - return &sctp_transport_ops; -} -#else // HAVE_NETINET_SCTP_H -knet_transport_ops_t *get_sctp_transport() -{ - return NULL; -} -#endif diff --git a/libknet/transport_udp.c b/libknet/transport_udp.c deleted file mode 100644 index 946879fe..00000000 --- a/libknet/transport_udp.c +++ /dev/null @@ -1,408 +0,0 @@ -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#if defined (IP_RECVERR) || defined (IPV6_RECVERR) -#include -#endif - -#include "libknet.h" -#include "compat.h" -#include "host.h" -#include "link.h" -#include "logging.h" -#include "common.h" -#include "transports.h" -#include "threads_common.h" - -#define KNET_PMTUD_UDP_OVERHEAD 8 - -typedef struct udp_handle_info { - struct knet_list_head links_list; -} udp_handle_info_t; - -typedef struct udp_link_info { - struct knet_list_head list; - struct sockaddr_storage local_address; - int socket_fd; - int on_epoll; -} udp_link_info_t; - -static int udp_transport_link_set_config(knet_handle_t knet_h, struct knet_link *kn_link) -{ - int err = 0, savederrno = 0; - int sock = -1; - struct epoll_event ev; - udp_link_info_t *info; - udp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_UDP]; -#if defined (IP_RECVERR) || defined (IPV6_RECVERR) - int value; -#endif - - /* - * Only allocate a new link if the local address is different - */ - knet_list_for_each_entry(info, &handle_info->links_list, list) { - if (memcmp(&info->local_address, &kn_link->src_addr, sizeof(struct sockaddr_storage)) == 0) { - log_debug(knet_h, KNET_SUB_TRANSP_UDP, "Re-using existing UDP socket for new link"); - kn_link->outsock = info->socket_fd; - kn_link->transport_link = info; - kn_link->transport_connected = 1; - return 0; - } - } - - info = malloc(sizeof(udp_link_info_t)); - if (!info) { - err = -1; - goto exit_error; - } - - sock = socket(kn_link->src_addr.ss_family, SOCK_DGRAM, 0); - if (sock < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_LISTENER, "Unable to create listener socket: %s", - strerror(savederrno)); - goto exit_error; - } - - if (_configure_transport_socket(knet_h, sock, &kn_link->src_addr, "UDP") < 0) { - savederrno = errno; - err = -1; - goto exit_error; - } - -#ifdef IP_RECVERR - if (kn_link->src_addr.ss_family == AF_INET) { - value = 1; - if (setsockopt(sock, SOL_IP, IP_RECVERR, &value, sizeof(value)) <0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to set RECVERR on socket: %s", - strerror(savederrno)); - goto exit_error; - } - } -#endif -#ifdef IPV6_RECVERR - if (kn_link->src_addr.ss_family == AF_INET6) { - value = 1; - if (setsockopt(sock, SOL_IPV6, IPV6_RECVERR, &value, sizeof(value)) <0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to set RECVERR on socket: %s", - strerror(savederrno)); - goto exit_error; - } - } -#endif - - if (bind(sock, (struct sockaddr *)&kn_link->src_addr, sockaddr_len(&kn_link->src_addr))) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to bind listener socket: %s", - strerror(savederrno)); - goto exit_error; - } - - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = sock; - - if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_ADD, sock, &ev)) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to add listener to epoll pool: %s", - strerror(savederrno)); - goto exit_error; - } - - info->on_epoll = 1; - - if (_set_fd_tracker(knet_h, sock, KNET_TRANSPORT_UDP, 0, info) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to set fd tracker: %s", - strerror(savederrno)); - goto exit_error; - } - - memcpy(&info->local_address, &kn_link->src_addr, sizeof(struct sockaddr_storage)); - info->socket_fd = sock; - knet_list_add(&info->list, &handle_info->links_list); - - kn_link->outsock = sock; - kn_link->transport_link = info; - kn_link->transport_connected = 1; - -exit_error: - if (err) { - if (info) { - if (info->on_epoll) { - epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_DEL, sock, &ev); - } - free(info); - } - if (sock >= 0) { - close(sock); - } - } - errno = savederrno; - return err; -} - -static int udp_transport_link_clear_config(knet_handle_t knet_h, struct knet_link *kn_link) -{ - int err = 0, savederrno = 0; - int found = 0; - struct knet_host *host; - int link_idx; - udp_link_info_t *info = kn_link->transport_link; - struct epoll_event ev; - - for (host = knet_h->host_head; host != NULL; host = host->next) { - for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { - if (&host->link[link_idx] == kn_link) - continue; - - if ((host->link[link_idx].transport_link == info) && - (host->link[link_idx].status.enabled == 1)) { - found = 1; - break; - } - } - } - - if (found) { - log_debug(knet_h, KNET_SUB_TRANSP_UDP, "UDP socket %d still in use", info->socket_fd); - savederrno = EBUSY; - err = -1; - goto exit_error; - } - - if (info->on_epoll) { - memset(&ev, 0, sizeof(struct epoll_event)); - ev.events = EPOLLIN; - ev.data.fd = info->socket_fd; - - if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_DEL, info->socket_fd, &ev) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to remove UDP socket from epoll poll: %s", - strerror(errno)); - goto exit_error; - } - info->on_epoll = 0; - } - - if (_set_fd_tracker(knet_h, info->socket_fd, KNET_MAX_TRANSPORTS, 0, NULL) < 0) { - savederrno = errno; - err = -1; - log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to set fd tracker: %s", - strerror(savederrno)); - goto exit_error; - } - - close(info->socket_fd); - knet_list_del(&info->list); - free(kn_link->transport_link); - -exit_error: - errno = savederrno; - return err; -} - -static int udp_transport_free(knet_handle_t knet_h) -{ - udp_handle_info_t *handle_info; - - if (!knet_h->transports[KNET_TRANSPORT_UDP]) { - errno = EINVAL; - return -1; - } - - handle_info = knet_h->transports[KNET_TRANSPORT_UDP]; - - /* - * keep it here while we debug list usage and such - */ - if (!knet_list_empty(&handle_info->links_list)) { - log_err(knet_h, KNET_SUB_TRANSP_UDP, "Internal error. handle list is not empty"); - return -1; - } - - free(handle_info); - - knet_h->transports[KNET_TRANSPORT_UDP] = NULL; - - return 0; -} - -static int udp_transport_init(knet_handle_t knet_h) -{ - udp_handle_info_t *handle_info; - - if (knet_h->transports[KNET_TRANSPORT_UDP]) { - errno = EEXIST; - return -1; - } - - handle_info = malloc(sizeof(udp_handle_info_t)); - if (!handle_info) { - return -1; - } - - memset(handle_info, 0, sizeof(udp_handle_info_t)); - - knet_h->transports[KNET_TRANSPORT_UDP] = handle_info; - - knet_list_init(&handle_info->links_list); - - return 0; -} - -#if defined (IP_RECVERR) || defined (IPV6_RECVERR) -static int read_errs_from_sock(knet_handle_t knet_h, int sockfd) -{ - int err = 0, savederrno = 0; - int got_err = 0; - char buffer[1024]; - struct iovec iov; - struct msghdr msg; - struct cmsghdr *cmsg; - struct sock_extended_err *sock_err; - struct icmphdr icmph; - struct sockaddr_storage remote; - struct sockaddr_storage *origin; - char addr_str[KNET_MAX_HOST_LEN]; - char port_str[KNET_MAX_PORT_LEN]; - - iov.iov_base = &icmph; - iov.iov_len = sizeof(icmph); - msg.msg_name = (void*)&remote; - msg.msg_namelen = sizeof(remote); - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - msg.msg_flags = 0; - msg.msg_control = buffer; - msg.msg_controllen = sizeof(buffer); - - for (;;) { - err = recvmsg(sockfd, &msg, MSG_ERRQUEUE); - savederrno = errno; - if (err < 0) { - if (!got_err) { - errno = savederrno; - return -1; - } else { - return 0; - } - } - got_err = 1; - for (cmsg = CMSG_FIRSTHDR(&msg);cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { - if (((cmsg->cmsg_level == SOL_IP) && (cmsg->cmsg_type == IP_RECVERR)) || - ((cmsg->cmsg_level == SOL_IPV6 && (cmsg->cmsg_type == IPV6_RECVERR)))) { - sock_err = (struct sock_extended_err*)CMSG_DATA(cmsg); - if (sock_err) { - switch (sock_err->ee_origin) { - case 0: /* no origin */ - case 1: /* local source (EMSGSIZE) */ - /* - * those errors are way too noisy - */ - break; - case 2: /* ICMP */ - case 3: /* ICMP6 */ - origin = (struct sockaddr_storage *)SO_EE_OFFENDER(sock_err); - if (knet_addrtostr(origin, sizeof(origin), - addr_str, KNET_MAX_HOST_LEN, - port_str, KNET_MAX_PORT_LEN) < 0) { - log_debug(knet_h, KNET_SUB_TRANSP_UDP, "Received ICMP error from unknown source: %s", strerror(sock_err->ee_errno)); - - } else { - log_debug(knet_h, KNET_SUB_TRANSP_UDP, "Received ICMP error from %s: %s", addr_str, strerror(sock_err->ee_errno)); - } - break; - } - } else { - log_debug(knet_h, KNET_SUB_TRANSP_UDP, "No data in MSG_ERRQUEUE"); - } - } - } - } -} -#else -static int read_errs_from_sock(knet_handle_t knet_h, int sockfd) -{ - return 0; -} -#endif - -static int udp_transport_rx_sock_error(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno) -{ - if (recv_errno == EAGAIN) { - read_errs_from_sock(knet_h, sockfd); - } - return 0; -} - -static int udp_transport_tx_sock_error(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno) -{ - if (recv_err < 0) { - if ((recv_errno == ENOBUFS) || (recv_errno == EAGAIN)) { -#ifdef DEBUG - log_debug(knet_h, KNET_SUB_TRANSP_UDP, "Sock: %d is overloaded. Slowing TX down", sockfd); -#endif - usleep(KNET_THREADS_TIMERES / 16); - return 1; - } - read_errs_from_sock(knet_h, sockfd); - if (recv_errno == EMSGSIZE) { - return 0; - } - return -1; - } - - return 0; -} - -static int udp_transport_rx_is_data(knet_handle_t knet_h, int sockfd, struct knet_mmsghdr *msg) -{ - if (msg->msg_len == 0) - return 0; - - return 2; -} - -static int udp_transport_link_dyn_connect(knet_handle_t knet_h, int sockfd, struct knet_link *kn_link) -{ - kn_link->status.dynconnected = 1; - return 0; -} - -static knet_transport_ops_t udp_transport_ops = { - .transport_name = "UDP", - .transport_id = KNET_TRANSPORT_UDP, - .transport_mtu_overhead = KNET_PMTUD_UDP_OVERHEAD, - .transport_init = udp_transport_init, - .transport_free = udp_transport_free, - .transport_link_set_config = udp_transport_link_set_config, - .transport_link_clear_config = udp_transport_link_clear_config, - .transport_link_dyn_connect = udp_transport_link_dyn_connect, - .transport_rx_sock_error = udp_transport_rx_sock_error, - .transport_tx_sock_error = udp_transport_tx_sock_error, - .transport_rx_is_data = udp_transport_rx_is_data, -}; - -knet_transport_ops_t *get_udp_transport() -{ - return &udp_transport_ops; -} diff --git a/libknet/transports.h b/libknet/transports.h deleted file mode 100644 index 343a13fe..00000000 --- a/libknet/transports.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (C) 2016 Red Hat, Inc. All rights reserved. - * - * Authors: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __KNET_TRANSPORTS_H__ -#define __KNET_TRANSPORTS_H__ - -knet_transport_ops_t *get_udp_transport(void); -knet_transport_ops_t *get_sctp_transport(void); - -int _configure_common_socket(knet_handle_t knet_h, int sock, const char *type); -int _configure_transport_socket(knet_handle_t knet_h, int sock, struct sockaddr_storage *address, const char *type); - -int _init_socketpair(knet_handle_t knet_h, int *sock); -void _close_socketpair(knet_handle_t knet_h, int *sock); - -int _set_fd_tracker(knet_handle_t knet_h, int sockfd, uint8_t transport, uint8_t data_type, void *data); -int _is_valid_fd(knet_handle_t knet_h, int sockfd); - -int _sendmmsg(int sockfd, struct knet_mmsghdr *msgvec, unsigned int vlen, unsigned int flags); -int _recvmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags); - -#endif diff --git a/libtap/Makefile.am b/libtap/Makefile.am deleted file mode 100644 index 261ffda1..00000000 --- a/libtap/Makefile.am +++ /dev/null @@ -1,60 +0,0 @@ -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -MAINTAINERCLEANFILES = Makefile.in - -include $(top_srcdir)/build-aux/check.mk - - -SYMFILE = libtap_exported_syms - -EXTRA_DIST = $(SYMFILE) tap_updown_bad tap_updown_good api-test-coverage - -sources = libtap.c - -libversion = 0:0:0 - -# override global LIBS that pulls in lots of craft we don't need here -LIBS = -lpthread - -if BUILD_LIBTAP - -include_HEADERS = libtap.h - -pkgconfigdir = $(libdir)/pkgconfig - -pkgconfig_DATA = libtap.pc - -lib_LTLIBRARIES = libtap.la - -libtap_la_SOURCES = $(sources) - -EXTRA_libtap_la_DEPENDENCIES = $(SYMFILE) - -libtap_la_LDFLAGS = -Wl,-version-script,$(srcdir)/$(SYMFILE) \ - --export-dynamic \ - -version-number $(libversion) - -TESTS = $(check_PROGRAMS) - -noinst_PROGRAMS = $(check_PROGRAMS) - -check_PROGRAMS = tap_test - -check-local: check-api-test-coverage - -check-api-test-coverage: - chmod u+x $(top_srcdir)/libtap/api-test-coverage - $(top_srcdir)/libtap/api-test-coverage $(top_srcdir) $(top_builddir) - -tap_test_SOURCES = $(sources) - -tap_test_CPPFLAGS = -DTEST \ - -DABSBUILDDIR=\"$(abs_builddir)\" - -endif diff --git a/libtap/api-test-coverage b/libtap/api-test-coverage deleted file mode 100755 index b2c9c97f..00000000 --- a/libtap/api-test-coverage +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/bash - -srcdir="$1"/libtap -builddir="$2"/libtap - -headerapicalls="$(grep tap_ "$srcdir"/libtap.h | grep -v "^ \*" | grep -v ^struct | grep -v "^[[:space:]]" | grep -v typedef | sed -e 's/(.*//g' -e 's/^const //g' -e 's/\*//g' | awk '{print $2}')" - -exportedapicalls="$(nm -B -D "$builddir"/.libs/libtap.so | grep ' T ' | awk '{print $3}')" - -echo "Checking for exported symbols NOT available in header file" - -for i in $exportedapicalls; do - found=0 - for x in $headerapicalls; do - if [ "$x" = "$i" ]; then - found=1 - break; - fi - done - if [ "$found" = 0 ]; then - echo "Symbol $i not found in header file" - exit 1 - fi -done - -echo "Checking for symbols in header file NOT exported by binary lib" - -for i in $headerapicalls; do - found=0 - for x in $exportedapicalls; do - if [ "$x" = "$i" ]; then - found=1 - break; - fi - done - if [ "$found" = 0 ]; then - echo "Symbol $i not found in binary lib" - exit 1 - fi -done - -exit 0 diff --git a/libtap/libtap.c b/libtap/libtap.c deleted file mode 100644 index c7f2373b..00000000 --- a/libtap/libtap.c +++ /dev/null @@ -1,2125 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "libtap.h" - -#define MAX_IP_CHAR 128 -#define MAX_PREFIX_CHAR 4 -#define MAX_MAC_CHAR 18 - -struct _ip { - char ip_addr[MAX_IP_CHAR]; - char prefix[MAX_PREFIX_CHAR]; - int domain; - struct _ip *next; -}; - -struct _iface { - struct ifreq ifr; - int fd; - char tapname[IFNAMSIZ]; - char default_mac[MAX_MAC_CHAR]; - int default_mtu; - int current_mtu; - char updownpath[PATH_MAX]; - int hasupdown; - int up; - struct _ip *ip; - struct _iface *next; -}; -#define ifname ifr.ifr_name - -struct _config { - struct _iface *head; - int sockfd; -}; - -static int lib_init = 0; -static struct _config lib_cfg; -static pthread_mutex_t lib_mutex = PTHREAD_MUTEX_INITIALIZER; - -/* forward declarations */ -static int _execute_shell(const char *command, char **error_string); -static int _exec_updown(const tap_t tap, const char *action, char **error_string); -static int _read_pipe(int fd, char **file, size_t *length); -static int _check(const tap_t tap); -static void _close(tap_t tap); -static void _close_cfg(void); -static int _get_mtu(const tap_t tap); -static int _get_mac(const tap_t tap, char **ether_addr); -static int _set_down(tap_t tap, char **error_down, char **error_postdown); -static char *_get_v4_broadcast(const char *ip_addr, const char *prefix); -static int _set_ip(tap_t tap, const char *command, - const char *ip_addr, const char *prefix, - char **error_string); -static int _find_ip(tap_t tap, - const char *ip_addr, const char *prefix, - struct _ip **ip, struct _ip **ip_prev); - -static int _read_pipe(int fd, char **file, size_t *length) -{ - char buf[4096]; - int n; - int done = 0; - - *file = NULL; - *length = 0; - - memset(buf, 0, sizeof(buf)); - - while (!done) { - - n = read(fd, buf, sizeof(buf)); - - if (n < 0) { - if (errno == EINTR) - continue; - - if (*file) - free(*file); - - return n; - } - - if (n == 0 && (!*length)) - return 0; - - if (n == 0) - done = 1; - - if (*file) - *file = realloc(*file, (*length) + n + done); - else - *file = malloc(n + done); - - if (!*file) - return -1; - - memcpy((*file) + (*length), buf, n); - *length += (done + n); - } - - /* Null terminator */ - (*file)[(*length) - 1] = 0; - - return 0; -} - -static int _execute_shell(const char *command, char **error_string) -{ - pid_t pid; - int status, err = 0; - int fd[2]; - size_t size = 0; - - if ((command == NULL) || (!error_string)) { - errno = EINVAL; - return -1; - } - - *error_string = NULL; - - err = pipe(fd); - if (err) - goto out_clean; - - pid = fork(); - if (pid < 0) { - err = pid; - goto out_clean; - } - - if (pid) { /* parent */ - - close(fd[1]); - err = _read_pipe(fd[0], error_string, &size); - if (err) - goto out_clean0; - - waitpid(pid, &status, 0); - if (!WIFEXITED(status)) { - err = -1; - goto out_clean0; - } - if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { - err = WEXITSTATUS(status); - goto out_clean0; - } - goto out_clean0; - } else { /* child */ - close(0); - close(1); - close(2); - - close(fd[0]); - dup2(fd[1], 1); - dup2(fd[1], 2); - close(fd[1]); - - execlp("/bin/sh", "/bin/sh", "-c", command, NULL); - exit(EXIT_FAILURE); - } - -out_clean: - close(fd[1]); -out_clean0: - close(fd[0]); - - return err; -} - -static int _exec_updown(const tap_t tap, const char *action, char **error_string) -{ - char command[PATH_MAX]; - struct stat sb; - int err = 0; - - if (!tap->hasupdown) - return 0; - - memset(command, 0, PATH_MAX); - - snprintf(command, PATH_MAX, "%s%s/%s", tap->updownpath, action, tap->tapname); - - err = stat(command, &sb); - if ((err < 0) && (errno == ENOENT)) - return 0; - - err = _execute_shell(command, error_string); - if ((!err) && (*error_string)) { - free(*error_string); - *error_string = NULL; - } - - return err; -} - -static int _check(const tap_t tap) -{ - tap_t temp = lib_cfg.head; - - if (!tap) { - return 0; - } - - while (temp != NULL) { - if (tap == temp) - return 1; - - temp = temp->next; - } - - return 0; -} - -static void _close(tap_t tap) -{ - if (!tap) - return; - - if (tap->fd) - close(tap->fd); - - free(tap); - - return; -} - -static void _close_cfg(void) -{ - if (lib_cfg.head == NULL) { - close(lib_cfg.sockfd); - lib_init = 0; - } -} - -static int _get_mtu(const tap_t tap) -{ - int err; - - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, tap->tapname, IFNAMSIZ); - - err = ioctl(lib_cfg.sockfd, SIOCGIFMTU, &tap->ifr); - if (err) - goto out_clean; - - err = tap->ifr.ifr_mtu; - -out_clean: - return err; -} - -static int _get_mac(const tap_t tap, char **ether_addr) -{ - int err; - char mac[MAX_MAC_CHAR]; - - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, tap->tapname, IFNAMSIZ); - - err = ioctl(lib_cfg.sockfd, SIOCGIFHWADDR, &tap->ifr); - if (err) - goto out_clean; - - ether_ntoa_r((struct ether_addr *)tap->ifr.ifr_hwaddr.sa_data, mac); - - *ether_addr = strdup(mac); - if (!*ether_addr) - err = -1; - -out_clean: - - return err; -} - -tap_t tap_find(char *dev, size_t dev_size) -{ - tap_t tap; - - if (dev == NULL) { - errno = EINVAL; - return NULL; - } - - if (dev_size < IFNAMSIZ) { - errno = EINVAL; - return NULL; - } - - if (strlen(dev) > IFNAMSIZ) { - errno = E2BIG; - return NULL; - } - - pthread_mutex_lock(&lib_mutex); - - tap = lib_cfg.head; - while (tap != NULL) { - if (!strcmp(dev, tap->tapname)) - break; - tap = tap->next; - } - - pthread_mutex_unlock(&lib_mutex); - return tap; -} - -tap_t tap_open(char *dev, size_t dev_size, const char *updownpath) -{ - tap_t tap; - char *temp_mac = NULL; - - if (dev == NULL) { - errno = EINVAL; - return NULL; - } - - if (dev_size < IFNAMSIZ) { - errno = EINVAL; - return NULL; - } - - if (strlen(dev) > IFNAMSIZ) { - errno = E2BIG; - return NULL; - } - - if (updownpath) { - /* only absolute paths */ - if (updownpath[0] != '/') { - errno = EINVAL; - return NULL; - } - /* 14: 2 for /, 1 for \0 + 11 (post-down.d) */ - if (strlen(updownpath) >= (PATH_MAX - (strlen(dev) + 14))) { - errno = E2BIG; - return NULL; - } - } - - pthread_mutex_lock(&lib_mutex); - - tap = malloc(sizeof(struct _iface)); - if (!tap) - return NULL; - - memset(tap, 0, sizeof(struct _iface)); - - if ((tap->fd = open("/dev/net/tun", O_RDWR)) < 0) - goto out_error; - - strncpy(tap->tapname, dev, IFNAMSIZ); - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, dev, IFNAMSIZ); - tap->ifr.ifr_flags = IFF_TAP | IFF_NO_PI; - - if (ioctl(tap->fd, TUNSETIFF, &tap->ifr) < 0) - goto out_error; - - if ((strlen(dev) > 0) && (strcmp(dev, tap->ifname) != 0)) { - errno = EBUSY; - goto out_error; - } - - strcpy(dev, tap->ifname); - strcpy(tap->tapname, tap->ifname); - - if (!lib_init) { - lib_cfg.head = NULL; - lib_cfg.sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (lib_cfg.sockfd < 0) - goto out_error; - lib_init = 1; - } - - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, tap->tapname, IFNAMSIZ); - if (ioctl(lib_cfg.sockfd, SIOGIFINDEX, &tap->ifr) < 0) - goto out_error; - - tap->default_mtu = _get_mtu(tap); - if (tap->default_mtu < 0) - goto out_error; - - if (_get_mac(tap, &temp_mac) < 0) - goto out_error; - - strncpy(tap->default_mac, temp_mac, 18); - free(temp_mac); - - if (updownpath) { - int len = strlen(updownpath); - - strcpy(tap->updownpath, updownpath); - if (tap->updownpath[len-1] != '/') { - tap->updownpath[len] = '/'; - } - tap->hasupdown = 1; - } - - tap->next = lib_cfg.head; - lib_cfg.head = tap; - - pthread_mutex_unlock(&lib_mutex); - return tap; - -out_error: - _close(tap); - _close_cfg(); - pthread_mutex_unlock(&lib_mutex); - return NULL; -} - -// TODO: consider better error report from here -int tap_close(tap_t tap) -{ - int err = 0; - tap_t temp = lib_cfg.head; - tap_t prev = lib_cfg.head; - struct _ip *ip, *ip_next; - char *error_string = NULL; - char *error_down = NULL, *error_postdown = NULL; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - while ((temp) && (temp != tap)) { - prev = temp; - temp = temp->next; - } - - if (tap == prev) { - lib_cfg.head = tap->next; - } else { - prev->next = tap->next; - } - - _set_down(tap, &error_down, &error_postdown); - if (error_down) - free(error_down); - if (error_postdown) - free(error_postdown); - - ip = tap->ip; - while (ip) { - ip_next = ip->next; - _set_ip(tap, "del", ip->ip_addr, ip->prefix, &error_string); - if (error_string) { - free(error_string); - error_string = NULL; - } - free(ip); - ip = ip_next; - } - - _close(tap); - _close_cfg(); - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_get_mtu(const tap_t tap) -{ - int err; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - err = _get_mtu(tap); - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_set_mtu(tap_t tap, const int mtu) -{ - struct _ip *tmp_ip; - char *error_string = NULL; - int err; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - err = tap->current_mtu = _get_mtu(tap); - if (err < 0) - goto out_clean; - - tap->ifr.ifr_mtu = mtu; - - err = ioctl(lib_cfg.sockfd, SIOCSIFMTU, &tap->ifr); - - if ((!err) && (tap->current_mtu < 1280) && (mtu >= 1280)) { - tmp_ip = tap->ip; - while(tmp_ip) { - if (tmp_ip->domain == AF_INET6) { - err = _set_ip(tap, "add", tmp_ip->ip_addr, tmp_ip->prefix, &error_string); - if (error_string) { - free(error_string); - error_string = NULL; - } - } - tmp_ip = tmp_ip->next; - } - } - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_reset_mtu(tap_t tap) -{ - return tap_set_mtu(tap, tap->default_mtu); -} - -int tap_get_mac(const tap_t tap, char **ether_addr) -{ - int err; - - pthread_mutex_lock(&lib_mutex); - - if ((!_check(tap)) || (!ether_addr)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - err = _get_mac(tap, ether_addr); - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_set_mac(tap_t tap, const char *ether_addr) -{ - int err; - - pthread_mutex_lock(&lib_mutex); - - if ((!_check(tap)) || (!ether_addr)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, tap->tapname, IFNAMSIZ); - err = ioctl(lib_cfg.sockfd, SIOCGIFHWADDR, &tap->ifr); - if (err) - goto out_clean; - - memcpy(tap->ifr.ifr_hwaddr.sa_data, ether_aton(ether_addr), ETH_ALEN); - - err = ioctl(lib_cfg.sockfd, SIOCSIFHWADDR, &tap->ifr); - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_reset_mac(tap_t tap) -{ - return tap_set_mac(tap, tap->default_mac); -} - -int tap_set_up(tap_t tap, char **error_preup, char **error_up) -{ - int err = 0; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - if ((tap->hasupdown) && ((!error_preup) || (!error_up))) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - if (tap->up) - goto out_clean; - - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, tap->tapname, IFNAMSIZ); - - err=ioctl(lib_cfg.sockfd, SIOCGIFFLAGS, &tap->ifr); - if (err) - goto out_clean; - - _exec_updown(tap, "pre-up.d", error_preup); - - tap->ifr.ifr_flags |= IFF_UP | IFF_RUNNING; - err=ioctl(lib_cfg.sockfd, SIOCSIFFLAGS, &tap->ifr); - - if (err) - goto out_clean; - - _exec_updown(tap, "up.d", error_up); - - tap->up = 1; -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -static int _set_down(tap_t tap, char **error_down, char **error_postdown) -{ - int err = 0; - - if (!tap->up) - goto out_clean; - - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, tap->tapname, IFNAMSIZ); - - err=ioctl(lib_cfg.sockfd, SIOCGIFFLAGS, &tap->ifr); - if (err) - goto out_clean; - - _exec_updown(tap, "down.d", error_down); - - tap->ifr.ifr_flags &= ~IFF_UP; - err=ioctl(lib_cfg.sockfd, SIOCSIFFLAGS, &tap->ifr); - - if (err) - goto out_clean; - - _exec_updown(tap, "post-down.d", error_postdown); - - tap->up = 0; - -out_clean: - return err; -} - -int tap_set_down(tap_t tap, char **error_down, char **error_postdown) -{ - int err = 0; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - if ((tap->hasupdown) && ((!error_down) || (!error_postdown))) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - err = _set_down(tap, error_down, error_postdown); - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -static char *_get_v4_broadcast(const char *ip_addr, const char *prefix) -{ - int prefix_len; - struct in_addr mask; - struct in_addr broadcast; - struct in_addr address; - - prefix_len = atoi(prefix); - - if ((prefix_len > 32) || (prefix_len < 0)) - return NULL; - - if (inet_pton(AF_INET, ip_addr, &address) <= 0) - return NULL; - - mask.s_addr = htonl(~((1 << (32 - prefix_len)) - 1)); - - memset(&broadcast, 0, sizeof(broadcast)); - broadcast.s_addr = (address.s_addr & mask.s_addr) | ~mask.s_addr; - - return strdup(inet_ntoa(broadcast)); -} - -static int _set_ip(tap_t tap, const char *command, - const char *ip_addr, const char *prefix, - char **error_string) -{ - char *broadcast = NULL; - char cmdline[4096]; - - if (!strchr(ip_addr, ':')) { - broadcast = _get_v4_broadcast(ip_addr, prefix); - if (!broadcast) { - errno = EINVAL; - return -1; - } - } - - memset(cmdline, 0, sizeof(cmdline)); - - if (broadcast) { - snprintf(cmdline, sizeof(cmdline)-1, - "ip addr %s %s/%s dev %s broadcast %s", - command, ip_addr, prefix, - tap->tapname, broadcast); - free(broadcast); - } else { - snprintf(cmdline, sizeof(cmdline)-1, - "ip addr %s %s/%s dev %s", - command, ip_addr, prefix, - tap->tapname); - } - - return _execute_shell(cmdline, error_string); -} - -static int _find_ip(tap_t tap, - const char *ip_addr, const char *prefix, - struct _ip **ip, struct _ip **ip_prev) -{ - struct _ip *local_ip, *local_ip_prev; - int found = 0; - - local_ip = local_ip_prev = tap->ip; - - while(local_ip) { - if ((!strcmp(local_ip->ip_addr, ip_addr)) && (!strcmp(local_ip->prefix, prefix))) { - found = 1; - break; - } - local_ip_prev = local_ip; - local_ip = local_ip->next; - } - - if (found) { - *ip = local_ip; - *ip_prev = local_ip_prev; - } - - return found; -} - -int tap_add_ip(tap_t tap, const char *ip_addr, const char *prefix, char **error_string) -{ - int err = 0, found; - struct _ip *ip = NULL, *ip_prev = NULL, *ip_last = NULL; - - pthread_mutex_lock(&lib_mutex); - - if ((!_check(tap)) || (!ip_addr) || (!prefix) || (!error_string)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - found = _find_ip(tap, ip_addr, prefix, &ip, &ip_prev); - if (found) - goto out_clean; - - ip = malloc(sizeof(struct _ip)); - if (!ip) { - err = -1 ; - goto out_clean; - } - memset(ip, 0, sizeof(struct _ip)); - strncpy(ip->ip_addr, ip_addr, MAX_IP_CHAR); - strncpy(ip->prefix, prefix, MAX_PREFIX_CHAR); - if (!strchr(ip->ip_addr, ':')) { - ip->domain = AF_INET; - } else { - ip->domain = AF_INET6; - } - - /* - * if user asks for an IPv6 address, but MTU < 1280 - * store the IP and bring it up later if and when MTU > 1280 - */ - if ((ip->domain == AF_INET6) && (_get_mtu(tap) < 1280)) { - err = 0; - } else { - err = _set_ip(tap, "add", ip_addr, prefix, error_string); - } - - if (err) { - free(ip); - goto out_clean; - } - - if (tap->ip) { - ip_last = tap->ip; - while (ip_last->next != NULL) { - ip_last = ip_last->next; - } - ip_last->next = ip; - } else { - tap->ip = ip; - } - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_del_ip(tap_t tap, const char *ip_addr, const char *prefix, char **error_string) -{ - int err = 0, found; - struct _ip *ip = NULL, *ip_prev = NULL; - - pthread_mutex_lock(&lib_mutex); - - if ((!_check(tap)) || (!ip_addr) || (!prefix) || (!error_string)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - found = _find_ip(tap, ip_addr, prefix, &ip, &ip_prev); - if (!found) - goto out_clean; - - err = _set_ip(tap, "del", ip_addr, prefix, error_string); - - if (!err) { - if (ip == ip_prev) { - tap->ip = ip->next; - } else { - ip_prev->next = ip->next; - } - free(ip); - } - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_get_fd(const tap_t tap) -{ - int fd; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - fd = -1; - goto out_clean; - } - - fd = tap->fd; - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return fd; -} - -const char *tap_get_name(const tap_t tap) -{ - char *name = NULL; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - goto out_clean; - } - - name = tap->tapname; - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return name; -} - -int tap_get_ips(const tap_t tap, char **ip_addr_list, int *entries) -{ - int err = 0; - int found = 0; - char *ip_list = NULL; - int size = 0, offset = 0, len; - struct _ip *ip = tap->ip; - - pthread_mutex_lock(&lib_mutex); - - while (ip) { - found++; - ip = ip->next; - } - - size = found * (MAX_IP_CHAR + MAX_PREFIX_CHAR + 2); - ip_list = malloc(size); - if (!ip_list) { - err = -1; - goto out_clean; - } - - memset(ip_list, 0, size); - - ip = tap->ip; - - while (ip) { - len = strlen(ip->ip_addr); - memcpy(ip_list + offset, ip->ip_addr, len); - offset = offset + len + 1; - len = strlen(ip->prefix); - memcpy(ip_list + offset, ip->prefix, len); - offset = offset + len + 1; - ip = ip->next; - } - - *ip_addr_list = ip_list; - *entries = found; - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -#ifdef TEST -static int is_if_in_system(char *name) -{ - struct ifaddrs *ifap = NULL; - struct ifaddrs *ifa; - int found = 0; - - if (getifaddrs(&ifap) < 0) { - printf("Unable to get interface list.\n"); - return -1; - } - - ifa = ifap; - - while (ifa) { - if (!strncmp(name, ifa->ifa_name, IFNAMSIZ)) { - found = 1; - break; - } - ifa=ifa->ifa_next; - } - - freeifaddrs(ifap); - return found; -} - -static int test_iface(char *name, size_t size, const char *updownpath) -{ - tap_t tap; - - tap=tap_open(name, size, updownpath); - if (!tap) { - if (lib_cfg.sockfd < 0) - printf("Unable to open knet_socket\n"); - printf("Unable to open knet.\n"); - return -1; - } - printf("Created interface: %s\n", name); - - if (is_if_in_system(name) > 0) { - printf("Found interface %s on the system\n", name); - } else { - printf("Unable to find interface %s on the system\n", name); - } - - if (!tap_find(name, size)) { - printf("Unable to find interface %s in tap db\n", name); - } else { - printf("Found interface %s in tap db\n", name); - } - - tap_close(tap); - - if (is_if_in_system(name) == 0) - printf("Successfully removed interface %s from the system\n", name); - - return 0; -} - -static int check_tap_open_close(void) -{ - char device_name[2*IFNAMSIZ]; - char fakepath[PATH_MAX]; - size_t size = IFNAMSIZ; - - memset(device_name, 0, sizeof(device_name)); - - printf("Creating random tap interface:\n"); - if (test_iface(device_name, size, NULL) < 0) { - printf("Unable to create random interface\n"); - return -1; - } - - printf("Creating kronostest tap interface:\n"); - strncpy(device_name, "kronostest", IFNAMSIZ); - if (test_iface(device_name, size, NULL) < 0) { - printf("Unable to create kronosnet interface\n"); - return -1; - } - - printf("Testing ERROR conditions\n"); - - printf("Testing dev == NULL\n"); - errno=0; - if ((test_iface(NULL, size, NULL) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_open sanity checks\n"); - return -1; - } - - printf("Testing size < IFNAMSIZ\n"); - errno=0; - if ((test_iface(device_name, 1, NULL) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_open sanity checks\n"); - return -1; - } - - printf("Testing device_name size > IFNAMSIZ\n"); - errno=0; - strcpy(device_name, "abcdefghilmnopqrstuvwz"); - if ((test_iface(device_name, IFNAMSIZ, NULL) >= 0) || (errno != E2BIG)) { - printf("Something is wrong in tap_open sanity checks\n"); - return -1; - } - - printf("Testing updown path != abs\n"); - errno=0; - strcpy(device_name, "kronostest"); - if ((test_iface(device_name, IFNAMSIZ, "foo") >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_open sanity checks\n"); - return -1; - } - - memset(fakepath, 0, PATH_MAX); - memset(fakepath, '/', PATH_MAX - 2); - - printf("Testing updown path > PATH_MAX\n"); - errno=0; - strcpy(device_name, "kronostest"); - if ((test_iface(device_name, IFNAMSIZ, fakepath) >= 0) || (errno != E2BIG)) { - printf("Something is wrong in tap_open sanity checks\n"); - return -1; - } - - return 0; -} - -static int check_knet_multi_eth(void) -{ - char device_name1[IFNAMSIZ]; - char device_name2[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap1 = NULL; - tap_t tap2 = NULL; - - printf("Testing multiple knet interface instances\n"); - - memset(device_name1, 0, size); - memset(device_name2, 0, size); - - strncpy(device_name1, "kronostest1", size); - strncpy(device_name2, "kronostest2", size); - - tap1 = tap_open(device_name1, size, NULL); - if (!tap1) { - printf("Unable to init %s\n", device_name1); - err = -1; - goto out_clean; - } - - if (is_if_in_system(device_name1) > 0) { - printf("Found interface %s on the system\n", device_name1); - } else { - printf("Unable to find interface %s on the system\n", device_name1); - } - - tap2 = tap_open(device_name2, size, NULL); - if (!tap2) { - printf("Unable to init %s\n", device_name2); - err = -1; - goto out_clean; - } - - if (is_if_in_system(device_name2) > 0) { - printf("Found interface %s on the system\n", device_name2); - } else { - printf("Unable to find interface %s on the system\n", device_name2); - } - - if (tap1) - tap_close(tap1); - if (tap2) - tap_close(tap2); - - printf("Testing error conditions\n"); - - printf("Open same device twice\n"); - - tap1 = tap_open(device_name1, size, NULL); - if (!tap1) { - printf("Unable to init %s\n", device_name1); - err = -1; - goto out_clean; - } - - if (is_if_in_system(device_name1) > 0) { - printf("Found interface %s on the system\n", device_name1); - } else { - printf("Unable to find interface %s on the system\n", device_name1); - } - - tap2 = tap_open(device_name1, size, NULL); - if (tap2) { - printf("We were able to init 2 interfaces with the same name!\n"); - err = -1; - goto out_clean; - } - -out_clean: - if (tap1) - tap_close(tap1); - if (tap2) - tap_close(tap2); - return err; -} - -static int check_knet_mtu(void) -{ - char device_name[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap; - - int current_mtu = 0; - int expected_mtu = 1500; - - printf("Testing get/set MTU\n"); - - memset(device_name, 0, size); - strncpy(device_name, "kronostest", size); - tap = tap_open(device_name, size, NULL); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Comparing default MTU\n"); - current_mtu = tap_get_mtu(tap); - if (current_mtu < 0) { - printf("Unable to get MTU\n"); - err = -1; - goto out_clean; - } - if (current_mtu != expected_mtu) { - printf("current mtu [%d] does not match expected default [%d]\n", current_mtu, expected_mtu); - err = -1; - goto out_clean; - } - - printf("Setting MTU to 9000\n"); - expected_mtu = 9000; - if (tap_set_mtu(tap, expected_mtu) < 0) { - printf("Unable to set MTU to %d\n", expected_mtu); - err = -1; - goto out_clean; - } - - current_mtu = tap_get_mtu(tap); - if (current_mtu < 0) { - printf("Unable to get MTU\n"); - err = -1; - goto out_clean; - } - if (current_mtu != expected_mtu) { - printf("current mtu [%d] does not match expected value [%d]\n", current_mtu, expected_mtu); - err = -1; - goto out_clean; - } - - printf("Testing ERROR conditions\n"); - - printf("Passing empty struct to get_mtu\n"); - if (tap_get_mtu(NULL) > 0) { - printf("Something is wrong in tap_get_mtu sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Passing empty struct to set_mtu\n"); - if (tap_set_mtu(NULL, 1500) == 0) { - printf("Something is wrong in tap_set_mtu sanity checks\n"); - err = -1; - goto out_clean; - } - -out_clean: - tap_close(tap); - - return err; -} - -static int check_knet_mtu_ipv6(void) -{ - char device_name[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap; - char *error_string = NULL; - - printf("Testing get/set MTU with IPv6 address\n"); - - memset(device_name, 0, size); - strncpy(device_name, "kronostest", size); - tap = tap_open(device_name, size, NULL); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Adding ip: 3ffe::1/64\n"); - - err = tap_add_ip(tap, "3ffe::1", "64", &error_string); - if (error_string) { - printf("add ipv6 output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::1/64", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err) { - printf("Unable to verify IP address\n"); - err=-1; - goto out_clean; - } - - printf("Setting MTU to 1200\n"); - if (tap_set_mtu(tap, 1200) < 0) { - printf("Unable to set MTU to 1200\n"); - err = -1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::1/64", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (!err) { - printf("Unable to verify IP address\n"); - err=-1; - goto out_clean; - } - - printf("Adding ip: 3ffe::2/64\n"); - err = tap_add_ip(tap, "3ffe::2", "64", &error_string); - if (error_string) { - printf("add ipv6 output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::2/64", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (!err) { - printf("Unable to verify IP address\n"); - err=-1; - goto out_clean; - } - - printf("Restoring MTU to default\n"); - if (tap_reset_mtu(tap) < 0) { - printf("Unable to reset mtu\n"); - err = -1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::1/64", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err) { - printf("Unable to verify IP address\n"); - err=-1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::2/64", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err) { - printf("Unable to verify IP address\n"); - err=-1; - goto out_clean; - } - -out_clean: - tap_close(tap); - - return err; -} - -static int check_knet_mac(void) -{ - char device_name[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap; - char *current_mac = NULL, *temp_mac = NULL, *err_mac = NULL; - struct ether_addr *cur_mac, *tmp_mac; - - printf("Testing get/set MAC\n"); - - memset(device_name, 0, size); - strncpy(device_name, "kronostest", size); - tap = tap_open(device_name, size, NULL); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Get current MAC\n"); - - if (tap_get_mac(tap, ¤t_mac) < 0) { - printf("Unable to get current MAC address.\n"); - err = -1; - goto out_clean; - } - - printf("Current MAC: %s\n", current_mac); - - printf("Setting MAC: 00:01:01:01:01:01\n"); - - if (tap_set_mac(tap, "00:01:01:01:01:01") < 0) { - printf("Unable to set current MAC address.\n"); - err = -1; - goto out_clean; - } - - if (tap_get_mac(tap, &temp_mac) < 0) { - printf("Unable to get current MAC address.\n"); - err = -1; - goto out_clean; - } - - printf("Current MAC: %s\n", temp_mac); - - cur_mac = ether_aton(current_mac); - tmp_mac = ether_aton(temp_mac); - - printf("Comparing MAC addresses\n"); - if (memcmp(cur_mac, tmp_mac, sizeof(struct ether_addr))) { - printf("Mac addresses are not the same?!\n"); - err = -1; - goto out_clean; - } - - printf("Testing ERROR conditions\n"); - - printf("Pass NULL to get_mac (pass1)\n"); - errno = 0; - if ((tap_get_mac(NULL, &err_mac) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_get_mac sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to get_mac (pass2)\n"); - errno = 0; - if ((tap_get_mac(tap, NULL) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_get_mac sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to set_mac (pass1)\n"); - errno = 0; - if ((tap_set_mac(tap, NULL) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_mac sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to set_mac (pass2)\n"); - errno = 0; - if ((tap_set_mac(NULL, err_mac) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_mac sanity checks\n"); - err = -1; - goto out_clean; - } - -out_clean: - if (err_mac) { - printf("Something managed to set err_mac!\n"); - err = -1; - free(err_mac); - } - - if (current_mac) - free(current_mac); - if (temp_mac) - free(temp_mac); - - tap_close(tap); - - return err; -} - -static int check_tap_execute_shell(void) -{ - int err = 0; - char command[4096]; - char *error_string = NULL; - - memset(command, 0, sizeof(command)); - - printf("Testing _execute_shell\n"); - - printf("command /bin/true\n"); - - err = _execute_shell("/bin/true", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to execute /bin/true ?!?!\n"); - goto out_clean; - } - - printf("Testing ERROR conditions\n"); - - printf("command /bin/false\n"); - - err = _execute_shell("/bin/false", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (!err) { - printf("Can we really execute /bin/false successfully?!?!\n"); - err = -1; - goto out_clean; - } - - printf("command that outputs to stdout (enforcing redirect)\n"); - - err = _execute_shell("/bin/grep -h 2>&1", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (!err) { - printf("Can we really execute /bin/grep -h successfully?!?\n"); - err = -1; - goto out_clean; - } - - printf("command that outputs to stderr\n"); - err = _execute_shell("/bin/grep -h", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (!err) { - printf("Can we really execute /bin/grep -h successfully?!?\n"); - err = -1; - goto out_clean; - } - - printf("empty command\n"); - err = _execute_shell(NULL, &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (!err) { - printf("Can we really execute (nil) successfully?!?!\n"); - err = -1; - goto out_clean; - } - - printf("empty error\n"); - err = _execute_shell("/bin/true", NULL); - if (!err) { - printf("Check EINVAL filter for no error_string!\n"); - err = -1; - goto out_clean; - } - - err = 0; - -out_clean: - - return err; -} - -static int check_knet_up_down(void) -{ - char device_name[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap; - char *error_string = NULL; - char *error_preup = NULL, *error_up = NULL; - char *error_down = NULL, *error_postdown = NULL; - - printf("Testing interface up/down\n"); - - memset(device_name, 0, size); - strncpy(device_name, "kronostest", size); - tap = tap_open(device_name, size, NULL); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Put the interface up\n"); - - err = tap_set_up(tap, &error_preup, &error_up); - if (error_preup) { - printf("preup output: %s\n", error_preup); - free(error_preup); - error_preup = NULL; - } - if (error_up) { - printf("up output: %s\n", error_up); - free(error_up); - error_up = NULL; - } - if (err < 0) { - printf("Unable to set interface up\n"); - err = -1; - goto out_clean; - } - - - err = _execute_shell("ip addr show dev kronostest | grep -q UP", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to verify inteface UP\n"); - err = -1; - goto out_clean; - } - - printf("Put the interface down\n"); - - err = tap_set_down(tap, &error_down, &error_postdown); - if (error_down) { - printf("down output: %s\n", error_down); - free(error_down); - error_down = NULL; - } - if (error_postdown) { - printf("postdown output: %s\n", error_down); - free(error_down); - error_down = NULL; - } - if (err < 0) { - printf("Unable to put the interface down\n"); - err = -1; - goto out_clean; - } - - err = _execute_shell("ifconfig kronostest | grep -q UP", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (!err) { - printf("Unable to verify inteface DOWN\n"); - err = -1; - goto out_clean; - } - - tap_close(tap); - - printf("Testing interface pre-up/up/down/post-down (exec errors)\n"); - - tap = tap_open(device_name, size, ABSBUILDDIR "/tap_updown_bad"); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Put the interface up\n"); - - err = tap_set_up(tap, &error_preup, &error_up); - if (error_preup) { - printf("preup output: %s\n", error_preup); - free(error_preup); - error_preup = NULL; - } - if (error_up) { - printf("up output: %s\n", error_up); - free(error_up); - error_up = NULL; - } - if (err < 0) { - printf("Unable to set interface up\n"); - err = -1; - goto out_clean; - } - - printf("Put the interface down\n"); - - err = tap_set_down(tap, &error_down, &error_postdown); - if (error_down) { - printf("down output: %s\n", error_down); - free(error_down); - error_down = NULL; - } - if (error_postdown) { - printf("postdown output: %s\n", error_down); - free(error_down); - error_down = NULL; - } - if (err < 0) { - printf("Unable to put the interface down\n"); - err = -1; - goto out_clean; - } - - tap_close(tap); - - printf("Testing interface pre-up/up/down/post-down\n"); - - tap = tap_open(device_name, size, ABSBUILDDIR "/tap_updown_good"); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Put the interface up\n"); - - err = tap_set_up(tap, &error_preup, &error_up); - if (error_preup) { - printf("preup output: %s\n", error_preup); - free(error_preup); - error_preup = NULL; - } - if (error_up) { - printf("up output: %s\n", error_up); - free(error_up); - error_up = NULL; - } - if (err < 0) { - printf("Unable to set interface up\n"); - err = -1; - goto out_clean; - } - - printf("Put the interface down\n"); - - err = tap_set_down(tap, &error_down, &error_postdown); - if (error_down) { - printf("down output: %s\n", error_down); - free(error_down); - error_down = NULL; - } - if (error_postdown) { - printf("postdown output: %s\n", error_down); - free(error_down); - error_down = NULL; - } - if (err < 0) { - printf("Unable to put the interface down\n"); - err = -1; - goto out_clean; - } - - tap_close(tap); - - printf("Test ERROR conditions\n"); - - printf("Pass NULL to tap set_up\n"); - errno = 0; - if ((tap_set_up(NULL, &error_preup, &error_up) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_up sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to error_preup set_up\n"); - errno = 0; - if ((tap_set_up(tap, NULL, &error_up) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_up sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to error_up set_up\n"); - errno = 0; - if ((tap_set_up(tap, &error_preup, NULL) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_up sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to tap set_down\n"); - errno = 0; - if ((tap_set_down(NULL, &error_down, &error_postdown) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_down sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to error_down set_down\n"); - errno = 0; - if ((tap_set_down(tap, NULL, &error_postdown) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_down sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to error_postdown set_down\n"); - errno = 0; - if ((tap_set_down(tap, &error_down, NULL) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_down sanity checks\n"); - err = -1; - goto out_clean; - } - -out_clean: - - tap_close(tap); - - return err; -} - -static int check_knet_close_leak(void) -{ - char device_name[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap; - char *error_string = NULL; - - printf("Testing close leak (needs valgrind)\n"); - - memset(device_name, 0, size); - strncpy(device_name, "kronostest", size); - tap = tap_open(device_name, size, NULL); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Adding ip: 192.168.168.168/24\n"); - - err = tap_add_ip(tap, "192.168.168.168", "24", &error_string); - if (error_string) { - printf("add ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - printf("Adding ip: 192.168.169.169/24\n"); - - err = tap_add_ip(tap, "192.168.169.169", "24", &error_string); - if (error_string) { - printf("add ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - -out_clean: - - tap_close(tap); - - return err; -} - -static int check_knet_set_del_ip(void) -{ - char device_name[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap; - char *ip_list = NULL; - int ip_list_entries = 0, i, offset = 0; - char *error_string = NULL; - - printf("Testing interface add/remove ip\n"); - - memset(device_name, 0, size); - strncpy(device_name, "kronostest", size); - tap = tap_open(device_name, size, NULL); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Adding ip: 192.168.168.168/24\n"); - - err = tap_add_ip(tap, "192.168.168.168", "24", &error_string); - if (error_string) { - printf("add ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - printf("Adding ip: 192.168.169.169/24\n"); - - err = tap_add_ip(tap, "192.168.169.169", "24", &error_string); - if (error_string) { - printf("add ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - printf("Adding duplicate ip: 192.168.168.168/24\n"); - - err = tap_add_ip(tap, "192.168.168.168", "24", &error_string); - if (error_string) { - printf("add ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to find IP address in libtap db\n"); - err=-1; - goto out_clean; - } - - printf("Checking ip: 192.168.168.168/24\n"); - - err = _execute_shell("ip addr show dev kronostest | grep -q 192.168.168.168/24", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err) { - printf("Unable to verify IP address\n"); - err=-1; - goto out_clean; - } - - printf("Get ip list from libtap:\n"); - - if (tap_get_ips(tap, &ip_list, &ip_list_entries) < 0) { - printf("Not enough mem?\n"); - err=-1; - goto out_clean; - } - - if (ip_list_entries != 2) { - printf("Didn't get enough ip back from libtap?\n"); - err=-1; - goto out_clean; - } - - for (i = 1; i <= ip_list_entries; i++) { - printf("Found IP %s %s in libtap db\n", ip_list + offset, ip_list + offset + strlen(ip_list + offset) + 1); - offset = offset + strlen(ip_list) + 1; - offset = offset + strlen(ip_list + offset) + 1; - } - - free(ip_list); - - printf("Deleting ip: 192.168.168.168/24\n"); - - err = tap_del_ip(tap, "192.168.168.168", "24", &error_string); - if (error_string) { - printf("del ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to delete IP address\n"); - err=-1; - goto out_clean; - } - - printf("Deleting ip: 192.168.169.169/24\n"); - - err = tap_del_ip(tap, "192.168.169.169", "24", &error_string); - if (error_string) { - printf("del ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to delete IP address\n"); - err=-1; - goto out_clean; - } - - printf("Deleting again ip: 192.168.168.168/24\n"); - - err = tap_del_ip(tap, "192.168.168.168", "24", &error_string); - if (error_string) { - printf("del ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to delete IP address\n"); - err=-1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 192.168.168.168/24", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (!err) { - printf("Unable to verify IP address\n"); - err=-1; - goto out_clean; - } - - printf("Adding ip: 3ffe::1/64\n"); - - err = tap_add_ip(tap, "3ffe::1", "64", &error_string); - if (error_string) { - printf("add ipv6 output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::1/64", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err) { - printf("Unable to verify IP address\n"); - err=-1; - goto out_clean; - } - - printf("Deleting ip: 3ffe::1/64\n"); - - err = tap_del_ip(tap, "3ffe::1", "64", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err) { - printf("Unable to delete IP address\n"); - err=-1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::1/64", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (!err) { - printf("Unable to verify IP address\n"); - err=-1; - goto out_clean; - } - -out_clean: - - tap_close(tap); - - return err; -} - -int main(void) -{ - if (geteuid() != 0) { - printf("This test requires root privileges\n"); - exit(77); - } - - if (check_tap_open_close() < 0) - return -1; - - if (check_knet_multi_eth() < 0) - return -1; - - if (check_knet_mtu() < 0) - return -1; - - if (check_knet_mtu_ipv6() < 0) - return -1; - - if (check_knet_mac() < 0) - return -1; - - if (check_tap_execute_shell() < 0) - return -1; - - if (check_knet_up_down() < 0) - return -1; - - if (check_knet_set_del_ip() < 0) - return -1; - - if (check_knet_close_leak() < 0) - return -1; - - return 0; -} -#endif diff --git a/libtap/libtap.h b/libtap/libtap.h deleted file mode 100644 index 015ef9cb..00000000 --- a/libtap/libtap.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __LIBTAP_H__ -#define __LIBTAP_H__ - -#include - -typedef struct _iface *tap_t; - -tap_t tap_open(char *dev, size_t dev_size, const char *updownpath); -int tap_close(tap_t tap); - -tap_t tap_find(char *dev, size_t dev_size); - -int tap_get_fd(const tap_t tap); - -const char *tap_get_name(const tap_t tap); - -int tap_get_mtu(const tap_t tap); -int tap_set_mtu(tap_t tap, const int mtu); -int tap_reset_mtu(tap_t tap); - -int tap_get_mac(const tap_t tap, char **ether_addr); -int tap_set_mac(tap_t tap, const char *ether_addr); -int tap_reset_mac(tap_t tap); - -int tap_set_up(tap_t tap, char **error_preup, char **error_up); -int tap_set_down(tap_t tap, char **error_down, char **error_postdown); - -int tap_add_ip(tap_t tap, const char *ip_addr, const char *prefix, char **error_string); -int tap_del_ip(tap_t tap, const char *ip_addr, const char *prefix, char **error_string); -int tap_get_ips(const tap_t tap, char **ip_addr_list, int *entries); - -#endif diff --git a/libtap/libtap.pc.in b/libtap/libtap.pc.in deleted file mode 100644 index d5a4fa58..00000000 --- a/libtap/libtap.pc.in +++ /dev/null @@ -1,19 +0,0 @@ -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -prefix=@prefix@ -exec_prefix=${prefix} -libdir=@libdir@ -includedir=${prefix}/include - -Name: libtap -Version: @VERSION@ -Description: library to manage a pool of tap devices -Requires: -Libs: -L${libdir} -ltap -lpthread -Cflags: -I${includedir} diff --git a/libtap/libtap_exported_syms b/libtap/libtap_exported_syms deleted file mode 100644 index 77a3dbb7..00000000 --- a/libtap/libtap_exported_syms +++ /dev/null @@ -1,6 +0,0 @@ -Base { - global: - tap_*; - local: - *; -}; diff --git a/libtap/tap_updown_bad/down.d/kronostest b/libtap/tap_updown_bad/down.d/kronostest deleted file mode 100755 index 93c4dcac..00000000 --- a/libtap/tap_updown_bad/down.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 1 diff --git a/libtap/tap_updown_bad/post-down.d/kronostest b/libtap/tap_updown_bad/post-down.d/kronostest deleted file mode 100755 index 93c4dcac..00000000 --- a/libtap/tap_updown_bad/post-down.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 1 diff --git a/libtap/tap_updown_bad/pre-up.d/kronostest b/libtap/tap_updown_bad/pre-up.d/kronostest deleted file mode 100755 index 93c4dcac..00000000 --- a/libtap/tap_updown_bad/pre-up.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 1 diff --git a/libtap/tap_updown_bad/up.d/kronostest b/libtap/tap_updown_bad/up.d/kronostest deleted file mode 100755 index 93c4dcac..00000000 --- a/libtap/tap_updown_bad/up.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 1 diff --git a/libtap/tap_updown_good/down.d/kronostest b/libtap/tap_updown_good/down.d/kronostest deleted file mode 100755 index ad56f64f..00000000 --- a/libtap/tap_updown_good/down.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 0 diff --git a/libtap/tap_updown_good/post-down.d/kronostest b/libtap/tap_updown_good/post-down.d/kronostest deleted file mode 100755 index ad56f64f..00000000 --- a/libtap/tap_updown_good/post-down.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 0 diff --git a/libtap/tap_updown_good/pre-up.d/kronostest b/libtap/tap_updown_good/pre-up.d/kronostest deleted file mode 100755 index ad56f64f..00000000 --- a/libtap/tap_updown_good/pre-up.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 0 diff --git a/libtap/tap_updown_good/up.d/kronostest b/libtap/tap_updown_good/up.d/kronostest deleted file mode 100755 index ad56f64f..00000000 --- a/libtap/tap_updown_good/up.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 0 diff --git a/poc-code/Makefile.am b/poc-code/Makefile.am deleted file mode 100644 index 4b12510e..00000000 --- a/poc-code/Makefile.am +++ /dev/null @@ -1,13 +0,0 @@ -# -# Copyright (C) 2016 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -MAINTAINERCLEANFILES = Makefile.in - -include $(top_srcdir)/build-aux/check.mk - -SUBDIRS = access-list iov-hash diff --git a/poc-code/access-list/.gitignore b/poc-code/access-list/.gitignore deleted file mode 100644 index 955474ba..00000000 --- a/poc-code/access-list/.gitignore +++ /dev/null @@ -1 +0,0 @@ -test_ipcheck diff --git a/poc-code/access-list/Makefile.am b/poc-code/access-list/Makefile.am deleted file mode 100644 index 35f15473..00000000 --- a/poc-code/access-list/Makefile.am +++ /dev/null @@ -1,22 +0,0 @@ -# -# Copyright (C) 2016 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -MAINTAINERCLEANFILES = Makefile.in - -include $(top_srcdir)/build-aux/check.mk - -# override global LIBS that pulls in lots of craft we don't need here -LIBS = - -EXTRA_DIST = test_ipcheck.txt - -noinst_PROGRAMS = test_ipcheck - -noinst_HEADERS = ipcheck.h - -test_ipcheck_SOURCES = ipcheck.c test_ipcheck.c diff --git a/poc-code/access-list/ipcheck.c b/poc-code/access-list/ipcheck.c deleted file mode 100644 index b71ac5c1..00000000 --- a/poc-code/access-list/ipcheck.c +++ /dev/null @@ -1,210 +0,0 @@ -#include -#include -#include -#include -#include -#include "ipcheck.h" - -struct ip_match_entry { - ipcheck_type_t type; - ipcheck_acceptreject_t acceptreject; - struct sockaddr_storage addr1; /* Actual IP address, mask top or low IP */ - struct sockaddr_storage addr2; /* high IP address or address bitmask */ - struct ip_match_entry *next; -}; - - -/* Lists of things to match against. These are dummy structs to provide a quick list head */ -static struct ip_match_entry match_entry_head_v4; -static struct ip_match_entry match_entry_head_v6; - -/* - * IPv4 See if the address we have matches the current match entry - * - */ -static int ip_matches_v4(struct sockaddr_storage *checkip, struct ip_match_entry *match_entry) -{ - struct sockaddr_in *ip_to_check; - struct sockaddr_in *match1; - struct sockaddr_in *match2; - - ip_to_check = (struct sockaddr_in *)checkip; - match1 = (struct sockaddr_in *)&match_entry->addr1; - match2 = (struct sockaddr_in *)&match_entry->addr2; - - switch(match_entry->type) { - case IPCHECK_TYPE_ADDRESS: - if (ip_to_check->sin_addr.s_addr == match1->sin_addr.s_addr) - return 1; - break; - case IPCHECK_TYPE_MASK: - if ((ip_to_check->sin_addr.s_addr & match2->sin_addr.s_addr) == - match1->sin_addr.s_addr) - return 1; - break; - case IPCHECK_TYPE_RANGE: - if ((ntohl(ip_to_check->sin_addr.s_addr) >= ntohl(match1->sin_addr.s_addr)) && - (ntohl(ip_to_check->sin_addr.s_addr) <= ntohl(match2->sin_addr.s_addr))) - return 1; - break; - - } - return 0; -} - -/* Compare two IPv6 addresses */ -static int ip6addr_cmp(struct in6_addr *a, struct in6_addr *b) -{ - uint64_t a_high, a_low; - uint64_t b_high, b_low; - - /* Not sure why '&' doesn't work below, so I used '+' instead which is effectively - the same thing because the bottom 32bits are always zero and the value unsigned */ - a_high = ((uint64_t)htonl(a->s6_addr32[0]) << 32) + (uint64_t)htonl(a->s6_addr32[1]); - a_low = ((uint64_t)htonl(a->s6_addr32[2]) << 32) + (uint64_t)htonl(a->s6_addr32[3]); - - b_high = ((uint64_t)htonl(b->s6_addr32[0]) << 32) + (uint64_t)htonl(b->s6_addr32[1]); - b_low = ((uint64_t)htonl(b->s6_addr32[2]) << 32) + (uint64_t)htonl(b->s6_addr32[3]); - - if (a_high > b_high) - return 1; - if (a_high < b_high) - return -1; - - if (a_low > b_low) - return 1; - if (a_low < b_low) - return -1; - - return 0; -} - -/* - * IPv6 See if the address we have matches the current match entry - * - */ -static int ip_matches_v6(struct sockaddr_storage *checkip, struct ip_match_entry *match_entry) -{ - struct sockaddr_in6 *ip_to_check; - struct sockaddr_in6 *match1; - struct sockaddr_in6 *match2; - int i; - - ip_to_check = (struct sockaddr_in6 *)checkip; - match1 = (struct sockaddr_in6 *)&match_entry->addr1; - match2 = (struct sockaddr_in6 *)&match_entry->addr2; - - switch(match_entry->type) { - case IPCHECK_TYPE_ADDRESS: - if (!memcmp(ip_to_check->sin6_addr.s6_addr32, match1->sin6_addr.s6_addr32, sizeof(struct in6_addr))) - return 1; - break; - - case IPCHECK_TYPE_MASK: - /* - * Note that this little loop will quit early if there is a non-match so the - * comparison might look backwards compared to the IPv4 one - */ - for (i=sizeof(struct in6_addr)/4-1; i>=0; i--) { - if ((ip_to_check->sin6_addr.s6_addr32[i] & match2->sin6_addr.s6_addr32[i]) != - match1->sin6_addr.s6_addr32[i]) - return 0; - } - return 1; - case IPCHECK_TYPE_RANGE: - if ((ip6addr_cmp(&ip_to_check->sin6_addr, &match1->sin6_addr) >= 0) && - (ip6addr_cmp(&ip_to_check->sin6_addr, &match2->sin6_addr) <= 0)) - return 1; - break; - } - return 0; -} - - -/* - * YOU ARE HERE - */ -int ipcheck_validate(struct sockaddr_storage *checkip) -{ - struct ip_match_entry *match_entry; - int (*match_fn)(struct sockaddr_storage *checkip, struct ip_match_entry *match_entry); - - if (checkip->ss_family == AF_INET){ - match_entry = match_entry_head_v4.next; - match_fn = ip_matches_v4; - } else { - match_entry = match_entry_head_v6.next; - match_fn = ip_matches_v6; - } - while (match_entry) { - if (match_fn(checkip, match_entry)) { - if (match_entry->acceptreject == IPCHECK_ACCEPT) - return 1; - else - return 0; - } - match_entry = match_entry->next; - } - return 0; /* Default reject */ -} - -/* - * Routines to manuipulate the lists - */ - -void ipcheck_clear(void) -{ - struct ip_match_entry *match_entry; - struct ip_match_entry *next_match_entry; - - match_entry = match_entry_head_v4.next; - while (match_entry) { - next_match_entry = match_entry->next; - free(match_entry); - match_entry = next_match_entry; - } - - match_entry = match_entry_head_v6.next; - while (match_entry) { - next_match_entry = match_entry->next; - free(match_entry); - match_entry = next_match_entry; - } -} - -int ipcheck_addip(struct sockaddr_storage *ip1, struct sockaddr_storage *ip2, - ipcheck_type_t type, ipcheck_acceptreject_t acceptreject) -{ - struct ip_match_entry *match_entry; - struct ip_match_entry *new_match_entry; - - if (type == IPCHECK_TYPE_RANGE && - (ip1->ss_family != ip2->ss_family)) - return -1; - - if (ip1->ss_family == AF_INET){ - match_entry = &match_entry_head_v4; - } else { - match_entry = &match_entry_head_v6; - } - - - new_match_entry = malloc(sizeof(struct ip_match_entry)); - if (!new_match_entry) - return -1; - - memcpy(&new_match_entry->addr1, ip1, sizeof(struct sockaddr_storage)); - memcpy(&new_match_entry->addr2, ip2, sizeof(struct sockaddr_storage)); - new_match_entry->type = type; - new_match_entry->acceptreject = acceptreject; - new_match_entry->next = NULL; - - /* Find the end of the list */ - /* is this OK, or should we use a doubly-linked list or bulk-load API call? */ - while (match_entry->next) { - match_entry = match_entry->next; - } - match_entry->next = new_match_entry; - - return 0; -} diff --git a/poc-code/access-list/ipcheck.h b/poc-code/access-list/ipcheck.h deleted file mode 100644 index c7b3fee6..00000000 --- a/poc-code/access-list/ipcheck.h +++ /dev/null @@ -1,10 +0,0 @@ - - -typedef enum {IPCHECK_TYPE_ADDRESS, IPCHECK_TYPE_MASK, IPCHECK_TYPE_RANGE} ipcheck_type_t; -typedef enum {IPCHECK_ACCEPT, IPCHECK_REJECT} ipcheck_acceptreject_t; - -int ipcheck_validate(struct sockaddr_storage *checkip); - -void ipcheck_clear(void); -int ipcheck_addip(struct sockaddr_storage *ip1, struct sockaddr_storage *ip2, - ipcheck_type_t type, ipcheck_acceptreject_t acceptreject); diff --git a/poc-code/access-list/test_ipcheck.c b/poc-code/access-list/test_ipcheck.c deleted file mode 100644 index 851efebd..00000000 --- a/poc-code/access-list/test_ipcheck.c +++ /dev/null @@ -1,185 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include "ipcheck.h" - -/* This is a test program .. remember! */ -#define BUFLEN 1024 - -static int get_ipaddress(char *buf, struct sockaddr_storage *addr) -{ - struct addrinfo *info; - struct addrinfo hints; - int res; - - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; - - res = getaddrinfo(buf, NULL, &hints, &info); - if (!res) { - memcpy(addr, info->ai_addr, info->ai_addrlen); - free(info); - } - return res; -} - -static int read_address(char *buf, struct sockaddr_storage *addr) -{ - return get_ipaddress(buf, addr); -} - -static int read_mask(char *buf, struct sockaddr_storage *addr, struct sockaddr_storage *addr2) -{ - char tmpbuf[BUFLEN]; - char *slash; - int ret; - - slash = strchr(buf, '/'); - if (!slash) - return 1; - - strncpy(tmpbuf, buf, slash-buf); - tmpbuf[slash-buf] = '\0'; - - ret = get_ipaddress(tmpbuf, addr); - if (ret) - return ret; - - ret = get_ipaddress(slash+1, addr2); - if (ret) - return ret; - - return 0; -} - -static int read_range(char *buf, struct sockaddr_storage *addr1, struct sockaddr_storage *addr2) -{ - char tmpbuf[BUFLEN]; - char *hyphen; - int ret; - - hyphen = strchr(buf, '-'); - if (!hyphen) - return 1; - - strncpy(tmpbuf, buf, hyphen-buf); - tmpbuf[hyphen-buf] = '\0'; - - ret = get_ipaddress(tmpbuf, addr1); - if (ret) - return ret; - - ret = get_ipaddress(hyphen+1, addr2); - if (ret) - return ret; - - return 0; -} - - -static int load_file(void) -{ - FILE *filterfile; - char filebuf[BUFLEN]; - int line = 0; - int ret; - ipcheck_type_t type; - ipcheck_acceptreject_t acceptreject; - struct sockaddr_storage addr1; - struct sockaddr_storage addr2; - - ipcheck_clear(); - - filterfile = fopen("test_ipcheck.txt", "r"); - if (!filterfile) { - fprintf(stderr, "Cannot open test_ipcheck.txt\n"); - return 1; - } - - while (fgets(filebuf, sizeof(filebuf), filterfile)) { - filebuf[strlen(filebuf)-1] = '\0'; /* remove trailing LF */ - line++; - - /* - * First char is A (accept) or R (Reject) - */ - switch(filebuf[0] & 0x5F) { - case 'A': - acceptreject = IPCHECK_ACCEPT; - break; - case 'R': - acceptreject = IPCHECK_REJECT; - break; - default: - fprintf(stderr, "Unknown record type on line %d: %s\n", line, filebuf); - goto next_record; - } - - /* - * Second char is the filter type: - * A Address - * M Mask - * R Range - */ - switch(filebuf[1] & 0x5F) { - case 'A': - type = IPCHECK_TYPE_ADDRESS; - ret = read_address(filebuf+2, &addr1); - break; - case 'M': - type = IPCHECK_TYPE_MASK; - ret = read_mask(filebuf+2, &addr1, &addr2); - break; - case 'R': - type = IPCHECK_TYPE_RANGE; - ret = read_range(filebuf+2, &addr1, &addr2); - break; - default: - fprintf(stderr, "Unknown filter type on line %d: %s\n", line, filebuf); - goto next_record; - break; - } - if (ret) { - fprintf(stderr, "Failed to parse address on line %d: %s\n", line, filebuf); - } - else { - ipcheck_addip(&addr1, &addr2, type, acceptreject); - } - next_record: {} /* empty statement to mollify the compiler */ - } - fclose(filterfile); - - return 0; -} - -int main(int argc, char *argv[]) -{ - struct sockaddr_storage saddr; - int ret; - int i; - - if (load_file()) - return 1; - - for (i=1; i -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -MAINTAINERCLEANFILES = Makefile.in - -include $(top_srcdir)/build-aux/check.mk - -# override global LIBS that pulls in lots of craft we don't need here -LIBS = - -noinst_PROGRAMS = nss_hash - -nss_hash_SOURCES = main.c - -nss_hash_CFLAGS = $(nss_CFLAGS) - -nss_hash_LDFLAGS = $(nss_LIBS) diff --git a/poc-code/iov-hash/main.c b/poc-code/iov-hash/main.c deleted file mode 100644 index d2f51662..00000000 --- a/poc-code/iov-hash/main.c +++ /dev/null @@ -1,171 +0,0 @@ -/* Example code to illustrate DES enccryption/decryption using NSS. - * The example skips the details of obtaining the Key & IV to use, and - * just uses a hardcoded Key & IV. - * Note: IV is only needed if Cipher Blocking Chaining (CBC) mode of encryption - * is used - * - * The recommended approach is to store and transport WRAPPED (encrypted) - * DES Keys (IVs can be in the clear). However, it is a common (and dangerous) - * practice to use raw DES Keys. This example shows the use of a RAW key. - */ - - -#include -#include -#include -#include - -/* example Key & IV */ -unsigned char gKey[] = {0xe8, 0xa7, 0x7c, 0xe2, 0x05, 0x63, 0x6a, 0x31}; -unsigned char gIV[] = {0xe4, 0xbb, 0x3b, 0xd3, 0xc3, 0x71, 0x2e, 0x58}; - -int main(int argc, char **argv) -{ - CK_MECHANISM_TYPE hashMech; - PK11SlotInfo* slot = NULL; - PK11SymKey* SymKey = NULL; - SECItem SecParam; - PK11Context* HashContext = NULL; - SECItem keyItem; - SECStatus rv, rv1, rv2; - unsigned char buf1[1024], buf2[1024]; - char data[1024]; - int i; - unsigned int tmp2_outlen; - - /* Initialize NSS - * * If your application code has already initialized NSS, you can skip it - * * here. - * * This code uses the simplest of the Init functions, which does not - * * require a NSS database to exist - * */ - rv = NSS_NoDB_Init("."); - if (rv != SECSuccess) - { - fprintf(stderr, "NSS initialization failed (err %d)\n", - PR_GetError()); - goto out; - } - - /* choose mechanism: CKM_DES_CBC_PAD, CKM_DES3_ECB, CKM_DES3_CBC..... - * * Note that some mechanisms (*_PAD) imply the padding is handled for you - * * by NSS. If you choose something else, then data padding is the - * * application's responsibility - * */ - hashMech = CKM_SHA_1_HMAC; - slot = PK11_GetBestSlot(hashMech, NULL); - /* slot = PK11_GetInternalKeySlot(); is a simpler alternative but in - * * theory, it *may not* return the optimal slot for the operation. For - * * DES ops, Internal slot is typically the best slot - * */ - if (slot == NULL) - { - fprintf(stderr, "Unable to find security device (err %d)\n", - PR_GetError()); - goto out; - } - - /* NSS passes blobs around as SECItems. These contain a pointer to - * * data and a length. Turn the raw key into a SECItem. */ - keyItem.type = siBuffer; - keyItem.data = gKey; - keyItem.len = sizeof(gKey); - - /* Turn the raw key into a key object. We use PK11_OriginUnwrap - * * to indicate the key was unwrapped - which is what should be done - * * normally anyway - using raw keys isn't a good idea */ - SymKey = PK11_ImportSymKey(slot, hashMech, PK11_OriginUnwrap, CKA_SIGN, - &keyItem, NULL); - if (SymKey == NULL) - { - fprintf(stderr, "Failure to import key into NSS (err %d)\n", - PR_GetError()); - goto out; - } - - SecParam.type = siBuffer; - SecParam.data = 0; - SecParam.len = 0; - - /* sample data we'll hash */ - strcpy(data, "Hash me!"); - fprintf(stderr, "Clear Data: %s\n", data); - - /* ========================= START SECTION ============================= */ - /* If using the the same key and iv over and over, stuff before this */ - /* section and after this section needs to be done only ONCE */ - - /* Create cipher context */ - HashContext = PK11_CreateContextBySymKey(hashMech, CKA_SIGN, - SymKey, &SecParam); - - if (!HashContext) { - fprintf(stderr, "no hash context today?\n"); - goto out; - } - - if (PK11_DigestBegin(HashContext) != SECSuccess) { - fprintf(stderr, "hash doesn't begin?\n"); - goto out; - } - - rv1 = PK11_DigestOp(HashContext, (unsigned char *)data, strlen(data)+1); - - rv2 = PK11_DigestFinal(HashContext, buf2, &tmp2_outlen, SHA1_BLOCK_LENGTH); - - PK11_DestroyContext(HashContext, PR_TRUE); - if (rv1 != SECSuccess || rv2 != SECSuccess) - goto out; - - fprintf(stderr, "Hash Data: "); - for (i=0; i