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 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.
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
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
AVAHI_LLIST_FIELDS(DNSServerInfo, servers);
};
static AVAHI_LLIST_HEAD(DNSServerInfo, servers);
static void server_info_free(DNSServerInfo *i) {
AVAHI_LLIST_FIELDS(DNSServerInfo, servers);
};
static AVAHI_LLIST_HEAD(DNSServerInfo, servers);
static void server_info_free(DNSServerInfo *i) {
AVAHI_LLIST_REMOVE(DNSServerInfo, servers, servers, i);
AVAHI_LLIST_REMOVE(DNSServerInfo, servers, servers, i);
-static DNSServerInfo* get_server_info(gint interface, guchar protocol, const gchar *address) {
+static DNSServerInfo* get_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
-static DNSServerInfo* new_server_info(gint interface, guchar protocol, const gchar *address) {
+static DNSServerInfo* new_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);
AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);
+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);
+}
+
memset(&sa, 0, sizeof(sa));
sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
memset(&sa, 0, sizeof(sa));
sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
+ DNSServerInfo *j;
+ char *t;
+
+ /* Filter out double entries */
+ for (j = servers; j != i; j = j->servers_next)
+ if (j->interface == interface || interface <= 0)
+ if (strcmp(i->address, j->address) == 0)
+ break;
+
+ if (j != i)
+ continue;
- g_assert(interface >= 0);
-
- if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
- daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
- goto finish;
- }
+ assert(name);
+ assert(value);
- memset(&ifr, 0, sizeof(ifr));
- ifr.ifr_ifindex = interface;
-
- if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
- daemon_log(LOG_ERR, "SIOCGIFNAME: %s\n", strerror(errno));
- goto finish;
+ l = strlen(name);
+
+ for (e = environ; *e; e++) {
+ /* Search for the variable */
+ if (strlen(*e) < l+1)
+ continue;
+
+ if (strncmp(*e, name, l) != 0 || (*e)[l] != '=')
+ continue;
+
+ /* We simply free the record, sicne we know that we created it previously */
+ avahi_free(*e);
+ *e = avahi_strdup_printf("%s=%s", name, value);
+ return;
-static void run_script(gboolean new, gint interface, guchar protocol, const gchar *address) {
- gchar *p;
- g_assert(interface > 0);
- gint ret;
- gchar ia[16], pa[16];
- gchar name[IFNAMSIZ+1];
+static void run_script(int new, AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
+ char *p;
+ int ret;
+ char ia[16], pa[16];
+ char name[IF_NAMESIZE];
- g_setenv("AVAHI_INTERFACE", name, TRUE);
-
- snprintf(ia, sizeof(ia), "%i", interface);
- snprintf(pa, sizeof(pa), "%u", protocol);
+ set_env(ENV_INTERFACE, name);
- if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, NULL) < 0)
+ snprintf(ia, sizeof(ia), "%i", (int) interface);
+ snprintf(pa, sizeof(pa), "%i", (int) protocol);
+
+ if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, avahi_proto_to_string(protocol), NULL) < 0)
daemon_log(LOG_WARNING, "Failed to run script");
else if (ret != 0)
daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
}
daemon_log(LOG_WARNING, "Failed to run script");
else if (ret != 0)
daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
}
- gint interface;
- guint protocol;
- guint port;
- gchar a[64];
-
- g_assert(state == BROWSING);
+ AvahiIfIndex interface;
+ AvahiProtocol protocol;
+ int i_interface, i_protocol, port;
+ char a[AVAHI_ADDRESS_STR_MAX];
+
+ assert(state == BROWSING);
if (*l != '<' && *l != '>') {
daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
return -1;
}
if (*l != '<' && *l != '>') {
daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
return -1;
}
- if (sscanf(l+1, "%i %u %64s %u", &interface, &protocol, a, &port) != 4) {
+ if (sscanf(l+1, "%i %i %39s %i", &i_interface, &i_protocol, a, &port) != 4) {
- daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%u)", a, interface, protocol);
- new_server_info(interface, (guchar) protocol, a);
- run_script(TRUE, interface, (guchar) protocol, a);
+ daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
+ new_server_info(interface, protocol, a);
+ run_script(1, interface, protocol, a);
- if (port == 53)
- if ((i = get_server_info(interface, (guchar) protocol, a))) {
- daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%u)", a, interface, protocol);
+ if (port == 53)
+ if ((i = get_server_info(interface, protocol, a))) {
+ daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
- run_script(FALSE, interface, (guchar) protocol, a);
+ run_script(0, interface, protocol, a);
- gint interface = servers->interface;
- guchar protocol = servers->protocol;
- gchar *address = g_strdup(servers->address);
+ AvahiIfIndex interface = servers->interface;
+ AvahiProtocol protocol = servers->protocol;
+ char *address = avahi_strdup(servers->address);
-
- run_script(FALSE, interface, protocol, address);
- g_free(address);
+
+ run_script(0, interface, protocol, address);
+ avahi_free(address);
" -k --kill Kill a running daemon\n"
" -r --refresh Request a running daemon to refresh DNS server data\n"
" -c --check Return 0 if a daemon is already running\n"
" -k --kill Kill a running daemon\n"
" -r --refresh Request a running daemon to refresh DNS server data\n"
" -c --check Return 0 if a daemon is already running\n"
-static gint parse_command_line(int argc, char *argv[]) {
- gint c;
-
- static const struct option const long_options[] = {
+static int parse_command_line(int argc, char *argv[]) {
+ int c;
+
+ static const struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "daemonize", no_argument, NULL, 'D' },
{ "help", no_argument, NULL, 'h' },
{ "daemonize", no_argument, NULL, 'D' },
{ "kill", no_argument, NULL, 'k' },
{ "version", no_argument, NULL, 'V' },
{ "refresh", no_argument, NULL, 'r' },
{ "check", no_argument, NULL, 'c' },
{ "kill", no_argument, NULL, 'k' },
{ "version", no_argument, NULL, 'V' },
{ "refresh", no_argument, NULL, 'r' },
{ "check", no_argument, NULL, 'c' },
- opterr = 0;
- while ((c = getopt_long(argc, argv, "hDkVrc", long_options, NULL)) >= 0) {
+ while ((c = getopt_long(argc, argv, "hDkVrcs", long_options, NULL)) >= 0) {
daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
-gint main(gint argc, gchar *argv[]) {
- gchar *argv0;
- gint r = 1;
- gboolean wrote_pid_file = FALSE;
+int main(int argc, char *argv[]) {
+ char *argv0;
+ int r = 1;
+ int wrote_pid_file = 0;
r = 0;
} else if (command == DAEMON_VERSION) {
printf("%s "PACKAGE_VERSION"\n", argv0);
r = 0;
} else if (command == DAEMON_VERSION) {
printf("%s "PACKAGE_VERSION"\n", argv0);
r = 0;
} else if (command == DAEMON_KILL) {
if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
goto finish;
}
r = 0;
} else if (command == DAEMON_KILL) {
if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
goto finish;
}