Commit d9138e8f authored by Tien-Thinh Nguyen's avatar Tien-Thinh Nguyen

Polish the code

parent faaf20f5
......@@ -136,14 +136,14 @@ void ausf_app::handle_ue_authentications(
// 5g he av from udm
// get authentication related info
std::string udmUri, Method, Response;
std::string udmUri, method, response;
udmUri = "http://" +
std::string(
inet_ntoa(*((struct in_addr*) &ausf_cfg.udm_addr.ipv4_addr))) +
":" + std::to_string(ausf_cfg.udm_addr.port) + "/nudm-ueau/v1/" +
supi + "/security-information/generate-auth-data";
Logger::ausf_server().debug("UDM's URI %s", udmUri.c_str());
Method = "POST";
method = "POST";
// form udm request body AuthInfo
nlohmann::json AuthInfo =
......@@ -169,16 +169,16 @@ void ausf_app::handle_ue_authentications(
}
// Send request to UDM
ausf_client_inst->curl_http_client(udmUri, Method, AuthInfo.dump(), Response);
ausf_client_inst->curl_http_client(udmUri, method, AuthInfo.dump(), response);
Logger::ausf_server().error("Response from UDM: %s", Response.c_str());
Logger::ausf_server().error("Response from UDM: %s", response.c_str());
ProblemDetails problemDetails;
nlohmann::json problemDetails_json = {};
nlohmann::json response_data = {};
try {
response_data = nlohmann::json::parse(Response.c_str());
response_data = nlohmann::json::parse(response.c_str());
} catch (nlohmann::json::exception& e) {
Logger::ausf_server().info(
"Could not Parse Json content from UDM response");
......@@ -429,9 +429,8 @@ void ausf_app::handle_ue_authentications_confirmation(
confirmResponse.setSupi(sc->supi_ausf);
}
// Send authResult to UDM
// send authentication result info
std::string udmUri, Method, Response;
// Send authResult to UDM (authentication result info)
std::string udmUri, method, response;
udmUri = "http://" +
std::string(inet_ntoa(
*((struct in_addr*) &ausf_cfg.udm_addr.ipv4_addr))) +
......@@ -439,7 +438,7 @@ void ausf_app::handle_ue_authentications_confirmation(
sc->supi_ausf + "/auth-events";
cout << udmUri.c_str() << endl;
Logger::ausf_server().debug("UDM's URI: %s", udmUri.c_str());
Method = "POST";
method = "POST";
// form request body
nlohmann::json confirmResultInfo = {};
......@@ -461,7 +460,7 @@ void ausf_app::handle_ue_authentications_confirmation(
Logger::ausf_server().debug(
"confirmResultInfo: %s", confirmResultInfo.dump().c_str());
ausf_client_inst->curl_http_client(
udmUri, Method, confirmResultInfo.dump(), Response);
udmUri, method, confirmResultInfo.dump(), response);
}
}
......
......@@ -95,14 +95,6 @@ class ausf_app {
const std::string& contextId, std::shared_ptr<security_context> sc);
private:
/* AUSF_AV_s ausf_av_s;
// stored temporarily
uint8_t XRES_STAR[16]; // store xres*
std::string SUPI_AUSF; // store supi
std::string AUTH_TYPE; // store authType
std::string SERVING_NN; // store serving network name
std::string KAUSF_TMP; // store Kausf(string)
*/
std::map<supi64_t, std::shared_ptr<security_context>> imsi2security_context;
mutable std::shared_mutex m_imsi2security_context;
......
......@@ -66,9 +66,9 @@ ausf_client::~ausf_client() {
//------------------------------------------------------------------------------
void ausf_client::curl_http_client(
std::string remoteUri, std::string Method, std::string msgBody,
std::string& Response) {
Logger::ausf_server().info("Send HTTP message with body %s", msgBody.c_str());
std::string remoteUri, std::string method, std::string msgBody,
std::string& response) {
Logger::ausf_app().info("Send HTTP message with body %s", msgBody.c_str());
uint32_t str_len = msgBody.length();
char* body_data = (char*) malloc(str_len + 1);
......@@ -81,30 +81,32 @@ void ausf_client::curl_http_client(
if (curl) {
CURLcode res = {};
struct curl_slist* headers = nullptr;
if (!Method.compare("POST") || !Method.compare("PUT") ||
!Method.compare("PATCH")) {
if ((method.compare("POST") == 0) or (method.compare("PUT") == 0) or
(method.compare("PATCH") == 0)) {
std::string content_type = "Content-Type: application/json";
headers = curl_slist_append(headers, content_type.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
}
curl_easy_setopt(curl, CURLOPT_URL, remoteUri.c_str());
if (!Method.compare("POST"))
if (method.compare("POST") == 0)
curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1);
else if (!Method.compare("PUT"))
else if (method.compare("PUT") == 0)
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
else if (!Method.compare("DELETE"))
else if (method.compare("DELETE") == 0)
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
else if (!Method.compare("PATCH"))
else if (method.compare("PATCH") == 0)
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
else
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, CURL_TIMEOUT_MS);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1);
curl_easy_setopt(curl, CURLOPT_INTERFACE, ausf_cfg.sbi.if_name.c_str());
Logger::ausf_server().info(
"[CURL] request sent by interface " + ausf_cfg.sbi.if_name);
Logger::ausf_app().info(
"Request sent by interface " + ausf_cfg.sbi.if_name);
// Response information.
// response information.
long httpCode = {0};
std::unique_ptr<std::string> httpData(new std::string());
std::unique_ptr<std::string> httpHeaderData(new std::string());
......@@ -113,8 +115,9 @@ void ausf_client::curl_http_client(
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get());
curl_easy_setopt(curl, CURLOPT_HEADERDATA, httpHeaderData.get());
if (!Method.compare("POST") || !Method.compare("PUT") ||
!Method.compare("PATCH")) {
if ((method.compare("POST") == 0) or (method.compare("PUT") == 0) or
(method.compare("PATCH") == 0)) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, msgBody.length());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body_data);
}
......@@ -126,10 +129,10 @@ void ausf_client::curl_http_client(
std::string json_data_response = "";
std::string resMsg = "";
bool is_response_ok = true;
Logger::ausf_server().info("Get response with httpcode (%d)", httpCode);
Logger::ausf_app().info("Get response with httpcode (%d)", httpCode);
if (httpCode == 0) {
Logger::ausf_server().info(
Logger::ausf_app().info(
"Cannot get response when calling %s", remoteUri.c_str());
// free curl before returning
curl_slist_free_all(headers);
......@@ -139,67 +142,37 @@ void ausf_client::curl_http_client(
nlohmann::json response_data = {};
if (httpCode != 200 && httpCode != 201 && httpCode != 204) {
if (httpCode != 200 && httpCode != 201 &&
httpCode != 204) { // TODO: remove hardcoded values
is_response_ok = false;
if (response.size() < 1) {
Logger::ausf_server().info("There's no content in the response");
Logger::ausf_app().info("There's no content in the response");
// TODO: send context response error
return;
}
Logger::ausf_server().info("Wrong response code");
Logger::ausf_app().warn("Receive response with Http Code %d", httpCode);
return;
}
else { // httpCode = 200 || httpCode = 201 || httpCode = 204
/*
//store location of the created context
std::string header_response = *httpHeaderData.get();
std::string CRLF = "\r\n";
std::size_t location_pos = header_response.find("Location");
if (location_pos != std::string::npos)
{
std::size_t crlf_pos = header_response.find(CRLF, location_pos);
if (crlf_pos != std::string::npos)
{
std::string location = header_response.substr(location_pos + 10,
crlf_pos - (location_pos + 10)); printf("Location of the created SMF
context: %s", location.c_str());
}
}
try
{
response_data = nlohmann::json::parse(response);
}
catch (nlohmann::json::exception &e)
{
printf("Could not get Json content from the response");
//Set the default Cause
response_data["error"]["cause"] = "504 Gateway Timeout";
}*/
Response = *httpData.get();
response = *httpData.get();
}
if (!is_response_ok) {
try {
response_data = nlohmann::json::parse(json_data_response);
} catch (nlohmann::json::exception& e) {
Logger::ausf_server().info(
"Could not get Json content from the response");
Logger::ausf_app().info("Could not get Json content from the response");
// Set the default Cause
response_data["error"]["cause"] = "504 Gateway Timeout";
}
Logger::ausf_server().info(
Logger::ausf_app().info(
"Get response with jsonData: %s", json_data_response.c_str());
std::string cause = response_data["error"]["cause"];
Logger::ausf_server().info("Call Network Function services failure");
Logger::ausf_server().info("Cause value: %s", cause.c_str());
Logger::ausf_app().info("Call Network Function services failure");
Logger::ausf_app().info("Cause value: %s", cause.c_str());
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
......
......@@ -50,8 +50,8 @@ class ausf_client {
ausf_client(ausf_client const&) = delete;
void curl_http_client(
std::string remoteUri, std::string Method, std::string msgBody,
std::string& Response);
std::string remoteUri, std::string method, std::string msgBody,
std::string& response);
};
} // namespace app
} // namespace ausf
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment