net-proto 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 83b848fce79a24c404a269ecfaf838c8999e000a
4
+ data.tar.gz: 33fada4059a1ac04b305e0d04b621d777cc4e5e9
5
+ SHA512:
6
+ metadata.gz: fd37187da1d51e62cf87fb54dab95ab6a5dc718aa397f25b9fb7e2e7898926d00ef38445bfa71619606ddee887610bbb89fb103b7f39fcc989fddb3ef8e5b270
7
+ data.tar.gz: d36e65afe0656f492d34ada92d6833aa18da0c36ec3846b26bb1cef8172c64a82dadda712d6c282d86c811013e597bb7745e9cb5a1ecc500ec23f06dab869968
data/CHANGES CHANGED
@@ -1,3 +1,7 @@
1
+ == 1.1.1 - 9-Oct-2014
2
+ * Implemented getprotoent on Windows using pure Ruby.
3
+ * Miscellaneous minor updates to the Rakefile, gemspec and docs.
4
+
1
5
  == 1.1.0 - 18-Jan-2012
2
6
  * Switched to FFI instead of C backend. Now works with JRuby, too.
3
7
  * Added the generic get_protocol instance method that accepts either a
data/README CHANGED
@@ -26,10 +26,6 @@ Net::Proto.getprotoent do |ent|
26
26
  p ent
27
27
  end
28
28
 
29
- == MS Windows
30
- The Proto.getprotoent method is not supported on MS Windows because the
31
- underlying function isn't supported by the MS Windows API.
32
-
33
29
  == Why should I use this?
34
30
  Ruby has a predefined set of constants in socket.c in the general form of
35
31
  IPPROTO_XXX, Y. However, using constants in this fashion can be unreliable
data/Rakefile CHANGED
@@ -8,13 +8,18 @@ namespace 'gem' do
8
8
  desc 'Create the net-proto gem'
9
9
  task :create => :clean do
10
10
  spec = eval(IO.read('net-proto.gemspec'))
11
- Gem::Builder.new(spec).build
11
+ if Gem::VERSION.to_f < 2.0
12
+ Gem::Builder.new(spec).build
13
+ else
14
+ require 'rubygems/package'
15
+ Gem::Package.build(spec)
16
+ end
12
17
  end
13
18
 
14
19
  desc 'Install the net-proto gem'
15
20
  task :install => [:create] do
16
21
  file = Dir["net-proto*.gem"].last
17
- sh "gem install #{file}"
22
+ sh "gem install -l #{file}"
18
23
  end
19
24
  end
20
25
 
@@ -39,8 +39,6 @@ Proto.getprotoent{ |struct| ... }
39
39
 
40
40
  The fields are 'name' (a String), 'aliases' (an Array of String's,
41
41
  though often only one element), and 'proto' (a Fixnum).
42
-
43
- This method is NOT supported on MS Windows.
44
42
 
45
43
  == Notes
46
44
  This module uses the reentrant (i.e. thread safe) functions on those
@@ -52,11 +50,12 @@ Proto.getprotoent{ |struct| ... }
52
50
  separate method calls. Rather, these are called internally by the various
53
51
  methods, except on Windows, which does not support them.
54
52
 
55
- The 'getprotoent()' method is not supported on the MS Windows platform.
56
- It's not part of the API as of Windows XP.
53
+ The 'getprotoent()' method on Windows is using Ruby to read directly from
54
+ your %SystemRoot%\system32\drivers\etc\protocol file. If you do not have
55
+ read access to that file, the method will fail.
57
56
 
58
57
  == Known Bugs
59
- None that I'm aware of. Please log any bug reports on the project page
58
+ None that I'm aware of. Please log any bug reports on the project page
60
59
  at https://github.com/djberg96/net-proto.
61
60
 
62
61
  == Future Plans
@@ -64,7 +63,7 @@ Proto.getprotoent{ |struct| ... }
64
63
  WSAAsyncGetProtoByNumber) on MS Windows systems.
65
64
 
66
65
  == Copyright
67
- (C) 2003-2012 Daniel J. Berger
66
+ (C) 2003-2014 Daniel J. Berger
68
67
  All rights reserved.
69
68
 
70
69
  == Warranty
@@ -1,10 +1,12 @@
1
1
  require 'rbconfig'
2
2
 
3
3
  case RbConfig::CONFIG['host_os']
