]> git.meshlink.io Git - catta/blob - src/socket.c
socket.c needs closesocket from internal.h
[catta] / src / socket.c
1 /***
2   This file is part of catta.
3
4   catta is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   catta is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with catta; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <inttypes.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sys/time.h>
31 #include <sys/ioctl.h>
32 #ifdef HAVE_SYS_FILIO_H
33 #include <sys/filio.h>
34 #endif
35 #include <assert.h>
36
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <net/if.h>
42 #include <sys/uio.h>
43
44 #ifdef IP_RECVIF
45 #include <net/if_dl.h>
46 #endif
47
48 #include <catta/log.h>
49 #include "dns.h"
50 #include "fdutil.h"
51 #include "socket.h"
52 #include "addr-util.h"
53 #include "internal.h"
54
55 /* this is a portability hack */
56 #ifndef IPV6_ADD_MEMBERSHIP
57 #ifdef  IPV6_JOIN_GROUP
58 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
59 #endif
60 #endif
61
62 #ifndef IPV6_DROP_MEMBERSHIP
63 #ifdef  IPV6_LEAVE_GROUP
64 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
65 #endif
66 #endif
67
68 static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
69     assert(ret_sa);
70
71     memset(ret_sa, 0, sizeof(struct sockaddr_in));
72     ret_sa->sin_family = AF_INET;
73     ret_sa->sin_port = htons(CATTA_MDNS_PORT);
74     inet_pton(AF_INET, CATTA_IPV4_MCAST_GROUP, &ret_sa->sin_addr);
75 }
76
77 static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) {
78     assert(ret_sa);
79
80     memset(ret_sa, 0, sizeof(struct sockaddr_in6));
81     ret_sa->sin6_family = AF_INET6;
82     ret_sa->sin6_port = htons(CATTA_MDNS_PORT);
83     inet_pton(AF_INET6, CATTA_IPV6_MCAST_GROUP, &ret_sa->sin6_addr);
84 }
85
86 static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const CattaIPv4Address *a, uint16_t port) {
87     assert(ret_sa);
88     assert(a);
89     assert(port > 0);
90
91     memset(ret_sa, 0, sizeof(struct sockaddr_in));
92     ret_sa->sin_family = AF_INET;
93     ret_sa->sin_port = htons(port);
94     memcpy(&ret_sa->sin_addr, a, sizeof(CattaIPv4Address));
95 }
96
97 static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const CattaIPv6Address *a, uint16_t port) {
98     assert(ret_sa);
99     assert(a);
100     assert(port > 0);
101
102     memset(ret_sa, 0, sizeof(struct sockaddr_in6));
103     ret_sa->sin6_family = AF_INET6;
104     ret_sa->sin6_port = htons(port);
105     memcpy(&ret_sa->sin6_addr, a, sizeof(CattaIPv6Address));
106 }
107
108 int catta_mdns_mcast_join_ipv4(int fd, const CattaIPv4Address *a, int idx, int join) {
109 #ifdef HAVE_STRUCT_IP_MREQN
110     struct ip_mreqn mreq;
111 #else
112     struct ip_mreq mreq;
113 #endif
114     struct sockaddr_in sa;
115
116     assert(fd >= 0);
117     assert(idx >= 0);
118     assert(a);
119
120     memset(&mreq, 0, sizeof(mreq));
121 #ifdef HAVE_STRUCT_IP_MREQN
122     mreq.imr_ifindex = idx;
123     mreq.imr_address.s_addr = a->address;
124 #else
125     mreq.imr_interface.s_addr = a->address;
126 #endif
127     mdns_mcast_group_ipv4(&sa);
128     mreq.imr_multiaddr = sa.sin_addr;
129
130     /* Some network drivers have issues with dropping membership of
131      * mcast groups when the iface is down, but don't allow rejoining
132      * when it comes back up. This is an ugly workaround */
133     if (join)
134         setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
135
136     if (setsockopt(fd, IPPROTO_IP, join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
137         catta_log_warn("%s failed: %s", join ? "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP", strerror(errno));
138         return -1;
139     }
140
141     return 0;
142 }
143
144 int catta_mdns_mcast_join_ipv6(int fd, const CattaIPv6Address *a, int idx, int join) {
145     struct ipv6_mreq mreq6;
146     struct sockaddr_in6 sa6;
147
148     assert(fd >= 0);
149     assert(idx >= 0);
150     assert(a);
151
152     memset(&mreq6, 0, sizeof(mreq6));
153     mdns_mcast_group_ipv6 (&sa6);
154     mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
155     mreq6.ipv6mr_interface = idx;
156
157     if (join)
158         setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6));
159
160     if (setsockopt(fd, IPPROTO_IPV6, join ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
161         catta_log_warn("%s failed: %s", join ? "IPV6_ADD_MEMBERSHIP" : "IPV6_DROP_MEMBERSHIP", strerror(errno));
162         return -1;
163     }
164
165     return 0;
166 }
167
168 static int reuseaddr(int fd) {
169     int yes;
170
171     yes = 1;
172     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
173         catta_log_warn("SO_REUSEADDR failed: %s", strerror(errno));
174         return -1;
175     }
176
177 #ifdef SO_REUSEPORT
178     yes = 1;
179     if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)) < 0) {
180         catta_log_warn("SO_REUSEPORT failed: %s", strerror(errno));
181         return -1;
182     }
183 #endif
184
185     return 0;
186 }
187
188 static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) {
189
190     assert(fd >= 0);
191     assert(sa);
192     assert(l > 0);
193
194     if (bind(fd, sa, l) < 0) {
195
196         if (errno != EADDRINUSE) {
197             catta_log_warn("bind() failed: %s", strerror(errno));
198             return -1;
199         }
200
201         catta_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***",
202                        sa->sa_family == AF_INET ? "IPv4" : "IPv6");
203
204         /* Try again, this time with SO_REUSEADDR set */
205         if (reuseaddr(fd) < 0)
206             return -1;
207
208         if (bind(fd, sa, l) < 0) {
209             catta_log_warn("bind() failed: %s", strerror(errno));
210             return -1;
211         }
212     } else {
213
214         /* We enable SO_REUSEADDR afterwards, to make sure that the
215          * user may run other mDNS implementations if he really
216          * wants. */
217
218         if (reuseaddr(fd) < 0)
219             return -1;
220     }
221
222     return 0;
223 }
224
225 static int ipv4_pktinfo(int fd) {
226     int yes;
227
228 #ifdef IP_PKTINFO
229     yes = 1;
230     if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
231         catta_log_warn("IP_PKTINFO failed: %s", strerror(errno));
232         return -1;
233     }
234 #else
235
236 #ifdef IP_RECVINTERFACE
237     yes = 1;
238     if (setsockopt (fd, IPPROTO_IP, IP_RECVINTERFACE, &yes, sizeof(yes)) < 0) {
239         catta_log_warn("IP_RECVINTERFACE failed: %s", strerror(errno));
240         return -1;
241     }
242 #elif defined(IP_RECVIF)
243     yes = 1;
244     if (setsockopt (fd, IPPROTO_IP, IP_RECVIF, &yes, sizeof(yes)) < 0) {
245         catta_log_warn("IP_RECVIF failed: %s", strerror(errno));
246         return -1;
247     }
248 #endif
249
250 #ifdef IP_RECVDSTADDR
251     yes = 1;
252     if (setsockopt (fd, IPPROTO_IP, IP_RECVDSTADDR, &yes, sizeof(yes)) < 0) {
253         catta_log_warn("IP_RECVDSTADDR failed: %s", strerror(errno));
254         return -1;
255     }
256 #endif
257
258 #endif /* IP_PKTINFO */
259
260 #ifdef IP_RECVTTL
261     yes = 1;
262     if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
263         catta_log_warn("IP_RECVTTL failed: %s", strerror(errno));
264         return -1;
265     }
266 #endif
267
268     return 0;
269 }
270
271 static int ipv6_pktinfo(int fd) {
272     int yes;
273
274 #ifdef IPV6_RECVPKTINFO
275     yes = 1;
276     if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0) {
277         catta_log_warn("IPV6_RECVPKTINFO failed: %s", strerror(errno));
278         return -1;
279     }
280 #elif defined(IPV6_PKTINFO)
281     yes = 1;
282     if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
283         catta_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
284         return -1;
285     }
286 #endif
287
288 #ifdef IPV6_RECVHOPS
289     yes = 1;
290     if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPS, &yes, sizeof(yes)) < 0) {
291         catta_log_warn("IPV6_RECVHOPS failed: %s", strerror(errno));
292         return -1;
293     }
294 #elif defined(IPV6_RECVHOPLIMIT)
295     yes = 1;
296     if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0) {
297         catta_log_warn("IPV6_RECVHOPLIMIT failed: %s", strerror(errno));
298         return -1;
299     }
300 #elif defined(IPV6_HOPLIMIT)
301     yes = 1;
302     if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
303         catta_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
304         return -1;
305     }
306 #endif
307
308     return 0;
309 }
310
311 int catta_open_socket_ipv4(int no_reuse) {
312     struct sockaddr_in local;
313     int fd = -1, r, ittl;
314     uint8_t ttl, cyes;
315
316     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
317         catta_log_warn("socket() failed: %s", strerror(errno));
318         goto fail;
319     }
320
321     ttl = 255;
322     if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
323         catta_log_warn("IP_MULTICAST_TTL failed: %s", strerror(errno));
324         goto fail;
325     }
326
327     ittl = 255;
328     if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ittl, sizeof(ittl)) < 0) {
329         catta_log_warn("IP_TTL failed: %s", strerror(errno));
330         goto fail;
331     }
332
333     cyes = 1;
334     if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &cyes, sizeof(cyes)) < 0) {
335         catta_log_warn("IP_MULTICAST_LOOP failed: %s", strerror(errno));
336         goto fail;
337     }
338
339     memset(&local, 0, sizeof(local));
340     local.sin_family = AF_INET;
341     local.sin_port = htons(CATTA_MDNS_PORT);
342
343     if (no_reuse)
344         r = bind(fd, (struct sockaddr*) &local, sizeof(local));
345     else
346         r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
347
348     if (r < 0)
349         goto fail;
350
351     if (ipv4_pktinfo (fd) < 0)
352          goto fail;
353
354     if (catta_set_cloexec(fd) < 0) {
355         catta_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
356         goto fail;
357     }
358
359     if (catta_set_nonblock(fd) < 0) {
360         catta_log_warn("O_NONBLOCK failed: %s", strerror(errno));
361         goto fail;
362     }
363
364     return fd;
365
366 fail:
367     if (fd >= 0)
368         closesocket(fd);
369
370     return -1;
371 }
372
373 int catta_open_socket_ipv6(int no_reuse) {
374     struct sockaddr_in6 sa, local;
375     int fd = -1, yes, r;
376     int ttl;
377
378     mdns_mcast_group_ipv6(&sa);
379
380     if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
381         catta_log_warn("socket() failed: %s", strerror(errno));
382         goto fail;
383     }
384
385     ttl = 255;
386     if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
387         catta_log_warn("IPV6_MULTICAST_HOPS failed: %s", strerror(errno));
388         goto fail;
389     }
390
391     ttl = 255;
392     if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
393         catta_log_warn("IPV6_UNICAST_HOPS failed: %s", strerror(errno));
394         goto fail;
395     }
396
397     yes = 1;
398     if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
399         catta_log_warn("IPV6_V6ONLY failed: %s", strerror(errno));
400         goto fail;
401     }
402
403     yes = 1;
404     if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
405         catta_log_warn("IPV6_MULTICAST_LOOP failed: %s", strerror(errno));
406         goto fail;
407     }
408
409     memset(&local, 0, sizeof(local));
410     local.sin6_family = AF_INET6;
411     local.sin6_port = htons(CATTA_MDNS_PORT);
412
413     if (no_reuse)
414         r = bind(fd, (struct sockaddr*) &local, sizeof(local));
415     else
416         r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
417
418     if (r < 0)
419         goto fail;
420
421     if (ipv6_pktinfo(fd) < 0)
422         goto fail;
423
424     if (catta_set_cloexec(fd) < 0) {
425         catta_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
426         goto fail;
427     }
428
429     if (catta_set_nonblock(fd) < 0) {
430         catta_log_warn("O_NONBLOCK failed: %s", strerror(errno));
431         goto fail;
432     }
433
434     return fd;
435
436 fail:
437     if (fd >= 0)
438         closesocket(fd);
439
440     return -1;
441 }
442
443 static int sendmsg_loop(int fd, struct msghdr *msg, int flags) {
444     assert(fd >= 0);
445     assert(msg);
446
447     for (;;) {
448
449         if (sendmsg(fd, msg, flags) >= 0)
450             break;
451
452         if (errno == EINTR)
453             continue;
454
455         if (errno != EAGAIN) {
456             char where[64];
457             struct sockaddr_in *sin = msg->msg_name;
458
459             inet_ntop(sin->sin_family, &sin->sin_addr, where, sizeof(where));
460             catta_log_debug("sendmsg() to %s failed: %s", where, strerror(errno));
461             return -1;
462         }
463
464         if (catta_wait_for_write(fd) < 0)
465             return -1;
466     }
467
468     return 0;
469 }
470
471 int catta_send_dns_packet_ipv4(
472         int fd,
473         CattaIfIndex iface,
474         CattaDnsPacket *p,
475         const CattaIPv4Address *src_address,
476         const CattaIPv4Address *dst_address,
477         uint16_t dst_port) {
478
479     struct sockaddr_in sa;
480     struct msghdr msg;
481     struct iovec io;
482 #ifdef IP_PKTINFO
483     struct cmsghdr *cmsg;
484     size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
485 #elif !defined(IP_MULTICAST_IF) && defined(IP_SENDSRCADDR)
486     struct cmsghdr *cmsg;
487     size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_addr)) / sizeof(size_t)) + 1];
488 #endif
489
490     assert(fd >= 0);
491     assert(p);
492     assert(catta_dns_packet_check_valid(p) >= 0);
493     assert(!dst_address || dst_port > 0);
494
495     if (!dst_address)
496         mdns_mcast_group_ipv4(&sa);
497     else
498         ipv4_address_to_sockaddr(&sa, dst_address, dst_port);
499
500     memset(&io, 0, sizeof(io));
501     io.iov_base = CATTA_DNS_PACKET_DATA(p);
502     io.iov_len = p->size;
503
504     memset(&msg, 0, sizeof(msg));
505     msg.msg_name = &sa;
506     msg.msg_namelen = sizeof(sa);
507     msg.msg_iov = &io;
508     msg.msg_iovlen = 1;
509     msg.msg_flags = 0;
510     msg.msg_control = NULL;
511     msg.msg_controllen = 0;
512
513 #ifdef IP_PKTINFO
514     if (iface > 0 || src_address) {
515         struct in_pktinfo *pkti;
516
517         memset(cmsg_data, 0, sizeof(cmsg_data));
518         msg.msg_control = cmsg_data;
519         msg.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo));
520
521         cmsg = CMSG_FIRSTHDR(&msg);
522         cmsg->cmsg_len = msg.msg_controllen;
523         cmsg->cmsg_level = IPPROTO_IP;
524         cmsg->cmsg_type = IP_PKTINFO;
525
526         pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
527
528         if (iface > 0)
529             pkti->ipi_ifindex = iface;
530
531 #ifdef HAVE_IPI_SPEC_DST
532         if (src_address)
533             pkti->ipi_spec_dst.s_addr = src_address->address;
534 #endif
535     }
536 #elif defined(IP_MULTICAST_IF)
537     if (src_address) {
538         struct in_addr any = { INADDR_ANY };
539         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, src_address ? &src_address->address : &any, sizeof(struct in_addr)) < 0) {
540             catta_log_warn("IP_MULTICAST_IF failed: %s", strerror(errno));
541             return -1;
542         }
543     }
544 #elif defined(IP_SENDSRCADDR)
545     if (src_address) {
546         struct in_addr *addr;
547
548         memset(cmsg_data, 0, sizeof(cmsg_data));
549         msg.msg_control = cmsg_data;
550         msg.msg_controllen = CMSG_LEN(sizeof(struct in_addr));
551
552         cmsg = CMSG_FIRSTHDR(&msg);
553         cmsg->cmsg_len = msg.msg_controllen;
554         cmsg->cmsg_level = IPPROTO_IP;
555         cmsg->cmsg_type = IP_SENDSRCADDR;
556
557         addr = (struct in_addr *)CMSG_DATA(cmsg);
558         addr->s_addr =  src_address->address;
559     }
560 #elif defined(__GNUC__)
561 #warning "FIXME: We need some code to set the outgoing interface/local address here if IP_PKTINFO/IP_MULTICAST_IF is not available"
562 #endif
563
564     return sendmsg_loop(fd, &msg, 0);
565 }
566
567 int catta_send_dns_packet_ipv6(
568         int fd,
569         CattaIfIndex iface,
570         CattaDnsPacket *p,
571         const CattaIPv6Address *src_address,
572         const CattaIPv6Address *dst_address,
573         uint16_t dst_port) {
574
575     struct sockaddr_in6 sa;
576     struct msghdr msg;
577     struct iovec io;
578     struct cmsghdr *cmsg;
579     size_t cmsg_data[(CMSG_SPACE(sizeof(struct in6_pktinfo))/sizeof(size_t)) + 1];
580
581     assert(fd >= 0);
582     assert(p);
583     assert(catta_dns_packet_check_valid(p) >= 0);
584     assert(!dst_address || dst_port > 0);
585
586     if (!dst_address)
587         mdns_mcast_group_ipv6(&sa);
588     else
589         ipv6_address_to_sockaddr(&sa, dst_address, dst_port);
590
591     memset(&io, 0, sizeof(io));
592     io.iov_base = CATTA_DNS_PACKET_DATA(p);
593     io.iov_len = p->size;
594
595     memset(&msg, 0, sizeof(msg));
596     msg.msg_name = &sa;
597     msg.msg_namelen = sizeof(sa);
598     msg.msg_iov = &io;
599     msg.msg_iovlen = 1;
600     msg.msg_flags = 0;
601
602     if (iface > 0 || src_address) {
603         struct in6_pktinfo *pkti;
604
605         memset(cmsg_data, 0, sizeof(cmsg_data));
606         msg.msg_control = cmsg_data;
607         msg.msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo));
608
609         cmsg = CMSG_FIRSTHDR(&msg);
610         cmsg->cmsg_len = msg.msg_controllen;
611         cmsg->cmsg_level = IPPROTO_IPV6;
612         cmsg->cmsg_type = IPV6_PKTINFO;
613
614         pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg);
615
616         if (iface > 0)
617             pkti->ipi6_ifindex = iface;
618
619         if (src_address)
620             memcpy(&pkti->ipi6_addr, src_address->address, sizeof(src_address->address));
621     } else {
622         msg.msg_control = NULL;
623         msg.msg_controllen = 0;
624     }
625
626     return sendmsg_loop(fd, &msg, 0);
627 }
628
629 CattaDnsPacket *catta_recv_dns_packet_ipv4(
630         int fd,
631         CattaIPv4Address *ret_src_address,
632         uint16_t *ret_src_port,
633         CattaIPv4Address *ret_dst_address,
634         CattaIfIndex *ret_iface,
635         uint8_t *ret_ttl) {
636
637     CattaDnsPacket *p= NULL;
638     struct msghdr msg;
639     struct iovec io;
640     size_t aux[1024 / sizeof(size_t)]; /* for alignment on ia64 ! */
641     ssize_t l;
642     struct cmsghdr *cmsg;
643     int found_addr = 0;
644     int ms;
645     struct sockaddr_in sa;
646
647     assert(fd >= 0);
648
649     if (ioctl(fd, FIONREAD, &ms) < 0) {
650         catta_log_warn("ioctl(): %s", strerror(errno));
651         goto fail;
652     }
653
654     if (ms < 0) {
655         catta_log_warn("FIONREAD returned negative value.");
656         goto fail;
657     }
658
659     p = catta_dns_packet_new(ms + CATTA_DNS_PACKET_EXTRA_SIZE);
660
661     io.iov_base = CATTA_DNS_PACKET_DATA(p);
662     io.iov_len = p->max_size;
663
664     memset(&msg, 0, sizeof(msg));
665     msg.msg_name = &sa;
666     msg.msg_namelen = sizeof(sa);
667     msg.msg_iov = &io;
668     msg.msg_iovlen = 1;
669     msg.msg_control = aux;
670     msg.msg_controllen = sizeof(aux);
671     msg.msg_flags = 0;
672
673     if ((l = recvmsg(fd, &msg, 0)) < 0) {
674         /* Linux returns EAGAIN when an invalid IP packet has been
675         received. We suppress warnings in this case because this might
676         create quite a bit of log traffic on machines with unstable
677         links. (See #60) */
678
679         if (errno != EAGAIN)
680             catta_log_warn("recvmsg(): %s", strerror(errno));
681
682         goto fail;
683     }
684
685     /* For corrupt packets FIONREAD returns zero size (See rhbz #607297). So
686      * fail after having read them. */
687     if (!ms)
688         goto fail;
689
690     if (sa.sin_addr.s_addr == INADDR_ANY)
691         /* Linux 2.4 behaves very strangely sometimes! */
692         goto fail;
693
694     assert(!(msg.msg_flags & MSG_CTRUNC));
695     assert(!(msg.msg_flags & MSG_TRUNC));
696
697     p->size = (size_t) l;
698
699     if (ret_src_port)
700         *ret_src_port = catta_port_from_sockaddr((struct sockaddr*) &sa);
701
702     if (ret_src_address) {
703         CattaAddress a;
704         catta_address_from_sockaddr((struct sockaddr*) &sa, &a);
705         *ret_src_address = a.data.ipv4;
706     }
707
708     if (ret_ttl)
709         *ret_ttl = 255;
710
711     if (ret_iface)
712         *ret_iface = CATTA_IF_UNSPEC;
713
714     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
715
716         if (cmsg->cmsg_level == IPPROTO_IP) {
717
718             switch (cmsg->cmsg_type) {
719 #ifdef IP_RECVTTL
720                 case IP_RECVTTL:
721 #endif
722                 case IP_TTL:
723                     if (ret_ttl)
724                         *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
725
726                     break;
727
728 #ifdef IP_PKTINFO
729                 case IP_PKTINFO: {
730                     struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
731
732                     if (ret_iface && i->ipi_ifindex > 0)
733                         *ret_iface = (int) i->ipi_ifindex;
734
735                     if (ret_dst_address)
736                         ret_dst_address->address = i->ipi_addr.s_addr;
737
738                     found_addr = 1;
739
740                     break;
741                 }
742 #endif
743
744 #ifdef IP_RECVIF
745                 case IP_RECVIF: {
746                     struct sockaddr_dl *sdl = (struct sockaddr_dl *) CMSG_DATA (cmsg);
747
748                     if (ret_iface) {
749 #ifdef __sun
750                         if (*(uint_t*) sdl > 0)
751                             *ret_iface = *(uint_t*) sdl;
752 #else
753
754                         if (sdl->sdl_index > 0)
755                             *ret_iface = (int) sdl->sdl_index;
756 #endif
757                     }
758
759                     break;
760                 }
761 #endif
762
763 #ifdef IP_RECVDSTADDR
764                 case IP_RECVDSTADDR:
765                     if (ret_dst_address)
766                         memcpy(&ret_dst_address->address, CMSG_DATA (cmsg), 4);
767
768                     found_addr = 1;
769                     break;
770 #endif
771
772                 default:
773                     catta_log_warn("Unhandled cmsg_type: %d", cmsg->cmsg_type);
774                     break;
775             }
776         }
777     }
778
779     assert(found_addr);
780
781     return p;
782
783 fail:
784     if (p)
785         catta_dns_packet_free(p);
786
787     return NULL;
788 }
789
790 CattaDnsPacket *catta_recv_dns_packet_ipv6(
791         int fd,
792         CattaIPv6Address *ret_src_address,
793         uint16_t *ret_src_port,
794         CattaIPv6Address *ret_dst_address,
795         CattaIfIndex *ret_iface,
796         uint8_t *ret_ttl) {
797
798     CattaDnsPacket *p = NULL;
799     struct msghdr msg;
800     struct iovec io;
801     size_t aux[1024 / sizeof(size_t)];
802     ssize_t l;
803     int ms;
804     struct cmsghdr *cmsg;
805     int found_ttl = 0, found_iface = 0;
806     struct sockaddr_in6 sa;
807
808     assert(fd >= 0);
809
810     if (ioctl(fd, FIONREAD, &ms) < 0) {
811         catta_log_warn("ioctl(): %s", strerror(errno));
812         goto fail;
813     }
814
815     if (ms < 0) {
816         catta_log_warn("FIONREAD returned negative value.");
817         goto fail;
818     }
819
820     p = catta_dns_packet_new(ms + CATTA_DNS_PACKET_EXTRA_SIZE);
821
822     io.iov_base = CATTA_DNS_PACKET_DATA(p);
823     io.iov_len = p->max_size;
824
825     memset(&msg, 0, sizeof(msg));
826     msg.msg_name = (struct sockaddr*) &sa;
827     msg.msg_namelen = sizeof(sa);
828
829     msg.msg_iov = &io;
830     msg.msg_iovlen = 1;
831     msg.msg_control = aux;
832     msg.msg_controllen = sizeof(aux);
833     msg.msg_flags = 0;
834
835     if ((l = recvmsg(fd, &msg, 0)) < 0) {
836         /* Linux returns EAGAIN when an invalid IP packet has been
837         received. We suppress warnings in this case because this might
838         create quite a bit of log traffic on machines with unstable
839         links. (See #60) */
840
841         if (errno != EAGAIN)
842             catta_log_warn("recvmsg(): %s", strerror(errno));
843
844         goto fail;
845     }
846
847     /* For corrupt packets FIONREAD returns zero size (See rhbz #607297). So
848      * fail after having read them. */
849     if (!ms)
850         goto fail;
851
852     assert(!(msg.msg_flags & MSG_CTRUNC));
853     assert(!(msg.msg_flags & MSG_TRUNC));
854
855     p->size = (size_t) l;
856
857     if (ret_src_port)
858         *ret_src_port = catta_port_from_sockaddr((struct sockaddr*) &sa);
859
860     if (ret_src_address) {
861         CattaAddress a;
862         catta_address_from_sockaddr((struct sockaddr*) &sa, &a);
863         *ret_src_address = a.data.ipv6;
864     }
865
866     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
867
868         if (cmsg->cmsg_level == IPPROTO_IPV6) {
869
870             switch (cmsg->cmsg_type) {
871
872                 case IPV6_HOPLIMIT:
873
874                     if (ret_ttl)
875                         *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
876
877                     found_ttl = 1;
878
879                     break;
880
881                 case IPV6_PKTINFO: {
882                     struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
883
884                     if (ret_iface && i->ipi6_ifindex > 0)
885                         *ret_iface = i->ipi6_ifindex;
886
887                     if (ret_dst_address)
888                         memcpy(ret_dst_address->address, i->ipi6_addr.s6_addr, 16);
889
890                     found_iface = 1;
891                     break;
892                 }
893
894                 default:
895                     catta_log_warn("Unhandled cmsg_type: %d", cmsg->cmsg_type);
896                     break;
897             }
898         }
899     }
900
901     assert(found_iface);
902     assert(found_ttl);
903
904     return p;
905
906 fail:
907     if (p)
908         catta_dns_packet_free(p);
909
910     return NULL;
911 }
912
913 int catta_open_unicast_socket_ipv4(void) {
914     struct sockaddr_in local;
915     int fd = -1;
916
917     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
918         catta_log_warn("socket() failed: %s", strerror(errno));
919         goto fail;
920     }
921
922     memset(&local, 0, sizeof(local));
923     local.sin_family = AF_INET;
924
925     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
926         catta_log_warn("bind() failed: %s", strerror(errno));
927         goto fail;
928     }
929
930     if (ipv4_pktinfo(fd) < 0) {
931          goto fail;
932     }
933
934     if (catta_set_cloexec(fd) < 0) {
935         catta_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
936         goto fail;
937     }
938
939     if (catta_set_nonblock(fd) < 0) {
940         catta_log_warn("O_NONBLOCK failed: %s", strerror(errno));
941         goto fail;
942     }
943
944     return fd;
945
946 fail:
947     if (fd >= 0)
948         closesocket(fd);
949
950     return -1;
951 }
952
953 int catta_open_unicast_socket_ipv6(void) {
954     struct sockaddr_in6 local;
955     int fd = -1, yes;
956
957     if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
958         catta_log_warn("socket() failed: %s", strerror(errno));
959         goto fail;
960     }
961
962     yes = 1;
963     if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
964         catta_log_warn("IPV6_V6ONLY failed: %s", strerror(errno));
965         goto fail;
966     }
967
968     memset(&local, 0, sizeof(local));
969     local.sin6_family = AF_INET6;
970
971     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
972         catta_log_warn("bind() failed: %s", strerror(errno));
973         goto fail;
974     }
975
976     if (ipv6_pktinfo(fd) < 0)
977         goto fail;
978
979     if (catta_set_cloexec(fd) < 0) {
980         catta_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
981         goto fail;
982     }
983
984     if (catta_set_nonblock(fd) < 0) {
985         catta_log_warn("O_NONBLOCK failed: %s", strerror(errno));
986         goto fail;
987     }
988
989     return fd;
990
991 fail:
992     if (fd >= 0)
993         closesocket(fd);
994
995     return -1;
996 }