1
0
mirror of https://github.com/openbsd/src.git synced 2024-12-22 07:27:59 -08:00

Let relayd make use of libagentx. No functional change intended.

OK tb@
Enthousiasm from Mischa Peters
This commit is contained in:
martijn 2020-10-26 16:52:06 +00:00
parent ced6338b8c
commit 1ae60b2abe
9 changed files with 457 additions and 6639 deletions

View File

@ -1,16 +1,15 @@
# $OpenBSD: Makefile,v 1.34 2020/09/14 11:30:25 martijn Exp $
# $OpenBSD: Makefile,v 1.35 2020/10/26 16:52:06 martijn Exp $
PROG= relayd
SRCS= parse.y
SRCS+= agentx.c agentx_control.c ca.c carp.c check_icmp.c \
check_script.c check_tcp.c check_tls.c config.c control.c \
hce.c log.c name2id.c pfe.c pfe_filter.c pfe_route.c proc.c \
relay.c relay_http.c relay_udp.c relayd.c \
shuffle.c ssl.c subagentx.c subagentx_log.c util.c
SRCS+= agentx_control.c ca.c carp.c check_icmp.c check_script.c \
check_tcp.c check_tls.c config.c control.c hce.c log.c \
name2id.c pfe.c pfe_filter.c pfe_route.c proc.c relay.c \
relay_http.c relay_udp.c relayd.c shuffle.c ssl.c util.c
MAN= relayd.8 relayd.conf.5
LDADD= -levent -ltls -lssl -lcrypto -lutil
DPADD= ${LIBEVENT} ${LIBSSL} ${LIBCRYPTO} ${LIBUTIL}
LDADD= -lagentx -levent -ltls -lssl -lcrypto -lutil
DPADD= ${LIBAGENTX} ${LIBEVENT} ${LIBSSL} ${LIBCRYPTO} ${LIBUTIL}
CFLAGS+= -Wall -I${.CURDIR}
CFLAGS+= -Wstrict-prototypes -Wmissing-prototypes
CFLAGS+= -Wmissing-declarations

File diff suppressed because it is too large Load Diff

View File

