Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement network debug logging #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -230,6 +231,12 @@ parse_conf(const char *config_path)
config.features |= FULLBOUNCE;
else if (strcmp(word, "NULLCLIENT") == 0 && data == NULL)
config.features |= NULLCLIENT;
else if (strcmp(word, "DEBUGLOG") == 0 && data != NULL) {
config.debugfd = open(data, O_APPEND|O_WRONLY);
corecode marked this conversation as resolved.
Show resolved Hide resolved
if (config.debugfd == -1)
errlog(EX_CONFIG, "could not open debug log file");
syslog(LOG_NOTICE, "debug logging to `%s'", data);
}
else {
errlogx(EX_CONFIG, "syntax error in %s:%d", config_path, lineno);
/* NOTREACHED */
Expand Down
1 change: 1 addition & 0 deletions dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ struct config config = {
.mailname = NULL,
.masquerade_host = NULL,
.masquerade_user = NULL,
.debugfd = -1,
};


Expand Down
1 change: 1 addition & 0 deletions dma.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ struct config {

/* XXX does not belong into config */
SSL *ssl;
int debugfd;
};


Expand Down
29 changes: 29 additions & 0 deletions net.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <arpa/inet.h>

Expand Down Expand Up @@ -74,6 +75,29 @@ ssl_errstr(void)
return (ERR_error_string(oerr, NULL));
}

static void
debug_log(const char *dir, const char *data, size_t len)
{
if (config.debugfd == -1)
return;

char tag[20];
snprintf(tag, sizeof(tag), "%d\t %s: ", getpid(), dir);

struct iovec iov[2] = {
{ tag, strlen(tag) },
{ (void *)data, len },
};

ssize_t res = writev(config.debugfd, iov, sizeof(iov)/sizeof(*iov));

if (res < 0) {
syslog(LOG_ERR, "could not write to debug log: %m");
close(config.debugfd);
config.debugfd = -1;
}
}

ssize_t
send_remote_command(int fd, const char* fmt, ...)
{
Expand All @@ -95,6 +119,8 @@ send_remote_command(int fd, const char* fmt, ...)
strcat(cmd, "\r\n");
len = strlen(cmd);

debug_log("C", cmd, len);

if (((config.features & SECURETRANS) != 0) &&
((config.features & NOSSL) == 0)) {
while ((s = SSL_write(config.ssl, (const char*)cmd, len)) <= 0) {
Expand Down Expand Up @@ -160,6 +186,9 @@ read_remote(int fd, int extbufsize, char *extbuf)
goto error;
}
}

debug_log("S", buff + len, rlen);

len += rlen;

copysize = sizeof(neterr) - strlen(neterr) - 1;
Expand Down