it2s-itss-facilities/src/requests.c

1026 lines
39 KiB
C

#include "requests.h"
#include <it2s-asn/camv2/asn_application.h>
#include <it2s-asn/cpm/CPM.h>
#include <it2s-asn/itss-facilities/FacilitiesReply.h>
#include <it2s-asn/itss-transport/TransportRequest.h>
#include <it2s-asn/itss-facilities/FacilitiesIndication.h>
#include <it2s-asn/itss-security/SecurityRequest.h>
#include <it2s-asn/itss-security/SecurityReply.h>
#include <it2s-tender/packet.h>
#include <it2s-tender/recorder.h>
#include <it2s-tender/space.h>
#include <it2s-tender/time.h>
#include <zmq.h>
#include "cam.h"
#include "cpm.h"
#include "denm.h"
#include "evm.h"
#include "facilities.h"
#include "infrastructure.h"
static void fwd_to_apps(uint8_t* msg, uint16_t msg_len, int its_msg_type, uint32_t iid) {
FacilitiesIndication_t* fi = NULL;
fi = calloc(1, sizeof(FacilitiesIndication_t));
fi->present = FacilitiesIndication_PR_message;
FacilitiesMessageIndication_t *fmi = &fi->choice.message;
fmi->id = iid;
fmi->itsMessageType = its_msg_type;
fmi->data.size = msg_len;
fmi->data.buf = malloc(msg_len);
memcpy(fmi->data.buf, msg, msg_len);
uint8_t buffer[ITSS_SDU_MAX_LEN];
buffer[0] = 4; // Facilities
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_FacilitiesIndication, NULL, fi, buffer + 1, ITSS_SDU_MAX_LEN - 1);
itss_queue_send(facilities.tx_queue, buffer, enc.encoded + 1, ITSS_APPLICATIONS, iid, "FI.message");
}
int facilities_request_result_accepted(void *responder) {
int rv = 0;
FacilitiesReply_t *fr = calloc(1, sizeof(FacilitiesReply_t));
fr->present = FacilitiesReply_PR_message;
fr->choice.message.returnCode = 0;
uint8_t fr_oer[32];
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_FacilitiesReply, NULL, fr, fr_oer, 32);
itss_0send(responder, fr_oer, enc.encoded);
ASN_STRUCT_FREE(asn_DEF_FacilitiesReply, fr);
return rv;
}
int facilities_request_result_rejected(void *responder) {
int rv = 0;
FacilitiesReply_t *fr = calloc(1, sizeof(FacilitiesReply_t));
fr->present = FacilitiesReply_PR_message;
fr->choice.message.returnCode = 1;
uint8_t fr_oer[32];
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_FacilitiesReply, NULL, fr, fr_oer, 32);
itss_0send(responder, fr_oer, enc.encoded);
ASN_STRUCT_FREE(asn_DEF_FacilitiesReply, fr);
return rv;
}
int facilities_request_single_message(void *responder, FacilitiesMessageRequest_t *frm) {
int rv = 0;
TransportRequest_t *tr = calloc(1, sizeof(TransportRequest_t));
tr->present = TransportRequest_PR_packet;
TransportPacketRequest_t *tpr = &tr->choice.packet;
tpr->present = TransportPacketRequest_PR_btp;
BTPPacketRequest_t *bpr = &tpr->choice.btp;
void *its_msg = NULL;
asn_TYPE_descriptor_t *its_msg_def = NULL;
bool fwd = true;
uint64_t transmission_duration = 0;
uint32_t transmission_interval = 0;
uint64_t transmission_start = 0;
switch (frm->itsMessageType) {
case ItsMessageType_cam:
its_msg_def = &asn_DEF_CAM;
its_msg = calloc(1, sizeof(CAM_t));
bpr->destinationPort = Port_cam;
bpr->gn.packetTransportType = PacketTransportType_shb;
bpr->gn.trafficClass = 1;
break;
case ItsMessageType_denm:
its_msg_def = &asn_DEF_DENM;
its_msg = calloc(1, sizeof(DENM_t));
bpr->destinationPort = Port_denm;
bpr->gn.packetTransportType = PacketTransportType_gbc;
bpr->gn.trafficClass = 2;
break;
case ItsMessageType_ivim:
its_msg_def = &asn_DEF_IVIM;
its_msg = calloc(1, sizeof(IVIM_t));
bpr->destinationPort = Port_ivim;
bpr->gn.packetTransportType = PacketTransportType_shb;
bpr->gn.trafficClass = 1;
break;
case ItsMessageType_cpm:
its_msg_def = &asn_DEF_CPM;
its_msg = calloc(1, sizeof(CPM_t));
bpr->destinationPort = Port_cpm;
bpr->gn.packetTransportType = PacketTransportType_shb;
bpr->gn.trafficClass = 2;
break;
case ItsMessageType_evrsr:
its_msg_def = &asn_DEF_EV_RSR;
its_msg = calloc(1, sizeof(EV_RSR_t));
bpr->destinationPort = Port_evrsr;
bpr->gn.packetTransportType = PacketTransportType_shb;
bpr->gn.trafficClass = 1;
break;
default:
log_error("unrecognized FR message type (%lld)", frm->itsMessageType);
facilities_request_result_rejected(responder);
rv = 1;
goto cleanup;
}
asn_dec_rval_t dec = uper_decode_complete(NULL, its_msg_def, (void **)&its_msg, frm->data.buf, frm->data.size);
if (dec.code) {
log_warn("invalid FR %s received", its_msg_def->name);
facilities_request_result_rejected(responder);
rv = 1;
goto cleanup;
}
uint64_t id = 0;
if (!frm->id) {
id = itss_id(frm->data.buf, frm->data.size);
} else if (*frm->id == 0) {
id = itss_id(frm->data.buf, frm->data.size);
}
bool is_update = false;
int managed_msg = false;
if (frm->itsMessageType == ItsMessageType_denm) {
managed_msg = true;
uint8_t event_type = event_manage(its_msg, &id, NULL, 0);
// Do not free its_msg! event_manage takes care of the msg
// id will get set to another val if EVENT NEW or UPDATE or CANCELLATION or NEGATION
if (event_type != EVENT_NEW &&
event_type != EVENT_UPDATE &&
event_type != EVENT_CANCELLATION &&
event_type != EVENT_NEGATION) {
fwd = false;
}
if (event_type == EVENT_UPDATE ||
event_type == EVENT_CANCELLATION ||
event_type == EVENT_NEGATION) {
is_update = true;
}
if (fwd) {
// set stationID
pthread_mutex_lock(&facilities.id.lock);
((DENM_t *)its_msg)->header.stationID = facilities.id.station_id;
pthread_mutex_unlock(&facilities.id.lock);
// Set only one trace
if (facilities.station_type != 15) {
pthread_mutex_lock(&facilities.lightship.lock);
if (facilities.lightship.path_history_len > 0) {
if (!((DENM_t *)its_msg)->denm.location) {
((DENM_t *)its_msg)->denm.location = calloc(1, sizeof(LocationContainer_t));
}
((DENM_t *)its_msg)->denm.location->traces.list.count = 1;
((DENM_t *)its_msg)->denm.location->traces.list.size = 1 * sizeof(void *);
((DENM_t *)its_msg)->denm.location->traces.list.array = malloc(1 * sizeof(void *));
((DENM_t *)its_msg)->denm.location->traces.list.array[0] = calloc(1, sizeof(PathHistory_t));
PathHistory_t *ph = ((DENM_t *)its_msg)->denm.location->traces.list.array[0];
pos_point_t **path_history = facilities.lightship.path_history;
uint16_t path_history_len = facilities.lightship.path_history_len;
ph->list.array = malloc((path_history_len) * sizeof(void *));
ph->list.count = path_history_len;
ph->list.size = (path_history_len) * sizeof(void *);
ph->list.array[0] = calloc(1, sizeof(PathPoint_t));
itss_space_lock();
if (path_history[0]->alt != AltitudeValue_unavailable && epv.space.altitude != AltitudeValue_unavailable) {
ph->list.array[0]->pathPosition.deltaAltitude = path_history[0]->alt - epv.space.altitude;
} else {
ph->list.array[0]->pathPosition.deltaAltitude = DeltaAltitude_unavailable;
}
if (path_history[0]->lat != Latitude_unavailable && epv.space.latitude != Latitude_unavailable) {
ph->list.array[0]->pathPosition.deltaLatitude = path_history[0]->lat - epv.space.latitude;
} else {
ph->list.array[0]->pathPosition.deltaLatitude = DeltaLatitude_unavailable;
}
if (path_history[0]->lon != Longitude_unavailable && epv.space.longitude != Longitude_unavailable) {
ph->list.array[0]->pathPosition.deltaLongitude = path_history[0]->lon - epv.space.longitude;
} else {
ph->list.array[0]->pathPosition.deltaLongitude = DeltaLongitude_unavailable;
}
itss_space_unlock();
ph->list.array[0]->pathDeltaTime = calloc(1, sizeof(PathDeltaTime_t));
*ph->list.array[0]->pathDeltaTime = (itss_time_get() - path_history[0]->ts) / 10;
for (int i = 1; i < path_history_len; ++i) {
ph->list.array[i] = calloc(1, sizeof(PathPoint_t));
if (path_history[i]->alt != AltitudeValue_unavailable && path_history[i - 1]->alt != AltitudeValue_unavailable) {
ph->list.array[i]->pathPosition.deltaAltitude = path_history[i]->alt - path_history[i - 1]->alt;
} else {
ph->list.array[i]->pathPosition.deltaAltitude = DeltaAltitude_unavailable;
}
if (path_history[i]->lat != Latitude_unavailable && path_history[i - 1]->lat != Latitude_unavailable) {
ph->list.array[i]->pathPosition.deltaLatitude = path_history[i]->lat - path_history[i - 1]->lat;
} else {
ph->list.array[i]->pathPosition.deltaLatitude = DeltaLatitude_unavailable;
}
if (path_history[i]->lon != Longitude_unavailable && path_history[i - 1]->lon != Longitude_unavailable) {
ph->list.array[i]->pathPosition.deltaLongitude = path_history[i]->lon - path_history[i - 1]->lon;
} else {
ph->list.array[i]->pathPosition.deltaLongitude = DeltaLongitude_unavailable;
}
ph->list.array[i]->pathDeltaTime = calloc(1, sizeof(PathDeltaTime_t));
*ph->list.array[i]->pathDeltaTime = (path_history[i - 1]->ts - path_history[i]->ts) / 10;
}
}
pthread_mutex_unlock(&facilities.lightship.lock);
}
// get, set retransmission, duration
if (((DENM_t *)its_msg)->denm.management.transmissionInterval) {
transmission_interval = *((uint32_t *)((DENM_t *)its_msg)->denm.management.transmissionInterval);
if (((DENM_t *)its_msg)->denm.management.validityDuration) {
transmission_duration = *((uint32_t *)((DENM_t *)its_msg)->denm.management.validityDuration) * 1000;
} else {
transmission_duration = 600 * 1000;
}
}
}
} else if (frm->itsMessageType == ItsMessageType_ivim) {
managed_msg = true;
uint8_t service_type = service_eval(SERVICE_IVI, its_msg, &id, NULL, 0);
if (service_type != SERVICE_NEW &&
service_type != SERVICE_UPDATE &&
service_type != SERVICE_CANCELLATION &&
service_type != SERVICE_NEGATION) {
fwd = false;
}
if (service_type == SERVICE_UPDATE ||
service_type == SERVICE_CANCELLATION ||
service_type == SERVICE_NEGATION) {
is_update = true;
}
if (fwd) {
uint64_t valid_to, valid_from;
if (!((IVIM_t *)its_msg)->ivi.mandatory.validFrom) {
valid_from = itss_time_get();
} else {
asn_INTEGER2ulong((INTEGER_t *)((IVIM_t *)its_msg)->ivi.mandatory.validFrom, (unsigned long long *)&valid_from);
}
if (!((IVIM_t *)its_msg)->ivi.mandatory.validTo) {
valid_to = valid_from + facilities.infrastructure.default_service_duration;
} else {
asn_INTEGER2ulong((INTEGER_t *)((IVIM_t *)its_msg)->ivi.mandatory.validTo, (unsigned long long *)&valid_to);
}
transmission_start = valid_from;
transmission_interval = facilities.infrastructure.replay_interval;
transmission_duration = valid_to - valid_from;
}
} else if (frm->itsMessageType == ItsMessageType_evrsr) {
managed_msg = true;
}
if (!facilities.replay) {
transmission_interval = 0;
transmission_duration = 0;
}
// Respond to [applications]
facilities_request_result_accepted(responder);
// Forward message to [transport]
if (fwd) {
bpr->btpType = BTPType_btpB;
bpr->id = id;
bpr->data.buf = malloc(2048);
asn_enc_rval_t enc = uper_encode_to_buffer(its_msg_def, NULL, its_msg, bpr->data.buf, 2048);
if (enc.encoded == -1) {
log_error("failed encoding ITS message into UPER (%s)", enc.failed_type->name);
rv = 1;
goto cleanup;
}
bpr->data.size = (enc.encoded + 7) / 8;
bpr->gn.destinationAddress.buf = malloc(6);
for (int i = 0; i < 6; ++i)
bpr->gn.destinationAddress.buf[i] = 0xff;
bpr->gn.destinationAddress.size = 6;
if (transmission_start) {
bpr->gn.repetitionStart = malloc(sizeof(long));
*bpr->gn.repetitionStart = transmission_start;
}
if (transmission_interval) {
bpr->gn.repetitionInterval = malloc(sizeof(long));
*bpr->gn.repetitionInterval = transmission_interval;
}
if (transmission_duration) {
bpr->gn.maximumRepetitionTime = malloc(sizeof(long));
*bpr->gn.maximumRepetitionTime = transmission_duration;
}
if (is_update) {
bpr->gn.isUpdate = malloc(sizeof(long));
*bpr->gn.isUpdate = 1;
}
bpr->gn.securityProfile.sign = true;
uint8_t tr_oer[2048];
tr_oer[0] = 4; // [facilities] service id
enc = oer_encode_to_buffer(&asn_DEF_TransportRequest, NULL, tr, tr_oer + 1, 2047);
if (enc.encoded == -1) {
log_error("failed encoding TR (%s)", enc.failed_type->name);
rv = 1;
goto cleanup;
}
itss_queue_send(facilities.tx_queue, tr_oer, enc.encoded + 1, ITSS_TRANSPORT, id, "TR.packet.btp");
}
// Logging
if (facilities.logging.recorder) {
uint16_t buffer_len = 2048;
uint8_t buffer[buffer_len];
int e = itss_management_record_packet_sdu(
buffer,
buffer_len,
frm->data.buf,
frm->data.size,
id,
itss_time_get(),
ITSS_FACILITIES,
true);
if (e != -1) {
itss_queue_send(facilities.tx_queue, buffer, e, ITSS_MANAGEMENT, id, "MReq.packet.set");
}
}
cleanup:
if (its_msg_def && !managed_msg)
ASN_STRUCT_FREE(*its_msg_def, its_msg);
ASN_STRUCT_FREE(asn_DEF_TransportRequest, tr);
return rv;
}
int facilities_request_active_episodes(void *responder, FacilitiesRequest_t *freq) {
int rv = 0;
FacilitiesReply_t *frep = calloc(1, sizeof(FacilitiesReply_t));
uint8_t *frep_oer = NULL;
frep->present = FacilitiesReply_PR_data;
frep->choice.data.present = FacilitiesDataReply_PR_episodes;
pthread_mutex_lock(&facilities.den.lock);
pthread_mutex_lock(&facilities.infrastructure.lock);
uint16_t nae = facilities.den.n_active_events;
uint16_t nas = facilities.infrastructure.n_active_services;
uint16_t na = 0;
for (int e = 0; e < freq->choice.data.choice.activeEpisodes.list.count; ++e) {
switch (*freq->choice.data.choice.activeEpisodes.list.array[e]) {
case EpisodeType_denm:
na += nae;
break;
case EpisodeType_ivim:
na += nas;
break;
}
}
frep->choice.data.choice.episodes.list.count = na;
frep->choice.data.choice.episodes.list.size = na * sizeof(void *);
frep->choice.data.choice.episodes.list.array = malloc(na * sizeof(void *));
for (int e = 0, j = 0; e < freq->choice.data.choice.activeEpisodes.list.count; ++e) {
switch (*freq->choice.data.choice.activeEpisodes.list.array[e]) {
case EpisodeType_denm:
for (int i = 0, n = 0; n < nae; ++i) {
if (facilities.den.events[i]->enabled && facilities.den.events[i]->state == EVENT_ACTIVE) {
frep->choice.data.choice.episodes.list.array[j] = calloc(1, sizeof(ItsMessage_t));
frep->choice.data.choice.episodes.list.array[j]->itsMessageType = ItsMessageType_denm;
frep->choice.data.choice.episodes.list.array[j]->data.buf = malloc(2048);
asn_enc_rval_t enc = uper_encode_to_buffer(&asn_DEF_DENM, NULL, facilities.den.events[i]->denm, frep->choice.data.choice.episodes.list.array[j]->data.buf, 2048);
if (enc.encoded == -1) { /* encoding shouldn't fail as all saved DENMs are structurally valid */
log_error("failed encoding DENM for FDResult (%s)", enc.failed_type->name);
frep->choice.data.choice.episodes.list.array[j]->data.size = 0;
continue;
}
frep->choice.data.choice.episodes.list.array[j]->data.size = (enc.encoded + 7) / 8;
++j;
++n;
}
}
break;
case EpisodeType_ivim:
for (int i = 0, n = 0; n < nas; ++i) {
if (facilities.infrastructure.services[i]->enabled && facilities.infrastructure.services[i]->state == SERVICE_ACTIVE) {
frep->choice.data.choice.episodes.list.array[j] = calloc(1, sizeof(ItsMessage_t));
frep->choice.data.choice.episodes.list.array[j]->itsMessageType = ItsMessageType_ivim;
frep->choice.data.choice.episodes.list.array[j]->data.buf = malloc(2048);
asn_enc_rval_t enc = uper_encode_to_buffer(&asn_DEF_IVIM, NULL, facilities.infrastructure.services[i]->ivim, frep->choice.data.choice.episodes.list.array[j]->data.buf, 2048);
if (enc.encoded == -1) { /* encoding shouldn't fail as all saved DENMs are structurally valid */
log_error("failed encoding IVIM for FDResult (%s)", enc.failed_type->name);
frep->choice.data.choice.episodes.list.array[j]->data.size = 0;
continue;
}
frep->choice.data.choice.episodes.list.array[j]->data.size = (enc.encoded + 7) / 8;
++j;
++n;
}
}
break;
default:
log_error("unrecognized FR event type (%lld)", *freq->choice.data.choice.activeEpisodes.list.array[e]);
pthread_mutex_unlock(&facilities.den.lock);
pthread_mutex_unlock(&facilities.infrastructure.lock);
facilities_request_result_rejected(responder);
rv = 1;
goto cleanup;
}
}
pthread_mutex_unlock(&facilities.den.lock);
pthread_mutex_unlock(&facilities.infrastructure.lock);
frep_oer = malloc(32768);
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_FacilitiesReply, NULL, frep, frep_oer, 32768);
if (enc.encoded == -1) {
log_error("failed encoding FReply (%s)", enc.failed_type->name);
facilities_request_result_rejected(responder);
rv = 1;
goto cleanup;
}
itss_0send(responder, frep_oer, enc.encoded);
cleanup:
free(frep_oer);
ASN_STRUCT_FREE(asn_DEF_FacilitiesReply, frep);
return rv;
}
int facilities_request_attribute_types(void *responder, FacilitiesRequest_t *freq) {
int rv = 0;
FacilitiesReply_t *frep = calloc(1, sizeof(FacilitiesReply_t));
frep->present = FacilitiesReply_PR_data;
int nra = freq->choice.data.choice.attributeTypes.list.count;
frep->choice.data.present = FacilitiesDataReply_PR_attributes;
frep->choice.data.choice.attributes.list.count = nra;
frep->choice.data.choice.attributes.list.size = sizeof(void *) * nra;
frep->choice.data.choice.attributes.list.array = malloc(sizeof(void *) * nra);
for (int j = 0; j < nra; ++j) {
switch (*freq->choice.data.choice.attributeTypes.list.array[j]) {
case FacilitiesAttributeType_stationId:
frep->choice.data.choice.attributes.list.array[j] = calloc(1, sizeof(FacilitiesAttribute_t));
frep->choice.data.choice.attributes.list.array[j]->data.size = 8;
frep->choice.data.choice.attributes.list.array[j]->data.buf = malloc(8);
pthread_mutex_lock(&facilities.id.lock);
*((uint64_t *)frep->choice.data.choice.attributes.list.array[j]->data.buf) = facilities.id.station_id;
pthread_mutex_unlock(&facilities.id.lock);
break;
default:
log_debug("unrecognized FR attribute type request");
facilities_request_result_rejected(responder);
rv = 1;
goto cleanup;
}
}
uint8_t frep_oer[256];
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_FacilitiesReply, NULL, frep, frep_oer, 256);
if (enc.encoded == -1) {
log_error("failed encoding FReply (%s)", enc.failed_type->name);
facilities_request_result_rejected(responder);
rv = 1;
goto cleanup;
}
itss_0send(responder, frep_oer, enc.encoded);
cleanup:
ASN_STRUCT_FREE(asn_DEF_FacilitiesReply, frep);
return rv;
}
int facilities_request_loaded_protected_zones(void *responder, FacilitiesRequest_t *freq) {
int rv = 0;
FacilitiesReply_t *frep = calloc(1, sizeof(FacilitiesReply_t));
frep->present = FacilitiesReply_PR_data;
frep->choice.data.present = FacilitiesDataReply_PR_protectedCommunicationZones;
pthread_mutex_lock(&facilities.lightship.lock);
frep->choice.data.choice.protectedCommunicationZones.list.count = facilities.lightship.protected_zones.pz_len;
frep->choice.data.choice.protectedCommunicationZones.list.size = facilities.lightship.protected_zones.pz_len * sizeof(void *);
frep->choice.data.choice.protectedCommunicationZones.list.array = malloc(facilities.lightship.protected_zones.pz_len * sizeof(void *));
uint8_t buf[256];
for (int z = 0; z < facilities.lightship.protected_zones.pz_len; ++z) {
frep->choice.data.choice.protectedCommunicationZones.list.array[z] = calloc(1, sizeof(ProtectedCommunicationZone_t));
asn_enc_rval_t enc = uper_encode_to_buffer(&asn_DEF_ProtectedCommunicationZone, NULL, facilities.lightship.protected_zones.pz[z], buf, 256);
uper_decode_complete(NULL, &asn_DEF_ProtectedCommunicationZone, (void **)&frep->choice.data.choice.protectedCommunicationZones.list.array[z], buf, (enc.encoded + 7) / 8);
}
pthread_mutex_unlock(&facilities.lightship.lock);
uint8_t frep_oer[1024];
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_FacilitiesReply, NULL, frep, frep_oer, 1024);
if (enc.encoded == -1) {
log_error("failed encoding FDResult (%s)", enc.failed_type->name);
facilities_request_result_rejected(responder);
rv = 1;
goto cleanup;
}
itss_0send(responder, frep_oer, enc.encoded);
cleanup:
ASN_STRUCT_FREE(asn_DEF_FacilitiesReply, frep);
return rv;
}
int facilities_request_chaininfo_set(void *responder, ChainInformationSet_t *cis) {
int rv = 0;
pthread_mutex_lock(&facilities.coordination.lock);
facilities.coordination.chain.id = cis->idSet;
for (int i = 0; i < cis->linkSet.list.count; ++i) {
memcpy(
facilities.coordination.chain.links[i],
cis->linkSet.list.array[i]->buf,
cis->linkSet.list.array[i]->size);
}
facilities.coordination.chain.n_links = cis->linkSet.list.count;
// TODO region
pthread_mutex_unlock(&facilities.coordination.lock);
FacilitiesReply_t *frep = calloc(1, sizeof(FacilitiesReply_t));
frep->present = FacilitiesReply_PR_data;
frep->choice.data.present = FacilitiesDataReply_PR_chainInfoSet;
frep->choice.data.choice.chainInfoSet = FacilitiesResultCode_accepted;
uint8_t frep_oer[1024];
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_FacilitiesReply, NULL, frep, frep_oer, 1024);
if (enc.encoded == -1) {
log_error("failed encoding FDResult (%s)", enc.failed_type->name);
facilities_request_result_rejected(responder);
rv = 1;
goto cleanup;
}
itss_0send(responder, frep_oer, enc.encoded);
cleanup:
ASN_STRUCT_FREE(asn_DEF_FacilitiesReply, frep);
return rv;
}
int transport_indication_btp(BTPPacketIndication_t* bpi, void** security_socket) {
int rv = 0;
FacilitiesIndication_t *fi = NULL;
asn_TYPE_descriptor_t *its_msg_descriptor = NULL;
void *its_msg = NULL;
int its_msg_type = 0;
bool fwd = false;
bool stored = false;
uint16_t buf_len = 2048;
uint8_t buf[buf_len];
uint64_t id = 0;
// Parse message
switch (bpi->destinationPort) {
case Port_cam:
its_msg_descriptor = &asn_DEF_CAM;
its_msg = calloc(1, sizeof(CAM_t));
its_msg_type = messageID_cam;
break;
case Port_denm:
its_msg_descriptor = &asn_DEF_DENM;
its_msg = calloc(1, sizeof(DENM_t));
its_msg_type = messageID_denm;
break;
case Port_ivim:
its_msg_descriptor = &asn_DEF_IVIM;
its_msg = calloc(1, sizeof(IVIM_t));
its_msg_type = messageID_ivim;
break;
case Port_cpm:
its_msg_descriptor = &asn_DEF_CPM;
its_msg = calloc(1, sizeof(CPM_t));
its_msg_type = 14;
break;
case Port_saem:
its_msg_descriptor = &asn_DEF_SAEM;
its_msg = calloc(1, sizeof(SAEM_t));
its_msg_type = messageID_saem;
break;
case 7011: /* tolling */
its_msg_descriptor = &asn_DEF_TPM;
its_msg = calloc(1, sizeof(TPM_t));
its_msg_type = 117;
break;
case 2043: /* maneuvers */
its_msg_descriptor = &asn_DEF_VCM;
its_msg = calloc(1, sizeof(VCM_t));
its_msg_type = 43;
break;
case Port_poi: /* EVCSNM */
its_msg_descriptor = &asn_DEF_EvcsnPdu;
its_msg = calloc(1, sizeof(EvcsnPdu_t));
its_msg_type = messageID_evcsn;
break;
case Port_evrsr: /* EVRSRM */
its_msg_descriptor = &asn_DEF_EV_RSR;
its_msg = calloc(1, sizeof(EV_RSR_t));
its_msg_type = messageID_evcsn;
break;
default:
log_debug("messsage with unhandled BTP port received (%lld), ignoring", bpi->destinationPort);
goto cleanup;
}
asn_dec_rval_t dec = uper_decode_complete(NULL, its_msg_descriptor, (void **)&its_msg, bpi->data.buf, bpi->data.size);
if (dec.code) {
log_debug("<- invalid %s received", its_msg_descriptor->name);
rv = 1;
goto cleanup;
}
// Get permisisons
uint8_t *ssp = NULL;
uint16_t ssp_len;
if (bpi->gn.securityPermissions) {
ssp = bpi->gn.securityPermissions->ssp.buf;
ssp_len = bpi->gn.securityPermissions->ssp.size;
}
// Get neighbour certificate ID
uint8_t *neighbour_cert = bpi->gn.securityNeighbour ? bpi->gn.securityNeighbour->buf : NULL;
// Manage message
switch (bpi->destinationPort) {
case Port_cam:
switch (check_cam(bpi, its_msg, ssp, ssp_len)) {
case CAM_OK:
fwd = true;
break;
case CAM_INVALID:
case CAM_BAD_PERMISSIONS:
default:
break;
}
break;
case Port_denm:
#ifdef DEBUG
uint8_t *xml_denm = malloc(32768);
asn_enc_rval_t rve = xer_encode_to_buffer(xml_denm, 32768, 0x02, &asn_DEF_DENM, its_msg);
log_debug("DENM XER %d: %.*s", (int)rve.encoded, (int)rve.encoded, xml_denm);
free(xml_denm);
#endif
stored = true;
switch (event_manage(its_msg, &id, ssp, ssp_len)) {
case EVENT_NEW:
case EVENT_CANCELLATION:
case EVENT_NEGATION:
case EVENT_UPDATE:
case EVENT_NUMBER_EXCEEDED:
fwd = true;
break;
case EVENT_INVALID:
case EVENT_PASSED:
case EVENT_REPEATED:
case EVENT_BAD_PERMISSIONS:
break;
}
break;
case Port_ivim:
stored = true;
switch (service_eval(SERVICE_IVI, its_msg, &id, ssp, ssp_len)) {
case SERVICE_NEW:
case SERVICE_CANCELLATION:
case SERVICE_NEGATION:
case SERVICE_UPDATE:
case SERVICE_NUMBER_EXCEEDED:
fwd = true;
break;
case SERVICE_INVALID:
case SERVICE_REPEATED:
case SERVICE_PASSED:
case SERVICE_BAD_PERMISSIONS:
default:
break;
}
break;
case Port_saem:
switch (saem_check(its_msg, neighbour_cert)) {
case SAEM_NEW:
fwd = true;
break;
default:
break;
}
break;
case 7011:
if (facilities.tolling.enabled) {
tpm_recv(its_msg, security_socket, neighbour_cert, NULL);
fwd = true;
}
break;
case 2043:
fwd_to_apps(bpi->data.buf, bpi->data.size, bpi->destinationPort, bpi->id);
fwd = false;
if (facilities.coordination.active) {
vcm_check(its_msg);
}
break;
case Port_poi:
if (facilities.evm_args.activate) {
evcsnm_check(its_msg);
fwd = true;
}
break;
case Port_evrsr:
if (facilities.evm_args.activate) {
evrsrm_check(its_msg);
fwd = true;
EV_RSR_t *evrsr_request = (EV_RSR_t *)its_msg;
evrsrm_recv(evrsr_request);
}
break;
default:
break;
}
// Forward to [applications]
if (fwd) {
fwd_to_apps(bpi->data.buf, bpi->data.size, bpi->destinationPort, bpi->id);
}
// Logging
if (facilities.logging.dbms) {
pthread_mutex_lock(&facilities.id.lock);
uint32_t station_id = facilities.id.station_id;
pthread_mutex_unlock(&facilities.id.lock);
itss_db_add(facilities.logging.dbms, station_id, bpi->id, false, its_msg_type, NULL, bpi->data.buf, bpi->data.size);
}
if (facilities.logging.recorder) {
int e = itss_management_record_packet_sdu(
buf,
buf_len,
bpi->data.buf,
bpi->data.size,
bpi->id,
itss_time_get(),
ITSS_FACILITIES,
false);
if (e != -1) {
itss_queue_send(facilities.tx_queue, buf, e, ITSS_MANAGEMENT, bpi->id, "MReq.packet.set");
}
}
cleanup:
if (its_msg && !stored) {
ASN_STRUCT_FREE(*its_msg_descriptor, its_msg);
}
ASN_STRUCT_FREE(asn_DEF_FacilitiesIndication, fi);
return rv;
}
int transport_indication_tcp(TCPPacketIndication_t* tpi, void** security_socket) {
int rv = 0;
uint16_t buf_len = 2048;
uint8_t buf[buf_len];
SecurityRequest_t* sreq = NULL;
SecurityReply_t* srep = NULL;
TransportRequest_t* tr = NULL;
FacilitiesIndication_t* fi = NULL;
void* its_msg;
sreq = calloc(1, sizeof(SecurityRequest_t));
sreq->present = SecurityRequest_PR_tlsRecv;
sreq->choice.tlsRecv.data.size = tpi->data.size;
sreq->choice.tlsRecv.data.buf = malloc(tpi->data.size);
memcpy(sreq->choice.tlsRecv.data.buf, tpi->data.buf, tpi->data.size);
uint64_t id = tpi->id;
pthread_mutex_lock(&facilities.tolling.lock);
tlsc_t *tlsc = tolling_tlsc_get(tpi->sourceAddress->buf, 7011);
if (tlsc) {
id = tlsc->id;
} else {
tlsc = tolling_tlsc_new(tpi->sourceAddress->buf, 7011);
id = tlsc->id;
}
++tlsc->nmsg;
pthread_mutex_unlock(&facilities.tolling.lock);
sreq->choice.tlsSend.connId = id;
uint8_t b_tx[2048], b_rx[2048];
b_tx[0] = 4;
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_SecurityRequest, NULL, sreq, b_tx + 1, 2047);
log_debug("->[security] SecurityRequest.tlsRecv (%ldB)", enc.encoded + 1);
itss_0send(*security_socket, b_tx, enc.encoded + 1);
int32_t rl = itss_0recv_rt(security_socket, b_rx, 2048, b_tx, enc.encoded + 1, 1000);
log_debug("<-[security] SecurityReply.tlsRecv (%dB)", rl);
if (oer_decode(NULL, &asn_DEF_SecurityReply, (void **)&srep, b_rx, rl).code) {
log_error("SecurityReply.tlsRecv decode failure");
rv = 1;
goto cleanup;
}
if (srep->returnCode == SecurityReplyReturnCode_rejected) {
log_error("SecurityReply.tlsRecv rejected");
SecurityRequest_t *sREQ = calloc(1, sizeof(SecurityRequest_t));
sREQ->present = SecurityRequest_PR_tlsShutdown;
sREQ->choice.tlsShutdown.connId = id;
b_tx[0] = 4;
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_SecurityRequest, NULL, sREQ, b_tx + 1, 2047);
log_debug("->[security] SecurityRequest.tlsShutdown (%ldB)", enc.encoded + 1);
itss_0send(*security_socket, b_tx, enc.encoded + 1);
int32_t rl = itss_0recv_rt(security_socket, b_rx, 2048, b_tx, enc.encoded + 1, 1000);
log_debug("<-[security] SecurityReply.tlsShutdown (%dB)", rl);
rv = 1;
goto cleanup;
}
log_debug("[tolling] tls n-msg:%d state:%d", tlsc->nmsg, tlsc->state);
// Forward to [transport]
if (srep->data->choice.tlsRecv.state != 1) {
tr = calloc(1, sizeof(TransportRequest_t));
tr->present = TransportRequest_PR_packet;
tr->choice.packet.present = TransportPacketRequest_PR_tcp;
TCPPacketRequest_t *tpr = &tr->choice.packet.choice.tcp;
tpr->data.size = srep->data->choice.tlsRecv.data.size;
tpr->data.buf = malloc(srep->data->choice.tlsRecv.data.size);
memcpy(tpr->data.buf, srep->data->choice.tlsRecv.data.buf, srep->data->choice.tlsRecv.data.size);
tpr->sourcePort = tpi->destinationPort;
tpr->destinationPort = tpi->sourcePort;
tpr->destinationAddress = calloc(1, sizeof(OCTET_STRING_t));
tpr->destinationAddress->buf = malloc(16);
tpr->destinationAddress->size = 16;
memcpy(tpr->destinationAddress->buf, tpi->sourceAddress->buf, 16);
tpr->destinationPort = 7011;
tpr->sourcePort = 7011;
if (facilities.tolling.protocol.p == TOLLING_PROTOCOL_TLS_GN ||
(facilities.tolling.protocol.p == TOLLING_PROTOCOL_TLS_SHS && tlsc->nmsg < 2)) {
tpr->gn = calloc(1, sizeof(GeonetworkingOutboundOptions_t));
tpr->gn->packetTransportType = PacketTransportType_shb;
tpr->gn->destinationAddress.buf = calloc(1, 6);
tpr->gn->destinationAddress.size = 6;
}
tpr->id = itss_id(tpr->data.buf, tpr->data.size);
buf[0] = 4;
enc = oer_encode_to_buffer(&asn_DEF_TransportRequest, NULL, tr, buf + 1, buf_len - 1);
if (enc.encoded == -1) {
log_error("TransportRequest encoding fail");
rv = 1;
goto cleanup;
}
itss_queue_send(facilities.tx_queue, buf, enc.encoded + 1, ITSS_TRANSPORT, tpr->id, "TR.packet.tcp");
} else {
if (facilities.tolling.enabled && srep->data->choice.tlsRecv.data.size) {
asn_dec_rval_t dec = uper_decode_complete(NULL, &asn_DEF_TPM, (void **)&its_msg, srep->data->choice.tlsRecv.data.buf, srep->data->choice.tlsRecv.data.size);
if (dec.code) {
log_debug("<- invalid TPM received");
rv = 1;
goto cleanup;
}
if (!dec.code) {
tpm_recv(its_msg, security_socket, NULL, tpi->sourceAddress->buf);
// Fwd to [applications]
fi = calloc(1, sizeof(FacilitiesIndication_t));
fi->present = FacilitiesIndication_PR_message;
FacilitiesMessageIndication_t *fmi = &fi->choice.message;
fmi->id = id;
fmi->itsMessageType = 7011;
fmi->data.size = srep->data->choice.tlsRecv.data.size;
fmi->data.buf = malloc(srep->data->choice.tlsRecv.data.size);
memcpy(fmi->data.buf, srep->data->choice.tlsRecv.data.buf, srep->data->choice.tlsRecv.data.size);
uint8_t buffer[ITSS_SDU_MAX_LEN];
buffer[0] = 4; // Facilities
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_FacilitiesIndication, NULL, fi, buffer + 1, ITSS_SDU_MAX_LEN - 1);
itss_queue_send(facilities.tx_queue, buffer, enc.encoded + 1, ITSS_APPLICATIONS, id, "FI.message");
// Logging
if (facilities.logging.dbms) {
pthread_mutex_lock(&facilities.id.lock);
uint32_t station_id = facilities.id.station_id;
pthread_mutex_unlock(&facilities.id.lock);
itss_db_add(facilities.logging.dbms, station_id, id, false, 117, NULL, srep->data->choice.tlsRecv.data.buf, srep->data->choice.tlsRecv.data.size);
}
if (facilities.logging.recorder) {
int e = itss_management_record_packet_sdu(
buf,
buf_len,
srep->data->choice.tlsRecv.data.buf,
srep->data->choice.tlsRecv.data.size,
id,
itss_time_get(),
ITSS_FACILITIES,
false);
if (e != -1) {
itss_queue_send(facilities.tx_queue, buf, e, ITSS_MANAGEMENT, id, "MReq.packet.set");
}
}
}
}
}
cleanup:
ASN_STRUCT_FREE(asn_DEF_FacilitiesIndication, fi);
ASN_STRUCT_FREE(asn_DEF_SecurityRequest, sreq);
ASN_STRUCT_FREE(asn_DEF_SecurityReply, srep);
ASN_STRUCT_FREE(asn_DEF_TransportRequest, tr);
return rv;
}
int transport_indication_udp() {}