diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..6deeb6d --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,15 @@ +image: archlinux + +stages: + - deploy release + - deploy debug + +deploy release: + stage: deploy release + script: + - curl http://192.168.94.221:3000/it2s-itss-facilities-git + +deploy debug: + stage: deploy debug + script: + - curl http://192.168.94.221:3000/it2s-itss-facilities-debug-git diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6f54c83..59c661c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,6 +10,7 @@ ADD_EXECUTABLE(it2s-itss-facilities TARGET_LINK_LIBRARIES(it2s-itss-facilities -lit2s-asn-itss-facilities -lit2s-asn-itss-transport + -lit2s-asn-itss-security -lzmq -lit2s-gps -lpthread diff --git a/src/cam.c b/src/cam.c index 3d95987..3d66e46 100644 --- a/src/cam.c +++ b/src/cam.c @@ -71,7 +71,9 @@ static int mk_cam(facilities_t* facilities, uint8_t *cam, uint32_t *cam_len) { cam_tx->header.protocolVersion = 2; cam_tx->header.messageID = ItsPduHeader__messageID_cam; - cam_tx->header.stationID = 0; + pthread_mutex_lock(&facilities->lock); + cam_tx->header.stationID = facilities->station_id; + pthread_mutex_unlock(&facilities->lock); cam_tx->cam.camParameters.basicContainer.stationType = facilities->station_type; if (facilities->station_type != StationType_roadSideUnit) { diff --git a/src/config.c b/src/config.c index 0198a20..5aaf18c 100644 --- a/src/config.c +++ b/src/config.c @@ -86,10 +86,10 @@ int itss_config(void* facilities_s, char* config_file) { fclose(fp); // Tables - toml_table_t *general, *general_station_id; + toml_table_t *general, *security; if (0 == (general = toml_table_in(conf, "general"))) {syslog_err("[facilities] [config] failed locating [general] table"); return 1;} - if (0 == (general_station_id = toml_table_in(general, "station-id"))) {syslog_err("CONFIG: Failed locating [general] station-id table"); return 1;} + if (0 == (security = toml_table_in(conf, "security"))) {syslog_err("[facilities] [config] failed locating [security] table"); return 1;} // Values // General @@ -106,13 +106,14 @@ int itss_config(void* facilities_s, char* config_file) { free(itss_type); int station_id_random = 1; - rv = extract_val_bool(&station_id_random, general_station_id, "random"); + rv = extract_val_bool(&station_id_random, security, "id-random"); + facilities->id_random = station_id_random; if (station_id_random) { srand(time(NULL)); facilities->station_id = rand(); } else { int64_t station_id_number; - rv = extract_val_int(&station_id_number, general_station_id, "number"); + rv = extract_val_int(&station_id_number, security, "station-id"); facilities->station_id = station_id_number; } diff --git a/src/facilities.c b/src/facilities.c index 7e505db..ac2ff2e 100644 --- a/src/facilities.c +++ b/src/facilities.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include @@ -388,7 +390,9 @@ static int facilities_request(facilities_t *facilities, void* responder, uint8_t fdres->result->choice.attributes.list.array[j] = calloc(1, sizeof(FacilitiesAttribute_t) ); fdres->result->choice.attributes.list.array[j]->data.size = 8; fdres->result->choice.attributes.list.array[j]->data.buf = malloc(8); + pthread_mutex_lock(&facilities->lock); *((uint64_t*) fdres->result->choice.attributes.list.array[j]->data.buf) = facilities->station_id; + pthread_mutex_unlock(&facilities->lock); break; default: syslog_debug("[facilities] unrecognized FDR attribute type request"); @@ -428,6 +432,87 @@ static int facilities_request(facilities_t *facilities, void* responder, uint8_t return rv; } + +static int security_indication(facilities_t *facilities, void* responder_secured, uint8_t *msg, uint32_t msg_len) { + int rv = 0; + + SecurityIndication_t* si = calloc(1, sizeof(SecurityIndication_t)); + SecurityResponse_t* sr = calloc(1, sizeof(SecurityResponse_t)); + + uint8_t buffer[64]; + + asn_enc_rval_t enc; + + asn_dec_rval_t dec = oer_decode(NULL, &asn_DEF_SecurityIndication, (void**) &si, msg, msg_len); + if (dec.code) { + syslog_err("[networking]<- invalid SIndication received"); + rv = 1; + goto cleanup; + } + + switch (si->choice.idChangeEvent.command) { + case SecurityIdChangeEventType_prepare: + pthread_mutex_lock(&facilities->lock); + + break; + case SecurityIdChangeEventType_commit: + ; + + facilities->station_id = rand(); + pthread_mutex_unlock(&facilities->lock); + + break; + case SecurityIdChangeEventType_abort: + pthread_mutex_unlock(&facilities->lock); + + break; + default: + syslog_err("[networking]<- unhandled idChangeEvent command type"); + rv = 1; + goto cleanup; + + } + + sr->present = SecurityResponse_PR_idChangeEvent; + sr->choice.idChangeEvent.returnCode = 0; + enc = oer_encode_to_buffer(&asn_DEF_SecurityResponse, NULL, sr, buffer, 64); + zmq_send(responder_secured, buffer, enc.encoded, 0); + + +cleanup: + if (rv) { + sr->present = SecurityResponse_PR_idChangeEvent; + sr->choice.idChangeEvent.returnCode = 1; + enc = oer_encode_to_buffer(&asn_DEF_SecurityResponse, NULL, sr, buffer, 64); + zmq_send(responder_secured, buffer, enc.encoded, 0); + zmq_recv(responder_secured, buffer, 64, 0); + } + + ASN_STRUCT_FREE(asn_DEF_SecurityResponse, sr); + ASN_STRUCT_FREE(asn_DEF_SecurityIndication, si); + + return rv; +} + + +static void* securer(void *fc) { + int rv = 0; + + facilities_t *facilities = (facilities_t*) fc; + + void *responder_secured = zmq_socket(facilities->ctx, ZMQ_REP); + int rc = zmq_bind(responder_secured, FACILITIES_SECURED_ADDRESS); + + uint8_t buffer[PACKET_MAX_LEN]; + + while (!facilities->exit) { + zmq_recv(responder_secured, buffer, PACKET_MAX_LEN, 0); + rv = security_indication(facilities, responder_secured, buffer, PACKET_MAX_LEN); + } + + return NULL; +} + void* tx(void* fc) { facilities_t *facilities = (facilities_t*) fc; @@ -483,6 +568,8 @@ int main() { facilities_t facilities; facilities.exit = false; + pthread_mutex_init(&facilities.lock, NULL); + struct stat st = {0}; if (stat("/tmp/itss", &st) == -1) { mkdir("/tmp/itss", 0777); @@ -498,6 +585,9 @@ int main() { void *responder = zmq_socket(context, ZMQ_REP); int rc = zmq_bind(responder, FACILITIES_ADDRESS); + void *security_socket = zmq_socket(context, ZMQ_REQ); + rc = zmq_bind(security_socket, SECURITY_ADDRESS); + facilities.tx_queue = queue_init(); facilities.lightship = lightship_init(); @@ -510,6 +600,9 @@ int main() { // Tx pthread_create(&facilities.transmitting, NULL, tx, (void*) &facilities); + // Tx + pthread_create(&facilities.securing, NULL, securer, (void*) &facilities); + // CA pthread_create(&facilities.ca_service, NULL, ca_service, (void*) &facilities); diff --git a/src/facilities.h b/src/facilities.h index b920f77..7b508e0 100644 --- a/src/facilities.h +++ b/src/facilities.h @@ -10,14 +10,17 @@ #include "queue.h" #define FACILITIES_ADDRESS "ipc:///tmp/itss/facilities" +#define FACILITIES_SECURED_ADDRESS "ipc:///tmp/itss/facilities-secured" #define TRANSPORT_ADDRESS "ipc:///tmp/itss/transport" #define APPLICATION_ADDRESS "ipc:///tmp/itss/application" +#define SECURITY_ADDRESS "ipc:///tmp/itss/security" typedef struct facilities { pthread_t ca_service; pthread_t den_service; pthread_t infrastructure_service; pthread_t transmitting; + pthread_t securing; // ZMQ void* ctx; @@ -36,7 +39,9 @@ typedef struct facilities { int station_type; + pthread_mutex_t lock; uint64_t station_id; + bool id_random; bool exit; } facilities_t;