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
/
S1APLayer.cpp
218 lines (183 loc) · 5.24 KB
/
S1APLayer.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
// S1APLayer.cpp: implementation of the CS1APLayer class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "PacketSender.h"
#include "S1APLayer.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CS1APLayer::CS1APLayer( char* pName )
: CBaseLayer( pName )
{
ResetHeader( );
}
CS1APLayer::~CS1APLayer()
{
}
void CS1APLayer::ResetHeader()
{
m_sHeader.s1ap_pdu = 0x00;
m_sHeader.s1ap_proc_code = 0x00;
m_sHeader.s1ap_crit = 0x00;
m_sHeader.s1ap_length = S1AP_HEADER_SIZE;
m_sHeader.s1ap_padding = 0x0000;
m_sHeader.s1ap_nitem = 0x00; // the number of items
memset(m_sHeader.s1ap_data, '\0', S1AP_DATA_SIZE);
m_sHeader2.s1ap_pdu = 0x00;
m_sHeader2.s1ap_proc_code = 0x00;
m_sHeader2.s1ap_crit = 0x0000;
m_sHeader2.s1ap_length = S1AP_HEADER_SIZE;
memset(m_sHeader2.s1ap_padding, 0x00, 2);
m_sHeader2.s1ap_nitem = 0x00; // the number of items
memset(m_sHeader2.s1ap_data, '\0', S1AP_DATA_SIZE + 1);
}
BOOL CS1APLayer::Send(u_char* ppayload, int nlength)
{
SelectMessage(nlength);
BOOL bSuccess = FALSE ;
memset(m_sHeader.s1ap_data, '\0', S1AP_DATA_SIZE);
memset(m_sHeader2.s1ap_data, '\0', S1AP_DATA_SIZE + 1);
if(MESSAGE_TYPE == S1AP_MSG_TYPE_ATTACH_ACCEPT) {
memcpy(m_sHeader2.s1ap_data, ppayload, nlength) ;
bSuccess = mp_UnderLayer->Send((u_char*)&m_sHeader2, S1AP_HEADER_SIZE + nlength + 1);
}
else {
memcpy(m_sHeader.s1ap_data, ppayload, nlength) ;
bSuccess = mp_UnderLayer->Send((u_char*)&m_sHeader, S1AP_HEADER_SIZE + nlength );
}
return bSuccess;
}
BOOL CS1APLayer::Receive(u_char* ppayload, int len)
{
u_char result[1];
if (len == 4)
{
PS1APLayer_HEADER2 pFrame = (PS1APLayer_HEADER2) ppayload ;
result[0] = SearchItems(pFrame->s1ap_proc_code, pFrame->s1ap_data);
}
else
{
PS1APLayer_HEADER pFrame = (PS1APLayer_HEADER) ppayload ;
result[0] = SearchItems(pFrame->s1ap_proc_code, pFrame->s1ap_data);
}
BOOL bSuccess = FALSE ;
bSuccess = mp_aUpperLayer[0]->Receive((u_char*)result);
return bSuccess ;
}
void CS1APLayer::SetMessageType(int msg_type)
{
MESSAGE_TYPE = msg_type;
}
void CS1APLayer::SetTheNumberOfItems(char num)
{
m_sHeader.s1ap_nitem = num;
}
void CS1APLayer::SetTheNumberOfItems2(char num)
{
m_sHeader2.s1ap_nitem = num;
}
u_char CS1APLayer::SearchItems(u_char proc, u_char* ppayload)
{
u_char ch;
short id;
ITEM_HEADER header;
int i = 0;
while(1)
{
memcpy((u_char*)&header, &ppayload[i], ITEM_HEADER_SIZE);
id = ntohs(header.id);
if (id == S1AP_ID_NAS_PDU)
{
// length (1)
// + security header type & proto discriminator (1)
// + msg auth code (4)
// + sequence number (1)
// + security header type & proto discriminator (1)
if (proc == S1AP_PROC_CODE_DOWNLINK_NAS_TRANS)
i += 6;
else
i += 12;
ch = ppayload[i]; // NAS EPS MME type
return ch;
}
else if (id == S1AP_ID_E_RAB_TOBE_SETUP_LIST_CTXT_SUREQ)
{
ch = ppayload[i + 33]; // NAS EPS MME type
return ch;
}
else
{
i += ITEM_HEADER_SIZE + header.length;
}
if(i >= m_sHeader.s1ap_length)
return 0;
}
return 0;
}
// 어떤 메시지를 보낼 것인지 지정
void CS1APLayer::SelectMessage(int nlength)
{
switch (MESSAGE_TYPE)
{
case S1AP_MSG_TYPE_ATTACH_REQUEST:
AttachReqMsg(nlength);
break;
case S1AP_MSG_TYPE_AUTHEN_RESPONSE:
AuthenticationRepMsg(nlength);
break;
case S1AP_MSG_TYPE_ATTACH_COMPLETE1:
initialContextSetupResponseItems(nlength);
break;
case S1AP_MSG_TYPE_ATTACH_COMPLETE2:
attachCompleteItems(nlength);
break;
default:
break;
}
}
void CS1APLayer::AttachReqMsg(int nlength)
{
m_sHeader.s1ap_pdu = S1AP_PDU_INITIAL_MESSAGE; // a type of PDU
m_sHeader.s1ap_proc_code = S1AP_PROC_CODE_INITIAL_UE_MSG; // procedure code
m_sHeader.s1ap_crit = 0x40;
m_sHeader.s1ap_length = nlength + 3;
((CSCTPLayer*)GetUnderLayer())->SetFlags(SCTP_FLAGS_B | SCTP_FLAGS_E);
((CSCTPLayer*)GetUnderLayer())->SetSSN(0);
}
void CS1APLayer::AuthenticationRepMsg(int nlength)
{
m_sHeader.s1ap_pdu = S1AP_ID_NAS_PDU; // a type of PDU
m_sHeader.s1ap_proc_code = S1AP_PROC_CODE_UPLINK_NAS_TRANS; // procedure code
m_sHeader.s1ap_crit = 0x40;
m_sHeader.s1ap_length = nlength + 3;
((CSCTPLayer*)GetUnderLayer())->SetFlags(SCTP_FLAGS_B | SCTP_FLAGS_E);
((CSCTPLayer*)GetUnderLayer())->SetSSN(2);
}
void CS1APLayer::initialContextSetupResponseItems(int nlength)
{
// begin: 알맞은 값을 채우시오
m_sHeader.s1ap_pdu = S1AP_PDU_SUCCESSFUL_OUTCOME; // a type of PDU
m_sHeader.s1ap_proc_code = S1AP_PROC_CODE_INITIAL_CONTEXTSETUP; // procedure code
// end
m_sHeader.s1ap_crit = 0x00;
m_sHeader.s1ap_length = nlength + 3;
((CSCTPLayer*)GetUnderLayer())->SetFlags(SCTP_FLAGS_B | SCTP_FLAGS_E);
((CSCTPLayer*)GetUnderLayer())->SetSSN(6);
}
void CS1APLayer::attachCompleteItems(int nlength)
{
// begin: 알맞은 값을 채우시오
m_sHeader.s1ap_pdu = S1AP_ID_NAS_PDU; // a type of PDU
m_sHeader.s1ap_proc_code = S1AP_PROC_CODE_UPLINK_NAS_TRANS; // procedure code
// end
m_sHeader.s1ap_crit = 0x40;
m_sHeader.s1ap_length = nlength + 3;
((CSCTPLayer*)GetUnderLayer())->SetFlags(SCTP_FLAGS_B | SCTP_FLAGS_E);
((CSCTPLayer*)GetUnderLayer())->SetSSN(7);
}