4
- when /linux/i
5
- require 'linux/net/proto'
6
- when /sunos|solaris/i
7
- require 'sunos/net/proto'
8
- else
9
- require 'generic/net/proto'
4
+ when /linux/i
5
+ require 'linux/net/proto'
6
+ when /sunos|solaris/i
7
+ require 'sunos/net/proto'
8
+ when /mingw|cygwin|win32|windows|mswin/i
9
+ require 'windows/net/proto'
10
+ else
11
+ require 'generic/net/proto'
10
12
  end
@@ -5,7 +5,7 @@ module Net
5
5
  extend FFI::Library
6
6
 
7
7
  # The version of the net-proto library
8
- VERSION = '1.1.0'
8
+ VERSION = '1.1.1'
9
9
 
10
10
  private_class_method :new
11
11
 
@@ -0,0 +1,123 @@
1
+ require 'net/proto/common'
2
+
3
+ # The Net module serves as a namespace only.
4
+ module Net
5
+ # The Proto class serves as the base class for the various protocol methods.
6
+ class Proto
7
+ extend FFI::Library
8
+
9
+ ffi_lib FFI::Library::LIBC
10
+ ffi_lib 'ws2_32'
11
+ ffi_convention :stdcall
12
+
13
+ private
14
+
15
+ # These should exist on every platform.
16
+ attach_function :getprotobyname_c, :getprotobyname, [:string], :pointer
17
+ attach_function :getprotobynumber_c, :getprotobynumber, [:int], :pointer
18
+
19
+ private_class_method :getprotobyname_c
20
+ private_class_method :getprotobynumber_c
21
+
22
+ public
23
+
24
+ # If given a protocol string, returns the corresponding number. If
25
+ # given a protocol number, returns the corresponding string.
26
+ #
27
+ # Returns nil if not found in either case.
28
+ #
29
+ # Examples:
30
+ #
31
+ # Net::Proto.get_protocol('tcp') # => 6
32
+ # Net::Proto.get_protocol(1) # => 'icmp'
33
+ #
34
+ def self.get_protocol(arg)
35
+ if arg.is_a?(String)
36
+ getprotobyname(arg)
37
+ else
38
+ getprotobynumber(arg)
39
+ end
40
+ end
41
+
42
+ # Given a protocol string, returns the corresponding number, or nil if
43
+ # not found.
44
+ #
45
+ # Examples:
46
+ #
47
+ # Net::Proto.getprotobyname('tcp') # => 6
48
+ # Net::Proto.getprotobyname('bogus') # => nil
49
+ #
50
+ def self.getprotobyname(protocol)
51
+ raise TypeError unless protocol.is_a?(String)
52
+
53
+ begin
54
+ ptr = getprotobyname_c(protocol)
55
+ struct = ProtocolStruct.new(ptr) unless ptr.null?
56
+ ensure
57
+ endprotoent() if respond_to?(:endprotoent, true)
58
+ end
59
+
60
+ ptr.null? ? nil : struct[:p_proto]
61
+ end
62
+
63
+ # Given a protocol number, returns the corresponding string, or nil if
64
+ # not found.
65
+ #
66
+ # Examples:
67
+ #
68
+ # Net::Proto.getprotobynumber(6) # => 'tcp'
69
+ # Net::Proto.getprotobynumber(999) # => nil
70
+ #
71
+ def self.getprotobynumber(protocol)
72
+ raise TypeError unless protocol.is_a?(Integer)
73
+
74
+ begin
75
+ ptr = getprotobynumber_c(protocol)
76
+ struct = ProtocolStruct.new(ptr) unless ptr.null?
77
+ ensure
78
+ endprotoent() if respond_to?(:endprotoent, true)
79
+ end
80
+
81
+ ptr.null? ? nil: struct[:p_name]
82
+ end
83
+
84
+ # In block form, yields each entry from /etc/protocol as a struct of type
85
+ # Proto::ProtoStruct. In non-block form, returns an array of structs.
86
+ #
87
+ # The fields are 'name' (a string), 'aliases' (an array of strings,
88
+ # though often only one element), and 'proto' (a number).
89
+ #
90
+ # Example:
91
+ #
92
+ # Net::Proto.getprotoent.each{ |prot|
93
+ # p prot.name
94
+ # p prot.aliases
95
+ # p prot.proto
96
+ # }
97
+ #
98
+ # Note that on Windows this code reads directly out of a %SystemRoot%
99
+ # subfolder using pure Ruby, so you will need read access or this method
100
+ # will fail.
101
+ #
102
+ def self.getprotoent
103
+ structs = block_given? ? nil : []
104
+ file = ENV['SystemRoot'] + '/system32/drivers/etc/protocol'
105
+
106
+ IO.foreach(file) do |line|
107
+ next if line.lstrip[0] == '#' # Skip comments
108
+ next if line.lstrip.size == 0 # Skip blank lines
109
+ line = line.split
110
+
111
+ ruby_struct = ProtoStruct.new(line[0], line[2].split(','), line[1].to_i).freeze
112
+
113
+ if block_given?
114
+ yield ruby_struct
115
+ else
116
+ structs << ruby_struct
117
+ end
118
+ end
119
+
120
+ structs
121
+ end
122
+ end
123
+ end
@@ -2,21 +2,20 @@ require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = 'net-proto'
5
- gem.version = '1.1.0'
5
+ gem.version = '1.1.1'
6
6
  gem.author = 'Daniel J. Berger'
