RAW Socket Programming – Generate Google QUIC Protocol UDP Packets
RAW Socket Programming – Generate Google QUIC Protocol UDP Packets

Here is the QUIC Protocol raw sockets sample source-code discussed in the video:


/*---------- The Linux Channel ------------*/

#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <netpacket/packet.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <malloc.h>
#include <net/ethernet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

typedef unsigned char  BYTE;    /* 8-bit   */
typedef unsigned short BYTEx2;  /* 16-bit  */

typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;

#define IPHSIZE   20
#define ETHSIZE	14 

enum _Boolean_ { FALSE =0, TRUE  =1 };


static unsigned short in_cksum(u16 *ptr, int nbytes)
{
  register long sum=0;
  u16 oddbyte;
  register u16 answer;

  while(nbytes>1) { sum += *ptr++; nbytes -= 2; }
  if(nbytes == 1) { oddbyte = 0; *((unsigned char *) &oddbyte) = *(unsigned char *)ptr; sum += oddbyte; }
  sum  = (sum >> 16)+(sum & 0xffff); sum+=(sum >> 16); answer = ~sum;
  return(answer);
}

unsigned short magic_tcpudp_cksum(u32 src, u32 dst, u8 proto, u16 len, u8 *hstart)
{
	struct pseudo { u32 src; u32 dst; u8 zero; u8 proto; u16 length;
	} *hdr = (struct pseudo *) (hstart - sizeof(struct pseudo));
	
	hdr->src = src;
	hdr->dst = dst;
	hdr->zero = 0;
	hdr->proto = proto;
	hdr->length = htons(len);
	return in_cksum((u16 *) hdr, len + sizeof(struct pseudo));
}

