announce_timer(struct uloop_timeout *timeout)
{
struct interface *iface = container_of(timeout, struct interface, announce_timer);
- char host[256];
-
- snprintf(host, sizeof(host), "%s.local", hostname);
switch (iface->announce_state) {
case STATE_PROBE1:
case STATE_PROBE2:
case STATE_PROBE3:
- dns_send_question(iface, host, TYPE_ANY);
+ dns_send_question(iface, mdns_hostname_local, TYPE_ANY);
uloop_timeout_set(timeout, 250);
iface->announce_state++;
break;
break;
case STATE_PROBE_END:
- if (cache_host_is_known(host)) {
- fprintf(stderr, "the host %s already exists. stopping announce service\n", host);
+ if (cache_host_is_known(mdns_hostname_local)) {
+ fprintf(stderr, "the host %s already exists. stopping announce service\n", mdns_hostname_local);
return;
}
iface->announce_state++;
switch (q->type) {
case TYPE_ANY:
- host = service_name("local");
- if (!strcmp(name, host))
+ if (!strcmp(name, mdns_hostname_local))
service_reply(iface, NULL);
break;
host = strstr(name, ".local");
if (host)
*host = '\0';
- if (!strcmp(hostname, name))
+ if (!strcmp(mdns_hostname, name))
service_reply_a(iface, q->type);
break;
};
{
int ch, ttl;
- while ((ch = getopt(argc, argv, "h:t:i:d")) != -1) {
+ while ((ch = getopt(argc, argv, "t:i:d")) != -1) {
switch (ch) {
- case 'h':
- hostname = optarg;
- break;
case 't':
ttl = atoi(optarg);
if (ttl > 0)
static struct blob_buf b;
static VLIST_TREE(services, avl_strcmp, service_update, false, false);
-char *hostname = NULL;
static char *sdudp = "_services._dns-sd._udp.local";
static char *sdtcp = "_services._dns-sd._tcp.local";
-char *
+static const char *
service_name(const char *domain)
{
static char buffer[256];
- snprintf(buffer, sizeof(buffer), "%s.%s", hostname, domain);
+ snprintf(buffer, sizeof(buffer), "%s.%s", mdns_hostname, domain);
return buffer;
}
service_add_srv(struct service *s)
{
struct dns_srv_data *sd = (struct dns_srv_data *) mdns_buf;
- char *host = service_name("local");
int len = sizeof(*sd);
- len += dn_comp(host, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
+ len += dn_comp(mdns_hostname_local, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
if (len <= sizeof(*sd))
return;
sd->port = cpu_to_be16(s->port);
dns_add_answer(TYPE_SRV, mdns_buf, len);
- service_name(s->service);
}
#define TOUT_LOOKUP 60
dns_init_answer();
dns_add_answer(TYPE_A, (uint8_t *) &iface->v4_addr.s_addr, 4);
- dns_send_answer(iface, service_name("local"));
+ dns_send_answer(iface, mdns_hostname_local);
}
void
struct service *s;
vlist_for_each_element(&services, s, node) {
- char *host = service_name(s->service);
+ const char *host = service_name(s->service);
char *service = strstr(host, "._");
if (!s->active || !service || !service_timeout(s))
void
service_init(void)
{
- if (!hostname)
- hostname = get_hostname();
+ get_hostname();
vlist_update(&services);
service_load("/tmp/run/mdnsd/*");
#ifndef _SERVICE_H__
#define _SERVICE_H__
-extern char *hostname;
-extern char *service_name(const char *domain);
extern void service_init(void);
extern void service_cleanup(void);
extern void service_announce(struct interface *iface);
uint8_t mdns_buf[MDNS_BUF_LEN];
int debug = 0;
+char mdns_hostname[HOSTNAME_LEN];
+char mdns_hostname_local[HOSTNAME_LEN + 6];
+
static void
signal_shutdown(int signal)
{
return val;
}
-char*
-get_hostname(void)
+void get_hostname(void)
{
- static struct utsname utsname;
+ struct utsname utsname;
+
+ mdns_hostname[0] = 0;
+ mdns_hostname_local[0] = 0;
if (uname(&utsname) < 0)
- return NULL;
+ return;
- return utsname.nodename;
+ snprintf(mdns_hostname, sizeof(mdns_hostname), "%s", utsname.nodename);
+ snprintf(mdns_hostname_local, sizeof(mdns_hostname_local), "%s.local", utsname.nodename);
}
void*
} while (0)
#define MDNS_BUF_LEN (8 * 1024)
+#define HOSTNAME_LEN 256
extern int debug;
extern uint8_t mdns_buf[MDNS_BUF_LEN];
+extern char mdns_hostname[HOSTNAME_LEN];
+extern char mdns_hostname_local[HOSTNAME_LEN + 6];
void *memdup(const void *d, int l);
extern void signal_setup(void);
-extern char* get_hostname(void);
+extern void get_hostname(void);
extern uint32_t rand_time_delta(uint32_t t);
#endif