trema 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/ruby/trema/match.c +4 -3
- data/ruby/trema/packet-in.c +19 -18
- data/ruby/trema/stats-reply.c +1 -1
- data/ruby/trema/version.rb +1 -1
- data/spec/trema/flow-removed_spec.rb +3 -2
- data/spec/trema/packet-in_spec.rb +2 -2
- data/trema.gemspec +1 -0
- metadata +124 -84
- checksums.yaml +0 -7
- data/ruby/trema/ip.rb +0 -111
- data/spec/trema/ip_spec.rb +0 -54
data/ruby/trema/match.c
CHANGED
@@ -321,7 +321,8 @@ match_nw( VALUE self, uint8_t which ) {
|
|
321
321
|
masklen = ( match->wildcards & OFPFW_NW_DST_MASK ) >> OFPFW_NW_DST_SHIFT;
|
322
322
|
}
|
323
323
|
uint32_t prefixlen = masklen > 32 ? 0 : 32 - masklen;
|
324
|
-
|
324
|
+
VALUE ipv4_addr = rb_funcall( rb_eval_string( "Pio::IPv4Address" ), rb_intern( "new" ), 1, UINT2NUM( nw_addr ) );
|
325
|
+
return rb_funcall( ipv4_addr, rb_intern( "mask" ), 1, UINT2NUM( prefixlen ) );;
|
325
326
|
}
|
326
327
|
|
327
328
|
|
@@ -526,7 +527,7 @@ match_init( int argc, VALUE *argv, VALUE self ) {
|
|
526
527
|
|
527
528
|
VALUE nw_src = rb_hash_aref( options, ID2SYM( rb_intern( "nw_src" ) ) );
|
528
529
|
if ( nw_src != Qnil ) {
|
529
|
-
VALUE nw_addr = rb_funcall( rb_eval_string( "
|
530
|
+
VALUE nw_addr = rb_funcall( rb_eval_string( "Pio::IPv4Address" ), rb_intern( "new" ), 1, nw_src );
|
530
531
|
uint32_t prefixlen = ( uint32_t ) NUM2UINT( rb_funcall( nw_addr, rb_intern( "prefixlen" ), 0 ) );
|
531
532
|
if ( prefixlen > 0 ) {
|
532
533
|
match->nw_src = nw_addr_to_i( nw_addr );
|
@@ -537,7 +538,7 @@ match_init( int argc, VALUE *argv, VALUE self ) {
|
|
537
538
|
|
538
539
|
VALUE nw_dst = rb_hash_aref( options, ID2SYM( rb_intern( "nw_dst" ) ) );
|
539
540
|
if ( nw_dst != Qnil ) {
|
540
|
-
VALUE nw_addr = rb_funcall( rb_eval_string( "
|
541
|
+
VALUE nw_addr = rb_funcall( rb_eval_string( "Pio::IPv4Address" ), rb_intern( "new" ), 1, nw_dst );
|
541
542
|
uint32_t prefixlen = ( uint32_t ) NUM2UINT( rb_funcall( nw_addr, rb_intern( "prefixlen" ), 0 ) );
|
542
543
|
if ( prefixlen > 0 ) {
|
543
544
|
match->nw_dst = nw_addr_to_i( nw_addr );
|
data/ruby/trema/packet-in.c
CHANGED
@@ -35,7 +35,7 @@ VALUE cPacketIn;
|
|
35
35
|
#define PACKET_IN_RETURN_IP( packet_member ) \
|
36
36
|
{ \
|
37
37
|
VALUE ret = ULONG2NUM( get_packet_in_info( self )->packet_member ); \
|
38
|
-
return rb_funcall( rb_eval_string( "
|
38
|
+
return rb_funcall( rb_eval_string( "Pio::IPv4Address" ), rb_intern( "new" ), 1, ret ); \
|
39
39
|
}
|
40
40
|
|
41
41
|
#define PACKET_IN_RETURN_NUM( flag, func, packet_member ) \
|
@@ -391,8 +391,8 @@ packet_in_arp_sha( VALUE self ) {
|
|
391
391
|
/*
|
392
392
|
* The ARP source protocol address of a packet.
|
393
393
|
*
|
394
|
-
* @return [
|
395
|
-
* the value of ARP source protocol address as a
|
394
|
+
* @return [Pio::IPv4Address, nil]
|
395
|
+
* the value of ARP source protocol address as a Pio::IPv4Address object or nil if
|
396
396
|
* packet is not an ARP.
|
397
397
|
*/
|
398
398
|
static VALUE
|
@@ -427,8 +427,8 @@ packet_in_arp_tha( VALUE self ) {
|
|
427
427
|
/*
|
428
428
|
* The ARP target protocol address of a packet.
|
429
429
|
*
|
430
|
-
* @return [
|
431
|
-
* the value of ARP target protocol address as a
|
430
|
+
* @return [Pio::IPv4Address]
|
431
|
+
* the value of ARP target protocol address as a Pio::IPv4Address object or nil if
|
432
432
|
* packet is not an ARP.
|
433
433
|
*/
|
434
434
|
static VALUE
|
@@ -522,8 +522,8 @@ packet_in_rarp_sha( VALUE self ) {
|
|
522
522
|
/*
|
523
523
|
* The RARP source protocol address of a packet.
|
524
524
|
*
|
525
|
-
* @return [
|
526
|
-
* the value of RARP source protocol address as a
|
525
|
+
* @return [Pio::IPv4Address, nil]
|
526
|
+
* the value of RARP source protocol address as a Pio::IPv4Address object or nil if
|
527
527
|
* packet is not an RARP.
|
528
528
|
*/
|
529
529
|
static VALUE
|
@@ -558,8 +558,8 @@ packet_in_rarp_tha( VALUE self ) {
|
|
558
558
|
/*
|
559
559
|
* The ARP target protocol address of a packet.
|
560
560
|
*
|
561
|
-
* @return [
|
562
|
-
* the value of RARP target protocol address as a
|
561
|
+
* @return [Pio::IPv4Address]
|
562
|
+
* the value of RARP target protocol address as a Pio::IPv4Address object or nil if
|
563
563
|
* packet is not an RARP.
|
564
564
|
*/
|
565
565
|
static VALUE
|
@@ -691,8 +691,8 @@ packet_in_ipv4_checksum( VALUE self ) {
|
|
691
691
|
/*
|
692
692
|
* The IPv4 source IP address of a packet.
|
693
693
|
*
|
694
|
-
* @return [
|
695
|
-
* the value of IPv4 source IP address as a
|
694
|
+
* @return [Pio::IPv4Address,nil]
|
695
|
+
* the value of IPv4 source IP address as a Pio::IPv4Address object or nil if
|
696
696
|
* packet is not an IPv4.
|
697
697
|
*/
|
698
698
|
static VALUE
|
@@ -709,8 +709,8 @@ packet_in_ipv4_saddr( VALUE self ) {
|
|
709
709
|
/*
|
710
710
|
* The IPV4 destination IP address of a packet.
|
711
711
|
*
|
712
|
-
* @return [
|
713
|
-
* the value of IPv4 destination IP address as a
|
712
|
+
* @return [Pio::IPv4Address, nil]
|
713
|
+
* the value of IPv4 destination IP address as a Pio::IPv4Address object or nil if
|
714
714
|
* packet is not an IPv4.
|
715
715
|
*/
|
716
716
|
static VALUE
|
@@ -814,8 +814,8 @@ packet_in_icmpv4_seq( VALUE self ) {
|
|
814
814
|
/*
|
815
815
|
* The ICMPv4 redirect message gateway IP address.
|
816
816
|
*
|
817
|
-
* @return [
|
818
|
-
* the value of ICMPv4 redirect message IP gateway address as a
|
817
|
+
* @return [Pio::IPv4Address, nil]
|
818
|
+
* the value of ICMPv4 redirect message IP gateway address as a Pio::IPv4Address
|
819
819
|
* object or nil if packet is not ICMPv4.
|
820
820
|
*/
|
821
821
|
static VALUE
|
@@ -1009,8 +1009,8 @@ packet_in_igmp_type( VALUE self ) {
|
|
1009
1009
|
/*
|
1010
1010
|
* The IGMP group address.
|
1011
1011
|
*
|
1012
|
-
* @return [
|
1013
|
-
* the value of IGMP IP group address as a
|
1012
|
+
* @return [Pio::IPv4Address, nil]
|
1013
|
+
* the value of IGMP IP group address as a Pio::IPv4Address object
|
1014
1014
|
* or nil if packet is not IGMP.
|
1015
1015
|
*/
|
1016
1016
|
static VALUE
|
@@ -1228,7 +1228,8 @@ packet_in_udp_checksum( VALUE self ) {
|
|
1228
1228
|
*/
|
1229
1229
|
void
|
1230
1230
|
Init_packet_in() {
|
1231
|
-
rb_require( "
|
1231
|
+
rb_require( "rubygems" );
|
1232
|
+
rb_require( "pio" );
|
1232
1233
|
rb_require( "trema/mac" );
|
1233
1234
|
mTrema = rb_eval_string( "Trema" );
|
1234
1235
|
cPacketIn = rb_define_class_under( mTrema, "PacketIn", rb_cObject );
|
data/ruby/trema/stats-reply.c
CHANGED
@@ -225,7 +225,7 @@ get_action( const struct ofp_action_header *ah ) {
|
|
225
225
|
{
|
226
226
|
const struct ofp_action_nw_addr *action_nw_addr = ( const struct ofp_action_nw_addr * ) ah;
|
227
227
|
|
228
|
-
VALUE ip_address = rb_funcall( rb_eval_string( "
|
228
|
+
VALUE ip_address = rb_funcall( rb_eval_string( "Pio::IPv4Address " ), rb_intern( "new" ), 1, UINT2NUM( action_nw_addr->nw_addr ) );
|
229
229
|
if ( ah->type == OFPAT_SET_NW_SRC ) {
|
230
230
|
action = rb_funcall( rb_eval_string( "Trema::SetIpSrcAddr" ), rb_intern( "new" ), 1, rb_funcall( ip_address, rb_intern( "to_s" ), 0 ) );
|
231
231
|
}
|
data/ruby/trema/version.rb
CHANGED
@@ -17,6 +17,7 @@
|
|
17
17
|
|
18
18
|
|
19
19
|
require File.join( File.dirname( __FILE__ ), "..", "spec_helper" )
|
20
|
+
require "pio"
|
20
21
|
require "trema"
|
21
22
|
|
22
23
|
|
@@ -112,8 +113,8 @@ describe Trema::FlowRemoved, ".new( VALID OPTIONS )" do
|
|
112
113
|
expect( message.match.dl_vlan_pcp ).to eq( 0 )
|
113
114
|
expect( message.match.nw_tos ).to eq( 0 )
|
114
115
|
expect( message.match.nw_proto ).to eq( 17 )
|
115
|
-
expect(
|
116
|
-
expect(
|
116
|
+
expect( Pio::IPv4Address.new( message.match.nw_src ).to_s ).to eq( "192.168.0.1" )
|
117
|
+
expect( Pio::IPv4Address.new( message.match.nw_dst ).to_s ).to eq( "192.168.0.2" )
|
117
118
|
expect( message.match.tp_src ).to eq( 1 )
|
118
119
|
expect( message.match.tp_dst ).to eq( 1 )
|
119
120
|
expect( message.cookie ).to eq( 123456789 )
|
@@ -86,9 +86,9 @@ describe Trema::PacketIn do
|
|
86
86
|
expect( message.eth_type ).to eq( 0x0800 )
|
87
87
|
expect( message.ipv4? ).to be_true
|
88
88
|
expect( message.ipv4_version ).to eq( 4 )
|
89
|
-
expect( message.ipv4_saddr ).to be_instance_of(
|
89
|
+
expect( message.ipv4_saddr ).to be_instance_of( Pio::IPv4Address )
|
90
90
|
expect( message.ipv4_saddr.to_s ).to eq( "192.168.1.1" )
|
91
|
-
expect( message.ipv4_daddr ).to be_instance_of(
|
91
|
+
expect( message.ipv4_daddr ).to be_instance_of( Pio::IPv4Address )
|
92
92
|
expect( message.ipv4_daddr.to_s ).to eq( "192.168.1.2" )
|
93
93
|
end
|
94
94
|
send_and_wait
|
data/trema.gemspec
CHANGED
@@ -29,6 +29,7 @@ Gem::Specification.new do | gem |
|
|
29
29
|
gem.add_dependency "bundler"
|
30
30
|
gem.add_dependency "gli", "~> 2.8.0"
|
31
31
|
gem.add_dependency "paper_house", "~> 0.4.0"
|
32
|
+
gem.add_dependency "pio", "~> 0.2.4"
|
32
33
|
gem.add_dependency "rake", "~> 10.1.0"
|
33
34
|
gem.add_dependency "rdoc", "~> 4.0.1"
|
34
35
|
end
|
metadata
CHANGED
@@ -1,97 +1,127 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: trema
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 13
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 4
|
9
|
+
- 1
|
10
|
+
version: 0.4.1
|
5
11
|
platform: ruby
|
6
|
-
authors:
|
12
|
+
authors:
|
7
13
|
- Yasuhito Takamiya
|
8
14
|
autorequire:
|
9
15
|
bindir: .
|
10
16
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
17
|
+
|
18
|
+
date: 2013-09-27 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
22
|
+
none: false
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
hash: 3
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
14
30
|
name: bundler
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - '>='
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :runtime
|
21
31
|
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - '>='
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: gli
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ~>
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 2.8.0
|
34
32
|
type: :runtime
|
35
|
-
|
36
|
-
|
37
|
-
|
33
|
+
requirement: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
38
|
- - ~>
|
39
|
-
- !ruby/object:Gem::Version
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
hash: 47
|
41
|
+
segments:
|
42
|
+
- 2
|
43
|
+
- 8
|
44
|
+
- 0
|
40
45
|
version: 2.8.0
|
41
|
-
|
42
|
-
name: paper_house
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ~>
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: 0.4.0
|
48
|
-
type: :runtime
|
46
|
+
name: gli
|
49
47
|
prerelease: false
|
50
|
-
|
51
|
-
|
48
|
+
type: :runtime
|
49
|
+
requirement: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
52
54
|
- - ~>
|
53
|
-
- !ruby/object:Gem::Version
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 15
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
- 4
|
60
|
+
- 0
|
54
61
|
version: 0.4.0
|
55
|
-
|
56
|
-
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: 10.1.0
|
62
|
+
name: paper_house
|
63
|
+
prerelease: false
|
62
64
|
type: :runtime
|
65
|
+
requirement: *id003
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ~>
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 31
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
- 2
|
76
|
+
- 4
|
77
|
+
version: 0.2.4
|
78
|
+
name: pio
|
63
79
|
prerelease: false
|
64
|
-
|
65
|
-
|
80
|
+
type: :runtime
|
81
|
+
requirement: *id004
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
66
86
|
- - ~>
|
67
|
-
- !ruby/object:Gem::Version
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 75
|
89
|
+
segments:
|
90
|
+
- 10
|
91
|
+
- 1
|
92
|
+
- 0
|
68
93
|
version: 10.1.0
|
69
|
-
|
70
|
-
name: rdoc
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ~>
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: 4.0.1
|
76
|
-
type: :runtime
|
94
|
+
name: rake
|
77
95
|
prerelease: false
|
78
|
-
|
79
|
-
|
96
|
+
type: :runtime
|
97
|
+
requirement: *id005
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
80
102
|
- - ~>
|
81
|
-
- !ruby/object:Gem::Version
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
hash: 61
|
105
|
+
segments:
|
106
|
+
- 4
|
107
|
+
- 0
|
108
|
+
- 1
|
82
109
|
version: 4.0.1
|
83
|
-
|
84
|
-
|
85
|
-
|
110
|
+
name: rdoc
|
111
|
+
prerelease: false
|
112
|
+
type: :runtime
|
113
|
+
requirement: *id006
|
114
|
+
description: Trema is a full-stack, easy-to-use framework for developing OpenFlow controllers in Ruby and C.
|
115
|
+
email:
|
86
116
|
- yasuhito@gmail.com
|
87
|
-
executables:
|
117
|
+
executables:
|
88
118
|
- trema
|
89
119
|
- trema-config
|
90
|
-
extensions:
|
120
|
+
extensions:
|
91
121
|
- Rakefile
|
92
|
-
extra_rdoc_files:
|
122
|
+
extra_rdoc_files:
|
93
123
|
- README.md
|
94
|
-
files:
|
124
|
+
files:
|
95
125
|
- .gitignore
|
96
126
|
- .ruby-version
|
97
127
|
- .travis.yml
|
@@ -242,7 +272,6 @@ files:
|
|
242
272
|
- ruby/trema/hello.c
|
243
273
|
- ruby/trema/hello.h
|
244
274
|
- ruby/trema/host.rb
|
245
|
-
- ruby/trema/ip.rb
|
246
275
|
- ruby/trema/link.rb
|
247
276
|
- ruby/trema/list-switches-reply.c
|
248
277
|
- ruby/trema/list-switches-reply.h
|
@@ -376,7 +405,6 @@ files:
|
|
376
405
|
- spec/trema/hardware-switch_spec.rb
|
377
406
|
- spec/trema/hello_spec.rb
|
378
407
|
- spec/trema/host_spec.rb
|
379
|
-
- spec/trema/ip_spec.rb
|
380
408
|
- spec/trema/link_spec.rb
|
381
409
|
- spec/trema/list-switches-reply_spec.rb
|
382
410
|
- spec/trema/mac_spec.rb
|
@@ -785,28 +813,40 @@ files:
|
|
785
813
|
- ./trema
|
786
814
|
- ./trema-config
|
787
815
|
homepage: http://github.com/trema/trema
|
788
|
-
licenses:
|
816
|
+
licenses:
|
789
817
|
- GPL2
|
790
|
-
metadata: {}
|
791
818
|
post_install_message:
|
792
819
|
rdoc_options: []
|
793
|
-
|
820
|
+
|
821
|
+
require_paths:
|
794
822
|
- ruby
|
795
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
796
|
-
|
797
|
-
|
798
|
-
|
823
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
824
|
+
none: false
|
825
|
+
requirements:
|
826
|
+
- - ">="
|
827
|
+
- !ruby/object:Gem::Version
|
828
|
+
hash: 57
|
829
|
+
segments:
|
830
|
+
- 1
|
831
|
+
- 8
|
832
|
+
- 7
|
799
833
|
version: 1.8.7
|
800
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
834
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
835
|
+
none: false
|
836
|
+
requirements:
|
837
|
+
- - ">="
|
838
|
+
- !ruby/object:Gem::Version
|
839
|
+
hash: 3
|
840
|
+
segments:
|
841
|
+
- 0
|
842
|
+
version: "0"
|
805
843
|
requirements: []
|
844
|
+
|
806
845
|
rubyforge_project:
|
807
|
-
rubygems_version:
|
846
|
+
rubygems_version: 1.8.25
|
808
847
|
signing_key:
|
809
|
-
specification_version:
|
848
|
+
specification_version: 3
|
810
849
|
summary: Full-stack OpenFlow framework.
|
811
850
|
test_files: []
|
851
|
+
|
812
852
|
has_rdoc:
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: ef20c00d0e61a93ddf449432c366f78b620ac54c
|
4
|
-
data.tar.gz: c516614e8c862044a82765a2faee850cbdccb30a
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 1169678d2c9832448bbdab636a8323784873ca7f89ba3354ea8552f905c5e78bc2a77e8fd9a09e5ed3fddfc720166a530df3b47ae1282e54fe2b551196d63c47
|
7
|
-
data.tar.gz: a357cf352ea0004e22575e50e8060925bd9892923e9a8338aed04c5c42b25da402e4cb5cfff65409dc9a9b370b8373422dbd3743ba65d56fe6842a3c7be86a45
|
data/ruby/trema/ip.rb
DELETED
@@ -1,111 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (C) 2008-2013 NEC Corporation
|
3
|
-
#
|
4
|
-
# This program is free software; you can redistribute it and/or modify
|
5
|
-
# it under the terms of the GNU General Public License, version 2, as
|
6
|
-
# published by the Free Software Foundation.
|
7
|
-
#
|
8
|
-
# This program is distributed in the hope that it will be useful,
|
9
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
-
# GNU General Public License for more details.
|
12
|
-
#
|
13
|
-
# You should have received a copy of the GNU General Public License along
|
14
|
-
# with this program; if not, write to the Free Software Foundation, Inc.,
|
15
|
-
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
module Trema
|
20
|
-
#
|
21
|
-
# A wrapper class to IPAddr
|
22
|
-
#
|
23
|
-
class IP
|
24
|
-
require "ipaddr"
|
25
|
-
|
26
|
-
|
27
|
-
#
|
28
|
-
# @return [IPAddr] value object instance of proxied IPAddr.
|
29
|
-
#
|
30
|
-
attr_reader :value
|
31
|
-
|
32
|
-
|
33
|
-
#
|
34
|
-
# Creates a {IP} instance object as a proxy to IPAddr class.
|
35
|
-
#
|
36
|
-
# @overload initialize(addr)
|
37
|
-
#
|
38
|
-
# @param [String, Number] addr
|
39
|
-
# an IPv4 address specified either as a String or Number.
|
40
|
-
#
|
41
|
-
# @param [Number] prefixlen
|
42
|
-
# masking IPv4 address with given prefixlen.
|
43
|
-
#
|
44
|
-
# @raise [ArgumentError] invalid address if supplied argument is invalid
|
45
|
-
# IPv4 address.
|
46
|
-
#
|
47
|
-
# @return [IP] self
|
48
|
-
# a proxy to IPAddr.
|
49
|
-
#
|
50
|
-
def initialize addr, prefixlen = 32
|
51
|
-
if !addr.kind_of? String
|
52
|
-
@value = IPAddr.new( addr, Socket::AF_INET )
|
53
|
-
else
|
54
|
-
@value = IPAddr.new( addr )
|
55
|
-
end
|
56
|
-
if prefixlen < 32
|
57
|
-
@value = @value.mask( prefixlen )
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
|
62
|
-
#
|
63
|
-
# @return [ Number ] prefixlen of IPv4 address.
|
64
|
-
#
|
65
|
-
def prefixlen
|
66
|
-
range = @value.to_range
|
67
|
-
mask = range.first.to_i ^ range.last.to_i
|
68
|
-
masklen = 0
|
69
|
-
while mask != 0 do
|
70
|
-
mask = mask >> 1
|
71
|
-
masklen += 1
|
72
|
-
end
|
73
|
-
return 32 - masklen
|
74
|
-
end
|
75
|
-
|
76
|
-
|
77
|
-
#
|
78
|
-
# @return [String] the IPv4 address in its text representation.
|
79
|
-
#
|
80
|
-
def to_s
|
81
|
-
@value.to_s
|
82
|
-
end
|
83
|
-
|
84
|
-
|
85
|
-
#
|
86
|
-
# @return [Number] the IPv4 address in its numeric representation.
|
87
|
-
#
|
88
|
-
def to_i
|
89
|
-
@value.to_i
|
90
|
-
end
|
91
|
-
|
92
|
-
|
93
|
-
#
|
94
|
-
# @return [Array]
|
95
|
-
# an array of decimal numbers converted from IP address.
|
96
|
-
#
|
97
|
-
def to_a
|
98
|
-
to_s.split( "." ).collect do | each |
|
99
|
-
each.to_i
|
100
|
-
end
|
101
|
-
end
|
102
|
-
alias :to_array :to_a
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
|
107
|
-
### Local variables:
|
108
|
-
### mode: Ruby
|
109
|
-
### coding: utf-8-unix
|
110
|
-
### indent-tabs-mode: nil
|
111
|
-
### End:
|
data/spec/trema/ip_spec.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (C) 2008-2013 NEC Corporation
|
3
|
-
#
|
4
|
-
# This program is free software; you can redistribute it and/or modify
|
5
|
-
# it under the terms of the GNU General Public License, version 2, as
|
6
|
-
# published by the Free Software Foundation.
|
7
|
-
#
|
8
|
-
# This program is distributed in the hope that it will be useful,
|
9
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
-
# GNU General Public License for more details.
|
12
|
-
#
|
13
|
-
# You should have received a copy of the GNU General Public License along
|
14
|
-
# with this program; if not, write to the Free Software Foundation, Inc.,
|
15
|
-
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
require File.join( File.dirname( __FILE__ ), "..", "spec_helper" )
|
20
|
-
require "trema/ip"
|
21
|
-
|
22
|
-
|
23
|
-
module Trema
|
24
|
-
describe IP do
|
25
|
-
context "when creating" do
|
26
|
-
subject { IP.new( ip_address, prefixlen ) }
|
27
|
-
|
28
|
-
context %{when "192.168.1.1/32"} do
|
29
|
-
let( :ip_address ) { "192.168.1.1" }
|
30
|
-
let( :prefixlen ) { 32 }
|
31
|
-
|
32
|
-
its( :to_s ) { should == "192.168.1.1" }
|
33
|
-
its( :to_i ) { should == 3232235777 }
|
34
|
-
its( :to_a ) { should == [ 0xc0, 0xa8, 0x01, 0x01 ] }
|
35
|
-
end
|
36
|
-
|
37
|
-
context %{when "10.1.1.1/8"} do
|
38
|
-
let( :ip_address ) { "10.1.1.1" }
|
39
|
-
let( :prefixlen ) { 8 }
|
40
|
-
|
41
|
-
its( :to_s ) { should == "10.0.0.0" }
|
42
|
-
its( :to_i ) { should == 10 * 256 * 256 * 256 }
|
43
|
-
its( :to_a ) { should == [ 0x0a, 0x00, 0x00, 0x00 ] }
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
|
50
|
-
### Local variables:
|
51
|
-
### mode: Ruby
|
52
|
-
### coding: utf-8-unix
|
53
|
-
### indent-tabs-mode: nil
|
54
|
-
### End:
|