In the previous article, we analyzed the source code of example in ndpi. This time, we will try to gain an in-depth understanding of the structure and operation of ndpi. We will take the following three purposes (questions) to read the ndpi source code.
1. How does ndpi register and maintain the protocols that need to be checked internally?
2. What does ndpi do during initialization?
3. What Data Structure does ndpi use in the underlying implementation?
Note: Due to the limited length, this article analyzes the source code of the initialization section in use. The analysis functions and specific protocols of the subject will be described later. If you have any questions or do not understand them correctly, you are welcome to discuss them together.
I. Index
In the example (pcapreader) introduced above, we provide a very useful navigation. It is the setupdetection function, which basically contains the common functions used to initialize ndpi. In the following source code analysis, we use this function as an index. Gradually snoop into the inner of ndpi. The details are as follows:
In the above function, the operation is mainly performed on the first 11 rows. The operations following line 11 are mainly for the application requirements of pcapreader (in fact, we have already noted simple annotations ). Next we will introduce it in two phases.
Note: The title of section 2 and section 3 is selected only because of the specific gravity relationship. We will introduce the Macros in the source code below.
2. ndpi_init_detection_module
In this section, we will introduce the initialization function ndpi_init_detection_module in row 8th, and the definitions in the above 3rd will be introduced in the third part of the system. The implementation of ndpi_init_detection_module is in the ndpi_main.c file. This function does not maintain the protocol, but initializes more underlying parameters. When the function ends, the ndpi_detection_module_struct type pointer is returned. This type runs through the entire initialization process and provides the container for storing parameters and the set data. We will introduce them one by one:
Function prototype:
Struct ndpi_detection_module_struct * ndpi_init_detection_module (u_int32_t ticks_per_second,
Void * (* _ ndpi_malloc) (unsigned long size ),
Void (* _ ndpi_free) (void * PTR ),
Ndpi_debug_function_ptr ndpi_debug_printf)
1. Memory Management Function Initialization
_ Ndpi_malloc = _ ndpi_malloc;
_ Ndpi_free = _ ndpi_free;
Here _ ndpi_malloc and _ ndpi_free are our own defined functions, while _ ndpi_malloc and _ ndpi_free on the left of the equal sign do not refer to variables. Here is a function pointer encapsulated in NDI. It is defined in ndpi_main.c as follows:
2. debug function Initialization
Here, the ndpi_debug_printf function is used in two places. The first place is the error handling during memory application in step 3 downstairs. The second step is to pass in the debug function, which is similar to step 1. However, the debug information output function is not declared in ndpi_main.c, but in the ndpi_detection_module_struct structure. The ndpi_debug_printf function pointer is declared in the structure. For details, see the source code, which is not listed here.
3. Create and initialize the ndpi_detection_module_struct pointer
Here is the description of the ndpi_detection_module_struct pointer and the memory application through malloc. Here ndpi_detection_module_struct will return at the end of the function.
4. Protocol ing diagram Initialization
Ndpi_bitmask_reset (ndpi_str-> detection_bitmask );
This part is mainly implemented through the macro ndpi_bitmask_reset and the detection_bitmask inside the struct (ndpi_detection_module_struct. Detection_bitmask is actually an array of u_int32_t. ndpi_bitmask_reset is one of the macros defined in the ndpi_macros.h header file to maintain the registration protocol. At the underlying layer, you can use menset to set the detection_bitmask array to 0. This is the core content of the article, just like our big question. This article will be systematically introduced in the third part.
5. redis Initialization
Redis is a key-value storage system. Similar to memcached, memcached supports more storage value types, including string, list, set, and zset) and hash (hash type ). The function performs simple initialization (ndpi_detection_module_struct) using the redis variable of the struct (ndpi_str-> redis = NULL ;). This section is not detailed here. It is defined in ndpi_credis.h and ndpi_credis.c.
6. Protocol timeout value Initialization
In this function, a relatively large amount of space is used for timeout initialization. Here is actually nothing to explain, the timeout values of each application layer software.
7. AC algorithm initialization
The AC algorithm refers to the Aho-corasick automatic machine algorithm. In the header file # include <ahocorasick. h> of ndpi_main.h, ahocorasick is encapsulated and implemented in ahocorasick. c In ndpi. The regression here mainly involves initialization through the ac_automata_init function. The initialization here does not involve the algorithm itself, but creates and initializes the ac_autimata_t struct and passes it back to the struct (ndpi_detection_module_struct ).
8. LRU memory management Initialization
Ndpi_init_lru_cache (& ndpi_str-> skypecache, 4096 );
This is a deep hole in memory management. However, the functions implemented by ndpi_init_lru_cache alone are not very complex. You can check them out. In ndpi_cache.c.
9. multi-thread Initialization
Here is the multi-thread initialization. The pthread_mutex_init () function dynamically creates mutex locks. the ATTR parameter specifies the attribute for creating a mutex lock. If the ATTR parameter is null, the default mutex lock attribute is used. The default attribute is quick mutex. The mutex lock attribute is specified when the lock is created. There is only one lock type attribute in the linuxthreads implementation. Different lock types are different when trying to lock a locked mutex lock.
After the pthread_mutexattr_init () function is successfully completed, zero is returned. Any other returned values indicate an error. After the function is successfully executed, the mutex lock is initialized to the unlocked state.
10. initialize the default port
Ndpi_init_protocol_defaults (ndpi_str );
Here ndpi_str is the struct after initialization in the previous step (ndpi_detection_module_struct ). The main function of the ndpi_init_protocol_defaults function is to maintain a binary tree structure to record the default ports of each protocol. The repeatability in this function is relatively high. We stick a representative program section to explain it together:
Two functions are involved: ndpi_build_default_ports and ndpi_set_proto_defaults. The ndpi_build_default_ports operation is simple and not detailed. Here, it only passes the port number in the parameter to ports_a/ports_ B and returns it. This section describes how to implement the ndpi_set_proto_defaults function.
Note: The functions described above (10) are implemented in ndpi_main.h.
Iii. ndpi_set_protocol_detection_bitmask2
Before talking about this function, let's first introduce the origins of the parameters used by this function. That is, macro definitions and variables.
1. ndpi_protocol_bitmaskall
In fact, when we first saw this statement, the first response was still defined as a macro. However, ndpi_protocol_bitmask represents a variable type, while all represents an instance (variable) for definition processing ). The detailed definition is in ndpi_macros.h, as follows:
After reading the above definition, it is not difficult to find that the ndpi_protocol_bitmask type is a u_int32_t array. Let's continue to see how the array size ndpi_num_fds_bits is calculated:
A series of questions are raised here? Why (x) + (y)-1)/(y ))? What is the purpose of this definition?
According to my understanding, the data structure for maintaining protocol ing is the ndpi_protocol_bitmask_struct (u_int32_t array) mentioned above ). For each position of the array, such as fds_bits [1], the u_int32_t contains 4 bytes. That's 32 bits. Each represents the ing of this Protocol. This not only shows from the above definition, but in the next part, we can see that this is a hash-like ing structure. Then return to the question of why (x) + (y)-1)/(y). Here, Y is actually 32, so here we calculate the array to get an array size that exactly meets the requirements for storing protocol ing (of course, the number of digits of the array is not all applied and mapped, and there will be a waste of space after all)
2. ndpi_bitmask_set_all (all)
The main function of this macro is to set all the applications in the ing. However, this is just a superficial understanding. In ndpi_macros.h, ndpi provides sound ing setting functions. As follows:
Note: What should we do if we only want to register individual applications for detection? You can add them through the ndpi_bitmask_add macro. The number of hashes corresponding to the Protocol is defined in ndpi_protocols_osdpi.h. The limited space is not listed here.
3. ndpi_set_protocol_detection_bitmask2 (ndpi_struct, & all)
This can be said to be the core function of Protocol Registration. Like 10 in part 1, the repetition rate here is also relatively high. However, the dependencies between some protocols are involved, so we will list a typical code segment below. Let's take a look at what it actually does.
So far, the initialization analysis has basically been completed.
Reprint Please note: http://blog.csdn.net/grublinux/article/details/37670619
In addition, we are honored to have participated in the blog contest in this blog article pcapreader-source code analysis. If you think it is helpful to you, please support me. Voting address: http: // vote.Blog. csdn. neT/article/deTails? ArticlEid = 31603915