Commonly used functions

libnet_init()

role

Packet memory initialization and environment establishment

The function prototype

libnet_t *libnet_init(int injection_type, char *device, char *err_buf);
Copy the code

The return value

Success: a pointer of type libnet * that must be used for all subsequent operations: NULL

Parameters,

  • Injection_type: type of construction LIBNET_LINK, link layer LIBNET_RAW4, network interface layer (network layer) LIBNET_LINK_ADV, link layer advanced version LIBNET_RAW4_ADV, network layer advanced version
  • Device: Network interface, such as “eth0”, or IP address, or NULL (automatic query search)
  • Err_buf: Stores error information

C language code example

/ / no
Copy the code

libnet_destroy()

role

Release resources

The function prototype

void libnet_destroy(libnet_t *l);
Copy the code

The return value

There is no

Parameters,

  • L: The libnet * pointer returned by libnet_init()

C language code example

/ / no
Copy the code

libnet_addr2name4()

role

Converts network byte order to dotted decimal string

The function prototype

char* libnet_addr2name4(u_int32_t in, u_int8_t use_name);
Copy the code

The return value

Successful: indicates that the dotted decimal IP address fails: indicates NULL

Parameters,

  • In: indicates the IP address of the network byte order
  • Use_name: LIBNET_RESOLVE, corresponding to the host name. LIBNET_DONT_RESOLVE is an IPv4 address in dotted decimal notation. The second argument to this function is typically specified as LIBNET_RESOLVE.

C language code example

/ / no
Copy the code

libnet_name2addr4()

role

Converts a dotted decimal string to a network byte order IP address

The function prototype

u_int32_t libnet_name2addr4(libnet_t *l, char *host_name, u_int8_t use_name);
Copy the code

The return value

Success: network byte order IP address failed: -1

Parameters,

  • L: The libnet * pointer returned by libnet_init()
  • Host_name: indicates the IP address string in dotted decimal notation
  • Use_name: LIBNET_RESOLVE, corresponding to the host name. LIBNET_DONT_RESOLVE is an IPv4 address in dotted decimal notation. The second argument to this function is typically specified as LIBNET_RESOLVE.

C language code example

/ / no
Copy the code

libnet_get_ipaddr4()

role

Obtain the IP address of the interface device

The function prototype

u_int32_t libnet_get_ipaddr4(libnet_t *l);
Copy the code

The return value

Success: the IP address of the network byte order failed: -1

Parameters,

  • L: The libnet * pointer returned by libnet_init()

C language code example

/ / no
Copy the code

libnet_get_hwaddr()

role

Obtain the hardware address of the interface device

The function prototype

struct libnet_ether_addr* libnet_get_hwaddr(libnet_t *l);
Copy the code

The return value

Success: pointer to MAC address failed: NULL

Parameters,

  • L: The libnet * pointer returned by libnet_init()

C language code example

/ / no
Copy the code

libnet_build_udp()

role

Constructing udp packets

The function prototype

libnet_ptag_t libnet_build_udp(u_int16_t sp, u_int16_t dp, u_int16_t len, u_int16_t sum, u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag);
Copy the code

The return value

Success: Protocol marking failure: -1

Parameters,

  • Sp: indicates the source port number
  • Dp: indicates the destination port number
  • Len: indicates the total length of the UDP packet
  • Sum: the checksum, set to 0, is automatically filled by libnet
  • Payload: Payload. It is the text content sent to applications. This parameter can be set to NULL if there is no content
  • Payload_s: Load length, the length of text content sent to the application, or 0
  • L: The libnet * pointer returned by libnet_init()
  • Ptag: protocol flag. The first time a new packet is assembled, 0 is written here. The next time the same application is assembled, the value of this position is written to the return value of this function.

C language code example

/ / no
Copy the code

libnet_build_tcp()

role

Constructing TCP packets

The function prototype

libnet_ptag_t libnet_build_tcp(u_int16_t sp, u_int16_t dp, u_int32_t seq, u_int32_t ack, u_int8_t control, u_int16_t win, u_int16_t sum, u_int16_t urg, u_int16_t len, u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag);
Copy the code

The return value

Success: Protocol marking failure: -1

Parameters,

  • Sp: indicates the source port number
  • Dp: indicates the destination port number
  • Seq: serial number
  • Ack: Indicates the ACK flag
  • Control: Indicates the control flag
  • Win: Window size
  • Sum: the checksum, set to 0, is automatically filled by libnet
  • Urg: emergency pointer
  • Len: indicates the length of the TCP packet
  • Payload: Payload. It is the text sent to an application. The value can be NULL
  • Payload_s: load length, or 0
  • L: The libnet * pointer returned by libnet_init()
  • Ptag: protocol flag. The first time a new packet is assembled, 0 is written here. The next time the same application is assembled, the value of this position is written to the return value of this function.

