s4-socket: use set_close_on_exec()
[kai/samba.git] / source4 / lib / socket / socket_ip.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Socket IPv4/IPv6 functions
5
6    Copyright (C) Stefan Metzmacher 2004
7    Copyright (C) Andrew Tridgell 2004-2005
8    Copyright (C) Jelmer Vernooij 2004
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "lib/socket/socket.h"
27 #include "system/network.h"
28 #include "lib/util/util_net.h"
29
30 _PUBLIC_ const struct socket_ops *socket_ipv4_ops(enum socket_type type);
31 _PUBLIC_ const struct socket_ops *socket_ipv6_ops(enum socket_type type);
32
33 static NTSTATUS ipv4_init(struct socket_context *sock)
34 {
35         int type;
36
37         switch (sock->type) {
38         case SOCKET_TYPE_STREAM:
39                 type = SOCK_STREAM;
40                 break;
41         case SOCKET_TYPE_DGRAM:
42                 type = SOCK_DGRAM;
43                 break;
44         default:
45                 return NT_STATUS_INVALID_PARAMETER;
46         }
47
48         sock->fd = socket(PF_INET, type, 0);
49         if (sock->fd == -1) {
50                 return map_nt_error_from_unix_common(errno);
51         }
52
53         set_close_on_exec(sock->fd);
54
55         sock->backend_name = "ipv4";
56         sock->family = AF_INET;
57
58         return NT_STATUS_OK;
59 }
60
61 static void ip_close(struct socket_context *sock)
62 {
63         if (sock->fd != -1) {
64                 close(sock->fd);
65                 sock->fd = -1;
66         }
67 }
68
69 static NTSTATUS ip_connect_complete(struct socket_context *sock, uint32_t flags)
70 {
71         int error=0, ret;
72         socklen_t len = sizeof(error);
73
74         /* check for any errors that may have occurred - this is needed
75            for non-blocking connect */
76         ret = getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, &error, &len);
77         if (ret == -1) {
78                 return map_nt_error_from_unix_common(errno);
79         }
80         if (error != 0) {
81                 return map_nt_error_from_unix_common(error);
82         }
83
84         if (!(flags & SOCKET_FLAG_BLOCK)) {
85                 ret = set_blocking(sock->fd, false);
86                 if (ret == -1) {
87                         return map_nt_error_from_unix_common(errno);
88                 }
89         }
90
91         sock->state = SOCKET_STATE_CLIENT_CONNECTED;
92
93         return NT_STATUS_OK;
94 }
95
96
97 static NTSTATUS ipv4_connect(struct socket_context *sock,
98                              const struct socket_address *my_address, 
99                              const struct socket_address *srv_address,
100                              uint32_t flags)
101 {
102         struct sockaddr_in srv_addr;
103         struct in_addr my_ip;
104         struct in_addr srv_ip;
105         int ret;
106
107         if (my_address && my_address->sockaddr) {
108                 ret = bind(sock->fd, my_address->sockaddr, my_address->sockaddrlen);
109                 if (ret == -1) {
110                         return map_nt_error_from_unix_common(errno);
111                 }
112         } else if (my_address) {
113                 my_ip = interpret_addr2(my_address->addr);
114                 
115                 if (my_ip.s_addr != 0 || my_address->port != 0) {
116                         struct sockaddr_in my_addr;
117                         ZERO_STRUCT(my_addr);
118 #ifdef HAVE_SOCK_SIN_LEN
119                         my_addr.sin_len         = sizeof(my_addr);
120 #endif
121                         my_addr.sin_addr.s_addr = my_ip.s_addr;
122                         my_addr.sin_port        = htons(my_address->port);
123                         my_addr.sin_family      = PF_INET;
124                         
125                         ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
126                         if (ret == -1) {
127                                 return map_nt_error_from_unix_common(errno);
128                         }
129                 }
130         }
131
132         if (srv_address->sockaddr) {
133                 ret = connect(sock->fd, srv_address->sockaddr, srv_address->sockaddrlen);
134                 if (ret == -1) {
135                         return map_nt_error_from_unix_common(errno);
136                 }
137         } else {
138                 srv_ip = interpret_addr2(srv_address->addr);
139                 if (!srv_ip.s_addr) {
140                         return NT_STATUS_BAD_NETWORK_NAME;
141                 }
142
143                 SMB_ASSERT(srv_address->port != 0);
144                 
145                 ZERO_STRUCT(srv_addr);
146 #ifdef HAVE_SOCK_SIN_LEN
147                 srv_addr.sin_len        = sizeof(srv_addr);
148 #endif
149                 srv_addr.sin_addr.s_addr= srv_ip.s_addr;
150                 srv_addr.sin_port       = htons(srv_address->port);
151                 srv_addr.sin_family     = PF_INET;
152
153                 ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
154                 if (ret == -1) {
155                         return map_nt_error_from_unix_common(errno);
156                 }
157         }
158
159         return ip_connect_complete(sock, flags);
160 }
161
162
163 /*
164   note that for simplicity of the API, socket_listen() is also
165   use for DGRAM sockets, but in reality only a bind() is done
166 */
167 static NTSTATUS ipv4_listen(struct socket_context *sock,
168                             const struct socket_address *my_address, 
169                             int queue_size, uint32_t flags)
170 {
171         struct sockaddr_in my_addr;
172         struct in_addr ip_addr;
173         int ret;
174
175         socket_set_option(sock, "SO_REUSEADDR=1", NULL);
176
177         if (my_address->sockaddr) {
178                 ret = bind(sock->fd, my_address->sockaddr, my_address->sockaddrlen);
179         } else {
180                 ip_addr = interpret_addr2(my_address->addr);
181                 
182                 ZERO_STRUCT(my_addr);
183 #ifdef HAVE_SOCK_SIN_LEN
184                 my_addr.sin_len         = sizeof(my_addr);
185 #endif
186                 my_addr.sin_addr.s_addr = ip_addr.s_addr;
187                 my_addr.sin_port        = htons(my_address->port);
188                 my_addr.sin_family      = PF_INET;
189                 
190                 ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
191         }
192
193         if (ret == -1) {
194                 return map_nt_error_from_unix_common(errno);
195         }
196
197         if (sock->type == SOCKET_TYPE_STREAM) {
198                 ret = listen(sock->fd, queue_size);
199                 if (ret == -1) {
200                         return map_nt_error_from_unix_common(errno);
201                 }
202         }
203
204         if (!(flags & SOCKET_FLAG_BLOCK)) {
205                 ret = set_blocking(sock->fd, false);
206                 if (ret == -1) {
207                         return map_nt_error_from_unix_common(errno);
208                 }
209         }
210
211         sock->state= SOCKET_STATE_SERVER_LISTEN;
212
213         return NT_STATUS_OK;
214 }
215
216 static NTSTATUS ipv4_accept(struct socket_context *sock, struct socket_context **new_sock)
217 {
218         struct sockaddr_in cli_addr;
219         socklen_t cli_addr_len = sizeof(cli_addr);
220         int new_fd;
221
222         if (sock->type != SOCKET_TYPE_STREAM) {
223                 return NT_STATUS_INVALID_PARAMETER;
224         }
225
226         new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
227         if (new_fd == -1) {
228                 return map_nt_error_from_unix_common(errno);
229         }
230
231         if (!(sock->flags & SOCKET_FLAG_BLOCK)) {
232                 int ret = set_blocking(new_fd, false);
233                 if (ret == -1) {
234                         close(new_fd);
235                         return map_nt_error_from_unix_common(errno);
236                 }
237         }
238
239         /* TODO: we could add a 'accept_check' hook here
240          *       which get the black/white lists via socket_set_accept_filter()
241          *       or something like that
242          *       --metze
243          */
244
245         (*new_sock) = talloc(NULL, struct socket_context);
246         if (!(*new_sock)) {
247                 close(new_fd);
248                 return NT_STATUS_NO_MEMORY;
249         }
250
251         /* copy the socket_context */
252         (*new_sock)->type               = sock->type;
253         (*new_sock)->state              = SOCKET_STATE_SERVER_CONNECTED;
254         (*new_sock)->flags              = sock->flags;
255
256         (*new_sock)->fd                 = new_fd;
257
258         (*new_sock)->private_data       = NULL;
259         (*new_sock)->ops                = sock->ops;
260         (*new_sock)->backend_name       = sock->backend_name;
261
262         return NT_STATUS_OK;
263 }
264
265 static NTSTATUS ip_recv(struct socket_context *sock, void *buf, 
266                               size_t wantlen, size_t *nread)
267 {
268         ssize_t gotlen;
269
270         *nread = 0;
271
272         gotlen = recv(sock->fd, buf, wantlen, 0);
273         if (gotlen == 0) {
274                 return NT_STATUS_END_OF_FILE;
275         } else if (gotlen == -1) {
276                 return map_nt_error_from_unix_common(errno);
277         }
278
279         *nread = gotlen;
280
281         return NT_STATUS_OK;
282 }
283
284
285 static NTSTATUS ipv4_recvfrom(struct socket_context *sock, void *buf, 
286                               size_t wantlen, size_t *nread, 
287                               TALLOC_CTX *addr_ctx, struct socket_address **_src)
288 {
289         ssize_t gotlen;
290         struct sockaddr_in *from_addr;
291         socklen_t from_len = sizeof(*from_addr);
292         struct socket_address *src;
293         char addrstring[INET_ADDRSTRLEN];
294         
295         src = talloc(addr_ctx, struct socket_address);
296         if (!src) {
297                 return NT_STATUS_NO_MEMORY;
298         }
299         
300         src->family = sock->backend_name;
301
302         from_addr = talloc(src, struct sockaddr_in);
303         if (!from_addr) {
304                 talloc_free(src);
305                 return NT_STATUS_NO_MEMORY;
306         }
307
308         src->sockaddr = (struct sockaddr *)from_addr;
309
310         *nread = 0;
311
312         gotlen = recvfrom(sock->fd, buf, wantlen, 0, 
313                           src->sockaddr, &from_len);
314         if (gotlen == 0) {
315                 talloc_free(src);
316                 return NT_STATUS_END_OF_FILE;
317         } else if (gotlen == -1) {
318                 talloc_free(src);
319                 return map_nt_error_from_unix_common(errno);
320         }
321
322         src->sockaddrlen = from_len;
323
324         if (inet_ntop(AF_INET, &from_addr->sin_addr, addrstring, 
325                          sizeof(addrstring)) == NULL) {
326                 talloc_free(src);
327                 return NT_STATUS_INTERNAL_ERROR;
328         }
329         src->addr = talloc_strdup(src, addrstring);
330         if (src->addr == NULL) {
331                 talloc_free(src);
332                 return NT_STATUS_NO_MEMORY;
333         }
334         src->port = ntohs(from_addr->sin_port);
335
336         *nread  = gotlen;
337         *_src   = src;
338         return NT_STATUS_OK;
339 }
340
341 static NTSTATUS ip_send(struct socket_context *sock, 
342                               const DATA_BLOB *blob, size_t *sendlen)
343 {
344         ssize_t len;
345
346         *sendlen = 0;
347
348         len = send(sock->fd, blob->data, blob->length, 0);
349         if (len == -1) {
350                 return map_nt_error_from_unix_common(errno);
351         }       
352
353         *sendlen = len;
354
355         return NT_STATUS_OK;
356 }
357
358 static NTSTATUS ipv4_sendto(struct socket_context *sock, 
359                             const DATA_BLOB *blob, size_t *sendlen, 
360                             const struct socket_address *dest_addr)
361 {
362         ssize_t len;
363
364         if (dest_addr->sockaddr) {
365                 len = sendto(sock->fd, blob->data, blob->length, 0, 
366                              dest_addr->sockaddr, dest_addr->sockaddrlen);
367         } else {
368                 struct sockaddr_in srv_addr;
369                 struct in_addr addr;
370
371                 SMB_ASSERT(dest_addr->port != 0);
372                 
373                 ZERO_STRUCT(srv_addr);
374 #ifdef HAVE_SOCK_SIN_LEN
375                 srv_addr.sin_len         = sizeof(srv_addr);
376 #endif
377                 addr                     = interpret_addr2(dest_addr->addr);
378                 if (addr.s_addr == 0) {
379                         return NT_STATUS_HOST_UNREACHABLE;
380                 }
381                 srv_addr.sin_addr.s_addr = addr.s_addr;
382                 srv_addr.sin_port        = htons(dest_addr->port);
383                 srv_addr.sin_family      = PF_INET;
384                 
385                 *sendlen = 0;
386                 
387                 len = sendto(sock->fd, blob->data, blob->length, 0, 
388                              (struct sockaddr *)&srv_addr, sizeof(srv_addr));
389         }
390         if (len == -1) {
391                 return map_nt_error_from_unix_common(errno);
392         }       
393
394         *sendlen = len;
395
396         return NT_STATUS_OK;
397 }
398
399 static NTSTATUS ipv4_set_option(struct socket_context *sock, const char *option, const char *val)
400 {
401         set_socket_options(sock->fd, option);
402         return NT_STATUS_OK;
403 }
404
405 static char *ipv4_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx)
406 {
407         struct sockaddr_in peer_addr;
408         socklen_t len = sizeof(peer_addr);
409         struct hostent *he;
410         int ret;
411
412         ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
413         if (ret == -1) {
414                 return NULL;
415         }
416
417         he = gethostbyaddr((char *)&peer_addr.sin_addr, sizeof(peer_addr.sin_addr), AF_INET);
418         if (he == NULL) {
419                 return NULL;
420         }
421
422         return talloc_strdup(mem_ctx, he->h_name);
423 }
424
425 static struct socket_address *ipv4_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
426 {
427         struct sockaddr_in *peer_addr;
428         socklen_t len = sizeof(*peer_addr);
429         struct socket_address *peer;
430         char addrstring[INET_ADDRSTRLEN];
431         int ret;
432         
433         peer = talloc(mem_ctx, struct socket_address);
434         if (!peer) {
435                 return NULL;
436         }
437         
438         peer->family = sock->backend_name;
439         peer_addr = talloc(peer, struct sockaddr_in);
440         if (!peer_addr) {
441                 talloc_free(peer);
442                 return NULL;
443         }
444
445         peer->sockaddr = (struct sockaddr *)peer_addr;
446
447         ret = getpeername(sock->fd, peer->sockaddr, &len);
448         if (ret == -1) {
449                 talloc_free(peer);
450                 return NULL;
451         }
452
453         peer->sockaddrlen = len;
454
455         if (inet_ntop(AF_INET, &peer_addr->sin_addr, addrstring,
456                          sizeof(addrstring)) == NULL) {
457                 talloc_free(peer);
458                 return NULL;
459         }
460         peer->addr = talloc_strdup(peer, addrstring);
461         if (!peer->addr) {
462                 talloc_free(peer);
463                 return NULL;
464         }
465         peer->port = ntohs(peer_addr->sin_port);
466
467         return peer;
468 }
469
470 static struct socket_address *ipv4_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
471 {
472         struct sockaddr_in *local_addr;
473         socklen_t len = sizeof(*local_addr);
474         struct socket_address *local;
475         char addrstring[INET_ADDRSTRLEN];
476         int ret;
477         
478         local = talloc(mem_ctx, struct socket_address);
479         if (!local) {
480                 return NULL;
481         }
482         
483         local->family = sock->backend_name;
484         local_addr = talloc(local, struct sockaddr_in);
485         if (!local_addr) {
486                 talloc_free(local);
487                 return NULL;
488         }
489
490         local->sockaddr = (struct sockaddr *)local_addr;
491
492         ret = getsockname(sock->fd, local->sockaddr, &len);
493         if (ret == -1) {
494                 talloc_free(local);
495                 return NULL;
496         }
497
498         local->sockaddrlen = len;
499
500         if (inet_ntop(AF_INET, &local_addr->sin_addr, addrstring, 
501                          sizeof(addrstring)) == NULL) {
502                 talloc_free(local);
503                 return NULL;
504         }
505         local->addr = talloc_strdup(local, addrstring);
506         if (!local->addr) {
507                 talloc_free(local);
508                 return NULL;
509         }
510         local->port = ntohs(local_addr->sin_port);
511
512         return local;
513 }
514 static int ip_get_fd(struct socket_context *sock)
515 {
516         return sock->fd;
517 }
518
519 static NTSTATUS ip_pending(struct socket_context *sock, size_t *npending)
520 {
521         int value = 0;
522         if (ioctl(sock->fd, FIONREAD, &value) == 0) {
523                 *npending = value;
524                 return NT_STATUS_OK;
525         }
526         return map_nt_error_from_unix_common(errno);
527 }
528
529 static const struct socket_ops ipv4_ops = {
530         .name                   = "ipv4",
531         .fn_init                = ipv4_init,
532         .fn_connect             = ipv4_connect,
533         .fn_connect_complete    = ip_connect_complete,
534         .fn_listen              = ipv4_listen,
535         .fn_accept              = ipv4_accept,
536         .fn_recv                = ip_recv,
537         .fn_recvfrom            = ipv4_recvfrom,
538         .fn_send                = ip_send,
539         .fn_sendto              = ipv4_sendto,
540         .fn_pending             = ip_pending,
541         .fn_close               = ip_close,
542
543         .fn_set_option          = ipv4_set_option,
544
545         .fn_get_peer_name       = ipv4_get_peer_name,
546         .fn_get_peer_addr       = ipv4_get_peer_addr,
547         .fn_get_my_addr         = ipv4_get_my_addr,
548
549         .fn_get_fd              = ip_get_fd
550 };
551
552 _PUBLIC_ const struct socket_ops *socket_ipv4_ops(enum socket_type type)
553 {
554         return &ipv4_ops;
555 }
556
557 #if HAVE_IPV6
558
559 static struct in6_addr interpret_addr6(const char *name)
560 {
561         char addr[INET6_ADDRSTRLEN];
562         struct in6_addr dest6;
563         const char *sp = name;
564         char *p;
565         int ret;
566
567         if (sp == NULL) return in6addr_any;
568
569         p = strchr_m(sp, '%');
570
571         if (strcasecmp(sp, "localhost") == 0) {
572                 sp = "::1";
573         }
574
575         /*
576          * Cope with link-local.
577          * This is IP:v6:addr%ifname.
578          */
579
580         if (p && (p > sp) && (if_nametoindex(p+1) != 0)) {
581                 strlcpy(addr, sp,
582                         MIN(PTR_DIFF(p,sp)+1,
583                                 sizeof(addr)));
584                 sp = addr;
585         }
586
587         ret = inet_pton(AF_INET6, sp, &dest6);
588         if (ret > 0) {
589                 return dest6;
590         }
591
592         return in6addr_any;
593 }
594
595 static NTSTATUS ipv6_init(struct socket_context *sock)
596 {
597         int type;
598
599         switch (sock->type) {
600         case SOCKET_TYPE_STREAM:
601                 type = SOCK_STREAM;
602                 break;
603         case SOCKET_TYPE_DGRAM:
604                 type = SOCK_DGRAM;
605                 break;
606         default:
607                 return NT_STATUS_INVALID_PARAMETER;
608         }
609
610         sock->fd = socket(PF_INET6, type, 0);
611         if (sock->fd == -1) {
612                 return map_nt_error_from_unix_common(errno);
613         }
614
615         set_close_on_exec(sock->fd);
616
617         sock->backend_name = "ipv6";
618         sock->family = AF_INET6;
619
620         return NT_STATUS_OK;
621 }
622
623 static NTSTATUS ipv6_tcp_connect(struct socket_context *sock,
624                                  const struct socket_address *my_address,
625                                  const struct socket_address *srv_address,
626                                  uint32_t flags)
627 {
628         int ret;
629
630         if (my_address && my_address->sockaddr) {
631                 ret = bind(sock->fd, my_address->sockaddr, my_address->sockaddrlen);
632                 if (ret == -1) {
633                         return map_nt_error_from_unix_common(errno);
634                 }
635         } else if (my_address) {
636                 struct in6_addr my_ip;
637                 my_ip = interpret_addr6(my_address->addr);
638
639                 if (memcmp(&my_ip, &in6addr_any, sizeof(my_ip)) || my_address->port != 0) {
640                         struct sockaddr_in6 my_addr;
641                         ZERO_STRUCT(my_addr);
642                         my_addr.sin6_addr       = my_ip;
643                         my_addr.sin6_port       = htons(my_address->port);
644                         my_addr.sin6_family     = PF_INET6;
645                         
646                         ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
647                         if (ret == -1) {
648                                 return map_nt_error_from_unix_common(errno);
649                         }
650                 }
651         }
652
653         if (srv_address->sockaddr) {
654                 ret = connect(sock->fd, srv_address->sockaddr, srv_address->sockaddrlen);
655         } else {
656                 struct in6_addr srv_ip;
657                 struct sockaddr_in6 srv_addr;
658                 srv_ip = interpret_addr6(srv_address->addr);
659                 if (memcmp(&srv_ip, &in6addr_any, sizeof(srv_ip)) == 0) {
660                         return NT_STATUS_BAD_NETWORK_NAME;
661                 }
662                 
663                 ZERO_STRUCT(srv_addr);
664                 srv_addr.sin6_addr      = srv_ip;
665                 srv_addr.sin6_port      = htons(srv_address->port);
666                 srv_addr.sin6_family    = PF_INET6;
667                 
668                 ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
669         }
670         if (ret == -1) {
671                 return map_nt_error_from_unix_common(errno);
672         }
673
674         return ip_connect_complete(sock, flags);
675 }
676
677 /*
678   fix the sin6_scope_id based on the address interface
679  */
680 static void fix_scope_id(struct sockaddr_in6 *in6,
681                          const char *address)
682 {
683         const char *p = strchr(address, '%');
684         if (p != NULL) {
685                 in6->sin6_scope_id = if_nametoindex(p+1);
686         }
687 }
688
689
690 static NTSTATUS ipv6_listen(struct socket_context *sock,
691                             const struct socket_address *my_address,
692                             int queue_size, uint32_t flags)
693 {
694         struct sockaddr_in6 my_addr;
695         struct in6_addr ip_addr;
696         int ret;
697
698         socket_set_option(sock, "SO_REUSEADDR=1", NULL);
699
700         if (my_address->sockaddr) {
701                 ret = bind(sock->fd, my_address->sockaddr, my_address->sockaddrlen);
702         } else {
703                 int one = 1;
704                 ip_addr = interpret_addr6(my_address->addr);
705                 
706                 ZERO_STRUCT(my_addr);
707                 my_addr.sin6_addr       = ip_addr;
708                 my_addr.sin6_port       = htons(my_address->port);
709                 my_addr.sin6_family     = PF_INET6;
710                 fix_scope_id(&my_addr, my_address->addr);
711
712                 /* when binding on ipv6 we always want to only bind on v6 */
713                 ret = setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY,
714                                  (const void *)&one, sizeof(one));
715                 if (ret != -1) {
716                         ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
717                 }
718         }
719
720         if (ret == -1) {
721                 return map_nt_error_from_unix_common(errno);
722         }
723
724         if (sock->type == SOCKET_TYPE_STREAM) {
725                 ret = listen(sock->fd, queue_size);
726                 if (ret == -1) {
727                         return map_nt_error_from_unix_common(errno);
728                 }
729         }
730
731         if (!(flags & SOCKET_FLAG_BLOCK)) {
732                 ret = set_blocking(sock->fd, false);
733                 if (ret == -1) {
734                         return map_nt_error_from_unix_common(errno);
735                 }
736         }
737
738         sock->state= SOCKET_STATE_SERVER_LISTEN;
739
740         return NT_STATUS_OK;
741 }
742
743 static NTSTATUS ipv6_tcp_accept(struct socket_context *sock, struct socket_context **new_sock)
744 {
745         struct sockaddr_in6 cli_addr;
746         socklen_t cli_addr_len = sizeof(cli_addr);
747         int new_fd;
748         
749         if (sock->type != SOCKET_TYPE_STREAM) {
750                 return NT_STATUS_INVALID_PARAMETER;
751         }
752
753         new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
754         if (new_fd == -1) {
755                 return map_nt_error_from_unix_common(errno);
756         }
757
758         if (!(sock->flags & SOCKET_FLAG_BLOCK)) {
759                 int ret = set_blocking(new_fd, false);
760                 if (ret == -1) {
761                         close(new_fd);
762                         return map_nt_error_from_unix_common(errno);
763                 }
764         }
765
766         /* TODO: we could add a 'accept_check' hook here
767          *       which get the black/white lists via socket_set_accept_filter()
768          *       or something like that
769          *       --metze
770          */
771
772         (*new_sock) = talloc(NULL, struct socket_context);
773         if (!(*new_sock)) {
774                 close(new_fd);
775                 return NT_STATUS_NO_MEMORY;
776         }
777
778         /* copy the socket_context */
779         (*new_sock)->type               = sock->type;
780         (*new_sock)->state              = SOCKET_STATE_SERVER_CONNECTED;
781         (*new_sock)->flags              = sock->flags;
782
783         (*new_sock)->fd                 = new_fd;
784
785         (*new_sock)->private_data       = NULL;
786         (*new_sock)->ops                = sock->ops;
787         (*new_sock)->backend_name       = sock->backend_name;
788
789         return NT_STATUS_OK;
790 }
791
792 static NTSTATUS ipv6_recvfrom(struct socket_context *sock, void *buf, 
793                               size_t wantlen, size_t *nread, 
794                               TALLOC_CTX *addr_ctx, struct socket_address **_src)
795 {
796         ssize_t gotlen;
797         struct sockaddr_in6 *from_addr;
798         socklen_t from_len = sizeof(*from_addr);
799         struct socket_address *src;
800         char addrstring[INET6_ADDRSTRLEN];
801         
802         src = talloc(addr_ctx, struct socket_address);
803         if (!src) {
804                 return NT_STATUS_NO_MEMORY;
805         }
806         
807         src->family = sock->backend_name;
808
809         from_addr = talloc(src, struct sockaddr_in6);
810         if (!from_addr) {
811                 talloc_free(src);
812                 return NT_STATUS_NO_MEMORY;
813         }
814
815         src->sockaddr = (struct sockaddr *)from_addr;
816
817         *nread = 0;
818
819         gotlen = recvfrom(sock->fd, buf, wantlen, 0, 
820                           src->sockaddr, &from_len);
821         if (gotlen == 0) {
822                 talloc_free(src);
823                 return NT_STATUS_END_OF_FILE;
824         } else if (gotlen == -1) {
825                 talloc_free(src);
826                 return map_nt_error_from_unix_common(errno);
827         }
828
829         src->sockaddrlen = from_len;
830
831         if (inet_ntop(AF_INET6, &from_addr->sin6_addr, addrstring, sizeof(addrstring)) == NULL) {
832                 DEBUG(0, ("Unable to convert address to string: %s\n", strerror(errno)));
833                 talloc_free(src);
834                 return NT_STATUS_INTERNAL_ERROR;
835         }
836
837         src->addr = talloc_strdup(src, addrstring);
838         if (src->addr == NULL) {
839                 talloc_free(src);
840                 return NT_STATUS_NO_MEMORY;
841         }
842         src->port = ntohs(from_addr->sin6_port);
843
844         *nread  = gotlen;
845         *_src   = src;
846         return NT_STATUS_OK;
847 }
848
849 static NTSTATUS ipv6_sendto(struct socket_context *sock, 
850                             const DATA_BLOB *blob, size_t *sendlen, 
851                             const struct socket_address *dest_addr)
852 {
853         ssize_t len;
854
855         if (dest_addr->sockaddr) {
856                 len = sendto(sock->fd, blob->data, blob->length, 0, 
857                              dest_addr->sockaddr, dest_addr->sockaddrlen);
858         } else {
859                 struct sockaddr_in6 srv_addr;
860                 struct in6_addr addr;
861                 
862                 ZERO_STRUCT(srv_addr);
863                 addr                     = interpret_addr6(dest_addr->addr);
864                 if (addr.s6_addr == 0) {
865                         return NT_STATUS_HOST_UNREACHABLE;
866                 }
867                 srv_addr.sin6_addr = addr;
868                 srv_addr.sin6_port        = htons(dest_addr->port);
869                 srv_addr.sin6_family      = PF_INET6;
870                 
871                 *sendlen = 0;
872                 
873                 len = sendto(sock->fd, blob->data, blob->length, 0, 
874                              (struct sockaddr *)&srv_addr, sizeof(srv_addr));
875         }
876         if (len == -1) {
877                 return map_nt_error_from_unix_common(errno);
878         }       
879
880         *sendlen = len;
881
882         return NT_STATUS_OK;
883 }
884
885 static NTSTATUS ipv6_set_option(struct socket_context *sock, const char *option, const char *val)
886 {
887         set_socket_options(sock->fd, option);
888         return NT_STATUS_OK;
889 }
890
891 static char *ipv6_tcp_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx)
892 {
893         struct sockaddr_in6 peer_addr;
894         socklen_t len = sizeof(peer_addr);
895         struct hostent *he;
896         int ret;
897
898         ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
899         if (ret == -1) {
900                 return NULL;
901         }
902
903         he = gethostbyaddr((char *)&peer_addr.sin6_addr, sizeof(peer_addr.sin6_addr), AF_INET6);
904         if (he == NULL) {
905                 return NULL;
906         }
907
908         return talloc_strdup(mem_ctx, he->h_name);
909 }
910
911 static struct socket_address *ipv6_tcp_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
912 {
913         struct sockaddr_in6 *peer_addr;
914         socklen_t len = sizeof(*peer_addr);
915         struct socket_address *peer;
916         int ret;
917         char addr[128];
918         const char *addr_ret;
919         
920         peer = talloc(mem_ctx, struct socket_address);
921         if (!peer) {
922                 return NULL;
923         }
924         
925         peer->family = sock->backend_name;
926         peer_addr = talloc(peer, struct sockaddr_in6);
927         if (!peer_addr) {
928                 talloc_free(peer);
929                 return NULL;
930         }
931
932         peer->sockaddr = (struct sockaddr *)peer_addr;
933
934         ret = getpeername(sock->fd, peer->sockaddr, &len);
935         if (ret == -1) {
936                 talloc_free(peer);
937                 return NULL;
938         }
939
940         peer->sockaddrlen = len;
941
942         addr_ret = inet_ntop(AF_INET6, &peer_addr->sin6_addr, addr, sizeof(addr));
943         if (addr_ret == NULL) {
944                 talloc_free(peer);
945                 return NULL;
946         }
947
948         peer->addr = talloc_strdup(peer, addr_ret);
949         if (peer->addr == NULL) {
950                 talloc_free(peer);
951                 return NULL;
952         }
953
954         peer->port = ntohs(peer_addr->sin6_port);
955
956         return peer;
957 }
958
959 static struct socket_address *ipv6_tcp_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
960 {
961         struct sockaddr_in6 *local_addr;
962         socklen_t len = sizeof(*local_addr);
963         struct socket_address *local;
964         int ret;
965         char addrstring[INET6_ADDRSTRLEN];
966         
967         local = talloc(mem_ctx, struct socket_address);
968         if (!local) {
969                 return NULL;
970         }
971         
972         local->family = sock->backend_name;
973         local_addr = talloc(local, struct sockaddr_in6);
974         if (!local_addr) {
975                 talloc_free(local);
976                 return NULL;
977         }
978
979         local->sockaddr = (struct sockaddr *)local_addr;
980
981         ret = getsockname(sock->fd, local->sockaddr, &len);
982         if (ret == -1) {
983                 talloc_free(local);
984                 return NULL;
985         }
986
987         local->sockaddrlen = len;
988
989         if (inet_ntop(AF_INET6, &local_addr->sin6_addr, addrstring, 
990                        sizeof(addrstring)) == NULL) {
991                 DEBUG(0, ("Unable to convert address to string: %s\n", 
992                           strerror(errno)));
993                 talloc_free(local);
994                 return NULL;
995         }
996         
997         local->addr = talloc_strdup(mem_ctx, addrstring);
998         if (!local->addr) {
999                 talloc_free(local);
1000                 return NULL;
1001         }
1002         local->port = ntohs(local_addr->sin6_port);
1003
1004         return local;
1005 }
1006
1007 static const struct socket_ops ipv6_tcp_ops = {
1008         .name                   = "ipv6",
1009         .fn_init                = ipv6_init,
1010         .fn_connect             = ipv6_tcp_connect,
1011         .fn_connect_complete    = ip_connect_complete,
1012         .fn_listen              = ipv6_listen,
1013         .fn_accept              = ipv6_tcp_accept,
1014         .fn_recv                = ip_recv,
1015         .fn_recvfrom            = ipv6_recvfrom,
1016         .fn_send                = ip_send,
1017         .fn_sendto              = ipv6_sendto,
1018         .fn_pending             = ip_pending,
1019         .fn_close               = ip_close,
1020
1021         .fn_set_option          = ipv6_set_option,
1022
1023         .fn_get_peer_name       = ipv6_tcp_get_peer_name,
1024         .fn_get_peer_addr       = ipv6_tcp_get_peer_addr,
1025         .fn_get_my_addr         = ipv6_tcp_get_my_addr,
1026
1027         .fn_get_fd              = ip_get_fd
1028 };
1029
1030 _PUBLIC_ const struct socket_ops *socket_ipv6_ops(enum socket_type type)
1031 {
1032         return &ipv6_tcp_ops;
1033 }
1034
1035 #endif