49d1b5c3d1d05a90ff4464486bcbcecb4342657d
[icmp-dn.git] / src / icmpdnd.c
1 /*
2  *  icmpdnd - ICMP Domain Name responder daemon for Linux
3  *  Copyright (C) 2005 Fredrik Tolf <fredrik@dolda2000.com>
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>
24 #include <syslog.h>
25 #include <time.h>
26 #include <sys/socket.h>
27 #include <sys/poll.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <netinet/ip.h>
31 #include <sys/types.h>
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #ifndef MAXHNAME
37 #define MAXHNAME 1024
38 #endif
39
40 struct icmphdr {
41     u_int8_t type;
42     u_int8_t code;
43     u_int16_t checksum;
44 };
45
46 struct 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
54 struct 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
66 volatile int alive;
67 char myname[MAXHNAME] = "";
68
69 void setname(char *newname)
70 {
71     int nl;
72     
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         }
96     }
97 }
98
99 size_t filldn(char *dst)
100 {
101     char *p, *p2, *dp;
102     char namebuf[MAXHNAME];
103     
104     strcpy(namebuf, myname);
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
119 void 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
154 int main(int argc, char **argv)
155 {
156     int i, n, ret;
157     int c, cs, s4, s6, datalen;
158     int daemonize, ttl;
159     unsigned char buf[65536];
160     struct sockaddr_storage name;
161     struct reqhdr req;
162     struct rephdr rep;
163     struct iphdr iphdr;
164     struct msghdr mhdr;
165     struct iovec iov;
166     char cmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
167     size_t hdrlen;
168     struct pollfd pfd[2];
169     time_t curtime, lasterr;
170     
171     daemonize = 1;
172     ttl = 3600;
173     while((c = getopt(argc, argv, "nht:d:")) != -1) {
174         switch(c) {
175         case 't':
176             ttl = atoi(optarg);
177             break;
178         case 'd':
179             setname(optarg);
180             break;
181         case 'n':
182             daemonize = 0;
183             break;
184         case 'h':
185         case '?':
186         case ':':
187         default:
188             fprintf(stderr, "usage: icmpdnd [-nh] [-t ttl] [-d domainname]\n");
189             exit((c == 'h')?0:1);
190         }
191     }
192     if(*myname == 0)
193         setname(NULL);
194     
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");
199         exit(1);
200     }
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     }
208     
209     if(daemonize)
210         daemon(0, 0);
211     
212     openlog("icmpdnd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
213     
214     alive = 1;
215     lasterr = 0;
216     while(alive) {
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
230         curtime = time(NULL);
231         if(ret < 0) {
232             if(errno == EINTR)
233                 continue;
234             syslog(LOG_ERR, "error while polling sockets: %m");
235             if(lasterr == curtime) {
236                 syslog(LOG_CRIT, "exiting due to repeated errors");
237                 exit(1);
238             }
239             lasterr = curtime;
240         }
241         
242         for(i = 0; i < n; i++) {
243             if((pfd[i].revents & POLLIN) == 0)
244                 continue;
245             cs = pfd[i].fd;
246             memset(&name, 0, sizeof(name));
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);
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;
270                 mhdr.msg_control = NULL;
271                 mhdr.msg_controllen = 0;
272             } else if(cs == s6) {
273                 if(ret < sizeof(req))
274                     continue;
275                 ((struct sockaddr_in6 *)&name)->sin6_port = 0;
276                 hdrlen = 0;
277                 /* Just keep mhdr.msg_control. */
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));
292         
293             cksum(buf, datalen + sizeof(rep));
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);
300             if(ret < 0)
301                 syslog(LOG_WARNING, "error in sending reply: %m");
302         }
303     }
304     
305     close(s4);
306     close(s6);
307     return(0);
308 }
309
310 /*
311  * Local Variables:
312  * compile-command: "make CFLAGS='-Wall -g'"
313  * End:
314  */