Make it possible to set the hostname explicitly rather than using get{host,domain...
[icmp-dn.git] / src / icmpdnd.c
CommitLineData
fa6ac2fc
DC
1/*
2 * icmpdnd - ICMP Domain Name responder daemon for Linux
b39da562 3 * Copyright (C) 2005 Fredrik Tolf <fredrik@dolda2000.com>
fa6ac2fc
DC
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19#include <stdlib.h>
20#include <unistd.h>
21#include <stdio.h>
22#include <string.h>
23#include <errno.h>
b39da562 24#include <syslog.h>
dcb0aee6 25#include <time.h>
fa6ac2fc 26#include <sys/socket.h>
6796167f 27#include <sys/poll.h>
fa6ac2fc
DC
28#include <netinet/in.h>
29#include <arpa/inet.h>
30#include <netinet/ip.h>
31#include <sys/types.h>
606a39c9 32#ifdef HAVE_CONFIG_H
2269406c 33#include "config.h"
606a39c9 34#endif
fa6ac2fc 35
ac46a58c
DC
36#ifndef MAXHNAME
37#define MAXHNAME 1024
38#endif
39
fa6ac2fc
DC
40struct icmphdr {
41 u_int8_t type;
42 u_int8_t code;
43 u_int16_t checksum;
44};
45
46struct reqhdr {
47 u_int8_t type;
48 u_int8_t code;
49 u_int16_t checksum;
50 u_int16_t id;
51 u_int16_t seq;
52};
53
54struct rephdr {
55 u_int8_t type;
56 u_int8_t code;
57 u_int16_t checksum;
58 u_int16_t id;
59 u_int16_t seq;
60 int32_t ttl;
61};
62
63#define ICMP_NAMEREQ 37
64#define ICMP_NAMEREP 38
65
fa6ac2fc 66volatile int alive;
ac46a58c 67char myname[MAXHNAME] = "";
fa6ac2fc 68
ac46a58c 69void setname(char *newname)
fa6ac2fc 70{
ac46a58c 71 int nl;
fa6ac2fc 72
ac46a58c
DC
73 if(newname == NULL) {
74 if(gethostname(myname, sizeof(myname)) < 0) {
75 perror("gethostname");
76 exit(1);
77 }
78 nl = strlen(myname);
79 myname[nl++] = '.';
80 if(getdomainname(myname + nl, sizeof(myname) - nl) < 0) {
81 perror("getdomainname");
82 exit(1);
83 }
84 if(strlen(myname + nl) != 0) {
85 nl = strlen(myname);
86 myname[nl++] = '.';
87 }
88 myname[nl] = 0;
89 } else {
90 strcpy(myname, newname);
91 nl = strlen(myname);
92 if(myname[nl - 1] != '.') {
93 myname[nl] = '.';
94 myname[nl + 1] = 0;
95 }
fa6ac2fc 96 }
ac46a58c
DC
97}
98
99size_t filldn(char *dst)
100{
101 char *p, *p2, *dp;
102 char namebuf[MAXHNAME];
fa6ac2fc 103
ac46a58c 104 strcpy(namebuf, myname);
fa6ac2fc
DC
105 p = namebuf;
106 dp = dst;
107 while((p2 = strchr(p, '.')) != NULL) {
108 *p2 = 0;
109 *(dp++) = p2 - p;
110 memcpy(dp, p, p2 - p);
111 dp += p2 - p;
112 p = p2 + 1;
113 }
114 *(dp++) = 0;
115
116 return(dp - dst);
117}
118
119void cksum(void *hdr, size_t len)
120{
121 struct icmphdr *ih;
122 u_int8_t *cb;
123 int i;
124 int b1, b2;
125
126 ih = (struct icmphdr *)hdr;
127 cb = (u_int8_t *)hdr;
128 ih->checksum = 0;
129 b1 = b2 = 0;
130 for(i = 0; i < (len & ~1); i += 2) {
131 b1 += cb[i];
132 b2 += cb[i + 1];
133 }
134 if(i & 1)
135 b1 += cb[len - 1];
136 while(1) {
137 if(b1 >= 256) {
138 b2 += b1 >> 8;
139 b1 &= 0xff;
140 continue;
141 }
142 if(b2 >= 256) {
143 b1 += b2 >> 8;
144 b2 &= 0xff;
145 continue;
146 }
147 break;
148 }
149 cb = (u_int8_t *)&ih->checksum;
150 cb[0] = ~(u_int8_t)b1;
151 cb[1] = ~(u_int8_t)b2;
152}
153
154int main(int argc, char **argv)
155{
6796167f 156 int i, n, ret;
df4e01a5 157 int c, cs, s4, s6, datalen;
b39da562 158 int daemonize, ttl;
fa6ac2fc 159 unsigned char buf[65536];
6796167f 160 struct sockaddr_storage name;
fa6ac2fc
DC
161 struct reqhdr req;
162 struct rephdr rep;
163 struct iphdr iphdr;
df4e01a5
DC
164 struct msghdr mhdr;
165 struct iovec iov;
166 char cmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
6796167f
DC
167 size_t hdrlen;
168 struct pollfd pfd[2];
dcb0aee6 169 time_t curtime, lasterr;
b39da562
DC
170
171 daemonize = 1;
172 ttl = 3600;
ac46a58c 173 while((c = getopt(argc, argv, "nht:d:")) != -1) {
b39da562
DC
174 switch(c) {
175 case 't':
176 ttl = atoi(optarg);
177 break;
ac46a58c
DC
178 case 'd':
179 setname(optarg);
180 break;
b39da562
DC
181 case 'n':
182 daemonize = 0;
183 break;
184 case 'h':
185 case '?':
186 case ':':
187 default:
188 fprintf(stderr, "usage: icmpdnd [-n]");
189 exit((c == 'h')?0:1);
190 }
191 }
ac46a58c
DC
192 if(*myname == 0)
193 setname(NULL);
b39da562 194
6796167f
DC
195 s4 = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
196 s6 = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMP);
197 if((s4 < 0) && (s6 < 0)) {
198 perror("could not open raw socket");
fa6ac2fc
DC
199 exit(1);
200 }
df4e01a5
DC
201 if(s6 >= 0) {
202 i = 1;
203 if(setsockopt(s6, IPPROTO_IPV6, IPV6_PKTINFO, &i, sizeof(i))) {
204 perror("could not set IPV6_PKTINFO sockopt");
205 exit(1);
206 }
207 }
fa6ac2fc 208
b39da562
DC
209 if(daemonize)
210 daemon(0, 0);
211
6796167f 212 openlog("icmpdnd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
b39da562 213
fa6ac2fc 214 alive = 1;
dcb0aee6 215 lasterr = 0;
fa6ac2fc 216 while(alive) {
6796167f
DC
217 n = 0;
218 if(s4 >= 0) {
219 pfd[n].fd = s4;
220 pfd[n].events = POLLIN;
221 n++;
222 }
223 if(s6 >= 0) {
224 pfd[n].fd = s6;
225 pfd[n].events = POLLIN;
226 n++;
227 }
228 ret = poll(pfd, n, -1);
229
dcb0aee6 230 curtime = time(NULL);
fa6ac2fc
DC
231 if(ret < 0) {
232 if(errno == EINTR)
233 continue;
6796167f 234 syslog(LOG_ERR, "error while polling sockets: %m");
dcb0aee6
DC
235 if(lasterr == curtime) {
236 syslog(LOG_CRIT, "exiting due to repeated errors");
237 exit(1);
238 }
239 lasterr = curtime;
fa6ac2fc 240 }
dcb0aee6 241
6796167f
DC
242 for(i = 0; i < n; i++) {
243 if((pfd[i].revents & POLLIN) == 0)
244 continue;
245 cs = pfd[i].fd;
6796167f 246 memset(&name, 0, sizeof(name));
df4e01a5
DC
247
248 iov.iov_len = sizeof(buf);
249 iov.iov_base = buf;
250 mhdr.msg_name = &name;
251 mhdr.msg_namelen = sizeof(name);
252 mhdr.msg_iov = &iov;
253 mhdr.msg_iovlen = 1;
254 mhdr.msg_control = cmsgbuf;
255 mhdr.msg_controllen = sizeof(cmsgbuf);
256
257 ret = recvmsg(cs, &mhdr, 0);
6796167f
DC
258 if(ret < 0) {
259 syslog(LOG_WARNING, "error while receiving datagram: %m");
260 continue;
261 }
262
263 if(cs == s4) {
264 if(ret < sizeof(iphdr) + sizeof(req))
265 continue;
266 hdrlen = sizeof(iphdr);
267 memcpy(&iphdr, buf, sizeof(iphdr));
268 if(iphdr.protocol != IPPROTO_ICMP)
269 continue;
df4e01a5
DC
270 mhdr.msg_control = NULL;
271 mhdr.msg_controllen = 0;
6796167f
DC
272 } else if(cs == s6) {
273 if(ret < sizeof(req))
274 continue;
275 ((struct sockaddr_in6 *)&name)->sin6_port = 0;
276 hdrlen = 0;
df4e01a5 277 /* Just keep mhdr.msg_control. */
6796167f
DC
278 } else {
279 syslog(LOG_CRIT, "strangeness!");
280 abort();
281 }
282 memcpy(&req, buf + hdrlen, sizeof(req));
283 if(req.type != ICMP_NAMEREQ)
284 continue;
285 rep.type = ICMP_NAMEREP;
286 rep.code = 0;
287 rep.id = req.id;
288 rep.seq = req.seq;
289 rep.ttl = htonl(ttl);
290 memcpy(buf, &rep, sizeof(rep));
291 datalen = filldn(buf + sizeof(rep));
b39da562 292
6796167f 293 cksum(buf, datalen + sizeof(rep));
df4e01a5
DC
294
295 iov.iov_len = sizeof(rep) + datalen;
296 iov.iov_base = buf;
297 mhdr.msg_iov = &iov;
298 mhdr.msg_iovlen = 1;
299 ret = sendmsg(cs, &mhdr, 0);
6796167f
DC
300 if(ret < 0)
301 syslog(LOG_WARNING, "error in sending reply: %m");
302 }
fa6ac2fc
DC
303 }
304
6796167f
DC
305 close(s4);
306 close(s6);
fa6ac2fc
DC
307 return(0);
308}
f82409dd
DC
309
310/*
311 * Local Variables:
ac46a58c 312 * compile-command: "make CFLAGS='-Wall -g'"
f82409dd
DC
313 * End:
314 */