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