2005-02-17 Colin Walters <walters@verbum.org>
[platform/upstream/dbus.git] / doc / dbus-specification.xml
1 <?xml version="1.0" standalone="no"?>
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"
4 [
5 ]>
6
7 <article id="index">
8   <articleinfo>
9     <title>D-BUS Specification</title>
10     <releaseinfo>Version 0.11</releaseinfo>
11     <date>6 February 2005</date>
12     <authorgroup>
13       <author>
14         <firstname>Havoc</firstname>
15         <surname>Pennington</surname>
16         <affiliation>
17           <orgname>Red Hat, Inc.</orgname>
18           <address>
19             <email>hp@pobox.com</email>
20           </address>
21         </affiliation>
22       </author>
23       <author>
24         <firstname>Anders</firstname>
25         <surname>Carlsson</surname>
26         <affiliation>
27           <orgname>CodeFactory AB</orgname>
28           <address>
29             <email>andersca@codefactory.se</email>
30           </address>
31         </affiliation>
32       </author>
33       <author>
34         <firstname>Alexander</firstname>
35         <surname>Larsson</surname>
36         <affiliation>
37           <orgname>Red Hat, Inc.</orgname>
38           <address>
39             <email>alexl@redhat.com</email>
40           </address>
41         </affiliation>
42       </author>
43     </authorgroup>
44   </articleinfo>
45
46   <sect1 id="introduction">
47     <title>Introduction</title>
48     <para>
49       D-BUS is a system for low-latency, low-overhead, easy to use
50       interprocess communication (IPC). In more detail:
51       <itemizedlist>
52         <listitem>
53           <para>
54             D-BUS is <emphasis>low-latency</emphasis> because it is designed 
55             to avoid round trips and allow asynchronous operation, much like 
56             the X protocol.
57           </para>
58         </listitem>
59         <listitem>
60           <para>
61             D-BUS is <emphasis>low-overhead</emphasis> because it uses a
62             binary protocol, and does not have to convert to and from a text
63             format such as XML. Because D-BUS is intended for potentially
64             high-resolution same-machine IPC, not primarily for Internet IPC,
65             this is an interesting optimization.
66           </para>
67         </listitem>
68         <listitem>
69           <para>
70             D-BUS is <emphasis>easy to use</emphasis> because it works in terms
71             of <firstterm>messages</firstterm> rather than byte streams, and
72             automatically handles a lot of the hard IPC issues. Also, the D-BUS
73             library is designed to be wrapped in a way that lets developers use
74             their framework's existing object/type system, rather than learning
75             a new one specifically for IPC.
76           </para>
77         </listitem>
78       </itemizedlist>
79     </para>
80
81     <para>
82       The base D-BUS protocol is a one-to-one (peer-to-peer or client-server)
83       protocol, specified in <xref linkend="message-protocol"/>. That is, it is
84       a system for one application to talk to a single other
85       application. However, the primary intended application of the protocol is the
86       D-BUS <firstterm>message bus</firstterm>, specified in <xref
87       linkend="message-bus"/>. The message bus is a special application that
88       accepts connections from multiple other applications, and forwards
89       messages among them.
90     </para>
91
92     <para>
93       Uses of D-BUS include notification of system changes (notification of when
94       a camera is plugged in to a computer, or a new version of some software
95       has been installed), or desktop interoperability, for example a file
96       monitoring service or a configuration service.
97     </para>
98
99     <para>
100       D-BUS is designed for two specific use cases:
101       <itemizedlist>
102         <listitem>
103           <para>
104             A "system bus" for notifications from the system to user sessions,
105             and to allow the system to request input from user sessions.
106           </para>
107         </listitem>
108         <listitem>
109           <para>
110             A "session bus" used to implement desktop environments such as 
111             GNOME and KDE.
112           </para>
113         </listitem>
114       </itemizedlist>
115       D-BUS is not intended to be a generic IPC system for any possible 
116       application, and intentionally omits many features found in other 
117       IPC systems for this reason. D-BUS may turn out to be useful 
118       in unanticipated applications, but future versions of this 
119       spec and the reference implementation probably will not 
120       incorporate features that interfere with the core use cases.
121     </para>
122
123     <para>
124       The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
125       "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
126       document are to be interpreted as described in RFC 2119. However, the
127       document could use a serious audit to be sure it makes sense to do
128       so. Also, they are not capitalized.
129     </para>
130     
131   </sect1>
132
133   <sect1 id="message-protocol">
134     <title>Message Protocol</title>
135
136     <para>
137       A <firstterm>message</firstterm> consists of a
138       <firstterm>header</firstterm> and a <firstterm>body</firstterm>. If you
139       think of a message as a package, the header is the address, and the body
140       contains the package contents. The message delivery system uses the header
141       information to figure out where to send the message and how to interpret
142       it; the recipient inteprets the body of the message.
143     </para>
144     
145     <para>
146       The body of the message is made up of zero or more
147       <firstterm>arguments</firstterm>, which are typed values, such as an
148       integer or a byte array.
149     </para>
150
151     <para>
152       Both header and body use the same type system and format for 
153       serializing data. Each type of value has a wire format. 
154       Converting a value from some other representation into the wire
155       format is called <firstterm>marshaling</firstterm> and converting
156       it back from the wire format is <firstterm>unmarshaling</firstterm>.
157     </para>
158
159     <sect2 id="message-protocol-signatures">
160       <title>Type Signatures</title>
161
162       <para>
163         The D-BUS protocol does not include type tags in the marshaled data; a
164         block of marshaled values must have a known <firstterm>type
165         signature</firstterm>.  The type signature is made up of <firstterm>type
166         codes</firstterm>. A type code is an ASCII character representing the
167         type of a value. Because ASCII characters are used, the type signature
168         will always form a valid ASCII string. A simple string compare 
169         determines whether two type signatures are equivalent.
170       </para>
171
172       <para>
173         As a simple example, the type code for 32-bit integer (<literal>INT32</literal>) is
174         the ASCII character 'i'. So the signature for a block of values 
175         containing a single <literal>INT32</literal> would be:
176         <programlisting>
177           "i"
178         </programlisting>
179         A block of values containing two <literal>INT32</literal> would have this signature:
180         <programlisting>
181           "ii"
182         </programlisting>        
183       </para>
184
185       <para>
186         All <firstterm>basic</firstterm> types work like 
187         <literal>INT32</literal> in this example. To marshal and unmarshal 
188         basic types, you simply read one value from the data
189         block corresponding to each type code in the signature.
190         In addition to basic types, there are four <firstterm>container</firstterm> 
191         types: <literal>STRUCT</literal>, <literal>ARRAY</literal>, <literal>VARIANT</literal>, 
192         and <literal>DICT_ENTRY</literal>.
193       </para>
194
195       <para>
196         <literal>STRUCT</literal> has a type code, ASCII character 'r', but this type 
197         code does not appear in signatures. Instead, ASCII characters
198         '(' and ')' are used to mark the beginning and end of the struct.
199         So for example, a struct containing two integers would have this 
200         signature:
201         <programlisting>
202           "(ii)"
203         </programlisting>
204         Structs can be nested, so for example a struct containing 
205         an integer and another struct:
206         <programlisting>
207           "(i(ii))"
208         </programlisting>
209         The value block storing that struct would contain three integers; the
210         type signature allows you to distinguish "(i(ii))" from "((ii)i)" or
211         "(iii)" or "iii".
212       </para>
213
214       <para>
215         The <literal>STRUCT</literal> type code 'r' is not currently used in the D-BUS protocol,
216         but is useful in code that implements the protocol. This type code 
217         is specified to allow such code to interoperate in non-protocol contexts.
218       </para>
219       
220       <para>
221         <literal>ARRAY</literal> has ASCII character 'a' as type code. The array type code must be
222         followed by a <firstterm>single complete type</firstterm>. The single
223         complete type following the array is the type of each array element. So
224         the simple example is:
225         <programlisting>
226           "ai"
227         </programlisting>
228         which is an array of 32-bit integers. But an array can be of any type, 
229         such as this array-of-struct-with-two-int32-fields:
230         <programlisting>
231           "a(ii)"
232         </programlisting>
233         Or this array of array of integer:
234         <programlisting>
235           "aai"
236         </programlisting>
237       </para>
238
239       <para>
240         The phrase <firstterm>single complete type</firstterm> deserves some 
241         definition. A single complete type is a basic type code, a variant type code, 
242         an array with its element type, or a struct with its fields. 
243         So the following signatures are not single complete types:
244         <programlisting>
245           "aa"
246         </programlisting>
247         <programlisting>
248           "(ii"
249         </programlisting>
250         <programlisting>
251           "ii)"
252         </programlisting>
253         And the following signatures contain multiple complete types:
254         <programlisting>
255           "ii"
256         </programlisting>
257         <programlisting>
258           "aiai"
259         </programlisting>
260         <programlisting>
261           "(ii)(ii)"
262         </programlisting>
263         Note however that a single complete type may <emphasis>contain</emphasis>
264         multiple other single complete types.
265       </para>
266
267       <para>
268         <literal>VARIANT</literal> has ASCII character 'v' as its type code. A marshaled value of
269         type <literal>VARIANT</literal> will have the signature of a single complete type as part
270         of the <emphasis>value</emphasis>.  This signature will be followed by a
271         marshaled value of that type.
272       </para>
273
274       <para>
275         A <literal>DICT_ENTRY</literal> works exactly like a struct, but rather
276         than parentheses it uses curly braces, and it has more restrictions.
277         The restrictions are: it occurs only as an array element type; it has
278         exactly two single complete types inside the curly braces; the first
279         single complete type (the "key") must be a basic type rather than a
280         container type. Implementations must not accept dict entries outside of
281         arrays, must not accept dict entries with zero, one, or more than two
282         fields, and must not accept dict entries with non-basic-typed keys. A
283         dict entry is always a key-value pair.
284       </para>
285       
286       <para>
287         The first field in the <literal>DICT_ENTRY</literal> is always the key.
288         A message is considered corrupt if the same key occurs twice in the same
289         array of <literal>DICT_ENTRY</literal>. However, for performance reasons
290         implementations are not required to reject dicts with duplicate keys.
291       </para>
292
293       <para>
294         In most languages, an array of dict entry would be represented as a 
295         map, hash table, or dict object.
296       </para>
297
298       <para>
299         The following table summarizes the D-BUS types.
300         <informaltable>
301           <tgroup cols="3">
302             <thead>
303               <row>
304                 <entry>Conventional Name</entry>
305                 <entry>Code</entry>
306                 <entry>Description</entry>
307               </row>
308             </thead>
309             <tbody>
310               <row>
311                 <entry><literal>INVALID</literal></entry>
312                 <entry>0 (ASCII NUL)</entry>
313                 <entry>Not a valid type code, used to terminate signatures</entry>
314               </row><row>
315                 <entry><literal>BYTE</literal></entry>
316                 <entry>121 (ASCII 'y')</entry>
317                 <entry>8-bit unsigned integer</entry>
318               </row><row>
319                 <entry><literal>BOOLEAN</literal></entry>
320                 <entry>98 (ASCII 'b')</entry>
321                 <entry>Boolean value, 0 is <literal>FALSE</literal> and 1 is <literal>TRUE</literal>. Everything else is invalid.</entry>
322               </row><row>
323                 <entry><literal>INT16</literal></entry>
324                 <entry>110 (ASCII 'n')</entry>
325                 <entry>16-bit signed integer</entry>
326               </row><row>
327                 <entry><literal>UINT16</literal></entry>
328                 <entry>113 (ASCII 'q')</entry>
329                 <entry>16-bit unsigned integer</entry>
330               </row><row>
331                 <entry><literal>INT32</literal></entry>
332                 <entry>105 (ASCII 'i')</entry>
333                 <entry>32-bit signed integer</entry>
334               </row><row>
335                 <entry><literal>UINT32</literal></entry>
336                 <entry>117 (ASCII 'u')</entry>
337                 <entry>32-bit unsigned integer</entry>
338               </row><row>
339                 <entry><literal>INT64</literal></entry>
340                 <entry>120 (ASCII 'x')</entry>
341                 <entry>64-bit signed integer</entry>
342               </row><row>
343                 <entry><literal>UINT64</literal></entry>
344                 <entry>116 (ASCII 't')</entry>
345                 <entry>64-bit unsigned integer</entry>
346               </row><row>
347                 <entry><literal>DOUBLE</literal></entry>
348                 <entry>100 (ASCII 'd')</entry>
349                 <entry>IEEE 754 double</entry>
350               </row><row>
351                 <entry><literal>STRING</literal></entry>
352                 <entry>115 (ASCII 's')</entry>
353                 <entry>UTF-8 string (<emphasis>must</emphasis> be valid UTF-8). Must be nul terminated.</entry>
354               </row><row>
355                 <entry><literal>OBJECT_PATH</literal></entry>
356                 <entry>111 (ASCII 'o')</entry>
357                 <entry>Name of an object instance</entry>
358               </row><row>
359                 <entry><literal>SIGNATURE</literal></entry>
360                 <entry>103 (ASCII 'g')</entry>
361                 <entry>A type signature</entry>
362               </row><row>
363                 <entry><literal>ARRAY</literal></entry>
364                 <entry>97 (ASCII 'a')</entry>
365                 <entry>Array</entry>
366               </row><row>
367                 <entry><literal>STRUCT</literal></entry>
368                 <entry>114 (ASCII 'r'), 40 (ASCII '('), 41 (ASCII ')')</entry>
369                 <entry>Struct</entry>
370               </row><row>
371                 <entry><literal>VARIANT</literal></entry>
372                 <entry>118 (ASCII 'v') </entry>
373                 <entry>Variant type (the type of the value is part of the value itself)</entry>
374               </row><row>
375                 <entry><literal>DICT_ENTRY</literal></entry>
376                 <entry>101 (ASCII 'e'), 123 (ASCII '{'), 125 (ASCII '}') </entry>
377                 <entry>Entry in a dict or map (array of key-value pairs)</entry>
378               </row>
379             </tbody>
380           </tgroup>
381         </informaltable>
382       </para>
383
384     </sect2>
385
386     <sect2 id="message-protocol-marshaling">
387       <title>Marshaling (Wire Format)</title>
388
389       <para>
390         Given a type signature, a block of bytes can be converted into typed
391         values. This section describes the format of the block of bytes.  Byte
392         order and alignment issues are handled uniformly for all D-BUS types.
393       </para>
394
395       <para>
396         A block of bytes has an associated byte order. The byte order 
397         has to be discovered in some way; for D-BUS messages, the 
398         byte order is part of the message header as described in 
399         <xref linkend="message-protocol-messages"/>. For now, assume 
400         that the byte order is known to be either little endian or big 
401           endian.
402       </para>
403
404       <para>
405         Each value in a block of bytes is aligned "naturally," for example
406         4-byte values are aligned to a 4-byte boundary, and 8-byte values to an
407         8-byte boundary. To properly align a value, <firstterm>alignment
408         padding</firstterm> may be necessary. The alignment padding must always
409         be the minimum required padding to properly align the following value;
410         and it must always be made up of nul bytes. The alignment padding must
411         not be left uninitialized (it can't contain garbage), and more padding
412         than required must not be used.
413       </para>
414
415       <para>
416         Given all this, the types are marshaled on the wire as follows:
417         <informaltable>
418           <tgroup cols="3">
419             <thead>
420               <row>
421                 <entry>Conventional Name</entry>
422                 <entry>Encoding</entry>
423                 <entry>Alignment</entry>
424               </row>
425             </thead>
426             <tbody>
427               <row>
428                 <entry><literal>INVALID</literal></entry>
429                 <entry>Not applicable; cannot be marshaled.</entry>
430                 <entry>N/A</entry>
431               </row><row>
432                 <entry><literal>BYTE</literal></entry>
433                 <entry>A single 8-bit byte.</entry>
434                 <entry>1</entry>
435               </row><row>
436                 <entry><literal>BOOLEAN</literal></entry>
437                 <entry>As for <literal>UINT32</literal>, but only 0 and 1 are valid values.</entry>
438                 <entry>4</entry>
439               </row><row>
440                 <entry><literal>INT16</literal></entry>
441                 <entry>16-bit signed integer in the message's byte order.</entry>
442                 <entry>2</entry>
443               </row><row>
444                 <entry><literal>UINT16</literal></entry>
445                 <entry>16-bit unsigned integer in the message's byte order.</entry>
446                 <entry>2</entry>
447               </row><row>
448                 <entry><literal>INT32</literal></entry>
449                 <entry>32-bit signed integer in the message's byte order.</entry>
450                 <entry>4</entry>
451               </row><row>
452                 <entry><literal>UINT32</literal></entry>
453                 <entry>32-bit unsigned integer in the message's byte order.</entry>
454                 <entry>4</entry>
455               </row><row>
456                 <entry><literal>INT64</literal></entry>
457                 <entry>64-bit signed integer in the message's byte order.</entry>
458                 <entry>8</entry>
459               </row><row>
460                 <entry><literal>UINT64</literal></entry>
461                 <entry>64-bit unsigned integer in the message's byte order.</entry>
462                 <entry>8</entry>
463               </row><row>
464                 <entry><literal>DOUBLE</literal></entry>
465                 <entry>64-bit IEEE 754 double in the message's byte order.</entry>
466                 <entry>8</entry>
467               </row><row>
468                 <entry><literal>STRING</literal></entry>
469                 <entry>A <literal>UINT32</literal> indicating the string's 
470                   length in bytes excluding its terminating nul, followed by 
471                   string data of the given length, followed by a terminating nul 
472                   byte.
473                 </entry>
474                 <entry>
475                   4 (for the length)
476                 </entry>
477               </row><row>
478                 <entry><literal>OBJECT_PATH</literal></entry>
479                 <entry>Exactly the same as <literal>STRING</literal> except the 
480                   content must be a valid object path (see below).
481                 </entry>
482                 <entry>
483                   4 (for the length)
484                 </entry>
485               </row><row>
486                 <entry><literal>SIGNATURE</literal></entry>
487                 <entry>The same as <literal>STRING</literal> except the length is a single 
488                   byte (thus signatures have a maximum length of 255)
489                   and the content must be a valid signature (see below).
490                 </entry>
491                 <entry>
492                   1
493                 </entry>
494               </row><row>
495                 <entry><literal>ARRAY</literal></entry>
496                 <entry>
497                   A <literal>UINT32</literal> giving the length of the array data in bytes, followed by 
498                   alignment padding to the alignment boundary of the array element type, 
499                   followed by each array element. The array length is from the 
500                   end of the alignment padding to the end of the last element,
501                   i.e. it does not include the padding after the length,
502                   or any padding after the last element.
503                   Arrays have a maximum length defined to be 2 to the 26th power or
504                   67108864. Implementations must not send or accept arrays exceeding this
505                   length.
506                 </entry>
507                 <entry>
508                   4 (for the length)
509                 </entry>
510               </row><row>
511                 <entry><literal>STRUCT</literal></entry>
512                 <entry>
513                   A struct must start on an 8-byte boundary regardless of the
514                   type of the struct fields. The struct value consists of each
515                   field marshaled in sequence starting from that 8-byte
516                   alignment boundary.
517                 </entry>
518                 <entry>
519                   8
520                 </entry>
521               </row><row>
522                 <entry><literal>VARIANT</literal></entry>
523                 <entry>
524                   A variant type has a marshaled <literal>SIGNATURE</literal>
525                   followed by a marshaled value with the type
526                   given in the signature.
527                   Unlike a message signature, the variant signature 
528                   can contain only a single complete type.
529                   So "i" is OK, "ii" is not.
530                 </entry>
531                 <entry>
532                   1 (alignment of the signature)
533                 </entry>
534               </row><row>
535                 <entry><literal>DICT_ENTRY</literal></entry>
536                 <entry>
537                   Identical to STRUCT.
538                 </entry>
539                 <entry>
540                   8
541                 </entry>
542               </row>
543             </tbody>
544           </tgroup>
545         </informaltable>
546       </para>
547       
548       <sect3 id="message-protocol-marshaling-object-path">
549         <title>Valid Object Paths</title>
550         
551         <para>
552           An object path is a name used to refer to an object instance.
553           Conceptually, each participant in a D-BUS message exchange may have
554           any number of object instances (think of C++ or Java objects) and each
555           such instance will have a path. Like a filesystem, the object
556           instances in an application form a hierarchical tree.
557         </para>
558         
559         <para>
560           The following rules define a valid object path. Implementations must 
561           not send or accept messages with invalid object paths.
562           <itemizedlist>
563             <listitem>
564               <para>
565                 The path may be of any length.
566               </para>
567             </listitem>
568             <listitem>
569               <para>
570                 The path must begin with an ASCII '/' (integer 47) character, 
571                 and must consist of elements separated by slash characters.
572               </para>
573             </listitem>
574             <listitem>
575               <para>
576                 Each element must only contain the ASCII characters 
577                 "[A-Z][a-z][0-9]_"
578               </para>
579             </listitem>
580             <listitem>
581               <para>
582                 No element may be the empty string.
583               </para>
584             </listitem>
585             <listitem>
586               <para>
587                 Multiple '/' characters cannot occur in sequence.
588               </para>
589             </listitem>
590             <listitem>
591               <para>
592                 A trailing '/' character is not allowed unless the 
593                 path is the root path (a single '/' character).
594               </para>
595             </listitem>
596           </itemizedlist>
597         </para>
598
599       </sect3>
600
601       
602       <sect3 id="message-protocol-marshaling-signature">
603         <title>Valid Signatures</title>
604         <para>
605           An implementation must not send or accept invalid signatures.
606           Valid signatures will conform to the following rules:
607           <itemizedlist>
608             <listitem>
609               <para>
610                 The signature ends with a nul byte.
611               </para>
612             </listitem>
613             <listitem>
614               <para>
615                 The signature is a list of single complete types. 
616                 Arrays must have element types, and structs must 
617                 have both open and close parentheses.
618               </para>
619             </listitem>
620             <listitem>
621               <para>
622                 Only type codes and open and close parentheses are 
623                 allowed in the signature. The <literal>STRUCT</literal> type code
624                 is not allowed in signatures, because parentheses
625                 are used instead.
626               </para>
627             </listitem>
628             <listitem>
629               <para>
630                 The maximum depth of container type nesting is 32 array type
631                 codes and 32 open parentheses. This implies that the maximum
632                 total depth of recursion is 64, for an "array of array of array
633                 of ... struct of struct of struct of ..."  where there are 32
634                 array and 32 struct.
635               </para>
636             </listitem>
637             <listitem>
638               <para>
639                 The maximum length of a signature is 255.
640               </para>
641             </listitem>
642             <listitem>
643               <para>
644                 Signatures must be nul-terminated.
645               </para>
646             </listitem>
647           </itemizedlist>
648         </para>
649       </sect3>
650       
651     </sect2>
652
653     <sect2 id="message-protocol-messages">
654       <title>Message Format</title>
655
656       <para>
657         A message consists of a header and a body. The header is a block of
658         values with a fixed signature and meaning.  The body is a separate block
659         of values, with a signature specified in the header.
660       </para>
661
662       <para>
663         The length of the header must be a multiple of 8, allowing the body to
664         begin on an 8-byte boundary when storing the entire message in a single
665         buffer. If the header does not naturally end on an 8-byte boundary 
666         up to 7 bytes of nul-initialized alignment padding must be added.
667       </para>
668
669       <para>
670         The message body need not end on an 8-byte boundary.
671       </para>
672
673       <para>
674         The maximum length of a message, including header, header alignment padding, 
675         and body is 2 to the 27th power or 134217728. Implementations must not 
676         send or accept messages exceeding this size.
677       </para>
678       
679       <para>
680         The signature of the header is:
681         <programlisting>
682           "yyyyuua(yv)"
683         </programlisting>
684         Written out more readably, this is:
685         <programlisting>
686           BYTE, BYTE, BYTE, BYTE, UINT32, UINT32, ARRAY of STRUCT of (BYTE,VARIANT)
687         </programlisting>
688       </para>
689
690       <para>
691         These values have the following meanings:
692         <informaltable>
693           <tgroup cols="2">
694             <thead>
695               <row>
696                 <entry>Value</entry>
697                 <entry>Description</entry>
698               </row>
699             </thead>
700             <tbody>
701               <row>
702                 <entry>1st <literal>BYTE</literal></entry>
703                 <entry>Endianness flag; ASCII 'l' for little-endian 
704                   or ASCII 'B' for big-endian. Both header and body are 
705                 in this endianness.</entry>
706               </row>
707               <row>
708                 <entry>2nd <literal>BYTE</literal></entry>
709                 <entry><firstterm>Message type</firstterm>. Unknown types must be ignored. 
710                   Currently-defined types are described below.
711                 </entry>
712               </row>
713               <row>
714                 <entry>3rd <literal>BYTE</literal></entry>
715                 <entry>Bitwise OR of flags. Unknown flags
716                   must be ignored. Currently-defined flags are described below.
717                 </entry>
718               </row>
719               <row>
720                 <entry>4th <literal>BYTE</literal></entry>
721                 <entry>Major protocol version of the sending application.  If
722                 the major protocol version of the receiving application does not
723                 match, the applications will not be able to communicate and the
724                 D-BUS connection must be disconnected. The major protocol
725                 version for this version of the specification is 0.
726                   FIXME this field is stupid and pointless to put in 
727                   every message.
728                 </entry>
729               </row>
730               <row>
731                 <entry>1st <literal>UINT32</literal></entry>
732                 <entry>Length in bytes of the message body, starting 
733                   from the end of the header. The header ends after 
734                   its alignment padding to an 8-boundary.
735                 </entry>
736               </row>
737               <row>
738                 <entry>2nd <literal>UINT32</literal></entry>
739                 <entry>The serial of this message, used as a cookie 
740                   by the sender to identify the reply corresponding
741                   to this request.
742                 </entry>
743               </row>      
744               <row>
745                 <entry><literal>ARRAY</literal> of <literal>STRUCT</literal> of (<literal>BYTE</literal>,<literal>VARIANT</literal>)</entry>
746                 <entry>An array of zero or more <firstterm>header
747                   fields</firstterm> where the byte is the field code, and the
748                   variant is the field value. The message type determines 
749                   which fields are required.
750                 </entry>
751               </row>
752             </tbody>
753           </tgroup>
754         </informaltable>
755       </para>
756       <para>
757         <firstterm>Message types</firstterm> that can appear in the second byte
758         of the header are:
759         <informaltable>
760           <tgroup cols="3">
761             <thead>
762               <row>
763                 <entry>Conventional name</entry>
764                 <entry>Decimal value</entry>
765                 <entry>Description</entry>
766               </row>
767             </thead>
768             <tbody>
769               <row>
770                 <entry><literal>INVALID</literal></entry>
771                 <entry>0</entry>
772                 <entry>This is an invalid type.</entry>
773               </row>
774               <row>
775                 <entry><literal>METHOD_CALL</literal></entry>
776                 <entry>1</entry>
777                 <entry>Method call.</entry>
778               </row>
779               <row>
780                 <entry><literal>METHOD_RETURN</literal></entry>
781                 <entry>2</entry>
782                 <entry>Method reply with returned data.</entry>
783               </row>
784               <row>
785                 <entry><literal>ERROR</literal></entry>
786                 <entry>3</entry>
787                 <entry>Error reply. If the first argument exists and is a
788                 string, it is an error message.</entry>
789               </row>
790               <row>
791                 <entry><literal>SIGNAL</literal></entry>
792                 <entry>4</entry>
793                 <entry>Signal emission.</entry>
794               </row>
795             </tbody>
796           </tgroup>
797         </informaltable>
798       </para>
799       <para>
800         Flags that can appear in the third byte of the header:
801         <informaltable>
802           <tgroup cols="3">
803             <thead>
804               <row>
805                 <entry>Conventional name</entry>
806                 <entry>Hex value</entry>
807                 <entry>Description</entry>
808               </row>
809             </thead>
810             <tbody>
811               <row>
812                 <entry><literal>NO_REPLY_EXPECTED</literal></entry>
813                 <entry>0x1</entry>
814                 <entry>This message does not expect method return replies or
815                 error replies; the reply can be omitted as an
816                 optimization. However, it is compliant with this specification
817                 to return the reply despite this flag and the only harm 
818                   from doing so is extra network traffic.
819                 </entry>
820               </row>
821               <row>
822                 <entry><literal>NO_AUTO_START</literal></entry>
823                 <entry>0x2</entry>
824                 <entry>The bus must not launch an owner
825                   for the destination name in response to this message.
826                 </entry>
827               </row>
828             </tbody>
829           </tgroup>
830         </informaltable>
831       </para>
832
833       <sect3 id="message-protocol-header-fields">
834         <title>Header Fields</title>
835
836         <para>
837           The array at the end of the header contains <firstterm>header
838           fields</firstterm>, where each field is a 1-byte field code followed
839           by a field value. A header must contain the required header fields for
840           its message type, and zero or more of any optional header
841           fields. Future versions of this protocol specification may add new
842           fields. Implementations must ignore fields they do not
843           understand. Implementations must not invent their own header fields;
844           only changes to this specification may introduce new header fields.
845         </para>
846
847         <para>
848           Again, if an implementation sees a header field code that it does not
849           expect, it must ignore that field, as it will be part of a new
850           (but compatible) version of this specification. This also applies 
851           to known header fields appearing in unexpected messages, for 
852           example: if a signal has a reply serial it must be ignored
853           even though it has no meaning as of this version of the spec.
854         </para>
855
856         <para>
857           However, implementations must not send or accept known header fields
858           with the wrong type stored in the field value. So for example a
859           message with an <literal>INTERFACE</literal> field of type
860           <literal>UINT32</literal> would be considered corrupt.
861         </para>
862
863         <para>
864           Here are the currently-defined header fields:
865           <informaltable>
866             <tgroup cols="5">
867               <thead>
868                 <row>
869                   <entry>Conventional Name</entry>
870                   <entry>Decimal Code</entry>
871                   <entry>Type</entry>
872                   <entry>Required In</entry>
873                   <entry>Description</entry>
874                 </row>
875               </thead>
876               <tbody>
877                 <row>
878                   <entry><literal>INVALID</literal></entry>
879                   <entry>0</entry>
880                   <entry>N/A</entry>
881                   <entry>not allowed</entry>
882                   <entry>Not a valid field name (error if it appears in a message)</entry>
883                 </row>
884                 <row>
885                   <entry><literal>PATH</literal></entry>
886                   <entry>1</entry>
887                   <entry><literal>OBJECT_PATH</literal></entry>
888                   <entry><literal>METHOD_CALL</literal>, <literal>SIGNAL</literal></entry>
889                   <entry>The object to send a call to, 
890                     or the object a signal is emitted from.
891                   </entry>
892                 </row>
893                 <row>
894                   <entry><literal>INTERFACE</literal></entry>
895                   <entry>2</entry>
896                   <entry><literal>STRING</literal></entry>
897                   <entry><literal>SIGNAL</literal></entry>
898                   <entry>
899                     The interface to invoke a method call on, or 
900                     that a signal is emitted from. Optional for 
901                     method calls, required for signals.
902                   </entry>
903                 </row>
904                 <row>
905                   <entry><literal>MEMBER</literal></entry>
906                   <entry>3</entry>
907                   <entry><literal>STRING</literal></entry>
908                   <entry><literal>METHOD_CALL</literal>, <literal>SIGNAL</literal></entry>
909                   <entry>The member, either the method name or signal name.</entry>
910                 </row>
911                 <row>
912                   <entry><literal>ERROR_NAME</literal></entry>
913                   <entry>4</entry>
914                   <entry><literal>STRING</literal></entry>
915                   <entry><literal>ERROR</literal></entry>
916                   <entry>The name of the error that occurred, for errors</entry>
917                 </row>
918                 <row>
919                   <entry><literal>REPLY_SERIAL</literal></entry>
920                   <entry>5</entry>
921                   <entry><literal>UINT32</literal></entry>
922                   <entry><literal>ERROR</literal>, <literal>METHOD_RETURN</literal></entry>
923                   <entry>The serial number of the message this message is a reply
924                     to. (The serial number is the second <literal>UINT32</literal> in the header.)</entry>
925                 </row>
926                 <row>
927                   <entry><literal>DESTINATION</literal></entry>
928                   <entry>6</entry>
929                   <entry><literal>STRING</literal></entry>
930                   <entry>optional</entry>
931                   <entry>The name of the connection this message is intended for.
932                     Only used in combination with the message bus, see 
933                     <xref linkend="message-bus"/>.</entry>
934                 </row>
935                 <row>
936                   <entry><literal>SENDER</literal></entry>
937                   <entry>7</entry>
938                   <entry><literal>STRING</literal></entry>
939                   <entry>optional</entry>
940                   <entry>Unique name of the sending connection.
941                     The message bus fills in this field so it is reliable; the field is
942                     only meaningful in combination with the message bus.</entry>
943                 </row>
944                 <row>
945                   <entry><literal>SIGNATURE</literal></entry>
946                   <entry>8</entry>
947                   <entry><literal>SIGNATURE</literal></entry>
948                   <entry>optional</entry>
949                   <entry>The signature of the message body.
950                   If omitted, it is assumed to be the 
951                   empty signature "" (i.e. the body must be 0-length).</entry>
952                 </row>
953               </tbody>
954             </tgroup>
955           </informaltable>
956         </para>
957       </sect3>
958     </sect2>
959
960     <sect2 id="message-protocol-names">
961       <title>Valid Names</title>
962       <para>
963         The various names in D-BUS messages have some restrictions.
964       </para>
965       <para>
966         There is a <firstterm>maximum name length</firstterm> 
967         of 255 which applies to bus names, interfaces, and members. 
968       </para>
969       <sect3 id="message-protocol-names-interface">
970         <title>Interface names</title>
971         <para>
972           Interfaces have names with type <literal>STRING</literal>, meaning that 
973           they must be valid UTF-8. However, there are also some 
974           additional restrictions that apply to interface names 
975           specifically:
976           <itemizedlist>
977             <listitem><para>They are composed of 1 or more elements separated by
978                 a period ('.') character. All elements must contain at least 
979                 one character.
980                 </para>
981             </listitem>
982             <listitem><para>Each element must only contain the ASCII characters 
983                 "[A-Z][a-z][0-9]_" and must not begin with a digit.
984                 </para>
985             </listitem>
986
987             <listitem><para>They must contain at least one '.' (period)
988               character (and thus at least two elements).
989               </para></listitem>
990
991             <listitem><para>They must not begin with a '.' (period) character.</para></listitem>
992             <listitem><para>They must not exceed the maximum name length.</para></listitem>
993           </itemizedlist>
994         </para>
995       </sect3>
996       <sect3 id="message-protocol-names-bus">
997         <title>Bus names</title>
998         <para>
999           Bus names have the same restrictions as interface names, with a
1000           special exception for unique connection names. A unique name's first
1001           element must start with a colon (':') character. After the colon, any
1002           characters in "[A-Z][a-z][0-9]_" may appear. Elements after
1003           the first must follow the usual rules, except that they may start with
1004           a digit. Bus names not starting with a colon have none of these 
1005           exceptions and follow the same rules as interface names.
1006         </para>
1007       </sect3>
1008       <sect3 id="message-protocol-names-member">
1009         <title>Member names</title>
1010         <para>
1011           Member (i.e. method or signal) names:
1012           <itemizedlist>
1013             <listitem><para>Must only contain the ASCII characters
1014                 "[A-Z][a-z][0-9]_" and may not begin with a
1015                 digit.</para></listitem>
1016             <listitem><para>Must not contain the '.' (period) character.</para></listitem>
1017             <listitem><para>Must not exceed the maximum name length.</para></listitem>
1018             <listitem><para>Must be at least 1 byte in length.</para></listitem>
1019           </itemizedlist>
1020         </para>
1021       </sect3>
1022       <sect3 id="message-protocol-names-error">
1023         <title>Error names</title>
1024         <para>
1025           Error names have the same restrictions as interface names.
1026         </para>
1027       </sect3>
1028     </sect2>
1029
1030     <sect2 id="message-protocol-types">
1031       <title>Message Types</title>
1032       <para>
1033         Each of the message types (<literal>METHOD_CALL</literal>, <literal>METHOD_RETURN</literal>, <literal>ERROR</literal>, and
1034         <literal>SIGNAL</literal>) has its own expected usage conventions and header fields.
1035         This section describes these conventions.
1036       </para>
1037       <sect3 id="message-protocol-types-method">
1038         <title>Method Calls</title>
1039         <para>
1040           Some messages invoke an operation on a remote object.  These are
1041           called method call messages and have the type tag <literal>METHOD_CALL</literal>. Such
1042           messages map naturally to methods on objects in a typical program.
1043         </para>
1044         <para>
1045           A method call message is required to have a <literal>MEMBER</literal> header field
1046           indicating the name of the method. Optionally, the message has an
1047           <literal>INTERFACE</literal> field giving the interface the method is a part of. In the
1048           absence of an <literal>INTERFACE</literal> field, if two interfaces on the same object have
1049           a method with the same name, it is undefined which of the two methods
1050           will be invoked. Implementations may also choose to return an error in
1051           this ambiguous case. However, if a method name is unique
1052           implementations must not require an interface field.
1053         </para>
1054         <para>
1055           Method call messages also include a <literal>PATH</literal> field
1056           indicating the object to invoke the method on. If the call is passing
1057           through a message bus, the message will also have a
1058           <literal>DESTINATION</literal> field giving the name of the connection
1059           to receive the message.
1060         </para>
1061         <para>
1062           When an application handles a method call message, it is required to
1063           return a reply. The reply is identified by a <literal>REPLY_SERIAL</literal> header field
1064           indicating the serial number of the <literal>METHOD_CALL</literal> being replied to. The
1065           reply can have one of two types; either <literal>METHOD_RETURN</literal> or <literal>ERROR</literal>.
1066         </para>
1067         <para>
1068           If the reply has type <literal>METHOD_RETURN</literal>, the arguments to the reply message 
1069           are the return value(s) or "out parameters" of the method call. 
1070           If the reply has type <literal>ERROR</literal>, then an "exception" has been thrown, 
1071           and the call fails; no return value will be provided. It makes 
1072           no sense to send multiple replies to the same method call.
1073         </para>
1074         <para>
1075           Even if a method call has no return values, a <literal>METHOD_RETURN</literal> 
1076           reply is required, so the caller will know the method 
1077           was successfully processed.
1078         </para>
1079         <para>
1080           The <literal>METHOD_RETURN</literal> or <literal>ERROR</literal> reply message must have the <literal>REPLY_SERIAL</literal> 
1081           header field.
1082         </para>
1083         <para>
1084           If a <literal>METHOD_CALL</literal> message has the flag <literal>NO_REPLY_EXPECTED</literal>, 
1085           then as an optimization the application receiving the method 
1086           call may choose to omit the reply message (regardless of 
1087           whether the reply would have been <literal>METHOD_RETURN</literal> or <literal>ERROR</literal>). 
1088           However, it is also acceptable to ignore the <literal>NO_REPLY_EXPECTED</literal>
1089           flag and reply anyway.
1090         </para>
1091         <para>
1092           Unless a message has the flag <literal>NO_AUTO_START</literal>, if the
1093           destination name does not exist then a program to own the destination
1094           name will be started before the message is delivered.  The message
1095           will be held until the new program is successfully started or has
1096           failed to start; in case of failure, an error will be returned. This
1097           flag is only relevant in the context of a message bus, it is ignored
1098           during one-to-one communication with no intermediate bus.
1099         </para>
1100         <sect4 id="message-protocol-types-method-apis">
1101           <title>Mapping method calls to native APIs</title>
1102           <para>
1103             APIs for D-BUS may map method calls to a method call in a specific
1104             programming language, such as C++, or may map a method call written
1105             in an IDL to a D-BUS message.
1106           </para>
1107           <para>
1108             In APIs of this nature, arguments to a method are often termed "in"
1109             (which implies sent in the <literal>METHOD_CALL</literal>), or "out" (which implies
1110             returned in the <literal>METHOD_RETURN</literal>). Some APIs such as CORBA also have
1111             "inout" arguments, which are both sent and received, i.e. the caller
1112             passes in a value which is modified. Mapped to D-BUS, an "inout"
1113             argument is equivalent to an "in" argument, followed by an "out"
1114             argument. You can't pass things "by reference" over the wire, so
1115             "inout" is purely an illusion of the in-process API.
1116           </para>
1117           <para>
1118             Given a method with zero or one return values, followed by zero or more
1119             arguments, where each argument may be "in", "out", or "inout", the
1120             caller constructs a message by appending each "in" or "inout" argument,
1121             in order. "out" arguments are not represented in the caller's message.
1122           </para>
1123           <para>
1124             The recipient constructs a reply by appending first the return value 
1125             if any, then each "out" or "inout" argument, in order. 
1126             "in" arguments are not represented in the reply message.
1127           </para>
1128           <para>
1129             Error replies are normally mapped to exceptions in languages that have
1130             exceptions.
1131           </para>
1132           <para>
1133             In converting from native APIs to D-BUS, it is perhaps nice to 
1134             map D-BUS naming conventions ("FooBar") to native conventions 
1135             such as "fooBar" or "foo_bar" automatically. This is OK 
1136             as long as you can say that the native API is one that 
1137             was specifically written for D-BUS. It makes the most sense
1138             when writing object implementations that will be exported 
1139             over the bus. Object proxies used to invoke remote D-BUS 
1140             objects probably need the ability to call any D-BUS method,
1141             and thus a magic name mapping like this could be a problem.
1142           </para>
1143           <para>
1144             This specification doesn't require anything of native API bindings;
1145             the preceding is only a suggested convention for consistency 
1146             among bindings.
1147           </para>
1148         </sect4>
1149       </sect3>
1150
1151       <sect3 id="message-protocol-types-signal">
1152         <title>Signal Emission</title>
1153         <para>
1154           Unlike method calls, signal emissions have no replies. 
1155           A signal emission is simply a single message of type <literal>SIGNAL</literal>.
1156           It must have three header fields: <literal>PATH</literal> giving the object 
1157           the signal was emitted from, plus <literal>INTERFACE</literal> and <literal>MEMBER</literal> giving
1158           the fully-qualified name of the signal.
1159         </para>
1160       </sect3>
1161
1162       <sect3 id="message-protocol-types-errors">
1163         <title>Errors</title>
1164         <para>
1165           Messages of type <literal>ERROR</literal> are most commonly replies 
1166           to a <literal>METHOD_CALL</literal>, but may be returned in reply 
1167           to any kind of message. The message bus for example
1168           will return an <literal>ERROR</literal> in reply to a signal emission if 
1169           the bus does not have enough memory to send the signal.
1170         </para>
1171         <para>
1172           An <literal>ERROR</literal> may have any arguments, but if the first 
1173           argument is a <literal>STRING</literal>, it must be an error message.
1174           The error message may be logged or shown to the user
1175           in some way.
1176         </para>
1177       </sect3>
1178
1179       <sect3 id="message-protocol-types-notation">
1180         <title>Notation in this document</title>
1181         <para>
1182           This document uses a simple pseudo-IDL to describe particular method 
1183           calls and signals. Here is an example of a method call:
1184           <programlisting>
1185             org.freedesktop.DBus.StartServiceByName (in STRING name, in UINT32 flags,
1186                                                      out UINT32 resultcode)
1187           </programlisting>
1188           This means <literal>INTERFACE</literal> = org.freedesktop.DBus, <literal>MEMBER</literal> = StartServiceByName, 
1189           <literal>METHOD_CALL</literal> arguments are <literal>STRING</literal> and <literal>UINT32</literal>, <literal>METHOD_RETURN</literal> argument
1190           is <literal>UINT32</literal>. Remember that the <literal>MEMBER</literal> field can't contain any '.' (period)
1191           characters so it's known that the last part of the name in
1192           the "IDL" is the member name.
1193         </para>
1194         <para>
1195           In C++ that might end up looking like this:
1196           <programlisting>
1197             unsigned int org::freedesktop::DBus::StartServiceByName (const char  *name,
1198                                                                      unsigned int flags);
1199           </programlisting>
1200           or equally valid, the return value could be done as an argument:
1201           <programlisting>
1202             void org::freedesktop::DBus::StartServiceByName (const char   *name, 
1203                                                              unsigned int  flags,
1204                                                              unsigned int *resultcode);
1205           </programlisting>
1206           It's really up to the API designer how they want to make 
1207           this look. You could design an API where the namespace wasn't used 
1208           in C++, using STL or Qt, using varargs, or whatever you wanted.
1209         </para>
1210         <para>
1211           Signals are written as follows:
1212           <programlisting>
1213             org.freedesktop.DBus.NameLost (STRING name)
1214           </programlisting>
1215           Signals don't specify "in" vs. "out" because only 
1216           a single direction is possible.
1217         </para>
1218         <para>
1219           It isn't especially encouraged to use this lame pseudo-IDL in actual
1220           API implementations; you might use the native notation for the
1221           language you're using, or you might use COM or CORBA IDL, for example.
1222         </para>
1223       </sect3>
1224     </sect2>
1225
1226     <sect2 id="message-protocol-handling-invalid">
1227       <title>Invalid Protocol and Spec Extensions</title>
1228       
1229       <para>
1230         For security reasons, the D-BUS protocol should be strictly parsed and
1231         validated, with the exception of defined extension points. Any invalid
1232         protocol or spec violations should result in immediately dropping the
1233         connection without notice to the other end. Exceptions should be
1234         carefully considered, e.g. an exception may be warranted for a
1235         well-understood idiosyncracy of a widely-deployed implementation.  In
1236         cases where the other end of a connection is 100% trusted and known to
1237         be friendly, skipping validation for performance reasons could also make
1238         sense in certain cases.
1239       </para>
1240
1241       <para>
1242         Generally speaking violations of the "must" requirements in this spec 
1243         should be considered possible attempts to exploit security, and violations 
1244         of the "should" suggestions should be considered legitimate (though perhaps
1245         they should generate an error in some cases).
1246       </para>
1247
1248       <para>
1249         The following extension points are built in to D-BUS on purpose and must
1250         not be treated as invalid protocol. The extension points are intended
1251         for use by future versions of this spec, they are not intended for third
1252         parties.  At the moment, the only way a third party could extend D-BUS
1253         without breaking interoperability would be to introduce a way to negotiate new
1254         feature support as part of the auth protocol, using EXTENSION_-prefixed
1255         commands. There is not yet a standard way to negotiate features.
1256         <itemizedlist>
1257           <listitem>
1258             <para>
1259               In the authentication protocol (see <xref linkend="auth-protocol"/>) unknown 
1260                 commands result in an ERROR rather than a disconnect. This enables 
1261                 future extensions to the protocol. Commands starting with EXTENSION_ are 
1262                 reserved for third parties.
1263             </para>
1264           </listitem>
1265           <listitem>
1266             <para>
1267               The authentication protocol supports pluggable auth mechanisms.
1268             </para>
1269           </listitem>
1270           <listitem>
1271             <para>
1272               The address format (see <xref linkend="addresses"/>) supports new
1273               kinds of transport.
1274             </para>
1275           </listitem>
1276           <listitem>
1277             <para>
1278               Messages with an unknown type (something other than
1279               <literal>METHOD_CALL</literal>, <literal>METHOD_RETURN</literal>,
1280               <literal>ERROR</literal>, <literal>SIGNAL</literal>) are ignored. 
1281               Unknown-type messages must still be well-formed in the same way 
1282               as the known messages, however. They still have the normal 
1283               header and body.
1284             </para>
1285           </listitem>
1286           <listitem>
1287             <para>
1288               Header fields with an unknown or unexpected field code must be ignored, 
1289               though again they must still be well-formed.
1290             </para>
1291           </listitem>
1292           <listitem>
1293             <para>
1294               New standard interfaces (with new methods and signals) can of course be added.
1295             </para>
1296           </listitem>
1297         </itemizedlist>
1298       </para>
1299
1300     </sect2>
1301
1302   </sect1>
1303
1304   <sect1 id="auth-protocol">
1305     <title>Authentication Protocol</title>
1306     <para>
1307       Before the flow of messages begins, two applications must
1308       authenticate. A simple plain-text protocol is used for
1309       authentication; this protocol is a SASL profile, and maps fairly
1310       directly from the SASL specification. The message encoding is
1311       NOT used here, only plain text messages.
1312     </para>
1313     <para>
1314       In examples, "C:" and "S:" indicate lines sent by the client and
1315       server respectively.
1316     </para>
1317     <sect2 id="auth-protocol-overview">
1318       <title>Protocol Overview</title>
1319       <para>
1320         The protocol is a line-based protocol, where each line ends with
1321         \r\n. Each line begins with an all-caps ASCII command name containing
1322         only the character range [A-Z_], a space, then any arguments for the
1323         command, then the \r\n ending the line. The protocol is
1324         case-sensitive. All bytes must be in the ASCII character set.
1325
1326         Commands from the client to the server are as follows:
1327
1328         <itemizedlist>
1329           <listitem><para>AUTH [mechanism] [initial-response]</para></listitem>
1330           <listitem><para>CANCEL</para></listitem>
1331           <listitem><para>BEGIN</para></listitem>
1332           <listitem><para>DATA &lt;data in hex encoding&gt;</para></listitem>
1333           <listitem><para>ERROR [human-readable error explanation]</para></listitem>
1334         </itemizedlist>
1335
1336         From server to client are as follows:
1337
1338         <itemizedlist>
1339           <listitem><para>REJECTED &lt;space-separated list of mechanism names&gt;</para></listitem>
1340           <listitem><para>OK</para></listitem>
1341           <listitem><para>DATA &lt;data in hex encoding&gt;</para></listitem>
1342           <listitem><para>ERROR</para></listitem>
1343         </itemizedlist>
1344       </para>
1345       <para>
1346         Unofficial extensions to the command set must begin with the letters 
1347         "EXTENSION_", to avoid conflicts with future official commands.
1348         For example, "EXTENSION_COM_MYDOMAIN_DO_STUFF".
1349       </para>
1350     </sect2>
1351     <sect2 id="auth-nul-byte">
1352       <title>Special credentials-passing nul byte</title>
1353       <para>
1354         Immediately after connecting to the server, the client must send a
1355         single nul byte. This byte may be accompanied by credentials
1356         information on some operating systems that use sendmsg() with
1357         SCM_CREDS or SCM_CREDENTIALS to pass credentials over UNIX domain
1358         sockets. However, the nul byte must be sent even on other kinds of
1359         socket, and even on operating systems that do not require a byte to be
1360         sent in order to transmit credentials. The text protocol described in
1361         this document begins after the single nul byte. If the first byte
1362         received from the client is not a nul byte, the server may disconnect 
1363         that client.
1364       </para>
1365       <para>
1366         A nul byte in any context other than the initial byte is an error; 
1367         the protocol is ASCII-only.
1368       </para>
1369       <para>
1370         The credentials sent along with the nul byte may be used with the 
1371         SASL mechanism EXTERNAL.
1372       </para>
1373     </sect2>
1374     <sect2 id="auth-command-auth">
1375       <title>AUTH command</title>
1376       <para>
1377         If an AUTH command has no arguments, it is a request to list
1378         available mechanisms. The server must respond with a REJECTED
1379         command listing the mechanisms it understands, or with an error.
1380       </para>
1381       <para>
1382         If an AUTH command specifies a mechanism, and the server supports
1383         said mechanism, the server should begin exchanging SASL
1384         challenge-response data with the client using DATA commands.
1385       </para>
1386       <para>
1387         If the server does not support the mechanism given in the AUTH
1388         command, it must send either a REJECTED command listing the mechanisms
1389         it does support, or an error.
1390       </para>
1391       <para>
1392         If the [initial-response] argument is provided, it is intended for use
1393         with mechanisms that have no initial challenge (or an empty initial
1394         challenge), as if it were the argument to an initial DATA command. If
1395         the selected mechanism has an initial challenge and [initial-response]
1396         was provided, the server should reject authentication by sending
1397         REJECTED.
1398       </para>
1399       <para>
1400         If authentication succeeds after exchanging DATA commands, 
1401         an OK command must be sent to the client.
1402       </para>
1403       <para>
1404         The first octet received by the client after the \r\n of the OK
1405         command must be the first octet of the authenticated/encrypted 
1406         stream of D-BUS messages.
1407       </para>
1408       <para>
1409         The first octet received by the server after the \r\n of the BEGIN
1410         command from the client must be the first octet of the
1411         authenticated/encrypted stream of D-BUS messages.
1412       </para>
1413     </sect2>
1414     <sect2 id="auth-command-cancel">
1415       <title>CANCEL Command</title>
1416       <para>
1417         At any time up to sending the BEGIN command, the client may send a
1418         CANCEL command. On receiving the CANCEL command, the server must
1419         send a REJECTED command and abort the current authentication
1420         exchange.
1421       </para>
1422     </sect2>
1423     <sect2 id="auth-command-data">
1424       <title>DATA Command</title>
1425       <para>
1426         The DATA command may come from either client or server, and simply 
1427         contains a hex-encoded block of data to be interpreted 
1428         according to the SASL mechanism in use.
1429       </para>
1430       <para>
1431         Some SASL mechanisms support sending an "empty string"; 
1432         FIXME we need some way to do this.
1433       </para>
1434     </sect2>
1435     <sect2 id="auth-command-begin">
1436       <title>BEGIN Command</title>
1437       <para>
1438         The BEGIN command acknowledges that the client has received an 
1439         OK command from the server, and that the stream of messages
1440         is about to begin. 
1441       </para>
1442       <para>
1443         The first octet received by the server after the \r\n of the BEGIN
1444         command from the client must be the first octet of the
1445         authenticated/encrypted stream of D-BUS messages.
1446       </para>
1447     </sect2>
1448     <sect2 id="auth-command-rejected">
1449       <title>REJECTED Command</title>
1450       <para>
1451         The REJECTED command indicates that the current authentication
1452         exchange has failed, and further exchange of DATA is inappropriate.
1453         The client would normally try another mechanism, or try providing
1454         different responses to challenges.
1455       </para><para>
1456         Optionally, the REJECTED command has a space-separated list of
1457         available auth mechanisms as arguments. If a server ever provides
1458         a list of supported mechanisms, it must provide the same list 
1459         each time it sends a REJECTED message. Clients are free to 
1460         ignore all lists received after the first.
1461       </para>
1462     </sect2>
1463     <sect2 id="auth-command-ok">
1464       <title>OK Command</title>
1465       <para>
1466         The OK command indicates that the client has been authenticated,
1467         and that further communication will be a stream of D-BUS messages
1468         (optionally encrypted, as negotiated) rather than this protocol.
1469       </para>
1470       <para>
1471         The first octet received by the client after the \r\n of the OK
1472         command must be the first octet of the authenticated/encrypted 
1473         stream of D-BUS messages.
1474       </para>
1475       <para>
1476         The client must respond to the OK command by sending a BEGIN
1477         command, followed by its stream of messages, or by disconnecting.
1478         The server must not accept additional commands using this protocol 
1479         after the OK command has been sent.
1480       </para>
1481     </sect2>
1482     <sect2 id="auth-command-error">
1483       <title>ERROR Command</title>
1484       <para>
1485         The ERROR command indicates that either server or client did not
1486         know a command, does not accept the given command in the current
1487         context, or did not understand the arguments to the command. This
1488         allows the protocol to be extended; a client or server can send a
1489         command present or permitted only in new protocol versions, and if
1490         an ERROR is received instead of an appropriate response, fall back
1491         to using some other technique.
1492       </para>
1493       <para>
1494         If an ERROR is sent, the server or client that sent the
1495         error must continue as if the command causing the ERROR had never been
1496         received. However, the the server or client receiving the error 
1497         should try something other than whatever caused the error; 
1498         if only canceling/rejecting the authentication.
1499       </para>
1500       <para>
1501         If the D-BUS protocol changes incompatibly at some future time,
1502         applications implementing the new protocol would probably be able to
1503         check for support of the new protocol by sending a new command and
1504         receiving an ERROR from applications that don't understand it. Thus the
1505         ERROR feature of the auth protocol is an escape hatch that lets us
1506         negotiate extensions or changes to the D-BUS protocol in the future.
1507       </para>
1508     </sect2>
1509     <sect2 id="auth-examples">
1510       <title>Authentication examples</title>
1511       
1512       <para>
1513         <figure>
1514           <title>Example of successful magic cookie authentication</title>
1515           <programlisting>
1516             (MAGIC_COOKIE is a made up mechanism)
1517
1518             C: AUTH MAGIC_COOKIE 3138363935333137393635383634
1519             S: OK
1520             C: BEGIN
1521           </programlisting>
1522         </figure>
1523         <figure>
1524           <title>Example of finding out mechanisms then picking one</title>
1525           <programlisting>
1526             C: AUTH
1527             S: REJECTED KERBEROS_V4 SKEY
1528             C: AUTH SKEY 7ab83f32ee
1529             S: DATA 8799cabb2ea93e
1530             C: DATA 8ac876e8f68ee9809bfa876e6f9876g8fa8e76e98f
1531             S: OK
1532             C: BEGIN
1533           </programlisting>
1534         </figure>
1535         <figure>
1536           <title>Example of client sends unknown command then falls back to regular auth</title>
1537           <programlisting>
1538             C: FOOBAR
1539             S: ERROR
1540             C: AUTH MAGIC_COOKIE 3736343435313230333039
1541             S: OK
1542             C: BEGIN
1543           </programlisting>
1544         </figure>
1545         <figure>
1546           <title>Example of server doesn't support initial auth mechanism</title>
1547           <programlisting>
1548             C: AUTH MAGIC_COOKIE 3736343435313230333039
1549             S: REJECTED KERBEROS_V4 SKEY
1550             C: AUTH SKEY 7ab83f32ee
1551             S: DATA 8799cabb2ea93e
1552             C: DATA 8ac876e8f68ee9809bfa876e6f9876g8fa8e76e98f
1553             S: OK
1554             C: BEGIN
1555           </programlisting>
1556         </figure>
1557         <figure>
1558           <title>Example of wrong password or the like followed by successful retry</title>
1559           <programlisting>
1560             C: AUTH MAGIC_COOKIE 3736343435313230333039
1561             S: REJECTED KERBEROS_V4 SKEY
1562             C: AUTH SKEY 7ab83f32ee
1563             S: DATA 8799cabb2ea93e
1564             C: DATA 8ac876e8f68ee9809bfa876e6f9876g8fa8e76e98f
1565             S: REJECTED
1566             C: AUTH SKEY 7ab83f32ee
1567             S: DATA 8799cabb2ea93e
1568             C: DATA 8ac876e8f68ee9809bfa876e6f9876g8fa8e76e98f
1569             S: OK
1570             C: BEGIN
1571           </programlisting>
1572         </figure>
1573         <figure>
1574           <title>Example of skey cancelled and restarted</title>
1575           <programlisting>
1576             C: AUTH MAGIC_COOKIE 3736343435313230333039
1577             S: REJECTED KERBEROS_V4 SKEY
1578             C: AUTH SKEY 7ab83f32ee
1579             S: DATA 8799cabb2ea93e
1580             C: CANCEL
1581             S: REJECTED
1582             C: AUTH SKEY 7ab83f32ee
1583             S: DATA 8799cabb2ea93e
1584             C: DATA 8ac876e8f68ee9809bfa876e6f9876g8fa8e76e98f
1585             S: OK
1586             C: BEGIN
1587           </programlisting>
1588         </figure>
1589       </para>
1590     </sect2>
1591     <sect2 id="auth-states">
1592       <title>Authentication state diagrams</title>
1593       
1594       <para>
1595         This section documents the auth protocol in terms of 
1596         a state machine for the client and the server. This is 
1597         probably the most robust way to implement the protocol.
1598       </para>
1599
1600       <sect3 id="auth-states-client">
1601         <title>Client states</title>
1602         
1603         <para>
1604           To more precisely describe the interaction between the
1605           protocol state machine and the authentication mechanisms the
1606           following notation is used: MECH(CHALL) means that the
1607           server challenge CHALL was fed to the mechanism MECH, which
1608           returns one of
1609
1610           <itemizedlist>
1611             <listitem>
1612               <para>
1613                 CONTINUE(RESP) means continue the auth conversation
1614                 and send RESP as the response to the server;
1615               </para>
1616             </listitem>
1617
1618             <listitem>
1619               <para>
1620                 OK(RESP) means that after sending RESP to the server
1621                 the client side of the auth conversation is finished
1622                 and the server should return "OK";
1623               </para>
1624             </listitem>
1625
1626             <listitem>
1627               <para>
1628                 ERROR means that CHALL was invalid and could not be
1629                 processed.
1630               </para>
1631             </listitem>
1632           </itemizedlist>
1633           
1634           Both RESP and CHALL may be empty.
1635         </para>
1636         
1637         <para>
1638           The Client starts by getting an initial response from the
1639           default mechanism and sends AUTH MECH RESP, or AUTH MECH if
1640           the mechanism did not provide an initial response.  If the
1641           mechanism returns CONTINUE, the client starts in state
1642           <emphasis>WaitingForData</emphasis>, if the mechanism
1643           returns OK the client starts in state
1644           <emphasis>WaitingForOK</emphasis>.
1645         </para>
1646         
1647         <para>
1648           The client should keep track of available mechanisms and
1649           which it mechanisms it has already attempted. This list is
1650           used to decide which AUTH command to send. When the list is
1651           exhausted, the client should give up and close the
1652           connection.
1653         </para>
1654
1655         <formalpara>
1656           <title><emphasis>WaitingForData</emphasis></title>
1657           <para>
1658             <itemizedlist>
1659               <listitem>
1660                 <para>
1661                   Receive DATA CHALL
1662                   <simplelist>
1663                     <member>
1664                       MECH(CHALL) returns CONTINUE(RESP) &rarr; send
1665                       DATA RESP, goto
1666                       <emphasis>WaitingForData</emphasis>
1667                     </member>
1668
1669                     <member>
1670                       MECH(CHALL) returns OK(RESP) &rarr; send DATA
1671                       RESP, goto <emphasis>WaitingForOK</emphasis>
1672                     </member>
1673
1674                     <member>
1675                       MECH(CHALL) returns ERROR &rarr; send ERROR
1676                       [msg], goto <emphasis>WaitingForData</emphasis>
1677                     </member>
1678                   </simplelist>
1679                 </para>
1680               </listitem>
1681
1682               <listitem>
1683                 <para>
1684                   Receive REJECTED [mechs] &rarr;
1685                   send AUTH [next mech], goto
1686                   WaitingForData or <emphasis>WaitingForOK</emphasis>
1687                 </para>
1688               </listitem>
1689               <listitem>
1690                 <para>
1691                   Receive ERROR &rarr; send
1692                   CANCEL, goto
1693                   <emphasis>WaitingForReject</emphasis>
1694                 </para>
1695               </listitem>
1696               <listitem>
1697                 <para>
1698                   Receive OK &rarr; send
1699                   BEGIN, terminate auth
1700                   conversation, authenticated
1701                 </para>
1702               </listitem>
1703               <listitem>
1704                 <para>
1705                   Receive anything else &rarr; send
1706                   ERROR, goto
1707                   <emphasis>WaitingForData</emphasis>
1708                 </para>
1709               </listitem>
1710             </itemizedlist>
1711           </para>
1712         </formalpara>
1713
1714         <formalpara>
1715           <title><emphasis>WaitingForOK</emphasis></title>
1716           <para>
1717             <itemizedlist>
1718               <listitem>
1719                 <para>
1720                   Receive OK &rarr; send BEGIN, terminate auth
1721                   conversation, <emphasis>authenticated</emphasis>
1722                 </para>
1723               </listitem>
1724               <listitem>
1725                 <para>
1726                   Receive REJECT [mechs] &rarr; send AUTH [next mech],
1727                   goto <emphasis>WaitingForData</emphasis> or
1728                   <emphasis>WaitingForOK</emphasis>
1729                 </para>
1730               </listitem>
1731
1732               <listitem>
1733                 <para>
1734                   Receive DATA &rarr; send CANCEL, goto
1735                   <emphasis>WaitingForReject</emphasis>
1736                 </para>
1737               </listitem>
1738
1739               <listitem>
1740                 <para>
1741                   Receive ERROR &rarr; send CANCEL, goto
1742                   <emphasis>WaitingForReject</emphasis>
1743                 </para>
1744               </listitem>
1745
1746               <listitem>
1747                 <para>
1748                   Receive anything else &rarr; send ERROR, goto
1749                   <emphasis>WaitingForOK</emphasis>
1750                 </para>
1751               </listitem>
1752             </itemizedlist>
1753           </para>
1754         </formalpara>
1755
1756         <formalpara>
1757           <title><emphasis>WaitingForReject</emphasis></title>
1758           <para>
1759             <itemizedlist>
1760               <listitem>
1761                 <para>
1762                   Receive REJECT [mechs] &rarr; send AUTH [next mech],
1763                   goto <emphasis>WaitingForData</emphasis> or
1764                   <emphasis>WaitingForOK</emphasis>
1765                 </para>
1766               </listitem>
1767
1768               <listitem>
1769                 <para>
1770                   Receive anything else &rarr; terminate auth
1771                   conversation, disconnect
1772                 </para>
1773               </listitem>
1774             </itemizedlist>
1775           </para>
1776         </formalpara>
1777
1778       </sect3>
1779
1780       <sect3 id="auth-states-server">
1781         <title>Server states</title>
1782  
1783         <para>
1784           For the server MECH(RESP) means that the client response
1785           RESP was fed to the the mechanism MECH, which returns one of
1786
1787           <itemizedlist>
1788             <listitem>
1789               <para>
1790                 CONTINUE(CHALL) means continue the auth conversation and
1791                 send CHALL as the challenge to the client;
1792               </para>
1793             </listitem>
1794
1795             <listitem>
1796               <para>
1797                 OK means that the client has been successfully
1798                 authenticated;
1799               </para>
1800             </listitem>
1801
1802             <listitem>
1803               <para>
1804                 REJECT means that the client failed to authenticate or
1805                 there was an error in RESP.
1806               </para>
1807             </listitem>
1808           </itemizedlist>
1809
1810           The server starts out in state
1811           <emphasis>WaitingForAuth</emphasis>.  If the client is
1812           rejected too many times the server must disconnect the
1813           client.
1814         </para>
1815
1816         <formalpara>
1817           <title><emphasis>WaitingForAuth</emphasis></title>
1818           <para>
1819             <itemizedlist>
1820
1821               <listitem>
1822                 <para>
1823                   Receive AUTH &rarr; send REJECTED [mechs], goto
1824                   <emphasis>WaitingForAuth</emphasis>
1825                 </para>
1826               </listitem>
1827
1828               <listitem>
1829                 <para>
1830                   Receive AUTH MECH RESP
1831
1832                   <simplelist>
1833                     <member>
1834                       MECH not valid mechanism &rarr; send REJECTED
1835                       [mechs], goto
1836                       <emphasis>WaitingForAuth</emphasis>
1837                     </member>
1838
1839                     <member>
1840                       MECH(RESP) returns CONTINUE(CHALL) &rarr; send
1841                       DATA CHALL, goto
1842                       <emphasis>WaitingForData</emphasis>
1843                     </member>
1844
1845                     <member>
1846                       MECH(RESP) returns OK &rarr; send OK, goto
1847                       <emphasis>WaitingForBegin</emphasis>
1848                     </member>
1849
1850                     <member>
1851                       MECH(RESP) returns REJECT &rarr; send REJECTED
1852                       [mechs], goto
1853                       <emphasis>WaitingForAuth</emphasis>
1854                     </member>
1855                   </simplelist>
1856                 </para>
1857               </listitem>
1858
1859               <listitem>
1860                 <para>
1861                   Receive BEGIN &rarr; terminate
1862                   auth conversation, disconnect
1863                 </para>
1864               </listitem>
1865
1866               <listitem>
1867                 <para>
1868                   Receive ERROR &rarr; send REJECTED [mechs], goto
1869                   <emphasis>WaitingForAuth</emphasis>
1870                 </para>
1871               </listitem>
1872
1873               <listitem>
1874                 <para>
1875                   Receive anything else &rarr; send
1876                   ERROR, goto
1877                   <emphasis>WaitingForAuth</emphasis>
1878                 </para>
1879               </listitem>
1880             </itemizedlist>
1881           </para>
1882         </formalpara>
1883
1884        
1885         <formalpara>
1886           <title><emphasis>WaitingForData</emphasis></title>
1887           <para>
1888             <itemizedlist>
1889               <listitem>
1890                 <para>
1891                   Receive DATA RESP
1892                   <simplelist>
1893                     <member>
1894                       MECH(RESP) returns CONTINUE(CHALL) &rarr; send
1895                       DATA CHALL, goto
1896                       <emphasis>WaitingForData</emphasis>
1897                     </member>
1898
1899                     <member>
1900                       MECH(RESP) returns OK &rarr; send OK, goto
1901                       <emphasis>WaitingForBegin</emphasis>
1902                     </member>
1903
1904                     <member>
1905                       MECH(RESP) returns REJECT &rarr; send REJECTED
1906                       [mechs], goto
1907                       <emphasis>WaitingForAuth</emphasis>
1908                     </member>
1909                   </simplelist>
1910                 </para>
1911               </listitem>
1912
1913               <listitem>
1914                 <para>
1915                   Receive BEGIN &rarr; terminate auth conversation,
1916                   disconnect
1917                 </para>
1918               </listitem>
1919
1920               <listitem>
1921                 <para>
1922                   Receive CANCEL &rarr; send REJECTED [mechs], goto
1923                   <emphasis>WaitingForAuth</emphasis>
1924                 </para>
1925               </listitem>
1926
1927               <listitem>
1928                 <para>
1929                   Receive ERROR &rarr; send REJECTED [mechs], goto
1930                   <emphasis>WaitingForAuth</emphasis>
1931                 </para>
1932               </listitem>
1933
1934               <listitem>
1935                 <para>
1936                   Receive anything else &rarr; send ERROR, goto
1937                   <emphasis>WaitingForData</emphasis>
1938                 </para>
1939               </listitem>
1940             </itemizedlist>
1941           </para>
1942         </formalpara>
1943
1944         <formalpara>
1945           <title><emphasis>WaitingForBegin</emphasis></title>
1946           <para>
1947             <itemizedlist>
1948               <listitem>
1949                 <para>
1950                   Receive BEGIN &rarr; terminate auth conversation,
1951                   client authenticated
1952                 </para>
1953               </listitem>
1954
1955               <listitem>
1956                 <para>
1957                   Receive CANCEL &rarr; send REJECTED [mechs], goto
1958                   <emphasis>WaitingForAuth</emphasis>
1959                 </para>
1960               </listitem>
1961
1962               <listitem>
1963                 <para>
1964                   Receive ERROR &rarr; send REJECTED [mechs], goto
1965                   <emphasis>WaitingForAuth</emphasis>
1966                 </para>
1967               </listitem>
1968
1969               <listitem>
1970                 <para>
1971                   Receive anything else &rarr; send ERROR, goto
1972                   <emphasis>WaitingForBegin</emphasis>
1973                 </para>
1974               </listitem>
1975             </itemizedlist>
1976           </para>
1977         </formalpara>
1978
1979       </sect3>
1980       
1981     </sect2>
1982     <sect2 id="auth-mechanisms">
1983       <title>Authentication mechanisms</title>
1984       <para>
1985         This section describes some new authentication mechanisms.
1986         D-BUS also allows any standard SASL mechanism of course.
1987       </para>
1988       <sect3 id="auth-mechanisms-sha">
1989         <title>DBUS_COOKIE_SHA1</title>
1990         <para>
1991           The DBUS_COOKIE_SHA1 mechanism is designed to establish that a client
1992           has the ability to read a private file owned by the user being
1993           authenticated. If the client can prove that it has access to a secret
1994           cookie stored in this file, then the client is authenticated. 
1995           Thus the security of DBUS_COOKIE_SHA1 depends on a secure home 
1996           directory.
1997         </para>
1998         <para>
1999           Authentication proceeds as follows:
2000           <itemizedlist>
2001             <listitem>
2002               <para>
2003                 The client sends the username it would like to authenticate 
2004                 as.
2005               </para>
2006             </listitem>
2007             <listitem>
2008               <para>
2009                 The server sends the name of its "cookie context" (see below); a
2010                 space character; the integer ID of the secret cookie the client
2011                 must demonstrate knowledge of; a space character; then a
2012                 hex-encoded randomly-generated challenge string.
2013               </para>
2014             </listitem>
2015             <listitem>
2016               <para>
2017                 The client locates the cookie, and generates its own hex-encoded
2018                 randomly-generated challenge string.  The client then
2019                 concatentates the server's hex-encoded challenge, a ":"
2020                 character, its own hex-encoded challenge, another ":" character,
2021                 and the hex-encoded cookie.  It computes the SHA-1 hash of this
2022                 composite string.  It sends back to the server the client's
2023                 hex-encoded challenge string, a space character, and the SHA-1
2024                 hash.
2025               </para>
2026             </listitem>
2027             <listitem>
2028               <para>
2029                 The server generates the same concatenated string used by the
2030                 client and computes its SHA-1 hash. It compares the hash with
2031                 the hash received from the client; if the two hashes match, the
2032                 client is authenticated.
2033               </para>
2034             </listitem>
2035           </itemizedlist>
2036         </para>
2037         <para>
2038           Each server has a "cookie context," which is a name that identifies a
2039           set of cookies that apply to that server. A sample context might be
2040           "org_freedesktop_session_bus". Context names must be valid ASCII,
2041           nonzero length, and may not contain the characters slash ("/"),
2042           backslash ("\"), space (" "), newline ("\n"), carriage return ("\r"),
2043           tab ("\t"), or period ("."). There is a default context,
2044           "org_freedesktop_general" that's used by servers that do not specify
2045           otherwise.
2046         </para>
2047         <para>
2048           Cookies are stored in a user's home directory, in the directory
2049           <filename>~/.dbus-keyrings/</filename>. This directory must 
2050           not be readable or writable by other users. If it is, 
2051           clients and servers must ignore it. The directory 
2052           contains cookie files named after the cookie context.
2053         </para>
2054         <para>
2055           A cookie file contains one cookie per line. Each line 
2056           has three space-separated fields:
2057           <itemizedlist>
2058             <listitem>
2059               <para>
2060                 The cookie ID number, which must be a non-negative integer and
2061                 may not be used twice in the same file.
2062               </para>
2063             </listitem>
2064             <listitem>
2065               <para>
2066                 The cookie's creation time, in UNIX seconds-since-the-epoch
2067                 format.
2068               </para>
2069             </listitem>
2070             <listitem>
2071               <para>
2072                 The cookie itself, a hex-encoded random block of bytes. The cookie
2073                 may be of any length, though obviously security increases 
2074                 as the length increases.
2075               </para>
2076             </listitem>
2077           </itemizedlist>
2078         </para>
2079         <para>
2080           Only server processes modify the cookie file.
2081           They must do so with this procedure:
2082           <itemizedlist>
2083             <listitem>
2084               <para>
2085                 Create a lockfile name by appending ".lock" to the name of the
2086                 cookie file.  The server should attempt to create this file
2087                 using <literal>O_CREAT | O_EXCL</literal>.  If file creation
2088                 fails, the lock fails. Servers should retry for a reasonable
2089                 period of time, then they may choose to delete an existing lock
2090                 to keep users from having to manually delete a stale
2091                 lock. <footnote><para>Lockfiles are used instead of real file
2092                 locking <literal>fcntl()</literal> because real locking
2093                 implementations are still flaky on network
2094                 filesystems.</para></footnote>
2095               </para>
2096             </listitem>
2097             <listitem>
2098               <para>
2099                 Once the lockfile has been created, the server loads the cookie
2100                 file. It should then delete any cookies that are old (the
2101                 timeout can be fairly short), or more than a reasonable
2102                 time in the future (so that cookies never accidentally 
2103                 become permanent, if the clock was set far into the future 
2104                 at some point). If no recent keys remain, the 
2105                 server may generate a new key.
2106               </para>
2107             </listitem>
2108             <listitem>
2109               <para>
2110                 The pruned and possibly added-to cookie file 
2111                 must be resaved atomically (using a temporary 
2112                 file which is rename()'d).
2113               </para>
2114             </listitem>
2115             <listitem>
2116               <para>
2117                 The lock must be dropped by deleting the lockfile.
2118               </para>
2119             </listitem>
2120           </itemizedlist>
2121         </para>
2122         <para>
2123           Clients need not lock the file in order to load it, 
2124           because servers are required to save the file atomically.          
2125         </para>
2126       </sect3>
2127     </sect2>
2128   </sect1>
2129   <sect1 id="addresses">
2130     <title>Server Addresses</title>
2131     <para>
2132       Server addresses consist of a transport name followed by a colon, and
2133       then an optional, comma-separated list of keys and values in the form key=value.
2134       Each value is escaped.
2135     </para>
2136     <para>
2137       For example: 
2138       <programlisting>unix:path=/tmp/dbus-test</programlisting>
2139       Which is the address to a unix socket with the path /tmp/dbus-test.
2140     </para>
2141     <para>
2142       Value escaping is similar to URI escaping but simpler.
2143       <itemizedlist>
2144         <listitem>
2145           <para>
2146             The set of optionally-escaped bytes is:
2147             <literal>[0-9A-Za-z_-/.\]</literal>. To escape, each
2148             <emphasis>byte</emphasis> (note, not character) which is not in the
2149             set of optionally-escaped bytes must be replaced with an ASCII
2150             percent (<literal>%</literal>) and the value of the byte in hex.
2151             The hex value must always be two digits, even if the first digit is
2152             zero. The optionally-escaped bytes may be escaped if desired.
2153           </para>
2154         </listitem>
2155         <listitem>
2156           <para>
2157             To unescape, append each byte in the value; if a byte is an ASCII
2158             percent (<literal>%</literal>) character then append the following
2159             hex value instead. It is an error if a <literal>%</literal> byte
2160             does not have two hex digits following. It is an error if a
2161             non-optionally-escaped byte is seen unescaped.
2162           </para>
2163         </listitem>
2164       </itemizedlist>
2165       The set of optionally-escaped bytes is intended to preserve address 
2166       readability and convenience.
2167     </para>
2168     <para>
2169       [FIXME clarify if attempting to connect to each is a requirement 
2170       or just a suggestion]
2171       When connecting to a server, multiple server addresses can be
2172       separated by a semi-colon. The library will then try to connect
2173       to the first address and if that fails, it'll try to connect to
2174       the next one specified, and so forth. For example
2175       <programlisting>unix:path=/tmp/dbus-test;unix:path=/tmp/dbus-test2</programlisting>
2176     </para>
2177     <para>
2178       [FIXME we need to specify in detail each transport and its possible arguments]
2179       Current transports include: unix domain sockets (including 
2180       abstract namespace on linux), TCP/IP, and a debug/testing transport using 
2181       in-process pipes. Future possible transports include one that 
2182       tunnels over X11 protocol.
2183     </para>
2184   </sect1>
2185
2186   <sect1 id="naming-conventions">
2187     <title>Naming Conventions</title>
2188     
2189     <para>
2190       D-BUS namespaces are all lowercase and correspond to reversed domain
2191       names, as with Java. e.g. "org.freedesktop"
2192     </para>
2193     <para>
2194       Interface, signal, method, and property names are "WindowsStyleCaps", note
2195       that the first letter is capitalized, unlike Java.
2196     </para>
2197     <para>
2198       Object paths are normally all lowercase with underscores used rather than
2199       hyphens.
2200     </para>
2201   </sect1>
2202     
2203   <sect1 id="standard-interfaces">
2204     <title>Standard Interfaces</title>
2205     <para>
2206       See <xref linkend="message-protocol-types-notation"/> for details on 
2207        the notation used in this section. There are some standard interfaces
2208       that may be useful across various D-BUS applications.
2209     </para>
2210     <sect2 id="standard-interfaces-peer">
2211       <title><literal>org.freedesktop.Peer</literal></title>
2212       <para>
2213         The <literal>org.freedesktop.Peer</literal> interface 
2214         has one method:
2215         <programlisting>
2216           org.freedesktop.Peer.Ping ()
2217         </programlisting>
2218       </para>
2219       <para>
2220         On receipt of the <literal>METHOD_CALL</literal> message
2221         <literal>org.freedesktop.Peer.Ping</literal>, an application should do
2222         nothing other than reply with a <literal>METHOD_RETURN</literal> as
2223         usual.  It does not matter which object path a ping is sent to.  The
2224         reference implementation should simply handle this method on behalf of
2225         all objects, though it doesn't yet. (The point is, you're really pinging
2226         the peer process, not a specific object.)
2227       </para>
2228     </sect2>
2229
2230     <sect2 id="standard-interfaces-introspectable">
2231       <title><literal>org.freedesktop.Introspectable</literal></title>
2232       <para>
2233         This interface has one method:
2234         <programlisting>
2235           org.freedesktop.Introspectable.Introspect (out STRING xml_data)
2236         </programlisting>
2237       </para>
2238       <para>
2239         Objects instances may implement
2240         <literal>Introspect</literal> which returns an XML description of
2241         the object, including its interfaces (with signals and methods), objects
2242         below it in the object path tree, and its properties.
2243       </para>
2244       <para>
2245         <xref linkend="introspection-format"/> describes the format of this XML string.
2246       </para>
2247     </sect2>
2248     <sect2 id="standard-interfaces-properties">
2249       <title><literal>org.freedesktop.Properties</literal></title>
2250       <para>
2251         Many native APIs will have a concept of object <firstterm>properties</firstterm> 
2252         or <firstterm>attributes</firstterm>. These can be exposed via the 
2253         <literal>org.freedesktop.Properties</literal> interface.
2254       </para>
2255       <para>
2256         <programlisting>
2257               org.freedesktop.Properties.Get (in STRING interface_name,
2258                                               in STRING property_name,
2259                                               out VARIANT value);
2260               org.freedesktop.Properties.Set (in STRING interface_name,
2261                                               in STRING property_name,
2262                                               in VARIANT value);
2263         </programlisting>
2264       </para>
2265       <para>
2266         The available properties and whether they are writable can be determined
2267         by calling <literal>org.freedesktop.Introspectable.Introspect</literal>,
2268         see <xref linkend="standard-interfaces-introspectable"/>.
2269       </para>
2270       <para>
2271         An empty string may be provided for the interface name; in this case, 
2272         if there are multiple properties on an object with the same name, 
2273         the results are undefined (picking one by according to an arbitrary 
2274         deterministic rule, or returning an error, are the reasonable 
2275         possibilities).
2276       </para>
2277     </sect2>
2278   </sect1>
2279
2280   <sect1 id="introspection-format">
2281     <title>Introspection Data Format</title>
2282     <para>
2283       As described in <xref linkend="standard-interfaces-introspectable"/>, 
2284       objects may be introspected at runtime, returning an XML string 
2285       that describes the object. The same XML format may be used in 
2286       other contexts as well, for example as an "IDL" for generating 
2287       static language bindings.
2288     </para>
2289     <para>
2290       Here is an example of introspection data:
2291       <programlisting>
2292         &lt;!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
2293          "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"&gt;
2294         &lt;node name="/org/freedesktop/sample_object"&gt;
2295           &lt;interface name="org.freedesktop.SampleInterface"&gt;
2296             &lt;method name="Frobate"&gt;
2297               &lt;arg name="foo" type="int32" direction="in"/&gt;
2298               &lt;arg name="bar" type="string" direction="out"/&gt;
2299               &lt;annotation name="org.freedesktop.DBus.Deprecated" value="true"/&gt;
2300             &lt;/method&gt;
2301             &lt;signal name="Changed"&gt;
2302               &lt;arg name="new_value" type="boolean"/&gt;
2303             &lt;/signal&gt;
2304             &lt;property name="Bar" type="byte" access="readwrite"/&gt;
2305           &lt;/interface&gt;
2306           &lt;node name="child_of_sample_object"/&gt;
2307           &lt;node name="another_child_of_sample_object"/&gt;
2308        &lt;/node&gt;
2309       </programlisting>
2310     </para>
2311     <para>
2312       A more formal DTD and spec needs writing, but here are some quick notes.
2313       <itemizedlist>
2314         <listitem>
2315           <para>
2316             Only the root &lt;node&gt; element can omit the node name, as it's
2317             known to be the object that was introspected.  If the root
2318             &lt;node&gt; does have a name attribute, it must be an absolute
2319             object path. If child &lt;node&gt; have object paths, they must be
2320             relative.
2321           </para>
2322         </listitem>
2323         <listitem>
2324           <para>
2325             If a child &lt;node&gt; has any sub-elements, then they 
2326             must represent a complete introspection of the child.
2327             If a child &lt;node&gt; is empty, then it may or may 
2328             not have sub-elements; the child must be introspected
2329             in order to find out. The intent is that if an object 
2330             knows that its children are "fast" to introspect
2331             it can go ahead and return their information, but 
2332             otherwise it can omit it.
2333           </para>
2334         </listitem>
2335         <listitem>
2336           <para>
2337             The direction element on &lt;arg&gt; may be omitted, 
2338             in which case it defaults to "in" for method calls 
2339             and "out" for signals. Signals only allow "out" 
2340             so while direction may be specified, it's pointless.
2341           </para>
2342         </listitem>
2343         <listitem>
2344           <para>
2345             The possible directions are "in" and "out", 
2346             unlike CORBA there is no "inout"
2347           </para>
2348         </listitem>
2349         <listitem>
2350           <para>
2351             The possible property access flags are 
2352             "readwrite", "read", and "write"
2353           </para>
2354         </listitem>
2355         <listitem>
2356           <para>
2357             The current type="uint32" stuff is totally broken,
2358             instead we have to do full signatures. 
2359             However, then this format will suck for human readability. 
2360             So, some thinking to do here.
2361           </para>
2362         </listitem>
2363         <listitem>
2364           <para>
2365             Multiple interfaces can of course be listed for 
2366             one &lt;node&gt;.
2367           </para>
2368         </listitem>
2369         <listitem>
2370           <para>
2371             The "name" attribute on arguments is optional.
2372           </para>
2373         </listitem>
2374       </itemizedlist>
2375     </para>
2376     <para>
2377         Method, interface, property, and signal elements may have
2378         "annotations", which are generic key/value pairs of metadata.
2379         They are similar conceptually to Java's annotations and C# attributes.
2380         Well-known annotations:
2381      </para>
2382      <informaltable>
2383        <tgroup cols="3">
2384          <thead>
2385            <row>
2386              <entry>Name</entry>
2387              <entry>Values (separated by ,)</entry>
2388              <entry>Description</entry>
2389            </row>
2390          </thead>
2391          <tbody>
2392            <row>
2393              <entry>org.freedesktop.DBus.Deprecated</entry>
2394              <entry>true,false</entry>
2395              <entry>Whether or not the entity is deprecated; defaults to false</entry>
2396            </row>
2397            <row>
2398              <entry>org.freedesktop.DBus.GLib.CSymbol</entry>
2399              <entry>(string)</entry>
2400              <entry>The C symbol; may be used for methods and interfaces</entry>
2401            </row>
2402          </tbody>
2403        </tgroup>
2404      </informaltable>
2405   </sect1>
2406   <sect1 id="message-bus">
2407     <title>Message Bus Specification</title>
2408     <sect2 id="message-bus-overview">
2409       <title>Message Bus Overview</title>
2410       <para>
2411         The message bus accepts connections from one or more applications. 
2412         Once connected, applications can exchange messages with other 
2413         applications that are also connected to the bus.
2414       </para>
2415       <para>
2416         In order to route messages among connections, the message bus keeps a
2417         mapping from names to connections. Each connection has one
2418         unique-for-the-lifetime-of-the-bus name automatically assigned.
2419         Applications may request additional names for a connection. Additional
2420         names are usually "well-known names" such as
2421         "org.freedesktop.TextEditor". When a name is bound to a connection,
2422         that connection is said to <firstterm>own</firstterm> the name.
2423       </para>
2424       <para>
2425         The bus itself owns a special name, <literal>org.freedesktop.DBus</literal>. 
2426         This name routes messages to the bus, allowing applications to make 
2427         administrative requests. For example, applications can ask the bus 
2428         to assign a name to a connection.
2429       </para>
2430       <para>
2431         Each name may have <firstterm>queued owners</firstterm>.  When an
2432         application requests a name for a connection and the name is already in
2433         use, the bus will optionally add the connection to a queue waiting for 
2434         the name. If the current owner of the name disconnects or releases
2435         the name, the next connection in the queue will become the new owner.
2436       </para>
2437
2438       <para>
2439         This feature causes the right thing to happen if you start two text
2440         editors for example; the first one may request "org.freedesktop.TextEditor", 
2441         and the second will be queued as a possible owner of that name. When 
2442         the first exits, the second will take over.
2443       </para>
2444
2445       <para>
2446         Messages may have a <literal>DESTINATION</literal> field (see <xref
2447         linkend="message-protocol-header-fields"/>).  If the
2448         <literal>DESTINATION</literal> field is present, it specifies a message
2449         recipient by name. Method calls and replies normally specify this field.
2450       </para>
2451
2452       <para>
2453         Signals normally do not specify a destination; they are sent to all
2454         applications with <firstterm>message matching rules</firstterm> that
2455         match the message.
2456       </para>
2457
2458       <para>
2459         When the message bus receives a method call, if the
2460         <literal>DESTINATION</literal> field is absent, the call is taken to be
2461         a standard one-to-one message and interpreted by the message bus
2462         itself. For example, sending an
2463         <literal>org.freedesktop.Peer.Ping</literal> message with no
2464         <literal>DESTINATION</literal> will cause the message bus itself to
2465         reply to the ping immediately; the message bus will not make this
2466         message visible to other applications.
2467       </para>
2468
2469       <para>
2470         Continuing the <literal>org.freedesktop.Peer.Ping</literal> example, if
2471         the ping message were sent with a <literal>DESTINATION</literal> name of
2472         <literal>com.yoyodyne.Screensaver</literal>, then the ping would be
2473         forwarded, and the Yoyodyne Corporation screensaver application would be
2474         expected to reply to the ping.
2475       </para>
2476     </sect2>
2477
2478     <sect2 id="message-bus-names">
2479       <title>Message Bus Names</title>
2480       <para>
2481         Each connection has at least one name, assigned at connection time and
2482         returned in response to the
2483         <literal>org.freedesktop.DBus.Hello</literal> method call.  This
2484         automatically-assigned name is called the connection's <firstterm>unique
2485         name</firstterm>.  Unique names are never reused for two different
2486         connections to the same bus.
2487       </para>
2488       <para>
2489         Ownership of a unique name is a prerequisite for interaction with 
2490         the message bus. It logically follows that the unique name is always 
2491         the first name that an application comes to own, and the last 
2492         one that it loses ownership of.
2493       </para>
2494       <para>
2495         Unique connection names must begin with the character ':' (ASCII colon
2496         character); bus names that are not unique names must not begin
2497         with this character. (The bus must reject any attempt by an application
2498         to manually request a name beginning with ':'.) This restriction
2499         categorically prevents "spoofing"; messages sent to a unique name
2500         will always go to the expected connection.
2501       </para>
2502       <para>
2503         When a connection is closed, all the names that it owns are deleted (or
2504         transferred to the next connection in the queue if any).
2505       </para>
2506       <para>
2507         A connection can request additional names to be associated with it using
2508         the <literal>org.freedesktop.DBus.RequestName</literal> message. <xref
2509         linkend="message-protocol-names-bus"/> describes the format of a valid
2510         name.
2511       </para>
2512
2513       <sect3 id="bus-messages-request-name">
2514         <title><literal>org.freedesktop.DBus.RequestName</literal></title>
2515         <para>
2516           As a method:
2517           <programlisting>
2518             UINT32 RequestName (in STRING name, in UINT32 flags)
2519           </programlisting>
2520           Message arguments:
2521           <informaltable>
2522             <tgroup cols="3">
2523               <thead>
2524                 <row>
2525                   <entry>Argument</entry>
2526                   <entry>Type</entry>
2527                   <entry>Description</entry>
2528                 </row>
2529               </thead>
2530               <tbody>
2531                 <row>
2532                   <entry>0</entry>
2533                   <entry>STRING</entry>
2534                   <entry>Name to request</entry>
2535                 </row>
2536                 <row>
2537                   <entry>1</entry>
2538                   <entry>UINT32</entry>
2539                   <entry>Flags</entry>
2540                 </row>
2541               </tbody>
2542             </tgroup>
2543           </informaltable>
2544           Reply arguments:
2545           <informaltable>
2546             <tgroup cols="3">
2547               <thead>
2548                 <row>
2549                   <entry>Argument</entry>
2550                   <entry>Type</entry>
2551                   <entry>Description</entry>
2552                 </row>
2553               </thead>
2554               <tbody>
2555                 <row>
2556                   <entry>0</entry>
2557                   <entry>UINT32</entry>
2558                   <entry>Return value</entry>
2559                 </row>
2560               </tbody>
2561             </tgroup>
2562           </informaltable>
2563         </para>
2564         <para>
2565           This method call should be sent to
2566           <literal>org.freedesktop.DBus</literal> and asks the message bus to
2567           assign the given name to the method caller.  The flags argument
2568           contains any of the following values logically ORed together:
2569
2570           <informaltable>
2571             <tgroup cols="3">
2572               <thead>
2573                 <row>
2574                   <entry>Conventional Name</entry>
2575                   <entry>Value</entry>
2576                   <entry>Description</entry>
2577                 </row>
2578               </thead>
2579               <tbody>
2580                 <row>
2581                   <entry>DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT</entry>
2582                   <entry>0x1</entry>
2583                   <entry>
2584                     If the application succeeds in becoming the owner of the specified name,
2585                     then ownership of the name can't be transferred until the application
2586                     disconnects. If this flag is not set, then any application trying to become
2587                     the owner of the name will succeed and the previous owner will be
2588                     sent a <literal>org.freedesktop.DBus.NameLost</literal> signal.
2589                   </entry>
2590                 </row>
2591                 <row>
2592                   <entry>DBUS_NAME_FLAG_REPLACE_EXISTING</entry>
2593                   <entry>0x2</entry>
2594                   <entry>
2595                     Try to replace the current owner if there is one. If this
2596                     flag is not set the application will only become the owner of
2597                     the name if there is no current owner.
2598                   </entry>
2599                 </row>
2600               </tbody>
2601             </tgroup>
2602           </informaltable>
2603
2604           The return code can be one of the following values:
2605
2606           <informaltable>
2607             <tgroup cols="3">
2608               <thead>
2609                 <row>
2610                   <entry>Conventional Name</entry>
2611                   <entry>Value</entry>
2612                   <entry>Description</entry>
2613                 </row>
2614               </thead>
2615               <tbody>
2616                 <row>
2617                   <entry>DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER</entry>
2618                   <entry>1</entry> <entry>The caller is now the primary owner of
2619                   the name, replacing any previous owner. Either the name had no
2620                   owner before, or the caller specified
2621                   DBUS_NAME_FLAG_REPLACE_EXISTING and the current owner did not
2622                   specify DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT.</entry>
2623                 </row>
2624                 <row>
2625                   <entry>DBUS_REQUEST_NAME_REPLY_IN_QUEUE</entry>
2626                   <entry>2</entry>
2627                   <entry>The name already had an owner, DBUS_NAME_FLAG_REPLACE_EXISTING was not specified, and the current owner specified DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT.</entry>
2628                 </row>
2629                 <row>
2630                   <entry>DBUS_REQUEST_NAME_REPLY_EXISTS</entry>
2631                   <entry>3</entry>
2632                   <entry>The name already has an owner, and DBUS_NAME_FLAG_REPLACE_EXISTING was not specified.</entry>
2633                 </row>
2634                 <row>
2635                   <entry>DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER</entry>
2636                   <entry>4</entry>
2637                   <entry>The application trying to request ownership of a name is already the owner of it.</entry>
2638                 </row>
2639               </tbody>
2640             </tgroup>
2641           </informaltable>
2642         </para>
2643       </sect3>
2644     </sect2>
2645
2646     <sect2 id="message-bus-routing">
2647       <title>Message Bus Message Routing</title>
2648       <para>
2649         FIXME 
2650       </para>
2651     </sect2>
2652     <sect2 id="message-bus-starting-services">
2653       <title>Message Bus Starting Services</title>
2654       <para>
2655         The message bus can start applications on behalf of other applications.
2656         In CORBA terms, this would be called <firstterm>activation</firstterm>.
2657         An application that can be started in this way is called a
2658         <firstterm>service</firstterm>.
2659       </para>
2660       <para>
2661         With D-BUS, starting a service is normally done by name. That is,
2662         applications ask the message bus to start some program that will own a
2663         well-known name, such as <literal>org.freedesktop.TextEditor</literal>.
2664         This implies a contract documented along with the name 
2665         <literal>org.freedesktop.TextEditor</literal> for which objects 
2666         the owner of that name will provide, and what interfaces those 
2667         objects will have.
2668       </para>
2669       <para>
2670         To find an executable corresponding to a particular name, the bus daemon
2671         looks for <firstterm>service description files</firstterm>.  Service
2672         description files define a mapping from names to executables. Different
2673         kinds of message bus will look for these files in different places, see
2674         <xref linkend="message-bus-types"/>.
2675       </para>
2676       <para>
2677         [FIXME the file format should be much better specified than "similar to
2678         .desktop entries" esp. since desktop entries are already
2679         badly-specified. ;-)] Service description files have the ".service" file
2680         extension. The message bus will only load service description files
2681         ending with .service; all other files will be ignored.  The file format
2682         is similar to that of <ulink
2683         url="http://www.freedesktop.org/standards/desktop-entry-spec/desktop-entry-spec.html">desktop
2684         entries</ulink>. All service description files must be in UTF-8
2685         encoding. To ensure that there will be no name collisions, service files
2686         must be namespaced using the same mechanism as messages and service
2687         names.
2688
2689         <figure>
2690           <title>Example service description file</title>
2691           <programlisting>
2692             # Sample service description file
2693             [D-BUS Service]
2694             Names=org.freedesktop.ConfigurationDatabase;org.gnome.GConf;
2695             Exec=/usr/libexec/gconfd-2
2696           </programlisting>
2697         </figure>
2698       </para>
2699       <para>
2700         When an application asks to start a service by name, the bus daemon tries to
2701         find a service that will own that name. It then tries to spawn the
2702         executable associated with it. If this fails, it will report an
2703         error. [FIXME what happens if two .service files offer the same service;
2704         what kind of error is reported, should we have a way for the client to
2705         choose one?]
2706       </para>
2707       <para>
2708         The executable launched will have the environment variable
2709         <literal>DBUS_STARTER_ADDRESS</literal> set to the address of the
2710         message bus so it can connect and request the appropriate names.
2711       </para>
2712       <para>
2713         The executable being launched may want to know whether the message bus
2714         starting it is one of the well-known message buses (see <xref
2715         linkend="message-bus-types"/>). To facilitate this, the bus must also set
2716         the <literal>DBUS_STARTER_BUS_TYPE</literal> environment variable if it is one
2717         of the well-known buses. The currently-defined values for this variable
2718         are <literal>system</literal> for the systemwide message bus,
2719         and <literal>session</literal> for the per-login-session message
2720         bus. The new executable must still connect to the address given
2721         in <literal>DBUS_STARTER_ADDRESS</literal>, but may assume that the
2722         resulting connection is to the well-known bus.
2723       </para>
2724       <para>
2725         [FIXME there should be a timeout somewhere, either specified
2726         in the .service file, by the client, or just a global value
2727         and if the client being activated fails to connect within that
2728         timeout, an error should be sent back.]
2729       </para>
2730
2731       <sect3 id="message-bus-starting-services-scope">
2732         <title>Message Bus Service Scope</title>
2733         <para>
2734           The "scope" of a service is its "per-", such as per-session,
2735           per-machine, per-home-directory, or per-display. The reference
2736           implementation doesn't yet support starting services in a different
2737           scope from the message bus itself. So e.g. if you start a service
2738           on the session bus its scope is per-session.
2739         </para>
2740         <para>
2741           We could add an optional scope to a bus name. For example, for
2742           per-(display,session pair), we could have a unique ID for each display
2743           generated automatically at login and set on screen 0 by executing a
2744           special "set display ID" binary. The ID would be stored in a
2745           <literal>_DBUS_DISPLAY_ID</literal> property and would be a string of
2746           random bytes. This ID would then be used to scope names.
2747           Starting/locating a service could be done by ID-name pair rather than
2748           only by name.
2749         </para>
2750         <para>
2751           Contrast this with a per-display scope. To achieve that, we would 
2752           want a single bus spanning all sessions using a given display.
2753           So we might set a <literal>_DBUS_DISPLAY_BUS_ADDRESS</literal> 
2754           property on screen 0 of the display, pointing to this bus.
2755         </para>
2756       </sect3>
2757     </sect2>
2758
2759     <sect2 id="message-bus-types">
2760       <title>Well-known Message Bus Instances</title>
2761       <para>
2762         Two standard message bus instances are defined here, along with how 
2763         to locate them and where their service files live.
2764       </para>
2765       <sect3 id="message-bus-types-login">
2766         <title>Login session message bus</title>
2767         <para>
2768           Each time a user logs in, a <firstterm>login session message
2769             bus</firstterm> may be started. All applications in the user's login
2770           session may interact with one another using this message bus.
2771         </para>
2772         <para>
2773           The address of the login session message bus is given 
2774           in the <literal>DBUS_SESSION_BUS_ADDRESS</literal> environment 
2775           variable. If that variable is not set, applications may 
2776           also try to read the address from the X Window System root 
2777           window property <literal>_DBUS_SESSION_BUS_ADDRESS</literal>.
2778           The root window property must have type <literal>STRING</literal>.
2779           The environment variable should have precedence over the 
2780           root window property.
2781         </para>
2782         <para>
2783           [FIXME specify location of .service files, probably using 
2784           DESKTOP_DIRS etc. from basedir specification, though login session 
2785           bus is not really desktop-specific]
2786         </para>
2787       </sect3>
2788       <sect3 id="message-bus-types-system">
2789         <title>System message bus</title>
2790         <para>
2791           A computer may have a <firstterm>system message bus</firstterm>,
2792           accessible to all applications on the system. This message bus may be
2793           used to broadcast system events, such as adding new hardware devices, 
2794           changes in the printer queue, and so forth.
2795         </para>
2796         <para>
2797           The address of the system message bus is given 
2798           in the <literal>DBUS_SYSTEM_BUS_ADDRESS</literal> environment 
2799           variable. If that variable is not set, applications should try 
2800           to connect to the well-known address
2801           <literal>unix:path=/var/run/dbus/system_bus_socket</literal>.
2802           <footnote>
2803             <para>
2804               The D-BUS reference implementation actually honors the 
2805               <literal>$(localstatedir)</literal> configure option 
2806               for this address, on both client and server side.
2807             </para>
2808           </footnote>
2809         </para>
2810         <para>
2811           [FIXME specify location of system bus .service files]
2812         </para>
2813       </sect3>
2814     </sect2>
2815
2816     <sect2 id="message-bus-messages">
2817       <title>Message Bus Messages</title>
2818       <para>
2819         The special message bus name <literal>org.freedesktop.DBus</literal>
2820         responds to a number of additional messages.
2821       </para>
2822
2823       <sect3 id="bus-messages-hello">
2824         <title><literal>org.freedesktop.DBus.Hello</literal></title>
2825         <para>
2826           As a method:
2827           <programlisting>
2828             STRING Hello ()
2829           </programlisting>
2830           Reply arguments:
2831           <informaltable>
2832             <tgroup cols="3">
2833               <thead>
2834                 <row>
2835                   <entry>Argument</entry>
2836                   <entry>Type</entry>
2837                   <entry>Description</entry>
2838                 </row>
2839               </thead>
2840               <tbody>
2841                 <row>
2842                   <entry>0</entry>
2843                   <entry>STRING</entry>
2844                   <entry>Unique name assigned to the connection</entry>
2845                 </row>
2846               </tbody>
2847             </tgroup>
2848           </informaltable>
2849         </para>
2850         <para>
2851           Before an application is able to send messages to other applications
2852           it must send the <literal>org.freedesktop.DBus.Hello</literal> message
2853           to the message bus to obtain a unique name. If an application without
2854           a unique name tries to send a message to another application, or a
2855           message to the message bus itself that isn't the
2856           <literal>org.freedesktop.DBus.Hello</literal> message, it will be
2857           disconnected from the bus.
2858         </para>
2859         <para>
2860           There is no corresponding "disconnect" request; if a client wishes to
2861           disconnect from the bus, it simply closes the socket (or other 
2862           communication channel).
2863         </para>
2864       </sect3>
2865       <sect3 id="bus-messages-list-names">
2866         <title><literal>org.freedesktop.DBus.ListNames</literal></title>
2867         <para>
2868           As a method:
2869           <programlisting>
2870             ARRAY of STRING ListNames ()
2871           </programlisting>
2872           Reply arguments:
2873           <informaltable>
2874             <tgroup cols="3">
2875               <thead>
2876                 <row>
2877                   <entry>Argument</entry>
2878                   <entry>Type</entry>
2879                   <entry>Description</entry>
2880                 </row>
2881               </thead>
2882               <tbody>
2883                 <row>
2884                   <entry>0</entry>
2885                   <entry>ARRAY of STRING</entry>
2886                   <entry>Array of strings where each string is a bus name</entry>
2887                 </row>
2888               </tbody>
2889             </tgroup>
2890           </informaltable>
2891         </para>
2892         <para>
2893           Returns a list of all currently-owned names on the bus.
2894         </para>
2895       </sect3>
2896       <sect3 id="bus-messages-name-exists">
2897         <title><literal>org.freedesktop.DBus.NameHasOwner</literal></title>
2898         <para>
2899           As a method:
2900           <programlisting>
2901             BOOLEAN NameHasOwner (in STRING name)
2902           </programlisting>
2903           Message arguments:
2904           <informaltable>
2905             <tgroup cols="3">
2906               <thead>
2907                 <row>
2908                   <entry>Argument</entry>
2909                   <entry>Type</entry>
2910                   <entry>Description</entry>
2911                 </row>
2912               </thead>
2913               <tbody>
2914                 <row>
2915                   <entry>0</entry>
2916                   <entry>STRING</entry>
2917                   <entry>Name to check</entry>
2918                 </row>
2919               </tbody>
2920             </tgroup>
2921           </informaltable>
2922           Reply arguments:
2923           <informaltable>
2924             <tgroup cols="3">
2925               <thead>
2926                 <row>
2927                   <entry>Argument</entry>
2928                   <entry>Type</entry>
2929                   <entry>Description</entry>
2930                 </row>
2931               </thead>
2932               <tbody>
2933                 <row>
2934                   <entry>0</entry>
2935                   <entry>BOOLEAN</entry>
2936                   <entry>Return value, true if the name exists</entry>
2937                 </row>
2938               </tbody>
2939             </tgroup>
2940           </informaltable>
2941         </para>
2942         <para>
2943           Checks if the specified name exists (currently has an owner).
2944         </para>
2945       </sect3>
2946
2947       <sect3 id="bus-messages-name-owner-changed">
2948         <title><literal>org.freedesktop.DBus.NameOwnerChanged</literal></title>
2949         <para>
2950           This is a signal:
2951           <programlisting>
2952             NameOwnerChanged (STRING name, STRING old_owner, STRING new_owner)
2953           </programlisting>
2954           Message arguments:
2955           <informaltable>
2956             <tgroup cols="3">
2957               <thead>
2958                 <row>
2959                   <entry>Argument</entry>
2960                   <entry>Type</entry>
2961                   <entry>Description</entry>
2962                 </row>
2963               </thead>
2964               <tbody>
2965                 <row>
2966                   <entry>0</entry>
2967                   <entry>STRING</entry>
2968                   <entry>Name with a new owner</entry>
2969                 </row>
2970                 <row>
2971                   <entry>1</entry>
2972                   <entry>STRING</entry>
2973                   <entry>Old owner or empty string if none</entry>
2974                 </row>
2975                 <row>
2976                   <entry>2</entry>
2977                   <entry>STRING</entry>
2978                   <entry>New owner or empty string if none</entry>
2979                 </row>
2980               </tbody>
2981             </tgroup>
2982           </informaltable>
2983         </para>
2984         <para>
2985           This signal indicates that the owner of a name has changed.
2986           It's also the signal to use to detect the appearance of 
2987           new names on the bus.
2988         </para>
2989       </sect3>
2990       <sect3 id="bus-messages-name-lost">
2991         <title><literal>org.freedesktop.DBus.NameLost</literal></title>
2992         <para>
2993           This is a signal:
2994           <programlisting>
2995             NameLost (STRING name)
2996           </programlisting>
2997           Message arguments:
2998           <informaltable>
2999             <tgroup cols="3">
3000               <thead>
3001                 <row>
3002                   <entry>Argument</entry>
3003                   <entry>Type</entry>
3004                   <entry>Description</entry>
3005                 </row>
3006               </thead>
3007               <tbody>
3008                 <row>
3009                   <entry>0</entry>
3010                   <entry>STRING</entry>
3011                   <entry>Name which was lost</entry>
3012                 </row>
3013               </tbody>
3014             </tgroup>
3015           </informaltable>
3016         </para>
3017         <para>
3018           This signal is sent to a specific application when it loses
3019           ownership of a name.
3020         </para>
3021       </sect3>
3022
3023       <sect3 id="bus-messages-name-acquired">
3024         <title><literal>org.freedesktop.DBus.NameAcquired</literal></title>
3025         <para>
3026           This is a signal:
3027           <programlisting>
3028             NameAcquired (STRING name)
3029           </programlisting>
3030           Message arguments:
3031           <informaltable>
3032             <tgroup cols="3">
3033               <thead>
3034                 <row>
3035                   <entry>Argument</entry>
3036                   <entry>Type</entry>
3037                   <entry>Description</entry>
3038                 </row>
3039               </thead>
3040               <tbody>
3041                 <row>
3042                   <entry>0</entry>
3043                   <entry>STRING</entry>
3044                   <entry>Name which was acquired</entry>
3045                 </row>
3046               </tbody>
3047             </tgroup>
3048           </informaltable>
3049         </para>
3050         <para>
3051           This signal is sent to a specific application when it gains
3052           ownership of a name.
3053         </para>
3054       </sect3>
3055
3056       <sect3 id="bus-messages-start-service-by-name">
3057         <title><literal>org.freedesktop.DBus.StartServiceByName</literal></title>
3058         <para>
3059           As a method:
3060           <programlisting>
3061             UINT32 StartServiceByName (in STRING name, in UINT32 flags)
3062           </programlisting>
3063           Message arguments:
3064           <informaltable>
3065             <tgroup cols="3">
3066               <thead>
3067                 <row>
3068                   <entry>Argument</entry>
3069                   <entry>Type</entry>
3070                   <entry>Description</entry>
3071                 </row>
3072               </thead>
3073               <tbody>
3074                 <row>
3075                   <entry>0</entry>
3076                   <entry>STRING</entry>
3077                   <entry>Name of the service to start</entry>
3078                 </row>
3079                 <row>
3080                   <entry>1</entry>
3081                   <entry>UINT32</entry>
3082                   <entry>Flags (currently not used)</entry>
3083                 </row>
3084               </tbody>
3085             </tgroup>
3086           </informaltable>
3087         Reply arguments:
3088         <informaltable>
3089           <tgroup cols="3">
3090             <thead>
3091               <row>
3092                 <entry>Argument</entry>
3093                 <entry>Type</entry>
3094                 <entry>Description</entry>
3095               </row>
3096             </thead>
3097             <tbody>
3098               <row>
3099                 <entry>0</entry>
3100                 <entry>UINT32</entry>
3101                 <entry>Return value</entry>
3102               </row>
3103             </tbody>
3104           </tgroup>
3105         </informaltable>
3106           Tries to launch the executable associated with a name. For more information, see <xref linkend="message-bus-starting-services"/>.
3107
3108         </para>
3109         <para>
3110           The return value can be one of the following values:
3111           <informaltable>
3112             <tgroup cols="3">
3113               <thead>
3114                 <row>
3115                   <entry>Identifier</entry>
3116                   <entry>Value</entry>
3117                   <entry>Description</entry>
3118                 </row>
3119               </thead>
3120               <tbody>
3121                 <row>
3122                   <entry>DBUS_START_REPLY_SUCCESS</entry>
3123                   <entry>1</entry>
3124                   <entry>The service was successfully started.</entry>
3125                 </row>
3126                 <row>
3127                   <entry>DBUS_START_REPLY_ALREADY_RUNNING</entry>
3128                   <entry>2</entry>
3129                   <entry>A connection already owns the given name.</entry>
3130                 </row>
3131               </tbody>
3132              </tgroup>
3133            </informaltable>
3134         </para>
3135
3136       </sect3>
3137
3138       <sect3 id="bus-messages-get-name-owner">
3139         <title><literal>org.freedesktop.DBus.GetNameOwner</literal></title>
3140         <para>
3141           As a method:
3142           <programlisting>
3143             STRING GetNameOwner (in STRING name)
3144           </programlisting>
3145           Message arguments:
3146           <informaltable>
3147             <tgroup cols="3">
3148               <thead>
3149                 <row>
3150                   <entry>Argument</entry>
3151                   <entry>Type</entry>
3152                   <entry>Description</entry>
3153                 </row>
3154               </thead>
3155               <tbody>
3156                 <row>
3157                   <entry>0</entry>
3158                   <entry>STRING</entry>
3159                   <entry>Name to get the owner of</entry>
3160                 </row>
3161               </tbody>
3162             </tgroup>
3163           </informaltable>
3164         Reply arguments:
3165         <informaltable>
3166           <tgroup cols="3">
3167             <thead>
3168               <row>
3169                 <entry>Argument</entry>
3170                 <entry>Type</entry>
3171                 <entry>Description</entry>
3172               </row>
3173             </thead>
3174             <tbody>
3175               <row>
3176                 <entry>0</entry>
3177                 <entry>STRING</entry>
3178                 <entry>Return value, a unique connection name</entry>
3179               </row>
3180             </tbody>
3181           </tgroup>
3182         </informaltable>
3183         Returns the unique connection name of the primary owner of the name
3184         given. If the requested name doesn't have an owner, returns a
3185         <literal>org.freedesktop.DBus.Error.NameHasNoOwner</literal> error.
3186        </para>
3187       </sect3>
3188
3189       <sect3 id="bus-messages-get-connection-unix-user">
3190         <title><literal>org.freedesktop.DBus.GetConnectionUnixUser</literal></title>
3191         <para>
3192           As a method:
3193           <programlisting>
3194             UINT32 GetConnectionUnixUser (in STRING connection_name)
3195           </programlisting>
3196           Message arguments:
3197           <informaltable>
3198             <tgroup cols="3">
3199               <thead>
3200                 <row>
3201                   <entry>Argument</entry>
3202                   <entry>Type</entry>
3203                   <entry>Description</entry>
3204                 </row>
3205               </thead>
3206               <tbody>
3207                 <row>
3208                   <entry>0</entry>
3209                   <entry>STRING</entry>
3210                   <entry>Name of the connection to query</entry>
3211                 </row>
3212               </tbody>
3213             </tgroup>
3214           </informaltable>
3215         Reply arguments:
3216         <informaltable>
3217           <tgroup cols="3">
3218             <thead>
3219               <row>
3220                 <entry>Argument</entry>
3221                 <entry>Type</entry>
3222                 <entry>Description</entry>
3223               </row>
3224             </thead>
3225             <tbody>
3226               <row>
3227                 <entry>0</entry>
3228                 <entry>UINT32</entry>
3229                 <entry>unix user id</entry>
3230               </row>
3231             </tbody>
3232           </tgroup>
3233         </informaltable>
3234         Returns the unix uid of the process connected to the server. If unable to
3235         determine it, a <literal>org.freedesktop.DBus.Error.Failed</literal>
3236         error is returned.
3237        </para>
3238       </sect3>
3239
3240     </sect2>
3241
3242   </sect1>
3243 <!--
3244   <appendix id="implementation-notes">
3245     <title>Implementation notes</title>
3246     <sect1 id="implementation-notes-subsection">
3247       <title></title>
3248       <para>
3249       </para>
3250     </sect1>
3251   </appendix>
3252 -->
3253
3254   <glossary><title>Glossary</title>
3255     <para>
3256       This glossary defines some of the terms used in this specification.
3257     </para>
3258
3259     <glossentry id="term-bus-name"><glossterm>Bus Name</glossterm>
3260       <glossdef>
3261         <para>
3262           The message bus maintains an association between names and
3263           connections. (Normally, there's one connection per application.)  A
3264           bus name is simply an identifier used to locate connections. For
3265           example, the hypothetical <literal>com.yoyodyne.Screensaver</literal>
3266           name might be used to send a message to a screensaver from Yoyodyne
3267           Corporation.  An application is said to <firstterm>own</firstterm> a
3268           name if the message bus has associated the application's connection
3269           with the name.  Names may also have <firstterm>queued
3270           owners</firstterm> (see <xref linkend="term-queued-owner"/>).
3271             The bus assigns a unique name to each connection, 
3272             see <xref linkend="term-unique-name"/>. Other names 
3273               can be thought of as "well-known names" and are 
3274               used to find applications that offer specific functionality.
3275         </para>
3276       </glossdef>
3277     </glossentry>
3278       
3279     <glossentry id="term-message"><glossterm>Message</glossterm>
3280       <glossdef>
3281         <para>
3282           A message is the atomic unit of communication via the D-BUS
3283           protocol. It consists of a <firstterm>header</firstterm> and a
3284           <firstterm>body</firstterm>; the body is made up of
3285           <firstterm>arguments</firstterm>.
3286         </para>
3287       </glossdef>
3288     </glossentry>
3289
3290     <glossentry id="term-message-bus"><glossterm>Message Bus</glossterm>
3291       <glossdef>
3292         <para>
3293           The message bus is a special application that forwards 
3294           or routes messages between a group of applications
3295           connected to the message bus. It also manages 
3296           <firstterm>names</firstterm> used for routing
3297           messages.
3298         </para>
3299       </glossdef>
3300     </glossentry>
3301
3302     <glossentry id="term-name"><glossterm>Name</glossterm>
3303       <glossdef>
3304         <para>
3305           See <xref linkend="term-bus-name"/>. "Name" may 
3306             also be used to refer to some of the other names
3307             in D-BUS, such as interface names.
3308         </para>
3309       </glossdef>
3310     </glossentry>
3311
3312     <glossentry id="namespace"><glossterm>Namespace</glossterm>
3313       <glossdef>
3314         <para>
3315           Used to prevent collisions when defining new interfaces or bus
3316           names. The convention used is the same one Java uses for defining
3317           classes: a reversed domain name.
3318         </para>
3319       </glossdef>
3320     </glossentry>
3321
3322     <glossentry id="term-object"><glossterm>Object</glossterm>
3323       <glossdef>
3324         <para>
3325           Each application contains <firstterm>objects</firstterm>, which have
3326           <firstterm>interfaces</firstterm> and
3327           <firstterm>methods</firstterm>. Objects are referred to by a name,
3328           called a <firstterm>path</firstterm>.
3329         </para>
3330       </glossdef>
3331     </glossentry>
3332
3333     <glossentry id="one-to-one"><glossterm>One-to-One</glossterm>
3334       <glossdef>
3335         <para>
3336           An application talking directly to another application, without going
3337           through a message bus. One-to-one connections may be "peer to peer" or
3338           "client to server." The D-BUS protocol has no concept of client
3339           vs. server after a connection has authenticated; the flow of messages
3340           is symmetrical (full duplex).
3341         </para>
3342       </glossdef>
3343     </glossentry>
3344
3345     <glossentry id="term-path"><glossterm>Path</glossterm>
3346       <glossdef>
3347         <para>
3348           Object references (object names) in D-BUS are organized into a
3349           filesystem-style hierarchy, so each object is named by a path. As in
3350           LDAP, there's no difference between "files" and "directories"; a path
3351           can refer to an object, while still having child objects below it.
3352         </para>
3353       </glossdef>
3354     </glossentry>
3355
3356     <glossentry id="term-queued-owner"><glossterm>Queued Name Owner</glossterm>
3357       <glossdef>
3358         <para>
3359           Each bus name has a primary owner; messages sent to the name go to the
3360           primary owner. However, certain names also maintain a queue of
3361           secondary owners "waiting in the wings." If the primary owner releases
3362           the name, then the first secondary owner in the queue automatically
3363           becomes the new owner of the name.
3364         </para>
3365       </glossdef>
3366     </glossentry>
3367
3368     <glossentry id="term-service"><glossterm>Service</glossterm>
3369       <glossdef>
3370         <para>
3371           A service is an executable that can be launched by the bus daemon.
3372           Services normally guarantee some particular features, for example they
3373           may guarantee that they will request a specific name such as
3374           "org.freedesktop.Screensaver", have a singleton object
3375           "/org/freedesktop/Application", and that object will implement the
3376           interface "org.freedesktop.ScreensaverControl".
3377         </para>
3378       </glossdef>
3379     </glossentry>
3380
3381     <glossentry id="term-service-description-files"><glossterm>Service Description Files</glossterm>
3382       <glossdef>
3383         <para>
3384           ".service files" tell the bus about service applications that can be
3385           launched (see <xref linkend="term-service"/>). Most importantly they
3386           provide a mapping from bus names to services that will request those
3387             names when they start up.
3388         </para>
3389       </glossdef>
3390     </glossentry>
3391
3392     <glossentry id="term-unique-name"><glossterm>Unique Connection Name</glossterm>
3393       <glossdef>
3394         <para>
3395           The special name automatically assigned to each connection by the
3396           message bus. This name will never change owner, and will be unique
3397           (never reused during the lifetime of the message bus).
3398           It will begin with a ':' character.
3399         </para>
3400       </glossdef>
3401     </glossentry>
3402
3403   </glossary>
3404 </article>
3405