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.
22 #include "system/network.h"
24 static NTSTATUS ipv4_tcp_init(struct socket_context *sock)
26 sock->fd = socket(PF_INET, SOCK_STREAM, 0);
28 return map_nt_error_from_unix(errno);
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 srv_addr;
45 struct ipv4_addr my_ip;
46 struct ipv4_addr srv_ip;
49 my_ip = interpret_addr2(my_address);
51 if (my_ip.s_addr != 0 || my_port != 0) {
52 struct sockaddr_in my_addr;
54 #ifdef HAVE_SOCK_SIN_LEN
55 my_addr.sin_len = sizeof(my_addr);
57 my_addr.sin_addr.s_addr = my_ip.s_addr;
58 my_addr.sin_port = htons(my_port);
59 my_addr.sin_family = PF_INET;
61 ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
63 return map_nt_error_from_unix(errno);
67 srv_ip = interpret_addr2(srv_address);
69 ZERO_STRUCT(srv_addr);
70 #ifdef HAVE_SOCK_SIN_LEN
71 srv_addr.sin_len = sizeof(srv_addr);
73 srv_addr.sin_addr.s_addr= srv_ip.s_addr;
74 srv_addr.sin_port = htons(srv_port);
75 srv_addr.sin_family = PF_INET;
77 ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
79 return map_nt_error_from_unix(errno);
82 if (!(flags & SOCKET_FLAG_BLOCK)) {
83 ret = set_blocking(sock->fd, False);
85 return map_nt_error_from_unix(errno);
89 sock->state = SOCKET_STATE_CLIENT_CONNECTED;
94 static NTSTATUS ipv4_tcp_listen(struct socket_context *sock,
95 const char *my_address, int port,
96 int queue_size, uint32_t flags)
98 struct sockaddr_in my_addr;
99 struct ipv4_addr ip_addr;
102 ip_addr = interpret_addr2(my_address);
104 ZERO_STRUCT(my_addr);
105 #ifdef HAVE_SOCK_SIN_LEN
106 my_addr.sin_len = sizeof(my_addr);
108 my_addr.sin_addr.s_addr = ip_addr.s_addr;
109 my_addr.sin_port = htons(port);
110 my_addr.sin_family = PF_INET;
112 ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
114 return map_nt_error_from_unix(errno);
117 ret = listen(sock->fd, queue_size);
119 return map_nt_error_from_unix(errno);
122 if (!(flags & SOCKET_FLAG_BLOCK)) {
123 ret = set_blocking(sock->fd, False);
125 return map_nt_error_from_unix(errno);
129 sock->state= SOCKET_STATE_SERVER_LISTEN;
134 static NTSTATUS ipv4_tcp_accept(struct socket_context *sock, struct socket_context **new_sock)
136 struct sockaddr_in cli_addr;
137 socklen_t cli_addr_len = sizeof(cli_addr);
140 new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
142 return map_nt_error_from_unix(errno);
145 if (!(sock->flags & SOCKET_FLAG_BLOCK)) {
146 int ret = set_blocking(new_fd, False);
149 return map_nt_error_from_unix(errno);
153 /* TODO: we could add a 'accept_check' hook here
154 * which get the black/white lists via socket_set_accept_filter()
155 * or something like that
159 (*new_sock) = talloc_p(NULL, struct socket_context);
162 return NT_STATUS_NO_MEMORY;
165 /* copy the socket_context */
166 (*new_sock)->type = sock->type;
167 (*new_sock)->state = SOCKET_STATE_SERVER_CONNECTED;
168 (*new_sock)->flags = sock->flags;
170 (*new_sock)->fd = new_fd;
172 (*new_sock)->private_data = NULL;
173 (*new_sock)->ops = sock->ops;
178 static NTSTATUS ipv4_tcp_recv(struct socket_context *sock, void *buf,
179 size_t wantlen, size_t *nread, uint32_t flags)
184 /* TODO: we need to map all flags here */
185 if (flags & SOCKET_FLAG_PEEK) {
189 if (flags & SOCKET_FLAG_BLOCK) {
195 gotlen = recv(sock->fd, buf, wantlen, flgs);
197 return NT_STATUS_END_OF_FILE;
198 } else if (gotlen == -1) {
199 return map_nt_error_from_unix(errno);
207 static NTSTATUS ipv4_tcp_send(struct socket_context *sock,
208 const DATA_BLOB *blob, size_t *sendlen, uint32_t flags)
215 len = send(sock->fd, blob->data, blob->length, flgs);
217 return map_nt_error_from_unix(errno);
225 static NTSTATUS ipv4_tcp_set_option(struct socket_context *sock, const char *option, const char *val)
227 set_socket_options(sock->fd, option);
231 static char *ipv4_tcp_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx)
233 struct sockaddr_in peer_addr;
234 socklen_t len = sizeof(peer_addr);
238 ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
243 he = gethostbyaddr((char *)&peer_addr.sin_addr, sizeof(peer_addr.sin_addr), AF_INET);
248 return talloc_strdup(mem_ctx, he->h_name);
251 static char *ipv4_tcp_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
253 struct sockaddr_in peer_addr;
254 socklen_t len = sizeof(peer_addr);
257 ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
262 return talloc_strdup(mem_ctx, inet_ntoa(peer_addr.sin_addr));
265 static int ipv4_tcp_get_peer_port(struct socket_context *sock)
267 struct sockaddr_in peer_addr;
268 socklen_t len = sizeof(peer_addr);
271 ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
276 return ntohs(peer_addr.sin_port);
279 static char *ipv4_tcp_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
281 struct sockaddr_in my_addr;
282 socklen_t len = sizeof(my_addr);
285 ret = getsockname(sock->fd, (struct sockaddr *)&my_addr, &len);
290 return talloc_strdup(mem_ctx, inet_ntoa(my_addr.sin_addr));
293 static int ipv4_tcp_get_my_port(struct socket_context *sock)
295 struct sockaddr_in my_addr;
296 socklen_t len = sizeof(my_addr);
299 ret = getsockname(sock->fd, (struct sockaddr *)&my_addr, &len);
304 return ntohs(my_addr.sin_port);
307 static int ipv4_tcp_get_fd(struct socket_context *sock)
312 static const struct socket_ops ipv4_tcp_ops = {
314 .type = SOCKET_TYPE_STREAM,
316 .init = ipv4_tcp_init,
317 .connect = ipv4_tcp_connect,
318 .listen = ipv4_tcp_listen,
319 .accept = ipv4_tcp_accept,
320 .recv = ipv4_tcp_recv,
321 .send = ipv4_tcp_send,
322 .close = ipv4_tcp_close,
324 .set_option = ipv4_tcp_set_option,
326 .get_peer_name = ipv4_tcp_get_peer_name,
327 .get_peer_addr = ipv4_tcp_get_peer_addr,
328 .get_peer_port = ipv4_tcp_get_peer_port,
329 .get_my_addr = ipv4_tcp_get_my_addr,
330 .get_my_port = ipv4_tcp_get_my_port,
332 .get_fd = ipv4_tcp_get_fd
335 const struct socket_ops *socket_ipv4_ops(void)
337 return &ipv4_tcp_ops;
340 NTSTATUS socket_ipv4_init(void)