-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmme.cpp
345 lines (271 loc) · 8.85 KB
/
mme.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
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
336
337
338
339
340
341
342
343
344
345
#include "mme.h"
TunData::TunData() {
sgw_addr = allocate_str_mem(INET_ADDRSTRLEN);
pgw_addr = allocate_str_mem(INET_ADDRSTRLEN);
}
TunData::TunData(const TunData &src_obj) {
sgw_addr = allocate_str_mem(INET_ADDRSTRLEN);
pgw_addr = allocate_str_mem(INET_ADDRSTRLEN);
mme_cteid = src_obj.mme_cteid;
sgw_cteid = src_obj.sgw_cteid;
sgw_uteid = src_obj.sgw_uteid;
enodeb_uteid = src_obj.enodeb_uteid;
sgw_port = src_obj.sgw_port;
pgw_port = src_obj.pgw_port;
strcpy(sgw_addr, src_obj.sgw_addr);
strcpy(pgw_addr, src_obj.pgw_addr);
}
void swap(TunData &src_obj, TunData &dst_obj) {
using std::swap;
swap(src_obj.mme_cteid, dst_obj.mme_cteid);
swap(src_obj.sgw_cteid, dst_obj.sgw_cteid);
swap(src_obj.sgw_uteid, dst_obj.sgw_uteid);
swap(src_obj.enodeb_uteid, dst_obj.enodeb_uteid);
swap(src_obj.sgw_port, dst_obj.sgw_port);
swap(src_obj.pgw_port, dst_obj.pgw_port);
swap(src_obj.sgw_addr, dst_obj.sgw_addr);
swap(src_obj.pgw_addr, dst_obj.pgw_addr);
}
TunData& TunData::operator=(TunData src_obj) {
swap(*this, src_obj);
return *this;
}
TunData::TunData(TunData &&src_obj)
:TunData() {
swap(*this, src_obj);
}
TunData::~TunData() {
free(sgw_addr);
free(pgw_addr);
}
MME::MME() {
ue_ip = allocate_str_mem(INET_ADDRSTRLEN);
reply = allocate_str_mem(BUFFER_SIZE);
}
MME::MME(const MME &src_obj) {
ue_ip = allocate_str_mem(INET_ADDRSTRLEN);
reply = allocate_str_mem(BUFFER_SIZE);
mme_server = src_obj.mme_server;
to_hss = src_obj.to_hss;
to_sgw = src_obj.to_sgw;
ue_num = src_obj.ue_num;
bearer_id = src_obj.bearer_id;
type = src_obj.type;
strcpy(ue_ip, src_obj.ue_ip);
strcpy(reply, src_obj.reply);
autn_num = src_obj.autn_num;
rand_num = src_obj.rand_num;
autn_xres = src_obj.autn_xres;
autn_res = src_obj.autn_res;
tun_data = src_obj.tun_data;
}
void swap(MME &src_obj, MME &dst_obj) {
using std::swap;
swap(src_obj.mme_server, dst_obj.mme_server);
swap(src_obj.to_hss, dst_obj.to_hss);
swap(src_obj.to_sgw, dst_obj.to_sgw);
swap(src_obj.ue_num, dst_obj.ue_num);
swap(src_obj.bearer_id, dst_obj.bearer_id);
swap(src_obj.type, dst_obj.type);
swap(src_obj.ue_ip, dst_obj.ue_ip);
swap(src_obj.reply, dst_obj.reply);
swap(src_obj.autn_num, dst_obj.autn_num);
swap(src_obj.rand_num, dst_obj.rand_num);
swap(src_obj.autn_xres, dst_obj.autn_xres);
swap(src_obj.autn_res, dst_obj.autn_res);
swap(src_obj.tun_data, dst_obj.tun_data);
}
MME& MME::operator=(MME src_obj) {
swap(*this, src_obj);
return *this;
}
MME::MME(MME &&src_obj)
:MME() {
swap(*this, src_obj);
}
void MME::set_cteid() {
tun_data.mme_cteid = generate_cteid(ue_num);
}
uint16_t MME::generate_cteid(int &ue_number) {
return ue_number;
}
void MME::set_bearer_id() {
bearer_id = generate_bearer_id(ue_num);
}
int MME::generate_bearer_id(int &ue_number) {
return ue_number;
}
void MME::set_sgw() {
tun_data.sgw_port = g_sgw1_port;
strcpy(tun_data.sgw_addr, g_sgw1_addr);
}
void MME::set_pgw() {
tun_data.pgw_port = g_pgw_port;
strcpy(tun_data.pgw_addr, g_pgw_addr);
}
void MME::startup_mme_server(ClientDetails &entity) {
mme_server.fill_server_details(g_freeport, g_mme_addr);
mme_server.bind_server();
mme_server.client_sock_addr = entity.client_sock_addr;
mme_server.client_num = entity.num;
mme_server.connect_with_client();
}
void MME::set_ue_num() {
ue_num = mme_server.client_num;
}
void MME::attach_req_from_ue() {
mme_server.read_data();
}
void MME::setup_hss_client() {
to_hss.bind_client();
to_hss.fill_server_details(g_hss_port, g_hss_addr);
to_hss.connect_with_server(ue_num);
}
void MME::fetch_ue_data() {
to_hss.pkt.clear_data();
to_hss.pkt.fill_data(0, mme_server.pkt.data_len, mme_server.pkt.data);
to_hss.pkt.make_data_packet();
to_hss.write_data();
to_hss.read_data();
memcpy(&autn_num, to_hss.pkt.data, sizeof(unsigned long long));
memcpy(&rand_num, to_hss.pkt.data + sizeof(unsigned long long), sizeof(unsigned long long));
memcpy(&autn_xres, to_hss.pkt.data + 2 * sizeof(unsigned long long), sizeof(unsigned long long));
}
void MME::authenticate_ue() {
mme_server.pkt.clear_data();
mme_server.pkt.fill_data(0, 2 * sizeof(unsigned long long), to_hss.pkt.data);
mme_server.pkt.make_data_packet();
mme_server.write_data();
mme_server.read_data();
memcpy(&autn_res, mme_server.pkt.data, sizeof(unsigned long long));
if (autn_xres == autn_res) {
strcpy(reply, "OK");
mme_server.pkt.clear_data();
mme_server.pkt.fill_data(0, strlen(reply), reply);
mme_server.pkt.make_data_packet();
mme_server.write_data();
cout << "Authentication is successful for UE - " << ue_num << endl;
}
else {
strcpy(reply, "FAILED");
mme_server.pkt.clear_data();
mme_server.pkt.fill_data(0, strlen(reply), reply);
mme_server.pkt.make_data_packet();
mme_server.write_data();
cout << "Authentication is not successful for UE - " << ue_num << endl;
}
}
void MME::setup_sgw_client() {
to_sgw.bind_client();
to_sgw.fill_server_details(tun_data.sgw_port, tun_data.sgw_addr);
to_sgw.connect_with_server(ue_num);
}
void MME::create_session_req_to_sgw() {
type = 1;
to_sgw.pkt.clear_data();
to_sgw.pkt.fill_data(0, sizeof(int), type);
to_sgw.pkt.fill_data(sizeof(int), sizeof(int), ue_num);
to_sgw.pkt.fill_data(2 * sizeof(int), sizeof(int), bearer_id);
to_sgw.pkt.fill_data(3 * sizeof(int), sizeof(uint16_t), tun_data.mme_cteid);
to_sgw.pkt.make_data_packet();
to_sgw.write_data();
}
void MME::create_session_res_from_sgw() {
cout << "Waiting to read Create Session Request response from SGW" << endl;
to_sgw.read_data();
to_sgw.pkt.rem_gtpc_hdr();
memcpy(&tun_data.sgw_cteid, to_sgw.pkt.data, sizeof(uint16_t));
memcpy(reply, to_sgw.pkt.data + sizeof(uint16_t), to_sgw.pkt.data_len - sizeof(uint16_t));
if (strcmp((const char*)reply, "OK") == 0) {
cout << "Create session request was successful for UE - " << ue_num << endl;
}
// else {
// cout << "Create session request failed: Please disconnect and connect again" << endl;
// handle_exceptions();
// }
}
void MME::recv_enodeb() {
mme_server.read_data();
memcpy(&tun_data.enodeb_uteid, mme_server.pkt.data, sizeof(uint16_t));
}
void MME::modify_session_req_to_sgw() {
to_sgw.pkt.clear_data();
to_sgw.pkt.fill_gtpc_hdr(tun_data.sgw_cteid);
to_sgw.pkt.fill_data(0, sizeof(uint16_t), tun_data.enodeb_uteid);
to_sgw.pkt.add_gtpc_hdr();
to_sgw.pkt.make_data_packet();
to_sgw.write_data();
}
void MME::modify_session_res_from_sgw() {
to_sgw.read_data();
to_sgw.pkt.rem_gtpc_hdr();
memcpy(&tun_data.sgw_uteid, to_sgw.pkt.data, sizeof(uint16_t));
memcpy(ue_ip, to_sgw.pkt.data + sizeof(uint16_t), INET_ADDRSTRLEN);
memcpy(reply, to_sgw.pkt.data + sizeof(uint16_t) + INET_ADDRSTRLEN, to_sgw.pkt.data_len - sizeof(uint16_t) - INET_ADDRSTRLEN);
if (strcmp((const char*)reply, "OK") == 0) {
cout << "Modify Session Request was successful for UE - " << ue_num << endl;
}
// else {
// cout << "Modify session request failed: Please disconnect and connect again" << endl;
// handle_exceptions();
// }
}
void MME::send_enodeb() {
mme_server.pkt.clear_data();
mme_server.pkt.fill_data(0, sizeof(uint16_t), tun_data.sgw_uteid);
mme_server.pkt.fill_data(sizeof(uint16_t), INET_ADDRSTRLEN, ue_ip);
mme_server.pkt.make_data_packet();
mme_server.write_data();
}
void MME::detach_req_from_ue() {
mme_server.read_data();
memcpy(&type, mme_server.pkt.data, sizeof(int));
if (type == 3) {
cout << "Detach request has been received successfully at MME for UE - " << ue_num << endl;
}
// else {
// cout << "Invalid Detach type num - " << type << " : Please disconnect and connect again" << endl;
// handle_exceptions();
// }
}
void MME::delete_session_req_to_sgw() {
to_sgw.pkt.clear_data();
to_sgw.pkt.fill_data(0, mme_server.pkt.data_len, mme_server.pkt.data);
to_sgw.pkt.fill_gtpc_hdr(tun_data.sgw_cteid);
to_sgw.pkt.add_gtpc_hdr();
to_sgw.pkt.make_data_packet();
to_sgw.write_data();
}
void MME::delete_session_res_from_sgw() {
cout << "Waiting to read Detach session response from SGW" << endl;
to_sgw.read_data();
to_sgw.pkt.rem_gtpc_hdr();
memcpy(reply, to_sgw.pkt.data, to_sgw.pkt.data_len);
if (strcmp((const char*)reply, "OK") == 0) {
cout << "MME has received successful detach response for UE - " << ue_num << endl;
}
// else {
// cout << "Detach process failure at SGW: UE - " << ue_num << ", Reply = " << reply << ". Please disconnect and connect again" << endl;
// handle_exceptions();
// }
}
void MME::detach_res_to_ue() {
strcpy(reply, "OK");
mme_server.pkt.clear_data();
mme_server.pkt.fill_data(0, strlen(reply), reply);
mme_server.pkt.make_data_packet();
mme_server.write_data();
cout << "MME has successfully deallocated resources for UE - " << ue_num << endl;
}
void MME::rem_bearer_id() {
bearer_id = -1; // Dummy statement
}
void MME::rem_tun_data() {
/* Since this MME object is dedicated to this paricular UE, the moment it completes its job and goes out of scope,
all data related to the UE will automatically de deleted
*/
}
MME::~MME() {
free(ue_ip);
free(reply);
}