Server端对于租赁出去的IP的管理是基于结构体dhcpOfferedAddr的,该结构体的定义是在leases.c文件里:(结构体的成员介绍说明见详解之数据结构)
[cpp] view plaincopy
- struct dhcpOfferedAddr {
- uint8_t chaddr[16];
- uint32_t yiaddr; /* network order */
- uint32_t expires; /* host order */
- };
在dhcpd.c文件里用该结构体定义指向该结构数组的指针leases;
/* dhcpd.c */
#include <leases.h>
struct dhcpOfferedAddr *leases;
在读取完配置信息server_config后,就可以依据配置信息的内存为该结构数组申请内存空间了:
[cpp] view plaincopy
- /*
- * strut dhcpOfferedAddr {
- * uint8_t chaddr[16];
- * uint32_t yiaddr; //network order
- * uint32_t expires; //host order
- * };
- *
- * 该结构数组leases是记录租赁出去的IP, yiaddr
- * 租赁IP客户机的MAC, charddr
- * 租赁的到期时间,expires(从1970年1月1日00:00:00到租赁到期的总共的秒数)
- *
- * dhcp server启动后(可能是异常重启)
- * 所以要充server_config.file里读取上次为哪些客户机分配了IP
- * 并把读取到的内容添加到leases数组里
- */
- leases = xzalloc(server_config.max_leases * sizeof(struct dhcpOfferedAddr));
- read_leases(server_config.lease_file);
- //server_config.lease_file = "/var/lib/misc/udhcpd.leases"
2). 查找租赁最早到期的租约:
[cpp] view plaincopy
- /*
- * add a lease into the table, clearing out any old ones
- * add_lease是把MAC:chaddr, IP : yiaddr, expires: lease
- * 租赁信息添加到leases数组里
- * 函数首先调用clear_lease把数组里旧的删除掉(与chaddr or yiaddr相同的)
- * 从leases数组里找到最早到期的,再添加
- *
- * 返回oldest (NULL 表示未添加成功)
- */
- struct dhcpOfferedAddr *add_lease(uint8_t *chaddr, uint32_t yiaddr, unsigned long lease)
- {
- struct dhcpOfferedAddr *oldest;
- /* clean out any old ones */
- clear_lease(chaddr, yiaddr);
- /* 从leases数组里找到一个最早到期的oldest,以便添加新的 */
- oldest = oldest_expired_lease();
- if (oldest) //oldest != NULL ,Found...
- {
- memcpy(oldest->chaddr, chaddr, 16);
- oldest->yiaddr = yiaddr;
- /*
- * 函数的第三个参数,即lease,表示的是租赁的剩余时间
- * 在动态租赁数组leases里记录的租赁到期时间
- *
- * 所以 oldest->expires = time(0) + lease;
- */
- oldest->expires = time(0) + lease;
- }
- return oldest;
- }
3). 通过MAC或IP查找租约:
[cpp] view plaincopy
- /* Find the first lease that matches chaddr, NULL if no match */
- struct dhcpOfferedAddr *find_lease_by_chaddr(uint8_t *chaddr)
- {
- unsigned int i;
- for (i = 0; i < server_config.max_leases; i++)
- if (!memcmp(leases[i].chaddr, chaddr, 16)) return &(leases[i]);
- return NULL;
- }
- /* Find the first lease that matches yiaddr, NULL is no match */
- struct dhcpOfferedAddr *find_lease_by_yiaddr(uint32_t yiaddr)
- {
- unsigned int i;
- for (i = 0; i < server_config.max_leases; i++)
- if (leases[i].yiaddr == yiaddr) return &(leases[i]);
- return NULL;
- }
4). 检查IP addr是否被网络中主机所使用,若被使用的话添加到leases数组里:
[cpp] view plaincopy
- /*
- * check is an IP is taken, if it is, add it to the lease table
- *
- * 检查addr is a free IP(网络中是否有主机使用addr)
- * if addr is a used IP, 把addr添加到租赁数组里
- *
- * 1. 返回0 表示addr 是a free IP
- * 2. 返回1表示addr已被网络中的某台主机使用了
- * 并且把使用addr的主机添加到leases数组里
- * 其中MAC: blank_chaddr, IP: addr, expires: server_config.confict_time(def: 3600sec)
- *
- */
- static int check_ip(uint32_t addr)
- {
- struct in_addr temp;
- /* arpping()返回0表示addr used */
- if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) {
- temp.s_addr = addr;
- LOG(LOG_INFO, "%s belongs to someone, reserving it for %ld seconds",
- inet_ntoa(temp), server_config.conflict_time);
- /*
- * 因为刚在未在leases数组里找到这个已被used 的IP
- * 所以要把这个used IP添加到leases数组里
- * 租赁客户机MAC 为blank_chaddr(黑户)
- * 租赁的IP即used IP(addr)
- * 租赁时间:server_config.conflict_time
- */
- add_lease(blank_chaddr, addr, server_config.conflict_time);
- return 1;
- } else return 0;
- }
5) 从可用IP地址池里找到一个可用的IP(a Free IP)
uint32_t find_address(int check_expire);
该函数的调用是在,server端接收到DHCPDISOCVER的报文的时候,会为client提供一个IP地址:
a) server首先利用client的MAC地址在leases数组里查找该client以前是否在这里租赁过IP,租赁过的话,把以前的IP提供给client
b) 第一种情况不满足的话,server会检查DHCPDISCOVER报文的选项字段,client是否有请求的IP(该选项信息的CODE :DHCP_REQUESTED_IP),有的话检查该IP是否为Free,可以的话把Request IP提供给client。
c) 上面两种情况都不满足的话,就调用find_address这个函数了。
[cpp] view plaincopy
- /*
- * find an assignable address, it check_expired is true,
- * we check all the expired leases as well.
- * Maybe this should try expired leases by age...
- *
- * 找到一个可分配的IP,如果check_expired is true,
- * 会检查所有到期租约
- * find_address()函数找到free IP 返回IP的值(network order)
- * 未找到返回0
- *
- */
- uint32_t find_address(int check_expired)
- {
- uint32_t addr, ret;
- struct dhcpOfferedAddr *lease = NULL;
- /*
- * 在端上进行计算比较的时候使用 host order
- * 一般存储的时候采用 network order
- *
- * 遍历整个可分配地址server_config.start -- server_config.end
- */
- /* addr is in host order here */
- addr = ntohl(server_config.start);
- for (;addr <= ntohl(server_config.end); addr++)
- {
- /* ie, 192.168.55.0 */
- if (!(addr & 0xFF)) continue;
- /* ie, 192.168.55.255 */
- if ((addr & 0xFF) == 0xFF) continue;
- /*
- * Only do if it isn't
- * an assigned as a static lease
- * rexervedIP()检查addr是否在静态租赁链表里
- * addr在静态链表的话,函数返回1,反之返回0
- */
- if(!reservedIp(server_config.static_leases, htonl(addr)))
- {
- /* lease is not taken */
- ret = htonl(addr);
- /* !(lease = find_lease_by_yiaddr(ret)) 地址addr是否已被租赁出去了 */
- if ((!(lease = find_lease_by_yiaddr(ret)) ||
- /*
- * or it expired and we are checking for expired leases
- * or 租赁出去的话,
- * addr是否是a free IP还取决与
- * lease_expired(lease)是否已到期和check_expired
- *
- */
- (check_expired && lease_expired(lease))) &&
- /*
- * and it isn't on the network
- * 如果addr满足上面两个条件的其中一个
- * 1.!(lease = find_lease_by_yiaddr(ret)) 没有被租赁出去 * 2.(check_expired && lease_expired(lease)) 已被租赁出去了,
- * 但租赁过期了(check_expired == ture)
- *
- * addr 满足上面条件之一,还要检查addr是否被网络上其他主机使用
- *
- * check_ip() 发送arp Request包确认addr是否被使用
- * 返回0, addr is a free IP
- */
- !check_ip(ret))
- {
- return ret;
- break;
- }
- }
- }
- return 0;
- }