本系列的文章是基于libnet 1.1.5 版本.
下面我们来看libnet_init()函数:
/**
* Creates the libnet environment. It initializes the library and returns a
* libnet context. If the injection_type is LIBNET_LINK or LIBNET_LINK_ADV, the
* function initializes the injection primitives for the link-layer interface
* enabling the application programmer to build packets starting at the
* data-link layer (which also provides more granular control over the IP
* layer). If libnet uses the link-layer and the device argument is non-NULL,
* the function attempts to use the specified network device for packet
* injection. This is either a canonical string that references the device
* (such as "eth0" for a 100MB Ethernet card on Linux or "fxp0" for a 100MB
* Ethernet card on OpenBSD) or the dots and decimals representation of the
* device's IP address (192.168.0.1). If device is NULL, libnet attempts to
* find a suitable device to use. If the injection_type is LIBNET_RAW4 or
* LIBNET_RAW4_ADV, the function initializes the injection primitives for the
* IPv4 raw socket interface. The final argument, err_buf, should be a buffer
* of size LIBNET_ERRBUF_SIZE and holds an error message if the function fails.
* This function requires root privileges to execute successfully. Upon
* success, the function returns a valid libnet context for use in later
* function calls; upon failure, the function returns NULL.
* @param injection_type packet injection type (LIBNET_LINK, LIBNET_LINK_ADV, LIBNET_RAW4, LIBNET_RAW4_ADV, LIBNET_RAW6, LIBNET_RAW6_ADV)
* @param device the interface to use (NULL and libnet will choose one)
* @param err_buf will contain an error message on failure
* @return libnet context ready for use or NULL on error.
*/
libnet_t *
libnet_init(int injection_type, const char *device, char *err_buf);