summaryrefslogtreecommitdiffstats
path: root/lib/tsocket/tsocket.h
blob: 3aca536124172c3f58842359dfd09769d75b664e (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
/*
   Unix SMB/CIFS implementation.

   Copyright (C) Stefan Metzmacher 2009

     ** NOTE! The following LGPL license applies to the tsocket
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _TSOCKET_H
#define _TSOCKET_H

#include <talloc.h>
#include <tevent.h>

struct tsocket_address;
struct tdgram_context;
struct tstream_context;
struct iovec;

/**
 * @mainpage
 *
 * The tsocket abstraction is an API ...
 */

/**
 * @defgroup tsocket The tsocket API
 *
 * The tsocket abstraction is split into two different kinds of
 * communication interfaces.
 *
 * There's the "tstream_context" interface with abstracts the communication
 * through a bidirectional byte stream between two endpoints.
 *
 * And there's the "tdgram_context" interface with abstracts datagram based
 * communication between any number of endpoints.
 *
 * Both interfaces share the "tsocket_address" abstraction for endpoint
 * addresses.
 *
 * The whole library is based on the talloc(3) and 'tevent' libraries and
 * provides "tevent_req" based "foo_send()"/"foo_recv()" functions pairs for
 * all abstracted methods that need to be async.
 *
 * @section vsock Virtual Sockets
 *
 * The abstracted layout of tdgram_context and tstream_context allow
 * implementations around virtual sockets for encrypted tunnels (like TLS,
 * SASL or GSSAPI) or named pipes over smb.
 *
 * @section npa Named Pipe Auth (NPA) Sockets
 *
 * Samba has an implementation to abstract named pipes over smb (within the
 * server side). See libcli/named_pipe_auth/npa_tstream.[ch] for the core code.
 * The current callers are located in source4/ntvfs/ipc/vfs_ipc.c and
 * source4/rpc_server/service_rpc.c for the users.
 */

/**
 * @defgroup tsocket_address The tsocket_address abstraction
 * @ingroup tsocket
 *
 * The tsocket_address represents an socket endpoint genericly.
 * As it's like an abstract class it has no specific constructor.
 * The specific constructors are descripted in later sections.
 *
 * @{
 */

/**
 * @brief Get a string representation of the endpoint.
 *
 * This function creates a string representation of the endpoint for debugging.
 * The output will look as followed:
 *      prefix:address:port
 *
 * e.g.
 *      ipv4:192.168.1.1:143
 *
 * Callers should not try to parse the string! The should use additional methods
 * of the specific tsocket_address implemention to get more details.
 *
 * @param[in]  addr     The address to convert.
 *
 * @param[in]  mem_ctx  The talloc memory context to allocate the memory.
 *
 * @return              The address as a string representation, NULL on error.
 *
 * @see tsocket_address_is_inet()
 * @see tsocket_address_inet_addr_string()
 * @see tsocket_address_inet_port()
 */
char *tsocket_address_string(const struct tsocket_address *addr,
			     TALLOC_CTX *mem_ctx);

#ifdef DOXYGEN
/**
 * @brief This creates a copy of a tsocket_address.
 *
 * This is useful when before doing modifications to a socket via additional
 * methods of the specific tsocket_address implementation.
 *
 * @param[in]  addr     The address to create the copy from.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @return              A newly allocated copy of addr (tsocket_address *), NULL
 *                      on error.
 */
struct tsocket_address *tsocket_address_copy(const struct tsocket_address *addr,
		TALLOC_CTX *mem_ctx);
#else
struct tsocket_address *_tsocket_address_copy(const struct tsocket_address *addr,
					      TALLOC_CTX *mem_ctx,
					      const char *location);

#define tsocket_address_copy(addr, mem_ctx) \
	_tsocket_address_copy(addr, mem_ctx, __location__)
#endif

/**
 * @}
 */

/**
 * @defgroup tdgram_context The tdgram_context abstraction
 * @ingroup tsocket
 *
 * The tdgram_context is like an abstract class for datagram based sockets. The
 * interface provides async 'tevent_req' based functions on top functionality
 * is similar to the recvfrom(2)/sendto(2)/close(2) syscalls.
 *
 * @note You can always use talloc_free(tdgram) to cleanup the resources
 * of the tdgram_context on a fatal error.
 * @{
 */

/**
 * @brief Ask for next available datagram on the abstracted tdgram_context.
 *
 * It returns a 'tevent_req' handle, where the caller can register
 * a callback with tevent_req_set_callback(). The callback is triggered
 * when a datagram is available or an error happened.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  ev       The tevent_context to run on.
 *
 * @param[in]  dgram    The dgram context to work on.
 *
 * @return              Returns a 'tevent_req' handle, where the caller can
 *                      register a callback with tevent_req_set_callback().
 *                      NULL on fatal error.
 *
 * @see tdgram_inet_udp_socket()
 * @see tdgram_unix_socket()
 */
struct tevent_req *tdgram_recvfrom_send(TALLOC_CTX *mem_ctx,
					struct tevent_context *ev,
					struct tdgram_context *dgram);

/**
 * @brief Receive the next available datagram on the abstracted tdgram_context.
 *
 * This function should be called by the callback when a datagram is available
 * or an error happened.
 *
 * The caller can only have one outstanding tdgram_recvfrom_send() at a time
 * otherwise the caller will get '*perrno = EBUSY'.
 *
 * @param[in]  req      The tevent request from tdgram_recvfrom_send().
 *
 * @param[out] perrno   The error number, set if an error occurred.
 *
 * @param[in]  mem_ctx  The memory context to use.
 *
 * @param[out] buf      This will hold the buffer of the datagram.
 *
 * @param[out] src      The abstracted tsocket_address of the sender of the
 *                      received datagram.
 *
 * @return              The length of the datagram (0 is never returned!),
 *                      -1 on error with perrno set to the actual errno.
 *
 * @see tdgram_recvfrom_send()
 */
ssize_t tdgram_recvfrom_recv(struct tevent_req *req,
			     int *perrno,
			     TALLOC_CTX *mem_ctx,
			     uint8_t **buf,
			     struct tsocket_address **src);

/**
 * @brief Send a datagram to a destination endpoint.
 *
 * The function can be called to send a datagram (specified by a buf/len) to a
 * destination endpoint (specified by dst). It's not allowed for len to be 0.
 *
 * It returns a 'tevent_req' handle, where the caller can register a callback
 * with tevent_req_set_callback(). The callback is triggered when the specific
 * implementation (assumes it) has delivered the datagram to the "wire".
 *
 * The callback is then supposed to get the result by calling
 * tdgram_sendto_recv() on the 'tevent_req'.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  ev       The tevent_context to run on.
 *
 * @param[in]  dgram    The dgram context to work on.
 *
 * @param[in]  buf      The buffer to send.
 *
 * @param[in]  len      The length of the buffer to send. It has to be bigger
 *                      than 0.
 *
 * @param[in]  dst      The destination to send the datagram to in form of a
 *                      tsocket_address.
 *
 * @return              Returns a 'tevent_req' handle, where the caller can
 *                      register a callback with tevent_req_set_callback().
 *                      NULL on fatal error.
 *
 * @see tdgram_inet_udp_socket()
 * @see tdgram_unix_socket()
 * @see tdgram_sendto_recv()
 */
struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
				      struct tevent_context *ev,
				      struct tdgram_context *dgram,
				      const uint8_t *buf, size_t len,
				      const struct tsocket_address *dst);

