NetconUtilities.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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 3 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, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <stdio.h>
  30. #include <sys/socket.h>
  31. #include "lwip/ip.h"
  32. #include "lwip/ip_addr.h"
  33. #include "lwip/ip_frag.h"
  34. #ifndef _NETCON_UTILITIES_CPP
  35. #define _NETCON_UTILITIES_CPP
  36. namespace ZeroTier
  37. {
  38. // Functions used to pass file descriptors between processes
  39. ssize_t sock_fd_write(int sock, int fd)
  40. {
  41. ssize_t size;
  42. struct msghdr msg;
  43. struct iovec iov;
  44. char buf = '\0';
  45. int buflen = 1;
  46. union
  47. {
  48. struct cmsghdr cmsghdr;
  49. char control[CMSG_SPACE(sizeof (int))];
  50. } cmsgu;
  51. struct cmsghdr *cmsg;
  52. iov.iov_base = &buf;
  53. iov.iov_len = buflen;
  54. msg.msg_name = NULL;
  55. msg.msg_namelen = 0;
  56. msg.msg_iov = &iov;
  57. msg.msg_iovlen = 1;
  58. if (fd != -1) {
  59. msg.msg_control = cmsgu.control;
  60. msg.msg_controllen = sizeof(cmsgu.control);
  61. cmsg = CMSG_FIRSTHDR(&msg);
  62. cmsg->cmsg_len = CMSG_LEN(sizeof (int));
  63. cmsg->cmsg_level = SOL_SOCKET;
  64. cmsg->cmsg_type = SCM_RIGHTS;
  65. *((int *) CMSG_DATA(cmsg)) = fd;
  66. } else {
  67. msg.msg_control = NULL;
  68. msg.msg_controllen = 0;
  69. }
  70. size = sendmsg(sock, &msg, 0);
  71. if (size < 0)
  72. perror ("sendmsg");
  73. return size;
  74. }
  75. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
  76. {
  77. ssize_t size;
  78. if (fd) {
  79. struct msghdr msg;
  80. struct iovec iov;
  81. union
  82. {
  83. struct cmsghdr cmsghdr;
  84. char control[CMSG_SPACE(sizeof (int))];
  85. } cmsgu;
  86. struct cmsghdr *cmsg;
  87. iov.iov_base = buf;
  88. iov.iov_len = bufsize;
  89. msg.msg_name = NULL;
  90. msg.msg_namelen = 0;
  91. msg.msg_iov = &iov;
  92. msg.msg_iovlen = 1;
  93. msg.msg_control = cmsgu.control;
  94. msg.msg_controllen = sizeof(cmsgu.control);
  95. size = recvmsg (sock, &msg, 0);
  96. if (size < 0) {
  97. perror ("recvmsg");
  98. exit(1);
  99. }
  100. cmsg = CMSG_FIRSTHDR(&msg);
  101. if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
  102. if (cmsg->cmsg_level != SOL_SOCKET) {
  103. fprintf (stderr, "invalid cmsg_level %d\n",
  104. cmsg->cmsg_level);
  105. exit(1);
  106. }
  107. if (cmsg->cmsg_type != SCM_RIGHTS) {
  108. fprintf (stderr, "invalid cmsg_type %d\n",
  109. cmsg->cmsg_type);
  110. exit(1);
  111. }
  112. *fd = *((int *) CMSG_DATA(cmsg));
  113. } else
  114. *fd = -1;
  115. } else {
  116. size = read (sock, buf, bufsize);
  117. if (size < 0) {
  118. perror("read");
  119. exit(1);
  120. }
  121. }
  122. return size;
  123. }
  124. }
  125. #endif