summaryrefslogtreecommitdiff
path: root/src/channel.c
diff options
context:
space:
mode:
authorPablo Neira Ayuso <pablo@netfilter.org>2009-03-12 21:09:27 +0100
committerPablo Neira Ayuso <pablo@netfilter.org>2009-03-12 21:09:27 +0100
commit656d5ad7c69a5a7d356c6251743890f1eec0bb71 (patch)
tree46c7fe13a32382c556a51d258fab5b7bebe9cd77 /src/channel.c
parent56b484e3acc7205f0ebd71eec6905253eeace132 (diff)
downloadconntrack-tools-656d5ad7c69a5a7d356c6251743890f1eec0bb71.tar.gz
conntrack-tools-656d5ad7c69a5a7d356c6251743890f1eec0bb71.zip
sync-mode: add abstract layer to make daemon independent of multicast
This patch reworks conntrackd to make it independent of the protocol used to propagate state-changes. This patch adds the channel layer abstraction, this layer allows you to add support for different protocols like unicast UDP or TIPC. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/channel.c')
-rw-r--r--src/channel.c180
1 files changed, 180 insertions, 0 deletions
diff --git a/src/channel.c b/src/channel.c
new file mode 100644
index 0000000..733fd03
--- /dev/null
+++ b/src/channel.c
@@ -0,0 +1,180 @@
+/*
+ * (C) 2009 by Pablo Neira Ayuso <pablo@netfilter.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <net/if.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <netinet/in.h>
+
+#include "channel.h"
+#include "network.h"
+
+static struct channel_ops *ops[CHANNEL_MAX];
+extern struct channel_ops channel_mcast;
+
+void channel_init(void)
+{
+ ops[CHANNEL_MCAST] = &channel_mcast;
+}
+
+#define HEADERSIZ 28 /* IP header (20 bytes) + UDP header 8 (bytes) */
+
+struct channel_buffer {
+ char *data;
+ int size;
+ int len;
+};
+
+static struct channel_buffer *
+channel_buffer_open(int mtu)
+{
+ struct channel_buffer *b;
+
+ b = calloc(sizeof(struct channel_buffer), 1);
+ if (b == NULL)
+ return NULL;
+
+ b->size = mtu - HEADERSIZ;
+
+ b->data = malloc(b->size);
+ if (b->data == NULL) {
+ free(b);
+ return NULL;
+ }
+ return b;
+}
+
+static void
+channel_buffer_close(struct channel_buffer *b)
+{
+ free(b->data);
+ free(b);
+}
+
+struct channel *
+channel_open(struct channel_conf *conf)
+{
+ struct channel *c;
+ struct ifreq ifr;
+ int fd;
+
+ if (conf->channel_type >= CHANNEL_MAX)
+ return NULL;
+ if (!conf->channel_ifname[0])
+ return NULL;
+ if (conf->channel_flags >= CHANNEL_F_MAX)
+ return NULL;
+
+ c = calloc(sizeof(struct channel), 1);
+ if (c == NULL)
+ return NULL;
+
+ c->channel_type = conf->channel_type;
+
+ fd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (fd == -1) {
+ free(c);
+ return NULL;
+ }
+ strncpy(ifr.ifr_name, conf->channel_ifname, sizeof(ifr.ifr_name));
+
+ if (ioctl(fd, SIOCGIFMTU, &ifr) == -1) {
+ free(c);
+ return NULL;
+ }
+ close(fd);
+ c->channel_ifmtu = ifr.ifr_mtu;
+
+ c->channel_ifindex = if_nametoindex(conf->channel_ifname);
+ if (c->channel_ifindex == 0) {
+ free(c);
+ return NULL;
+ }
+ c->ops = ops[conf->channel_type];
+
+ if (conf->channel_flags & CHANNEL_F_BUFFERED) {
+ c->buffer = channel_buffer_open(c->channel_ifmtu);
+ if (c->buffer == NULL) {
+ free(c);
+ return NULL;
+ }
+ }
+ c->channel_flags = conf->channel_flags;
+
+ c->data = c->ops->open(&conf->u);
+ if (c->data == NULL) {
+ channel_buffer_close(c->buffer);
+ free(c);
+ return NULL;
+ }
+ return c;
+}
+
+void
+channel_close(struct channel *c)
+{
+ c->ops->close(c->data);
+ if (c->channel_flags & CHANNEL_F_BUFFERED)
+ channel_buffer_close(c->buffer);
+ free(c);
+}
+
+int channel_send(struct channel *c, const struct nethdr *net)
+{
+ int ret = 0, len = ntohs(net->len);
+
+ if (!(c->channel_flags & CHANNEL_F_BUFFERED)) {
+ c->ops->send(c->data, net, len);
+ return 1;
+ }
+retry:
+ if (c->buffer->len + len < c->buffer->size) {
+ memcpy(c->buffer->data + c->buffer->len, net, len);
+ c->buffer->len += len;
+ } else {
+ c->ops->send(c->data, c->buffer->data, c->buffer->len);
+ ret = 1;
+ c->buffer->len = 0;
+ goto retry;
+ }
+ return ret;
+}
+
+int channel_send_flush(struct channel *c)
+{
+ if (!(c->channel_flags & CHANNEL_F_BUFFERED) || c->buffer->len == 0)
+ return 0;
+
+ c->ops->send(c->data, c->buffer->data, c->buffer->len);
+ c->buffer->len = 0;
+ return 1;
+}
+
+int channel_recv(struct channel *c, char *buf, int size)
+{
+ return c->ops->recv(c->data, buf, size);
+}
+
+int channel_get_fd(struct channel *c)
+{
+ return c->ops->get_fd(c->data);
+}
+
+void channel_stats(struct channel *c, int fd)
+{
+ return c->ops->stats(c, fd);
+}
+
+void channel_stats_extended(struct channel *c, int active,
+ struct nlif_handle *h, int fd)
+{
+ return c->ops->stats_extended(c, active, h, fd);
+}