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>
63 #define ICMP_NAMEREQ 37
64 #define ICMP_NAMEREP 38
67 char myname[MAXHNAME] = "";
69 void setname(char *newname)
74 if(gethostname(myname, sizeof(myname)) < 0) {
75 perror("gethostname");
80 if(getdomainname(myname + nl, sizeof(myname) - nl) < 0) {
81 perror("getdomainname");
84 if(strlen(myname + nl) != 0) {
90 strcpy(myname, newname);
92 if(myname[nl - 1] != '.') {
99 size_t filldn(char *dst)
102 char namebuf[MAXHNAME];
104 strcpy(namebuf, myname);
107 while((p2 = strchr(p, '.')) != NULL) {
110 memcpy(dp, p, p2 - p);
119 void cksum(void *hdr, size_t len)
126 ih = (struct icmphdr *)hdr;
127 cb = (u_int8_t *)hdr;
130 for(i = 0; i < (len & ~1); i += 2) {
149 cb = (u_int8_t *)&ih->checksum;
150 cb[0] = ~(u_int8_t)b1;
151 cb[1] = ~(u_int8_t)b2;
154 int main(int argc, char **argv)
157 int c, cs, s4, s6, datalen;
159 unsigned char buf[65536];
160 struct sockaddr_storage name;
166 char cmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
168 struct pollfd pfd[2];
169 time_t curtime, lasterr;
173 while((c = getopt(argc, argv, "nht:d:")) != -1) {
188 fprintf(stderr, "usage: icmpdnd [-nh] [-t ttl] [-d domainname]\n");
189 exit((c == 'h')?0:1);
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");
203 if(setsockopt(s6, IPPROTO_IPV6, IPV6_PKTINFO, &i, sizeof(i))) {
204 perror("could not set IPV6_PKTINFO sockopt");
212 openlog("icmpdnd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
220 pfd[n].events = POLLIN;
225 pfd[n].events = POLLIN;
228 ret = poll(pfd, n, -1);
230 curtime = time(NULL);
234 syslog(LOG_ERR, "error while polling sockets: %m");
235 if(lasterr == curtime) {
236 syslog(LOG_CRIT, "exiting due to repeated errors");
242 for(i = 0; i < n; i++) {
243 if((pfd[i].revents & POLLIN) == 0)
246 memset(&name, 0, sizeof(name));
248 iov.iov_len = sizeof(buf);
250 mhdr.msg_name = &name;
251 mhdr.msg_namelen = sizeof(name);
254 mhdr.msg_control = cmsgbuf;
255 mhdr.msg_controllen = sizeof(cmsgbuf);
257 ret = recvmsg(cs, &mhdr, 0);
259 syslog(LOG_WARNING, "error while receiving datagram: %m");
264 if(ret < sizeof(iphdr) + sizeof(req))
266 hdrlen = sizeof(iphdr);
267 memcpy(&iphdr, buf, sizeof(iphdr));
268 if(iphdr.protocol != IPPROTO_ICMP)
270 mhdr.msg_control = NULL;
271 mhdr.msg_controllen = 0;
272 } else if(cs == s6) {
273 if(ret < sizeof(req))
275 ((struct sockaddr_in6 *)&name)->sin6_port = 0;
277 /* Just keep mhdr.msg_control. */
279 syslog(LOG_CRIT, "strangeness!");
282 memcpy(&req, buf + hdrlen, sizeof(req));
283 if(req.type != ICMP_NAMEREQ)
285 rep.type = ICMP_NAMEREP;
289 rep.ttl = htonl(ttl);
290 memcpy(buf, &rep, sizeof(rep));
291 datalen = filldn(buf + sizeof(rep));
293 cksum(buf, datalen + sizeof(rep));
295 iov.iov_len = sizeof(rep) + datalen;
299 ret = sendmsg(cs, &mhdr, 0);
301 syslog(LOG_WARNING, "error in sending reply: %m");
312 * compile-command: "make CFLAGS='-Wall -g'"