Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4842511
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2008-10-27 17:06:51

   有幸拜读了DHCP RFC文档,只有一句话,真不是写个人看得.飘忽不定,像做广告般的参考xxx参考xxx.无语.

DHCP的报文格式

   尝试的翻译了点,实在头大.能懒就懒,这是我一贯的风格,没办法大多数就都还是copy的e文,当做是学英语喽^_^

DHCP的报文格式,如图1,


OP(1)

Htype(1)

Hlen(1)

Hops(1)

Transaction ID(4)

Seconds(2)

Flags(2)

Ciaddr(4)

Yiaddr(4)

Siaddr(4)

Giaddr(4)

Chaddr(16)

Sname(64)

File(128)

Options(variable)

(图1 DHCP的 报文格式)

  1. OP:若是client送给server的封包,设为1,反向为2;
  2. Htype:硬件类别,ethernet为1;
  3. Hlen:硬件长度,ethernet为6;
  4. Hops:若数据包需经过router传送,每站加1,若在同一网内,为0;
  5. Transaction ID:事务ID,是个随机数,用于客户和服务器之间匹配请求和相应消息;
  6. Seconds:由用户指定的时间,指开始地址获取和更新进行后的时间;
  7. Flags:从0-15bits,最左一bit为1时表示server将以广播方式传送封包给 client,其余尚未使用;
  8. Ciaddr:用户IP地址;
  9. Yiaddr:客户IP地址;
  10. Siaddr:用于bootstrap过程中的IP地址;
  11. Giaddr:转发代理(网关)IP地址;
  12. Chaddr:client的硬件地址;
  13. Sname:可选server的名称,以0x00结尾;
  14. File:启动文件名;
  15. Options:,厂商标识,可选的参数字段

 还是wiki的人性化点

DHCP uses the same two assigned ports as : 67/udp for the , and 68/udp for the .

DHCP operations fall into four basic phases. These phases are IP discovery, IP lease offer, IP request, and IP lease acknowledgement.

After the client obtained an IP address, the client may start an to prevent IP conflicts caused by address pool overlapping of DHCP servers.

DHCP discovery

The client broadcasts on the physical subnet to find available servers. Network administrators can configure a local router to forward DHCP packets to a DHCP server on a different subnet. This client-implementation creates a packet with the broadcast destination of 255.255.255.255 or subnet broadcast address.

A client can also request its last-known IP address (in the example below, 192.168.1.100). If the client is still in a network where this IP is valid, the server might grant the request. Otherwise, it depends whether the server is set up as or not. An authoritative server will deny the request, making the client ask for a new IP immediately. A non-authoritative server simply ignores the request, leading to an implementation-dependent timeout for the client to give up on the request and ask for a new IP address.

DHCP offers

When a DHCP server receives an IP lease request from a client, it extends an IP lease offer. This is done by reserving an IP address for the client and sending a DHCPOFFER message across the network to the client. This message contains the client's MAC address, followed by the IP address that the server is offering, the subnet mask, the lease duration, and the IP address of the DHCP server making the offer.

The server determines the configuration, based on the client's hardware address as specified in the CHADDR field. Here the server, 192.168.1.1, specifies the IP address in the YIADDR field.

DHCP requests

When the client PC receives an IP lease offer, it must tell all the other DHCP servers that it has accepted an offer. To do this, the client broadcasts a DHCPREQUEST message containing the IP address of the server that made the offer. When the other DHCP servers receive this message, they withdraw any offers that they might have made to the client. They then return the address that they had reserved for the client back to the pool of valid addresses that they can offer to another computer. Any number of DHCP servers can respond to an IP lease request, but the client can only accept one offer per network interface card.

 DHCP acknowledgement

When the DHCP server receives the DHCPREQUEST message from the client, which is basically a unicast packet unlike the DHCPDISCOVER packet as the client now knows who the server is. This initiates the final phase of the configuration process. The acknowledgement phase involves sending a DHCPACK packet to the client. This packet includes the lease duration and any other configuration information that the client might have requested. At this point, the IP configuration process is complete.

The server acknowledges the request and sends the acknowledgement to the client. The system as a whole expects the client to configure its network interface with the supplied options.

