ctdb-protocol: Use ctdb_req_header marshalling
authorAmitay Isaacs <amitay@gmail.com>
Thu, 21 Apr 2016 14:19:49 +0000 (00:19 +1000)
committerMartin Schwenke <martins@samba.org>
Tue, 3 May 2016 02:43:20 +0000 (04:43 +0200)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/protocol/protocol_call.c
ctdb/protocol/protocol_control.c
ctdb/protocol/protocol_message.c
ctdb/tests/src/protocol_client_test.c

index 212b93a220355d0b36c4a60a8065ce1650e9f6a2..bf303a28fea9fb08c0697ec3133ce583f07171ac 100644 (file)
@@ -95,7 +95,7 @@ int ctdb_req_call_push(struct ctdb_req_header *h, struct ctdb_req_call *c,
        wire = (struct ctdb_req_call_wire *)buf;
 
        h->length = buflen;
-       memcpy(&wire->hdr, h, sizeof(struct ctdb_req_header));
+       ctdb_req_header_push(h, (uint8_t *)&wire->hdr);
 
        wire->flags = c->flags;
        wire->db_id = c->db_id;
@@ -132,7 +132,10 @@ int ctdb_req_call_pull(uint8_t *pkt, size_t pkt_len,
        }
 
        if (h != NULL) {
-               memcpy(h, &wire->hdr, sizeof(struct ctdb_req_header));
+               ret = ctdb_req_header_pull((uint8_t *)&wire->hdr, pkt_len, h);
+               if (ret != 0) {
+                       return ret;
+               }
        }
 
        c->flags = wire->flags;
@@ -173,7 +176,7 @@ int ctdb_reply_call_push(struct ctdb_req_header *h, struct ctdb_reply_call *c,
        wire = (struct ctdb_reply_call_wire *)buf;
 
        h->length = buflen;
-       memcpy(&wire->hdr, h, sizeof(struct ctdb_req_header));
+       ctdb_req_header_push(h, (uint8_t *)&wire->hdr);
 
        wire->status = c->status;
        wire->datalen = ctdb_tdb_data_len(c->data);
@@ -205,7 +208,10 @@ int ctdb_reply_call_pull(uint8_t *pkt, size_t pkt_len,
        }
 
        if (h != NULL) {
-               memcpy(h, &wire->hdr, sizeof(struct ctdb_req_header));
+               ret = ctdb_req_header_pull((uint8_t *)&wire->hdr, pkt_len, h);
+               if (ret != 0) {
+                       return ret;
+               }
        }
 
        c->status = wire->status;
@@ -237,7 +243,7 @@ int ctdb_reply_error_push(struct ctdb_req_header *h, struct ctdb_reply_error *c,
        wire = (struct ctdb_reply_error_wire *)buf;
 
        h->length = buflen;
-       memcpy(&wire->hdr, h, sizeof(struct ctdb_req_header));
+       ctdb_req_header_push(h, (uint8_t *)&wire->hdr);
 
        wire->status = c->status;
        wire->msglen = ctdb_tdb_data_len(c->msg);
@@ -269,7 +275,10 @@ int ctdb_reply_error_pull(uint8_t *pkt, size_t pkt_len,
        }
 
        if (h != NULL) {
-               memcpy(h, &wire->hdr, sizeof(struct ctdb_req_header));
+               ret = ctdb_req_header_pull((uint8_t *)&wire->hdr, pkt_len, h);
+               if (ret != 0) {
+                       return ret;
+               }
        }
 
        c->status = wire->status;
@@ -302,7 +311,7 @@ int ctdb_req_dmaster_push(struct ctdb_req_header *h, struct ctdb_req_dmaster *c,
        wire = (struct ctdb_req_dmaster_wire *)buf;
 
        h->length = buflen;
-       memcpy(&wire->hdr, h, sizeof(struct ctdb_req_header));
+       ctdb_req_header_push(h, (uint8_t *)&wire->hdr);
 
        wire->db_id = c->db_id;
        wire->rsn = c->rsn;
@@ -338,7 +347,10 @@ int ctdb_req_dmaster_pull(uint8_t *pkt, size_t pkt_len,
        }
 
        if (h != NULL) {
-               memcpy(h, &wire->hdr, sizeof(struct ctdb_req_header));
+               ret = ctdb_req_header_pull((uint8_t *)&wire->hdr, pkt_len, h);
+               if (ret != 0) {
+                       return ret;
+               }
        }
 
        c->db_id = wire->db_id;
@@ -380,7 +392,7 @@ int ctdb_reply_dmaster_push(struct ctdb_req_header *h,
        wire = (struct ctdb_reply_dmaster_wire *)buf;
 
        h->length = buflen;
-       memcpy(&wire->hdr, h, sizeof(struct ctdb_req_header));
+       ctdb_req_header_push(h, (uint8_t *)&wire->hdr);
 
        wire->db_id = c->db_id;
        wire->rsn = c->rsn;
@@ -415,7 +427,10 @@ int ctdb_reply_dmaster_pull(uint8_t *pkt, size_t pkt_len,
        }
 
        if (h != NULL) {
-               memcpy(h, &wire->hdr, sizeof(struct ctdb_req_header));
+               ret = ctdb_req_header_pull((uint8_t *)&wire->hdr, pkt_len, h);
+               if (ret != 0) {
+                       return ret;
+               }
        }
 
        c->db_id = wire->db_id;
index d07fd93d72c9773fa0ed9f13b295d1d3dbaf61b6..0e250c2862d6be61d8515ecc3084d2eab77462e6 100644 (file)
@@ -1911,7 +1911,7 @@ int ctdb_req_control_push(struct ctdb_req_header *h,
        wire = (struct ctdb_req_control_wire *)buf;
 
        h->length = buflen;
-       memcpy(&wire->hdr, h, sizeof(struct ctdb_req_header));
+       ctdb_req_header_push(h, (uint8_t *)&wire->hdr);
 
        wire->opcode = request->opcode;
        wire->pad = request->pad;
@@ -1946,7 +1946,10 @@ int ctdb_req_control_pull(uint8_t *pkt, size_t pkt_len,
        }
 
        if (h != NULL) {
-               memcpy(h, &wire->hdr, sizeof(struct ctdb_req_header));
+               ret = ctdb_req_header_pull((uint8_t *)&wire->hdr, pkt_len, h);
+               if (ret != 0) {
+                       return ret;
+               }
        }
 
        request->opcode = wire->opcode;
@@ -1992,7 +1995,7 @@ int ctdb_reply_control_push(struct ctdb_req_header *h,
        wire = (struct ctdb_reply_control_wire *)buf;
 
        h->length = buflen;
-       memcpy(&wire->hdr, h, sizeof(struct ctdb_req_header));
+       ctdb_req_header_push(h, (uint8_t *)&wire->hdr);
 
        wire->status = reply->status;
 
@@ -2029,7 +2032,10 @@ int ctdb_reply_control_pull(uint8_t *pkt, size_t pkt_len, uint32_t opcode,
        }
 
        if (h != NULL) {
-               memcpy(h, &wire->hdr, sizeof(struct ctdb_req_header));
+               ret = ctdb_req_header_pull((uint8_t *)&wire->hdr, pkt_len, h);
+               if (ret != 0) {
+                       return ret;
+               }
        }
 
        reply->status = wire->status;
index 2e12bbb3b1fa6a3505fe49b865365877a1efb694..543300dec1c9d787bbbc5fce770edd6469265db9 100644 (file)
@@ -301,7 +301,7 @@ int ctdb_req_message_push(struct ctdb_req_header *h,
        wire = (struct ctdb_req_message_wire *)buf;
 
        h->length = buflen;
-       memcpy(&wire->hdr, h, sizeof(struct ctdb_req_header));
+       ctdb_req_header_push(h, (uint8_t *)&wire->hdr);
 
        wire->srvid = message->srvid;
        wire->datalen = datalen;
@@ -332,7 +332,10 @@ int ctdb_req_message_pull(uint8_t *pkt, size_t pkt_len,
        }
 
        if (h != NULL) {
-               memcpy(h, &wire->hdr, sizeof(struct ctdb_req_header));
+               ret = ctdb_req_header_pull((uint8_t *)&wire->hdr, pkt_len, h);
+               if (ret != 0) {
+                       return ret;
+               }
        }
 
        message->srvid = wire->srvid;
@@ -362,7 +365,7 @@ int ctdb_req_message_data_push(struct ctdb_req_header *h,
        wire = (struct ctdb_req_message_wire *)buf;
 
        h->length = buflen;
-       memcpy(&wire->hdr, h, sizeof(struct ctdb_req_header));
+       ctdb_req_header_push(h, (uint8_t *)&wire->hdr);
 
        wire->srvid = message->srvid;
        wire->datalen = ctdb_tdb_data_len(message->data);
@@ -393,7 +396,10 @@ int ctdb_req_message_data_pull(uint8_t *pkt, size_t pkt_len,
        }
 
        if (h != NULL) {
-               memcpy(h, &wire->hdr, sizeof(struct ctdb_req_header));
+               ret = ctdb_req_header_pull((uint8_t *)&wire->hdr, pkt_len, h);
+               if (ret != 0) {
+                       return ret;
+               }
        }
 
        message->srvid = wire->srvid;
index c7ac233d7784ef6794317065c82e070fc6057f05..df4048febadccf054db2912033050c44a1126e3b 100644 (file)
@@ -40,7 +40,7 @@
 static void verify_ctdb_req_header(struct ctdb_req_header *h,
                                   struct ctdb_req_header *h2)
 {
-       verify_buffer(h, h2, sizeof(struct ctdb_req_header));
+       verify_buffer(h, h2, ctdb_req_header_len(h));
 }
 
 static void fill_ctdb_req_call(TALLOC_CTX *mem_ctx,
@@ -1945,7 +1945,7 @@ static void test_ctdb_req_header(void)
        TALLOC_CTX *mem_ctx;
        uint8_t *pkt;
        size_t pkt_len;
-       struct ctdb_req_header *h, h2;
+       struct ctdb_req_header h, h2;
        int ret;
 
        printf("ctdb_req_header\n");
@@ -1954,18 +1954,21 @@ static void test_ctdb_req_header(void)
        mem_ctx = talloc_new(NULL);
        assert(mem_ctx != NULL);
 
-       ret = allocate_pkt(mem_ctx, sizeof(struct ctdb_req_header),
+       ctdb_req_header_fill(&h, GENERATION, OPERATION, DESTNODE, SRCNODE,
+                            REQID);
+
+       ret = allocate_pkt(mem_ctx, ctdb_req_header_len(&h),
                           &pkt, &pkt_len);
        assert(ret == 0);
+       assert(pkt != NULL);
+       assert(pkt_len >= ctdb_req_header_len(&h));
 
-       h = (struct ctdb_req_header *)pkt;
-       ctdb_req_header_fill(h, GENERATION, OPERATION, DESTNODE, SRCNODE,
-                            REQID);
+       ctdb_req_header_push(&h, pkt);
 
        ret = ctdb_req_header_pull(pkt, pkt_len, &h2);
        assert(ret == 0);
 
-       verify_ctdb_req_header(h, &h2);
+       verify_ctdb_req_header(&h, &h2);
 
        talloc_free(mem_ctx);
 }