-
Notifications
You must be signed in to change notification settings - Fork 45
/
avahi.c
73 lines (67 loc) · 3.09 KB
/
avahi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Brand new version that just forks and execs the commands 'avahi-browse' or 'avahi-publish'
// Using the Avahi API is a nightmare; it's a twisted mess of callbacks between "callbacks", "services", "resolvers", etc.
// March 2024, Phil Karn KA9Q
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "avahi.h"
int avahi_start(char const *service_name,char const *service_type,int const service_port,char const *dns_name,int address,char const *description,void *sock,int *socksize){
if(sock != NULL && socksize != NULL){
// Return sockaddr structure
if(*socksize >= sizeof(struct sockaddr_in)){
struct sockaddr_in *sin = sock;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = htonl(address);
sin->sin_port = htons(service_port);
*socksize = sizeof(struct sockaddr_in);
} else
*socksize = 0;
}
int pid = getpid(); // Advertise the parent's pid, not the child's
if(fork() == 0){
#if 0
fprintf(stdout,"avahi-publish-service child pid %d\n",getpid());
#endif
// run "avahi-publish-service --no-fail --host=dns_name service_name service_type service_port description pid hostname" in subprocess
// No need to free the asprintf-allocated strings, we're calling exec anyway
// Turn off warnings about unused return values from asprintf generated by some compilers
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
char *port_string = NULL;
asprintf(&port_string, "%d", service_port);
char *host_string = NULL;
asprintf(&host_string, "--host=%s",dns_name);
char *pid_string = NULL;
asprintf(&pid_string, "pid=%d", pid);
char hostname[sysconf(_SC_HOST_NAME_MAX)];
gethostname(hostname,sizeof(hostname));
char *source_string = NULL;
asprintf(&source_string, "source=%s", hostname);
#pragma GCC diagnostic pop
#if 0
fprintf(stdout,"%s %s %s %s %s %s %s %s %s %s\n",
"avahi-publish-service", "avahi-publish-service", "--no-fail", host_string, service_name, service_type, port_string, description, pid_string, hostname);
#endif
execlp("avahi-publish-service", "avahi-publish-service", "--no-fail", host_string, service_name, service_type, port_string, description, pid_string, source_string, NULL);
perror("exec avahi publish service");
return -1;
}
if(fork() == 0){
// run "avahi-publish-address dns_name address"
char *ip_address_string = NULL; // No need to free, we're calling exec
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
asprintf(&ip_address_string,"%d.%d.%d.%d",(address >> 24) & 0xff, (address >> 16) & 0xff, (address >> 8) & 0xff, address & 0xff);
#pragma GCC diagnostic pop
#if 0
fprintf(stdout,"avahi start: ip address string = %s\n",ip_address_string);
fprintf(stdout,"avahi-publish-address child pid %d\n",getpid());
fprintf(stdout,"%s %s %s %s\n",
"avahi-publish-address", "avahi-publish-address",dns_name,ip_address_string);
#endif
execlp("avahi-publish-address", "avahi-publish-address",dns_name,ip_address_string, NULL);
perror("exec avahi publish address");
return -1;
}
return 0;
}