diff --git a/CHANGELOG b/CHANGELOG
index 8f2c9e7ab..3db7aaa73 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,10 @@
# Nmap Changelog ($Id$); -*-text-*-
+o [Nsock] Reworked the logging infrastructure to make it more flexible
+ and consistent. Updated nmap, nping and ncat accordingly. Nsock log level
+ can now be adjusted at runtime by pressing d/D in nmap.
+ [Henri Doreau, David Fifield]
+
o [NSE] Fixed scripts using unconnected UDP sockets. The bug was
reported by Dhiru Kholia. [David Fifield]
diff --git a/FPEngine.cc b/FPEngine.cc
index ba9a1ee46..22c50d1f8 100644
--- a/FPEngine.cc
+++ b/FPEngine.cc
@@ -157,12 +157,11 @@ void FPNetworkControl::init(const char *ifname, devtype iftype) {
/* Create a new nsock pool */
if ((this->nsp = nsp_new(NULL)) == NULL)
fatal("Unable to obtain an Nsock pool");
- nsp_setdevice(nsp, o.device);
- /* Set Trace level */
- if (o.packetTrace()) {
- nsp_settrace(this->nsp, NULL, NSOCK_TRACE_LEVEL, o.getStartTime());
- }
+ nsock_set_log_function(this->nsp, nmap_nsock_stderr_logger);
+ nmap_adjust_loglevel(this->nsp, o.packetTrace());
+
+ nsp_setdevice(nsp, o.device);
/* Allow broadcast addresses */
nsp_setbroadcast(this->nsp, 1);
@@ -384,6 +383,7 @@ int FPNetworkControl::setup_sniffer(const char *iface, const char *bpf_filter) {
/* This method makes the controller process pending events (like packet
* transmissions or packet captures). */
void FPNetworkControl::handle_events() {
+ nmap_adjust_loglevel(nsp, o.packetTrace());
nsock_loop(nsp, 50);
}
diff --git a/nbase/nbase.vcxproj b/nbase/nbase.vcxproj
index 129521081..8ae49e023 100644
--- a/nbase/nbase.vcxproj
+++ b/nbase/nbase.vcxproj
@@ -112,6 +112,7 @@
+
diff --git a/nbase/nbase_winconfig.h b/nbase/nbase_winconfig.h
index 1fdd1a520..39dfa26c3 100644
--- a/nbase/nbase_winconfig.h
+++ b/nbase/nbase_winconfig.h
@@ -132,7 +132,7 @@
#define HAVE_GETNAMEINFO 1
#define HAVE_SNPRINTF 1
-#define HAVE_VASPRINTF 1
+// #undef HAVE_VASPRINTF
#define HAVE_VSNPRINTF 1
typedef unsigned __int8 uint8_t;
diff --git a/ncat/ncat_connect.c b/ncat/ncat_connect.c
index 635e503be..5b0e278d9 100644
--- a/ncat/ncat_connect.c
+++ b/ncat/ncat_connect.c
@@ -492,9 +492,14 @@ int ncat_connect(void)
if ((mypool = nsp_new(NULL)) == NULL)
bye("Failed to create nsock_pool.");
- if (o.debug > 1)
- /* A trace level of 1 still gives you an awful lot. */
- nsp_settrace(mypool, stderr, 1, nsock_gettimeofday());
+ if (o.debug >= 6)
+ nsock_set_loglevel(mypool, NSOCK_LOG_DBG_ALL);
+ else if (o.debug >= 3)
+ nsock_set_loglevel(mypool, NSOCK_LOG_DBG);
+ else if (o.debug >= 1)
+ nsock_set_loglevel(mypool, NSOCK_LOG_INFO);
+ else
+ nsock_set_loglevel(mypool, NSOCK_LOG_ERROR);
/* Allow connections to broadcast addresses. */
nsp_setbroadcast(mypool, 1);
diff --git a/nmap_dns.cc b/nmap_dns.cc
index 2cc8dc275..a65119dfe 100644
--- a/nmap_dns.cc
+++ b/nmap_dns.cc
@@ -1164,7 +1164,6 @@ static void nmap_mass_rdns_core(Target **targets, int num_targets) {
int timeout;
char *tpname;
int i;
- bool lasttrace = false;
char spmobuf[1024];
// If necessary, set up the dns server list
@@ -1209,10 +1208,11 @@ static void nmap_mass_rdns_core(Target **targets, int num_targets) {
if ((dnspool = nsp_new(NULL)) == NULL)
fatal("Unable to create nsock pool in %s()", __func__);
- nsp_setdevice(dnspool, o.device);
- if ((lasttrace = o.packetTrace()))
- nsp_settrace(dnspool, NULL, NSOCK_TRACE_LEVEL, o.getStartTime());
+ nsock_set_log_function(dnspool, nmap_nsock_stderr_logger);
+ nmap_adjust_loglevel(dnspool, o.packetTrace());
+
+ nsp_setdevice(dnspool, o.device);
connect_dns_servers();
@@ -1231,12 +1231,8 @@ static void nmap_mass_rdns_core(Target **targets, int num_targets) {
if (total_reqs <= 0) break;
/* Because this can change with runtime interaction */
- if (o.packetTrace() != lasttrace) {
- lasttrace = !lasttrace;
- if (lasttrace)
- nsp_settrace(dnspool, NULL, NSOCK_TRACE_LEVEL, o.getStartTime());
- else nsp_settrace(dnspool, NULL, 0, o.getStartTime());
- }
+ nmap_adjust_loglevel(dnspool, o.packetTrace());
+
nsock_loop(dnspool, timeout);
}
diff --git a/nping/EchoServer.cc b/nping/EchoServer.cc
index 73a0252b5..0608c11c5 100644
--- a/nping/EchoServer.cc
+++ b/nping/EchoServer.cc
@@ -1419,10 +1419,10 @@ int EchoServer::start() {
/* Set nsock trace level */
gettimeofday(&now, NULL);
- if( o.getDebugging() == DBG_5)
- nsp_settrace(nsp, NULL, 1 , &now);
+ if( o.getDebugging() == DBG_5 )
+ nsock_set_loglevel(nsp, NSOCK_LOG_INFO);
else if( o.getDebugging() > DBG_5 )
- nsp_settrace(nsp, NULL, 10 , &now);
+ nsock_set_loglevel(nsp, NSOCK_LOG_DBG_ALL);
/* Create new IOD for pcap */
if ((pcap_nsi = nsi_new(nsp, NULL)) == NULL)
diff --git a/nping/ProbeMode.cc b/nping/ProbeMode.cc
index 689388e31..dd2b55398 100644
--- a/nping/ProbeMode.cc
+++ b/nping/ProbeMode.cc
@@ -129,9 +129,9 @@ int ProbeMode::init_nsock(){
/* Set nsock trace level */
gettimeofday(&now, NULL);
if( o.getDebugging() == DBG_5)
- nsp_settrace(nsp, NULL, 1 , &now);
+ nsock_set_loglevel(nsp, NSOCK_LOG_INFO);
else if( o.getDebugging() > DBG_5 )
- nsp_settrace(nsp, NULL, 10 , &now);
+ nsock_set_loglevel(nsp, NSOCK_LOG_DBG_ALL);
/* Flag it as already inited so we don't do it again */
nsock_init=true;
}
diff --git a/nse_nsock.cc b/nse_nsock.cc
index f5e8c6906..1b36f2b84 100644
--- a/nse_nsock.cc
+++ b/nse_nsock.cc
@@ -80,8 +80,14 @@ static nsock_pool new_pool (lua_State *L)
{
nsock_pool nsp = nsp_new(NULL);
nsock_pool *nspp;
+
+ /* configure logging */
+ nsock_set_log_function(nsp, nmap_nsock_stderr_logger);
+ nmap_adjust_loglevel(nsp, o.scriptTrace());
+
nsp_setdevice(nsp, o.device);
nsp_setbroadcast(nsp, true);
+
nspp = (nsock_pool *) lua_newuserdata(L, sizeof(nsock_pool));
*nspp = nsp;
lua_newtable(L);
@@ -408,6 +414,7 @@ static int l_loop (lua_State *L)
socket_unlock(L); /* clean up old socket locks */
+ nmap_adjust_loglevel(nsp, o.scriptTrace());
if (nsock_loop(nsp, tout) == NSOCK_LOOP_ERROR)
return luaL_error(L, "a fatal error occurred in nsock_loop");
return 0;
@@ -1077,8 +1084,6 @@ LUALIB_API int luaopen_nsock (lua_State *L)
nse_nsock_init_ssl_cert(L);
#endif
- if (o.scriptTrace())
- nsp_settrace(nsp, NULL, NSOCK_TRACE_LEVEL, o.getStartTime());
#if HAVE_OPENSSL
/* Value speed over security in SSL connections. */
nsp_ssl_init_max_speed(nsp);
diff --git a/nsock/include/nsock.h b/nsock/include/nsock.h
index b9c1c5b06..33eb1e5a8 100644
--- a/nsock/include/nsock.h
+++ b/nsock/include/nsock.h
@@ -75,6 +75,8 @@
#include
#include
#include
+#else
+#include /* for struct timeval... */
#endif
#if HAVE_SYS_UN_H
@@ -130,6 +132,35 @@ typedef void *nsock_ssl_ctx;
typedef void *nsock_ssl;
+/* Logging-related data structures */
+typedef enum {
+ NSOCK_LOG_DBG_ALL,
+ NSOCK_LOG_DBG,
+ NSOCK_LOG_INFO,
+ NSOCK_LOG_ERROR
+} nsock_loglevel_t;
+
+struct nsock_log_rec {
+ /* Message emission time */
+ struct timeval time;
+ /* Message log level */
+ nsock_loglevel_t level;
+ /* Source file */
+ const char *file;
+ /* Statement line in nsock source */
+ int line;
+ /* Function that emitted the message */
+ const char *func;
+ /* Actual log message */
+ char *msg;
+};
+
+/* Nsock logging function. This function receives all nsock log records whose
+ * level is greater than or equal to nsp loglevel. The rec structure is
+ * allocated and freed by nsock. */
+typedef void (*nsock_logger_t)(nsock_pool nsp, const struct nsock_log_rec *rec);
+
+
/* ------------------- PROTOTYPES ------------------- */
/* Here is the all important looping function that tells the event
@@ -232,6 +263,14 @@ nsock_pool nsp_new(void *userdata);
* all outstanding iods are deleted. */
void nsp_delete(nsock_pool nsp);
+/* Logging subsystem: set custom logging function.
+ * (See nsock_logger_t type definition). */
+void nsock_set_log_function(nsock_pool nsp, nsock_logger_t logger);
+
+nsock_loglevel_t nsock_get_loglevel(nsock_pool nsp);
+void nsock_set_loglevel(nsock_pool nsp, nsock_loglevel_t loglevel);
+
+
/* nsock_event handles a single event. Its ID is generally returned when the
* event is created, and the event itself is included in callbacks
*
diff --git a/nsock/nsock.vcxproj b/nsock/nsock.vcxproj
index 8637cba3a..a8713db6d 100644
--- a/nsock/nsock.vcxproj
+++ b/nsock/nsock.vcxproj
@@ -195,6 +195,7 @@
+
@@ -209,6 +210,7 @@
+
@@ -222,4 +224,4 @@
-
+
\ No newline at end of file
diff --git a/nsock/src/Makefile.in b/nsock/src/Makefile.in
index 7fdc856ce..2cb6b1e9d 100644
--- a/nsock/src/Makefile.in
+++ b/nsock/src/Makefile.in
@@ -27,11 +27,11 @@ NBASEDIR=@NBASEDIR@
TARGET = libnsock.a
-SRCS = error.c filespace.c gh_list.c nsock_connect.c nsock_core.c nsock_iod.c nsock_read.c nsock_timers.c nsock_write.c nsock_ssl.c nsock_event.c nsock_pool.c netutils.c nsock_pcap.c nsock_engines.c engine_select.c engine_epoll.c engine_kqueue.c engine_poll.c @COMPAT_SRCS@
+SRCS = error.c filespace.c gh_list.c nsock_connect.c nsock_core.c nsock_iod.c nsock_read.c nsock_timers.c nsock_write.c nsock_ssl.c nsock_event.c nsock_pool.c netutils.c nsock_pcap.c nsock_engines.c engine_select.c engine_epoll.c engine_kqueue.c engine_poll.c nsock_log.c @COMPAT_SRCS@
-OBJS = error.o filespace.o gh_list.o nsock_connect.o nsock_core.o nsock_iod.o nsock_read.o nsock_timers.o nsock_write.o nsock_ssl.o nsock_event.o nsock_pool.o netutils.o nsock_pcap.o nsock_engines.o engine_select.o engine_epoll.o engine_kqueue.o engine_poll.o @COMPAT_OBJS@
+OBJS = error.o filespace.o gh_list.o nsock_connect.o nsock_core.o nsock_iod.o nsock_read.o nsock_timers.o nsock_write.o nsock_ssl.o nsock_event.o nsock_pool.o netutils.o nsock_pcap.o nsock_engines.o engine_select.o engine_epoll.o engine_kqueue.o engine_poll.o nsock_log.o @COMPAT_OBJS@
-DEPS = error.h filespace.h gh_list.h nsock_internal.h netutils.h nsock_pcap.h ../include/nsock.h $(NBASEDIR)/libnbase.a
+DEPS = error.h filespace.h gh_list.h nsock_internal.h netutils.h nsock_pcap.h nsock_log.h ../include/nsock.h $(NBASEDIR)/libnbase.a
.c.o:
$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@
diff --git a/nsock/src/engine_epoll.c b/nsock/src/engine_epoll.c
index ff300a728..a2670d714 100644
--- a/nsock/src/engine_epoll.c
+++ b/nsock/src/engine_epoll.c
@@ -65,6 +65,7 @@
#include
#include "nsock_internal.h"
+#include "nsock_log.h"
#if HAVE_PCAP
#include "nsock_pcap.h"
@@ -260,8 +261,7 @@ int epoll_loop(mspool *nsp, int msec_timeout) {
}
do {
- if (nsp->tracelevel > 6)
- nsock_trace(nsp, "wait_for_events");
+ nsock_log_debug_all(nsp, "wait_for_events");
if (nsp->next_ev.tv_sec == 0)
event_msecs = -1; /* None of the events specified a timeout */
@@ -305,7 +305,7 @@ int epoll_loop(mspool *nsp, int msec_timeout) {
} while (results_left == -1 && sock_err == EINTR); /* repeat only if signal occurred */
if (results_left == -1 && sock_err != EINTR) {
- nsock_trace(nsp, "nsock_loop error %d: %s", sock_err, socket_strerror(sock_err));
+ nsock_log_error(nsp, "nsock_loop error %d: %s", sock_err, socket_strerror(sock_err));
nsp->errnum = sock_err;
return -1;
}
diff --git a/nsock/src/engine_kqueue.c b/nsock/src/engine_kqueue.c
index 37bd822e0..bc56dcfcc 100644
--- a/nsock/src/engine_kqueue.c
+++ b/nsock/src/engine_kqueue.c
@@ -67,6 +67,7 @@
#include
#include "nsock_internal.h"
+#include "nsock_log.h"
#if HAVE_PCAP
#include "nsock_pcap.h"
@@ -236,8 +237,7 @@ int kqueue_loop(mspool *nsp, int msec_timeout) {
}
do {
- if (nsp->tracelevel > 6)
- nsock_trace(nsp, "wait_for_events");
+ nsock_log_debug_all(nsp, "wait_for_events");
if (nsp->next_ev.tv_sec == 0)
event_msecs = -1; /* None of the events specified a timeout */
@@ -287,7 +287,7 @@ int kqueue_loop(mspool *nsp, int msec_timeout) {
} while (results_left == -1 && sock_err == EINTR); /* repeat only if signal occurred */
if (results_left == -1 && sock_err != EINTR) {
- nsock_trace(nsp, "nsock_loop error %d: %s", sock_err, socket_strerror(sock_err));
+ nsock_log_error(nsp, "nsock_loop error %d: %s", sock_err, socket_strerror(sock_err));
nsp->errnum = sock_err;
return -1;
}
diff --git a/nsock/src/engine_poll.c b/nsock/src/engine_poll.c
index 8e1499a6e..2676d9aae 100644
--- a/nsock/src/engine_poll.c
+++ b/nsock/src/engine_poll.c
@@ -77,6 +77,7 @@
#endif /* ^WIN32 */
#include "nsock_internal.h"
+#include "nsock_log.h"
#if HAVE_PCAP
#include "nsock_pcap.h"
@@ -310,8 +311,7 @@ int poll_loop(mspool *nsp, int msec_timeout) {
return 0; /* No need to wait on 0 events ... */
do {
- if (nsp->tracelevel > 6)
- nsock_trace(nsp, "wait_for_events");
+ nsock_log_debug_all(nsp, "wait_for_events");
if (nsp->next_ev.tv_sec == 0)
event_msecs = -1; /* None of the events specified a timeout */
@@ -355,7 +355,7 @@ int poll_loop(mspool *nsp, int msec_timeout) {
} while (results_left == -1 && sock_err == EINTR); /* repeat only if signal occurred */
if (results_left == -1 && sock_err != EINTR) {
- nsock_trace(nsp, "nsock_loop error %d: %s", sock_err, socket_strerror(sock_err));
+ nsock_log_error(nsp, "nsock_loop error %d: %s", sock_err, socket_strerror(sock_err));
nsp->errnum = sock_err;
return -1;
}
diff --git a/nsock/src/engine_select.c b/nsock/src/engine_select.c
index 2bc541661..89819951d 100644
--- a/nsock/src/engine_select.c
+++ b/nsock/src/engine_select.c
@@ -62,6 +62,7 @@
#include
#include "nsock_internal.h"
+#include "nsock_log.h"
#if HAVE_PCAP
#include "nsock_pcap.h"
@@ -281,8 +282,7 @@ int select_loop(mspool *nsp, int msec_timeout) {
return 0; /* No need to wait on 0 events ... */
do {
- if (nsp->tracelevel > 6)
- nsock_trace(nsp, "wait_for_events");
+ nsock_log_debug_all(nsp, "wait_for_events");
if (nsp->next_ev.tv_sec == 0)
event_msecs = -1; /* None of the events specified a timeout */
@@ -343,7 +343,7 @@ int select_loop(mspool *nsp, int msec_timeout) {
} while (results_left == -1 && sock_err == EINTR); /* repeat only if signal occurred */
if (results_left == -1 && sock_err != EINTR) {
- nsock_trace(nsp, "nsock_loop error %d: %s", sock_err, socket_strerror(sock_err));
+ nsock_log_error(nsp, "nsock_loop error %d: %s", sock_err, socket_strerror(sock_err));
nsp->errnum = sock_err;
return -1;
}
diff --git a/nsock/src/nsock_connect.c b/nsock/src/nsock_connect.c
index 5403619c8..d2966853f 100644
--- a/nsock/src/nsock_connect.c
+++ b/nsock/src/nsock_connect.c
@@ -57,6 +57,7 @@
#include "nsock.h"
#include "nsock_internal.h"
+#include "nsock_log.h"
#include "netutils.h"
#include
@@ -85,41 +86,33 @@ static int nsock_make_socket(mspool *ms, msiod *iod, int family, int type, int p
setsockopt(iod->sd, SOL_SOCKET, SO_REUSEADDR, (const char *)&one, sizeof(one));
if (bind(iod->sd, (struct sockaddr *)&iod->local, (int) iod->locallen) == -1) {
- if (ms->tracelevel > 0) {
- const char *addrstr = NULL;
+ const char *addrstr = NULL;
#if HAVE_SYS_UN_H
- if (iod->local.ss_family == AF_UNIX)
- addrstr = get_unixsock_path(&iod->local);
- else
+ if (iod->local.ss_family == AF_UNIX)
+ addrstr = get_unixsock_path(&iod->local);
+ else
#endif
- addrstr = inet_ntop_ez(&iod->local, iod->locallen);
+ addrstr = inet_ntop_ez(&iod->local, iod->locallen);
- nsock_trace(ms, "Bind to %s failed (IOD #%li)", addrstr, iod->id);
-#if HAVE_SYS_UN_H
- /* Failure to bind an AF_UNIX socket is an unrecoverable error. */
- if (iod->local.ss_family == AF_UNIX)
- return -1;
-#endif
- }
+ nsock_log_error(ms, "Bind to %s failed (IOD #%li)", addrstr, iod->id);
}
}
if (iod->ipoptslen && family == AF_INET) {
- if (setsockopt(iod->sd, IPPROTO_IP, IP_OPTIONS, (const char *)iod->ipopts, iod->ipoptslen) == -1) {
- if (ms->tracelevel > 0)
- nsock_trace(ms, "Setting of IP options failed (IOD #%li)", iod->id);
- }
+ if (setsockopt(iod->sd, IPPROTO_IP, IP_OPTIONS, (const char *)iod->ipopts, iod->ipoptslen) == -1)
+ nsock_log_error(ms, "Setting of IP options failed (IOD #%li)", iod->id);
}
if (ms->device) {
errno = 0;
if (!socket_bindtodevice(iod->sd, ms->device)) {
- if ((errno != EPERM && ms->tracelevel > 0) || ms->tracelevel > 5)
- nsock_trace(ms, "Setting of SO_BINDTODEVICE failed (IOD #%li)", iod->id);
+ if (errno != EPERM)
+ nsock_log_error(ms, "Setting of SO_BINDTODEVICE failed (IOD #%li)", iod->id);
+ else
+ nsock_log_debug_all(ms, "Setting of SO_BINDTODEVICE failed (IOD #%li)", iod->id);
}
}
if (ms->broadcast) {
if (setsockopt(iod->sd, SOL_SOCKET, SO_BROADCAST, (const char *)&(ms->broadcast), sizeof(int)) == -1) {
- if (ms->tracelevel > 0)
- nsock_trace(ms, "Setting of SO_BROADCAST failed (IOD #%li)", iod->id);
+ nsock_log_error(ms, "Setting of SO_BROADCAST failed (IOD #%li)", iod->id);
}
}
return iod->sd;
@@ -131,8 +124,7 @@ int nsock_setup_udp(nsock_pool nsp, nsock_iod ms_iod, int af) {
assert(nsi->state == NSIOD_STATE_INITIAL || nsi->state == NSIOD_STATE_UNKNOWN);
- if (ms->tracelevel > 0)
- nsock_trace(ms, "UDP unconnected socket (IOD #%li)", nsi->id);
+ nsock_log_info(ms, "UDP unconnected socket (IOD #%li)", nsi->id);
if (nsock_make_socket(ms, nsi, af, SOCK_DGRAM, IPPROTO_UDP) == -1)
return -1;
@@ -209,9 +201,8 @@ nsock_event_id nsock_connect_unixsock_stream(nsock_pool nsp, nsock_iod nsiod, ns
nse = msevent_new(ms, NSE_TYPE_CONNECT, nsi, timeout_msecs, handler, userdata);
assert(nse);
- if (ms->tracelevel > 0)
- nsock_trace(ms, "UNIX domain socket (STREAM) connection requested to %s (IOD #%li) EID %li",
- get_unixsock_path(ss), nsi->id, nse->id);
+ nsock_log_info(ms, "UNIX domain socket (STREAM) connection requested to %s (IOD #%li) EID %li",
+ get_unixsock_path(ss), nsi->id, nse->id);
nsock_connect_internal(ms, nse, SOCK_STREAM, 0, ss, sslen, 0);
nsp_add_event(ms, nse);
@@ -236,9 +227,8 @@ nsock_event_id nsock_connect_unixsock_datagram(nsock_pool nsp, nsock_iod nsiod,
nse = msevent_new(ms, NSE_TYPE_CONNECT, nsi, -1, handler, userdata);
assert(nse);
- if (ms->tracelevel > 0)
- nsock_trace(ms, "UNIX domain socket (DGRAM) connection requested to %s (IOD #%li) EID %li",
- get_unixsock_path(ss), nsi->id, nse->id);
+ nsock_log_info(ms, "UNIX domain socket (DGRAM) connection requested to %s (IOD #%li) EID %li",
+ get_unixsock_path(ss), nsi->id, nse->id);
nsock_connect_internal(ms, nse, SOCK_DGRAM, 0, ss, sslen, 0);
nsp_add_event(ms, nse);
@@ -266,9 +256,8 @@ nsock_event_id nsock_connect_tcp(nsock_pool nsp, nsock_iod ms_iod, nsock_ev_hand
nse = msevent_new(ms, NSE_TYPE_CONNECT, nsi, timeout_msecs, handler, userdata);
assert(nse);
- if (ms->tracelevel > 0)
- nsock_trace(ms, "TCP connection requested to %s:%hu (IOD #%li) EID %li",
- inet_ntop_ez(ss, sslen), port, nsi->id, nse->id);
+ nsock_log_info(ms, "TCP connection requested to %s:%hu (IOD #%li) EID %li",
+ inet_ntop_ez(ss, sslen), port, nsi->id, nse->id);
/* Do the actual connect() */
nsock_connect_internal(ms, nse, SOCK_STREAM, IPPROTO_TCP, ss, sslen, port);
@@ -295,9 +284,8 @@ nsock_event_id nsock_connect_sctp(nsock_pool nsp, nsock_iod ms_iod, nsock_ev_han
nse = msevent_new(ms, NSE_TYPE_CONNECT, nsi, timeout_msecs, handler, userdata);
assert(nse);
- if (ms->tracelevel > 0)
- nsock_trace(ms, "SCTP association requested to %s:%hu (IOD #%li) EID %li",
- inet_ntop_ez(ss, sslen), port, nsi->id, nse->id);
+ nsock_log_info(ms, "SCTP association requested to %s:%hu (IOD #%li) EID %li",
+ inet_ntop_ez(ss, sslen), port, nsi->id, nse->id);
/* Do the actual connect() */
nsock_connect_internal(ms, nse, SOCK_STREAM, IPPROTO_SCTP, ss, sslen, port);
@@ -337,9 +325,9 @@ nsock_event_id nsock_connect_ssl(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handl
/* Set our SSL_SESSION so we can benefit from session-id reuse. */
nsi_set_ssl_session(nsi, (SSL_SESSION *)ssl_session);
- if (ms->tracelevel > 0)
- nsock_trace(ms, "SSL connection requested to %s:%hu/%s (IOD #%li) EID %li",
- inet_ntop_ez(ss, sslen), port, (proto == IPPROTO_TCP ? "tcp" : "sctp"), nsi->id, nse->id);
+ nsock_log_info(ms, "SSL connection requested to %s:%hu/%s (IOD #%li) EID %li",
+ inet_ntop_ez(ss, sslen), port, (proto == IPPROTO_TCP ? "tcp" : "sctp"),
+ nsi->id, nse->id);
/* Do the actual connect() */
nsock_connect_internal(ms, nse, SOCK_STREAM, proto, ss, sslen, port);
@@ -373,8 +361,8 @@ nsock_event_id nsock_reconnect_ssl(nsock_pool nsp, nsock_iod nsiod, nsock_ev_han
/* Set our SSL_SESSION so we can benefit from session-id reuse. */
nsi_set_ssl_session(nsi, (SSL_SESSION *)ssl_session);
- if (ms->tracelevel > 0)
- nsock_trace(ms, "SSL reconnection requested (IOD #%li) EID %li", nsi->id, nse->id);
+ nsock_log_info(ms, "SSL reconnection requested (IOD #%li) EID %li",
+ nsi->id, nse->id);
/* Do the actual connect() */
nse->event_done = 0;
@@ -412,8 +400,8 @@ nsock_event_id nsock_connect_udp(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handl
nse = msevent_new(ms, NSE_TYPE_CONNECT, nsi, -1, handler, userdata);
assert(nse);
- if (ms->tracelevel > 0)
- nsock_trace(ms, "UDP connection requested to %s:%hu (IOD #%li) EID %li", inet_ntop_ez(ss, sslen), port, nsi->id, nse->id);
+ nsock_log_info(ms, "UDP connection requested to %s:%hu (IOD #%li) EID %li",
+ inet_ntop_ez(ss, sslen), port, nsi->id, nse->id);
nsock_connect_internal(ms, nse, SOCK_DGRAM, IPPROTO_UDP, ss, sslen, port);
nsp_add_event(ms, nse);
diff --git a/nsock/src/nsock_core.c b/nsock/src/nsock_core.c
index d21a1e88f..a8bf09af1 100644
--- a/nsock/src/nsock_core.c
+++ b/nsock/src/nsock_core.c
@@ -59,6 +59,7 @@
#include "nsock_internal.h"
#include "gh_list.h"
#include "filespace.h"
+#include "nsock_log.h"
#include
#if HAVE_ERRNO_H
@@ -425,7 +426,7 @@ void handle_connect_result(mspool *ms, msevent *nse, enum nse_status status) {
if (iod->ssl_session) {
rc = SSL_set_session(iod->ssl, iod->ssl_session);
if (rc == 0)
- fprintf(stderr, "Uh-oh: SSL_set_session() failed - please tell Fyodor\n");
+ nsock_log_error(ms, "Uh-oh: SSL_set_session() failed - please tell nmap-dev@insecure.org\n");
iod->ssl_session = NULL; /* No need for this any more */
}
@@ -446,9 +447,8 @@ void handle_connect_result(mspool *ms, msevent *nse, enum nse_status status) {
if (nsi_ssl_post_connect_verify(iod)) {
nse->status = NSE_STATUS_SUCCESS;
} else {
- if (ms->tracelevel > 0)
- nsock_trace(ms, "certificate verification error for EID %li: %s",
- nse->id, ERR_error_string(ERR_get_error(), NULL));
+ nsock_log_error(ms, "certificate verification error for EID %li: %s",
+ nse->id, ERR_error_string(ERR_get_error(), NULL));
nse->status = NSE_STATUS_ERROR;
}
} else {
@@ -471,8 +471,7 @@ void handle_connect_result(mspool *ms, msevent *nse, enum nse_status status) {
* was initialized with nsp_ssl_init_max_speed. Try reconnecting with
* SSL_OP_NO_SSLv2. Never downgrade a NO_SSLv2 connection to one that
* might use SSLv2. */
- if (ms->tracelevel > 0)
- nsock_trace(ms, "EID %li reconnecting with SSL_OP_NO_SSLv2", nse->id);
+ nsock_log_info(ms, "EID %li reconnecting with SSL_OP_NO_SSLv2", nse->id);
saved_ev = iod->watched_events;
ms->engine->iod_unregister(ms, iod);
@@ -490,8 +489,8 @@ void handle_connect_result(mspool *ms, msevent *nse, enum nse_status status) {
update_events(iod, ms, EV_READ|EV_WRITE, EV_NONE);
nse->sslinfo.ssl_desire = SSL_ERROR_WANT_CONNECT;
} else {
- if (ms->tracelevel > 0)
- nsock_trace(ms, "EID %li %s", nse->id, ERR_error_string(ERR_get_error(), NULL));
+ nsock_log_info(ms, "EID %li %s",
+ nse->id, ERR_error_string(ERR_get_error(), NULL));
nse->event_done = 1;
nse->status = NSE_STATUS_ERROR;
nse->errnum = EIO;
@@ -706,9 +705,8 @@ static int do_actual_read(mspool *ms, msevent *nse) {
nse->event_done = 1;
nse->status = NSE_STATUS_ERROR;
nse->errnum = EIO;
- if (ms->tracelevel > 2)
- nsock_trace(ms, "SSL_read() failed for reason %s on NSI %li",
- ERR_reason_error_string(err), iod->id);
+ nsock_log_info(ms, "SSL_read() failed for reason %s on NSI %li",
+ ERR_reason_error_string(err), iod->id);
return -1;
}
}
@@ -884,14 +882,12 @@ enum nsock_loopstatus nsock_loop(nsock_pool nsp, int msec_timeout) {
TIMEVAL_MSEC_ADD(loop_timeout, nsock_tod, msec_timeout);
msecs_left = msec_timeout;
- if (ms->tracelevel > 2) {
- if (msec_timeout >= 0)
- nsock_trace(ms, "nsock_loop() started (timeout=%dms). %d events pending",
- msec_timeout, ms->events_pending);
- else
- nsock_trace(ms, "nsock_loop() started (no timeout). %d events pending",
- ms->events_pending);
- }
+ if (msec_timeout >= 0)
+ nsock_log_debug(ms, "nsock_loop() started (timeout=%dms). %d events pending",
+ msec_timeout, ms->events_pending);
+ else
+ nsock_log_debug(ms, "nsock_loop() started (no timeout). %d events pending",
+ ms->events_pending);
while (1) {
if (ms->quit) {
@@ -935,8 +931,7 @@ void process_event(mspool *nsp, gh_list *evlist, msevent *nse, int ev) {
int desire_r = 0, desire_w = 0;
#endif
- if (nsp->tracelevel > 7)
- nsock_trace(nsp, "Processing event %lu", nse->id);
+ nsock_log_debug_all(nsp, "Processing event %lu", nse->id);
if (!nse->event_done) {
switch(nse->type) {
@@ -989,8 +984,7 @@ void process_event(mspool *nsp, gh_list *evlist, msevent *nse, int ev) {
#if HAVE_PCAP
case NSE_TYPE_PCAP_READ:{
- if (nsp->tracelevel > 5)
- nsock_trace(nsp, "PCAP iterating %lu", nse->id);
+ nsock_log_debug_all(nsp, "PCAP iterating %lu", nse->id);
if (ev & EV_READ) {
/* buffer empty? check it! */
@@ -1017,14 +1011,12 @@ void process_event(mspool *nsp, gh_list *evlist, msevent *nse, int ev) {
update_first_events(nse);
gh_list_remove(&nsp->pcap_read_events, nse);
- if (nsp->tracelevel > 8)
- nsock_trace(nsp, "PCAP NSE #%lu: Removing event from PCAP_READ_EVENTS", nse->id);
+ nsock_log_debug_all(nsp, "PCAP NSE #%lu: Removing event from PCAP_READ_EVENTS", nse->id);
}
if (((mspcap *)nse->iod->pcap)->pcap_desc >= 0 && nse->event_done && evlist == &nsp->pcap_read_events) {
update_first_events(nse);
gh_list_remove(&nsp->read_events, nse);
- if (nsp->tracelevel > 8)
- nsock_trace(nsp, "PCAP NSE #%lu: Removing event from READ_EVENTS", nse->id);
+ nsock_log_debug_all(nsp, "PCAP NSE #%lu: Removing event from READ_EVENTS", nse->id);
}
#endif
break;
@@ -1040,8 +1032,7 @@ void process_event(mspool *nsp, gh_list *evlist, msevent *nse, int ev) {
if (nse->type == NSE_TYPE_CONNECT_SSL && nse->status == NSE_STATUS_SUCCESS)
assert(nse->iod->ssl != NULL);
- if (nsp->tracelevel > 8)
- nsock_trace(nsp, "NSE #%lu: Sending event", nse->id);
+ nsock_log_debug_all(nsp, "NSE #%lu: Sending event", nse->id);
/* WooHoo! The event is ready to be sent */
msevent_dispatch_and_delete(nsp, nse, 1);
@@ -1141,8 +1132,7 @@ const struct timeval *nsock_gettimeofday() {
* adjusting the descriptor select/poll lists, registering the timeout value,
* etc. */
void nsp_add_event(mspool *nsp, msevent *nse) {
- if (nsp->tracelevel > 5)
- nsock_trace(nsp, "NSE #%lu: Adding event", nse->id);
+ nsock_log_debug(nsp, "NSE #%lu: Adding event", nse->id);
/* First lets do the event-type independent stuff, starting with timeouts */
if (nse->event_done) {
@@ -1211,20 +1201,17 @@ void nsp_add_event(mspool *nsp, msevent *nse) {
socket_count_readpcap_inc(nse->iod);
update_events(nse->iod, nsp, EV_READ, EV_NONE);
}
- if (nsp->tracelevel > 8)
- nsock_trace(nsp, "PCAP NSE #%lu: Adding event to READ_EVENTS", nse->id);
+ nsock_log_debug_all(nsp, "PCAP NSE #%lu: Adding event to READ_EVENTS", nse->id);
#if PCAP_BSD_SELECT_HACK
/* when using BSD hack we must do pcap_next() after select().
* Let's insert this pcap to bot queues, to selectable and nonselectable.
* This will result in doing pcap_next_ex() just before select() */
- if (nsp->tracelevel > 8)
- nsock_trace(nsp, "PCAP NSE #%lu: Adding event to PCAP_READ_EVENTS", nse->id);
+ nsock_log_debug_all(nsp, "PCAP NSE #%lu: Adding event to PCAP_READ_EVENTS", nse->id);
#endif
} else {
/* pcap isn't selectable. Add it to pcap-specific queue. */
- if (nsp->tracelevel > 8)
- nsock_trace(nsp, "PCAP NSE #%lu: Adding event to PCAP_READ_EVENTS", nse->id);
+ nsock_log_debug_all(nsp, "PCAP NSE #%lu: Adding event to PCAP_READ_EVENTS", nse->id);
}
iod_add_event(nse->iod, nse);
break;
@@ -1237,20 +1224,6 @@ void nsp_add_event(mspool *nsp, msevent *nse) {
}
}
-void nsock_trace(mspool *ms, char *fmt, ...) {
- va_list ap;
- int elapsed_time_ms;
-
- assert(ms->tracefile != NULL);
- elapsed_time_ms = TIMEVAL_MSEC_SUBTRACT(nsock_tod, ms->tracebasetime);
- va_start(ap, fmt);
- fflush(ms->tracefile);
- fprintf(ms->tracefile, "NSOCK (%.4fs) ", elapsed_time_ms / 1000.0);
- vfprintf(ms->tracefile, fmt, ap);
- fprintf(ms->tracefile, "\n");
- va_end(ap);
-}
-
/* An event has been completed and the handler is about to be called. This
* function writes out tracing data about the event if necessary */
void nsock_trace_handler_callback(mspool *ms, msevent *nse) {
@@ -1260,7 +1233,7 @@ void nsock_trace_handler_callback(mspool *ms, msevent *nse) {
char displaystr[256];
char errstr[256];
- if (ms->tracelevel == 0)
+ if (ms->loglevel > NSOCK_LOG_INFO)
return;
nsi = nse->iod;
@@ -1274,25 +1247,25 @@ void nsock_trace_handler_callback(mspool *ms, msevent *nse) {
switch(nse->type) {
case NSE_TYPE_CONNECT:
case NSE_TYPE_CONNECT_SSL:
- nsock_trace(ms, "Callback: %s %s %sfor EID %li [%s]",
- nse_type2str(nse->type), nse_status2str(nse->status),
- errstr, nse->id, get_peeraddr_string(nsi));
+ nsock_log_info(ms, "Callback: %s %s %sfor EID %li [%s]",
+ nse_type2str(nse->type), nse_status2str(nse->status),
+ errstr, nse->id, get_peeraddr_string(nsi));
break;
case NSE_TYPE_READ:
if (nse->status != NSE_STATUS_SUCCESS) {
if (nsi->peerlen > 0) {
- nsock_trace(ms, "Callback: %s %s %sfor EID %li [%s]",
- nse_type2str(nse->type), nse_status2str(nse->status),
- errstr, nse->id, get_peeraddr_string(nsi));
+ nsock_log_info(ms, "Callback: %s %s %sfor EID %li [%s]",
+ nse_type2str(nse->type), nse_status2str(nse->status),
+ errstr, nse->id, get_peeraddr_string(nsi));
} else {
- nsock_trace(ms, "Callback: %s %s %sfor EID %li (peer unspecified)",
- nse_type2str(nse->type), nse_status2str(nse->status),
- errstr, nse->id);
+ nsock_log_info(ms, "Callback: %s %s %sfor EID %li (peer unspecified)",
+ nse_type2str(nse->type), nse_status2str(nse->status),
+ errstr, nse->id);
}
} else {
str = nse_readbuf(nse, &strlength);
- if (ms->tracelevel > 1 && strlength < 80) {
+ if (strlength < 80) {
memcpy(displaystr, ": ", 2);
memcpy(displaystr + 2, str, strlength);
displaystr[2 + strlength] = '\0';
@@ -1302,35 +1275,35 @@ void nsock_trace_handler_callback(mspool *ms, msevent *nse) {
}
if (nsi->peerlen > 0) {
- nsock_trace(ms, "Callback: %s %s for EID %li [%s] %s(%d bytes)%s",
- nse_type2str(nse->type), nse_status2str(nse->status),
- nse->id, get_peeraddr_string(nsi),
- nse_eof(nse)? "[EOF]" : "", strlength, displaystr);
+ nsock_log_info(ms, "Callback: %s %s for EID %li [%s] %s(%d bytes)%s",
+ nse_type2str(nse->type), nse_status2str(nse->status),
+ nse->id, get_peeraddr_string(nsi),
+ nse_eof(nse)? "[EOF]" : "", strlength, displaystr);
} else {
- nsock_trace(ms, "Callback %s %s for EID %li (peer unspecified) %s(%d bytes)%s",
- nse_type2str(nse->type), nse_status2str(nse->status),
- nse->id, nse_eof(nse)? "[EOF]" : "", strlength, displaystr);
+ nsock_log_info(ms, "Callback %s %s for EID %li (peer unspecified) %s(%d bytes)%s",
+ nse_type2str(nse->type), nse_status2str(nse->status),
+ nse->id, nse_eof(nse)? "[EOF]" : "", strlength, displaystr);
}
}
break;
case NSE_TYPE_WRITE:
- nsock_trace(ms, "Callback: %s %s %sfor EID %li [%s]",
- nse_type2str(nse->type), nse_status2str(nse->status), errstr,
- nse->id, get_peeraddr_string(nsi));
+ nsock_log_info(ms, "Callback: %s %s %sfor EID %li [%s]",
+ nse_type2str(nse->type), nse_status2str(nse->status), errstr,
+ nse->id, get_peeraddr_string(nsi));
break;
case NSE_TYPE_TIMER:
- nsock_trace(ms, "Callback: %s %s %sfor EID %li",
- nse_type2str(nse->type), nse_status2str(nse->status), errstr,
- nse->id);
+ nsock_log_info(ms, "Callback: %s %s %sfor EID %li",
+ nse_type2str(nse->type), nse_status2str(nse->status), errstr,
+ nse->id);
break;
#if HAVE_PCAP
case NSE_TYPE_PCAP_READ:
- nsock_trace(ms, "Callback: %s %s %sfor EID %li ",
- nse_type2str(nse->type), nse_status2str(nse->status),
- errstr, nse->id);
+ nsock_log_info(ms, "Callback: %s %s %sfor EID %li ",
+ nse_type2str(nse->type), nse_status2str(nse->status),
+ errstr, nse->id);
break;
#endif
diff --git a/nsock/src/nsock_event.c b/nsock/src/nsock_event.c
index f24848789..e6d44fa33 100644
--- a/nsock/src/nsock_event.c
+++ b/nsock/src/nsock_event.c
@@ -60,6 +60,7 @@
/* $Id$ */
#include "nsock_internal.h"
+#include "nsock_log.h"
#include "gh_list.h"
#if HAVE_PCAP
@@ -187,9 +188,7 @@ int nsock_event_cancel(nsock_pool ms_pool, nsock_event_id id, int notify ) {
assert(nsp);
type = get_event_id_type(id);
- if (nsp->tracelevel > 0) {
- nsock_trace(nsp, "Event #%li (type %s) cancelled", id, nse_type2str(type));
- }
+ nsock_log_info(nsp, "Event #%li (type %s) cancelled", id, nse_type2str(type));
/* First we figure out what list it is in */
switch(type) {
@@ -258,8 +257,7 @@ int msevent_cancel(mspool *nsp, msevent *nse, gh_list *event_list, gh_list_elem
return 0;
}
- if (nsp->tracelevel > 0)
- nsock_trace(nsp, "msevent_cancel on event #%li (type %s)", nse->id, nse_type2str(nse->type));
+ nsock_log_info(nsp, "msevent_cancel on event #%li (type %s)", nse->id, nse_type2str(nse->type));
/* Now that we found the event... we go through the motions of cleanly
* cancelling it */
@@ -295,16 +293,14 @@ int msevent_cancel(mspool *nsp, msevent *nse, gh_list *event_list, gh_list_elem
update_first_events(nse);
gh_list_remove_elem(event_list, elem);
- if (nsp->tracelevel > 8)
- nsock_trace(nsp, "NSE #%lu: Removing event from list", nse->id);
+ nsock_log_debug_all(nsp, "NSE #%lu: Removing event from list", nse->id);
#if HAVE_PCAP
#if PCAP_BSD_SELECT_HACK
if (nse->type == NSE_TYPE_PCAP_READ) {
- if (nsp->tracelevel > 8)
- nsock_trace(nsp, "PCAP NSE #%lu: CANCEL TEST pcap=%p read=%p curr=%p sd=%i",
- nse->id, &nsp->pcap_read_events, &nsp->read_events,
- event_list,((mspcap *)nse->iod->pcap)->pcap_desc);
+ nsock_log_debug_all(nsp, "PCAP NSE #%lu: CANCEL TEST pcap=%p read=%p curr=%p sd=%i",
+ nse->id, &nsp->pcap_read_events, &nsp->read_events,
+ event_list,((mspcap *)nse->iod->pcap)->pcap_desc);
/* If event occurred, and we're in BSD_HACK mode, then this event was added to
* two queues. read_event and pcap_read_event Of course we should
@@ -314,8 +310,7 @@ int msevent_cancel(mspool *nsp, msevent *nse, gh_list *event_list, gh_list_elem
/* event is done, list is read_events and we're in BSD_HACK mode. So unlink
* event from pcap_read_events */
gh_list_remove(&nsp->pcap_read_events, nse);
- if (nsp->tracelevel > 8)
- nsock_trace(nsp, "PCAP NSE #%lu: Removing event from PCAP_READ_EVENTS", nse->id);
+ nsock_log_debug_all(nsp, "PCAP NSE #%lu: Removing event from PCAP_READ_EVENTS", nse->id);
}
if (((mspcap *)nse->iod->pcap)->pcap_desc >= 0 && event_list == &nsp->pcap_read_events) {
@@ -323,8 +318,7 @@ int msevent_cancel(mspool *nsp, msevent *nse, gh_list *event_list, gh_list_elem
* So unlink event from read_events */
gh_list_remove(&nsp->read_events, nse);
- if (nsp->tracelevel > 8)
- nsock_trace(nsp, "PCAP NSE #%lu: Removing event from READ_EVENTS", nse->id);
+ nsock_log_debug_all(nsp, "PCAP NSE #%lu: Removing event from READ_EVENTS", nse->id);
}
}
#endif
@@ -446,12 +440,10 @@ msevent *msevent_new(mspool *nsp, enum nse_type type, msiod *msiod, int timeout_
nse->userdata = userdata;
nse->time_created = nsock_tod;
- if (nsp->tracelevel > 3) {
if (nse->iod == NULL)
- nsock_trace(nsp, "msevent_new (IOD #NULL) (EID #%li)", nse->id);
+ nsock_log_debug(nsp, "msevent_new (IOD #NULL) (EID #%li)", nse->id);
else
- nsock_trace(nsp, "msevent_new (IOD #%li) (EID #%li)", nse->iod->id, nse->id);
- }
+ nsock_log_debug(nsp, "msevent_new (IOD #%li) (EID #%li)", nse->iod->id, nse->id);
return nse;
}
@@ -461,12 +453,10 @@ msevent *msevent_new(mspool *nsp, enum nse_type type, msiod *msiod, int timeout_
* remember to do this if you call msevent_delete() directly */
void msevent_delete(mspool *nsp, msevent *nse) {
- if (nsp->tracelevel > 3) {
- if (nse->iod == NULL)
- nsock_trace(nsp, "msevent_delete (IOD #NULL) (EID #%li)", nse->id);
- else
- nsock_trace(nsp, "msevent_delete (IOD #%li) (EID #%li)", nse->iod->id, nse->id);
- }
+ if (nse->iod == NULL)
+ nsock_log_debug(nsp, "msevent_delete (IOD #NULL) (EID #%li)", nse->id);
+ else
+ nsock_log_debug(nsp, "msevent_delete (IOD #%li) (EID #%li)", nse->iod->id, nse->id);
/* First free the IOBuf inside it if neccessary */
if (nse->type == NSE_TYPE_READ || nse->type == NSE_TYPE_WRITE) {
@@ -475,8 +465,7 @@ void msevent_delete(mspool *nsp, msevent *nse) {
#if HAVE_PCAP
if (nse->type == NSE_TYPE_PCAP_READ) {
fs_free(&nse->iobuf);
- if (nsp->tracelevel > 5)
- nsock_trace(nsp, "PCAP removed %lu",nse->id);
+ nsock_log_debug_all(nsp, "PCAP removed %lu", nse->id);
}
#endif
diff --git a/nsock/src/nsock_internal.h b/nsock/src/nsock_internal.h
index 1800d814f..0cecc342f 100644
--- a/nsock/src/nsock_internal.h
+++ b/nsock/src/nsock_internal.h
@@ -199,12 +199,9 @@ typedef struct {
* error (errnum fashion) */
int errnum;
- /* Trace/debug level - set by nsp_settrace. If positive, trace logs are
- * printted to tracefile. */
- int tracelevel;
- FILE *tracefile;
- /* This time is subtracted from the current time for trace reports */
- struct timeval tracebasetime;
+ /* Logging information. */
+ nsock_logger_t logger;
+ nsock_loglevel_t loglevel;
/* If true, new sockets will have SO_BROADCAST set */
int broadcast;
diff --git a/nsock/src/nsock_iod.c b/nsock/src/nsock_iod.c
index 5df3ce121..590aac784 100644
--- a/nsock/src/nsock_iod.c
+++ b/nsock/src/nsock_iod.c
@@ -60,6 +60,7 @@
#include "nsock.h"
#include "nsock_internal.h"
+#include "nsock_log.h"
#include "gh_list.h"
#include "netutils.h"
@@ -143,8 +144,7 @@ nsock_iod nsi_new2(nsock_pool nsockp, int sd, void *userdata) {
/* The nsp keeps track of active msiods so it can delete them if it is deleted */
nsi->entry_in_nsp_active_iods = gh_list_append(&nsp->active_iods, nsi);
- if (nsp->tracelevel > 1)
- nsock_trace(nsp, "nsi_new (IOD #%lu)", nsi->id);
+ nsock_log_info(nsp, "nsi_new (IOD #%lu)", nsi->id);
return (nsock_iod)nsi;
}
@@ -175,8 +175,7 @@ void nsi_delete(nsock_iod nsockiod, int pending_response) {
return;
}
- if (nsi->nsp->tracelevel > 1)
- nsock_trace(nsi->nsp, "nsi_delete (IOD #%lu)", nsi->id);
+ nsock_log_info(nsi->nsp, "nsi_delete (IOD #%lu)", nsi->id);
if (nsi->events_pending > 0) {
/* shit -- they killed the msiod while an event was still pending on it.
@@ -232,10 +231,8 @@ void nsi_delete(nsock_iod nsockiod, int pending_response) {
#endif
if (SSL_shutdown(nsi->ssl) == -1) {
-
- if (nsi->nsp->tracelevel > 1)
- nsock_trace(nsi->nsp, "nsi_delete: SSL shutdown failed (%s) on NSI %li",
- ERR_reason_error_string(SSL_get_error(nsi->ssl, -1)), nsi->id);
+ nsock_log_info(nsi->nsp, "nsi_delete: SSL shutdown failed (%s) on NSI %li",
+ ERR_reason_error_string(SSL_get_error(nsi->ssl, -1)), nsi->id);
}
/* I don't really care if the SSL_shutdown() succeeded politely. I could
diff --git a/nsock/src/nsock_log.c b/nsock/src/nsock_log.c
new file mode 100644
index 000000000..e7a044452
--- /dev/null
+++ b/nsock/src/nsock_log.c
@@ -0,0 +1,120 @@
+/***************************************************************************
+ * nsock_log.c -- nsock logging infrastructure. *
+ * *
+ ***********************IMPORTANT NSOCK LICENSE TERMS***********************
+ * *
+ * The nsock parallel socket event library is (C) 1999-2012 Insecure.Com *
+ * LLC This library is free software; you may redistribute and/or *
+ * modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; Version 2. This guarantees *
+ * your right to use, modify, and redistribute this software under certain *
+ * conditions. If this license is unacceptable to you, Insecure.Com LLC *
+ * may be willing to sell alternative licenses (contact *
+ * sales@insecure.com ). *
+ * *
+ * As a special exception to the GPL terms, Insecure.Com LLC grants *
+ * permission to link the code of this program with any version of the *
+ * OpenSSL library which is distributed under a license identical to that *
+ * listed in the included docs/licenses/OpenSSL.txt file, and distribute *
+ * linked combinations including the two. You must obey the GNU GPL in all *
+ * respects for all of the code used other than OpenSSL. If you modify *
+ * this file, you may extend this exception to your version of the file, *
+ * but you are not obligated to do so. *
+ * *
+ * If you received these files with a written license agreement stating *
+ * terms other than the (GPL) terms above, then that alternative license *
+ * agreement takes precedence over this comment. *
+ * *
+ * Source is provided to this software because we believe users have a *
+ * right to know exactly what a program is going to do before they run it. *
+ * This also allows you to audit the software for security holes (none *
+ * have been found so far). *
+ * *
+ * Source code also allows you to port Nmap to new platforms, fix bugs, *
+ * and add new features. You are highly encouraged to send your changes *
+ * to the dev@nmap.org mailing list for possible incorporation into the *
+ * main distribution. By sending these changes to Fyodor or one of the *
+ * Insecure.Org development mailing lists, or checking them into the Nmap *
+ * source code repository, it is understood (unless you specify otherwise) *
+ * that you are offering the Nmap Project (Insecure.Com LLC) the *
+ * unlimited, non-exclusive right to reuse, modify, and relicense the *
+ * code. Nmap will always be available Open Source, but this is important *
+ * because the inability to relicense code has caused devastating problems *
+ * for other Free Software projects (such as KDE and NASM). We also *
+ * occasionally relicense the code to third parties as discussed above. *
+ * If you wish to specify special license conditions of your *
+ * contributions, just say so when you send them. *
+ * *
+ * This program is distributed in the hope that it will be useful, but *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * General Public License v2.0 for more details *
+ * (http://www.gnu.org/licenses/gpl-2.0.html). *
+ * *
+ ***************************************************************************/
+
+/* $Id$ */
+
+
+#define _GNU_SOURCE
+#include
+
+#include
+#include
+#include
+#include
+
+#include "nsock_internal.h"
+#include "nsock_log.h"
+
+extern struct timeval nsock_tod;
+
+
+void nsock_set_log_function(nsock_pool nsp, nsock_logger_t logger) {
+ mspool *ms = (mspool *)nsp;
+
+ ms->logger = logger;
+ nsock_log_debug(ms, "Registered external logging function: %p", logger);
+}
+
+nsock_loglevel_t nsock_get_loglevel(nsock_pool nsp) {
+ mspool *ms = (mspool *)nsp;
+
+ return ms->loglevel;
+}
+
+void nsock_set_loglevel(nsock_pool nsp, nsock_loglevel_t loglevel) {
+ mspool *ms = (mspool *)nsp;
+
+ ms->loglevel = loglevel;
+}
+
+void nsock_stderr_logger(nsock_pool nsp, const struct nsock_log_rec *rec) {
+ fprintf(stderr, "libnsock %s(): %s\n", rec->func, rec->msg);
+}
+
+void __nsock_log_internal(nsock_pool nsp, nsock_loglevel_t loglevel,
+ const char *file, int line, const char *func,
+ const char *format, ...) {
+ struct nsock_log_rec rec;
+ va_list args;
+ int rc;
+
+ va_start(args, format);
+
+ rec.level = loglevel;
+ rec.time = nsock_tod;
+ rec.file = file;
+ rec.line = line;
+ rec.func = func;
+
+ rc = vasprintf(&rec.msg, format, args);
+ if (rc >= 0) {
+ mspool *ms = (mspool *)nsp;
+
+ ms->logger(nsp, &rec);
+ free(rec.msg);
+ }
+ va_end(args);
+}
+
diff --git a/nsock/src/nsock_log.h b/nsock/src/nsock_log.h
new file mode 100644
index 000000000..ddc7239fe
--- /dev/null
+++ b/nsock/src/nsock_log.h
@@ -0,0 +1,103 @@
+/***************************************************************************
+ * nsock_log.c -- nsock logging infrastructure. *
+ * *
+ ***********************IMPORTANT NSOCK LICENSE TERMS***********************
+ * *
+ * The nsock parallel socket event library is (C) 1999-2012 Insecure.Com *
+ * LLC This library is free software; you may redistribute and/or *
+ * modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; Version 2. This guarantees *
+ * your right to use, modify, and redistribute this software under certain *
+ * conditions. If this license is unacceptable to you, Insecure.Com LLC *
+ * may be willing to sell alternative licenses (contact *
+ * sales@insecure.com ). *
+ * *
+ * As a special exception to the GPL terms, Insecure.Com LLC grants *
+ * permission to link the code of this program with any version of the *
+ * OpenSSL library which is distributed under a license identical to that *
+ * listed in the included docs/licenses/OpenSSL.txt file, and distribute *
+ * linked combinations including the two. You must obey the GNU GPL in all *
+ * respects for all of the code used other than OpenSSL. If you modify *
+ * this file, you may extend this exception to your version of the file, *
+ * but you are not obligated to do so. *
+ * *
+ * If you received these files with a written license agreement stating *
+ * terms other than the (GPL) terms above, then that alternative license *
+ * agreement takes precedence over this comment. *
+ * *
+ * Source is provided to this software because we believe users have a *
+ * right to know exactly what a program is going to do before they run it. *
+ * This also allows you to audit the software for security holes (none *
+ * have been found so far). *
+ * *
+ * Source code also allows you to port Nmap to new platforms, fix bugs, *
+ * and add new features. You are highly encouraged to send your changes *
+ * to the dev@nmap.org mailing list for possible incorporation into the *
+ * main distribution. By sending these changes to Fyodor or one of the *
+ * Insecure.Org development mailing lists, or checking them into the Nmap *
+ * source code repository, it is understood (unless you specify otherwise) *
+ * that you are offering the Nmap Project (Insecure.Com LLC) the *
+ * unlimited, non-exclusive right to reuse, modify, and relicense the *
+ * code. Nmap will always be available Open Source, but this is important *
+ * because the inability to relicense code has caused devastating problems *
+ * for other Free Software projects (such as KDE and NASM). We also *
+ * occasionally relicense the code to third parties as discussed above. *
+ * If you wish to specify special license conditions of your *
+ * contributions, just say so when you send them. *
+ * *
+ * This program is distributed in the hope that it will be useful, but *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * General Public License v2.0 for more details *
+ * (http://www.gnu.org/licenses/gpl-2.0.html). *
+ * *
+ ***************************************************************************/
+
+/* $Id$ */
+
+
+#ifndef NSOCK_LOG_H
+#define NSOCK_LOG_H
+
+#include "nsock.h"
+
+
+#define NSOCK_LOG_WRAP(nsp, lvl, ...) \
+ do { \
+ if ((nsp)->logger && (lvl) >= (nsp)->loglevel) { \
+ __nsock_log_internal((nsp), (lvl), __FILE__, __LINE__, __func__, __VA_ARGS__); \
+ } \
+ } while (0)
+
+
+/* -- Internal logging macros -- */
+/**
+ * Most detailed debug messages, like allocating or moving objects.
+ */
+#define nsock_log_debug_all(nsp, ...) NSOCK_LOG_WRAP(nsp, NSOCK_LOG_DBG_ALL, __VA_ARGS__)
+
+/**
+ * Detailed debug messages, describing internal operations.
+ */
+#define nsock_log_debug(nsp, ...) NSOCK_LOG_WRAP(nsp, NSOCK_LOG_DBG, __VA_ARGS__)
+
+/**
+ * High level debug messages, describing top level operations and external
+ * requests.
+ */
+#define nsock_log_info(nsp, ...) NSOCK_LOG_WRAP(nsp, NSOCK_LOG_INFO, __VA_ARGS__)
+
+/**
+ * Error messages.
+ */
+#define nsock_log_error(nsp, ...) NSOCK_LOG_WRAP(nsp, NSOCK_LOG_ERROR, __VA_ARGS__)
+
+
+void __nsock_log_internal(nsock_pool nsp, nsock_loglevel_t loglevel,
+ const char *file, int line, const char *func,
+ const char *format, ...);
+
+void nsock_stderr_logger(nsock_pool nsp, const struct nsock_log_rec *rec);
+
+#endif /* NSOCK_LOG_H */
+
diff --git a/nsock/src/nsock_pcap.c b/nsock/src/nsock_pcap.c
index 8cc2496c4..bc97b03b1 100644
--- a/nsock/src/nsock_pcap.c
+++ b/nsock/src/nsock_pcap.c
@@ -58,6 +58,7 @@
#include "nsock.h"
#include "nsock_internal.h"
+#include "nsock_log.h"
#include
#if HAVE_SYS_IOCTL_H
@@ -133,10 +134,9 @@ char* nsock_pcap_open(nsock_pool nsp, nsock_iod nsiod, const char *pcap_device,
}
va_end(ap);
- if (ms->tracelevel > 0)
- nsock_trace(ms,
- "PCAP requested on device '%s' with berkeley filter '%s' (promisc=%i snaplen=%i to_ms=%i) (IOD #%li)",
- pcap_device,bpf, promisc, snaplen, to_ms, nsi->id);
+ nsock_log_info(ms,
+ "PCAP requested on device '%s' with berkeley filter '%s' (promisc=%i snaplen=%i to_ms=%i) (IOD #%li)",
+ pcap_device,bpf, promisc, snaplen, to_ms, nsi->id);
failed = 0;
do {
@@ -221,22 +221,28 @@ char* nsock_pcap_open(nsock_pool nsp, nsock_iod nsiod, const char *pcap_device,
pcap_device, err0r);
}
- if (ms->tracelevel > 0)
- nsock_trace(ms, "PCAP created successfully on device '%s' (pcap_desc=%i bsd_hack=%i to_valid=%i l3_offset=%i) (IOD #%li)",
- pcap_device,
- mp->pcap_desc,
-#if PCAP_BSD_SELECT_HACK
- 1,
-#else
- 0,
-#endif
-#if PCAP_RECV_TIMEVAL_VALID
- 1,
-#else
- 0,
-#endif
- mp->l3_offset,
- nsi->id);
+ if (ms->loglevel <= NSOCK_LOG_INFO) {
+ #if PCAP_BSD_SELECT_HACK
+ int bsd_select_hack = 1;
+ #else
+ int bsd_select_hack = 0;
+ #endif
+
+ #if PCAP_RECV_TIMEVAL_VALID
+ int recv_timeval_valid = 1;
+ #else
+ int recv_timeval_valid = 0;
+ #endif
+
+ nsock_log_info(ms, "PCAP created successfully on device '%s'"
+ " (pcap_desc=%i bsd_hack=%i to_valid=%i l3_offset=%i) (IOD #%li)",
+ pcap_device,
+ mp->pcap_desc,
+ bsd_select_hack,
+ recv_timeval_valid,
+ mp->l3_offset,
+ nsi->id);
+ }
return NULL;
}
@@ -343,9 +349,7 @@ nsock_event_id nsock_pcap_read_packet(nsock_pool nsp, nsock_iod nsiod,
nse = msevent_new(ms, NSE_TYPE_PCAP_READ, nsi, timeout_msecs, handler, userdata);
assert(nse);
- if (ms->tracelevel > 0)
- nsock_trace(ms, "Pcap read request from IOD #%li EID %li",
- nsi->id, nse->id);
+ nsock_log_info(ms, "Pcap read request from IOD #%li EID %li", nsi->id, nse->id);
nsp_add_event(ms, nse);
@@ -363,9 +367,8 @@ int do_actual_pcap_read(msevent *nse) {
memset(&npp, 0, sizeof(nsock_pcap));
- if (nse->iod->nsp->tracelevel > 2)
- nsock_trace(nse->iod->nsp, "PCAP do_actual_pcap_read TEST (IOD #%li) (EID #%li)",
- nse->iod->id, nse->id);
+ nsock_log_debug_all(nse->iod->nsp, "PCAP do_actual_pcap_read TEST (IOD #%li) (EID #%li)",
+ nse->iod->id, nse->id);
assert( FILESPACE_LENGTH(&(nse->iobuf)) == 0 );
@@ -387,9 +390,8 @@ int do_actual_pcap_read(msevent *nse) {
n = (nsock_pcap *)FILESPACE_STR(&(nse->iobuf));
n->packet = (unsigned char *)FILESPACE_STR(&(nse->iobuf)) + sizeof(npp);
- if (nse->iod->nsp->tracelevel > 2)
- nsock_trace(nse->iod->nsp, "PCAP do_actual_pcap_read READ (IOD #%li) (EID #%li) size=%i",
- nse->iod->id, nse->id, pkt_header->caplen);
+ nsock_log_debug_all(nse->iod->nsp, "PCAP do_actual_pcap_read READ (IOD #%li) (EID #%li) size=%i",
+ nse->iod->id, nse->id, pkt_header->caplen);
return(1);
case 0: /* timeout */
diff --git a/nsock/src/nsock_pool.c b/nsock/src/nsock_pool.c
index 1380599e9..e878ab30d 100644
--- a/nsock/src/nsock_pool.c
+++ b/nsock/src/nsock_pool.c
@@ -59,6 +59,7 @@
/* $Id$ */
#include "nsock_internal.h"
+#include "nsock_log.h"
#include "gh_list.h"
#include "netutils.h"
@@ -117,29 +118,6 @@ void *nsp_getud(nsock_pool nsp) {
return mt->userdata;
}
-/* Sets a trace/debug level and stream. A level of 0 (the default) turns
- * tracing off, while higher numbers are more verbose. If the stream given is
- * NULL, it defaults to stdout. This is generally only used for debugging
- * purposes. A level of 1 or 2 is usually sufficient, but 10 will ensure you get
- * everything. The basetime can be NULL to print trace lines with the current
- * time, otherwise the difference between the current time and basetime will be
- * used (the time program execution starts would be a good candidate) */
-void nsp_settrace(nsock_pool nsp, FILE *file, int level, const struct timeval *basetime) {
- mspool *mt = (mspool *)nsp;
-
- if (file == NULL)
- mt->tracefile = stdout;
- else
- mt->tracefile = file;
-
- mt->tracelevel = level;
-
- if (!basetime)
- memset(&mt->tracebasetime, 0, sizeof(struct timeval));
- else
- mt->tracebasetime = *basetime;
-}
-
/* Turns on or off broadcast support on new sockets. Default is off (0, false)
* set in nsp_new(). Any non-zero (true) value sets SO_BROADCAST on all new
* sockets (value of optval will be used directly in the setsockopt() call */
@@ -171,7 +149,9 @@ nsock_pool nsp_new(void *userdata) {
memset(nsp, 0, sizeof(*nsp));
gettimeofday(&nsock_tod, NULL);
- nsp_settrace(nsp, NULL, 0, NULL);
+
+ nsp->loglevel = NSOCK_LOG_ERROR;
+ nsp->logger = (nsock_logger_t)nsock_stderr_logger;
nsp->id = nsp_next_id++;
diff --git a/nsock/src/nsock_read.c b/nsock/src/nsock_read.c
index 354f0e644..2bcc257c0 100644
--- a/nsock/src/nsock_read.c
+++ b/nsock/src/nsock_read.c
@@ -57,6 +57,7 @@
/* $Id$ */
#include "nsock_internal.h"
+#include "nsock_log.h"
#include "netutils.h"
@@ -74,15 +75,12 @@ nsock_event_id nsock_readlines(nsock_pool nsp, nsock_iod ms_iod, nsock_ev_handle
nse = msevent_new(ms, NSE_TYPE_READ, nsi, timeout_msecs, handler, userdata);
assert(nse);
- if (ms->tracelevel > 0) {
- if (nsi->peerlen > 0)
- nsock_trace(ms, "Read request for %d lines from IOD #%li [%s] EID %li",
- nlines, nsi->id, get_peeraddr_string(nsi),
- nse->id);
- else
- nsock_trace(ms, "Read request for %d lines from IOD #%li (peer unspecified) EID %li",
- nlines, nsi->id, nse->id);
- }
+ if (nsi->peerlen > 0)
+ nsock_log_info(ms, "Read request for %d lines from IOD #%li [%s] EID %li",
+ nlines, nsi->id, get_peeraddr_string(nsi), nse->id);
+ else
+ nsock_log_info(ms, "Read request for %d lines from IOD #%li (peer unspecified) EID %li",
+ nlines, nsi->id, nse->id);
nse->readinfo.read_type = NSOCK_READLINES;
nse->readinfo.num = nlines;
@@ -103,15 +101,12 @@ nsock_event_id nsock_readbytes(nsock_pool nsp, nsock_iod ms_iod, nsock_ev_handle
nse = msevent_new(ms, NSE_TYPE_READ, nsi, timeout_msecs, handler, userdata);
assert(nse);
- if (ms->tracelevel > 0) {
- if (nsi->peerlen > 0)
- nsock_trace(ms, "Read request for %d bytes from IOD #%li [%s] EID %li",
- nbytes, nsi->id, get_peeraddr_string(nsi),
- nse->id);
- else
- nsock_trace(ms, "Read request for %d bytes from IOD #%li (peer unspecified) EID %li",
- nbytes, nsi->id, nse->id);
- }
+ if (nsi->peerlen > 0)
+ nsock_log_info(ms, "Read request for %d bytes from IOD #%li [%s] EID %li",
+ nbytes, nsi->id, get_peeraddr_string(nsi), nse->id);
+ else
+ nsock_log_info(ms, "Read request for %d bytes from IOD #%li (peer unspecified) EID %li",
+ nbytes, nsi->id, nse->id);
nse->readinfo.read_type = NSOCK_READBYTES;
nse->readinfo.num = nbytes;
@@ -132,15 +127,12 @@ nsock_event_id nsock_read(nsock_pool nsp, nsock_iod ms_iod, nsock_ev_handler han
nse = msevent_new(ms, NSE_TYPE_READ, nsi, timeout_msecs, handler, userdata);
assert(nse);
- if (ms->tracelevel > 0) {
- if (nsi->peerlen > 0)
- nsock_trace(ms, "Read request from IOD #%li [%s] (timeout: %dms) EID %li",
- nsi->id, get_peeraddr_string(nsi),
- timeout_msecs, nse->id);
- else
- nsock_trace(ms, "Read request from IOD #%li (peer unspecified) (timeout: %dms) EID %li",
- nsi->id, timeout_msecs, nse->id);
- }
+ if (nsi->peerlen > 0)
+ nsock_log_info(ms, "Read request from IOD #%li [%s] (timeout: %dms) EID %li",
+ nsi->id, get_peeraddr_string(nsi), timeout_msecs, nse->id);
+ else
+ nsock_log_info(ms, "Read request from IOD #%li (peer unspecified) (timeout: %dms) EID %li",
+ nsi->id, timeout_msecs, nse->id);
nse->readinfo.read_type = NSOCK_READ;
diff --git a/nsock/src/nsock_timers.c b/nsock/src/nsock_timers.c
index f1e9ff344..014f22766 100644
--- a/nsock/src/nsock_timers.c
+++ b/nsock/src/nsock_timers.c
@@ -57,6 +57,7 @@
/* $Id$ */
#include "nsock_internal.h"
+#include "nsock_log.h"
extern struct timeval nsock_tod;
@@ -70,8 +71,8 @@ nsock_event_id nsock_timer_create(nsock_pool ms_pool, nsock_ev_handler handler,
nse = msevent_new(nsp, NSE_TYPE_TIMER, NULL, timeout_msecs, handler, userdata);
assert(nse);
- if (nsp->tracelevel > 0)
- nsock_trace(nsp, "Timer created - %dms from now. EID %li", timeout_msecs, nse->id);
+ nsock_log_info(nsp, "Timer created - %dms from now. EID %li", timeout_msecs,
+ nse->id);
nsp_add_event(nsp, nse);
diff --git a/nsock/src/nsock_write.c b/nsock/src/nsock_write.c
index 50f13e7b8..32dd1a121 100644
--- a/nsock/src/nsock_write.c
+++ b/nsock/src/nsock_write.c
@@ -58,6 +58,7 @@
#include "nsock.h"
#include "nsock_internal.h"
+#include "nsock_log.h"
#include "netutils.h"
#include
@@ -104,20 +105,16 @@ nsock_event_id nsock_sendto(nsock_pool ms_pool, nsock_iod ms_iod, nsock_ev_handl
if (datalen < 0)
datalen = (int) strlen(data);
- if (nsp->tracelevel > 0) {
- if (nsp->tracelevel > 1 && datalen < 80) {
- memcpy(displaystr, ": ", 2);
- memcpy(displaystr + 2, data, datalen);
- displaystr[2 + datalen] = '\0';
- replacenonprintable(displaystr + 2, datalen, '.');
- } else {
- displaystr[0] = '\0';
- }
- nsock_trace(nsp, "Sendto request for %d bytes to IOD #%li EID %li [%s]%s",
- datalen, nsi->id, nse->id,
- get_peeraddr_string(nse->iod),
- displaystr);
+ if (nsp->loglevel == NSOCK_LOG_DBG_ALL && datalen < 80) {
+ memcpy(displaystr, ": ", 2);
+ memcpy(displaystr + 2, data, datalen);
+ displaystr[2 + datalen] = '\0';
+ replacenonprintable(displaystr + 2, datalen, '.');
+ } else {
+ displaystr[0] = '\0';
}
+ nsock_log_debug(nsp, "Sendto request for %d bytes to IOD #%li EID %li [%s]%s",
+ datalen, nsi->id, nse->id, get_peeraddr_string(nse->iod), displaystr);
fscat(&nse->iobuf, data, datalen);
@@ -143,23 +140,22 @@ nsock_event_id nsock_write(nsock_pool ms_pool, nsock_iod ms_iod,
nse->writeinfo.dest.ss_family = AF_UNSPEC;
if (datalen < 0)
- datalen = (int) strlen(data);
+ datalen = (int)strlen(data);
- if (nsp->tracelevel > 0) {
- if (nsp->tracelevel > 1 && datalen < 80) {
+ if (nsp->loglevel == NSOCK_LOG_DBG_ALL && datalen < 80) {
memcpy(displaystr, ": ", 2);
memcpy(displaystr + 2, data, datalen);
displaystr[2 + datalen] = '\0';
replacenonprintable(displaystr + 2, datalen, '.');
- } else displaystr[0] = '\0';
+ } else {
+ displaystr[0] = '\0';
+ }
if (nsi->peerlen > 0)
- nsock_trace(nsp, "Write request for %d bytes to IOD #%li EID %li [%s]%s", datalen, nsi->id,
- nse->id, get_peeraddr_string(nsi),
- displaystr);
+ nsock_log_debug(nsp, "Write request for %d bytes to IOD #%li EID %li [%s]%s",
+ datalen, nsi->id, nse->id, get_peeraddr_string(nsi), displaystr);
else
- nsock_trace(nsp, "Write request for %d bytes to IOD #%li EID %li (peer unspecified)%s", datalen,
- nsi->id, nse->id, displaystr);
- }
+ nsock_log_debug(nsp, "Write request for %d bytes to IOD #%li EID %li (peer unspecified)%s",
+ datalen, nsi->id, nse->id, displaystr);
fscat(&nse->iobuf, data, datalen);
@@ -217,23 +213,20 @@ nsock_event_id nsock_printf(nsock_pool ms_pool, nsock_iod ms_iod,
}
}
- if (nsp->tracelevel > 0) {
- if (nsp->tracelevel > 1 && nse->status != NSE_STATUS_ERROR && strlength < 80) {
- memcpy(displaystr, ": ", 2);
- memcpy(displaystr + 2, buf2, strlength);
- displaystr[2 + strlength] = '\0';
- replacenonprintable(displaystr + 2, strlength, '.');
- } else {
- displaystr[0] = '\0';
- }
- if (nsi->peerlen > 0)
- nsock_trace(nsp, "Write request for %d bytes to IOD #%li EID %li [%s]%s", strlength, nsi->id,
- nse->id, get_peeraddr_string(nsi),
- displaystr);
- else
- nsock_trace(nsp, "Write request for %d bytes to IOD #%li EID %li (peer unspecified)%s", strlength,
- nsi->id, nse->id, displaystr);
+ if (nsp->loglevel == NSOCK_LOG_DBG_ALL && nse->status != NSE_STATUS_ERROR && strlength < 80) {
+ memcpy(displaystr, ": ", 2);
+ memcpy(displaystr + 2, buf2, strlength);
+ displaystr[2 + strlength] = '\0';
+ replacenonprintable(displaystr + 2, strlength, '.');
+ } else {
+ displaystr[0] = '\0';
}
+ if (nsi->peerlen > 0)
+ nsock_log_debug(nsp, "Write request for %d bytes to IOD #%li EID %li [%s]%s",
+ strlength, nsi->id, nse->id, get_peeraddr_string(nsi), displaystr);
+ else
+ nsock_log_debug(nsp, "Write request for %d bytes to IOD #%li EID %li (peer unspecified)%s",
+ strlength, nsi->id, nse->id, displaystr);
if (buf2 != buf)
free(buf2);
diff --git a/output.cc b/output.cc
index c3119c1f5..ff17e255d 100644
--- a/output.cc
+++ b/output.cc
@@ -2617,3 +2617,43 @@ void printdatafilepaths() {
}
}
}
+
+static inline const char *nslog2str(nsock_loglevel_t loglevel) {
+ switch(loglevel) {
+ case NSOCK_LOG_DBG_ALL:
+ return "DEBUG FULL";
+ case NSOCK_LOG_DBG:
+ return "DEBUG";
+ case NSOCK_LOG_INFO:
+ return "INFO";
+ case NSOCK_LOG_ERROR:
+ return "ERROR";
+ default:
+ return "???";
+ };
+}
+
+void nmap_adjust_loglevel(nsock_pool nsp, bool trace) {
+ nsock_loglevel_t nsock_loglevel;
+
+ if (o.debugging >= 7)
+ nsock_loglevel = NSOCK_LOG_DBG_ALL;
+ else if (o.debugging >= 4)
+ nsock_loglevel = NSOCK_LOG_DBG;
+ else if (trace || o.debugging >= 2)
+ nsock_loglevel = NSOCK_LOG_INFO;
+ else
+ nsock_loglevel = NSOCK_LOG_ERROR;
+
+ nsock_set_loglevel(nsp, nsock_loglevel);
+}
+
+void nmap_nsock_stderr_logger(nsock_pool nsp, const struct nsock_log_rec *rec) {
+ int elapsed_time;
+
+ elapsed_time = TIMEVAL_MSEC_SUBTRACT(rec->time, *(o.getStartTime()));
+
+ log_write(LOG_STDERR, "NSOCK %s [%.4fs] %s(): %s\n", nslog2str(rec->level),
+ elapsed_time/1000.0, rec->func, rec->msg);
+}
+
diff --git a/output.h b/output.h
index 53c7edbb8..2f8278290 100644
--- a/output.h
+++ b/output.h
@@ -134,6 +134,7 @@
#include "portlist.h"
#include "nmap.h"
#include "global_structures.h"
+#include
#include
@@ -252,4 +253,8 @@ void printfinaloutput();
were found. */
void printdatafilepaths();
+/* nsock logging interface */
+void nmap_adjust_loglevel(nsock_pool nsp, bool trace);
+void nmap_nsock_stderr_logger(nsock_pool nsp, const struct nsock_log_rec *rec);
+
#endif /* OUTPUT_H */
diff --git a/service_scan.cc b/service_scan.cc
index 2a9c1dc45..38e06652b 100644
--- a/service_scan.cc
+++ b/service_scan.cc
@@ -2077,9 +2077,10 @@ static int scanThroughTunnel(nsock_pool nsp, nsock_iod nsi, ServiceGroup *SG,
}
/* Prints completion estimates and the like when appropriate */
-static void considerPrintingStats(ServiceGroup *SG) {
+static void considerPrintingStats(nsock_pool nsp, ServiceGroup *SG) {
/* Check for status requests */
if (keyWasPressed()) {
+ nmap_adjust_loglevel(nsp, o.versionTrace());
SG->SPM->printStats(SG->services_finished.size() /
((double)SG->services_remaining.size() + SG->services_in_progress.size() +
SG->services_finished.size()), nsock_gettimeofday());
@@ -2148,7 +2149,7 @@ static void end_svcprobe(nsock_pool nsp, enum serviceprobestate probe_state, Ser
SG->services_finished.push_back(svc);
- considerPrintingStats(SG);
+ considerPrintingStats(nsp, SG);
if (nsi) {
nsi_delete(nsi, NSOCK_PENDING_SILENT);
@@ -2677,11 +2678,10 @@ int service_scan(std::vector &Targets) {
if ((nsp = nsp_new(SG)) == NULL) {
fatal("%s() failed to create new nsock pool.", __func__);
}
- nsp_setdevice(nsp, o.device);
+ nsock_set_log_function(nsp, nmap_nsock_stderr_logger);
+ nmap_adjust_loglevel(nsp, o.versionTrace());
- if (o.versionTrace()) {
- nsp_settrace(nsp, NULL, NSOCK_TRACE_LEVEL, o.getStartTime());
- }
+ nsp_setdevice(nsp, o.device);
#if HAVE_OPENSSL
/* We don't care about connection security in version detection. */