From e91d082e398fcfc8bbe0dcc0d26f7f91b0d3df62 Mon Sep 17 00:00:00 2001 From: emanuel Date: Mon, 13 Jun 2022 18:50:28 +0100 Subject: [PATCH] TPM closed system efforts --- src/config.c | 7 +- src/facilities.h | 2 +- src/indications.c | 2 +- src/saem.c | 4 +- src/tpm.c | 327 +++++++++++++++++++++++++++++++++------------- src/tpm.h | 16 +-- 6 files changed, 250 insertions(+), 108 deletions(-) diff --git a/src/config.c b/src/config.c index 7e2bf38..ca3dcf7 100644 --- a/src/config.c +++ b/src/config.c @@ -402,12 +402,13 @@ int facilities_config(void* facilities_s) { asn_dec_rval_t dec = xer_decode(NULL, &asn_DEF_TollingPaymentInfo, (void**) &ti, ti_xml, size); if (!dec.code) { - facilities->tolling.infos.z[i] = calloc(1, sizeof(tolling_info_s)); + facilities->tolling.infos.z[i] = calloc(1, sizeof(tolling_info_t)); facilities->tolling.infos.z[i]->asn = ti; ++facilities->tolling.infos.length; ++i; - syslog_debug("[facilities] [config] loaded tolling info with %d payment option(s)", - (ti->fiatInfos ? ti->fiatInfos->list.count : 0) + (ti->ledgerInfos ? ti->ledgerInfos->list.count : 0) + syslog_debug("[facilities] [config] loaded tolling info | id:%lld type:%s", + ti->id, + ti->tollType==TollType_entry ? "entry": ti->tollType==TollType_exit ? "exit": "single" ); } else { syslog_err("[facilities] [config] failure to decode tolling info '%s'", dir->d_name); diff --git a/src/facilities.h b/src/facilities.h index f0460ad..97cf004 100644 --- a/src/facilities.h +++ b/src/facilities.h @@ -66,7 +66,7 @@ typedef struct facilities { bulletin_t bulletin; // TP - tolling_s tolling; + tolling_t tolling; // PC coordination_s coordination; diff --git a/src/indications.c b/src/indications.c index 2fdaa5f..111086c 100644 --- a/src/indications.c +++ b/src/indications.c @@ -10,7 +10,7 @@ static void tcp_conn_reset(facilities_t* facilities, TCPConnRSTInfo_t* cri, void SecurityRequest_t* sreq = NULL; // Reset tolling, tls - tolling_s* tolling = &facilities->tolling; + tolling_t* tolling = &facilities->tolling; bulletin_t* bulletin = &facilities->bulletin; if (tolling->enabled && tolling->protocol == TOLLING_PROTOCOL_TLS) { for (int i = 0; i < bulletin->to_consume_len; ++i) { diff --git a/src/saem.c b/src/saem.c index 75cb386..610c25d 100644 --- a/src/saem.c +++ b/src/saem.c @@ -80,7 +80,7 @@ SAEM_CODE_R saem_check(void* fc, bulletin_t* bulletin, SAEM_t* saem, uint8_t* ne bool found = false; for (int t = 0; t < bulletin->to_consume_len; ++t) { - if (((tolling_info_s*)bulletin->to_consume[t]->info.internal_p)->asn->id == tpi->id) { + if (((tolling_info_t*)bulletin->to_consume[t]->info.internal_p)->asn->id == tpi->id) { found = true; break; } @@ -381,7 +381,7 @@ void *sa_service(void *fc) { now > bulletin->to_consume[a]->t_trigger + TOLLING_PAYMENT_MIN_PERIOD_MS && facilities->station_type != 15) { - tolling_info_s* info = (tolling_info_s*) bulletin->to_consume[a]->info.internal_p; + tolling_info_t* info = (tolling_info_t*) bulletin->to_consume[a]->info.internal_p; if (!tpm_is_inside_zone(facilities, info)) { continue; diff --git a/src/tpm.c b/src/tpm.c index 2890113..fef8fec 100644 --- a/src/tpm.c +++ b/src/tpm.c @@ -13,7 +13,7 @@ #include #include -int tpm_is_inside_zone(void* fc, tolling_info_s* ti) { +int tpm_is_inside_zone(void* fc, tolling_info_t* ti) { int rv = 0; facilities_t* facilities = (facilities_t*) fc; @@ -31,21 +31,21 @@ int tpm_is_inside_zone(void* fc, tolling_info_s* ti) { return 0; } -int tpm_pay(void* fc, tolling_info_s* info, void* security_socket, uint8_t* neighbour, uint8_t* dst_addr) { +int tpm_pay(void* fc, tolling_info_t* info, void* security_socket, uint8_t* neighbour, uint8_t* dst_addr) { int rv = 0; facilities_t* facilities = (facilities_t*) fc; - tolling_s* tolling = (tolling_s*) &facilities->tolling; + tolling_t* tolling = (tolling_t*) &facilities->tolling; pthread_mutex_lock(&facilities->epv.time.lock); tolling->tz = it2s_tender_get_now(TIME_MICROSECONDS) ; pthread_mutex_unlock(&facilities->epv.time.lock); + TPM_t* tpm = NULL; TransportRequest_t* tr = NULL; SecurityRequest_t* sreq = NULL; SecurityReply_t* srep = NULL; FacilitiesIndication_t* fi = NULL; - TPM_t* tpm = NULL; const size_t buf_len = 2048; uint8_t tpm_uper[buf_len]; @@ -96,13 +96,22 @@ int tpm_pay(void* fc, tolling_info_s* info, void* security_socket, uint8_t* neig tpm->tpm.referencePosition.positionConfidenceEllipse.semiMajorOrientation = 900; } - // tollingFlow - tpm->tpm.tollingFlow.present = TollingFlow_PR_request; - tpm->tpm.tollingFlow.choice.request.clientId = tolling->client_id; - tpm->tpm.tollingFlow.choice.request.infoId = info->asn->id; - tpm->tpm.tollingFlow.choice.request.paymentMethod.present = TollPaymentMethod_PR_fiat; - tpm->tpm.tollingFlow.choice.request.paymentMethod.choice.fiat.fiatId = FiatId_eur; - tpm->tpm.tollingFlow.choice.request.transactionNonce = tolling->nonce; + tpm->tpm.tollingType = calloc(1, sizeof(TollingType_t)); + TollingType_t* type = tpm->tpm.tollingType; + + switch (info->asn->tollType) { + case TollType_entry: + break; + case TollType_exit: + break; + case TollType_single: + type->present = TollingType_PR_single; + type->choice.single.present = TollSingle_PR_request; + type->choice.single.choice.request.clientId = tolling->client_id; + type->choice.single.choice.request.infoId = info->asn->id; + type->choice.single.choice.request.transactionNonce = tolling->nonce; + break; + } // Encode TollingPaymentMessage asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_TollingPaymentMessage, NULL, &tpm->tpm, buf, buf_len); @@ -139,17 +148,17 @@ int tpm_pay(void* fc, tolling_info_s* info, void* security_socket, uint8_t* neig goto cleanup; } - tpm->signature = calloc(1, sizeof(TpmSignature_t)); - tpm->signature->r.size = srep->data->choice.sign.r.size; - tpm->signature->r.buf = malloc(srep->data->choice.sign.r.size); - memcpy(tpm->signature->r.buf, srep->data->choice.sign.r.buf, srep->data->choice.sign.r.size); - tpm->signature->s.size = srep->data->choice.sign.s.size; - tpm->signature->s.buf = malloc(srep->data->choice.sign.s.size); - memcpy(tpm->signature->s.buf, srep->data->choice.sign.s.buf, srep->data->choice.sign.s.size); - tpm->signature->signer.size = srep->data->choice.sign.signer.size; - tpm->signature->signer.buf = malloc(srep->data->choice.sign.signer.size); - memcpy(tpm->signature->signer.buf, srep->data->choice.sign.signer.buf, srep->data->choice.sign.signer.size); - tpm->signature->type = srep->data->choice.sign.type; + tpm->tpmSignature = calloc(1, sizeof(TpmSignature_t)); + tpm->tpmSignature->r.size = srep->data->choice.sign.r.size; + tpm->tpmSignature->r.buf = malloc(srep->data->choice.sign.r.size); + memcpy(tpm->tpmSignature->r.buf, srep->data->choice.sign.r.buf, srep->data->choice.sign.r.size); + tpm->tpmSignature->s.size = srep->data->choice.sign.s.size; + tpm->tpmSignature->s.buf = malloc(srep->data->choice.sign.s.size); + memcpy(tpm->tpmSignature->s.buf, srep->data->choice.sign.s.buf, srep->data->choice.sign.s.size); + tpm->tpmSignature->signer.size = srep->data->choice.sign.signer.size; + tpm->tpmSignature->signer.buf = malloc(srep->data->choice.sign.signer.size); + memcpy(tpm->tpmSignature->signer.buf, srep->data->choice.sign.signer.buf, srep->data->choice.sign.signer.size); + tpm->tpmSignature->type = srep->data->choice.sign.type; } // Encode TPM @@ -325,23 +334,73 @@ cleanup: static void rsu_handle_recv(facilities_t* facilities, TPM_t* tpm_rx, void* security_socket, uint8_t* neighbour, uint8_t* src_addr) { - TollRequest_t* req = &tpm_rx->tpm.tollingFlow.choice.request; - tolling_s* tolling = &facilities->tolling; + if (!tpm_rx->tpm.tollingType) { + syslog_err("[facilities] [tolling] received TPM does not have a type"); + return; + } + + TollingType_t* type_rx = tpm_rx->tpm.tollingType; + + uint64_t client_id, nonce, info_id; + + switch (type_rx->present) { + case TollingType_PR_entry: + if (type_rx->choice.entry.present != TollEntry_PR_request) { + syslog_err("[facilities] [tolling] received TPM.entry is not request"); + return; + } + client_id = type_rx->choice.entry.choice.request.clientId; + nonce = type_rx->choice.entry.choice.request.transactionNonce; + info_id = type_rx->choice.entry.choice.request.infoId; + break; + case TollingType_PR_exit: + if (!type_rx->choice.exit || + type_rx->choice.exit->present != TollExit_PR_request || + !type_rx->choice.exit->choice.request + ) { + syslog_err("[facilities] [tolling] received TPM.exit is not request"); + return; + } + client_id = type_rx->choice.exit->choice.request->clientId; + nonce = type_rx->choice.exit->choice.request->transactionNonce; + info_id = type_rx->choice.exit->choice.request->infoId; + + if (!type_rx->choice.exit->choice.request->entryProof) { + syslog_err("[facilities] [tolling] received TPM.exit.request does not contain entry proof"); + return; + } + + break; + case TollingType_PR_single: + if (type_rx->choice.single.present != TollSingle_PR_request) { + syslog_err("[facilities] [tolling] received TPM.single is not request"); + return; + } + client_id = type_rx->choice.single.choice.request.clientId; + nonce = type_rx->choice.single.choice.request.transactionNonce; + info_id = type_rx->choice.single.choice.request.infoId; + break; + default: + syslog_err("[facilities] [tolling] received TPM has unrecognized type"); + return; + } + + tolling_t* tolling = &facilities->tolling; switch (tolling->protocol) { case TOLLING_PROTOCOL_SIMPLE: syslog_info("[facilities] [tolling] received toll payment > client: %lld (certificate id: %02x%02x%02x) | nonce: %lld", - req->clientId, + (long long) client_id, neighbour ? neighbour[5] : 0, neighbour ? neighbour[6] : 0, neighbour ? neighbour[7] : 0, - req->transactionNonce + (long long) nonce ); break; case TOLLING_PROTOCOL_TLS: syslog_info("[facilities] [tolling] received toll payment > client: %lld | nonce: %lld", - req->clientId, - req->transactionNonce + (long long) client_id, + (long long) nonce ); break; } @@ -359,7 +418,7 @@ static void rsu_handle_recv(facilities_t* facilities, TPM_t* tpm_rx, void* secur if (tolling->protocol == TOLLING_PROTOCOL_SIMPLE) { - if (!tpm_rx->signature) { + if (!tpm_rx->tpmSignature) { syslog_err("[facilities] [tolling] in simple mode but TPM without signature received"); goto cleanup; } @@ -380,19 +439,19 @@ static void rsu_handle_recv(facilities_t* facilities, TPM_t* tpm_rx, void* secur memcpy(sreq->choice.verify.message.buf, buf, enc.encoded); // r - sreq->choice.verify.r.size = tpm_rx->signature->r.size; - sreq->choice.verify.r.buf = malloc(tpm_rx->signature->r.size); - memcpy(sreq->choice.verify.r.buf, tpm_rx->signature->r.buf, tpm_rx->signature->r.size); + sreq->choice.verify.r.size = tpm_rx->tpmSignature->r.size; + sreq->choice.verify.r.buf = malloc(tpm_rx->tpmSignature->r.size); + memcpy(sreq->choice.verify.r.buf, tpm_rx->tpmSignature->r.buf, tpm_rx->tpmSignature->r.size); // s - sreq->choice.verify.s.size = tpm_rx->signature->s.size; - sreq->choice.verify.s.buf = malloc(tpm_rx->signature->s.size); - memcpy(sreq->choice.verify.s.buf, tpm_rx->signature->s.buf, tpm_rx->signature->s.size); + sreq->choice.verify.s.size = tpm_rx->tpmSignature->s.size; + sreq->choice.verify.s.buf = malloc(tpm_rx->tpmSignature->s.size); + memcpy(sreq->choice.verify.s.buf, tpm_rx->tpmSignature->s.buf, tpm_rx->tpmSignature->s.size); // signer - sreq->choice.verify.signer.size = tpm_rx->signature->signer.size; - sreq->choice.verify.signer.buf = malloc(tpm_rx->signature->signer.size); - memcpy(sreq->choice.verify.signer.buf, tpm_rx->signature->signer.buf, tpm_rx->signature->signer.size); + sreq->choice.verify.signer.size = tpm_rx->tpmSignature->signer.size; + sreq->choice.verify.signer.buf = malloc(tpm_rx->tpmSignature->signer.size); + memcpy(sreq->choice.verify.signer.buf, tpm_rx->tpmSignature->signer.buf, tpm_rx->tpmSignature->signer.size); // signature type - sreq->choice.verify.type = tpm_rx->signature->type; + sreq->choice.verify.type = tpm_rx->tpmSignature->type; buf[0] = 4; enc = oer_encode_to_buffer(&asn_DEF_SecurityRequest, NULL, sreq, buf+1, 2047); @@ -457,14 +516,38 @@ static void rsu_handle_recv(facilities_t* facilities, TPM_t* tpm_rx, void* secur tpm->tpm.referencePosition.positionConfidenceEllipse.semiMajorOrientation = 900; } - // tollingFlow - tpm->tpm.tollingFlow.present = TollingFlow_PR_reply; - tpm->tpm.tollingFlow.choice.reply.clientId = req->clientId; - tpm->tpm.tollingFlow.choice.reply.infoId = req->infoId; - tpm->tpm.tollingFlow.choice.reply.transactionNonce = req->transactionNonce; + tpm->tpm.tollingType = calloc(1, sizeof(TollingType_t)); + switch (tpm_rx->tpm.tollingType->present) { + case TollingType_PR_entry: + tpm->tpm.tollingType->present = TollingType_PR_entry; + tpm->tpm.tollingType->choice.entry.present = TollEntry_PR_reply; + tpm->tpm.tollingType->choice.entry.choice.reply.clientId = client_id; + tpm->tpm.tollingType->choice.entry.choice.reply.infoId = info_id; + tpm->tpm.tollingType->choice.entry.choice.reply.transactionNonce = nonce; + tpm->tpm.tollingType->choice.entry.choice.reply.confirmationCode = TollConfirmationCode_accepted; + break; + case TollingType_PR_exit: + tpm->tpm.tollingType->present = TollingType_PR_exit; + tpm->tpm.tollingType->choice.exit = calloc(1, sizeof(TollExit_t)); + tpm->tpm.tollingType->choice.exit->present = TollExit_PR_reply; + tpm->tpm.tollingType->choice.exit->choice.reply.clientId = client_id; + tpm->tpm.tollingType->choice.exit->choice.reply.infoId = info_id; + tpm->tpm.tollingType->choice.exit->choice.reply.transactionNonce = nonce; + tpm->tpm.tollingType->choice.exit->choice.reply.confirmationCode = TollConfirmationCode_accepted; + break; + case TollingType_PR_single: + tpm->tpm.tollingType->present = TollingType_PR_single; + tpm->tpm.tollingType->choice.single.present = TollSingle_PR_reply; + tpm->tpm.tollingType->choice.single.choice.reply.clientId = client_id; + tpm->tpm.tollingType->choice.single.choice.reply.infoId = info_id; + tpm->tpm.tollingType->choice.single.choice.reply.transactionNonce = nonce; + tpm->tpm.tollingType->choice.single.choice.reply.confirmationCode = TollConfirmationCode_accepted; + break; + default: + break; + } // TODO check clientId - // TODO dlt: check transaction // Encode TollingPaymentMessage enc = oer_encode_to_buffer(&asn_DEF_TollingPaymentMessage, NULL, &tpm->tpm, buf, buf_len); @@ -498,16 +581,16 @@ static void rsu_handle_recv(facilities_t* facilities, TPM_t* tpm_rx, void* secur goto cleanup; } - tpm->signature->r.size = srep->data->choice.sign.r.size; - tpm->signature->r.buf = malloc(srep->data->choice.sign.r.size); - memcpy(tpm->signature->r.buf, srep->data->choice.sign.r.buf, srep->data->choice.sign.r.size); - tpm->signature->s.size = srep->data->choice.sign.s.size; - tpm->signature->s.buf = malloc(srep->data->choice.sign.s.size); - memcpy(tpm->signature->s.buf, srep->data->choice.sign.s.buf, srep->data->choice.sign.s.size); - tpm->signature->signer.size = srep->data->choice.sign.signer.size; - tpm->signature->signer.buf = malloc(srep->data->choice.sign.signer.size); - memcpy(tpm->signature->signer.buf, srep->data->choice.sign.signer.buf, srep->data->choice.sign.signer.size); - tpm->signature->type = srep->data->choice.sign.type; + tpm->tpmSignature->r.size = srep->data->choice.sign.r.size; + tpm->tpmSignature->r.buf = malloc(srep->data->choice.sign.r.size); + memcpy(tpm->tpmSignature->r.buf, srep->data->choice.sign.r.buf, srep->data->choice.sign.r.size); + tpm->tpmSignature->s.size = srep->data->choice.sign.s.size; + tpm->tpmSignature->s.buf = malloc(srep->data->choice.sign.s.size); + memcpy(tpm->tpmSignature->s.buf, srep->data->choice.sign.s.buf, srep->data->choice.sign.s.size); + tpm->tpmSignature->signer.size = srep->data->choice.sign.signer.size; + tpm->tpmSignature->signer.buf = malloc(srep->data->choice.sign.signer.size); + memcpy(tpm->tpmSignature->signer.buf, srep->data->choice.sign.signer.buf, srep->data->choice.sign.signer.size); + tpm->tpmSignature->type = srep->data->choice.sign.type; } // encode TPM @@ -677,16 +760,64 @@ cleanup: ASN_STRUCT_FREE(asn_DEF_FacilitiesIndication, fi); } -static void veh_handle_recv(tolling_s* tolling, TPM_t* tpm_rx, void* security_socket, uint8_t* neighbour) { +static void veh_handle_recv(tolling_t* tolling, TPM_t* tpm_rx, void* security_socket, uint8_t* neighbour) { - TollReply_t* rep = &tpm_rx->tpm.tollingFlow.choice.reply; + if (!tpm_rx->tpm.tollingType) { + syslog_err("[facilities] [tolling] received TPM does not have a type"); + return; + } - if (rep->clientId != tolling->client_id) { + TollingType_t* type_rx = tpm_rx->tpm.tollingType; + + uint64_t client_id, nonce, info_id; + int confirmation_code; + + switch (type_rx->present) { + case TollingType_PR_entry: + if (type_rx->choice.entry.present != TollEntry_PR_reply) { + syslog_err("[facilities] [tolling] received TPM.entry is not reply"); + return; + } + client_id = type_rx->choice.entry.choice.reply.clientId; + nonce = type_rx->choice.entry.choice.reply.transactionNonce; + info_id = type_rx->choice.entry.choice.reply.infoId; + confirmation_code = type_rx->choice.entry.choice.reply.confirmationCode; + break; + case TollingType_PR_exit: + if (!type_rx->choice.exit || + type_rx->choice.exit->present != TollExit_PR_reply + ) { + syslog_err("[facilities] [tolling] received TPM.exit is not reply"); + return; + } + client_id = type_rx->choice.exit->choice.reply.clientId; + nonce = type_rx->choice.exit->choice.reply.transactionNonce; + info_id = type_rx->choice.exit->choice.reply.infoId; + confirmation_code = type_rx->choice.exit->choice.reply.confirmationCode; + break; + case TollingType_PR_single: + if (type_rx->choice.single.present != TollSingle_PR_reply) { + syslog_err("[facilities] [tolling] received TPM.single is not reply"); + return; + } + client_id = type_rx->choice.single.choice.reply.clientId; + nonce = type_rx->choice.single.choice.reply.transactionNonce; + info_id = type_rx->choice.single.choice.reply.infoId; + confirmation_code = type_rx->choice.single.choice.reply.confirmationCode; + break; + default: + syslog_err("[facilities] [tolling] received TPM has unrecognized type"); + return; + } + + // TODO if sent ENTRY.REQUEST expect ENTRY.REPLY, if send SINGLE.REQUEST expect ENTRY.REPLY, etc + + if (client_id != tolling->client_id) { syslog_debug("[facilities] [tolling]<- received TPM.reply clientId different from ego"); return; } - if (rep->transactionNonce != tolling->nonce) { + if (nonce != tolling->nonce) { syslog_err("[facilities] [tolling]<- received TPM.reply nonce different from sent request"); return; } @@ -714,19 +845,19 @@ static void veh_handle_recv(tolling_s* tolling, TPM_t* tpm_rx, void* security_so sreq->choice.verify.message.buf = malloc(enc.encoded); memcpy(sreq->choice.verify.message.buf, buf, enc.encoded); // r - sreq->choice.verify.r.size = tpm_rx->signature->r.size; - sreq->choice.verify.r.buf = malloc(tpm_rx->signature->r.size); - memcpy(sreq->choice.verify.r.buf, tpm_rx->signature->r.buf, tpm_rx->signature->r.size); + sreq->choice.verify.r.size = tpm_rx->tpmSignature->r.size; + sreq->choice.verify.r.buf = malloc(tpm_rx->tpmSignature->r.size); + memcpy(sreq->choice.verify.r.buf, tpm_rx->tpmSignature->r.buf, tpm_rx->tpmSignature->r.size); // s - sreq->choice.verify.s.size = tpm_rx->signature->s.size; - sreq->choice.verify.s.buf = malloc(tpm_rx->signature->s.size); - memcpy(sreq->choice.verify.s.buf, tpm_rx->signature->s.buf, tpm_rx->signature->s.size); + sreq->choice.verify.s.size = tpm_rx->tpmSignature->s.size; + sreq->choice.verify.s.buf = malloc(tpm_rx->tpmSignature->s.size); + memcpy(sreq->choice.verify.s.buf, tpm_rx->tpmSignature->s.buf, tpm_rx->tpmSignature->s.size); // signer - sreq->choice.verify.signer.size = tpm_rx->signature->signer.size; - sreq->choice.verify.signer.buf = malloc(tpm_rx->signature->signer.size); - memcpy(sreq->choice.verify.signer.buf, tpm_rx->signature->signer.buf, tpm_rx->signature->signer.size); + sreq->choice.verify.signer.size = tpm_rx->tpmSignature->signer.size; + sreq->choice.verify.signer.buf = malloc(tpm_rx->tpmSignature->signer.size); + memcpy(sreq->choice.verify.signer.buf, tpm_rx->tpmSignature->signer.buf, tpm_rx->tpmSignature->signer.size); // signature type - sreq->choice.verify.type = tpm_rx->signature->type; + sreq->choice.verify.type = tpm_rx->tpmSignature->type; buf[0] = 4; enc = oer_encode_to_buffer(&asn_DEF_SecurityRequest, NULL, sreq, buf+1, 2047); @@ -750,10 +881,10 @@ static void veh_handle_recv(tolling_s* tolling, TPM_t* tpm_rx, void* security_so goto cleanup; } - accepted = rep->confirmationCode == TollConfirmationCode_accepted; + accepted = confirmation_code == TollConfirmationCode_accepted; } - syslog_info("[facilities] [tolling] received tolling payment reply > client: %ld | nonce: %ld | accepted: %s", tolling->client_id, tolling->nonce, accepted ? "yes" : "no"); + syslog_info("[facilities] [tolling] received tolling payment reply | client:%lld nonce:%ld accepted:%s", (long long) tolling->client_id, tolling->nonce, accepted ? "yes" : "no"); tolling->active = false; @@ -766,32 +897,42 @@ int tpm_recv(void* fc, TPM_t* tpm_rx, void* security_socket, uint8_t* neighbour, int rv = 0; facilities_t* facilities = (facilities_t*) fc; - tolling_s* tolling = (tolling_s*) &facilities->tolling; + tolling_t* tolling = (tolling_t*) &facilities->tolling; if (!tolling->enabled) { syslog_debug("[facilities] [tolling] tolling is disabled"); goto cleanup; } - switch (tpm_rx->tpm.tollingFlow.present) { - case TollingFlow_PR_request: - if (facilities->station_type != 15) { - syslog_debug("[facilities] [tolling] received TPM.request, ignoring"); - goto cleanup; - } - rsu_handle_recv(facilities, tpm_rx, security_socket, neighbour, src_addr); - break; + if (!tpm_rx->tpm.tollingType) { + syslog_err("[facilities] [tolling] received TPM does not have a type"); + rv = 1; + goto cleanup; + } - case TollingFlow_PR_reply: - if (facilities->station_type == 15) { - syslog_debug("[facilities] [tolling] received TPM.reply, ignoring"); - goto cleanup; + switch (tpm_rx->tpm.tollingType->present) { + case TollingType_PR_single: + switch (tpm_rx->tpm.tollingType->choice.single.present) { + case TollSingle_PR_request: + if (facilities->station_type != 15) { + syslog_debug("[facilities] [tolling] received TPM.request, ignoring"); + goto cleanup; + } + rsu_handle_recv(facilities, tpm_rx, security_socket, neighbour, src_addr); + break; + case TollSingle_PR_reply: + if (facilities->station_type == 15) { + syslog_debug("[facilities] [tolling] received TPM.reply, ignoring"); + goto cleanup; + } + pthread_mutex_lock(&facilities->epv.time.lock); + syslog_info("[facilities] [tolling] reply took %ld us", it2s_tender_get_now(TIME_MICROSECONDS) - tolling->tz); + pthread_mutex_unlock(&facilities->epv.time.lock); + veh_handle_recv(tolling, tpm_rx, security_socket, neighbour); + break; + default: + break; } - pthread_mutex_lock(&facilities->epv.time.lock); - syslog_info("[facilities] [tolling] reply took %ld us", it2s_tender_get_now(TIME_MICROSECONDS) - tolling->tz); - pthread_mutex_unlock(&facilities->epv.time.lock); - veh_handle_recv(tolling, tpm_rx, security_socket, neighbour); - break; default: break; @@ -802,12 +943,12 @@ cleanup: return rv; } -int tolling_init(tolling_s* tolling, void* zmq_ctx, char* security_address) { +int tolling_init(tolling_t* tolling, void* zmq_ctx, char* security_address) { return 0; } -tolling_info_s* tolling_info_new(it2s_tender_epv_t* epv, TollingPaymentInfo_t* tpi) { - tolling_info_s* ti = calloc(1, sizeof(tolling_info_s)); +tolling_info_t* tolling_info_new(it2s_tender_epv_t* epv, TollingPaymentInfo_t* tpi) { + tolling_info_t* ti = calloc(1, sizeof(tolling_info_t)); ti->timestamp = it2s_tender_get_clock(epv); ti->asn = tpi; @@ -823,7 +964,7 @@ tolling_info_s* tolling_info_new(it2s_tender_epv_t* epv, TollingPaymentInfo_t* t return ti; } -void tolling_info_free(tolling_info_s* ti) { +void tolling_info_free(tolling_info_t* ti) { ASN_STRUCT_FREE(asn_DEF_TollingPaymentInfo, ti->asn); free(ti->zone.polygon); free(ti); diff --git a/src/tpm.h b/src/tpm.h index 02849e8..5ad5eed 100644 --- a/src/tpm.h +++ b/src/tpm.h @@ -20,7 +20,7 @@ typedef struct tolling_info { size_t polygon_len; } zone; TollingPaymentInfo_t* asn; -} tolling_info_s; +} tolling_info_t; typedef struct tolling { bool enabled; @@ -35,11 +35,11 @@ typedef struct tolling { uint64_t tz; struct { - tolling_info_s* z[TOLLING_INFOS_MAX_LENGTH]; + tolling_info_t* z[TOLLING_INFOS_MAX_LENGTH]; uint8_t length; } infos; -} tolling_s; +} tolling_t; /** * Initializes the main tolling struture @@ -49,11 +49,11 @@ typedef struct tolling { * @param security_address The security service ZMQ address * @return Always successful */ -int tolling_init(tolling_s* tolling, void* zmq_ctx, char* security_address); +int tolling_init(tolling_t* tolling, void* zmq_ctx, char* security_address); -int tpm_pay(void* fc, tolling_info_s* info, void* security_socket, uint8_t* neighbour, uint8_t* dst_addr); +int tpm_pay(void* fc, tolling_info_t* info, void* security_socket, uint8_t* neighbour, uint8_t* dst_addr); int tpm_recv(void* fc, TPM_t* tpm_rx, void* security_socket, uint8_t* neighbour, uint8_t* src_addr); -int tpm_is_inside_zone(void* fc, tolling_info_s* ti); +int tpm_is_inside_zone(void* fc, tolling_info_t* ti); -tolling_info_s* tolling_info_new(it2s_tender_epv_t* epv, TollingPaymentInfo_t* tpi); -void tolling_info_free(tolling_info_s* ti); +tolling_info_t* tolling_info_new(it2s_tender_epv_t* epv, TollingPaymentInfo_t* tpi); +void tolling_info_free(tolling_info_t* ti);