#include <glib.h>
#include <avahi-core/llist.h>
+#include <avahi-core/log.h>
#include "simple-protocol.h"
#include "main.h"
-#define BUFFER_SIZE (10*1024)
+#define BUFFER_SIZE (20*1024)
-#define UNIX_SOCKET_PATH "/tmp/avahi"
-#define UNIX_SOCKET UNIX_SOCKET_PATH"/socket"
+#define UNIX_SOCKET AVAHI_RUNTIME_DIR "/socket"
#define CLIENTS_MAX 50
CLIENT_IDLE,
CLIENT_RESOLVE_HOSTNAME,
CLIENT_RESOLVE_ADDRESS,
+ CLIENT_BROWSE_DNS_SERVERS,
CLIENT_DEAD
} ClientState;
AvahiHostNameResolver *host_name_resolver;
AvahiAddressResolver *address_resolver;
+ AvahiDNSServerBrowser *dns_server_browser;
AVAHI_LLIST_FIELDS(Client, clients);
};
AVAHI_LLIST_HEAD(Client, clients);
guint n_clients;
+ gboolean bind_successful;
};
static Server *server = NULL;
if (c->address_resolver)
avahi_address_resolver_free(c->address_resolver);
+
+ if (c->dns_server_browser)
+ avahi_dns_server_browser_free(c->dns_server_browser);
g_source_remove_poll(&c->server->source, &c->poll_fd);
close(c->fd);
c->host_name_resolver = NULL;
c->address_resolver = NULL;
+ c->dns_server_browser = NULL;
memset(&c->poll_fd, 0, sizeof(GPollFD));
c->poll_fd.fd = fd;
}
static void client_output_printf(Client *c, gchar *format, ...) {
- gchar txt[256];
+ gchar *t;
va_list ap;
va_start(ap, format);
- vsnprintf(txt, sizeof(txt), format, ap);
+ t = g_strdup_vprintf(format, ap);
va_end(ap);
- client_output(c, (guint8*) txt, strlen(txt));
+ client_output(c, (guint8*) t, strlen(t));
+ g_free(t);
}
static void host_name_resolver_callback(AvahiHostNameResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) {
else {
gchar t[64];
avahi_address_snprint(t, sizeof(t), a);
- client_output_printf(c, "+ %s\n", t);
+ client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
}
c->state = CLIENT_DEAD;
static void address_resolver_callback(AvahiAddressResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) {
Client *c = userdata;
+
g_assert(c);
if (event == AVAHI_RESOLVER_TIMEOUT)
client_output_printf(c, "- Query timed out\n");
else
- client_output_printf(c, "+ %s\n", hostname);
+ client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
c->state = CLIENT_DEAD;
}
+static void dns_server_browser_callback(AvahiDNSServerBrowser *b, gint interface, guchar protocol, AvahiBrowserEvent event, const gchar *host_name, const AvahiAddress *a, guint16 port, gpointer userdata) {
+ Client *c = userdata;
+ gchar t[64];
+
+ g_assert(c);
+
+ if (!a)
+ return;
+
+ avahi_address_snprint(t, sizeof(t), a);
+ client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<', interface, protocol, t, port);
+}
+
static void handle_line(Client *c, const gchar *s) {
gchar cmd[64], arg[64];
gint n_args;
"+ RESOLVE-HOSTNAME <hostname>\n"
"+ RESOLVE-HOSTNAME-IPV6 <hostname>\n"
"+ RESOLVE-HOSTNAME-IPV4 <hostname>\n"
- "+ RESOLVE-ADDRESS <address>\n");
+ "+ RESOLVE-ADDRESS <address>\n"
+ "+ BROWSE-DNS-SERVERS\n"
+ "+ BROWSE-DNS-SERVERS-IPV4\n"
+ "+ BROWSE-DNS-SERVERS-IPV6\n");
c->state = CLIENT_DEAD; }
else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
client_output_printf(c, "FUCK: Go fuck yourself!\n");
c->state = CLIENT_RESOLVE_ADDRESS;
c->address_resolver = avahi_address_resolver_new(avahi_server, -1, AF_UNSPEC, &addr, address_resolver_callback, c);
}
+ } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
+ c->state = CLIENT_BROWSE_DNS_SERVERS;
+ c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, AF_INET, dns_server_browser_callback, c);
+ client_output_printf(c, "+ Browsing ...\n");
+ } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
+ c->state = CLIENT_BROWSE_DNS_SERVERS;
+ c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, AF_INET6, dns_server_browser_callback, c);
+ client_output_printf(c, "+ Browsing ...\n");
+ } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
+ c->state = CLIENT_BROWSE_DNS_SERVERS;
+ c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, AF_UNSPEC, dns_server_browser_callback, c);
+ client_output_printf(c, "+ Browsing ...\n");
} else {
client_output_printf(c, "- Invalid command \"%s\", try \"HELP\".\n", cmd);
c->state = CLIENT_DEAD;
if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
if (r < 0)
- g_warning("read(): %s", strerror(errno));
+ avahi_log_warn("read(): %s", strerror(errno));
client_free(c);
return;
}
ssize_t r;
if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
- g_warning("write(): %s", strerror(errno));
+ avahi_log_warn("write(): %s", strerror(errno));
client_free(c);
return;
}
gint fd;
if ((fd = accept(s->fd, NULL, NULL)) < 0)
- g_warning("accept(): %s", strerror(errno));
+ avahi_log_warn("accept(): %s", strerror(errno));
else
client_new(s, fd);
} else if (s->poll_fd.revents)
g_assert(!server);
server = (Server*) g_source_new(&source_funcs, sizeof(Server));
+ server->bind_successful = FALSE;
server->fd = -1;
AVAHI_LLIST_HEAD_INIT(Client, server->clients);
- if (c)
- g_main_context_ref(server->context = c);
- else
- server->context = g_main_context_default();
+ g_main_context_ref(server->context = (c ? c : g_main_context_default()));
server->clients = NULL;
u = umask(0000);
- if (mkdir(UNIX_SOCKET_PATH, 0755) < 0 && errno != EEXIST) {
- g_warning("mkdir(): %s", strerror(errno));
- goto fail;
- }
-
if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
- g_warning("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
+ avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
goto fail;
}
sa.sun_family = AF_LOCAL;
strncpy(sa.sun_path, UNIX_SOCKET, sizeof(sa.sun_path)-1);
+ /* We simply remove existing UNIX sockets under this name. The
+ Avahi daemons makes sure that it runs only once on a host,
+ therefore sockets that already exist are stale and may be
+ removed without any ill effects */
+
+ unlink(UNIX_SOCKET);
+
if (bind(server->fd, &sa, sizeof(sa)) < 0) {
- g_warning("bind(): %s", strerror(errno));
+ avahi_log_warn("bind(): %s", strerror(errno));
goto fail;
}
+
+ server->bind_successful = TRUE;
if (listen(server->fd, 2) < 0) {
- g_warning("listen(): %s", strerror(errno));
+ avahi_log_warn("listen(): %s", strerror(errno));
goto fail;
}
while (server->clients)
client_free(server->clients);
+
+ if (server->bind_successful)
+ unlink(UNIX_SOCKET);
- if (server->fd >= 0) {
- unlink(UNIX_SOCKET_PATH);
+ if (server->fd >= 0)
close(server->fd);
- }
g_main_context_unref(server->context);
+
g_source_destroy(&server->source);
g_source_unref(&server->source);