lib-dhcp 0.2.1 → 0.2.2
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 +4 -4
- data/README.md +108 -2
- data/lib/lib/dhcp/messages/message.rb +2 -0
- data/lib/lib/dhcp/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 469107e2ffb835ce16363c3551c164b4599452f141f875d7e747608307f63fe6
|
4
|
+
data.tar.gz: 4daff9d9484b41dc9ff57707bb3a364546a499228d8b16e7910a3a219a57e4b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb49b3e6f14db18cfb548d1b4ab82d4e02ce46be31a8b2b5b222288780885dbe9fc487941171646c5972d16f9205e2191b39ad5cc458aa1d22b6c59756179b9a
|
7
|
+
data.tar.gz: bede9b42341af0dca0b8e5563700bad97f9dccb7f479a3f2f0755b2c3654c13479d5a165444a8921953c9e9850324eb0245bf3ac07591d4da475b2f4eff6072c
|
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
|
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
|
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).
|
data/lib/lib/dhcp/version.rb
CHANGED