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 --- src/Makefile.am | 2 +- src/avahi.c | 7 ++--- src/dns.c | 16 ++++++++---- src/nss.c | 2 +- src/query.c | 81 +++++++++++++++++---------------------------------------- src/util.c | 9 ++----- 6 files changed, 43 insertions(+), 74 deletions(-) (limited to 'src') 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