dnet-ffi 0.1.3

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ ref
7
+ mirror
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Eric Monti
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,37 @@
1
+ = dnet-ffi
2
+
3
+ Ruby FFI bindings for libdnet.
4
+
5
+ == REQUIREMENTS
6
+
7
+ * libdnet up to v1.12 - see http://libdnet.sourceforge.net or
8
+ http://code.google.com/p/libdnet
9
+
10
+ * Ruby ffi >= 0.5.0 - see http://github.com/ffi/ffi or http://rubyforge.org/projects/ffi/
11
+
12
+ * ffi_dry - see http://github.com/emonti/ffi_dry
13
+
14
+
15
+ == LICENSE
16
+
17
+ Copyright (c) 2009 Eric Monti
18
+
19
+ Permission is hereby granted, free of charge, to any person obtaining
20
+ a copy of this software and associated documentation files (the
21
+ "Software"), to deal in the Software without restriction, including
22
+ without limitation the rights to use, copy, modify, merge, publish,
23
+ distribute, sublicense, and/or sell copies of the Software, and to
24
+ permit persons to whom the Software is furnished to do so, subject to
25
+ the following conditions:
26
+
27
+ The above copyright notice and this permission notice shall be
28
+ included in all copies or substantial portions of the Software.
29
+
30
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37
+
data/Rakefile ADDED
@@ -0,0 +1,64 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'rake/clean'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "dnet-ffi"
10
+ gem.summary = %Q{Ruby FFI bindings for libdnet}
11
+ gem.description = %Q{Ruby FFI bindings for the libdnet raw network library}
12
+ gem.email = "emonti@matasano.com"
13
+ gem.homepage = "http://github.com/emonti/dnet-ffi"
14
+ gem.authors = ["Eric Monti"]
15
+ gem.files.delete_if {|f| f =~ /^mirror\//}
16
+ # gem.add_dependency "emonti-ffi_dry"
17
+ gem.add_development_dependency "rspec"
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_opts = ['--colour', '--format', 'nested']
28
+ spec.spec_files = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ task :spec => :check_dependencies
38
+
39
+ task :default => :spec
40
+
41
+
42
+ begin
43
+ require 'yard'
44
+ YARD::Rake::YardocTask.new
45
+ rescue LoadError
46
+ task :yardoc do
47
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
48
+ end
49
+ end
50
+
51
+ require 'rake/rdoctask'
52
+ Rake::RDocTask.new do |rdoc|
53
+ if File.exist?('VERSION')
54
+ version = File.read('VERSION')
55
+ else
56
+ version = ""
57
+ end
58
+
59
+ rdoc.rdoc_dir = 'rdoc'
60
+ rdoc.title = "dnet-ffi #{version}"
61
+ rdoc.rdoc_files.include('README*')
62
+ rdoc.rdoc_files.include('lib/**/*.rb')
63
+ end
64
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.3
data/dnet-ffi.gemspec ADDED
@@ -0,0 +1,99 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{dnet-ffi}
8
+ s.version = "0.1.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Eric Monti"]
12
+ s.date = %q{2009-09-17}
13
+ s.description = %q{Ruby FFI bindings for the libdnet raw network library}
14
+ s.email = %q{emonti@matasano.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "dnet-ffi.gemspec",
27
+ "lib/dnet.rb",
28
+ "lib/dnet/addr.rb",
29
+ "lib/dnet/arp.rb",
30
+ "lib/dnet/blob.rb",
31
+ "lib/dnet/bsd.rb",
32
+ "lib/dnet/constants.rb",
33
+ "lib/dnet/eth.rb",
34
+ "lib/dnet/fw.rb",
35
+ "lib/dnet/helpers.rb",
36
+ "lib/dnet/icmp.rb",
37
+ "lib/dnet/intf.rb",
38
+ "lib/dnet/ip.rb",
39
+ "lib/dnet/ip6.rb",
40
+ "lib/dnet/rand.rb",
41
+ "lib/dnet/route.rb",
42
+ "lib/dnet/tcp.rb",
43
+ "lib/dnet/tun.rb",
44
+ "lib/dnet/typedefs.rb",
45
+ "lib/dnet/udp.rb",
46
+ "lib/dnet/util.rb",
47
+ "samples/eth_send_raw.rb",
48
+ "samples/ifconfig-alike.rb",
49
+ "samples/udp_send_raw.rb",
50
+ "spec/addr_spec.rb",
51
+ "spec/arp_spec.rb",
52
+ "spec/blob_spec.rb",
53
+ "spec/bsd_spec.rb",
54
+ "spec/dnet-ffi_spec.rb",
55
+ "spec/eth_spec.rb",
56
+ "spec/fw_spec.rb",
57
+ "spec/intf_spec.rb",
58
+ "spec/ip6_spec.rb",
59
+ "spec/ip_spec.rb",
60
+ "spec/rand_spec.rb",
61
+ "spec/route_spec.rb",
62
+ "spec/spec_helper.rb",
63
+ "spec/tun_spec.rb"
64
+ ]
65
+ s.homepage = %q{http://github.com/emonti/dnet-ffi}
66
+ s.rdoc_options = ["--charset=UTF-8"]
67
+ s.require_paths = ["lib"]
68
+ s.rubygems_version = %q{1.3.4}
69
+ s.summary = %q{Ruby FFI bindings for libdnet}
70
+ s.test_files = [
71
+ "spec/addr_spec.rb",
72
+ "spec/arp_spec.rb",
73
+ "spec/blob_spec.rb",
74
+ "spec/bsd_spec.rb",
75
+ "spec/dnet-ffi_spec.rb",
76
+ "spec/eth_spec.rb",
77
+ "spec/fw_spec.rb",
78
+ "spec/intf_spec.rb",
79
+ "spec/ip6_spec.rb",
80
+ "spec/ip_spec.rb",
81
+ "spec/rand_spec.rb",
82
+ "spec/route_spec.rb",
83
+ "spec/spec_helper.rb",
84
+ "spec/tun_spec.rb"
85
+ ]
86
+
87
+ if s.respond_to? :specification_version then
88
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
89
+ s.specification_version = 3
90
+
91
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
92
+ s.add_development_dependency(%q<rspec>, [">= 0"])
93
+ else
94
+ s.add_dependency(%q<rspec>, [">= 0"])
95
+ end
96
+ else
97
+ s.add_dependency(%q<rspec>, [">= 0"])
98
+ end
99
+ end
data/lib/dnet.rb ADDED
@@ -0,0 +1,35 @@
1
+ begin
2
+ require 'rubygems'
3
+ rescue LoadError
4
+ end
5
+ require 'ffi'
6
+ require 'ffi/dry'
7
+
8
+ module Dnet
9
+ extend FFI::Library
10
+ ffi_lib 'libdnet'
11
+ end
12
+
13
+ require 'dnet/typedefs.rb'
14
+ require 'dnet/constants.rb' unless defined?(Dnet::DNET_CONSTANTS)
15
+
16
+ require 'dnet/util.rb'
17
+ require 'dnet/helpers.rb'
18
+
19
+ require 'dnet/bsd' # bsd sockaddr structs
20
+
21
+ require 'dnet/addr'
22
+ require 'dnet/eth'
23
+ require 'dnet/arp'
24
+ require 'dnet/ip'
25
+ require 'dnet/ip6'
26
+ require 'dnet/icmp'
27
+ require 'dnet/tcp.rb'
28
+ require 'dnet/udp.rb'
29
+ require 'dnet/intf'
30
+ require 'dnet/route'
31
+ require 'dnet/fw'
32
+ require 'dnet/tun'
33
+ require 'dnet/blob'
34
+ require 'dnet/rand'
35
+
data/lib/dnet/addr.rb ADDED
@@ -0,0 +1,204 @@
1
+ # Network addressing
2
+
3
+ module Dnet
4
+
5
+ # FFI mapping to dnet(3)'s 'addr' network address structure.
6
+ #
7
+ # dnet(3)'s network addresses are described by the following C structure:
8
+ #
9
+ # struct addr {
10
+ # uint16_t atype;
11
+ # uint16_t abits;
12
+ # union {
13
+ # eth_addr_t __eth;
14
+ # ip_addr_t __ip;
15
+ # ip6_addr_t __ip6;
16
+ #
17
+ # uint8_t __data8[16];
18
+ # uint16_t __data16[8];
19
+ # uint32_t __data32[4];
20
+ # } __addr_u;
21
+ # };
22
+ #
23
+ class Addr < ::FFI::Struct
24
+ include ::FFI::DRY::StructHelper
25
+
26
+ # A mapping of dnet address types
27
+ ADDR_TYPES = [ nil, :link, :inet, :inet6 ]
28
+
29
+ # struct addr { ... };
30
+ dsl_layout do
31
+ field :atype, :uint16
32
+ field :bits, :uint16
33
+ array :addr, [:uchar, 16]
34
+ end
35
+
36
+ # If passed a String argument (and only 1 arg), it will be parsed as
37
+ # an address using set_string().
38
+ #
39
+ # The fields :bits and :addr aren't allowed to be used together.
40
+ # Instead, just use :addr => 'n.n.n.n/bits'. See also set_string()
41
+ def initialize(*args)
42
+ if args.size == 1 and (str=args[0]).is_a? String
43
+ super()
44
+ self.aton(str)
45
+ else
46
+ super(*args)
47
+ end
48
+ end
49
+
50
+ # Overrides set_fields to reject :bits fields when :addr is supplied as
51
+ # well.
52
+ #
53
+ # Forces you to use :addr => 'x.x.x.x/nn' instead. Having both together
54
+ # leaves setting :bits up to chance depending on which hash key gets
55
+ # plucked first in set_fields() because of how ip_aton() works under
56
+ # the hood.
57
+ def set_fields(params=nil)
58
+ params ||= {}
59
+ if params[:bits] and params[:addr]
60
+ raise( ::ArgumentError,
61
+ "Don't use :addr and :bits fields together. "+
62
+ "Just use :addr => 'x.x.x.x/nn' where nn are the bits." )
63
+ else
64
+ super(params)
65
+ end
66
+ end
67
+
68
+ # Looks up this object's 'atype' member against ADDR_TYPES
69
+ # Returns a symbol for the type or nil a type is not found.
70
+ def addr_type; ADDR_TYPES[ self[:atype] ] ; end
71
+ alias lookup_atype addr_type
72
+
73
+ # Returns a human-readable network address from self. Uses dnet(3)'s
74
+ # addr_ntoa function under the hood.
75
+ def ntoa
76
+ Dnet.addr_ntoa(self)
77
+ end
78
+ alias string ntoa
79
+ alias addr ntoa
80
+
81
+ # Returns a new Addr object containing the broadcast address for the
82
+ # network specified in this object. Uses dnet(3)'s addr_bcast under the
83
+ # hood.
84
+ def bcast
85
+ bcast = self.class.new()
86
+ Dnet.addr_bcast(self, bcast)
87
+ return bcast
88
+ end
89
+ alias broadcast bcast
90
+
91
+ # Returns a new Addr object containing the network address for the
92
+ # network specified in this object. Uses dnet(3)'s addr_net under the
93
+ # hood.
94
+ def net
95
+ n = self.class.new()
96
+ Dnet.addr_net(self, n)
97
+ return n
98
+ end
99
+ alias network net
100
+
101
+ # Compare one Addr object against another. Uses dnet(3)'s addr_cmp
102
+ # under the hood.
103
+ #
104
+ # addr_cmp() compares network addresses a and b, returning an integer less
105
+ # than, equal to, or greater than zero if a is found, respectively, to be
106
+ # less than, equal to, or greater than b. Both addresses must be of the
107
+ # same address type.
108
+ def <=>(other)
109
+ raise "can only compare another #{self.class}" unless other.is_a? Addr
110
+ Dnet.addr_cmp(self, other)
111
+ end
112
+
113
+ # Converts an address (or hostname) from a string to network format
114
+ # storing the result in this object. Uses dnet(3)'s addr_pton under
115
+ # the hood.
116
+ def set_string(str)
117
+ return self if Dnet.addr_pton(str, self) == 0
118
+ end
119
+ alias aton set_string
120
+ alias pton set_string
121
+ alias from_string set_string
122
+ alias addr= set_string
123
+
124
+ # Convert an address from network format to a string and store the
125
+ # result in a destination buffer. This uses dnet(3)'s addr_ntop under
126
+ # the hood.
127
+ def ntop(buf_p, sz)
128
+ Dnet.addr_ntop(self, buf_p, sz)
129
+ end
130
+
131
+ # Returns a new Addr object from a string address. Hostnames work too.
132
+ def self.from_string(str)
133
+ new().set_string(str)
134
+ end
135
+
136
+ # Returns a new Addr object containing the broadcast address for the
137
+ # given string address.
138
+ def self.bcast(str)
139
+ return nil unless b=from_string(str)
140
+ b.bcast
141
+ end
142
+
143
+ # Returns a new Addr object containing the broadcast address for the
144
+ # given string address.
145
+ def self.net(str)
146
+ return nil unless n=from_string(str)
147
+ n.net
148
+ end
149
+ end
150
+
151
+ attach_function :addr_cmp, [Addr, Addr], :int
152
+ attach_function :addr_bcast, [Addr, Addr], :int
153
+ attach_function :addr_net, [Addr, Addr], :int
154
+ attach_function :addr_ntop, [Addr, :pointer, :size_t], :string
155
+ attach_function :addr_ntoa, [Addr], :string
156
+ attach_function :addr_pton, [:string, Addr], :int
157
+
158
+ # addr_aton is just an alias for addr_pton. eh, it's in the manpage...
159
+ class <<self ; alias_method :addr_aton, :addr_pton; end
160
+
161
+ ### Misc unused stuff from dnet(3):
162
+
163
+ # addr_ntos converts an address from network format to the appropriate
164
+ # struct sockaddr.
165
+ #
166
+ # int addr_ntos(const struct addr *a, struct sockaddr *sa);
167
+ #
168
+ attach_function :addr_ntos, [:pointer, :pointer], :int
169
+
170
+ # addr_ston converts an address from a struct sockaddr to network format.
171
+ #
172
+ # int addr_ston(const struct sockaddr *sa, struct addr *a);
173
+ #
174
+ attach_function :addr_ston, [:pointer, :pointer], :int
175
+
176
+ # addr_btos converts a network mask length to a network mask specified as
177
+ # a struct sockaddr.
178
+ #
179
+ # int addr_btos(uint16_t bits, struct sockaddr *sa);
180
+ #
181
+ attach_function :addr_btos, [:uint16, :pointer], :int
182
+
183
+ # addr_stob converts a network mask specified in a struct sockaddr to a
184
+ # network mask length.
185
+ #
186
+ # int addr_stob(const struct sockaddr *sa, uint16_t *bits);
187
+ #
188
+ attach_function :addr_stob, [:pointer, :pointer], :int
189
+
190
+ # addr_btom converts a network mask length to a network mask in network
191
+ # byte order.
192
+ #
193
+ # int addr_btom(uint16_t bits, void *mask, size_t size);
194
+ attach_function :addr_btom, [:uint16, :pointer, :size_t], :int
195
+
196
+ # addr_mtob converts a network mask in network byte order to a network
197
+ # mask length.
198
+ #
199
+ # int addr_mtob(const void *mask, size_t size, uint16_t *bits);
200
+ #
201
+ attach_function :addr_mtob, [:pointer, :size_t, :uint16], :int
202
+
203
+ end
204
+