Linux original socket (4)-construct IP_UDP, linuxip_udp

Source: Internet
Author: User

Linux original socket (4)-construct IP_UDP, linuxip_udp

I. Overview

Like tcp in the previous article, udp is encapsulated in ip packets. Create the original UDP socket as follows:

1 (sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);

Similarly, if you want to construct the udp ip header, you must enable the IP_HDRINCL option!

Udp Header Format:

The reliability of udp is much simpler than that of tcp packets. The above 16-bit UDP length is the total length of UDP header + normal data, which is the same as the total length of 16-bit ip header!

The udp structure is defined in netinet/udp. h.

 1 struct udphdr 2 { 3   __extension__ union 4   { 5     struct 6     { 7       u_int16_t uh_sport;        /* source port */ 8       u_int16_t uh_dport;        /* destination port */ 9       u_int16_t uh_ulen;        /* udp length */10       u_int16_t uh_sum;        /* udp checksum */11     };12     struct13     {14       u_int16_t source;15       u_int16_t dest;16       u_int16_t len;17       u_int16_t check;18     };19   };20 };

2. Construct IP_UDP packet transmission

1/** 2 * @ file ip_udp_send.c 3 */4 5 # include <stdio. h> 6 # include <stdlib. h> 7 # include <string. h> 8 # include <unistd. h> 9 # include <sys/socket. h> 10 # include <arpa/inet. h> 11 # include <netinet/ip. h> 12 # include <netinet/udp. h> 13 14/* ip header length */15 # define IP_HEADER_LEN sizeof (struct ip) 16/* udp header length */17 # define UDP_HEADER_LEN sizeof (struct udphdr) 18/* ip header + udp header length */19 # define IP_UDP_H EADER_LEN IP_HEADER_LEN + UDP_HEADER_LEN 20 21 void err_exit (const char * err_msg) 22 {23 perror (err_msg); 24 exit (1 ); 25} 26 27/* fill in the ip header */28 struct ip * fill_ip_header (const char * src_ip, const char * dst_ip, int ip_packet_len) 29 {30 struct ip * ip_header; 31 32 ip_header = (struct ip *) malloc (IP_HEADER_LEN); 33 ip_header-> ip_v = IPVERSION; 34 ip_header-> ip_hl = IP_HEADER_LEN/4; 35 ip_header -> Ip_tos = 0; 36 ip_header-> ip_len = htons (ip_packet_len); 37 ip_header-> ip_id = 0; 38 ip_header-> ip_off = 0; 39 ip_header-> ip_ttl = MAXTTL; 40 ip_header-> ip_p = IPPROTO_UDP;/* UDP */41 ip_header-> ip_sum = 0; 42 ip_header-> ip_src.s_addr = inet_addr (src_ip); 43 ip_header-> ip_dst.s_addr = inet_addr (dst_ip); 44 45 return ip_header; 46} 47 48/* fill udp header */49 struct udphdr * fill_udp_header (int s Rc_port, int dst_port, int udp_packet_len) 50 {51 struct udphdr * udp_header; 52 53 udp_header = (struct udphdr *) malloc (UDP_HEADER_LEN ); 54 udp_header-> source = htons (src_port); 55 udp_header-> source = htons (dst_port ); 56/* The length here is the entire UDP packet */57 udp_header-> len = htons (udp_packet_len); 58 udp_header-> check = 0; 59 60 return udp_header; 61} 62 63/* Send ip_udp packet */64 void ip_udp_send (const char * sr C_ip, int src_port, const char * dst_ip, int dst_port, const char * data) 65 {66 struct ip * ip_header; 67 struct udphdr * udp_header; 68 struct sockaddr_in dst_addr; 69 socklen_t sock_addrlen = sizeof (struct sockaddr_in); 70 71 int data_len = strlen (data); 72 int rows = bytes + data_len; 73 int udp_packet_len = UDP_HEADER_LEN + data_len; 74 char buf [ip_packet_len]; 75 int sockfd, Ret_len, on = 1; 76 77 bzero (& dst_addr, sock_addrlen); 78 rows = PF_INET; 79 rows = inet_addr (dst_ip); 80 rows = htons (dst_port ); 81 82/* Create udp original socket */83 if (sockfd = socket (PF_INET, SOCK_RAW, IPPROTO_UDP) =-1) 84 err_exit ("socket ()"); 85 86/* enable IP_HDRINCL, custom IP header */87 if (setsockopt (sockfd, IPPROTO_IP, IP_HDRINCL, & on, sizeof (on) =-1) 8 8 err_exit ("setsockopt ()"); 89 90/* ip header */91 ip_header = fill_ip_header (src_ip, dst_ip, ip_packet_len ); 92/* udp header */93 udp_header = fill_udp_header (src_port, dst_port, udp_packet_len); 94 95 bzero (buf, callback); 96 memcpy (buf, ip_header, IP_HEADER_LEN ); 97 memcpy (buf + IP_HEADER_LEN, udp_header, UDP_HEADER_LEN); 98 memcpy (buf + IP_UDP_HEADER_LEN, data, data_len); 99 100/* send packets */101 r Et_len = sendto (sockfd, buf, ip_packet_len, 0, (struct sockaddr *) & dst_addr, sock_addrlen); 102 if (ret_len> 0) 103 printf ("sendto () OK !!! \ N "); 104 else printf (" sendto () failed \ n "); 105 106 close (sockfd); 107 free (ip_header); 108 free (udp_header ); 109} 110 111 int main (int argc, const char * argv []) 112 {113 if (argc! = 6) 114 {115 printf ("usage: % s src_ip src_port dst_ip dst_port data \ n", argv [0]); 116 exit (1 ); 117} 118 119/* Send ip_udp packets */120 ip_udp_send (argv [1], atoi (argv [2]), argv [3], atoi (argv [4]), argv [5]); 121 122 return 0; 123}

Most of the above Code is similar to the previous one. The difference is that this time the udp header is filled, and the type of the original socket to be created is UDP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.