File: lldp_mand_clif.c

package info (click to toggle)
lldpad 1.0.1%2Bgit20180808.4e642bd-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,216 kB
  • sloc: ansic: 47,078; sh: 2,001; lex: 1,036; makefile: 135
file content (335 lines) | stat: -rw-r--r-- 7,824 bytes parent folder | download | duplicates (5)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*******************************************************************************

  LLDP Agent Daemon (LLDPAD) Software
  Copyright(c) 2007-2010 Intel Corporation.

  This program is free software; you can redistribute it and/or modify it
  under the terms and conditions of the GNU General Public License,
  version 2, as published by the Free Software Foundation.

  This program is distributed in the hope 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; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Contact Information:
  open-lldp Mailing List <lldp-devel@open-lldp.org>

*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include "lldp.h"
#include "lldp_mod.h"
#include "lldp_mand.h"
#include "lldp_mand_clif.h"
#include "clif_msgs.h"

void print_port_id(u16, char *info);
void print_chassis_id(u16, char *info);
void print_ttl(u16, char *info);
u32 mand_lookup_tlv_name(char *tlvid_str);
int mand_print_help();

static const struct lldp_mod_ops mand_ops_clif = {
	.lldp_mod_register 	= mand_cli_register,
	.lldp_mod_unregister 	= mand_cli_unregister,
	.print_tlv		= mand_print_tlv,
	.lookup_tlv_name	= mand_lookup_tlv_name,
	.print_help		= mand_print_help,
};


struct type_name_info mand_tlv_names[] = {
	{	.type = END_OF_LLDPDU_TLV,
		.name = "End of LLDPDU TLV", .key = "", },
	{	.type = CHASSIS_ID_TLV,
		.name = "Chassis ID TLV", .key = "chassisID",
		.print_info = print_chassis_id, },
	{	.type = PORT_ID_TLV,
		.name = "Port ID TLV", .key = "portID",
		.print_info = print_port_id, },
	{	.type = TIME_TO_LIVE_TLV,
		.name = "Time to Live TLV", .key = "TTL",
		.print_info = print_ttl, },
	{	.type = INVALID_TLVID, }
};

int mand_print_help()
{
	struct type_name_info *tn = &mand_tlv_names[0];

	while (tn->type != INVALID_TLVID) {
		if (tn->key && strlen(tn->key) && tn->name) {
			printf("   %s", tn->key);
			if (strlen(tn->key)+3 < 8)
				printf("\t");
			printf("\t: %s\n", tn->name);
		}
		tn++;
	}
	
	return 0;
}

struct lldp_module *mand_cli_register(void)
{
	struct lldp_module *mod;

	mod = malloc(sizeof(*mod));
	if (!mod) {
		fprintf(stderr, "failed to malloc module data\n");
		goto out_err;
	}
 	mod->id = LLDP_MOD_MAND;
	mod->ops = &mand_ops_clif;

	return mod;
out_err:
	return NULL;
}

void mand_cli_unregister(struct lldp_module *mod)
{
	free(mod);
}

void print_chassis_id(u16 len, char *info)
{
	u8 subtype;
	u8 addrnum;
	char buf[512];
	int i;

	if (!len || len > 256) {
		printf("Invalid length = %d\n", len);
		return;
	}

	hexstr2bin(info, (u8 *)&subtype, sizeof(subtype));

	memset(buf, 0, sizeof(buf));
	switch (subtype) {
	case CHASSIS_ID_MAC_ADDRESS:
		if (len != 1 + 6)
			return;
		printf("MAC: ");
		for (i = 0; i < 12; i+=2) {
			printf("%2.2s", info + 2 + i);
			if (i < 10)
				printf(":");
			else
				printf("\n");
		}
		break;
	case CHASSIS_ID_NETWORK_ADDRESS:
		if (len <=2) {
			printf("Bad Network Address\n");
			break;
		}

		hexstr2bin(info+2, (u8 *)&addrnum, sizeof(addrnum));

		switch(addrnum) {
		case MANADDR_IPV4:
			if (len == 6) {
				struct in_addr addr;
				hexstr2bin(info+4, (u8 *)&addr, sizeof(addr));
				inet_ntop(AF_INET, (void *)&addr, buf,
					  sizeof(buf));
				printf("IPv4: %s\n", buf);
			} else {
				printf("Bad IPv4: %*.*s\n",
				       2*(len-2), 2*(len-2), info+4);
			}
			break;
		case MANADDR_IPV6:
			if (len == 18) {
				struct in6_addr addr;
				hexstr2bin(info+4, (u8 *)&addr, sizeof(addr));
				inet_ntop(AF_INET6, (void *)&addr, buf,
					  sizeof(buf));
				printf("IPv6: %s\n", buf);
			} else {
				printf("Bad IPv6: %*.*s\n",
				       2*(len-2), 2*(len-2), info+4);
			}
			break;
		default:
			printf("Network Address Type %d: %*.*s\n", addrnum,
			       2*(len-2), 2*(len-2), info+2);
			break;
		}
		break;
	case CHASSIS_ID_CHASSIS_COMPONENT:
		hexstr2bin(info+2, (u8 *)&buf[0], len-1);
		printf("Chassis Component: %s\n", buf);
		break;
	case CHASSIS_ID_INTERFACE_ALIAS:
		hexstr2bin(info+2, (u8 *)&buf[0], len-1);
		printf("IfAlias: %s\n", buf);
		break;
	case CHASSIS_ID_PORT_COMPONENT:
		hexstr2bin(info+2, (u8 *)&buf[0], len-1);
		printf("Port Component: %s\n", buf);
		break;
	case CHASSIS_ID_INTERFACE_NAME:
		hexstr2bin(info+2, (u8 *)&buf[0], len-1);
		printf("Ifname: %s\n", buf);
		break;
	case CHASSIS_ID_LOCALLY_ASSIGNED:
		hexstr2bin(info+2, (u8 *)&buf[0], len-1);
		printf("Local: %s\n", buf);
		break;
	default:
		printf("Bad Chassis ID: %*.*s\n", 2*len, 2*len, info);
		break;
	}
}

