While doing that convert IMSG_CTL_SHOW_RIB over to the new ibuf api.
OK tb@
-/* $OpenBSD: bgpctl.c,v 1.302 2024/01/25 09:54:21 claudio Exp $ */
+/* $OpenBSD: bgpctl.c,v 1.303 2024/01/30 13:51:13 claudio Exp $ */
/*
* Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
struct ctl_show_rib rib;
struct rde_memstats stats;
struct ibuf ibuf;
- u_char *asdata;
u_int rescode, ilen;
- size_t aslen;
switch (imsg->hdr.type) {
case IMSG_CTL_SHOW_NEIGHBOR:
output->fib_table(&kt);
break;
case IMSG_CTL_SHOW_RIB:
- if (imsg->hdr.len < IMSG_HEADER_SIZE + sizeof(rib))
- errx(1, "wrong imsg len");
if (output->rib == NULL)
break;
- /* XXX */
- memcpy(&rib, imsg->data, sizeof(rib));
- aslen = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof(rib);
- asdata = imsg->data;
- asdata += sizeof(rib);
- output->rib(&rib, asdata, aslen, res);
+ if (imsg_get_ibuf(imsg, &ibuf) == -1)
+ err(1, "imsg_get_ibuf");
+ if (ibuf_get(&ibuf, &rib, sizeof(rib)) == -1)
+ err(1, "imsg_get_ibuf");
+ output->rib(&rib, &ibuf, res);
break;
case IMSG_CTL_SHOW_RIB_COMMUNITIES:
if (output->communities == NULL)
struct parse_result res;
struct ctl_show_rib_request *req = arg;
struct mrt_rib_entry *mre;
+ struct ibuf ibuf;
time_t now;
uint16_t i, j;
!match_aspath(mre->aspath, mre->aspath_len, &req->as))
continue;
- output->rib(&ctl, mre->aspath, mre->aspath_len, &res);
+ ibuf_from_buffer(&ibuf, mre->aspath, mre->aspath_len);
+ output->rib(&ctl, &ibuf, &res);
if (req->flags & F_CTL_DETAIL) {
for (j = 0; j < mre->nattrs; j++)
output->attr(mre->attrs[j].attr,
-/* $OpenBSD: bgpctl.h,v 1.22 2024/01/25 09:54:21 claudio Exp $ */
+/* $OpenBSD: bgpctl.h,v 1.23 2024/01/30 13:51:13 claudio Exp $ */
/*
* Copyright (c) 2019 Claudio Jeker <claudio@openbsd.org>
void (*interface)(struct ctl_show_interface *);
void (*attr)(u_char *, size_t, int, int);
void (*communities)(struct ibuf *, struct parse_result *);
- void (*rib)(struct ctl_show_rib *, u_char *, size_t,
+ void (*rib)(struct ctl_show_rib *, struct ibuf *,
struct parse_result *);
void (*rib_mem)(struct rde_memstats *);
void (*set)(struct ctl_show_set *);
-/* $OpenBSD: output.c,v 1.48 2024/01/25 09:54:21 claudio Exp $ */
+/* $OpenBSD: output.c,v 1.49 2024/01/30 13:51:13 claudio Exp $ */
/*
* Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
static void
show_attr(u_char *data, size_t len, int reqflags, int addpath)
{
- u_char *path;
struct in_addr id;
struct bgpd_addr prefix;
- struct ibuf ibuf, *buf = &ibuf;
+ struct ibuf ibuf, *buf = &ibuf, asbuf, *path = NULL;
char *aspath;
uint32_t as, pathid;
uint16_t alen, ioff, short_as, afi;
break;
case ATTR_ASPATH:
case ATTR_AS4_PATH:
+ ibuf_from_buffer(buf, data, alen);
/* prefer 4-byte AS here */
- e4 = aspath_verify(data, alen, 1, 0);
- e2 = aspath_verify(data, alen, 0, 0);
+ e4 = aspath_verify(buf, 1, 0);
+ e2 = aspath_verify(buf, 0, 0);
if (e4 == 0 || e4 == AS_ERR_SOFT) {
- path = data;
+ ibuf_from_ibuf(&asbuf, buf);
} else if (e2 == 0 || e2 == AS_ERR_SOFT) {
- path = aspath_inflate(data, alen, &alen);
- if (path == NULL)
- errx(1, "aspath_inflate failed");
+ if ((path = aspath_inflate(buf)) == NULL) {
+ printf("aspath_inflate failed");
+ break;
+ }
+ ibuf_from_ibuf(&asbuf, path);
} else {
printf("bad AS-Path");
break;
}
- if (aspath_asprint(&aspath, path, alen) == -1)
+ if (aspath_asprint(&aspath, &asbuf) == -1)
err(1, NULL);
printf("%s", aspath);
free(aspath);
- if (path != data)
- free(path);
+ ibuf_free(path);
break;
case ATTR_NEXTHOP:
if (alen == 4) {
}
static void
-show_rib_brief(struct ctl_show_rib *r, u_char *asdata, size_t aslen)
+show_rib_brief(struct ctl_show_rib *r, struct ibuf *asbuf)
{
char *p, *aspath;
log_addr(&r->exit_nexthop), r->local_pref, r->med);
free(p);
- if (aspath_asprint(&aspath, asdata, aslen) == -1)
+ if (aspath_asprint(&aspath, asbuf) == -1)
err(1, NULL);
if (strlen(aspath) > 0)
printf("%s ", aspath);
}
static void
-show_rib_detail(struct ctl_show_rib *r, u_char *asdata, size_t aslen,
- int flag0)
+show_rib_detail(struct ctl_show_rib *r, struct ibuf *asbuf, int flag0)
{
struct in_addr id;
char *aspath, *s;
log_addr(&r->prefix), r->prefixlen,
EOL0(flag0));
- if (aspath_asprint(&aspath, asdata, aslen) == -1)
+ if (aspath_asprint(&aspath, asbuf) == -1)
err(1, NULL);
if (strlen(aspath) > 0)
printf(" %s%c", aspath, EOL0(flag0));
}
static void
-show_rib(struct ctl_show_rib *r, u_char *asdata, size_t aslen,
- struct parse_result *res)
+show_rib(struct ctl_show_rib *r, struct ibuf *aspath, struct parse_result *res)
{
if (res->flags & F_CTL_DETAIL)
- show_rib_detail(r, asdata, aslen, res->flags);
+ show_rib_detail(r, aspath, res->flags);
else
- show_rib_brief(r, asdata, aslen);
+ show_rib_brief(r, aspath);
}
static void
-/* $OpenBSD: output_json.c,v 1.40 2024/01/25 09:54:21 claudio Exp $ */
+/* $OpenBSD: output_json.c,v 1.41 2024/01/30 13:51:13 claudio Exp $ */
/*
* Copyright (c) 2020 Claudio Jeker <claudio@openbsd.org>
{
struct bgpd_addr prefix;
struct in_addr id;
- struct ibuf ibuf, *buf = &ibuf;
+ struct ibuf ibuf, *buf = &ibuf, asbuf, *path = NULL;
char *aspath;
- u_char *path;
uint32_t as, pathid;
uint16_t alen, afi, off, short_as;
uint8_t flags, type, safi, aid, prefixlen;
break;
case ATTR_ASPATH:
case ATTR_AS4_PATH:
+ ibuf_from_buffer(buf, data, alen);
/* prefer 4-byte AS here */
- e4 = aspath_verify(data, alen, 1, 0);
- e2 = aspath_verify(data, alen, 0, 0);
+ e4 = aspath_verify(buf, 1, 0);
+ e2 = aspath_verify(buf, 0, 0);
if (e4 == 0 || e4 == AS_ERR_SOFT) {
- path = data;
+ ibuf_from_ibuf(&asbuf, buf);
} else if (e2 == 0 || e2 == AS_ERR_SOFT) {
- path = aspath_inflate(data, alen, &alen);
- if (path == NULL)
- errx(1, "aspath_inflate failed");
+ if ((path = aspath_inflate(buf)) == NULL) {
+ json_do_string("error",
+ "aspath_inflate failed");
+ break;
+ }
+ ibuf_from_ibuf(&asbuf, path);
} else {
json_do_string("error", "bad AS-Path");
break;
}
- if (aspath_asprint(&aspath, path, alen) == -1)
+ if (aspath_asprint(&aspath, &asbuf) == -1)
err(1, NULL);
json_do_string("aspath", aspath);
free(aspath);
- if (path != data)
- free(path);
+ ibuf_free(path);
break;
case ATTR_NEXTHOP:
if (alen == 4) {
}
static void
-json_rib(struct ctl_show_rib *r, u_char *asdata, size_t aslen,
- struct parse_result *res)
+json_rib(struct ctl_show_rib *r, struct ibuf *asbuf, struct parse_result *res)
{
struct in_addr id;
char *aspath;
json_do_printf("prefix", "%s/%u", log_addr(&r->prefix), r->prefixlen);
- if (aspath_asprint(&aspath, asdata, aslen) == -1)
+ if (aspath_asprint(&aspath, asbuf) == -1)
err(1, NULL);
json_do_string("aspath", aspath);
free(aspath);