-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwitch.cpp
175 lines (143 loc) · 3.48 KB
/
Switch.cpp
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
/*
* File: Switch.cpp
* Author: xpalam00
*
* Created on 11. únor 2012, 20:18
*/
#include "Switch.hpp"
#define DBG false
pthread_cond_t cond;
Switch::Switch() {
if(DBG) cerr << "Switch:Switch()" << endl;
pthread_attr_t attr;
int res;
/* vytvoření implicitních atributů */
if( (res = pthread_attr_init(&attr)) != 0 )
{
fprintf(stderr, "pthread_attr_init() err %d\n", res);
}
pthread_cond_init(&cond, NULL);
pthread_create(&(this->_id), NULL, &_entry, this);
//int res;
void *result;
/* čekání na dokončení a převzetí stavu */
if( (res = pthread_join(_id, &result)) != 0 )
{
fprintf(stderr, "pthread_attr_init() err %d\n",res);
}
}
/**
* Pokusí se otevřít všechna Ethernetová rozhraní systému.
*/
void Switch::initDevices()
{
short ret = -1;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if *devList;
ret = pcap_findalldevs(&devList, errbuf);
if (ret == -1) {
cerr << errbuf << endl;
exit(1);
}
char errbuf_libnet[LIBNET_ERRBUF_SIZE];
if(DBG) cerr << "Dev list:" << endl;
for(devList=devList; devList != NULL; devList = devList->next)
{
pcap_t *pcap_handle = pcap_open_live(devList->name, BUFSIZ, 1, 1000, errbuf);
if (pcap_handle == NULL)
{
if(DBG) cerr << "Couldn't open device " << devList->name << ". (" << errbuf << ")" << endl;
continue;
}
// Test, zda nalezené rozhraní je ethernetové
if(pcap_datalink(pcap_handle) != DLT_EN10MB)
{
if(DBG) cerr << "Rozhranní " << devList->name << " není Ethrnet." << endl;
continue;
}
pcap_close(pcap_handle);
// Test na loopback
if( (devList->flags & PCAP_IF_LOOPBACK) == true ) { continue; }
// zjistí MAC adresu interface
libnet_t *l = libnet_init(LIBNET_RAW4, devList->name, errbuf_libnet);
if ( l == NULL )
{
fprintf(stderr, "libnet_init() failed: %s\n", errbuf_libnet);
continue;
//exit(EXIT_FAILURE);
}
struct libnet_ether_addr * mac_addr = libnet_get_hwaddr(l);
if ( mac_addr == NULL ) { continue; }
MacAddress *mac = new MacAddress(mac_addr);
libnet_destroy(l);
Iface *iface = new Iface(this);
iface->setName(devList->name);
iface->macAddress = mac;
this->ifaces->add(iface);
if(DBG) cerr << devList->name << devList->flags << endl;
if( devList->description != NULL ) cout << devList->description << endl;
}
if(DBG) cerr << "Dev list end." << endl;
}
Switch::~Switch()
{
delete this->camTable;
delete this->ifaces;
}
CamTable * Switch::getCamTable()
{
return this->camTable;
}
Ifaces * Switch::getIfaces()
{
return this->ifaces;
}
void *Switch::_entry(void *data)
{
if(DBG) cerr << "Switch::_entry()" << endl;
Switch *thread = (Switch *)data;
return thread->main();
}
void *Switch::main()
{
if(DBG) cerr << "Switch::main()" << endl;
this->camTable = new CamTable();
this->ifaces = new Ifaces();
// inicializace portů
this->initDevices();
// uživatelské rozhraní
this->userPromt();
return 0;
}
/**
* Obsluhuje uživatelské rozhraní - čeká na příkazy a provadí operace
*/
void Switch::userPromt()
{
if(DBG) cerr << "Switch::userPromt()" << endl;
string userCommand;
bool ask = true;
do
{
cout << endl << "switch> ";
cin >> userCommand;
if( userCommand.compare("stat") == 0 )
{
cout << this->getIfaces();
}
else if ( userCommand.compare("cam") == 0 )
{
cout << this->getCamTable();
}
else if ( userCommand.compare("igmp") == 0 )
{}
else if ( userCommand.compare("quit") == 0 )
{
ask = false;
}
else
{
cout << "Bad command. Try stat|cam|igmp|quit." << endl;
}
} while(ask == true);
}