libnet use the example (2) 2000-01-01 Baiyun Huang Wuhan station Safety of resources: a small four
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.