ruby-radix 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/radixlib.h ADDED
@@ -0,0 +1,161 @@
1
+ /*
2
+ * Copyright (c) 1999-2000
3
+ *
4
+ * The Regents of the University of Michigan ("The Regents") and
5
+ * Merit Network, Inc. All rights reserved. Redistribution and use
6
+ * in source and binary forms, with or without modification, are
7
+ * permitted provided that the following conditions are met:
8
+ *
9
+ * 1. Redistributions of source code must retain the above
10
+ * copyright notice, this list of conditions and the
11
+ * following disclaimer.
12
+ *
13
+ * 2. Redistributions in binary form must reproduce the above
14
+ * copyright notice, this list of conditions and the
15
+ * following disclaimer in the documentation and/or other
16
+ * materials provided with the distribution.
17
+ *
18
+ * 3. All advertising materials mentioning features or use of
19
+ * this software must display the following acknowledgement:
20
+ *
21
+ * This product includes software developed by the University of
22
+ * Michigan, Merit Network, Inc., and their contributors.
23
+ *
24
+ * 4. Neither the name of the University, Merit Network, nor the
25
+ * names of their contributors may be used to endorse or
26
+ * promote products derived from this software without
27
+ * specific prior written permission.
28
+ *
29
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS"
30
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
32
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL TH E REGENTS
33
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
36
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HO WEVER CAUSED
37
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
39
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40
+ * POSSIBILITY OF SUCH DAMAGE.
41
+ */
42
+ /*
43
+ * Portions Copyright (c) 2004,2005 Damien Miller <djm@mindrot.org>
44
+ *
45
+ * Permission to use, copy, modify, and distribute this software for any
46
+ * purpose with or without fee is hereby granted, provided that the above
47
+ * copyright notice and this permission notice appear in all copies.
48
+ *
49
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
50
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
51
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
52
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
53
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
54
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
55
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
56
+ */
57
+
58
+ /* $Id: radix.h,v 1.9 2007/10/24 06:03:08 djm Exp $ */
59
+
60
+ #ifndef _RADIX_H
61
+ #define _RADIX_H
62
+
63
+ #if defined(_MSC_VER)
64
+ #include <winsock2.h>
65
+ #include <ws2tcpip.h>
66
+ #else
67
+ # include <sys/types.h>
68
+ # include <sys/socket.h>
69
+ # include <netinet/in.h>
70
+ # include <arpa/inet.h>
71
+ # include <netdb.h>
72
+ #endif
73
+
74
+ #if defined(_MSC_VER)
75
+ # define snprintf _snprintf
76
+ typedef unsigned __int8 u_int8_t;
77
+ typedef unsigned __int16 u_int16_t;
78
+ typedef unsigned __int32 u_int32_t;
79
+ const char *inet_ntop(int af, const void *src, char *dst, size_t size);
80
+ size_t strlcpy(char *dst, const char *src, size_t size);
81
+ #endif
82
+
83
+ /*
84
+ * Originally from MRT include/mrt.h
85
+ * $MRTId: mrt.h,v 1.1.1.1 2000/08/14 18:46:10 labovit Exp $
86
+ */
87
+ typedef struct _prefix_t {
88
+ u_int family; /* AF_INET | AF_INET6 */
89
+ u_int bitlen; /* same as mask? */
90
+ int ref_count; /* reference count */
91
+ union {
92
+ struct in_addr sin;
93
+ struct in6_addr sin6;
94
+ } add;
95
+ } prefix_t;
96
+
97
+ void Deref_Prefix(prefix_t *prefix);
98
+
99
+ /*
100
+ * Originally from MRT include/radix.h
101
+ * $MRTId: radix.h,v 1.1.1.1 2000/08/14 18:46:10 labovit Exp $
102
+ */
103
+ typedef struct _radix_node_t {
104
+ u_int bit; /* flag if this node used */
105
+ prefix_t *prefix; /* who we are in radix tree */
106
+ struct _radix_node_t *l, *r; /* left and right children */
107
+ struct _radix_node_t *parent; /* may be used */
108
+ void *data; /* pointer to data */
109
+ } radix_node_t;
110
+
111
+ typedef struct _radix_tree_t {
112
+ radix_node_t *head;
113
+ u_int maxbits; /* for IP, 32 bit addresses */
114
+ int num_active_node; /* for debug purpose */
115
+ } radix_tree_t;
116
+
117
+ /* Type of callback function */
118
+ typedef void (*rdx_cb_t)(radix_node_t *, void *);
119
+
120
+ radix_tree_t *New_Radix(void);
121
+ void Destroy_Radix(radix_tree_t *radix, rdx_cb_t func, void *cbctx);
122
+ radix_node_t *radix_lookup(radix_tree_t *radix, prefix_t *prefix);
123
+ void radix_remove(radix_tree_t *radix, radix_node_t *node);
124
+ radix_node_t *radix_search_exact(radix_tree_t *radix, prefix_t *prefix);
125
+ radix_node_t *radix_search_best(radix_tree_t *radix, prefix_t *prefix);
126
+ void radix_process(radix_tree_t *radix, rdx_cb_t func, void *cbctx);
127
+
128
+ #define RADIX_MAXBITS 128
129
+
130
+ #define RADIX_WALK(Xhead, Xnode) \
131
+ do { \
132
+ radix_node_t *Xstack[RADIX_MAXBITS+1]; \
133
+ radix_node_t **Xsp = Xstack; \
134
+ radix_node_t *Xrn = (Xhead); \
135
+ while ((Xnode = Xrn)) { \
136
+ if (Xnode->prefix)
137
+
138
+ #define RADIX_WALK_END \
139
+ if (Xrn->l) { \
140
+ if (Xrn->r) { \
141
+ *Xsp++ = Xrn->r; \
142
+ } \
143
+ Xrn = Xrn->l; \
144
+ } else if (Xrn->r) { \
145
+ Xrn = Xrn->r; \
146
+ } else if (Xsp != Xstack) { \
147
+ Xrn = *(--Xsp); \
148
+ } else { \
149
+ Xrn = (radix_node_t *) 0; \
150
+ } \
151
+ } \
152
+ } while (0)
153
+
154
+ /* Local additions */
155
+
156
+ prefix_t *prefix_pton(const char *string, long len, const char **errmsg);
157
+ prefix_t *prefix_from_blob(u_char *blob, int len, int prefixlen);
158
+ const char *prefix_addr_ntop(prefix_t *prefix, char *buf, size_t len);
159
+ const char *prefix_ntop(prefix_t *prefix, char *buf, size_t len);
160
+
161
+ #endif /* _RADIX_H */
data/lib/ruby-radix.rb ADDED
@@ -0,0 +1,6 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module RubyRadix
5
+ VERSION = '0.0.2'
6
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/ruby-radix.rb'}"
9
+ puts "Loading ruby-radix gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/ruby-radix'
@@ -0,0 +1,127 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ require 'radix'
4
+
5
+ def int2ipaddr(val)
6
+ rstr = ""
7
+ rstr += sprintf("%d.", (val & 0xff000000) >> 24)
8
+ rstr += sprintf("%d.", (val & 0xff0000) >> 16)
9
+ rstr += sprintf("%d.", (val & 0xff00) >> 8)
10
+ rstr += sprintf("%d", val & 0xff)
11
+ rstr
12
+ end
13
+
14
+ class TestRubyRadix < Test::Unit::TestCase
15
+
16
+ def setup
17
+ end
18
+
19
+ def test_add
20
+ r = Radix.new
21
+ r.add("192.168.0.0", 24)
22
+ assert r.class == Radix
23
+ end
24
+
25
+ def test_store
26
+ r = Radix.new
27
+ r["172.31.0.0"] = "world!"
28
+ assert r["172.31.0.0/30"].msg == "world!"
29
+ end
30
+
31
+ def test_add_objects
32
+ r = Radix.new
33
+ r["192.168.0.0/24"] = ["Hello", "Radix", "Tree"]
34
+ r["172.31.0.0/30"] = {"Hello" => 123, "Radix" => "abc", :Tree => "def"}
35
+ assert r["192.168.0.0/24"].msg.class == Array
36
+ assert r["172.31.0.0/30"].msg.class == Hash
37
+ assert r["192.168.0.0/24"].msg[2] == "Tree"
38
+ assert r["172.31.0.0/30"].msg[:Tree] = "def"
39
+ end
40
+
41
+ def test_search_best
42
+ r = Radix.new
43
+ r["10.0.0.0/8"] = "message 1"
44
+ r["4.3.2.0/24"] = "message 2"
45
+ assert r.search_best("10.0.0.1").msg == "message 1"
46
+ assert r.search_best("11.0.0.1") == nil
47
+ assert r.search_best("4.3.2.0/24").msg == "message 2"
48
+ end
49
+
50
+ def test_search_exact
51
+ r = Radix.new
52
+ r["10.0.0.0/8"] = "message 1"
53
+ r["172.31.1.224/29"] = "message 2"
54
+ assert r.search_exact("10.0.0.0/8").msg == "message 1"
55
+ assert r.search_exact("10.0.0.0/24") == nil
56
+ assert r.search_exact("172.31.1.224/29").msg == "message 2"
57
+ end
58
+
59
+
60
+ def test_search_prefix
61
+ r = Radix.new
62
+ r["10.0.0.0/8"] = "next hop of 10.0.0.0/24 is 2.2.2.2"
63
+ node = r.search_best("10.0.1.2/32")
64
+ assert_match(/^next hop of 10\.0\.0\.0\/24 is 2\.2\.2\.2$/, node.msg)
65
+ end
66
+
67
+ def test_values
68
+ r = Radix.new
69
+ r["10.0.0.0/8"] = "message 1"
70
+ r["4.3.2.0/24"] = "message 2"
71
+ ret0 = r.values[0]
72
+ ret = ret0 + r.values[1]
73
+ assert ret.include? "message 1"
74
+ assert ret.include? "message 2"
75
+ end
76
+
77
+ def test_keys
78
+ r = Radix.new
79
+ r["10.0.0.0/8"] = "message 1"
80
+ r["4.3.2.0/24"] = "message 2"
81
+ ret1 = r.keys[0]
82
+ ret2 = r.keys[1]
83
+ ret = ret1 + " " + ret2
84
+ assert ret.include? "10.0.0.0/8"
85
+ assert ret.include? "4.3.2.0/24"
86
+ end
87
+
88
+ def test_length
89
+ r = Radix.new
90
+ r["10.0.0.0/8"] = "message 1"
91
+ r["4.3.2.0/24"] = "message 2"
92
+ assert r.length == 2
93
+ end
94
+
95
+ def test_clear
96
+ for i in 1..100
97
+ r = Radix.new
98
+ r[int2ipaddr(i)] = int2ipaddr(i)
99
+ r.clear
100
+ assert r.length == 0
101
+ end
102
+ end
103
+
104
+ def test_add10000
105
+ r = Radix.new
106
+ for i in 1..1000
107
+ r[int2ipaddr(i)] = int2ipaddr(i)
108
+ end
109
+ assert r.length == 1000
110
+ assert r[int2ipaddr(1000)].msg == int2ipaddr(1000)
111
+ end
112
+
113
+ def test_eachpair
114
+ r = Radix.new
115
+ r["10.0.0.0/8"] = "message 1"
116
+ r["4.3.2.0/24"] = "message 2"
117
+ r.each_pair do |k, v|
118
+ if (k == "1.2.3.4/32")
119
+ assert v == "message 1"
120
+ end
121
+ if (k == "4.3.2.1/32")
122
+ assert v == "message 2"
123
+ end
124
+ end
125
+ end
126
+
127
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-radix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Takashi Sogabe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.10'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.10'
30
+ - !ruby/object:Gem::Dependency
31
+ name: newgem
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.5.3
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.5.3
46
+ - !ruby/object:Gem::Dependency
47
+ name: hoe
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ description: ! 'ruby-radix is an implementation of a radix tree data structure for
63
+ the storage
64
+
65
+ and retrieval of IPv4 and IPv6 network prefixes.
66
+
67
+
68
+ The radix tree is the data structure most commonly used for routing table
69
+
70
+ lookups. It efficiently stores network prefixes of varying lengths and
71
+
72
+ allows fast lookups of containing networks.'
73
+ email:
74
+ - sogabe@iij.ad.jp
75
+ executables: []
76
+ extensions:
77
+ - ext/extconf.rb
78
+ extra_rdoc_files:
79
+ - History.txt
80
+ - Manifest.txt
81
+ - PostInstall.txt
82
+ - README.rdoc
83
+ files:
84
+ - History.txt
85
+ - Manifest.txt
86
+ - PostInstall.txt
87
+ - README.rdoc
88
+ - Rakefile
89
+ - lib/ruby-radix.rb
90
+ - script/console
91
+ - script/destroy
92
+ - script/generate
93
+ - test/test_helper.rb
94
+ - test/test_ruby-radix.rb
95
+ - ext/radix.c
96
+ - ext/radixlib.c
97
+ - ext/radixlib.h
98
+ - ext/extconf.rb
99
+ - .gemtest
100
+ homepage: http://github.com/iij/ruby-radix
101
+ licenses: []
102
+ post_install_message: PostInstall.txt
103
+ rdoc_options:
104
+ - --main
105
+ - README.rdoc
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project: ruby-radix
122
+ rubygems_version: 1.8.24
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: ruby-radix is an implementation of a radix tree data structure for the storage
126
+ and retrieval of IPv4 and IPv6 network prefixes
127
+ test_files:
128
+ - test/test_ruby-radix.rb
129
+ - test/test_helper.rb