SHARE
TWEET

netcode

a guest Mar 29th, 2018 17 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdint.h>
  2.  
  3. #define IP_HEADER_MIN_SIZE 20
  4. #define IP_HEADER_MAX_SIZE 60
  5. struct ip_header
  6. {
  7.     unsigned int version:4;
  8.     unsigned int ihl:4;
  9.     uint8_t tos;
  10.     uint16_t tot_len;
  11.     uint16_t id;
  12.     uint16_t frag_off;
  13.     uint8_t ttl;
  14.     uint8_t protocol;
  15.     uint16_t check;
  16.     uint32_t saddr;
  17.     uint32_t daddr;
  18.     // IP options starts here
  19. };
  20.  
  21. struct udp_header
  22. {
  23.     uint16_t uh_sport;              
  24.     uint16_t uh_dport;              
  25.     uint16_t uh_ulen;              
  26.     uint16_t uh_sum;      
  27.     // UDP payload starts here
  28. };
  29.  
  30. /* Build a UDP/IP packet and store it into buffer passed as parameter */
  31. void new_packet(char *buf, char *options, int optsize, int udp_udp_size) {
  32.   int i;
  33.   int ip_header_size = optsize + sizeof(struct ip_header); /* compute total IP header size, from option size */
  34.   struct ip_header *ip = (void*) buf;
  35.  
  36.   for (i = 0 ; i < optsize; i++)
  37.     buf[i + sizeof(struct ip_header)] = options[i];
  38.  
  39.   ip->ihl = ip_header_size >> 2; /* put correct header size in ip header */
  40.   struct udp_header *udp = (void*)buf + ip_header_size; /* udp headers starts immediately after ip header end */
  41.   udp->uh_ulen = udp_udp_size; /* put udp payload size in udp header */
  42.  
  43.   // Initialization of irrelevant fields (source/destination IP, ports, etc) have been omitted.
  44. }
  45.  
  46. void raw_socket_write(char *data, int size) {
  47.   // Write to raw socket (omitted)
  48. }
  49.  
  50. void send_packet(char *buf) {
  51.   struct ip_header *h1 = (void*) buf;
  52.   int header_size = h1->ihl << 2; /* get ip header size */
  53.   struct udp_header *h2 = (void*) buf + header_size; /* udp header starts immediately after ip header end */
  54.   int udp_size = h2->uh_ulen; /* get udp payload size from udp header */
  55.   int i;
  56.  
  57.   uint32_t checksum = 0;
  58.  
  59.   // compute UDP checksum
  60.   for (i = 0; i < udp_size; i++) {
  61.     // Actual checksum computation omitted
  62.   }
  63.   h2->uh_sum = checksum;
  64.  
  65.   raw_socket_write(buf, header_size + udp_size);
  66. }
  67.  
  68. #define PAYLOAD_MIN_SIZE 16
  69. #define PAYLOAD_MAX_SIZE 1024
  70. int send_request_over_udp(char *options, int optsize, int udpsize) {
  71.   char buf[1024];
  72.   int i;
  73.   if ((optsize + sizeof(struct ip_header) >= IP_HEADER_MIN_SIZE) &&
  74.       (optsize + sizeof(struct ip_header) <= IP_HEADER_MAX_SIZE) &&
  75.       (udpsize + sizeof(struct udp_header) >= PAYLOAD_MIN_SIZE) &&
  76.       (udpsize + sizeof(struct udp_header) <= PAYLOAD_MAX_SIZE))
  77.   {
  78.     /* sizes are in acceptable range */
  79.     new_packet(buf, options, optsize, udpsize);
  80.    
  81.     // Create packet payload here (omitted)
  82.    
  83.     // ...
  84.    
  85.     send_packet(buf);
  86.   } else {
  87.     return -1; // Error
  88.   }
  89. }
  90.  
  91. int main(void) {
  92.   char options[1024];
  93.   int optsize, udpsize;
  94.   send_request_over_udp(options, optsize, udpsize);
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top