This is my try at cleaning up commons in ospfd. I made one big combined diff but will probably split up a few things into own commits. E.g. the lsupdate.c and lsreq.c ones.
I had to cleanup the control.c code a bit since this was a bit of a mess. While in bgpd I was able to remove the global bgpd_process variable this is not really possible in ospfd. Too much code depends on it and unrolling it just gets messy. -- :wq Claudio Index: control.c =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/control.c,v retrieving revision 1.46 diff -u -p -r1.46 control.c --- control.c 16 Sep 2020 20:50:10 -0000 1.46 +++ control.c 18 Jan 2021 10:11:05 -0000 @@ -32,12 +32,20 @@ #include "log.h" #include "control.h" +TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns); + #define CONTROL_BACKLOG 5 struct ctl_conn *control_connbyfd(int); struct ctl_conn *control_connbypid(pid_t); void control_close(int); +struct { + struct event ev; + struct event evt; + int fd; +} control_state; + int control_check(char *path) { @@ -108,8 +116,9 @@ control_init(char *path) } int -control_listen(void) +control_listen(int fd) { + control_state.fd = fd; if (listen(control_state.fd, CONTROL_BACKLOG) == -1) { log_warn("control_listen: listen"); Index: control.h =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/control.h,v retrieving revision 1.8 diff -u -p -r1.8 control.h --- control.h 16 Sep 2020 20:50:10 -0000 1.8 +++ control.h 18 Jan 2021 10:11:18 -0000 @@ -23,12 +23,6 @@ #include <sys/time.h> #include <event.h> -struct { - struct event ev; - struct event evt; - int fd; -} control_state; - struct ctl_conn { TAILQ_ENTRY(ctl_conn) entry; struct imsgev iev; @@ -36,7 +30,7 @@ struct ctl_conn { int control_check(char *); int control_init(char *); -int control_listen(void); +int control_listen(int); void control_accept(int, short, void *); void control_dispatch_imsg(int, short, void *); int control_imsg_relay(struct imsg *); Index: lsreq.c =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/lsreq.c,v retrieving revision 1.21 diff -u -p -r1.21 lsreq.c --- lsreq.c 15 Jul 2019 18:26:39 -0000 1.21 +++ lsreq.c 18 Jan 2021 10:48:04 -0000 @@ -27,8 +27,6 @@ #include "log.h" #include "ospfe.h" -extern struct imsgev *iev_rde; - /* link state request packet handling */ int send_ls_req(struct nbr *nbr) @@ -107,8 +105,7 @@ recv_ls_req(struct nbr *nbr, char *buf, case NBR_STA_XCHNG: case NBR_STA_LOAD: case NBR_STA_FULL: - imsg_compose_event(iev_rde, IMSG_LS_REQ, nbr->peerid, - 0, -1, buf, len); + ospfe_imsg_compose_rde(IMSG_LS_REQ, nbr->peerid, 0, buf, len); break; default: fatalx("recv_ls_req: unknown neighbor state"); Index: lsupdate.c =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/lsupdate.c,v retrieving revision 1.48 diff -u -p -r1.48 lsupdate.c --- lsupdate.c 6 May 2020 14:40:54 -0000 1.48 +++ lsupdate.c 18 Jan 2021 10:48:10 -0000 @@ -32,9 +32,6 @@ #include "ospfe.h" #include "rde.h" -extern struct ospfd_conf *oeconf; -extern struct imsgev *iev_rde; - struct ibuf *prepare_ls_update(struct iface *); int add_ls_update(struct ibuf *, struct iface *, void *, u_int16_t, u_int16_t); @@ -276,8 +273,8 @@ recv_ls_update(struct nbr *nbr, char *bu "neighbor ID %s", inet_ntoa(nbr->id)); return; } - imsg_compose_event(iev_rde, IMSG_LS_UPD, nbr->peerid, 0, - -1, buf, ntohs(lsa.len)); + ospfe_imsg_compose_rde(IMSG_LS_UPD, nbr->peerid, 0, + buf, ntohs(lsa.len)); buf += ntohs(lsa.len); len -= ntohs(lsa.len); } Index: ospfd.c =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/ospfd.c,v retrieving revision 1.114 diff -u -p -r1.114 ospfd.c --- ospfd.c 16 Sep 2020 20:50:10 -0000 1.114 +++ ospfd.c 18 Jan 2021 10:50:40 -0000 @@ -64,9 +64,10 @@ int pipe_parent2ospfe[2]; int pipe_parent2rde[2]; int pipe_ospfe2rde[2]; +enum ospfd_process ospfd_process; struct ospfd_conf *ospfd_conf = NULL; -struct imsgev *iev_ospfe; -struct imsgev *iev_rde; +static struct imsgev *iev_ospfe; +static struct imsgev *iev_rde; char *conffile; pid_t ospfe_pid = 0; Index: ospfd.h =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/ospfd.h,v retrieving revision 1.107 diff -u -p -r1.107 ospfd.h --- ospfd.h 2 Nov 2020 00:30:56 -0000 1.107 +++ ospfd.h 18 Jan 2021 10:49:54 -0000 @@ -374,11 +374,12 @@ struct ifaddrchange { }; /* ospf_conf */ -enum { +enum ospfd_process { PROC_MAIN, PROC_OSPF_ENGINE, PROC_RDE_ENGINE -} ospfd_process; +}; +extern enum ospfd_process ospfd_process; struct ospfd_conf { struct event ev; Index: ospfe.c =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/ospfe.c,v retrieving revision 1.107 diff -u -p -r1.107 ospfe.c --- ospfe.c 2 Nov 2020 00:29:58 -0000 1.107 +++ ospfe.c 18 Jan 2021 10:43:07 -0000 @@ -47,9 +47,9 @@ __dead void ospfe_shutdown(void); void orig_rtr_lsa_all(struct area *); struct iface *find_vlink(struct abr_rtr *); -struct ospfd_conf *oeconf = NULL, *nconf; -struct imsgev *iev_main; -struct imsgev *iev_rde; +struct ospfd_conf *oeconf = NULL, *noeconf; +static struct imsgev *iev_main; +static struct imsgev *iev_rde; int oe_nofib; /* ARGSUSED */ @@ -185,9 +185,6 @@ ospfe(struct ospfd_conf *xconf, int pipe } } - if ((pkt_ptr = calloc(1, READ_BUF_SIZE)) == NULL) - fatal("ospfe"); - /* start interfaces */ LIST_FOREACH(area, &oeconf->area_list, entry) { ospfe_demote_area(area, 0); @@ -240,7 +237,6 @@ ospfe_shutdown(void) free(iev_rde); free(iev_main); free(oeconf); - free(pkt_ptr); log_info("ospf engine exiting"); _exit(0); @@ -403,13 +399,13 @@ ospfe_dispatch_main(int fd, short event, } break; case IMSG_RECONF_CONF: - if ((nconf = malloc(sizeof(struct ospfd_conf))) == + if ((noeconf = malloc(sizeof(struct ospfd_conf))) == NULL) fatal(NULL); - memcpy(nconf, imsg.data, sizeof(struct ospfd_conf)); + memcpy(noeconf, imsg.data, sizeof(struct ospfd_conf)); - LIST_INIT(&nconf->area_list); - LIST_INIT(&nconf->cand_list); + LIST_INIT(&noeconf->area_list); + LIST_INIT(&noeconf->cand_list); break; case IMSG_RECONF_AREA: if ((narea = area_new()) == NULL) @@ -421,7 +417,7 @@ ospfe_dispatch_main(int fd, short event, RB_INIT(&narea->lsa_tree); SIMPLEQ_INIT(&narea->redist_list); - LIST_INSERT_HEAD(&nconf->area_list, narea, entry); + LIST_INSERT_HEAD(&noeconf->area_list, narea, entry); break; case IMSG_RECONF_IFACE: if ((niface = malloc(sizeof(struct iface))) == NULL) @@ -442,12 +438,12 @@ ospfe_dispatch_main(int fd, short event, break; case IMSG_RECONF_END: if ((oeconf->flags & OSPFD_FLAG_STUB_ROUTER) != - (nconf->flags & OSPFD_FLAG_STUB_ROUTER)) + (noeconf->flags & OSPFD_FLAG_STUB_ROUTER)) stub_changed = 1; else stub_changed = 0; - merge_config(oeconf, nconf); - nconf = NULL; + merge_config(oeconf, noeconf); + noeconf = NULL; if (stub_changed) orig_rtr_lsa_all(NULL); break; @@ -461,10 +457,8 @@ ospfe_dispatch_main(int fd, short event, if ((fd = imsg.fd) == -1) fatalx("%s: expected to receive imsg control" "fd but didn't receive any", __func__); - control_state.fd = fd; /* Listen on control socket. */ - TAILQ_INIT(&ctl_conns); - control_listen(); + control_listen(fd); if (pledge("stdio inet mcast", NULL) == -1) fatal("pledge"); break; Index: ospfe.h =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/ospfe.h,v retrieving revision 1.46 diff -u -p -r1.46 ospfe.h --- ospfe.h 25 Oct 2014 03:23:49 -0000 1.46 +++ ospfe.h 18 Jan 2021 10:08:42 -0000 @@ -26,8 +26,6 @@ #include <netinet/in.h> #include <netinet/ip.h> -TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns; - struct lsa_entry { TAILQ_ENTRY(lsa_entry) entry; union { @@ -95,6 +93,8 @@ struct nbr { u_int8_t dd_snapshot; /* snapshot running */ }; +struct ctl_conn; + /* auth.c */ int auth_validate(void *buf, u_int16_t len, struct iface *, struct nbr *); @@ -237,7 +237,5 @@ struct lsa_hdr *lsa_hdr_new(void); int gen_ospf_hdr(struct ibuf *, struct iface *, u_int8_t); int send_packet(struct iface *, struct ibuf *, struct sockaddr_in *); void recv_packet(int, short, void *); - -char *pkt_ptr; /* packet buffer */ #endif /* _OSPFE_H_ */ Index: packet.c =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/packet.c,v retrieving revision 1.33 diff -u -p -r1.33 packet.c --- packet.c 12 Aug 2019 20:32:39 -0000 1.33 +++ packet.c 18 Jan 2021 10:04:03 -0000 @@ -107,6 +107,7 @@ send_packet(struct iface *iface, struct void recv_packet(int fd, short event, void *bula) { + static char pkt_ptr[READ_BUF_SIZE]; union { struct cmsghdr hdr; char buf[CMSG_SPACE(sizeof(struct sockaddr_dl))]; Index: rde.c =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/rde.c,v retrieving revision 1.110 diff -u -p -r1.110 rde.c --- rde.c 19 Nov 2019 09:55:55 -0000 1.110 +++ rde.c 18 Jan 2021 10:43:20 -0000 @@ -65,8 +65,8 @@ struct lsa *orig_asext_lsa(struct kroute struct lsa *orig_sum_lsa(struct rt_node *, struct area *, u_int8_t, int); struct ospfd_conf *rdeconf = NULL, *nconf = NULL; -struct imsgev *iev_ospfe; -struct imsgev *iev_main; +static struct imsgev *iev_ospfe; +static struct imsgev *iev_main; struct rde_nbr *nbrself; struct lsa_tree asext_tree;