3 * Copyright (C) James R. Leu 2001
6 * This software is covered under the LGPL, for more
7 * info check out http://www.gnu.org/copyleft/lgpl.html
10 #include "ldp_struct.h"
11 #include "ldp_hop_list.h"
13 #include "ldp_tunnel.h"
15 #include "mpls_assert.h"
16 #include "mpls_mm_impl.h"
17 #include "mpls_trace_impl.h"
19 static uint32_t _ldp_hop_list_next_index
= 1;
21 ldp_hop_list
*ldp_hop_list_create()
23 ldp_hop_list
*h
= (ldp_hop_list
*) mpls_malloc(sizeof(ldp_hop_list
));
26 memset(h
, 0, sizeof(ldp_hop_list
));
27 MPLS_REFCNT_INIT(h
, 0);
28 MPLS_LIST_ELEM_INIT(h
, _global
);
29 MPLS_LIST_INIT(&h
->hop
, ldp_hop
);
31 h
->index
= _ldp_hop_list_get_next_index();
36 void ldp_hop_list_delete(ldp_hop_list
* h
)
38 // LDP_PRINT(g->user_data,"hop_list delete\n");
39 MPLS_REFCNT_ASSERT(h
, 0);
43 uint32_t _ldp_hop_list_get_next_index()
45 uint32_t retval
= _ldp_hop_list_next_index
;
47 _ldp_hop_list_next_index
++;
48 if (retval
> _ldp_hop_list_next_index
) {
49 _ldp_hop_list_next_index
= 1;
54 mpls_return_enum
ldp_hop_list_find_hop_index(ldp_hop_list
* hl
, uint32_t index
,
59 if (hl
&& index
> 0) {
60 /* because we sort our inserts by index, this lets us know
61 if we've "walked" past the end of the list */
63 h
= MPLS_LIST_TAIL(&hl
->hop
);
64 if (h
== NULL
|| h
->index
< index
) {
66 return MPLS_END_OF_LIST
;
69 h
= MPLS_LIST_HEAD(&hl
->hop
);
71 if (h
->index
== index
) {
75 h
= MPLS_LIST_NEXT(&hl
->hop
, h
, _hop_list
);
82 mpls_return_enum
ldp_hop_list_add_hop(ldp_hop_list
* hl
, ldp_hop
* h
)
88 hp
= MPLS_LIST_HEAD(&hl
->hop
);
90 if (hp
->index
> h
->index
) {
91 MPLS_LIST_INSERT_BEFORE(&hl
->hop
, hp
, h
, _hop_list
);
92 _ldp_hop_add_hop_list(h
, hl
);
95 hp
= MPLS_LIST_NEXT(&hl
->hop
, hp
, _hop_list
);
97 MPLS_LIST_ADD_TAIL(&hl
->hop
, h
, _hop_list
, ldp_hop
);
98 _ldp_hop_add_hop_list(h
, hl
);
104 mpls_return_enum
ldp_hop_list_del_hop(ldp_hop_list
* hl
, ldp_hop
* h
)
107 MPLS_LIST_REMOVE(&hl
->hop
, h
, _hop_list
);
108 _ldp_hop_del_hop_list(h
);
109 MPLS_REFCNT_RELEASE(h
, ldp_hop_delete
);
115 mpls_return_enum
_ldp_hop_list_add_tunnel(ldp_hop_list
* h
, ldp_tunnel
* t
)
125 mpls_return_enum
_ldp_hop_list_del_tunnel(ldp_hop_list
* h
)
127 if (h
&& h
->tunnel
) {
128 MPLS_REFCNT_RELEASE(h
->tunnel
, ldp_tunnel_delete
);