You cannot possible compile the LSR (or the LDP) lib without a porting
[mpls-ldp-portable.git] / ldp / ldp_outlabel.c
blob6c645feb911b61861eeb79c9b12e7f37a75f26ef
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 "ldp_struct.h"
12 #include "ldp_addr.h"
13 #include "ldp_attr.h"
14 #include "ldp_outlabel.h"
15 #include "ldp_inlabel.h"
16 #include "ldp_session.h"
17 #include "ldp_tunnel.h"
18 #include "ldp_global.h"
20 #include "mpls_mm_impl.h"
21 #include "mpls_trace_impl.h"
23 static uint32_t _ldp_outlabel_next_index = 1;
25 ldp_outlabel *ldp_outlabel_create()
27 ldp_outlabel *o = (ldp_outlabel *) mpls_malloc(sizeof(ldp_outlabel));
29 if (o) {
30 memset(o, 0, sizeof(ldp_outlabel));
31 MPLS_REFCNT_INIT(o, 0);
32 MPLS_LIST_INIT(&o->inlabel_root, ldp_inlabel);
33 MPLS_LIST_INIT(&o->tunnel_root, ldp_tunnel);
34 MPLS_LIST_ELEM_INIT(o, _global);
35 MPLS_LIST_ELEM_INIT(o, _session);
36 o->index = _ldp_outlabel_get_next_index();
37 o->info.label.type = MPLS_LABEL_TYPE_NONE;
38 o->switching = MPLS_BOOL_FALSE;
40 return o;
43 ldp_outlabel *ldp_outlabel_create_complete(ldp_global * g, ldp_session * s,
44 ldp_addr * nh, ldp_attr * a)
46 ldp_outlabel *out = ldp_outlabel_create();
47 /* JLEU: for now pick the first adj */
48 ldp_adj *adj = MPLS_LIST_HEAD(&s->adj_root);
49 ldp_entity *e = adj->entity;
51 if (out != NULL) {
52 ldp_outlabel_add_nexthop(out, nh);
53 ldp_session_add_outlabel(s, out);
55 out->info.push_label = MPLS_BOOL_TRUE;
56 out->info.owner = MPLS_OWNER_LDP;
58 memcpy(&out->info.nexthop.ip, &nh->address, sizeof(mpls_inet_addr));
60 memcpy(&out->info.nexthop.if_handle,
61 &e->p.iff->handle, sizeof(mpls_if_handle));
62 out->info.nexthop.type = MPLS_NH_IP | MPLS_NH_IF;
64 ldp_attr2mpls_label_struct(a, &out->info.label);
66 _ldp_global_add_outlabel(g, out);
68 ldp_attr_add_outlabel(a, out);
70 return out;
73 void ldp_outlabel_delete(ldp_outlabel * o)
75 LDP_PRINT(g->user_data,"outlabel delete\n");
76 MPLS_REFCNT_ASSERT(o, 0);
77 mpls_free(o);
80 void ldp_outlabel_delete_complete(ldp_global * g, ldp_outlabel * out)
82 ldp_attr_del_outlabel(out->attr);
83 if (out->session) {
84 ldp_session_del_outlabel(out->session, out);
86 _ldp_global_del_outlabel(g, out);
87 ldp_outlabel_del_nexthop(out);
90 mpls_return_enum _ldp_outlabel_add_inlabel(ldp_outlabel * o, ldp_inlabel * i)
92 if (o && i) {
93 MPLS_REFCNT_HOLD(i);
94 o->merge_count++;
95 MPLS_LIST_ADD_HEAD(&o->inlabel_root, i, _outlabel, ldp_inlabel);
96 return MPLS_SUCCESS;
98 return MPLS_FAILURE;
101 mpls_return_enum _ldp_outlabel_del_inlabel(ldp_outlabel * o, ldp_inlabel * i)
103 if (o && i) {
104 MPLS_LIST_REMOVE(&o->inlabel_root, i, _outlabel);
105 o->merge_count--;
106 MPLS_REFCNT_RELEASE(i, ldp_inlabel_delete);
107 return MPLS_SUCCESS;
109 return MPLS_FAILURE;
112 mpls_return_enum _ldp_outlabel_add_attr(ldp_outlabel * o, ldp_attr * a)
114 if (o && a) {
115 MPLS_REFCNT_HOLD(a);
116 o->attr = a;
117 return MPLS_SUCCESS;
119 return MPLS_FAILURE;
122 mpls_return_enum _ldp_outlabel_del_attr(ldp_outlabel * o)
124 if (o && o->attr) {
125 MPLS_REFCNT_RELEASE(o->attr, ldp_attr_delete);
126 o->attr = NULL;
127 return MPLS_SUCCESS;
129 return MPLS_FAILURE;
132 mpls_return_enum ldp_outlabel_add_nexthop(ldp_outlabel * o, ldp_addr * nh)
134 if (o && nh) {
135 MPLS_REFCNT_HOLD(nh);
136 o->addr = nh;
137 return MPLS_SUCCESS;
139 return MPLS_FAILURE;
142 mpls_return_enum ldp_outlabel_del_nexthop(ldp_outlabel * o)
144 if (o && o->addr) {
145 MPLS_REFCNT_RELEASE(o->addr, ldp_addr_delete);
146 o->addr = NULL;
147 return MPLS_SUCCESS;
149 return MPLS_FAILURE;
152 mpls_return_enum _ldp_outlabel_add_session(ldp_outlabel * o, ldp_session * s)
154 if (o && s) {
155 MPLS_REFCNT_HOLD(s);
156 o->session = s;
157 return MPLS_SUCCESS;
159 return MPLS_FAILURE;
162 mpls_return_enum _ldp_outlabel_del_session(ldp_outlabel * o)
164 if (o && o->session) {
165 MPLS_REFCNT_RELEASE(o->session, ldp_session_delete);
166 o->session = NULL;
167 return MPLS_SUCCESS;
169 return MPLS_FAILURE;
172 uint32_t _ldp_outlabel_get_next_index()
174 uint32_t retval = _ldp_outlabel_next_index;
176 _ldp_outlabel_next_index++;
177 if (retval > _ldp_outlabel_next_index) {
178 _ldp_outlabel_next_index = 1;
180 return retval;
183 mpls_return_enum _ldp_outlabel_add_tunnel(ldp_outlabel * o, ldp_tunnel * t)
185 if (o && t) {
186 MPLS_REFCNT_HOLD(t);
187 o->merge_count++;
188 MPLS_LIST_ADD_HEAD(&o->tunnel_root, t, _outlabel, ldp_tunnel);
189 return MPLS_SUCCESS;
191 return MPLS_FAILURE;
194 mpls_return_enum _ldp_outlabel_del_tunnel(ldp_outlabel * o, ldp_tunnel * t)
196 if (o && t) {
197 MPLS_LIST_REMOVE(&o->tunnel_root, t, _outlabel);
198 o->merge_count--;
199 MPLS_REFCNT_RELEASE(t, ldp_tunnel_delete);
200 return MPLS_SUCCESS;
202 return MPLS_FAILURE;