7
7
  gem.license = 'Artistic 2.0'
8
8
  gem.email = 'djberg96@gmail.com'
9
- gem.homepage = 'http://www.rubyforge.org/projects/sysutils'
10
- gem.platform = Gem::Platform::RUBY
9
+ gem.homepage = 'https://github.com/djberg96/net-proto'
11
10
  gem.summary = 'A Ruby interface for determining protocol information'
12
11
  gem.test_file = 'test/test_net_proto.rb'
13
12
  gem.files = Dir['**/*'].reject{ |f| f.include?('git') }
14
13
 
15
- gem.rubyforge_project = 'sysutils'
16
14
  gem.extra_rdoc_files = ['CHANGES', 'README', 'MANIFEST', 'doc/netproto.txt']
17
15
 
18
16
  gem.add_dependency('ffi', '>= 1.0.0')
19
17
  gem.add_development_dependency('test-unit', '>= 2.2.0')
18
+ gem.add_development_dependency('rake')
20
19
 
21
20
  gem.description = <<-EOF
22
21
  The net-proto library provides an interface for get protocol information
@@ -4,10 +4,8 @@
4
4
  # Test suite for net-proto - all platforms. This test suite should be run
5
5
  # via the 'rake test' task.
6
6
  ###########################################################################
7
- require 'rubygems'
8
- gem 'test-unit'
9
7
  require 'net/proto'
10
- require 'test/unit'
8
+ require 'test-unit'
11
9
 
12
10
  class TC_Net_Proto < Test::Unit::TestCase
13
11
 
@@ -19,8 +17,6 @@ class TC_Net_Proto < Test::Unit::TestCase
19
17
  mobile ospf pim ipcomp vrrp sctp hopopt ipv6
20
18
  ipv6-route ipv6-frag esp ah ipv6-icmp ipv6-nonxt ipv6-opts
21
19
  /
22
-
23
- @@windows = File::ALT_SEPARATOR
24
20
  end
25
21
 
26
22
  def setup
@@ -28,7 +24,7 @@ class TC_Net_Proto < Test::Unit::TestCase
28
24
  end
29
25
 
30
26
  test "version number is set to expected value" do
31
- assert_equal('1.1.0', Net::Proto::VERSION)
27
+ assert_equal('1.1.1', Net::Proto::VERSION)
32
28
  end
33
29
 
34
30
  test "get_protocol method basic functionality" do
@@ -91,35 +87,30 @@ class TC_Net_Proto < Test::Unit::TestCase
91
87
  end
92
88
 
93
89
  test "getprotoent basic functionality" do
94
- omit_if(@@windows, 'getprotoent tests skipped on MS Windows')
95
90
  assert_respond_to(Net::Proto, :getprotoent)
96
91
  assert_nothing_raised{ Net::Proto.getprotoent }
97
92
  assert_kind_of(Array, Net::Proto.getprotoent)
98
93
  end
99
94
 
100
95
  test "getprotoent method returns the expected results" do
101
- omit_if(@@windows, 'getprotoent tests skipped on MS Windows')
102
96
  assert_kind_of(Struct::ProtoStruct, Net::Proto.getprotoent.first)
103
97
  assert_nil(Net::Proto.getprotoent{})
104
98
  end
105
99
 
106
100
  test "struct returned by getprotoent method contains the expected data" do
107
- omit_if(@@windows, 'getprotoent tests skipped on MS Windows')
108
101
  @protoent = Net::Proto.getprotoent.first
