summaryrefslogtreecommitdiff
path: root/mdns_tinysvcmdns.c
diff options
context:
space:
mode:
authorMke Brady <mikebrady@eircom.net>2015-06-01 23:16:50 +0100
committerMke Brady <mikebrady@eircom.net>2015-06-01 23:16:50 +0100
commit87a0475c16a8203c9029a34e60cec00e23a86ab7 (patch)
treec454ba62ad2d964f7fde8481774115662cc3587f /mdns_tinysvcmdns.c
parent04e27cfe4f37f411612e44a7af939ec5848f35d6 (diff)
Reformat using clang-format.
Diffstat (limited to 'mdns_tinysvcmdns.c')
-rw-r--r--mdns_tinysvcmdns.c200
1 files changed, 90 insertions, 110 deletions
diff --git a/mdns_tinysvcmdns.c b/mdns_tinysvcmdns.c
index 8b79d49..a3c475a 100644
--- a/mdns_tinysvcmdns.c
+++ b/mdns_tinysvcmdns.c
@@ -27,7 +27,7 @@
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
-#include <net/if.h>
+#include <net/if.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <netinet/in.h>
@@ -39,128 +39,108 @@
static struct mdnsd *svr = NULL;
static int mdns_tinysvcmdns_register(char *apname, int port) {
- struct ifaddrs *ifalist;
- struct ifaddrs *ifa;
-
- svr = mdnsd_start();
- if (svr == NULL) {
- warn("tinysvcmdns: mdnsd_start() failed");
- return -1;
- }
-
- // Thanks to Paul Lietar for this
- // room for name + .local + NULL
- char hostname[100 + 6];
- gethostname(hostname, 99);
- // according to POSIX, this may be truncated without a final NULL !
- hostname[99] = 0;
-
- // will not work if the hostname doesn't end in .local
- char *hostend = hostname + strlen(hostname);
- if ((strlen(hostname) < strlen(".local")) || (strcmp(hostend - 6, ".local")!=0)) {
- strcat(hostname, ".local");
- }
-
- if (getifaddrs(&ifalist) < 0)
- {
- warn("tinysvcmdns: getifaddrs() failed");
- return -1;
+ struct ifaddrs *ifalist;
+ struct ifaddrs *ifa;
+
+ svr = mdnsd_start();
+ if (svr == NULL) {
+ warn("tinysvcmdns: mdnsd_start() failed");
+ return -1;
+ }
+
+ // Thanks to Paul Lietar for this
+ // room for name + .local + NULL
+ char hostname[100 + 6];
+ gethostname(hostname, 99);
+ // according to POSIX, this may be truncated without a final NULL !
+ hostname[99] = 0;
+
+ // will not work if the hostname doesn't end in .local
+ char *hostend = hostname + strlen(hostname);
+ if ((strlen(hostname) < strlen(".local")) || (strcmp(hostend - 6, ".local") != 0)) {
+ strcat(hostname, ".local");
+ }
+
+ if (getifaddrs(&ifalist) < 0) {
+ warn("tinysvcmdns: getifaddrs() failed");
+ return -1;
+ }
+
+ ifa = ifalist;
+
+ // Look for an ipv4/ipv6 non-loopback interface to use as the main one.
+ for (ifa = ifalist; ifa != NULL; ifa = ifa->ifa_next) {
+ if (!(ifa->ifa_flags & IFF_LOOPBACK) && ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
+ uint32_t main_ip = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
+
+ mdnsd_set_hostname(svr, hostname, main_ip); // TTL should be 120 seconds
+ break;
+ } else if (!(ifa->ifa_flags & IFF_LOOPBACK) && ifa->ifa_addr &&
+ ifa->ifa_addr->sa_family == AF_INET6) {
+ struct in6_addr *addr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
+
+ mdnsd_set_hostname_v6(svr, hostname, addr); // TTL should be 120 seconds
+ break;
}
-
- ifa = ifalist;
-
- // Look for an ipv4/ipv6 non-loopback interface to use as the main one.
- for (ifa = ifalist; ifa != NULL; ifa = ifa->ifa_next)
- {
- if (!(ifa->ifa_flags & IFF_LOOPBACK) && ifa->ifa_addr &&
- ifa->ifa_addr->sa_family == AF_INET)
- {
- uint32_t main_ip = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
-
- mdnsd_set_hostname(svr, hostname, main_ip); // TTL should be 120 seconds
- break;
- }
- else if (!(ifa->ifa_flags & IFF_LOOPBACK) && ifa->ifa_addr &&
- ifa->ifa_addr->sa_family == AF_INET6)
- {
- struct in6_addr *addr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
-
- mdnsd_set_hostname_v6(svr, hostname, addr); // TTL should be 120 seconds
- break;
- }
- }
-
- if (ifa == NULL)
- {
- warn("tinysvcmdns: no non-loopback ipv4 or ipv6 interface found");
- return -1;
+ }
+
+ if (ifa == NULL) {
+ warn("tinysvcmdns: no non-loopback ipv4 or ipv6 interface found");
+ return -1;
+ }
+
+ // Skip the first one, it was already added by set_hostname
+ for (ifa = ifa->ifa_next; ifa != NULL; ifa = ifa->ifa_next) {
+ if (ifa->ifa_flags & IFF_LOOPBACK) // Skip loop-back interfaces
+ continue;
+
+ switch (ifa->ifa_addr->sa_family) {
+ case AF_INET: { // ipv4
+ uint32_t ip = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
+ struct rr_entry *a_e = rr_create_a(create_nlabel(hostname), ip); // TTL should be 120 seconds
+ mdnsd_add_rr(svr, a_e);
+ } break;
+ case AF_INET6: { // ipv6
+ struct in6_addr *addr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
+ struct rr_entry *aaaa_e =
+ rr_create_aaaa(create_nlabel(hostname), addr); // TTL should be 120 seconds
+ mdnsd_add_rr(svr, aaaa_e);
+ } break;
}
+ }
+ freeifaddrs(ifa);
- // Skip the first one, it was already added by set_hostname
- for (ifa = ifa->ifa_next; ifa != NULL; ifa = ifa->ifa_next)
- {
- if (ifa->ifa_flags & IFF_LOOPBACK) // Skip loop-back interfaces
- continue;
-
- switch (ifa->ifa_addr->sa_family)
- {
- case AF_INET: { // ipv4
- uint32_t ip = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
- struct rr_entry *a_e = rr_create_a(create_nlabel(hostname), ip); // TTL should be 120 seconds
- mdnsd_add_rr(svr, a_e);
- }
- break;
- case AF_INET6: { // ipv6
- struct in6_addr *addr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
- struct rr_entry *aaaa_e = rr_create_aaaa(create_nlabel(hostname), addr); // TTL should be 120 seconds
- mdnsd_add_rr(svr, aaaa_e);
- }
- break;
- }
- }
-
- freeifaddrs(ifa);
+ char *txtwithoutmetadata[] = {MDNS_RECORD_WITHOUT_METADATA, NULL};
+#ifdef CONFIG_METADATA
+ char *txtwithmetadata[] = {MDNS_RECORD_WITH_METADATA, NULL};
+#endif
+ char **txt;
- char *txtwithoutmetadata[] = { MDNS_RECORD_WITHOUT_METADATA, NULL };
#ifdef CONFIG_METADATA
- char *txtwithmetadata[] = { MDNS_RECORD_WITH_METADATA, NULL };
-#endif
- char **txt;
-
-#ifdef CONFIG_METADATA
- if (config.meta_dir)
- txt = txtwithmetadata;
- else
+ if (config.meta_dir)
+ txt = txtwithmetadata;
+ else
#endif
- txt = txtwithoutmetadata;
+ txt = txtwithoutmetadata;
-
-
- struct mdns_service *svc = mdnsd_register_svc(svr,
- apname,
- "_raop._tcp.local",
- port,
- NULL,
- (const char **)txt); // TTL should be 75 minutes, i.e. 4500 seconds
+ struct mdns_service *svc =
+ mdnsd_register_svc(svr, apname, "_raop._tcp.local", port, NULL,
+ (const char **)txt); // TTL should be 75 minutes, i.e. 4500 seconds
- mdns_service_destroy(svc);
+ mdns_service_destroy(svc);
- return 0;
+ return 0;
}
static void mdns_tinysvcmdns_unregister(void) {
- if (svr)
- {
- mdnsd_stop(svr);
- svr = NULL;
- }
+ if (svr) {
+ mdnsd_stop(svr);
+ svr = NULL;
+ }
}
-mdns_backend mdns_tinysvcmdns = {
- .name = "tinysvcmdns",
- .mdns_register = mdns_tinysvcmdns_register,
- .mdns_unregister = mdns_tinysvcmdns_unregister
-};
-
+mdns_backend mdns_tinysvcmdns = {.name = "tinysvcmdns",
+ .mdns_register = mdns_tinysvcmdns_register,
+ .mdns_unregister = mdns_tinysvcmdns_unregister};