summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPablo Neira Ayuso <pablo@netfilter.org>2010-07-11 14:01:20 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2010-07-11 14:01:20 +0200
commit899706c44d6391ca0f0ea83609e507b986c88789 (patch)
tree63fa66de6c9d255371db3e9422266a2ac69fe3b0
parent4eb7c493dabf47fc559b7a41e583fa656d616e05 (diff)
nflog: consistent nflog_snprintf_xml() return value with snprintf()
With this patch, nflog_snprintf_xml() returns the number of characters printed. If the output was truncated, then the return value is the number of characters that would have been written if enough space had been available. This makes nflog_snprintf_xml() consistent with the behaviour of snprintf(). Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
-rw-r--r--src/libnetfilter_log.c123
1 files changed, 63 insertions, 60 deletions
diff --git a/src/libnetfilter_log.c b/src/libnetfilter_log.c
index acc1edf..7ace0d6 100644
--- a/src/libnetfilter_log.c
+++ b/src/libnetfilter_log.c
@@ -482,24 +482,27 @@ int nflog_get_seq_global(struct nflog_data *nfad, u_int32_t *seq)
return 0;
}
-#define SNPRINTF_FAILURE(size, len, offset) \
+#define SNPRINTF_FAILURE(ret, rem, offset, len) \
do { \
- if (size < 0 || (unsigned int) size >= len) \
- return size; \
- offset += size; \
- len -= size; \
+ if (ret < 0) \
+ return ret; \
+ len += ret; \
+ if (ret > rem) \
+ ret = rem; \
+ offset += ret; \
+ rem -= ret; \
} while (0)
-int nflog_snprintf_xml(char *buf, size_t len, struct nflog_data *tb, int flags)
+int nflog_snprintf_xml(char *buf, size_t rem, struct nflog_data *tb, int flags)
{
struct nfulnl_msg_packet_hdr *ph;
struct nfulnl_msg_packet_hw *hwph;
u_int32_t mark, ifi;
- int size, offset = 0, ret;
+ int size, offset = 0, len = 0, ret;
char *data;
- size = snprintf(buf + offset, len, "<log>");
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "<log>");
+ SNPRINTF_FAILURE(size, rem, offset, len);
if (flags & NFLOG_XML_TIME) {
time_t t;
@@ -509,130 +512,130 @@ int nflog_snprintf_xml(char *buf, size_t len, struct nflog_data *tb, int flags)
if (localtime_r(&t, &tm) == NULL)
return -1;
- size = snprintf(buf + offset, len, "<when>");
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "<when>");
+ SNPRINTF_FAILURE(size, rem, offset, len);
- size = snprintf(buf + offset, len,
+ size = snprintf(buf + offset, rem,
"<hour>%d</hour>", tm.tm_hour);
- SNPRINTF_FAILURE(size, len, offset);
+ SNPRINTF_FAILURE(size, rem, offset, len);
size = snprintf(buf + offset,
- len, "<min>%02d</min>", tm.tm_min);
- SNPRINTF_FAILURE(size, len, offset);
+ rem, "<min>%02d</min>", tm.tm_min);
+ SNPRINTF_FAILURE(size, rem, offset, len);
size = snprintf(buf + offset,
- len, "<sec>%02d</sec>", tm.tm_sec);
- SNPRINTF_FAILURE(size, len, offset);
+ rem, "<sec>%02d</sec>", tm.tm_sec);
+ SNPRINTF_FAILURE(size, rem, offset, len);
- size = snprintf(buf + offset, len, "<wday>%d</wday>",
+ size = snprintf(buf + offset, rem, "<wday>%d</wday>",
tm.tm_wday + 1);
- SNPRINTF_FAILURE(size, len, offset);
+ SNPRINTF_FAILURE(size, rem, offset, len);
- size = snprintf(buf + offset, len, "<day>%d</day>", tm.tm_mday);
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "<day>%d</day>", tm.tm_mday);
+ SNPRINTF_FAILURE(size, rem, offset, len);
- size = snprintf(buf + offset, len, "<month>%d</month>",
+ size = snprintf(buf + offset, rem, "<month>%d</month>",
tm.tm_mon + 1);
- SNPRINTF_FAILURE(size, len, offset);
+ SNPRINTF_FAILURE(size, rem, offset, len);
- size = snprintf(buf + offset, len, "<year>%d</year>",
+ size = snprintf(buf + offset, rem, "<year>%d</year>",
1900 + tm.tm_year);
- SNPRINTF_FAILURE(size, len, offset);
+ SNPRINTF_FAILURE(size, rem, offset, len);
- size = snprintf(buf + offset, len, "</when>");
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "</when>");
+ SNPRINTF_FAILURE(size, rem, offset, len);
}
data = nflog_get_prefix(tb);
if (data && (flags & NFLOG_XML_PREFIX)) {
- size = snprintf(buf + offset, len, "<prefix>%s</prefix>", data);
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "<prefix>%s</prefix>", data);
+ SNPRINTF_FAILURE(size, rem, offset, len);
}
ph = nflog_get_msg_packet_hdr(tb);
if (ph) {
- size = snprintf(buf + offset, len, "<hook>%u</hook>", ph->hook);
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "<hook>%u</hook>", ph->hook);
+ SNPRINTF_FAILURE(size, rem, offset, len);
hwph = nflog_get_packet_hw(tb);
if (hwph && (flags & NFLOG_XML_HW)) {
int i, hlen = ntohs(hwph->hw_addrlen);
- size = snprintf(buf + offset, len, "<hw><proto>0x%04x"
+ size = snprintf(buf + offset, rem, "<hw><proto>0x%04x"
"</proto>",
ntohs(ph->hw_protocol));
- SNPRINTF_FAILURE(size, len, offset);
+ SNPRINTF_FAILURE(size, rem, offset, len);
- size = snprintf(buf + offset, len, "<src>");
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "<src>");
+ SNPRINTF_FAILURE(size, rem, offset, len);
for (i=0; i<hlen-1; i++) {
- size = snprintf(buf + offset, len, "%02x:",
+ size = snprintf(buf + offset, rem, "%02x:",
ntohs(ph->hw_protocol));
- SNPRINTF_FAILURE(size, len, offset);
+ SNPRINTF_FAILURE(size, rem, offset, len);
}
- size = snprintf(buf + offset, len, "</src></hw>");
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "</src></hw>");
+ SNPRINTF_FAILURE(size, rem, offset, len);
} else if (flags & NFLOG_XML_HW) {
- size = snprintf(buf + offset, len, "<hw><proto>0x%04x"
+ size = snprintf(buf + offset, rem, "<hw><proto>0x%04x"
"</proto></hw>",
ntohs(ph->hw_protocol));
- SNPRINTF_FAILURE(size, len, offset);
+ SNPRINTF_FAILURE(size, rem, offset, len);
}
}
mark = nflog_get_nfmark(tb);
if (mark && (flags & NFLOG_XML_MARK)) {
- size = snprintf(buf + offset, len, "<mark>%u</mark>", mark);
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "<mark>%u</mark>", mark);
+ SNPRINTF_FAILURE(size, rem, offset, len);
}
ifi = nflog_get_indev(tb);
if (ifi && (flags & NFLOG_XML_DEV)) {
- size = snprintf(buf + offset, len, "<indev>%u</indev>", ifi);
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "<indev>%u</indev>", ifi);
+ SNPRINTF_FAILURE(size, rem, offset, len);
}
ifi = nflog_get_outdev(tb);
if (ifi && (flags & NFLOG_XML_DEV)) {
- size = snprintf(buf + offset, len, "<outdev>%u</outdev>", ifi);
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "<outdev>%u</outdev>", ifi);
+ SNPRINTF_FAILURE(size, rem, offset, len);
}
ifi = nflog_get_physindev(tb);
if (ifi && (flags & NFLOG_XML_PHYSDEV)) {
- size = snprintf(buf + offset, len,
+ size = snprintf(buf + offset, rem,
"<physindev>%u</physindev>", ifi);
- SNPRINTF_FAILURE(size, len, offset);
+ SNPRINTF_FAILURE(size, rem, offset, len);
}
ifi = nflog_get_physoutdev(tb);
if (ifi && (flags & NFLOG_XML_PHYSDEV)) {
- size = snprintf(buf + offset, len,
+ size = snprintf(buf + offset, rem,
"<physoutdev>%u</physoutdev>", ifi);
- SNPRINTF_FAILURE(size, len, offset);
+ SNPRINTF_FAILURE(size, rem, offset, len);
}
ret = nflog_get_payload(tb, &data);
if (ret >= 0 && (flags & NFLOG_XML_PAYLOAD)) {
int i;
- size = snprintf(buf + offset, len, "<payload>");
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "<payload>");
+ SNPRINTF_FAILURE(size, rem, offset, len);
for (i=0; i<ret; i++) {
- size = snprintf(buf + offset, len, "%02x",
+ size = snprintf(buf + offset, rem, "%02x",
data[i] & 0xff);
- SNPRINTF_FAILURE(size, len, offset);
+ SNPRINTF_FAILURE(size, rem, offset, len);
}
- size = snprintf(buf + offset, len, "</payload>");
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "</payload>");
+ SNPRINTF_FAILURE(size, rem, offset, len);
}
- size = snprintf(buf + offset, len, "</log>");
- SNPRINTF_FAILURE(size, len, offset);
+ size = snprintf(buf + offset, rem, "</log>");
+ SNPRINTF_FAILURE(size, rem, offset, len);
- return size;
+ return len;
}