Skip to content

Commit

Permalink
Add ztee to zmap repo
Browse files Browse the repository at this point in the history
  • Loading branch information
dadrian committed Jan 4, 2015
1 parent 6455f35 commit 933ca78
Show file tree
Hide file tree
Showing 5 changed files with 623 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ _CPack_Packages/*
*.deb
!.gitignore
src/zblacklist
src/ztee
112 changes: 112 additions & 0 deletions lib/queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "queue.h"
#include <pthread.h>
//queue_lock used for push and pop

pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t queue_empty = PTHREAD_COND_INITIALIZER;

queue* queue_init()
{
//call with queue_init(&queue);
queue *p = malloc(sizeof(queue));
p->front = NULL;
p->back = NULL;
p->size = 0;
return p;
}

int is_empty (queue *my_queue)
{
if (my_queue->front == NULL && my_queue->front == NULL) return 1;
return 0;
}

void push_back(char* data, queue *my_queue)
{
node *new_node = malloc(sizeof(node));
new_node->prev = NULL;
new_node->next = NULL;
new_node->data = strdup(data);

pthread_mutex_lock(&queue_lock);
if (is_empty(my_queue)) {
my_queue->front = new_node;
my_queue->back = new_node;
} else {
my_queue->back->next = (struct node*)new_node;
new_node->prev = (struct node*)my_queue->back;
my_queue->back = new_node;
}
my_queue->size++;
pthread_cond_signal(&queue_empty);
pthread_mutex_unlock(&queue_lock);
}

node* pop_front(queue *my_queue)
{
pthread_mutex_lock(&queue_lock);

while (is_empty(my_queue)) {
pthread_cond_wait(&queue_empty, &queue_lock);
}
node *temp = my_queue->front;
my_queue->front = (node*)temp->next;
if (my_queue->front != NULL) {
my_queue->front->prev = NULL;
}
my_queue->size--;
pthread_mutex_unlock(&queue_lock);
return temp;
}

node* get_front(queue *my_queue)
{
pthread_mutex_lock(&queue_lock);

while (is_empty(my_queue)) {
pthread_cond_wait(&queue_empty, &queue_lock);
}

node *temp = malloc(sizeof(node));
temp = my_queue->front;
pthread_mutex_unlock(&queue_lock);
return temp;
}

node* get_back(queue *my_queue)
{
pthread_mutex_lock(&queue_lock);

while (is_empty(my_queue)) {
pthread_cond_wait(&queue_empty, &queue_lock);
}

node *temp = malloc(sizeof(node));
temp = my_queue->back;
pthread_mutex_unlock(&queue_lock);
return temp;
}

void delete_queue(queue *my_queue)
{
while (!is_empty(my_queue)) {
pop_front(my_queue);
}
}

void check_queue(queue *my_queue)
{
node *temp = my_queue->front;
while(temp){
temp = (node*)temp->next;
}
}

int get_size(queue *my_queue)
{
int buffer_size;
pthread_mutex_lock(&queue_lock);
buffer_size = my_queue->size;
pthread_mutex_unlock(&queue_lock);
return buffer_size;
}
26 changes: 26 additions & 0 deletions lib/queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

typedef struct queue_node {
char* data;
struct node *prev;
struct node *next;
} node;

typedef struct ztee_queue {
node *front;
node *back;
int size;
} queue;

queue* queue_init ();
int is_empty (queue *my_queue);
void push_back (char* data, queue *my_queue);
node* pop_front (queue *my_queue);
node* get_front (queue *my_queue);
node* get_back (queue *my_queue);
void delete_queue (queue *my_queue);
void check_queue (queue *my_queue);
int get_size (queue *my_queue);
15 changes: 14 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ SET(LIB_SOURCES
${PROJECT_SOURCE_DIR}/lib/xalloc.c
${PROJECT_SOURCE_DIR}/lib/lockfd.c
${PROJECT_SOURCE_DIR}/lib/util.c
${PROJECT_SOURCE_DIR}/lib/queue.c
)

# ADD YOUR PROBE MODULE HERE
Expand Down Expand Up @@ -87,6 +88,11 @@ SET(ZBLSOURCES
${LIB_SOURCES}
)

SET(ZTEESOURCES
ztee.c
${LIB_SOURCES}
)

# Handle various versions of socket
if(WITH_PFRING)
SET(SOURCES ${SOURCES} socket-pfring.c)
Expand Down Expand Up @@ -128,6 +134,7 @@ add_custom_command(OUTPUT parser.c

add_executable(zmap ${SOURCES})
add_executable(zblacklist ${ZBLSOURCES})
add_executable(ztee ${ZTEESOURCES})

if (APPLE OR BSD)
set(DNET_LIBRARIES "dnet")
Expand All @@ -147,7 +154,13 @@ target_link_libraries(
target_link_libraries(
zblacklist
m
)
)

target_link_libraries(
ztee
m
)

# Install binary
install(
TARGETS
Expand Down
Loading

0 comments on commit 933ca78

Please sign in to comment.