xref: /openbsd/usr.sbin/ldpd/keepalive.c (revision 60e1e0e7)
1 /*	$OpenBSD: keepalive.c,v 1.17 2016/07/01 23:36:38 renato Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <string.h>
21 
22 #include "ldpd.h"
23 #include "ldpe.h"
24 #include "log.h"
25 
26 void
send_keepalive(struct nbr * nbr)27 send_keepalive(struct nbr *nbr)
28 {
29 	struct ibuf	*buf;
30 	uint16_t	 size;
31 
32 	size = LDP_HDR_SIZE + LDP_MSG_SIZE;
33 	if ((buf = ibuf_open(size)) == NULL)
34 		fatal(__func__);
35 
36 	gen_ldp_hdr(buf, size);
37 	size -= LDP_HDR_SIZE;
38 	gen_msg_hdr(buf, MSG_TYPE_KEEPALIVE, size);
39 
40 	evbuf_enqueue(&nbr->tcp->wbuf, buf);
41 }
42 
43 int
recv_keepalive(struct nbr * nbr,char * buf,uint16_t len)44 recv_keepalive(struct nbr *nbr, char *buf, uint16_t len)
45 {
46 	struct ldp_msg msg;
47 
48 	memcpy(&msg, buf, sizeof(msg));
49 	if (len != LDP_MSG_SIZE) {
50 		session_shutdown(nbr, S_BAD_MSG_LEN, msg.id, msg.type);
51 		return (-1);
52 	}
53 
54 	if (nbr->state != NBR_STA_OPER)
55 		nbr_fsm(nbr, NBR_EVT_KEEPALIVE_RCVD);
56 
57 	return (0);
58 }
59