Added config file.
[icmp-dn.git] / 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 <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <netinet/ip.h>
30 #include <sys/types.h>
31
32 struct icmphdr {
33     u_int8_t type;
34     u_int8_t code;
35     u_int16_t checksum;
36 };
37
38 struct 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
46 struct 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
58 volatile int alive;
59
60 size_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
96 void 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
131 int main(int argc, char **argv)
132 {
133     int ret;
134     int c, s, namelen, datalen;
135     int daemonize, ttl;
136     unsigned char buf[65536];
137     struct sockaddr_in name;
138     struct reqhdr req;
139     struct rephdr rep;
140     struct iphdr iphdr;
141     time_t curtime, lasterr;
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     
162     if((s = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
163         perror("could not create raw socket");
164         exit(1);
165     }
166     
167     if(daemonize)
168         daemon(0, 0);
169     
170     openlog("icmpdnd", LOG_PID, LOG_DAEMON);
171     
172     alive = 1;
173     lasterr = 0;
174     while(alive) {
175         namelen = sizeof(name);
176         ret = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&name, &namelen);
177         
178         curtime = time(NULL);
179         if(ret < 0) {
180             if(errno == EINTR)
181                 continue;
182             syslog(LOG_ERR, "error in receiving datagram: %m");
183             if(lasterr == curtime) {
184                 syslog(LOG_CRIT, "exiting due to repeated errors");
185                 exit(1);
186             }
187             lasterr = curtime;
188         }
189         
190         if(ret < sizeof(iphdr) + sizeof(req))
191             continue;
192         memcpy(&iphdr, buf, sizeof(iphdr));
193         memcpy(&req, buf + sizeof(iphdr), sizeof(req));
194         if(iphdr.protocol != IPPROTO_ICMP)
195             continue;
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;
202         rep.ttl = htonl(ttl);
203         memcpy(buf, &rep, sizeof(rep));
204         datalen = filldn(buf + sizeof(rep));
205         
206         cksum(buf, datalen + sizeof(rep));
207         
208         /* XXX: The correct source address needs to be filled in from
209          * the request's destination address. */
210         ret = sendto(s, buf, datalen + sizeof(rep), 0, (struct sockaddr *)&name, namelen);
211         if(ret < 0)
212             syslog(LOG_WARNING, "error in sending reply: %m");
213     }
214     
215     close(s);
216     return(0);
217 }