bgp4r 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/COPYING +674 -0
  2. data/LICENSE.txt +53 -0
  3. data/README.rdoc +259 -0
  4. data/bgp/aggregator.rb +104 -0
  5. data/bgp/as_path.rb +223 -0
  6. data/bgp/atomic_aggregate.rb +42 -0
  7. data/bgp/attribute.rb +181 -0
  8. data/bgp/attributes.rb +34 -0
  9. data/bgp/cluster_list.rb +117 -0
  10. data/bgp/common.rb +204 -0
  11. data/bgp/communities.rb +139 -0
  12. data/bgp/extended_communities.rb +107 -0
  13. data/bgp/extended_community.rb +254 -0
  14. data/bgp/iana.rb +269 -0
  15. data/bgp/io.rb +116 -0
  16. data/bgp/label.rb +94 -0
  17. data/bgp/local_pref.rb +75 -0
  18. data/bgp/message.rb +894 -0
  19. data/bgp/mp_reach.rb +208 -0
  20. data/bgp/multi_exit_disc.rb +76 -0
  21. data/bgp/neighbor.rb +291 -0
  22. data/bgp/next_hop.rb +63 -0
  23. data/bgp/nlri.rb +303 -0
  24. data/bgp/orf.rb +88 -0
  25. data/bgp/origin.rb +88 -0
  26. data/bgp/originator_id.rb +73 -0
  27. data/bgp/path_attribute.rb +210 -0
  28. data/bgp/prefix_orf.rb +263 -0
  29. data/bgp/rd.rb +107 -0
  30. data/examples/bgp +65 -0
  31. data/examples/routegen +85 -0
  32. data/examples/routegen.yml +50 -0
  33. data/test/aggregator_test.rb +66 -0
  34. data/test/as_path_test.rb +149 -0
  35. data/test/atomic_aggregate_test.rb +35 -0
  36. data/test/attribute_test.rb +57 -0
  37. data/test/cluster_list_test.rb +39 -0
  38. data/test/common_test.rb +68 -0
  39. data/test/communities_test.rb +75 -0
  40. data/test/extended_communities_test.rb +111 -0
  41. data/test/extended_community_test.rb +93 -0
  42. data/test/label_test.rb +50 -0
  43. data/test/local_pref_test.rb +43 -0
  44. data/test/message_test.rb +294 -0
  45. data/test/mp_reach_test.rb +143 -0
  46. data/test/multi_exit_disc_test.rb +46 -0
  47. data/test/neighbor_test.rb +50 -0
  48. data/test/next_hop_test.rb +37 -0
  49. data/test/nlri_test.rb +189 -0
  50. data/test/origin_test.rb +57 -0
  51. data/test/originator_id_test.rb +38 -0
  52. data/test/path_attribute_test.rb +127 -0
  53. data/test/prefix_orf_test.rb +97 -0
  54. data/test/rd_test.rb +44 -0
  55. metadata +133 -0