int create_socket(char *device)
{	int sock_fd;
   struct ifreq ifr;
   struct sockaddr_ll sll;
	memset(&ifr, 0, sizeof(ifr));
   memset(&sll, 0, sizeof(sll));

   sock_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

   if(sock_fd == 0){ printf("ERR: socket creation for device: %s\n", device); return FALSE; }
   strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
   if(ioctl(sock_fd, SIOCGIFINDEX, &ifr) == -1) { printf(" ERR: ioctl failed for device: %s\n", device); return FALSE; }
	
	sll.sll_family      = AF_PACKET;
	sll.sll_ifindex     = ifr.ifr_ifindex;
	sll.sll_protocol    = htons(ETH_P_ALL);
	if(bind(sock_fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) { printf("ERR: bind failed for device: %s\n", device); return FALSE; }
  return sock_fd;
}


void main(int argc, char ** argv)
{
BYTE quic[631]={0x01,0xbb,0xe1,0x50,0x02,0x77,0x00,0x00,
0x00,0x3e,0x86,0x1c,0x84,0x90,0x8f,0xc9,0x3f,0xd1,0xdc,0x58,0x2d,0xde,0xd4,0x3c,
0x00,0xc6,0x50,0xa9,0x80,0xfd,0xac,0xa8,0x84,0x20,0x77,0x1b,0x10,0x2e,0x3d,0x1b,
0xee,0x1a,0x7c,0xc8,0x8d,0xe9,0x1e,0x5f,0x95,0x1a,0xb6,0xcd,0x8c,0x2d,0x1f,0xc4,
0x7b,0xeb,0xa6,0x1b,0x8a,0x35,0x4b,0xc0,0x68,0x49,0x81,0x1f,0xc3,0xd4,0x54,0x08,
0xee,0x6b,0x41,0xe1,0x0f,0x77,0x27,0x35,0xc4,0xd9,0xd1,0x78,0x4f,0xbc,0xba,0xd6,
0x0e,0x81,0x8a,0xfb,0x54,0x61,0x91,0x5a,0x40,0xb5,0xc5,0xef,0xbd,0xae,0x7d,0xe1,
0xb6,0x70,0x43,0xd8,0x2e,0x91,0x3e,0x32,0x30,0x94,0xdf,0x94,0x54,0xac,0x90,0x0b,
0x43,0x9d,0x5b,0x5f,0x9b,0x81,0xaa,0x9f,0x63,0x74,0x66,0x2d,0x20,0x32,0xf6,0xe7,
0xc3,0x16,0xbd,0x08,0x16,0x04,0xe1,0xc8,0x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,0xa7,0xf0,0xc0,0x9a,0xbe,0xdb,0x98,0x32,0x88,0x35,
0xd3,0xc5,0xe3,0x0e,0xb2,0x52,0x65,0x3b,0x68,0x6e,0x5a,0xaf,0x68,0xdf,0xb7,0x10,
0x1b,0xa4,0x0e,0x14,0xe7,0xf0,0x2f,0x63,0xaa,0x99,0x2e,0x95,0x2f,0x91,0x70,0x94,
0xa8,0x30,0x9b,0x82,0xa2,0x9b,0xa5,0x23,0x9b,0x93,0xec,0xe5,0xb8,0x68,0x6a,0x99,
0x99,0x99,0x22,0x40,0xe9,0xe9,0xf0,0x71,0x49,0x61,0x36,0x21,0x7a,0xb4,0xe5,0x54,
0xd2,0x2f,0xe1,0x0f,0x7c,0x12,0xb8,0x67,0xdb,0x90,0x20,0x50,0x52,0xac,0x22,0x04,
0x02,0x3d,0xa5,0x95,0xf0,0x97,0x27,0x20,0x43,0x2d,0xc9,0x01,0x08,0x93,0x61,0x8c,
0x23,0xa8,0x38,0xc4,0x8f,0x6a,0xab,0x6f,0x53,0xcd,0x5a,0x03,0x32,0x35,0x8c,0x50,
0xa9,0xc6,0xb9,0x8a,0xa3,0x3e,0x44,0x81,0xbd,0x92,0x77,0x63,0x7e,0xfa,0x96,0x67,
0x1c,0x99,0xc7,0xb3,0x72,0x0d,0x5e,0x5f,0xb0,0xbe,0x34,0xab,0x76,0xe8,0xb3,0x46,
0x4d,0x4a,0x5d,0x44,0xab,0xf6,0xd1,0x9e,0x6e,0xb7,0x16,0x60,0x23,0xed,0x83,0x03,
0x21,0xeb,0x5f,0xe6,0x34,0x48,0x3f,0x5c,0x98,0xf2,0x2c,0xf3,0xbc,0x93,0xba,0x2a,
0x7d,0x32,0xeb,0xec,0x72,0x85,0xf5,0x94,0x68,0x00,0x23,0x13,0x07,0x36,0x09,0xeb,
0x6c,0x19,0x10,0xd9,0xc9,0xa9,0x2b,0xe1,0xec,0x3d,0x88,0x5b,0x75,0x05,0xa6,0x4b,
0xfb,0x0b,0xb5,0xd3,0x7a,0x8e,0x43,0x96,0xaa,0x2f,0xff,0xf6,0x55,0x4a,0x9e,0x28,
0x69,0x8e,0xc5,0xa3,0xb3,0xb6,0x02,0x37,0xc5,0xcf,0x93,0x99,0x22,0xf7,0xe2,0x0c,
0x46,0xf9,0x36,0xe6,0x28,0xf5,0x9e,0xe1,0x03,0x05,0x71,0xb3,0xee,0x0c,0x6b};
BYTE l2[14] = {0x00,0x30,0x54,0x00,0x34,0x56,0x00,0xe0,0xed,0x01,0x6e,0xbd,0x08,0x00};
BYTEx2 new_udp_chksum = 0x0000;
struct iphdr ip;
	memset(&ip, 0x0, IPHSIZE);
	ip.version    = 4;
   ip.ihl        = IPHSIZE >> 2;
   ip.tos        = 0;
   ip.id         = htons(rand()%65535);
   ip.frag_off   = 0;
   ip.ttl        = htons(254);
   ip.protocol   = IPPROTO_UDP;
   ip.saddr      = inet_addr("1.1.1.1");
   ip.daddr      = inet_addr("2.2.2.2"); 
   ip.tot_len    = htons(631+IPHSIZE);

   BYTE buf[1600];
   BYTE *buf_chksum = (BYTE *)(buf+120); //use after few bytes !
   
	memcpy(buf_chksum, quic,631);
	new_udp_chksum = magic_tcpudp_cksum(ip.saddr, ip.daddr, ip.protocol, (u16)631, (u8 *)buf_chksum);
   memcpy((BYTE *)(quic+6), &new_udp_chksum, 2);
   ip.check=(unsigned short)in_cksum((unsigned short *)&ip, IPHSIZE);
   
   memcpy(buf, l2, ETHSIZE);
   memcpy(buf+ETHSIZE, &ip, IPHSIZE);
   memcpy(buf+ETHSIZE+IPHSIZE, quic, 631);
   
   int sock_fd = create_socket(argv[1]);
	if(!(sock_fd) ) { printf("no sock_fd found\n"); }
 
	write(sock_fd, buf, ETHSIZE+IPHSIZE+631);

}