Skip to content

Commit

Permalink
introduce some basic 'connect to udp server' code.
Browse files Browse the repository at this point in the history
early days, but something to build on
  • Loading branch information
kernelslacker committed Feb 22, 2017
1 parent c541485 commit 1570804
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 17 deletions.
3 changes: 0 additions & 3 deletions include/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,4 @@ void output_syscall_prefix(struct syscallrecord *rec);
void output_syscall_postfix(struct syscallrecord *rec);
void output_rendered_buffer(char *buffer);

void init_logging(void);
void shutdown_logging(void);

void debugf(const char *fmt, ...);
7 changes: 7 additions & 0 deletions include/udp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#define TRINITY_LOG_PORT 6665

void init_logging(char *optarg);
void shutdown_logging(void);
void sendudp(char *buffer);
14 changes: 2 additions & 12 deletions log.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include "log.h"
#include "params.h" // logging, quiet_level
#include "pids.h"
#include "shm.h"
#include "trinity.h"
#include "params.h" // quiet_level

#define BUFSIZE 1024 // decoded syscall args are fprintf'd directly, this is for everything else.

Expand Down Expand Up @@ -81,7 +80,6 @@ void outputstd(const char *fmt, ...)
}


// TODO: combine the below with output()
void output_rendered_buffer(char *buffer)
{
/* Output to stdout only if -q param is not specified */
Expand All @@ -90,11 +88,3 @@ void output_rendered_buffer(char *buffer)
fflush(stdout);
}
}

void init_logging(void)
{
}

void shutdown_logging(void)
{
}
2 changes: 2 additions & 0 deletions params.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "tables.h"
#include "taint.h"
#include "trinity.h" // progname
#include "udp.h"

bool set_debug = FALSE;
bool do_specific_syscall = FALSE;
Expand Down Expand Up @@ -209,6 +210,7 @@ void parse_args(int argc, char *argv[])

case 'l':
outputerr("-l currently does nothing. TBD.\n");
init_logging(optarg);
break;

case 'L':
Expand Down
3 changes: 1 addition & 2 deletions trinity.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "tables.h"
#include "taint.h"
#include "trinity.h"
#include "udp.h"
#include "uid.h"
#include "version.h"

Expand Down Expand Up @@ -121,8 +122,6 @@ int main(int argc, char* argv[])

change_tmp_dir();

init_logging();

init_shm();

init_taint_checking();
Expand Down
80 changes: 80 additions & 0 deletions udp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "trinity.h"
#include "udp.h"

static int logging_enabled = FALSE;

static int logsocket = -1;

static struct sockaddr_in udpserver;

void init_logging(char *optarg)
{
struct hostent *he;
struct sockaddr_in udpclient;
struct in_addr **addr_list;
char *ip;
int ret;
unsigned int i;

if ((he = gethostbyname(optarg)) == NULL) {
printf("gethostbyname:%s\n", strerror(errno));
exit(EXIT_FAILURE);
}

addr_list = (struct in_addr **)he->h_addr_list;
for (i = 0; addr_list[i] != NULL; i++) {
ip = inet_ntoa(*addr_list[i]);

udpserver.sin_family = AF_INET;
udpserver.sin_addr.s_addr = inet_addr(ip);
udpserver.sin_port = htons(TRINITY_LOG_PORT);
}
printf("Logging to %s\n", ip);

logsocket = socket(AF_INET, SOCK_DGRAM, 0);
if (logsocket == -1) {
printf("Could not create a socket: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

udpclient.sin_family = AF_INET;
udpclient.sin_addr.s_addr = INADDR_ANY;
udpclient.sin_port = 0;

ret = bind(logsocket, (struct sockaddr *) &udpclient, sizeof(udpclient));
if (ret != 0) {
printf("Could not bind to address: %s\n", strerror(errno));
close(logsocket);
exit(EXIT_FAILURE);
}

logging_enabled = TRUE;
}

void sendudp(char *buffer)
{
int ret;

if (logging_enabled == FALSE)
return;

ret = sendto(logsocket, buffer, strlen(buffer) + 1, 0, (struct sockaddr *) &udpserver, sizeof(udpserver));
if (ret == -1) {
fprintf(stderr, "sendto: %s\n", strerror(errno));
close(logsocket);
exit(EXIT_FAILURE);
}
}

void shutdown_logging(void)
{
}

0 comments on commit 1570804

Please sign in to comment.