[avahi] [PATCH 2/2] avahi-utils: add avahi-simple-browse-service test program
Thomas Petazzoni
thomas.petazzoni at free-electrons.com
Fri May 18 06:08:07 PDT 2012
This new utility allows to easily test the simple protocol
implementation of the BROWSE-SERVICE-TYPE command.
Signed-off-by: Thomas Petazzoni <thomas.petazzoni at free-electrons.com>
---
avahi-utils/Makefile.am | 7 ++-
avahi-utils/avahi-simple-browse-service.c | 86 +++++++++++++++++++++++++++++
2 files changed, 92 insertions(+), 1 deletion(-)
create mode 100644 avahi-utils/avahi-simple-browse-service.c
diff --git a/avahi-utils/Makefile.am b/avahi-utils/Makefile.am
index a644b4a..c7c4545 100644
--- a/avahi-utils/Makefile.am
+++ b/avahi-utils/Makefile.am
@@ -22,9 +22,14 @@ AM_CFLAGS+='-DDEBUG_TRAP=__asm__("int $$3")'
pkglibdatadir=$(libdir)/avahi
+bin_PROGRAMS = avahi-simple-browse-service
+
+avahi_simple_browse_service_SOURCES = avahi-simple-browse-service.c
+avahi_simple_browse_service_CFLAGS = -DAVAHI_SOCKET=\"$(avahi_socket)\"
+
if HAVE_DBUS
-bin_PROGRAMS = avahi-browse avahi-resolve avahi-publish avahi-set-host-name
+bin_PROGRAMS += avahi-browse avahi-resolve avahi-publish avahi-set-host-name
avahi_browse_SOURCES = avahi-browse.c sigint.c sigint.h
avahi_browse_CFLAGS = $(AM_CFLAGS)
diff --git a/avahi-utils/avahi-simple-browse-service.c b/avahi-utils/avahi-simple-browse-service.c
new file mode 100644
index 0000000..d3a5c41
--- /dev/null
+++ b/avahi-utils/avahi-simple-browse-service.c
@@ -0,0 +1,86 @@
+#include <sys/socket.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <arpa/inet.h>
+#include <sys/un.h>
+#include <assert.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <fcntl.h>
+
+#define WHITESPACE " \t"
+
+static int set_cloexec(int fd) {
+ int n;
+ assert(fd >= 0);
+
+ if ((n = fcntl(fd, F_GETFD)) < 0)
+ return -1;
+
+ if (n & FD_CLOEXEC)
+ return 0;
+
+ return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
+}
+
+static FILE *open_socket(void) {
+ int fd = -1;
+ struct sockaddr_un sa;
+ FILE *f = NULL;
+
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
+ goto fail;
+
+ set_cloexec(fd);
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sun_family = AF_UNIX;
+ strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
+ sa.sun_path[sizeof(sa.sun_path)-1] = 0;
+
+ if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0)
+ goto fail;
+
+ if (!(f = fdopen(fd, "r+")))
+ goto fail;
+
+ return f;
+
+fail:
+ if (fd >= 0)
+ close(fd);
+
+ return NULL;
+}
+
+int main(int argc, char *argv[])
+{
+ FILE *f;
+ char ln[256];
+
+ if (argc != 2) {
+ fprintf(stderr, "avahi-simple-browse-service service-type\n");
+ fprintf(stderr, "Examples of services: _http._tcp, _workstation._tcp\n");
+ exit(1);
+ }
+
+ f = open_socket();
+ if (! f) {
+ fprintf(stderr, "Error opening socket\n");
+ exit(1);
+ }
+
+ fprintf(f, "BROWSE-SERVICE-TYPE %s\n", argv[1]);
+
+ while(1) {
+ if (! fgets(ln, sizeof(ln), f)) {
+ fclose(f);
+ exit (1);
+ }
+
+ printf("%s", ln);
+ }
+
+ return 0;
+}
--
1.7.9.5
More information about the avahi
mailing list