I. Overview
The same as TCP, UDP is also encapsulated in the IP packet. The original sockets for creating UDP are as follows:
1 ipproto_udp);
Similarly, if you want to construct the IP header for UDP, turn on the ip_hdrincl option!
UDP header Format:
The unreliability of UDP is much simpler than TCP packets. The above 16-bit UDP length is the total length of the UDP header + normal data , which is the same as the 16-bit total length of the IP header!
The UDP structure is defined in the netinet/udp.h
1 structUDPHDR2 {3 __extension__ Union4 {5 struct6 {7u_int16_t Uh_sport;/*Source Port*/8u_int16_t Uh_dport;/*Destination Port*/9u_int16_t Uh_ulen;/*UDP length*/Tenu_int16_t uh_sum;/*UDP checksum*/ One }; A struct - { - u_int16_t Source; the u_int16_t dest; - u_int16_t Len; - u_int16_t check; - }; + }; -};
Two. construct IP_UDP message Sending
1 /**2 * @file IP_UDP_SEND.C3 */4 5#include <stdio.h>6#include <stdlib.h>7#include <string.h>8#include <unistd.h>9#include <sys/socket.h>Ten#include <arpa/inet.h> One#include <netinet/ip.h> A#include <netinet/udp.h> - - /*IP First ministerial degree*/ the #defineIp_header_len sizeof (struct IP) - /*UDP first Ministerial degree*/ - #defineUdp_header_len sizeof (struct UDPHDR) - /*IP header + UDP first ministerial degree*/ + #defineIp_udp_header_len Ip_header_len + Udp_header_len - + voidErr_exit (Const Char*err_msg) A { at perror (err_msg); -Exit1); - } - - /*Populate IP Header*/ - structIP *fill_ip_header (Const Char*SRC_IP,Const Char*DST_IP,intIp_packet_len) in { - structIP *Ip_header; to +Ip_header = (structIP *)malloc(Ip_header_len); -Ip_header->ip_v =ipversion; theIP_HEADER->IP_HL = Ip_header_len/4; *Ip_header->ip_tos =0; $Ip_header->ip_len =htons (Ip_packet_len);Panax Notoginsengip_header->ip_id =0; -Ip_header->ip_off =0; theIp_header->ip_ttl =Maxttl; +Ip_header->ip_p = IPPROTO_UDP;/*this is UDP .*/ AIp_header->ip_sum =0; theIP_HEADER->IP_SRC.S_ADDR =inet_addr (SRC_IP); +IP_HEADER->IP_DST.S_ADDR =inet_addr (DST_IP); - $ returnIp_header; $ } - - /*populating the UDP header*/ the structUdphdr *fill_udp_header (intSrc_port,intDst_port,intUdp_packet_len) - {Wuyi structUDPHDR *Udp_header; the -Udp_header = (structUDPHDR *)malloc(Udp_header_len); WuUdp_header->source =htons (src_port); -Udp_header->source =htons (dst_port); About /*the length here is the entire UDP message*/ $Udp_header->len =htons (Udp_packet_len); -Udp_header->check =0; - - returnUdp_header; A } + the /*Send IP_UDP messages*/ - voidIp_udp_send (Const Char*SRC_IP,intSrc_port,Const Char*DST_IP,intDst_port,Const Char*data) $ { the structIP *Ip_header; the structUDPHDR *Udp_header; the structsockaddr_in dst_addr; thesocklen_t Sock_addrlen =sizeof(structsockaddr_in); - in intData_len =strlen (data); the intIp_packet_len = Ip_udp_header_len +Data_len; the intUdp_packet_len = Udp_header_len +Data_len; About CharBuf[ip_packet_len]; the intSOCKFD, ret_len, on =1; the theBzero (&dst_addr, Sock_addrlen); +dst_addr.sin_family =pf_inet; -DST_ADDR.SIN_ADDR.S_ADDR =inet_addr (DST_IP); theDst_addr.sin_port =htons (dst_port);Bayi the /*To create a UDP raw socket*/ the if(SOCKFD = socket (pf_inet, sock_raw, ipproto_udp) = =-1) -Err_exit ("socket ()"); - the /*turn on ip_hdrincl, customize IP header*/ the if(SetSockOpt (SOCKFD, Ipproto_ip, Ip_hdrincl, &on,sizeof(ON)) == -1) theErr_exit ("setsockopt ()"); the - /*IP Header*/ theIp_header =Fill_ip_header (src_ip, Dst_ip, Ip_packet_len); the /*UDP Header*/ theUdp_header =Fill_udp_header (Src_port, Dst_port, Udp_packet_len);94 the bzero (buf, Ip_packet_len); the memcpy (buf, Ip_header, Ip_header_len); thememcpy (buf +Ip_header_len, Udp_header, Udp_header_len);98memcpy (buf +Ip_udp_header_len, data, Data_len); About - /*Send a delivery paper*/101Ret_len = SendTo (SOCKFD, buf, Ip_packet_len,0, (structSOCKADDR *) &dst_addr, Sock_addrlen);102 if(Ret_len >0)103printf"sendto () OK!!! \ n");104 Elseprintf"sendto () failed\n"); the 106 Close (SOCKFD);107 Free(Ip_header);108 Free(Udp_header);109 } the 111 intMainintargcConst Char*argv[]) the {113 if(ARGC! =6) the { theprintf"usage:%s src_ip src_port dst_ip dst_port data\n", argv[0]); theExit1);117 }118 119 /*Send IP_UDP messages*/ -Ip_udp_send (argv[1], Atoi (argv[2]), argv[3], Atoi (argv[4]), argv[5]);121 122 return 0;123}
Most of the code above is similar to the previous one. The difference is that this time the UDP header is populated, and the type that created the original socket is UDP.
Linux raw Sockets (4)-Construction IP_UDP