a4a117bc2ef4f49da0b0c90982330cd917654db5
[project/mdnsd.git] / util.c
1 /*
2  * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <sys/socket.h>
15 #include <sys/ioctl.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/utsname.h>
19 #include <linux/if.h>
20 #include <linux/sockios.h>
21 #include <arpa/inet.h>
22
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdlib.h>
29 #include <signal.h>
30
31 #include <libubox/uloop.h>
32
33 #include "dns.h"
34 #include "util.h"
35
36 int debug = 0;
37 struct uloop_fd listener;
38
39 static void
40 signal_shutdown(int signal)
41 {
42         uloop_end();
43 }
44
45 void
46 signal_setup(void)
47 {
48         signal(SIGPIPE, SIG_IGN);
49         signal(SIGTERM, signal_shutdown);
50         signal(SIGKILL, signal_shutdown);
51 }
52
53 uint32_t
54 rand_time_delta(uint32_t t)
55 {
56         uint32_t val;
57         int fd = open("/dev/urandom", O_RDONLY);
58
59         if (!fd)
60                 return t;
61
62         if (read(fd, &val, sizeof(val)) == sizeof(val)) {
63                 int range = t / 30;
64
65                 srand(val);
66                 val = t + (rand() % range) - (range / 2);
67         } else {
68                 val = t;
69         }
70
71         close(fd);
72
73         return val;
74 }
75
76 const char*
77 get_iface_ipv4(const char *ifname)
78 {
79         static char buffer[INET_ADDRSTRLEN];
80         struct ifreq ir;
81         const char *ret;
82         int sock;
83
84         sock = socket(AF_INET, SOCK_DGRAM, 0);
85         if (sock < 0)
86                 return NULL;
87
88         memset(&ir, 0, sizeof(struct ifreq));
89
90         strncpy(ir.ifr_name, ifname, sizeof(ir.ifr_name));
91
92         if (ioctl(sock, SIOCGIFADDR, &ir) < 0)
93                 return NULL;
94
95         ret = inet_ntop(AF_INET, &((struct sockaddr_in *) &ir.ifr_addr)->sin_addr, buffer, sizeof(buffer));
96         close(sock);
97
98         return ret;
99 }
100
101 char*
102 get_hostname(void)
103 {
104         static struct utsname utsname;
105
106         if (uname(&utsname) < 0)
107                 return NULL;
108
109         return utsname.nodename;
110 }
111
112 int
113 socket_setup(int fd, const char *ip)
114 {
115         struct ip_mreqn mreq;
116         uint8_t ttl = 255;
117         int yes = 1;
118         int no = 0;
119         struct sockaddr_in sa;
120
121         sa.sin_family = AF_INET;
122         sa.sin_port = htons(MCAST_PORT);
123         inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
124
125         memset(&mreq, 0, sizeof(mreq));
126         mreq.imr_address.s_addr = htonl(INADDR_ANY);
127         mreq.imr_multiaddr = sa.sin_addr;
128
129         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
130                 fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n");
131
132         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
133                 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
134
135         if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
136                 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
137                 close(fd);
138                 fd = -1;
139                 return -1;
140         }
141
142         if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0)
143                 fprintf(stderr, "ioctl failed: IP_RECVTTL\n");
144
145         if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
146                 fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
147
148         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0)
149                 fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n");
150
151         return 0;
152 }
153
154 void*
155 memdup(void *d, int l)
156 {
157         void *r = malloc(l);
158         if (!r)
159                 return NULL;
160         memcpy(r, d, l);
161         return r;
162 }
163