/**
 * @brief Receive the result of the sent datagram.
 *
 * The caller can only have one outstanding tdgram_sendto_send() at a time
 * otherwise the caller will get '*perrno = EBUSY'.
 *
 * @param[in]  req      The tevent request from tdgram_sendto_send().
 *
 * @param[out] perrno   The error number, set if an error occurred.
 *
 * @return              The length of the datagram (0 is never returned!), -1 on
 *                      error with perrno set to the actual errno.
 *
 * @see tdgram_sendto_send()
 */
ssize_t tdgram_sendto_recv(struct tevent_req *req,
			   int *perrno);

/**
 * @brief Shutdown/close an abstracted socket.
 *
 * It returns a 'tevent_req' handle, where the caller can register a callback
 * with tevent_req_set_callback(). The callback is triggered when the specific
 * implementation (assumes it) has delivered the datagram to the "wire".
 *
 * The callback is then supposed to get the result by calling
 * tdgram_sendto_recv() on the 'tevent_req'.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  ev       The tevent_context to run on.
 *
 * @param[in]  dgram    The dgram context diconnect from.
 *
 * @return              Returns a 'tevent_req' handle, where the caller can
 *                      register a callback with tevent_req_set_callback().
 *                      NULL on fatal error.
 *
 * @see tdgram_disconnect_recv()
 */
struct tevent_req *tdgram_disconnect_send(TALLOC_CTX *mem_ctx,
					  struct tevent_context *ev,
					  struct tdgram_context *dgram);

/**
 * @brief Receive the result from a tdgram_disconnect_send() request.
 *
 * The caller should make sure there're no outstanding tdgram_recvfrom_send()
 * and tdgram_sendto_send() calls otherwise the caller will get
 * '*perrno = EBUSY'.
 *
 * @param[in]  req      The tevent request from tdgram_disconnect_send().
 *
 * @param[out] perrno   The error number, set if an error occurred.
 *
 * @return              The length of the datagram (0 is never returned!), -1 on
 *                      error with perrno set to the actual errno.
 *
 * @see tdgram_disconnect_send()
 */
int tdgram_disconnect_recv(struct tevent_req *req,
			   int *perrno);

/**
 * @}
 */

/**
 * @defgroup tstream_context The tstream_context abstraction
 * @ingroup tsocket
 *
 * The tstream_context is like an abstract class for stream based sockets. The
 * interface provides async 'tevent_req' based functions on top functionality
 * is similar to the readv(2)/writev(2)/close(2) syscalls.
 *
 * @note You can always use talloc_free(tstream) to cleanup the resources
 * of the tstream_context on a fatal error.
 *
 * @{
 */

/**
 * @brief Report the number of bytes received but not consumed yet.
 *
 * The tstream_pending_bytes() function reports how much bytes of the incoming
 * stream have been received but not consumed yet.
 *
 * @param[in]  stream   The tstream_context to check for pending bytes.
 *
 * @return              The number of bytes received, -1 on error with errno
 *                      set.
 */
ssize_t tstream_pending_bytes(struct tstream_context *stream);

/**
 * @brief Read a specific amount of bytes from a stream socket.
 *
 * The function can be called to read for a specific amount of bytes from the
 * stream into given buffers. The caller has to preallocate the buffers.
 *
 * The caller might need to use tstream_pending_bytes() if the protocol doesn't
 * have a fixed pdu header containing the pdu size.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  ev       The tevent_context to run on.
 *
 * @param[in]  stream   The tstream context to work on.
 *
 * @param[out] vector   A preallocated iovec to store the data to read.
 *
 * @param[in]  count    The number of buffers in the vector allocated.
 *
 * @return              A 'tevent_req' handle, where the caller can register
 *                      a callback with tevent_req_set_callback(). NULL on
 *                      fatal error.
 *
 * @see tstream_unix_connect_send()
 * @see tstream_inet_tcp_connect_send()
 */
struct tevent_req *tstream_readv_send(TALLOC_CTX *mem_ctx,
				      struct tevent_context *ev,
				      struct tstream_context *stream,
				      struct iovec *vector,
				      size_t count);

/**
 * @brief Get the result of a tstream_readv_send().
 *
 * The caller can only have one outstanding tstream_readv_send()
 * at a time otherwise the caller will get *perrno = EBUSY.
 *
 * @param[in]  req      The tevent request from tstream_readv_send().
 *
 * @param[out] perrno   The error number, set if an error occurred.
 *
 * @return              The length of the stream (0 is never returned!), -1 on
 *                      error with perrno set to the actual errno.
 */
int tstream_readv_recv(struct tevent_req *req,
		       int *perrno);

/**
 * @brief Write buffers from a vector into a stream socket.
 *
 * The function can be called to write buffers from a given vector
 * to a stream socket.
 *
 * You have to ensure that the vector is not empty.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  ev       The tevent_context to run on.
 *
 * @param[in]  stream   The tstream context to work on.
 *
 * @param[in]  vector   The iovec vector with data to write on a stream socket.
 *
 * @param[in]  count    The number of buffers in the vector to write.
 *
 * @return              A 'tevent_req' handle, where the caller can register
 *                      a callback with tevent_req_set_callback(). NULL on
 *                      fatal error.
 */
struct tevent_req *tstream_writev_send(TALLOC_CTX *mem_ctx,
				       struct tevent_context *ev,
				       struct tstream_context *stream,
				       const struct iovec *vector,
				       size_t count);

