tsocket: remove DGRAM support from tsocket_context
[sfrench/samba-autobuild/.git] / lib / tsocket / tsocket_internal.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_INTERNAL_H
25 #define _TSOCKET_INTERNAL_H
26
27 struct tsocket_context_ops {
28         const char *name;
29
30         /* event handling */
31         int (*set_event_context)(struct tsocket_context *sock,
32                                  struct tevent_context *ev);
33         int (*set_read_handler)(struct tsocket_context *sock,
34                                 tsocket_event_handler_t handler,
35                                 void *private_data);
36         int (*set_write_handler)(struct tsocket_context *sock,
37                                  tsocket_event_handler_t handler,
38                                  void *private_data);
39
40         /* client ops */
41         int (*connect_to)(struct tsocket_context *sock,
42                           const struct tsocket_address *remote_addr);
43
44         /* server ops */
45         int (*listen_on)(struct tsocket_context *sock,
46                          int queue_size);
47         int (*accept_new)(struct tsocket_context *sock,
48                           TALLOC_CTX *mem_ctx,
49                           struct tsocket_context **new_sock,
50                           const char *location);
51
52         /* general ops */
53         ssize_t (*pending_data)(struct tsocket_context *sock);
54
55         int (*readv_data)(struct tsocket_context *sock,
56                           const struct iovec *vector, size_t count);
57         int (*writev_data)(struct tsocket_context *sock,
58                            const struct iovec *vector, size_t count);
59
60         /* info */
61         int (*get_status)(const struct tsocket_context *sock);
62         int (*get_local_address)(const struct tsocket_context *sock,
63                                 TALLOC_CTX *mem_ctx,
64                                 struct tsocket_address **local_addr,
65                                 const char *location);
66         int (*get_remote_address)(const struct tsocket_context *sock,
67                                   TALLOC_CTX *mem_ctx,
68                                   struct tsocket_address **remote_addr,
69                                   const char *location);
70
71         /* options */
72         int (*get_option)(const struct tsocket_context *sock,
73                           const char *option,
74                           TALLOC_CTX *mem_ctx,
75                           char **value);
76         int (*set_option)(const struct tsocket_context *sock,
77                           const char *option,
78                           bool force,
79                           const char *value);
80
81         /* close/disconnect */
82         void (*disconnect)(struct tsocket_context *sock);
83 };
84
85 struct tsocket_context {
86         const char *location;
87         const struct tsocket_context_ops *ops;
88
89         void *private_data;
90
91         struct {
92                 struct tevent_context *ctx;
93                 void *read_private;
94                 tsocket_event_handler_t read_handler;
95                 void *write_private;
96                 tsocket_event_handler_t write_handler;
97         } event;
98 };
99
100 struct tsocket_context *_tsocket_context_create(TALLOC_CTX *mem_ctx,
101                                         const struct tsocket_context_ops *ops,
102                                         void *pstate,
103                                         size_t psize,
104                                         const char *type,
105                                         const char *location);
106 #define tsocket_context_create(mem_ctx, ops, state, type, location) \
107         _tsocket_context_create(mem_ctx, ops, state, sizeof(type), \
108                                 #type, location)
109
110 struct tsocket_address_ops {
111         const char *name;
112
113         char *(*string)(const struct tsocket_address *addr,
114                         TALLOC_CTX *mem_ctx);
115
116         struct tsocket_address *(*copy)(const struct tsocket_address *addr,
117                                         TALLOC_CTX *mem_ctx,
118                                         const char *location);
119
120         int (*create_socket)(const struct tsocket_address *addr,
121                              enum tsocket_type,
122                              TALLOC_CTX *mem_ctx,
123                              struct tsocket_context **sock,
124                              const char *location);
125 };
126
127 struct tsocket_address {
128         const char *location;
129         const struct tsocket_address_ops *ops;
130
131         void *private_data;
132 };
133
134 struct tsocket_address *_tsocket_address_create(TALLOC_CTX *mem_ctx,
135                                         const struct tsocket_address_ops *ops,
136                                         void *pstate,
137                                         size_t psize,
138                                         const char *type,
139                                         const char *location);
140 #define tsocket_address_create(mem_ctx, ops, state, type, location) \
141         _tsocket_address_create(mem_ctx, ops, state, sizeof(type), \
142                                 #type, location)
143
144 struct tdgram_context_ops {
145         const char *name;
146
147         struct tevent_req *(*recvfrom_send)(TALLOC_CTX *mem_ctx,
148                                             struct tevent_context *ev,
149                                             struct tdgram_context *dgram);
150         ssize_t (*recvfrom_recv)(struct tevent_req *req,
151                                  int *perrno,
152                                  TALLOC_CTX *mem_ctx,
153                                  uint8_t **buf,
154                                  struct tsocket_address **src);
155
156         struct tevent_req *(*sendto_send)(TALLOC_CTX *mem_ctx,
157                                           struct tevent_context *ev,
158                                           struct tdgram_context *dgram,
159                                           const uint8_t *buf, size_t len,
160                                           const struct tsocket_address *dst);
161         ssize_t (*sendto_recv)(struct tevent_req *req,
162                                int *perrno);
163
164         struct tevent_req *(*disconnect_send)(TALLOC_CTX *mem_ctx,
165                                               struct tevent_context *ev,
166                                               struct tdgram_context *dgram);
167         int (*disconnect_recv)(struct tevent_req *req,
168                                int *perrno);
169 };
170
171 struct tdgram_context *_tdgram_context_create(TALLOC_CTX *mem_ctx,
172                                         const struct tdgram_context_ops *ops,
173                                         void *pstate,
174                                         size_t psize,
175                                         const char *type,
176                                         const char *location);
177 #define tdgram_context_create(mem_ctx, ops, state, type, location) \
178         _tdgram_context_create(mem_ctx, ops, state, sizeof(type), \
179                                 #type, location)
180
181 void *_tdgram_context_data(struct tdgram_context *dgram);
182 #define tdgram_context_data(_req, _type) \
183         talloc_get_type_abort(_tdgram_context_data(_req), _type)
184
185 int tsocket_error_from_errno(int ret, int sys_errno, bool *retry);
186 int tsocket_simple_int_recv(struct tevent_req *req, int *perrno);
187 int tsocket_common_prepare_fd(int fd, bool high_fd);
188
189 #endif /* _TSOCKET_H */
190