| 1 |
#pragma once |
| 2 |
|
| 3 |
/* |
| 4 |
* D-Bus Messages |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <c-stdaux.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include "dbus/address.h" |
| 10 |
#include "dbus/protocol.h" |
| 11 |
#include "util/ref.h" |
| 12 |
|
| 13 |
typedef struct FDList FDList; |
| 14 |
typedef struct Log Log; |
| 15 |
typedef struct Message Message; |
| 16 |
typedef struct MessageHeader MessageHeader; |
| 17 |
typedef struct MessageMetadata MessageMetadata; |
| 18 |
|
| 19 |
/* max message size; taken from spec */ |
| 20 |
#define MESSAGE_SIZE_MAX (128UL * 1024UL * 1024UL) |
| 21 |
|
| 22 |
/* max patch buffer size; see message_stitch_sender() */ |
| 23 |
#define MESSAGE_PATCH_MAX (C_ALIGN_TO(1 + 3 + 4 + ADDRESS_ID_STRING_MAX + 1, 8)) |
| 24 |
|
| 25 |
enum { |
| 26 |
_MESSAGE_E_SUCCESS, |
| 27 |
|
| 28 |
MESSAGE_E_CORRUPT_HEADER, |
| 29 |
MESSAGE_E_TOO_LARGE, |
| 30 |
MESSAGE_E_INVALID_HEADER, |
| 31 |
MESSAGE_E_INVALID_BODY, |
| 32 |
|
| 33 |
MESSAGE_E_MISSING_FDS, |
| 34 |
}; |
| 35 |
|
| 36 |
struct MessageMetadata { |
| 37 |
struct { |
| 38 |
uint8_t type; |
| 39 |
uint8_t flags; |
| 40 |
uint8_t version; |
| 41 |
uint32_t serial; |
| 42 |
} header; |
| 43 |
|
| 44 |
uint64_t sender_id; |
| 45 |
|
| 46 |
struct { |
| 47 |
unsigned int available; |
| 48 |
const char *path; |
| 49 |
const char *interface; |
| 50 |
const char *member; |
| 51 |
const char *error_name; |
| 52 |
uint32_t reply_serial; |
| 53 |
const char *destination; |
| 54 |
const char *sender; |
| 55 |
const char *signature; |
| 56 |
uint32_t unix_fds; |
| 57 |
} fields; |
| 58 |
|
| 59 |
struct { |
| 60 |
char element; |
| 61 |
const void *value; |
| 62 |
} args[64]; |
| 63 |
size_t n_args; |
| 64 |
}; |
| 65 |
|
| 66 |
#define MESSAGE_METADATA_INIT { \ |
| 67 |
.sender_id = ADDRESS_ID_INVALID, \ |
| 68 |
} |
| 69 |
|
| 70 |
struct Message { |
| 71 |
_Atomic unsigned long n_refs; |
| 72 |
|
| 73 |
bool big_endian : 1; |
| 74 |
bool allocated_data : 1; |
| 75 |
bool parsed : 1; |
| 76 |
|
| 77 |
FDList *fds; |
| 78 |
|
| 79 |
size_t n_data; |
| 80 |
size_t n_copied; |
| 81 |
size_t n_header; |
| 82 |
size_t n_body; |
| 83 |
|
| 84 |
void *data; |
| 85 |
MessageHeader *header; |
| 86 |
MessageMetadata metadata; |
| 87 |
void *body; |
| 88 |
|
| 89 |
void *original_sender; |
| 90 |
struct iovec vecs[4]; |
| 91 |
alignas(8) uint8_t patch[MESSAGE_PATCH_MAX]; |
| 92 |
alignas(8) uint8_t extra[]; |
| 93 |
}; |
| 94 |
|
| 95 |
#define MESSAGE_INIT(_big_endian) { \ |
| 96 |
.n_refs = REF_INIT, \ |
| 97 |
.big_endian = _big_endian, \ |
| 98 |
.metadata = MESSAGE_METADATA_INIT, \ |
| 99 |
} |
| 100 |
|
| 101 |
struct MessageHeader { |
| 102 |
uint8_t endian; |
| 103 |
uint8_t type; |
| 104 |
uint8_t flags; |
| 105 |
uint8_t version; |
| 106 |
uint32_t n_body; |
| 107 |
uint32_t serial; |
| 108 |
uint32_t n_fields; |
| 109 |
} _c_packed_; |
| 110 |
|
| 111 |
int message_new_incoming(Message **messagep, MessageHeader header); |
| 112 |
int message_new_outgoing(Message **messagep, void *data, size_t n_data); |
| 113 |
void message_free(_Atomic unsigned long *n_refs, void *userdata); |
| 114 |
|
| 115 |
int message_parse_metadata(Message *message); |
| 116 |
void message_stitch_sender(Message *message, uint64_t sender_id); |
| 117 |
|
| 118 |
void message_log_append(Message *message, Log *log); |
| 119 |
|
| 120 |
/* inline helpers */ |
| 121 |
|
| 122 |
/** |
| 123 |
* message_ref() - XXX |
| 124 |
*/ |
| 125 |
static inline Message *message_ref(Message *message) { |
| 126 |
if (message) |
| 127 |
ref_inc(&message->n_refs); |
| 128 |
return message; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* message_unref() - XXX |
| 133 |
*/ |
| 134 |
static inline Message *message_unref(Message *message) { |
| 135 |
if (message) |
| 136 |
ref_dec(&message->n_refs, message_free, NULL); |
| 137 |
return NULL; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* message_read_serial() - XXX |
| 142 |
*/ |
| 143 |
static inline uint32_t message_read_serial(Message *message) { |
| 144 |
if (message->header->type != DBUS_MESSAGE_TYPE_METHOD_CALL || |
| 145 |
_c_unlikely_(message->header->flags & DBUS_HEADER_FLAG_NO_REPLY_EXPECTED)) |
| 146 |
return 0; |
| 147 |
|
| 148 |
if (_c_likely_(!message->big_endian)) |
| 149 |
return le32toh(message->header->serial); |
| 150 |
else |
| 151 |
return be32toh(message->header->serial); |
| 152 |
} |
| 153 |
|
| 154 |
C_DEFINE_CLEANUP(Message *, message_unref); |