Module: IANA::Port
- Defined in:
- lib/iana/port.rb
Defined Under Namespace
Classes: Port
Class Method Summary collapse
-
.load(pathname) ⇒ Object
load IANA ports list from flat file: www.iana.org/assignments/port-numbers.
Class Method Details
.load(pathname) ⇒ Object
load IANA ports list from flat file: www.iana.org/assignments/port-numbers
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/iana/port.rb', line 12 def self.load(pathname) raise ArgumentError, 'nil pathname' if pathname.nil? raise ArgumentError, 'invalid pathname class' if pathname.class != String raise ArgumentError, 'empty pathname' if pathname.empty? # TODO: better error checking for files with incorrect content ports = {} updated = nil begin f = File.new(pathname, 'r') while (line = f.gets) line.chomp! # extract update stamp if line =~ /^\(last updated (\d{4}-\d{2}-\d{2})\)\s*$/ updated = $1 next end # skip commented lines next if line =~ /^#/ # skip lines which do not contain the /{proto} pattern if line !~ /\/tcp/ && line !~ /\/udp/ && line !~ /\/sctp/ && \ line !~ /\/dccp/ next end line.strip! tokens = line.split(/[\s\t]+/) # if first token is a port/proto pair then the port is unnamed if tokens[0] =~ /\// name = nil num,proto = tokens[0].split(/\//) tokens.delete_at(0) else name = tokens[0] num,proto = tokens[1].split(/\//) 2.times { tokens.delete_at(0) } end # remainder of tokens serves as the description desc = tokens.join(' ') p = Port.new(name, proto, desc) if ports[num.to_i].nil? ports[num.to_i] = p else c = [] c << ports[num.to_i] c << p ports[num.to_i] = c.flatten end end ensure f.close if !f.nil? end return ports, updated end |