Commit 99b7781b authored by Robert Ricci's avatar Robert Ricci
Browse files

Add netmond, a program that allows conections (via a UNIX domain socket)

from apps being monitored with libnetmon. This is so that we can
invoke netmond from the monitor, and attach whatever process we want
to it.

If LIBNETMON_SOCKPATH is set in the environment, libnetmon will now
spit results to it rather than stdout.
parent 51082970
......@@ -9,7 +9,7 @@ else
LD_FLAGS=
endif
all: libnetmon.so
all: libnetmon.so netmond
%.so: %.c %.h
$(CC) $(CFLAGS) $(LD_FLAGS) -shared -o $@ $<
......
......@@ -27,6 +27,7 @@ void croak(char *format, ...) {
void lnm_init() {
static bool intialized = false;
char *sockpath;
if (intialized == false) {
DEBUG(printf("Initializing\n"));
......@@ -54,6 +55,38 @@ void lnm_init() {
FIND_REAL_FUN(write);
FIND_REAL_FUN(send);
sockpath = getenv("LIBNETMON_SOCKPATH");
if (sockpath) {
int sockfd;
struct sockaddr_un servaddr;
DEBUG(printf("Opening socket at path %s\n",sockpath));
sockfd = real_socket(AF_LOCAL, SOCK_STREAM, 0);
if (!sockfd) {
croak("Unable to create socket\n");
}
servaddr.sun_family = AF_LOCAL;
strcpy(servaddr.sun_path,sockpath);
if (real_connect(sockfd,
(struct sockaddr*) &servaddr,
sizeof(servaddr))) {
croak("Unable to connect to netmond socket\n");
}
outstream = fdopen(sockfd,"w");
if (!outstream) {
croak("fdopen() failed on socket\n");
}
DEBUG(printf("Done opening socket\n"));
} else {
outstream = stdout;
}
intialized = true;
} else {
/* DEBUG(printf("Skipping intialization\n")); */
......@@ -174,8 +207,9 @@ void log_packet(int fd, size_t len) {
/*
fprintf(stderr,"%lu.%08lu [%i, %i]\n",time.tv_sec, time.tv_usec, fd,len);
*/
fprintf(stdout,"%lu.%06lu > %s.%i (%i)\n",time.tv_sec, time.tv_usec,
fprintf(outstream,"%lu.%06lu > %s.%i (%i)\n",time.tv_sec, time.tv_usec,
monitorFDs[fd].remote_hostname, monitorFDs[fd].remote_port, len);
fflush(outstream);
}
/*
......
......@@ -26,8 +26,9 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
/* #define DEBUGGING */
#define DEBUGGING
#ifdef DEBUGGING
#define DEBUG(x) (x)
......@@ -85,6 +86,11 @@ typedef struct {
static fdRecord* monitorFDs;
static unsigned int fdSize;
/*
* Stream on which to write reports
*/
FILE *outstream;
/*
* Manipulate the monitorFDs structure
*/
......
/*
* EMULAB-COPYRIGHT
* Copyright (c) 2006 University of Utah and the Flux Group.
*
* netmond, a 'server' for libnetmon - simply repeat what a process being
* monitored with libnetmon tell us on a unix-domian socket
*
* TODO: Handle more than one client at a time
*/
#define SOCKPATH "/var/tmp/netmon.sock"
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
int main() {
int sockfd;
struct sockaddr_un servaddr;
/*
* Make ourselves a socket
*/
sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
/*
* Bind to a local socket
*/
unlink(SOCKPATH);
servaddr.sun_family = AF_LOCAL;
strcpy(servaddr.sun_path, SOCKPATH);
if (bind(sockfd, (struct sockaddr*) &servaddr, sizeof(servaddr))) {
perror("Failed to bind() socket\n");
return 1;
}
if (listen(sockfd, 10)) {
perror("Failed to listen() on socket\n");
return 1;
}
while (1) {
fprintf(stderr,"Waiting for clients\n");
/*
* Do a blocking wait for a client to connect() to us
*/
struct sockaddr_un clientaddr;
socklen_t clientlen;
int clientfd;
clientlen = sizeof(clientaddr);
clientfd = accept(sockfd, (struct sockaddr*)&clientaddr, &clientlen);
if (clientfd) {
char *buf[1024];
size_t bufsize = 1024;
size_t read_bytes;
fprintf(stderr,"Got a client\n");
/*
* As long as the client is connected, just take whatever it
* tells us and spit it to stdout
*/
while ((read_bytes = read(clientfd,buf,bufsize))) {
write(1,buf,read_bytes);
}
close(clientfd);
}
}
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment