rpsl 1.0.0

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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +11 -0
  3. data/README.md +3 -0
  4. data/lib/rpsl.rb +193 -0
  5. metadata +51 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0e38b87f9210a62ab14099d58203fe8ebdd90f09a5cf767c56b528bf38a0567e
4
+ data.tar.gz: c60737bfe227e86e97bd1209db5334b40b3f24ef7b44bff58c964ae07ef71005
5
+ SHA512:
6
+ metadata.gz: 858f827591d0ce04d15820b3d4f323a346019615fa2b45f39ef7762c1970a560598da6fdf7bd34496438b4f73ad0dc926bcc30a6179f17afcd9219219a95ac3a
7
+ data.tar.gz: 9d4491da2e2313b0809f40c3ddf9e5716a39ec0e14871cb353d636b825a480661f2027560e857d4901f368377fd05ed844540616a8330c10af735c9777711e59
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.
7
+
8
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
9
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
10
+
11
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # RPSL
2
+
3
+ Library which provides functions for reading and writing Routing Policy Specification Language (RPSL).
data/lib/rpsl.rb ADDED
@@ -0,0 +1,193 @@
1
+ # Library which provides functions for reading and writing Routing Policy Specification Language (RPSL, RFC2622).
2
+ module RPSL
3
+ # Generic error when processing RPSL
4
+ class RPSLError < StandardError; end
5
+
6
+ # Error while loading RPSL
7
+ class ParserError < RPSLError; end
8
+
9
+ # Error while generating RPSL
10
+ class DumpError < RPSLError; end
11
+
12
+ # Parses a string as RPSL.
13
+ # @param rpsl [String] The RPSL data as an object which can be iterated
14
+ # with each_line.
15
+ # @return [Hash] Hash containing the RPSL data.
16
+ # @example
17
+ # rpsl = <<EOF
18
+ # route6: fd42:4242:2601:ffff::/64
19
+ # descr: This is an ROA test object
20
+ # max-length: 112
21
+ # remarks:
22
+ # This object is designed to test ROA scripts
23
+ # in a number of ways.
24
+ # +
25
+ # The expected output for this route should be:
26
+ # roa fd42:4242:2601:ffff::/64 max 64 as 0;
27
+ # +
28
+ # This is not a blank line
29
+ # + ----- +
30
+ # +
31
+ # The first test is to include a number of syntax
32
+ # corner cases, designed to trip up parsers
33
+ # +
34
+ # The first max-length line should be clamped
35
+ # to the maximums defined in filter6.txt
36
+ # +
37
+ # The second max-length is part of the remark
38
+ # and should be ignored.
39
+ # +
40
+ # max-length: 48
41
+ # +
42
+ # origin: AS0
43
+ # mnt-by: BURBLE-MNT
44
+ # source: DN42
45
+ # EOF
46
+ #
47
+ # RPSL.load(rpsl) # => {"route6"=>"fd42:4242:2601:ffff::/64\n",
48
+ # # "descr"=>"This is an ROA test object\n",
49
+ # # "max-length"=>"112\n",
50
+ # # "remarks"=>
51
+ # # "\n" +
52
+ # # "This object is designed to test ROA scripts\n" +
53
+ # # "in a number of ways.\n" +
54
+ # # "\n" +
55
+ # # "The expected output for this route should be:\n" +
56
+ # # "roa fd42:4242:2601:ffff::/64 max 64 as 0;\n" +
57
+ # # "\n" +
58
+ # # "This is not a blank line\n" +
59
+ # # "+ ----- +\n" +
60
+ # # "\n" +
61
+ # # "The first test is to include a number of syntax\n" +
62
+ # # "corner cases, designed to trip up parsers\n" +
63
+ # # "\n" +
64
+ # # "The first max-length line should be clamped\n" +
65
+ # # "to the maximums defined in filter6.txt\n" +
66
+ # # "\n" +
67
+ # # "The second max-length is part of the remark\n" +
68
+ # # "and should be ignored.\n" +
69
+ # # "\n" +
70
+ # # "max-length: 48\n" +
71
+ # # "\n",
72
+ # # "origin"=>"AS0\n",
73
+ # # "mnt-by"=>"BURBLE-MNT\n",
74
+ # # "source"=>"DN42\n"}
75
+ def self.load(rpsl)
76
+ obj = {}
77
+ current_section = nil
78
+
79
+ rpsl.to_s.each_line.with_index do |line, lineno|
80
+ line.chomp!
81
+
82
+ case line
83
+ when /^(.+): *(.*)$/
84
+ current_section = $1
85
+
86
+ obj[current_section] = "#{obj[current_section]}#{$2}\n"
87
+ when /^[+ \s] *(.+)$/
88
+ raise ParserError, "Value without key in line #{lineno + 1}" unless current_section
89
+
90
+ obj[current_section] = "#{obj[current_section]}#{$1}\n"
91
+ when /^\+ *$/
92
+ raise ParserError, "Empty line without key in line #{lineno + 1}" unless current_section
93
+
94
+ obj[current_section] = "#{obj[current_section]}\n"
95
+ else
96
+ raise ParserError, "Failed to parse line #{lineno + 1}."
97
+ end
98
+ end
99
+
100
+ return obj
101
+ end
102
+
103
+ # Converts a hash into the RPSL format.
104
+ # @param obj [Hash] Hash in which the data to be converted is stored.
105
+ # @param value_column [Integer, NilClass] `nil` or the column where the
106
+ # value should start.
107
+ # @example
108
+ # obj = {"route6"=>"fd42:4242:2601:ffff::/64",
109
+ # "descr"=>"This is an ROA test object",
110
+ # "max-length"=>"112",
111
+ # "remarks"=>
112
+ # "\n" +
113
+ # "This object is designed to test ROA scripts\n" +
114
+ # "in a number of ways.\n" +
115
+ # "\n" +
116
+ # "The expected output for this route should be:\n" +
117
+ # "roa fd42:4242:2601:ffff::/64 max 64 as 0;\n" +
118
+ # "\n" +
119
+ # "This is not a blank line\n" +
120
+ # "+ ----- +\n" +
121
+ # "\n" +
122
+ # "The first test is to include a number of syntax\n" +
123
+ # "corner cases, designed to trip up parsers\n" +
124
+ # "\n" +
125
+ # "The first max-length line should be clamped\n" +
126
+ # "to the maximums defined in filter6.txt\n" +
127
+ # "\n" +
128
+ # "The second max-length is part of the remark\n" +
129
+ # "and should be ignored.\n" +
130
+ # "\n" +
131
+ # "max-length: 48",
132
+ # "origin"=>"AS0",
133
+ # "mnt-by"=>"BURBLE-MNT",
134
+ # "source"=>"DN42"}
135
+ #
136
+ # RPSL.dump(obj, 20) # => "route6: fd42:4242:2601:ffff::/64\n" +
137
+ # # "descr: This is an ROA test object\n" +
138
+ # # "max-length: 112\n" +
139
+ # # "remarks: \n" +
140
+ # # " This object is designed to test ROA scripts\n" +
141
+ # # " in a number of ways.\n" +
142
+ # # "+\n" +
143
+ # # " The expected output for this route should be:\n" +
144
+ # # " roa fd42:4242:2601:ffff::/64 max 64 as 0;\n" +
145
+ # # "+\n" +
146
+ # # " This is not a blank line\n" +
147
+ # # " + ----- +\n" +
148
+ # # "+\n" +
149
+ # # " The first test is to include a number of syntax\n" +
150
+ # # " corner cases, designed to trip up parsers\n" +
151
+ # # "+\n" +
152
+ # # " The first max-length line should be clamped\n" +
153
+ # # " to the maximums defined in filter6.txt\n" +
154
+ # # "+\n" +
155
+ # # " The second max-length is part of the remark\n" +
156
+ # # " and should be ignored.\n" +
157
+ # # "+\n" +
158
+ # # " max-length: 48\n" +
159
+ # # "origin: AS0\n" +
160
+ # # "mnt-by: BURBLE-MNT\n" +
161
+ # # "source: DN42\n"
162
+ def self.dump(obj, value_column = nil)
163
+ rpsl = ''
164
+
165
+ obj.each do |key, value|
166
+ lines = value.to_s.lines
167
+ lines.map!(&:chomp)
168
+ first_value = lines.shift
169
+
170
+ key = key.to_s
171
+
172
+ if value_column
173
+ padding_length = value_column.to_i - 1 - key.length
174
+ raise DumpError, 'Key too long.' if padding_length.negative?
175
+
176
+ padding = ' ' * padding_length
177
+ end
178
+
179
+ rpsl += "#{key}:#{padding}#{first_value}\n"
180
+ lines.each do |line|
181
+ rpsl += if line.empty?
182
+ "+\n"
183
+ elsif value_column
184
+ "#{' ' * value_column}#{line}\n"
185
+ else
186
+ " #{line}\n"
187
+ end
188
+ end
189
+ end
190
+
191
+ return rpsl
192
+ end
193
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rpsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Marek Küthe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-06-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Library which provides functions for reading and writing Routing Policy
14
+ Specification Language (RPSL, RFC2622).
15
+ email: m.k@mk16.de
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - LICENSE
20
+ - README.md
21
+ files:
22
+ - LICENSE
23
+ - README.md
24
+ - lib/rpsl.rb
25
+ homepage: https://codeberg.org/mark22k/rpsl
26
+ licenses:
27
+ - WTFPL
28
+ metadata:
29
+ source_code_uri: https://codeberg.org/mark22k/rpsl
30
+ bug_tracker_uri: https://codeberg.org/mark22k/rpsl/issues
31
+ rubygems_mfa_required: 'true'
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.4.14
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Library which provides functions for reading and writing RPSL.
51
+ test_files: []