Added comments to clarify the relationships
[mpls-ldp-portable.git] / ldp / ldp_mesg.c
blobf445edf69473f91ec6c3ca62a8660c2116962d07
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 <stdio.h>
11 #include <sys/socket.h>
12 #include "ldp_struct.h"
13 #include "ldp_mesg.h"
14 #include "ldp_buf.h"
16 #include "mpls_assert.h"
17 #include "mpls_mm_impl.h"
18 #include "mpls_socket_impl.h"
19 #include "mpls_trace_impl.h"
21 mpls_return_enum ldp_mesg_send_tcp(ldp_global * g, ldp_session * s,
22 ldp_mesg * msg)
24 int32_t result = 0;
26 MPLS_ASSERT(s);
28 result = ldp_encode_one_mesg(g, g->lsr_identifier.u.ipv4,
29 s->cfg_label_space, s->tx_buffer, msg);
31 if (result <= 0)
32 return MPLS_FAILURE;
34 s->mesg_tx++;
36 result = mpls_socket_tcp_write(g->socket_handle, s->socket,
37 s->tx_buffer->buffer, s->tx_buffer->size);
39 if (result <= 0) {
40 LDP_PRINT(g->user_data, "send failed(%d)\n", result);
41 perror("send");
42 return MPLS_FAILURE;
44 return MPLS_SUCCESS;
47 mpls_return_enum ldp_mesg_send_udp(ldp_global * g, ldp_entity * e,
48 ldp_mesg * msg)
50 ldp_buf *buf = NULL;
51 mpls_dest *dest = NULL;
52 int32_t result = 0;
53 uint16_t label_space = 0;
55 MPLS_ASSERT(e);
57 switch (e->entity_type) {
58 case LDP_DIRECT:
59 MPLS_ASSERT(e->p.iff != NULL);
60 if (mpls_socket_multicast_if_tx(g->socket_handle, g->hello_socket,
61 e->p.iff) == MPLS_FAILURE) {
62 LDP_PRINT(g->user_data, "ldp_mesg_send_udp: muticast tx error(%d)\n",
63 mpls_socket_get_errno(g->socket_handle, g->hello_socket));
64 return MPLS_FAILURE;
66 dest = &e->p.iff->dest;
67 buf = e->p.iff->tx_buffer;
68 label_space = e->p.iff->label_space;
69 break;
70 case LDP_INDIRECT:
71 MPLS_ASSERT(e->p.peer != NULL);
72 dest = &e->p.peer->dest;
73 buf = e->p.peer->tx_buffer;
74 label_space = e->p.peer->label_space;
75 break;
76 default:
77 MPLS_ASSERT(0);
79 result =
80 ldp_encode_one_mesg(g, g->lsr_identifier.u.ipv4, label_space, buf, msg);
82 if (result <= 0)
83 return MPLS_FAILURE;
85 e->mesg_tx++;
87 result = mpls_socket_udp_sendto(g->socket_handle, g->hello_socket,
88 buf->buffer, buf->size, dest);
90 switch (e->entity_type) {
91 case LDP_DIRECT:
92 mpls_socket_multicast_if_tx(g->socket_handle, g->hello_socket, NULL);
93 break;
94 case LDP_INDIRECT:
95 break;
96 default:
97 MPLS_ASSERT(0);
100 if (result <= 0) {
101 LDP_PRINT(g->user_data, "sendto failed(%d)\n", result);
102 perror("sendto");
103 return MPLS_FAILURE;
105 return MPLS_SUCCESS;
108 ldp_mesg *ldp_mesg_create()
110 ldp_mesg *msg = (ldp_mesg *) mpls_malloc(sizeof(ldp_mesg));
112 if (!msg) {
113 return NULL;
115 return msg;
118 void ldp_mesg_prepare(ldp_mesg * msg, uint16_t type, uint32_t id)
120 memset(msg, 0, sizeof(ldp_mesg));
122 msg->u.generic.flags.flags.msgType = type;
123 msg->u.generic.msgId = id;
124 msg->u.generic.msgLength = MPLS_MSGIDFIXLEN;
127 void ldp_mesg_delete(ldp_mesg * msg)
129 MPLS_ASSERT(msg);
130 mpls_free(msg);
133 void ldp_mesg_hdr_get_lsraddr(ldp_mesg * msg, mpls_inet_addr * lsraddr)
135 MPLS_ASSERT(msg && lsraddr);
137 lsraddr->type = MPLS_FAMILY_IPV4;
138 lsraddr->u.ipv4 = msg->header.lsrAddress;
141 void ldp_mesg_hdr_get_labelspace(ldp_mesg * msg, int *labelspace)
143 MPLS_ASSERT(msg && labelspace);
144 *labelspace = msg->header.labelSpace;
147 uint16_t ldp_mesg_get_type(ldp_mesg * msg)
149 MPLS_ASSERT(msg);
150 return msg->u.generic.flags.flags.msgType;
153 mpls_return_enum ldp_mesg_hello_get_traddr(ldp_mesg * msg,
154 mpls_inet_addr * traddr)
156 MPLS_MSGPTR(Hello);
157 MPLS_ASSERT(msg && traddr);
159 MPLS_MSGPARAM(Hello) = &msg->u.hello;
160 if (!MPLS_MSGPARAM(Hello)->trAdrTlvExists)
161 return MPLS_FAILURE;
163 traddr->type = MPLS_FAMILY_IPV4;
164 traddr->u.ipv4 = MPLS_MSGPARAM(Hello)->trAdr.address;
166 return MPLS_SUCCESS;
169 mpls_return_enum ldp_mesg_hello_get_hellotime(ldp_mesg * msg, int *hellotime)
171 MPLS_MSGPTR(Hello);
172 MPLS_ASSERT(msg && hellotime);
174 MPLS_MSGPARAM(Hello) = &msg->u.hello;
175 if (!MPLS_MSGPARAM(Hello)->chpTlvExists) {
176 fprintf(stderr, "No chp!\n");
177 return MPLS_FAILURE;
180 *hellotime = MPLS_MSGPARAM(Hello)->chp.holdTime;
182 return MPLS_SUCCESS;
185 mpls_return_enum ldp_mesg_hello_get_csn(ldp_mesg * msg, uint32_t * csn)
187 MPLS_MSGPTR(Hello);
188 MPLS_ASSERT(msg && csn);
190 MPLS_MSGPARAM(Hello) = &msg->u.hello;
191 if (!MPLS_MSGPARAM(Hello)->csnTlvExists)
192 return MPLS_FAILURE;
194 *csn = MPLS_MSGPARAM(Hello)->csn.seqNumber;
196 return MPLS_SUCCESS;
199 mpls_return_enum ldp_mesg_hello_get_request(ldp_mesg * msg, int *req)
201 MPLS_MSGPTR(Hello);
202 MPLS_ASSERT(msg && req);
204 MPLS_MSGPARAM(Hello) = &msg->u.hello;
206 *req = MPLS_MSGPARAM(Hello)->chp.flags.flags.request;
208 return MPLS_SUCCESS;
211 mpls_return_enum ldp_mesg_hello_get_targeted(ldp_mesg * msg, int *tar)
213 MPLS_MSGPTR(Hello);
214 MPLS_ASSERT(msg && tar);
216 MPLS_MSGPARAM(Hello) = &msg->u.hello;
218 *tar = MPLS_MSGPARAM(Hello)->chp.flags.flags.target;
220 return MPLS_SUCCESS;