Honor the `nocache' config option.
[icmp-dn.git] / 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
DC
26#include <sys/socket.h>
27#include <netinet/in.h>
28#include <arpa/inet.h>
29#include <netinet/ip.h>
30#include <sys/types.h>
31
32struct icmphdr {
33 u_int8_t type;
34 u_int8_t code;
35 u_int16_t checksum;
36};
37
38struct reqhdr {
39 u_int8_t type;
40 u_int8_t code;
41 u_int16_t checksum;
42 u_int16_t id;
43 u_int16_t seq;
44};
45
46struct rephdr {
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 int32_t ttl;
53};
54
55#define ICMP_NAMEREQ 37
56#define ICMP_NAMEREP 38
57
fa6ac2fc
DC
58volatile int alive;
59
60size_t filldn(char *dst)
61{
62 char *p, *p2, *dp;
63 char namebuf[1024];
64 int hl;
65
66 if(gethostname(namebuf, sizeof(namebuf)) < 0) {
67 perror("gethostname");
68 exit(1);
69 }
70 hl = strlen(namebuf);
71 namebuf[hl++] = '.';
72 if(getdomainname(namebuf + hl, sizeof(namebuf) - hl) < 0) {
73 perror("getdomainname");
74 exit(1);
75 }
76 if(strlen(namebuf + hl) != 0) {
77 hl = strlen(namebuf);
78 namebuf[hl++] = '.';
79 }
80 namebuf[hl] = 0;
81
82 p = namebuf;
83 dp = dst;
84 while((p2 = strchr(p, '.')) != NULL) {
85 *p2 = 0;
86 *(dp++) = p2 - p;
87 memcpy(dp, p, p2 - p);
88 dp += p2 - p;
89 p = p2 + 1;
90 }
91 *(dp++) = 0;
92
93 return(dp - dst);
94}
95
96void cksum(void *hdr, size_t len)
97{
98 struct icmphdr *ih;
99 u_int8_t *cb;
100 int i;
101 int b1, b2;
102
103 ih = (struct icmphdr *)hdr;
104 cb = (u_int8_t *)hdr;
105 ih->checksum = 0;
106 b1 = b2 = 0;
107 for(i = 0; i < (len & ~1); i += 2) {
108 b1 += cb[i];
109 b2 += cb[i + 1];
110 }
111 if(i & 1)
112 b1 += cb[len - 1];
113 while(1) {
114 if(b1 >= 256) {
115 b2 += b1 >> 8;
116 b1 &= 0xff;
117 continue;
118 }
119 if(b2 >= 256) {
120 b1 += b2 >> 8;
121 b2 &= 0xff;
122 continue;
123 }
124 break;
125 }
126 cb = (u_int8_t *)&ih->checksum;
127 cb[0] = ~(u_int8_t)b1;
128 cb[1] = ~(u_int8_t)b2;
129}
130
131int main(int argc, char **argv)
132{
133 int ret;
b39da562
DC
134 int c, s, namelen, datalen;
135 int daemonize, ttl;
fa6ac2fc
DC
136 unsigned char buf[65536];
137 struct sockaddr_in name;
138 struct reqhdr req;
139 struct rephdr rep;
140 struct iphdr iphdr;
dcb0aee6 141 time_t curtime, lasterr;
b39da562
DC
142
143 daemonize = 1;
144 ttl = 3600;
145 while((c = getopt(argc, argv, "nht:")) != -1) {
146 switch(c) {
147 case 't':
148 ttl = atoi(optarg);
149 break;
150 case 'n':
151 daemonize = 0;
152 break;
153 case 'h':
154 case '?':
155 case ':':
156 default:
157 fprintf(stderr, "usage: icmpdnd [-n]");
158 exit((c == 'h')?0:1);
159 }
160 }
161
fa6ac2fc
DC
162 if((s = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
163 perror("could not create raw socket");
164 exit(1);
165 }
166
b39da562
DC
167 if(daemonize)
168 daemon(0, 0);
169
170 openlog("icmpdnd", LOG_PID, LOG_DAEMON);
171
fa6ac2fc 172 alive = 1;
dcb0aee6 173 lasterr = 0;
fa6ac2fc
DC
174 while(alive) {
175 namelen = sizeof(name);
176 ret = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&name, &namelen);
dcb0aee6
DC
177
178 curtime = time(NULL);
fa6ac2fc
DC
179 if(ret < 0) {
180 if(errno == EINTR)
181 continue;
b39da562 182 syslog(LOG_ERR, "error in receiving datagram: %m");
dcb0aee6
DC
183 if(lasterr == curtime) {
184 syslog(LOG_CRIT, "exiting due to repeated errors");
185 exit(1);
186 }
187 lasterr = curtime;
fa6ac2fc 188 }
dcb0aee6 189
b39da562 190 if(ret < sizeof(iphdr) + sizeof(req))
fa6ac2fc
DC
191 continue;
192 memcpy(&iphdr, buf, sizeof(iphdr));
b39da562 193 memcpy(&req, buf + sizeof(iphdr), sizeof(req));
fa6ac2fc
DC
194 if(iphdr.protocol != IPPROTO_ICMP)
195 continue;
fa6ac2fc
DC
196 if(req.type != ICMP_NAMEREQ)
197 continue;
198 rep.type = ICMP_NAMEREP;
199 rep.code = 0;
200 rep.id = req.id;
201 rep.seq = req.seq;
b39da562 202 rep.ttl = htonl(ttl);
fa6ac2fc
DC
203 memcpy(buf, &rep, sizeof(rep));
204 datalen = filldn(buf + sizeof(rep));
b39da562 205
fa6ac2fc 206 cksum(buf, datalen + sizeof(rep));
b39da562
DC
207
208 /* XXX: The correct source address needs to be filled in from
209 * the request's destination address. */
fa6ac2fc 210 ret = sendto(s, buf, datalen + sizeof(rep), 0, (struct sockaddr *)&name, namelen);
1ac6ec0c 211 if(ret < 0)
b39da562 212 syslog(LOG_WARNING, "error in sending reply: %m");
fa6ac2fc
DC
213 }
214
215 close(s);
216 return(0);
217}