s4: fix LIBEVENTS dependencies and use more forward declarations
[sfrench/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 event_context;
24 struct socket_context;
25
26 enum socket_type {
27         SOCKET_TYPE_STREAM,
28         SOCKET_TYPE_DGRAM
29 };
30
31 struct socket_address {
32         const char *family;
33         char *addr;
34         int port;
35         struct sockaddr *sockaddr;
36         size_t sockaddrlen;
37 };
38
39 struct socket_ops {
40         const char *name;
41
42         NTSTATUS (*fn_init)(struct socket_context *sock);
43
44         /* client ops */
45         NTSTATUS (*fn_connect)(struct socket_context *sock,
46                                const struct socket_address *my_address,
47                                const struct socket_address *server_address,
48                                uint32_t flags);
49
50         /* complete a non-blocking connect */
51         NTSTATUS (*fn_connect_complete)(struct socket_context *sock,
52                                         uint32_t flags);
53
54         /* server ops */
55         NTSTATUS (*fn_listen)(struct socket_context *sock,
56                               const struct socket_address *my_address, 
57                               int queue_size, uint32_t flags);
58         NTSTATUS (*fn_accept)(struct socket_context *sock,      
59                               struct socket_context **new_sock);
60
61         /* general ops */
62         NTSTATUS (*fn_recv)(struct socket_context *sock, void *buf,
63                             size_t wantlen, size_t *nread);
64         NTSTATUS (*fn_send)(struct socket_context *sock, 
65                             const DATA_BLOB *blob, size_t *sendlen);
66
67         NTSTATUS (*fn_sendto)(struct socket_context *sock, 
68                               const DATA_BLOB *blob, size_t *sendlen,
69                               const struct socket_address *dest_addr);
70         NTSTATUS (*fn_recvfrom)(struct socket_context *sock, 
71                                 void *buf, size_t wantlen, size_t *nread,
72                                 TALLOC_CTX *addr_ctx, struct socket_address **src_addr);
73         NTSTATUS (*fn_pending)(struct socket_context *sock, size_t *npending);      
74
75         void (*fn_close)(struct socket_context *sock);
76
77         NTSTATUS (*fn_set_option)(struct socket_context *sock, const char *option, const char *val);
78
79         char *(*fn_get_peer_name)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
80         struct socket_address *(*fn_get_peer_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
81         struct socket_address *(*fn_get_my_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
82
83         int (*fn_get_fd)(struct socket_context *sock);
84 };
85
86 enum socket_state {
87         SOCKET_STATE_UNDEFINED,
88
89         SOCKET_STATE_CLIENT_START,
90         SOCKET_STATE_CLIENT_CONNECTED,
91         SOCKET_STATE_CLIENT_STARTTLS,
92         SOCKET_STATE_CLIENT_ERROR,
93         
94         SOCKET_STATE_SERVER_LISTEN,
95         SOCKET_STATE_SERVER_CONNECTED,
96         SOCKET_STATE_SERVER_STARTTLS,
97         SOCKET_STATE_SERVER_ERROR
98 };
99
100 #define SOCKET_FLAG_BLOCK        0x00000001
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
130 /* prototypes */
131 NTSTATUS socket_create_with_ops(TALLOC_CTX *mem_ctx, const struct socket_ops *ops,
132                                 struct socket_context **new_sock, 
133                                 enum socket_type type, uint32_t flags);
134 NTSTATUS socket_create(const char *name, enum socket_type type, 
135                        struct socket_context **new_sock, uint32_t flags);
136 NTSTATUS socket_connect(struct socket_context *sock,
137                         const struct socket_address *my_address, 
138                         const struct socket_address *server_address,
139                         uint32_t flags);
140 NTSTATUS socket_connect_complete(struct socket_context *sock, uint32_t flags);
141 NTSTATUS socket_listen(struct socket_context *sock, 
142                        const struct socket_address *my_address, 
143                        int queue_size, uint32_t flags);
144 NTSTATUS socket_accept(struct socket_context *sock, struct socket_context **new_sock);
145 NTSTATUS socket_recv(struct socket_context *sock, void *buf, 
146                      size_t wantlen, size_t *nread);
147 NTSTATUS socket_recvfrom(struct socket_context *sock, void *buf, 
148                          size_t wantlen, size_t *nread, 
149                          TALLOC_CTX *addr_ctx, struct socket_address **src_addr);
150 NTSTATUS socket_send(struct socket_context *sock, 
151                      const DATA_BLOB *blob, size_t *sendlen);
152 NTSTATUS socket_sendto(struct socket_context *sock, 
153                        const DATA_BLOB *blob, size_t *sendlen,
154                        const struct socket_address *dest_addr);
155 NTSTATUS socket_pending(struct socket_context *sock, size_t *npending);
156 NTSTATUS socket_set_option(struct socket_context *sock, const char *option, const char *val);
157 char *socket_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx);
158 struct socket_address *socket_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
159 struct socket_address *socket_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
160 int socket_get_fd(struct socket_context *sock);
161 NTSTATUS socket_dup(struct socket_context *sock);
162 struct socket_address *socket_address_from_strings(TALLOC_CTX *mem_ctx,
163                                                    const char *type, 
164                                                    const char *host,
165                                                    int port);
166 struct socket_address *socket_address_from_sockaddr(TALLOC_CTX *mem_ctx, 
167                                                     struct sockaddr *sockaddr, 
168                                                     size_t addrlen);
169 const struct socket_ops *socket_getops_byname(const char *name, enum socket_type type);
170 bool allow_access(TALLOC_CTX *mem_ctx,
171                   const char **deny_list, const char **allow_list,
172                   const char *cname, const char *caddr);
173 bool socket_check_access(struct socket_context *sock, 
174                          const char *service_name,
175                          const char **allow_list, const char **deny_list);
176
177 struct composite_context *socket_connect_send(struct socket_context *sock,
178                                               struct socket_address *my_address,
179                                               struct socket_address *server_address, 
180                                               uint32_t flags,
181                                               struct resolve_context *resolve_ctx,
182                                               struct event_context *event_ctx);
183 NTSTATUS socket_connect_recv(struct composite_context *ctx);
184 NTSTATUS socket_connect_ev(struct socket_context *sock,
185                            struct socket_address *my_address,
186                            struct socket_address *server_address, 
187                            uint32_t flags, 
188                            struct resolve_context *resolve_ctx,
189                            struct event_context *ev);
190
191 struct composite_context *socket_connect_multi_send(TALLOC_CTX *mem_ctx,
192                                                     const char *server_address,
193                                                     int num_server_ports,
194                                                     uint16_t *server_ports,
195                                                     struct resolve_context *resolve_ctx,
196                                                     struct event_context *event_ctx);
197 NTSTATUS socket_connect_multi_recv(struct composite_context *ctx,
198                                    TALLOC_CTX *mem_ctx,
199                                    struct socket_context **result,
200                                    uint16_t *port);
201 NTSTATUS socket_connect_multi(TALLOC_CTX *mem_ctx, const char *server_address,
202                               int num_server_ports, uint16_t *server_ports,
203                               struct resolve_context *resolve_ctx,
204                               struct event_context *event_ctx,
205                               struct socket_context **result,
206                               uint16_t *port);
207 void set_socket_options(int fd, const char *options);
208 void socket_set_flags(struct socket_context *socket, unsigned flags);
209
210 extern bool testnonblock;
211
212 #endif /* _SAMBA_SOCKET_H */