This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PacketSenderDlg.cpp
549 lines (430 loc) · 13.8 KB
/
PacketSenderDlg.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// PacketSenderDlg.cpp : 구현 파일
//
#include "stdafx.h"
#include "PacketSender.h"
#include "PacketSenderDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 응용 프로그램 정보에 사용되는 CAboutDlg 대화 상자입니다.
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 대화 상자 데이터입니다.
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 지원입니다.
// 구현입니다.
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CPacketSenderDlg 대화 상자
CPacketSenderDlg::CPacketSenderDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CPacketSenderDlg::IDD, pParent),
CBaseLayer( "AppDlg" )
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_LayerMgr.AddLayer( this ) ;
m_LayerMgr.AddLayer( new CNILayer( "NI" ) ) ;
m_LayerMgr.AddLayer( new CLinkLayer( "Link" ) ) ;
m_LayerMgr.AddLayer( new CIPLayer( "IP" ) );
m_LayerMgr.AddLayer( new CSCTPLayer( "SCTP" ) );
m_LayerMgr.AddLayer( new CS1APLayer( "S1AP" ) );
m_LayerMgr.ConnectLayers("NI ( *Link ( *IP ( *SCTP ( *S1AP ( *AppDlg ) ) ) ) )");
m_IP = (CIPLayer *)m_LayerMgr.GetLayer("IP");
m_Link = (CLinkLayer *)m_LayerMgr.GetLayer("Link");
m_NI = (CNILayer *)m_LayerMgr.GetLayer("NI");
m_SCTP = (CSCTPLayer *)m_LayerMgr.GetLayer("SCTP");
m_S1AP = (CS1APLayer *)m_LayerMgr.GetLayer("S1AP");
}
void CPacketSenderDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BTN_SEND, btnSend);
DDX_Control(pDX, IDC_SERVER_IPADDRESS, ctrlIP);
DDX_Control(pDX,IDC_COMBO_MAC,comboMac);
}
BEGIN_MESSAGE_MAP(CPacketSenderDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_SEND, &CPacketSenderDlg::OnBnClickedBtnSend)
ON_BN_CLICKED(IDC_BTN_SETTING, &CPacketSenderDlg::OnBnClickedBtnSetting)
ON_BN_CLICKED(IDC_BTN_START, &CPacketSenderDlg::OnBnClickedBtnStart)
ON_BN_CLICKED(IDC_BTN_STOP, &CPacketSenderDlg::OnBnClickedBtnStop)
ON_BN_CLICKED(IDC_BTN_RESET, &CPacketSenderDlg::OnBnClickedBtnReset)
ON_CBN_SELCHANGE(IDC_COMBO_MAC, &CPacketSenderDlg::OnCbnSelchangeComboMac)
END_MESSAGE_MAP()
// CPacketSenderDlg 메시지 처리기
BOOL CPacketSenderDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 시스템 메뉴에 "정보..." 메뉴 항목을 추가합니다.
// IDM_ABOUTBOX는 시스템 명령 범위에 있어야 합니다.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 이 대화 상자의 아이콘을 설정합니다. 응용 프로그램의 주 창이 대화 상자가 아닐 경우에는
// 프레임워크가 이 작업을 자동으로 수행합니다.
SetIcon(m_hIcon, TRUE); // 큰 아이콘을 설정합니다.
SetIcon(m_hIcon, FALSE); // 작은 아이콘을 설정합니다.
// TODO: 여기에 추가 초기화 작업을 추가합니다.
int i;
CString device_description;
for(i=0;i<NI_COUNT_NIC;i++){
if(!m_NI->GetAdapterObject(i))
break;
else{
device_description = m_NI->GetAdapterObject(i)->description;
device_description.Trim();
comboMac.AddString(device_description);
}
}
// 콤보 박스 초기값 설정
comboMac.SetCurSel(0);
CString NiName = m_NI->GetAdapterObject(0)->name;
CString mac_address = m_NI->GetNICardAddress((char*)NiName.GetString());
int indexMac = comboMac.GetCurSel();
m_NI->SetAdapterNumber(indexMac);
u_char ip[4];
memcpy(ip,(*m_NI->GetIpAddress(MacAddrToHexInt(mac_address))).addrs_i,4);
ctrlIP.SetAddress(ip[0], ip[1], ip[2], 0); // Destination Address
m_IP->SetSrcIPAddress(ip);
m_Link->SetEnetSrcAddress(MacAddrToHexInt(mac_address));
SetDeligate(DELG_DEFAULT);
return TRUE; // 포커스를 컨트롤에 설정하지 않으면 TRUE를 반환합니다.
}
void CPacketSenderDlg::SetDeligate(int deligate)
{
switch(deligate)
{
case DELG_SETTING :
comboMac.EnableWindow(FALSE);
(CIPAddressCtrl*)GetDlgItem(IDC_SERVER_IPADDRESS)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_SETTING)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_RESET)->EnableWindow(TRUE);
(CButton*)GetDlgItem(IDC_BTN_START)->EnableWindow(TRUE);
(CButton*)GetDlgItem(IDC_BTN_STOP)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_SEND)->EnableWindow(FALSE);
break;
case DELG_RESET:
comboMac.EnableWindow(TRUE);
(CIPAddressCtrl*)GetDlgItem(IDC_SERVER_IPADDRESS)->EnableWindow(TRUE);
(CButton*)GetDlgItem(IDC_BTN_SETTING)->EnableWindow(TRUE);
(CButton*)GetDlgItem(IDC_BTN_RESET)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_START)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_STOP)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_SEND)->EnableWindow(FALSE);
break;
case DELG_START :
comboMac.EnableWindow(FALSE);
(CIPAddressCtrl*)GetDlgItem(IDC_SERVER_IPADDRESS)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_SETTING)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_RESET)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_START)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_STOP)->EnableWindow(TRUE);
(CButton*)GetDlgItem(IDC_BTN_SEND)->EnableWindow(TRUE);
break;
case DELG_STOP :
comboMac.EnableWindow(FALSE);
(CIPAddressCtrl*)GetDlgItem(IDC_SERVER_IPADDRESS)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_SETTING)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_RESET)->EnableWindow(TRUE);
(CButton*)GetDlgItem(IDC_BTN_START)->EnableWindow(TRUE);
(CButton*)GetDlgItem(IDC_BTN_STOP)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_SEND)->EnableWindow(FALSE);
break;
default:
comboMac.EnableWindow(TRUE);
(CIPAddressCtrl*)GetDlgItem(IDC_SERVER_IPADDRESS)->EnableWindow(TRUE);
(CButton*)GetDlgItem(IDC_BTN_SETTING)->EnableWindow(TRUE);
(CButton*)GetDlgItem(IDC_BTN_RESET)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_START)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_STOP)->EnableWindow(FALSE);
(CButton*)GetDlgItem(IDC_BTN_SEND)->EnableWindow(FALSE);
break;
}
}
void CPacketSenderDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 대화 상자에 최소화 단추를 추가할 경우 아이콘을 그리려면
// 아래 코드가 필요합니다. 문서/뷰 모델을 사용하는 MFC 응용 프로그램의 경우에는
// 프레임워크에서 이 작업을 자동으로 수행합니다.
void CPacketSenderDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 그리기를 위한 디바이스 컨텍스트입니다.
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 클라이언트 사각형에서 아이콘을 가운데에 맞춥니다.
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 아이콘을 그립니다.
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
// 사용자가 최소화된 창을 끄는 동안에 커서가 표시되도록 시스템에서
// 이 함수를 호출합니다.
HCURSOR CPacketSenderDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
BOOL CPacketSenderDlg::Send(u_char* ppayload, int length)
{
BOOL bSuccess = FALSE;
assert(mp_UnderLayer != nullptr);
bSuccess = mp_UnderLayer->Send((u_char*) ppayload, length);
return bSuccess;
}
BOOL CPacketSenderDlg::Receive(u_char* ppayload)
{
BOOL bSuccess = FALSE;
// 어떤 메시지 인지 확인하고 알맞는 메시지 전송!!!
// 하위 레이어로부터 받은 정보를 바탕으로
// 아래 조건문을 이용해서 Req/Rep 메시지를 처리!!!!
u_char spayload[S1AP_DATA_SIZE];
int length = 0;
char items = 0;
memset(spayload, '\0', S1AP_DATA_SIZE);
// 수신 메시지 타입 확인 후 처리
switch (ppayload[0])
{
// UE
case S1AP_MSG_TYPE_AUTHEN_REQUEST:
// 받은 메시지 타입 (두 번째 과제)
length = 57;
m_S1AP->SetMessageType(S1AP_MSG_TYPE_AUTHEN_RESPONSE);
m_S1AP->SetTheNumberOfItems(5);
memcpy(spayload, authenticationRspItems(), length);
break;
// UE
case S1AP_MSG_TYPE_ATTACH_ACCEPT: // 받은 메시지 타입 (세 번째 과제)
length = 32;
memcpy(spayload, "\0", length); // 보낼 메시지 패킷 데이터 (현재 파일 아래 함수 호출)
m_S1AP->SetMessageType(S1AP_MSG_TYPE_ATTACH_COMPLETE1); // 50 = 0x32
m_S1AP->SetTheNumberOfItems(3);
memcpy(spayload, initialContextSetupResponseItems(), length);
Send(spayload, length);
// Attach Complete
length = 53;
memcpy(spayload, "\0", length);
m_S1AP->SetMessageType(S1AP_MSG_TYPE_ATTACH_COMPLETE2); // 0x43
m_S1AP->SetTheNumberOfItems(5);
memcpy(spayload, attachCompleteItems(), length);
break;
default:
return FALSE;
}
Send(spayload, length);
return bSuccess;
}
u_char* CPacketSenderDlg::MacAddrToHexInt(CString ether)
{
// 콜론(:)으로 구분 되어진 Ethernet 주소를
// 콜론(:)을 토큰으로 한 바이트씩 값을 가져와서 Ethernet배열에 넣어준다.
CString cstr;
u_char *arp_ether = (u_char *)malloc(sizeof(u_char)*6);
for(int i = 0; i < 6; i++){
AfxExtractSubString(cstr, ether, i, ':');
// strtoul -> 문자열을 원하는 진수로 변환 시켜준다.
arp_ether[i] = (u_char)strtoul(cstr.GetString(), NULL, 16);
}
arp_ether[6] = '\0';
return arp_ether;
}
// Send 버튼 이벤트 처리
void CPacketSenderDlg::OnBnClickedBtnSend()
{
// 메시지에 따라서 Item을 채워서 내려보내면 됨.
u_char ppayload[S1AP_DATA_SIZE];
int length = 0;
char items = 0;
length = 90;
memset(ppayload, '\0', S1AP_DATA_SIZE);
memcpy(ppayload, attachReqItems(), length);
items = 5;
m_S1AP->SetTheNumberOfItems(items);
m_S1AP->SetMessageType(S1AP_MSG_TYPE_ATTACH_REQUEST);
Send(ppayload, length);
}
void CPacketSenderDlg::OnBnClickedBtnSetting()
{
// 168.188.127.52 a8.bc.7f.34
u_char dstip[4] = {
0xa8, 0xbc, 0x7f, 0x34
};
ctrlIP.GetAddress(dstip[0], dstip[1], dstip[2], dstip[3]);
m_IP->SetDstIPAddress(dstip);
// 선택한 NICard로 Adapter 등록
int indexMac = comboMac.GetCurSel();
m_NI->SetAdapterNumber(indexMac);
SetDeligate(DELG_SETTING);
}
void CPacketSenderDlg::OnBnClickedBtnReset()
{
SetDeligate(DELG_RESET);
}
void CPacketSenderDlg::OnBnClickedBtnStart()
{
// PacketDriver 실행
m_NI->PacketStartDriver();
SetDeligate(DELG_START);
}
void CPacketSenderDlg::OnBnClickedBtnStop()
{
// PacketDriver 중지
m_NI->PacketStopDriver();
SetDeligate(DELG_STOP);
}
void CPacketSenderDlg::OnCbnSelchangeComboMac()
{
UpdateData ( TRUE );
// NICard 이름으로 Mac address 가져오기
// ComboBox에서 선택된 인덱스 번호 가져오기
int indexMac = comboMac.GetCurSel();
// 가져온 인덱스 번호로 NIC의 Adapter 이름 가져오기
CString NiName = m_NI->GetAdapterObject(indexMac)->name;
// Adapter 이름으로 Mac Address를 가져오기
CString mac_address = m_NI->GetNICardAddress((char *)NiName.GetString());
// Mac Address로 IP를 가져오기
u_char ip[4];
memcpy(ip,(*m_NI->GetIpAddress(MacAddrToHexInt(mac_address))).addrs_i,4);
// 화면에 IP 설정
ctrlIP.SetAddress(ip[0], ip[1], ip[2], 0);
// IP Layer에 Source IP 주소 설정
m_IP->SetSrcIPAddress(ip);
// Link Layer에 Source mac 주소 설정
m_Link->SetEnetSrcAddress(MacAddrToHexInt(mac_address));
UpdateData ( FALSE );
}
int CPacketSenderDlg::item_size(ITEM_HEADER item)
{
return 2 + 1 + 1 + item.length;
}
// 1번째 과제
u_char* CPacketSenderDlg::attachReqItems()
{
u_char temp[93] = {
// item0 id-eNB-UE-S1AP-ID
0x00,0x08,0x00,0x02,0x00,0x00,
// Item1 id-NAS-PDU
0x00,0x1a,0x00,0x35,0x34,0x17,0x7c,0x2a,0x01,0x33,0x06,0x07,0x41,0x51,0x0b,0xf6
,0x54,0xf0,0x60,0x80,0x01,0x01,0xdb,0x00,0x15,0x3b,0x02,0xe0,0xe0,0x00,0x14,0x02
,0x01,0xd0,0x31,0xd1,0x27,0x0d,0x80,0x00,0x0d,0x00,0x00,0x03,0x00,0x00,0x0c,0x00
,0x00,0x01,0x00,0x52,0x54,0xf0,0x60,0x01,0xf4
// Item2 id-TAI
,0x00,0x43,0x00,0x06,0x00,0x54,0xf0,0x60,0x01,0xf4
// Item3 id-EUTRAN-CGI
,0x00,0x64,0x40,0x08,0x00,0x54,0xf0,0x60,0x00,0x00,0x10,0xe0
// Item4 id-RRC-Establishment-Cause
,0x00,0x86,0x40,0x01,0x30
// padding
,0x00,0x00,0x00
};
return temp;
}
// MME에서 주는 부분 (우리가 채우는 부분 아님..)
u_char* CPacketSenderDlg::authenticationReqItems()
{
u_char temp[57] = {
0
};
return temp;
}
// MME에서 주는 부분 (우리가 채우는 부분 아님..)
u_char* CPacketSenderDlg::initialContextSetupRequestAttachAcceptItems()
{
u_char temp[229] = {
0
};
return temp;
}
// 2번째 과제
u_char* CPacketSenderDlg::authenticationRspItems()
{
u_char temp[57] = {
// item0 id-MME-UE-S1AP-ID
0x00,0x00,0x00,0x03,0x40,0x12,0x86
// Item1 eNB-UE-S1AP-ID
,0x00,0x08,0x00,0x02,0x00,0x00
// Item2 NAS-PDU
,0x00,0x1a,0x00,0x12,0x11,0x17,0x38,0x6f,0x95,0x5b,0x08,0x07,0x53,0x08,0xaa,0x7a,0xdf,0x21,0x9c,0xa2,0x52,0x82
// Item3 id-EUTRAN-CGI
,0x00,0x64,0x40,0x08,0x00,0x54,0xf0,0x60,0x00,0x00,0x10,0xe0
// Item4 id-TAI
,0x00,0x43,0x40,0x06,0x00,0x54,0xf0,0x60,0x01,0xf4
};
return temp;
}
// 3번째 과제
u_char* CPacketSenderDlg::initialContextSetupResponseItems()
{
// warning: length should be 32
u_char temp[33] = {
// item0, id-MME-UE-S1AP-ID (7)
0x00,0x00,0x00,0x03,0x40,0x12,0x86
// item1, id-eNB-UE-S1-AP-ID (6)
,0x00,0x08,0x00,0x02,0x00,0x00
// item2, id-E-RABSetupListCtxtSURes (Msg type: 0x32) (19)
,0x00,0x33,0x40,0x0f,0x00,0x00,0x32,0x40,0x0a,0x0a,0x1f,0x04,0x05,0x01,0x11,0x01,0x00,0x00,0x0a
};
return temp;
}
// 3번째 과제
u_char* CPacketSenderDlg::attachCompleteItems()
{
u_char temp[57] = {
// item0, id-MME-UE-S1AP-ID
0x00,0x00,0x00,0x03,0x40,0x12,0x86
// item1, id-eNB-UE-S1AP-ID
,0x00,0x08,0x00,0x02,0x00,0x00
// item2, id-NAS-PDU (Msg type: 0x43)
,0x00,0x1a,0x00,0x0e,0x0d,0x27,0x40,0x73,0x5f,0x51,0x02,0x07,0x43,0x00,0x03,0x52,0x00,0xc2
// item3, id-EUTRAN-CGI
,0x00,0x64,0x40,0x08,0x00,0x54,0xf0,0x60,0x00,0x00,0x10,0xe0
// item4, id-TAI
,0x00,0x43,0x40,0x06,0x00,0x54,0xf0,0x60,0x01,0xf4
};
return temp;
}