├── LICENSE ├── README.md ├── client.go ├── client_linux_test.go ├── client_test.go ├── generatexid.go ├── generatexid_test.go ├── inetsock.go └── pktsock_linux.go /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License, version 2.0 2 | 3 | 1. Definitions 4 | 5 | 1.1. “Contributor” 6 | 7 | means each individual or legal entity that creates, contributes to the 8 | creation of, or owns Covered Software. 9 | 10 | 1.2. “Contributor Version” 11 | 12 | means the combination of the Contributions of others (if any) used by a 13 | Contributor and that particular Contributor’s Contribution. 14 | 15 | 1.3. “Contribution” 16 | 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. “Covered Software” 20 | 21 | means Source Code Form to which the initial Contributor has attached the 22 | notice in Exhibit A, the Executable Form of such Source Code Form, and 23 | Modifications of such Source Code Form, in each case including portions 24 | thereof. 25 | 26 | 1.5. “Incompatible With Secondary Licenses” 27 | means 28 | 29 | a. that the initial Contributor has attached the notice described in 30 | Exhibit B to the Covered Software; or 31 | 32 | b. that the Covered Software was made available under the terms of version 33 | 1.1 or earlier of the License, but not also under the terms of a 34 | Secondary License. 35 | 36 | 1.6. “Executable Form” 37 | 38 | means any form of the work other than Source Code Form. 39 | 40 | 1.7. “Larger Work” 41 | 42 | means a work that combines Covered Software with other material, in a separate 43 | file or files, that is not Covered Software. 44 | 45 | 1.8. “License” 46 | 47 | means this document. 48 | 49 | 1.9. “Licensable” 50 | 51 | means having the right to grant, to the maximum extent possible, whether at the 52 | time of the initial grant or subsequently, any and all of the rights conveyed by 53 | this License. 54 | 55 | 1.10. “Modifications” 56 | 57 | means any of the following: 58 | 59 | a. any file in Source Code Form that results from an addition to, deletion 60 | from, or modification of the contents of Covered Software; or 61 | 62 | b. any new file in Source Code Form that contains any Covered Software. 63 | 64 | 1.11. “Patent Claims” of a Contributor 65 | 66 | means any patent claim(s), including without limitation, method, process, 67 | and apparatus claims, in any patent Licensable by such Contributor that 68 | would be infringed, but for the grant of the License, by the making, 69 | using, selling, offering for sale, having made, import, or transfer of 70 | either its Contributions or its Contributor Version. 71 | 72 | 1.12. “Secondary License” 73 | 74 | means either the GNU General Public License, Version 2.0, the GNU Lesser 75 | General Public License, Version 2.1, the GNU Affero General Public 76 | License, Version 3.0, or any later versions of those licenses. 77 | 78 | 1.13. “Source Code Form” 79 | 80 | means the form of the work preferred for making modifications. 81 | 82 | 1.14. “You” (or “Your”) 83 | 84 | means an individual or a legal entity exercising rights under this 85 | License. For legal entities, “You” includes any entity that controls, is 86 | controlled by, or is under common control with You. For purposes of this 87 | definition, “control” means (a) the power, direct or indirect, to cause 88 | the direction or management of such entity, whether by contract or 89 | otherwise, or (b) ownership of more than fifty percent (50%) of the 90 | outstanding shares or beneficial ownership of such entity. 91 | 92 | 93 | 2. License Grants and Conditions 94 | 95 | 2.1. Grants 96 | 97 | Each Contributor hereby grants You a world-wide, royalty-free, 98 | non-exclusive license: 99 | 100 | a. under intellectual property rights (other than patent or trademark) 101 | Licensable by such Contributor to use, reproduce, make available, 102 | modify, display, perform, distribute, and otherwise exploit its 103 | Contributions, either on an unmodified basis, with Modifications, or as 104 | part of a Larger Work; and 105 | 106 | b. under Patent Claims of such Contributor to make, use, sell, offer for 107 | sale, have made, import, and otherwise transfer either its Contributions 108 | or its Contributor Version. 109 | 110 | 2.2. Effective Date 111 | 112 | The licenses granted in Section 2.1 with respect to any Contribution become 113 | effective for each Contribution on the date the Contributor first distributes 114 | such Contribution. 115 | 116 | 2.3. Limitations on Grant Scope 117 | 118 | The licenses granted in this Section 2 are the only rights granted under this 119 | License. No additional rights or licenses will be implied from the distribution 120 | or licensing of Covered Software under this License. Notwithstanding Section 121 | 2.1(b) above, no patent license is granted by a Contributor: 122 | 123 | a. for any code that a Contributor has removed from Covered Software; or 124 | 125 | b. for infringements caused by: (i) Your and any other third party’s 126 | modifications of Covered Software, or (ii) the combination of its 127 | Contributions with other software (except as part of its Contributor 128 | Version); or 129 | 130 | c. under Patent Claims infringed by Covered Software in the absence of its 131 | Contributions. 132 | 133 | This License does not grant any rights in the trademarks, service marks, or 134 | logos of any Contributor (except as may be necessary to comply with the 135 | notice requirements in Section 3.4). 136 | 137 | 2.4. Subsequent Licenses 138 | 139 | No Contributor makes additional grants as a result of Your choice to 140 | distribute the Covered Software under a subsequent version of this License 141 | (see Section 10.2) or under the terms of a Secondary License (if permitted 142 | under the terms of Section 3.3). 143 | 144 | 2.5. Representation 145 | 146 | Each Contributor represents that the Contributor believes its Contributions 147 | are its original creation(s) or it has sufficient rights to grant the 148 | rights to its Contributions conveyed by this License. 149 | 150 | 2.6. Fair Use 151 | 152 | This License is not intended to limit any rights You have under applicable 153 | copyright doctrines of fair use, fair dealing, or other equivalents. 154 | 155 | 2.7. Conditions 156 | 157 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in 158 | Section 2.1. 159 | 160 | 161 | 3. Responsibilities 162 | 163 | 3.1. Distribution of Source Form 164 | 165 | All distribution of Covered Software in Source Code Form, including any 166 | Modifications that You create or to which You contribute, must be under the 167 | terms of this License. You must inform recipients that the Source Code Form 168 | of the Covered Software is governed by the terms of this License, and how 169 | they can obtain a copy of this License. You may not attempt to alter or 170 | restrict the recipients’ rights in the Source Code Form. 171 | 172 | 3.2. Distribution of Executable Form 173 | 174 | If You distribute Covered Software in Executable Form then: 175 | 176 | a. such Covered Software must also be made available in Source Code Form, 177 | as described in Section 3.1, and You must inform recipients of the 178 | Executable Form how they can obtain a copy of such Source Code Form by 179 | reasonable means in a timely manner, at a charge no more than the cost 180 | of distribution to the recipient; and 181 | 182 | b. You may distribute such Executable Form under the terms of this License, 183 | or sublicense it under different terms, provided that the license for 184 | the Executable Form does not attempt to limit or alter the recipients’ 185 | rights in the Source Code Form under this License. 186 | 187 | 3.3. Distribution of a Larger Work 188 | 189 | You may create and distribute a Larger Work under terms of Your choice, 190 | provided that You also comply with the requirements of this License for the 191 | Covered Software. If the Larger Work is a combination of Covered Software 192 | with a work governed by one or more Secondary Licenses, and the Covered 193 | Software is not Incompatible With Secondary Licenses, this License permits 194 | You to additionally distribute such Covered Software under the terms of 195 | such Secondary License(s), so that the recipient of the Larger Work may, at 196 | their option, further distribute the Covered Software under the terms of 197 | either this License or such Secondary License(s). 198 | 199 | 3.4. Notices 200 | 201 | You may not remove or alter the substance of any license notices (including 202 | copyright notices, patent notices, disclaimers of warranty, or limitations 203 | of liability) contained within the Source Code Form of the Covered 204 | Software, except that You may alter any license notices to the extent 205 | required to remedy known factual inaccuracies. 206 | 207 | 3.5. Application of Additional Terms 208 | 209 | You may choose to offer, and to charge a fee for, warranty, support, 210 | indemnity or liability obligations to one or more recipients of Covered 211 | Software. However, You may do so only on Your own behalf, and not on behalf 212 | of any Contributor. You must make it absolutely clear that any such 213 | warranty, support, indemnity, or liability obligation is offered by You 214 | alone, and You hereby agree to indemnify every Contributor for any 215 | liability incurred by such Contributor as a result of warranty, support, 216 | indemnity or liability terms You offer. You may include additional 217 | disclaimers of warranty and limitations of liability specific to any 218 | jurisdiction. 219 | 220 | 4. Inability to Comply Due to Statute or Regulation 221 | 222 | If it is impossible for You to comply with any of the terms of this License 223 | with respect to some or all of the Covered Software due to statute, judicial 224 | order, or regulation then You must: (a) comply with the terms of this License 225 | to the maximum extent possible; and (b) describe the limitations and the code 226 | they affect. Such description must be placed in a text file included with all 227 | distributions of the Covered Software under this License. Except to the 228 | extent prohibited by statute or regulation, such description must be 229 | sufficiently detailed for a recipient of ordinary skill to be able to 230 | understand it. 231 | 232 | 5. Termination 233 | 234 | 5.1. The rights granted under this License will terminate automatically if You 235 | fail to comply with any of its terms. However, if You become compliant, 236 | then the rights granted under this License from a particular Contributor 237 | are reinstated (a) provisionally, unless and until such Contributor 238 | explicitly and finally terminates Your grants, and (b) on an ongoing basis, 239 | if such Contributor fails to notify You of the non-compliance by some 240 | reasonable means prior to 60 days after You have come back into compliance. 241 | Moreover, Your grants from a particular Contributor are reinstated on an 242 | ongoing basis if such Contributor notifies You of the non-compliance by 243 | some reasonable means, this is the first time You have received notice of 244 | non-compliance with this License from such Contributor, and You become 245 | compliant prior to 30 days after Your receipt of the notice. 246 | 247 | 5.2. If You initiate litigation against any entity by asserting a patent 248 | infringement claim (excluding declaratory judgment actions, counter-claims, 249 | and cross-claims) alleging that a Contributor Version directly or 250 | indirectly infringes any patent, then the rights granted to You by any and 251 | all Contributors for the Covered Software under Section 2.1 of this License 252 | shall terminate. 253 | 254 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user 255 | license agreements (excluding distributors and resellers) which have been 256 | validly granted by You or Your distributors under this License prior to 257 | termination shall survive termination. 258 | 259 | 6. Disclaimer of Warranty 260 | 261 | Covered Software is provided under this License on an “as is” basis, without 262 | warranty of any kind, either expressed, implied, or statutory, including, 263 | without limitation, warranties that the Covered Software is free of defects, 264 | merchantable, fit for a particular purpose or non-infringing. The entire 265 | risk as to the quality and performance of the Covered Software is with You. 266 | Should any Covered Software prove defective in any respect, You (not any 267 | Contributor) assume the cost of any necessary servicing, repair, or 268 | correction. This disclaimer of warranty constitutes an essential part of this 269 | License. No use of any Covered Software is authorized under this License 270 | except under this disclaimer. 271 | 272 | 7. Limitation of Liability 273 | 274 | Under no circumstances and under no legal theory, whether tort (including 275 | negligence), contract, or otherwise, shall any Contributor, or anyone who 276 | distributes Covered Software as permitted above, be liable to You for any 277 | direct, indirect, special, incidental, or consequential damages of any 278 | character including, without limitation, damages for lost profits, loss of 279 | goodwill, work stoppage, computer failure or malfunction, or any and all 280 | other commercial damages or losses, even if such party shall have been 281 | informed of the possibility of such damages. This limitation of liability 282 | shall not apply to liability for death or personal injury resulting from such 283 | party’s negligence to the extent applicable law prohibits such limitation. 284 | Some jurisdictions do not allow the exclusion or limitation of incidental or 285 | consequential damages, so this exclusion and limitation may not apply to You. 286 | 287 | 8. Litigation 288 | 289 | Any litigation relating to this License may be brought only in the courts of 290 | a jurisdiction where the defendant maintains its principal place of business 291 | and such litigation shall be governed by laws of that jurisdiction, without 292 | reference to its conflict-of-law provisions. Nothing in this Section shall 293 | prevent a party’s ability to bring cross-claims or counter-claims. 294 | 295 | 9. Miscellaneous 296 | 297 | This License represents the complete agreement concerning the subject matter 298 | hereof. If any provision of this License is held to be unenforceable, such 299 | provision shall be reformed only to the extent necessary to make it 300 | enforceable. Any law or regulation which provides that the language of a 301 | contract shall be construed against the drafter shall not be used to construe 302 | this License against a Contributor. 303 | 304 | 305 | 10. Versions of the License 306 | 307 | 10.1. New Versions 308 | 309 | Mozilla Foundation is the license steward. Except as provided in Section 310 | 10.3, no one other than the license steward has the right to modify or 311 | publish new versions of this License. Each version will be given a 312 | distinguishing version number. 313 | 314 | 10.2. Effect of New Versions 315 | 316 | You may distribute the Covered Software under the terms of the version of 317 | the License under which You originally received the Covered Software, or 318 | under the terms of any subsequent version published by the license 319 | steward. 320 | 321 | 10.3. Modified Versions 322 | 323 | If you create software not governed by this License, and you want to 324 | create a new license for such software, you may create and use a modified 325 | version of this License if you rename the license and remove any 326 | references to the name of the license steward (except to note that such 327 | modified license differs from this License). 328 | 329 | 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses 330 | If You choose to distribute Source Code Form that is Incompatible With 331 | Secondary Licenses under the terms of this version of the License, the 332 | notice described in Exhibit B of this License must be attached. 333 | 334 | Exhibit A - Source Code Form License Notice 335 | 336 | This Source Code Form is subject to the 337 | terms of the Mozilla Public License, v. 338 | 2.0. If a copy of the MPL was not 339 | distributed with this file, You can 340 | obtain one at 341 | http://mozilla.org/MPL/2.0/. 342 | 343 | If it is not possible or desirable to put the notice in a particular file, then 344 | You may include the notice in a location (such as a LICENSE file in a relevant 345 | directory) where a recipient would be likely to look for such a notice. 346 | 347 | You may add additional accurate notices of copyright ownership. 348 | 349 | Exhibit B - “Incompatible With Secondary Licenses” Notice 350 | 351 | This Source Code Form is “Incompatible 352 | With Secondary Licenses”, as defined by 353 | the Mozilla Public License, v. 2.0. 354 | 355 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | dhcp4client 2 | =========== 3 | 4 | Please use the [**v2** branch][v2] for all projects. 5 | 6 | [v2]: https://github.com/d2g/dhcp4client/tree/v2 7 | -------------------------------------------------------------------------------- /client.go: -------------------------------------------------------------------------------- 1 | package dhcp4client 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "hash/fnv" 7 | "math/rand" 8 | "net" 9 | "sync" 10 | "syscall" 11 | "time" 12 | 13 | "github.com/d2g/dhcp4" 14 | ) 15 | 16 | const ( 17 | MaxDHCPLen = 576 18 | ) 19 | 20 | type Client struct { 21 | hardwareAddr net.HardwareAddr //The HardwareAddr to send in the request. 22 | ignoreServers []net.IP //List of Servers to Ignore requests from. 23 | timeout time.Duration //Time before we timeout. 24 | broadcast bool //Set the Bcast flag in BOOTP Flags 25 | connection ConnectionInt //The Connection Method to use 26 | generateXID func([]byte) //Function Used to Generate a XID 27 | } 28 | 29 | //Abstracts the type of underlying socket used 30 | type ConnectionInt interface { 31 | Close() error 32 | Write(packet []byte) error 33 | ReadFrom() ([]byte, net.IP, error) 34 | SetReadTimeout(t time.Duration) error 35 | } 36 | 37 | func New(options ...func(*Client) error) (*Client, error) { 38 | c := Client{ 39 | timeout: time.Second * 10, 40 | broadcast: true, 41 | } 42 | 43 | err := c.SetOption(options...) 44 | if err != nil { 45 | return nil, err 46 | } 47 | 48 | if c.generateXID == nil { 49 | // https://tools.ietf.org/html/rfc2131#section-4.1 explains: 50 | // 51 | // A DHCP client MUST choose 'xid's in such a way as to minimize the chance 52 | // of using an 'xid' identical to one used by another client. 53 | // 54 | // Hence, seed a random number generator with the current time and hardware 55 | // address. 56 | h := fnv.New64() 57 | h.Write(c.hardwareAddr) 58 | seed := int64(h.Sum64()) + time.Now().Unix() 59 | rnd := rand.New(rand.NewSource(seed)) 60 | var rndMu sync.Mutex 61 | c.generateXID = func(b []byte) { 62 | rndMu.Lock() 63 | defer rndMu.Unlock() 64 | rnd.Read(b) 65 | } 66 | } 67 | 68 | //if connection hasn't been set as an option create the default. 69 | if c.connection == nil { 70 | conn, err := NewInetSock() 71 | if err != nil { 72 | return nil, err 73 | } 74 | c.connection = conn 75 | } 76 | 77 | return &c, nil 78 | } 79 | 80 | func (c *Client) SetOption(options ...func(*Client) error) error { 81 | for _, opt := range options { 82 | if err := opt(c); err != nil { 83 | return err 84 | } 85 | } 86 | return nil 87 | } 88 | 89 | func Timeout(t time.Duration) func(*Client) error { 90 | return func(c *Client) error { 91 | c.timeout = t 92 | return nil 93 | } 94 | } 95 | 96 | func IgnoreServers(s []net.IP) func(*Client) error { 97 | return func(c *Client) error { 98 | c.ignoreServers = s 99 | return nil 100 | } 101 | } 102 | 103 | func HardwareAddr(h net.HardwareAddr) func(*Client) error { 104 | return func(c *Client) error { 105 | c.hardwareAddr = h 106 | return nil 107 | } 108 | } 109 | 110 | func Broadcast(b bool) func(*Client) error { 111 | return func(c *Client) error { 112 | c.broadcast = b 113 | return nil 114 | } 115 | } 116 | 117 | func Connection(conn ConnectionInt) func(*Client) error { 118 | return func(c *Client) error { 119 | c.connection = conn 120 | return nil 121 | } 122 | } 123 | 124 | func GenerateXID(g func([]byte)) func(*Client) error { 125 | return func(c *Client) error { 126 | c.generateXID = g 127 | return nil 128 | } 129 | } 130 | 131 | //Close Connections 132 | func (c *Client) Close() error { 133 | if c.connection != nil { 134 | return c.connection.Close() 135 | } 136 | return nil 137 | } 138 | 139 | //Send the Discovery Packet to the Broadcast Channel 140 | func (c *Client) SendDiscoverPacket() (dhcp4.Packet, error) { 141 | discoveryPacket := c.DiscoverPacket() 142 | discoveryPacket.PadToMinSize() 143 | 144 | return discoveryPacket, c.SendPacket(discoveryPacket) 145 | } 146 | 147 | // TimeoutError records a timeout when waiting for a DHCP packet. 148 | type TimeoutError struct { 149 | Timeout time.Duration 150 | } 151 | 152 | func (te *TimeoutError) Error() string { 153 | return fmt.Sprintf("no DHCP packet received within %v", te.Timeout) 154 | } 155 | 156 | //Retreive Offer... 157 | //Wait for the offer for a specific Discovery Packet. 158 | func (c *Client) GetOffer(discoverPacket *dhcp4.Packet) (dhcp4.Packet, error) { 159 | start := time.Now() 160 | 161 | for { 162 | timeout := c.timeout - time.Since(start) 163 | if timeout < 0 { 164 | return dhcp4.Packet{}, &TimeoutError{Timeout: c.timeout} 165 | } 166 | 167 | c.connection.SetReadTimeout(timeout) 168 | readBuffer, source, err := c.connection.ReadFrom() 169 | if err != nil { 170 | if errno, ok := err.(syscall.Errno); ok && errno == syscall.EAGAIN { 171 | return dhcp4.Packet{}, &TimeoutError{Timeout: c.timeout} 172 | } 173 | return dhcp4.Packet{}, err 174 | } 175 | 176 | offerPacket := dhcp4.Packet(readBuffer) 177 | offerPacketOptions := offerPacket.ParseOptions() 178 | 179 | // Ignore Servers in my Ignore list 180 | for _, ignoreServer := range c.ignoreServers { 181 | if source.Equal(ignoreServer) { 182 | continue 183 | } 184 | 185 | if offerPacket.SIAddr().Equal(ignoreServer) { 186 | continue 187 | } 188 | } 189 | 190 | if len(offerPacketOptions[dhcp4.OptionDHCPMessageType]) < 1 || dhcp4.MessageType(offerPacketOptions[dhcp4.OptionDHCPMessageType][0]) != dhcp4.Offer || !bytes.Equal(discoverPacket.XId(), offerPacket.XId()) { 191 | continue 192 | } 193 | 194 | return offerPacket, nil 195 | } 196 | 197 | } 198 | 199 | //Send Request Based On the offer Received. 200 | func (c *Client) SendRequest(offerPacket *dhcp4.Packet) (dhcp4.Packet, error) { 201 | requestPacket := c.RequestPacket(offerPacket) 202 | requestPacket.PadToMinSize() 203 | 204 | return requestPacket, c.SendPacket(requestPacket) 205 | } 206 | 207 | //Retreive Acknowledgement 208 | //Wait for the offer for a specific Request Packet. 209 | func (c *Client) GetAcknowledgement(requestPacket *dhcp4.Packet) (dhcp4.Packet, error) { 210 | start := time.Now() 211 | 212 | for { 213 | timeout := c.timeout - time.Since(start) 214 | if timeout < 0 { 215 | return dhcp4.Packet{}, &TimeoutError{Timeout: c.timeout} 216 | } 217 | 218 | c.connection.SetReadTimeout(timeout) 219 | readBuffer, source, err := c.connection.ReadFrom() 220 | if err != nil { 221 | if errno, ok := err.(syscall.Errno); ok && errno == syscall.EAGAIN { 222 | return dhcp4.Packet{}, &TimeoutError{Timeout: c.timeout} 223 | } 224 | return dhcp4.Packet{}, err 225 | } 226 | 227 | acknowledgementPacket := dhcp4.Packet(readBuffer) 228 | acknowledgementPacketOptions := acknowledgementPacket.ParseOptions() 229 | 230 | // Ignore Servers in my Ignore list 231 | for _, ignoreServer := range c.ignoreServers { 232 | if source.Equal(ignoreServer) { 233 | continue 234 | } 235 | 236 | if acknowledgementPacket.SIAddr().Equal(ignoreServer) { 237 | continue 238 | } 239 | } 240 | 241 | if !bytes.Equal(requestPacket.XId(), acknowledgementPacket.XId()) || len(acknowledgementPacketOptions[dhcp4.OptionDHCPMessageType]) < 1 || (dhcp4.MessageType(acknowledgementPacketOptions[dhcp4.OptionDHCPMessageType][0]) != dhcp4.ACK && dhcp4.MessageType(acknowledgementPacketOptions[dhcp4.OptionDHCPMessageType][0]) != dhcp4.NAK) { 242 | continue 243 | } 244 | 245 | return acknowledgementPacket, nil 246 | } 247 | } 248 | 249 | //Send Decline to the received acknowledgement. 250 | func (c *Client) SendDecline(acknowledgementPacket *dhcp4.Packet) (dhcp4.Packet, error) { 251 | declinePacket := c.DeclinePacket(acknowledgementPacket) 252 | declinePacket.PadToMinSize() 253 | 254 | return declinePacket, c.SendPacket(declinePacket) 255 | } 256 | 257 | //Send a DHCP Packet. 258 | func (c *Client) SendPacket(packet dhcp4.Packet) error { 259 | return c.connection.Write(packet) 260 | } 261 | 262 | //Create Discover Packet 263 | func (c *Client) DiscoverPacket() dhcp4.Packet { 264 | messageid := make([]byte, 4) 265 | c.generateXID(messageid) 266 | 267 | packet := dhcp4.NewPacket(dhcp4.BootRequest) 268 | packet.SetCHAddr(c.hardwareAddr) 269 | packet.SetXId(messageid) 270 | packet.SetBroadcast(c.broadcast) 271 | 272 | packet.AddOption(dhcp4.OptionDHCPMessageType, []byte{byte(dhcp4.Discover)}) 273 | //packet.PadToMinSize() 274 | return packet 275 | } 276 | 277 | //Create Request Packet 278 | func (c *Client) RequestPacket(offerPacket *dhcp4.Packet) dhcp4.Packet { 279 | offerOptions := offerPacket.ParseOptions() 280 | 281 | packet := dhcp4.NewPacket(dhcp4.BootRequest) 282 | packet.SetCHAddr(c.hardwareAddr) 283 | 284 | packet.SetXId(offerPacket.XId()) 285 | packet.SetCIAddr(offerPacket.CIAddr()) 286 | packet.SetSIAddr(offerPacket.SIAddr()) 287 | 288 | packet.SetBroadcast(c.broadcast) 289 | packet.AddOption(dhcp4.OptionDHCPMessageType, []byte{byte(dhcp4.Request)}) 290 | packet.AddOption(dhcp4.OptionRequestedIPAddress, (offerPacket.YIAddr()).To4()) 291 | packet.AddOption(dhcp4.OptionServerIdentifier, offerOptions[dhcp4.OptionServerIdentifier]) 292 | 293 | return packet 294 | } 295 | 296 | //Create Request Packet For a Renew 297 | func (c *Client) RenewalRequestPacket(acknowledgement *dhcp4.Packet) dhcp4.Packet { 298 | messageid := make([]byte, 4) 299 | c.generateXID(messageid) 300 | 301 | acknowledgementOptions := acknowledgement.ParseOptions() 302 | 303 | packet := dhcp4.NewPacket(dhcp4.BootRequest) 304 | packet.SetCHAddr(acknowledgement.CHAddr()) 305 | 306 | packet.SetXId(messageid) 307 | packet.SetCIAddr(acknowledgement.YIAddr()) 308 | packet.SetSIAddr(acknowledgement.SIAddr()) 309 | 310 | packet.SetBroadcast(c.broadcast) 311 | packet.AddOption(dhcp4.OptionDHCPMessageType, []byte{byte(dhcp4.Request)}) 312 | packet.AddOption(dhcp4.OptionRequestedIPAddress, (acknowledgement.YIAddr()).To4()) 313 | packet.AddOption(dhcp4.OptionServerIdentifier, acknowledgementOptions[dhcp4.OptionServerIdentifier]) 314 | 315 | return packet 316 | } 317 | 318 | //Create Release Packet For a Release 319 | func (c *Client) ReleasePacket(acknowledgement *dhcp4.Packet) dhcp4.Packet { 320 | messageid := make([]byte, 4) 321 | c.generateXID(messageid) 322 | 323 | acknowledgementOptions := acknowledgement.ParseOptions() 324 | 325 | packet := dhcp4.NewPacket(dhcp4.BootRequest) 326 | packet.SetCHAddr(acknowledgement.CHAddr()) 327 | 328 | packet.SetXId(messageid) 329 | packet.SetCIAddr(acknowledgement.YIAddr()) 330 | 331 | packet.AddOption(dhcp4.OptionDHCPMessageType, []byte{byte(dhcp4.Release)}) 332 | packet.AddOption(dhcp4.OptionServerIdentifier, acknowledgementOptions[dhcp4.OptionServerIdentifier]) 333 | 334 | return packet 335 | } 336 | 337 | //Create Decline Packet 338 | func (c *Client) DeclinePacket(acknowledgement *dhcp4.Packet) dhcp4.Packet { 339 | messageid := make([]byte, 4) 340 | c.generateXID(messageid) 341 | 342 | acknowledgementOptions := acknowledgement.ParseOptions() 343 | 344 | packet := dhcp4.NewPacket(dhcp4.BootRequest) 345 | packet.SetCHAddr(acknowledgement.CHAddr()) 346 | packet.SetXId(messageid) 347 | 348 | packet.AddOption(dhcp4.OptionDHCPMessageType, []byte{byte(dhcp4.Decline)}) 349 | packet.AddOption(dhcp4.OptionRequestedIPAddress, (acknowledgement.YIAddr()).To4()) 350 | packet.AddOption(dhcp4.OptionServerIdentifier, acknowledgementOptions[dhcp4.OptionServerIdentifier]) 351 | 352 | return packet 353 | } 354 | 355 | //Lets do a Full DHCP Request. 356 | func (c *Client) Request() (bool, dhcp4.Packet, error) { 357 | discoveryPacket, err := c.SendDiscoverPacket() 358 | if err != nil { 359 | return false, discoveryPacket, err 360 | } 361 | 362 | offerPacket, err := c.GetOffer(&discoveryPacket) 363 | if err != nil { 364 | return false, offerPacket, err 365 | } 366 | 367 | requestPacket, err := c.SendRequest(&offerPacket) 368 | if err != nil { 369 | return false, requestPacket, err 370 | } 371 | 372 | acknowledgement, err := c.GetAcknowledgement(&requestPacket) 373 | if err != nil { 374 | return false, acknowledgement, err 375 | } 376 | 377 | acknowledgementOptions := acknowledgement.ParseOptions() 378 | if dhcp4.MessageType(acknowledgementOptions[dhcp4.OptionDHCPMessageType][0]) != dhcp4.ACK { 379 | return false, acknowledgement, nil 380 | } 381 | 382 | return true, acknowledgement, nil 383 | } 384 | 385 | //Renew a lease backed on the Acknowledgement Packet. 386 | //Returns Sucessfull, The AcknoledgementPacket, Any Errors 387 | func (c *Client) Renew(acknowledgement dhcp4.Packet) (bool, dhcp4.Packet, error) { 388 | renewRequest := c.RenewalRequestPacket(&acknowledgement) 389 | renewRequest.PadToMinSize() 390 | 391 | err := c.SendPacket(renewRequest) 392 | if err != nil { 393 | return false, renewRequest, err 394 | } 395 | 396 | newAcknowledgement, err := c.GetAcknowledgement(&renewRequest) 397 | if err != nil { 398 | return false, newAcknowledgement, err 399 | } 400 | 401 | newAcknowledgementOptions := newAcknowledgement.ParseOptions() 402 | if dhcp4.MessageType(newAcknowledgementOptions[dhcp4.OptionDHCPMessageType][0]) != dhcp4.ACK { 403 | return false, newAcknowledgement, nil 404 | } 405 | 406 | return true, newAcknowledgement, nil 407 | } 408 | 409 | //Release a lease backed on the Acknowledgement Packet. 410 | //Returns Any Errors 411 | func (c *Client) Release(acknowledgement dhcp4.Packet) error { 412 | release := c.ReleasePacket(&acknowledgement) 413 | release.PadToMinSize() 414 | 415 | return c.SendPacket(release) 416 | } 417 | -------------------------------------------------------------------------------- /client_linux_test.go: -------------------------------------------------------------------------------- 1 | package dhcp4client_test 2 | 3 | import ( 4 | "log" 5 | "net" 6 | "testing" 7 | "syscall" 8 | 9 | "github.com/d2g/dhcp4" 10 | "github.com/d2g/dhcp4client" 11 | ) 12 | 13 | //Example Client 14 | func Test_ExampleLinuxClient(test *testing.T) { 15 | var err error 16 | 17 | m, err := net.ParseMAC("08-00-27-00-A8-E8") 18 | if err != nil { 19 | log.Printf("MAC Error:%v\n", err) 20 | } 21 | 22 | //Create a connection to use 23 | c, err := dhcp4client.NewPacketSock(2) 24 | if err != nil { 25 | test.Error("Client Connection Generation:" + err.Error()) 26 | } 27 | defer c.Close() 28 | 29 | exampleClient, err := dhcp4client.New(dhcp4client.HardwareAddr(m), dhcp4client.Connection(c)) 30 | if err != nil { 31 | test.Fatalf("Error:%v\n", err) 32 | } 33 | defer exampleClient.Close() 34 | 35 | success := false 36 | 37 | discoveryPacket, err := exampleClient.SendDiscoverPacket() 38 | test.Logf("Discovery:%v\n", discoveryPacket) 39 | 40 | if err != nil { 41 | sc, ok := err.(syscall.Errno) 42 | if ok { 43 | //Don't report a network down 44 | if sc != syscall.ENETDOWN { 45 | test.Fatalf("Discovery Error:%v\n", err) 46 | } 47 | } else { 48 | test.Fatalf("Discovery Error:%v\n", err) 49 | } 50 | 51 | } 52 | 53 | offerPacket, err := exampleClient.GetOffer(&discoveryPacket) 54 | if err != nil { 55 | test.Fatalf("Offer Error:%v\n", err) 56 | } 57 | 58 | requestPacket, err := exampleClient.SendRequest(&offerPacket) 59 | if err != nil { 60 | test.Fatalf("Send Offer Error:%v\n", err) 61 | } 62 | 63 | acknowledgementpacket, err := exampleClient.GetAcknowledgement(&requestPacket) 64 | if err != nil { 65 | test.Fatalf("Get Ack Error:%v\n", err) 66 | } 67 | 68 | acknowledgementOptions := acknowledgementpacket.ParseOptions() 69 | if dhcp4.MessageType(acknowledgementOptions[dhcp4.OptionDHCPMessageType][0]) != dhcp4.ACK { 70 | test.Fatalf("Not Acknoledged") 71 | } else { 72 | success = true 73 | } 74 | 75 | test.Logf("Packet:%v\n", acknowledgementpacket) 76 | 77 | if err != nil { 78 | networkError, ok := err.(net.Error) 79 | if ok && networkError.Timeout() { 80 | test.Log("Test Skipping as it didn't find a DHCP Server") 81 | test.SkipNow() 82 | } 83 | test.Fatalf("Error:%v\n", err) 84 | } 85 | 86 | if !success { 87 | test.Error("We didn't sucessfully get a DHCP Lease?") 88 | } else { 89 | log.Printf("IP Received:%v\n", acknowledgementpacket.YIAddr().String()) 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /client_test.go: -------------------------------------------------------------------------------- 1 | package dhcp4client_test 2 | 3 | import ( 4 | "log" 5 | "net" 6 | "testing" 7 | 8 | "github.com/d2g/dhcp4client" 9 | ) 10 | 11 | //Example Client 12 | func Test_ExampleClient(test *testing.T) { 13 | var err error 14 | 15 | m, err := net.ParseMAC("08-00-27-00-A8-E8") 16 | if err != nil { 17 | log.Printf("MAC Error:%v\n", err) 18 | } 19 | 20 | //Create a connection to use 21 | //We need to set the connection ports to 1068 and 1067 so we don't need root access 22 | c, err := dhcp4client.NewInetSock(dhcp4client.SetLocalAddr(net.UDPAddr{IP: net.IPv4(0, 0, 0, 0), Port: 1068}), dhcp4client.SetRemoteAddr(net.UDPAddr{IP: net.IPv4bcast, Port: 1067})) 23 | if err != nil { 24 | test.Error("Client Connection Generation:" + err.Error()) 25 | } 26 | defer c.Close() 27 | 28 | exampleClient, err := dhcp4client.New(dhcp4client.HardwareAddr(m), dhcp4client.Connection(c)) 29 | if err != nil { 30 | test.Fatalf("Error:%v\n", err) 31 | } 32 | defer exampleClient.Close() 33 | 34 | success, acknowledgementpacket, err := exampleClient.Request() 35 | 36 | test.Logf("Success:%v\n", success) 37 | test.Logf("Packet:%v\n", acknowledgementpacket) 38 | 39 | if err != nil { 40 | networkError, ok := err.(*net.OpError) 41 | if ok && networkError.Timeout() { 42 | test.Log("Test Skipping as it didn't find a DHCP Server") 43 | test.SkipNow() 44 | } 45 | test.Fatalf("Error:%v\n", err) 46 | } 47 | 48 | if !success { 49 | test.Error("We didn't sucessfully get a DHCP Lease?") 50 | } else { 51 | log.Printf("IP Received:%v\n", acknowledgementpacket.YIAddr().String()) 52 | } 53 | 54 | test.Log("Start Renewing Lease") 55 | success, acknowledgementpacket, err = exampleClient.Renew(acknowledgementpacket) 56 | if err != nil { 57 | networkError, ok := err.(*net.OpError) 58 | if ok && networkError.Timeout() { 59 | test.Log("Renewal Failed! Because it didn't find the DHCP server very Strange") 60 | test.Errorf("Error" + err.Error()) 61 | } 62 | test.Fatalf("Error:%v\n", err) 63 | } 64 | 65 | if !success { 66 | test.Error("We didn't sucessfully Renew a DHCP Lease?") 67 | } else { 68 | log.Printf("IP Received:%v\n", acknowledgementpacket.YIAddr().String()) 69 | } 70 | 71 | } 72 | 73 | //Example Client (With MathGID) 74 | func Test_ExampleClientWithMathGenerateXID(test *testing.T) { 75 | var err error 76 | 77 | m, err := net.ParseMAC("08-00-27-00-A8-E8") 78 | if err != nil { 79 | log.Printf("MAC Error:%v\n", err) 80 | } 81 | 82 | //Create a connection to use 83 | //We need to set the connection ports to 1068 and 1067 so we don't need root access 84 | c, err := dhcp4client.NewInetSock(dhcp4client.SetLocalAddr(net.UDPAddr{IP: net.IPv4(0, 0, 0, 0), Port: 1068}), dhcp4client.SetRemoteAddr(net.UDPAddr{IP: net.IPv4bcast, Port: 1067})) 85 | if err != nil { 86 | test.Error("Client Connection Generation:" + err.Error()) 87 | } 88 | defer c.Close() 89 | 90 | // If you ar using MathGenerateXID then you are responsible for seeding math/rand 91 | exampleClient, err := dhcp4client.New(dhcp4client.HardwareAddr(m), dhcp4client.Connection(c), dhcp4client.GenerateXID(dhcp4client.MathGenerateXID)) 92 | if err != nil { 93 | test.Fatalf("Error:%v\n", err) 94 | } 95 | defer exampleClient.Close() 96 | 97 | success, acknowledgementpacket, err := exampleClient.Request() 98 | 99 | test.Logf("Success:%v\n", success) 100 | test.Logf("Packet:%v\n", acknowledgementpacket) 101 | 102 | if err != nil { 103 | networkError, ok := err.(*net.OpError) 104 | if ok && networkError.Timeout() { 105 | test.Log("Test Skipping as it didn't find a DHCP Server") 106 | test.SkipNow() 107 | } 108 | test.Fatalf("Error:%v\n", err) 109 | } 110 | 111 | if !success { 112 | test.Error("We didn't sucessfully get a DHCP Lease?") 113 | } else { 114 | log.Printf("IP Received:%v\n", acknowledgementpacket.YIAddr().String()) 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /generatexid.go: -------------------------------------------------------------------------------- 1 | package dhcp4client 2 | 3 | import ( 4 | cryptorand "crypto/rand" 5 | mathrand "math/rand" 6 | ) 7 | 8 | func CryptoGenerateXID(b []byte) { 9 | if _, err := cryptorand.Read(b); err != nil { 10 | panic(err) 11 | } 12 | } 13 | 14 | func MathGenerateXID(b []byte) { 15 | if _, err := mathrand.Read(b); err != nil { 16 | panic(err) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /generatexid_test.go: -------------------------------------------------------------------------------- 1 | package dhcp4client_test 2 | 3 | import ( 4 | "bytes" 5 | "math/rand" 6 | "testing" 7 | 8 | "github.com/d2g/dhcp4client" 9 | ) 10 | 11 | func Test_GenerateXID(t *testing.T) { 12 | //Set the math seed so we always get the same result. 13 | rand.Seed(1) 14 | 15 | crypto_messageid := make([]byte, 4) 16 | dhcp4client.CryptoGenerateXID(crypto_messageid) 17 | 18 | t.Logf("Crypto Token: %v", crypto_messageid) 19 | 20 | math_messageid := make([]byte, 4) 21 | dhcp4client.MathGenerateXID(math_messageid) 22 | 23 | //Math token shouldn't change as we don't seed it. 24 | if !bytes.Equal(math_messageid, []byte{82, 253, 252, 7}) { 25 | t.Errorf("Math Token was %v, expected %v", math_messageid, []byte{82, 253, 252, 7}) 26 | t.Fail() 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /inetsock.go: -------------------------------------------------------------------------------- 1 | package dhcp4client 2 | 3 | import ( 4 | "net" 5 | "time" 6 | ) 7 | 8 | type inetSock struct { 9 | *net.UDPConn 10 | 11 | laddr net.UDPAddr 12 | raddr net.UDPAddr 13 | } 14 | 15 | func NewInetSock(options ...func(*inetSock) error) (*inetSock, error) { 16 | c := &inetSock{ 17 | laddr: net.UDPAddr{IP: net.IPv4(0, 0, 0, 0), Port: 68}, 18 | raddr: net.UDPAddr{IP: net.IPv4bcast, Port: 67}, 19 | } 20 | 21 | err := c.setOption(options...) 22 | if err != nil { 23 | return nil, err 24 | } 25 | 26 | conn, err := net.ListenUDP("udp4", &c.laddr) 27 | if err != nil { 28 | return nil, err 29 | } 30 | 31 | c.UDPConn = conn 32 | return c, err 33 | } 34 | 35 | func (c *inetSock) setOption(options ...func(*inetSock) error) error { 36 | for _, opt := range options { 37 | if err := opt(c); err != nil { 38 | return err 39 | } 40 | } 41 | return nil 42 | } 43 | 44 | func SetLocalAddr(l net.UDPAddr) func(*inetSock) error { 45 | return func(c *inetSock) error { 46 | c.laddr = l 47 | return nil 48 | } 49 | } 50 | 51 | func SetRemoteAddr(r net.UDPAddr) func(*inetSock) error { 52 | return func(c *inetSock) error { 53 | c.raddr = r 54 | return nil 55 | } 56 | } 57 | 58 | func (c *inetSock) Write(packet []byte) error { 59 | _, err := c.WriteToUDP(packet, &c.raddr) 60 | return err 61 | } 62 | 63 | func (c *inetSock) ReadFrom() ([]byte, net.IP, error) { 64 | readBuffer := make([]byte, MaxDHCPLen) 65 | n, source, err := c.ReadFromUDP(readBuffer) 66 | if source != nil { 67 | return readBuffer[:n], source.IP, err 68 | } else { 69 | return readBuffer[:n], net.IP{}, err 70 | } 71 | } 72 | 73 | func (c *inetSock) SetReadTimeout(t time.Duration) error { 74 | return c.SetReadDeadline(time.Now().Add(t)) 75 | } 76 | -------------------------------------------------------------------------------- /pktsock_linux.go: -------------------------------------------------------------------------------- 1 | package dhcp4client 2 | 3 | import ( 4 | "encoding/binary" 5 | "math/rand" 6 | "net" 7 | "time" 8 | 9 | "golang.org/x/sys/unix" 10 | ) 11 | 12 | const ( 13 | minIPHdrLen = 20 14 | maxIPHdrLen = 60 15 | udpHdrLen = 8 16 | ip4Ver = 0x40 17 | ttl = 16 18 | srcPort = 68 19 | dstPort = 67 20 | ) 21 | 22 | var ( 23 | bcastMAC = []byte{255, 255, 255, 255, 255, 255} 24 | ) 25 | 26 | // abstracts AF_PACKET 27 | type packetSock struct { 28 | fd int 29 | ifindex int 30 | } 31 | 32 | func NewPacketSock(ifindex int) (*packetSock, error) { 33 | fd, err := unix.Socket(unix.AF_PACKET, unix.SOCK_DGRAM, int(swap16(unix.ETH_P_IP))) 34 | if err != nil { 35 | return nil, err 36 | } 37 | 38 | addr := unix.SockaddrLinklayer{ 39 | Ifindex: ifindex, 40 | Protocol: swap16(unix.ETH_P_IP), 41 | } 42 | 43 | if err = unix.Bind(fd, &addr); err != nil { 44 | return nil, err 45 | } 46 | 47 | return &packetSock{ 48 | fd: fd, 49 | ifindex: ifindex, 50 | }, nil 51 | } 52 | 53 | func (pc *packetSock) Close() error { 54 | return unix.Close(pc.fd) 55 | } 56 | 57 | func (pc *packetSock) Write(packet []byte) error { 58 | lladdr := unix.SockaddrLinklayer{ 59 | Ifindex: pc.ifindex, 60 | Protocol: swap16(unix.ETH_P_IP), 61 | Halen: uint8(len(bcastMAC)), 62 | } 63 | copy(lladdr.Addr[:], bcastMAC) 64 | 65 | pkt := make([]byte, minIPHdrLen+udpHdrLen+len(packet)) 66 | 67 | fillIPHdr(pkt[0:minIPHdrLen], udpHdrLen+uint16(len(packet))) 68 | fillUDPHdr(pkt[minIPHdrLen:minIPHdrLen+udpHdrLen], uint16(len(packet))) 69 | 70 | // payload 71 | copy(pkt[minIPHdrLen+udpHdrLen:len(pkt)], packet) 72 | 73 | return unix.Sendto(pc.fd, pkt, 0, &lladdr) 74 | } 75 | 76 | func (pc *packetSock) ReadFrom() ([]byte, net.IP, error) { 77 | pkt := make([]byte, maxIPHdrLen+udpHdrLen+MaxDHCPLen) 78 | n, _, err := unix.Recvfrom(pc.fd, pkt, 0) 79 | if err != nil { 80 | return nil, nil, err 81 | } 82 | 83 | // IP hdr len 84 | ihl := int(pkt[0]&0x0F) * 4 85 | // Source IP address 86 | src := net.IP(pkt[12:16]) 87 | 88 | return pkt[ihl+udpHdrLen : n], src, nil 89 | } 90 | 91 | func (pc *packetSock) SetReadTimeout(t time.Duration) error { 92 | 93 | tv := unix.NsecToTimeval(t.Nanoseconds()) 94 | return unix.SetsockoptTimeval(pc.fd, unix.SOL_SOCKET, unix.SO_RCVTIMEO, &tv) 95 | } 96 | 97 | // compute's 1's complement checksum 98 | func chksum(p []byte, csum []byte) { 99 | cklen := len(p) 100 | s := uint32(0) 101 | for i := 0; i < (cklen - 1); i += 2 { 102 | s += uint32(p[i+1])<<8 | uint32(p[i]) 103 | } 104 | if cklen&1 == 1 { 105 | s += uint32(p[cklen-1]) 106 | } 107 | s = (s >> 16) + (s & 0xffff) 108 | s = s + (s >> 16) 109 | s = ^s 110 | 111 | csum[0] = uint8(s & 0xff) 112 | csum[1] = uint8(s >> 8) 113 | } 114 | 115 | func fillIPHdr(hdr []byte, payloadLen uint16) { 116 | // version + IHL 117 | hdr[0] = ip4Ver | (minIPHdrLen / 4) 118 | // total length 119 | binary.BigEndian.PutUint16(hdr[2:4], uint16(len(hdr))+payloadLen) 120 | // identification 121 | if _, err := rand.Read(hdr[4:5]); err != nil { 122 | panic(err) 123 | } 124 | // TTL 125 | hdr[8] = 16 126 | // Protocol 127 | hdr[9] = unix.IPPROTO_UDP 128 | // dst IP 129 | copy(hdr[16:20], net.IPv4bcast.To4()) 130 | // compute IP hdr checksum 131 | chksum(hdr[0:len(hdr)], hdr[10:12]) 132 | } 133 | 134 | func fillUDPHdr(hdr []byte, payloadLen uint16) { 135 | // src port 136 | binary.BigEndian.PutUint16(hdr[0:2], srcPort) 137 | // dest port 138 | binary.BigEndian.PutUint16(hdr[2:4], dstPort) 139 | // length 140 | binary.BigEndian.PutUint16(hdr[4:6], udpHdrLen+payloadLen) 141 | } 142 | 143 | func swap16(x uint16) uint16 { 144 | var b [2]byte 145 | binary.BigEndian.PutUint16(b[:], x) 146 | return binary.LittleEndian.Uint16(b[:]) 147 | } 148 | --------------------------------------------------------------------------------