Updated cksum.
[icmp-dn.git] / icmpdnd.c
CommitLineData
fa6ac2fc
DC
1/*
2 * icmpdnd - ICMP Domain Name responder daemon for Linux
3 * Copyright (C) 2004 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 <sys/socket.h>
25#include <netinet/in.h>
26#include <arpa/inet.h>
27#include <netinet/ip.h>
28#include <sys/types.h>
29
30struct icmphdr {
31 u_int8_t type;
32 u_int8_t code;
33 u_int16_t checksum;
34};
35
36struct reqhdr {
37 u_int8_t type;
38 u_int8_t code;
39 u_int16_t checksum;
40 u_int16_t id;
41 u_int16_t seq;
42};
43
44struct rephdr {
45 u_int8_t type;
46 u_int8_t code;
47 u_int16_t checksum;
48 u_int16_t id;
49 u_int16_t seq;
50 int32_t ttl;
51};
52
53#define ICMP_NAMEREQ 37
54#define ICMP_NAMEREP 38
55
56#define TTL 3600
57
58volatile int alive;
59
60size_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
96void 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
131int main(int argc, char **argv)
132{
133 int ret;
134 int s, namelen, datalen;
135 unsigned char buf[65536];
136 struct sockaddr_in name;
137 struct reqhdr req;
138 struct rephdr rep;
139 struct iphdr iphdr;
140
141 if((s = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
142 perror("could not create raw socket");
143 exit(1);
144 }
145
146 alive = 1;
147 while(alive) {
148 namelen = sizeof(name);
149 ret = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&name, &namelen);
150 if(ret < 0) {
151 if(errno == EINTR)
152 continue;
153 perror("recvfrom");
154 exit(1);
155 }
156 if(ret < sizeof(iphdr))
157 continue;
158 memcpy(&iphdr, buf, sizeof(iphdr));
159 if(iphdr.protocol != IPPROTO_ICMP)
160 continue;
161 if(ret < sizeof(iphdr) + sizeof(req))
162 continue;
163 memcpy(&req, buf + sizeof(iphdr), sizeof(req));
164 if(req.type != ICMP_NAMEREQ)
165 continue;
166 rep.type = ICMP_NAMEREP;
167 rep.code = 0;
168 rep.id = req.id;
169 rep.seq = req.seq;
170 rep.ttl = htonl(TTL);
171 memcpy(buf, &rep, sizeof(rep));
172 datalen = filldn(buf + sizeof(rep));
173
174 cksum(buf, datalen + sizeof(rep));
175
176 ret = sendto(s, buf, datalen + sizeof(rep), 0, (struct sockaddr *)&name, namelen);
177 if(ret < 0) {
178 perror("sendto");
179 exit(1);
180 }
181 }
182
183 close(s);
184 return(0);
185}