From 0e203251414d56b3f6b8d4dafde19a1d27b18caf Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 13 Oct 2005 01:26:32 +0000 Subject: [PATCH] implement address related functions git-svn-id: file:///home/lennart/svn/public/avahi/trunk@753 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-compat-howl/Makefile.am | 20 +-- avahi-compat-howl/address-test.c | 51 ++++++++ avahi-compat-howl/address.c | 213 +++++++++++++++++++++++++++++++ avahi-compat-howl/funcs.txt | 26 ++-- avahi-compat-howl/text.c | 118 +++++++++++++++++ avahi-compat-howl/unsupported.c | 165 ------------------------ 6 files changed, 406 insertions(+), 187 deletions(-) create mode 100644 avahi-compat-howl/address-test.c create mode 100644 avahi-compat-howl/address.c create mode 100644 avahi-compat-howl/text.c diff --git a/avahi-compat-howl/Makefile.am b/avahi-compat-howl/Makefile.am index 123cdd7..8d0f1dd 100644 --- a/avahi-compat-howl/Makefile.am +++ b/avahi-compat-howl/Makefile.am @@ -49,22 +49,24 @@ avahi_compat_howl_HEADERS = \ lib_LTLIBRARIES = libavahi-compat-howl.la -#noinst_PROGRAMS = txt-test +noinst_PROGRAMS = address-test libavahi_compat_howl_la_SOURCES = \ $(avahi_compat_howl_HEADERS) \ warn.c warn.h \ - unsupported.c + unsupported.c \ + address.c \ + text.c libavahi_compat_howl_la_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) -I $(srcdir)/include libavahi_compat_howl_la_LDFLAGS = $(AM_LDFLAGS) -export-dynamic -version-info $(LIBAVAHI_COMPAT_HOWL_VERSION_INFO) $(PTHREAD_LIBS) ../avahi-common/libavahi-common.la ../avahi-client/libavahi-client.la -#txt_test_SOURCES = \ -# dns_sd.h \ -# txt.c \ -# txt-test.c \ -# warn.c warn.h -#txt_test_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) -#txt_test_LDADD = $(AM_LDADD) ../avahi-common/libavahi-common.la +address_test_SOURCES = \ + $(avahi_compat_howl_HEADERS) \ + address.c \ + address-test.c \ + warn.c warn.h +address_test_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) -I $(srcdir)/include +address_test_LDADD = $(AM_LDADD) ../avahi-common/libavahi-common.la # You can test the compatibility layer by sticking in mDNSResponder's # dns-sd.c source here, naming it "libdns_sd-test.c" and running "make diff --git a/avahi-compat-howl/address-test.c b/avahi-compat-howl/address-test.c new file mode 100644 index 0000000..b9c8b13 --- /dev/null +++ b/avahi-compat-howl/address-test.c @@ -0,0 +1,51 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +#include + +#define ASSERT_SW_OKAY(t) { sw_result r; r = (t); assert(r == SW_OKAY); } +#define ASSERT_NOT_NULL(t) { const void* r; r = (t); assert(r); } + +int main(int argc, char *argv[]) { + sw_ipv4_address a; + char t[256]; + uint8_t a1, a2, a3, a4; + + ASSERT_SW_OKAY(sw_ipv4_address_init_from_name(&a, "heise.de")); + ASSERT_NOT_NULL(sw_ipv4_address_name(a, t, sizeof(t))); + printf("%s\n", t); + + ASSERT_SW_OKAY(sw_ipv4_address_init_from_this_host(&a)); + ASSERT_NOT_NULL(sw_ipv4_address_name(a, t, sizeof(t))); + printf("%s\n", t); + + ASSERT_SW_OKAY(sw_ipv4_address_decompose(a, &a1, &a2, &a3, &a4)); + printf("%i.%i.%i.%i\n", a1, a2, a3, a4); + + return 0; +} diff --git a/avahi-compat-howl/address.c b/avahi-compat-howl/address.c new file mode 100644 index 0000000..6f26d1a --- /dev/null +++ b/avahi-compat-howl/address.c @@ -0,0 +1,213 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "warn.h" + +sw_ipv4_address sw_ipv4_address_any(void) { + sw_ipv4_address a; + + AVAHI_WARN_LINKAGE; + + a.m_addr = htonl(INADDR_ANY); + return a; +} + +sw_ipv4_address sw_ipv4_address_loopback(void) { + sw_ipv4_address a; + + AVAHI_WARN_LINKAGE; + + a.m_addr = htonl(INADDR_LOOPBACK); + return a; +} + +sw_result sw_ipv4_address_init(sw_ipv4_address * self) { + assert(self); + + AVAHI_WARN_LINKAGE; + + self->m_addr = htonl(INADDR_ANY); + return SW_OKAY; +} + +sw_result sw_ipv4_address_init_from_saddr( + sw_ipv4_address *self, + sw_saddr addr) { + + assert(self); + + AVAHI_WARN_LINKAGE; + + self->m_addr = addr; + return SW_OKAY; +} + +sw_result sw_ipv4_address_init_from_name( + sw_ipv4_address *self, + sw_const_string name) { + + struct hostent *he; + + assert(self); + assert(name); + + AVAHI_WARN_LINKAGE; + + if (!(he = gethostbyname(name))) + return SW_E_UNKNOWN; + + self->m_addr = *(uint32_t*) he->h_addr; + return SW_OKAY; +} + +sw_result sw_ipv4_address_init_from_address( + sw_ipv4_address *self, + sw_ipv4_address addr) { + + assert(self); + + AVAHI_WARN_LINKAGE; + + self->m_addr = addr.m_addr; + return SW_OKAY; +} + +sw_result sw_ipv4_address_init_from_this_host(sw_ipv4_address *self) { + struct sockaddr_in sa; + int fd; + socklen_t l = sizeof(sa); + + assert(self); + + AVAHI_WARN_LINKAGE; + + /* This is so fucked up ... */ + + memset(&sa, 0, sizeof(sa)); + sa.sin_family = AF_INET; + sa.sin_addr.s_addr = inet_addr("192.168.1.1"); /* Ouch */ + sa.sin_port = htons(5555); + + if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0 || + connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0 || + getsockname(fd, (struct sockaddr*) &sa, &l) < 0) { + if (fd >= 0) + close(fd); + + perror("fuck"); + return SW_E_UNKNOWN; + } + + assert(l == sizeof(sa)); + close(fd); + + self->m_addr = sa.sin_addr.s_addr; + + return SW_OKAY; +} + +sw_result sw_ipv4_address_fina(sw_ipv4_address self) { + + AVAHI_WARN_LINKAGE; + + /* This is ridiculous ... */ + + return SW_OKAY; +} + +sw_bool sw_ipv4_address_is_any(sw_ipv4_address self) { + AVAHI_WARN_LINKAGE; + return self.m_addr == htonl(INADDR_ANY); +} + +sw_saddr sw_ipv4_address_saddr(sw_ipv4_address self) { + AVAHI_WARN_LINKAGE; + return self.m_addr; +} + +sw_string sw_ipv4_address_name( + sw_ipv4_address self, + sw_string name, + sw_uint32 len) { + + assert(name); + assert(len > 0); + + AVAHI_WARN_LINKAGE; + + if (len < INET_ADDRSTRLEN) + return NULL; + + if (!(inet_ntop(AF_INET, &self.m_addr, name, len))) + return NULL; + + return name; +} + +sw_result sw_ipv4_address_decompose( + sw_ipv4_address self, + sw_uint8 * a1, + sw_uint8 * a2, + sw_uint8 * a3, + sw_uint8 * a4) { + + uint32_t a; + + AVAHI_WARN_LINKAGE; + + a = ntohl(self.m_addr); + + assert(a1); + assert(a2); + assert(a3); + assert(a4); + + *a1 = (uint8_t) (a >> 24); + *a2 = (uint8_t) (a >> 16); + *a3 = (uint8_t) (a >> 8); + *a4 = (uint8_t) (a); + + return SW_OKAY; +} + +sw_bool sw_ipv4_address_equals( + sw_ipv4_address self, + sw_ipv4_address addr) { + + AVAHI_WARN_LINKAGE; + + return self.m_addr == addr.m_addr; +} + diff --git a/avahi-compat-howl/funcs.txt b/avahi-compat-howl/funcs.txt index 818b831..fe5145f 100644 --- a/avahi-compat-howl/funcs.txt +++ b/avahi-compat-howl/funcs.txt @@ -31,19 +31,19 @@ sw_text_record_string_iterator_init sw_text_record_string_iterator_fina sw_text_record_string_iterator_next -sw_ipv4_address_any -sw_ipv4_address_loopback -sw_ipv4_address_init -sw_ipv4_address_init_from_saddr -sw_ipv4_address_init_from_name -sw_ipv4_address_init_from_address -sw_ipv4_address_init_from_this_host -sw_ipv4_address_fina -sw_ipv4_address_is_any -sw_ipv4_address_saddr -sw_ipv4_address_name -sw_ipv4_address_decompose -sw_ipv4_address_equals +x sw_ipv4_address_any +x sw_ipv4_address_loopback +x sw_ipv4_address_init +x sw_ipv4_address_init_from_saddr +x sw_ipv4_address_init_from_name +x sw_ipv4_address_init_from_address +x sw_ipv4_address_init_from_this_host +x sw_ipv4_address_fina +x sw_ipv4_address_is_any +x sw_ipv4_address_saddr +x sw_ipv4_address_name +x sw_ipv4_address_decompose +x sw_ipv4_address_equals sw_salt_step diff --git a/avahi-compat-howl/text.c b/avahi-compat-howl/text.c new file mode 100644 index 0000000..1a3be6d --- /dev/null +++ b/avahi-compat-howl/text.c @@ -0,0 +1,118 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include "warn.h" + +sw_result sw_text_record_init( + sw_text_record * self) { + AVAHI_WARN_UNSUPPORTED; + return SW_DISCOVERY_E_NOT_SUPPORTED; +} + +sw_result sw_text_record_fina( + sw_text_record self) { + AVAHI_WARN_UNSUPPORTED; + return SW_DISCOVERY_E_NOT_SUPPORTED; +} + +sw_result sw_text_record_add_string( + sw_text_record self, + sw_const_string string) { + AVAHI_WARN_UNSUPPORTED; + return SW_DISCOVERY_E_NOT_SUPPORTED; +} + +sw_result sw_text_record_add_key_and_string_value( + sw_text_record self, + sw_const_string key, + sw_const_string val) { + AVAHI_WARN_UNSUPPORTED; + return SW_DISCOVERY_E_NOT_SUPPORTED; +} + +sw_result sw_text_record_add_key_and_binary_value( + sw_text_record self, + sw_const_string key, + sw_octets val, + sw_uint32 len) { + AVAHI_WARN_UNSUPPORTED; + return SW_DISCOVERY_E_NOT_SUPPORTED; +} + +sw_octets sw_text_record_bytes(sw_text_record self) { + AVAHI_WARN_UNSUPPORTED; + return NULL; +} + +sw_uint32 sw_text_record_len(sw_text_record self) { + AVAHI_WARN_UNSUPPORTED; + return 0; +} + +sw_result sw_text_record_iterator_init( + sw_text_record_iterator * self, + sw_octets text_record, + sw_uint32 text_record_len) { + AVAHI_WARN_UNSUPPORTED; + return SW_DISCOVERY_E_NOT_SUPPORTED; +} + +sw_result sw_text_record_iterator_fina( + sw_text_record_iterator self) { + AVAHI_WARN_UNSUPPORTED; + return SW_DISCOVERY_E_NOT_SUPPORTED; +} + +sw_result sw_text_record_iterator_next( + sw_text_record_iterator self, + char key[255], + sw_uint8 val[255], + sw_uint32 * val_len) { + AVAHI_WARN_UNSUPPORTED; + return SW_DISCOVERY_E_NOT_SUPPORTED; +} + +sw_result sw_text_record_string_iterator_init( + sw_text_record_string_iterator * self, + sw_const_string text_record_string) { + AVAHI_WARN_UNSUPPORTED; + return SW_DISCOVERY_E_NOT_SUPPORTED; +} + +sw_result sw_text_record_string_iterator_fina( + sw_text_record_string_iterator self) { + AVAHI_WARN_UNSUPPORTED; + return SW_DISCOVERY_E_NOT_SUPPORTED; +} + +sw_result sw_text_record_string_iterator_next( + sw_text_record_string_iterator self, + char key[255], + char val[255]) { + AVAHI_WARN_UNSUPPORTED; + return SW_DISCOVERY_E_NOT_SUPPORTED; +} diff --git a/avahi-compat-howl/unsupported.c b/avahi-compat-howl/unsupported.c index dc5077a..b7beb0a 100644 --- a/avahi-compat-howl/unsupported.c +++ b/avahi-compat-howl/unsupported.c @@ -232,82 +232,6 @@ void sw_print_debug( AVAHI_WARN_UNSUPPORTED; } -sw_ipv4_address sw_ipv4_address_any(void) { - AVAHI_WARN_UNSUPPORTED_ABORT; -} - -sw_ipv4_address sw_ipv4_address_loopback(void) { - AVAHI_WARN_UNSUPPORTED_ABORT; -} - -sw_result sw_ipv4_address_init(sw_ipv4_address * self) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_ipv4_address_init_from_saddr( - sw_ipv4_address * self, - sw_saddr saddr) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_ipv4_address_init_from_name( - sw_ipv4_address * self, - sw_const_string name) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_ipv4_address_init_from_address( - sw_ipv4_address * self, - sw_ipv4_address addr) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_ipv4_address_init_from_this_host(sw_ipv4_address * self) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_ipv4_address_fina(sw_ipv4_address self) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_bool sw_ipv4_address_is_any(sw_ipv4_address self) { - AVAHI_WARN_UNSUPPORTED_ABORT; -} - -sw_saddr sw_ipv4_address_saddr(sw_ipv4_address self) { - AVAHI_WARN_UNSUPPORTED_ABORT; -} - -sw_string sw_ipv4_address_name( - sw_ipv4_address self, - sw_string name, - sw_uint32 len) { - AVAHI_WARN_UNSUPPORTED; - return NULL; -} - -sw_result sw_ipv4_address_decompose( - sw_ipv4_address self, - sw_uint8 * a1, - sw_uint8 * a2, - sw_uint8 * a3, - sw_uint8 * a4) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_bool sw_ipv4_address_equals( - sw_ipv4_address self, - sw_ipv4_address addr) { - AVAHI_WARN_UNSUPPORTED_ABORT; -} - sw_result sw_tcp_socket_init(sw_socket * self) { AVAHI_WARN_UNSUPPORTED; return SW_DISCOVERY_E_NOT_SUPPORTED; @@ -1194,92 +1118,3 @@ sw_result sw_discovery_salt( return SW_DISCOVERY_E_NOT_SUPPORTED; } -sw_result sw_text_record_init( - sw_text_record * self) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_text_record_fina( - sw_text_record self) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_text_record_add_string( - sw_text_record self, - sw_const_string string) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_text_record_add_key_and_string_value( - sw_text_record self, - sw_const_string key, - sw_const_string val) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_text_record_add_key_and_binary_value( - sw_text_record self, - sw_const_string key, - sw_octets val, - sw_uint32 len) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_octets sw_text_record_bytes(sw_text_record self) { - AVAHI_WARN_UNSUPPORTED; - return NULL; -} - -sw_uint32 sw_text_record_len(sw_text_record self) { - AVAHI_WARN_UNSUPPORTED; - return 0; -} - -sw_result sw_text_record_iterator_init( - sw_text_record_iterator * self, - sw_octets text_record, - sw_uint32 text_record_len) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_text_record_iterator_fina( - sw_text_record_iterator self) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_text_record_iterator_next( - sw_text_record_iterator self, - char key[255], - sw_uint8 val[255], - sw_uint32 * val_len) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_text_record_string_iterator_init( - sw_text_record_string_iterator * self, - sw_const_string text_record_string) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_text_record_string_iterator_fina( - sw_text_record_string_iterator self) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} - -sw_result sw_text_record_string_iterator_next( - sw_text_record_string_iterator self, - char key[255], - char val[255]) { - AVAHI_WARN_UNSUPPORTED; - return SW_DISCOVERY_E_NOT_SUPPORTED; -} -- 2.39.5