-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some changes coming. still not working : )
- Loading branch information
Showing
9 changed files
with
332 additions
and
16 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include <string.h> | ||
#include <netinet/in.h> | ||
#include <stdlib.h> | ||
|
||
#include "data_queue.h" | ||
|
||
int data_queue_init(data_queue * dq, int size) { | ||
if(dq == (void *) 0 || size <= 0) { | ||
return 1; | ||
} | ||
|
||
dq->data = (data_queue_element *) malloc(sizeof(data_queue_element) * size); | ||
|
||
if(dq->data == (void *) 0) { | ||
return 2; | ||
} | ||
|
||
if(pthread_mutex_init(&(dq->mutex), (void *) 0)) { | ||
free((void *)dq->data); | ||
return 3; | ||
} | ||
|
||
if(pthread_cond_init(&(dq->cond), (void *) 0)) { | ||
pthread_mutex_destroy(&(dq->mutex)); | ||
free((void *)dq->data); | ||
return 4; | ||
} | ||
|
||
dq->size = size; | ||
dq->count = 0; | ||
|
||
return 0; | ||
} | ||
|
||
int data_queue_get_size(const data_queue * dq) { | ||
if(dq == (void *) 0) { | ||
return 0; | ||
} | ||
return dq->size; | ||
} | ||
|
||
int data_queue_get_count(data_queue * dq) { | ||
int res = -1; | ||
if(dq != (void *) 0) { | ||
if(!pthread_mutex_lock(&(dq->mutex))) { | ||
res = dq->count; | ||
pthread_mutex_unlock(&(dq->mutex)); | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
int data_queue_get_data(data_queue * dq, data_queue_element * data) { | ||
int res = -1; | ||
if(dq != (void *) 0) { | ||
if(!pthread_mutex_lock(&(dq->mutex))) { | ||
while(1) { | ||
if(dq->count == 0) { | ||
pthread_cond_wait(&(dq->cond), &(dq->mutex)); | ||
continue; | ||
} | ||
|
||
dq->count --; | ||
if(data != (void *) 0) { | ||
memcpy((void *) data, | ||
(void *) &(dq->data[dq->count]), | ||
sizeof(data_queue_element)); | ||
} | ||
res = 0; | ||
break; | ||
} | ||
|
||
|
||
pthread_mutex_unlock(&(dq->mutex)); | ||
} | ||
} | ||
return res; | ||
} | ||
int data_queue_put_data(data_queue * dq, const struct sockaddr_in * addr, const void * data, int data_len) { | ||
int res = -1; | ||
if(dq != (void *) 0 && | ||
data != (void *) 0 && | ||
addr != (void *) 0 && | ||
data_len > 0 && | ||
data_len <= TESTDNSD_MAX_PACKET_SIZE) { | ||
|
||
if(!pthread_mutex_lock(&(dq->mutex))) { | ||
if(dq->count < dq->size) { | ||
memcpy((void *) &(dq->data[dq->count].addr), | ||
(void *) addr, | ||
sizeof(addr)); | ||
memcpy((void *) &(dq->data[dq->count].data), | ||
(void *) data, | ||
data_len); | ||
dq->data[dq->count].data_len = data_len; | ||
dq->count ++; | ||
res = 0; | ||
pthread_cond_signal(&(dq->cond)); | ||
} | ||
pthread_mutex_unlock(&(dq->mutex)); | ||
} | ||
} | ||
return res; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef __TESTDNSD_DATA_QUEUE_H__ | ||
#define __TESTDNSD_DATA_QUEUE_H__ | ||
|
||
#include <netinet/in.h> | ||
#include <pthread.h> | ||
|
||
#include "config.h" | ||
|
||
struct _data_queue_element { | ||
struct sockaddr_in addr; | ||
char data[TESTDNSD_MAX_PACKET_SIZE]; | ||
int data_len; | ||
}; | ||
|
||
typedef struct _data_queue_element data_queue_element; | ||
|
||
struct _data_queue { | ||
int size; | ||
int count; | ||
data_queue_element * data; | ||
pthread_mutex_t mutex; | ||
pthread_cond_t cond; | ||
}; | ||
|
||
typedef struct _data_queue data_queue; | ||
|
||
int data_queue_init(data_queue * dq, int size); | ||
int data_queue_get_size(const data_queue * dq); | ||
int data_queue_get_count(data_queue * dq); | ||
int data_queue_get_data(data_queue * dq, data_queue_element * data); | ||
int data_queue_put_data(data_queue * dq, const struct sockaddr_in * addr, const void * data, int data_len); | ||
|
||
#endif /* __TESTDNSD_DATA_QUEUE_H__ */ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef __TESTDNSD_DNS_HEADER_H__ | ||
#define __TESTDNSD_DNS_HEADER_H__ | ||
|
||
#include <stdint.h> | ||
|
||
#pragma pack(1) | ||
struct _dns_header { | ||
uint16_t id; | ||
uint16_t flags; | ||
uint16_t qdcount; | ||
uint16_t ancount; | ||
uint16_t arcount; | ||
}; | ||
#pragma pack() | ||
|
||
typedef struct _dns_header dns_header; | ||
|
||
#endif /* __TESTDNSD_DNS_HEADER_H__ */ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include "globals.h" | ||
|
||
lookupdb DB; | ||
int SOCKET; | ||
data_queue INQ; | ||
data_queue OUTQ; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef __TESTDNSD_GLOBALS_H__ | ||
#define __TESTDNSD_GLOBALS_H__ | ||
|
||
#include "config.h" | ||
#include "lookupdb.h" | ||
#include "data_queue.h" | ||
|
||
extern lookupdb DB; | ||
extern int SOCKET; | ||
extern data_queue INQ; | ||
extern data_queue OUTQ; | ||
|
||
#ifndef XXLOG | ||
#if TESTDNSD_DAEMON == 1 | ||
#include<syslog.h> | ||
#define XXLOG_INIT() openlog(TESTDNSD_SYSLOG_IDENT, LOG_PID | LOG_NDELAY, LOG_USER) | ||
#define XXLOG(...) syslog ( LOG_INFO , __VA_ARGS__ ) | ||
#else | ||
#include<stdio.h> | ||
#define XXLOG_INIT() {} | ||
#define XXLOG(...) printf ( __VA_ARGS__ ) | ||
#endif /* TESTDNSD_DAEMON == 1 */ | ||
#endif /* XXLOG */ | ||
|
||
#endif /* __TESTDNSD_GLOBALS_H__ */ |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <string.h> | ||
#include <errno.h> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <arpa/inet.h> | ||
|
||
#include "globals.h" | ||
#include "receiver.h" | ||
#include "dns_header.h" | ||
|
||
#define RECEIVER_DEBUG 1 | ||
|
||
void receiver() { | ||
|
||
ssize_t bytes_read; | ||
char read_buffer[RECEIVER_READ_BUFFER_SIZE]; | ||
struct sockaddr_in src_addr; | ||
socklen_t src_len; | ||
|
||
XXLOG("Starting receiver"); | ||
|
||
while(1) { | ||
|
||
#ifdef RECEIVER_DEBUG | ||
XXLOG("Waiting for data"); | ||
#endif /* RECEIVER_DEBUG */ | ||
|
||
bytes_read = recvfrom(SOCKET, | ||
(void *) read_buffer, | ||
RECEIVER_READ_BUFFER_SIZE, | ||
0, | ||
(struct sockaddr *) &src_addr, | ||
&src_len); | ||
if(bytes_read == -1) { | ||
if(errno == EINTR) { | ||
continue; | ||
} | ||
else { | ||
/* Shit happens */ | ||
XXLOG("Receiver failed (%s)", strerror(errno)); | ||
return; | ||
} | ||
} | ||
|
||
XXLOG("Receiver receives %d bytes", bytes_read); | ||
|
||
if(bytes_read > 0) { | ||
#ifdef RECEIVER_DEBUG | ||
XXLOG("Putting data to the queue"); | ||
#endif /* RECEIVER_DEBUG */ | ||
if(data_queue_put_data(&INQ, &src_addr, (void *) read_buffer, bytes_read)) { | ||
XXLOG("Receiver error: input queue operation failure"); | ||
} | ||
#ifdef RECEIVER_DEBUG | ||
else { | ||
XXLOG("Putting data to the queue success"); | ||
} | ||
#endif /* RECEIVER_DEBUG */ | ||
} | ||
} | ||
|
||
XXLOG("Receiver finished"); | ||
return; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef __TESTDNSD_RECEIVER_H__ | ||
#define __TESTDNSD_RECEIVER_H__ | ||
|
||
#define RECEIVER_READ_BUFFER_SIZE 1024 | ||
|
||
void receiver(); | ||
|
||
#endif /* __TESTDNSD_RECEIVER_H__ */ |