2
* Copyright (C) 2008-2011 Codership Oy <info@codership.com>
2
* Copyright (C) 2008-2013 Codership Oy <info@codership.com>
4
* $Id: gcs_act_proto.c 2832 2012-06-25 20:06:26Z alex $
4
* $Id: gcs_act_proto.c 2962 2013-02-12 12:06:38Z alex $
7
7
* Interface to action protocol
31
31
static const size_t PROTO_AT_OFFSET = 16;
32
32
static const size_t PROTO_DATA_OFFSET = 20;
34
static const gcs_seqno_t PROTO_ACT_ID_MAX = 0x00FFFFFFFFFFFFLL;
35
static const size_t PROTO_ACT_SIZE_MAX = 0xFFFFFFFF;
36
static const ulong PROTO_FRAG_NO_MAX = 0xFFFFFFFF;
37
static const ulong PROTO_AT_MAX = 0xFF;
34
static const gcs_seqno_t PROTO_ACT_ID_MAX = 0x00FFFFFFFFFFFFLL;
35
static const unsigned int PROTO_FRAG_NO_MAX = 0xFFFFFFFF;
36
static const unsigned char PROTO_AT_MAX = 0xFF;
39
static const int PROTO_VERSION = GCS_ACT_PROTO_MAX;
38
static const int PROTO_VERSION = GCS_ACT_PROTO_MAX;
41
40
#define PROTO_MAX_HDR_SIZE PROTO_DATA_OFFSET // for now
48
47
gcs_act_proto_write (gcs_act_frag_t* frag, void* buf, size_t buf_len)
50
49
#ifdef GCS_DEBUG_PROTO
51
if ((frag->act_id > PROTO_ACT_ID_MAX) ||
52
(frag->act_size > PROTO_ACT_SIZE_MAX) ||
53
(frag->frag_no > PROTO_FRAG_NO_MAX) ||
50
if ((frag->act_id > PROTO_ACT_ID_MAX) ||
51
(frag->act_size > GCS_MAX_ACT_SIZE) ||
52
(frag->frag_no > PROTO_FRAG_NO_MAX) ||
54
53
(frag->act_type > PROTO_AT_MAX)) {
55
54
gu_error ("Exceeded protocol limits: %d(%d), %d(%d), %d(%d), %d(%d)",
56
55
frag->act_id, PROTO_ACT_ID_MAX,
57
frag->act_size, PROTO_ACT_SIZE_MAX,
56
frag->act_size, GCS_MAX_ACT_SIZE,
58
57
frag->frag_no, PROTO_FRAG_NO_MAX,
59
58
frag->act_type, PROTO_AT_MAX);
63
62
if (buf_len < PROTO_DATA_OFFSET) return -EMSGSIZE;
65
// assert (frag->act_size <= PROTO_ACT_SIZE_MAX);
66
67
((uint64_t*)buf)[0] = gu_be64(frag->act_id);
67
68
((uint32_t*)buf)[2] = htogl ((uint32_t)frag->act_size);
68
69
((uint32_t*)buf)[3] = htogl (frag->frag_no);
84
85
gcs_act_proto_read (gcs_act_frag_t* frag, const void* buf, size_t buf_len)
86
87
frag->proto_ver = ((uint8_t*)buf)[PROTO_PV_OFFSET];
87
#ifdef GCS_DEBUG_PROTO
89
if (gu_unlikely(buf_len < PROTO_DATA_OFFSET)) {
90
gu_error ("Action message too short: %zu, expected at least %d",
91
buf_len, PROTO_DATA_OFFSET);
88
95
if (gu_unlikely(frag->proto_ver > PROTO_VERSION)) {
89
96
gu_error ("Bad protocol version %d, expected %d",
90
97
frag->proto_ver, PROTO_VERSION);
91
98
return -EPROTO; // this fragment should be dropped
93
if (gu_unlikely(buf_len < PROTO_DATA_OFFSET)) {
94
gu_error ("Action message too short: %zu, expected at least %d",
95
buf_len, PROTO_DATA_OFFSET);
99
101
((uint8_t*)buf)[PROTO_PV_OFFSET] = 0x0;
100
102
frag->act_id = gu_be64(*(uint64_t*)buf);
101
103
frag->act_size = gtohl (((uint32_t*)buf)[2]);
102
104
frag->frag_no = gtohl (((uint32_t*)buf)[3]);
103
105
frag->act_type = ((uint8_t*)buf)[PROTO_AT_OFFSET];
105
106
frag->frag = ((uint8_t*)buf) + PROTO_DATA_OFFSET;
106
107
frag->frag_len = buf_len - PROTO_DATA_OFFSET;
109
/* return 0 or -EMSGSIZE */
110
return ((frag->act_size > GCS_MAX_ACT_SIZE) * -EMSGSIZE);
110
113
/*! Returns protocol header size */