Skip to content

Instantly share code, notes, and snippets.

@Alcaro
Created January 6, 2020 16:25
Show Gist options
  • Save Alcaro/c3b48a9dbc5cf8ddc732ce78c0d02b62 to your computer and use it in GitHub Desktop.
Save Alcaro/c3b48a9dbc5cf8ddc732ce78c0d02b62 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <stdbool.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("");
}
}
}
}
/*
#define PORT 8080
#define MAXLINE 1024
// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from client";
struct sockaddr_in servaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = INADDR_ANY;
int n, len;
sendto(sockfd, (const char *)hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &servaddr,
sizeof(servaddr));
printf("Hello message sent.\n");
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, (struct sockaddr *) &servaddr,
&len);
buffer[n] = '\0';
printf("Server : %s\n", buffer);
close(sockfd);
return 0;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment