]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/iputils.cc
Merge pull request #6505 from rgacogne/rec-multi-index
[thirdparty/pdns.git] / pdns / iputils.cc
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "iputils.hh"
26 #include <sys/socket.h>
27
28 /** these functions provide a very lightweight wrapper to the Berkeley sockets API. Errors -> exceptions! */
29
30 static void RuntimeError(const boost::format& fmt)
31 {
32 throw runtime_error(fmt.str());
33 }
34
35 static void NetworkErr(const boost::format& fmt)
36 {
37 throw NetworkError(fmt.str());
38 }
39
40 int SSocket(int family, int type, int flags)
41 {
42 int ret = socket(family, type, flags);
43 if(ret < 0)
44 RuntimeError(boost::format("creating socket of type %d: %s") % family % strerror(errno));
45 return ret;
46 }
47
48 int SConnect(int sockfd, const ComboAddress& remote)
49 {
50 int ret = connect(sockfd, (struct sockaddr*)&remote, remote.getSocklen());
51 if(ret < 0) {
52 int savederrno = errno;
53 RuntimeError(boost::format("connecting socket to %s: %s") % remote.toStringWithPort() % strerror(savederrno));
54 }
55 return ret;
56 }
57
58 int SConnectWithTimeout(int sockfd, const ComboAddress& remote, int timeout)
59 {
60 int ret = connect(sockfd, (struct sockaddr*)&remote, remote.getSocklen());
61 if(ret < 0) {
62 int savederrno = errno;
63 if (savederrno == EINPROGRESS) {
64 if (timeout <= 0) {
65 return ret;
66 }
67
68 /* we wait until the connection has been established */
69 bool error = false;
70 bool disconnected = false;
71 int res = waitForRWData(sockfd, false, timeout, 0, &error, &disconnected);
72 if (res == 1) {
73 if (error) {
74 savederrno = 0;
75 socklen_t errlen = sizeof(savederrno);
76 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&savederrno, &errlen) == 0) {
77 NetworkErr(boost::format("connecting to %s failed: %s") % remote.toStringWithPort() % string(strerror(savederrno)));
78 }
79 else {
80 NetworkErr(boost::format("connecting to %s failed") % remote.toStringWithPort());
81 }
82 }
83 if (disconnected) {
84 NetworkErr(boost::format("%s closed the connection") % remote.toStringWithPort());
85 }
86 return 0;
87 }
88 else if (res == 0) {
89 NetworkErr(boost::format("timeout while connecting to %s") % remote.toStringWithPort());
90 } else if (res < 0) {
91 savederrno = errno;
92 NetworkErr(boost::format("waiting to connect to %s: %s") % remote.toStringWithPort() % string(strerror(savederrno)));
93 }
94 }
95 else {
96 NetworkErr(boost::format("connecting to %s: %s") % remote.toStringWithPort() % string(strerror(savederrno)));
97 }
98 }
99
100 return ret;
101 }
102
103 int SBind(int sockfd, const ComboAddress& local)
104 {
105 int ret = bind(sockfd, (struct sockaddr*)&local, local.getSocklen());
106 if(ret < 0) {
107 int savederrno = errno;
108 RuntimeError(boost::format("binding socket to %s: %s") % local.toStringWithPort() % strerror(savederrno));
109 }
110 return ret;
111 }
112
113 int SAccept(int sockfd, ComboAddress& remote)
114 {
115 socklen_t remlen = remote.getSocklen();
116
117 int ret = accept(sockfd, (struct sockaddr*)&remote, &remlen);
118 if(ret < 0)
119 RuntimeError(boost::format("accepting new connection on socket: %s") % strerror(errno));
120 return ret;
121 }
122
123 int SListen(int sockfd, int limit)
124 {
125 int ret = listen(sockfd, limit);
126 if(ret < 0)
127 RuntimeError(boost::format("setting socket to listen: %s") % strerror(errno));
128 return ret;
129 }
130
131 int SSetsockopt(int sockfd, int level, int opname, int value)
132 {
133 int ret = setsockopt(sockfd, level, opname, &value, sizeof(value));
134 if(ret < 0)
135 RuntimeError(boost::format("setsockopt for level %d and opname %d to %d failed: %s") % level % opname % value % strerror(errno));
136 return ret;
137 }
138
139
140 bool HarvestTimestamp(struct msghdr* msgh, struct timeval* tv)
141 {
142 #ifdef SO_TIMESTAMP
143 struct cmsghdr *cmsg;
144 for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(msgh,cmsg)) {
145 if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMP || cmsg->cmsg_type == SCM_TIMESTAMP) &&
146 CMSG_LEN(sizeof(*tv)) == cmsg->cmsg_len) {
147 memcpy(tv, CMSG_DATA(cmsg), sizeof(*tv));
148 return true;
149 }
150 }
151 #endif
152 return false;
153 }
154 bool HarvestDestinationAddress(const struct msghdr* msgh, ComboAddress* destination)
155 {
156 destination->reset();
157 #ifdef __NetBSD__
158 struct cmsghdr* cmsg;
159 #else
160 const struct cmsghdr* cmsg;
161 #endif
162 for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(const_cast<struct msghdr*>(msgh), const_cast<struct cmsghdr*>(cmsg))) {
163 #if defined(IP_PKTINFO)
164 if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_PKTINFO)) {
165 struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
166 destination->sin4.sin_addr = i->ipi_addr;
167 destination->sin4.sin_family = AF_INET;
168 return true;
169 }
170 #elif defined(IP_RECVDSTADDR)
171 if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_RECVDSTADDR)) {
172 struct in_addr *i = (struct in_addr *) CMSG_DATA(cmsg);
173 destination->sin4.sin_addr = *i;
174 destination->sin4.sin_family = AF_INET;
175 return true;
176 }
177 #endif
178
179 if ((cmsg->cmsg_level == IPPROTO_IPV6) && (cmsg->cmsg_type == IPV6_PKTINFO)) {
180 struct in6_pktinfo *i = (struct in6_pktinfo *) CMSG_DATA(cmsg);
181 destination->sin6.sin6_addr = i->ipi6_addr;
182 destination->sin4.sin_family = AF_INET6;
183 return true;
184 }
185 }
186 return false;
187 }
188
189 bool IsAnyAddress(const ComboAddress& addr)
190 {
191 if(addr.sin4.sin_family == AF_INET)
192 return addr.sin4.sin_addr.s_addr == 0;
193 else if(addr.sin4.sin_family == AF_INET6)
194 return !memcmp(&addr.sin6.sin6_addr, &in6addr_any, sizeof(addr.sin6.sin6_addr));
195
196 return false;
197 }
198
199 ssize_t sendfromto(int sock, const char* data, size_t len, int flags, const ComboAddress& from, const ComboAddress& to)
200 {
201 struct msghdr msgh;
202 struct iovec iov;
203 char cbuf[256];
204
205 /* Set up iov and msgh structures. */
206 memset(&msgh, 0, sizeof(struct msghdr));
207 iov.iov_base = (void*)data;
208 iov.iov_len = len;
209 msgh.msg_iov = &iov;
210 msgh.msg_iovlen = 1;
211 msgh.msg_name = (struct sockaddr*)&to;
212 msgh.msg_namelen = to.getSocklen();
213
214 if(from.sin4.sin_family) {
215 addCMsgSrcAddr(&msgh, cbuf, &from, 0);
216 }
217 else {
218 msgh.msg_control=NULL;
219 }
220 return sendmsg(sock, &msgh, flags);
221 }
222
223 // be careful: when using this for receive purposes, make sure addr->sin4.sin_family is set appropriately so getSocklen works!
224 // be careful: when using this function for *send* purposes, be sure to set cbufsize to 0!
225 // be careful: if you don't call addCMsgSrcAddr after fillMSGHdr, make sure to set msg_control to NULL
226 void fillMSGHdr(struct msghdr* msgh, struct iovec* iov, char* cbuf, size_t cbufsize, char* data, size_t datalen, ComboAddress* addr)
227 {
228 iov->iov_base = data;
229 iov->iov_len = datalen;
230
231 memset(msgh, 0, sizeof(struct msghdr));
232
233 msgh->msg_control = cbuf;
234 msgh->msg_controllen = cbufsize;
235 msgh->msg_name = addr;
236 msgh->msg_namelen = addr->getSocklen();
237 msgh->msg_iov = iov;
238 msgh->msg_iovlen = 1;
239 msgh->msg_flags = 0;
240 }
241
242 // warning: various parts of PowerDNS assume 'truncate' will never throw
243 void ComboAddress::truncate(unsigned int bits) noexcept
244 {
245 uint8_t* start;
246 int len=4;
247 if(sin4.sin_family==AF_INET) {
248 if(bits >= 32)
249 return;
250 start = (uint8_t*)&sin4.sin_addr.s_addr;
251 len=4;
252 }
253 else {
254 if(bits >= 128)
255 return;
256 start = (uint8_t*)&sin6.sin6_addr.s6_addr;
257 len=16;
258 }
259
260 auto tozero= len*8 - bits; // if set to 22, this will clear 1 byte, as it should
261
262 memset(start + len - tozero/8, 0, tozero/8); // blot out the whole bytes on the right
263
264 auto bitsleft=tozero % 8; // 2 bits left to clear
265
266 // a b c d, to truncate to 22 bits, we just zeroed 'd' and need to zero 2 bits from c
267 // so and by '11111100', which is ~((1<<2)-1) = ~3
268 uint8_t* place = start + len - 1 - tozero/8;
269 *place &= (~((1<<bitsleft)-1));
270 }
271
272 ssize_t sendMsgWithTimeout(int fd, const char* buffer, size_t len, int timeout, ComboAddress& dest, const ComboAddress& local, unsigned int localItf)
273 {
274 struct msghdr msgh;
275 struct iovec iov;
276 char cbuf[256];
277 bool firstTry = true;
278 fillMSGHdr(&msgh, &iov, cbuf, sizeof(cbuf), const_cast<char*>(buffer), len, &dest);
279 addCMsgSrcAddr(&msgh, cbuf, &local, localItf);
280
281 do {
282 ssize_t written = sendmsg(fd, &msgh, 0);
283
284 if (written > 0)
285 return written;
286
287 if (errno == EAGAIN) {
288 if (firstTry) {
289 int res = waitForRWData(fd, false, timeout, 0);
290 if (res > 0) {
291 /* there is room available */
292 firstTry = false;
293 }
294 else if (res == 0) {
295 throw runtime_error("Timeout while waiting to write data");
296 } else {
297 throw runtime_error("Error while waiting for room to write data");
298 }
299 }
300 else {
301 throw runtime_error("Timeout while waiting to write data");
302 }
303 }
304 else {
305 unixDie("failed in write2WithTimeout");
306 }
307 }
308 while (firstTry);
309
310 return 0;
311 }
312
313 template class NetmaskTree<bool>;
314
315 bool sendSizeAndMsgWithTimeout(int sock, uint16_t bufferLen, const char* buffer, int idleTimeout, const ComboAddress* dest, const ComboAddress* local, unsigned int localItf, int totalTimeout, int flags)
316 {
317 uint16_t size = htons(bufferLen);
318 char cbuf[256];
319 struct msghdr msgh;
320 struct iovec iov[2];
321 int remainingTime = totalTimeout;
322 time_t start = 0;
323 if (totalTimeout) {
324 start = time(NULL);
325 }
326
327 /* Set up iov and msgh structures. */
328 memset(&msgh, 0, sizeof(struct msghdr));
329 msgh.msg_control = nullptr;
330 msgh.msg_controllen = 0;
331 if (dest) {
332 msgh.msg_name = reinterpret_cast<void*>(const_cast<ComboAddress*>(dest));
333 msgh.msg_namelen = dest->getSocklen();
334 }
335 else {
336 msgh.msg_name = nullptr;
337 msgh.msg_namelen = 0;
338 }
339
340 msgh.msg_flags = 0;
341
342 if (localItf != 0 && local) {
343 addCMsgSrcAddr(&msgh, cbuf, local, localItf);
344 }
345
346 iov[0].iov_base = &size;
347 iov[0].iov_len = sizeof(size);
348 iov[1].iov_base = reinterpret_cast<void*>(const_cast<char*>(buffer));
349 iov[1].iov_len = bufferLen;
350
351 size_t pos = 0;
352 size_t sent = 0;
353 size_t nbElements = sizeof(iov)/sizeof(*iov);
354 while (true) {
355 msgh.msg_iov = &iov[pos];
356 msgh.msg_iovlen = nbElements - pos;
357
358 ssize_t res = sendmsg(sock, &msgh, flags);
359 if (res > 0) {
360 size_t written = static_cast<size_t>(res);
361 sent += written;
362
363 if (sent == (sizeof(size) + bufferLen)) {
364 return true;
365 }
366 /* partial write, we need to keep only the (parts of) elements
367 that have not been written.
368 */
369 do {
370 if (written < iov[pos].iov_len) {
371 iov[pos].iov_len -= written;
372 iov[pos].iov_base = reinterpret_cast<void*>(reinterpret_cast<char*>(iov[pos].iov_base) + written);
373 written = 0;
374 }
375 else {
376 written -= iov[pos].iov_len;
377 iov[pos].iov_len = 0;
378 pos++;
379 }
380 }
381 while (written > 0 && pos < nbElements);
382 }
383 else if (res == -1) {
384 if (errno == EINTR) {
385 continue;
386 }
387 else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS) {
388 /* EINPROGRESS might happen with non blocking socket,
389 especially with TCP Fast Open */
390 int ret = waitForRWData(sock, false, (totalTimeout == 0 || idleTimeout <= remainingTime) ? idleTimeout : remainingTime, 0);
391 if (ret > 0) {
392 /* there is room available */
393 }
394 else if (ret == 0) {
395 throw runtime_error("Timeout while waiting to send data");
396 } else {
397 throw runtime_error("Error while waiting for room to send data");
398 }
399 }
400 else {
401 unixDie("failed in sendSizeAndMsgWithTimeout");
402 }
403 }
404 if (totalTimeout) {
405 time_t now = time(NULL);
406 int elapsed = now - start;
407 if (elapsed >= remainingTime) {
408 throw runtime_error("Timeout while sending data");
409 }
410 start = now;
411 remainingTime -= elapsed;
412 }
413 }
414
415 return false;
416 }
417
418 /* requires a non-blocking socket.
419 On Linux, we could use MSG_DONTWAIT on a blocking socket
420 but this is not portable.
421 */
422 bool isTCPSocketUsable(int sock)
423 {
424 int err = 0;
425 char buf = '\0';
426 size_t buf_size = sizeof(buf);
427
428 do {
429 ssize_t got = recv(sock, &buf, buf_size, MSG_PEEK);
430
431 if (got > 0) {
432 /* socket is usable, some data is even waiting to be read */
433 return true;
434 }
435 else if (got == 0) {
436 /* other end has closed the socket */
437 return false;
438 }
439 else {
440 err = errno;
441
442 if (err == EAGAIN || err == EWOULDBLOCK) {
443 /* socket is usable, no data waiting */
444 return true;
445 }
446 else {
447 if (err != EINTR) {
448 /* something is wrong, could be ECONNRESET,
449 ENOTCONN, EPIPE, but anyway this socket is
450 not usable. */
451 return false;
452 }
453 }
454 }
455 } while (err == EINTR);
456
457 return false;
458 }