From ef97e32ba2ed98b791af4504103b5f0378bb6b9b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 20 Nov 2005 00:45:03 +0000 Subject: * bump version number * make nss-mdns shut up * deal with OOM * other cleanups git-svn-id: file:///home/lennart/svn/public/nss-mdns/trunk@87 0ee8848e-81ea-0310-a63a-f631d1a40d77 --- configure.ac | 2 +- doc/README.html.in | 39 ++++++++++++-------------- src/Makefile.am | 2 +- src/avahi.c | 7 +++-- src/dns.c | 16 +++++++---- src/nss.c | 2 +- src/query.c | 81 ++++++++++++++++-------------------------------------- src/util.c | 9 ++---- 8 files changed, 61 insertions(+), 97 deletions(-) diff --git a/configure.ac b/configure.ac index c50c10c..8ded07b 100644 --- a/configure.ac +++ b/configure.ac @@ -21,7 +21,7 @@ # USA. AC_PREREQ(2.57) -AC_INIT([nss-mdns],[0.6],[mzaffzqaf (at) 0pointer (dot) de]) +AC_INIT([nss-mdns],[0.7],[mzaffzqaf (at) 0pointer (dot) de]) AC_CONFIG_SRCDIR([src/query.c]) AC_CONFIG_HEADERS([config.h]) AM_INIT_AUTOMAKE([foreign 1.9 -Wall]) diff --git a/doc/README.html.in b/doc/README.html.in index 72e66e2..28e5402 100644 --- a/doc/README.html.in +++ b/doc/README.html.in @@ -42,6 +42,12 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

News

+
Sat Nov 19 2005:

Version 0.7 +released. Changes include: Portability patch for ARM from Philipp +Zabel; make sure not to print any messages to STDERR; deal with OOM +situations properly; other cleanups

+
Sun Aug 21 2005:

Version 0.6 released. Changes include: honour search list in @@ -81,20 +87,20 @@ href="@PACKAGE_URL@nss-mdns-0.1.tar.gz">Version 0.1 released

Switch (NSS) functionality of the GNU C Library (glibc) providing host name resolution via Multicast DNS (aka -Zeroconf, aka Apple Rendezvous), effectively allowing +Zeroconf, aka Apple Rendezvous, aka Apple Bonjour), effectively allowing name resolution by common Unix/Linux programs in the ad-hoc mDNS domain .local.

nss-mdns provides client functionality only, which means that you have to run a mDNS responder daemon seperately from nss-mdns if you want to register the local host name via -mDNS (e.g. Avahi).

+mDNS. I recommend Avahi.

nss-mdns is very lightweight (23 KByte stripped binary -.so compiled with -DNDEBUG=1 on i386, gcc -3.3), has no dependencies besides the glibc and requires only +.so compiled with -DNDEBUG=1 -Os on i386, gcc +4.0), has no dependencies besides the glibc and requires only minimal configuration.

-

Optionally, nss-mdns can try to contact a running Optionally nss-mdns can try to contact a running avahi-daemon to make use of its superior record cacheing.

@@ -103,7 +109,8 @@ make use of its superior record cacheing.

It works!

While nss-mdns supports resolving IPv6 addresses it does -so via IPv4 multicasts only.

+so via IPv4 multicasts only. (Unless, of course, it finds a running +Avahi daemon which supports IPv6 properly.)

Documentation

@@ -153,12 +160,6 @@ use glibc's getent tool:

Replace foo whith a host name that has been registered with an mDNS responder.

-

To reduce the traffic nss-mdns is responsible for consider -installing glibc's name service cache daemon -nscd. However, when doing troubleshooting for -nss-mdns, don't forget to disable nscd for getting -sensible results.

-

If you run a firewall, don't forget to allow UDP traffic to the the mDNS multicast address 224.0.0.251 on port 5353.

@@ -187,14 +188,6 @@ mDNS.

If the configuration file is existent but empty, mDNS name lookups are disabled completely.

-

nss-mdns does not honour the domain search list of -/etc/resolv.conf, because I don't consider that this would be -a good idea, since every name lookup for non-existing domains would -result in a series of long timeouts of nss-mdns. If somebody -still considers this a good idea, he is free to send me a sensible -patch, which I might apply, but only if the domain search list may be -disabled.