void print_port_id(u16 len, char *info)
{
	u8 subtype;
	u8 addrnum;
	char buf[512];
	int i;

	if (!len || len > 256) {
		printf("Invalid length = %d\n", len);
		return;
	}

	hexstr2bin(info, (u8 *)&subtype, sizeof(subtype));

	memset(buf, 0, sizeof(buf));
	switch (subtype) {
	case PORT_ID_MAC_ADDRESS:
		if (len != 1 + 6)
			return;
		printf("MAC: ");
		for (i = 0; i < 12; i+=2) {
			printf("%2.2s", info + 2 + i);
			if (i < 10)
				printf(":");
			else
				printf("\n");
		}
		break;
	case PORT_ID_NETWORK_ADDRESS:
		if (len <=2) {
			printf("Bad Network Address\n");
			break;
		}

		hexstr2bin(info+2, (u8 *)&addrnum, sizeof(addrnum));

		switch(addrnum) {
		case MANADDR_IPV4:
			if (len == 6) {
				struct in_addr addr;
				hexstr2bin(info+4, (u8 *)&addr, sizeof(addr));
				inet_ntop(AF_INET, (void *)&addr, buf,
					  sizeof(buf));
				printf("IPv4: %s\n", buf);
			} else {
				printf("Bad IPv4: %*.*s\n",
				       2*(len-2), 2*(len-2), info+4);
			}
			break;
		case MANADDR_IPV6:
			if (len == 18) {
				struct in6_addr addr;
				hexstr2bin(info+4, (u8 *)&addr, sizeof(addr));
				inet_ntop(AF_INET6, (void *)&addr, buf,
					  sizeof(buf));
				printf("IPv6: %s\n", buf);
			} else {
				printf("Bad IPv6: %*.*s\n",
				       2*(len-2), 2*(len-2), info+4);
			}
			break;
		default:
			printf("Network Address Type %d: %*.*s\n", addrnum,
			       2*(len-2), 2*(len-2), info+2);
			break;
		}
		break;
	case PORT_ID_INTERFACE_ALIAS:
		hexstr2bin(info+2, (u8 *)&buf[0], len-1);
		printf("Interface Alias: %s\n", buf);
		break;
	case PORT_ID_PORT_COMPONENT:
		hexstr2bin(info+2, (u8 *)&buf[0], len-1);
		printf("Port Component: %s\n", buf);
		break;
	case PORT_ID_INTERFACE_NAME:
		hexstr2bin(info+2, (u8 *)&buf[0], len-1);
		printf("Ifname: %s\n", buf);
		break;
	case PORT_ID_LOCALLY_ASSIGNED:
		hexstr2bin(info+2, (u8 *)&buf[0], len-1);
		printf("Local: %s\n", buf);
		break;
	case PORT_ID_AGENT_CIRCUIT_ID:
		printf("Agent Circuit ID: %*.*s\n", 2*(len-1), 2*(len-1),
		       info+2);
		break;
	default:
		printf("Bad Port ID: %*.*s\n", 2*len, 2*len, info);
		break;
	}
}

void print_ttl(UNUSED u16 len, char *info)
{
	u16 ttl;

	hexstr2bin(info, (u8 *)&ttl, sizeof(ttl));
	ttl = ntohs(ttl);
	printf("%d\n", ttl);
}

/* return 1: if it printed the TLV
 *        0: if it did not
 */
int mand_print_tlv(u32 tlvid, u16 len, char *info)
{
	struct type_name_info *tn = &mand_tlv_names[0];

	while (tn->type != INVALID_TLVID) {
		if (tlvid == tn->type) {
			printf("%s\n", tn->name);
			if (tn->print_info) {
				printf("\t");
				tn->print_info(len, info);
			}
			return 1;
		}
		tn++;
	}
	
	return 0;
}

u32 mand_lookup_tlv_name(char *tlvid_str)
{
	struct type_name_info *tn = &mand_tlv_names[0];

	while (tn->type != INVALID_TLVID) {
		if (!strcasecmp(tn->key, tlvid_str))
			return tn->type;
		tn++;
	}
	return INVALID_TLVID;
}