-
-
Save Alcaro/a2ee7fa7613184f25442e2f7c3250322 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <arpa/inet.h> | |
#include <errno.h> | |
#include <fcntl.h> | |
#include <netdb.h> | |
#include <netinet/in.h> | |
#include <netinet/tcp.h> | |
#include <stdbool.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
int main() | |
{ | |
struct addrinfo hints; | |
memset(&hints, 0, sizeof(hints)); | |
hints.ai_family = AF_UNSPEC; | |
hints.ai_socktype = SOCK_DGRAM; | |
hints.ai_flags = AI_NUMERICHOST; | |
struct addrinfo * addr_1111 = NULL; | |
getaddrinfo("1.1.1.1", "53", &hints, &addr_1111); | |
struct addrinfo * addr_8888 = NULL; | |
getaddrinfo("8.8.8.8", "53", &hints, &addr_8888); | |
int fd_1111 = socket(addr_1111->ai_family, addr_1111->ai_socktype, addr_1111->ai_protocol); | |
int fd_8888 = socket(addr_8888->ai_family, addr_8888->ai_socktype, addr_8888->ai_protocol); | |
connect(fd_8888, addr_8888->ai_addr, addr_8888->ai_addrlen); | |
while (true) | |
{ | |
static const uint8_t dns_google[] = { | |
0x12,0x34, 1,0, 0,1, 0,0, 0,0, 0,0, | |
6,'g','o','o','g','l','e',3,'c','o','m',0, 0,1, 0,1 | |
}; | |
static const uint8_t dns_cloudflare[] = { | |
0x12,0x34, 1,0, 0,1, 0,0, 0,0, 0,0, | |
10,'c','l','o','u','d','f','l','a','r','e',3,'c','o','m',0, 0,1, 0,1 | |
}; | |
sendto(fd_1111, dns_cloudflare, sizeof(dns_cloudflare), MSG_NOSIGNAL, addr_1111->ai_addr, addr_1111->ai_addrlen); | |
sendto(fd_8888, dns_google, sizeof(dns_google), MSG_NOSIGNAL, addr_8888->ai_addr, addr_8888->ai_addrlen); | |
usleep(1000000); | |
for (int nsock=0;nsock<2;nsock++) | |
{ | |
while (true) | |
{ | |
char buf[1024]; | |
int ret = recvfrom(nsock ? fd_1111 : fd_8888, buf, sizeof(buf), MSG_DONTWAIT, NULL, NULL); | |
if (ret < 0) break; | |
for (int i=0;i<ret;i++) | |
printf("%.2x ", buf[i]&0xff); | |
puts(""); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment