lib/tsocket: add generic socket abstraction layer
[ira/wip.git] / lib / tsocket / tsocket.h
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Stefan Metzmacher 2009
5
6      ** NOTE! The following LGPL license applies to the tevent
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 3 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #ifndef _TSOCKET_H
25 #define _TSOCKET_H
26
27 #include <talloc.h>
28 #include <tevent.h>
29
30 struct tsocket_context;
31 struct tsocket_address;
32 struct iovec;
33
34 enum tsocket_type {
35         TSOCKET_TYPE_STREAM = 1,
36         TSOCKET_TYPE_DGRAM,
37         TSOCKET_TYPE_MESSAGE
38 };
39
40 typedef void (*tsocket_event_handler_t)(struct tsocket_context *, void *);
41 int tsocket_set_event_context(struct tsocket_context *sock,
42                               struct tevent_context *ev);
43 int tsocket_set_readable_handler(struct tsocket_context *sock,
44                                  tsocket_event_handler_t handler,
45                                  void *private_data);
46 int tsocket_set_writeable_handler(struct tsocket_context *sock,
47                                   tsocket_event_handler_t handler,
48                                   void *private_data);
49
50 int tsocket_connect(struct tsocket_context *sock,
51                     const struct tsocket_address *remote_addr);
52
53 int tsocket_listen(struct tsocket_context *sock,
54                    int queue_size);
55
56 int _tsocket_accept(struct tsocket_context *sock,
57                     TALLOC_CTX *mem_ctx,
58                     struct tsocket_context **new_sock,
59                     const char *location);
60 #define tsocket_accept(sock, mem_ctx, new_sock) \
61         _tsocket_accept(sock, mem_ctx, new_sock, __location__)
62
63 ssize_t tsocket_pending(struct tsocket_context *sock);
64
65 int tsocket_readv(struct tsocket_context *sock,
66                   const struct iovec *vector, size_t count);
67 int tsocket_writev(struct tsocket_context *sock,
68                    const struct iovec *vector, size_t count);
69
70 ssize_t tsocket_recvfrom(struct tsocket_context *sock,
71                          uint8_t *data, size_t len,
72                          TALLOC_CTX *addr_ctx,
73                          struct tsocket_address **src_addr);
74 ssize_t tsocket_sendto(struct tsocket_context *sock,
75                        const uint8_t *data, size_t len,
76                        const struct tsocket_address *dest_addr);
77
78 int tsocket_get_status(const struct tsocket_context *sock);
79
80 int _tsocket_get_local_address(const struct tsocket_context *sock,
81                                TALLOC_CTX *mem_ctx,
82                                struct tsocket_address **local_addr,
83                                const char *location);
84 #define tsocket_get_local_address(sock, mem_ctx, local_addr) \
85         _tsocket_get_local_address(sock, mem_ctx, local_addr, __location__)
86 int _tsocket_get_remote_address(const struct tsocket_context *sock,
87                                 TALLOC_CTX *mem_ctx,
88                                 struct tsocket_address **remote_addr,
89                                 const char *location);
90 #define tsocket_get_remote_address(sock, mem_ctx, remote_addr) \
91         _tsocket_get_remote_address(sock, mem_ctx, remote_addr, __location__)
92
93 int tsocket_get_option(const struct tsocket_context *sock,
94                        const char *option,
95                        TALLOC_CTX *mem_ctx,
96                        char **value);
97 int tsocket_set_option(const struct tsocket_context *sock,
98                        const char *option,
99                        bool force,
100                        const char *value);
101
102 void tsocket_disconnect(struct tsocket_context *sock);
103
104 char *tsocket_address_string(const struct tsocket_address *addr,
105                              TALLOC_CTX *mem_ctx);
106
107 struct tsocket_address *_tsocket_address_copy(const struct tsocket_address *addr,
108                                               TALLOC_CTX *mem_ctx,
109                                               const char *location);
110
111 #define tsocket_address_copy(addr, mem_ctx) \
112         _tsocket_address_copy(addr, mem_ctx, __location__)
113
114 int _tsocket_address_create_socket(const struct tsocket_address *addr,
115                                    enum tsocket_type type,
116                                    TALLOC_CTX *mem_ctx,
117                                    struct tsocket_context **sock,
118                                    const char *location);
119 #define tsocket_address_create_socket(addr, type, mem_ctx, sock) \
120         _tsocket_address_create_socket(addr, type, mem_ctx, sock,\
121                                        __location__)
122
123 #endif /* _TSOCKET_H */
124