data/bgp/io.rb ADDED
@@ -0,0 +1,116 @@
1
+ #--
2
+ # Copyright 2008, 2009 Jean-Michel Esnault.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #
6
+ #
7
+ # This file is part of BGP4R.
8
+ #
9
+ # BGP4R is free software: you can redistribute it and/or modify
10
+ # it under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation, either version 3 of the License, or
12
+ # (at your option) any later version.
13
+ #
14
+ # BGP4R is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with BGP4R. If not, see <http://www.gnu.org/licenses/>.
21
+ #++
22
+
23
+
24
+ require 'observer'
25
+ require 'thread'
26
+ require 'bgp/common'
27
+ require 'bgp/message'
28
+
29
+ module BGP
30
+ module IO
31
+
32
+ class Output < Queue
33
+ include Observable
34
+ def initialize(session, holdtime, *obs)
35
+ @session, @holdtime = session, holdtime
36
+ obs.each { |o| add_observer(o) }
37
+ @continue = true
38
+ super()
39
+ end
40
+ attr_reader :thread
41
+ attr_writer :holdtime
42
+ def start
43
+ @thread = Thread.new(@session, @holdtime) do |s, h|
44
+ Thread.current['name']='BGP IO Ouput'
45
+ Log.debug "#{self} #{Thread.current} started"
46
+ begin
47
+ while @continue
48
+ obj = deq
49
+ break unless @continue
50
+ s.write obj.respond_to?(:encode) ? obj.encode : obj
51
+ end
52
+ rescue IOError, Errno::ECONNRESET, Errno::EBADF => e
53
+ puts "ECONNRESET"
54
+ changed and notify_observers(:ev_conn_reset, e)
55
+ ensure
56
+ Log.debug "Exiting #{Thread.current['name']}"
57
+ end
58
+ end
59
+ end
60
+ def exit
61
+ @continue = false
62
+ enq('you\'re done dude!')
63
+ end
64
+ end
65
+
66
+ class Input
67
+ include Observable
68
+ def initialize(session, holdtime, *obs)
69
+ @session, @holdtime = session, holdtime
70
+ obs.each { |o| add_observer(o) }
71
+ end
72
+ attr_reader :thread
73
+ attr_writer :holdtime
74
+ def recv_msg(s)
75
+ loop do
76
+ if @buf.size<18 or @buf.size <@len
77
+ recv = s.recv(4100)
78
+ @buf += recv unless recv.nil?
79
+ break unless @continue
80
+ end
81
+ if @buf.size>18
82
+ @len, @type=@buf[16,3].unpack('nC')
83
+ return @buf.slice!(0,@len) if @len<=@buf.size
84
+ end
85
+ end
86
+ end
87
+
88
+ def start
89
+ @thread = Thread.new(@session, @holdtime) do |s, h|
90
+ Thread.current['name']='BGP IO Input'
91
+ Log.debug "#{self} #{Thread.current} started"
92
+ @buf = ''
93
+ @continue = true
94
+ @len=0
95
+ begin
96
+ while @continue
97
+ begin
98
+ Timeout::timeout(h) do |h|
99
+ message, type = recv_msg(s)
100
+ break unless @continue
101
+ changed and notify_observers(:ev_msg, @type, message)
102
+ end
103
+ rescue Timeout::Error => e
104
+ changed and notify_observers(:ev_holdtime_expire, e)
105
+ @continue = false
106
+ end
107
+ end
108
+ rescue IOError, Errno::ECONNRESET, Errno::EBADF => e
109
+ changed and notify_observers(:ev_conn_reset, e)
110
+ end
111
+ Log.debug "Exiting #{Thread.current['name']}"
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
data/bgp/label.rb ADDED
@@ -0,0 +1,94 @@
1
+ #--
2
+ # Copyright 2008, 2009 Jean-Michel Esnault.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #
6
+ #
7
+ # This file is part of BGP4R.
8
+ #
9
+ # BGP4R is free software: you can redistribute it and/or modify
10
+ # it under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation, either version 3 of the License, or
12
+ # (at your option) any later version.
13
+ #
14
+ # BGP4R is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with BGP4R. If not, see <http://www.gnu.org/licenses/>.
21
+ #++
22
+
23
+ require 'bgp/common'
24
+
25
+ module BGP
26
+ class Label
27
+ attr_reader :label
28
+ def initialize(*args)
29
+ @label, @exp = [0]*3
30
+ if args.size==1 and args[0].is_a?(Hash)
31
+ @label=args[0][:label] if args[0][:label]
32
+ @exp=args[0][:exp] if args[0][:exp]
33
+ elsif args.size==1 and args[0].is_a?(String) and args[0].is_packed?
34
+ parse(args[0])
35
+ else args[0].is_a?(Fixnum)
36
+ @label, @exp = args + [0]
37
+ end
38
+ end
39
+ def encode(bottom=1)
40
+ n = (@label <<4) | (@exp & 0x7)<<1 |(bottom &0x1)
41
+ o1 = (n & 0xff0000) >> 16
42
+ o2 = (n & 0x00ff00) >> 8
43
+ o3 = (n & 0x0000ff)
44
+ [o1,o2,o3].pack('CCC')
45
+ end
46
+ def parse(s)
47
+ octets = s.slice!(0,3).unpack('CCC')
48
+ n = (octets[0] << 16) + (octets[1] << 8) + octets[2]
49
+ @exp = (n >> 1) & 0x7
50
+ @label = (n >> 4)
51
+ end
52
+ def to_hash
53
+ {:label=>@label, :exp=>@exp}
54
+ end
55
+ end
56
+ class Label_stack
57
+ def initialize(*args)
58
+ @label_stack=[]
59
+ if args.size==1 and args[0].is_a?(String) and args[0].is_packed?
60
+ parse(args[0])
61
+ else
62
+ args.each { |arg| @label_stack << (arg.is_a?(Label) ? arg : Label.new(arg)) }
63
+ end
64
+ end
65
+ def size
66
+ @label_stack.size
67
+ end
68
+ def encode
69
+ enc = @label_stack[0..-2].collect { |l| l.encode(0) }
70
+ enc << @label_stack[-1].encode
71
+ enc.join
72
+ end
73
+ def parse(s)
74
+ while s.size>0
75
+ bottom = s[2,1].unpack('C')[0]
76
+ @label_stack << label = Label.new(s)
77
+ break if bottom & 1 > 0
78
+ end
79
+ end
80
+ def to_s
81
+ if @label_stack.empty?
82
+ "Label stack:(empty)"
83
+ else
84
+ "Label Stack=#{@label_stack.collect{ |l| l.label }.join(',')} (bottom)"
85
+ end
86
+ end
87
+ def bit_length
88
+ @label_stack.compact.size*24
89
+ end
90
+ end
91
+ end
92
+
93
+ load "../test/#{ File.basename($0.gsub(/.rb/,'_test.rb'))}" if __FILE__ == $0
94
+
data/bgp/local_pref.rb ADDED
@@ -0,0 +1,75 @@
1
+ #--
2
+ # Copyright 2008, 2009 Jean-Michel Esnault.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #
6
+ #
7
+ # This file is part of BGP4R.
8
+ #
9
+ # BGP4R is free software: you can redistribute it and/or modify
10
+ # it under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation, either version 3 of the License, or
12
+ # (at your option) any later version.
13
+ #
14
+ # BGP4R is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with BGP4R. If not, see <http://www.gnu.org/licenses/>.
21
+ #++
22
+
23
+
24
+ require 'bgp/attribute'
25
+
26
+ module BGP
27
+
28
+ class Local_pref < Attr
29
+
30
+ def initialize(arg=100)
31
+ @flags, @type = WELL_KNOWN_MANDATORY, LOCAL_PREF
32
+ if arg.is_a?(String) and arg.is_packed?
33
+ parse(arg)
34
+ elsif arg.is_a?(self.class)
35
+ parse(arg.encode)
36
+ elsif arg.is_a?(Fixnum) or arg.is_a?(Bignum)
37
+ self.local_pref = arg
38
+ elsif arg.is_a?(Hash) and arg[:local_pref]
39
+ self.local_pref = arg[:local_pref]
40
+ else
41
+ raise ArgumentError, "invalid argument, #{arg.inspect}"
42
+ end
43
+ end
44
+
45
+ def local_pref=(val)
46
+ raise ArgumentError, "invalid argument" unless val.is_a?(Integer)
47
+ @local_pref=val
48
+ end
49
+
50
+ def to_i
51
+ @local_pref
52
+ end
53
+
54
+ def encode
55
+ super([to_i].pack('N'))
56
+ end
57
+
58
+ def local_pref
59
+ format("(0x%4.4x) %d", to_i, to_i)
60
+ end
61
+
62
+ def to_s(method=:default)
63
+ super(local_pref, method)
64
+ end
65
+
66
+ private
67
+
68
+ def parse(s)
69
+ @flags, @type, len, @local_pref = super(s,'N')
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+ load "../test/#{ File.basename($0.gsub(/.rb/,'_test.rb'))}" if __FILE__ == $0