Destination.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2002-2009 Moxie Marlinspike
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA
  18. */
  19. // XXX define one of these through autoconf
  20. //#define HAVE_PF
  21. #define HAVE_NETFILTER
  22. #include <arpa/inet.h>
  23. #ifdef HAVE_NETFILTER
  24. #include <limits.h>
  25. #include <linux/netfilter_ipv4.h>
  26. #endif
  27. #ifdef HAVE_PF
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/fcntl.h>
  32. #include <net/if.h>
  33. #include <netinet/in.h>
  34. #include <net/pfvar.h>
  35. #endif
  36. #include "util/Destination.hpp"
  37. int Destination::getOriginalDestination(boost::asio::ip::tcp::socket &socket,
  38. boost::asio::ip::tcp::endpoint &originalDestination)
  39. {
  40. #ifdef HAVE_NETFILTER
  41. struct sockaddr_in serverAddr;
  42. int fd = (int)socket.native();
  43. int size = sizeof(serverAddr);
  44. if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, &serverAddr, (socklen_t*)&size) < 0) {
  45. perror("Could not determine socket's original destination.");
  46. throw IndeterminateDestinationException();
  47. }
  48. originalDestination = boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(ntohl(serverAddr.sin_addr.s_addr)),
  49. ntohs(serverAddr.sin_port));
  50. return 1;
  51. #elif defined(HAVE_PF)
  52. boost::asio::ip::tcp::endpoint le = socket.local_endpoint();
  53. boost::asio::ip::tcp::endpoint re = socket.remote_endpoint();
  54. static int fd = -1;
  55. struct pfioc_natlook nl;
  56. if (fd < 0) {
  57. fd = open("/dev/pf", O_RDONLY);
  58. if (fd >= 0) {
  59. fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
  60. }
  61. }
  62. if (fd < 0) {
  63. perror("PF open failed");
  64. throw IndeterminateDestinationException();
  65. }
  66. memset(&nl, 0, sizeof(struct pfioc_natlook));
  67. nl.saddr.v4.s_addr = htonl(re.address().to_v4().to_ulong());
  68. nl.sport = htons(re.port());
  69. nl.daddr.v4.s_addr = htonl(le.address().to_v4().to_ulong());
  70. nl.dport = htons(le.port());
  71. nl.af = AF_INET;
  72. nl.proto = IPPROTO_TCP;
  73. nl.direction = PF_OUT;
  74. if (ioctl(fd, DIOCNATLOOK, &nl)) {
  75. if (errno != ENOENT) {
  76. perror("PF lookup failed: ioctl(DIOCNATLOOK)");
  77. close(fd);
  78. fd = -1;
  79. }
  80. throw IndeterminateDestinationException();
  81. }
  82. if (nl.daddr.v4.s_addr == nl.rdaddr.v4.s_addr && nl.dport == nl.rdport) {
  83. /* no destination addr/port rewriting in place */
  84. throw IndeterminateDestinationException();
  85. }
  86. originalDestination = boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(ntohl(nl.rdaddr.v4.s_addr)),
  87. ntohs(nl.rdport));
  88. return 1;
  89. #else
  90. throw IndeterminateDestinationException();
  91. #endif
  92. }