vtun 读配置文件

在main函数中,reread_config(0);

经过下面的分析,知reread_config(0);是将每个会话信息作为一个结点存于host_list中(一个会话信息包含默认信息和本会话特有信息,且本会话信息会覆盖默认的相同选项)。

void reread_config(int sig)
{
    if( !read_config(vtun.cfg_file) )
    {
        vtun_syslog(LOG_ERR,"No hosts defined");
        exit(1);
    }
}

这个函数只用了两次,一次是main函数中,一次是server的挂起信号处理函数中。

即server端挂起要重读配置文件。

read_config(vtun.cfg_file) 在cfg_file.y 中,

int read_config(char *file)
{
   static int cfg_loaded = 0;
   extern FILE *yyin;

   if( cfg_loaded ){
      free_host_list();
      vtun_syslog(LOG_INFO,"Reloading configuration file");
   }    
   cfg_loaded = 1;

   llist_init(&host_list);

   if( !(yyin = fopen(file,"r")) ){
      vtun_syslog(LOG_ERR,"Can not open %s", file);
      return -1;     
   }

   yyparse();

   free_host(&default_host, NULL);

   fclose(yyin);
   return !llist_empty(&host_list);    
}

看llist_init(&host_list);

void llist_init(llist *l)
{
    l->head = l->tail = NULL;
}

分析host_list;

llist host_list;即host_list中有两个链表结点。

 

host_list在下面函数被改变,

llist_add(&host_list, (void *)parse_host);

struct vtun_host *parse_host;

猜测parse_host先是存放了option  default以及main中对全局变量的初始化信息,

然后存放每个会话的信息,即将每个会话的信息加上默认信息再赋给host_list.

每个会话信息会覆盖默认信息。

如何实现读配置文件的呢?

有关lex和yacc的使用见其他文章。

源码包中与读配置文件有关的文件cfg_file.y  cfg_file.l   cfg_kwords.h     llist.c llist.h其他cfg….文件是编译生成的。

会lex的yacc的使用,则此处读配置文件很好理解了。

cfg_file.y中vtun和default_host都是在main中定义和初始化的。

 

 

最终结论:host_list存放所有会话的信息。

配置文件中的命令在tunnel.c中运行。

 

 

如何让配置文件和源文件分离?

原文地址:https://www.cnblogs.com/helloweworld/p/2709183.html