datalink-socket 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,52 +2,51 @@
2
2
 
3
3
  To receive and send ethernet frames using ruby.
4
4
 
5
- * Datalink::Socket class
6
- * Ethernet::Socket class < Datalink::Socket
7
- * Ethernet::Frame class
8
- * Arp class
5
+ * A Socket class
6
+ * A Frame class
7
+ * A Arp class
9
8
 
10
9
  == Arp
11
10
 
12
- > Arp.methods false
11
+ > Arp.methods false
13
12
  => [:new_request, :new_reply]
14
13
 
15
- > Arp.new_request :hw_src=> '00:50:56:c0:00:01',
14
+ > Arp.new_request :hw_src=> '00:50:56:c0:00:01',
16
15
  :proto_src=> '192.168.1.10',
17
16
  :proto_tgt=> '192.168.1.13'
18
17
  => ARP, Request who-has 192.168.1.13 tell 192.168.1.10
19
18
 
20
19
  == Frame
21
20
 
21
+ > s = Ethernet::Socket.new 'en1'
22
22
  > data, _ = s.recv
23
- =>
24
23
  > frame = Ethernet::Frame.new data
25
- => #<Ethernet::Frame:0x00000100c94b08 ... >
26
24
  > frame.src
27
- => 00:24:b2:51:a8:8e
25
+ => 00:24:b2:51:a8:8e
28
26
  > frame.dst
29
- => 00:1f:5b:ce:bd:6a
27
+ => 00:1f:5b:ce:bd:6a
30
28
  > frame.ether_type
31
- => 2048
32
- > frame.payload.unpack('H*')
33
- => ["45200034d5ae400035061ad .... 08195fba"]
34
-
29
+ => 2048
30
+ > frame.payload.unpack('H*')[0]
31
+ => "45200034d5ae400035061ad .... 08195fba"
35
32
 
36
33
  == Receiving
37
34
 
38
- require 'ethernet'
39
- s = Ethernet::Socket.new "en1"
40
- s.open
41
- s.recv
42
-
35
+ require 'ethernet'
36
+ s = Ethernet::Socket.new "en1"
37
+ s.open
38
+ s.recv
43
39
 
44
40
  == Sending
45
41
 
46
- request = Arp.new_request :hw_src=> '00:50:56:c0:00:01',
47
- :proto_src=> '192.168.1.10',
48
- :proto_tgt=> '192.168.1.13'
49
42
 
50
- s.send request
43
+ request = Arp.new_request :hw_src=> '00:50:56:c0:00:01',
44
+ :proto_src=> '192.168.1.10',
45
+ :proto_tgt=> '192.168.1.13'
46
+
47
+ # send method checks if the arg passed 'respond_to? :encode'
48
+ # and sends its encoded representation when it does.
49
+ s.send request
51
50
 
52
51
  = Installation
53
52
 
data/lib/arp.rb CHANGED
@@ -1,3 +1,6 @@
1
+ #
2
+ # Copyright (c) 2011 Jean-Michel Esnault. Released under the same license as Ruby
3
+ #
1
4
  require 'ethernet'
2
5
  require 'ipaddr'
3
6
 
@@ -34,12 +37,8 @@ class Arp
34
37
  end
35
38
 
36
39
  def parse(s)
37
- # p s.unpack('H*')
38
- # p s.size
39
40
  s.slice!(0,14) if s.size>32
40
41
  htype, ptype, hlen, plen, opcode = s.unpack('nnccn')
41
- # p htype
42
- # p ptype
43
42
  hw_src, proto_src, hw_tgt, proto_tgt = s[8..-1].unpack("a#{hlen}a#{plen}"*2)
44
43
  raise RuntimeError, "Unsupported Hardware Type" unless htype == 1 && ptype == 0x0800
45
44
  @hw_src = HwSrc.new(hw_src)
@@ -1,3 +1,7 @@
1
+ #
2
+ # Copyright (c) 2011 Jean-Michel Esnault. Released under the same license as Ruby
3
+ #
4
+
1
5
  module Datalink
2
6
 
3
7
  BIOCGBLEN = 0x40044266
@@ -1,5 +1,8 @@
1
- require 'socket'
1
+ #
2
+ # Copyright (c) 2011 Jean-Michel Esnault. Released under the same license as Ruby
3
+ #
2
4
 
5
+ require 'socket'
3
6
  module Datalink
4
7
  class Socket < ::Socket
5
8
 
@@ -1,14 +1,28 @@
1
+ #
2
+ # Copyright (c) 2011 Jean-Michel Esnault. Released under the same license as Ruby
3
+ #
4
+
1
5
  require 'ruby-ext'
2
6
  require 'dl_socket'
