• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 #include "hitls_build.h"
17 #include "bsl_sal.h"
18 #include "bsl_module_list.h"
19 #include "tls_binlog_id.h"
20 #include "hitls_error.h"
21 #include "rec.h"
22 #include "bsl_uio.h"
23 #include "record.h"
24 
25 #if defined(HITLS_TLS_PROTO_DTLS12) && defined(HITLS_BSL_UIO_UDP)
REC_RetransmitListAppend(REC_Ctx * recCtx,REC_Type type,const uint8_t * msg,uint32_t len)26 int32_t REC_RetransmitListAppend(REC_Ctx *recCtx, REC_Type type, const uint8_t *msg, uint32_t len)
27 {
28     RecRetransmitList *retransmitList = &recCtx->retransmitList;
29     RecRetransmitList *retransmitNode = (RecRetransmitList *)BSL_SAL_Calloc(1u, sizeof(RecRetransmitList));
30     if (retransmitNode == NULL) {
31         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17277, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Calloc fail", 0, 0, 0, 0);
32         return HITLS_MEMALLOC_FAIL;
33     }
34 
35     LIST_INIT(&(retransmitNode->head));
36     retransmitNode->type = type;
37     retransmitNode->msg = BSL_SAL_Dump(msg, len);
38     if (retransmitNode->msg == NULL) {
39         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17278, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Dump fail", 0, 0, 0, 0);
40         BSL_SAL_FREE(retransmitNode);
41         return HITLS_MEMALLOC_FAIL;
42     }
43     retransmitNode->len = len;
44 
45     if (type == REC_TYPE_CHANGE_CIPHER_SPEC) {
46         retransmitList->isExistCcsMsg = true;
47     }
48 
49     /* insert new node */
50     LIST_ADD_BEFORE(&retransmitList->head, &retransmitNode->head);
51     return HITLS_SUCCESS;
52 }
53 
REC_RetransmitListClean(REC_Ctx * recCtx)54 void REC_RetransmitListClean(REC_Ctx *recCtx)
55 {
56     ListHead *head = NULL;
57     ListHead *tmpHead = NULL;
58     RecRetransmitList *retransmitList = &recCtx->retransmitList;
59     RecRetransmitList *retransmitNode = NULL;
60 
61     retransmitList->isExistCcsMsg = false;
62     LIST_FOR_EACH_ITEM_SAFE(head, tmpHead, &(retransmitList->head)) {
63         LIST_REMOVE(head);
64         retransmitNode = LIST_ENTRY(head, RecRetransmitList, head);
65         BSL_SAL_FREE(retransmitNode->msg);
66         BSL_SAL_FREE(retransmitNode);
67     }
68     return;
69 }
70 
REC_RetransmitListFlush(TLS_Ctx * ctx)71 void REC_RetransmitListFlush(TLS_Ctx *ctx)
72 {
73     REC_Ctx *recCtx = ctx->recCtx;
74     RecRetransmitList *retransmitList = &recCtx->retransmitList;
75     RecRetransmitList *retransmitNode = NULL;
76 
77     if (retransmitList->isExistCcsMsg == true) {
78         REC_ActiveOutdatedWriteState(ctx);
79     }
80 
81     ListHead *head = NULL;
82     ListHead *tmpHead = NULL;
83     LIST_FOR_EACH_ITEM_SAFE(head, tmpHead, &(retransmitList->head)) {
84         retransmitNode = LIST_ENTRY(head, RecRetransmitList, head);
85         /* UDP does not fail to send. Therefore, the sending failure case does not need to be considered. */
86         (void)REC_Write(ctx, retransmitNode->type, retransmitNode->msg, retransmitNode->len);
87         if (retransmitNode->type == REC_TYPE_CHANGE_CIPHER_SPEC) {
88             REC_DeActiveOutdatedWriteState(ctx);
89         }
90     }
91     if (ctx->config.tlsConfig.isFlightTransmitEnable) {
92         (void)BSL_UIO_Ctrl(ctx->uio, BSL_UIO_FLUSH, 0, NULL);
93     }
94     return;
95 }
96 #endif /* HITLS_TLS_PROTO_DTLS12 && HITLS_BSL_UIO_UDP */