TUNInterface_openbsd.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "interface/Interface.h"
  17. #include "interface/TUNConfigurator.h"
  18. #include "util/Bits.h"
  19. #include <errno.h>
  20. #include <ctype.h>
  21. #include <fcntl.h>
  22. #include <stdio.h>
  23. #include <sys/ioctl.h>
  24. #include <unistd.h>
  25. #include <sys/socket.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <stdlib.h>
  29. #include <stddef.h>
  30. #include <net/if.h>
  31. #include <string.h>
  32. #include <netdb.h>
  33. #include <net/if_tun.h>
  34. #include <netinet/in.h>
  35. #include <netinet6/in6_var.h>
  36. #include <netinet6/nd6.h>
  37. /* Tun Configurator for OpenBSD. */
  38. struct Interface* TUNInterface_new(const char* interfaceName,
  39. char assignedInterfaceName[TUNInterface_IFNAMSIZ],
  40. struct EventBase* base,
  41. struct Log* logger,
  42. struct Except* eh,
  43. struct Allocator* alloc)
  44. {
  45. // to store the tunnel device index
  46. int ppa = 0;
  47. // Open the descriptor
  48. int tunFd = open("/dev/tun0", O_RDWR);
  49. if (tunFd == -1) {
  50. tunFd = open("/dev/tun1", O_RDWR);
  51. ppa = 1;
  52. }
  53. if (tunFd == -1) {
  54. tunFd = open("/dev/tun2", O_RDWR);
  55. ppa = 2;
  56. }
  57. if (tunFd == -1) {
  58. tunFd = open("/dev/tun3", O_RDWR);
  59. ppa = 3;
  60. }
  61. if (tunFd < 0 ) {
  62. int err = errno;
  63. close(tunFd);
  64. char* error = NULL;
  65. if (tunFd < 0) {
  66. error = "open(\"/dev/tunX\")";
  67. }
  68. Except_raise(eh, TUNConfigurator_initTun_INTERNAL, error, strerror(err));
  69. }
  70. // Since devices are numbered rather than named, it's not possible to have tun0 and cjdns0
  71. // so we'll skip the pretty names and call everything tunX
  72. snprintf(assignedInterfaceName, TUNConfigurator_IFNAMSIZ, "tun%d", ppa);
  73. char* error = NULL;
  74. if (error) {
  75. int err = errno;
  76. close(tunFd);
  77. Except_raise(eh, TUNConfigurator_initTun_INTERNAL, "%s [%s]", error, strerror(err));
  78. }
  79. struct Pipe* p = Pipe_forFiles(tunFd, tunFd, base, eh, alloc);
  80. return BSDMessageTypeWrapper_new(&p->iface, logger);
  81. }