tsocket: add tdgram_sendto_queue_send/recv()
[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 tdgram_context;
33 struct iovec;
34
35 enum tsocket_type {
36         TSOCKET_TYPE_STREAM = 1,
37         TSOCKET_TYPE_DGRAM,
38         TSOCKET_TYPE_MESSAGE
39 };
40
41 typedef void (*tsocket_event_handler_t)(struct tsocket_context *, void *);
42 int tsocket_set_event_context(struct tsocket_context *sock,
43                               struct tevent_context *ev);
44 int tsocket_set_readable_handler(struct tsocket_context *sock,
45                                  tsocket_event_handler_t handler,
46                                  void *private_data);
47 int tsocket_set_writeable_handler(struct tsocket_context *sock,
48                                   tsocket_event_handler_t handler,
49                                   void *private_data);
50
51 int tsocket_connect(struct tsocket_context *sock,
52                     const struct tsocket_address *remote_addr);
53
54 int tsocket_listen(struct tsocket_context *sock,
55                    int queue_size);
56
57 int _tsocket_accept(struct tsocket_context *sock,
58                     TALLOC_CTX *mem_ctx,
59                     struct tsocket_context **new_sock,
60                     const char *location);
61 #define tsocket_accept(sock, mem_ctx, new_sock) \
62         _tsocket_accept(sock, mem_ctx, new_sock, __location__)
63
64 ssize_t tsocket_pending(struct tsocket_context *sock);
65
66 int tsocket_readv(struct tsocket_context *sock,
67                   const struct iovec *vector, size_t count);
68 int tsocket_writev(struct tsocket_context *sock,
69                    const struct iovec *vector, size_t count);
70
71 ssize_t tsocket_recvfrom(struct tsocket_context *sock,
72                          uint8_t *data, size_t len,
73                          TALLOC_CTX *addr_ctx,
74                          struct tsocket_address **src_addr);
75 ssize_t tsocket_sendto(struct tsocket_context *sock,
76                        const uint8_t *data, size_t len,
77                        const struct tsocket_address *dest_addr);
78
79 int tsocket_get_status(const struct tsocket_context *sock);
80
81 int _tsocket_get_local_address(const struct tsocket_context *sock,
82                                TALLOC_CTX *mem_ctx,
83                                struct tsocket_address **local_addr,
84                                const char *location);
85 #define tsocket_get_local_address(sock, mem_ctx, local_addr) \
86         _tsocket_get_local_address(sock, mem_ctx, local_addr, __location__)
87 int _tsocket_get_remote_address(const struct tsocket_context *sock,
88                                 TALLOC_CTX *mem_ctx,
89                                 struct tsocket_address **remote_addr,
90                                 const char *location);
91 #define tsocket_get_remote_address(sock, mem_ctx, remote_addr) \
92         _tsocket_get_remote_address(sock, mem_ctx, remote_addr, __location__)
93
94 int tsocket_get_option(const struct tsocket_context *sock,
95                        const char *option,
96                        TALLOC_CTX *mem_ctx,
97                        char **value);
98 int tsocket_set_option(const struct tsocket_context *sock,
99                        const char *option,
100                        bool force,
101                        const char *value);
102
103 void tsocket_disconnect(struct tsocket_context *sock);
104
105 char *tsocket_address_string(const struct tsocket_address *addr,
106                              TALLOC_CTX *mem_ctx);
107
108 struct tsocket_address *_tsocket_address_copy(const struct tsocket_address *addr,
109                                               TALLOC_CTX *mem_ctx,
110                                               const char *location);
111
112 #define tsocket_address_copy(addr, mem_ctx) \
113         _tsocket_address_copy(addr, mem_ctx, __location__)
114
115 int _tsocket_address_create_socket(const struct tsocket_address *addr,
116                                    enum tsocket_type type,
117                                    TALLOC_CTX *mem_ctx,
118                                    struct tsocket_context **sock,
119                                    const char *location);
120 #define tsocket_address_create_socket(addr, type, mem_ctx, sock) \
121         _tsocket_address_create_socket(addr, type, mem_ctx, sock,\
122                                        __location__)
123
124 /*
125  * tdgram_context related functions
126  */
127 struct tevent_req *tdgram_recvfrom_send(TALLOC_CTX *mem_ctx,
128                                         struct tevent_context *ev,
129                                         struct tdgram_context *dgram);
130 ssize_t tdgram_recvfrom_recv(struct tevent_req *req,
131                              int *perrno,
132                              TALLOC_CTX *mem_ctx,
133                              uint8_t **buf,
134                              struct tsocket_address **src);
135
136 struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
137                                       struct tevent_context *ev,
138                                       struct tdgram_context *dgram,
139                                       const uint8_t *buf, size_t len,
140                                       const struct tsocket_address *dst);
141 ssize_t tdgram_sendto_recv(struct tevent_req *req,
142                            int *perrno);
143
144 struct tevent_req *tdgram_disconnect_send(TALLOC_CTX *mem_ctx,
145                                           struct tevent_context *ev,
146                                           struct tdgram_context *dgram);
147 int tdgram_disconnect_recv(struct tevent_req *req,
148                            int *perrno);
149
150 /*
151  * BSD sockets: inet, inet6 and unix
152  */
153
154 int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
155                                        const char *fam,
156                                        const char *addr,
157                                        uint16_t port,
158                                        struct tsocket_address **_addr,
159                                        const char *location);
160 #define tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr) \
161         _tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr, \
162                                            __location__)
163
164 char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
165                                        TALLOC_CTX *mem_ctx);
166 uint16_t tsocket_address_inet_port(const struct tsocket_address *addr);
167 int tsocket_address_inet_set_port(struct tsocket_address *addr,
168                                   uint16_t port);
169 void tsocket_address_inet_set_broadcast(struct tsocket_address *addr,
170                                         bool broadcast);
171
172 int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
173                                     const char *path,
174                                     struct tsocket_address **_addr,
175                                     const char *location);
176 #define tsocket_address_unix_from_path(mem_ctx, path, _addr) \
177         _tsocket_address_unix_from_path(mem_ctx, path, _addr, \
178                                         __location__)
179 char *tsocket_address_unix_path(const struct tsocket_address *addr,
180                                 TALLOC_CTX *mem_ctx);
181
182 int _tsocket_context_bsd_wrap_existing(TALLOC_CTX *mem_ctx,
183                                        int fd, bool close_on_disconnect,
184                                        struct tsocket_context **_sock,
185                                        const char *location);
186 #define tsocket_context_bsd_wrap_existing(mem_ctx, fd, cod, _sock) \
187         _tsocket_context_bsd_wrap_existing(mem_ctx, fd, cod, _sock, \
188                                            __location__)
189
190 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
191                             const struct tsocket_address *remote,
192                             TALLOC_CTX *mem_ctx,
193                             struct tdgram_context **dgram,
194                             const char *location);
195 #define tdgram_inet_udp_socket(local, remote, mem_ctx, dgram) \
196         _tdgram_inet_udp_socket(local, remote, mem_ctx, dgram, __location__)
197
198 int _tdgram_unix_dgram_socket(const struct tsocket_address *local,
199                               const struct tsocket_address *remote,
200                               TALLOC_CTX *mem_ctx,
201                               struct tdgram_context **dgram,
202                               const char *location);
203 #define tdgram_unix_dgram_socket(local, remote, mem_ctx, dgram) \
204         _tdgram_unix_dgram_socket(local, remote, mem_ctx, dgram, __location__)
205
206 /*
207  * Async helpers
208  */
209
210 struct tevent_req *tsocket_recvfrom_send(struct tsocket_context *sock,
211                                          TALLOC_CTX *mem_ctx);
212 ssize_t tsocket_recvfrom_recv(struct tevent_req *req,
213                               int *perrno,
214                               TALLOC_CTX *mem_ctx,
215                               uint8_t **buf,
216                               struct tsocket_address **src);
217
218 struct tevent_req *tsocket_sendto_send(struct tsocket_context *sock,
219                                        TALLOC_CTX *mem_ctx,
220                                        const uint8_t *buf,
221                                        size_t len,
222                                        const struct tsocket_address *dst);
223 ssize_t tsocket_sendto_recv(struct tevent_req *req, int *perrno);
224
225 struct tevent_req *tsocket_sendto_queue_send(TALLOC_CTX *mem_ctx,
226                                              struct tsocket_context *sock,
227                                              struct tevent_queue *queue,
228                                              const uint8_t *buf,
229                                              size_t len,
230                                              struct tsocket_address *dst);
231 ssize_t tsocket_sendto_queue_recv(struct tevent_req *req, int *perrno);
232
233 struct tevent_req *tsocket_connect_send(struct tsocket_context *sock,
234                                         TALLOC_CTX *mem_ctx,
235                                         const struct tsocket_address *dst);
236 int tsocket_connect_recv(struct tevent_req *req, int *perrno);
237
238 struct tevent_req *tsocket_writev_send(struct tsocket_context *sock,
239                                        TALLOC_CTX *mem_ctx,
240                                        const struct iovec *vector,
241                                        size_t count);
242 int tsocket_writev_recv(struct tevent_req *req, int *perrno);
243
244 struct tevent_req *tsocket_writev_queue_send(TALLOC_CTX *mem_ctx,
245                                              struct tsocket_context *sock,
246                                              struct tevent_queue *queue,
247                                              const struct iovec *vector,
248                                              size_t count);
249 int tsocket_writev_queue_recv(struct tevent_req *req, int *perrno);
250
251 typedef int (*tsocket_readv_next_iovec_t)(struct tsocket_context *sock,
252                                           void *private_data,
253                                           TALLOC_CTX *mem_ctx,
254                                           struct iovec **vector,
255                                           size_t *count);
256 struct tevent_req *tsocket_readv_send(struct tsocket_context *sock,
257                                       TALLOC_CTX *mem_ctx,
258                                       tsocket_readv_next_iovec_t next_iovec_fn,
259                                       void *private_data);
260 int tsocket_readv_recv(struct tevent_req *req, int *perrno);
261
262 /*
263  * Queue helpers
264  */
265
266 struct tevent_req *tdgram_sendto_queue_send(TALLOC_CTX *mem_ctx,
267                                             struct tevent_context *ev,
268                                             struct tdgram_context *dgram,
269                                             struct tevent_queue *queue,
270                                             const uint8_t *buf,
271                                             size_t len,
272                                             struct tsocket_address *dst);
273 ssize_t tdgram_sendto_queue_recv(struct tevent_req *req, int *perrno);
274
275 #endif /* _TSOCKET_H */
276