github.com/20yyq/packet@v0.1.4-0.20231013092308-386a004a3baa/dhcpv4.go (about)

     1  // @@
     2  // @ Author       : Eacher
     3  // @ Date         : 2023-07-04 08:48:44
     4  // @ LastEditTime : 2023-09-04 09:40:22
     5  // @ LastEditors  : Eacher
     6  // @ --------------------------------------------------------------------------------<
     7  // @ Description  : 
     8  // @ --------------------------------------------------------------------------------<
     9  // @ FilePath     : /20yyq/packet/dhcpv4.go
    10  // @@
    11  package packet
    12  
    13  import (
    14  	"time"
    15  	"unsafe"
    16  	"encoding/binary"
    17  )
    18  
    19  var MagicCookie 		= [4]byte{99, 130, 83, 99}
    20  
    21  const (
    22  	DHCP_ServerPort 	= 0x43
    23  	DHCP_ClientPort 	= 0x44
    24  
    25  	DHCP_BOOTREQUEST 	= 0x01
    26  	DHCP_BOOTREPLY 		= 0x02
    27  	DHCP_Ethernet_TYPE 	= 0x01
    28  	DHCP_Ethernet_LEN 	= 0x06
    29  
    30  	SizeofDhcpV4Packet 	= 0xf0
    31  	SizeofOptionsPacket = 0x02
    32  )
    33  
    34  /*
    35  	// 14.byte  EthernetPacket 
    36  	// 20.byte  IPv4Packet 
    37  
    38  	FIELD   BYTES   DESCRIPTION
    39  	-----   -----   -----------
    40  	op      1       packet op code / message type. 1 = BOOTREQUEST, 2 = BOOTREPLY
    41  	htype   1       hardware address type, see ARP section in "Assigned Numbers" RFC. '1' = 10mb ethernet
    42  	hlen    1       hardware address length (eg '6' for 10mb ethernet).
    43  	hops    1       client sets to zero, optionally used by gateways in cross-gateway booting.
    44  	xid     4       transaction ID, a random number, used to match this boot request with the responses it generates.
    45  	secs    2       filled in by client, seconds elapsed since client started trying to boot.
    46  	--      2       unused
    47  	ciaddr  4       client IP address; filled in by client in bootrequest if known.
    48  	yiaddr  4       'your' (client) IP address; filled by server if client doesn't know its own address (ciaddr was 0).
    49  	siaddr  4       server IP address; returned in bootreply by server.
    50  	giaddr  4       gateway IP address, used in optional cross-gateway booting.
    51  	chaddr  16      client hardware address, filled in by client.
    52  	sname   64      optional server host name, null terminated string.
    53  	file    128     boot file name, null terminated string; 'generic' name or null in bootrequest, fully qualified directory-path name in bootreply.
    54  	vend    64      optional vendor-specific area, e.g. could be hardware type/serial on request, or 'capability' / remote file system handle on reply.  This info may be set aside for use by a third phase bootstrap or kernel.
    55   */
    56  type DhcpV4Packet struct {
    57  	Op 				uint8
    58  	HardwareType 	uint8
    59  	HardwareLen  	uint8
    60  	Hops 			uint8
    61  	XID 			uint32
    62  	Secs 			uint16
    63  	Flags 			uint16
    64  	CIAddr   		IPv4
    65  	YIAddr   		IPv4
    66  	SIAddr   		IPv4
    67  	GIAddr   		IPv4
    68  	ChHardware 		[16]byte
    69  	HostName 		[64]byte
    70  	FileName 		[128]byte
    71  	cookie   		[4]byte
    72  	Options  		[]OptionsPacket
    73  }
    74  
    75  type OptionsPacket struct {
    76  	Code 		uint8
    77  	Length 		uint8
    78  	Value 		[]byte
    79  }
    80  
    81  // 14.byte  EthernetPacket
    82  // 20.byte  IPv4Packet 或者 IPv6Packet
    83  func NewDhcpV4Packet(b []byte) (dhcp DhcpV4Packet) {
    84  	if len(b) > SizeofDhcpV4Packet {
    85  		dhcp = *(*DhcpV4Packet)(unsafe.Pointer((*[SizeofDhcpV4Packet]byte)(b)))
    86  		dhcp.XID = binary.BigEndian.Uint32(b[4:8])
    87  		dhcp.Secs = binary.BigEndian.Uint16(b[8:10])
    88  		dhcp.Flags 	 = binary.BigEndian.Uint16(b[10:12])
    89  		dhcp.Options = NewOptionsPacket(b[SizeofDhcpV4Packet:])
    90  	}
    91  	return
    92  }
    93  
    94  func (dhcp DhcpV4Packet) WireFormat() []byte {
    95  	var opb []byte
    96  	if 0 < len(dhcp.Options) {
    97  		for _, val := range dhcp.Options {
    98  			opb = append(opb, val.WireFormat()...)
    99  		}
   100  		b := make([]byte, len(opb) + SizeofDhcpV4Packet + 1)
   101  		copy(b[SizeofDhcpV4Packet:], opb)
   102  		opb, b[len(b) - 1] = b, 255
   103  		b[0], b[1], b[2], b[3] 	= dhcp.Op, dhcp.HardwareType, dhcp.HardwareLen, dhcp.Hops
   104  		binary.BigEndian.PutUint32(b[4:8], dhcp.XID)
   105  		binary.BigEndian.PutUint16(b[8:10], dhcp.Secs)
   106  		binary.BigEndian.PutUint16(b[10:12], dhcp.Flags)
   107  		*(*IPv4)(b[12:16]) 	= dhcp.CIAddr
   108  		*(*IPv4)(b[16:20]) 	= dhcp.YIAddr
   109  		*(*IPv4)(b[20:24]) 	= dhcp.SIAddr
   110  		*(*IPv4)(b[24:28]) 	= dhcp.GIAddr
   111  		*(*[16]byte)(b[28:44]) 	= dhcp.ChHardware
   112  		*(*[64]byte)(b[44:108]) = dhcp.HostName
   113  		*(*[128]byte)(b[108:236]) = dhcp.FileName
   114  		*(*[4]byte)(b[236:240]) 	= MagicCookie
   115  	}
   116  	return opb
   117  }
   118  
   119  func NewOptionsPacket(b []byte) (list []OptionsPacket) {
   120  	var idx, next uint8
   121  	if len(b) > SizeofOptionsPacket {
   122  		opp := OptionsPacket{b[idx], b[idx+1], nil}
   123  		idx = 2
   124  		for opp.Code != 255 {
   125  			opp.Length = b[idx-1]
   126  			next = idx + opp.Length
   127  			opp.Value = make([]byte, opp.Length)
   128  			copy(opp.Value, b[idx:next])
   129  			list = append(list, opp)
   130  			opp = OptionsPacket{b[next], 0, nil}
   131  			idx = next + 2
   132  		}
   133  	}
   134  	return
   135  }
   136  
   137  func (opp OptionsPacket) WireFormat() []byte {
   138  	b := make([]byte, SizeofOptionsPacket)
   139  	b[0], b[1] = opp.Code, opp.Length
   140  	return append(b, opp.Value...)
   141  }
   142  
   143  /*
   144  9.6. DHCP Message Type
   145  	This option is used to convey the type of the DHCP message.  The code
   146  	for this option is 53, and its length is 1.  Legal values for this
   147  	option are:
   148  
   149  	       Value   Message Type
   150  	       -----   ------------
   151  	         1     DHCPDISCOVER
   152  	         2     DHCPOFFER
   153  	         3     DHCPREQUEST
   154  	         4     DHCPDECLINE
   155  	         5     DHCPACK
   156  	         6     DHCPNAK
   157  	         7     DHCPRELEASE
   158  	         8     DHCPINFORM
   159   */
   160  type DHCP_Message_Type uint8
   161  
   162  const (
   163  	DHCP_DISCOVER DHCP_Message_Type = iota + 1
   164  	DHCP_OFFER
   165  	DHCP_REQUEST
   166  	DHCP_DECLINE
   167  	DHCP_ACK
   168  	DHCP_NAK
   169  	DHCP_RELEASE
   170  	DHCP_INFORM
   171  )
   172  
   173  func SetDHCPMessage(t DHCP_Message_Type) OptionsPacket {
   174  	return OptionsPacket{53, 1, []byte{byte(t)}}
   175  }
   176  
   177  /*
   178  9.8. Parameter Request List
   179  	This option is used by a DHCP client to request values for specified
   180  	configuration parameters.  The list of requested parameters is
   181  	specified as n octets, where each octet is a valid DHCP option code
   182  	as defined in this document.
   183  
   184  	The client MAY list the options in order of preference.  The DHCP
   185  	server is not required to return the options in the requested order,
   186  	but MUST try to insert the requested options in the order requested
   187  	by the client.
   188  
   189  	The code for this option is 55.  Its minimum length is 1.
   190  
   191  	Code   Len   Option Codes
   192  	+-----+-----+-----+-----+---
   193  	|  55 |  n  |  c1 |  c2 | ...
   194  	+-----+-----+-----+-----+---
   195   */
   196  func SetDHCPOptionsRequestList(codes ...uint8) OptionsPacket {
   197  	return OptionsPacket{55, uint8(len(codes)), codes}
   198  }
   199  
   200  /*
   201  9.10. Maximum DHCP Message Size
   202  	This option specifies the maximum length DHCP message that it is
   203  	willing to accept.  The length is specified as an unsigned 16-bit
   204  	integer.  A client may use the maximum DHCP message size option in
   205  	DHCPDISCOVER or DHCPREQUEST messages, but should not use the option
   206  	in DHCPDECLINE messages.
   207  
   208  	The code for this option is 57, and its length is 2.  The minimum
   209  	legal value is 576 octets.
   210  
   211  	Code   Len     Length
   212  	+-----+-----+-----+-----+
   213  	|  57 |  2  |  l1 |  l2 |
   214  	+-----+-----+-----+-----+
   215   */
   216  func SetDHCPMaximumMessageSize(size uint16) OptionsPacket {
   217  	if size < 576 {
   218  		size = 576
   219  	}
   220  	return OptionsPacket{57, 2, binary.BigEndian.AppendUint16(nil, size)}
   221  }
   222  
   223  /*
   224  8.1. Network Information Service Domain Option
   225  	This option specifies the name of the client's NIS [17] domain.  The
   226  	domain is formatted as a character string consisting of characters
   227  	from the NVT ASCII character set.
   228  
   229  	The code for this option is 40.  Its minimum length is 1.
   230  
   231  	Code   Len      NIS Domain Name
   232  	+-----+-----+-----+-----+-----+-----+---
   233  	|  40 |  n  |  n1 |  n2 |  n3 |  n4 | ...
   234  	+-----+-----+-----+-----+-----+-----+---
   235  8.8. NetBIOS over TCP/IP Scope Option
   236  	The NetBIOS scope option specifies the NetBIOS over TCP/IP scope
   237  	parameter for the client as specified in RFC 1001/1002. See [19],
   238  	[20], and [8] for character-set restrictions.
   239  
   240  	The code for this option is 47.  The minimum length of this option is
   241  	1.
   242  
   243  	Code   Len       NetBIOS Scope
   244  	+-----+-----+-----+-----+-----+-----+----
   245  	|  47 |  n  |  s1 |  s2 |  s3 |  s4 | ...
   246  	+-----+-----+-----+-----+-----+-----+----
   247  8.11. Network Information Service+ Domain Option
   248  	This option specifies the name of the client's NIS+ [17] domain.  The
   249  	domain is formatted as a character string consisting of characters
   250  	from the NVT ASCII character set.
   251  
   252  	The code for this option is 64.  Its minimum length is 1.
   253  
   254  	Code   Len      NIS Client Domain Name
   255  	+-----+-----+-----+-----+-----+-----+---
   256  	|  64 |  n  |  n1 |  n2 |  n3 |  n4 | ...
   257  	+-----+-----+-----+-----+-----+-----+---
   258  9.4 TFTP server name
   259  	This option is used to identify a TFTP server when the 'sname' field
   260  	in the DHCP header has been used for DHCP options.
   261  
   262  	The code for this option is 66, and its minimum length is 1.
   263  
   264  	Code  Len   TFTP server
   265  	+-----+-----+-----+-----+-----+---
   266  	| 66  |  n  |  c1 |  c2 |  c3 | ...
   267  	+-----+-----+-----+-----+-----+---
   268  9.5 Bootfile name
   269  	This option is used to identify a bootfile when the 'file' field in
   270  	the DHCP header has been used for DHCP options.
   271  
   272  	The code for this option is 67, and its minimum length is 1.
   273  
   274  	   Code  Len   Bootfile name
   275  	  +-----+-----+-----+-----+-----+---
   276  	  | 67  |  n  |  c1 |  c2 |  c3 | ...
   277  	  +-----+-----+-----+-----+-----+---
   278  9.9. Message
   279  	This option is used by a DHCP server to provide an error message to a
   280  	DHCP client in a DHCPNAK message in the event of a failure. A client
   281  	may use this option in a DHCPDECLINE message to indicate the why the
   282  	client declined the offered parameters.  The message consists of n
   283  	octets of NVT ASCII text, which the client may display on an
   284  	available output device.
   285  
   286  	The code for this option is 56 and its minimum length is 1.
   287  
   288  	Code   Len     Text
   289  	+-----+-----+-----+-----+---
   290  	|  56 |  n  |  c1 |  c2 | ...
   291  	+-----+-----+-----+-----+---
   292  9.13. Vendor class identifier
   293  	This option is used by DHCP clients to optionally identify the vendor
   294  	type and configuration of a DHCP client.  The information is a string
   295  	of n octets, interpreted by servers.  Vendors may choose to define
   296  	specific vendor class identifiers to convey particular configuration
   297  	or other identification information about a client.  For example, the
   298  	identifier may encode the client's hardware configuration.  Servers
   299  	not equipped to interpret the class-specific information sent by a
   300  	client MUST ignore it (although it may be reported). Servers that
   301  	respond SHOULD only use option 43 to return the vendor-specific
   302  	information to the client.
   303  
   304  	The code for this option is 60, and its minimum length is 1.
   305  
   306  	Code   Len   Vendor class Identifier
   307  	+-----+-----+-----+-----+---
   308  	|  60 |  n  |  i1 |  i2 | ...
   309  	+-----+-----+-----+-----+---
   310   */
   311  type DHCP_STRING_TYPE uint8
   312  
   313  const (
   314  	DHCP_Network_Information_Service_Domain DHCP_STRING_TYPE = 40
   315  	DHCP_NetBIOS_Scope DHCP_STRING_TYPE 							= 47
   316  	DHCP_Network_Information_ServiceS_Domain DHCP_STRING_TYPE= 64
   317  	DHCP_TFTP_Server_Name DHCP_STRING_TYPE 						= 66
   318  	DHCP_Bootfile_Name DHCP_STRING_TYPE								= 67
   319  	DHCP_Error_Message DHCP_STRING_TYPE								= 56
   320  	DHCP_Vendor_Class_Identifier DHCP_STRING_TYPE 				= 60
   321  )
   322  
   323  func SetDHCPString(t DHCP_STRING_TYPE, s string) OptionsPacket {
   324  	length := len(s) + 1
   325  	if length > 255 {
   326  		return OptionsPacket{}
   327  	}
   328  	s += string([]byte{0})
   329  	return OptionsPacket{uint8(t), uint8(length), []byte(s)}
   330  }
   331  
   332  /*
   333  3.3. Subnet Mask
   334  	The subnet mask option specifies the client's subnet mask as per RFC
   335  	950 [5].
   336  
   337  	If both the subnet mask and the router option are specified in a DHCP
   338  	reply, the subnet mask option MUST be first.
   339  	The code for the subnet mask option is 1, and its length is 4 octets.
   340  
   341  	Code   Len        Subnet Mask
   342  	+-----+-----+-----+-----+-----+-----+
   343  	|  1  |  4  |  m1 |  m2 |  m3 |  m4 |
   344  	+-----+-----+-----+-----+-----+-----+
   345  3.5. Router Option
   346  	The router option specifies a list of IP addresses for routers on the
   347  	client's subnet.  Routers SHOULD be listed in order of preference.
   348  
   349  	The code for the router option is 3.  The minimum length for the
   350  	router option is 4 octets, and the length MUST always be a multiple
   351  	of 4.
   352  
   353  	Code   Len         Address 1               Address 2
   354  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   355  	|  3  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   356  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   357  3.6. Time Server Option
   358  	The time server option specifies a list of RFC 868 [6] time servers
   359  	available to the client.  Servers SHOULD be listed in order of
   360  	preference.
   361  
   362  	The code for the time server option is 4.  The minimum length for
   363  	this option is 4 octets, and the length MUST always be a multiple of
   364  	4.
   365  	Code   Len         Address 1               Address 2
   366  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   367  	|  4  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   368  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   369  3.7. Name Server Option
   370  	The name server option specifies a list of IEN 116 [7] name servers
   371  	available to the client.  Servers SHOULD be listed in order of
   372  	preference.
   373  
   374  	The code for the name server option is 5.  The minimum length for
   375  	this option is 4 octets, and the length MUST always be a multiple of
   376  	4.
   377  
   378  	Code   Len         Address 1               Address 2
   379  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   380  	|  5  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   381  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   382  3.8. Domain Name Server Option
   383  	The domain name server option specifies a list of Domain Name System
   384  	(STD 13, RFC 1035 [8]) name servers available to the client.  Servers
   385  	SHOULD be listed in order of preference.
   386  
   387  	The code for the domain name server option is 6.  The minimum length
   388  	for this option is 4 octets, and the length MUST always be a multiple
   389  	of 4.
   390  
   391  	Code   Len         Address 1               Address 2
   392  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   393  	|  6  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   394  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   395  3.9. Log Server Option
   396  	The log server option specifies a list of MIT-LCS UDP log servers
   397  	available to the client.  Servers SHOULD be listed in order of
   398  	preference.
   399  
   400  	The code for the log server option is 7.  The minimum length for this
   401  	option is 4 octets, and the length MUST always be a multiple of 4.
   402  
   403  	Code   Len         Address 1               Address 2
   404  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   405  	|  7  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   406  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   407  3.10. Cookie Server Option
   408  	The cookie server option specifies a list of RFC 865 [9] cookie
   409  	servers available to the client.  Servers SHOULD be listed in order
   410  	of preference.
   411  
   412  	The code for the log server option is 8.  The minimum length for this
   413  	option is 4 octets, and the length MUST always be a multiple of 4.
   414  
   415  	Code   Len         Address 1               Address 2
   416  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   417  	|  8  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   418  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   419  3.11. LPR Server Option
   420  	The LPR server option specifies a list of RFC 1179 [10] line printer
   421  	servers available to the client.  Servers SHOULD be listed in order
   422  	of preference.
   423  
   424  	The code for the LPR server option is 9.  The minimum length for this
   425  	option is 4 octets, and the length MUST always be a multiple of 4.
   426  
   427  	Code   Len         Address 1               Address 2
   428  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   429  	|  9  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   430  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   431  3.12. Impress Server Option
   432  	The Impress server option specifies a list of Imagen Impress servers
   433  	available to the client.  Servers SHOULD be listed in order of
   434  	preference.
   435  
   436  	The code for the Impress server option is 10.  The minimum length for
   437  	this option is 4 octets, and the length MUST always be a multiple of
   438  	4.
   439  
   440  	Code   Len         Address 1               Address 2
   441  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   442  	|  10 |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   443  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   444  3.13. Resource Location Server Option
   445  	This option specifies a list of RFC 887 [11] Resource Location
   446  	servers available to the client.  Servers SHOULD be listed in order
   447  	of preference.
   448  
   449  	The code for this option is 11.  The minimum length for this option
   450  	is 4 octets, and the length MUST always be a multiple of 4.
   451  
   452  	Code   Len         Address 1               Address 2
   453  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   454  	|  11 |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   455  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   456  8.2. Network Information Servers Option
   457  	This option specifies a list of IP addresses indicating NIS servers
   458  	available to the client.  Servers SHOULD be listed in order of
   459  	preference.
   460  
   461  	The code for this option is 41.  Its minimum length is 4, and the
   462  	length MUST be a multiple of 4.
   463  
   464  	Code   Len         Address 1               Address 2
   465  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   466  	|  41 |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   467  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   468  8.3. Network Time Protocol Servers Option
   469  	This option specifies a list of IP addresses indicating NTP [18]
   470  	servers available to the client.  Servers SHOULD be listed in order
   471  	of preference.
   472  
   473  	The code for this option is 42.  Its minimum length is 4, and the
   474  	length MUST be a multiple of 4.
   475  	Code   Len         Address 1               Address 2
   476  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   477  	|  42 |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   478  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   479  8.5. NetBIOS over TCP/IP Name Server Option
   480  	The NetBIOS name server (NBNS) option specifies a list of RFC
   481  	1001/1002 [19] [20] NBNS name servers listed in order of preference.
   482  
   483  	The code for this option is 44.  The minimum length of the option is
   484  	4 octets, and the length must always be a multiple of 4.
   485  
   486  	Code   Len           Address 1              Address 2
   487  	+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
   488  	|  44 |  n  |  a1 |  a2 |  a3 |  a4 |  b1 |  b2 |  b3 |  b4 | ...
   489  	+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
   490  8.6. NetBIOS over TCP/IP Datagram Distribution Server Option
   491  	The NetBIOS datagram distribution server (NBDD) option specifies a
   492  	list of RFC 1001/1002 NBDD servers listed in order of preference. The
   493  	code for this option is 45.  The minimum length of the option is 4
   494  	octets, and the length must always be a multiple of 4.
   495  
   496  	Code   Len           Address 1              Address 2
   497  	+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
   498  	|  45 |  n  |  a1 |  a2 |  a3 |  a4 |  b1 |  b2 |  b3 |  b4 | ...
   499  	+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
   500  8.9. X Window System Font Server Option
   501  	This option specifies a list of X Window System [21] Font servers
   502  	available to the client. Servers SHOULD be listed in order of
   503  	preference.
   504  
   505  	The code for this option is 48.  The minimum length of this option is
   506  	4 octets, and the length MUST be a multiple of 4.
   507  
   508  	Code   Len         Address 1               Address 2
   509  	+-----+-----+-----+-----+-----+-----+-----+-----+---
   510  	|  48 |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |   ...
   511  	+-----+-----+-----+-----+-----+-----+-----+-----+---
   512  8.10. X Window System Display Manager Option
   513  	This option specifies a list of IP addresses of systems that are
   514  	running the X Window System Display Manager and are available to the
   515  	client.
   516  
   517  	Addresses SHOULD be listed in order of preference.
   518  	The code for the this option is 49. The minimum length of this option
   519  	is 4, and the length MUST be a multiple of 4.
   520  
   521  	Code   Len         Address 1               Address 2
   522  
   523  	+-----+-----+-----+-----+-----+-----+-----+-----+---
   524  	|  49 |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |   ...
   525  	+-----+-----+-----+-----+-----+-----+-----+-----+---
   526  8.12. Network Information Service+ Servers Option
   527  	This option specifies a list of IP addresses indicating NIS+ servers
   528  	available to the client.  Servers SHOULD be listed in order of
   529  	preference.
   530  
   531  	The code for this option is 65.  Its minimum length is 4, and the
   532  	length MUST be a multiple of 4.
   533  
   534  	Code   Len         Address 1               Address 2
   535  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   536  	|  65 |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   537  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   538  8.13. Mobile IP Home Agent option
   539  	This option specifies a list of IP addresses indicating mobile IP
   540  	home agents available to the client.  Agents SHOULD be listed in
   541  	order of preference.
   542  
   543  	The code for this option is 68.  Its minimum length is 0 (indicating
   544  	no home agents are available) and the length MUST be a multiple of 4.
   545  	It is expected that the usual length will be four octets, containing
   546  	a single home agent's address.
   547  
   548  	Code Len    Home Agent Addresses (zero or more)
   549  	+-----+-----+-----+-----+-----+-----+--
   550  	| 68  |  n  | a1  | a2  | a3  | a4  | ...
   551  	+-----+-----+-----+-----+-----+-----+--
   552  8.14. Simple Mail Transport Protocol (SMTP) Server Option
   553  	The SMTP server option specifies a list of SMTP servers available to
   554  	the client.  Servers SHOULD be listed in order of preference.
   555  
   556  	The code for the SMTP server option is 69.  The minimum length for
   557  	this option is 4 octets, and the length MUST always be a multiple of
   558  	4.
   559  
   560  	Code   Len         Address 1               Address 2
   561  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   562  	| 69  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   563  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   564  8.15. Post Office Protocol (POP3) Server Option
   565  	The POP3 server option specifies a list of POP3 available to the
   566  	client.  Servers SHOULD be listed in order of preference.
   567  
   568  	The code for the POP3 server option is 70.  The minimum length for
   569  	this option is 4 octets, and the length MUST always be a multiple of
   570  	4.
   571  
   572  	Code   Len         Address 1               Address 2
   573  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   574  	| 70  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   575  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   576  8.16. Network News Transport Protocol (NNTP) Server Option
   577  	The NNTP server option specifies a list of NNTP available to the
   578  	client.  Servers SHOULD be listed in order of preference.
   579  
   580  	The code for the NNTP server option is 71. The minimum length for
   581  	this option is 4 octets, and the length MUST always be a multiple of
   582  	4.
   583  
   584  	Code   Len         Address 1               Address 2
   585  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   586  	| 71  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   587  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   588  8.17. Default World Wide Web (WWW) Server Option
   589  	The WWW server option specifies a list of WWW available to the
   590  	client.  Servers SHOULD be listed in order of preference.
   591  
   592  	The code for the WWW server option is 72.  The minimum length for
   593  	this option is 4 octets, and the length MUST always be a multiple of
   594  	4.
   595  
   596  	Code   Len         Address 1               Address 2
   597  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   598  	| 72  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   599  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   600  8.18. Default Finger Server Option
   601  	The Finger server option specifies a list of Finger available to the
   602  	client.  Servers SHOULD be listed in order of preference.
   603  
   604  	The code for the Finger server option is 73.  The minimum length for
   605  	this option is 4 octets, and the length MUST always be a multiple of
   606  	4.
   607  
   608  	Code   Len         Address 1               Address 2
   609  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   610  	| 73  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   611  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   612  8.19. Default Internet Relay Chat (IRC) Server Option
   613  	The IRC server option specifies a list of IRC available to the
   614  	client.  Servers SHOULD be listed in order of preference.
   615  
   616  	The code for the IRC server option is 74.  The minimum length for
   617  	this option is 4 octets, and the length MUST always be a multiple of
   618  	4.
   619  
   620  	Code   Len         Address 1               Address 2
   621  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   622  	| 74  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   623  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   624  8.20. StreetTalk Server Option
   625  	The StreetTalk server option specifies a list of StreetTalk servers
   626  	available to the client.  Servers SHOULD be listed in order of
   627  	preference.
   628  
   629  	The code for the StreetTalk server option is 75.  The minimum length
   630  	for this option is 4 octets, and the length MUST always be a multiple
   631  	of 4.
   632  
   633  	Code   Len         Address 1               Address 2
   634  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   635  	| 75  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   636  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   637  8.21. StreetTalk Directory Assistance (STDA) Server Option
   638  	The StreetTalk Directory Assistance (STDA) server option specifies a
   639  	list of STDA servers available to the client.  Servers SHOULD be
   640  	listed in order of preference.
   641  
   642  	The code for the StreetTalk Directory Assistance server option is 76.
   643  	The minimum length for this option is 4 octets, and the length MUST
   644  	always be a multiple of 4.
   645  
   646  	Code   Len         Address 1               Address 2
   647  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   648  	| 76  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   649  	+-----+-----+-----+-----+-----+-----+-----+-----+--
   650  9.1. Requested IP Address
   651  	This option is used in a client request (DHCPDISCOVER) to allow the
   652  	client to request that a particular IP address be assigned.
   653  
   654  	The code for this option is 50, and its length is 4.
   655  
   656  	Code   Len          Address
   657  	+-----+-----+-----+-----+-----+-----+
   658  	|  50 |  4  |  a1 |  a2 |  a3 |  a4 |
   659  	+-----+-----+-----+-----+-----+-----+
   660  9.7. Server Identifier
   661  	This option is used in DHCPOFFER and DHCPREQUEST messages, and may
   662  	optionally be included in the DHCPACK and DHCPNAK messages.  DHCP
   663  	servers include this option in the DHCPOFFER in order to allow the
   664  	client to distinguish between lease offers.  DHCP clients use the
   665  	contents of the 'server identifier' field as the destination address
   666  	for any DHCP messages unicast to the DHCP server.  DHCP clients also
   667  	indicate which of several lease offers is being accepted by including
   668  	this option in a DHCPREQUEST message.
   669  
   670  	The identifier is the IP address of the selected server.
   671  
   672  	The code for this option is 54, and its length is 4.
   673  
   674  	Code   Len            Address
   675  	+-----+-----+-----+-----+-----+-----+
   676  	|  54 |  4  |  a1 |  a2 |  a3 |  a4 |
   677  	+-----+-----+-----+-----+-----+-----+
   678   */
   679  type DHCP_IPv4_TYPE uint8
   680  
   681  const (
   682  	DHCP_Subnet_Mask DHCP_IPv4_TYPE = iota + 1
   683  	_
   684  	DHCP_Router
   685  	DHCP_Time_Server
   686  	DHCP_Name_Server
   687  	DHCP_Domain_Name_Server
   688  	DHCP_Log_Server
   689  	DHCP_Cookie_Server
   690  	DHCP_LPR_Server
   691  	DHCP_Impress_Server
   692  	DHCP_Resource_Location_Server
   693  )
   694  
   695  const (
   696  	DHCP_Information_Servers DHCP_IPv4_TYPE = iota + 41
   697  	DHCP_Protocol_Servers
   698  	_
   699  	DHCP_NetBIOS_Name_Server
   700  	DHCP_NetBIOS_Distribution_Server
   701  	_
   702  	_
   703  	DHCP_Window_System_Server
   704  	DHCP_Window_System_Display_Manager
   705  	DHCP_Requested_IP_Address
   706  	_
   707  	_
   708  	_
   709  	DHCP_Server_Identifier
   710  )
   711  
   712  const (
   713  	DHCP_Network_Information_Service_Servers DHCP_IPv4_TYPE = iota + 65
   714  	_
   715  	_
   716  	DHCP_Mobile_IP_Home_Agent
   717  	DHCP_SMTP_Server
   718  	DHCP_POP3_Server
   719  	DHCP_NNTP_Server
   720  	DHCP_WWW_Server
   721  	DHCP_Default_Finger_Server
   722  	DHCP_IRC_Server
   723  	DHCP_StreetTalk_Server
   724  	DHCP_STDA_Server
   725  )
   726  
   727  func SetDHCPIPv4(t DHCP_IPv4_TYPE, ip ...IPv4) OptionsPacket {
   728  	length := len(ip)*4
   729  	if length > 255 || length < 1 {
   730  		return OptionsPacket{}
   731  	}
   732  	b := make([]byte, length)
   733  	for i, v := range ip {
   734  		copy(b[i*4:], v[:])
   735  	}
   736  	return OptionsPacket{uint8(t), uint8(length), b}
   737  }
   738  
   739  /*
   740  8.7. NetBIOS over TCP/IP Node Type Option
   741  	The NetBIOS node type option allows NetBIOS over TCP/IP clients which
   742  	are configurable to be configured as described in RFC 1001/1002.  The
   743  	value is specified as a single octet which identifies the client type
   744  	as follows:
   745  
   746  	Value         Node Type
   747  	-----         ---------
   748  	0x1           B-node
   749  	0x2           P-node
   750  	0x4           M-node
   751  	0x8           H-node
   752   */
   753  type DHCP_NetBIOS_Node_Type uint8
   754  
   755  const (
   756  	DHCP_B_node DHCP_NetBIOS_Node_Type = iota + 1
   757  	DHCP_P_node
   758  	_
   759  	DHCP_M_node
   760  	_
   761  	_
   762  	_
   763  	DHCP_H_node
   764  )
   765  
   766  func SetDHCPNetBIOSNodeType(t DHCP_NetBIOS_Node_Type) OptionsPacket {
   767  	return OptionsPacket{46, 1, []byte{byte(t)}}
   768  }
   769  
   770  /*
   771  9.2. IP Address Lease Time
   772  	This option is used in a client request (DHCPDISCOVER or DHCPREQUEST)
   773  	to allow the client to request a lease time for the IP address.  In a
   774  	server reply (DHCPOFFER), a DHCP server uses this option to specify
   775  	the lease time it is willing to offer.
   776  
   777  	The time is in units of seconds, and is specified as a 32-bit
   778  	unsigned integer.
   779  
   780  	The code for this option is 51, and its length is 4.
   781  
   782  	Code   Len         Lease Time
   783  	+-----+-----+-----+-----+-----+-----+
   784  	|  51 |  4  |  t1 |  t2 |  t3 |  t4 |
   785  	+-----+-----+-----+-----+-----+-----+
   786  9.11. Renewal (T1) Time Value
   787  	This option specifies the time interval from address assignment until
   788  	the client transitions to the RENEWING state.
   789  
   790  	The value is in units of seconds, and is specified as a 32-bit
   791  	unsigned integer.
   792  
   793  	The code for this option is 58, and its length is 4.
   794  
   795  	Code   Len         T1 Interval
   796  	+-----+-----+-----+-----+-----+-----+
   797  	|  58 |  4  |  t1 |  t2 |  t3 |  t4 |
   798  	+-----+-----+-----+-----+-----+-----+
   799  9.12. Rebinding (T2) Time Value
   800  	This option specifies the time interval from address assignment until
   801  	the client transitions to the REBINDING state.
   802  
   803  	The value is in units of seconds, and is specified as a 32-bit
   804  	unsigned integer.
   805  
   806  	The code for this option is 59, and its length is 4.
   807  
   808  	Code   Len         T2 Interval
   809  	+-----+-----+-----+-----+-----+-----+
   810  	|  59 |  4  |  t1 |  t2 |  t3 |  t4 |
   811  	+-----+-----+-----+-----+-----+-----+
   812   */
   813  type DHCP_TIME_TYPE uint8
   814  
   815  const (
   816  	DHCP_Renewal_Time DHCP_TIME_TYPE = iota + 58
   817  	DHCP_Rebinding_Time
   818  	DHCP_IP_Address_Lease DHCP_TIME_TYPE = 51
   819  )
   820  
   821  func SetDHCPTime(t DHCP_TIME_TYPE, d time.Duration) OptionsPacket {
   822  	return OptionsPacket{uint8(t), 4, binary.BigEndian.AppendUint32(nil, uint32(d.Seconds()))}
   823  }