Skip to content

Commit

Permalink
Merge pull request #4 from evuraan/marutha
Browse files Browse the repository at this point in the history
Marutha
  • Loading branch information
evuraan authored Mar 4, 2021
2 parents 0c5d2a2 + 262ffbc commit 830afda
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 37 deletions.
Binary file removed bin/magan-armv7l
Binary file not shown.
Binary file removed bin/magan-go-linux-386
Binary file not shown.
Binary file removed bin/magan-go-linux-amd64
Binary file not shown.
Binary file removed bin/magan-go-linux-armv6l
Binary file not shown.
Binary file removed bin/magan-go-linux-armv7l
Binary file not shown.
Binary file removed bin/magan-go-win-386.exe
Binary file not shown.
Binary file removed bin/magan-go-win-amd64.exe
Binary file not shown.
Binary file removed bin/magan-x86_64
Binary file not shown.
70 changes: 41 additions & 29 deletions src/magan.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,15 @@ curl_version_info_data *curl_version_data;
char getter_url[] = "https://dns.google.com/";
int pid;
char Name[] = "Magan";
char Version[] = "Magan/2.0h epoll+pool+cache";
char Version[] = "Magan/3.0a";
int LISTEN_PORT = 53;
int debug = 0;
pthread_t udpWorkers[UDP_THREADS] = { 0 };
int udpPipe[2] = { 0 };
struct cacheStruct cache[CACHE_MAX_ITEMS] = { 0 };

char dot[] = ".";

int sockfd = 0;

int epollFD = 0;
Expand Down Expand Up @@ -668,11 +670,7 @@ void get_reply(char *request, int PROTO, struct reply *reply, int cut_here) {
struct dns_question dns_question = { 0 };
memcpy(&dns_question, r, sizeof(dns_question));

char Google_url[bufsize] = { 0 };
snprintf(Google_url, bufsize, "%sresolve?name=%s&type=%d", getter_url, readable, ntohs(dns_question.type));

//printf("Google_url: %s\n" , Google_url);
debug_print("Url: %s\n", Google_url);
uint16_t queType = ntohs(dns_question.type);

int Question_Size = end + 1 + sizeof(dns_question);
char Question[bufsize] = { 0 };
Expand All @@ -687,20 +685,35 @@ void get_reply(char *request, int PROTO, struct reply *reply, int cut_here) {
struct dns_header reply_header = { 0 };
memcpy(&reply_header, &header, sizeof(header)); // copy header from request,

// deduce url to go for:
char url[bufsize] = { 0 };
char *pointTo = readable;
if ((queType == 2) && (!strnlen(readable, 10))) {
// a recursive query.
pointTo = dot;
}
snprintf(url, bufsize, "%sresolve?name=%s&type=%d", getter_url, pointTo, queType);
debug_print("Url: %s\n", url);

char *json_buffer = 0;
char awkward[bufsize] = { 0 };
int gotData = 0;

char *json_buffer = getCacheEntry(Google_url);
json_buffer = getCacheEntry(url);
if (!json_buffer) {
if (doCurlStuff(Google_url, awkward)) {
if (doCurlStuff(url, awkward)) {
json_buffer = awkward;
gotData++;
}
} else {
debug_print("Cache hit for %s\n", Google_url);
debug_print("Cache hit for %s\n", url);
gotData++;
}

if (!json_buffer) {
// this may need an rcode 5, pivots on gotData below.
json_buffer = awkward;
}

if (gotData) {
rcode = 0;
reply_header.rcode = rcode;
Expand Down Expand Up @@ -823,7 +836,6 @@ void get_reply(char *request, int PROTO, struct reply *reply, int cut_here) {
char *interim_buf = (char *)json_object_get_string(name);
convert(con_ns, interim_buf);
int end_here = strnlen(con_ns, bufsize);

memcpy(R, con_ns, end_here);
R += end_here + 1;
reply->sendSize += end_here + 1;
Expand Down Expand Up @@ -1464,27 +1476,27 @@ struct Node *get_CURLHANDLE() {
// max len: ONE_K
void findNthWord(char *line_in, int n, char *word) {

// since we decay, copy the incoming to another buffer
char line[bufsize] = { 0 };
memcpy(line, line_in, strnlen(line_in, ONE_K));

int i = 0;
char delim[] = " ";
char *Field = word;
char *LinePtr = strtok(line, delim);
while (LinePtr) {
int _len = strnlen(LinePtr, ONE_K);
if (i == n) {
strncpy(Field, LinePtr, _len + 1);
// since we decay, copy the incoming to another buffer
char line[bufsize] = { 0 };
memcpy(line, line_in, strnlen(line_in, ONE_K));

int i = 0;
char delim[] = " ";
char *Field = word;
char *LinePtr = strtok(line, delim);
while (LinePtr) {
int _len = strnlen(LinePtr, ONE_K);
if (i == n) {
strncpy(Field, LinePtr, _len + 1);
// printf("[%s] nth: %d, Field: %s\n", __func__, n, Field);
break;
}
break;
}

i++;
LinePtr = strtok(NULL, delim);
}
i++;
LinePtr = strtok(NULL, delim);
}

return;
return;
}

int debug_print(char *format, ...) {
Expand Down
19 changes: 11 additions & 8 deletions src/magan_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (

const (
binaryName = "Magan"
version = "Magan/1.5.0a"
version = "Magan/1.6"
validity = 5 * 60 // cache item validity in seconds
cacheMax = 100 // Max items to keep in the cache.
interval = 10 // seconds to wait for house keeping runs
Expand Down Expand Up @@ -373,10 +373,14 @@ func gatherReply(queryBuffer []uint8) *bytes.Buffer {
binary.Write(buf, binary.BigEndian, tempReply[12:qlen+12])

for i := 0; i < anCountInt; i++ {

converted := convert(response.Answer[i].Name)
buf.Write([]byte(converted))

if (_type == "NS") && (theyAskedFor == ".") {
if i > 0 {
buf.WriteByte(0)
}
} else {
convertName := convert(response.Answer[i].Name)
buf.Write([]byte(convertName))
}
var dnsRRStruct dnsRRStruct
dnsRRStruct.TYPE = uint16(response.Answer[i].Type)
dnsRRStruct.CLASS = 1
Expand All @@ -395,6 +399,7 @@ func gatherReply(queryBuffer []uint8) *bytes.Buffer {
dnsRRStruct.RDLEN = uint16(len(mehu))
binary.Write(buf, binary.BigEndian, dnsRRStruct)
buf.Write([]byte(mehu))

case 16, 99:
allRaw := response.Answer[i].Data
var mehu string
Expand Down Expand Up @@ -536,9 +541,7 @@ func convert(_input string) string {
j++
}
}

out := b.String()
return out
return b.String()
}

func print(strings string, args ...interface{}) {
Expand Down

0 comments on commit 830afda

Please sign in to comment.