C language code example

/ / no
Copy the code

libnet_build_tcp_options()

role

Construct TCP option packets

The function prototype

libnet_ptag_t libnet_build_tcp_options(u_int8_t *options, u_int32_t options_s, libnet_t *l, libnet_ptag_t ptag);
Copy the code

The return value

Success: Protocol marking failure: -1

Parameters,

  • Options: indicates TCP options
  • Options_s: indicates the length of the option
  • L: libnet handle, the libnet * pointer returned by libnet_init()
  • Ptag: protocol flag. The first time a new packet is assembled, 0 is written here. The next time the same application is assembled, the value of this position is written to the return value of this function.

C language code example

/ / no
Copy the code

libnet_build_ipv4()

role

Construct an IPv4 packet

The function prototype

libnet_ptag_t libnet_build_ipv4(u_int16_t ip_len, u_int8_t tos, u_int16_t id, u_int16_t flag, u_int8_t ttl, u_int8_t prot, u_int16 sum, u_int32_t src, u_int32_t dst, u_int8_t *payload, u_int32_t payload_s,libnet_t *l, libnet_ptag_t ptag);
Copy the code

The return value

Success: Protocol marking failure: -1

Parameters,

  • Ip_len: indicates the total length of IP packets
  • Tos: indicates the service type
  • Id: indicates the IP id
  • Flag: indicates chip offset
  • TTL: indicates the TTL
  • Prot: indicates the upper-layer protocol
  • Sum: the checksum, set to 0, is automatically filled by libnet
  • SRC: indicates the source IP address
  • DST: indicates the destination IP address
  • Payload: payload, which can be set to NULL
  • Payload_s: Load length, or 0 (where 0 is usually written)
  • L: libnet handle, the libnet * pointer returned by libnet_init()
  • Ptag: protocol flag. The first time a new packet is assembled, 0 is written here. The next time the same application is assembled, the value of this position is written to the return value of this function.

C language code example

/ / no
Copy the code

libnet_build_ipv4_options()

role

Construct IPv4 option packets

The function prototype

libnet_ptag_t libnet_build_ipv4_options(u_int8_t*options, u_int32_t options, libnet_t*l, libnet_ptag_t ptag);
Copy the code

The return value

Success: Protocol marking failure: -1

Parameters,

  • Options: indicates TCP options
  • Options_s: indicates the length of the option
  • L: libnet handle, the libnet * pointer returned by libnet_init()
  • Ptag: indicates the protocol tag. If the value is 0, a new protocol is created

C language code example

/ / no
Copy the code

libnet_build_arp()

role

Constructing ARP Packets

The function prototype

libnet_ptag_t libnet_build_arp(u_int16_t hrd, u_int16_t pro, u_int8_t hln, u_int8_t pln, u_int16_t op, u_int8_t *sha, u_int8_t *spa, u_int8_t *tha, u_int8_t *tpa, u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag);
Copy the code

The return value

Success: Protocol marking failure: -1

Parameters,

  • HRD: Hardware address format, ARPHRD_ETHER (Ethernet)
  • Pro: Protocol address format, ETHERTYPE_IP (IP protocol)
  • HLN: Hardware address length
  • PLN: indicates the length of a protocol address
  • Op: ARP operation type (1: ARP request, 2: ARP reply, 3: RARP request, 4: RARP reply)
  • Sha: indicates the hardware address of the sender
  • Spa: indicates the sender protocol address
  • Tha: Indicates the destination hardware address
  • Tpa: indicates the destination protocol address
  • Payload: payload, which can be set to NULL
  • Payload_s: Load length, or 0 (where 0 is usually written)
  • L: libnet handle, the libnet * pointer returned by libnet_init()
  • Ptag: protocol flag. The first time a new packet is assembled, 0 is written here. The next time the same application is assembled, the value of this position is written to the return value of this function.

C language code example

/ / no
Copy the code

libnet_build_ethernet()

role

Construct an Ethernet packet

The function prototype

libnet_ptag_t libnet_build_ethernet(u_int8_t*dst, u_int8_t *src, u_int16_ttype, u_int8_t*payload, u_int32_tpayload_s, libnet_t*l, libnet_ptag_t ptag);
Copy the code

The return value

Success: Protocol marking failure: -1

Parameters,

  • DST: indicates the destination MAC address
  • SRC: source MAC
  • Type: indicates the type of the upper-layer protocol
  • Payload: payload, which can be set to NULL
  • Payload_s: Load length, or 0 (where 0 is usually written)
  • L: libnet handle, the libnet * pointer returned by libnet_init()
  • Ptag: protocol flag. The first time a new packet is assembled, 0 is written here. The next time the same application is assembled, the value of this position is written to the return value of this function.

