IpcConnection.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <stdarg.h>
  32. #include <stdexcept>
  33. #include "IpcConnection.hpp"
  34. #ifndef __WINDOWS__
  35. #include <sys/socket.h>
  36. #include <sys/un.h>
  37. #include <unistd.h>
  38. #endif
  39. namespace ZeroTier {
  40. IpcConnection::IpcConnection(const char *endpoint,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
  41. _handler(commandHandler),
  42. _arg(arg),
  43. #ifdef __WINDOWS__
  44. _sock(INVALID_HANDLE_VALUE),
  45. _incoming(false),
  46. #else
  47. _sock(-1),
  48. #endif
  49. _run(true),
  50. _running(true)
  51. {
  52. #ifdef __WINDOWS__
  53. _sock = CreateFileA(endpoint,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,NULL,OPEN_EXISTING,0,NULL);
  54. if (_sock == INVALID_HANDLE_VALUE)
  55. throw std::runtime_error("IPC endpoint unreachable");
  56. DWORD pipeMode = PIPE_READMODE_BYTE;
  57. SetNamedPipeHandleState(_sock,&pipeMode,NULL,NULL);
  58. #else
  59. struct sockaddr_un unaddr;
  60. unaddr.sun_family = AF_UNIX;
  61. strncpy(unaddr.sun_path,endpoint,sizeof(unaddr.sun_path));
  62. unaddr.sun_path[sizeof(unaddr.sun_path) - 1] = (char)0;
  63. _sock = socket(AF_UNIX,SOCK_STREAM,0);
  64. if (_sock <= 0)
  65. throw std::runtime_error("unable to create socket of type AF_UNIX");
  66. if (connect(_sock,(struct sockaddr *)&unaddr,sizeof(unaddr))) {
  67. ::close(_sock);
  68. throw std::runtime_error("IPC endpoint unreachable");
  69. }
  70. #endif
  71. _thread = Thread::start(this);
  72. }
  73. #ifdef __WINDOWS__
  74. IpcConnection::IpcConnection(HANDLE s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
  75. #else
  76. IpcConnection::IpcConnection(int s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
  77. #endif
  78. _handler(commandHandler),
  79. _arg(arg),
  80. _sock(s),
  81. #ifdef __WINDOWS__
  82. _incoming(true),
  83. #endif
  84. _run(true),
  85. _running(true)
  86. {
  87. _thread = Thread::start(this);
  88. }
  89. IpcConnection::~IpcConnection()
  90. {
  91. _writeLock.lock();
  92. _run = false;
  93. _writeLock.unlock();
  94. #ifdef __WINDOWS__
  95. while (_running) {
  96. Thread::cancelIO(_thread);
  97. Sleep(100);
  98. }
  99. #else
  100. int s = _sock;
  101. _sock = 0;
  102. if (s > 0) {
  103. ::shutdown(s,SHUT_RDWR);
  104. ::close(s);
  105. }
  106. #endif
  107. }
  108. void IpcConnection::printf(const char *format,...)
  109. {
  110. va_list ap;
  111. int n;
  112. char tmp[65536];
  113. va_start(ap,format);
  114. n = (int)::vsnprintf(tmp,sizeof(tmp),format,ap);
  115. va_end(ap);
  116. if (n <= 0)
  117. return;
  118. Mutex::Lock _l(_writeLock);
  119. #ifdef __WINDOWS__
  120. _writeBuf.append(tmp,n);
  121. Thread::cancelIO(_thread);
  122. #else
  123. if (_sock > 0)
  124. ::write(_sock,tmp,n);
  125. #endif
  126. }
  127. void IpcConnection::threadMain()
  128. throw()
  129. {
  130. char tmp[65536];
  131. char linebuf[65536];
  132. unsigned int lineptr = 0;
  133. char c;
  134. #ifdef __WINDOWS__
  135. DWORD n,i;
  136. std::string wbuf;
  137. #else
  138. int s,n,i;
  139. #endif
  140. while (_run) {
  141. #ifdef __WINDOWS__
  142. {
  143. Mutex::Lock _l(_writeLock);
  144. if (!_run)
  145. break;
  146. if (_writeBuf.length() > 0) {
  147. wbuf.append(_writeBuf);
  148. _writeBuf.clear();
  149. }
  150. }
  151. if (wbuf.length() > 0) {
  152. n = 0;
  153. if ((WriteFile(_sock,wbuf.data(),(DWORD)(wbuf.length()),&n,NULL))&&(n > 0)) {
  154. if (n < (DWORD)wbuf.length())
  155. wbuf.erase(0,n);
  156. else wbuf.clear();
  157. } else if (GetLastError() != ERROR_OPERATION_ABORTED)
  158. break;
  159. FlushFileBuffers(_sock);
  160. }
  161. if (!_run)
  162. break;
  163. n = 0;
  164. if ((!ReadFile(_sock,tmp,sizeof(tmp),&n,NULL))||(n <= 0)) {
  165. if (GetLastError() == ERROR_OPERATION_ABORTED)
  166. n = 0;
  167. else break;
  168. }
  169. if (!_run)
  170. break;
  171. #else
  172. if ((s = _sock) <= 0)
  173. break;
  174. n = (int)::read(s,tmp,sizeof(tmp));
  175. if ((n <= 0)||(_sock <= 0))
  176. break;
  177. #endif
  178. for(i=0;i<n;++i) {
  179. c = (linebuf[lineptr] = tmp[i]);
  180. if ((c == '\r')||(c == '\n')||(lineptr == (sizeof(linebuf) - 1))) {
  181. if (lineptr) {
  182. linebuf[lineptr] = (char)0;
  183. _handler(_arg,this,IPC_EVENT_COMMAND,linebuf);
  184. lineptr = 0;
  185. }
  186. } else ++lineptr;
  187. }
  188. }
  189. _writeLock.lock();
  190. bool r = _run;
  191. _writeLock.unlock();
  192. #ifdef __WINDOWS__
  193. if (_incoming)
  194. DisconnectNamedPipe(_sock);
  195. CloseHandle(_sock);
  196. _running = false;
  197. #endif
  198. if (r)
  199. _handler(_arg,this,IPC_EVENT_CONNECTION_CLOSED,(const char *)0);
  200. }
  201. } // namespace ZeroTier