DHCPDISCOVER
Src=0.0.0.0
sPort=68 Dest=255.255.255.255 dPort=67
OP HTYPE HLEN HOPS
0x01 0x01 0x06 0x00
XID
0x3903F326
SECS FLAGS
0x0000 0x0000
CIADDR
0x00000000
YIADDR
0x00000000
SIADDR
0x00000000
GIADDR
0x00000000
CHADDR
0x00053C04
0x8D590000
0x00000000
0x00000000
192 octets of 0's. legacy
Magic Cookie
0x63825363
DHCP Options
DHCP option 53: DHCP Discover
DHCP option 50: 192.168.1.100 requested
DHCPOFFER
UDP Src=192.168.1.1
sPort=67 Dest=192.168.1.100 dPort=68
OP HTYPE HLEN HOPS
0x02 0x01 0x06 0x00
XID
0x3903F326
SECS FLAGS
0x0000 0x0000
CIADDR
0x00000000
YIADDR
0xC0A80164
SIADDR
0x00000000
GIADDR
0x00000000
CHADDR
0x00053C04
0x8D590000
0x00000000
0x00000000
192 octets of 0's. legacy
Magic Cookie
0x63825363
DHCP Options
DHCP option 53: DHCP Offer
DHCP option 1: 255.255.255.0 subnet mask
DHCP option 3: 192.168.1.1 router
DHCP option 51: 1 day IP lease time
DHCP option 54: 192.168.1.1 DHCP server
DHCPREQUEST
UDP Src=0.0.0.0
sPort=68 Dest=255.255.255.255 dPort=67
OP HTYPE HLEN HOPS
0x01 0x01 0x06 0x00
XID
0x3903F326
SECS FLAGS
0x0000 0x0000
CIADDR
0x00000000
YIADDR
0x00000000
SIADDR
0x00000000
GIADDR
0x00000000
CHADDR
0x00053C04
0x8D590000
0x00000000
0x00000000
192 octets of 0's. legacy
Magic Cookie
0x63825363
DHCP Options
DHCP option 53: DHCP Request
DHCP option 50: 192.168.1.100 requested
DHCP option 54: 192.168.1.1 DHCP server.
DHCPACK
UDP Src=192.168.1.1
sPort=67 Dest=192.168.1.100 dPort=68
OP HTYPE HLEN HOPS
0x02 0x01 0x06 0x00
XID
0x3903F326
SECS FLAGS
0x0000 0x0000
CIADDR (Client IP Address)
0x00000000
YIADDR (Your IP Address)
0xC0A80164
SIADDR (Server IP Address)
0x00000000
GIADDR (Gateway IP Address switched by relay)
0x00000000
CHADDR (Client Hardware Address)
0x00053C04
0x8D590000
0x00000000
0x00000000
192 octets of 0's. legacy
Magic Cookie
0x63825363
DHCP Options
DHCP option 53: DHCP ACK
DHCP option 1: 255.255.255.0 subnet mask
DHCP option 3: 192.168.1.1 router
DHCP option 51: 1 day IP lease time
DHCP option 54: 192.168.1.1 DHCP server

DHCP information

The client to the DHCP server: either to request more information than the server sent with the original DHCPACK; or to repeat data for a particular application - for example, browsers use DHCP Inform to obtain web proxy settings via . Such queries do not cause the DHCP server to refresh the IP expiry time in its database.

DHCP releasing

The client sends a request to the DHCP server to release the DHCP and the client unconfigures its IP address. As clients usually do not know when users may unplug them from the network, the protocol does not mandate the sending of DHCP Release.


