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.
23 static NTSTATUS ipv4_tcp_init(struct socket_context *sock)
25 sock->fd = socket(PF_INET, SOCK_STREAM, 0);
27 /* TODO: we need to map from errno to NTSTATUS here! */
28 return NT_STATUS_FOOBAR;
34 static void ipv4_tcp_close(struct socket_context *sock)
39 static NTSTATUS ipv4_tcp_connect(struct socket_context *sock,
40 const char *my_address, int my_port,
41 const char *srv_address, int srv_port,
44 struct sockaddr_in my_addr;
45 struct sockaddr_in srv_addr;
47 struct in_addr srv_ip;
50 ret = inet_aton(my_address, &my_ip);
52 /* not a valid ipv4 address */
53 return NT_STATUS_FOOBAR;
57 #ifdef HAVE_SOCK_SIN_LEN
58 my_addr.sin_len = sizeof(my_addr);
60 my_addr.sin_addr = my_ip;
61 my_addr.sin_port = htons(my_port);
62 my_addr.sin_family = PF_INET;
64 ret = inet_aton(srv_address, &srv_ip);
66 /* not a valid ipv4 address */
67 return NT_STATUS_FOOBAR;
70 ret = bind(sock->fd, &my_addr, sizeof(my_addr));
72 /* TODO: we need to map from errno to NTSTATUS here! */
73 return NT_STATUS_FOOBAR;
76 ZERO_STRUCT(srv_addr);
77 #ifdef HAVE_SOCK_SIN_LEN
78 srv_addr.sin_len = sizeof(srv_addr);
80 srv_addr.sin_addr = srv_ip;
81 srv_addr.sin_port = htons(srv_port);
82 srv_addr.sin_family = PF_INET;
84 if (!(flags & SOCKET_OPTION_BLOCK)) {
85 ret = set_blocking(sock->fd, False);
87 /* TODO: we need to map from errno to NTSTATUS here! */
88 return NT_STATUS_FOOBAR;
93 ret = connect(sock->fd, &srv_addr, sizeof(srv_addr));
95 /* TODO: we need to map from errno to NTSTATUS here! */
96 return NT_STATUS_FOOBAR;
99 sock->state = SOCKET_STATE_CLIENT_CONNECTED;
104 static NTSTATUS ipv4_tcp_listen(struct socket_context *sock,
105 const char *my_address, int port,
106 int queue_size, uint32_t flags)
108 struct sockaddr_in my_addr;
109 struct in_addr ip_addr;
112 ZERO_STRUCT(my_addr);
114 ret = inet_aton(my_address, &ip_addr);
116 /* not a valid ipv4 address */
117 return NT_STATUS_FOOBAR;
120 #ifdef HAVE_SOCK_SIN_LEN
121 my_addr.sin_len = sizeof(my_addr);
123 my_addr.sin_addr = ip_addr;
124 my_addr.sin_port = htons(port);
125 my_addr.sin_family = PF_INET;
127 ret = bind(sock->fd, &my_addr, sizeof(my_addr));
129 /* TODO: we need to map from errno to NTSTATUS here! */
130 return NT_STATUS_FOOBAR;
133 ret = listen(sock->fd, queue_size);
135 /* TODO: we need to map from errno to NTSTATUS here! */
136 return NT_STATUS_FOOBAR;
139 return NT_STATUS_NOT_IMPLEMENTED;
142 static NTSTATUS ipv4_tcp_accept(struct socket_context *sock, struct socket_context **new_sock, uint32_t flags)
144 struct sockaddr_in cli_addr;
145 socklen_t cli_addr_len = 0;
149 new_fd = accept(sock->fd, &cli_addr, &cli_addr_len);
151 /* TODO: we need to map from errno to NTSTATUS here! */
152 return NT_STATUS_FOOBAR;
155 if (!(flags & SOCKET_OPTION_BLOCK)) {
156 ret = set_blocking(sock->fd, False);
158 /* TODO: we need to map from errno to NTSTATUS here! */
159 return NT_STATUS_FOOBAR;
163 /* TODO: we could add a 'accept_check' hook here
164 * which get the black/white lists via socket_set_accept_filter()
165 * or something like that
169 (*new_sock) = talloc_p(NULL, struct socket_context);
171 return NT_STATUS_NO_MEMORY;
174 /* copy the socket_context */
175 (*new_sock)->type = sock->type;
176 (*new_sock)->state = SOCKET_STATE_SERVER_CONNECTED;
177 (*new_sock)->flags = flags;
179 (*new_sock)->fd = new_fd;
181 (*new_sock)->private_data = NULL;
182 (*new_sock)->ops = sock->ops;
184 return NT_STATUS_NOT_IMPLEMENTED;
187 static NTSTATUS ipv4_tcp_recv(struct socket_context *sock, TALLOC_CTX *mem_ctx,
188 DATA_BLOB *blob, size_t wantlen, uint32_t flags)
194 buf = talloc(mem_ctx, wantlen);
196 return NT_STATUS_NO_MEMORY;
199 /* TODO: we need to map the flags here */
201 gotlen = recv(sock->fd, buf, wantlen, flgs);
204 /* TODO: we need to map from errno to NTSTATUS here! */
205 return NT_STATUS_FOOBAR;
208 blob->length = gotlen;
214 static NTSTATUS ipv4_tcp_send(struct socket_context *sock, TALLOC_CTX *mem_ctx,
215 const DATA_BLOB *blob, size_t *sendlen, uint32_t flags)
220 /* TODO: we need to map the flags here */
222 len = send(sock->fd, blob->data, blob->length, flgs);
224 /* TODO: we need to map from errno to NTSTATUS here! */
225 return NT_STATUS_FOOBAR;
233 static NTSTATUS ipv4_tcp_set_option(struct socket_context *sock, const char *option, const char *val)
235 return NT_STATUS_NOT_SUPPORTED;
238 static const char *ipv4_tcp_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
243 static int ipv4_tcp_get_peer_port(struct socket_context *sock, TALLOC_CTX *mem_ctx)
248 static const char *ipv4_tcp_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
253 static int ipv4_tcp_get_my_port(struct socket_context *sock, TALLOC_CTX *mem_ctx)
258 static int ipv4_tcp_get_fd(struct socket_context *sock, TALLOC_CTX *mem_ctx)
263 static const struct socket_ops ipv4_tcp_ops = {
265 .type = SOCKET_TYPE_STREAM,
267 .init = ipv4_tcp_init,
268 .connect = ipv4_tcp_connect,
269 .listen = ipv4_tcp_listen,
270 .accept = ipv4_tcp_accept,
271 .recv = ipv4_tcp_recv,
272 .send = ipv4_tcp_send,
273 .close = ipv4_tcp_close,
275 .set_option = ipv4_tcp_set_option,
277 .get_peer_addr = ipv4_tcp_get_peer_addr,
278 .get_peer_port = ipv4_tcp_get_peer_port,
279 .get_my_addr = ipv4_tcp_get_my_addr,
280 .get_my_port = ipv4_tcp_get_my_port,
282 .get_fd = ipv4_tcp_get_fd
285 const struct socket_ops *socket_ipv4_ops(void)
287 return &ipv4_tcp_ops;
290 NTSTATUS socket_ipv4_init(void)