NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
SwitchTable.c
1/************************************************************************************
2* Copyright (C) 2023 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* Author: Shashi Kant Suman *
12* *
13* ---------------------------------------------------------------------------------*/
14
15#include "main.h"
16#include "Ethernet.h"
17
18ptrSWITCHTABLE SWITCHTABLE_FIND(ptrETH_LAN lan, PNETSIM_MACADDRESS dest)
19{
20 ptrSWITCHTABLE table = lan->switchTable;
21 while (table)
22 {
23 if (!MAC_COMPARE(dest, table->mac))
24 return table;
25 SWITCHTABLE_NEXT(table);
26 }
27 return NULL;
28}
29
30void SWITCHTABLE_NEW(ptrETH_LAN lan, PNETSIM_MACADDRESS dest, NETSIM_ID outport)
31{
32 if (!dest)
33 {
34 fnNetSimError("Mac address is NULL in function %s\n", __FUNCTION__);
35 return;
36 }
37
38 ptrSWITCHTABLE table = SWITCHTABLE_FIND(lan, dest);
39 if (!table)
40 {
41 table = SWITCHTABLE_ALLOC();
42 table->mac = dest;
43 table->outPort = outport;
44 SWITCHTABLE_ADD(&SWITCHTABLE_GET_LAN(lan), table);
45 }
46}
47
48PMETRICSNODE menu = NULL;
49
50static PMETRICSNODE write_header(char* name)
51{
52 if(!menu)
53 menu = init_metrics_node(MetricsNode_Menu, "Switch Mac address table", NULL);
54 PMETRICSNODE submenu = init_metrics_node(MetricsNode_Menu, name, NULL);
55 add_node_to_menu(menu, submenu);
56 return submenu;
57}
58
59void switchtable_metrics_print(PMETRICSWRITER metricsWriter)
60{
61 NETSIM_ID i;
62 for (i = 0; i < NETWORK->nDeviceCount; i++)
63 {
64 PMETRICSNODE submenu = NULL;
65 bool isStarted = false;
66 ptrETH_VAR eth = GET_ETH_VAR(i + 1);
67 if (!eth)
68 continue;
69
70 UINT j;
71 for (j = 0; j < eth->lanCount; j++)
72 {
73 ptrETH_LAN lan = eth->lanVar[j];
74 if(!lan->switchTable)
75 continue;
76
77 if (!isStarted)
78 {
79 submenu = write_header(DEVICE_NAME(i + 1));
80 isStarted = true;
81 }
82
83 char heading[BUFSIZ];
84 sprintf(heading, "%s_%d", DEVICE_NAME(i + 1), j);
85 PMETRICSNODE table = init_metrics_node(MetricsNode_Table, heading, NULL);
86 add_node_to_menu(submenu, table);
87
88 add_table_heading_special(table, "Mac Address#1,Type#1,OutPort#1,");
89
90 ptrSWITCHTABLE s = lan->switchTable;
91 while (s)
92 {
93 if (s->mac) {
94 add_table_row_formatted(false, table, "%s,%s,%d,",
95 s->mac->szmacaddress,
96 "Dynamic",
97 s->outPort);
98 }
99 SWITCHTABLE_NEXT(s);
100 }
101 }
102 }
103 if (menu)
104 {
105 write_metrics_node(metricsWriter, WriterPosition_Current, NULL, menu);
106 delete_metrics_node(menu);
107 }
108}