Field      DHCPDISCOVER          DHCPREQUEST           DHCPDECLINE,
DHCPINFORM DHCPRELEASE
----- ------------ ----------- -----------
'op' BOOTREQUEST BOOTREQUEST BOOTREQUEST
'htype' (From "Assigned Numbers" RFC)
'hlen' (Hardware address length in octets)
'hops' 0 0 0
'xid' selected by client 'xid' from server selected by
DHCPOFFER message client
'secs' 0 or seconds since 0 or seconds since 0
DHCP process started DHCP process started
'flags' Set 'BROADCAST' Set 'BROADCAST' 0
flag if client flag if client
requires broadcast requires broadcast
reply reply
'ciaddr' 0 (DHCPDISCOVER) 0 or client's 0 (DHCPDECLINE)
client's network address client's network
network address (BOUND/RENEW/REBIND) address
(DHCPINFORM) (DHCPRELEASE)
'yiaddr' 0 0 0
'siaddr' 0 0 0
'giaddr' 0 0 0
'chaddr' client's hardware client's hardware client's hardware
address address address
'sname' options, if options, if (unused)
indicated in indicated in
'sname/file' 'sname/file'
option; otherwise option; otherwise
unused unused
'file' options, if options, if (unused)
indicated in indicated in
'sname/file' 'sname/file'
option; otherwise option; otherwise
unused unused
'options' options options (unused)

Option DHCPDISCOVER DHCPREQUEST DHCPDECLINE,
DHCPINFORM DHCPRELEASE
------ ------------ ----------- -----------
Requested IP address MAY MUST (in MUST
(DISCOVER) SELECTING or (DHCPDECLINE),
MUST NOT INIT-REBOOT) MUST NOT
(INFORM) MUST NOT (in (DHCPRELEASE)
BOUND or
RENEWING)
IP address lease time MAY MAY MUST NOT
(DISCOVER)
MUST NOT
(INFORM)
Use 'file'/'sname' fields MAY MAY MAY
DHCP message type DHCPDISCOVER/ DHCPREQUEST DHCPDECLINE/
DHCPINFORM DHCPRELEASE
Client identifier MAY MAY MAY
Vendor class identifier MAY MAY MUST NOT
Server identifier MUST NOT MUST (after MUST
SELECTING)
MUST NOT (after
INIT-REBOOT,
BOUND, RENEWING
or REBINDING)
Parameter request list MAY MAY MUST NOT
Maximum message size MAY MAY MUST NOT
Message SHOULD NOT SHOULD NOT SHOULD
Site-specific MAY MAY MUST NOT
All others MAY MAY MUST NOT

   在网络上search的时候你会发现server to client到底是broadcast还是unicast众说分云.感该下国内的网络环境.这里直接接用了一朋友的网友的一段话.我努力想在rfc文档中找到权威解释,可惜...也许我该补补E文了.
   server 维护了一张表,表里有请求者的mac 与 offered ip, 如果一个dhcp request 的src-mac 在这张表里,offer 就是unicast, 因为这时client 是有ip 的;如果是一个新的client, client 会先发一个dhcp discover, server 收到后会回一个 dhcp offer,这个offer 是broadcast,因为这时client 还没有ip, client 收到 这个offer 后会发一个dhcp request, 然后server 给一个 dhcp ack。 这两个报文可以是broadcast or unicast, based on bootp flags.
   在rfc文档中倒是找到个client to server,broadcast还是unicast的.
  

Use of broadcast and unicast

The DHCP client broadcasts DHCPDISCOVER, DHCPREQUEST and DHCPINFORM
messages, unless the client knows the address of a DHCP server. The
client unicasts DHCPRELEASE messages to the server. Because the
client is declining the use of the IP address supplied by the server,
the client broadcasts DHCPDECLINE messages.

When the DHCP client knows the address of a DHCP server, in either
INIT or REBOOTING state, the client may use that address in the
DHCPDISCOVER or DHCPREQUEST rather than the IP broadcast address.
The client may also use unicast to send DHCPINFORM messages to a
known DHCP server. If the client receives no response to DHCP
messages sent to the IP address of a known DHCP server, the DHCP
client reverts to using the IP broadcast address.
   想想应该也就是这么个道理吧.你在有ip的情况下server发包,server就应该unicast了吧.
  
   dhcp介绍就废话这么多了.有机会跟大家分享下个dhcp client代码.算是在这里给自己下个任务吧.自己写个dhcp client code和linux飞鸽传输
代码,gtk和qt现在还不是太会,linux飞鸽尽量GUI的,实在不行也就只能terminal的了.
    文档看得头有点大,先休息会了.

  
阅读(2706) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-10-28 17:19:37

不早说啊,我这边很多文档的,可以借鉴下的 不过还是有点研究啊,顶一个