D-BUS Protocol Specification Version 0.1 22 January 2003 Havoc Pennington
hp@pobox.com
Introduction D-BUS is a system for low-latency, low-overhead, easy to use interprocess communication (IPC). In more detail: D-BUS is low-latency because it is designed to avoid round trips and allow asynchronous operation, much like the X protocol. D-BUS is low-overhead because it is uses a binary protocol, and does not have to convert to and from a text format such as XML. Because D-BUS is intended for potentially high-resolution same-machine IPC, not primarily for Internet IPC, this is an interesting optimization. D-BUS is easy to use because it works in terms of messages rather than byte streams, and does not require users to understand any complex concepts such as a new type system or elaborate APIs. Libraries implementing D-BUS may choose to abstract messages as "method calls" (see ). The base D-BUS protocol is a peer-to-peer protocol, specified in . That is, it is a system for one application to talk to a single other application. However, the primary intended application of D-BUS is the D-BUS message bus, specified in . The message bus is a special application that accepts connections from multiple other applications, and forwards messages among them. Message Protocol A message consists of a header and a body. If you think of a message as a package, the header is the address, and the body contains the package contents. The message delivery system uses the header information to figure out where to send the message and how to interpret it; the recipient inteprets the body of the message. The body of the message is made up of zero or more arguments, which are typed values, such as an integer or a byte array. Header Encoding Following the mandatory fields, there are zero or more named fields (see ), and then nul bytes padding the header such that its total length in bytes is a multiple of 8. The header MUST begin with the following mandatory fields in the following order: Size Description 1 byte Endianness flag; ASCII 'l' for little-endian or ASCII 'B' for big-endian. 1 byte Bitwise OR of flags. Unknown flags MUST be ignored. Currently-defined flags are described below. 1 byte Major protocol version of the sending application. If the major protocol version of the receiving application does not match, the applications will not be able to communicate and the D-BUS connection MUST be disconnected. The major protocol version for this version of the specification is 0. 1 byte A nul byte, reserved for future use. Any value for this byte MUST be accepted. 4 bytes An unsigned 32-bit integer in the message's byte order, indicating the total length in bytes of the header including named fields and any alignment padding. MUST be a multiple of 8. 4 bytes An unsigned 32-bit integer in the message's byte order, indicating the total length in bytes of the message body. 4 bytes The message's serial number, a signed 32-bit integer in the message's byte order. Applications MUST NOT reuse the same serial number for different messages more often than 32-bit integer wraparound. Serial numbers must be greater than zero. Flags that can appear in the second byte of the header: Hex value Description 0x1 This message is an error reply. Header Fields In addition to the required header information mentioned in , the header may contain zero or more named header fields. These fields are named to allow future versions of this protocol specification to add new fields; implementations must ignore fields they do not understand. Implementations must not invent their own header fields; only changes to this specification may introduce new header fields. Header Alignment Padding To allow implementations to keep the header and the body in a single buffer while keeping data types aligned, the total length of the header must be a multiple of 8 bytes. To achieve this, the header MUST be padded with nul bytes to align its total length on an 8-byte boundary. The minimum number of padding bytes MUST be used. Because all possible named fields use at least 8 bytes, implementations can distinguish padding (which must be less than 8 bytes) from additional named fields (which must be at least 8 bytes). Message Arguments The message body is made up of arguments. Each argument is a type code, followed by the value of the argument in a type-dependent format. The type codes are as follows: Type name Code Description INVALID 0 Not a valid type code (error if it appears in a message) NIL 1 Marks an "unset" or "nonexistent" argument INT32 2 32-bit signed integer UINT32 3 32-bit unsigned integer DOUBLE 4 IEEE 754 double STRING 5 UTF-8 string (must be valid UTF-8) INT32_ARRAY 6 Array of INT32 UINT32_ARRAY 7 Array of UINT32 DOUBLE_ARRAY 8 Array of DOUBLE BYTE_ARRAY 9 Array of bytes STRING_ARRAY 10 Array of STRING The types are encoded as follows: Authentication Protocol Before the flow of messages begins, two applications must authenticate. A simple text protocol is used for authentication; this protocol is a SASL profile, and maps fairly directly from the SASL specification. [move the dbus-sasl-profile.txt stuff into here and clean it up] Server Addresses [document the string format of an address, and how it maps to unix domain sockets, tcp, or whatever] Message Conventions This section documents conventions that are not essential to D-BUS functionality, but should generally be followed in order to simplify programmer's lives. Message Naming Messages are normally named in the form "org.freedesktop.Peer.Ping", which has three distinct components: Namespace e.g. org.freedesktop Message names have a Java-style namespace: a reversed domain name. The components of the domain are normally lowercase. Package or object e.g. Peer The next part of the message name can be thought of as the name of a singleton object, or as the name of a package of related messages. More than one dot-separated component might be used here. (Note that D-BUS does not define any idea of object instances or object references.) The package or object name is capitalized LikeThis. Method or operation e.g. Ping The final part of the message name is the most specific, and should be a verb indicating an operation to be performed on the object. The method or operation name is capitalized LikeThis. A reply to a message is conventionally named by appending the string :Reply. So the reply to org.freedesktop.Peer.Ping is org.freedesktop.Peer.Ping:Reply. Method Call Mapping Some implementations of D-BUS may present an API that translates object method calls into D-BUS messages. This document does not specify in detail how such an API should look or work. However, it does specify how message-based protocols should be designed to be friendly to such an API. Remember that D-BUS does not have object references or object instances. So when one application sends the message org.freedesktop.Peer.Ping, it sends it to another application, not to any kind of sub-portion of that application. However, a convenience API used within the recipient application may route all messages that start with org.freedesktop.Peer to a particular object instance, and may invoke the Ping() method on said instance in order to handle the message. This is a convenience API based on method calls. A "method call" consists of a message and, optionally, a reply to that message. The name of the "method" is the last component of the message, for example, org.freedesktop.Peer.Ping would map to the method Ping() on some object. Arguments to a method may be considered "in" (processed by the recipient of the message), or "out" (returned to the sender of the message in the reply). "inout" arguments are both sent and received, i.e. the caller passes in a value which is modified. Given a method with zero or one return values, followed by zero or more arguments, where each argument may be "in", "out", or "inout", the caller constructs a message by appending each "in" or "inout" argument, in order. "out" arguments are not represented in the caller's message. The recipient constructs a reply by appending first the return value if any, then each "out" or "inout" argument, in order. "in" arguments are not represented in the reply message. Standard Peer-to-Peer Messages In the following message definitions, "method call notation" is presented in addition to simply listing the message names and arguments. The special type name ANY means any type other than NIL, and the special type name ANY_OR_NIL means any valid type. [FIXME the messages here are just made up to illustrate the format for defining them] <literal>org.freedesktop.Peer.Ping</literal> As a method: void Ping () On receipt of the message org.freedesktop.Peer.Ping, an application should reply with org.freedesktop.Peer.Ping:Reply. Neither the message nor its reply have any arguments. [FIXME the messages here are just made up to illustrate the format for defining them] <literal>org.freedesktop.Props.Get</literal> As a method: ANY_OR_NIL Get (in STRING property_name) Message arguments: Argument Type Description 0 STRING Name of the property to get Reply arguments: Argument Type Description 0 ANY_OR_NIL The value of the property. The type depends on the property. [FIXME the messages here are just made up to illustrate the format for defining them] Message Bus Specification Message Bus Overview The message bus accepts connections from one or more applications. Once connected, applications can send and receive messages from the message bus, as in the peer-to-peer case. The message bus keeps track of a set of services. A service is simply a name, such as com.yoyodyne.Screensaver, which can be owned by one of the connected applications. The message bus itself always owns the special service org.freedesktop.DBus. Messages may have a srvc field (see ). When the message bus receives a message, if the srvc field is absent, the message is taken to be a standard peer-to-peer message and interpreted by the message bus itself. For example, sending an org.freedesktop.Peer.Ping message with no srvc will cause the message bus itself to reply to the ping immediately; the message bus would never make this message visible to other applications. If the srvc field is present, then it indicates a request for the message bus to route the message. In the usual case, messages are routed to the owner of the named service. Messages may also be broadcast by sending them to the special service org.freedesktop.Broadcast. Broadcast messages are sent to all applications with message matching rules that match the message. Continuing the org.freedesktop.Peer.Ping example, if the ping message were sent with a srvc name of com.yoyodyne.Screensaver, then the ping would be forwarded, and the Yoyodyne Corporation screensaver application would be expected to reply to the ping. If org.freedesktop.Peer.Ping were sent to org.freedesktop.Broadcast, then multiple applications might receive the ping, and all would normally reply to it. Message Bus Messages The special message bus service org.freedesktop.DBus responds to a number of messages, allowing applications to interact with the message bus. [document the messages here] Message Bus Service Activation [document file format, filesystem locations, etc. for activation] Finding The Message Bus Two standard message bus instances are defined here, along with how to locate them. Each time a user logs in, a desktop session message bus may be started. All applications in the user's login session may interact with one another using this message bus. [specify how to find the address of the desktop session message bus via environment variable and/or X property] A computer may have a system message bus, accessible to all applications on the system. This message bus may be used to broadcast system events, such as adding new hardware devices. [specify how to find the address of the system message bus] Implementation notes Glossary This glossary defines some of the terms used in this specification. Broadcast A message sent to the special org.freedesktop.Broadcast service; the message bus will forward the broadcast message to all clients that have expressed interest in it. Message A message is the atomic unit of communication via the D-BUS protocol. It consists of a header and a body; the body is made up of arguments. Message Bus The message bus is a special application that forwards or broadcasts messages between a group of applications connected to the message bus. It also manages services. Service A service is simply a named application that other applications can refer to. For example, the hypothetical com.yoyodyne.Screensaver service might accept messages that affect a screensaver from Yoyodyne Corporation. An application is said to own a service if the message bus has associated the application with the service name.