Changeset View
Changeset View
Standalone View
Standalone View
head/contrib/blacklist/bin/blacklistctl.c
/* $NetBSD: blacklistctl.c,v 1.21 2016/11/02 03:15:07 jnemeth Exp $ */ | /* $NetBSD: blacklistctl.c,v 1.23 2018/05/24 19:21:01 christos Exp $ */ | ||||
/*- | /*- | ||||
* Copyright (c) 2015 The NetBSD Foundation, Inc. | * Copyright (c) 2015 The NetBSD Foundation, Inc. | ||||
* All rights reserved. | * All rights reserved. | ||||
* | * | ||||
* This code is derived from software contributed to The NetBSD Foundation | * This code is derived from software contributed to The NetBSD Foundation | ||||
* by Christos Zoulas. | * by Christos Zoulas. | ||||
* | * | ||||
Show All 18 Lines | |||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||||
* POSSIBILITY OF SUCH DAMAGE. | * POSSIBILITY OF SUCH DAMAGE. | ||||
*/ | */ | ||||
#ifdef HAVE_CONFIG_H | #ifdef HAVE_CONFIG_H | ||||
#include "config.h" | #include "config.h" | ||||
#endif | #endif | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__RCSID("$NetBSD: blacklistctl.c,v 1.21 2016/11/02 03:15:07 jnemeth Exp $"); | __RCSID("$NetBSD: blacklistctl.c,v 1.23 2018/05/24 19:21:01 christos Exp $"); | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <time.h> | #include <time.h> | ||||
#ifdef HAVE_LIBUTIL_H | #ifdef HAVE_LIBUTIL_H | ||||
#include <libutil.h> | #include <libutil.h> | ||||
#endif | #endif | ||||
#ifdef HAVE_UTIL_H | #ifdef HAVE_UTIL_H | ||||
#include <util.h> | #include <util.h> | ||||
Show All 17 Lines | usage(int c) | ||||
if (c == 0) | if (c == 0) | ||||
warnx("Missing/unknown command"); | warnx("Missing/unknown command"); | ||||
else | else | ||||
warnx("Unknown option `%c'", (char)c); | warnx("Unknown option `%c'", (char)c); | ||||
fprintf(stderr, "Usage: %s dump [-abdnrw]\n", getprogname()); | fprintf(stderr, "Usage: %s dump [-abdnrw]\n", getprogname()); | ||||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | ||||
} | } | ||||
static const char * | |||||
star(char *buf, size_t len, int val) | |||||
{ | |||||
if (val == -1) | |||||
return "*"; | |||||
snprintf(buf, len, "%d", val); | |||||
return buf; | |||||
} | |||||
int | int | ||||
main(int argc, char *argv[]) | main(int argc, char *argv[]) | ||||
{ | { | ||||
const char *dbname = _PATH_BLSTATE; | const char *dbname = _PATH_BLSTATE; | ||||
DB *db; | DB *db; | ||||
struct conf c; | struct conf c; | ||||
struct dbinfo dbi; | struct dbinfo dbi; | ||||
unsigned int i; | unsigned int i; | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | main(int argc, char *argv[]) | ||||
clock_gettime(CLOCK_REALTIME, &ts); | clock_gettime(CLOCK_REALTIME, &ts); | ||||
wide = wide ? 8 * 4 + 7 : 4 * 3 + 3; | wide = wide ? 8 * 4 + 7 : 4 * 3 + 3; | ||||
if (!noheader) | if (!noheader) | ||||
printf("%*.*s/ma:port\tid\tnfail\t%s\n", wide, wide, | printf("%*.*s/ma:port\tid\tnfail\t%s\n", wide, wide, | ||||
"address", remain ? "remaining time" : "last access"); | "address", remain ? "remaining time" : "last access"); | ||||
for (i = 1; state_iterate(db, &c, &dbi, i) != 0; i = 0) { | for (i = 1; state_iterate(db, &c, &dbi, i) != 0; i = 0) { | ||||
char buf[BUFSIZ]; | char buf[BUFSIZ]; | ||||
char mbuf[64], pbuf[64]; | |||||
if (!all) { | if (!all) { | ||||
if (blocked) { | if (blocked) { | ||||
if (dbi.count < c.c_nfail) | if (c.c_nfail == -1 || dbi.count < c.c_nfail) | ||||
continue; | continue; | ||||
} else { | } else { | ||||
if (dbi.count >= c.c_nfail) | if (dbi.count >= c.c_nfail) | ||||
continue; | continue; | ||||
} | } | ||||
} | } | ||||
sockaddr_snprintf(buf, sizeof(buf), "%a", (void *)&c.c_ss); | sockaddr_snprintf(buf, sizeof(buf), "%a", (void *)&c.c_ss); | ||||
printf("%*.*s/%d:%d\t", wide, wide, buf, c.c_lmask, c.c_port); | printf("%*.*s/%s:%s\t", wide, wide, buf, | ||||
star(mbuf, sizeof(mbuf), c.c_lmask), | |||||
star(pbuf, sizeof(pbuf), c.c_port)); | |||||
if (c.c_duration == -1) { | |||||
strlcpy(buf, "never", sizeof(buf)); | |||||
} else { | |||||
if (remain) | if (remain) | ||||
fmtydhms(buf, sizeof(buf), | fmtydhms(buf, sizeof(buf), | ||||
c.c_duration - (ts.tv_sec - dbi.last)); | c.c_duration - (ts.tv_sec - dbi.last)); | ||||
else | else | ||||
fmttime(buf, sizeof(buf), dbi.last); | fmttime(buf, sizeof(buf), dbi.last); | ||||
printf("%s\t%d/%d\t%-s\n", dbi.id, dbi.count, c.c_nfail, buf); | } | ||||
printf("%s\t%d/%s\t%-s\n", dbi.id, dbi.count, | |||||
star(mbuf, sizeof(mbuf), c.c_nfail), buf); | |||||
} | } | ||||
state_close(db); | state_close(db); | ||||
return EXIT_SUCCESS; | return EXIT_SUCCESS; | ||||
} | } |