rpc: move tracked announcement addresses into struct instance_info

This way, they can be used by announcement callbacks.

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
2 files changed, 26 insertions(+), 15 deletions(-)

M rpc/announce-track.c
M rpc/include/hlog-rpc/announce-sub.h
M rpc/announce-track.c +16 -15
@@ 49,11 49,6 @@ 
  */
 #define NUM_QSO_DEDUP	4
 
-struct addr {
-	union xsockaddr addr;
-	uint64_t ts;
-};
-
 struct instance {
 	struct rb_node node;
 	struct lock lock;

          
@@ 65,7 60,6 @@ struct instance {
 
 	struct str *hlog_version;
 
-	struct addr *addrs; /* announcement addresses & times */
 	/* TODO: RPC addresses & ports */
 
 	struct qso_dedup {

          
@@ 104,8 98,8 @@ static struct instance *find_instance(ui
 	if (!inst) {
 		inst = zalloc(sizeof(struct instance));
 		if (inst) {
-			inst->addrs = array_alloc(sizeof(struct addr), 1);
-			if (!inst->addrs) {
+			inst->info.addrs = malloc(sizeof(struct instance_addr));
+			if (!inst->info.addrs) {
 				free(inst);
 				inst = NULL;
 			} else {

          
@@ 303,6 297,8 @@ err:
 static void update_addrs(struct instance *inst, const union xsockaddr *addr,
 			 size_t addrlen, uint64_t now)
 {
+	struct instance_info *info = &inst->info;
+	struct instance_addr *tmp;
 	size_t i;
 
 	/* safety check */

          
@@ 310,18 306,23 @@ static void update_addrs(struct instance
 		return; /* ignore addresses that don't fit into a xsockaddr */
 
 	/* check that we don't already have it */
-	for (i = 0; i < array_size(inst->addrs); i++)
-		if (xsockaddr_cmp(&inst->addrs[i].addr.sa, &addr->sa))
+	for (i = 0; i < info->num_addrs; i++)
+		if (xsockaddr_cmp(&info->addrs[i].addr.sa, &addr->sa))
 			return; /* match - already have it */
 
 	/* grow the array */
-	if (array_truncate(&inst->addrs, array_size(inst->addrs) + 1))
+	tmp = mem_reallocarray(info->addrs, info->num_addrs + 1,
+			       sizeof(struct instance_addr));
+	if (!tmp)
 		return; /* failed to grow - ignore this address */
 
-	i = array_size(inst->addrs) - 1;
+	info->addrs = tmp;
+	info->num_addrs++;
 
-	memcpy(&inst->addrs[i].addr, addr, sizeof(*addr));
-	inst->addrs[i].ts = now;
+	i = info->num_addrs - 1;
+
+	memcpy(&info->addrs[i].addr, addr, sizeof(*addr));
+	info->addrs[i].ts = now;
 }
 
 void announce_track_msg(const union xsockaddr *addr, size_t addrlen,

          
@@ 397,7 398,7 @@ void announce_track_cleanup(void)
 	while ((inst = rb_destroy_nodes(&instances, &cookie))) {
 		MXDESTROY(&inst->lock);
 
-		free(inst->addrs);
+		free(inst->info.addrs);
 		free(inst);
 	}
 

          
M rpc/include/hlog-rpc/announce-sub.h +10 -0
@@ 23,8 23,15 @@ 
 #ifndef __RPC_ANNOUNCE_SUB_H
 #define __RPC_ANNOUNCE_SUB_H
 
+#include <jeffpc/sock.h>
+
 #include <hlog-rpc/announce.h>
 
+struct instance_addr {
+	union xsockaddr addr;
+	uint64_t ts;
+};
+
 struct instance_info {
 	uint64_t last_rx_ts; /* our time @ rx of last announcement */
 	uint32_t id;

          
@@ 55,6 62,9 @@ struct instance_info {
 		enum hrig_mode tx_mode;
 		enum hrig_mode rx_mode;
 	} rig;
+
+	size_t num_addrs;
+	struct instance_addr *addrs; /* announcement addresses & times */
 };
 
 extern int announce_connect(void (*all)(const struct instance_info *),