xref: /netbsd-src/usr.sbin/ldpd/tlv.c (revision e7341ada4cdbb7ac3cfa0776e83e30459dc76ef5)
1 /* $NetBSD: tlv.c,v 1.1 2010/12/08 07:20:15 kefren Exp $ */
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Mihai Chelaru <kefren@NetBSD.org>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/stat.h>
33 
34 #include <stdlib.h>
35 #include <strings.h>
36 #include <stdio.h>
37 
38 #include "ldp.h"
39 #include "fsm.h"
40 #include "ldp_errors.h"
41 #include "tlv.h"
42 
43 /* Reads and checks a tlv struct from a buffer */
44 struct hello_tlv *
get_hello_tlv(unsigned char * s,uint max)45 get_hello_tlv(unsigned char *s, uint max)
46 {
47 	struct hello_tlv *t;
48 
49 	/* Do we have at least Type + Length + MSG_ID ? */
50 	if (max <= TLV_TYPE_LENGTH + MSGID_SIZE)
51 		return NULL;
52 
53 	t = (struct hello_tlv *) s;
54 
55 	if (ntohs(t->type) != LDP_HELLO)
56 		return NULL;
57 
58 	/* Does its size fit into max ? */
59 	if (ntohs(t->length) + TLV_TYPE_LENGTH > max)
60 		return NULL;
61 
62 	t->type = ntohs(t->type);
63 	t->length = ntohs(t->length);
64 	t->messageid = ntohl(t->messageid);
65 	return t;
66 	/* We don't check for Common Hello Params here */
67 }
68 
69 /* Prints out some information about TLV */
70 void
debug_tlv(struct tlv * t)71 debug_tlv(struct tlv * t)
72 {
73 	warnp("TLV type %.4X, Length %d, Message ID %.8X\n", ntohs(t->type),
74 	       ntohs(t->length), ntohs(t->messageid));
75 }
76