2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
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 2 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #ifndef _SAMBA_SOCKET_H
22 #define _SAMBA_SOCKET_H
24 #include "lib/events/events.h"
26 struct socket_context;
33 struct socket_address {
37 struct sockaddr *sockaddr;
44 NTSTATUS (*fn_init)(struct socket_context *sock);
47 NTSTATUS (*fn_connect)(struct socket_context *sock,
48 const struct socket_address *my_address,
49 const struct socket_address *server_address,
52 /* complete a non-blocking connect */
53 NTSTATUS (*fn_connect_complete)(struct socket_context *sock,
57 NTSTATUS (*fn_listen)(struct socket_context *sock,
58 const struct socket_address *my_address,
59 int queue_size, uint32_t flags);
60 NTSTATUS (*fn_accept)(struct socket_context *sock,
61 struct socket_context **new_sock);
64 NTSTATUS (*fn_recv)(struct socket_context *sock, void *buf,
65 size_t wantlen, size_t *nread);
66 NTSTATUS (*fn_send)(struct socket_context *sock,
67 const DATA_BLOB *blob, size_t *sendlen);
69 NTSTATUS (*fn_sendto)(struct socket_context *sock,
70 const DATA_BLOB *blob, size_t *sendlen,
71 const struct socket_address *dest_addr);
72 NTSTATUS (*fn_recvfrom)(struct socket_context *sock,
73 void *buf, size_t wantlen, size_t *nread,
74 TALLOC_CTX *addr_ctx, struct socket_address **src_addr);
75 NTSTATUS (*fn_pending)(struct socket_context *sock, size_t *npending);
77 void (*fn_close)(struct socket_context *sock);
79 NTSTATUS (*fn_set_option)(struct socket_context *sock, const char *option, const char *val);
81 char *(*fn_get_peer_name)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
82 struct socket_address *(*fn_get_peer_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
83 struct socket_address *(*fn_get_my_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
85 int (*fn_get_fd)(struct socket_context *sock);
89 SOCKET_STATE_UNDEFINED,
91 SOCKET_STATE_CLIENT_START,
92 SOCKET_STATE_CLIENT_CONNECTED,
93 SOCKET_STATE_CLIENT_STARTTLS,
94 SOCKET_STATE_CLIENT_ERROR,
96 SOCKET_STATE_SERVER_LISTEN,
97 SOCKET_STATE_SERVER_CONNECTED,
98 SOCKET_STATE_SERVER_STARTTLS,
99 SOCKET_STATE_SERVER_ERROR
102 #define SOCKET_FLAG_BLOCK 0x00000001
103 #define SOCKET_FLAG_PEEK 0x00000002
104 #define SOCKET_FLAG_TESTNONBLOCK 0x00000004
105 #define SOCKET_FLAG_ENCRYPT 0x00000008 /* This socket
106 * implementation requires
108 * consistant, because it
109 * is encrypting data.
111 * TESTNONBLOCK case */
113 struct socket_context {
114 enum socket_type type;
115 enum socket_state state;
121 const struct socket_ops *ops;
122 const char *backend_name;
127 NTSTATUS socket_create_with_ops(TALLOC_CTX *mem_ctx, const struct socket_ops *ops,
128 struct socket_context **new_sock,
129 enum socket_type type, uint32_t flags);
130 NTSTATUS socket_create(const char *name, enum socket_type type,
131 struct socket_context **new_sock, uint32_t flags);
132 NTSTATUS socket_connect(struct socket_context *sock,
133 const struct socket_address *my_address,
134 const struct socket_address *server_address,
136 NTSTATUS socket_connect_complete(struct socket_context *sock, uint32_t flags);
137 NTSTATUS socket_listen(struct socket_context *sock,
138 const struct socket_address *my_address,
139 int queue_size, uint32_t flags);
140 NTSTATUS socket_accept(struct socket_context *sock, struct socket_context **new_sock);
141 NTSTATUS socket_recv(struct socket_context *sock, void *buf,
142 size_t wantlen, size_t *nread);
143 NTSTATUS socket_recvfrom(struct socket_context *sock, void *buf,
144 size_t wantlen, size_t *nread,
145 TALLOC_CTX *addr_ctx, struct socket_address **src_addr);
146 NTSTATUS socket_send(struct socket_context *sock,
147 const DATA_BLOB *blob, size_t *sendlen);
148 NTSTATUS socket_sendto(struct socket_context *sock,
149 const DATA_BLOB *blob, size_t *sendlen,
150 const struct socket_address *dest_addr);
151 NTSTATUS socket_pending(struct socket_context *sock, size_t *npending);
152 NTSTATUS socket_set_option(struct socket_context *sock, const char *option, const char *val);
153 char *socket_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx);
154 struct socket_address *socket_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
155 struct socket_address *socket_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
156 int socket_get_fd(struct socket_context *sock);
157 NTSTATUS socket_dup(struct socket_context *sock);
158 struct socket_address *socket_address_from_strings(TALLOC_CTX *mem_ctx,
162 struct socket_address *socket_address_from_sockaddr(TALLOC_CTX *mem_ctx,
163 struct sockaddr *sockaddr,
165 const struct socket_ops *socket_getops_byname(const char *name, enum socket_type type);
166 BOOL allow_access(TALLOC_CTX *mem_ctx,
167 const char **deny_list, const char **allow_list,
168 const char *cname, const char *caddr);
169 BOOL socket_check_access(struct socket_context *sock,
170 const char *service_name,
171 const char **allow_list, const char **deny_list);
173 struct composite_context *socket_connect_send(struct socket_context *sock,
174 struct socket_address *my_address,
175 struct socket_address *server_address,
177 struct event_context *event_ctx);
178 NTSTATUS socket_connect_recv(struct composite_context *ctx);
179 NTSTATUS socket_connect_ev(struct socket_context *sock,
180 struct socket_address *my_address,
181 struct socket_address *server_address,
182 uint32_t flags, struct event_context *ev);
184 struct composite_context *socket_connect_multi_send(TALLOC_CTX *mem_ctx,
185 const char *server_address,
186 int num_server_ports,
187 uint16_t *server_ports,
188 struct event_context *event_ctx);
189 NTSTATUS socket_connect_multi_recv(struct composite_context *ctx,
191 struct socket_context **result,
193 NTSTATUS socket_connect_multi(TALLOC_CTX *mem_ctx, const char *server_address,
194 int num_server_ports, uint16_t *server_ports,
195 struct event_context *event_ctx,
196 struct socket_context **result,
198 void set_socket_options(int fd, const char *options);
200 #endif /* _SAMBA_SOCKET_H */