1// Copyright 2016 The Fuchsia Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "message.h" 6 7#include <stdint.h> 8 9_Static_assert(sizeof(mojo_message_header_t) == 16u, "mojo_message_header_t should be 16 bytes"); 10 11_Static_assert(sizeof(mojo_message_header_with_request_id_t) == 24u, 12 "mojo_message_header_t should be 24 bytes"); 13 14bool mojo_validate_message_header(const mojo_struct_header_t* header, size_t size) { 15 if (header->num_bytes < sizeof(mojo_message_header_t) || size < sizeof(mojo_message_header_t) || 16 size > UINT32_MAX) { 17 return false; 18 } 19 20 const mojo_message_header_t* message_header = (const mojo_message_header_t*)header; 21 22 // Message expects response and message is response flags are mutually 23 // exclusive. 24 if ((message_header->flags & MOJO_MESSAGE_HEADER_FLAGS_EXPECTS_RESPONSE) && 25 (message_header->flags & MOJO_MESSAGE_HEADER_FLAGS_IS_RESPONSE)) { 26 return false; 27 } 28 29 if (header->version == 0u) { 30 if (header->num_bytes != sizeof(mojo_message_header_t)) { 31 return false; 32 } 33 34 // Version 0 has no request id and should not have either of these flags. 35 if ((message_header->flags & MOJO_MESSAGE_HEADER_FLAGS_EXPECTS_RESPONSE) || 36 (message_header->flags & MOJO_MESSAGE_HEADER_FLAGS_IS_RESPONSE)) { 37 return false; 38 } 39 } else if (header->version == 1u) { 40 if (header->num_bytes != sizeof(mojo_message_header_with_request_id_t)) { 41 return false; 42 } 43 } 44 // Accept unknown versions of the message header to be future-proof. 45 46 return true; 47} 48