# ch16: Boostrap Protocol

![16.BOOTP](https://tva1.sinaimg.cn/large/007S8ZIlly1ggln96izjmj311y0u0q87.jpg)

## Intro

RARP有两个问题：

* 只返回一个IP地址；
* 使用数据链路层，不能被路由器转发。

可以使用BOOTP，使用UDP协议，并与TFTP一起使用。

## BOOTP的分组格式

BOOTP数据封装在UDP中：

![](https://tva1.sinaimg.cn/large/007S8ZIlly1gglnb02nehj314e0es0v6.jpg)

数据一共300字节。

具体格式如下：

![](https://tva1.sinaimg.cn/large/007S8ZIlly1gglnbjfoloj30u00xwgtn.jpg)

* opcode：一个字节，1表示请求，2表示响应；
* hardware type：一个字节，1来标识以太网；
* hardware address：一个字节，6来标识以太网地址；
* hop count：跳数，被转发的时候增1。一个字节；
* transaction ID：4个字节，用来匹配请求和响应；
* number of seconds：2个字节，超时时间；
* （unused）：2个字节；
* client IP address：4个字节，client的IP地址，如果还不知道，就是0；这种情况server就会将分配给这个client的地址填入your IP address中；
* your IP address：4个字节，服务器分配给client的IP 地址；
* server IP address：服务器的IP地址；
* gateway IP address：如果中间经过了路由器，就是路由器的IP地址；
* client hardware address：16个字节，client的硬件地址，必填，只有填了这个服务器的UDP进程才能知道client的硬件地址；
* server hostname：64个字节，服务器的主机名；
* boot filename：128个字节，引导文件名字；
* vendor-specific information：64个字节，一个扩展字段，和特定厂商有关。

### Port Number

client使用端口68，服务器使用端口67。

client也使用一个固定的端口的原因是，reply可能是一个广播。

## BOOTP Serve Design

有一个问题，就是TFTP服务器不知道如何将一个响应返回给client，因为这个时候还不知道client的IP地址，也不能发起一个ARP请求。

两个解决办法：

* 服务器通过ioctl将分配给client的IP地址添加到内核的ARP缓存中；
* 服务器广播这个BOOTP响应。

## BOOTP Through a Router

BOOTP和RARP的一个不同就是BOOTP可以被路由器转发。

当被一个路由器转发后，路由器将自己的IP地址填到gateway IP address中，然后转发给真正的服务器。

服务器将响应返回router。

然后router返回给client。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://valineliu.gitbook.io/deuterium-wiki/reading/cs-jing-dian-shu-ji/tcpv1/ch-16-boostrap-protocol.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
