Merge branch 'master' of git://git.samba.org/samba into minschema
[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 /*
124  * BSD sockets: inet, inet6 and unix
125  */
126
127 int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
128                                        const char *fam,
129                                        const char *addr,
130                                        uint16_t port,
131                                        struct tsocket_address **_addr,
132                                        const char *location);
133 #define tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr) \
134         _tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr, \
135                                            __location__)
136
137 char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
138                                        TALLOC_CTX *mem_ctx);
139 uint16_t tsocket_address_inet_port(const struct tsocket_address *addr);
140 int tsocket_address_inet_set_port(struct tsocket_address *addr,
141                                   uint16_t port);
142 void tsocket_address_inet_set_broadcast(struct tsocket_address *addr,
143                                         bool broadcast);
144
145 int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
146                                     const char *path,
147                                     struct tsocket_address **_addr,
148                                     const char *location);
149 #define tsocket_address_unix_from_path(mem_ctx, path, _addr) \
150         _tsocket_address_unix_from_path(mem_ctx, path, _addr, \
151                                         __location__)
152 char *tsocket_address_unix_path(const struct tsocket_address *addr,
153                                 TALLOC_CTX *mem_ctx);
154
155 int _tsocket_context_bsd_wrap_existing(TALLOC_CTX *mem_ctx,
156                                        int fd, bool close_on_disconnect,
157                                        struct tsocket_context **_sock,
158                                        const char *location);
159 #define tsocket_context_bsd_wrap_existing(mem_ctx, fd, cod, _sock) \
160         _tsocket_context_bsd_wrap_existing(mem_ctx, fd, cod, _sock, \
161                                            __location__)
162
163 /*
164  * Async helpers
165  */
166
167 struct tevent_req *tsocket_recvfrom_send(struct tsocket_context *sock,
168                                          TALLOC_CTX *mem_ctx);
169 ssize_t tsocket_recvfrom_recv(struct tevent_req *req,
170                               int *perrno,
171                               TALLOC_CTX *mem_ctx,
172                               uint8_t **buf,
173                               struct tsocket_address **src);
174
175 struct tevent_req *tsocket_sendto_send(struct tsocket_context *sock,
176                                        TALLOC_CTX *mem_ctx,
177                                        const uint8_t *buf,
178                                        size_t len,
179                                        const struct tsocket_address *dst);
180 ssize_t tsocket_sendto_recv(struct tevent_req *req, int *perrno);
181
182 struct tevent_req *tsocket_sendto_queue_send(TALLOC_CTX *mem_ctx,
183                                              struct tsocket_context *sock,
184                                              struct tevent_queue *queue,
185                                              const uint8_t *buf,
186                                              size_t len,
187                                              struct tsocket_address *dst);
188 ssize_t tsocket_sendto_queue_recv(struct tevent_req *req, int *perrno);
189
190 struct tevent_req *tsocket_connect_send(struct tsocket_context *sock,
191                                         TALLOC_CTX *mem_ctx,
192                                         const struct tsocket_address *dst);
193 int tsocket_connect_recv(struct tevent_req *req, int *perrno);
194
195 struct tevent_req *tsocket_writev_send(struct tsocket_context *sock,
196                                        TALLOC_CTX *mem_ctx,
197                                        const struct iovec *vector,
198                                        size_t count);
199 int tsocket_writev_recv(struct tevent_req *req, int *perrno);
200
201 struct tevent_req *tsocket_writev_queue_send(TALLOC_CTX *mem_ctx,
202                                              struct tsocket_context *sock,
203                                              struct tevent_queue *queue,
204                                              const struct iovec *vector,
205                                              size_t count);
206 int tsocket_writev_queue_recv(struct tevent_req *req, int *perrno);
207
208 typedef int (*tsocket_readv_next_iovec_t)(struct tsocket_context *sock,
209                                           void *private_data,
210                                           TALLOC_CTX *mem_ctx,
211                                           struct iovec **vector,
212                                           size_t *count);
213 struct tevent_req *tsocket_readv_send(struct tsocket_context *sock,
214                                       TALLOC_CTX *mem_ctx,
215                                       tsocket_readv_next_iovec_t next_iovec_fn,
216                                       void *private_data);
217 int tsocket_readv_recv(struct tevent_req *req, int *perrno);
218
219 #endif /* _TSOCKET_H */
220