• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "nstackx_dfile_frame.h"
17 #include "nstackx_util.h"
18 #include "nstackx_error.h"
19 #include "nstackx_dfile.h"
20 #include "nstackx_log.h"
21 #include "nstackx_dfile_config.h"
22 #include "nstackx_dev.h"
23 #include "securec.h"
24 
25 #define TAG "nStackXDFile"
26 
GetFrameName(DFileFrameType frameType)27 const char *GetFrameName(DFileFrameType frameType)
28 {
29     uint32_t i;
30 
31     static const char *frameNameList[] = {
32         [NSTACKX_DFILE_FILE_HEADER_FRAME] = "FILE_HEADER_FRAME",
33         [NSTACKX_DFILE_FILE_HEADER_CONFIRM_FRAME] = "FILE_HEADER_CONFIRM_FRAME",
34         [NSTACKX_DFILE_FILE_TRANSFER_REQ_FRAME] = "FILE_TRANSFER_REQ_FRAME",
35         [NSTACKX_DFILE_FILE_DATA_FRAME] = "FILE_DATA_FRAME",
36         [NSTACKX_DFILE_FILE_TRANSFER_DONE_FRAME] = "FILE_TRANSFER_DONE_FRAME",
37         [NSTACKX_DFILE_FILE_TRANSFER_DONE_ACK_FRAME] = "FILE_TRANSFER_DONE_ACK_FRAME",
38         [NSTACKX_DFILE_SETTING_FRAME] = "SETTING_FRAME",
39         [NSTACKX_DFILE_RST_FRAME] = "RST_FRAME",
40     };
41 
42     for (i = 0; i < sizeof(frameNameList) / sizeof(frameNameList[0]); i++) {
43         if (i == frameType) {
44             return frameNameList[i];
45         }
46     }
47 
48     return "unknown";
49 }
50 
GetTarTotalBlockLength(FileList * fileList)51 uint64_t GetTarTotalBlockLength(FileList *fileList)
52 {
53     char pathSeparator = '/';
54     uint64_t tarFilesTotalLen = 0;
55     uint64_t blockCnt;
56     uint32_t i;
57     int32_t ret;
58     struct stat statInfo;
59     char *path = NULL;
60 
61     for (i = 0; i < fileList->num; i++) {
62         tarFilesTotalLen += BLOCK_LEN;
63 
64         path = fileList->list[i].fullFileName;
65         if (strlen(path) > MAX_NAME_LEN) {
66             // +1 because of the string end '\0'
67             tarFilesTotalLen += BLOCK_LEN +
68                 (((strlen(path) + 1 - (long long)(path[0] == pathSeparator) + BLOCK_LEN - 1) / BLOCK_LEN) * BLOCK_LEN);
69         }
70 
71         ret = stat(path, &statInfo);
72         if (ret != NSTACKX_EOK) {
73             LOGE(TAG, "get stat error: %d", ret);
74             return 0;
75         }
76 
77         // file body length
78         blockCnt = (uint64_t)((statInfo.st_size + BLOCK_LEN - 1) / BLOCK_LEN);
79         tarFilesTotalLen += (blockCnt * BLOCK_LEN);
80     }
81 
82     // file tail paddings length
83     tarFilesTotalLen += BLOCK_LEN;
84 
85     return tarFilesTotalLen;
86 }
87 
EncodeFileInfo(FileList * fileList,uint16_t fileId,uint8_t * buffer,size_t length,size_t * fileInfoSize)88 static int32_t EncodeFileInfo(FileList *fileList, uint16_t fileId, uint8_t *buffer, size_t length,
89     size_t *fileInfoSize)
90 {
91     FileInfoUnit *fileInfoUnit = (FileInfoUnit *)buffer;
92     const char *fileName = NULL;
93     uint16_t fileNameLen;
94     size_t remainLength;
95     uint64_t filesTotalLen;
96 
97     if (fileId == 0) {
98         if (fileList->packedUserData != NULL) {
99             fileName = (char *)fileList->packedUserData;
100             fileNameLen = fileList->packedUserDataLen;
101         } else {
102             fileName = fileList->userData;
103             fileNameLen = (uint16_t)strlen(fileName);
104         }
105         filesTotalLen = 0;
106     } else {
107         fileName = FileListGetRemotePath(fileList, fileId);
108         if (fileName == NULL) {
109             fileName = FileListGetFileName(fileList, fileId);
110         }
111         fileNameLen = (uint16_t)strlen(fileName);
112         if (fileList->tarFlag == NSTACKX_FALSE) {
113             filesTotalLen = FileListGetFileSize(fileList, fileId);
114         } else {
115             filesTotalLen = GetTarTotalBlockLength(fileList);
116         }
117     }
118 
119     if (length <= offsetof(FileInfoUnit, fileName)) {
120         /* Running out of buffer */
121         LOGE(TAG, "buffer length %u is not enough", length);
122         return NSTACKX_EAGAIN;
123     }
124     remainLength = length - offsetof(FileInfoUnit, fileName);
125     if (memcpy_s(fileInfoUnit->fileName, remainLength, fileName, fileNameLen) != EOK) {
126         /* Running out of buffer */
127         LOGE(TAG, "memcpy_s fileName error. remain length %u, fileNameLen %u", remainLength, fileNameLen);
128         return NSTACKX_EAGAIN;
129     }
130     fileInfoUnit->fileId = htons(fileId);
131     fileInfoUnit->fileSize = htobe64(filesTotalLen);
132     fileInfoUnit->fileNameLength = htons(fileNameLen);
133     *fileInfoSize = offsetof(FileInfoUnit, fileName) + fileNameLen;
134     return NSTACKX_EOK;
135 }
136 
137 /* Caller should make sure that "length" can cover the minimum header length */
EncodeFileHeaderFrameSp(FileList * fileList,int32_t * fileId,uint8_t * buffer,size_t length,size_t * frameLength)138 void EncodeFileHeaderFrameSp(FileList *fileList, int32_t *fileId, uint8_t *buffer, size_t length,
139     size_t *frameLength)
140 {
141     FileHeaderFrame *headerFrame = (FileHeaderFrame *)buffer;
142     size_t offset = 0;
143     size_t fileInfoSize = 0;
144     size_t bufferLength = length;
145     int32_t nextFileId;
146     int32_t lastAddedFileId = *fileId;
147     uint32_t fileNum;
148 
149     /* Remaining buffer length for fileInfoUnit */
150     bufferLength -= (sizeof(uint16_t) + DFILE_FRAME_HEADER_LEN);
151 
152     if (fileList->tarFlag == NSTACKX_TRUE) {
153         fileNum = 1;
154     } else {
155         fileNum = FileListGetNum(fileList);
156     }
157 
158     do {
159         if (lastAddedFileId == (int32_t)fileNum || bufferLength <= offset) {
160             break;
161         }
162 
163         nextFileId = lastAddedFileId + 1;
164         if (FileListGetFileNameAcked(fileList, (uint16_t)nextFileId) ||
165             (nextFileId == 0 && fileList->userData == NULL && fileList->packedUserData == NULL)) {
166             LOGI(TAG, "SKIP FILE ID %d", nextFileId);
167             lastAddedFileId = nextFileId;
168             continue;
169         }
170 
171         if (EncodeFileInfo(fileList, (uint16_t)nextFileId, &headerFrame->fileInfoUnit[offset],
172             bufferLength - offset, &fileInfoSize) != NSTACKX_EOK) {
173             LOGE(TAG, "EncodeFileInfo fileId %d failed", nextFileId);
174             break;
175         }
176 
177         offset += fileInfoSize;
178         lastAddedFileId = nextFileId;
179     } while (NSTACKX_TRUE);
180 
181     headerFrame->header.type = NSTACKX_DFILE_FILE_HEADER_FRAME;
182     if (fileList->userData != NULL) {
183         SetDfileFrameUserDataFlag(&headerFrame->header);
184     }
185 
186     if (fileList->pathType > 0) {
187         SetDfileFramePathTypeFlag(&headerFrame->header);
188     }
189     /* set no sync flag */
190     if (fileList->noSyncFlag) {
191         SetDfileFrameNoSyncFlag(&headerFrame->header);
192     }
193     /* Add "node number" */
194     offset += sizeof(uint16_t);
195     headerFrame->header.length = htons((uint16_t)offset);
196     headerFrame->nodeNumber = htons((uint16_t)fileNum);
197     /* Update internal buffer length */
198     *frameLength = DFILE_FRAME_HEADER_LEN + offset;
199     *fileId = lastAddedFileId;
200 }
201 
EncodeFileHeaderFrame(FileList * fileList,int32_t * fileId,uint8_t * buffer,size_t length,size_t * frameLength)202 void EncodeFileHeaderFrame(FileList *fileList, int32_t *fileId, uint8_t *buffer, size_t length,
203     size_t *frameLength)
204 {
205     EncodeFileHeaderFrameSp(fileList, fileId, buffer, length, frameLength);
206 }
207 
208 /* Caller should make sure that "length" can cover the minimum header length */
EncodeFileHeaderConfirmFrame(FileList * fileList,uint16_t * fileId,uint8_t * buffer,size_t length,size_t * frameLength)209 void EncodeFileHeaderConfirmFrame(FileList *fileList, uint16_t *fileId, uint8_t *buffer, size_t length,
210     size_t *frameLength)
211 {
212     FileHeaderConfirmFrame *confirmFrame = (FileHeaderConfirmFrame *)buffer;
213     size_t bufferLength = length;
214     uint16_t payloadLength;
215     uint16_t nextFileId;
216     uint16_t lastAckedFileId = *fileId;
217     uint16_t i = 0;
218 
219     /* Remaining buffer length for payload */
220     bufferLength -= DFILE_FRAME_HEADER_LEN;
221 
222     if ((fileList->userDataFlag & NSTACKX_DFILE_HEADER_FRAME_USER_DATA_FLAG) &&
223         (fileList->userDataFlag & NSTACKX_FLAGS_FILE_NAME_RECEIVED)) {
224         confirmFrame->fileId[i] = 0;
225         i++;
226     }
227 
228     do {
229         if (lastAckedFileId == FileListGetNum(fileList)) {
230             break;
231         }
232 
233         nextFileId = lastAckedFileId + 1;
234         if (!FileListGetFileNameReceived(fileList, nextFileId)) {
235             lastAckedFileId = nextFileId;
236             LOGE(TAG, "fileId %u is not acked yet", nextFileId);
237             continue;
238         }
239         if (i >= (bufferLength >> 1)) {
240             break;
241         }
242         confirmFrame->fileId[i++] = htons(nextFileId);
243         lastAckedFileId = nextFileId;
244     } while (NSTACKX_TRUE);
245 
246     payloadLength = (uint16_t)(i * sizeof(uint16_t)); /* length should be within MTU to avoid overflow */
247     confirmFrame->header.type = NSTACKX_DFILE_FILE_HEADER_CONFIRM_FRAME;
248     /* Don't have to set flag for ACK frame */
249     confirmFrame->header.flag = 0;
250     confirmFrame->header.length = htons(payloadLength);
251 
252     /* Update internal buffer length */
253     *frameLength = DFILE_FRAME_HEADER_LEN + payloadLength;
254     *fileId = lastAckedFileId;
255 }
256 
257 /* Caller should make sure that "length" can cover the minimum header length */
EncodeFileTransferDoneFrame(uint8_t * buffer,size_t length,uint16_t fileIdList[],uint32_t fileIdNum,size_t * frameLength)258 void EncodeFileTransferDoneFrame(uint8_t *buffer, size_t length, uint16_t fileIdList[], uint32_t fileIdNum,
259                                  size_t *frameLength)
260 {
261     uint32_t i;
262     uint32_t maxIdNum;
263     uint16_t payloadLength;
264     FileTransferDoneFrame *transferDoneFrame = (FileTransferDoneFrame *)buffer;
265 
266     maxIdNum = (uint32_t)((length - DFILE_FRAME_HEADER_LEN) / sizeof(uint16_t));
267     if (maxIdNum > fileIdNum) {
268         maxIdNum = fileIdNum;
269     }
270 
271     for (i = 0; i < maxIdNum; i++) {
272         transferDoneFrame->fileId[i] = htons(fileIdList[i]);
273     }
274 
275     transferDoneFrame->header.type = NSTACKX_DFILE_FILE_TRANSFER_DONE_FRAME;
276 
277     payloadLength = (uint16_t)(maxIdNum * sizeof(uint16_t));
278     transferDoneFrame->header.length = htons(payloadLength);
279 
280     *frameLength = DFILE_FRAME_HEADER_LEN + payloadLength;
281 }
282 
283 /* Caller should make sure that "length" can cover the minimum header length */
EncodeSettingFrame(uint8_t * buffer,size_t length,size_t * frameLength,const SettingFrame * settingFramePara)284 void EncodeSettingFrame(uint8_t *buffer, size_t length, size_t *frameLength, const SettingFrame *settingFramePara)
285 {
286     SettingFrame *settingFrame = (SettingFrame *)buffer;
287 
288     *frameLength = sizeof(SettingFrame);
289     if (*frameLength > length) {
290         return;
291     }
292     settingFrame->header.type = NSTACKX_DFILE_SETTING_FRAME;
293     settingFrame->header.flag = 0;
294     settingFrame->header.sessionId = 0;
295     settingFrame->header.transId = 0;
296     settingFrame->mtu = htons(settingFramePara->mtu);
297     settingFrame->connType = htons(settingFramePara->connType);
298     settingFrame->dFileVersion = htonl(NSTACKX_DFILE_VERSION);
299     settingFrame->abmCapability = 0;
300     settingFrame->header.length = htons(*frameLength - DFILE_FRAME_HEADER_LEN);
301     settingFrame->capability = htonl(settingFramePara->capability);
302     settingFrame->dataFrameSize = htonl(settingFramePara->dataFrameSize);
303     settingFrame->capsCheck = htonl(settingFramePara->capsCheck);
304 }
305 
306 /* Caller should make sure that "length" can cover the minimum header length */
EncodeRstFrame(uint8_t * buffer,size_t length,size_t * frameLength,uint16_t transId,uint16_t errCode)307 void EncodeRstFrame(uint8_t *buffer, size_t length, size_t *frameLength, uint16_t transId, uint16_t errCode)
308 {
309     RstFrame *rstFrame = (RstFrame *)buffer;
310     uint16_t payloadLength;
311 
312     payloadLength = sizeof(uint16_t);
313     *frameLength = DFILE_FRAME_HEADER_LEN + payloadLength;
314     if (*frameLength > length) {
315         return;
316     }
317     rstFrame->header.type = NSTACKX_DFILE_RST_FRAME;
318     rstFrame->header.flag = 0;
319     rstFrame->header.sessionId = 0;
320     rstFrame->header.transId = htons(transId);
321     rstFrame->code = htons(errCode);
322     rstFrame->header.length = htons(payloadLength);
323 }
324 
EncodeBackPressFrame(uint8_t * buffer,size_t length,size_t * frameLength,uint8_t recvListOverIo)325 void EncodeBackPressFrame(uint8_t *buffer, size_t length, size_t *frameLength, uint8_t recvListOverIo)
326 {
327     BackPressureFrame *backPressFrame = (BackPressureFrame *)buffer;
328     uint16_t payloadLength;
329 
330     payloadLength = sizeof(DataBackPressure);
331     *frameLength = DFILE_FRAME_HEADER_LEN + payloadLength;
332     if (*frameLength > length) {
333         return;
334     }
335 
336     backPressFrame->header.type = NSTACKX_DFILE_FILE_BACK_PRESSURE_FRAME;
337     backPressFrame->header.flag = 0;
338     backPressFrame->header.sessionId = 0;
339     backPressFrame->header.transId = 0;
340     backPressFrame->header.length = htons(payloadLength);
341     backPressFrame->backPressure.recvListOverIo = recvListOverIo;
342     backPressFrame->backPressure.recvBufThreshold = 0;
343     backPressFrame->backPressure.stopSendPeriod = htonl(0);
344 }
345 
346 /* Caller should make sure that "length" can cover the minimum header length */
EncodeFileTransferDoneAckFrame(uint8_t * buffer,size_t length,uint16_t transId,size_t * frameLength)347 void EncodeFileTransferDoneAckFrame(uint8_t *buffer, size_t length, uint16_t transId, size_t *frameLength)
348 {
349     FileTransferDoneAckFrame *transferDoneAckFrame = (FileTransferDoneAckFrame *)buffer;
350 
351     *frameLength = DFILE_FRAME_HEADER_LEN;
352     if (*frameLength > length) {
353         return;
354     }
355     transferDoneAckFrame->header.type = NSTACKX_DFILE_FILE_TRANSFER_DONE_ACK_FRAME;
356     transferDoneAckFrame->header.transId = htons(transId);
357 }
358 
DecodeDFileFrame(const uint8_t * buffer,size_t bufferLength,DFileFrame ** frame)359 int32_t DecodeDFileFrame(const uint8_t *buffer, size_t bufferLength, DFileFrame **frame)
360 {
361     DFileFrame *dFileFrame = (DFileFrame *)buffer;
362     uint16_t payloadLength;
363 
364     if (bufferLength < DFILE_FRAME_HEADER_LEN) {
365         LOGE(TAG, "drop malformed frame");
366         return NSTACKX_EFAILED;
367     }
368 
369     payloadLength = ntohs(dFileFrame->header.length);
370     if (bufferLength - DFILE_FRAME_HEADER_LEN != payloadLength) {
371         LOGE(TAG, "drop malformed frame");
372         return NSTACKX_EFAILED;
373     }
374 
375     *frame = dFileFrame;
376     return NSTACKX_EOK;
377 }
378 
SetDfileHeaderFrameUserDataFlag(FileList * fileList)379 static inline void SetDfileHeaderFrameUserDataFlag(FileList *fileList)
380 {
381     fileList->userDataFlag = fileList->userDataFlag | NSTACKX_DFILE_HEADER_FRAME_USER_DATA_FLAG;
382 }
383 
CheckDfileHeaderFrameUserDataFlag(uint8_t flag)384 static inline uint8_t CheckDfileHeaderFrameUserDataFlag(uint8_t flag)
385 {
386     return flag & NSTACKX_DFILE_HEADER_FRAME_USER_DATA_FLAG;
387 }
388 
CheckDfileHeaderFramePathTypeFlag(uint8_t flag)389 static inline uint8_t CheckDfileHeaderFramePathTypeFlag(uint8_t flag)
390 {
391     return flag & NSTACKX_DFILE_HEADER_FRAME_PATH_TYPE_FLAG;
392 }
393 
CheckDfileHeaderFrameNoSyncFlag(uint8_t flag)394 static inline uint8_t CheckDfileHeaderFrameNoSyncFlag(uint8_t flag)
395 {
396     return flag & NSTACKX_DFILE_HEADER_FRAME_NO_SYNC_FLAG;
397 }
398 
DecodeFileHeaderFrameSp(FileList * fileList,FileHeaderFrame * headerFrame)399 int32_t DecodeFileHeaderFrameSp(FileList *fileList, FileHeaderFrame *headerFrame)
400 {
401     FileInfoUnit *fileInfoUnit = NULL;
402     size_t offset = 0;
403     if (ntohs(headerFrame->header.length) <= sizeof(uint16_t)) {
404         return NSTACKX_EFAILED;
405     }
406     uint8_t *buffer = (uint8_t *)headerFrame->fileInfoUnit;
407     uint16_t length = ntohs(headerFrame->header.length) - sizeof(uint16_t);
408     uint16_t nodeNumber = ntohs(headerFrame->nodeNumber);
409     int32_t ret;
410     if (FileListSetNum(fileList, nodeNumber) != NSTACKX_EOK) {
411         return NSTACKX_EFAILED;
412     }
413     if (CheckDfileHeaderFrameUserDataFlag(headerFrame->header.flag) != 0 ||
414         (CheckDfileHeaderFramePathTypeFlag(headerFrame->header.flag) != 0)) {
415         SetDfileHeaderFrameUserDataFlag(fileList);
416     }
417     if (CheckDfileHeaderFrameNoSyncFlag(headerFrame->header.flag) != 0) {
418         fileList->noSyncFlag = NSTACKX_TRUE;
419     }
420     while (offset < length) {
421         fileInfoUnit = (FileInfoUnit *)&buffer[offset];
422         if (length - offset <= offsetof(FileInfoUnit, fileName)) {
423             LOGE(TAG, "length %u is too small", length);
424             return NSTACKX_EFAILED;
425         }
426         size_t fileNameLength = ntohs(fileInfoUnit->fileNameLength);
427         uint16_t fileId = ntohs(fileInfoUnit->fileId);
428         if ((fileId == 0) && !(CheckDfileHeaderFrameUserDataFlag(headerFrame->header.flag)) &&
429             !(CheckDfileHeaderFramePathTypeFlag(headerFrame->header.flag))) {
430             /* Invalid file id, discard the whole frame. */
431             return NSTACKX_EFAILED;
432         }
433         /*
434          * fileNameLength validation:
435          * 1) Minimum value:
436          *    For normal file name (file id != 0), it should be > 0;
437          *    For user data (file id = 0), 0 is allowed.
438          * 2) Maximum value: should be <= remaining buffer - offsetof(FileInfoUnit, fileName)
439          */
440         if ((fileId != 0 && fileNameLength == 0) ||
441             (fileNameLength > (length - offset - offsetof(FileInfoUnit, fileName)))) {
442             return NSTACKX_EFAILED;
443         }
444         if (fileId == 0) {
445             ret = FileListAddUserData(fileList, fileInfoUnit->fileName, fileNameLength, headerFrame->header.flag);
446         } else {
447             uint64_t fileSize = be64toh(fileInfoUnit->fileSize);
448             ret = FileListAddFile(fileList, fileId, fileInfoUnit->fileName, fileNameLength, fileSize);
449         }
450         if (ret != NSTACKX_EOK) {
451             return NSTACKX_EFAILED;
452         }
453         offset += (offsetof(FileInfoUnit, fileName) + fileNameLength);
454     }
455 
456     return NSTACKX_EOK;
457 }
458 
DecodeFileHeaderFrame(FileList * fileList,FileHeaderFrame * headerFrame)459 int32_t DecodeFileHeaderFrame(FileList *fileList, FileHeaderFrame *headerFrame)
460 {
461     return DecodeFileHeaderFrameSp(fileList, headerFrame);
462 }
463 
DecodeFileHeaderConfirmFrame(FileList * fileList,FileHeaderConfirmFrame * confirmFrame)464 int32_t DecodeFileHeaderConfirmFrame(FileList *fileList, FileHeaderConfirmFrame *confirmFrame)
465 {
466     uint16_t fileId;
467     uint16_t i;
468     size_t length = ntohs(confirmFrame->header.length);
469     LOGI(TAG, "header confirm frame length %u", length);
470     /* Make sure payload buffer should contain valid number of file ID. */
471     if (length == 0 || length % sizeof(uint16_t) != 0) {
472         return NSTACKX_EFAILED;
473     }
474     uint16_t *fileIdList = confirmFrame->fileId;
475     uint16_t fileIdCount = (uint16_t)(length >> 1);
476 
477     for (i = 0; i < fileIdCount; i++) {
478         fileId = ntohs(fileIdList[i]);
479         if (fileId > FileListGetNum(fileList)) {
480             LOGE(TAG, "Invalid file ID %u", fileId);
481             continue;
482         }
483         FileListSetFileNameAcked(fileList, fileId);
484     }
485 
486     return NSTACKX_EOK;
487 }
488 
GetFileIdFromFileDataFrame(const FileList * fileList,const FileDataFrame * dataFrame)489 int16_t GetFileIdFromFileDataFrame(const FileList *fileList, const FileDataFrame *dataFrame)
490 {
491     uint16_t fileId;
492 
493     fileId = ntohs(dataFrame->fileId);
494     if (fileId == NSTACKX_RESERVED_FILE_ID || fileId > FileListGetNum(fileList)) {
495         return 0;
496     }
497 
498     return (int16_t)fileId;
499 }
500 
DecodeFileTransferDoneFrame(FileList * fileList,FileTransferDoneFrame * transferDoneFrame)501 int32_t DecodeFileTransferDoneFrame(FileList *fileList, FileTransferDoneFrame *transferDoneFrame)
502 {
503     uint32_t i;
504     uint32_t fileIdNum;
505     uint16_t fileId;
506 
507     uint16_t length = ntohs(transferDoneFrame->header.length);
508     if (length == 0 || length % sizeof(uint16_t) != 0) {
509         return NSTACKX_EFAILED;
510     }
511     fileIdNum = length / sizeof(uint16_t);
512 
513     LOGI(TAG, "transId %u, FileTransferDone:fileIdNum %u, file number %u",
514          ntohs(transferDoneFrame->header.transId), fileIdNum, FileListGetNum(fileList));
515     for (i = 0; i < fileIdNum; i++) {
516         fileId = ntohs(transferDoneFrame->fileId[i]);
517         if (fileId == 0 || fileId > FileListGetNum(fileList)) {
518             continue;
519         }
520         FileListSetFileSendSuccess(fileList, fileId);
521     }
522     return NSTACKX_EOK;
523 }
524 
IsSettingFrameLengthValid(const SettingFrame * hostSettingFrame,uint16_t payloadLength)525 static uint8_t IsSettingFrameLengthValid(const SettingFrame *hostSettingFrame, uint16_t payloadLength)
526 {
527     /*
528      * From dfile with historical version NSTACKX_DFILE_VERSION_0, whose setting frame is composed of header,
529      * mtu and connType.
530      */
531     if (payloadLength == sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType)) {
532         return NSTACKX_TRUE;
533     }
534 
535     if (payloadLength == sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType) +
536         sizeof(hostSettingFrame->dFileVersion)) {
537         return NSTACKX_TRUE;
538     }
539 
540     if (payloadLength == sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType) +
541         sizeof(hostSettingFrame->dFileVersion) + sizeof(hostSettingFrame->abmCapability)) {
542         return NSTACKX_TRUE;
543     }
544 
545     if (payloadLength == sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType) +
546         sizeof(hostSettingFrame->dFileVersion) + sizeof(hostSettingFrame->abmCapability) +
547         sizeof(hostSettingFrame->capability)) {
548         return NSTACKX_TRUE;
549     }
550 
551     if (payloadLength == sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType) +
552         sizeof(hostSettingFrame->dFileVersion) + sizeof(hostSettingFrame->abmCapability) +
553         sizeof(hostSettingFrame->capability) + sizeof(hostSettingFrame->dataFrameSize)) {
554         return NSTACKX_TRUE;
555     }
556 
557     /*
558      * From dfile with the same version with local dfile.
559      */
560     if (payloadLength >= sizeof(SettingFrame) - DFILE_FRAME_HEADER_LEN) {
561         return NSTACKX_TRUE;
562     }
563 
564     return NSTACKX_FALSE;
565 }
566 
IsSettingFrameMtuAndTypeValid(const SettingFrame * netSettingFrame)567 static uint8_t IsSettingFrameMtuAndTypeValid(const SettingFrame *netSettingFrame)
568 {
569     if (ntohs(netSettingFrame->mtu) < NSTACKX_MIN_MTU_SIZE ||
570         ntohs(netSettingFrame->connType) == CONNECT_TYPE_NONE ||
571         ntohs(netSettingFrame->connType) >= CONNECT_TYPE_MAX) {
572         return NSTACKX_FALSE;
573     }
574     return NSTACKX_TRUE;
575 }
576 
577 /*
578  * Note: netSettingFrame is a malloced buffer with length reading from network, so it is not reliable and its length
579  * may be shorter than sizeof(SettingFrame).
580  */
DecodeSettingFrame(SettingFrame * netSettingFrame,SettingFrame * hostSettingFrame)581 int32_t DecodeSettingFrame(SettingFrame *netSettingFrame, SettingFrame *hostSettingFrame)
582 {
583     if (netSettingFrame->header.sessionId != 0 || netSettingFrame->header.transId != 0) {
584         LOGE(TAG, "error transId for Setting Frame");
585         return NSTACKX_EFAILED;
586     }
587     uint16_t payloadLength = ntohs(netSettingFrame->header.length);
588     if (!IsSettingFrameLengthValid(hostSettingFrame, payloadLength)) {
589         LOGE(TAG, "illegal setting frame length %u", payloadLength);
590         return NSTACKX_EFAILED;
591     }
592     if (!IsSettingFrameMtuAndTypeValid(netSettingFrame)) {
593         LOGE(TAG, "illegal setting frame mtu or type");
594         return NSTACKX_EFAILED;
595     }
596     hostSettingFrame->header.sessionId = netSettingFrame->header.sessionId;
597     hostSettingFrame->mtu = ntohs(netSettingFrame->mtu);
598     hostSettingFrame->connType = ntohs(netSettingFrame->connType);
599     if (payloadLength == sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType)) {
600         /*
601          * In this condition, this netSettingFrame is from an old version and doesn't have the member dFileVersion.
602          * Then the dFileVersion will be set to zero by defulat.
603          * Note that the usage of netSettingFrame->dFileVersion is illegal in this condition.
604          */
605         LOGI(TAG, "this setting frame is from an old version whose setting frame doesn't have the member dFileVersion");
606         hostSettingFrame->dFileVersion = 0;
607     } else if (payloadLength == sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType) +
608         sizeof(hostSettingFrame->dFileVersion)) {
609         hostSettingFrame->dFileVersion = ntohl(netSettingFrame->dFileVersion);
610         hostSettingFrame->abmCapability = 0;
611     } else {
612         hostSettingFrame->dFileVersion = ntohl(netSettingFrame->dFileVersion);
613         hostSettingFrame->abmCapability = ntohl(netSettingFrame->abmCapability);
614         if (payloadLength > (sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType) +
615             sizeof(hostSettingFrame->dFileVersion) + sizeof(hostSettingFrame->abmCapability))) {
616             hostSettingFrame->capability = ntohl(netSettingFrame->capability);
617         }
618         if (payloadLength > (sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType) +
619             sizeof(hostSettingFrame->dFileVersion) + sizeof(hostSettingFrame->abmCapability) +
620             sizeof(hostSettingFrame->capability))) {
621             hostSettingFrame->dataFrameSize = ntohl(netSettingFrame->dataFrameSize);
622         }
623         if (payloadLength > (sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType) +
624             sizeof(hostSettingFrame->dFileVersion) + sizeof(hostSettingFrame->abmCapability) +
625             sizeof(hostSettingFrame->capability) + sizeof(hostSettingFrame->dataFrameSize))) {
626             hostSettingFrame->capsCheck = ntohl(netSettingFrame->capsCheck);
627         }
628     }
629     LOGI(TAG, "local dfile version is %u, remote dfile version is %u capability 0x%x dataFrameSize %u capsCheck 0x%x",
630          NSTACKX_DFILE_VERSION, hostSettingFrame->dFileVersion, hostSettingFrame->capability,
631          hostSettingFrame->dataFrameSize, hostSettingFrame->capsCheck);
632     return NSTACKX_EOK;
633 }
634 
DecodeRstFrame(RstFrame * rstFrame,uint16_t * code,uint16_t ** fileIdList,uint16_t * listCount)635 int32_t DecodeRstFrame(RstFrame *rstFrame, uint16_t *code, uint16_t **fileIdList, uint16_t *listCount)
636 {
637     uint16_t payloadLen = ntohs(rstFrame->header.length);
638     if (payloadLen < sizeof(uint16_t) || (payloadLen - sizeof(uint16_t)) % sizeof(uint16_t) != 0) {
639         return NSTACKX_EFAILED;
640     }
641     *code = ntohs(rstFrame->code);
642 
643     if (fileIdList != NULL && listCount != NULL) {
644         *fileIdList = rstFrame->fileId;
645         *listCount = (payloadLen - sizeof(uint16_t)) / sizeof(uint16_t);
646     }
647     return NSTACKX_EOK;
648 }
649 
DecodeBackPressFrame(const BackPressureFrame * backPressFrame,DataBackPressure * backPressInfo)650 int32_t DecodeBackPressFrame(const BackPressureFrame *backPressFrame, DataBackPressure *backPressInfo)
651 {
652     uint16_t payloadLen = ntohs(backPressFrame->header.length);
653     if (payloadLen < sizeof(BackPressureFrame) - sizeof(DFileFrameHeader)) {
654         return NSTACKX_EFAILED;
655     }
656 
657     backPressInfo->recvListOverIo = backPressFrame->backPressure.recvListOverIo;
658     backPressInfo->recvBufThreshold = backPressFrame->backPressure.recvBufThreshold;
659     backPressInfo->stopSendPeriod = ntohl(backPressFrame->backPressure.stopSendPeriod);
660 
661     return NSTACKX_EOK;
662 }
663