@ -1,232 +0,0 @@
/*
* Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdint.h>
#define AGENTX_PDU_FLAG_INSTANCE_REGISTRATION (1 << 0)
#define AGENTX_PDU_FLAG_NEW_INDEX (1 << 1)
#define AGENTX_PDU_FLAG_ANY_INDEX (1 << 2)
#define AGENTX_PDU_FLAG_NON_DEFAULT_CONTEXT (1 << 3)
#define AGENTX_PDU_FLAG_NETWORK_BYTE_ORDER (1 << 4)
#define AGENTX_PRIORITY_DEFAULT 127
enum agentx_byte_order {
AGENTX_BYTE_ORDER_BE,
AGENTX_BYTE_ORDER_LE
};
#if BYTE_ORDER == BIG_ENDIAN
#define AGENTX_BYTE_ORDER_NATIVE AGENTX_BYTE_ORDER_BE
#else
#define AGENTX_BYTE_ORDER_NATIVE AGENTX_BYTE_ORDER_LE
#endif
enum agentx_pdu_type {
AGENTX_PDU_TYPE_OPEN = 1,
AGENTX_PDU_TYPE_CLOSE = 2,
AGENTX_PDU_TYPE_REGISTER = 3,
AGENTX_PDU_TYPE_UNREGISTER = 4,
AGENTX_PDU_TYPE_GET = 5,
AGENTX_PDU_TYPE_GETNEXT = 6,
AGENTX_PDU_TYPE_GETBULK = 7,
AGENTX_PDU_TYPE_TESTSET = 8,
AGENTX_PDU_TYPE_COMMITSET = 9,
AGENTX_PDU_TYPE_UNDOSET = 10,
AGENTX_PDU_TYPE_CLEANUPSET = 11,
AGENTX_PDU_TYPE_NOTIFY = 12,
AGENTX_PDU_TYPE_PING = 13,
AGENTX_PDU_TYPE_INDEXALLOCATE = 14,
AGENTX_PDU_TYPE_INDEXDEALLOCATE = 15,
AGENTX_PDU_TYPE_ADDAGENTCAPS = 16,
AGENTX_PDU_TYPE_REMOVEAGENTCAPS = 17,
AGENTX_PDU_TYPE_RESPONSE = 18
};
enum agentx_pdu_error {
AGENTX_PDU_ERROR_NOERROR = 0,
AGENTX_PDU_ERROR_GENERR = 5,
AGENTX_PDU_ERROR_NOACCESS = 6,
AGENTX_PDU_ERROR_WRONGTYPE = 7,
AGENTX_PDU_ERROR_WRONGLENGTH = 8,
AGENTX_PDU_ERROR_WRONGENCODING = 9,
AGENTX_PDU_ERROR_WRONGVALUE = 10,
AGENTX_PDU_ERROR_NOCREATION = 11,
AGENTX_PDU_ERROR_INCONSISTENTVALUE = 12,
AGENTX_PDU_ERROR_RESOURCEUNAVAILABLE = 13,
AGENTX_PDU_ERROR_COMMITFAILED = 14,
AGENTX_PDU_ERROR_UNDOFAILED = 15,
AGENTX_PDU_ERROR_NOTWRITABLE = 17,
AGENTX_PDU_ERROR_INCONSISTENTNAME = 18,
AGENTX_PDU_ERROR_OPENFAILED = 256,
AGENTX_PDU_ERROR_NOTOPEN = 257,
AGENTX_PDU_ERROR_INDEXWRONGTYPE = 258,
AGENTX_PDU_ERROR_INDEXALREADYALLOCATED = 259,
AGENTX_PDU_ERROR_INDEXNONEAVAILABLE = 260,
AGENTX_PDU_ERROR_INDEXNOTALLOCATED = 261,
AGENTX_PDU_ERROR_UNSUPPORTEDCONETXT = 262,
AGENTX_PDU_ERROR_DUPLICATEREGISTRATION = 263,
AGENTX_PDU_ERROR_UNKNOWNREGISTRATION = 264,
AGENTX_PDU_ERROR_UNKNOWNAGENTCAPS = 265,
AGENTX_PDU_ERROR_PARSEERROR = 266,
AGENTX_PDU_ERROR_REQUESTDENIED = 267,
AGENTX_PDU_ERROR_PROCESSINGERROR = 268
};
enum agentx_data_type {
AGENTX_DATA_TYPE_INTEGER = 2,
AGENTX_DATA_TYPE_OCTETSTRING = 4,
AGENTX_DATA_TYPE_NULL = 5,
AGENTX_DATA_TYPE_OID = 6,
AGENTX_DATA_TYPE_IPADDRESS = 64,
AGENTX_DATA_TYPE_COUNTER32 = 65,
AGENTX_DATA_TYPE_GAUGE32 = 66,
AGENTX_DATA_TYPE_TIMETICKS = 67,
AGENTX_DATA_TYPE_OPAQUE = 68,
AGENTX_DATA_TYPE_COUNTER64 = 70,
AGENTX_DATA_TYPE_NOSUCHOBJECT = 128,
AGENTX_DATA_TYPE_NOSUCHINSTANCE = 129,
AGENTX_DATA_TYPE_ENDOFMIBVIEW = 130
};
enum agentx_close_reason {
AGENTX_CLOSE_OTHER = 1,
AGENTX_CLOSEN_PARSEERROR = 2,
AGENTX_CLOSE_PROTOCOLERROR = 3,
AGENTX_CLOSE_TIMEOUTS = 4,
AGENTX_CLOSE_SHUTDOWN = 5,
AGENTX_CLOSE_BYMANAGER = 6
};
struct agentx {
int ax_fd;
enum agentx_byte_order ax_byteorder;
uint8_t *ax_rbuf;
size_t ax_rblen;
size_t ax_rbsize;
uint8_t *ax_wbuf;
size_t ax_wblen;
size_t ax_wbtlen;
size_t ax_wbsize;
uint32_t *ax_packetids;
size_t ax_packetidsize;
};
#ifndef AGENTX_PRIMITIVE
#define AGENTX_PRIMITIVE
#define AGENTX_OID_MAX_LEN 128
struct agentx_oid {
uint8_t aoi_include;
uint32_t aoi_id[AGENTX_OID_MAX_LEN];
size_t aoi_idlen;
};
struct agentx_ostring {
unsigned char *aos_string;
uint32_t aos_slen;
};
#endif
struct agentx_searchrange {
struct agentx_oid asr_start;
struct agentx_oid asr_stop;
};
struct agentx_pdu_header {
uint8_t aph_version;
uint8_t aph_type;
uint8_t aph_flags;
uint8_t aph_reserved;
uint32_t aph_sessionid;
uint32_t aph_transactionid;
uint32_t aph_packetid;
uint32_t aph_plength;
} __packed;
struct agentx_varbind {
enum agentx_data_type avb_type;
struct agentx_oid avb_oid;
union agentx_data {
uint32_t avb_uint32;
uint64_t avb_uint64;
struct agentx_ostring avb_ostring;
struct agentx_oid avb_oid;
} avb_data;
};
struct agentx_pdu {
struct agentx_pdu_header ap_header;
struct agentx_ostring ap_context;
union {
struct agentx_pdu_searchrangelist {
size_t ap_nsr;
struct agentx_searchrange *ap_sr;
} ap_srl;
struct agentx_pdu_getbulk {
uint16_t ap_nonrep;
uint16_t ap_maxrep;
struct agentx_pdu_searchrangelist ap_srl;
} ap_getbulk;
struct agentx_pdu_varbindlist {
struct agentx_varbind *ap_varbind;
size_t ap_nvarbind;
} ap_vbl;
struct agentx_pdu_response {
uint32_t ap_uptime;
enum agentx_pdu_error ap_error;
uint16_t ap_index;
struct agentx_varbind *ap_varbindlist;
size_t ap_nvarbind;
} ap_response;
void *ap_raw;
} ap_payload;
};
struct agentx *agentx_new(int);
void agentx_free(struct agentx *);
struct agentx_pdu *agentx_recv(struct agentx *);
ssize_t agentx_send(struct agentx *);
uint32_t agentx_open(struct agentx *, uint8_t, struct agentx_oid *,
struct agentx_ostring *);
uint32_t agentx_close(struct agentx *, uint32_t, enum agentx_close_reason);
uint32_t agentx_indexallocate(struct agentx *, uint8_t, uint32_t,
struct agentx_ostring *, struct agentx_varbind *, size_t);
uint32_t agentx_indexdeallocate(struct agentx *, uint32_t,
struct agentx_ostring *, struct agentx_varbind *, size_t);
uint32_t agentx_addagentcaps(struct agentx *, uint32_t, struct agentx_ostring *,
struct agentx_oid *, struct agentx_ostring *);
uint32_t agentx_removeagentcaps(struct agentx *, uint32_t,
struct agentx_ostring *, struct agentx_oid *);
uint32_t agentx_register(struct agentx *, uint8_t, uint32_t,
struct agentx_ostring *, uint8_t, uint8_t, uint8_t, struct agentx_oid *,
uint32_t);
uint32_t agentx_unregister(struct agentx *, uint32_t, struct agentx_ostring *,
uint8_t, uint8_t, struct agentx_oid *, uint32_t);
int agentx_response(struct agentx *, uint32_t, uint32_t, uint32_t,
struct agentx_ostring *, uint32_t, uint16_t, uint16_t,
struct agentx_varbind *, size_t);
void agentx_pdu_free(struct agentx_pdu *);
void agentx_varbind_free(struct agentx_varbind *);
const char *agentx_error2string(enum agentx_pdu_error);
const char *agentx_pdutype2string(enum agentx_pdu_type);
const char *agentx_oid2string(struct agentx_oid *);
const char *agentx_oidrange2string(struct agentx_oid *, uint8_t, uint32_t);
const char *agentx_varbind2string(struct agentx_varbind *);
const char *agentx_closereason2string(enum agentx_close_reason);
int agentx_oid_cmp(struct agentx_oid *, struct agentx_oid *);
int agentx_oid_add(struct agentx_oid *, uint32_t);

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
/* $OpenBSD: parse.y,v 1.247 2020/10/25 10:17:49 denis Exp $ */
/* $OpenBSD: parse.y,v 1.248 2020/10/26 16:52:06 martijn Exp $ */
/*
* Copyright (c) 2007 - 2014 Reyk Floeter <reyk@openbsd.org>
@ -39,6 +39,7 @@
#include <net/pfvar.h>
#include <net/route.h>
#include <agentx.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
@ -56,7 +57,6 @@
#include "relayd.h"
#include "http.h"
#include "subagentx.h"
TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
static struct file {
@ -454,7 +454,7 @@ main : INTERVAL NUMBER {
free($3);
} else
(void)strlcpy(conf->sc_conf.agentx_path,
SUBAGENTX_AGENTX_MASTER,
AGENTX_MASTER_PATH,
sizeof(conf->sc_conf.agentx_path));
}
| SNMP trap optstring {

File diff suppressed because it is too large Load Diff

View File

@ -1,147 +0,0 @@
/*
* Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <netinet/in.h>
#include <stdint.h>
#include <stddef.h>
struct subagentx;
struct subagentx_session;
struct subagentx_context;
struct subagentx_agentcaps;
struct subagentx_region;
struct subagentx_index;
struct subagentx_object;
struct subagentx_varbind;
enum subagentx_request_type {
SUBAGENTX_REQUEST_TYPE_GET,
SUBAGENTX_REQUEST_TYPE_GETNEXT,
SUBAGENTX_REQUEST_TYPE_GETNEXTINCLUSIVE
};
#define SUBAGENTX_AGENTX_MASTER "/var/agentx/master"
#define SUBAGENTX_OID_MAX_LEN 128
#define SUBAGENTX_OID_INDEX_MAX_LEN 10
#define SUBAGENTX_MIB2 1, 3, 6, 1, 2, 1
#define SUBAGENTX_ENTERPRISES 1, 3, 6, 1, 4, 1
#define SUBAGENTX_OID(...) (uint32_t []) { __VA_ARGS__ }, \
(sizeof((uint32_t []) { __VA_ARGS__ }) / sizeof(uint32_t))
extern void (*subagentx_log_fatal)(const char *, ...)
__attribute__((__format__ (printf, 1, 2)));
extern void (*subagentx_log_warn)(const char *, ...)
__attribute__((__format__ (printf, 1, 2)));
extern void (*subagentx_log_info)(const char *, ...)
__attribute__((__format__ (printf, 1, 2)));
extern void (*subagentx_log_debug)(const char *, ...)
__attribute__((__format__ (printf, 1, 2)));
struct subagentx *subagentx(void (*)(struct subagentx *, void *, int), void *);
void subagentx_connect(struct subagentx *, int);
void subagentx_read(struct subagentx *);
void subagentx_write(struct subagentx *);
extern void (*subagentx_wantwrite)(struct subagentx *, int);
void subagentx_free(struct subagentx *);
struct subagentx_session *subagentx_session(struct subagentx *,
uint32_t[], size_t, const char *, uint8_t);
void subagentx_session_free(struct subagentx_session *);
struct subagentx_context *subagentx_context(struct subagentx_session *,
const char *);
struct subagentx_object *subagentx_context_object_find(
struct subagentx_context *, const uint32_t[], size_t, int, int);
struct subagentx_object *subagentx_context_object_nfind(
struct subagentx_context *, const uint32_t[], size_t, int, int);
uint32_t subagentx_context_uptime(struct subagentx_context *);
void subagentx_context_free(struct subagentx_context *);
struct subagentx_agentcaps *subagentx_agentcaps(struct subagentx_context *,
uint32_t[], size_t, const char *);
void subagentx_agentcaps_free(struct subagentx_agentcaps *);
struct subagentx_region *subagentx_region(struct subagentx_context *,
uint32_t[], size_t, uint8_t);
void subagentx_region_free(struct subagentx_region *);
struct subagentx_index *subagentx_index_integer_new(struct subagentx_region *,
uint32_t[], size_t);
struct subagentx_index *subagentx_index_integer_any(struct subagentx_region *,
uint32_t[], size_t);
struct subagentx_index *subagentx_index_integer_value(struct subagentx_region *,
uint32_t[], size_t, uint32_t);
struct subagentx_index *subagentx_index_integer_dynamic(
struct subagentx_region *, uint32_t[], size_t);
struct subagentx_index *subagentx_index_string_dynamic(
struct subagentx_region *, uint32_t[], size_t);
struct subagentx_index *subagentx_index_nstring_dynamic(
struct subagentx_region *, uint32_t[], size_t, size_t);
struct subagentx_index *subagentx_index_oid_dynamic(struct subagentx_region *,
uint32_t[], size_t);
struct subagentx_index *subagentx_index_noid_dynamic(struct subagentx_region *,
uint32_t[], size_t, size_t);
struct subagentx_index *subagentx_index_ipaddress_dynamic(
struct subagentx_region *, uint32_t[], size_t);
void subagentx_index_free(struct subagentx_index *);
struct subagentx_object *subagentx_object(struct subagentx_region *, uint32_t[],
size_t, struct subagentx_index *[], size_t, int,
void (*)(struct subagentx_varbind *));
void subagentx_object_free(struct subagentx_object *);
void subagentx_varbind_integer(struct subagentx_varbind *, uint32_t);
void subagentx_varbind_string(struct subagentx_varbind *, const char *);
void subagentx_varbind_nstring(struct subagentx_varbind *,
const unsigned char *, size_t);
void subagentx_varbind_printf(struct subagentx_varbind *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void subagentx_varbind_null(struct subagentx_varbind *);
void subagentx_varbind_oid(struct subagentx_varbind *, const uint32_t[],
size_t);
void subagentx_varbind_object(struct subagentx_varbind *,
struct subagentx_object *);
void subagentx_varbind_index(struct subagentx_varbind *,
struct subagentx_index *);
void subagentx_varbind_ipaddress(struct subagentx_varbind *,
const struct in_addr *);
void subagentx_varbind_counter32(struct subagentx_varbind *, uint32_t);
void subagentx_varbind_gauge32(struct subagentx_varbind *, uint32_t);
void subagentx_varbind_timeticks(struct subagentx_varbind *, uint32_t);
void subagentx_varbind_opaque(struct subagentx_varbind *, const char *, size_t);
void subagentx_varbind_counter64(struct subagentx_varbind *, uint64_t);
void subagentx_varbind_notfound(struct subagentx_varbind *);
void subagentx_varbind_error(struct subagentx_varbind *);
enum subagentx_request_type subagentx_varbind_request(
struct subagentx_varbind *);
struct subagentx_object *
subagentx_varbind_get_object(struct subagentx_varbind *);
uint32_t subagentx_varbind_get_index_integer(struct subagentx_varbind *,
struct subagentx_index *);
const unsigned char *subagentx_varbind_get_index_string(
struct subagentx_varbind *, struct subagentx_index *, size_t *, int *);
const uint32_t *subagentx_varbind_get_index_oid(struct subagentx_varbind *,
struct subagentx_index *, size_t *, int *);
const struct in_addr *subagentx_varbind_get_index_ipaddress(
struct subagentx_varbind *, struct subagentx_index *);
void subagentx_varbind_set_index_integer(struct subagentx_varbind *,
struct subagentx_index *, uint32_t);
void subagentx_varbind_set_index_string(struct subagentx_varbind *,
struct subagentx_index *, const char *);
void subagentx_varbind_set_index_nstring(struct subagentx_varbind *,
struct subagentx_index *, const unsigned char *, size_t);
void subagentx_varbind_set_index_oid(struct subagentx_varbind *,
struct subagentx_index *, const uint32_t *, size_t);
void subagentx_varbind_set_index_object(struct subagentx_varbind *,
struct subagentx_index *, struct subagentx_object *);
void subagentx_varbind_set_index_ipaddress(struct subagentx_varbind *,
struct subagentx_index *, const struct in_addr *);

View File

@ -1,124 +0,0 @@
/*
* Copyright (c) 2020 Martijn van Duren <martijn@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <sys/time.h>
#include <sys/tree.h>
#include "agentx.h"
enum subagentx_cstate { /* Current state */
SA_CSTATE_CLOSE, /* Closed */
SA_CSTATE_WAITOPEN, /* Connection requested */
SA_CSTATE_OPEN, /* Open */
SA_CSTATE_WAITCLOSE /* Close requested */
};
enum subagentx_dstate { /* Desired state */
SA_DSTATE_OPEN, /* Open */
SA_DSTATE_CLOSE /* Close/free */
};
struct subagentx {
void (*sa_nofd)(struct subagentx *, void *, int);
void *sa_cookie;
int sa_fd;
enum subagentx_cstate sa_cstate;
enum subagentx_dstate sa_dstate;
struct agentx *sa_ax;
TAILQ_HEAD(, subagentx_session) sa_sessions;
TAILQ_HEAD(, subagentx_get) sa_getreqs;
RB_HEAD(sa_requests, subagentx_request) sa_requests;
};
struct subagentx_session {
struct subagentx *sas_sa;
uint32_t sas_id;
uint32_t sas_timeout;
struct agentx_oid sas_oid;
struct agentx_ostring sas_descr;
enum subagentx_cstate sas_cstate;
enum subagentx_dstate sas_dstate;
uint32_t sas_packetid;
TAILQ_HEAD(, subagentx_context) sas_contexts;
TAILQ_ENTRY(subagentx_session) sas_sa_sessions;
};
struct subagentx_context {
struct subagentx_session *sac_sas;
int sac_name_default;
struct agentx_ostring sac_name;
uint32_t sac_sysuptime;
struct timespec sac_sysuptimespec;
enum subagentx_cstate sac_cstate;
enum subagentx_dstate sac_dstate;
TAILQ_HEAD(, subagentx_agentcaps) sac_agentcaps;
TAILQ_HEAD(, subagentx_region) sac_regions;
RB_HEAD(sac_objects, subagentx_object) sac_objects;
TAILQ_ENTRY(subagentx_context) sac_sas_contexts;
};
struct subagentx_get {
struct subagentx_context *sag_sac;
int sag_fd; /* Only used for logging */
uint32_t sag_sessionid;
uint32_t sag_transactionid;
uint32_t sag_packetid;
int sag_context_default;
struct agentx_ostring sag_context;
enum agentx_pdu_type sag_type;
uint16_t sag_nonrep;
uint16_t sag_maxrep;
size_t sag_nvarbind;
struct subagentx_varbind *sag_varbind;
TAILQ_ENTRY(subagentx_get) sag_sa_getreqs;
};
__dead void subagentx_log_sa_fatalx(struct subagentx *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sa_warn(struct subagentx *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sa_warnx(struct subagentx *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sa_info(struct subagentx *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sa_debug(struct subagentx *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
__dead void subagentx_log_sas_fatalx(struct subagentx_session *, const char *,
...) __attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sas_warnx(struct subagentx_session *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sas_warn(struct subagentx_session *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sas_info(struct subagentx_session *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
__dead void subagentx_log_sac_fatalx(struct subagentx_context *, const char *,
...) __attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sac_warnx(struct subagentx_context *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sac_warn(struct subagentx_context *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sac_info(struct subagentx_context *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sac_debug(struct subagentx_context *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
__dead void subagentx_log_sag_fatalx(struct subagentx_get *, const char *,
...) __attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sag_warnx(struct subagentx_get *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sag_warn(struct subagentx_get *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
void subagentx_log_sag_debug(struct subagentx_get *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));

View File

@ -1,316 +0,0 @@
/*
* Copyright (c) 2020 Martijn van Duren <martijn@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "subagentx_internal.h"
#define SUBAGENTX_CONTEXT_NAME(sac) (sac->sac_name_default ? "<default>" : \
(char *)sac->sac_name.aos_string)
#define SUBAGENTX_GET_CTXNAME(sag) (sag->sag_context_default ? "<default>" : \
(char *)sag->sag_context.aos_string)
enum subagentx_log_type {
SUBAGENTX_LOG_TYPE_FATAL,
SUBAGENTX_LOG_TYPE_WARN,
SUBAGENTX_LOG_TYPE_INFO,
SUBAGENTX_LOG_TYPE_DEBUG
};
void (*subagentx_log_fatal)(const char *, ...)
__attribute__((__format__ (printf, 1, 2))) = NULL;
void (*subagentx_log_warn)(const char *, ...)
__attribute__((__format__ (printf, 1, 2))) = NULL;
void (*subagentx_log_info)(const char *, ...)
__attribute__((__format__ (printf, 1, 2))) = NULL;
void (*subagentx_log_debug)(const char *, ...)
__attribute__((__format__ (printf, 1, 2))) = NULL;
static void
subagentx_log_do(enum subagentx_log_type, const char *, va_list, int,
struct subagentx *, struct subagentx_session *, struct subagentx_context *,
struct subagentx_get *);
void
subagentx_log_sa_fatalx(struct subagentx *sa, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_FATAL, fmt, ap, 0, sa, NULL, NULL,
NULL);
va_end(ap);
abort();
}
void
subagentx_log_sa_warn(struct subagentx *sa, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_WARN, fmt, ap, 1, sa, NULL, NULL,
NULL);
va_end(ap);
}
void
subagentx_log_sa_warnx(struct subagentx *sa, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_WARN, fmt, ap, 0, sa, NULL, NULL,
NULL);
va_end(ap);
}
void
subagentx_log_sa_info(struct subagentx *sa, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_INFO, fmt, ap, 0, sa, NULL, NULL,
NULL);
va_end(ap);
}
void
subagentx_log_sa_debug(struct subagentx *sa, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_DEBUG, fmt, ap, 0, sa, NULL, NULL,
NULL);
va_end(ap);
}
void
subagentx_log_sas_fatalx(struct subagentx_session *sas, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_FATAL, fmt, ap, 0, NULL, sas, NULL,
NULL);
va_end(ap);
abort();
}
void
subagentx_log_sas_warnx(struct subagentx_session *sas, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_WARN, fmt, ap, 0, NULL, sas, NULL,
NULL);
va_end(ap);
}
void
subagentx_log_sas_warn(struct subagentx_session *sas, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_WARN, fmt, ap, 1, NULL, sas, NULL,
NULL);
va_end(ap);
}
void
subagentx_log_sas_info(struct subagentx_session *sas, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_INFO, fmt, ap, 0, NULL, sas, NULL,
NULL);
va_end(ap);
}
void
subagentx_log_sac_fatalx(struct subagentx_context *sac, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_FATAL, fmt, ap, 0, NULL, NULL, sac,
NULL);
va_end(ap);
abort();
}
void
subagentx_log_sac_warnx(struct subagentx_context *sac, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_WARN, fmt, ap, 0, NULL, NULL, sac,
NULL);
va_end(ap);
}
void
subagentx_log_sac_warn(struct subagentx_context *sac, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_WARN, fmt, ap, 1, NULL, NULL, sac,
NULL);
va_end(ap);
}
void
subagentx_log_sac_info(struct subagentx_context *sac, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_INFO, fmt, ap, 0, NULL, NULL, sac,
NULL);
va_end(ap);
}
void
subagentx_log_sac_debug(struct subagentx_context *sac, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_DEBUG, fmt, ap, 0, NULL, NULL, sac,
NULL);
va_end(ap);
}
void
subagentx_log_sag_fatalx(struct subagentx_get *sag, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_FATAL, fmt, ap, 0, NULL, NULL, NULL,
sag);
va_end(ap);
abort();
}
void
subagentx_log_sag_warnx(struct subagentx_get *sag, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_WARN, fmt, ap, 0, NULL, NULL, NULL,
sag);
va_end(ap);
}
void
subagentx_log_sag_warn(struct subagentx_get *sag, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_WARN, fmt, ap, 1, NULL, NULL, NULL,
sag);
va_end(ap);
}
void
subagentx_log_sag_debug(struct subagentx_get *sag, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
subagentx_log_do(SUBAGENTX_LOG_TYPE_DEBUG, fmt, ap, 0, NULL, NULL, NULL,
sag);
va_end(ap);
}
static void
subagentx_log_do(enum subagentx_log_type type, const char *fmt, va_list ap,
int useerrno, struct subagentx *sa, struct subagentx_session *sas,
struct subagentx_context *sac, struct subagentx_get *sag)
{
void (*subagentx_log)(const char *, ...);
char buf[1500];
if (type == SUBAGENTX_LOG_TYPE_FATAL)
subagentx_log = subagentx_log_fatal;
else if (type == SUBAGENTX_LOG_TYPE_WARN)
subagentx_log = subagentx_log_warn;
else if (type == SUBAGENTX_LOG_TYPE_INFO)
subagentx_log = subagentx_log_info;
else
subagentx_log = subagentx_log_debug;
if (subagentx_log == NULL)
return;
vsnprintf(buf, sizeof(buf), fmt, ap);
if (sag != NULL) {
if (useerrno)
subagentx_log("[fd:%d sess:%u ctx:%s trid:%u pid:%u]: "
"%s: %s", sag->sag_fd, sag->sag_sessionid,
SUBAGENTX_GET_CTXNAME(sag), sag->sag_transactionid,
sag->sag_packetid, buf, strerror(errno));
else
subagentx_log("[fd:%d sess:%u ctx:%s trid:%u pid:%u]: "
"%s", sag->sag_fd, sag->sag_sessionid,
SUBAGENTX_GET_CTXNAME(sag), sag->sag_transactionid,
sag->sag_packetid, buf);
} else if (sac != NULL) {
sas = sac->sac_sas;
sa = sas->sas_sa;
if (useerrno)
subagentx_log("[fd:%d sess:%u ctx:%s]: %s: %s",
sa->sa_fd, sas->sas_id, SUBAGENTX_CONTEXT_NAME(sac),
buf, strerror(errno));
else
subagentx_log("[fd:%d sess:%u ctx:%s]: %s", sa->sa_fd,
sas->sas_id, SUBAGENTX_CONTEXT_NAME(sac), buf);
} else if (sas != NULL) {
sa = sas->sas_sa;
if (useerrno)
subagentx_log("[fd:%d sess:%u]: %s: %s", sa->sa_fd,
sas->sas_id, buf, strerror(errno));
else
subagentx_log("[fd:%d sess:%u]: %s", sa->sa_fd,
sas->sas_id, buf);
} else if (sa->sa_fd == -1) {
if (useerrno)
subagentx_log("%s: %s", buf, strerror(errno));
else
subagentx_log("%s", buf);
} else {
if (useerrno)
subagentx_log("[fd:%d]: %s: %s", sa->sa_fd, buf,
strerror(errno));
else
subagentx_log("[fd:%d]: %s", sa->sa_fd, buf);
}
}