3 * Copyright (C) James R. Leu 2000
6 * This software is covered under the LGPL, for more
7 * info check out http://www.gnu.org/copyleft/lgpl.html
11 #include <sys/socket.h>
12 #include "ldp_struct.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
,
28 result
= ldp_encode_one_mesg(g
, g
->lsr_identifier
.u
.ipv4
,
29 s
->cfg_label_space
, s
->tx_buffer
, msg
);
36 result
= mpls_socket_tcp_write(g
->socket_handle
, s
->socket
,
37 s
->tx_buffer
->buffer
, s
->tx_buffer
->size
);
40 LDP_PRINT(g
->user_data
, "send failed(%d)\n", result
);
47 mpls_return_enum
ldp_mesg_send_udp(ldp_global
* g
, ldp_entity
* e
,
51 mpls_dest
*dest
= NULL
;
53 uint16_t label_space
= 0;
57 switch (e
->entity_type
) {
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
));
66 dest
= &e
->p
.iff
->dest
;
67 buf
= e
->p
.iff
->tx_buffer
;
68 label_space
= e
->p
.iff
->label_space
;
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
;
80 ldp_encode_one_mesg(g
, g
->lsr_identifier
.u
.ipv4
, label_space
, buf
, msg
);
87 result
= mpls_socket_udp_sendto(g
->socket_handle
, g
->hello_socket
,
88 buf
->buffer
, buf
->size
, dest
);
90 switch (e
->entity_type
) {
92 mpls_socket_multicast_if_tx(g
->socket_handle
, g
->hello_socket
, NULL
);
101 LDP_PRINT(g
->user_data
, "sendto failed(%d)\n", result
);
108 ldp_mesg
*ldp_mesg_create()
110 ldp_mesg
*msg
= (ldp_mesg
*) mpls_malloc(sizeof(ldp_mesg
));
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
)
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
)
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
)
157 MPLS_ASSERT(msg
&& traddr
);
159 MPLS_MSGPARAM(Hello
) = &msg
->u
.hello
;
160 if (!MPLS_MSGPARAM(Hello
)->trAdrTlvExists
)
163 traddr
->type
= MPLS_FAMILY_IPV4
;
164 traddr
->u
.ipv4
= MPLS_MSGPARAM(Hello
)->trAdr
.address
;
169 mpls_return_enum
ldp_mesg_hello_get_hellotime(ldp_mesg
* msg
, int *hellotime
)
172 MPLS_ASSERT(msg
&& hellotime
);
174 MPLS_MSGPARAM(Hello
) = &msg
->u
.hello
;
175 if (!MPLS_MSGPARAM(Hello
)->chpTlvExists
) {
176 fprintf(stderr
, "No chp!\n");
180 *hellotime
= MPLS_MSGPARAM(Hello
)->chp
.holdTime
;
185 mpls_return_enum
ldp_mesg_hello_get_csn(ldp_mesg
* msg
, uint32_t * csn
)
188 MPLS_ASSERT(msg
&& csn
);
190 MPLS_MSGPARAM(Hello
) = &msg
->u
.hello
;
191 if (!MPLS_MSGPARAM(Hello
)->csnTlvExists
)
194 *csn
= MPLS_MSGPARAM(Hello
)->csn
.seqNumber
;
199 mpls_return_enum
ldp_mesg_hello_get_request(ldp_mesg
* msg
, int *req
)
202 MPLS_ASSERT(msg
&& req
);
204 MPLS_MSGPARAM(Hello
) = &msg
->u
.hello
;
206 *req
= MPLS_MSGPARAM(Hello
)->chp
.flags
.flags
.request
;
211 mpls_return_enum
ldp_mesg_hello_get_targeted(ldp_mesg
* msg
, int *tar
)
214 MPLS_ASSERT(msg
&& tar
);
216 MPLS_MSGPARAM(Hello
) = &msg
->u
.hello
;
218 *tar
= MPLS_MSGPARAM(Hello
)->chp
.flags
.flags
.target
;