/**
 * @brief Get the result of a tstream_writev_send().
 *
 * The caller can only have one outstanding tstream_writev_send()
 * at a time otherwise the caller will get *perrno = EBUSY.
 *
 * @param[in]  req      The tevent request from tstream_writev_send().
 *
 * @param[out] perrno   The error number, set if an error occurred.
 *
 * @return              The length of the stream (0 is never returned!), -1 on
 *                      error with perrno set to the actual errno.
 */
int tstream_writev_recv(struct tevent_req *req,
			int *perrno);

/**
 * @brief Shutdown/close an abstracted socket.
 *
 * It returns a 'tevent_req' handle, where the caller can register a callback
 * with tevent_req_set_callback(). The callback is triggered when the specific
 * implementation (assumes it) has delivered the stream to the "wire".
 *
 * The callback is then supposed to get the result by calling
 * tdgram_sendto_recv() on the 'tevent_req'.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  ev       The tevent_context to run on.
 *
 * @param[in]  stream   The tstream context to work on.
 *
 * @return              A 'tevent_req' handle, where the caller can register
 *                      a callback with tevent_req_set_callback(). NULL on
 *                      fatal error.
 */
struct tevent_req *tstream_disconnect_send(TALLOC_CTX *mem_ctx,
					   struct tevent_context *ev,
					   struct tstream_context *stream);

/**
 * @brief Get the result of a tstream_disconnect_send().
 *
 * The caller can only have one outstanding tstream_writev_send()
 * at a time otherwise the caller will get *perrno = EBUSY.
 *
 * @param[in]  req      The tevent request from tstream_disconnect_send().
 *
 * @param[out] perrno   The error number, set if an error occurred.
 *
 * @return              The length of the stream (0 is never returned!), -1 on
 *                      error with perrno set to the actual errno.
 */
int tstream_disconnect_recv(struct tevent_req *req,
			    int *perrno);

/**
 * @}
 */


/**
 * @defgroup tsocket_bsd  tsocket_bsd - inet, inet6 and unix
 * @ingroup tsocket
 *
 * The main tsocket library comes with implentations for BSD style ipv4, ipv6
 * and unix sockets.
 *
 * @{
 */

/**
 * @brief Find out if the tsocket_address represents an ipv4 or ipv6 endpoint.
 *
 * @param[in]  addr     The tsocket_address pointer
 *
 * @param[in]  fam      The family can be can be "ipv4", "ipv6" or "ip". With
 *                      "ip" is autodetects "ipv4" or "ipv6" based on the
 *                      addr.
 *
 * @return              true if addr represents an address of the given family,
 *                      otherwise false.
 */
bool tsocket_address_is_inet(const struct tsocket_address *addr, const char *fam);

#if DOXYGEN
/**
 * @brief Create a tsocket_address for ipv4 and ipv6 endpoint addresses.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  fam      The family can be can be "ipv4", "ipv6" or "ip". With
 *                      "ip" is autodetects "ipv4" or "ipv6" based on the
 *                      addr.
 *
 * @param[in]  addr     A valid ip address string based on the selected family
 *                      (dns names are not allowed!). It's valid to pass NULL,
 *                      which gets mapped to "0.0.0.0" or "::".
 *
 * @param[in]  port     A valid port number.
 *
 * @param[out] _addr    A tsocket_address pointer to store the information.
 *
 * @return              0 on success, -1 on error with errno set.
 */
int tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
				      const char *fam,
				      const char *addr,
				      uint16_t port,
				      struct tsocket_address **_addr);
#else
int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
				       const char *fam,
				       const char *addr,
				       uint16_t port,
				       struct tsocket_address **_addr,
				       const char *location);

#define tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr) \
	_tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr, \
					   __location__)
#endif

/**
 * @brief Get the address of an 'inet' tsocket_address as a string.
 *
 * @param[in]  addr     The address to convert to a string.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @return              A newly allocated string of the address, NULL on error
 *                      with errno set.
 *
 * @see tsocket_address_is_inet()
 */
char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
				       TALLOC_CTX *mem_ctx);

/**
 * @brief Get the port number as an integer from an 'inet' tsocket_address.
 *
 * @param[in]  addr     The tsocket address to use.
 *
 * @return              The port number, 0 on error with errno set.
 */
uint16_t tsocket_address_inet_port(const struct tsocket_address *addr);

/**
 * @brief Set the port number of an existing 'inet' tsocket_address.
 *
 * @param[in]  addr     The existing tsocket_address to use.
 *
 * @param[in]  port     The valid port number to set.
 *
 * @return              0 on success, -1 on error with errno set.
 */
