|
|
|
@ -1,19 +1,20 @@
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <stdio.h> /* fprintf, printf */
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netdb.h> /* getaddrinfo, getnameinfo */
|
|
|
|
|
#include <stdlib.h> /* exit */
|
|
|
|
|
#include <string.h> /* memset */
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
#include "net.hpp"
|
|
|
|
|
#include "logger.hpp"
|
|
|
|
|
#include "net.hpp"
|
|
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
|
|
|
|
|
@ -26,9 +27,10 @@ handle_signal(int sig)
|
|
|
|
|
|
|
|
|
|
#define BUF_SIZE 512
|
|
|
|
|
|
|
|
|
|
struct query {
|
|
|
|
|
struct query
|
|
|
|
|
{
|
|
|
|
|
uint16_t length;
|
|
|
|
|
char * url;
|
|
|
|
|
char *url;
|
|
|
|
|
unsigned char request[BUF_SIZE];
|
|
|
|
|
uint16_t reqType;
|
|
|
|
|
};
|
|
|
|
@ -36,75 +38,84 @@ struct query {
|
|
|
|
|
#define SERVER "8.8.8.8"
|
|
|
|
|
#define PORT 53
|
|
|
|
|
|
|
|
|
|
struct sockaddr *resolveHost(char *url) {
|
|
|
|
|
struct sockaddr *
|
|
|
|
|
resolveHost(char *url)
|
|
|
|
|
{
|
|
|
|
|
struct query dnsQuery;
|
|
|
|
|
dnsQuery.length = 12;
|
|
|
|
|
dnsQuery.url = url;
|
|
|
|
|
dnsQuery.reqType = 0x01;
|
|
|
|
|
// dnsQuery.request = { 0xDB, 0x42, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
|
dnsQuery.request[ 0] = 0xDB;
|
|
|
|
|
dnsQuery.request[ 1] = 0x42;
|
|
|
|
|
dnsQuery.request[ 2] = 0x01;
|
|
|
|
|
dnsQuery.request[ 3] = 0x00;
|
|
|
|
|
dnsQuery.request[ 4] = 0x00;
|
|
|
|
|
dnsQuery.request[ 5] = 0x01;
|
|
|
|
|
dnsQuery.request[ 6] = 0x00;
|
|
|
|
|
dnsQuery.request[ 7] = 0x00;
|
|
|
|
|
dnsQuery.request[ 8] = 0x00;
|
|
|
|
|
dnsQuery.request[ 9] = 0x00;
|
|
|
|
|
// dnsQuery.request = { 0xDB, 0x42, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00 };
|
|
|
|
|
dnsQuery.request[0] = 0xDB;
|
|
|
|
|
dnsQuery.request[1] = 0x42;
|
|
|
|
|
dnsQuery.request[2] = 0x01;
|
|
|
|
|
dnsQuery.request[3] = 0x00;
|
|
|
|
|
dnsQuery.request[4] = 0x00;
|
|
|
|
|
dnsQuery.request[5] = 0x01;
|
|
|
|
|
dnsQuery.request[6] = 0x00;
|
|
|
|
|
dnsQuery.request[7] = 0x00;
|
|
|
|
|
dnsQuery.request[8] = 0x00;
|
|
|
|
|
dnsQuery.request[9] = 0x00;
|
|
|
|
|
dnsQuery.request[10] = 0x00;
|
|
|
|
|
dnsQuery.request[11] = 0x00;
|
|
|
|
|
|
|
|
|
|
char * word;
|
|
|
|
|
char *word;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
llarp::LogDebug("Asking DNS server %s about %s\n", SERVER, url);
|
|
|
|
|
dnsQuery.url = strdup(url);
|
|
|
|
|
dnsQuery.reqType = 0x01;
|
|
|
|
|
|
|
|
|
|
word = strtok(url, ".");
|
|
|
|
|
while (word) {
|
|
|
|
|
llarp::LogDebug("parsing hostname: \"%s\" is %zu characters\n", word, strlen(word));
|
|
|
|
|
while(word)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogDebug("parsing hostname: \"%s\" is %zu characters\n", word,
|
|
|
|
|
strlen(word));
|
|
|
|
|
dnsQuery.request[dnsQuery.length++] = strlen(word);
|
|
|
|
|
for (i = 0; i < strlen(word); i++) {
|
|
|
|
|
for(i = 0; i < strlen(word); i++)
|
|
|
|
|
{
|
|
|
|
|
dnsQuery.request[dnsQuery.length++] = word[i];
|
|
|
|
|
}
|
|
|
|
|
word = strtok(NULL, ".");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dnsQuery.request[dnsQuery.length++] = 0x00; // End of the host name
|
|
|
|
|
dnsQuery.request[dnsQuery.length++] = 0x00; // 0x0001 - Query is a Type A query (host address)
|
|
|
|
|
dnsQuery.request[dnsQuery.length++] =
|
|
|
|
|
0x00; // 0x0001 - Query is a Type A query (host address)
|
|
|
|
|
dnsQuery.request[dnsQuery.length++] = dnsQuery.reqType;
|
|
|
|
|
dnsQuery.request[dnsQuery.length++] = 0x00; // 0x0001 - Query is class IN (Internet address)
|
|
|
|
|
dnsQuery.request[dnsQuery.length++] =
|
|
|
|
|
0x00; // 0x0001 - Query is class IN (Internet address)
|
|
|
|
|
dnsQuery.request[dnsQuery.length++] = 0x01;
|
|
|
|
|
|
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
|
//int socket;
|
|
|
|
|
// int socket;
|
|
|
|
|
ssize_t ret;
|
|
|
|
|
int rcode;
|
|
|
|
|
socklen_t size;
|
|
|
|
|
int ip = 0;
|
|
|
|
|
int length;
|
|
|
|
|
unsigned char buffer[BUF_SIZE];
|
|
|
|
|
//unsigned char tempBuf[3];
|
|
|
|
|
uint16_t QDCOUNT; //No. of items in Question Section
|
|
|
|
|
uint16_t ANCOUNT; //No. of items in Answer Section
|
|
|
|
|
uint16_t NSCOUNT; //No. of items in Authority Section
|
|
|
|
|
uint16_t ARCOUNT; //No. of items in Additional Section
|
|
|
|
|
uint16_t QCLASS; //Specifies the class of the query
|
|
|
|
|
uint16_t ATYPE; //Specifies the meaning of the data in the RDATA field
|
|
|
|
|
uint16_t ACLASS; //Specifies the class of the data in the RDATA field
|
|
|
|
|
uint32_t TTL; //The number of seconds the results can be cached
|
|
|
|
|
uint16_t RDLENGTH; //The length of the RDATA field
|
|
|
|
|
// unsigned char tempBuf[3];
|
|
|
|
|
uint16_t QDCOUNT; // No. of items in Question Section
|
|
|
|
|
uint16_t ANCOUNT; // No. of items in Answer Section
|
|
|
|
|
uint16_t NSCOUNT; // No. of items in Authority Section
|
|
|
|
|
uint16_t ARCOUNT; // No. of items in Additional Section
|
|
|
|
|
uint16_t QCLASS; // Specifies the class of the query
|
|
|
|
|
uint16_t ATYPE; // Specifies the meaning of the data in the RDATA field
|
|
|
|
|
uint16_t ACLASS; // Specifies the class of the data in the RDATA field
|
|
|
|
|
uint32_t TTL; // The number of seconds the results can be cached
|
|
|
|
|
uint16_t RDLENGTH; // The length of the RDATA field
|
|
|
|
|
uint16_t MSGID;
|
|
|
|
|
|
|
|
|
|
int sockfd;
|
|
|
|
|
|
|
|
|
|
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
if (sockfd < 0) {
|
|
|
|
|
if(sockfd < 0)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogWarn("Error creating socket!\n");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
//socket = sockfd;
|
|
|
|
|
// socket = sockfd;
|
|
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
@ -112,97 +123,113 @@ struct sockaddr *resolveHost(char *url) {
|
|
|
|
|
addr.sin_port = htons(PORT);
|
|
|
|
|
size = sizeof(addr);
|
|
|
|
|
|
|
|
|
|
//hexdump("sending packet", &dnsQuery.request, dnsQuery.length);
|
|
|
|
|
ret = sendto(sockfd, dnsQuery.request, dnsQuery.length, 0, (struct sockaddr*)&addr, size);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
// hexdump("sending packet", &dnsQuery.request, dnsQuery.length);
|
|
|
|
|
ret = sendto(sockfd, dnsQuery.request, dnsQuery.length, 0,
|
|
|
|
|
(struct sockaddr *)&addr, size);
|
|
|
|
|
if(ret < 0)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogWarn("Error Sending Request");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
//printf("Sent\n");
|
|
|
|
|
// printf("Sent\n");
|
|
|
|
|
|
|
|
|
|
memset(&buffer, 0, BUF_SIZE);
|
|
|
|
|
ret = recvfrom(sockfd, buffer, BUF_SIZE, 0, (struct sockaddr*)&addr, &size);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
ret = recvfrom(sockfd, buffer, BUF_SIZE, 0, (struct sockaddr *)&addr, &size);
|
|
|
|
|
if(ret < 0)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogWarn("Error Receiving Response");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//hexdump("received packet", &buffer, ret);
|
|
|
|
|
// hexdump("received packet", &buffer, ret);
|
|
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
|
|
|
|
|
|
rcode = (buffer[3] & 0x0F);
|
|
|
|
|
|
|
|
|
|
//tempBuf[0] = buffer[4];
|
|
|
|
|
//tempBuf[1] = buffer[5];
|
|
|
|
|
//tempBuf[2] = '\0';
|
|
|
|
|
// tempBuf[0] = buffer[4];
|
|
|
|
|
// tempBuf[1] = buffer[5];
|
|
|
|
|
// tempBuf[2] = '\0';
|
|
|
|
|
|
|
|
|
|
//printf("%0x %0x %0x %0x\n", buffer[4], buffer[5], tempBuf[0], tempBuf[1]);
|
|
|
|
|
// printf("%0x %0x %0x %0x\n", buffer[4], buffer[5], tempBuf[0], tempBuf[1]);
|
|
|
|
|
|
|
|
|
|
//QDCOUNT = (uint16_t) strtol(tempBuf, NULL, 16);
|
|
|
|
|
QDCOUNT = (uint16_t) buffer[4] * 0x100 + buffer[5];
|
|
|
|
|
// QDCOUNT = (uint16_t) strtol(tempBuf, NULL, 16);
|
|
|
|
|
QDCOUNT = (uint16_t)buffer[4] * 0x100 + buffer[5];
|
|
|
|
|
llarp::LogDebug("entries in question section: %u\n", QDCOUNT);
|
|
|
|
|
ANCOUNT = (uint16_t) buffer[6] * 0x100 + buffer[7];
|
|
|
|
|
ANCOUNT = (uint16_t)buffer[6] * 0x100 + buffer[7];
|
|
|
|
|
llarp::LogDebug("records in answer section: %u\n", ANCOUNT);
|
|
|
|
|
NSCOUNT = (uint16_t) buffer[8] * 0x100 + buffer[9];
|
|
|
|
|
NSCOUNT = (uint16_t)buffer[8] * 0x100 + buffer[9];
|
|
|
|
|
llarp::LogDebug("name server resource record count: %u\n", NSCOUNT);
|
|
|
|
|
ARCOUNT = (uint16_t) buffer[10] * 0x100 + buffer[11];
|
|
|
|
|
ARCOUNT = (uint16_t)buffer[10] * 0x100 + buffer[11];
|
|
|
|
|
llarp::LogDebug("additional records count: %u\n", ARCOUNT);
|
|
|
|
|
|
|
|
|
|
llarp::LogDebug("query type: %u\n", dnsQuery.reqType);
|
|
|
|
|
QCLASS = (uint16_t) dnsQuery.request[dnsQuery.length - 2] * 0x100 + dnsQuery.request[dnsQuery.length - 1];
|
|
|
|
|
QCLASS = (uint16_t)dnsQuery.request[dnsQuery.length - 2] * 0x100
|
|
|
|
|
+ dnsQuery.request[dnsQuery.length - 1];
|
|
|
|
|
llarp::LogDebug("query class: %u\n", QCLASS);
|
|
|
|
|
length = dnsQuery.length + 1; // to skip 0xc00c
|
|
|
|
|
ATYPE = (uint16_t) buffer[length + 1] * 0x100 + buffer[length + 2];
|
|
|
|
|
ATYPE = (uint16_t)buffer[length + 1] * 0x100 + buffer[length + 2];
|
|
|
|
|
llarp::LogDebug("answer type: %u\n", ATYPE);
|
|
|
|
|
ACLASS = (uint16_t) buffer[length + 3] * 0x100 + buffer[length + 4];
|
|
|
|
|
ACLASS = (uint16_t)buffer[length + 3] * 0x100 + buffer[length + 4];
|
|
|
|
|
llarp::LogDebug("answer class: %u\n", ACLASS);
|
|
|
|
|
TTL = (uint32_t) buffer[length + 5] * 0x1000000 + buffer[length + 6] * 0x10000 + buffer[length + 7] * 0x100 + buffer[length + 8];
|
|
|
|
|
TTL = (uint32_t)buffer[length + 5] * 0x1000000 + buffer[length + 6] * 0x10000
|
|
|
|
|
+ buffer[length + 7] * 0x100 + buffer[length + 8];
|
|
|
|
|
llarp::LogDebug("seconds to cache: %u\n", TTL);
|
|
|
|
|
RDLENGTH = (uint16_t) buffer[length + 9] * 0x100 + buffer[length + 10];
|
|
|
|
|
RDLENGTH = (uint16_t)buffer[length + 9] * 0x100 + buffer[length + 10];
|
|
|
|
|
llarp::LogDebug("bytes in answer: %u\n", RDLENGTH);
|
|
|
|
|
MSGID = (uint16_t) buffer[0] * 0x100 + buffer[1];
|
|
|
|
|
MSGID = (uint16_t)buffer[0] * 0x100 + buffer[1];
|
|
|
|
|
llarp::LogDebug("answer msg id: %u\n", MSGID);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (rcode == 2) {
|
|
|
|
|
if(rcode == 2)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogWarn("nameserver %s returned SERVFAIL:\n", SERVER);
|
|
|
|
|
llarp::LogWarn(" the name server was unable to process this query due to a\n problem with the name server.\n");
|
|
|
|
|
llarp::LogWarn(
|
|
|
|
|
" the name server was unable to process this query due to a\n "
|
|
|
|
|
"problem with the name server.\n");
|
|
|
|
|
return nullptr;
|
|
|
|
|
} else if (rcode == 3) {
|
|
|
|
|
llarp::LogWarn("nameserver %s returned NXDOMAIN for %s:\n", SERVER, dnsQuery.url);
|
|
|
|
|
llarp::LogWarn(" the domain name referenced in the query does not exist\n");
|
|
|
|
|
}
|
|
|
|
|
else if(rcode == 3)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogWarn("nameserver %s returned NXDOMAIN for %s:\n", SERVER,
|
|
|
|
|
dnsQuery.url);
|
|
|
|
|
llarp::LogWarn(
|
|
|
|
|
" the domain name referenced in the query does not exist\n");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* search for and print IPv4 addresses */
|
|
|
|
|
if (dnsQuery.reqType == 0x01) {
|
|
|
|
|
if(dnsQuery.reqType == 0x01)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogDebug("DNS server's answer is: (type#=%u):", ATYPE);
|
|
|
|
|
//printf("IPv4 address(es) for %s:\n", dnsQuery.url);
|
|
|
|
|
for (i = 0 ; i < ret ; i++) {
|
|
|
|
|
if (buffer[i] == 0xC0 && buffer[i+3] == 0x01) {
|
|
|
|
|
ip++; i += 12; /* ! += buf[i+1]; */
|
|
|
|
|
llarp::LogDebug(" %u.%u.%u.%u\n", buffer[i], buffer[i+1], buffer[i+2], buffer[i+3]);
|
|
|
|
|
// printf("IPv4 address(es) for %s:\n", dnsQuery.url);
|
|
|
|
|
for(i = 0; i < ret; i++)
|
|
|
|
|
{
|
|
|
|
|
if(buffer[i] == 0xC0 && buffer[i + 3] == 0x01)
|
|
|
|
|
{
|
|
|
|
|
ip++;
|
|
|
|
|
i += 12; /* ! += buf[i+1]; */
|
|
|
|
|
llarp::LogDebug(" %u.%u.%u.%u\n", buffer[i], buffer[i + 1],
|
|
|
|
|
buffer[i + 2], buffer[i + 3]);
|
|
|
|
|
struct sockaddr *g_addr = new sockaddr;
|
|
|
|
|
g_addr->sa_family = AF_INET;
|
|
|
|
|
g_addr->sa_len = sizeof(in_addr);
|
|
|
|
|
// g_addr->sa_len = sizeof(in_addr);
|
|
|
|
|
struct in_addr *addr = &((struct sockaddr_in *)g_addr)->sin_addr;
|
|
|
|
|
unsigned char * ip;
|
|
|
|
|
unsigned char *ip;
|
|
|
|
|
|
|
|
|
|
//have ip point to s_addr
|
|
|
|
|
ip = (unsigned char *) &(addr->s_addr);
|
|
|
|
|
// have ip point to s_addr
|
|
|
|
|
ip = (unsigned char *)&(addr->s_addr);
|
|
|
|
|
|
|
|
|
|
ip[0]=buffer[i + 0];
|
|
|
|
|
ip[1]=buffer[i + 1];
|
|
|
|
|
ip[2]=buffer[i + 2];
|
|
|
|
|
ip[3]=buffer[i + 3];
|
|
|
|
|
ip[0] = buffer[i + 0];
|
|
|
|
|
ip[1] = buffer[i + 1];
|
|
|
|
|
ip[2] = buffer[i + 2];
|
|
|
|
|
ip[3] = buffer[i + 3];
|
|
|
|
|
|
|
|
|
|
return g_addr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ip) {
|
|
|
|
|
if(!ip)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogWarn(" No IPv4 address found in the DNS response!\n");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
@ -210,23 +237,26 @@ struct sockaddr *resolveHost(char *url) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int get16bits(const char*& buffer) throw ()
|
|
|
|
|
int
|
|
|
|
|
get16bits(const char *&buffer) throw()
|
|
|
|
|
{
|
|
|
|
|
int value = static_cast<unsigned char> (buffer[0]);
|
|
|
|
|
int value = static_cast< unsigned char >(buffer[0]);
|
|
|
|
|
value = value << 8;
|
|
|
|
|
value += static_cast<unsigned char> (buffer[1]);
|
|
|
|
|
value += static_cast< unsigned char >(buffer[1]);
|
|
|
|
|
buffer += 2;
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void put16bits(char*& buffer, uint value) throw ()
|
|
|
|
|
void
|
|
|
|
|
put16bits(char *&buffer, uint value) throw()
|
|
|
|
|
{
|
|
|
|
|
buffer[0] = (value & 0xFF00) >> 8;
|
|
|
|
|
buffer[1] = value & 0xFF;
|
|
|
|
|
buffer += 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void put32bits(char*& buffer, unsigned long value) throw ()
|
|
|
|
|
void
|
|
|
|
|
put32bits(char *&buffer, unsigned long value) throw()
|
|
|
|
|
{
|
|
|
|
|
buffer[0] = (value & 0xFF000000) >> 24;
|
|
|
|
|
buffer[1] = (value & 0xFF0000) >> 16;
|
|
|
|
@ -252,7 +282,8 @@ struct dns_msg
|
|
|
|
|
uint arCount;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dns_msg *decode_hdr(const char *buffer)
|
|
|
|
|
dns_msg *
|
|
|
|
|
decode_hdr(const char *buffer)
|
|
|
|
|
{
|
|
|
|
|
dns_msg *hdr = new dns_msg;
|
|
|
|
|
hdr->id = get16bits(buffer);
|
|
|
|
@ -271,16 +302,15 @@ dns_msg *decode_hdr(const char *buffer)
|
|
|
|
|
return hdr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void code_domain(char*& buffer, const std::string& domain) throw()
|
|
|
|
|
void
|
|
|
|
|
code_domain(char *&buffer, const std::string &domain) throw()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
int start(0), end; // indexes
|
|
|
|
|
llarp::LogInfo("domain [", domain, "]");
|
|
|
|
|
while ((end = domain.find('.', start)) != std::string::npos)
|
|
|
|
|
while((end = domain.find('.', start)) != std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
*buffer++ = end - start; // label length octet
|
|
|
|
|
for (int i=start; i<end; i++)
|
|
|
|
|
for(int i = start; i < end; i++)
|
|
|
|
|
{
|
|
|
|
|
*buffer++ = domain[i]; // label octets
|
|
|
|
|
llarp::LogInfo("Writing ", domain[i], " at ", i);
|
|
|
|
@ -291,7 +321,7 @@ void code_domain(char*& buffer, const std::string& domain) throw()
|
|
|
|
|
llarp::LogInfo("start ", start, " domain size ", domain.size());
|
|
|
|
|
|
|
|
|
|
*buffer++ = domain.size() - start; // last label length octet
|
|
|
|
|
for (int i=start; i<domain.size(); i++)
|
|
|
|
|
for(int i = start; i < domain.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
*buffer++ = domain[i]; // last label octets
|
|
|
|
|
llarp::LogInfo("Writing ", domain[i], " at ", i);
|
|
|
|
@ -323,10 +353,11 @@ main(int argc, char *argv[])
|
|
|
|
|
m_address.sin_family = AF_INET;
|
|
|
|
|
m_address.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
m_address.sin_port = htons(1053);
|
|
|
|
|
int rbind = bind(m_sockfd, (struct sockaddr *) & m_address,
|
|
|
|
|
sizeof (struct sockaddr_in));
|
|
|
|
|
int rbind =
|
|
|
|
|
bind(m_sockfd, (struct sockaddr *)&m_address, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
|
|
if (rbind != 0) {
|
|
|
|
|
if(rbind != 0)
|
|
|
|
|
{
|
|
|
|
|
llarp::LogError("Could not bind: ", strerror(errno));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
@ -335,30 +366,33 @@ main(int argc, char *argv[])
|
|
|
|
|
const size_t HDR_OFFSET = 12;
|
|
|
|
|
char buffer[BUFFER_SIZE]; // 1024 is buffer size
|
|
|
|
|
struct sockaddr_in clientAddress;
|
|
|
|
|
socklen_t addrLen = sizeof (struct sockaddr_in);
|
|
|
|
|
socklen_t addrLen = sizeof(struct sockaddr_in);
|
|
|
|
|
|
|
|
|
|
signal(SIGINT, handle_signal);
|
|
|
|
|
while(!done)
|
|
|
|
|
{
|
|
|
|
|
// sigint quits after next packet
|
|
|
|
|
int nbytes = recvfrom(m_sockfd, buffer, BUFFER_SIZE, 0,
|
|
|
|
|
(struct sockaddr *) &clientAddress, &addrLen);
|
|
|
|
|
(struct sockaddr *)&clientAddress, &addrLen);
|
|
|
|
|
llarp::LogInfo("Received Bytes ", nbytes);
|
|
|
|
|
|
|
|
|
|
const char* p_buffer = buffer;
|
|
|
|
|
const char *p_buffer = buffer;
|
|
|
|
|
dns_msg *msg = decode_hdr(p_buffer);
|
|
|
|
|
//llarp::LogInfo("DNS_MSG size", sizeof(dns_msg));
|
|
|
|
|
// llarp::LogInfo("DNS_MSG size", sizeof(dns_msg));
|
|
|
|
|
p_buffer += HDR_OFFSET;
|
|
|
|
|
std::string m_qName = "";
|
|
|
|
|
int length = *p_buffer++;
|
|
|
|
|
//llarp::LogInfo("qNamLen", length);
|
|
|
|
|
while (length != 0) {
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
|
// llarp::LogInfo("qNamLen", length);
|
|
|
|
|
while(length != 0)
|
|
|
|
|
{
|
|
|
|
|
for(int i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
char c = *p_buffer++;
|
|
|
|
|
m_qName.append(1, c);
|
|
|
|
|
}
|
|
|
|
|
length = *p_buffer++;
|
|
|
|
|
if (length != 0) m_qName.append(1,'.');
|
|
|
|
|
if(length != 0)
|
|
|
|
|
m_qName.append(1, '.');
|
|
|
|
|
}
|
|
|
|
|
uint m_qType = get16bits(p_buffer);
|
|
|
|
|
uint m_qClass = get16bits(p_buffer);
|
|
|
|
@ -368,7 +402,7 @@ main(int argc, char *argv[])
|
|
|
|
|
|
|
|
|
|
std::string copy(m_qName);
|
|
|
|
|
sockaddr *hostRes = resolveHost((char *)copy.c_str());
|
|
|
|
|
if (!hostRes)
|
|
|
|
|
if(!hostRes)
|
|
|
|
|
{
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
@ -384,8 +418,8 @@ main(int argc, char *argv[])
|
|
|
|
|
m_response.asString();
|
|
|
|
|
*/
|
|
|
|
|
memset(buffer, 0, BUFFER_SIZE);
|
|
|
|
|
char* write_buffer = buffer;
|
|
|
|
|
char* bufferBegin = buffer;
|
|
|
|
|
char *write_buffer = buffer;
|
|
|
|
|
char *bufferBegin = buffer;
|
|
|
|
|
// build header
|
|
|
|
|
put16bits(write_buffer, msg->id);
|
|
|
|
|
int fields = (1 << 15); // QR => message type, 1 = response
|
|
|
|
@ -420,9 +454,10 @@ main(int argc, char *argv[])
|
|
|
|
|
uint out_bytes = write_buffer - bufferBegin;
|
|
|
|
|
llarp::LogInfo("Sending ", out_bytes, " bytes");
|
|
|
|
|
|
|
|
|
|
//nbytes = m_response.code(buffer);
|
|
|
|
|
// nbytes = m_response.code(buffer);
|
|
|
|
|
|
|
|
|
|
sendto(m_sockfd, buffer, out_bytes, 0, (struct sockaddr *) &clientAddress, addrLen);
|
|
|
|
|
sendto(m_sockfd, buffer, out_bytes, 0, (struct sockaddr *)&clientAddress,
|
|
|
|
|
addrLen);
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
std::string host("www.google.com");
|
|
|
|
@ -436,5 +471,4 @@ main(int argc, char *argv[])
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
return code;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|