cache: reduce allocation of cache entries to one chunk of memory, using calloc_a
[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 int
102 get_iface_index(const char *ifname)
103 {
104 struct ifreq ir;
105 int sock;
106
107 sock = socket(AF_INET, SOCK_DGRAM, 0);
108 if (sock < 0)
109 return 0;
110
111 memset(&ir, 0, sizeof(struct ifreq));
112
113 strncpy(ir.ifr_name, ifname, sizeof(ir.ifr_name));
114
115 if (ioctl(sock, SIOCGIFINDEX, &ir) < 0)
116 return 0;
117
118 close(sock);
119
120 return ir.ifr_ifindex;
121 }
122
123 char*
124 get_hostname(void)
125 {
126 static struct utsname utsname;
127
128 if (uname(&utsname) < 0)
129 return NULL;
130
131 return utsname.nodename;
132 }
133
134 int
135 socket_setup(int fd, const char *ip)
136 {
137 struct ip_mreqn mreq;
138 uint8_t ttl = 255;
139 int yes = 1;
140 int no = 0;
141 struct sockaddr_in sa = { 0 };
142 struct in_addr in;
143
144 inet_aton(iface_ip, &in);
145
146 sa.sin_family = AF_INET;
147 sa.sin_port = htons(MCAST_PORT);
148 inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
149
150 memset(&mreq, 0, sizeof(mreq));
151 mreq.imr_address.s_addr = in.s_addr;
152 mreq.imr_multiaddr = sa.sin_addr;
153 mreq.imr_ifindex = iface_index;
154
155 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
156 fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n");
157
158 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
159 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
160
161 /* Some network drivers have issues with dropping membership of
162 * mcast groups when the iface is down, but don't allow rejoining
163 * when it comes back up. This is an ugly workaround
164 * -- this was copied from avahi --
165 */
166 setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
167
168 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
169 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
170 close(fd);
171 fd = -1;
172 return -1;
173 }
174
175 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0)
176 fprintf(stderr, "ioctl failed: IP_RECVTTL\n");
177
178 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
179 fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
180
181 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0)
182 fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n");
183
184 return 0;
185 }
186
187 void*
188 memdup(void *d, int l)
189 {
190 void *r = malloc(l);
191 if (!r)
192 return NULL;
193 memcpy(r, d, l);
194 return r;
195 }
196