C language code example

/ / no
Copy the code

libnet_write();

role

Send packet

The function prototype

int libnet_write(libnet_t * l);
Copy the code

The return value

Success: Length of packet sent failed: -1 is returned

Parameters,

  • L: libnet handle, the libnet * pointer returned by libnet_init()

C language code example

/ / no
Copy the code

Comprehensive example

C code

To send data

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libnet.h>

int main(int argc, char *argv[]){
    int res = 0;
    char send_msg[1000] = "";
    char err_buf[100] = "";
    libnet_t *lib_net = NULL;
    int lens = 0;
    libnet_ptag_t lib_t = 0;
    unsigned char src_mac[6] = {0x00.0x0c.0x29.0x86.0x24.0xf8};     // The sender's nic address 00:0c:29:86:24:f8
    unsigned char dst_mac[6] = {0x00.0x0c.0x29.0x86.0x24.0xf8};     // Receiver nic address 00:0C: 29:86:24:F8
    char *src_ip_str = "192.168.8.23";      // Source host IP address
    char *dst_ip_str = "192.168.8.23";      // Destination host IP address
    unsigned long src_ip, dst_ip = 0;

    lens = sprintf(send_msg, "%s"."this is for the udp test!");

    / / initialization
    lib_net = libnet_init(LIBNET_LINK_ADV, "ens33", err_buf); 
    if(lib_net == NULL){
        perror("libnet_init error!");
        return - 1;
    }

    // Converts a string IP to a sequential network byte stream
    src_ip = libnet_name2addr4(lib_net,src_ip_str,LIBNET_RESOLVE); 
    dst_ip = libnet_name2addr4(lib_net,dst_ip_str,LIBNET_RESOLVE);

    // Construct udp packets
    lib_t = libnet_build_udp(8080.8080.8+lens, 0, send_msg, lens, lib_net, 0);

    // Construct IP packets
    lib_t = libnet_build_ipv4(20+8+lens, 0.500.0.10.17.0, src_ip, dst_ip, NULL.0, lib_net, 0);

    // Construct Ethernet packets
    //0x800 或者,ETHERTYPE_IP
    lib_t = libnet_build_ethernet((u_int8_t *)dst_mac, (u_int8_t *)src_mac, 0x800.NULL.0, lib_net, 0);
    
    // Send the packet
    res = libnet_write(lib_net); 
    if(res == - 1){
        perror("libnet_write");
        return - 1;
    }

    // Destroy resources
    libnet_destroy(lib_net); 

    printf("----ok-----\n");
    return 0;
}
Copy the code

Receive data

#include <pcap.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <net/ethernet.h>

/* Ethernet header is always exactly 14 bytes */
#define SIZE_ETHERNET 14

/* THE UDP header is always exactly 8 bytes */
#define SIZE_UDP 8
 
void getPacket(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet){
    struct ethernet *ethernet;      // Ethernet header
    struct ip *ip;                  // IP protocol header
    struct tcphdr *tcp;             // TCP header
    struct udphdr *udp;             // UDP protocol header
    char *payload;                  / / data

    u_int size_ip;
    //u_int size_tcp;

    //ethernet = (struct ethernet*)(packet);
    ip = (struct ip*)(packet + SIZE_ETHERNET);
    size_ip = (ip->ip_hl)*4;
    if (size_ip < 20) {
        printf("* Invalid IP header length: %u bytes\n", size_ip);
        return;
    }
    //udp = (struct udphdr*)(packet + SIZE_ETHERNET + size_ip);

    payload = (u_char *)(packet + SIZE_ETHERNET + size_ip + SIZE_UDP);

    printf("message:%s\n", payload);
}
 
int main(a){
    char errBuf[PCAP_ERRBUF_SIZE];
    char *dev;
    pcap_t * handle;
    struct bpf_program filter;
    int id = 0;

    dev = pcap_lookupdev(errBuf);

    if(dev){
        printf("success: device: %s\n", dev);
    }
    else{
        printf("error: %s\n", errBuf);
        return 1;
    }

    handle = pcap_open_live(dev, 65535.1.0, errBuf);

    if(! handle){printf("error: pcap_open_live(): %s\n", errBuf);
        exit(1);
    }

    pcap_compile(handle, &filter, "src host 192.168.8.23 and port 8080".1.0);

    pcap_setfilter(handle, &filter);

    pcap_loop(handle, - 1, getPacket, (u_char*)&id);

    pcap_close(handle);

    return 0;
}

Copy the code

The results

Operation screenshot of sending data

Take a running screenshot of the data