lib-bootp 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cff1a108551607985c80a8b032204b539f6534f9
4
+ data.tar.gz: '031192352b530dfa03e1b8d70304d5c588693739'
5
+ SHA512:
6
+ metadata.gz: 9ce33dfc7523edf9464eca445d855afe33efbf270ea0a9455234f3cf51db9b0d286042efc6a3ddb7979c2ad5fddc59a45d5921fb94abe6dd19e944553b05821b
7
+ data.tar.gz: a92c15c0f4f6775557b667cf2a5f2c1dac3d895c97c86024d25649e85080e330eff0ef4e1bac165db80f52ae6e7da6986e5f639bcd044e962c79432b85d99dc4
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .idea/
13
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.16.0
6
+ deploy:
7
+ provider: rubygems
8
+ api_key:
9
+ secure: PmmHPxfXZEJ4Tr0clcWhO6+r70CNeOVg44V3bBuIp9OGC6scyaqnwrzn1SMloI8r7Hi6g2OB58Q4XgiC5omU7EjArdt1gP0lI1CcXZcU5dyHn+egYsmr0gyW//yWU36xD10/mtmJo920Waei0Le6R0x22TMAO0i3V+2S2vnT99fuMxSaL3pYoMehLssdqhrFVa6Yy6/HW/9+2T9N7EaF4yFqxttVLnoDHxp8xOwWMD32x4mn3+xjuTyVH6X2XJf6NGXU+zZ+6STuyXEkOVhP+x69pbQzsosSlx0R8viX5UcNZxcucbB4t4Zu6Da9Irfba7272mghqxhwZuEZ6x5NsjG7UEV6bnGPpBC5UcqFrSctqAlwNT71t6QiYYS7LTwGHzVOdC6hV/l5FmAKWlh/ijtaip5XTtC0muA+gO6nKupw2vSXYuVqQmbu/Bm5X9CyJusz3efCBEdf9aUO4gZJlNuYxYN+yAetAK0zh1IvSqZo1z+7aVECRwDxxzIhJXMhtjNPHTNPXBf+qO01BHgSNVWq/eU9PZB+AkH3RudnXcKRVi9fNAXyy3fiOzjc5xCWFYs4Q5eeR3Sp/CtqLjeNgCSL1PROrS3Oy9kJ+iK+8oSouHtBuAvU1YEV5iVEA6LzoOYhTdFlBYRWyJfDls2+xo2a6Hq06B+P4+6ZJXHqApk=
10
+ gemspec: lib-bootp.gemspec
11
+ gem: lib-bootp
12
+ on:
13
+ tags: true
14
+ repo: pwojcieszonek/lib-bootp
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at piotr.wojcieszonek@tkk.net.pl. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in lib-bootp.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Piotr Wojcieszonek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Lib::BOOTP
2
+
3
+ BOOT Library written in ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'lib-bootp'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install lib-bootp
20
+
21
+ ## Contributing
22
+
23
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pwojcieszonek/lib-bootp. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
24
+
25
+ ## License
26
+
27
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
28
+
29
+ ## Code of Conduct
30
+
31
+ Everyone interacting in the Lib::Bootp project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lib-bootp/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lib/bootp"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,39 @@
1
+ # Author: Piotr Wojcieszonek
2
+ # e-mail: piotr@wojcieszonek.pl
3
+ # Copyright 18.03.2016 by Piotr Wojcieszonek
4
+
5
+ require 'forwardable'
6
+ #require 'comparable'
7
+
8
+ module Lib
9
+ module BOOTP
10
+ class Packet
11
+ class BootFile
12
+
13
+ extend Forwardable
14
+ include Comparable
15
+
16
+ def_delegator :@file, :to_s
17
+
18
+ def initialize(file = '.')
19
+ raise ArgumentError, "Can't convert #{file.class.name} to String" unless file.respond_to? :to_s
20
+ raise ArgumentError, "Given Server Host Name is to long: #{file}" if file.to_s.size > 128
21
+ @file = file.to_s
22
+ end
23
+
24
+ def <=>(other)
25
+ self.to_s <=> other.to_s
26
+ end
27
+
28
+ def pack
29
+ [@file].pack('a128')
30
+ end
31
+
32
+ def self.unpack(packet)
33
+ new packet.unpack('a128').first.to_s
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,48 @@
1
+ # Author: Piotr Wojcieszonek
2
+ # e-mail: piotr@wojcieszonek.pl
3
+ # Copyright 18.03.2016 by Piotr Wojcieszonek
4
+
5
+ #require 'comparable'
6
+
7
+ module Lib
8
+ module BOOTP
9
+ class Packet
10
+ class ClientHardwareAddress
11
+
12
+ include Comparable
13
+
14
+ def initialize(chaddr)
15
+ raise ArgumentError, "Can't convert #{chaddr.class.name} to String" unless chaddr.respond_to? :to_s
16
+ clear_chaddr = chaddr.to_s.gsub(/([:\-.,])/, '').to_s
17
+ raise ArgumentError, "Given CHADDR is to long: #{chaddr}" if clear_chaddr.size > 32
18
+ @chaddr = [clear_chaddr.ljust(32, '0')].pack('H32').unpack('C*')
19
+ end
20
+
21
+ def to_s(len = nil)
22
+ unless len.nil? || len.is_a?(Integer)
23
+ raise ArgumentError "Len must be a Integer or nil, #{len.class.name} given"
24
+ end
25
+ raise ArgumentError "Len out of range: #{len}" if len.to_i > 16 || len.to_i < 0
26
+ (len ? @chaddr[0..(len - 1)] : @chaddr).map{ |item| item.to_s(16).rjust(2, '0') }.join(':')
27
+ end
28
+
29
+ def <=>(other)
30
+ clear_other = other.to_s.gsub(/([:\-.,])/, '').to_s
31
+ @chaddr <=> [clear_other.ljust(32, '0')].pack('H32').unpack('C*')
32
+ end
33
+
34
+ def to_a
35
+ @chaddr
36
+ end
37
+
38
+ def pack
39
+ [@chaddr.map{ |item| item.to_s(16).rjust(2, '0') }.join('')].pack('H32')
40
+ end
41
+
42
+ def self.unpack(packet)
43
+ new packet.unpack('H32').first.to_s
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,78 @@
1
+ #Author: Piotr Wojcieszonek
2
+ #e-mail: piotr@wojcieszonek.pl
3
+ # Copyright 17.03.2016 by Piotr Wojcieszonek
4
+
5
+ require 'forwardable'
6
+ #require 'comparable'
7
+
8
+ module Lib
9
+ module BOOTP
10
+ class Packet
11
+ class Flags
12
+
13
+ extend Forwardable
14
+ include Comparable
15
+
16
+ def_delegator :@flag, :to_i
17
+
18
+ def initialize(flags=0)
19
+ @flags = {}
20
+ @flag = 0
21
+ if flags.is_a? Integer
22
+ case flags
23
+ when 0x8000
24
+ @flags[:b] = 1
25
+ @flag = 0x8000
26
+ when 0
27
+ @flags[:b] = 0
28
+ @flag = 0
29
+ else
30
+ raise ArgumentError, "Unknown flags value #{flags}"
31
+ end
32
+ elsif flags.is_a? Symbol
33
+ case flags
34
+ when :broadcast
35
+ @flags[:b] = 1
36
+ @flag = 0x8000
37
+ when :unicast
38
+ @flags[:b] = 0
39
+ @flag = 0
40
+ else
41
+ raise ArgumentError, "Unknown flags value #{flags}"
42
+ end
43
+ else
44
+ raise ArgumentError, 'FLAGS must by an Integer or Symbol' unless flags.is_a?(Integer) or flags.is_a?(Symbol)
45
+ end
46
+ end
47
+
48
+ def <=>(other)
49
+ other = Lib::BOOTP::Packet::Flags.new(other) unless other.is_a?(Lib::BOOTP::Packet::Flags)
50
+ self.to_i <=> other.to_i
51
+ end
52
+
53
+ def broadcast?
54
+ @flags[:b] == 1 ? true : false
55
+ end
56
+
57
+ def broadcast
58
+ @flags[:b]
59
+ end
60
+
61
+ def to_s
62
+ "0x#{@flag.to_s(16)}"
63
+ end
64
+
65
+ def pack
66
+ [@flag.to_i].pack('n')
67
+ end
68
+
69
+ def self.unpack(flags)
70
+ self.new flags.unpack('n').first
71
+ end
72
+
73
+ alias_method :to_hex, :to_s
74
+
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,37 @@
1
+ #Author: Piotr Wojcieszonek
2
+ #e-mail: piotr@wojcieszonek.pl
3
+ # Copyright 15.03.2016 by Piotr Wojcieszonek
4
+
5
+ require 'forwardable'
6
+ #require 'comparable'
7
+
8
+ module Lib
9
+ module BOOTP
10
+ class Packet
11
+ class HardwareAddressLength
12
+
13
+ extend Forwardable
14
+ include Comparable
15
+
16
+ def_delegators :@hlen, :to_s, :to_i
17
+
18
+
19
+ def initialize(hlen=6)
20
+ @hlen = hlen.to_i
21
+ end
22
+
23
+ def pack
24
+ [@hlen.to_i].pack('C')
25
+ end
26
+
27
+ def <=>(other)
28
+ self.to_i <=> other.to_i
29
+ end
30
+
31
+ def self.unpack(hlen)
32
+ self.new hlen.unpack('C').first
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,77 @@
1
+ #Author: Piotr Wojcieszonek
2
+ #e-mail: piotr@wojcieszonek.pl
3
+ # Copyright 14.03.2016 by Piotr Wojcieszonek
4
+
5
+ require 'forwardable'
6
+ #require 'comparable'
7
+
8
+ module Lib
9
+ module BOOTP
10
+ class Packet
11
+ class HardwareAddressType
12
+
13
+ extend Forwardable
14
+ include Comparable
15
+
16
+ def_delegator :@htype, :to_i
17
+
18
+ def initialize(htype=1)
19
+ raise ArgumentError, "Hardware address type out of range : #{htype}" unless (0..12).include? htype
20
+ @htype = htype
21
+ end
22
+
23
+ def to_s
24
+ case @htype
25
+ when 0
26
+ 'Lease Query'
27
+ when 1
28
+ 'Ethernet (10Mb)'
29
+ when 2
30
+ 'Experimental Ethernet (3Mb)'
31
+ when 3
32
+ 'Amateur Radio AX.25'
33
+ when 4
34
+ 'Proteon ProNET Token Ring'
35
+ when 5
36
+ 'Chaos'
37
+ when 6
38
+ 'IEEE 802 Networks'
39
+ when 7
40
+ 'ARCNET'
41
+ when 8
42
+ 'Hyperchannel'
43
+ when 9
44
+ 'Lanstar'
45
+ when 10
46
+ 'Autonet Short Address'
47
+ when 11
48
+ 'LocalTalk'
49
+ when 12
50
+ 'LocalNet (IBM PCNet or SYTEK LocalNET)'
51
+ else
52
+ raise ArgumentError, "Hardware address type out of range : #{@htype}"
53
+ end
54
+ end
55
+
56
+ def <=>(other)
57
+ case other
58
+ when String
59
+ self.to_s <=> other
60
+ when Integer
61
+ self.to_i <=> other
62
+ else
63
+ self.to_i <=> other.to_i
64
+ end
65
+ end
66
+
67
+ def pack
68
+ [@htype.to_i].pack('C')
69
+ end
70
+
71
+ def self.unpack(htype)
72
+ self.new htype.unpack('C').first
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,37 @@
1
+ # Author: Piotr Wojcieszonek
2
+ # e-mail: piotr@wojcieszonek.pl
3
+ # Copyright 15.03.2016 by Piotr Wojcieszonek
4
+
5
+ require 'forwardable'
6
+ #require 'comparable'
7
+
8
+ module Lib
9
+ module BOOTP
10
+ class Packet
11
+ class HopCount
12
+
13
+ extend Forwardable
14
+ include Comparable
15
+
16
+ def_delegators :@hops, :to_s, :to_i
17
+
18
+ def initialize(hops=0)
19
+ raise ArgumentError, "Hop Count out of range: #{hops}" if hops > 255
20
+ @hops = hops
21
+ end
22
+
23
+ def <=>(other)
24
+ self.to_i <=> other.to_i
25
+ end
26
+
27
+ def pack
28
+ [@hops.to_i].pack('C')
29
+ end
30
+
31
+ def self.unpack(hops)
32
+ self.new hops.unpack('C').first
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,45 @@
1
+ # Author: Piotr Wojcieszonek
2
+ # e-mail: piotr@wojcieszonek.pl
3
+ # Copyright 18.03.2016 by Piotr Wojcieszonek
4
+
5
+ require 'forwardable'
6
+ require 'ipaddr'
7
+ #require 'comparable'
8
+
9
+ module Lib
10
+ module BOOTP
11
+ class Packet
12
+ class IPAddress
13
+ extend Forwardable
14
+ include Comparable
15
+
16
+ def_delegators :@ip, :to_s, :to_i
17
+
18
+ def initialize(ip)
19
+ @ip = if ip.is_a?(IPAddr)
20
+ ip
21
+ else
22
+ raise ArgumentError, "Wrong IP Address #{ip}" if ip.nil? || ip == ''
23
+ IPAddr.new(ip, Socket::AF_INET)
24
+ end
25
+ raise ArgumentError, "Wrong IP Address #{ip}" unless @ip.is_a?(IPAddr)
26
+ end
27
+
28
+ def <=>(other)
29
+ other = IPAddr.new(other, Socket::AF_INET) unless other.is_a?(IPAddr)
30
+ self.to_i <=> other.to_i
31
+ end
32
+
33
+ def pack
34
+ @ip = 0 if @ip.nil?
35
+ [@ip.to_i].pack('N')
36
+ end
37
+
38
+ def self.unpack(ip)
39
+ new ip.to_s.unpack('N').first.to_i
40
+ end
41
+
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,46 @@
1
+ #Author: Piotr Wojcieszonek
2
+ #e-mail: piotr@wojcieszonek.pl
3
+ # Copyright 14.03.2016 by Piotr Wojcieszonek
4
+
5
+ require 'forwardable'
6
+ #require 'comparable'
7
+
8
+ module Lib
9
+ module BOOTP
10
+ class Packet
11
+ class OpCode
12
+
13
+ extend Forwardable
14
+ include Comparable
15
+
16
+ def_delegator :@op, :to_i
17
+
18
+ def initialize(op=1)
19
+ raise ArgumentError, "OP Code out of range : #{op}" unless [1,2].include? op.to_i
20
+ @op = op
21
+ end
22
+
23
+ def to_s
24
+ if @op == 1
25
+ 'BOOTREQUEST'
26
+ elsif @op == 2
27
+ 'BOOTREPLY'
28
+ end
29
+ end
30
+
31
+ def <=>(other)
32
+ return self.to_i <=> other if other.is_a?(Integer)
33
+ self.to_s <=> other.to_s.upcase
34
+ end
35
+
36
+ def pack
37
+ [@op.to_i].pack('C')
38
+ end
39
+
40
+ def self.unpack(op)
41
+ self.new op.unpack('C').first
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,37 @@
1
+ #Author: Piotr Wojcieszonek
2
+ #e-mail: piotr@wojcieszonek.pl
3
+ # Copyright 15.03.2016 by Piotr Wojcieszonek
4
+
5
+ require 'forwardable'
6
+ #require 'comparable'
7
+
8
+ module Lib
9
+ module BOOTP
10
+ class Packet
11
+ class Seconds
12
+
13
+ extend Forwardable
14
+ include Comparable
15
+
16
+ def_delegators :@secs, :to_s, :to_i
17
+
18
+ def initialize(secs=0)
19
+ raise ArgumentError, "Seconds count out of range #{secs}" if secs.to_i < 0 or secs.to_i > 0xFFFF
20
+ @secs = secs
21
+ end
22
+
23
+ def <=>(other)
24
+ self.to_i <=> other.to_i
25
+ end
26
+
27
+ def pack
28
+ [@secs.to_i].pack('n')
29
+ end
30
+
31
+ def self.unpack(secs)
32
+ self.new secs.unpack('n').first
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ #Author: Piotr Wojcieszonek
2
+ #e-mail: piotr@wojcieszonek.pl
3
+ # Copyright 18.03.2016 by Piotr Wojcieszonek
4
+
5
+ require 'forwardable'
6
+ #require 'comparable'
7
+
8
+ module Lib
9
+ module BOOTP
10
+ class Packet
11
+ class ServerHostName
12
+ extend Forwardable
13
+ include Comparable
14
+
15
+ def_delegator :@sname, :to_s
16
+
17
+ def initialize(sname='.')
18
+ raise ArgumentError, "Can't convert #{sname.class.name} to String" unless sname.respond_to? :to_s
19
+ raise ArgumentError, "Given Server Host Name is to long: #{sname}" if sname.to_s.size > 64
20
+ @sname = sname.to_s
21
+ end
22
+
23
+ def <=>(other)
24
+ self.to_s <=> other.to_s
25
+ end
26
+
27
+ def pack
28
+ [@sname].pack('a64')
29
+ end
30
+
31
+ def self.unpack(packet)
32
+ new packet.unpack('a64').first.to_s
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,59 @@
1
+ # Author: Piotr Wojcieszonek
2
+ # e-mail: piotr@wojcieszonek.pl
3
+ # Copyright 15.03.2016 by Piotr Wojcieszonek
4
+
5
+ require 'forwardable'
6
+ #require 'comparable'
7
+
8
+ module Lib
9
+ module BOOTP
10
+ class Packet
11
+ class TransactionID
12
+
13
+ extend Forwardable
14
+ include Comparable
15
+
16
+ def_delegators :@xid, :to_s, :to_i
17
+
18
+ def initialize(xid = nil)
19
+ xid = generate if xid.nil?
20
+ unless xid >= 0 && xid <= 0xFFFFFFFF
21
+ raise ArgumentError, "Not valid XID - #{xid} - should by 4 octet length"
22
+ end
23
+ @xid = xid
24
+ end
25
+
26
+ def <=>(other)
27
+ self.to_i <=> other.to_i
28
+ end
29
+
30
+ def self.generate
31
+ new Random.rand(0xFFFFFFFF)
32
+ end
33
+
34
+ def generate!
35
+ @xid = generate
36
+ self
37
+ end
38
+
39
+ def to_hex
40
+ @xid.to_s(16)
41
+ end
42
+
43
+ def pack
44
+ [@xid.to_i].pack('N')
45
+ end
46
+
47
+ def self.unpack(xid)
48
+ new xid.unpack('N').first
49
+ end
50
+
51
+ private
52
+
53
+ def generate
54
+ Random.rand(0xFFFFFFFF)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,5 @@
1
+ module Lib
2
+ module BOOTP
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
data/lib/lib/bootp.rb ADDED
@@ -0,0 +1,173 @@
1
+ # Author: Piotr Wojcieszonek
2
+ # e-mail: piotr@wojcieszonek.pl
3
+ # Copyright 18.03.2016 by Piotr Wojcieszonek
4
+
5
+ require 'lib/bootp/version'
6
+ require 'lib/bootp/packet/op_code'
7
+ require 'lib/bootp/packet/hardware_address_type'
8
+ require 'lib/bootp/packet/hardware_address_length'
9
+ require 'lib/bootp/packet/hop_count'
10
+ require 'lib/bootp/packet/transaction_id'
11
+ require 'lib/bootp/packet/seconds'
12
+ require 'lib/bootp/packet/flags'
13
+ require 'lib/bootp/packet/ip_address'
14
+ require 'lib/bootp/packet/server_host_name'
15
+ require 'lib/bootp/packet/boot_file'
16
+ require 'lib/bootp/packet/client_hardware_address'
17
+
18
+
19
+ module Lib
20
+ module BOOTP
21
+ class Packet
22
+
23
+ class << self
24
+ private
25
+
26
+ def hash_attr_reader(hash_name, *key_names)
27
+ key_names.each do |key_name|
28
+ define_method key_name do
29
+ instance_variable_get("@#{hash_name}")[key_name]
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+
36
+ hash_attr_reader :packets, :op, :htype, :hlen, :hops, :xid, :secs, :flags, :ciaddr, :yiaddr, :siaddr, :giaddr, :chaddr, :sname, :file
37
+
38
+ def initialize(op: 1, htype: 1, hlen: 6, hops: 0, xid: nil, secs: 0, flags: 0, ciaddr: 0, yiaddr: 0, siaddr: 0, giaddr: 0, chaddr: nil, sname: '.', file: '.')
39
+ @packets = {}
40
+ @packets[:op] = (op.is_a? Lib::BOOTP::Packet::OpCode) ? op : Lib::BOOTP::Packet::OpCode.new(op)
41
+ @packets[:htype] = (htype.is_a? Lib::BOOTP::Packet::HardwareAddressType) ? htype : Lib::BOOTP::Packet::HardwareAddressType.new(htype)
42
+ @packets[:hlen] = (hlen.is_a? Lib::BOOTP::Packet::HardwareAddressLength) ? hlen : Lib::BOOTP::Packet::HardwareAddressLength.new(hlen)
43
+ @packets[:hops] = (hops.is_a? Lib::BOOTP::Packet::HopCount) ? hops : Lib::BOOTP::Packet::HopCount.new(hops)
44
+ @packets[:xid] = (xid.is_a? Lib::BOOTP::Packet::TransactionID) ? xid : Lib::BOOTP::Packet::TransactionID.new(xid)
45
+ @packets[:secs] = (secs.is_a? Lib::BOOTP::Packet::Seconds) ? secs : Lib::BOOTP::Packet::Seconds.new(secs)
46
+ @packets[:flags] = (flags.is_a? Lib::BOOTP::Packet::Flags) ? flags : Lib::BOOTP::Packet::Flags.new(flags)
47
+ @packets[:ciaddr] = (ciaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? ciaddr : Lib::BOOTP::Packet::IPAddress.new(ciaddr)
48
+ @packets[:yiaddr] = (yiaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? yiaddr : Lib::BOOTP::Packet::IPAddress.new(yiaddr)
49
+ @packets[:siaddr] = (siaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? siaddr : Lib::BOOTP::Packet::IPAddress.new(siaddr)
50
+ @packets[:giaddr] = (giaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? giaddr : Lib::BOOTP::Packet::IPAddress.new(giaddr)
51
+ @packets[:chaddr] = (chaddr.is_a? Lib::BOOTP::Packet::ClientHardwareAddress) ? chaddr : Lib::BOOTP::Packet::ClientHardwareAddress.new(chaddr)
52
+ @packets[:sname] = (sname.is_a? Lib::BOOTP::Packet::ServerHostName) ? sname : Lib::BOOTP::Packet::ServerHostName.new(sname)
53
+ @packets[:file] = (file.is_a? Lib::BOOTP::Packet::BootFile) ? file : Lib::BOOTP::Packet::BootFile.new(file)
54
+ end
55
+
56
+ def op=(op)
57
+ @packets[:op] = (op.is_a? Lib::BOOTP::Packet::OpCode) ? op : Lib::BOOTP::Packet::OpCode.new(op)
58
+ end
59
+
60
+ def htype=(htype)
61
+ @packets[:htype] = (htype.is_a? Lib::BOOTP::Packet::HardwareAddressType) ? htype : Lib::BOOTP::Packet::HardwareAddressType.new(htype)
62
+ end
63
+
64
+ def hlen=(hlen)
65
+ @packets[:hlen] = (hlen.is_a? Lib::BOOTP::Packet::HardwareAddressLength) ? hlen : Lib::BOOTP::Packet::HardwareAddressLength.new(hlen)
66
+ end
67
+
68
+ def hops=(hops)
69
+ @packets[:hops] = (hops.is_a? Lib::BOOTP::Packet::HopCount) ? hops : Lib::BOOTP::Packet::HopCount.new(hops)
70
+ end
71
+
72
+ def xid=(xid)
73
+ @packets[:xid] = (xid.is_a? Lib::BOOTP::Packet::TransactionID) ? xid : Lib::BOOTP::Packet::TransactionID.new(xid)
74
+ end
75
+
76
+ def secs=(secs)
77
+ @packets[:secs] = (secs.is_a? Lib::BOOTP::Packet::Seconds) ? secs : Lib::BOOTP::Packet::Seconds.new(secs)
78
+ end
79
+
80
+ def flags=(flags)
81
+ @packets[:flags] = (flags.is_a? Lib::BOOTP::Packet::Flags) ? flags : Lib::BOOTP::Packet::Flags.new(flags)
82
+ end
83
+
84
+ def ciaddr=(ciaddr)
85
+ @packets[:ciaddr] = (ciaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? ciaddr : Lib::BOOTP::Packet::IPAddress.new(ciaddr)
86
+ end
87
+
88
+ def yiaddr=(yiaddr)
89
+ @packets[:yiaddr] = (yiaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? yiaddr : Lib::BOOTP::Packet::IPAddress.new(yiaddr)
90
+ end
91
+
92
+ def siaddr=(siaddr)
93
+ @packets[:siaddr] = (siaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? siaddr : Lib::BOOTP::Packet::IPAddress.new(siaddr)
94
+ end
95
+
96
+ def giaddr=(giaddr)
97
+ @packets[:giaddr] = (giaddr.is_a? Lib::BOOTP::Packet::IPAddress) ? giaddr : Lib::BOOTP::Packet::IPAddress.new(giaddr)
98
+ end
99
+
100
+ def chaddr=(chaddr)
101
+ @packets[:chaddr] = (chaddr.is_a? Lib::BOOTP::Packet::ClientHardwareAddress) ? chaddr : Lib::BOOTP::Packet::ClientHardwareAddress.new(chaddr)
102
+ end
103
+
104
+ def sname=(sname)
105
+ @packets[:sname] = (sname.is_a? Lib::BOOTP::Packet::ServerHostName) ? sname : Lib::BOOTP::Packet::ServerHostName.new(sname)
106
+ end
107
+
108
+ def file=(file)
109
+ @packets[:file] = (file.is_a? Lib::BOOTP::Packet::BootFile) ? file : Lib::BOOTP::Packet::BootFile.new(file)
110
+ end
111
+
112
+
113
+ def to_h
114
+ @packets
115
+ end
116
+
117
+ def to_s
118
+ "OP: #{self.op}"+
119
+ "\nHTYPE: #{self.htype}"+
120
+ "\nHLEN: #{self.hlen}"+
121
+ "\nHOPS: #{self.hops}"+
122
+ "\nXID: #{self.xid}"+
123
+ "\nSECS: #{self.secs}"+
124
+ "\nFLAGS: #{self.flags}"+
125
+ "\nCIADDR: #{self.ciaddr}"+
126
+ "\nYIADDR: #{self.yiaddr}"+
127
+ "\nSIADDR: #{self.siaddr}"+
128
+ "\nGIADDR: #{self.giaddr}"+
129
+ "\nCHADDR: #{self.chaddr}"+
130
+ "\nSNAME: #{self.sname}"+
131
+ "\nFILE: #{self.file}"
132
+ end
133
+
134
+ def pack
135
+ self.op.pack +
136
+ self.htype.pack +
137
+ self.hlen.pack +
138
+ self.hops.pack +
139
+ self.xid.pack +
140
+ self.secs.pack +
141
+ self.flags.pack +
142
+ self.ciaddr.pack +
143
+ self.yiaddr.pack +
144
+ self.siaddr.pack +
145
+ self.giaddr.pack +
146
+ self.chaddr.pack +
147
+ self.sname.pack +
148
+ self.file.pack
149
+ end
150
+
151
+ def self.unpack(packet)
152
+ op, htype, hlen, hops, xid, secs, flags, ciaddr, yiaddr, siaddr, giaddr, chaddr, sname, file = packet.unpack('C4NnnN4a16a64a128')
153
+ op = Lib::BOOTP::Packet::OpCode.new op
154
+ htype = Lib::BOOTP::Packet::HardwareAddressType.new htype
155
+ hlen = Lib::BOOTP::Packet::HardwareAddressLength.new hlen
156
+ hops = Lib::BOOTP::Packet::HopCount.new hops
157
+ xid = Lib::BOOTP::Packet::TransactionID.new xid
158
+ secs = Lib::BOOTP::Packet::Seconds.new secs
159
+ flags = Lib::BOOTP::Packet::Flags.new flags
160
+ ciaddr = Lib::BOOTP::Packet::IPAddress.new ciaddr
161
+ yiaddr = Lib::BOOTP::Packet::IPAddress.new yiaddr
162
+ siaddr = Lib::BOOTP::Packet::IPAddress.new siaddr
163
+ giaddr = Lib::BOOTP::Packet::IPAddress.new giaddr
164
+ chaddr = Lib::BOOTP::Packet::ClientHardwareAddress.unpack chaddr
165
+ sname = Lib::BOOTP::Packet::ServerHostName.unpack sname
166
+ file = Lib::BOOTP::Packet::BootFile.unpack file
167
+
168
+ self.new(op:op, htype:htype, hlen:hlen, hops:hops, xid:xid, secs:secs, flags:flags, ciaddr:ciaddr, yiaddr:yiaddr, siaddr:siaddr, giaddr:giaddr, chaddr:chaddr, sname:sname, file:file)
169
+ end
170
+
171
+ end
172
+ end
173
+ end
data/lib-bootp.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "lib/bootp/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "lib-bootp"
7
+ spec.version = Lib::BOOTP::VERSION
8
+ spec.authors = ["Piotr Wojcieszonek"]
9
+ spec.email = ["piotr@wojcieszonek.pl"]
10
+
11
+ spec.summary = %q{BOOTP protocol library}
12
+ spec.description = %q{Set of classes to low level handle the BOOTP protocol}
13
+ spec.homepage = "https://github.com/pwojcieszonek/lib-bootp"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lib-bootp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Wojcieszonek
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Set of classes to low level handle the BOOTP protocol
56
+ email:
57
+ - piotr@wojcieszonek.pl
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib-bootp.gemspec
73
+ - lib/lib/bootp.rb
74
+ - lib/lib/bootp/packet/boot_file.rb
75
+ - lib/lib/bootp/packet/client_hardware_address.rb
76
+ - lib/lib/bootp/packet/flags.rb
77
+ - lib/lib/bootp/packet/hardware_address_length.rb
78
+ - lib/lib/bootp/packet/hardware_address_type.rb
79
+ - lib/lib/bootp/packet/hop_count.rb
80
+ - lib/lib/bootp/packet/ip_address.rb
81
+ - lib/lib/bootp/packet/op_code.rb
82
+ - lib/lib/bootp/packet/seconds.rb
83
+ - lib/lib/bootp/packet/server_host_name.rb
84
+ - lib/lib/bootp/packet/transaction_id.rb
85
+ - lib/lib/bootp/version.rb
86
+ homepage: https://github.com/pwojcieszonek/lib-bootp
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.6.14
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: BOOTP protocol library
110
+ test_files: []