2 * icmpdnd - ICMP Domain Name responder daemon for Linux
3 * Copyright (C) 2005 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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
26 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <netinet/ip.h>
31 #include <sys/types.h>
59 #define ICMP_NAMEREQ 37
60 #define ICMP_NAMEREP 38
64 size_t filldn(char *dst)
70 if(gethostname(namebuf, sizeof(namebuf)) < 0) {
71 perror("gethostname");
76 if(getdomainname(namebuf + hl, sizeof(namebuf) - hl) < 0) {
77 perror("getdomainname");
80 if(strlen(namebuf + hl) != 0) {
88 while((p2 = strchr(p, '.')) != NULL) {
91 memcpy(dp, p, p2 - p);
100 void cksum(void *hdr, size_t len)
107 ih = (struct icmphdr *)hdr;
108 cb = (u_int8_t *)hdr;
111 for(i = 0; i < (len & ~1); i += 2) {
130 cb = (u_int8_t *)&ih->checksum;
131 cb[0] = ~(u_int8_t)b1;
132 cb[1] = ~(u_int8_t)b2;
135 int main(int argc, char **argv)
138 int c, cs, s4, s6, datalen;
140 unsigned char buf[65536];
141 struct sockaddr_storage name;
147 char cmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
149 struct pollfd pfd[2];
150 time_t curtime, lasterr;
154 while((c = getopt(argc, argv, "nht:")) != -1) {
166 fprintf(stderr, "usage: icmpdnd [-n]");
167 exit((c == 'h')?0:1);
171 s4 = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
172 s6 = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMP);
173 if((s4 < 0) && (s6 < 0)) {
174 perror("could not open raw socket");
179 if(setsockopt(s6, IPPROTO_IPV6, IPV6_PKTINFO, &i, sizeof(i))) {
180 perror("could not set IPV6_PKTINFO sockopt");
188 openlog("icmpdnd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
196 pfd[n].events = POLLIN;
201 pfd[n].events = POLLIN;
204 ret = poll(pfd, n, -1);
206 curtime = time(NULL);
210 syslog(LOG_ERR, "error while polling sockets: %m");
211 if(lasterr == curtime) {
212 syslog(LOG_CRIT, "exiting due to repeated errors");
218 for(i = 0; i < n; i++) {
219 if((pfd[i].revents & POLLIN) == 0)
222 memset(&name, 0, sizeof(name));
224 iov.iov_len = sizeof(buf);
226 mhdr.msg_name = &name;
227 mhdr.msg_namelen = sizeof(name);
230 mhdr.msg_control = cmsgbuf;
231 mhdr.msg_controllen = sizeof(cmsgbuf);
233 ret = recvmsg(cs, &mhdr, 0);
235 syslog(LOG_WARNING, "error while receiving datagram: %m");
240 if(ret < sizeof(iphdr) + sizeof(req))
242 hdrlen = sizeof(iphdr);
243 memcpy(&iphdr, buf, sizeof(iphdr));
244 if(iphdr.protocol != IPPROTO_ICMP)
246 mhdr.msg_control = NULL;
247 mhdr.msg_controllen = 0;
248 } else if(cs == s6) {
249 if(ret < sizeof(req))
251 ((struct sockaddr_in6 *)&name)->sin6_port = 0;
253 /* Just keep mhdr.msg_control. */
255 syslog(LOG_CRIT, "strangeness!");
258 memcpy(&req, buf + hdrlen, sizeof(req));
259 if(req.type != ICMP_NAMEREQ)
261 rep.type = ICMP_NAMEREP;
265 rep.ttl = htonl(ttl);
266 memcpy(buf, &rep, sizeof(rep));
267 datalen = filldn(buf + sizeof(rep));
269 cksum(buf, datalen + sizeof(rep));
271 iov.iov_len = sizeof(rep) + datalen;
275 ret = sendmsg(cs, &mhdr, 0);
277 syslog(LOG_WARNING, "error in sending reply: %m");
288 * compile-command: "gcc -Wall -g -o icmpdnd icmpdnd.c"