]> git.meshlink.io Git - catta/blob - avahi-core/socket.c
* add new server state AVAHI_SERVER_SLEEPING to avoid conflicts by own responses
[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 <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <sys/time.h>
36 #include <net/if.h>
37 #include <sys/ioctl.h>
38
39 #include "dns.h"
40 #include "util.h"
41 #include "socket.h"
42
43 static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
44     g_assert(ret_sa);
45
46     memset(ret_sa, 0, sizeof(struct sockaddr_in));
47     
48     ret_sa->sin_family = AF_INET;
49     ret_sa->sin_port = htons(AVAHI_MDNS_PORT);
50     inet_pton(AF_INET, "224.0.0.251", &ret_sa->sin_addr);
51 }
52
53 static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) {
54
55     g_assert(ret_sa);
56
57     memset(ret_sa, 0, sizeof(struct sockaddr_in6));
58     
59     ret_sa->sin6_family = AF_INET6;
60     ret_sa->sin6_port = htons(AVAHI_MDNS_PORT);
61     inet_pton(AF_INET6, "ff02::fb", &ret_sa->sin6_addr);
62 }
63
64 static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const AvahiIPv4Address *a, guint16 port) {
65     g_assert(ret_sa);
66     g_assert(a);
67     g_assert(port > 0);
68
69     memset(ret_sa, 0, sizeof(struct sockaddr_in));
70     ret_sa->sin_family = AF_INET;
71     ret_sa->sin_port = htons(port);
72     memcpy(&ret_sa->sin_addr, a, sizeof(AvahiIPv4Address));
73 }
74
75 static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv6Address *a, guint16 port) {
76     g_assert(ret_sa);
77     g_assert(a);
78     g_assert(port > 0);
79
80     memset(ret_sa, 0, sizeof(struct sockaddr_in6));
81     ret_sa->sin6_family = AF_INET6;
82     ret_sa->sin6_port = htons(port);
83     memcpy(&ret_sa->sin6_addr, a, sizeof(AvahiIPv6Address));
84 }
85
86 int avahi_mdns_mcast_join_ipv4 (int index, int fd) {
87     struct ip_mreqn mreq; 
88     struct sockaddr_in sa;
89
90     mdns_mcast_group_ipv4 (&sa);
91  
92     memset(&mreq, 0, sizeof(mreq));
93     mreq.imr_multiaddr = sa.sin_addr;
94     mreq.imr_ifindex = index;
95  
96     if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
97         g_warning("IP_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
98         return -1;
99     } 
100
101     return 0;
102 }
103
104 int avahi_mdns_mcast_join_ipv6 (int index, int fd) {
105     struct ipv6_mreq mreq6; 
106     struct sockaddr_in6 sa6;
107
108     mdns_mcast_group_ipv6 (&sa6);
109
110     memset(&mreq6, 0, sizeof(mreq6));
111     mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
112     mreq6.ipv6mr_interface = index;
113
114     if (setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
115         g_warning("IPV6_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
116         return -1;
117     }
118
119     return 0;
120 }
121
122 int avahi_mdns_mcast_leave_ipv4 (int index, int fd) {
123     struct ip_mreqn mreq; 
124     struct sockaddr_in sa;
125     
126     mdns_mcast_group_ipv4 (&sa);
127  
128     memset(&mreq, 0, sizeof(mreq));
129     mreq.imr_multiaddr = sa.sin_addr;
130     mreq.imr_ifindex = index;
131  
132     if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
133         g_warning("IP_DROP_MEMBERSHIP failed: %s\n", strerror(errno));
134         return -1;
135     }
136
137     return 0;
138 }
139
140 int avahi_mdns_mcast_leave_ipv6 (int index, int fd) {
141     struct ipv6_mreq mreq6; 
142     struct sockaddr_in6 sa6;
143
144     mdns_mcast_group_ipv6 (&sa6);
145
146     memset(&mreq6, 0, sizeof(mreq6));
147     mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
148     mreq6.ipv6mr_interface = index;
149
150     if (setsockopt(fd, SOL_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
151         g_warning("IPV6_DROP_MEMBERSHIP failed: %s\n", strerror(errno));
152         return -1;
153     }
154
155     return 0;
156 }
157
158 gint avahi_open_socket_ipv4(void) {
159     struct sockaddr_in local;
160     int fd = -1, ttl, yes;
161         
162     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
163         g_warning("socket() failed: %s\n", strerror(errno));
164         goto fail;
165     }
166     
167     ttl = 255;
168     if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
169         g_warning("IP_MULTICAST_TTL failed: %s\n", strerror(errno));
170         goto fail;
171     }
172
173     ttl = 255;
174     if (setsockopt(fd, SOL_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
175         g_warning("IP_TTL failed: %s\n", strerror(errno));
176         goto fail;
177     }
178     
179     yes = 1;
180     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
181         g_warning("SO_REUSEADDR failed: %s\n", strerror(errno));
182         goto fail;
183     }
184
185     yes = 1;
186     if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
187         g_warning("IP_MULTICAST_LOOP failed: %s\n", strerror(errno));
188         goto fail;
189     }
190
191     
192     memset(&local, 0, sizeof(local));
193     local.sin_family = AF_INET;
194     local.sin_port = htons(AVAHI_MDNS_PORT);
195     
196     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
197         g_warning("bind() failed: %s\n", strerror(errno));
198         goto fail;
199     }
200
201     yes = 1;
202     if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
203         g_warning("IP_RECVTTL failed: %s\n", strerror(errno));
204         goto fail;
205     }
206
207     yes = 1;
208     if (setsockopt(fd, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
209         g_warning("IP_PKTINFO failed: %s\n", strerror(errno));
210         goto fail;
211     }
212     
213     if (avahi_set_cloexec(fd) < 0) {
214         g_warning("FD_CLOEXEC failed: %s\n", strerror(errno));
215         goto fail;
216     }
217     
218     if (avahi_set_nonblock(fd) < 0) {
219         g_warning("O_NONBLOCK failed: %s\n", strerror(errno));
220         goto fail;
221     }
222
223     return fd;
224
225 fail:
226     if (fd >= 0)
227         close(fd);
228
229     return -1;
230 }
231
232 gint avahi_open_socket_ipv6(void) {
233     struct sockaddr_in6 sa, local;
234     int fd = -1, ttl, yes;
235
236     mdns_mcast_group_ipv6(&sa);
237         
238     if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
239         g_warning("socket() failed: %s\n", strerror(errno));
240         goto fail;
241     }
242     
243     ttl = 255;
244     if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
245         g_warning("IPV6_MULTICAST_HOPS failed: %s\n", strerror(errno));
246         goto fail;
247     }
248
249     ttl = 255;
250     if (setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
251         g_warning("IPV6_UNICAST_HOPS failed: %s\n", strerror(errno));
252         goto fail;
253     }
254     
255     yes = 1;
256     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
257         g_warning("SO_REUSEADDR failed: %s\n", strerror(errno));
258         goto fail;
259     }
260
261     yes = 1;
262     if (setsockopt(fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
263         g_warning("IPV6_V6ONLY failed: %s\n", strerror(errno));
264         goto fail;
265     }
266
267     yes = 1;
268     if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
269         g_warning("IPV6_MULTICAST_LOOP failed: %s\n", strerror(errno));
270         goto fail;
271     }
272
273     memset(&local, 0, sizeof(local));
274     local.sin6_family = AF_INET6;
275     local.sin6_port = htons(AVAHI_MDNS_PORT);
276     
277     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
278         g_warning("bind() failed: %s\n", strerror(errno));
279         goto fail;
280     }
281
282     yes = 1;
283     if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
284         g_warning("IPV6_HOPLIMIT failed: %s\n", strerror(errno));
285         goto fail;
286     }
287
288     yes = 1;
289     if (setsockopt(fd, SOL_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
290         g_warning("IPV6_PKTINFO failed: %s\n", strerror(errno));
291         goto fail;
292     }
293     
294     if (avahi_set_cloexec(fd) < 0) {
295         g_warning("FD_CLOEXEC failed: %s\n", strerror(errno));
296         goto fail;
297     }
298     
299     if (avahi_set_nonblock(fd) < 0) {
300         g_warning("O_NONBLOCK failed: %s\n", strerror(errno));
301         goto fail;
302     }
303
304     return fd;
305
306 fail:
307     if (fd >= 0)
308         close(fd);
309
310     return -1;
311 }
312
313 static gint sendmsg_loop(gint fd, struct msghdr *msg, gint flags) {
314     g_assert(fd >= 0);
315     g_assert(msg);
316
317     for (;;) {
318     
319         if (sendmsg(fd, msg, flags) >= 0)
320             break;
321         
322         if (errno != EAGAIN) {
323             g_message("sendmsg() failed: %s\n", strerror(errno));
324             return -1;
325         }
326         
327         if (avahi_wait_for_write(fd) < 0)
328             return -1;
329     }
330
331     return 0;
332 }
333
334 gint avahi_send_dns_packet_ipv4(gint fd, gint interface, AvahiDnsPacket *p, const AvahiIPv4Address *a, guint16 port) {
335     struct sockaddr_in sa;
336     struct msghdr msg;
337     struct iovec io;
338     struct cmsghdr *cmsg;
339     struct in_pktinfo *pkti;
340     uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
341
342     g_assert(fd >= 0);
343     g_assert(p);
344     g_assert(avahi_dns_packet_check_valid(p) >= 0);
345     g_assert(!a || port > 0);
346
347     if (!a)
348         mdns_mcast_group_ipv4(&sa);
349     else
350         ipv4_address_to_sockaddr(&sa, a, port);
351
352     memset(&io, 0, sizeof(io));
353     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
354     io.iov_len = p->size;
355
356     memset(cmsg_data, 0, sizeof(cmsg_data));
357     cmsg = (struct cmsghdr*) cmsg_data;
358     cmsg->cmsg_len = sizeof(cmsg_data);
359     cmsg->cmsg_level = IPPROTO_IP;
360     cmsg->cmsg_type = IP_PKTINFO;
361
362     pkti = (struct in_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
363     pkti->ipi_ifindex = interface;
364     
365     memset(&msg, 0, sizeof(msg));
366     msg.msg_name = &sa;
367     msg.msg_namelen = sizeof(sa);
368     msg.msg_iov = &io;
369     msg.msg_iovlen = 1;
370     msg.msg_control = cmsg_data;
371     msg.msg_controllen = sizeof(cmsg_data);
372     msg.msg_flags = 0;
373
374     return sendmsg_loop(fd, &msg, MSG_DONTROUTE);
375 }
376
377 gint avahi_send_dns_packet_ipv6(gint fd, gint interface, AvahiDnsPacket *p, const AvahiIPv6Address *a, guint16 port) {
378     struct sockaddr_in6 sa;
379     struct msghdr msg;
380     struct iovec io;
381     struct cmsghdr *cmsg;
382     struct in6_pktinfo *pkti;
383     uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)];
384
385     g_assert(fd >= 0);
386     g_assert(p);
387     g_assert(avahi_dns_packet_check_valid(p) >= 0);
388
389     if (!a)
390         mdns_mcast_group_ipv6(&sa);
391     else
392         ipv6_address_to_sockaddr(&sa, a, port);
393
394     memset(&io, 0, sizeof(io));
395     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
396     io.iov_len = p->size;
397
398     memset(cmsg_data, 0, sizeof(cmsg_data));
399     cmsg = (struct cmsghdr*) cmsg_data;
400     cmsg->cmsg_len = sizeof(cmsg_data);
401     cmsg->cmsg_level = IPPROTO_IPV6;
402     cmsg->cmsg_type = IPV6_PKTINFO;
403
404     pkti = (struct in6_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
405     pkti->ipi6_ifindex = interface;
406     
407     memset(&msg, 0, sizeof(msg));
408     msg.msg_name = &sa;
409     msg.msg_namelen = sizeof(sa);
410     msg.msg_iov = &io;
411     msg.msg_iovlen = 1;
412     msg.msg_control = cmsg_data;
413     msg.msg_controllen = sizeof(cmsg_data);
414     msg.msg_flags = 0;
415
416     return sendmsg_loop(fd, &msg, MSG_DONTROUTE);
417 }
418
419 AvahiDnsPacket* avahi_recv_dns_packet_ipv4(gint fd, struct sockaddr_in *ret_sa, gint *ret_iface, guint8* ret_ttl) {
420     AvahiDnsPacket *p= NULL;
421     struct msghdr msg;
422     struct iovec io;
423     uint8_t aux[1024];
424     ssize_t l;
425     struct cmsghdr *cmsg;
426     gboolean found_ttl = FALSE, found_iface = FALSE;
427
428     g_assert(fd >= 0);
429     g_assert(ret_sa);
430     g_assert(ret_iface);
431     g_assert(ret_ttl);
432
433     p = avahi_dns_packet_new(0);
434
435     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
436     io.iov_len = p->max_size;
437     
438     memset(&msg, 0, sizeof(msg));
439     msg.msg_name = ret_sa;
440     msg.msg_namelen = sizeof(struct sockaddr_in);
441     msg.msg_iov = &io;
442     msg.msg_iovlen = 1;
443     msg.msg_control = aux;
444     msg.msg_controllen = sizeof(aux);
445     msg.msg_flags = 0;
446     
447     if ((l = recvmsg(fd, &msg, 0)) < 0) {
448         g_warning("recvmsg(): %s\n", strerror(errno));
449         goto fail;
450     }
451
452     if (ret_sa->sin_addr.s_addr == INADDR_ANY) {
453         /* Linux 2.4 behaves very strangely sometimes! */
454         goto fail;
455     }
456     
457     g_assert(!(msg.msg_flags & MSG_CTRUNC));
458     g_assert(!(msg.msg_flags & MSG_TRUNC));
459     p->size = (size_t) l;
460     
461     *ret_ttl = 0;
462
463 /*     avahi_hexdump(msg.msg_control, msg.msg_controllen); */
464         
465     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
466
467 /*         avahi_hexdump(CMSG_DATA(cmsg), cmsg->cmsg_len - sizeof(struct cmsghdr)); */
468         
469         if (cmsg->cmsg_level == SOL_IP) {
470             
471             if (cmsg->cmsg_type == IP_TTL) {
472                 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
473                 found_ttl = TRUE;
474             } else if (cmsg->cmsg_type == IP_PKTINFO) {
475                 *ret_iface = (gint) ((struct in_pktinfo*) CMSG_DATA(cmsg))->ipi_ifindex;
476                 found_iface = TRUE;
477             }
478         }
479     }
480
481 /*     g_message("ttl=%u iface=%i", *ret_ttl, *ret_iface); */
482
483     g_assert(found_iface);
484     g_assert(found_ttl);
485
486     return p;
487
488 fail:
489     if (p)
490         avahi_dns_packet_free(p);
491
492     return NULL;
493 }
494
495 AvahiDnsPacket* avahi_recv_dns_packet_ipv6(gint fd, struct sockaddr_in6 *ret_sa, gint *ret_iface, guint8* ret_ttl) {
496     AvahiDnsPacket *p = NULL;
497     struct msghdr msg;
498     struct iovec io;
499     uint8_t aux[64];
500     ssize_t l;
501     struct cmsghdr *cmsg;
502     gboolean found_ttl = FALSE, found_iface = FALSE;
503
504     g_assert(fd >= 0);
505     g_assert(ret_sa);
506     g_assert(ret_iface);
507     g_assert(ret_ttl);
508
509     p = avahi_dns_packet_new(0);
510
511     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
512     io.iov_len = p->max_size;
513     
514     memset(&msg, 0, sizeof(msg));
515     msg.msg_name = ret_sa;
516     msg.msg_namelen = sizeof(struct sockaddr_in6);
517     msg.msg_iov = &io;
518     msg.msg_iovlen = 1;
519     msg.msg_control = aux;
520     msg.msg_controllen = sizeof(aux);
521     msg.msg_flags = 0;
522     
523     if ((l = recvmsg(fd, &msg, 0)) < 0)
524         goto fail;
525
526     p->size = (size_t) l;
527     
528     *ret_ttl = 0;
529
530     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
531         if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
532             *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
533             found_ttl = TRUE;
534         }
535             
536         if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
537             *ret_iface = ((struct in6_pktinfo*) CMSG_DATA(cmsg))->ipi6_ifindex;
538             found_iface = TRUE;
539         }
540     }
541
542     g_assert(found_iface);
543     g_assert(found_ttl);
544
545     return p;
546
547 fail:
548     if (p)
549         avahi_dns_packet_free(p);
550
551     return NULL;
552 }
553