Sendfd.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /* Functions used to pass file descriptors between processes */
  32. ssize_t sock_fd_write(int sock, int fd);
  33. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd);
  34. ssize_t sock_fd_write(int sock, int fd)
  35. {
  36. ssize_t size;
  37. struct msghdr msg;
  38. struct iovec iov;
  39. char buf = '\0';
  40. int buflen = 1;
  41. union {
  42. struct cmsghdr cmsghdr;
  43. char control[CMSG_SPACE(sizeof (int))];
  44. } cmsgu;
  45. struct cmsghdr *cmsg;
  46. iov.iov_base = &buf;
  47. iov.iov_len = buflen;
  48. msg.msg_name = NULL;
  49. msg.msg_namelen = 0;
  50. msg.msg_iov = &iov;
  51. msg.msg_iovlen = 1;
  52. if (fd != -1) {
  53. msg.msg_control = cmsgu.control;
  54. msg.msg_controllen = sizeof(cmsgu.control);
  55. cmsg = CMSG_FIRSTHDR(&msg);
  56. cmsg->cmsg_len = CMSG_LEN(sizeof (int));
  57. cmsg->cmsg_level = SOL_SOCKET;
  58. cmsg->cmsg_type = SCM_RIGHTS;
  59. *((int *) CMSG_DATA(cmsg)) = fd;
  60. } else {
  61. msg.msg_control = NULL;
  62. msg.msg_controllen = 0;
  63. }
  64. size = sendmsg(sock, &msg, 0);
  65. if (size < 0)
  66. perror ("sendmsg");
  67. return size;
  68. }
  69. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
  70. {
  71. ssize_t size;
  72. if (fd) {
  73. struct msghdr msg;
  74. struct iovec iov;
  75. union {
  76. struct cmsghdr cmsghdr;
  77. char control[CMSG_SPACE(sizeof (int))];
  78. } cmsgu;
  79. struct cmsghdr *cmsg;
  80. iov.iov_base = buf;
  81. iov.iov_len = bufsize;
  82. msg.msg_name = NULL;
  83. msg.msg_namelen = 0;
  84. msg.msg_iov = &iov;
  85. msg.msg_iovlen = 1;
  86. msg.msg_control = cmsgu.control;
  87. msg.msg_controllen = sizeof(cmsgu.control);
  88. size = recvmsg (sock, &msg, 0);
  89. if (size < 0) {
  90. perror ("recvmsg");
  91. exit(1);
  92. }
  93. cmsg = CMSG_FIRSTHDR(&msg);
  94. if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
  95. if (cmsg->cmsg_level != SOL_SOCKET) {
  96. fprintf (stderr, "invalid cmsg_level %d\n",
  97. cmsg->cmsg_level);
  98. exit(1);
  99. }
  100. if (cmsg->cmsg_type != SCM_RIGHTS) {
  101. fprintf (stderr, "invalid cmsg_type %d\n",
  102. cmsg->cmsg_type);
  103. exit(1);
  104. }
  105. *fd = *((int *) CMSG_DATA(cmsg));
  106. } else
  107. *fd = -1;
  108. } else {
  109. size = read (sock, buf, bufsize);
  110. if (size < 0) {
  111. perror("read");
  112. exit(1);
  113. }
  114. }
  115. return size;
  116. }