udhcpd解析域名_udhcpd源码分析2--读取配置文件

本文详细介绍了udhcpd如何读取和解析配置文件,重点解析了重要的struct server_config_t结构体以及配置文件的处理流程。通过read_config函数逐行读取配置文件,利用C语言的字符串处理函数巧妙地提取关键信息,并通过配置关键字结构体数组进行匹配和处理。此外,文章还探讨了选项设置的存储方式,特别是CLV方式存储的option_set链表,以及attach_option函数中对升序链表的操作。这些实现方式提供了宝贵的学习和参考价值。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值