int tsocket_address_inet_set_port(struct tsocket_address *addr,
				  uint16_t port);

/**
 * @brief Find out if the tsocket_address represents an unix domain endpoint.
 *
 * @param[in]  addr     The tsocket_address pointer
 *
 * @return              true if addr represents an unix domain endpoint,
 *                      otherwise false.
 */
bool tsocket_address_is_unix(const struct tsocket_address *addr);

#ifdef DOXYGEN
/**
 * @brief Create a tsocket_address for a unix domain endpoint addresses.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  path     The filesystem path, NULL will map "".
 *
 * @param[in]  _addr    The tsocket_address pointer to store the information.
 *
 * @return              0 on success, -1 on error with errno set.
 *
 * @see tsocket_address_is_unix()
 */
int tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
				   const char *path,
				   struct tsocket_address **_addr);
#else
int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
				    const char *path,
				    struct tsocket_address **_addr,
				    const char *location);

#define tsocket_address_unix_from_path(mem_ctx, path, _addr) \
	_tsocket_address_unix_from_path(mem_ctx, path, _addr, \
					__location__)
#endif

/**
 * @brief Get the address of an 'unix' tsocket_address.
 *
 * @param[in]  addr     A valid 'unix' tsocket_address.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @return              The path of the unix domain socket, NULL on error or if
 *                      the tsocket_address doesn't represent an unix domain
 *                      endpoint path.
 */
char *tsocket_address_unix_path(const struct tsocket_address *addr,
				TALLOC_CTX *mem_ctx);

#ifdef DOXYGEN
/**
 * @brief Create a tdgram_context for a ipv4 or ipv6 UDP communication.
 *
 * @param[in]  local    An 'inet' tsocket_address for the local endpoint.
 *
 * @param[in]  remote   An 'inet' tsocket_address for the remote endpoint or
 *                      NULL (??? to create a listener?).
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  dgram    The tdgram_context pointer to setup the udp
 *                      communication. The function will allocate the memory.
 *
 * @return              0 on success, -1 on error with errno set.
 */
int tdgram_inet_udp_socket(const struct tsocket_address *local,
			    const struct tsocket_address *remote,
			    TALLOC_CTX *mem_ctx,
			    struct tdgram_context **dgram);
#else
int _tdgram_inet_udp_socket(const struct tsocket_address *local,
			    const struct tsocket_address *remote,
			    TALLOC_CTX *mem_ctx,
			    struct tdgram_context **dgram,
			    const char *location);
#define tdgram_inet_udp_socket(local, remote, mem_ctx, dgram) \
	_tdgram_inet_udp_socket(local, remote, mem_ctx, dgram, __location__)
#endif

#ifdef DOXYGEN
/**
 * @brief Create a tdgram_context for unix domain datagram communication.
 *
 * @param[in]  local    An 'unix' tsocket_address for the local endpoint.
 *
 * @param[in]  remote   An 'unix' tsocket_address for the remote endpoint or
 *                      NULL (??? to create a listener?).
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  dgram    The tdgram_context pointer to setup the udp
 *                      communication. The function will allocate the memory.
 *
 * @return              0 on success, -1 on error with errno set.
 */
int tdgram_unix_socket(const struct tsocket_address *local,
			const struct tsocket_address *remote,
			TALLOC_CTX *mem_ctx,
			struct tdgram_context **dgram);
#else
int _tdgram_unix_socket(const struct tsocket_address *local,
			const struct tsocket_address *remote,
			TALLOC_CTX *mem_ctx,
			struct tdgram_context **dgram,
			const char *location);

#define tdgram_unix_socket(local, remote, mem_ctx, dgram) \
	_tdgram_unix_socket(local, remote, mem_ctx, dgram, __location__)
#endif

/**
 * @brief Connect async to a TCP endpoint and create a tstream_context for the
 * stream based communication.
 *
 * Use this function to connenct asynchronously to a remote ipv4 or ipv6 TCP
 * endpoint and create a tstream_context for the stream based communication.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  ev       The tevent_context to run on.
 *
 * @param[in]  local    An 'inet' tsocket_address for the local endpoint.
 *
 * @param[in]  remote   An 'inet' tsocket_address for the remote endpoint.
 *
 * @return              A 'tevent_req' handle, where the caller can register a
 *                      callback with tevent_req_set_callback(). NULL on a fatal
 *                      error.
 *
 * @see tstream_inet_tcp_connect_recv()
 */
