da3a69e81eaa260c1a1bbbe09a65d08829a58baf
[bbaumbach/samba-autobuild/.git] / source4 / lib / socket / socket_ipv6.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Socket IPv6 functions
4    Copyright (C) Stefan Metzmacher 2004
5    Copyright (C) Jelmer Vernooij 2004
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "lib/socket/socket.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26
27 static struct in6_addr interpret_addr6(const char *name)
28 {
29         struct hostent *he;
30         
31         if (name == NULL) return in6addr_any;
32         
33         he = gethostbyname2(name, PF_INET6);
34
35         if (he == NULL) return in6addr_any;
36
37         return *((struct in6_addr *)he->h_addr);
38 }
39
40 static NTSTATUS ipv6_tcp_init(struct socket_context *sock)
41 {
42         sock->fd = socket(PF_INET6, SOCK_STREAM, 0);
43         if (sock->fd == -1) {
44                 return map_nt_error_from_unix(errno);
45         }
46
47         sock->backend_name = "ipv6";
48
49         return NT_STATUS_OK;
50 }
51
52 static void ipv6_tcp_close(struct socket_context *sock)
53 {
54         close(sock->fd);
55 }
56
57 static NTSTATUS ipv6_tcp_connect_complete(struct socket_context *sock, uint32_t flags)
58 {
59         int error=0, ret;
60         socklen_t len = sizeof(error);
61
62         /* check for any errors that may have occurred - this is needed
63            for non-blocking connect */
64         ret = getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, &error, &len);
65         if (ret == -1) {
66                 return map_nt_error_from_unix(errno);
67         }
68         if (error != 0) {
69                 return map_nt_error_from_unix(error);
70         }
71
72         if (!(flags & SOCKET_FLAG_BLOCK)) {
73                 ret = set_blocking(sock->fd, False);
74                 if (ret == -1) {
75                         return map_nt_error_from_unix(errno);
76                 }
77         }
78
79         sock->state = SOCKET_STATE_CLIENT_CONNECTED;
80
81         return NT_STATUS_OK;
82 }
83
84 static NTSTATUS ipv6_tcp_connect(struct socket_context *sock,
85                                  const char *my_address, int my_port,
86                                  const char *srv_address, int srv_port,
87                                  uint32_t flags)
88 {
89         struct sockaddr_in6 srv_addr;
90         struct in6_addr my_ip;
91         struct in6_addr srv_ip;
92         int ret;
93
94         my_ip = interpret_addr6(my_address);
95
96         if (memcmp(&my_ip, &in6addr_any, sizeof(my_ip)) || my_port != 0) {
97                 struct sockaddr_in6 my_addr;
98                 ZERO_STRUCT(my_addr);
99                 my_addr.sin6_addr       = my_ip;
100                 my_addr.sin6_port       = htons(my_port);
101                 my_addr.sin6_family     = PF_INET6;
102                 
103                 ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
104                 if (ret == -1) {
105                         return map_nt_error_from_unix(errno);
106                 }
107         }
108
109         srv_ip = interpret_addr6(srv_address);
110         if (memcmp(&srv_ip, &in6addr_any, sizeof(srv_ip)) == 0) {
111                 return NT_STATUS_BAD_NETWORK_NAME;
112         }
113
114         ZERO_STRUCT(srv_addr);
115         srv_addr.sin6_addr      = srv_ip;
116         srv_addr.sin6_port      = htons(srv_port);
117         srv_addr.sin6_family    = PF_INET6;
118
119         ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
120         if (ret == -1) {
121                 return map_nt_error_from_unix(errno);
122         }
123
124         return ipv6_tcp_connect_complete(sock, flags);
125 }
126
127 static NTSTATUS ipv6_tcp_listen(struct socket_context *sock,
128                                         const char *my_address, int port,
129                                         int queue_size, uint32_t flags)
130 {
131         struct sockaddr_in6 my_addr;
132         struct in6_addr ip_addr;
133         int ret;
134
135         socket_set_option(sock, "SO_REUSEADDR=1", NULL);
136
137         ip_addr = interpret_addr6(my_address);
138
139         ZERO_STRUCT(my_addr);
140         my_addr.sin6_addr       = ip_addr;
141         my_addr.sin6_port       = htons(port);
142         my_addr.sin6_family     = PF_INET6;
143
144         ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
145         if (ret == -1) {
146                 return map_nt_error_from_unix(errno);
147         }
148
149         ret = listen(sock->fd, queue_size);
150         if (ret == -1) {
151                 return map_nt_error_from_unix(errno);
152         }
153
154         if (!(flags & SOCKET_FLAG_BLOCK)) {
155                 ret = set_blocking(sock->fd, False);
156                 if (ret == -1) {
157                         return map_nt_error_from_unix(errno);
158                 }
159         }
160
161         sock->state= SOCKET_STATE_SERVER_LISTEN;
162
163         return NT_STATUS_OK;
164 }
165
166 static NTSTATUS ipv6_tcp_accept(struct socket_context *sock, struct socket_context **new_sock)
167 {
168         struct sockaddr_in cli_addr;
169         socklen_t cli_addr_len = sizeof(cli_addr);
170         int new_fd;
171
172         new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
173         if (new_fd == -1) {
174                 return map_nt_error_from_unix(errno);
175         }
176
177         if (!(sock->flags & SOCKET_FLAG_BLOCK)) {
178                 int ret = set_blocking(new_fd, False);
179                 if (ret == -1) {
180                         close(new_fd);
181                         return map_nt_error_from_unix(errno);
182                 }
183         }
184
185         /* TODO: we could add a 'accept_check' hook here
186          *       which get the black/white lists via socket_set_accept_filter()
187          *       or something like that
188          *       --metze
189          */
190
191         (*new_sock) = talloc(NULL, struct socket_context);
192         if (!(*new_sock)) {
193                 close(new_fd);
194                 return NT_STATUS_NO_MEMORY;
195         }
196
197         /* copy the socket_context */
198         (*new_sock)->type               = sock->type;
199         (*new_sock)->state              = SOCKET_STATE_SERVER_CONNECTED;
200         (*new_sock)->flags              = sock->flags;
201
202         (*new_sock)->fd                 = new_fd;
203
204         (*new_sock)->private_data       = NULL;
205         (*new_sock)->ops                = sock->ops;
206
207         return NT_STATUS_OK;
208 }
209
210 static NTSTATUS ipv6_tcp_recv(struct socket_context *sock, void *buf, 
211                               size_t wantlen, size_t *nread, uint32_t flags)
212 {
213         ssize_t gotlen;
214         int flgs = 0;
215
216         /* TODO: we need to map all flags here */
217         if (flags & SOCKET_FLAG_PEEK) {
218                 flgs |= MSG_PEEK;
219         }
220
221         if (flags & SOCKET_FLAG_BLOCK) {
222                 flgs |= MSG_WAITALL;
223         }
224
225         *nread = 0;
226
227         gotlen = recv(sock->fd, buf, wantlen, flgs);
228         if (gotlen == 0) {
229                 return NT_STATUS_END_OF_FILE;
230         } else if (gotlen == -1) {
231                 return map_nt_error_from_unix(errno);
232         }
233
234         *nread = gotlen;
235
236         return NT_STATUS_OK;
237 }
238
239 static NTSTATUS ipv6_tcp_send(struct socket_context *sock, 
240                               const DATA_BLOB *blob, size_t *sendlen, uint32_t flags)
241 {
242         ssize_t len;
243         int flgs = 0;
244
245         *sendlen = 0;
246
247         len = send(sock->fd, blob->data, blob->length, flgs);
248         if (len == -1) {
249                 return map_nt_error_from_unix(errno);
250         }       
251
252         *sendlen = len;
253
254         return NT_STATUS_OK;
255 }
256
257 static NTSTATUS ipv6_tcp_set_option(struct socket_context *sock, const char *option, const char *val)
258 {
259         set_socket_options(sock->fd, option);
260         return NT_STATUS_OK;
261 }
262
263 static char *ipv6_tcp_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx)
264 {
265         struct sockaddr_in6 peer_addr;
266         socklen_t len = sizeof(peer_addr);
267         struct hostent *he;
268         int ret;
269
270         ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
271         if (ret == -1) {
272                 return NULL;
273         }
274
275         he = gethostbyaddr((char *)&peer_addr.sin6_addr, sizeof(peer_addr.sin6_addr), AF_INET6);
276         if (he == NULL) {
277                 return NULL;
278         }
279
280         return talloc_strdup(mem_ctx, he->h_name);
281 }
282
283 static char *ipv6_tcp_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
284 {
285         struct sockaddr_in6 peer_addr;
286         socklen_t len = sizeof(peer_addr);
287         int ret;
288         struct hostent *he;
289
290         ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
291         if (ret == -1) {
292                 return NULL;
293         }
294
295         he = gethostbyaddr((char *)&peer_addr.sin6_addr, sizeof(peer_addr.sin6_addr), AF_INET6);
296
297         if (!he || !he->h_name) {
298                 return NULL;
299         }
300         
301         return talloc_strdup(mem_ctx, he->h_name);
302 }
303
304 static int ipv6_tcp_get_peer_port(struct socket_context *sock)
305 {
306         struct sockaddr_in6 peer_addr;
307         socklen_t len = sizeof(peer_addr);
308         int ret;
309
310         ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
311         if (ret == -1) {
312                 return -1;
313         }
314
315         return ntohs(peer_addr.sin6_port);
316 }
317
318 static char *ipv6_tcp_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
319 {
320         struct sockaddr_in6 my_addr;
321         socklen_t len = sizeof(my_addr);
322         int ret;
323         struct hostent *he;
324
325         ret = getsockname(sock->fd, (struct sockaddr *)&my_addr, &len);
326         if (ret == -1) {
327                 return NULL;
328         }
329
330         he = gethostbyaddr((char *)&my_addr.sin6_addr, sizeof(my_addr.sin6_addr), AF_INET6);
331         if (he == NULL) {
332                 return NULL;
333         }
334
335         return talloc_strdup(mem_ctx, he->h_name);
336 }
337
338 static int ipv6_tcp_get_my_port(struct socket_context *sock)
339 {
340         struct sockaddr_in6 my_addr;
341         socklen_t len = sizeof(my_addr);
342         int ret;
343
344         ret = getsockname(sock->fd, (struct sockaddr *)&my_addr, &len);
345         if (ret == -1) {
346                 return -1;
347         }
348
349         return ntohs(my_addr.sin6_port);
350 }
351
352 static int ipv6_tcp_get_fd(struct socket_context *sock)
353 {
354         return sock->fd;
355 }
356
357 static const struct socket_ops ipv6_tcp_ops = {
358         .name                   = "ipv6",
359         .fn_init                = ipv6_tcp_init,
360         .fn_connect             = ipv6_tcp_connect,
361         .fn_connect_complete    = ipv6_tcp_connect_complete,
362         .fn_listen              = ipv6_tcp_listen,
363         .fn_accept              = ipv6_tcp_accept,
364         .fn_recv                = ipv6_tcp_recv,
365         .fn_send                = ipv6_tcp_send,
366         .fn_close               = ipv6_tcp_close,
367
368         .fn_set_option          = ipv6_tcp_set_option,
369
370         .fn_get_peer_name       = ipv6_tcp_get_peer_name,
371         .fn_get_peer_addr       = ipv6_tcp_get_peer_addr,
372         .fn_get_peer_port       = ipv6_tcp_get_peer_port,
373         .fn_get_my_addr         = ipv6_tcp_get_my_addr,
374         .fn_get_my_port         = ipv6_tcp_get_my_port,
375
376         .fn_get_fd              = ipv6_tcp_get_fd
377 };
378
379 const struct socket_ops *socket_ipv6_ops(enum socket_type type)
380 {
381         if (type != SOCKET_TYPE_STREAM) {
382                 return NULL;
383         }
384         return &ipv6_tcp_ops;
385 }