summaryrefslogtreecommitdiffstats
path: root/lib/iso.c
blob: fe97776ade498c0c2eab1efd7ffdf7d4be72065c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * ISO Network functions - iso_net.c
 *
 * Author: Olivier Dugeon <olivier.dugeon@orange.com>
 *
 * Copyright (C) 2023 Orange http://www.orange.com
 *
 * This file is part of Free Range Routing (FRR).
 *
 * FRR is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * FRR is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "compiler.h"

#include <string.h>
#include <ctype.h>
#include <time.h>

#include "printfrr.h"
#include "iso.h"

/**
 * Print ISO System ID as 0000.0000.0000
 *
 * @param	Print buffer
 * @param	Print argument
 * @param	Pointer to the System ID to be printed
 *
 * @return	Number of printed characters
 */
printfrr_ext_autoreg_p("SY", printfrr_iso_sysid);
static ssize_t printfrr_iso_sysid(struct fbuf *buf, struct printfrr_eargs *ea,
				  const void *vptr)
{
	const uint8_t *id = vptr;

	if (!id)
		return bputs(buf, "(null)");

	return bprintfrr(buf, "%02x%02x.%02x%02x.%02x%02x",
			 id[0], id[1], id[2], id[3], id[4], id[5]);
}

/**
 * Print ISO Pseudo Node system ID as 0000.0000.0000.00
 *
 * @param	Print buffer
 * @param	Print argument
 * @param	Pointer to the System ID to be printed
 *
 * @return	Number of printed characters
 */
printfrr_ext_autoreg_p("PN", printfrr_iso_pseudo);
static ssize_t printfrr_iso_pseudo(struct fbuf *buf, struct printfrr_eargs *ea,
				   const void *vptr)
{
	const uint8_t *id = vptr;

	if (!id)
		return bputs(buf, "(null)");

	return bprintfrr(buf, "%02x%02x.%02x%02x.%02x%02x.%02x",
			 id[0], id[1], id[2], id[3], id[4], id[5], id[6]);
}

/**
 * Print ISO LSP Fragment System ID as 0000.0000.0000.00-00
 *
 * @param	Print buffer
 * @param	Print argument
 * @param	Pointer to the System ID to be printed
 *
 * @return	Number of printed characters
 */
printfrr_ext_autoreg_p("LS", printfrr_iso_frag_id);
static ssize_t printfrr_iso_frag_id(struct fbuf *buf, struct printfrr_eargs *ea,
				    const void *vptr)
{
	const uint8_t *id = vptr;

	if (!id)
		return bputs(buf, "(null)");

	return bprintfrr(buf, "%02x%02x.%02x%02x.%02x%02x.%02x-%02x",
			 id[0], id[1], id[2], id[3], id[4], id[5], id[6],
			 id[7]);
}

/**
 * Print ISO Network address as 00.0000.0000.0000 ... with the System ID
 * as 0000.0000.0000.00 when long 'l' option is added to '%pIS'
 *
 * @param	Print buffer
 * @param	Print argument
 * @param	Pointer to the ISO Network address
 *
 * @return	Number of printed characters
 */
printfrr_ext_autoreg_p("IS", printfrr_iso_addr);
static ssize_t printfrr_iso_addr(struct fbuf *buf, struct printfrr_eargs *ea,
				 const void *vptr)
{
	const struct iso_address *ia = vptr;
	uint8_t len = 0;
	int i = 0;
	ssize_t ret = 0;

	if (ea->fmt[0] == 'l') {
		len = 7; /* ISO SYSTEM ID + 1 */
		ea->fmt++;
	}

	if (!ia)
		return bputs(buf, "(null)");

	len += ia->addr_len;
	while (i < len) {
		/* No dot for odd index and at the end of address */
		if ((i & 1) || (i == (len - 1)))
			ret += bprintfrr(buf, "%02x", ia->area_addr[i]);
		else
			ret += bprintfrr(buf, "%02x.", ia->area_addr[i]);
		i++;
	}

	return ret;
}