This commit is contained in:
emanuel 2021-08-20 17:52:08 +01:00
parent 1b0c31f9bb
commit e5fef64fd0
2 changed files with 45 additions and 27 deletions

View File

@ -13,6 +13,8 @@
#include <itss-management/ManagementRequest.h> #include <itss-management/ManagementRequest.h>
#include <itss-management/ManagementReply.h> #include <itss-management/ManagementReply.h>
#include <itss-security/SecurityRequest.h>
#include <itss-security/SecurityReply.h>
#define syslog_info(msg, ...) syslog(LOG_INFO, "%s:%d [" msg "]", __func__, __LINE__, ##__VA_ARGS__) #define syslog_info(msg, ...) syslog(LOG_INFO, "%s:%d [" msg "]", __func__, __LINE__, ##__VA_ARGS__)
#define syslog_emerg(msg, ...) syslog(LOG_EMERG, "%s:%d [" msg "]", __func__, __LINE__, ##__VA_ARGS__) #define syslog_emerg(msg, ...) syslog(LOG_EMERG, "%s:%d [" msg "]", __func__, __LINE__, ##__VA_ARGS__)
@ -195,7 +197,7 @@ int facilities_config(void* facilities_s) {
} else if (!strcmp("roadSideUnit", config->general.itss_type)) { } else if (!strcmp("roadSideUnit", config->general.itss_type)) {
facilities->station_type = 15; facilities->station_type = 15;
} else { } else {
syslog_err("[facilities] [config] unrecognized ITSS type, running as OBU"); syslog_err("[facilities] [config] unrecognized ITS-S type, running as OBU");
facilities->station_type = 5; facilities->station_type = 5;
} }
@ -206,8 +208,43 @@ int facilities_config(void* facilities_s) {
facilities->id.change.random = config->security.identity.random; facilities->id.change.random = config->security.identity.random;
if (facilities->id.change.random) { if (facilities->id.change.random) {
srand(time(NULL)); // Ask [security] for station id
facilities->id.value = rand();
SecurityRequest_t* sREQ = calloc(1, sizeof(SecurityRequest_t));
SecurityReply_t* sREP = NULL;
sREQ->present = SecurityRequest_PR_ids;
sREQ->choice.ids.list.count = 1;
sREQ->choice.ids.list.size = sizeof(void*);
sREQ->choice.ids.list.array = malloc(sizeof(void*));
sREQ->choice.ids.list.array[0] = calloc(1, sizeof(SecurityIdType_t));
*sREQ->choice.ids.list.array[0] = SecurityIdType_stationId;
uint8_t b_sdu[256];
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_SecurityRequest, NULL, sREQ, b_sdu, 256);
void* ss = zmq_socket(facilities->zmq.ctx, ZMQ_REQ);
zmq_connect(ss, facilities->zmq.security_address);
zmq_send(ss, b_sdu, enc.encoded, 0);
zmq_recv(ss, b_sdu, 256, 0);
zmq_close(ss);
asn_dec_rval_t dec = oer_decode(NULL, &asn_DEF_SecurityReply, (void**) &sREP, b_sdu, 256);
if (sREP->returnCode == SecurityReplyReturnCode_rejected) {
// TODO handle it
goto cleanup;
}
for (int i = 0; i < sREP->data->choice.ids.list.count; ++i) {
if (sREP->data->choice.ids.list.array[i]->present == SecurityId_PR_stationId) {
facilities->id.value = sREP->data->choice.ids.list.array[i]->choice.stationId;
}
}
ASN_STRUCT_FREE(asn_DEF_SecurityRequest, sREQ);
ASN_STRUCT_FREE(asn_DEF_SecurityReply, sREP);
} else { } else {
facilities->id.value = config->security.identity.station_id; facilities->id.value = config->security.identity.station_id;
} }

View File

@ -347,7 +347,11 @@ static int security_indication(facilities_t *facilities, void* responder_secured
pthread_mutex_unlock(&facilities->lightship->lock); pthread_mutex_unlock(&facilities->lightship->lock);
// Change Station ID // Change Station ID
facilities->id.value = rand(); for (int i = 0; i < si->choice.idChangeEvent.ids.list.count; ++i) {
if (si->choice.idChangeEvent.ids.list.array[i]->present == SecurityId_PR_stationId) {
facilities->id.value = si->choice.idChangeEvent.ids.list.array[i]->choice.stationId;
}
}
facilities->id.change.stage = ID_CHANGE_INACTIVE; facilities->id.change.stage = ID_CHANGE_INACTIVE;
@ -377,29 +381,6 @@ static int security_indication(facilities_t *facilities, void* responder_secured
enc = oer_encode_to_buffer(&asn_DEF_SecurityResponse, NULL, sr, buffer, 64); enc = oer_encode_to_buffer(&asn_DEF_SecurityResponse, NULL, sr, buffer, 64);
zmq_send(responder_secured, buffer, enc.encoded, 0); zmq_send(responder_secured, buffer, enc.encoded, 0);
if (id_changed) {
// Inform management
ManagementRequest_t* mreq = calloc(1, sizeof(ManagementRequest_t));
mreq->present = ManagementRequest_PR_attributes;
mreq->choice.attributes.present = ManagementRequestAttributes_PR_set;
mreq->choice.attributes.choice.set.stationID = malloc(sizeof(long));
*mreq->choice.attributes.choice.set.stationID = facilities->id.value;
uint8_t b_oer[128];
asn_enc_rval_t enc = asn_encode_to_buffer(NULL, ATS_CANONICAL_OER, &asn_DEF_ManagementRequest, mreq, b_oer, 128);
if (enc.encoded != -1) {
int wait_ms = 1000;
void* management_socket = zmq_socket(facilities->zmq.ctx, ZMQ_REQ);
zmq_setsockopt(management_socket, ZMQ_RCVTIMEO, &wait_ms, sizeof(int));
zmq_connect(management_socket, facilities->zmq.management_address);
zmq_send(management_socket, b_oer, enc.encoded, 0);
uint8_t code;
rv = zmq_recv(management_socket, &code, 1, 0);
if (rv == -1) {syslog_err("[facilities]-> timeout sending MReq set.stationId to ->[management]");}
zmq_close(management_socket);
}
ASN_STRUCT_FREE(asn_DEF_ManagementRequest, mreq);
}
pthread_mutex_unlock(&facilities->id.change.lock); pthread_mutex_unlock(&facilities->id.change.lock);
cleanup: cleanup: