TUNInterface_linux.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "exception/Except.h"
  16. #include "memory/Allocator.h"
  17. #include "util/events/EventBase.h"
  18. #include "interface/Interface.h"
  19. #include "interface/tuntap/TUNInterface.h"
  20. #include "util/events/Pipe.h"
  21. #define string_strncpy
  22. #define string_strlen
  23. #define string_strerror
  24. #include "util/platform/libc/string.h"
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <sys/ioctl.h>
  28. #include <fcntl.h>
  29. #include <unistd.h>
  30. #include <sys/socket.h>
  31. #include <sys/types.h>
  32. #include <stdlib.h>
  33. #include <stddef.h>
  34. #include <net/if.h>
  35. #include <arpa/inet.h>
  36. #include <linux/if.h>
  37. #include <linux/if_tun.h>
  38. #include <linux/if_ether.h>
  39. struct Interface* TUNInterface_new(const char* interfaceName,
  40. char assignedInterfaceName[TUNInterface_IFNAMSIZ],
  41. struct EventBase* base,
  42. struct Log* logger,
  43. struct Except* eh,
  44. struct Allocator* alloc)
  45. {
  46. uint32_t maxNameSize = (IFNAMSIZ < TUNInterface_IFNAMSIZ) ? IFNAMSIZ : TUNInterface_IFNAMSIZ;
  47. Log_info(logger, "Initializing tun device [%s]", ((interfaceName) ? interfaceName : "auto"));
  48. struct ifreq ifRequest = { .ifr_flags = IFF_TUN };
  49. if (interfaceName) {
  50. if (strlen(interfaceName) > maxNameSize) {
  51. Except_throw(eh, "tunnel name too big, limit is [%d] characters", maxNameSize);
  52. }
  53. strncpy(ifRequest.ifr_name, interfaceName, maxNameSize);
  54. }
  55. int fileno = open("/dev/net/tun", O_RDWR);
  56. if (fileno < 0) {
  57. Except_throw(eh, "open(\"/dev/net/tun\") [%s]", strerror(errno));
  58. }
  59. if (ioctl(fileno, TUNSETIFF, &ifRequest) < 0) {
  60. int err = errno;
  61. close(fileno);
  62. Except_throw(eh, "ioctl(TUNSETIFF) [%s]", strerror(err));
  63. }
  64. strncpy(assignedInterfaceName, ifRequest.ifr_name, maxNameSize);
  65. struct Pipe* p = Pipe_forFiles(fileno, fileno, base, eh, alloc);
  66. return &p->iface;
  67. }