pax_global_header00006660000000000000000000000064122717763560014532gustar00rootroot0000000000000052 comment=0d6d0a543d10e2e695b85855dbd2b15389ead70a rpatricia-1.0.1/000077500000000000000000000000001227177635600135075ustar00rootroot00000000000000rpatricia-1.0.1/COPYING000066400000000000000000000015311227177635600145420ustar00rootroot00000000000000Copyright (C) 2007-2014 Tatsuya Mori Copyright (C) 2010-2014 Eric Wong rpatricia 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. rpatricia 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 rpatricia. If not, see ext/rpatricia/patricia.[ch] files retain their 2-clause BSD license, see ext/rpatricia/copyright for details. rpatricia-1.0.1/Changes000066400000000000000000000031571227177635600150100ustar00rootroot000000000000001.0.1 2014/01/28 - Update license to LGPLv2.1+ - missing CHAR_BIT macro for some platforms 1.0 2012/10/05 - Add Patricia#nodes methods to return all nodes as a Hash 0.09 2012/07/18 - fix bounds error leading to "invalid prefix" exceptions 0.08 2011/08/12 - IPv6 support: Patricia.new(:AF_INET6) - minor internal cleanups and speedups 0.07 2010/09/23 - fix segfault from unpredictable GC ordering when node objects outlive the trees they come from 0.06 2010/09/14 - rely on Ruby GC for allocations/free no need to explicitly destroy objects anymore - Ruby 1.9.2 C API compatibility - allow creation of subclasses of Patricia class - non-String objects may be stored as node-data - Patricia::Node objects are returned when match succeeds It's no longer possible to call tree methods on node objects and cause segfaults. - Patricia#show_nodes may be given a custom IO-like object - sync docs with remove, match_best and match_exact behavior - ArgumentError is raised for invalid addresses no more assertion failures for bad addresses - Patricia#show_nodes and Patricia#num_nodes no longer segfaults on empty trees 0.05 2010/03/12 - reorganized directory layout no chance of accidentally having 'extconf' in the require path 0.04 2008/2/19 - fixed the bug of "clear" method (destroy) node data were not cleared correctly - fixed the memory leak in search methods I had to clear the prefix instances 0.03 2008/2/17 - fixed the bug of allocating user_data strings 0.02 2008/1/31 - fixed the bug of printing prefix for 64-bit architecture from Aki Nakao 0.01 2007/11/16 - the first version rpatricia-1.0.1/LGPL-2.1000066400000000000000000000636421227177635600145010ustar00rootroot00000000000000 GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 Copyright (C) 1991, 1999 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. [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 Street, 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! rpatricia-1.0.1/README000066400000000000000000000127501227177635600143740ustar00rootroot00000000000000README for Ruby wrapper of Net::Patricia --------------------------------------- rPatricia - A ruby wrapper of Net::Patricia DESCRIPTION ----------- This is a ruby wrapper of Net::Patricia, which has been developed by Dave Plonka. The original Net::Patricia and its C API are available from: http://net.doit.wisc.edu/~plonka/Net-Patricia/ Net::Patricia is a module for fast IP address/prefix lookups. I have modified some interfaces for the Ruby wrapper version. NO WARANTY ---------- THE PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY. IT IS PROVIDED "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. INSTALLATION ------------ This package extracts, builds, installs in the usual fashion, i.e.: $ tar xvfz rpatricia.tar.gz $ cd rpatricia $ ruby extconf.rb $ make $ ruby test.rb # make install SYNOPSIS -------- require 'rpatricia' pt = Patricia.new pt.add("192.168.1.0/24") pt.add("127.0.0.0/8", "user_data") node = pt.search_best("127.0.0.1") puts node.data puts node.prefix puts node.network puts node.prefixlen pt.remove("127.0.0.0/8") puts pt.num_nodes pt.show_nodes pt.clear METHODS ------- new: pt = Patricia.new This is the class' constructor - it returns a Patricia object for handling IPv4 addresses. To handle IPv6 addresses (only), Patricia.new may be called with an additional argument: pt = Patricia.new(:AF_INET6) The Patricia object will be destroyed automatically when there are no longer any references to it. add: pt.add(key_string[,user_data]) The first argument, key_string, is a network or subnet specification in canonical form, e.g. ``10.0.0.0/8'', where the number after the slash represents the number of bits in the netmask. If no mask width is specified, the longest possible mask is assumed, i.e. 32 bits for AF_INET addresses and 128 bits for AF_INET6 addresses. The second argument, user_data, is optional. If supplied, it should be a STRING object specifying the user data that will be stored in the Patricia Trie node. Subsequently, this value will be returned by the match methods described below to indicate a successful search. If no second argument is passed, the key_string will be stored as the user data and therfore will likewise be returned by the match functions. On success, this method returns the object of the Patricia Trie node. family: Returns either :AF_INET or :AF_INET6 symbol depending on how the object was initialized. A Patricia object may only handle IPv4 or IPv6 addresses. add_node: An alias of add. search_best: pt.search_best(key_string); This method searches the Patricia Trie to find a matching node, according to normal subnetting rules for the address and mask specified. The key_string argument is a network or subnet specification in canonical form, e.g. ``10.0.0.0/8'', where the number after the slash represents the number of bits in the netmask. If no mask width value is specified, the longest mask is assumed, i.e. 32 bits for AF_INET addresses and 128 bits for AF_INET6 addresses. If a matching node is found in the Patricia Trie, this method returns the object of the node. This method returns nil on failure. match_best: An alias of search_best. search_exact: pt.search_exact(key_string); This method searches the Patricia Trie to find a matching node. Its semantics are exactly the same as those described for search_best except that the key must match a node exactly. I.e., it is not sufficient that the address and mask specified merely falls within the subnet specified by a particular node. match_exact: An alias of search_exact. include?: pt.include?(key_string); This method behaves like match_best, but returns true on success and false on failure. This method is more efficient than match_best as it does not allocate a new object. remove: pt.remove(key_string); This method removes the node which exactly matches the the address and mask specified from the Patricia Trie. If the matching node is found in the Patricia Trie, it is removed, and this method returns the true. This method returns false on failure. remove_node: An alias of remove num_nodes: pt.num_nodes This method returns the number of nodes in the Patricia Trie. show_nodes: pt.print_nodes This method prints all the nodes in the Patricia Trie. nodes: pt.nodes This method returns all the nodes and values in the Patricia Trie as a Ruby Hash object. data: node.data This method returns the user data of the Patricia Trie node. network: node.network This method returns the network of the Patricia Trie node. prefix: node.prefix This method returns the prefix of the Patricia Trie node. prefixlen: node.prefixlen This method returns the prefix length of the Patricia Trie node. AUTHORS ------- Tatsuya Mori Eric Wong rpatricia-1.0.1/TODO000066400000000000000000000000551227177635600141770ustar00rootroot000000000000001. figure out bugs 2. extension for AF_INET6 rpatricia-1.0.1/ext/000077500000000000000000000000001227177635600143075ustar00rootroot00000000000000rpatricia-1.0.1/ext/rpatricia/000077500000000000000000000000001227177635600162655ustar00rootroot00000000000000rpatricia-1.0.1/ext/rpatricia/copyright000066400000000000000000000023451227177635600202240ustar00rootroot00000000000000$Id: COPYRIGHT,v 1.1.1.1 2013/08/15 18:46:09 labovit Exp $ Copyright (c) 1999-2013 The Regents of the University of Michigan ("The Regents") and Merit Network, Inc. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.rpatricia-1.0.1/ext/rpatricia/credits.txt000066400000000000000000000054351227177635600204720ustar00rootroot00000000000000 [newtool.gif] MRT Credits The Multi-Threaded Routing Toolkit _________________________________________________________________ MRT was developed by [1]Merit Network, Inc., under National Science Foundation grant NCR-9318902, "Experimentation with Routing Technology to be Used for Inter-Domain Routing in the Internet." Current MRT Staff * [2]Craig Labovitz * [3]Makaki Hirabaru * [4]Farnam Jahanian * Susan Hares * Susan R. Harris * Nathan Binkert * Gerald Winters Project Alumni * [5]Marc Unangst * John Scudder The BGP4+ extension was originally written by Francis Dupont . The public domain Struct C-library of linked list, hash table and memory allocation routines was developed by Jonathan Dekock . Susan Rebecca Harris provided help with the documentation. David Ward provided bug fixes and helpful suggestions. Some sections of code and architecture ideas were taken from the GateD routing daemon. The first port to Linux with IPv6 was done by Pedro Roque . Some interface routines to the Linux kernel were originally written by him. Alexey Kuznetsov made enhancements to 1.4.3a and fixed the Linux kernel intarface. Linux's netlink interface was written, referring to his code "iproute2". We would also like to thank our other colleagues in Japan, Portugal, the Netherlands, the UK, and the US for their many contributions to the MRT development effort. _________________________________________________________________ Cisco is a registered trademark of Cisco Systems Inc. _________________________________________________________________ Merit Network 4251 Plymouth Road Suite C Ann Arbor, MI 48105-2785 734-764-9430 info@merit.edu _________________________________________________________________ © 1999 Merit Network, Inc. [6]www@merit.edu References 1. http://www.merit.edu/ 2. http://www.merit.edu/~labovit 3. http://www.merit.edu/~masaki 4. http://www.eecs.umich.edu/~farnam 5. http://www.contrib.andrew.cmu.edu/~mju/ 6. mailto:www@merit.edu rpatricia-1.0.1/ext/rpatricia/extconf.rb000077500000000000000000000002021227177635600202550ustar00rootroot00000000000000#!/usr/bin/env ruby require "mkmf" have_func("ruby_xcalloc") have_func("rb_str_set_len", "ruby.h") create_makefile("rpatricia") rpatricia-1.0.1/ext/rpatricia/patricia.c000066400000000000000000000361121227177635600202300ustar00rootroot00000000000000/* * $Id: patricia.c,v 1.7 2005/12/07 20:46:41 dplonka Exp $ * Dave Plonka * * This product includes software developed by the University of Michigan, * Merit Network, Inc., and their contributors. * * This file had been called "radix.c" in the MRT sources. * * I renamed it to "patricia.c" since it's not an implementation of a general * radix trie. Also I pulled in various requirements from "prefix.c" and * "demo.c" so that it could be used as a standalone API. */ static char copyright[] = "This product includes software developed by the University of Michigan, Merit" "Network, Inc., and their contributors."; #include /* assert */ #include /* isdigit */ #include /* errno */ #include /* CHAR_BIT */ #include /* sin */ #include /* NULL */ #include /* sprintf, fprintf, stderr */ #include /* free, atol, calloc */ #include /* memcpy, strchr, strlen */ #include /* BSD: for inet_addr */ #include /* BSD, Linux: for inet_addr */ #include /* BSD, Linux: for inet_addr */ #include /* BSD, Linux, Solaris: for inet_addr */ #include "patricia.h" #if defined(HAVE_RUBY_XCALLOC) # include # define calloc xcalloc # define Delete xfree #else # define Delete free #endif /* { from prefix.c */ /* prefix_tochar * convert prefix information to bytes */ u_char * prefix_tochar (prefix_t * prefix) { if (prefix == NULL) return (NULL); return ((u_char *) & prefix->add.sin); } int comp_with_mask (void *addr, void *dest, u_int mask) { if ( /* mask/8 == 0 || */ memcmp (addr, dest, mask / 8) == 0) { int n = mask / 8; int m = ((-1) << (8 - (mask % 8))); if (mask % 8 == 0 || (((u_char *)addr)[n] & m) == (((u_char *)dest)[n] & m)) return (1); } return (0); } /* * convert prefix information to ascii string with length * thread safe and re-entrant implementation * * buff must be at least PATRICIA_MAXSTRLEN bytes large if with_len is true * buff must be at least INET6_ADDRSTRLEN bytes large if with_len is false */ char * prefix_toa2x (prefix_t *prefix, char *buff, int with_len) { const char *dst; assert(prefix && "NULL prefix not allowed"); assert(prefix->ref_count >= 0); assert(buff != NULL && "buffer must be specified"); switch (prefix->family) { case AF_INET: assert(prefix->bitlen <= 32 && "bitlen > 32 for AF_INET"); break; case AF_INET6: assert(prefix->bitlen <= 128 && "bitlen > 128 for AF_INET6"); break; default: assert(0 && "unknown address family (memory corruption?)"); } dst = inet_ntop(prefix->family, &prefix->add.sin6, buff, INET6_ADDRSTRLEN); assert(dst && "corrupt address"); if (with_len) sprintf(buff + strlen(buff), "/%u", prefix->bitlen); return buff; } /* prefix_toa2 * convert prefix information to ascii string */ char * prefix_toa2 (prefix_t *prefix, char *buff) { return (prefix_toa2x (prefix, buff, 0)); } prefix_t * New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix) { prefix_t *orig_prefix = prefix; int default_bitlen; size_t size, addr_size; switch (family) { case AF_INET6: addr_size = sizeof(struct in6_addr); size = sizeof(prefix_t); break; case AF_INET: addr_size = sizeof(struct in_addr); size = sizeof(prefix4_t); break; default: return NULL; } default_bitlen = addr_size * CHAR_BIT; if (bitlen > default_bitlen) return NULL; if (!orig_prefix) prefix = calloc(1, size); prefix->bitlen = bitlen >= 0 ? bitlen : default_bitlen; prefix->family = family; prefix->ref_count = orig_prefix ? 0 : 1; memcpy(&prefix->add.sin6, dest, addr_size); return prefix; } prefix_t * New_Prefix (int family, void *dest, int bitlen) { return (New_Prefix2 (family, dest, bitlen, NULL)); } /* ascii2prefix */ prefix_t * ascii2prefix(char *string, prefix_t *prefix) { long bitlen; size_t maxbitlen; void *dest; char *slash, *end; int result; char save[INET6_ADDRSTRLEN]; size_t len; int family; union { struct in6_addr sin6; struct in_addr sin; } addr; assert(string && "string is NULL"); len = strlen(string); slash = memchr(string, '/', len); if (slash) { bitlen = strtol(slash + 1, &end, 10); len = slash - string; if (*end || (bitlen < 0) || len >= sizeof(save)) return NULL; /* copy the string to save. Avoid destroying the string */ memcpy(save, string, len); save[len] = '\0'; string = save; } else { bitlen = -1; } family = memchr(string, ':', len) ? AF_INET6 : AF_INET; result = inet_pton(family, string, &addr); if (result != 1) return NULL; return New_Prefix2(family, &addr, bitlen, prefix); } static prefix_t * Ref_Prefix (prefix_t * prefix) { if (prefix == NULL) return (NULL); if (prefix->ref_count == 0) { /* make a copy in case of a static prefix */ return (New_Prefix2 (prefix->family, &prefix->add, prefix->bitlen, NULL)); } prefix->ref_count++; return (prefix); } static void Deref_Prefix (prefix_t * prefix) { if (prefix == NULL) return; /* for secure programming, raise an assert. no static prefix can call this */ assert (prefix->ref_count > 0); prefix->ref_count--; assert (prefix->ref_count >= 0); if (prefix->ref_count <= 0) { Delete (prefix); return; } } /* } */ static int num_active_patricia = 0; /* these routines support continuous mask only */ patricia_tree_t * New_Patricia (int maxbits) { patricia_tree_t *patricia = calloc(1, sizeof *patricia); patricia->maxbits = maxbits; patricia->head = NULL; patricia->num_active_node = 0; assert (maxbits <= PATRICIA_MAXBITS); /* XXX */ num_active_patricia++; return (patricia); } /* * if func is supplied, it will be called as func(node->data) * before deleting the node */ void Clear_Patricia (patricia_tree_t *patricia, void_fn_t func) { assert (patricia); if (patricia->head) { patricia_node_t *Xstack[PATRICIA_MAXBITS+1]; patricia_node_t **Xsp = Xstack; patricia_node_t *Xrn = patricia->head; while (Xrn) { patricia_node_t *l = Xrn->l; patricia_node_t *r = Xrn->r; if (Xrn->prefix) { Deref_Prefix (Xrn->prefix); if (Xrn->data && func) func (Xrn->data); } else { assert (Xrn->data == NULL); } Delete (Xrn); patricia->num_active_node--; if (l) { if (r) { *Xsp++ = r; } Xrn = l; } else if (r) { Xrn = r; } else if (Xsp != Xstack) { Xrn = *(--Xsp); } else { Xrn = (patricia_node_t *) 0; } } } assert (patricia->num_active_node == 0); /* Delete (patricia); */ } void Destroy_Patricia (patricia_tree_t *patricia, void_fn_t func) { Clear_Patricia (patricia, func); Delete (patricia); num_active_patricia--; } /* * if func is supplied, it will be called as func(node->prefix, node->data) */ void patricia_process (patricia_tree_t *patricia, void_fn_t func) { patricia_node_t *node; assert (func); PATRICIA_WALK (patricia->head, node) { func (node->prefix, node->data); } PATRICIA_WALK_END; } size_t patricia_walk_inorder(patricia_node_t *node, void_fn_t func) { size_t n = 0; assert(func); if (node->l) { n += patricia_walk_inorder(node->l, func); } if (node->prefix) { func(node->prefix, node->data); n++; } if (node->r) { n += patricia_walk_inorder(node->r, func); } return n; } patricia_node_t * patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix) { patricia_node_t *node; u_char *addr; u_int bitlen; assert (patricia); assert (prefix); assert (prefix->bitlen <= patricia->maxbits); if (patricia->head == NULL) return (NULL); node = patricia->head; addr = prefix_touchar (prefix); bitlen = prefix->bitlen; while (node->bit < bitlen) { if (BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) { node = node->r; } else { node = node->l; } if (node == NULL) return (NULL); } if (node->bit > bitlen || node->prefix == NULL) return (NULL); assert (node->bit == bitlen); assert (node->bit == node->prefix->bitlen); if (comp_with_mask (prefix_tochar (node->prefix), prefix_tochar (prefix), bitlen)) { return (node); } return (NULL); } /* if inclusive != 0, "best" may be the given prefix itself */ patricia_node_t * patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, int inclusive) { patricia_node_t *node; patricia_node_t *stack[PATRICIA_MAXBITS + 1]; u_char *addr; u_int bitlen; int cnt = 0; assert (patricia); assert (prefix); assert (prefix->bitlen <= patricia->maxbits); if (patricia->head == NULL) return (NULL); node = patricia->head; addr = prefix_touchar (prefix); bitlen = prefix->bitlen; while (node->bit < bitlen) { if (node->prefix) { stack[cnt++] = node; } if (BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) { node = node->r; } else { node = node->l; } if (node == NULL) break; } if (inclusive && node && node->prefix) stack[cnt++] = node; if (cnt <= 0) return (NULL); while (--cnt >= 0) { node = stack[cnt]; if (comp_with_mask (prefix_tochar (node->prefix), prefix_tochar (prefix), node->prefix->bitlen)) { return (node); } } return (NULL); } patricia_node_t * patricia_search_best (patricia_tree_t *patricia, prefix_t *prefix) { return (patricia_search_best2 (patricia, prefix, 1)); } patricia_node_t * patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix) { patricia_node_t *node, *new_node, *parent, *glue; u_char *addr, *test_addr; u_int bitlen, check_bit, differ_bit; int i, j, r; assert (patricia); assert (prefix); assert (prefix->bitlen <= patricia->maxbits); if (patricia->head == NULL) { node = calloc(1, sizeof *node); node->bit = prefix->bitlen; node->prefix = Ref_Prefix (prefix); node->parent = NULL; node->l = node->r = NULL; node->data = NULL; patricia->head = node; patricia->num_active_node++; return (node); } addr = prefix_touchar (prefix); bitlen = prefix->bitlen; node = patricia->head; while (node->bit < bitlen || node->prefix == NULL) { if (node->bit < patricia->maxbits && BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) { if (node->r == NULL) break; node = node->r; } else { if (node->l == NULL) break; node = node->l; } assert (node); } assert (node->prefix); test_addr = prefix_touchar (node->prefix); /* find the first bit different */ check_bit = (node->bit < bitlen)? node->bit: bitlen; differ_bit = 0; for (i = 0; i*8 < (int)check_bit; i++) { if ((r = (addr[i] ^ test_addr[i])) == 0) { differ_bit = (i + 1) * 8; continue; } /* I know the better way, but for now */ for (j = 0; j < 8; j++) { if (BIT_TEST (r, (0x80 >> j))) break; } /* must be found */ assert (j < 8); differ_bit = i * 8 + j; break; } if (differ_bit > check_bit) differ_bit = check_bit; parent = node->parent; while (parent && parent->bit >= differ_bit) { node = parent; parent = node->parent; } if (differ_bit == bitlen && node->bit == bitlen) { if (node->prefix) { return (node); } node->prefix = Ref_Prefix (prefix); assert (node->data == NULL); return (node); } new_node = calloc(1, sizeof *new_node); new_node->bit = prefix->bitlen; new_node->prefix = Ref_Prefix (prefix); new_node->parent = NULL; new_node->l = new_node->r = NULL; new_node->data = NULL; patricia->num_active_node++; if (node->bit == differ_bit) { new_node->parent = node; if (node->bit < patricia->maxbits && BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) { assert (node->r == NULL); node->r = new_node; } else { assert (node->l == NULL); node->l = new_node; } return (new_node); } if (bitlen == differ_bit) { if (bitlen < patricia->maxbits && BIT_TEST (test_addr[bitlen >> 3], 0x80 >> (bitlen & 0x07))) { new_node->r = node; } else { new_node->l = node; } new_node->parent = node->parent; if (node->parent == NULL) { assert (patricia->head == node); patricia->head = new_node; } else if (node->parent->r == node) { node->parent->r = new_node; } else { node->parent->l = new_node; } node->parent = new_node; } else { glue = calloc(1, sizeof *glue); glue->bit = differ_bit; glue->prefix = NULL; glue->parent = node->parent; glue->data = NULL; patricia->num_active_node++; if (differ_bit < patricia->maxbits && BIT_TEST (addr[differ_bit >> 3], 0x80 >> (differ_bit & 0x07))) { glue->r = new_node; glue->l = node; } else { glue->r = node; glue->l = new_node; } new_node->parent = glue; if (node->parent == NULL) { assert (patricia->head == node); patricia->head = glue; } else if (node->parent->r == node) { node->parent->r = glue; } else { node->parent->l = glue; } node->parent = glue; } return (new_node); } void patricia_remove (patricia_tree_t *patricia, patricia_node_t *node) { patricia_node_t *parent, *child; assert (patricia); assert (node); if (node->r && node->l) { /* this might be a placeholder node -- have to check and make sure * there is a prefix aossciated with it ! */ if (node->prefix != NULL) Deref_Prefix (node->prefix); node->prefix = NULL; /* Also I needed to clear data pointer -- masaki */ node->data = NULL; return; } if (node->r == NULL && node->l == NULL) { parent = node->parent; Deref_Prefix (node->prefix); Delete (node); patricia->num_active_node--; if (parent == NULL) { assert (patricia->head == node); patricia->head = NULL; return; } if (parent->r == node) { parent->r = NULL; child = parent->l; } else { assert (parent->l == node); parent->l = NULL; child = parent->r; } if (parent->prefix) return; /* we need to remove parent too */ if (parent->parent == NULL) { assert (patricia->head == parent); patricia->head = child; } else if (parent->parent->r == parent) { parent->parent->r = child; } else { assert (parent->parent->l == parent); parent->parent->l = child; } child->parent = parent->parent; Delete (parent); patricia->num_active_node--; return; } if (node->r) { child = node->r; } else { assert (node->l); child = node->l; } parent = node->parent; child->parent = parent; Deref_Prefix (node->prefix); Delete (node); patricia->num_active_node--; if (parent == NULL) { assert (patricia->head == node); patricia->head = child; return; } if (parent->r == node) { parent->r = child; } else { assert (parent->l == node); parent->l = child; } } rpatricia-1.0.1/ext/rpatricia/patricia.h000066400000000000000000000102751227177635600202370ustar00rootroot00000000000000/* * $Id: patricia.h,v 1.6 2005/12/07 20:53:01 dplonka Exp $ * Dave Plonka * * This product includes software developed by the University of Michigan, * Merit Network, Inc., and their contributors. * * This file had been called "radix.h" in the MRT sources. * * I renamed it to "patricia.h" since it's not an implementation of a general * radix trie. Also, pulled in various requirements from "mrt.h" and added * some other things it could be used as a standalone API. */ #ifndef _PATRICIA_H #define _PATRICIA_H /* typedef unsigned int u_int; */ typedef void (*void_fn_t)(); /* { from defs.h */ #define prefix_touchar(prefix) ((u_char *)&(prefix)->add.sin) #define MAXLINE 1024 #define BIT_TEST(f, b) ((f) & (b)) /* } */ #include /* for u_* definitions (on FreeBSD 5) */ #include /* for EAFNOSUPPORT */ #ifndef EAFNOSUPPORT # defined EAFNOSUPPORT WSAEAFNOSUPPORT # include #else # include /* for struct in_addr */ #endif #include /* for AF_INET */ /* { from mrt.h */ typedef struct _prefix4_t { u_short family; /* AF_INET | AF_INET6 */ u_short bitlen; /* same as mask? */ int ref_count; /* reference count */ struct in_addr sin; } prefix4_t; typedef struct _prefix_t { u_short family; /* AF_INET | AF_INET6 */ u_short bitlen; /* same as mask? */ int ref_count; /* reference count */ union { struct in_addr sin; struct in6_addr sin6; } add; } prefix_t; /* } */ typedef struct _patricia_node_t { u_int bit; /* flag if this node used */ prefix_t *prefix; /* who we are in patricia tree */ struct _patricia_node_t *l, *r; /* left and right children */ struct _patricia_node_t *parent;/* may be used */ void *data; /* pointer to data */ void *user; /* pointer to usr data (ex. route flap info) */ } patricia_node_t; typedef struct _patricia_tree_t { patricia_node_t *head; u_int maxbits; /* for IP, 32 bit addresses */ int num_active_node; /* for debug purpose */ } patricia_tree_t; patricia_node_t *patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix); patricia_node_t *patricia_search_best (patricia_tree_t *patricia, prefix_t *prefix); patricia_node_t * patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, int inclusive); patricia_node_t *patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix); void patricia_remove (patricia_tree_t *patricia, patricia_node_t *node); patricia_tree_t *New_Patricia (int maxbits); void Clear_Patricia (patricia_tree_t *patricia, void_fn_t func); void Destroy_Patricia (patricia_tree_t *patricia, void_fn_t func); void patricia_process (patricia_tree_t *patricia, void_fn_t func); /* { from demo.c */ prefix_t * ascii2prefix (char *string, prefix_t *prefix); /* } */ #define PATRICIA_MAXBITS 128 #define PATRICIA_MAXSTRLEN (INET6_ADDRSTRLEN + sizeof("/128") - 1) #define PATRICIA_NBIT(x) (0x80 >> ((x) & 0x7f)) #define PATRICIA_NBYTE(x) ((x) >> 3) #define PATRICIA_DATA_GET(node, type) (type *)((node)->data) #define PATRICIA_DATA_SET(node, value) ((node)->data = (void *)(value)) #define PATRICIA_WALK(Xhead, Xnode) \ do { \ patricia_node_t *Xstack[PATRICIA_MAXBITS+1]; \ patricia_node_t **Xsp = Xstack; \ patricia_node_t *Xrn = (Xhead); \ while ((Xnode = Xrn)) { \ if (Xnode->prefix) #define PATRICIA_WALK_ALL(Xhead, Xnode) \ do { \ patricia_node_t *Xstack[PATRICIA_MAXBITS+1]; \ patricia_node_t **Xsp = Xstack; \ patricia_node_t *Xrn = (Xhead); \ while ((Xnode = Xrn)) { \ if (1) #define PATRICIA_WALK_BREAK { \ if (Xsp != Xstack) { \ Xrn = *(--Xsp); \ } else { \ Xrn = (patricia_node_t *) 0; \ } \ continue; } #define PATRICIA_WALK_END \ if (Xrn->l) { \ if (Xrn->r) { \ *Xsp++ = Xrn->r; \ } \ Xrn = Xrn->l; \ } else if (Xrn->r) { \ Xrn = Xrn->r; \ } else if (Xsp != Xstack) { \ Xrn = *(--Xsp); \ } else { \ Xrn = (patricia_node_t *) 0; \ } \ } \ } while (0) #endif /* _PATRICIA_H */ rpatricia-1.0.1/ext/rpatricia/rpatricia.c000066400000000000000000000232231227177635600204110ustar00rootroot00000000000000/* * rpatricia.c - Ruby wrapper of Net::Patricia * Tatsuya Mori */ #include "ruby.h" #include #include "patricia.h" #include size_t patricia_walk_inorder(patricia_node_t *node, void_fn_t func); char * prefix_toa2x(prefix_t *prefix, char *buff, int with_len); char * prefix_toa2(prefix_t *prefix, char *buff); static VALUE cPatricia, cNode; static VALUE sym_AF_INET, sym_AF_INET6; static void dummy(void) {} static VALUE p_destroy (VALUE self) { patricia_tree_t *tree; Data_Get_Struct(self, patricia_tree_t, tree); Clear_Patricia(tree, dummy); tree->head = NULL; /* Clear_Patricia() should do this, actually */ return Qtrue; } static void p_node_mark (void *ptr) { patricia_node_t *node = ptr; rb_gc_mark((VALUE)node->data); } static VALUE wrap_node(patricia_node_t *orig) { patricia_node_t *node = ALLOC(patricia_node_t); memcpy(node, orig, sizeof(patricia_node_t)); return Data_Wrap_Struct(cNode, p_node_mark, -1, node); } static void my_ascii2prefix(patricia_tree_t *tree, VALUE str, prefix_t *prefix) { char *cstr = StringValueCStr(str); prefix_t *ok = ascii2prefix(cstr, prefix); if (!ok) rb_raise(rb_eArgError, "invalid prefix: %s", cstr); if (prefix->bitlen > tree->maxbits) rb_raise(rb_eArgError, "prefix length (%u) larger than maxbits (%u)", prefix->bitlen, tree->maxbits); } static VALUE p_add (int argc, VALUE *argv, VALUE self) { VALUE user_data; patricia_tree_t *tree; patricia_node_t *node; prefix_t prefix; if (argc > 2 || argc < 1) return Qnil; Data_Get_Struct(self, patricia_tree_t, tree); my_ascii2prefix(tree, argv[0], &prefix); node = patricia_lookup(tree, &prefix); if (argc == 2) { user_data = argv[1]; /* for backwards compatibility, we always dup and return new strings */ if (TYPE(user_data) == T_STRING) user_data = rb_str_dup(user_data); } else { user_data = rb_str_new(NULL, 0); } PATRICIA_DATA_SET(node, user_data); /* node will be freed when parent is freed */ return wrap_node(node); } static VALUE p_remove (VALUE self, VALUE r_key) { patricia_tree_t *tree; patricia_node_t *node; prefix_t prefix; Data_Get_Struct(self, patricia_tree_t, tree); my_ascii2prefix(tree, r_key, &prefix); node = patricia_search_exact(tree, &prefix); if (node) { patricia_remove (tree, node); return Qtrue; } else { return Qfalse; } } static VALUE p_match (VALUE self, VALUE r_key) { patricia_tree_t *tree; patricia_node_t *node; prefix_t prefix; Data_Get_Struct(self, patricia_tree_t, tree); my_ascii2prefix(tree, r_key, &prefix); node = patricia_search_best(tree, &prefix); return node ? wrap_node(node) : Qnil; } static VALUE p_include (VALUE self, VALUE r_key) { patricia_tree_t *tree; patricia_node_t *node; prefix_t prefix; Data_Get_Struct(self, patricia_tree_t, tree); my_ascii2prefix(tree, r_key, &prefix); node = patricia_search_best(tree, &prefix); return node ? Qtrue : Qfalse; } static VALUE p_match_exact (VALUE self, VALUE r_key) { patricia_tree_t *tree; patricia_node_t *node; prefix_t prefix; Data_Get_Struct(self, patricia_tree_t, tree); my_ascii2prefix(tree, r_key, &prefix); node = patricia_search_exact(tree, &prefix); return node ? wrap_node(node) : Qnil; } static VALUE p_num_nodes (VALUE self) { int n; patricia_tree_t *tree; Data_Get_Struct(self, patricia_tree_t, tree); n = tree->head ? patricia_walk_inorder(tree->head, dummy) : 0; return INT2NUM(n); } /* needed for Ruby 1.8.6, in 1.8.7 and later */ #ifndef HAVE_RB_STR_SET_LEN static void rb_str_set_len(VALUE str, long len) { RSTRING(str)->len = len; RSTRING(str)->ptr[len] = '\0'; } #endif static VALUE p_print_nodes (int argc, VALUE *argv, VALUE self) { ID id_printf = rb_intern("printf"); VALUE fmt = rb_str_new2("node: %s\n"); VALUE buf = rb_str_new(0, 0); char *cbuf; patricia_tree_t *tree; patricia_node_t *node; VALUE out; Data_Get_Struct(self, patricia_tree_t, tree); rb_scan_args(argc, argv, "01", &out); if (NIL_P(out)) out = rb_stdout; if (tree->head) { PATRICIA_WALK(tree->head, node) { rb_str_resize(buf, PATRICIA_MAXSTRLEN); cbuf = RSTRING_PTR(buf); prefix_toa2x(node->prefix, cbuf, 1); rb_str_set_len(buf, strlen(cbuf)); rb_funcall(out, id_printf, 2, fmt, buf); } PATRICIA_WALK_END; } return Qtrue; } static VALUE p_nodes (VALUE self) { VALUE buf = rb_str_new(0, 0); char *cbuf; patricia_tree_t *tree; patricia_node_t *node; VALUE hash; Data_Get_Struct(self, patricia_tree_t, tree); hash = rb_hash_new(); if (tree->head) { PATRICIA_WALK(tree->head, node) { rb_str_resize(buf, PATRICIA_MAXSTRLEN); cbuf = RSTRING_PTR(buf); prefix_toa2x(node->prefix, cbuf, 1); rb_str_set_len(buf, strlen(cbuf)); rb_hash_aset(hash, buf, (VALUE)node->data); } PATRICIA_WALK_END; } return hash; } static VALUE p_data (VALUE self) { VALUE user_data; patricia_node_t *node; Data_Get_Struct(self, patricia_node_t, node); user_data = (VALUE)node->data; /* for backwards compatibility, we always dup and return new strings */ if (TYPE(user_data) == T_STRING) user_data = rb_str_dup(user_data); return user_data; } static VALUE p_network (VALUE self) { patricia_node_t *node; VALUE str = rb_str_new(0, PATRICIA_MAXSTRLEN); char *cstr = RSTRING_PTR(str); Data_Get_Struct(self, patricia_node_t, node); prefix_toa2x(node->prefix, cstr, 0); rb_str_set_len(str, strlen(cstr)); return str; } static VALUE p_prefix (VALUE self) { patricia_node_t *node; VALUE str = rb_str_new(0, INET6_ADDRSTRLEN); char *cstr = RSTRING_PTR(str); Data_Get_Struct(self, patricia_node_t, node); prefix_toa2(node->prefix, cstr); rb_str_set_len(str, strlen(cstr)); return str; } static VALUE p_prefixlen (VALUE self) { patricia_node_t *node; Data_Get_Struct(self, patricia_node_t, node); return INT2NUM(node->prefix->bitlen); } /* called during GC for each node->data attached to a Patricia tree */ static void p_tree_mark_each(prefix_t *prefix, void *data) { VALUE user_data = (VALUE)data; rb_gc_mark(user_data); } static void p_tree_mark (void *ptr) { patricia_tree_t *tree = ptr; if (tree) patricia_process(tree, p_tree_mark_each); } static void p_tree_free (void *ptr) { patricia_tree_t *tree = ptr; /* no need to explicitly free each node->data, GC will do it for us */ if (tree) Destroy_Patricia(tree, NULL); } static VALUE p_alloc(VALUE klass) { return Data_Wrap_Struct(klass, p_tree_mark, p_tree_free, NULL); } static VALUE p_init(int argc, VALUE *argv, VALUE self) { VALUE family; int maxbits; rb_scan_args(argc, argv, "01", &family); if (NIL_P(family) || family == sym_AF_INET) maxbits = 32; else if (family == sym_AF_INET6) maxbits = 128; else rb_raise(rb_eArgError, "unknown family (must be :AF_INET6 or :AF_INET)"); DATA_PTR(self) = New_Patricia(maxbits); return self; } static VALUE p_init_copy(VALUE self, VALUE orig) { patricia_tree_t *orig_tree; Data_Get_Struct(orig, patricia_tree_t, orig_tree); if (orig_tree->head) { patricia_tree_t *tree; patricia_node_t *orig_node, *node; VALUE user_data; DATA_PTR(self) = tree = New_Patricia(orig_tree->maxbits); PATRICIA_WALK(orig_tree->head, orig_node) { node = patricia_lookup(tree, orig_node->prefix); assert(node->prefix == orig_node->prefix); user_data = (VALUE)(orig_node->data); if (T_STRING == TYPE(user_data)) user_data = rb_str_dup(user_data); PATRICIA_DATA_SET(node, user_data); } PATRICIA_WALK_END; } return self; } static VALUE p_family(VALUE self) { patricia_tree_t *tree; Data_Get_Struct(self, patricia_tree_t, tree); switch (tree->maxbits) { case 32: return sym_AF_INET; case 128: return sym_AF_INET6; } assert(0 && "unknown maxbits, corrupt tree"); return Qnil; } void Init_rpatricia (void) { cPatricia = rb_define_class("Patricia", rb_cObject); cNode = rb_define_class_under(cPatricia, "Node", rb_cObject); sym_AF_INET = ID2SYM(rb_intern("AF_INET")); sym_AF_INET6 = ID2SYM(rb_intern("AF_INET6")); /* allocate new Patricia object, called before initialize */ rb_define_alloc_func(cPatricia, p_alloc); rb_define_private_method(cPatricia, "initialize", p_init, -1); rb_define_method(cPatricia, "initialize_copy", p_init_copy, 1); /*---------- methods to tree ----------*/ /* add string */ rb_define_method(cPatricia, "add", p_add, -1); rb_define_method(cPatricia, "add_node", p_add, -1); rb_define_method(cPatricia, "family", p_family, 0); /* match prefix */ rb_define_method(cPatricia, "match_best", p_match, 1); rb_define_method(cPatricia, "search_best", p_match, 1); /* exact match */ rb_define_method(cPatricia, "match_exact", p_match_exact, 1); rb_define_method(cPatricia, "search_exact", p_match_exact, 1); /* check existence */ rb_define_method(cPatricia, "include?", p_include, 1); /* removal */ rb_define_method(cPatricia, "remove", p_remove, 1); rb_define_method(cPatricia, "remove_node", p_remove, 1); /* derivatives of climb */ rb_define_method(cPatricia, "num_nodes", p_num_nodes, 0); rb_define_method(cPatricia, "show_nodes", p_print_nodes, -1); rb_define_method(cPatricia, "nodes", p_nodes, 0); /* destroy tree */ rb_define_method(cPatricia, "destroy", p_destroy, 0); rb_define_method(cPatricia, "clear", p_destroy, 0); /*---------- methods to node ----------*/ rb_define_method(cNode, "data", p_data, 0); rb_define_method(cNode, "show_data", p_data, 0); rb_define_method(cNode, "network", p_network, 0); rb_define_method(cNode, "prefix", p_prefix, 0); rb_define_method(cNode, "prefixlen", p_prefixlen, 0); } rpatricia-1.0.1/rpatricia.gemspec000066400000000000000000000023021227177635600170270ustar00rootroot00000000000000# to build a gem: # gem build rpatricia.gemspec Gem::Specification.new do |s| s.name = %q{rpatricia} s.version = %q{1.0.1} # remember to update Changes if this is changed s.homepage = "http://www.goto.info.waseda.ac.jp/~tatsuya/rpatricia/" # Tatsuya wrote the extension, Eric gemified this s.authors = [ "Tatsuya Mori", "Eric Wong" ] s.date = Time.now.utc.strftime('%Y-%m-%d') s.summary = %q{module for fast IP address/prefix lookups} s.description = %q{ This is a ruby wrapper of Net::Patricia, which has been developed by Dave Plonka. The original Net::Patricia and its C API are available from: http://net.doit.wisc.edu/~plonka/Net-Patricia/ Net::Patricia is a module for fast IP address/prefix lookups. I have modified some interfaces for the Ruby wrapper version. }.strip # bother Eric for all gem issues s.email = %q{normalperson@yhbt.net} # generated using "git ls-files" from Eric's git repo s.files = %w( Changes README TODO COPYING LGPL-2.1 ext/rpatricia/credits.txt ext/rpatricia/extconf.rb ext/rpatricia/patricia.c ext/rpatricia/patricia.h ext/rpatricia/rpatricia.c rpatricia.gemspec test.rb ) s.extensions = %w(ext/rpatricia/extconf.rb) s.licenses = %w(LGPLv2.1+) end rpatricia-1.0.1/test.rb000066400000000000000000000035331227177635600150170ustar00rootroot00000000000000require 'rpatricia' def my_test condition puts (condition)? "ok!" : "error!" end puts "test: creating Patricia object" my_test(t = Patricia.new) puts "test: adding 127.0.0.0/24" my_test(n = t.add("127.0.0.0/24")) puts " data of added node = #{n.data}" puts " prefix of added node = #{n.prefix}" puts " network of added node = #{n.network}" puts " prefixlen of added node = #{n.prefixlen}" puts "adding 192.168.1.0/24, 192.168.2.0/24, 192.168.3.100" t.add("192.168.1.0/24") t.add("192.168.2.0/24") t.add("192.168.3.100") puts "test: match_best 127.0.0.1" my_test(n = t.match_best("127.0.0.1")) puts "test: match_exact 192.168.3.100" my_test(n = t.match_best("192.168.3.100")) puts " data of matched node = #{n.data}" puts " prefix of matched node = #{n.prefix}" puts " network of matched node = #{n.network}" puts " prefixlen of matched node = #{n.prefixlen}" puts "test: adding prefix 10.0.0.0/8 with user data of 'pref_10.0.0.0/8'" my_test(n = t.add("10.0.0.0/8", "pref_10")) puts " data of added node = #{n.data}" puts " prefix of added node = #{n.prefix}" puts " network of added node = #{n.network}" puts " prefixlen of added node = #{n.prefixlen}" puts "test: match string 10.0.0.1" my_test(n = t.match_best("10.0.0.1")) puts " data of matched node = #{n.data}" puts " prefix of matched node = #{n.prefix}" puts " network of matched node = #{n.network}" puts " prefixlen of matched node = #{n.prefixlen}" puts "test: remove '42.0.0.0/8'; should return nil" my_test(!t.remove("42.0.0.0/8")) puts "test: remove '10.0.0.0/8'" my_test(n= t.remove("10.0.0.0/8")) puts "test: number of nodes in the tree should be '4'" my_test(4 == t.num_nodes) puts "test: showing all nodes" t.show_nodes puts "test: return all nodes in Hash" my_test(t.nodes == {"127.0.0.0/24"=>"", "192.168.1.0/24"=>"", "192.168.2.0/24"=>"", "192.168.3.100/32"=>""}) t.destroy rpatricia-1.0.1/test/000077500000000000000000000000001227177635600144665ustar00rootroot00000000000000rpatricia-1.0.1/test/test_duplicate.rb000066400000000000000000000024261227177635600200300ustar00rootroot00000000000000require 'test/unit' require 'rpatricia' require 'stringio' class TestDuplicate < Test::Unit::TestCase def test_dup tmp = {} t = Patricia.new t.add('127.0.0.0/8', tmp) t2 = t.dup assert_equal :AF_INET, t2.family assert_equal 1, t2.num_nodes assert_equal tmp.object_id, t2.match_best('127.0.0.1').data.object_id t2.add('10.0.0.0/8', zz = []) assert_equal 2, t2.num_nodes assert_equal 1, t.num_nodes tio = StringIO.new t.show_nodes(tio) assert_equal "node: 127.0.0.0/8\n", tio.string t2io = StringIO.new t2.show_nodes(t2io) assert_equal("node: 10.0.0.0/8\nnode: 127.0.0.0/8\n", t2io.string) end def test_dup_ipv6 tmp = {} t = Patricia.new :AF_INET6 t.add('1234:4321::/32', tmp) t.add('2600:0102:a100::/43', tmp) t2 = t.dup assert_equal :AF_INET6, t2.family assert_equal 2, t2.num_nodes t2.add('::1/128', zz = []) assert_equal 3, t2.num_nodes assert_equal 2, t.num_nodes tio = StringIO.new t.show_nodes(tio) expect = "node: 1234:4321::/32\nnode: 2600:102:a100::/43\n" assert_equal expect, tio.string t2io = StringIO.new t2.show_nodes(t2io) expect = "node: ::1/128\nnode: 1234:4321::/32\nnode: 2600:102:a100::/43\n" assert_equal expect, t2io.string end end rpatricia-1.0.1/test/test_gc.rb000066400000000000000000000030201227177635600164360ustar00rootroot00000000000000require 'test/unit' require 'rpatricia' # this takes a while, watch memory usage not grow in top(1) or similar class TestGc < Test::Unit::TestCase def setup @arrays = Patricia.new @arrays.add('10.0.0.0/8', []) @arrays.add('127.0.0.0/24', []) @strings = Patricia.new @strings.add('10.0.0.0/8', "big lan") @strings.add('127.0.0.0/24', "localhost") end def test_clear 1_000_000.times do @strings.clear assert_equal 0, @strings.num_nodes @strings.add('10.0.0.0/8', "big lan") assert_equal 1, @strings.num_nodes @strings.add('127.0.0.0/8', "localhost") assert_equal 2, @strings.num_nodes @strings.add('192.168.1.0/24', "home") assert_equal 3, @strings.num_nodes end end def test_gc_dup 100000.times do tmp = @strings.dup tmp.remove '127.0.0.0/24' tmp = @arrays.dup tmp.remove '127.0.0.0/24' end assert_equal [], @arrays.match_best('127.0.0.1').data assert_equal "localhost", @strings.match_best('127.0.0.1').data end def test_gc assert_nothing_raised do 500_000.times do t = Patricia.new t.add('10.0.0.0/8', {}) t.add('127.0.0.0/24', "home sweet home") end end # ensure what we created originally didn't get GC-ed' 500_000.times do assert_equal [], @arrays.match_best('127.0.0.1').data assert_equal "localhost", @strings.match_best('127.0.0.1').data end end def test_destroy assert @strings.destroy assert @strings.destroy end end rpatricia-1.0.1/test/test_include.rb000066400000000000000000000010661227177635600175000ustar00rootroot00000000000000require 'test/unit' require 'rpatricia' class TestInclude < Test::Unit::TestCase def setup @t = Patricia.new end def test_include_exact @t.add '127.0.0.1' assert_equal true, @t.include?('127.0.0.1') assert_equal false, @t.include?('127.0.0.2') @t.clear assert_equal false, @t.include?('127.0.0.1') end def test_include_match_prefix @t.add '127.0.0.0/8' assert_equal true, @t.include?('127.0.0.32') assert_equal false, @t.include?('12.0.0.32') @t.clear assert_equal false, @t.include?('127.0.0.32') end end rpatricia-1.0.1/test/test_invalid_inputs.rb000066400000000000000000000013511227177635600211020ustar00rootroot00000000000000require 'test/unit' require 'rpatricia' class TestInvalidInput < Test::Unit::TestCase def setup @t = Patricia.new end def test_embedded_null assert_raises(ArgumentError) { @t.add("100.0.0.0/8\0") } end def test_invalid_add assert_raises(ArgumentError) { @t.add('1000.0.0.0/8') } assert_raises(ArgumentError) { @t.add("\0") } assert_raises(ArgumentError) { @t.add("\0\0\0\0\0\0" * 1024 * 1024) } end def test_invalid_remove assert_raises(ArgumentError) { @t.remove('1000.0.0.0/8') } end def test_invalid_match_best assert_raises(ArgumentError) { @t.match_best('1000.0.0.0/8') } end def test_invalid_match_exact assert_raises(ArgumentError) { @t.match_exact('1000.0.0.0/8') } end end rpatricia-1.0.1/test/test_ipv6.rb000066400000000000000000000012471227177635600167420ustar00rootroot00000000000000require 'test/unit' require 'rpatricia' class TestIPv6 < Test::Unit::TestCase def test_ipv6 t = Patricia.new :AF_INET6 assert_equal :AF_INET6, t.family node = t.add('1234:567::/35', 'hello world') assert_kind_of Patricia::Node, node assert_equal 'hello world', node.data assert_equal 'hello world', t.match_best('1234:567::/128').data end def test_ipv4_mapped_ipv6 t = Patricia.new :AF_INET6 assert_equal :AF_INET6, t.family node = t.add('::ffff:10.0.0.0/8', 'hello world') assert_kind_of Patricia::Node, node assert_equal 'hello world', node.data assert_equal 'hello world', t.match_best('::ffff:10.0.0.1').data end end rpatricia-1.0.1/test/test_match_data.rb000066400000000000000000000017751227177635600201510ustar00rootroot00000000000000require 'test/unit' require 'rpatricia' class TestMatchData < Test::Unit::TestCase def test_match_data_non_string ary = [] t = Patricia.new t.add('10.0.0.0/8', ary) z = t.match_best '10.1.1.1' assert_equal ary.object_id, z.data.object_id z.data << '10.1.1.1' t.match_best('10.2.2.2').data.include?('10.1.1.1') end # backwards compatibility, we always return a new string def test_match_data_string data = "hello" t = Patricia.new t.add('10.0.0.0/8', data) z = t.match_best '10.1.1.1' assert_equal "hello", z.data assert(data.object_id != z.data.object_id) y = t.match_best '10.1.1.1' assert_equal "hello", y.data assert(y.data.object_id != z.data.object_id) end def test_match_data_nothing t = Patricia.new t.add('10.0.0.0/8') z = t.match_best '10.1.1.1' assert_equal "", z.data z_object_id = z.data y = t.match_best '10.1.1.1' assert_equal "", y.data assert(y.data.object_id != z_object_id) end end rpatricia-1.0.1/test/test_nodes.rb000066400000000000000000000014041227177635600171610ustar00rootroot00000000000000require 'test/unit' require 'rpatricia' class TestShowNodes < Test::Unit::TestCase def test_nodes t = Patricia.new string = "pref_10" array = [:something] t.add("127.0.0.0/24") t.add("192.168.1.0/24") t.add("192.168.2.0/24") t.add("192.168.3.100") t.add("10.0.0.0/8", string) t.add("10.0.0.0/9", array) assert_equal({"10.0.0.0/8" => "pref_10", "10.0.0.0/9" => [:something], "127.0.0.0/24" => "", "192.168.1.0/24" => "", "192.168.2.0/24" => "", "192.168.3.100/32" => ""}, t.nodes) assert(string.object_id != t.nodes["10.0.0.0/8"].object_id) assert(array.object_id == t.nodes["10.0.0.0/9"].object_id) end end rpatricia-1.0.1/test/test_old_segfaults.rb000066400000000000000000000022151227177635600207050ustar00rootroot00000000000000require 'test/unit' require 'rpatricia' # things that used to segfault before the introduction of # Patricia::Node will now raise NoMethodError class TestOldSegfaults < Test::Unit::TestCase def test_used_to_segfault assert_raises(NoMethodError) { Patricia.new.data } end def test_node_method t = Patricia.new t.add('10.0.0.0/8', 'big_lan') if matched = t.match_best('10.1.2.3') assert_kind_of Patricia::Node, matched assert_equal 'big_lan', matched.data assert_raises(NoMethodError) { matched.destroy } end end def test_empty_show_nodes assert_nothing_raised do t = Patricia.new t.show_nodes end end def test_empty_num_nodes t = Patricia.new assert_equal 0, t.num_nodes end def test_v6_v4_mismatch_search_best t = Patricia.new assert_raises(ArgumentError) { t.search_exact("::1/128") } end def test_v6_v4_mismatch_search_exact t = Patricia.new assert_raises(ArgumentError) { t.search_exact("::1/128") } end def test_gc_ordering_segfault added = [] 100000.times do tmp = Patricia.new added << tmp.add('8.8.8.8') end end end rpatricia-1.0.1/test/test_show_nodes.rb000066400000000000000000000024041227177635600202220ustar00rootroot00000000000000require 'test/unit' require 'rpatricia' require 'stringio' class TestShowNodes < Test::Unit::TestCase def test_show_nodes t = Patricia.new assert_nothing_raised do t.add("127.0.0.0/24") t.add("192.168.1.0/24") t.add("192.168.2.0/24") t.add("192.168.3.100") t.add("10.0.0.0/8", "pref_10") end begin oldout = $stdout $stdout = tmpout = StringIO.new assert_nothing_raised { t.show_nodes } ensure $stdout = oldout end expect = [ "node: 10.0.0.0/8", "node: 127.0.0.0/24", "node: 192.168.1.0/24", "node: 192.168.2.0/24", "node: 192.168.3.100/32" ].join("\n") << "\n" assert_equal expect, tmpout.string end def test_show_nodes_specify_io t = Patricia.new assert_nothing_raised do t.add("127.0.0.0/24") t.add("192.168.1.0/24") t.add("192.168.2.0/24") t.add("192.168.3.100") t.add("10.0.0.0/8", "pref_10") end tmpout = StringIO.new assert_nothing_raised { t.show_nodes(tmpout) } expect = [ "node: 10.0.0.0/8", "node: 127.0.0.0/24", "node: 192.168.1.0/24", "node: 192.168.2.0/24", "node: 192.168.3.100/32" ].join("\n") << "\n" assert_equal expect, tmpout.string end end rpatricia-1.0.1/test/test_subclass.rb000066400000000000000000000003021227177635600176640ustar00rootroot00000000000000require 'test/unit' require 'rpatricia' class SubPatricia < Patricia end class TestSubclass < Test::Unit::TestCase def test_new assert_equal SubPatricia, SubPatricia.new.class end end