1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/*
* Ouroboros - Copyright (C) 2016 - 2017
*
* Protocol Control Information of FRCT
*
* Dimitri Staessens <dimitri.staessens@ugent.be>
* Sander Vrijders <sander.vrijders@ugent.be>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., http://www.fsf.org/about/contact/.
*/
#include <ouroboros/frct_pci.h>
#include <ouroboros/hash.h>
#include <ouroboros/errno.h>
#define OUROBOROS_PREFIX "frct-pci"
#include <ouroboros/logs.h>
#include <assert.h>
#include <string.h>
#define TYPE_SIZE 1
#define SEQNO_SIZE 8
#define FLAGS_SIZE 1
/* FIXME: Head size will differ on type */
#define HEAD_SIZE TYPE_SIZE + FLAGS_SIZE + SEQNO_SIZE
int frct_pci_ser(struct shm_du_buff * sdb,
struct frct_pci * pci,
bool error_check)
{
uint8_t * head;
uint8_t * tail;
assert(sdb);
assert(pci);
head = shm_du_buff_head_alloc(sdb, HEAD_SIZE);
if (head == NULL)
return -EPERM;
memcpy(head, &pci->type, TYPE_SIZE);
memcpy(head + TYPE_SIZE, &pci->flags, FLAGS_SIZE);
memcpy(head + TYPE_SIZE + FLAGS_SIZE, &pci->seqno, SEQNO_SIZE);
if (error_check) {
tail = shm_du_buff_tail_alloc(sdb, hash_len(HASH_CRC32));
if (tail == NULL) {
shm_du_buff_head_release(sdb, HEAD_SIZE);
return -EPERM;
}
*((uint32_t *) tail) = 0;
mem_hash(HASH_CRC32, (uint32_t *) tail, head, tail - head);
}
return 0;
}
int frct_pci_des(struct shm_du_buff * sdb,
struct frct_pci * pci,
bool error_check)
{
uint8_t * head;
uint8_t * tail;
uint32_t crc;
uint32_t crc2;
assert(sdb);
assert(pci);
head = shm_du_buff_head(sdb);
/* FIXME: Depending on the type a different deserialization */
memcpy(&pci->type, head, TYPE_SIZE);
memcpy(&pci->flags, head + TYPE_SIZE, FLAGS_SIZE);
memcpy(&pci->seqno, head + TYPE_SIZE + FLAGS_SIZE, SEQNO_SIZE);
if (error_check) {
tail = shm_du_buff_tail(sdb);
if (tail == NULL)
return -EPERM;
mem_hash(HASH_CRC32, &crc, head,
tail - head - hash_len(HASH_CRC32));
memcpy(&crc2, tail - hash_len(HASH_CRC32),
hash_len(HASH_CRC32));
/* Corrupted SDU */
if (crc != crc2)
return -1;
shm_du_buff_tail_release(sdb, hash_len(HASH_CRC32));
}
shm_du_buff_head_release(sdb, HEAD_SIZE);
return 0;
}
|