Commit 3c1c54db authored by Yann Garcia's avatar Yann Garcia
Browse files

Merge Framework woth NG112

parent 3ee46626
......@@ -29,23 +29,6 @@ public: //! \publicsection
static const std::string& mac_dst; //! Destination MAC address parameter name
static const std::string& mac_bc; //! Broadcast MAC address parameter name
static const std::string& eth_type; //! Ethernet type parameter name
static const std::string& beaconing; //! Beaconing mode parameter name
static const std::string& station_type;
static const std::string& country;
static const std::string& type_of_address;
static const std::string& ssp; //! SSP parameter name
static const std::string& its_aid; //! ITS-AID parameter name
static const std::string& gn_payload; //! GeoNetworking Payload parameter name
static const std::string& gn_next_header; //! GeoNetworking NextHeader parameter name
static const std::string& gn_header_type; //! GeoNetworking HeaderType parameter name
static const std::string& gn_header_sub_type; //! GeoNetworking HeaderSubType parameter name
static const std::string& gn_lifetime; //! GeoNetworking Lifetime parameter name
static const std::string& gn_traffic_class; //! GeoNetworking Traffic class parameter name
static const std::string& btp_type; //! BTP Type parameter name
static const std::string& btp_payload; //! BTP Payload parameter name
static const std::string& btp_destination_port; //! BTP DestinationPort parameter name
static const std::string& btp_info; //! BTP Info parameter name
static const std::string& nic; //! Network Interface Card parameter name
static const std::string& ll_address; //! Test system GeoNetworking LL-Address parameter name
static const std::string& latitude; //! Test system Latitude parameter name
......
......@@ -80,6 +80,10 @@ const std::string& params::uri = std::string("uri");
const std::string& params::host = std::string("host");
const std::string& params::content_type = std::string("content_type");
const std::string& params::sip_url = std::string("sip_url");
const std::string& params::sip_version = std::string("sip_version");
const std::string& params::payload = std::string("payload");
const std::string& params::codecs = std::string("codecs");
void params::convert(params& p_param, const std::string p_parameters) {
......
......@@ -80,11 +80,12 @@ int http_codec::decode (const OCTETSTRING& data, LibItsHttp__TypesAndValues::Htt
response.statuscode() = std::stoi(m[3].str().c_str());
response.statustext() = CHARSTRING(m[4].str().c_str());
LibItsHttp__TypesAndValues::HeaderLines headers;
decode_headers(decoding_buffer, headers);
std::string content_type;
decode_headers(decoding_buffer, headers, content_type);
response.header() = headers;
loggers::get_instance().log_to_hexa("Before decoding Body: ", decoding_buffer);
LibItsHttp__MessageBodyTypes::HttpMessageBody body;
if (decode_body(decoding_buffer, body, std::string("application/x-its-request"/*TODO Add Content-Type*/)) == -1) {
if (decode_body(decoding_buffer, body, content_type) == -1) {
response.body().set_to_omit();
} else {
response.body() = OPTIONAL<LibItsHttp__MessageBodyTypes::HttpMessageBody>(body);
......@@ -104,11 +105,12 @@ int http_codec::decode (const OCTETSTRING& data, LibItsHttp__TypesAndValues::Htt
request.version__major() = std::stoi(m[3].str().c_str());
request.version__minor() = std::stoi(m[4].str().c_str());
LibItsHttp__TypesAndValues::HeaderLines headers;
decode_headers(decoding_buffer, headers);
std::string content_type;
decode_headers(decoding_buffer, headers, content_type);
request.header() = headers;
OPTIONAL<LibItsHttp__MessageBodyTypes::HttpMessageBody> body;
body.set_to_omit();
if (decode_body(decoding_buffer, body, std::string("application/x-its-request"/*TODO Add Content-Type*/)) == -1) {
if (decode_body(decoding_buffer, body, content_type) == -1) {
request.body().set_to_omit();
} else {
request.body() = body;
......@@ -156,7 +158,7 @@ int http_codec::encode_request(const LibItsHttp__TypesAndValues::Request& p_requ
if (v.size_of() > 0) {
loggers::get_instance().log_msg("http_codec::encode_request: Processing value ", v[0]);
if (std::string(static_cast<const char*>(header.header__name())).compare("Content-Type") == 0) { // Store it for HTTP body payload encoding
loggers::get_instance().log("http_codec::encode_request: Storing Content-Yype");
loggers::get_instance().log("http_codec::encode_request: Storing Content-Type");
int j = 0;
while (j < v.size_of()) {
content_type += v[j++];
......@@ -312,7 +314,7 @@ int http_codec::encode_response (const LibItsHttp__TypesAndValues::Response& p_r
return 0;
}
int http_codec::decode_headers(TTCN_Buffer& decoding_buffer, LibItsHttp__TypesAndValues::HeaderLines& headers) {
int http_codec::decode_headers(TTCN_Buffer& decoding_buffer, LibItsHttp__TypesAndValues::HeaderLines& headers, std::string& p_content_type) {
loggers::get_instance().log(">>> http_codec::decode_headers");
loggers::get_instance().log_to_hexa("http_codec::decode_headers", decoding_buffer);
......@@ -328,6 +330,14 @@ int http_codec::decode_headers(TTCN_Buffer& decoding_buffer, LibItsHttp__TypesAn
return -1;
}
headers[i++] = header;
if (std::string(static_cast<const char*>(header.header__name())).compare("Content-Type") == 0) {
if (header.header__value().is_present() != 0) {
const PreGenRecordOf::PREGEN__RECORD__OF__CHARSTRING& l = static_cast<const PreGenRecordOf::PREGEN__RECORD__OF__CHARSTRING&>(*header.header__value().get_opt_value());
p_content_type = static_cast<const char*>(l[0]);
} else {
p_content_type = "";
}
}
}
break;
case 1:
......@@ -472,7 +482,7 @@ int http_codec::decode_body(TTCN_Buffer& decoding_buffer, LibItsHttp__MessageBod
OCTETSTRING s(decoding_buffer.get_len() - decoding_buffer.get_pos(), decoding_buffer.get_data() + decoding_buffer.get_pos());
loggers::get_instance().log_msg("http_codec::decode_body: raw body=", s);
// Align the payload length with the specified Content-lenght value
// Align the payload length with the specified Content-Lenght value
loggers::get_instance().log("http_codec::decode_body: _dc.length=%d - body length=%d", _dc.length, s.lengthof());
OCTETSTRING body;
if (_dc.length != 0) {
......@@ -496,6 +506,7 @@ int http_codec::decode_body(TTCN_Buffer& decoding_buffer, LibItsHttp__MessageBod
}
}
loggers::get_instance().log("http_codec::decode_body: counter=%d", counter);
loggers::get_instance().log("http_codec::decode_body: body length=%d", body.lengthof());
body = OCTETSTRING(body.lengthof() - counter, static_cast<const unsigned char*>(body));
if (_dc.chunked){
counter = 0;
......@@ -593,7 +604,10 @@ int http_codec::decode_body(TTCN_Buffer& decoding_buffer, LibItsHttp__MessageBod
loggers::get_instance().log("http_codec::decode_body: Find xml message");
LibItsHttp__XmlMessageBodyTypes::XmlBody xml_body;
// TODO To be refined adding a string identifier to check which codec to use. E.g. held_code.id() returns "xmlns=\"urn:ietf:params:xml:ns:geopriv:held\">"
if (p["decode_str"].find("xmlns=\"urn:ietf:params:xml:ns:geopriv:held\"") != std::string::npos) {
if (
(p["decode_str"].find("=\"urn:ietf:params:xml:ns:geopriv:held\"") != std::string::npos) ||
(p["decode_str"].find("=\"urn:ietf:params:xml:ns:pidf\"") != std::string::npos)
) {
loggers::get_instance().log("http_codec::decode_body: Find 'urn:ietf:params:xml:ns:geopriv:held'");
if (_codecs["held"].get() != nullptr) {
loggers::get_instance().log("http_codec::decode_body: Call 'held_codec'");
......@@ -609,7 +623,7 @@ int http_codec::decode_body(TTCN_Buffer& decoding_buffer, LibItsHttp__MessageBod
xml_body.raw() = CHARSTRING(body.lengthof(), (char*)static_cast<const unsigned char*>(body));
}
message_body.xml__body() = xml_body;
} else if (p["decode_str"].find("xmlns=\"urn:ietf:params:xml:ns:lost1\"") != std::string::npos) {
} else if (p["decode_str"].find("=\"urn:ietf:params:xml:ns:lost1\"") != std::string::npos) {
loggers::get_instance().log("http_codec::decode_body: Find 'urn:ietf:params:xml:ns:lost1'");
if (_codecs["lost"].get() != nullptr) {
loggers::get_instance().log("http_codec::decode_body: Call 'lost_codec'");
......
......@@ -59,7 +59,7 @@ private:
int encode_response (const LibItsHttp__TypesAndValues::Response& p_response, TTCN_Buffer& p_encoding_buffer);
int encode_body(const LibItsHttp__MessageBodyTypes::HttpMessageBody& p_message_body, OCTETSTRING& p_encoding_buffer, const std::string& p_content_type);
int decode_headers(TTCN_Buffer& decoding_buffer, LibItsHttp__TypesAndValues::HeaderLines& headers);
int decode_headers(TTCN_Buffer& decoding_buffer, LibItsHttp__TypesAndValues::HeaderLines& headers, std::string& p_content_type);
int decode_header(CHARSTRING& header_line, LibItsHttp__TypesAndValues::HeaderLine& header);
int decode_body(TTCN_Buffer& decoding_buffer, LibItsHttp__MessageBodyTypes::HttpMessageBody& message_body, const std::string& p_content_type);
int get_line(TTCN_Buffer& buffer, CHARSTRING& to, const bool concatenate_header_lines = false);
......
......@@ -171,9 +171,9 @@ void* pcap_offline_layer::thread() {
if(lh.ts.tv_sec|lh.ts.tv_usec){
long diff = timeval_diff(pkt_header->ts, lh.ts);
if(diff > 0) {
// fprintf(stderr,"<<< pcap_offline_layer::run: Wait %d msec", diff);
loggers::get_instance().log("<<< pcap_offline_layer::run: Wait %d msec", diff);
std::this_thread::sleep_for(std::chrono::milliseconds(diff));
// fprintf(stderr,"<<< pcap_offline_layer::run: Wait done");
loggers::get_instance().log("<<< pcap_offline_layer::run: Wait done");
}
}
}
......
......@@ -28,7 +28,7 @@ tcp_layer::tcp_layer(const std::string & p_type, const std::string & param) : la
}
it = _params.find(params::server);
if (it == _params.cend()) {
_params.insert(std::pair<std::string, std::string>(std::string("server"), "127.0.0.1"));
_params.insert(std::pair<std::string, std::string>(std::string("server"), "127.0.0.1")); // TODO Try using params::server instead of std::string("server")
}
if (!parameter_set(params::server.c_str(), _params[params::server].c_str())) {
loggers::get_instance().warning("tcp_layer::set_parameter: Unprocessed parameter: %s", params::server.c_str());
......@@ -49,7 +49,7 @@ tcp_layer::tcp_layer(const std::string & p_type, const std::string & param) : la
}
}
if (!parameter_set(params::port.c_str(), _params[params::port].c_str())) {
loggers::get_instance().warning("tcp_layer::set_parameter: Unprocessed parameter: %s", params::server.c_str());
loggers::get_instance().warning("tcp_layer::set_parameter: Unprocessed parameter: %s", params::port.c_str());
}
it = _params.find(params::local_port);
if (it == _params.cend()) {
......
......@@ -8,6 +8,9 @@
#include "udp_layer_factory.hh"
#include <sys/ioctl.h>
#include <net/if.h>
udp_layer::udp_layer(const std::string & p_type, const std::string & param) : layer(p_type), PORT(p_type.c_str()), _params(), _saddr{0}, _daddr{0}, _fd(-1), _time_key("udp_layer::Handle_Fd_Event_Readable") {
loggers::get_instance().log(">>> udp_layer::udp_layer: %s, %s", to_string().c_str(), param.c_str());
......@@ -46,16 +49,25 @@ udp_layer::udp_layer(const std::string & p_type, const std::string & param) : la
loggers::get_instance().warning("udp_layer::udp_layer: Failed to set SO_REUSEADDR");
}
// Bind it
/*struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "eth1");
if (setsockopt(_fd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr.ifr_name, strlen(ifr.ifr_name)) < 0) {
close();
loggers::get_instance().error("udp_layer::udp_layer: Failed to bind socket to %s", ifr.ifr_name);
}
loggers::get_instance().log("udp_layer::udp_layer: Bound to device %s", ifr.ifr_name);*/
if(::bind(_fd, (struct sockaddr *)&_saddr, sizeof(_saddr)) < 0) {
close();
loggers::get_instance().error("udp_layer::udp_layer: Failed to bind socket");
}
loggers::get_instance().log("udp_layer::udp_layer: Bound on port %s", _params["src_port"].c_str());
// Pass the device file handler to the polling procedure
Handler_Add_Fd_Read(_fd);
_daddr.sin_family = AF_INET;
_daddr.sin_addr.s_addr = htonl(get_host_id(_params["dst_ip"]));
_daddr.sin_port = htons(std::atoi(_params["dst_port"].c_str()));
_daddr.sin_addr.s_addr = ::htonl(get_host_id(_params["dst_ip"]));
_daddr.sin_port = ::htons(std::atoi(_params["dst_port"].c_str()));
}
......@@ -90,21 +102,29 @@ void udp_layer::receive_data(OCTETSTRING& data, params& params) {
void udp_layer::Handle_Fd_Event_Readable(int fd) {
loggers::get_instance().log(">>> udp_layer::Handle_Fd_Event_Readable: %d", fd);
unsigned char buffer[2048] = {0};
unsigned char buffer[3072] = {0};
struct sockaddr_in from = {0};
socklen_t len = sizeof(struct sockaddr_in); // Length of sender's address
params params;
int result = ::recvfrom(_fd, buffer, 1024, 0, (struct sockaddr *)&from, &len); // FIXME Add support of EAGAIN
loggers::get_instance().log("udp_layer::receive_data: src_port = %s:%d, payload length = %d", ::inet_ntoa(from.sin_addr), ntohs(from.sin_port), result);
params.insert(std::pair<std::string, std::string>(std::string("timestamp"), std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count())));
if (result < 0) {
loggers::get_instance().warning("udp_layer::receive_data: Failed to read data, discard them");
std::vector<unsigned char> acc;
int result = ::recvfrom(_fd, buffer, 3072, 0, (struct sockaddr *)&from, &len);
loggers::get_instance().log("udp_layer::Handle_Fd_Event_Readable: src_port = %s:%d, payload length = %d, errno = %d", ::inet_ntoa(from.sin_addr), ntohs(from.sin_port), result, errno);
while ((result == 3072) && (errno == 0)) {
std::copy((unsigned char*)buffer, (unsigned char*)((unsigned char*)buffer + result), std::back_inserter(acc));
result = ::recvfrom(_fd, buffer, 3072, 0, (struct sockaddr *)&from, &len);
loggers::get_instance().log("udp_layer::Handle_Fd_Event_Readable: src_port = %s:%d, payload length = %d, errno = %d", ::inet_ntoa(from.sin_addr), ntohs(from.sin_port), result, errno);
} // End of 'while' statement
if (errno < 0) {
loggers::get_instance().warning("udp_layer::Handle_Fd_Event_Readable: Failed to read data, discard them: errno=%d", errno);
return;
} else {
std::copy((unsigned char*)buffer, (unsigned char*)((unsigned char*)buffer + result), std::back_inserter(acc));
}
params.insert(std::pair<std::string, std::string>(std::string("timestamp"), std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count())));
float duration;
loggers::get_instance().set_start_time(_time_key);
OCTETSTRING os(result, buffer);
OCTETSTRING os(acc.size(), acc.data());
receive_data(os, params); // TODO Check execution time for decoding operation
loggers::get_instance().set_stop_time(_time_key, duration);
}
......@@ -137,7 +157,7 @@ unsigned long udp_layer::get_host_id(const std::string& p_host_name) {
loggers::get_instance().log("udp_layer::get_host_id: Host name: %s, Host address: %u", p_host_name.c_str(), ip_addr);
return htonl(ip_addr);
return ::htonl(ip_addr);
}
udp_layer_factory udp_layer_factory::_f;
Subproject commit eb78a6e8deb33a581db65cfbf18aa98d2da9b8bc
Subproject commit 5be823f1a8fdd1ab14ae0c0e75f4c313d73bc0fc
Supports Markdown
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