00. Table of Contents
Article Directory
01. Introduction to libevent
What is libevent
Libevent is a lightweight open source high-performance event notification library written in C language. It has the following highlights: event-driven, high performance; lightweight, focusing on the network, not as good as ACE Bloated and huge; source code is fairly refined and easy to read; cross-platform, supports Windows, Linux, *BSD and Mac Os; supports multiple I/O multiplexing technologies, such as epoll, poll, dev/poll, select and kqueue; Support events such as I/O, timers and signals; register event priority.
Chromium, Memcached, NTP, HTTPSQS and other famous open source programs all use the libevent library, which shows the stability of libevent. More programs that use libevent can be viewed on the official website of libevent.
Libevent main components
Libevent includes event management, cache management, DNS, HTTP, and cache events. Event management includes various IO (socket), timers, signals and other events; cache management refers to the evbuffer function; DNS is an asynchronous DNS query function provided by libevent; HTTP is a lightweight http implementation of libevent, including servers and clients end. Libevent also supports ssl, which is very important for network programs with security requirements, but its support is not very complete. For example, the implementation of http server does not support ssl.
Libevent core implementation
Reactor (reactor) mode is the core framework of libevent. Libevent is event-driven and automatically triggers the callback function. The source code of the epoll reactor introduced earlier is extracted from libevent.
02. Benefits of Libevent
Learning libevent helps to improve programming skills. In addition to network programming, there are many useful design skills and basic data structures in Libevent's code, such as information hiding, function pointers, polymorphic support in C language, linked lists and heaps And so on, all help to improve their C language skills.
Program design not only needs to understand the framework, many details are also the key to the success or failure of the entire system. Only a general understanding of the framework of libevent itself may be only a little understanding. Without in-depth code analysis, it is difficult to understand the ingenuity of its design, and it is difficult to use it for yourself.
03. Installation and testing of Libevent
Official website: http://libevent.org
Source package download:
-
- 1.4.x series, relatively early version, suitable for source code learning
-
- 2.x series, the newer version, the code volume is much more than the 1.4 version, the function is more complete.
For the installation of the source code package, take version 2.0.22 as an example. The source code package libevent-2.0.22-stable.tar.gz can be downloaded from the official website. The basic installation steps are basically the same as those of the third-party library source package.
The installation steps are as follows:
Step 1: unzip
deng@itcast:~$ tar -xvf libevent-2.0.22-stable.tar.gz
Step 2: Enter the corresponding directory
deng@itcast:~$ cd libevent-2.0.22-stable/
Step 3: Check the environment and generate Makefile
deng@itcast:~/libevent-2.0.22-stable$ ./configure
Step 4: Compile and generate dynamic and static libraries
deng@itcast:~/libevent-2.0.22-stable$ make
Step 5: The installation must use administrator permissions
deng@itcast:~/libevent-2.0.22-stable$ sudo make install
Step 6: Test and execute hello-world
deng@itcast:~/libevent-2.0.22-stable$ cd sample/
deng@itcast:~/libevent-2.0.22-stable/sample$ pwd
/home/deng/libevent-2.0.22-stable/sampledeng@itcast:~/libevent-2.0.22-stable/sample$ ./hello-world
In addition, open a terminal and enter the following command
deng@itcast:~/libevent-2.0.22-stable/sample$ nc 192.168.73.44 9995
Hello, World!
If it appears Hello, world! The software installation is ok...
The directory where the library is located: /usr/local/lib
The directory where the header files are located: /usr/local/include
04. Libevent success story
- Chromium -Google's open source web browser (uses Libevent on Mac and Linux)
- Memcached -a high-performance distributed memory object caching system
- Transmission -A fast, simple and free BitTorrent client
- NTP -Network time protocol to make your clock correct (use Libevent in SNTP)
- tmux -A clean, modern, BSD licensed terminal multiplexer, similar to GNU screen
- Tor -an anonymous internet communication system.
- libevhtp -A fast and flexible alternative to libevent's http client/server API
- Prosody -Jabber/XMPP server written in Lua
- PgBouncer -Lightweight connection pool for PostgreSQL
- redsocks -a simple transparent TCP -> Socks5 / HTTPS proxy daemon.
- Crawl is a small and efficient HTTP crawling tool
- Libio -Input/output abstraction library
- Honeyd -a virtual honeynet daemon-can be used to fight Internet worms .
- Fragroute -IDS testing tool
- Nylon -Nested proxy server
- Disconcert -A distributed computing framework for loosely coupled workstations.
- watchcatd -software watchdog, designed to take actions that are not as drastic as usual solutions to reset the machine.
- ScanSSH -Fast SSH server and open agent scanner.
- Nttlscan -Honeyd's network topology scanner.
- NetChat -a combination of netcat and ppp chat.
- Io -a small programming language; use libevent for network communication.
- Systrace -System call sandbox.
- SpyBye -detects malware on web pages.
- GreenSQL -a SQL database firewall.
- dnsscan -A fast scanner for identifying open recursive dns resolvers
- Kargo Event -PHP extension of libevent.
- Scytale -a database encryption tool.
05. Reference Materials
Official reference website: https://www.monkey.org/~provos/libevent/doxygen-2.0.1/index.html