From c67acf4ccc381985f5da5881e976b749651f07b1 Mon Sep 17 00:00:00 2001 From: Daniel Collins Date: Thu, 9 Jan 2014 11:38:54 +0000 Subject: [PATCH] liboping: Add support for SO_MARK. Marks packets, which can be used for filtering and routing in Linux. --- src/liboping.c | 33 +++++++++++++++++++++++++++++++++ src/mans/ping_setopt.pod | 6 ++++++ src/oping.h | 1 + 3 files changed, 40 insertions(+) diff --git a/src/liboping.c b/src/liboping.c index 40a0ba2..dd9da46 100644 --- a/src/liboping.c +++ b/src/liboping.c @@ -142,6 +142,9 @@ struct pingobj char *device; + char set_mark; + int mark; + char errmsg[PING_ERRMSG_LEN]; pinghost_t *head; @@ -1322,6 +1325,19 @@ int ping_setopt (pingobj_t *obj, int option, void *value) } /* case PING_OPT_DEVICE */ break; + case PING_OPT_MARK: + { +#ifdef SO_MARK + obj->mark = *(int*)(value); + obj->set_mark = 1; +#else /* SO_MARK */ + ping_set_errno (obj, ENOTSUP); + ret = -1; +#endif /* !SO_MARK */ + + } /* case PING_OPT_MARK */ + break; + default: ret = -2; } /* switch (option) */ @@ -1508,6 +1524,23 @@ int ping_host_add (pingobj_t *obj, const char *host) } } #endif /* SO_BINDTODEVICE */ +#ifdef SO_MARK + if(obj->set_mark) + { + if(setsockopt(ph->fd, SOL_SOCKET, SO_MARK, &(obj->mark), sizeof(obj->mark)) != 0) + { +#if WITH_DEBUG + char errbuf[PING_ERRMSG_LEN]; + dprintf ("setsockopt (SO_MARK): %s\n", + sstrerror (errno, errbuf, sizeof (errbuf))); +#endif + ping_set_errno (obj, errno); + close (ph->fd); + ph->fd = -1; + continue; + } + } +#endif #ifdef SO_TIMESTAMP if (1) /* {{{ */ { diff --git a/src/mans/ping_setopt.pod b/src/mans/ping_setopt.pod index 11c63d8..ba7e44f 100644 --- a/src/mans/ping_setopt.pod +++ b/src/mans/ping_setopt.pod @@ -75,6 +75,12 @@ C (IPv4) or C (IPv6) option. It is the caller's responsibility to chose a valid bit combination. For details, read the L and L manual pages, as well as I2474>. +=item B + +Mark (as in netfilter) outgoing packets using the SO_MARK socket option. Takes +an int* pointer as a value. Setting this requires CAP_NET_ADMIN under Linux. +Fails with C on platforms which don't have SO_MARK. + =back The I argument is a pointer to the new value. It must not be NULL. It is diff --git a/src/oping.h b/src/oping.h index 9f8d5e1..e5bc5fa 100644 --- a/src/oping.h +++ b/src/oping.h @@ -53,6 +53,7 @@ typedef struct pingobj pingobj_t; #define PING_OPT_SOURCE 0x10 #define PING_OPT_DEVICE 0x20 #define PING_OPT_QOS 0x40 +#define PING_OPT_MARK 0x80 #define PING_DEF_TIMEOUT 1.0 #define PING_DEF_TTL 255 -- 2.30.2