3
7
  require 'rubygems'
8
+ #--
4
9
  # require 'oui'
10
+ #++
5
11
 
6
12
  module Ethernet
7
13
  class Socket < Datalink::Socket
8
- def recv(*args)
9
- eth_frame = super
10
- eth_type = eth_frame.slice(12,2).unpack('n')[0]
11
- [eth_frame, eth_type]
14
+ case `uname`.chomp.downcase
15
+ when 'darwin'
16
+ def recv(*args)
17
+ eth_frame = super
18
+ eth_type = eth_frame.slice(12,2).unpack('n')[0]
19
+ [eth_frame, eth_type]
20
+ end
21
+ when 'linux'
22
+ def recv(*args)
23
+ eth_frame, eth_type, _ = super
24
+ [eth_frame, eth_type]
25
+ end
12
26
  end
13
27
  end
14
28
 
@@ -66,6 +80,7 @@ module Ethernet
66
80
  end
67
81
  end
68
82
 
83
+ #--
69
84
  # def to_s_oui
70
85
  # comp_id = Ethernet::OUI.company_id_from_arr(@mac[0..2])
71
86
  # if comp_id == 'Unknown'
@@ -77,6 +92,7 @@ module Ethernet
77
92
  # s.join('_')
78
93
  # end
79
94
  # end
95
+ #++
80
96
 
81
97
  def encode
82
98
  @mac.pack('C*')
@@ -1,3 +1,6 @@
1
+ #
2
+ # Copyright (c) 2011 Jean-Michel Esnault. Released under the same license as Ruby
3
+ #
1
4
 
2
5
  class Object
3
6
  def to_shex(*args)
@@ -1,5 +1,4 @@
1
1
  require "test/unit"
2
-
3
2
  require "ethernet"
4
3
 
5
4
  class TestEthernetIeAddress < Test::Unit::TestCase
@@ -23,6 +22,7 @@ class TestEthernetIeAddress < Test::Unit::TestCase
23
22
  assert_equal Ethernet::Address, Ethernet::Address.new(['010203040506'].pack('H*')).class
24
23
  assert_equal '010203040506', Ethernet::Address.new(['010203040506'].pack('H*')).to_shex
25
24
  end
25
+ #--
26
26
  # def test_to_s_oui
27
27
  # assert_equal 'Apple_d8:93:a4', Ethernet::Address.new('00:1f:f3:d8:93:a4').to_s_oui
28
28
  # assert_equal 'Dell_01:3e:3d', Ethernet::Address.new('00:21:9b:01:3e:3d').to_s_oui
@@ -30,6 +30,7 @@ class TestEthernetIeAddress < Test::Unit::TestCase
30
30
  # assert_equal 'Intel_40:b8:8f', Ethernet::Address.new('0:2:b3:40:b8:8f').to_s_oui
31
31
  # assert_equal 'Cisco_fd:4f:6c', Ethernet::Address.new('00b0.64fd.4f6c').to_s_oui
32
32
  # end
33
+ #++
33
34
  end
34
35
 
35
36
  class TestEthernetType < Test::Unit::TestCase
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jean-Michel Esnault
@@ -24,8 +24,8 @@ executables: []
24
24
 
25
25
  extensions: []
26
26
 
27
- extra_rdoc_files: []
28
-
27
+ extra_rdoc_files:
28
+ - README.rdoc
29
29
  files:
30
30
  - README.rdoc
31
31
  - lib/arp.rb
@@ -34,8 +34,6 @@ files:
34
34
  - lib/dl_socket_linux.rb
35
35
  - lib/ethernet.rb
36
36
  - lib/ruby-ext.rb
37
- - test/arp_test.rb
38
- - test/ethernet_test.rb
39
37
  has_rdoc: true
40
38
  homepage: http://github.com/jesnault/dl
41
39
  licenses: []
@@ -49,7 +47,6 @@ rdoc_options:
49
47
  require_paths:
50
48
  - lib
51
49
  required_ruby_version: !ruby/object:Gem::Requirement
52
- none: false
53
50
  requirements:
54
51
  - - ">="
55
52
  - !ruby/object:Gem::Version
@@ -59,7 +56,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
56
  - 6
60
57
  version: 1.8.6
61
58
  required_rubygems_version: !ruby/object:Gem::Requirement
62
- none: false
63
59
  requirements:
64
60
  - - ">="
65
61
  - !ruby/object:Gem::Version
@@ -69,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
65
  requirements: []
70
66
 
71
67
  rubyforge_project:
72
- rubygems_version: 1.3.7
68
+ rubygems_version: 1.3.6
73
69
  signing_key:
74
70
  specification_version: 3
75
71
  summary: Datalink Socket