diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2010-06-12 15:15:51 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2010-06-12 15:15:51 +0300 |
commit | ac7510292e197ef5e045d8d52004b70fa84d5eca (patch) | |
tree | 054d8619e6c6d8df102f464391ca570c26a0162c | |
parent | f78a97f9742c350fb60a71152226fdaf7d1a6127 (diff) | |
download | smap-ac7510292e197ef5e045d8d52004b70fa84d5eca.tar.gz smap-ac7510292e197ef5e045d8d52004b70fa84d5eca.tar.bz2 |
Improve protocol error diagnostics.
* lib/sockmapstr.c (report_invalid_prefix): New function.
(read_payload_length)
(_sockmap_input_stream_read): Use report_invalid_prefix.
-rw-r--r-- | lib/sockmapstr.c | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/lib/sockmapstr.c b/lib/sockmapstr.c index 906cc1b..16ccbe3 100644 --- a/lib/sockmapstr.c +++ b/lib/sockmapstr.c @@ -24,6 +24,10 @@ #include <string.h> #include <errno.h> #include <unistd.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> + #include "smap/diag.h" #include "smap/stream.h" #include "smap/streamdef.h" @@ -175,6 +179,34 @@ struct sockmap_input_stream { char *debug_pfx; }; +static void +report_invalid_prefix(struct sockmap_input_stream *sp, const char *diag) +{ + static char buf[1024]; + size_t n; + struct sockaddr_in saddr; + socklen_t slen; + + slen = sizeof(saddr); + if (getpeername(sp->fd, (struct sockaddr*) &saddr, &slen) != -1) { + if (saddr.sin_family == AF_INET) + smap_stream_printf(smap_debug_str, + "%s:%d: ", + inet_ntoa(saddr.sin_addr), + ntohs(saddr.sin_port)); + else if (saddr.sin_family == AF_UNIX) + smap_stream_printf(smap_debug_str, + "[local socket]: "); + } + smap_stream_printf(smap_debug_str, + "sockmap protocol error " + "(%s): %s", diag, sp->nbuf); + n = recv(sp->fd, buf, sizeof(buf), 0); + if (n > 0) + smap_stream_write(smap_debug_str, buf, n, NULL); + smap_stream_write(smap_debug_str, "\n", 1, NULL); +} + int read_payload_length(struct sockmap_input_stream *sp) { @@ -204,9 +236,8 @@ read_payload_length(struct sockmap_input_stream *sp) } sp->nbuf[len] = 0; sp->nlen = len; - smap_debug(sp->debug_idx, 1, - ("sockmap protocol error (prefix too long): %s", - sp->nbuf)); + if (smap_debug_np(sp->debug_idx, 1)) + report_invalid_prefix(sp, "prefix too long"); return EPROTO; } @@ -242,9 +273,10 @@ _sockmap_input_stream_read(struct _smap_stream *stream, char *buf, for (i = 0; i < sp->cp; i++) { reqsz *= 10; if (!ISDIGIT(sp->nbuf[i])) { - smap_debug(sp->debug_idx, 1, - ("sockmap protocol error " - "(invalid prefix): %s", sp->nbuf)); + if (smap_debug_np(sp->debug_idx, 1)) + report_invalid_prefix(sp, + "invalid prefix"); + sp->nlen = sp->cp = sp->reqsz = 0; return EPROTO; } reqsz += sp->nbuf[i] - '0'; |