109
- assert_equal(['name', 'aliases', 'proto'], @protoent.members)
102
+ assert_equal([:name, :aliases, :proto], @protoent.members)
110
103
  assert_kind_of(String, @protoent.name)
111
104
  assert_kind_of(Array, @protoent.aliases)
112
105
  assert_kind_of(Integer, @protoent.proto)
113
106
  end
114
107
 
115
108
  test "all members of the aliases struct member are strings" do
116
- omit_if(@@windows, 'getprotoent tests on MS Windows')
117
109
  @protoent = Net::Proto.getprotoent.first
118
110
  assert_true(@protoent.aliases.all?{ |e| e.is_a?(String) })
119
111
  end
120
112
 
121
113
  test "struct returned by getprotoent method is frozen" do
122
- omit_if(@@windows, 'getprotoent tests skipped on MS Windows')
123
114
  @protoent = Net::Proto.getprotoent.first
124
115
  assert_true(@protoent.frozen?)
125
116
  end
metadata CHANGED
@@ -1,111 +1,107 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: net-proto
3
- version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease:
6
- segments:
7
- - 1
8
- - 1
9
- - 0
10
- version: 1.1.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Daniel J. Berger
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2012-01-18 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
11
+ date: 2014-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
21
14
  name: ffi
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
26
17
  - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 23
29
- segments:
30
- - 1
31
- - 0
32
- - 0
18
+ - !ruby/object:Gem::Version
33
19
  version: 1.0.0
34
20
  type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
27
+ - !ruby/object:Gem::Dependency
37
28
  name: test-unit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.0
34
+ type: :development
38
35
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
40
- none: false
41
- requirements:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
42
38
  - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 7
45
- segments:
46
- - 2
47
- - 2
48
- - 0
39
+ - !ruby/object:Gem::Version
49
40
  version: 2.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
50
48
  type: :development
51
- version_requirements: *id002
52
- description: " The net-proto library provides an interface for get protocol information\n by name or by number. It can also iterate over the list of protocol\n entries defined on your system.\n"
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: |2
56
+ The net-proto library provides an interface for get protocol information
57
+ by name or by number. It can also iterate over the list of protocol
58
+ entries defined on your system.
53
59
  email: djberg96@gmail.com
54
60
  executables: []
55
-
56
61
  extensions: []
57
-
58
- extra_rdoc_files:
62
+ extra_rdoc_files:
59
63
  - CHANGES
60
64
  - README
61
65
  - MANIFEST
62
66
  - doc/netproto.txt
63
- files:
67
+ files:
64
68
  - CHANGES
69
+ - MANIFEST
70
+ - README
71
+ - Rakefile
65
72
  - doc/netproto.txt
66
73
  - examples/example_net_proto.rb
67
74
  - lib/generic/net/proto.rb
68
75
  - lib/linux/net/proto.rb
69
- - lib/net/proto/common.rb
70
76
  - lib/net/proto.rb
77
+ - lib/net/proto/common.rb
71
78
  - lib/sunos/net/proto.rb
72
- - MANIFEST
79
+ - lib/windows/net/proto.rb
73
80
  - net-proto.gemspec
74
- - Rakefile
75
- - README
76
81
  - test/test_net_proto.rb
77
- homepage: http://www.rubyforge.org/projects/sysutils
78
- licenses:
82
+ homepage: https://github.com/djberg96/net-proto
83
+ licenses:
79
84
  - Artistic 2.0
85
+ metadata: {}
80
86
  post_install_message:
81
87
  rdoc_options: []
82
-
83
- require_paths:
88
+ require_paths:
84
89
  - lib
85
- required_ruby_version: !ruby/object:Gem::Requirement
86
- none: false
87
- requirements:
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
88
92
  - - ">="
89
- - !ruby/object:Gem::Version
90
- hash: 3
91
- segments:
92
- - 0
93
- version: "0"
94
- required_rubygems_version: !ruby/object:Gem::Requirement
95
- none: false
96
- requirements:
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
97
  - - ">="
98
- - !ruby/object:Gem::Version
99
- hash: 3
100
- segments:
101
- - 0
102
- version: "0"
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
103
100
  requirements: []
104
-
105
- rubyforge_project: sysutils
106
- rubygems_version: 1.8.10
101
+ rubyforge_project:
102
+ rubygems_version: 2.4.1
107
103
  signing_key:
108
- specification_version: 3
104
+ specification_version: 4
109
105
  summary: A Ruby interface for determining protocol information
110
- test_files:
106
+ test_files:
111
107
  - test/test_net_proto.rb