NetconUtilities.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /*
  39. ip_addr_t convert_ip(struct sockaddr_in * addr)
  40. {
  41. ip_addr_t conn_addr;
  42. struct sockaddr_in *ipv4 = addr;
  43. short a = ip4_addr1(&(ipv4->sin_addr));
  44. short b = ip4_addr2(&(ipv4->sin_addr));
  45. short c = ip4_addr3(&(ipv4->sin_addr));
  46. short d = ip4_addr4(&(ipv4->sin_addr));
  47. IP4_ADDR(&conn_addr, a,b,c,d);
  48. return conn_addr;
  49. }
  50. */
  51. /*
  52. ip_addr_t ip_addr_sin(register struct sockaddr_in *sin) {
  53. ip_addr_t ip;
  54. *((struct sockaddr_in*) &ip) = *sin;
  55. return ip;
  56. }
  57. */
  58. // Functions used to pass file descriptors between processes
  59. ssize_t sock_fd_write(int sock, int fd)
  60. {
  61. ssize_t size;
  62. struct msghdr msg;
  63. struct iovec iov;
  64. char buf = '\0';
  65. int buflen = 1;
  66. union
  67. {
  68. struct cmsghdr cmsghdr;
  69. char control[CMSG_SPACE(sizeof (int))];
  70. } cmsgu;
  71. struct cmsghdr *cmsg;
  72. iov.iov_base = &buf;
  73. iov.iov_len = buflen;
  74. msg.msg_name = NULL;
  75. msg.msg_namelen = 0;
  76. msg.msg_iov = &iov;
  77. msg.msg_iovlen = 1;
  78. if (fd != -1) {
  79. msg.msg_control = cmsgu.control;
  80. msg.msg_controllen = sizeof(cmsgu.control);
  81. cmsg = CMSG_FIRSTHDR(&msg);
  82. cmsg->cmsg_len = CMSG_LEN(sizeof (int));
  83. cmsg->cmsg_level = SOL_SOCKET;
  84. cmsg->cmsg_type = SCM_RIGHTS;
  85. *((int *) CMSG_DATA(cmsg)) = fd;
  86. } else {
  87. msg.msg_control = NULL;
  88. msg.msg_controllen = 0;
  89. }
  90. size = sendmsg(sock, &msg, 0);
  91. if (size < 0)
  92. perror ("sendmsg");
  93. return size;
  94. }
  95. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
  96. {
  97. ssize_t size;
  98. if (fd) {
  99. struct msghdr msg;
  100. struct iovec iov;
  101. union
  102. {
  103. struct cmsghdr cmsghdr;
  104. char control[CMSG_SPACE(sizeof (int))];
  105. } cmsgu;
  106. struct cmsghdr *cmsg;
  107. iov.iov_base = buf;
  108. iov.iov_len = bufsize;
  109. msg.msg_name = NULL;
  110. msg.msg_namelen = 0;
  111. msg.msg_iov = &iov;
  112. msg.msg_iovlen = 1;
  113. msg.msg_control = cmsgu.control;
  114. msg.msg_controllen = sizeof(cmsgu.control);
  115. size = recvmsg (sock, &msg, 0);
  116. if (size < 0) {
  117. perror ("recvmsg");
  118. exit(1);
  119. }
  120. cmsg = CMSG_FIRSTHDR(&msg);
  121. if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
  122. if (cmsg->cmsg_level != SOL_SOCKET) {
  123. fprintf (stderr, "invalid cmsg_level %d\n",
  124. cmsg->cmsg_level);
  125. exit(1);
  126. }
  127. if (cmsg->cmsg_type != SCM_RIGHTS) {
  128. fprintf (stderr, "invalid cmsg_type %d\n",
  129. cmsg->cmsg_type);
  130. exit(1);
  131. }
  132. *fd = *((int *) CMSG_DATA(cmsg));
  133. } else
  134. *fd = -1;
  135. } else {
  136. size = read (sock, buf, bufsize);
  137. if (size < 0) {
  138. perror("read");
  139. exit(1);
  140. }
  141. }
  142. return size;
  143. }
  144. }
  145. #endif