Ability to walk global addr list
[mpls-ldp-portable.git] / ldp / ldp_addr.c
blob10ddb28432df4378eef52d37953ad1b89d0e6dd0
2 /*
3 * Copyright (C) James R. Leu 2000
4 * jleu@mindspring.com
6 * This software is covered under the LGPL, for more
7 * info check out http://www.gnu.org/copyleft/lgpl.html
8 */
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/socket.h>
13 #include "ldp_struct.h"
14 #include "ldp_global.h"
15 #include "ldp_session.h"
16 #include "ldp_entity.h"
17 #include "ldp_pdu_setup.h"
18 #include "ldp_addr.h"
19 #include "ldp_nexthop.h"
20 #include "ldp_if.h"
21 #include "ldp_buf.h"
22 #include "ldp_mesg.h"
23 #include "mpls_list.h"
24 #include "mpls_ifmgr_impl.h"
25 #include "mpls_mm_impl.h"
26 #include "mpls_trace_impl.h"
27 #include "mpls_tree_impl.h"
29 static uint32_t _ldp_addr_next_index = 1;
31 ldp_addr *ldp_addr_create(ldp_global *g, mpls_inet_addr * address)
33 ldp_addr *a = (ldp_addr *) mpls_malloc(sizeof(ldp_addr));
35 if (a) {
37 * note: this is init to 1 for a reason!
38 * We're placing it in the global list, so this is our refcnt
39 * when this refcnt gets to zero, it will be removed from the
40 * global list and deleted
42 MPLS_REFCNT_INIT(a, 1);
43 mpls_link_list_init(&a->session_root);
44 MPLS_LIST_INIT(&a->nh_root, ldp_nexthop);
45 MPLS_LIST_ELEM_INIT(a, _global);
46 MPLS_LIST_ELEM_INIT(a, _if);
47 memcpy(&a->address, address, sizeof(mpls_inet_addr));
48 a->index = _ldp_addr_get_next_index();
49 _ldp_global_add_addr(g, a);
51 return a;
54 void ldp_addr_delete(ldp_global *g, ldp_addr * a)
56 LDP_PRINT(g->user_data,"addr delete\n");
57 MPLS_REFCNT_ASSERT(a, 0);
58 _ldp_global_del_addr(g, a);
59 mpls_free(a);
62 ldp_addr *ldp_addr_find(ldp_global *g, mpls_inet_addr * address)
64 ldp_addr *addr = NULL;
66 if (mpls_tree_get(g->addr_tree, address->u.ipv4, 32, (void **)&addr) !=
67 MPLS_SUCCESS) {
68 return NULL;
70 return addr;
73 ldp_addr *ldp_addr_insert(ldp_global *g, mpls_inet_addr * address)
75 ldp_addr *addr = NULL;
77 if ((addr = ldp_addr_create(g, address)) == NULL) {
78 LDP_PRINT(g->user_data, "ldp_addr_insert: error creating address\n");
79 return NULL;
82 if (ldp_addr_insert2(g, addr) != MPLS_SUCCESS) {
83 LDP_PRINT(g->user_data, "ldp_addr_insert: error adding addr\n");
84 return NULL;
86 return addr;
89 mpls_return_enum ldp_addr_insert2(ldp_global *g, ldp_addr *addr)
91 MPLS_REFCNT_HOLD(addr);
92 if (mpls_tree_insert(g->addr_tree, addr->address.u.ipv4, 32, (void *)addr) !=
93 MPLS_SUCCESS) {
94 LDP_PRINT(g->user_data, "ldp_addr_insert2: error adding addr\n");
95 MPLS_REFCNT_RELEASE2(g, addr, ldp_addr_delete);
96 return MPLS_FATAL;
98 return MPLS_SUCCESS;
101 void ldp_addr_remove(ldp_global *g, mpls_inet_addr * address)
103 ldp_addr *addr = NULL;
105 if (mpls_tree_remove(g->addr_tree, address->u.ipv4, 32, (void **)&addr) ==
106 MPLS_SUCCESS) {
107 MPLS_REFCNT_RELEASE2(g, addr, ldp_addr_delete);
111 void ldp_addr_add_if(ldp_addr * a, ldp_if * i)
113 MPLS_ASSERT(a && i);
114 MPLS_REFCNT_HOLD(i);
115 a->iff = i;
118 void ldp_addr_del_if(ldp_global *g, ldp_addr * a)
120 MPLS_ASSERT(a);
121 MPLS_REFCNT_RELEASE2(g, a->iff, ldp_if_delete);
122 a->iff = NULL;
125 mpls_bool ldp_addr_is_empty(ldp_addr *a)
127 if (a->iff == NULL && MPLS_LIST_EMPTY(&a->nh_root) &&
128 MPLS_LIST_EMPTY(&a->session_root)) {
129 return MPLS_BOOL_TRUE;
131 return MPLS_BOOL_FALSE;
134 mpls_return_enum _ldp_addr_add_session(ldp_addr * a, ldp_session * s)
136 MPLS_ASSERT(a && s);
137 MPLS_REFCNT_HOLD(s);
138 if (mpls_link_list_add_tail(&a->session_root, s) == MPLS_SUCCESS) {
139 return MPLS_SUCCESS;
141 MPLS_REFCNT_RELEASE(s, ldp_session_delete);
142 return MPLS_FAILURE;
145 void _ldp_addr_del_session(ldp_addr * a, ldp_session * s)
147 MPLS_ASSERT(a && s);
148 mpls_link_list_remove_data(&a->session_root, s);
149 MPLS_REFCNT_RELEASE(s, ldp_session_delete);
152 void ldp_addr_add_nexthop(ldp_addr * a, ldp_nexthop * nh)
154 ldp_nexthop *np = NULL;
156 MPLS_ASSERT(a && nh);
157 MPLS_REFCNT_HOLD(nh);
159 ldp_nexthop_add_addr(nh,a);
161 np = MPLS_LIST_HEAD(&a->nh_root);
162 while (np != NULL) {
163 if (np->index > nh->index) {
164 MPLS_LIST_INSERT_BEFORE(&a->nh_root, np, nh, _addr);
165 return;
167 np = MPLS_LIST_NEXT(&a->nh_root, np, _addr);
169 MPLS_LIST_ADD_TAIL(&a->nh_root, nh, _addr, ldp_nexthop);
172 void ldp_addr_del_nexthop(ldp_global *g, ldp_addr * a, ldp_nexthop * nh)
174 MPLS_ASSERT(a && nh);
175 MPLS_LIST_REMOVE(&a->nh_root, nh, _addr);
176 ldp_nexthop_del_addr(g, nh);
177 MPLS_REFCNT_RELEASE(nh, ldp_nexthop_delete);
180 uint32_t _ldp_addr_get_next_index()
182 uint32_t retval = _ldp_addr_next_index;
184 _ldp_addr_next_index++;
185 if (retval > _ldp_addr_next_index) {
186 _ldp_addr_next_index = 1;
188 return retval;
191 void ldp_addr_mesg_prepare(ldp_mesg * msg, ldp_global * g, uint32_t msgid,
192 mpls_inet_addr * addr)
194 MPLS_MSGPTR(Adr);
196 LDP_ENTER(g->user_data, "ldp_addr_mesg_prepare");
198 ldp_mesg_prepare(msg, MPLS_ADDR_MSGTYPE, msgid);
199 MPLS_MSGPARAM(Adr) = &msg->u.addr;
201 MPLS_MSGPARAM(Adr)->adrListTlvExists = 1;
202 MPLS_MSGPARAM(Adr)->baseMsg.msgLength +=
203 setupAddrTlv(&(MPLS_MSGPARAM(Adr)->addressList));
205 MPLS_MSGPARAM(Adr)->baseMsg.msgLength +=
206 addAddrElem2AddrTlv(&(MPLS_MSGPARAM(Adr)->addressList), addr->u.ipv4);
208 LDP_EXIT(g->user_data, "ldp_addr_mesg_prepare");
211 void ldp_waddr_mesg_prepare(ldp_mesg * msg, ldp_global * g, uint32_t msgid,
212 mpls_inet_addr * addr)
214 MPLS_MSGPTR(Adr);
216 LDP_ENTER(g->user_data, "ldp_waddr_mesg_prepare");
218 ldp_mesg_prepare(msg, MPLS_ADDRWITH_MSGTYPE, msgid);
219 MPLS_MSGPARAM(Adr) = &msg->u.addr;
221 MPLS_MSGPARAM(Adr)->adrListTlvExists = 1;
222 MPLS_MSGPARAM(Adr)->baseMsg.msgLength +=
223 setupAddrTlv(&(MPLS_MSGPARAM(Adr)->addressList));
225 MPLS_MSGPARAM(Adr)->baseMsg.msgLength +=
226 addAddrElem2AddrTlv(&(MPLS_MSGPARAM(Adr)->addressList), addr->u.ipv4);
228 LDP_EXIT(g->user_data, "ldp_waddr_mesg_prepare");
231 mpls_return_enum ldp_addr_send(ldp_global * g, ldp_session * s,
232 mpls_inet_addr * a)
234 mpls_return_enum result = MPLS_FAILURE;
236 MPLS_ASSERT(s && a);
238 LDP_ENTER(g->user_data, "ldp_addr_send");
240 ldp_addr_mesg_prepare(s->tx_message, g, g->message_identifier++, a);
242 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_SEND, LDP_TRACE_FLAG_ADDRESS,
243 "Addr Send: session(%d)\n", s->index);
245 result = ldp_mesg_send_tcp(g, s, s->tx_message);
247 LDP_EXIT(g->user_data, "ldp_addr_send");
249 return result;
252 mpls_return_enum ldp_waddr_send(ldp_global * g, ldp_session * s,
253 mpls_inet_addr * a)
255 mpls_return_enum result = MPLS_FAILURE;
257 MPLS_ASSERT(s && a);
259 LDP_ENTER(g->user_data, "ldp_waddr_send");
261 ldp_waddr_mesg_prepare(s->tx_message, g, g->message_identifier++, a);
263 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_SEND, LDP_TRACE_FLAG_ADDRESS,
264 "Addr Withdraw Send: session(%d)\n", s->index);
266 result = ldp_mesg_send_tcp(g, s, s->tx_message);
268 LDP_EXIT(g->user_data, "ldp_waddr_send");
270 return result;
273 mpls_return_enum ldp_addr_process(ldp_global * g, ldp_session * s,
274 ldp_entity * e, ldp_mesg * msg)
276 mplsLdpAdrMsg_t *body = &msg->u.addr;
277 mpls_inet_addr inet;
278 ldp_addr *addr = NULL;
279 ldp_nexthop *nh = NULL;
280 ldp_fec *fec = NULL;
281 int len = (body->addressList.baseTlv.length - MPLS_ADDFAMFIXLEN) /
282 MPLS_IPv4LEN;
283 int i;
285 LDP_ENTER(g->user_data, "ldp_addr_process");
287 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_RECV, LDP_TRACE_FLAG_ADDRESS,
288 "Addr Recv: session(%d)\n", s->index);
290 for (i = 0; i < len; i++) {
291 inet.type = MPLS_FAMILY_IPV4;
292 inet.u.ipv4 = body->addressList.address[i];
294 if (msg->u.generic.flags.flags.msgType == MPLS_ADDR_MSGTYPE) {
295 if (!(addr = ldp_addr_find(g, &inet))) {
296 /* it's not in the tree, put it there! */
297 if ((addr = ldp_addr_insert(g, &inet)) == NULL) {
298 LDP_PRINT(g->user_data, "ldp_addr_process: error adding addr\n");
299 goto ldp_addr_process_end;
303 /* the addr is in the tree */
304 if (ldp_session_add_addr(g, s, addr) == MPLS_FAILURE) {
305 LDP_PRINT(g->user_data,
306 "ldp_addr_process: error adding address to session\n");
307 return MPLS_FAILURE;
310 nh = MPLS_LIST_HEAD(&addr->nh_root);
311 while (nh != NULL) {
312 fec = nh->fec;
313 /* create cross connect */
314 /* send label mapping */
315 nh = MPLS_LIST_NEXT(&addr->nh_root, nh, _addr);
317 } else {
318 /* addr withdrawl */
319 if ((addr = ldp_addr_find(g, &inet))) {
320 nh = MPLS_LIST_HEAD(&addr->nh_root);
321 while (fec != NULL) {
322 /* send label withdrawl */
323 /* delete cross connect */
324 nh = MPLS_LIST_NEXT(&addr->nh_root, nh, _addr);
327 ldp_session_del_addr(g, s, addr);
332 LDP_EXIT(g->user_data, "ldp_addr_process");
334 return MPLS_SUCCESS;
336 ldp_addr_process_end:
338 LDP_EXIT(g->user_data, "ldp_addr_process-error");
340 return MPLS_FAILURE;