summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Franke <cfchris6@ts2server.com>2010-09-08 22:41:41 (GMT)
committerChristian Franke <cfchris6@ts2server.com>2010-09-08 22:41:41 (GMT)
commitbcccc1c64097dc9d2b92d1ec1902dc8a8c3f8ca4 (patch)
treec9100abf7b715efab34b8c6f3b4608cd57e43064
parentd50a06a4022e4f328da21209aa82296ab8b8f661 (diff)
do real logging
-rw-r--r--src/replarp.c51
1 files changed, 42 insertions, 9 deletions
diff --git a/src/replarp.c b/src/replarp.c
index 2692266..9e72d9b 100644
--- a/src/replarp.c
+++ b/src/replarp.c
@@ -24,6 +24,7 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <syslog.h>
#include <unistd.h>
#include <ctype.h>
@@ -101,8 +102,13 @@ static void debug_print_hwaddr(const char*, const uint8_t*, uint8_t);
#endif
static void main_loop();
int main(int,char**);
+static void log_f(int, const char*, ...);
#if !DEBUG
+static enum {
+ CLOSED, OPEN
+} syslog_state = CLOSED;
+
static void daemonize();
static void daemonize()
@@ -173,6 +179,29 @@ static void daemonize()
iofd++;
}
}
+
+void log_f(int priority, const char* format, ...)
+{
+ va_list arg;
+
+ if (syslog_state != OPEN) {
+ openlog("replarp", LOG_NDELAY | LOG_NOWAIT | LOG_PID, LOG_USER);
+ syslog_state = OPEN;
+ }
+
+ va_start(arg, format);
+ vsyslog(priority, format, arg);
+ va_end(arg);
+}
+#else /* this means we _are_ debugging... */
+void log_f(int __attribute__((unused)) priority, const char* format, ...)
+{
+ va_list arg;
+
+ va_start(arg, format);
+ vfprintf(stderr, format, arg);
+ va_end(arg);
+}
#endif
static void open_socket()
@@ -181,7 +210,8 @@ static void open_socket()
arp_socket = socket(PF_PACKET, SOCK_RAW, ETHERTYPE_ARP);
if (arp_socket < 0) {
- fprintf(perr, "Error creating raw socket: %s\n", strerror(errno));
+ fprintf(perr,
+ "Error creating raw socket: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
@@ -252,25 +282,28 @@ static void main_loop()
ssize_t red;
short found;
+ if (perr && perr != stderr) {
+ fclose(perr);
+ perr = NULL;
+ }
+
+ log_f(LOG_INFO, "Startup complete, listening for arp requests.\n");
while (1) {
memset(&lla, 0, sizeof(lla));
lla_size = sizeof(lla);
- if (perr && perr != stderr) {
- fclose(perr);
- perr = NULL;
- }
- red = recvfrom(arp_socket, recvbuffer.raw, sizeof(recvbuffer), 0,
+ red = recvfrom(arp_socket, recvbuffer.raw, sizeof(recvbuffer),0,
(struct sockaddr*)(&lla), &lla_size);
if (red <= 0) {
if (red < 0)
- fprintf(perr, "Error reading from socket: %s\n",
+ log_f(LOG_WARNING,
+ "Error reading from socket: %s\n",
strerror(errno));
break;
}
if (red < sizeof(recvbuffer.hdr)) {
- fprintf(perr, "Received a shorted packet.\n");
+ log_f(LOG_WARNING, "Received a shorted packet.\n");
continue;
}
@@ -316,7 +349,7 @@ static void main_loop()
if (send(arp_socket, sendbuffer.raw, sizeof(sendbuffer.hdr), 0)
!= sizeof(sendbuffer.hdr)) {
- fprintf(perr, "Error sending the answer: %s\n",
+ log_f(LOG_WARNING, "Error sending the answer: %s\n",
strerror(errno));
}
}