s3-libads: use ldap_init_fd() to initialize a ldap session if possible
[bbaumbach/samba-autobuild/.git] / source4 / lib / socket / socket.h
1 /* 
2    Unix SMB/CIFS implementation.
3    Socket functions
4    Copyright (C) Stefan Metzmacher 2004
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef _SAMBA_SOCKET_H
21 #define _SAMBA_SOCKET_H
22
23 struct tevent_context;
24 struct tevent_fd;
25 struct socket_context;
26
27 enum socket_type {
28         SOCKET_TYPE_STREAM,
29         SOCKET_TYPE_DGRAM
30 };
31
32 struct socket_address {
33         const char *family;
34         char *addr;
35         int port;
36         struct sockaddr *sockaddr;
37         size_t sockaddrlen;
38 };
39
40 struct socket_ops {
41         const char *name;
42
43         NTSTATUS (*fn_init)(struct socket_context *sock);
44
45         /* client ops */
46         NTSTATUS (*fn_connect)(struct socket_context *sock,
47                                const struct socket_address *my_address,
48                                const struct socket_address *server_address,
49                                uint32_t flags);
50
51         /* complete a non-blocking connect */
52         NTSTATUS (*fn_connect_complete)(struct socket_context *sock,
53                                         uint32_t flags);
54
55         /* server ops */
56         NTSTATUS (*fn_listen)(struct socket_context *sock,
57                               const struct socket_address *my_address, 
58                               int queue_size, uint32_t flags);
59         NTSTATUS (*fn_accept)(struct socket_context *sock,      
60                               struct socket_context **new_sock);
61
62         /* general ops */
63         NTSTATUS (*fn_recv)(struct socket_context *sock, void *buf,
64                             size_t wantlen, size_t *nread);
65         NTSTATUS (*fn_send)(struct socket_context *sock, 
66                             const DATA_BLOB *blob, size_t *sendlen);
67
68         NTSTATUS (*fn_sendto)(struct socket_context *sock, 
69                               const DATA_BLOB *blob, size_t *sendlen,
70                               const struct socket_address *dest_addr);
71         NTSTATUS (*fn_recvfrom)(struct socket_context *sock, 
72                                 void *buf, size_t wantlen, size_t *nread,
73                                 TALLOC_CTX *addr_ctx, struct socket_address **src_addr);
74         NTSTATUS (*fn_pending)(struct socket_context *sock, size_t *npending);      
75
76         void (*fn_close)(struct socket_context *sock);
77
78         NTSTATUS (*fn_set_option)(struct socket_context *sock, const char *option, const char *val);
79
80         char *(*fn_get_peer_name)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
81         struct socket_address *(*fn_get_peer_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
82         struct socket_address *(*fn_get_my_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
83
84         int (*fn_get_fd)(struct socket_context *sock);
85 };
86
87 enum socket_state {
88         SOCKET_STATE_UNDEFINED,
89
90         SOCKET_STATE_CLIENT_START,
91         SOCKET_STATE_CLIENT_CONNECTED,
92         SOCKET_STATE_CLIENT_STARTTLS,
93         SOCKET_STATE_CLIENT_ERROR,
94         
95         SOCKET_STATE_SERVER_LISTEN,
96         SOCKET_STATE_SERVER_CONNECTED,
97         SOCKET_STATE_SERVER_STARTTLS,
98         SOCKET_STATE_SERVER_ERROR
99 };
100
101 #define SOCKET_FLAG_PEEK         0x00000002
102 #define SOCKET_FLAG_TESTNONBLOCK 0x00000004
103 #define SOCKET_FLAG_ENCRYPT      0x00000008 /* This socket
104                                              * implementation requires
105                                              * that re-sends be
106                                              * consistant, because it
107                                              * is encrypting data.
108                                              * This modifies the
109                                              * TESTNONBLOCK case */
110 #define SOCKET_FLAG_NOCLOSE      0x00000010 /* don't auto-close on free */
111
112
113 struct socket_context {
114         enum socket_type type;
115         enum socket_state state;
116         uint32_t flags;
117
118         int fd;
119
120         void *private_data;
121         const struct socket_ops *ops;
122         const char *backend_name;
123
124         /* specific to the ip backend */
125         int family;
126 };
127
128 struct resolve_context;
129 struct tsocket_address;
130
131 /* prototypes */
132 NTSTATUS socket_create_with_ops(TALLOC_CTX *mem_ctx, const struct socket_ops *ops,
133                                 struct socket_context **new_sock, 
134                                 enum socket_type type, uint32_t flags);
135 NTSTATUS socket_create(TALLOC_CTX *mem_ctx,
136                        const char *name, enum socket_type type,
137                        struct socket_context **new_sock, uint32_t flags);
138 NTSTATUS socket_connect(struct socket_context *sock,
139                         const struct socket_address *my_address, 
140                         const struct socket_address *server_address,
141                         uint32_t flags);
142 NTSTATUS socket_connect_complete(struct socket_context *sock, uint32_t flags);
143 NTSTATUS socket_listen(struct socket_context *sock, 
144                        const struct socket_address *my_address, 
145                        int queue_size, uint32_t flags);
146 NTSTATUS socket_accept(struct socket_context *sock, struct socket_context **new_sock);
147 NTSTATUS socket_recv(struct socket_context *sock, void *buf, 
148                      size_t wantlen, size_t *nread);
149 NTSTATUS socket_recvfrom(struct socket_context *sock, void *buf, 
150                          size_t wantlen, size_t *nread, 
151                          TALLOC_CTX *addr_ctx, struct socket_address **src_addr);
152 NTSTATUS socket_send(struct socket_context *sock, 
153                      const DATA_BLOB *blob, size_t *sendlen);
154 NTSTATUS socket_sendto(struct socket_context *sock, 
155                        const DATA_BLOB *blob, size_t *sendlen,
156                        const struct socket_address *dest_addr);
157 NTSTATUS socket_pending(struct socket_context *sock, size_t *npending);
158 NTSTATUS socket_set_option(struct socket_context *sock, const char *option, const char *val);
159 char *socket_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx);
160 struct socket_address *socket_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
161 struct socket_address *socket_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
162 struct tsocket_address *socket_address_to_tsocket_address(TALLOC_CTX *mem_ctx,
163                                                           const struct socket_address *a);
164 struct socket_address *tsocket_address_to_socket_address(TALLOC_CTX *mem_ctx,
165                                                          const struct tsocket_address *a);
166 struct tsocket_address *socket_get_remote_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
167 struct tsocket_address *socket_get_local_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
168 int socket_get_fd(struct socket_context *sock);
169 NTSTATUS socket_dup(struct socket_context *sock);
170 struct socket_address *socket_address_from_strings(TALLOC_CTX *mem_ctx,
171                                                    const char *type, 
172                                                    const char *host,
173                                                    int port);
174 struct socket_address *socket_address_from_sockaddr(TALLOC_CTX *mem_ctx, 
175                                                     struct sockaddr *sockaddr, 
176                                                     size_t addrlen);
177 struct sockaddr_storage;
178 struct socket_address *socket_address_from_sockaddr_storage(TALLOC_CTX *mem_ctx,
179                                                             const struct sockaddr_storage *sockaddr,
180                                                             uint16_t port);
181 _PUBLIC_ void socket_address_set_port(struct socket_address *a,
182                                       uint16_t port);
183 struct socket_address *socket_address_copy(TALLOC_CTX *mem_ctx,
184                                            const struct socket_address *oaddr);
185 const struct socket_ops *socket_getops_byname(const char *name, enum socket_type type);
186 bool socket_check_access(struct socket_context *sock, 
187                          const char *service_name,
188                          const char **allow_list, const char **deny_list);
189
190 struct composite_context *socket_connect_send(struct socket_context *sock,
191                                               struct socket_address *my_address,
192                                               struct socket_address *server_address, 
193                                               uint32_t flags,
194                                               struct tevent_context *event_ctx);
195 NTSTATUS socket_connect_recv(struct composite_context *ctx);
196 NTSTATUS socket_connect_ev(struct socket_context *sock,
197                            struct socket_address *my_address,
198                            struct socket_address *server_address, 
199                            uint32_t flags, 
200                            struct tevent_context *ev);
201
202 struct socket_connect_multi_ex {
203         void *private_data;
204         struct tevent_req *(*establish_send)(TALLOC_CTX *mem_ctx,
205                                              struct tevent_context *ev,
206                                              struct socket_context *sock,
207                                              struct socket_address *addr,
208                                              void *private_data);
209         NTSTATUS (*establish_recv)(struct tevent_req *req);
210 };
211 struct composite_context *socket_connect_multi_ex_send(TALLOC_CTX *mem_ctx,
212                                                        const char *server_address,
213                                                        int num_server_ports,
214                                                        uint16_t *server_ports,
215                                                        struct resolve_context *resolve_ctx,
216                                                        struct tevent_context *event_ctx,
217                                                        struct socket_connect_multi_ex *ex);
218 NTSTATUS socket_connect_multi_ex_recv(struct composite_context *ctx,
219                                       TALLOC_CTX *mem_ctx,
220                                       struct socket_context **result,
221                                       uint16_t *port);
222 NTSTATUS socket_connect_multi_ex(TALLOC_CTX *mem_ctx, const char *server_address,
223                                  int num_server_ports, uint16_t *server_ports,
224                                  struct resolve_context *resolve_ctx,
225                                  struct tevent_context *event_ctx,
226                                  struct socket_connect_multi_ex *ex,
227                                  struct socket_context **result,
228                                  uint16_t *port);
229
230 struct composite_context *socket_connect_multi_send(TALLOC_CTX *mem_ctx,
231                                                     const char *server_address,
232                                                     int num_server_ports,
233                                                     uint16_t *server_ports,
234                                                     struct resolve_context *resolve_ctx,
235                                                     struct tevent_context *event_ctx);
236 NTSTATUS socket_connect_multi_recv(struct composite_context *ctx,
237                                    TALLOC_CTX *mem_ctx,
238                                    struct socket_context **result,
239                                    uint16_t *port);
240 NTSTATUS socket_connect_multi(TALLOC_CTX *mem_ctx, const char *server_address,
241                               int num_server_ports, uint16_t *server_ports,
242                               struct resolve_context *resolve_ctx,
243                               struct tevent_context *event_ctx,
244                               struct socket_context **result,
245                               uint16_t *port);
246 void set_socket_options(int fd, const char *options);
247 void socket_set_flags(struct socket_context *sock, unsigned flags);
248
249 void socket_tevent_fd_close_fn(struct tevent_context *ev,
250                                struct tevent_fd *fde,
251                                int fd,
252                                void *private_data);
253
254 extern bool testnonblock;
255
256 #endif /* _SAMBA_SOCKET_H */