Commit | Line | Data |
---|---|---|
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 | 26 | #include <sys/socket.h> |
6796167f | 27 | #include <sys/poll.h> |
fa6ac2fc DC |
28 | #include <netinet/in.h> |
29 | #include <arpa/inet.h> | |
30 | #include <netinet/ip.h> | |
31 | #include <sys/types.h> | |
606a39c9 | 32 | #ifdef HAVE_CONFIG_H |
2269406c | 33 | #include "config.h" |
606a39c9 | 34 | #endif |
fa6ac2fc | 35 | |
1eb230f9 | 36 | #include "icmpdefs.h" |
37 | ||
ac46a58c DC |
38 | #ifndef MAXHNAME |
39 | #define MAXHNAME 1024 | |
40 | #endif | |
41 | ||
fa6ac2fc | 42 | volatile int alive; |
ac46a58c | 43 | char myname[MAXHNAME] = ""; |
fa6ac2fc | 44 | |
ac46a58c | 45 | void setname(char *newname) |
fa6ac2fc | 46 | { |
ac46a58c | 47 | int nl; |
fa6ac2fc | 48 | |
ac46a58c DC |
49 | if(newname == NULL) { |
50 | if(gethostname(myname, sizeof(myname)) < 0) { | |
51 | perror("gethostname"); | |
52 | exit(1); | |
53 | } | |
54 | nl = strlen(myname); | |
55 | myname[nl++] = '.'; | |
56 | if(getdomainname(myname + nl, sizeof(myname) - nl) < 0) { | |
57 | perror("getdomainname"); | |
58 | exit(1); | |
59 | } | |
60 | if(strlen(myname + nl) != 0) { | |
61 | nl = strlen(myname); | |
62 | myname[nl++] = '.'; | |
63 | } | |
64 | myname[nl] = 0; | |
65 | } else { | |
66 | strcpy(myname, newname); | |
67 | nl = strlen(myname); | |
68 | if(myname[nl - 1] != '.') { | |
69 | myname[nl] = '.'; | |
70 | myname[nl + 1] = 0; | |
71 | } | |
fa6ac2fc | 72 | } |
ac46a58c DC |
73 | } |
74 | ||
75 | size_t filldn(char *dst) | |
76 | { | |
77 | char *p, *p2, *dp; | |
78 | char namebuf[MAXHNAME]; | |
fa6ac2fc | 79 | |
ac46a58c | 80 | strcpy(namebuf, myname); |
fa6ac2fc DC |
81 | p = namebuf; |
82 | dp = dst; | |
83 | while((p2 = strchr(p, '.')) != NULL) { | |
84 | *p2 = 0; | |
85 | *(dp++) = p2 - p; | |
86 | memcpy(dp, p, p2 - p); | |
87 | dp += p2 - p; | |
88 | p = p2 + 1; | |
89 | } | |
90 | *(dp++) = 0; | |
91 | ||
92 | return(dp - dst); | |
93 | } | |
94 | ||
95 | void cksum(void *hdr, size_t len) | |
96 | { | |
97 | struct icmphdr *ih; | |
98 | u_int8_t *cb; | |
99 | int i; | |
100 | int b1, b2; | |
101 | ||
102 | ih = (struct icmphdr *)hdr; | |
103 | cb = (u_int8_t *)hdr; | |
104 | ih->checksum = 0; | |
105 | b1 = b2 = 0; | |
106 | for(i = 0; i < (len & ~1); i += 2) { | |
107 | b1 += cb[i]; | |
108 | b2 += cb[i + 1]; | |
109 | } | |
110 | if(i & 1) | |
111 | b1 += cb[len - 1]; | |
112 | while(1) { | |
113 | if(b1 >= 256) { | |
114 | b2 += b1 >> 8; | |
115 | b1 &= 0xff; | |
116 | continue; | |
117 | } | |
118 | if(b2 >= 256) { | |
119 | b1 += b2 >> 8; | |
120 | b2 &= 0xff; | |
121 | continue; | |
122 | } | |
123 | break; | |
124 | } | |
125 | cb = (u_int8_t *)&ih->checksum; | |
126 | cb[0] = ~(u_int8_t)b1; | |
127 | cb[1] = ~(u_int8_t)b2; | |
128 | } | |
129 | ||
130 | int main(int argc, char **argv) | |
131 | { | |
6796167f | 132 | int i, n, ret; |
df4e01a5 | 133 | int c, cs, s4, s6, datalen; |
b39da562 | 134 | int daemonize, ttl; |
fa6ac2fc | 135 | unsigned char buf[65536]; |
6796167f | 136 | struct sockaddr_storage name; |
fa6ac2fc DC |
137 | struct reqhdr req; |
138 | struct rephdr rep; | |
139 | struct iphdr iphdr; | |
df4e01a5 DC |
140 | struct msghdr mhdr; |
141 | struct iovec iov; | |
142 | char cmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; | |
6796167f DC |
143 | size_t hdrlen; |
144 | struct pollfd pfd[2]; | |
dcb0aee6 | 145 | time_t curtime, lasterr; |
b39da562 DC |
146 | |
147 | daemonize = 1; | |
148 | ttl = 3600; | |
ac46a58c | 149 | while((c = getopt(argc, argv, "nht:d:")) != -1) { |
b39da562 DC |
150 | switch(c) { |
151 | case 't': | |
152 | ttl = atoi(optarg); | |
153 | break; | |
ac46a58c DC |
154 | case 'd': |
155 | setname(optarg); | |
156 | break; | |
b39da562 DC |
157 | case 'n': |
158 | daemonize = 0; | |
159 | break; | |
160 | case 'h': | |
161 | case '?': | |
162 | case ':': | |
163 | default: | |
4ca7c46f | 164 | fprintf(stderr, "usage: icmpdnd [-nh] [-t ttl] [-d domainname]\n"); |
b39da562 DC |
165 | exit((c == 'h')?0:1); |
166 | } | |
167 | } | |
ac46a58c DC |
168 | if(*myname == 0) |
169 | setname(NULL); | |
b39da562 | 170 | |
6796167f DC |
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"); | |
fa6ac2fc DC |
175 | exit(1); |
176 | } | |
df4e01a5 DC |
177 | if(s6 >= 0) { |
178 | i = 1; | |
179 | if(setsockopt(s6, IPPROTO_IPV6, IPV6_PKTINFO, &i, sizeof(i))) { | |
180 | perror("could not set IPV6_PKTINFO sockopt"); | |
181 | exit(1); | |
182 | } | |
183 | } | |
fa6ac2fc | 184 | |
b39da562 DC |
185 | if(daemonize) |
186 | daemon(0, 0); | |
187 | ||
6796167f | 188 | openlog("icmpdnd", LOG_PID | LOG_NDELAY, LOG_DAEMON); |
b39da562 | 189 | |
fa6ac2fc | 190 | alive = 1; |
dcb0aee6 | 191 | lasterr = 0; |
fa6ac2fc | 192 | while(alive) { |
6796167f DC |
193 | n = 0; |
194 | if(s4 >= 0) { | |
195 | pfd[n].fd = s4; | |
196 | pfd[n].events = POLLIN; | |
197 | n++; | |
198 | } | |
199 | if(s6 >= 0) { | |
200 | pfd[n].fd = s6; | |
201 | pfd[n].events = POLLIN; | |
202 | n++; | |
203 | } | |
204 | ret = poll(pfd, n, -1); | |
205 | ||
dcb0aee6 | 206 | curtime = time(NULL); |
fa6ac2fc DC |
207 | if(ret < 0) { |
208 | if(errno == EINTR) | |
209 | continue; | |
6796167f | 210 | syslog(LOG_ERR, "error while polling sockets: %m"); |
dcb0aee6 DC |
211 | if(lasterr == curtime) { |
212 | syslog(LOG_CRIT, "exiting due to repeated errors"); | |
213 | exit(1); | |
214 | } | |
215 | lasterr = curtime; | |
fa6ac2fc | 216 | } |
dcb0aee6 | 217 | |
6796167f DC |
218 | for(i = 0; i < n; i++) { |
219 | if((pfd[i].revents & POLLIN) == 0) | |
220 | continue; | |
221 | cs = pfd[i].fd; | |
6796167f | 222 | memset(&name, 0, sizeof(name)); |
df4e01a5 DC |
223 | |
224 | iov.iov_len = sizeof(buf); | |
225 | iov.iov_base = buf; | |
226 | mhdr.msg_name = &name; | |
227 | mhdr.msg_namelen = sizeof(name); | |
228 | mhdr.msg_iov = &iov; | |
229 | mhdr.msg_iovlen = 1; | |
230 | mhdr.msg_control = cmsgbuf; | |
231 | mhdr.msg_controllen = sizeof(cmsgbuf); | |
232 | ||
233 | ret = recvmsg(cs, &mhdr, 0); | |
6796167f DC |
234 | if(ret < 0) { |
235 | syslog(LOG_WARNING, "error while receiving datagram: %m"); | |
236 | continue; | |
237 | } | |
238 | ||
239 | if(cs == s4) { | |
240 | if(ret < sizeof(iphdr) + sizeof(req)) | |
241 | continue; | |
242 | hdrlen = sizeof(iphdr); | |
243 | memcpy(&iphdr, buf, sizeof(iphdr)); | |
244 | if(iphdr.protocol != IPPROTO_ICMP) | |
245 | continue; | |
df4e01a5 DC |
246 | mhdr.msg_control = NULL; |
247 | mhdr.msg_controllen = 0; | |
6796167f DC |
248 | } else if(cs == s6) { |
249 | if(ret < sizeof(req)) | |
250 | continue; | |
251 | ((struct sockaddr_in6 *)&name)->sin6_port = 0; | |
252 | hdrlen = 0; | |
df4e01a5 | 253 | /* Just keep mhdr.msg_control. */ |
6796167f DC |
254 | } else { |
255 | syslog(LOG_CRIT, "strangeness!"); | |
256 | abort(); | |
257 | } | |
258 | memcpy(&req, buf + hdrlen, sizeof(req)); | |
259 | if(req.type != ICMP_NAMEREQ) | |
260 | continue; | |
261 | rep.type = ICMP_NAMEREP; | |
262 | rep.code = 0; | |
263 | rep.id = req.id; | |
264 | rep.seq = req.seq; | |
265 | rep.ttl = htonl(ttl); | |
266 | memcpy(buf, &rep, sizeof(rep)); | |
267 | datalen = filldn(buf + sizeof(rep)); | |
b39da562 | 268 | |
6796167f | 269 | cksum(buf, datalen + sizeof(rep)); |
df4e01a5 DC |
270 | |
271 | iov.iov_len = sizeof(rep) + datalen; | |
272 | iov.iov_base = buf; | |
273 | mhdr.msg_iov = &iov; | |
274 | mhdr.msg_iovlen = 1; | |
275 | ret = sendmsg(cs, &mhdr, 0); | |
6796167f DC |
276 | if(ret < 0) |
277 | syslog(LOG_WARNING, "error in sending reply: %m"); | |
278 | } | |
fa6ac2fc DC |
279 | } |
280 | ||
6796167f DC |
281 | close(s4); |
282 | close(s6); | |
fa6ac2fc DC |
283 | return(0); |
284 | } | |
f82409dd DC |
285 | |
286 | /* | |
287 | * Local Variables: | |
ac46a58c | 288 | * compile-command: "make CFLAGS='-Wall -g'" |
f82409dd DC |
289 | * End: |
290 | */ |