Igmpproxy source code learning-load loadConfig and loadconfig for configuration information

Source: Internet
Author: User
Tags fread

Igmpproxy source code learning-load loadConfig and loadconfig for configuration information


Before the igmpproxy main program runs, you need to read the configuration file. The configuration file of igmpproxy is usually/etc/igmpproxy. conf or/var/igmpproxy. conf. Its content is as follows:

 
 
  1. quickleave
  2. mode 3
  3. phyint ppp0 upstream ratelimit 0 threshold 1
  4. phyint br0 downstream ratelimit 0 threshold 1
The main function calls loadConfig () to load the configuration file information. The code for loading the configuration file is mainly in config. c. Before analyzing the specific implementation of loadConfig, we should first understand the struct vifconfig in config. c. This struct records configurations such as the igmpproxy port.
 
 
  1. struct vifconfig {
  2. Char * name; // port name, such as eth0
  3. Invalid state; // port status 0-Invalid Port 1
  4. Intratelimit; // The access rate limit.
  5. Intthreshold; // TTl threshold
  6. // Keep allowed nets for VIF.
  7. Struct SubnetList * allowednets; // structure of the subnet IP address and subnet mask
  8. // Next config in list...
  9. Struct vifconfig * next; // next node
  10. };
In config. c, the global variable vifconf of struct vifconfig is defined. This global variable saves the configuration information we read from the configuration file.The main function of laodConfig () is to record the information in the configuration file in a linked list that can be accessed through vifconf. Then the main function calls igmpProxyInit (). This function accesses vifconf for related configuration. Of course, igmpProxyInit () has other functions.
LoadConfig LoadConfigUnderstand the main process of loading configuration information.
Void initCommonConfig ()
LoadConfig first calls the initCommonConfig () function to initialize some public constants.
OpenConfigFile (configFile)
Opening the configuration file is easy to understand. In addition to opening the configuration file, this function also allocates memory space for the global pointer variable iBuffer. IBuffer is used to save the 512 bytes read from the configuration file each time. The implementation of igmpproxy's loading configuration information is: each time the data read from the configuration file up to 512 bytes (in fact one row may be read) is put in IBuffer, then parse Every token from iBuffer (for more details about iBuffer in nextConfigToken ). NextConfigToken ()
As the name suggests, nextConfigToken () is used to parse a token from igmpproxy. conf. However, not every token accesses igmpproxy once. in conf, The READ_BUFFER_SIZE data is retrieved at a time in the buffer zone iBuffer, and then parsed from a word in iBuffer. When the 512 bytes are parsed, (bufPtr = readSize) is true, and fread generates 512 bytes of data.
If igmpproxy. conf is read and all the read data is parsed in nextConfigToken, (readSize <READ_BUFFER_SIZE & bufPtr = readSize) is true, then NULL is returned. Because nextConfigToken () uses the buffer iBuffer, it does not access igmpproxy. conf every time it runs. When running nextConfigToken for the first time, we must execute fread from the next igmpproxy. conf reads 512 bytes of data. When we execute nextConfigToken for the second time, because the data we read for the first time in iBuffer has not been used up, we do not need to use fread once. Core loadConfig Process
In the loadConfig () function, after the first execution of nextConfigToken to obtain a token, we enter the core process of loadConfig, that is, the configuration information parsing and the member variables that parse the configuration information into the struct vifconfig are stored in the global variable vifconf (such as port information, IP address, and so on ). The correct configuration file may contain three types of information, namely phyint, quickleave, and mode. LoadConfig matches the token with the three words to determine what kind of processing is required. (The official source code I downloaded does not support mode. This can be implemented by modifying the source code. We can also configure the igmpproxy file. write Other configuration information in conf, and then add the processing method in loadConfig)
If you find that the current read information is about phyint, you can call parsePhyintToken () to parse the information according to phyint rules and save the information in the form of struct vifconfig to the pointer tmpPtr, Then pass CurrPtr String to vifconf. The following sections describe parsePhyintToken. If quickleave is read, it is written to commonConfig. fastUpstreamLeave = 1 in public configuration;
ParsePhyintToken
The return value is a struct pointer of the struct vifconfig type. When you enter parsePhyintToken, the token must be the name of the network interface, for example (eth0, ppp0, br0). Therefore, the size of the first token cannot exceed sizeof (struct ifreq *) NULL) -> ifr_name ). Next, initialize the member variables of the tmpPtr struct pointer.
 
 
  1. tmpPtr->next = NULL; // Important to avoid seg fault...
  2. tmpPtr->ratelimit = 0;
  3. tmpPtr->threshold = 1;
  4. tmpPtr->state = IF_STATE_DOWNSTREAM;
  5. tmpPtr->allowednets = NULL
After initialization, copy the token (the interface name at this time) to tmpPtr-> name
 
 
  1.  strcpy(tmpPtr->name, token);
Then, use keywords such as altnet and upstream to match the tokens one by one. To match altnet, you need to use parseSubnetAddress for further parsing:
 
 
  1. *anetPtr = parseSubnetAddress(token);
When upstream, downstream, disabled, ratelimit, and threshold are matched, the member variables of tmpPtr are directly set. For example:
 
 
  1. else if(strcmp("upstream", token)==0) {
  2. // Upstream
  3. IF_DEBUG log(LOG_DEBUG, 0, "Config: IF: Got upstream token.");
  4. tmpPtr->state = IF_STATE_UPSTREAM;
ParseSubnetAddress
Resolve the subnet IP address in the format of a. B. c. d/n to the struct SubnetList.
 
 
  1. struct SubnetList {
  2. uint32 subnet_addr;
  3. uint32 subnet_mask;
  4. struct SubnetList* next;
  5. }


GetCurrentConfigToken
It is easy to explain whether the token is valid.


Summary: LoadConfig main loop.
 
 
  1. nextConfigToken()
  2. while(){
  3. phyint:
  4. parsePhyintToken()
  5. quickleave/mode:
  6. Directly modify public configurations
  7. }
ParsePhyintToken stores phyint information in vifconf through a linked list.









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.