-
Notifications
You must be signed in to change notification settings - Fork 15
/
oBus_wire.ml
1332 lines (1162 loc) · 44.3 KB
/
oBus_wire.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* oBus_lowlevel.ml
* ----------------
* Copyright : (c) 2008, Jeremie Dimino <jeremie@dimino.org>
* Licence : BSD3
*
* This file is a part of obus, an ocaml implementation of D-Bus.
*)
let section = Lwt_log.Section.make "obus(wire)"
open Printf
open Lwt
open OBus_value
open OBus_message
open OBus_protocol
(* +-----------------------------------------------------------------+
| Errors |
+-----------------------------------------------------------------+ *)
exception Data_error of string
exception Protocol_error of string
let () =
Printexc.register_printer
(function
| Data_error msg ->
Some(sprintf "failed to marshal D-Bus message: %s" msg)
| Protocol_error msg ->
Some(sprintf "D-Bus protocol error: %s" msg)
| _ ->
None)
(* Common error message *)
let array_too_big len = sprintf "array size exceed the limit: %d" len
let message_too_big len = sprintf "message size exceed the limit: %d" len
let signature_too_long s len = sprintf "too long signature: '%s', with len %d" (string_of_signature s) len
let invalid_protocol_version ver = sprintf "invalid protocol version: %d (obus implement protocol version %d)" ver OBus_info.protocol_version
let invalid_byte_order ch = sprintf "invalid byte order(%C)" ch
(* +-----------------------------------------------------------------+
| Padding |
+-----------------------------------------------------------------+ *)
let padding2 i = i land 1
let padding4 i = (4 - i) land 3
let padding8 i = (8 - i) land 7
let pad2 i = i + padding2 i
let pad4 i = i + padding4 i
let pad8 i = i + padding8 i
let pad8_p = function
| T.Structure _
| T.Basic T.Int64
| T.Basic T.Uint64
| T.Basic T.Double -> true
| _ -> false
(* +-----------------------------------------------------------------+
| Raw description of header fields |
+-----------------------------------------------------------------+ *)
type raw_fields = {
mutable rf_path : OBus_path.t option;
mutable rf_member : OBus_name.member;
mutable rf_interface : OBus_name.interface;
mutable rf_error_name : OBus_name.error;
mutable rf_reply_serial : serial option;
mutable rf_destination : OBus_name.bus;
mutable rf_sender : OBus_name.bus;
mutable rf_signature : signature;
mutable rf_unix_fds : int;
}
let missing_field message_type_name field_name =
raise (Protocol_error(sprintf "invalid header, field '%s' is required for '%s'"
field_name message_type_name))
let get_required_string message_type_name field_name = function
| "" ->
missing_field message_type_name field_name
| string ->
string
let get_required_option message_type_name field_name = function
| None ->
missing_field message_type_name field_name
| Some value ->
value
let method_call_of_raw fields =
Method_call(get_required_option "method-call" "path" fields.rf_path,
fields.rf_interface,
get_required_string "method-call" "member" fields.rf_member)
let method_return_of_raw fields =
Method_return(get_required_option "method-return" "reply-serial" fields.rf_reply_serial)
let error_of_raw fields =
Error(get_required_option "error" "reply-serial" fields.rf_reply_serial,
get_required_string "error" "error-name" fields.rf_error_name)
let signal_of_raw fields =
Signal(get_required_option "signal" "path" fields.rf_path,
get_required_string "signal" "interface" fields.rf_interface,
get_required_string "signal" "member" fields.rf_member)
(* +-----------------------------------------------------------------+
| Error mapping |
+-----------------------------------------------------------------+ *)
(* Maps error returned by [OBus_*.*] to [Data_error] or
[Protocol_error]: *)
let map_exn f = function
| OBus_string.Invalid_string err ->
raise (f (OBus_string.error_message err))
| OBus_value.Invalid_signature(str, msg) ->
raise (f (Printf.sprintf "invalid signature (%S): %s" str msg))
| exn ->
raise exn
let data_error msg = Data_error msg
let protocol_error msg = Protocol_error msg
(* +-----------------------------------------------------------------+
| Message size calculation |
+-----------------------------------------------------------------+ *)
module FD_set = Set.Make(struct type t = Unix.file_descr let compare = compare end)
module Count =
struct
(* The goal of this module is to compute the marshaled size of a
message, and the number of different file descriptors it
contains. *)
type counter = {
mutable ofs : int;
(* Simulate an offset *)
mutable fds : FD_set.t;
(* Set used to collect all file descriptors *)
}
let path_length = function
| [] -> 1
| l -> List.fold_left (fun acc x -> 1 + String.length x + acc) 0 l
let rec iter f c = function
| [] -> ()
| x :: l -> f c x; iter f c l
let rec tsingle c = function
| T.Basic _ ->
c.ofs <- c.ofs + 1
| T.Array t ->
c.ofs <- c.ofs + 1;
tsingle c t
| T.Dict(tk, tv) ->
c.ofs <- c.ofs + 4;
tsingle c tv
| T.Structure l ->
c.ofs <- c.ofs + 2;
iter tsingle c l
| T.Variant ->
c.ofs <- c.ofs + 1
let tsequence c l =
iter tsingle c l
let rec tsingle_of_single c = function
| V.Basic x ->
c.ofs <- c.ofs + 1
| V.Array(t, x) ->
c.ofs <- c.ofs + 1;
tsingle c t
| V.Byte_array _ ->
c.ofs <- c.ofs + 2
| V.Dict(tk, tv, x) ->
c.ofs <- c.ofs + 4;
tsingle c tv
| V.Structure l ->
c.ofs <- c.ofs + 2;
iter tsingle_of_single c l
| V.Variant x ->
c.ofs <- c.ofs + 1
let tsequence_of_sequence c l =
iter tsingle_of_single c l
let rec basic c = function
| V.Byte _ ->
c.ofs <- c.ofs + 1
| V.Int16 _
| V.Uint16 _ ->
c.ofs <- pad2 c.ofs + 2
| V.Boolean _
| V.Int32 _
| V.Uint32 _ ->
c.ofs <- pad4 c.ofs + 4
| V.Int64 _
| V.Uint64 _
| V.Double _ ->
c.ofs <- pad8 c.ofs + 8
| V.String s ->
c.ofs <- pad4 c.ofs + String.length s + 5
| V.Signature s ->
c.ofs <- c.ofs + 2;
tsequence c s
| V.Object_path p ->
c.ofs <- pad4 c.ofs + path_length p + 5
| V.Unix_fd fd ->
c.ofs <- pad4 c.ofs + 4;
c.fds <- FD_set.add fd c.fds
let rec single c = function
| V.Basic x ->
basic c x
| V.Array(t, l) ->
c.ofs <- pad4 c.ofs + 4;
if pad8_p t then c.ofs <- pad8 c.ofs;
iter single c l
| V.Byte_array bytes ->
c.ofs <- pad4 c.ofs + 4 + String.length bytes
| V.Dict(tk, tv, l) ->
c.ofs <- pad8 (pad4 c.ofs + 4);
iter dict_entry c l
| V.Structure l ->
c.ofs <- pad8 c.ofs;
iter single c l
| V.Variant x ->
c.ofs <- c.ofs + 2;
tsingle_of_single c x;
single c x
and dict_entry c (k, v) =
c.ofs <- pad8 c.ofs;
basic c k;
single c v
let sequence c l =
iter single c l
let message msg =
let c = { ofs = 16; fds = FD_set.empty } in
begin match msg.typ with
| Method_call(path, "", member) ->
(* +9 for:
- the code (1)
- the signature of one basic type code (3)
- the string length (4)
- the null byte (1) *)
c.ofs <- pad8 c.ofs + 9 + path_length path;
c.ofs <- pad8 c.ofs + 9 + String.length member
| Method_call(path, interface, member)
| Signal(path, interface, member) ->
c.ofs <- pad8 c.ofs + 9 + path_length path;
c.ofs <- pad8 c.ofs + 9 + String.length interface;
c.ofs <- pad8 c.ofs + 9 + String.length member
| Method_return serial ->
c.ofs <- pad8 c.ofs + 8
| Error(serial, name) ->
c.ofs <- pad8 c.ofs + 9 + String.length name;
c.ofs <- pad8 c.ofs + 8
end;
if msg.destination <> "" then
c.ofs <- pad8 c.ofs + 9 + String.length msg.destination;
if msg.sender <> "" then
c.ofs <- pad8 c.ofs + 9 + String.length msg.sender;
(* The signature *)
c.ofs <- pad8 c.ofs + 6;
tsequence_of_sequence c msg.body;
(* The number of fds: *)
c.ofs <- pad8 c.ofs + 8;
(* The message body: *)
sequence c msg.body;
c
end
(* +-----------------------------------------------------------------+
| Unsafe writing of integers |
+-----------------------------------------------------------------+ *)
let put_char = Bytes.unsafe_set
let put_uint8 buf ofs x = put_char buf ofs (Char.unsafe_chr x)
module type Integer_writers = sig
val put_int16 : bytes -> int -> int -> unit
val put_int32 : bytes -> int -> int32 -> unit
val put_int64 : bytes -> int -> int64 -> unit
val put_uint16 : bytes -> int -> int -> unit
val put_uint32 : bytes -> int -> int32 -> unit
val put_uint64 : bytes -> int -> int64 -> unit
val put_uint : bytes -> int -> int -> unit
end
module LE_integer_writers : Integer_writers =
struct
let put_int16 buf ofs v =
put_uint8 buf (ofs + 0) v;
put_uint8 buf (ofs + 1) (v lsr 8)
let put_uint16 = put_int16
let put_int32 buf ofs v =
put_uint8 buf (ofs + 0) (Int32.to_int v);
put_uint8 buf (ofs + 1) (Int32.to_int (Int32.shift_right v 8));
put_uint8 buf (ofs + 2) (Int32.to_int (Int32.shift_right v 16));
put_uint8 buf (ofs + 3) (Int32.to_int (Int32.shift_right v 24))
let put_uint32 = put_int32
let put_int64 buf ofs v =
put_uint8 buf (ofs + 0) (Int64.to_int v);
put_uint8 buf (ofs + 1) (Int64.to_int (Int64.shift_right v 8));
put_uint8 buf (ofs + 2) (Int64.to_int (Int64.shift_right v 16));
put_uint8 buf (ofs + 3) (Int64.to_int (Int64.shift_right v 24));
put_uint8 buf (ofs + 4) (Int64.to_int (Int64.shift_right v 32));
put_uint8 buf (ofs + 5) (Int64.to_int (Int64.shift_right v 40));
put_uint8 buf (ofs + 6) (Int64.to_int (Int64.shift_right v 48));
put_uint8 buf (ofs + 7) (Int64.to_int (Int64.shift_right v 56))
let put_uint64 = put_int64
let put_uint buf ofs v =
put_uint8 buf (ofs + 0) v;
put_uint8 buf (ofs + 1) (v lsr 8);
put_uint8 buf (ofs + 2) (v lsr 16);
put_uint8 buf (ofs + 3) (v asr 24)
end
module BE_integer_writers : Integer_writers =
struct
let put_int16 buf ofs v =
put_uint8 buf (ofs + 0) (v lsr 8);
put_uint8 buf (ofs + 1) v
let put_uint16 = put_int16
let put_int32 buf ofs v =
put_uint8 buf (ofs + 0) (Int32.to_int (Int32.shift_right v 24));
put_uint8 buf (ofs + 1) (Int32.to_int (Int32.shift_right v 16));
put_uint8 buf (ofs + 2) (Int32.to_int (Int32.shift_right v 8));
put_uint8 buf (ofs + 3) (Int32.to_int v)
let put_uint32 = put_int32
let put_int64 buf ofs v =
put_uint8 buf (ofs + 0) (Int64.to_int (Int64.shift_right v 56));
put_uint8 buf (ofs + 1) (Int64.to_int (Int64.shift_right v 48));
put_uint8 buf (ofs + 2) (Int64.to_int (Int64.shift_right v 40));
put_uint8 buf (ofs + 3) (Int64.to_int (Int64.shift_right v 32));
put_uint8 buf (ofs + 4) (Int64.to_int (Int64.shift_right v 24));
put_uint8 buf (ofs + 5) (Int64.to_int (Int64.shift_right v 16));
put_uint8 buf (ofs + 6) (Int64.to_int (Int64.shift_right v 8));
put_uint8 buf (ofs + 7) (Int64.to_int v)
let put_uint64 = put_int64
let put_uint buf ofs v =
put_uint8 buf (ofs + 0) (v asr 24);
put_uint8 buf (ofs + 1) (v lsr 16);
put_uint8 buf (ofs + 2) (v lsr 8);
put_uint8 buf (ofs + 3) v
end
(* +-----------------------------------------------------------------+
| Unsafe reading of integers |
+-----------------------------------------------------------------+ *)
let get_char = String.unsafe_get
let get_uint8 buf ofs = Char.code (get_char buf ofs)
module type Integer_readers = sig
val get_int16 : string -> int -> int
val get_int32 : string -> int -> int32
val get_int64 : string -> int -> int64
val get_uint16 : string -> int -> int
val get_uint32 : string -> int -> int32
val get_uint64 : string -> int -> int64
val get_uint : string -> int -> int
end
module LE_integer_readers : Integer_readers =
struct
let get_int16 buf ofs =
let v0 = get_uint8 buf (ofs + 0)
and v1 = get_uint8 buf (ofs + 1) in
let v = v0 lor (v1 lsl 8) in
if v land (1 lsl 15) = 0 then
v
else
((-1 land (lnot 0x7fff)) lor v)
let get_uint16 buf ofs =
let v0 = get_uint8 buf (ofs + 0)
and v1 = get_uint8 buf (ofs + 1) in
(v0 lor (v1 lsl 8))
let get_int32 buf ofs =
let v0 = get_uint8 buf (ofs + 0)
and v1 = get_uint8 buf (ofs + 1)
and v2 = get_uint8 buf (ofs + 2)
and v3 = get_uint8 buf (ofs + 3) in
(Int32.logor
(Int32.logor
(Int32.of_int v0)
(Int32.shift_left (Int32.of_int v1) 8))
(Int32.logor
(Int32.shift_left (Int32.of_int v2) 16)
(Int32.shift_left (Int32.of_int v3) 24)))
let get_uint32 = get_int32
let get_int64 buf ofs =
let v0 = get_uint8 buf (ofs + 0)
and v1 = get_uint8 buf (ofs + 1)
and v2 = get_uint8 buf (ofs + 2)
and v3 = get_uint8 buf (ofs + 3)
and v4 = get_uint8 buf (ofs + 4)
and v5 = get_uint8 buf (ofs + 5)
and v6 = get_uint8 buf (ofs + 6)
and v7 = get_uint8 buf (ofs + 7) in
(Int64.logor
(Int64.logor
(Int64.logor
(Int64.of_int v0)
(Int64.shift_left (Int64.of_int v1) 8))
(Int64.logor
(Int64.shift_left (Int64.of_int v2) 16)
(Int64.shift_left (Int64.of_int v3) 24)))
(Int64.logor
(Int64.logor
(Int64.shift_left (Int64.of_int v4) 32)
(Int64.shift_left (Int64.of_int v5) 40))
(Int64.logor
(Int64.shift_left (Int64.of_int v6) 48)
(Int64.shift_left (Int64.of_int v7) 56))))
let get_uint64 = get_int64
let get_uint buf ofs =
let v0 = get_uint8 buf (ofs + 0)
and v1 = get_uint8 buf (ofs + 1)
and v2 = get_uint8 buf (ofs + 2)
and v3 = get_uint8 buf (ofs + 3) in
(v0 lor (v1 lsl 8) lor (v2 lsl 16) lor (v3 lsl 24))
end
module BE_integer_readers : Integer_readers =
struct
let get_int16 buf ofs =
let v1 = get_uint8 buf (ofs + 0)
and v0 = get_uint8 buf (ofs + 1) in
let v = v0 lor (v1 lsl 8) in
if v land (1 lsl 15) = 0 then
v
else
((-1 land (lnot 0x7fff)) lor v)
let get_uint16 buf ofs =
let v1 = get_uint8 buf (ofs + 0)
and v0 = get_uint8 buf (ofs + 1) in
(v0 lor (v1 lsl 8))
let get_int32 buf ofs =
let v3 = get_uint8 buf (ofs + 0)
and v2 = get_uint8 buf (ofs + 1)
and v1 = get_uint8 buf (ofs + 2)
and v0 = get_uint8 buf (ofs + 3) in
(Int32.logor
(Int32.logor
(Int32.of_int v0)
(Int32.shift_left (Int32.of_int v1) 8))
(Int32.logor
(Int32.shift_left (Int32.of_int v2) 16)
(Int32.shift_left (Int32.of_int v3) 24)))
let get_uint32 = get_int32
let get_int64 buf ofs =
let v7 = get_uint8 buf (ofs + 0)
and v6 = get_uint8 buf (ofs + 1)
and v5 = get_uint8 buf (ofs + 2)
and v4 = get_uint8 buf (ofs + 3)
and v3 = get_uint8 buf (ofs + 4)
and v2 = get_uint8 buf (ofs + 5)
and v1 = get_uint8 buf (ofs + 6)
and v0 = get_uint8 buf (ofs + 7) in
(Int64.logor
(Int64.logor
(Int64.logor
(Int64.of_int v0)
(Int64.shift_left (Int64.of_int v1) 8))
(Int64.logor
(Int64.shift_left (Int64.of_int v2) 16)
(Int64.shift_left (Int64.of_int v3) 24)))
(Int64.logor
(Int64.logor
(Int64.shift_left (Int64.of_int v4) 32)
(Int64.shift_left (Int64.of_int v5) 40))
(Int64.logor
(Int64.shift_left (Int64.of_int v6) 48)
(Int64.shift_left (Int64.of_int v7) 56))))
let get_uint64 = get_int64
let get_uint buf ofs =
let v3 = get_uint8 buf (ofs + 0)
and v2 = get_uint8 buf (ofs + 1)
and v1 = get_uint8 buf (ofs + 2)
and v0 = get_uint8 buf (ofs + 3) in
(v0 lor (v1 lsl 8) lor (v2 lsl 16) lor (v3 lsl 24))
end
(* +---------------------------------------------------------------+
| Common writing functions |
+---------------------------------------------------------------+ *)
module FD_map = Map.Make(struct type t = Unix.file_descr let compare = Pervasives.compare end)
(* A pointer for serializing data *)
type wpointer = {
buf : bytes;
mutable ofs : int;
max : int;
fds : int FD_map.t;
(* Maps file descriptros to their index in the resulting fds
array *)
}
let write_padding2 ptr =
if ptr.ofs land 1 = 1 then begin
put_uint8 ptr.buf ptr.ofs 0;
ptr.ofs <- ptr.ofs + 1
end
let write_padding4 ptr =
for k = 1 to padding4 ptr.ofs do
put_uint8 ptr.buf ptr.ofs 0;
ptr.ofs <- ptr.ofs + 1
done
let write_padding8 ptr =
for k = 1 to padding8 ptr.ofs do
put_uint8 ptr.buf ptr.ofs 0;
ptr.ofs <- ptr.ofs + 1
done
let write1 writer ptr value =
writer ptr.buf ptr.ofs value;
ptr.ofs <- ptr.ofs + 1
let write2 writer ptr value =
write_padding2 ptr;
writer ptr.buf ptr.ofs value;
ptr.ofs <- ptr.ofs + 2
let write4 writer ptr value =
write_padding4 ptr;
writer ptr.buf ptr.ofs value;
ptr.ofs <- ptr.ofs + 4
let write8 writer ptr value =
write_padding8 ptr;
writer ptr.buf ptr.ofs value;
ptr.ofs <- ptr.ofs + 8
let write_bytes ptr value =
let len = String.length value in
String.unsafe_blit value 0 ptr.buf ptr.ofs len;
ptr.ofs <- ptr.ofs + len
(* +-----------------------------------------------------------------+
| Message writing |
+-----------------------------------------------------------------+ *)
module Make_writer(Integer_writers : Integer_writers) =
struct
open Integer_writers
let write_uint8 ptr value = write1 put_uint8 ptr value
let write_uint ptr value = write4 put_uint ptr value
(* Serialize one string, without verifying it *)
let write_string_no_check ptr string =
write_uint ptr (String.length string);
write_bytes ptr string;
write_uint8 ptr 0
(* Serialize a signature. *)
let write_signature ptr signature =
let string = OBus_value.string_of_signature signature in
write_uint8 ptr (String.length string);
write_bytes ptr string;
write_uint8 ptr 0
let write_object_path ptr path =
write_string_no_check ptr (OBus_path.to_string path)
let write_basic ptr = function
| V.Byte x -> write1 put_char ptr x
| V.Boolean x -> write4 put_uint ptr (match x with true -> 1 | false -> 0)
| V.Int16 x -> write2 put_int16 ptr x
| V.Int32 x -> write4 put_int32 ptr x
| V.Int64 x -> write8 put_int64 ptr x
| V.Uint16 x -> write2 put_uint16 ptr x
| V.Uint32 x -> write4 put_uint32 ptr x
| V.Uint64 x -> write8 put_uint64 ptr x
| V.Double x -> write8 put_uint64 ptr (Int64.bits_of_float x)
| V.String x -> begin match OBus_string.validate x with
| Some error ->
raise (Data_error(OBus_string.error_message error))
| None ->
write_string_no_check ptr x
end
| V.Signature x -> write_signature ptr x
| V.Object_path x -> write_object_path ptr x
| V.Unix_fd fd -> write4 put_uint ptr (FD_map.find fd ptr.fds)
let rec write_array ptr padded_on_8 write_element values =
(* Array are serialized as follow:
(1) padding to a 4-block alignement (for array size)
(2) array size
(3) alignement to array elements padding (even if the array is empty)
(4) serialized elements
The array size (2) is the size of serialized elements (4) *)
(* Write the padding *)
write_padding4 ptr;
(* Save the position where to write the length of the array: *)
let length_ofs = ptr.ofs in
(* Allocate 4 bytes for the length: *)
ptr.ofs <- ptr.ofs + 4;
(* After the size we are always padded on 4, so we only need to
add padding if elements padding is 8: *)
if padded_on_8 then write_padding8 ptr;
(* Save the position of the beginning of the elements of the
array: *)
let start_ofs = ptr.ofs in
List.iter (fun x -> write_element ptr x) values;
let length = ptr.ofs - start_ofs in
if length < 0 || length > max_array_size then raise (Data_error(array_too_big length));
(* Write the array length: *)
put_uint ptr.buf length_ofs length
let rec write_dict_entry ptr (k, v) =
(* Dict-entries are serialized as follow:
(1) alignement on a 8-block
(2) serialized key
(3) serialized value *)
write_padding8 ptr;
write_basic ptr k;
write_single ptr v
and write_single ptr = function
| V.Basic x ->
write_basic ptr x
| V.Array(t, x) ->
write_array ptr (pad8_p t) write_single x
| V.Byte_array s ->
write_uint ptr (String.length s);
write_bytes ptr s
| V.Dict(tk, tv, x) ->
write_array ptr true write_dict_entry x
| V.Structure x ->
(* Structure are serialized as follow:
(1) alignement to an 8-block
(2) serialized contents *)
write_padding8 ptr;
write_sequence ptr x
| V.Variant x ->
(* Variant are serialized as follow:
(1) marshaled variant signature
(2) serialized contents *)
write_signature ptr [OBus_value.V.type_of_single x];
write_single ptr x
and write_sequence ptr = function
| [] ->
()
| x :: l ->
write_single ptr x;
write_sequence ptr l
(* Header field ptr *)
let write_field_real ptr code typ writer value =
(* Each header field is a structure, so we need to be aligned on 8 *)
write_padding8 ptr;
write_uint8 ptr code;
write_signature ptr [T.Basic typ];
writer ptr value
(* Write a field if defined *)
let write_field ptr code typ writer = function
| None ->
()
| Some value ->
write_field_real ptr code typ writer value
(* Validate and write a field if defined *)
let write_name_field ptr code test = function
| "" ->
()
| string ->
match test string with
| Some error ->
raise (Data_error(OBus_string.error_message error))
| None ->
write_field_real ptr code T.String write_string_no_check string
(* Serialize one complete message *)
let write_message byte_order_char msg =
let { Count.ofs = size; Count.fds = fds } = Count.message msg in
if size > max_message_size then raise (Data_error(message_too_big size));
let buffer = Bytes.create size in
let ptr = {
buf = buffer;
ofs = 16;
max = size;
fds = snd (FD_set.fold (fun fd (n, map) -> (n + 1, FD_map.add fd n map)) fds (0, FD_map.empty));
} in
let fd_count = FD_set.cardinal fds in
(* Compute ``raw'' headers *)
let code, fields = match msg.typ with
| Method_call(path, interface, member) ->
if member = "" then raise (Data_error "invalid method-call message: field 'member' is empty");
(1,
{ rf_path = Some path;
rf_interface = interface;
rf_member = member;
rf_error_name = "";
rf_reply_serial = None;
rf_destination = msg.destination;
rf_sender = msg.sender;
rf_signature = V.type_of_sequence msg.body;
rf_unix_fds = fd_count })
| Method_return reply_serial ->
(2,
{ rf_path = None;
rf_interface = "";
rf_member = "";
rf_error_name = "";
rf_reply_serial = Some reply_serial;
rf_destination = msg.destination;
rf_sender = msg.sender;
rf_signature = V.type_of_sequence msg.body;
rf_unix_fds = fd_count })
| Error(reply_serial, error_name) ->
if error_name = "" then raise (Data_error "invalid error message: field 'error-name' is empty");
(3,
{ rf_path = None;
rf_interface = "";
rf_member = "";
rf_error_name = error_name;
rf_reply_serial = Some reply_serial;
rf_destination = msg.destination;
rf_sender = msg.sender;
rf_signature = V.type_of_sequence msg.body;
rf_unix_fds = fd_count })
| Signal(path, interface, member) ->
if interface = "" then raise (Data_error "invalid signal message, field 'interface' is empty");
if member = "" then raise (Data_error "invalid signal message, field 'member' is empty");
(4,
{ rf_path = Some path;
rf_interface = interface;
rf_member = member;
rf_error_name = "";
rf_reply_serial = None;
rf_destination = msg.destination;
rf_sender = msg.sender;
rf_signature = V.type_of_sequence msg.body;
rf_unix_fds = fd_count })
in
write_field ptr 1 T.Object_path write_object_path fields.rf_path;
write_name_field ptr 2 OBus_name.validate_interface fields.rf_interface;
write_name_field ptr 3 OBus_name.validate_member fields.rf_member;
write_name_field ptr 4 OBus_name.validate_error fields.rf_error_name;
write_field ptr 5 T.Uint32 (write4 put_uint32) fields.rf_reply_serial;
write_name_field ptr 6 OBus_name.validate_bus fields.rf_destination;
write_name_field ptr 7 OBus_name.validate_bus fields.rf_sender;
write_field_real ptr 8 T.Signature write_signature fields.rf_signature;
write_field_real ptr 9 T.Uint32 (write4 put_uint) fields.rf_unix_fds;
let fields_length = ptr.ofs - 16 in
if fields_length < 0 || fields_length > max_array_size then
raise (Data_error(array_too_big fields_length));
(* The message body start aligned on an 8-boundary after the
header: *)
write_padding8 ptr;
let start_ofs = ptr.ofs in
(* Write the message body *)
write_sequence ptr msg.body;
let body_length = ptr.ofs - start_ofs in
(* byte #0 : byte-order *)
put_char buffer 0 byte_order_char;
(* byte #1 : message type code *)
put_uint8 buffer 1 code;
(* byte #2 : message flags *)
put_uint8 buffer 2
((if msg.flags.no_reply_expected then 1 else 0) lor
(if msg.flags.no_auto_start then 2 else 0));
(* byte #3 : protocol version *)
put_uint8 buffer 3 OBus_info.protocol_version;
(* byte #4-7 : body length *)
put_uint buffer 4 body_length;
(* byte #8-11 : serial *)
put_uint32 buffer 8 msg.serial;
(* byte #12-15 : fields length *)
put_uint buffer 12 fields_length;
(* Create the array of file descriptors *)
let fds = Array.create fd_count Unix.stdin in
FD_map.iter (fun fd index -> Array.unsafe_set fds index fd) ptr.fds;
(Bytes.unsafe_to_string ptr.buf, fds)
end
module LE_writer = Make_writer(LE_integer_writers)
module BE_writer = Make_writer(BE_integer_writers)
let string_of_message ?(byte_order=Lwt_io.system_byte_order) msg =
try
match byte_order with
| Lwt_io.Little_endian ->
LE_writer.write_message 'l' msg
| Lwt_io.Big_endian ->
BE_writer.write_message 'B' msg
with exn ->
raise (map_exn data_error exn)
let write_message oc ?byte_order msg =
match string_of_message ?byte_order msg with
| str, [||] ->
Lwt_io.write oc str
| _ ->
raise_lwt (Data_error "Cannot send a message with file descriptors on a channel")
type writer = {
w_channel : Lwt_io.output_channel;
w_file_descr : Lwt_unix.file_descr;
}
let close_writer writer = Lwt_io.close writer.w_channel
let writer fd = {
w_channel = Lwt_io.of_fd ~mode:Lwt_io.output ~close:return fd;
w_file_descr = fd;
}
let write_message_with_fds writer ?byte_order msg =
match string_of_message ?byte_order msg with
| buf, [||] ->
(* No file descriptor to send, simply use the channel *)
Lwt_io.write writer.w_channel buf
| buf, fds ->
Lwt_io.atomic begin fun oc ->
(* Ensures there is nothing left to send: *)
lwt () = Lwt_io.flush oc in
let len = String.length buf in
(* Send the file descriptors and the message: *)
lwt n = Lwt_unix.send_msg writer.w_file_descr [Lwt_unix.io_vector buf 0 len] (Array.to_list fds) in
assert (n >= 0 && n <= len);
(* Write what is remaining: *)
Lwt_io.write_from_string_exactly oc buf n (len - n)
end writer.w_channel
(* +-----------------------------------------------------------------+
| Common reading operations |
+-----------------------------------------------------------------+ *)
(* A pointer for unserializing data *)
type rpointer = {
buf : string;
mutable ofs : int;
max : int;
mutable fds : Unix.file_descr array;
(* The array of file descriptors received with the message *)
}
let out_of_bounds () = raise (Protocol_error "out of bounds")
let unitialized_padding () = raise (Protocol_error "unitialized padding")
let read_padding ptr count =
for i = 1 to count do
if get_uint8 ptr.buf ptr.ofs <> 0 then unitialized_padding ();
ptr.ofs <- ptr.ofs + 1
done
let read_padding2 ptr =
if padding2 ptr.ofs = 1 then begin
if ptr.ofs + 1 > ptr.max then out_of_bounds ();
if get_uint8 ptr.buf ptr.ofs <> 0 then unitialized_padding ()
end
let read_padding4 ptr =
let padding = padding4 ptr.ofs in
if ptr.ofs + padding > ptr.max then out_of_bounds ();
read_padding ptr padding
let read_padding8 ptr =
let padding = padding8 ptr.ofs in
if ptr.ofs + padding > ptr.max then out_of_bounds ();
read_padding ptr padding
let read1 reader ptr =
if ptr.ofs + 1 > ptr.max then out_of_bounds ();
let x = reader ptr.buf ptr.ofs in
ptr.ofs <- ptr.ofs + 1;
x
let read2 reader ptr =
let padding = padding2 ptr.ofs in
if ptr.ofs + padding + 2 > ptr.max then out_of_bounds ();
read_padding ptr padding;
let x = reader ptr.buf ptr.ofs in
ptr.ofs <- ptr.ofs + 2;
x
let read4 reader ptr =
let padding = padding4 ptr.ofs in
if ptr.ofs + padding + 4 > ptr.max then out_of_bounds ();
read_padding ptr padding;
let x = reader ptr.buf ptr.ofs in
ptr.ofs <- ptr.ofs + 4;
x
let read8 reader ptr =
let padding = padding8 ptr.ofs in
if ptr.ofs + padding + 8 > ptr.max then out_of_bounds ();
read_padding ptr padding;
let x = reader ptr.buf ptr.ofs in
ptr.ofs <- ptr.ofs + 8;
x
let read_bytes ptr len =
if len < 0 || ptr.ofs + len > ptr.max then out_of_bounds ();
let s = Bytes.create len in
String.unsafe_blit ptr.buf ptr.ofs s 0 len;
ptr.ofs <- ptr.ofs + len;
Bytes.unsafe_to_string s
(* +-----------------------------------------------------------------+
| Message reading |
+-----------------------------------------------------------------+ *)
module Make_reader(Integer_readers : Integer_readers) =
struct
open Integer_readers
let read_uint ptr = read4 get_uint ptr
let read_uint8 ptr = read1 get_uint8 ptr
let read_string_no_check ptr =
let len = read_uint ptr in
let x = read_bytes ptr len in
if read_uint8 ptr <> 0 then raise (Protocol_error "missing string terminal null byte");
x
let read_signature ptr =
let len = read_uint8 ptr in
let x = read_bytes ptr len in
if read_uint8 ptr <> 0 then raise (Protocol_error "missing signature terminating null byte");
OBus_value.signature_of_string x
let read_object_path ptr =
let str = read_string_no_check ptr in
OBus_path.of_string str
let read_vbyte ptr = V.Byte(read1 get_char ptr)
let read_vboolean ptr = match read_uint ptr with
| 0 -> V.Boolean false
| 1 -> V.Boolean true
| n -> raise (Protocol_error(sprintf "invalid boolean value: %d" n))
let read_vint16 ptr = V.Int16(read2 get_int16 ptr)
let read_vint32 ptr = V.Int32(read4 get_int32 ptr)
let read_vint64 ptr = V.Int64(read8 get_int64 ptr)
let read_vuint16 ptr = V.Uint16(read2 get_uint16 ptr)
let read_vuint32 ptr = V.Uint32(read4 get_uint32 ptr)
let read_vuint64 ptr = V.Uint64(read8 get_uint64 ptr)
let read_vdouble ptr = V.Double(Int64.float_of_bits (read8 get_uint64 ptr))
let read_vstring ptr =
let str = read_string_no_check ptr in
match OBus_string.validate str with
| None -> V.String str
| Some error -> raise (Protocol_error(OBus_string.error_message error))
let read_vsignature ptr = V.Signature(read_signature ptr)
let read_vobject_path ptr = V.Object_path(read_object_path ptr)
let read_unix_fd ptr =
let index = read4 get_uint ptr in
if index < 0 || index >= Array.length ptr.fds then
raise (Protocol_error "fd index out of bounds")