-

Requirements

Currently, nss-mdns is tested on Linux only. A fairly @@ -222,6 +215,8 @@ compilation and make install (as root) for installation of

Sean Meiners for search list support.

+

Philipp Zabel for ARM support.

+

Download

The newest release is always available from @PACKAGE_URL@

@@ -232,12 +227,12 @@ compilation and make install (as root) for installation of
svn checkout svn://svn.0pointer.de/nss-mdns/trunk nss-mdns
-

You may find an up to date Debian package of nss-mdns on the Debian package repository.

+

You may find an up to date Debian package of nss-mdns on the Debian package repository. Many other distributions ship it, too.

If you want to be notified whenever I release a new version of this software use the subscription feature of Freshmeat.


-
Lennart Poettering <@PACKAGE_BUGREPORT@>, Aug 2005
+
Lennart Poettering <@PACKAGE_BUGREPORT@>, Nov 2005
$Id$
diff --git a/src/Makefile.am b/src/Makefile.am index 6e537cf..231b0b6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -22,7 +22,7 @@ AM_CFLAGS = \ -DRESOLV_CONF_FILE=\"$(sysconfdir)/resolv.conf\" \ -DAVAHI_SOCKET=\"$(localstatedir)/run/avahi-daemon/socket\" -#-DNDEBUG=1 +#AM_CFLAGS += -DNDEBUG=1 -Os # This cool debug trap works on i386/gcc only AM_CFLAGS+='-DDEBUG_TRAP=__asm__("int $$3")' diff --git a/src/avahi.c b/src/avahi.c index 96ce615..8e45e85 100644 --- a/src/avahi.c +++ b/src/avahi.c @@ -65,7 +65,6 @@ fail: close(fd); return NULL; - } int avahi_resolve_name(int af, const char* name, void* data) { @@ -74,7 +73,8 @@ int avahi_resolve_name(int af, const char* name, void* data) { int ret = -1; char ln[256]; - assert(af == AF_INET || af == AF_INET6); + if (af != AF_INET && af != AF_INET6) + goto finish; if (!(f = open_socket())) goto finish; @@ -127,7 +127,8 @@ int avahi_resolve_address(int af, const void *data, char* name, size_t name_len) int ret = -1; char a[256], ln[256]; - assert(af == AF_INET || af == AF_INET6); + if (af != AF_INET && af == AF_INET6) + goto finish; if (!(f = open_socket())) goto finish; diff --git a/src/dns.c b/src/dns.c index 61c66d8..e7e9765 100644 --- a/src/dns.c +++ b/src/dns.c @@ -34,8 +34,10 @@ struct dns_packet* dns_packet_new(void) { struct dns_packet *p; - p = malloc(sizeof(struct dns_packet)); - assert(p); + + if (!(p = malloc(sizeof(struct dns_packet)))) + return NULL; + p->size = p->rindex = 2*6; memset(p->data, 0, p->size); return p; @@ -47,13 +49,15 @@ void dns_packet_free(struct dns_packet *p) { } void dns_packet_set_field(struct dns_packet *p, unsigned idx, uint16_t v) { - assert(p && idx < 2*6); + assert(p); + assert(idx < 2*6); ((uint16_t*) p->data)[idx] = htons(v); } uint16_t dns_packet_get_field(struct dns_packet *p, unsigned idx) { - assert(p && idx < 2*6); + assert(p); + assert(idx < 2*6); return ntohs(((uint16_t*) p->data)[idx]); } @@ -70,6 +74,7 @@ uint8_t* dns_packet_append_name(struct dns_packet *p, const char *name) { d = dns_packet_extend(p, n+1); if (!f) f = d; + d[0] = n; memcpy(d+1, name, n); @@ -95,8 +100,10 @@ uint8_t* dns_packet_append_name(struct dns_packet *p, const char *name) { uint8_t* dns_packet_append_uint16(struct dns_packet *p, uint16_t v) { uint8_t *d; assert(p); + d = dns_packet_extend(p, sizeof(uint16_t)); *((uint16_t*) d) = htons(v); + return d; } @@ -158,7 +165,6 @@ int dns_packet_check_valid_response(struct dns_packet *p) { return -1; return 0; - } static ssize_t consume_labels(struct dns_packet *p, size_t idx, char *ret_name, size_t l) { diff --git a/src/nss.c b/src/nss.c index e45342d..5b5579a 100644 --- a/src/nss.c +++ b/src/nss.c @@ -8,7 +8,7 @@ by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - nss-mdns is distributed in the hope that it will be useful, but + nss-mdns is distributed in the hope that it will be useful, but1 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. diff --git a/src/query.c b/src/query.c index 8079a3d..7a3e0bd 100644 --- a/src/query.c +++ b/src/query.c @@ -73,46 +73,32 @@ int mdns_open_socket(void) { struct sockaddr_in sa; int fd = -1, ttl, yes; - if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - fprintf(stderr, "socket() failed: %s\n", strerror(errno)); + if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) goto fail; - } ttl = 255; - if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) { - fprintf(stderr, "IP_MULTICAST_TTL failed: %s\n", strerror(errno)); + if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) goto fail; - } sa.sin_family = AF_INET; sa.sin_port = 0; sa.sin_addr.s_addr = INADDR_ANY; - if (bind(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) { - fprintf(stderr, "bind() failed: %s\n", strerror(errno)); + if (bind(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) goto fail; - } yes = 1; - if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) { - fprintf(stderr, "IP_RECVTTL failed: %s\n", strerror(errno)); + if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) goto fail; - } - if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) { - fprintf(stderr, "IP_PKTINFO failed: %s\n", strerror(errno)); + if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) goto fail; - } - if (set_cloexec(fd) < 0) { - fprintf(stderr, "FD_CLOEXEC failed: %s\n", strerror(errno)); + if (set_cloexec(fd) < 0) goto fail; - } - if (set_nonblock(fd) < 0) { - fprintf(stderr, "O_ONONBLOCK failed: %s\n", strerror(errno)); + if (set_nonblock(fd) < 0) goto fail; - } return fd; @@ -129,7 +115,7 @@ static int send_dns_packet(int fd, struct dns_packet *p) { struct iovec io; struct cmsghdr *cmsg; struct in_pktinfo *pkti; - uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)]; + uint8_t cmsg_data[CMSG_LEN(sizeof(struct in_pktinfo))]; int i, n; struct ifreq ifreq[32]; struct ifconf ifconf; @@ -150,7 +136,7 @@ static int send_dns_packet(int fd, struct dns_packet *p) { cmsg->cmsg_level = IPPROTO_IP; cmsg->cmsg_type = IP_PKTINFO; - pkti = (struct in_pktinfo*) (cmsg_data + sizeof(struct cmsghdr)); + pkti = (struct in_pktinfo*) CMSG_DATA(cmsg); pkti->ipi_ifindex = 0; memset(&msg, 0, sizeof(msg)); @@ -165,10 +151,8 @@ static int send_dns_packet(int fd, struct dns_packet *p) { ifconf.ifc_req = ifreq; ifconf.ifc_len = sizeof(ifreq); - if (ioctl(fd, SIOCGIFCONF, &ifconf) < 0) { - fprintf(stderr, "SIOCGIFCONF failed: %s\n", strerror(errno)); + if (ioctl(fd, SIOCGIFCONF, &ifconf) < 0) return -1; - } for (i = 0, n = ifconf.ifc_len/sizeof(struct ifreq); i < n; i++) { struct sockaddr_in *ifsa; @@ -205,10 +189,8 @@ static int send_dns_packet(int fd, struct dns_packet *p) { if (sendmsg(fd, &msg, MSG_DONTROUTE) >= 0) break; - if (errno != EAGAIN) { - fprintf(stderr, "sendmsg() failed: %s\n", strerror(errno)); + if (errno != EAGAIN) return -1; - } if (wait_for_write(fd, NULL) < 0) return -1; @@ -228,7 +210,8 @@ static int recv_dns_packet(int fd, struct dns_packet **ret_packet, uint8_t *ret_ uint8_t aux[64]; assert(fd >= 0); - p = dns_packet_new(); + if (!(p = dns_packet_new())) + return -1; /* OOM */ io.iov_base = p->data; io.iov_len = sizeof(p->data); @@ -257,10 +240,8 @@ static int recv_dns_packet(int fd, struct dns_packet **ret_packet, uint8_t *ret_ } } - if (cmsg == NULL) { - fprintf(stderr, "Didn't recieve TTL\n"); + if (!cmsg) goto fail; - } p->size = (size_t) l; @@ -268,10 +249,8 @@ static int recv_dns_packet(int fd, struct dns_packet **ret_packet, uint8_t *ret_ return 0; } - if (errno != EAGAIN) { - fprintf(stderr, "recvfrom() failed: %s\n", strerror(errno)); + if (errno != EAGAIN) goto fail; - } if ((r = wait_for_read(fd, end)) < 0) goto fail; @@ -296,21 +275,17 @@ static int send_name_query(int fd, const char *name, uint16_t id, int query_ipv4 assert(fd >= 0 && name && (query_ipv4 || query_ipv6)); - if (!(p = dns_packet_new())) { - fprintf(stderr, "Failed to allocate DNS packet.\n"); - goto finish; - } + if (!(p = dns_packet_new())) + goto finish; /* OOM */ dns_packet_set_field(p, DNS_FIELD_ID, id); dns_packet_set_field(p, DNS_FIELD_FLAGS, DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); #ifndef NSS_IP6_ONLY if (query_ipv4) { - if (!(prev_name = dns_packet_append_name(p, name))) { - fprintf(stderr, "Bad host name\n"); - goto finish; - - } + if (!(prev_name = dns_packet_append_name(p, name))) + goto finish; /* Bad name */ + dns_packet_append_uint16(p, DNS_TYPE_A); dns_packet_append_uint16(p, DNS_CLASS_IN); qdcount++; @@ -319,10 +294,8 @@ static int send_name_query(int fd, const char *name, uint16_t id, int query_ipv4 #ifndef NSS_IP4_ONLY if (query_ipv6) { - if (!dns_packet_append_name_compressed(p, name, prev_name)) { - fprintf(stderr, "Bad host name\n"); - goto finish; - } + if (!dns_packet_append_name_compressed(p, name, prev_name)) + goto finish; /* Bad name */ dns_packet_append_uint16(p, DNS_TYPE_AAAA); dns_packet_append_uint16(p, DNS_CLASS_IN); @@ -510,18 +483,14 @@ static int send_reverse_query(int fd, const char *name, uint16_t id) { assert(fd >= 0 && name); - if (!(p = dns_packet_new())) { - fprintf(stderr, "Failed to allocate DNS packet.\n"); + if (!(p = dns_packet_new())) goto finish; - } dns_packet_set_field(p, DNS_FIELD_ID, id); dns_packet_set_field(p, DNS_FIELD_FLAGS, DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); - if (!dns_packet_append_name(p, name)) { - fprintf(stderr, "Bad host name\n"); + if (!dns_packet_append_name(p, name)) goto finish; - } dns_packet_append_uint16(p, DNS_TYPE_PTR); dns_packet_append_uint16(p, DNS_CLASS_IN); @@ -678,6 +647,4 @@ int mdns_query_ipv6(int fd, const ipv6_address_t *ipv6, void (*name_func)(const return query_reverse(fd, name, name_func, userdata); } - - #endif diff --git a/src/util.c b/src/util.c index b6b2d57..bf37711 100644 --- a/src/util.c +++ b/src/util.c @@ -26,7 +26,6 @@ #include #include #include -#include #include #include @@ -152,10 +151,8 @@ int wait_for_write(int fd, struct timeval *end) { } if ((r = select(fd+1, NULL, &fds, NULL, end ? &tv : NULL)) < 0) { - if (errno != EINTR) { - fprintf(stderr, "select() failed: %s\n", strerror(errno)); + if (errno != EINTR) return -1; - } } else if (r == 0) return 1; else { @@ -191,10 +188,8 @@ int wait_for_read(int fd, struct timeval *end) { } if ((r = select(fd+1, &fds, NULL, NULL, end ? &tv : NULL)) < 0) { - if (errno != EINTR) { - fprintf(stderr, "select() failed: %s\n", strerror(errno)); + if (errno != EINTR) return -1; - } } else if (r == 0) return 1; else { -- cgit