struct tevent_req *tstream_inet_tcp_connect_send(TALLOC_CTX *mem_ctx,
					struct tevent_context *ev,
					const struct tsocket_address *local,
					const struct tsocket_address *remote);

#ifdef DOXYGEN
/**
 * @brief Receive the result from a tstream_inet_tcp_connect_send().
 *
 * @param[in]  req      The tevent request from tstream_inet_tcp_connect_send().
 *
 * @param[out] perrno   The error number, set if an error occurred.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[out] stream   A tstream_context pointer to setup the tcp communication
 *                      on. This function will allocate the memory.
 *
 * @param[out] local    The real 'inet' tsocket_address of the local endpoint.
 *                      This parameter is optional and can be NULL.
 *
 * @return              0 on success, -1 on error with perrno set.
 */
int tstream_inet_tcp_connect_recv(struct tevent_req *req,
				  int *perrno,
				  TALLOC_CTX *mem_ctx,
				  struct tstream_context **stream,
				  struct tsocket_address **local)
#else
int _tstream_inet_tcp_connect_recv(struct tevent_req *req,
				   int *perrno,
				   TALLOC_CTX *mem_ctx,
				   struct tstream_context **stream,
				   struct tsocket_address **local,
				   const char *location);
#define tstream_inet_tcp_connect_recv(req, perrno, mem_ctx, stream, local) \
	_tstream_inet_tcp_connect_recv(req, perrno, mem_ctx, stream, local, \
				       __location__)
#endif

/**
 * @brief Connect async to a unix domain endpoint and create a tstream_context
 * for the stream based communication.
 *
 * Use this function to connenct asynchronously to a unix domainendpoint and
 * create a tstream_context for the stream based communication.
 *
 * The callback is triggered when a socket is connected and ready for IO or an
 * error happened.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  ev       The tevent_context to run on.
 *
 * @param[in]  local    An 'unix' tsocket_address for the local endpoint.
 *
 * @param[in]  remote   An 'unix' tsocket_address for the remote endpoint.
 *
 * @return              A 'tevent_req' handle, where the caller can register a
 *                      callback with tevent_req_set_callback(). NULL on a falal
 *                      error.
 *
 * @see tstream_unix_connect_recv()
 */
struct tevent_req * tstream_unix_connect_send(TALLOC_CTX *mem_ctx,
					struct tevent_context *ev,
					const struct tsocket_address *local,
					const struct tsocket_address *remote);

#ifdef DOXYGEN
/**
 * @brief Receive the result from a tstream_unix_connect_send().
 *
 * @param[in]  req      The tevent request from tstream_inet_tcp_connect_send().
 *
 * @param[out] perrno   The error number, set if an error occurred.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  stream   The tstream context to work on.
 *
 * @return              0 on success, -1 on error with perrno set.
 */
int tstream_unix_connect_recv(struct tevent_req *req,
			      int *perrno,
			      TALLOC_CTX *mem_ctx,
			      struct tstream_context **stream);
#else
int _tstream_unix_connect_recv(struct tevent_req *req,
			       int *perrno,
			       TALLOC_CTX *mem_ctx,
			       struct tstream_context **stream,
			       const char *location);
#define tstream_unix_connect_recv(req, perrno, mem_ctx, stream) \
	_tstream_unix_connect_recv(req, perrno, mem_ctx, stream, \
					  __location__)
#endif

#ifdef DOXYGEN
/**
 * @brief Create two connected 'unix' tsocket_contexts for stream based
 *        communication.
 *
 * @param[in]  mem_ctx1 The talloc memory context to use for stream1.
 *
 * @param[in]  stream1  The first stream to connect.
 *
 * @param[in]  mem_ctx2 The talloc memory context to use for stream2.
 *
 * @param[in]  stream2  The second stream to connect.
 *
 * @return              0 on success, -1 on error with errno set.
 */
int tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
			    struct tstream_context **stream1,
			    TALLOC_CTX *mem_ctx2,
			    struct tstream_context **stream2);
#else
int _tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
			     struct tstream_context **_stream1,
			     TALLOC_CTX *mem_ctx2,
			     struct tstream_context **_stream2,
			     const char *location);

#define tstream_unix_socketpair(mem_ctx1, stream1, mem_ctx2, stream2) \
	_tstream_unix_socketpair(mem_ctx1, stream1, mem_ctx2, stream2, \
				 __location__)
#endif

struct sockaddr;

