IpcListener.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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 <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include "IpcListener.hpp"
  32. #ifndef __WINDOWS__
  33. #include <sys/socket.h>
  34. #include <sys/un.h>
  35. #include <sys/stat.h>
  36. #include <sys/types.h>
  37. #include <unistd.h>
  38. #endif
  39. namespace ZeroTier {
  40. IpcListener::IpcListener(const char *ep,unsigned int timeout,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
  41. _endpoint(ep),
  42. _handler(commandHandler),
  43. _arg(arg),
  44. _timeout(timeout),
  45. #ifdef __WINDOWS__
  46. _run(true),
  47. _running(true)
  48. #else
  49. _sock(0)
  50. #endif
  51. {
  52. #ifndef __WINDOWS__
  53. struct sockaddr_un unaddr;
  54. unaddr.sun_family = AF_UNIX;
  55. strncpy(unaddr.sun_path,_endpoint.c_str(),sizeof(unaddr.sun_path));
  56. unaddr.sun_path[sizeof(unaddr.sun_path) - 1] = (char)0;
  57. struct stat stattmp;
  58. if (stat(_endpoint.c_str(),&stattmp)) {
  59. int testSock = socket(AF_UNIX,SOCK_STREAM,0);
  60. if (testSock <= 0)
  61. throw std::runtime_error("unable to create socket of type AF_UNIX");
  62. if (connect(testSock,(struct sockaddr *)&unaddr,sizeof(unaddr))) {
  63. // error means nothing is listening, orphaned name
  64. ::close(testSock);
  65. } else {
  66. // success means endpoint is being actively listened to by a process
  67. ::close(testSock);
  68. throw std::runtime_error("IPC endpoint address in use");
  69. }
  70. }
  71. ::unlink(_endpoint.c_str());
  72. _sock = socket(AF_UNIX,SOCK_STREAM,0);
  73. if (_sock <= 0)
  74. throw std::runtime_error("unable to create socket of type AF_UNIX");
  75. if (bind(_sock,(struct sockaddr *)&unaddr,sizeof(unaddr))) {
  76. ::close(_sock);
  77. throw std::runtime_error("IPC endpoint could not be bound");
  78. }
  79. if (listen(_sock,8)) {
  80. ::close(_sock);
  81. throw std::runtime_error("listen() failed for bound AF_UNIX socket");
  82. }
  83. ::chmod(_endpoint.c_str(),0777);
  84. #endif
  85. _thread = Thread::start(this);
  86. }
  87. IpcListener::~IpcListener()
  88. {
  89. #ifdef __WINDOWS__
  90. _run = false;
  91. while (_running) {
  92. Thread::cancelIO(_thread);
  93. HANDLE tmp = CreateFileA(_endpoint.c_str(),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,NULL,OPEN_EXISTING,0,NULL);
  94. if (tmp != INVALID_HANDLE_VALUE)
  95. CloseHandle(tmp);
  96. Sleep(250);
  97. }
  98. #else
  99. int s = _sock;
  100. _sock = 0;
  101. if (s > 0) {
  102. ::shutdown(s,SHUT_RDWR);
  103. ::close(s);
  104. }
  105. Thread::join(_thread);
  106. ::unlink(_endpoint.c_str());
  107. #endif
  108. }
  109. void IpcListener::threadMain()
  110. throw()
  111. {
  112. #ifdef __WINDOWS__
  113. HANDLE s;
  114. while (_run) {
  115. s = CreateNamedPipeA(_endpoint.c_str(),PIPE_ACCESS_DUPLEX,PIPE_READMODE_BYTE|PIPE_TYPE_BYTE|PIPE_WAIT,PIPE_UNLIMITED_INSTANCES,1024,1024,0,NULL);
  116. if (s != INVALID_HANDLE_VALUE) {
  117. if ((ConnectNamedPipe(s,NULL))||(GetLastError() == ERROR_PIPE_CONNECTED)) {
  118. if (!_run) {
  119. DisconnectNamedPipe(s);
  120. CloseHandle(s);
  121. break;
  122. }
  123. try {
  124. _handler(_arg,new IpcConnection(s,_timeout,_handler,_arg),IpcConnection::IPC_EVENT_NEW_CONNECTION,(const char *)0);
  125. } catch ( ... ) {} // handlers should not throw
  126. } else {
  127. CloseHandle(s);
  128. }
  129. }
  130. }
  131. _running = false;
  132. #else
  133. struct sockaddr_un unaddr;
  134. socklen_t socklen;
  135. int s;
  136. while (_sock > 0) {
  137. unaddr.sun_family = AF_UNIX;
  138. strncpy(unaddr.sun_path,_endpoint.c_str(),sizeof(unaddr.sun_path));
  139. unaddr.sun_path[sizeof(unaddr.sun_path) - 1] = (char)0;
  140. socklen = sizeof(unaddr);
  141. s = accept(_sock,(struct sockaddr *)&unaddr,&socklen);
  142. if (s <= 0)
  143. break;
  144. if (!_sock) {
  145. ::close(s);
  146. break;
  147. }
  148. try {
  149. _handler(_arg,new IpcConnection(s,_timeout,_handler,_arg),IpcConnection::IPC_EVENT_NEW_CONNECTION,(const char *)0);
  150. } catch ( ... ) {} // handlers should not throw
  151. }
  152. #endif
  153. }
  154. } // namespace ZeroTier