Http://blog.chinaunix.net/uid-21568264-id-203665.html1) Zebra is a very famous Linux open source routing software project, code written very beautiful, modular, very clear structure. I will not talk about the software framework. I have explained it in detail on the official zebra website. To put it simply, Zebra serves as a daemon to maintain Linux routing information, and rip is used by other modules, BGP and the daemon update and obtain route information through message communication. 2) The project mainly uses the RIP Protocol function. 3) Each module is actually a single process. In the process, thread blocks are executed in turn, and it seems to be running concurrently; each module has a master structure, which maintains several thread lists, including event, read, and write. When necessary, each thread block structure is added to the corresponding list, and then the main process will continuously poll to execute every thread block in the list. 4) command list is essentially a function pointer, key Code # define defun (funcname, role name, role STR, helpstr )\
Int funcname (struct cmd_element *, struct vty *, Int, char **);\
Struct cmd_element partition name = \
{\
Reverse STR ,\
Funcname ,\
Helpstr \
};\
Int funcname \
(Struct cmd_element * Self, struct vty * vty, int argc, char ** argv) when reading the input command, this command string may come from the user input or the configuration file, match the command node in the corresponding command list according to the command string information. Each command node will know a function pointer and call this function to execute the corresponding command. 5) the working process is as follows, first, start the zebra module, and then start the corresponding routing protocol module. We started the rip module. When we started the rip module, we specified the configuration file, the configuration file is generated based on the WAN connection status of the current device, for example, interface br0
Router rip
Redistribute Kernel
Redistribute connected
Redistribute static
Network br0
Interface br0
IP rip send Version 2
IP rip receive Version 2
Interface ipoa0
Router rip
Redistribute Kernel
Redistribute connected
Redistribute static
Network ipoa0
Interface ipoa0
IP rip send Version 2
IP rip receive version 2 obviously our device has a br0 bridge interface and an ipoa0 WAN connection. when parsing the configuration file, the rip module will execute each line in sequence, each line is actually a command. For example, "router rip" corresponds to the "router_rip" function in the command list. In the code, it is the defun (router_rip,
Router_rip_cmd,
"Router rip ",
"Enable a routing process \ n"
"Routing information protocol (RIP) \ n") The defun definition has been mentioned above. What does that function do? In fact, it is very simple. In fact, it creates a socket. According to the RFC documentation of the RIP Protocol, it listens to port 520 and creates two thread blocks. rip_event (rip_read, rip-> sock );
Rip_event (rip_update_event, 1); the two threads are not actually created, but are only in the master's tread list, so that the rip process will execute/* execute each thread .*/
While (thread_fetch (master, & Thread ))
Thread_call (& Thread); 6) communicates with the zebra module. When Rip executes the redistribute kernel command, it actually sends a message to Zebra, because the final route information is maintained by the zebra daemon, the message format is defined, int ret;
Struct stream * s; S = stream_new (zebra_max_packet_siz);/* total length of the messages .*/
Stream_putw (S, 4 );
Stream_putc (S, command );
Stream_putc (S, type); ret = writen (sock, S-> data, 4); stream_free (s); struct stream is the definition of the message. In fact, the communication between modules can be TCP/UDP communication or UNIX socket communication. This can be defined by a macro. After understanding this, the rest is the understanding of the Protocol, I have mastered network programming in Linux.