#ifdef DOXYGEN
/**
 * @brief Convert a tsocket address to a bsd socket address.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  sa       The sockaddr structure to convert.
 *
 * @param[in]  sa_socklen   The lenth of the sockaddr sturucte.
 *
 * @param[out] addr     The tsocket pointer to allocate and fill.
 *
 * @return              0 on success, -1 on error with errno set.
 */
int tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx,
				      struct sockaddr *sa,
				      size_t sa_socklen,
				      struct tsocket_address **addr);
#else
int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx,
				       struct sockaddr *sa,
				       size_t sa_socklen,
				       struct tsocket_address **_addr,
				       const char *location);

#define tsocket_address_bsd_from_sockaddr(mem_ctx, sa, sa_socklen, _addr) \
	_tsocket_address_bsd_from_sockaddr(mem_ctx, sa, sa_socklen, _addr, \
					   __location__)
#endif

/**
 * @brief Fill a bsd sockaddr structure.
 *
 * @param[in]  addr     The tsocket address structure to use.
 *
 * @param[in]  sa       The bsd sockaddr structure to fill out.
 *
 * @param[in]  sa_socklen   The length of the  bsd sockaddr structure to fill out.
 *
 * @return              The actual size of the sockaddr structure, -1 on error
 *                      with errno set. The size could differ from sa_socklen.
 *
 * @code
 *   ssize_t socklen;
 *   struct sockaddr_storage ss;
 *
 *   socklen = tsocket_address_bsd_sockaddr(taddr,
 *                    (struct sockaddr *) &ss,
 *                    sizeof(struct sockaddr_storage));
 *   if (socklen < 0) {
 *     return -1;
 *   }
 * @endcode
 */
ssize_t tsocket_address_bsd_sockaddr(const struct tsocket_address *addr,
				     struct sockaddr *sa,
				     size_t sa_socklen);

#ifdef DOXYGEN
/**
 * @brief Wrap an existing file descriptors into the tstream abstraction.
 *
 * You can use this function to wrap an existing file descriptors into the
 * tstream abstraction. After that you're not able to use this file descriptor
 * for anything else. The file descriptor will be closed when the stream gets
 * freed. If you still want to use the fd you have have to create a duplicate.
 *
 * @param[in]  mem_ctx  The talloc memory context to use.
 *
 * @param[in]  fd       The non blocking fd to use!
 *
 * @param[out] stream   A pointer to store an allocated tstream_context.
 *
 * @return              0 on success, -1 on error.
 *
 * Example:
 * @code
 *   fd2 = dup(fd);
 *   rc = tstream_bsd_existing_socket(mem_ctx, fd2, &tstream);
 *   if (rc < 0) {
 *     stream_terminate_connection(conn, "named_pipe_accept: out of memory");
 *     return;
 *   }
 * @endcode
 *
 * @warning This is an internal function. You should read the code to fully
 *          understand it if you plan to use it.
 */
int tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
				int fd,
				struct tstream_context **stream);
#else
int _tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
				 int fd,
				 struct tstream_context **_stream,
				 const char *location);
#define tstream_bsd_existing_socket(mem_ctx, fd, stream) \
	_tstream_bsd_existing_socket(mem_ctx, fd, stream, \
				     __location__)
#endif

/**
 * @}
 */

/**
 * @defgroup tsocket_helper Queue and PDU helpers
 * @ingroup tsocket
 *
 * In order to make the live easier for callers which want to implement a
 * function to receive a full PDU with a single async function pair, there're
 * some helper functions.
 *
 * There're some cases where the caller wants doesn't care about the order of
 * doing IO on the abstracted sockets.
 *
 * @{
 */

/**
 * @brief Queue a dgram blob for sending through the socket.
 *
 * This function queues a blob for sending to destination through an existing
 * dgram socket. The async callback is triggered when the whole blob is
 * delivered to the underlying system socket.
 *
 * The caller needs to make sure that all non-scalar input parameters hang
 * around for the whole lifetime of the request.
 *
 * @param[in]  mem_ctx  The memory context for the result.
 *
 * @param[in]  ev       The event context the operation should work on.
 *
 * @param[in]  dgram    The tdgram_context to send the message buffer.
 *
 * @param[in]  queue    The existing dgram queue.
 *
 * @param[in]  buf      The message buffer to send.
 *
 * @param[in]  len      The message length.
 *
 * @param[in]  dst      The destination socket address.
 *
 * @return              The async request handle. NULL on fatal error.
 *
 * @see tdgram_sendto_queue_recv()
 */
