1:重要的结构体
读取配置文件信息到全局的结构体struct server_config_t server_config中,这个结构在很多文件中都有引用到很重要。
/* dhcpd.h */
structserver_config_t {
u_int32_t server;/*Our IP, in network order*/u_int32_t start;/*Start address of leases, network order*/u_int32_t end;/*End of leases, network order*/
struct option_set *options;/*List of DHCP options loaded from the config file*/
char *interface; /*The name of the interface to use*/
int ifindex; /*Index number of the interface to use*/unsignedchar arp[6]; /*Our arp address*/unsignedlong lease; /*lease time in seconds (host order)*/unsignedlong max_leases; /*maximum number of leases (including reserved address)*/
char remaining; /*should the lease file be interpreted as lease time remaining, or
as the time the lease expires*/unsignedlong auto_time; /*how long should udhcpd wait before writing a config file.
if this is zero, it will only write one on SIGUSR1*/unsignedlong decline_time;/*how long an address is reserved if a client returns a
decline message*/unsignedlong conflict_time;/*how long an arp conflict offender is leased for*/unsignedlong offer_time; /*how long an offered address is reserved*/unsignedlong min_lease; /*minimum lease a client can request*/
char *lease_file;char *pidfile;char *notify_file; /*What to run whenever leases are written*/u_int32_t siaddr;/*next server bootp option*/
char *sname; /*bootp server name*/
char *boot_file; /*bootp boot file option*/};
英文释意也很明白,比较重要的有struct option_set *options;成员,它是一个指向记录配置文件中对opt配置的链表的指针,并且data以CLV方式存储,结构如下:
/* dhcpd.h */
structoption_set {
unsignedchar *data;struct option_set *next;
};
2:读入配置文件
/* dhcpd.c */
#ifdef COMBINED_BINARYint udhcpd_main(int argc, char *argv[])#else
int main(int argc, char *argv[])#endif{
fd_set rfds;structtimeval tv;int server_socket = -1;intbytes, retval;structdhcpMessage packet;
unsignedchar *state;
unsignedchar *server_id, *requested;
u_int32_t server_id_align, requested_align;
unsignedlongtimeout_end;struct option_set *option;struct dhcpOfferedAddr *lease;intpid_fd;intmax_sock;intsig;
OPEN_LOG("udhcpd");
LOG(LOG_INFO,"udhcp server (v%s) started", VERSION);
memset(&server_config, 0, sizeof(structserver_config_t));/*读取配置文件到server_config结构中供全局使用*/
if (argc < 2)
read_config(DHCPD_CONF_FILE);/*use default config file*/
else read_config(argv[1]);/*use designated config file*/
在files.c里的read_config函数是读取的入口:
/* files.c */
/*配置文件每一行的格式为'key(空格or\t)value'的格式(特殊:opt key(空格or\t)value),value值的类型有以下几种
分别对应以下的处理方法
value值类型 处理方法
ip read_ip
string read_str
number read_u32
yes/no read_yn
opt read_opt*/
int read_config(char *file)
{
FILE*in;cha