1 /*
2 * scamper_neighbourdisc.c
3 *
4 * $Id: scamper_neighbourdisc.c,v 1.6 2021/08/24 09:03:07 mjl Exp $
5 *
6 * Copyright (C) 2009-2021 Matthew Luckie
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, version 2.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "internal.h"
27
28 #include "scamper_list.h"
29 #include "scamper_addr.h"
30 #include "scamper_neighbourdisc.h"
31
32 #include "utils.h"
33
scamper_neighbourdisc_addr(const void * va)34 scamper_addr_t *scamper_neighbourdisc_addr(const void *va)
35 {
36 const scamper_neighbourdisc_t *nd = va;
37 return nd->dst_ip;
38 }
39
scamper_neighbourdisc_reply_alloc(void)40 scamper_neighbourdisc_reply_t *scamper_neighbourdisc_reply_alloc(void)
41 {
42 return malloc_zero(sizeof(scamper_neighbourdisc_reply_t));
43 }
44
scamper_neighbourdisc_reply_free(scamper_neighbourdisc_reply_t * reply)45 void scamper_neighbourdisc_reply_free(scamper_neighbourdisc_reply_t *reply)
46 {
47 if(reply->mac != NULL) scamper_addr_free(reply->mac);
48 free(reply);
49 return;
50 }
51
scamper_neighbourdisc_reply_add(scamper_neighbourdisc_probe_t * probe,scamper_neighbourdisc_reply_t * reply)52 int scamper_neighbourdisc_reply_add(scamper_neighbourdisc_probe_t *probe,
53 scamper_neighbourdisc_reply_t *reply)
54 {
55 size_t len = sizeof(scamper_neighbourdisc_reply_t *) * (probe->rxc+1);
56 if(realloc_wrap((void **)&probe->rxs, len) != 0)
57 return -1;
58 probe->rxs[probe->rxc++] = reply;
59 return 0;
60 }
61
scamper_neighbourdisc_replies_alloc(scamper_neighbourdisc_probe_t * probe,uint16_t c)62 int scamper_neighbourdisc_replies_alloc(scamper_neighbourdisc_probe_t *probe,
63 uint16_t c)
64 {
65 size_t len = sizeof(scamper_neighbourdisc_reply_t *) * c;
66 if((probe->rxs = malloc_zero(len)) == NULL)
67 return -1;
68 return 0;
69 }
70
scamper_neighbourdisc_probe_alloc(void)71 scamper_neighbourdisc_probe_t *scamper_neighbourdisc_probe_alloc(void)
72 {
73 return malloc_zero(sizeof(scamper_neighbourdisc_probe_t));
74 }
75
scamper_neighbourdisc_probe_free(scamper_neighbourdisc_probe_t * probe)76 void scamper_neighbourdisc_probe_free(scamper_neighbourdisc_probe_t *probe)
77 {
78 uint16_t i;
79
80 if(probe == NULL)
81 return;
82
83 if(probe->rxs != NULL)
84 {
85 for(i=0; i<probe->rxc; i++)
86 if(probe->rxs[i] != NULL)
87 scamper_neighbourdisc_reply_free(probe->rxs[i]);
88 free(probe->rxs);
89 }
90 free(probe);
91 return;
92 }
93
scamper_neighbourdisc_probe_add(scamper_neighbourdisc_t * nd,scamper_neighbourdisc_probe_t * probe)94 int scamper_neighbourdisc_probe_add(scamper_neighbourdisc_t *nd,
95 scamper_neighbourdisc_probe_t *probe)
96 {
97 size_t len = sizeof(scamper_neighbourdisc_probe_t *) * (nd->probec+1);
98 if(realloc_wrap((void **)&nd->probes, len) != 0)
99 return -1;
100 nd->probes[nd->probec++] = probe;
101 return 0;
102 }
103
scamper_neighbourdisc_probes_alloc(scamper_neighbourdisc_t * nd,uint16_t c)104 int scamper_neighbourdisc_probes_alloc(scamper_neighbourdisc_t *nd, uint16_t c)
105 {
106 size_t len = sizeof(scamper_neighbourdisc_probe_t *) * c;
107 if((nd->probes = malloc_zero(len)) == NULL)
108 return -1;
109 return 0;
110 }
111
scamper_neighbourdisc_alloc()112 scamper_neighbourdisc_t *scamper_neighbourdisc_alloc()
113 {
114 size_t len = sizeof(scamper_neighbourdisc_t);
115 return (scamper_neighbourdisc_t *)malloc_zero(len);
116 }
117
scamper_neighbourdisc_ifname_set(scamper_neighbourdisc_t * nd,char * ifname)118 int scamper_neighbourdisc_ifname_set(scamper_neighbourdisc_t *nd, char *ifname)
119 {
120 if(nd->ifname != NULL)
121 free(nd->ifname);
122
123 if((nd->ifname = strdup(ifname)) == NULL)
124 return -1;
125
126 return 0;
127 }
128
scamper_neighbourdisc_free(scamper_neighbourdisc_t * nd)129 void scamper_neighbourdisc_free(scamper_neighbourdisc_t *nd)
130 {
131 uint16_t i;
132
133 if(nd == NULL)
134 return;
135
136 if(nd->probes != NULL)
137 {
138 for(i=0; i<nd->probec; i++)
139 scamper_neighbourdisc_probe_free(nd->probes[i]);
140 free(nd->probes);
141 }
142
143 if(nd->ifname != NULL) free(nd->ifname);
144 if(nd->dst_mac != NULL) scamper_addr_free(nd->dst_mac);
145 if(nd->dst_ip != NULL) scamper_addr_free(nd->dst_ip);
146 if(nd->src_mac != NULL) scamper_addr_free(nd->src_mac);
147 if(nd->src_ip != NULL) scamper_addr_free(nd->src_ip);
148 if(nd->cycle != NULL) scamper_cycle_free(nd->cycle);
149 if(nd->list != NULL) scamper_list_free(nd->list);
150
151 free(nd);
152 return;
153 }
154