struct tevent_req *tdgram_sendto_queue_send(TALLOC_CTX *mem_ctx,
					    struct tevent_context *ev,
					    struct tdgram_context *dgram,
					    struct tevent_queue *queue,
					    const uint8_t *buf,
					    size_t len,
					    struct tsocket_address *dst);

/**
 * @brief Receive the result of the sent dgram blob.
 *
 * @param[in]  req      The tevent request from tdgram_sendto_queue_send().
 *
 * @param[out] perrno   The error set to the actual errno.
 *
 * @return              The length of the datagram (0 is never returned!), -1 on
 *                      error with perrno set to the actual errno.
 */
ssize_t tdgram_sendto_queue_recv(struct tevent_req *req, int *perrno);

typedef int (*tstream_readv_pdu_next_vector_t)(struct tstream_context *stream,
					       void *private_data,
					       TALLOC_CTX *mem_ctx,
					       struct iovec **vector,
					       size_t *count);

struct tevent_req *tstream_readv_pdu_send(TALLOC_CTX *mem_ctx,
				struct tevent_context *ev,
				struct tstream_context *stream,
				tstream_readv_pdu_next_vector_t next_vector_fn,
				void *next_vector_private);
int tstream_readv_pdu_recv(struct tevent_req *req, int *perrno);

/**
 * @brief Queue a read request for a PDU on the socket.
 *
 * This function queues a read request for a PDU on a stream socket. The async
 * callback is triggered when a full PDU has been read from the socket.
 *
 * The caller needs to make sure that all non-scalar input parameters hang
 * around for the whole lifetime of the request.
 *
 * @param[in]  mem_ctx  The memory context for the result
 *
 * @param[in]  ev       The tevent_context to run on
 *
 * @param[in]  stream   The stream to send data through
 *
 * @param[in]  queue    The existing send queue
 *
 * @param[in]  next_vector_fn  The next vector function
 *
 * @param[in]  next_vector_private  The private_data of the next vector function
 *
 * @return              The async request handle. NULL on fatal error.
 *
 * @see tstream_readv_pdu_queue_recv()
 */
struct tevent_req *tstream_readv_pdu_queue_send(TALLOC_CTX *mem_ctx,
				struct tevent_context *ev,
				struct tstream_context *stream,
				struct tevent_queue *queue,
				tstream_readv_pdu_next_vector_t next_vector_fn,
				void *next_vector_private);

/**
 * @brief Receive the PDU blob read from the stream.
 *
 * @param[in]  req      The tevent request from tstream_readv_pdu_queue_send().
 *
 * @param[out] perrno   The error set to the actual errno.
 *
 * @return              The number of bytes read on success, -1 on error with
 *                      perrno set to the actual errno.
 */
int tstream_readv_pdu_queue_recv(struct tevent_req *req, int *perrno);

/**
 * @brief Queue an iovector for sending through the socket
 *
 * This function queues an iovector for sending to destination through an
 * existing stream socket. The async callback is triggered when the whole
 * vectror has been delivered to the underlying system socket.
 *
 * The caller needs to make sure that all non-scalar input parameters hang
 * around for the whole lifetime of the request.
 *
 * @param[in]  mem_ctx  The memory context for the result.
 *
 * @param[in]  ev       The tevent_context to run on.
 *
 * @param[in]  stream   The stream to send data through.
 *
 * @param[in]  queue    The existing send queue.
 *
 * @param[in]  vector   The iovec vector so write.
 *
 * @param[in]  count    The size of the vector.
 *
 * @return              The async request handle. NULL on fatal error.
 */
struct tevent_req *tstream_writev_queue_send(TALLOC_CTX *mem_ctx,
					     struct tevent_context *ev,
					     struct tstream_context *stream,
					     struct tevent_queue *queue,
					     const struct iovec *vector,
					     size_t count);

/**
 * @brief Receive the result of the sent iovector.
 *
 * @param[in]  req      The tevent request from tstream_writev_queue_send().
 *
 * @param[out] perrno   The error set to the actual errno.
 *
 * @return              The length of the iovector (0 is never returned!), -1 on
 *                      error with perrno set to the actual errno.
 */
int tstream_writev_queue_recv(struct tevent_req *req, int *perrno);

/**
 * @}
 */

#endif /* _TSOCKET_H */