lib-dhcp 0.2.1 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fdd10e98acc7696e58820d6b1e40ce262a65c9a85a48d60229a5a9b9c2876b3b
4
- data.tar.gz: 739aff2909214a5dc49bf09d8169bf8f0fda1b4349bb67a1cd372502bc135950
3
+ metadata.gz: 150402c8072450972624ac6d7eff755cbbd2547d61e093a073e343e77ac0bdc9
4
+ data.tar.gz: 516249a023e90e21bf5b4b0c74010336b5f7ae6306972fb405d11a448aeee015
5
5
  SHA512:
6
- metadata.gz: 0465a4aa77a5fea5860de2676ffcce737ba494055db6fcda1296aa3b69c42fb412ff53f7477697834c55b8ca55bfb558d0b35691f0ce0fdafac0e0bae833b964
7
- data.tar.gz: c5705c5689e2155aff7225cdb8b7cd0cd8b57755366eb77ecff58264a7b26083b75b5bfdabd66ceba06be506578c4c8d3a04bd87c7aa57db3899856f4c3d2f93
6
+ metadata.gz: 6147efc2edabdbd47e50defc8833ac5fbc6268307cc8c75fe4a3c481fe36f4c319fd9714706a0d691c82be333961dfc8f7bdce136169674dfa4a60e3c60c9f41
7
+ data.tar.gz: 8b8c506ec46ccde05425d3b3511ff4a8dc53c8f59bdcff91644bf072ec9490fa95909ccbd53cf2de63fbb293544859307d19889b84b39acf3bfdc8c16d5ef5bb
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Ruby library to low level handle the DHCP protocol.
4
4
 
5
+ Lib::Dhcp provides tools for constructing, parsing, and manipulating DHCP packets, enabling developers to interact with
6
+ the DHCP protocol directly.
5
7
 
6
8
  ## Installation
7
9
 
@@ -19,10 +21,113 @@ Or install it yourself as:
19
21
 
20
22
  $ gem install lib-dhcp
21
23
 
24
+ ## Usage
25
+
26
+ Below are some examples of how to use the `lib-dhcp` library:
27
+
28
+ ### Creating a DHCP Packet
29
+
30
+ You can create a new DHCP Discovery packet:
31
+
32
+ ```ruby
33
+ require 'lib/dhcp'
34
+
35
+ # Create a new DHCP Discovery message
36
+ packet = Lib::DHCP::Message::Discover.new
37
+
38
+ # Print the generated packet
39
+ puts packet.to_s
40
+ ```
41
+
42
+ ### Parsing an Incoming DHCP Packet
43
+
44
+ To parse a binary DHCP packet:
45
+
46
+ ```ruby
47
+ require 'lib/dhcp'
48
+
49
+ # Binary data representing a DHCP packet
50
+ data = "\x01\x01\x06\x00\x8D\x9E2\xA4\x00\x00\x00\x00\x00\x00\x00\x00" \
51
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
52
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00.\x00\x00\x00\x00\x00\x00" \
53
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
54
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
55
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
56
+ "\x00\x00\x00\x00\x00\x00.\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
57
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
58
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
59
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
60
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
61
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
62
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
63
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00c\x82Sc5\x01" \
64
+ "\x01\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
65
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
66
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
67
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
68
+
69
+ # Parse the binary data into a Lib::DHCP::Message
70
+ packet = Lib::DHCP::Message.unpack(data)
71
+
72
+ # Access packet fields and options
73
+ puts "Transaction ID: #{packet.xid}"
74
+ puts "DHCP Message Type: #{packet.option53}"
75
+ ```
76
+
77
+ ### Working with DHCP Options
78
+
79
+ You can manipulate DHCP options easily:
80
+
81
+ ```ruby
82
+ require 'lib/dhcp'
83
+
84
+ packet = Lib::DHCP::Message::Request.new
85
+
86
+ # Add options
87
+ packet.options.add([51, 3600]) # Lease Time
88
+ packet.options.add([1, '255.255.255.0']) # Subnet mask
89
+ packet.option3 = '192.168.1.1' # Router (Gateway)
90
+
91
+ # Retrieve an option
92
+ lease_time = packet.option51 # Returns the lease time
93
+ puts "Lease Time: #{lease_time}s"
94
+ ```
95
+
96
+ ### Reading DHCP Data from a UDP Socket
97
+
98
+ You can listen for DHCP packets on a UDP socket:
99
+
100
+ ```ruby
101
+ require 'socket'
102
+ require 'lib/dhcp'
103
+
104
+ # Open UDP socket to listen on port 67 (DHCP Server Port)
105
+ socket = UDPSocket.new
106
+ socket.bind('0.0.0.0', 67)
107
+
108
+ puts "Listening for DHCP packets on port 67..."
109
+
110
+ loop do
111
+ # Receive data from the socket
112
+ data, _addr = socket.recvfrom(1024)
113
+
114
+ # Parse the received data
115
+ packet = Lib::DHCP::Message.unpack(data)
116
+
117
+ # Print information about the packet
118
+ puts "Received DHCP Packet:"
119
+ puts "Transaction ID: #{packet.xid}"
120
+ puts "Client IP: #{packet.ciaddr}"
121
+ puts "Your IP: #{packet.yiaddr}"
122
+ puts "DHCP Message Type: #{packet.options[53]}"
123
+ end
124
+ ```
22
125
 
23
126
  ## Contributing
24
127
 
25
- Bug reports and pull requests are welcome on GitHub at https://github.com/pwojcieszonek/lib-dhcp. 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.
128
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pwojcieszonek/lib-dhcp. This project is
129
+ intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
130
+ the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
26
131
 
27
132
  ## License
28
133
 
@@ -30,4 +135,5 @@ The gem is available as open source under the terms of the [MIT License](https:/
30
135
 
31
136
  ## Code of Conduct
32
137
 
33
- Everyone interacting in the Lib::Dhcp project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lib-dhcp/blob/master/CODE_OF_CONDUCT.md).
138
+ Everyone interacting in the Lib::Dhcp project’s codebases, issue trackers, chat rooms and mailing lists is expected to
139
+ follow the [code of conduct](https://github.com/pwojcieszonek/lib-dhcp/blob/master/CODE_OF_CONDUCT.md).
@@ -50,7 +50,9 @@ module Lib
50
50
  end
51
51
 
52
52
  def self.from_json(json)
53
- self.unpack(super(json).pack)
53
+ json = JSON.parse((json.is_a?(Hash) ? json.to_json : json), symbolize_names: true)
54
+ json[:options] = json[:options].reject { |option| option[:oid] == 255 || option[:oid] == 0 }
55
+ self.unpack(super(json.to_json).pack)
54
56
  end
55
57
 
56
58
  def self.unpack(packet)
@@ -1,5 +1,5 @@
1
1
  module Lib
2
2
  module DHCP
3
- VERSION = '0.2.1'
3
+ VERSION = '0.2.3'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lib-dhcp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Wojcieszonek