8f69490012e223ea33c7642ba5780a0795bc0f1e
[jra/samba/.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 tdgram_context;
33 struct iovec;
34
35 enum tsocket_type {
36         TSOCKET_TYPE_STREAM = 1,
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 int tsocket_get_status(const struct tsocket_context *sock);
71
72 int _tsocket_get_local_address(const struct tsocket_context *sock,
73                                TALLOC_CTX *mem_ctx,
74                                struct tsocket_address **local_addr,
75                                const char *location);
76 #define tsocket_get_local_address(sock, mem_ctx, local_addr) \
77         _tsocket_get_local_address(sock, mem_ctx, local_addr, __location__)
78 int _tsocket_get_remote_address(const struct tsocket_context *sock,
79                                 TALLOC_CTX *mem_ctx,
80                                 struct tsocket_address **remote_addr,
81                                 const char *location);
82 #define tsocket_get_remote_address(sock, mem_ctx, remote_addr) \
83         _tsocket_get_remote_address(sock, mem_ctx, remote_addr, __location__)
84
85 int tsocket_get_option(const struct tsocket_context *sock,
86                        const char *option,
87                        TALLOC_CTX *mem_ctx,
88                        char **value);
89 int tsocket_set_option(const struct tsocket_context *sock,
90                        const char *option,
91                        bool force,
92                        const char *value);
93
94 void tsocket_disconnect(struct tsocket_context *sock);
95
96 char *tsocket_address_string(const struct tsocket_address *addr,
97                              TALLOC_CTX *mem_ctx);
98
99 struct tsocket_address *_tsocket_address_copy(const struct tsocket_address *addr,
100                                               TALLOC_CTX *mem_ctx,
101                                               const char *location);
102
103 #define tsocket_address_copy(addr, mem_ctx) \
104         _tsocket_address_copy(addr, mem_ctx, __location__)
105
106 int _tsocket_address_create_socket(const struct tsocket_address *addr,
107                                    enum tsocket_type type,
108                                    TALLOC_CTX *mem_ctx,
109                                    struct tsocket_context **sock,
110                                    const char *location);
111 #define tsocket_address_create_socket(addr, type, mem_ctx, sock) \
112         _tsocket_address_create_socket(addr, type, mem_ctx, sock,\
113                                        __location__)
114
115 /*
116  * tdgram_context related functions
117  */
118 struct tevent_req *tdgram_recvfrom_send(TALLOC_CTX *mem_ctx,
119                                         struct tevent_context *ev,
120                                         struct tdgram_context *dgram);
121 ssize_t tdgram_recvfrom_recv(struct tevent_req *req,
122                              int *perrno,
123                              TALLOC_CTX *mem_ctx,
124                              uint8_t **buf,
125                              struct tsocket_address **src);
126
127 struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
128                                       struct tevent_context *ev,
129                                       struct tdgram_context *dgram,
130                                       const uint8_t *buf, size_t len,
131                                       const struct tsocket_address *dst);
132 ssize_t tdgram_sendto_recv(struct tevent_req *req,
133                            int *perrno);
134
135 struct tevent_req *tdgram_disconnect_send(TALLOC_CTX *mem_ctx,
136                                           struct tevent_context *ev,
137                                           struct tdgram_context *dgram);
138 int tdgram_disconnect_recv(struct tevent_req *req,
139                            int *perrno);
140
141 /*
142  * BSD sockets: inet, inet6 and unix
143  */
144
145 int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
146                                        const char *fam,
147                                        const char *addr,
148                                        uint16_t port,
149                                        struct tsocket_address **_addr,
150                                        const char *location);
151 #define tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr) \
152         _tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr, \
153                                            __location__)
154
155 char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
156                                        TALLOC_CTX *mem_ctx);
157 uint16_t tsocket_address_inet_port(const struct tsocket_address *addr);
158 int tsocket_address_inet_set_port(struct tsocket_address *addr,
159                                   uint16_t port);
160 void tsocket_address_inet_set_broadcast(struct tsocket_address *addr,
161                                         bool broadcast);
162
163 int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
164                                     const char *path,
165                                     struct tsocket_address **_addr,
166                                     const char *location);
167 #define tsocket_address_unix_from_path(mem_ctx, path, _addr) \
168         _tsocket_address_unix_from_path(mem_ctx, path, _addr, \
169                                         __location__)
170 char *tsocket_address_unix_path(const struct tsocket_address *addr,
171                                 TALLOC_CTX *mem_ctx);
172
173 int _tsocket_context_bsd_wrap_existing(TALLOC_CTX *mem_ctx,
174                                        int fd, bool close_on_disconnect,
175                                        struct tsocket_context **_sock,
176                                        const char *location);
177 #define tsocket_context_bsd_wrap_existing(mem_ctx, fd, cod, _sock) \
178         _tsocket_context_bsd_wrap_existing(mem_ctx, fd, cod, _sock, \
179                                            __location__)
180
181 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
182                             const struct tsocket_address *remote,
183                             TALLOC_CTX *mem_ctx,
184                             struct tdgram_context **dgram,
185                             const char *location);
186 #define tdgram_inet_udp_socket(local, remote, mem_ctx, dgram) \
187         _tdgram_inet_udp_socket(local, remote, mem_ctx, dgram, __location__)
188
189 int _tdgram_unix_dgram_socket(const struct tsocket_address *local,
190                               const struct tsocket_address *remote,
191                               TALLOC_CTX *mem_ctx,
192                               struct tdgram_context **dgram,
193                               const char *location);
194 #define tdgram_unix_dgram_socket(local, remote, mem_ctx, dgram) \
195         _tdgram_unix_dgram_socket(local, remote, mem_ctx, dgram, __location__)
196
197 /*
198  * Async helpers
199  */
200
201 struct tevent_req *tsocket_connect_send(struct tsocket_context *sock,
202                                         TALLOC_CTX *mem_ctx,
203                                         const struct tsocket_address *dst);
204 int tsocket_connect_recv(struct tevent_req *req, int *perrno);
205
206 struct tevent_req *tsocket_writev_send(struct tsocket_context *sock,
207                                        TALLOC_CTX *mem_ctx,
208                                        const struct iovec *vector,
209                                        size_t count);
210 int tsocket_writev_recv(struct tevent_req *req, int *perrno);
211
212 struct tevent_req *tsocket_writev_queue_send(TALLOC_CTX *mem_ctx,
213                                              struct tsocket_context *sock,
214                                              struct tevent_queue *queue,
215                                              const struct iovec *vector,
216                                              size_t count);
217 int tsocket_writev_queue_recv(struct tevent_req *req, int *perrno);
218
219 typedef int (*tsocket_readv_next_iovec_t)(struct tsocket_context *sock,
220                                           void *private_data,
221                                           TALLOC_CTX *mem_ctx,
222                                           struct iovec **vector,
223                                           size_t *count);
224 struct tevent_req *tsocket_readv_send(struct tsocket_context *sock,
225                                       TALLOC_CTX *mem_ctx,
226                                       tsocket_readv_next_iovec_t next_iovec_fn,
227                                       void *private_data);
228 int tsocket_readv_recv(struct tevent_req *req, int *perrno);
229
230 /*
231  * Queue helpers
232  */
233
234 struct tevent_req *tdgram_sendto_queue_send(TALLOC_CTX *mem_ctx,
235                                             struct tevent_context *ev,
236                                             struct tdgram_context *dgram,
237                                             struct tevent_queue *queue,
238                                             const uint8_t *buf,
239                                             size_t len,
240                                             struct tsocket_address *dst);
241 ssize_t tdgram_sendto_queue_recv(struct tevent_req *req, int *perrno);
242
243 #endif /* _TSOCKET_H */
244