Example of LIBNET (2)

zhaozj2021-02-16  82

libnet use the example (2) 2000-01-01 Baiyun Huang Wuhan station Safety of resources: a small four Home Page: http: //www.isbase.com Date: 2000-07-2620 : 10 This article first introduces the libnet_init_packet () function, its function prototype as follows: int Libnet_init_packet (size_t p_size, u_char ** buf); This function actually invoked a Malloc function for a memory allocation, the first ginseng is specified memory allocation the size of. The second point is used to return a pointer to this memory. If P_SIZE is omitted or negative, use libnet_max_packet by default. The memory allocation is successfully initialized into 0, and the function returns 1, otherwise returns -1. Obviously there is a corresponding function release memory: int libnet_destroy_packet (u_char ** buf); this function does not have other actions in addition to call free (). There is a definition in /usr/include/libnet/libnet-macros.h: #define libnet_max_packet 0xffff In fact we can do not use these two functions, they do not operate what internal data structure, just see the use of them for unified packaging . For p_size, libnet is defined in /usr/include/libnet/libnet-headers.h: #define libnet_arp_h 0x1c / * arp header: 28 Bytes * / # define libnet_dns_h 0xc / * DNS header base: 12 bytes * / # define LIBNET_ETH_H 0xe / * Etherner header: 14 bytes * / # define LIBNET_ICMP_H 0x4 / * ICMP header base: 4 bytes * / # define LIBNET_IGMP_H 0x8 / * IGMP header: 8 bytes * / # define LIBNET_IP_H 0x14 / * IP header: 20 bytes * / # define libnet_rip_h 0x18 / * rip header base: 24 bytes * / # define libnet_tcp_h 0x14 / * TCP header: 20 bytes * / # define libnet_udp_h 0x8 / * udp header: 8 Bytes * / top only lists commonly used Several macros, although not necessarily, it is recommended to use macros as possible. Next, the operation described raw_socket two functions: int libnet_open_raw_sock (int protocol); int libnet_close_raw_sock (int fd); libnet_open_raw_sock () to open the IPv4 protocol type specified raw_socket, IP_HDRINCL option is also set, it returns successfully socket, failed to return - 1. LIBNET_CLOSE_RAW_SOCK () Close the Raw_Socket opened by the former, and then returns 1, and the failed returns -1. In fact, the correspondence of the PROTOCOL is the third form of the socket () function, so IPPROTO_RAW, IPPROTO_ICMP, IPPROTO_TCP, etc., of course, whether the kernel supports such a designation.

We can work like W.Richard.stevens, package this function: -------------------------------- --------------------------------------- INT libnet_open_raw_sock (int protocol) {Int S; IF ((s = libnet_open_raw_sock (protocol)) == -1) {libnet_error (libnet_err_fatal, "can" t open raw socket% 08x / n ", procotol);} return (s);} / * end of libnet_open_raw_sock * / -------------------------------------------------- ------------------------ When you do not use the libnet library, open Raw_Socket, then build IP headers, libnet uses the following functions to complete this working: int libnet_build_ip (u_short len, u_char tos, u_short id, u_short frag, u_char ttl, u_char prot, u_long saddr, u_long daddr, const u_char * payload, int payload_s, u_char * buf); parameter len specifies the IP data The length of the area is neither IP header, nor the total length of IP packets, does not have a structural member to correspond to this ginseng, the brothers who are used to SOCKET programming. SADDR and DaddR are provided in the network word sequence, that is Big-endian sequence .PayLoad points to optional IP options and possible population, payload_s specify the length of the data. As for the last parameter buf, point to the data area that is allocated by libnet_init_packet, including IP headers in the future, including IP heads The data area, the actual BUF pointing is the actual IP header. Of course, this example is an example of IP packets. If it is other type of packet, it is not this conclusion. Do not confuse the PayLoad and IP data area, otherwise LIBNET Chaos appear when constructing the IP head .Libnet uses the following function to construct TCP header on IP packet: int Libnet_ build_tcp (u_short sport, u_short dport, u_long seq, u_long ack, u_char control, u_short win, u_short urg, const u_char * payload, int payload_s, u_char * buf); parameter control corresponding to the familiar SYN, ACK, RST and other signs Logic or. The last shape needs to point to an allocated data area, and the TCP head starts from the pointer. Similar to the IP layer, here PayLoad points to optional TCP options and possible fills, not confusing with the TCP data area. Next, you may be the TCP / UDP checksum calculation problem you once painted: int Libnet_do_checksum (u_char * buf, int protocol, int LEN); this function is used to calculate the transfer layer checksum and fill in the appropriate head position. Includes TCP / UDP checksum calculations. Obviously the function should be called after the transport layer message includes the data area configuration, otherwise the calculated checks will be invalidated due to the change of the transport layer data area. Success, return 1, otherwise returns -1.

转载请注明原文地址:https://www.9cbs.com/read-16450.html

New Post(0)