Merge branch 'master' of ssh://git.samba.org/data/git/samba into wspp-schema
[ira/wip.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         ssize_t (*recvfrom_data)(struct tsocket_context *sock,
61                                  uint8_t *data, size_t len,
62                                  TALLOC_CTX *addr_ctx,
63                                  struct tsocket_address **remote_addr);
64         ssize_t (*sendto_data)(struct tsocket_context *sock,
65                                const uint8_t *data, size_t len,
66                                const struct tsocket_address *remote_addr);
67
68         /* info */
69         int (*get_status)(const struct tsocket_context *sock);
70         int (*get_local_address)(const struct tsocket_context *sock,
71                                 TALLOC_CTX *mem_ctx,
72                                 struct tsocket_address **local_addr,
73                                 const char *location);
74         int (*get_remote_address)(const struct tsocket_context *sock,
75                                   TALLOC_CTX *mem_ctx,
76                                   struct tsocket_address **remote_addr,
77                                   const char *location);
78
79         /* options */
80         int (*get_option)(const struct tsocket_context *sock,
81                           const char *option,
82                           TALLOC_CTX *mem_ctx,
83                           char **value);
84         int (*set_option)(const struct tsocket_context *sock,
85                           const char *option,
86                           bool force,
87                           const char *value);
88
89         /* close/disconnect */
90         void (*disconnect)(struct tsocket_context *sock);
91 };
92
93 struct tsocket_context {
94         const char *location;
95         const struct tsocket_context_ops *ops;
96
97         void *private_data;
98
99         struct {
100                 struct tevent_context *ctx;
101                 void *read_private;
102                 tsocket_event_handler_t read_handler;
103                 void *write_private;
104                 tsocket_event_handler_t write_handler;
105         } event;
106 };
107
108 struct tsocket_context *_tsocket_context_create(TALLOC_CTX *mem_ctx,
109                                         const struct tsocket_context_ops *ops,
110                                         void *pstate,
111                                         size_t psize,
112                                         const char *type,
113                                         const char *location);
114 #define tsocket_context_create(mem_ctx, ops, state, type, location) \
115         _tsocket_context_create(mem_ctx, ops, state, sizeof(type), \
116                                 #type, location)
117
118 struct tsocket_address_ops {
119         const char *name;
120
121         char *(*string)(const struct tsocket_address *addr,
122                         TALLOC_CTX *mem_ctx);
123
124         struct tsocket_address *(*copy)(const struct tsocket_address *addr,
125                                         TALLOC_CTX *mem_ctx,
126                                         const char *location);
127
128         int (*create_socket)(const struct tsocket_address *addr,
129                              enum tsocket_type,
130                              TALLOC_CTX *mem_ctx,
131                              struct tsocket_context **sock,
132                              const char *location);
133 };
134
135 struct tsocket_address {
136         const char *location;
137         const struct tsocket_address_ops *ops;
138
139         void *private_data;
140 };
141
142 struct tsocket_address *_tsocket_address_create(TALLOC_CTX *mem_ctx,
143                                         const struct tsocket_address_ops *ops,
144                                         void *pstate,
145                                         size_t psize,
146                                         const char *type,
147                                         const char *location);
148 #define tsocket_address_create(mem_ctx, ops, state, type, location) \
149         _tsocket_address_create(mem_ctx, ops, state, sizeof(type), \
150                                 #type, location)
151
152 int tsocket_error_from_errno(int ret, int sys_errno, bool *retry);
153 int tsocket_simple_int_recv(struct tevent_req *req, int *perrno);
154 int tsocket_common_prepare_fd(int fd, bool high_fd);
155
156 #endif /* _TSOCKET_H */
157