1:重要的结构体
读取配置文件信息到全局的结构体struct server_config_t server_config中,这个结构在很多文件中都有引用到很重要。
/* dhcpd.h */
struct server_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 */ unsigned char arp[6]; /* Our arp address */ unsigned long lease; /* lease time in seconds (host order) */ unsigned long 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 */ unsigned long auto_time; /* how long should udhcpd wait before writing a config file. if this is zero, it will only write one on SIGUSR1 */ unsigned long decline_time;/* how long an address is reserved if a client returns a decline message */ unsigned long conflict_time;/* how long an arp conflict offender is leased for */ unsigned long offer_time; /* how long an offered address is reserved */ unsigned long 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 */
struct option_set { unsigned char *data; struct option_set *next; };
2:读入配置文件
/* dhcpd.c */
#ifdef COMBINED_BINARY int udhcpd_main(int argc, char *argv[]) #else int main(int argc, char *argv[]) #endif { fd_set rfds; struct timeval tv; int server_socket = -1; int bytes, retval; struct dhcpMessage packet; unsigned char *state; unsigned char *server_id, *requested; u_int32_t server_id_align, requested_align; unsigned long timeout_end; struct option_set *option; struct dhcpOfferedAddr *lease; int pid_fd; int max_sock; int sig; OPEN_LOG("udhcpd"); LOG(LOG_INFO, "udhcp server (v%s) started", VERSION); memset(&server_config, 0, sizeof(struct server_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 )value'的格式(特殊:opt key(空格or )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; char buffer[80], orig[80], *token, *line; int i; /* 先将默认配置解析到server_config<全局的配置信息结构体>结构中 */ for (i = 0; strlen(keywords[i].keyword); i++) if (strlen(keywords[i].def)) keywords[i].handler(keywords[i].def, keywords[i].var); if (!(in = fopen(file, "r"))) { LOG(LOG_ERR, "unable to open config file: %s", file); return 0; } /* 将外部配置文件一行一行解析 */ while (fgets(buffer, 80, in)) { /* fgets可能会将' '读入,如有就将其替换为' ' */ if (strchr(buffer, ' ')) *(strchr(buffer, ' ')) = '