view src/botblockd.h @ 0:9f8d79c98fd2 0.4.0

initial public release
author Arkadiusz Juszczyk <ark@wobblyhead.net>
date 2026-10-26 11:29 +0100
parents
children
line wrap: on
line source

/*
 * Copyright (c) 2026 Arkadiusz Juszczyk <ark@wobblyhead.net>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#ifndef BOTBLOCKD_H
#define BOTBLOCKD_H

#include <sys/types.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>

/* ---- compile-time paths and constants (PREFIX from the Makefile) ---- */

#define DEF_CONF            PREFIX "/etc/botblockd/conf"
#define DEF_PIDFILE         "/var/run/botblockd.pid"
#define PF_TABLE            "botblock"
#define NSTATUS             1000    /* HTTP status codes are 0..999 */

/* ---- shared configuration ---- */

struct config {
    int     inspect_secs;
    int     inspect_count;
    int     min_errors;
    int     cooldown_secs;
    int     block_secs;
    char    input_log[512];
    char    whitelist[512];
    bool    status_is_err[NSTATUS]; /* true if the code counts as error */
};

extern struct config cfg;

/* ---- config.c ---- */

int            parse_config(const char *path, struct config *out);
char          *read_entry(FILE *fp, char **line, size_t *cap, int *lineno);
bool           status_is_error(int code);

/* ---- ip.c ---- */

int            parse_ipv4(const char *s, uint32_t *out);
int            parse_cidr(const char *s, uint32_t *net, uint32_t *mask);
const char    *ip_str(uint32_t ip);

/* ---- whitelist.c ---- */

int            load_whitelist(const char *path);
bool           is_whitelisted(uint32_t ip);

/* ---- pf.c ---- */

int            pf_block(uint32_t ip);
int            pf_unblock(uint32_t ip);
int            pf_show(bool quiet);
int            pf_flush(void);

/* ---- suspect.c ---- */

void           process_request(uint32_t ip, int status);
void           sweep(void);

/* ---- logtail.c ---- */

int            tail_open(void);
int            tail_log(void);

/* ---- main.c ---- */

extern volatile sig_atomic_t running;
extern volatile sig_atomic_t reload_pending;
void           config_reload(void);
time_t         monotime(void);

#endif /* BOTBLOCKD_H */