summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author/C=DE/ST=Berlin/L=Berlin/O=Netfilter Project/OU=Development/CN=pablo/emailAddress=pablo@netfilter.org </C=DE/ST=Berlin/L=Berlin/O=Netfilter Project/OU=Development/CN=pablo/emailAddress=pablo@netfilter.org>2005-05-17 11:38:37 +0000
committer/C=DE/ST=Berlin/L=Berlin/O=Netfilter Project/OU=Development/CN=pablo/emailAddress=pablo@netfilter.org </C=DE/ST=Berlin/L=Berlin/O=Netfilter Project/OU=Development/CN=pablo/emailAddress=pablo@netfilter.org>2005-05-17 11:38:37 +0000
commit5c14f27ef46e65317ca4da8657f7c1a1a91da4c4 (patch)
treeabc22db769c30b45127c793012b357a6b03df2d2 /src
parenta75bb977ff16c9f3b3bdccdcd4173e9ef033463f (diff)
downloadconntrack-tools-5c14f27ef46e65317ca4da8657f7c1a1a91da4c4.tar.gz
conntrack-tools-5c14f27ef46e65317ca4da8657f7c1a1a91da4c4.zip
o Added descriptive error messages.
o Fix wrong flags check in [tcp|udp] proto helpers.
Diffstat (limited to 'src')
-rw-r--r--src/conntrack.c37
-rw-r--r--src/libct.c105
2 files changed, 90 insertions, 52 deletions
diff --git a/src/conntrack.c b/src/conntrack.c
index 676049e..11a6b54 100644
--- a/src/conntrack.c
+++ b/src/conntrack.c
@@ -38,6 +38,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
+#include <string.h>
#include <linux/netfilter_ipv4/ip_conntrack_tuple.h>
#include <linux/netfilter_ipv4/ip_conntrack.h>
#include "libctnetlink.h"
@@ -46,7 +47,7 @@
#include "libct_proto.h"
#define PROGNAME "conntrack"
-#define VERSION "0.60"
+#define VERSION "0.62"
#if 0
#define DEBUGP printf
@@ -299,6 +300,36 @@ merge_options(struct option *oldopts, const struct option *newopts,
return merge;
}
+/* From linux/errno.h */
+#define ENOTSUPP 524 /* Operation is not supported */
+
+/* Translates errno numbers into more human-readable form than strerror. */
+const char *
+err2str(int err, enum action command)
+{
+ unsigned int i;
+ struct table_struct {
+ enum action act;
+ int err;
+ const char *message;
+ } table [] =
+ { { CT_LIST, -ENOTSUPP, "function not implemented" },
+ { 0xFFFF, -EINVAL, "invalid parameters" },
+ { CT_CREATE|CT_GET|CT_DELETE, -ENOENT,
+ "such conntrack doesn't exist" },
+ { CT_CREATE|CT_GET, -ENOMEM, "not enough memory" },
+ { CT_GET, -EAFNOSUPPORT, "protocol not supported" },
+ { CT_CREATE, -ETIME, "conntrack has expired" },
+ };
+
+ for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
+ if ((table[i].act & command) && table[i].err == err)
+ return table[i].message;
+ }
+
+ return strerror(err);
+}
+
static void dump_tuple(struct ip_conntrack_tuple *tp)
{
fprintf(stdout, "tuple %p: %u %u.%u.%u.%u:%hu -> %u.%u.%u.%u:%hu\n",
@@ -732,6 +763,6 @@ int main(int argc, char *argv[])
global_option_offset = 0;
}
- if (res == -1)
- fprintf(stderr, "Operation failed\n");
+ if (res < 0)
+ fprintf(stderr, "Operation failed: %s\n", err2str(res, command));
}
diff --git a/src/libct.c b/src/libct.c
index cb0fabb..b40b818 100644
--- a/src/libct.c
+++ b/src/libct.c
@@ -216,18 +216,19 @@ int create_conntrack(struct ip_conntrack_tuple *orig,
struct cta_proto cta;
struct nfattr *cda[CTA_MAX];
struct ctnl_handle cth;
+ int ret;
cta.num_proto = orig->dst.protonum;
memcpy(&cta.proto, proto, sizeof(*proto));
- if (ctnl_open(&cth, 0) < 0)
- return -1;
+ if ((ret = ctnl_open(&cth, 0)) < 0)
+ return ret;
- /* FIXME: please unify returns values... */
- if (ctnl_new_conntrack(&cth, orig, reply, timeout, &cta, status) < 0)
- return -1;
+ if ((ret = ctnl_new_conntrack(&cth, orig, reply, timeout, &cta,
+ status)) < 0)
+ return ret;
- if (ctnl_close(&cth) < 0)
- return -1;
+ if ((ret = ctnl_close(&cth)) < 0)
+ return ret;
return 0;
}
@@ -237,16 +238,16 @@ int delete_conntrack(struct ip_conntrack_tuple *tuple,
{
struct nfattr *cda[CTA_MAX];
struct ctnl_handle cth;
+ int ret;
- if (ctnl_open(&cth, 0) < 0)
- return -1;
+ if ((ret = ctnl_open(&cth, 0)) < 0)
+ return ret;
- /* FIXME: please unify returns values... */
- if (ctnl_del_conntrack(&cth, tuple, t) < 0)
- return -1;
+ if ((ret = ctnl_del_conntrack(&cth, tuple, t)) < 0)
+ return ret;
- if (ctnl_close(&cth) < 0)
- return -1;
+ if ((ret = ctnl_close(&cth)) < 0)
+ return ret;
return 0;
}
@@ -262,18 +263,19 @@ int get_conntrack(struct ip_conntrack_tuple *tuple,
.type = 0,
.handler = handler
};
+ int ret;
- if (ctnl_open(&cth, 0) < 0)
- return -1;
+ if ((ret = ctnl_open(&cth, 0)) < 0)
+ return ret;
ctnl_register_handler(&cth, &h);
/* FIXME!!!! get_conntrack_handler returns -100 */
- if (ctnl_get_conntrack(&cth, tuple, t) != -100)
- return -1;
+ if ((ret = ctnl_get_conntrack(&cth, tuple, t)) != -100)
+ return ret;
- if (ctnl_close(&cth) < 0)
- return -1;
+ if ((ret = ctnl_close(&cth)) < 0)
+ return ret;
return 0;
}
@@ -287,8 +289,8 @@ int dump_conntrack_table(int zero)
.handler = handler
};
- if (ctnl_open(&cth, 0) < 0)
- return -1;
+ if ((ret = ctnl_open(&cth, 0)) < 0)
+ return ret;
ctnl_register_handler(&cth, &h);
@@ -298,10 +300,10 @@ int dump_conntrack_table(int zero)
ret = ctnl_list_conntrack(&cth, AF_INET);
if (ret != -100)
- return -1;
+ return ret;
- if (ctnl_close(&cth) < 0)
- return -1;
+ if ((ret = ctnl_close(&cth)) < 0)
+ return ret;
return 0;
}
@@ -317,17 +319,18 @@ int event_conntrack(unsigned int event_mask)
.type = 2, /* destroy */
.handler = event_handler
};
+ int ret;
- if (ctnl_open(&cth, event_mask) < 0)
- return -1;
+ if ((ret = ctnl_open(&cth, event_mask)) < 0)
+ return ret;
ctnl_register_handler(&cth, &hnew);
ctnl_register_handler(&cth, &hdestroy);
- if (ctnl_event_conntrack(&cth, AF_INET) < 0)
- return -1;
+ if ((ret = ctnl_event_conntrack(&cth, AF_INET)) < 0)
+ return ret;
- if (ctnl_close(&cth) < 0)
- return -1;
+ if ((ret = ctnl_close(&cth)) < 0)
+ return ret;
return 0;
}
@@ -383,17 +386,18 @@ int dump_expect_list()
.type = 0, /* Hm... really? */
.handler = expect_handler
};
+ int ret;
- if (ctnl_open(&cth, 0) < 0)
- return -1;
+ if ((ret = ctnl_open(&cth, 0)) < 0)
+ return ret;
ctnl_register_handler(&cth, &h);
- if (ctnl_list_expect(&cth, AF_INET) != -100)
- return -1;
+ if ((ret = ctnl_list_expect(&cth, AF_INET)) != -100)
+ return ret;
- if (ctnl_close(&cth) < 0)
- return -1;
+ if ((ret = ctnl_close(&cth)) < 0)
+ return ret;
return 0;
}
@@ -402,6 +406,7 @@ int set_mask(unsigned int mask, int type)
{
struct ctnl_handle cth;
enum ctattr_type_t cta_type;
+ int ret;
switch(type) {
case 0:
@@ -411,17 +416,18 @@ int set_mask(unsigned int mask, int type)
cta_type = CTA_EVENTMASK;
break;
default:
+ /* Shouldn't happen */
return -1;
}
- if (ctnl_open(&cth, 0) < 0)
- return -1;
+ if ((ret = ctnl_open(&cth, 0)) < 0)
+ return ret;
- if (ctnl_set_mask(&cth, mask, cta_type) < 0)
- return -1;
+ if ((ret = ctnl_set_mask(&cth, mask, cta_type)) < 0)
+ return ret;
- if (ctnl_close(&cth) < 0)
- return -1;
+ if ((ret = ctnl_close(&cth)) < 0)
+ return ret;
return 0;
}
@@ -429,15 +435,16 @@ int set_mask(unsigned int mask, int type)
int flush_conntrack()
{
struct ctnl_handle cth;
+ int ret;
- if (ctnl_open(&cth, 0) < 0)
- return -1;
+ if ((ret = ctnl_open(&cth, 0)) < 0)
+ return ret;
- if (ctnl_flush_conntrack(&cth) < 0)
- return -1;
+ if ((ret = ctnl_flush_conntrack(&cth)) < 0)
+ return ret;
- if (ctnl_close(&cth) < 0)
- return -1;
+ if ((ret = ctnl_close(&cth)) < 0)
+ return ret;
return 0;
}