IpcConnection.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #else
  46. _sock(0)
  47. #endif
  48. {
  49. #ifdef __WINDOWS__
  50. _sock = CreateFileA(endpoint,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,NULL,OPEN_EXISTING,0,NULL);
  51. if (_sock == INVALID_HANDLE_VALUE)
  52. throw std::runtime_error("IPC endpoint unreachable");
  53. DWORD pipeMode = PIPE_READMODE_BYTE;
  54. SetNamedPipeHandleState(_sock,&pipeMode,NULL,NULL);
  55. #else
  56. struct sockaddr_un unaddr;
  57. unaddr.sun_family = AF_UNIX;
  58. strncpy(unaddr.sun_path,endpoint,sizeof(unaddr.sun_path));
  59. unaddr.sun_path[sizeof(unaddr.sun_path) - 1] = (char)0;
  60. _sock = socket(AF_UNIX,SOCK_STREAM,0);
  61. if (_sock <= 0)
  62. throw std::runtime_error("unable to create socket of type AF_UNIX");
  63. if (connect(_sock,(struct sockaddr *)&unaddr,sizeof(unaddr))) {
  64. ::close(_sock);
  65. throw std::runtime_error("IPC endpoint unreachable");
  66. }
  67. #endif
  68. Thread::start(this);
  69. }
  70. #ifdef __WINDOWS__
  71. IpcConnection::IpcConnection(HANDLE s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
  72. #else
  73. IpcConnection::IpcConnection(int s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
  74. #endif
  75. _handler(commandHandler),
  76. _arg(arg),
  77. _sock(s)
  78. {
  79. Thread::start(this);
  80. }
  81. IpcConnection::~IpcConnection()
  82. {
  83. _writeLock.lock();
  84. #ifdef __WINDOWS__
  85. HANDLE s = _sock;
  86. _sock = INVALID_HANDLE_VALUE;
  87. if (s != INVALID_HANDLE_VALUE) {
  88. CloseHandle(s);
  89. }
  90. #else
  91. int s = _sock;
  92. _sock = 0;
  93. if (s > 0) {
  94. ::shutdown(s,SHUT_RDWR);
  95. ::close(s);
  96. }
  97. #endif
  98. _writeLock.unlock();
  99. }
  100. void IpcConnection::printf(const char *format,...)
  101. {
  102. va_list ap;
  103. int n;
  104. char tmp[65536];
  105. Mutex::Lock _l(_writeLock);
  106. if (_sock <= 0)
  107. return;
  108. va_start(ap,format);
  109. n = (int)::vsnprintf(tmp,sizeof(tmp),format,ap);
  110. va_end(ap);
  111. if (n <= 0)
  112. return;
  113. #ifdef __WINDOWS__
  114. DWORD bsent = 0;
  115. WriteFile(_sock,tmp,n,&bsent,NULL);
  116. #else
  117. ::write(_sock,tmp,n);
  118. #endif
  119. }
  120. void IpcConnection::threadMain()
  121. throw()
  122. {
  123. char tmp[65536];
  124. char linebuf[65536];
  125. unsigned int lineptr = 0;
  126. #ifdef __WINDOWS__
  127. HANDLE s;
  128. DWORD n,i;
  129. #else
  130. int s,n,i;
  131. #endif
  132. char c;
  133. for(;;) {
  134. #ifdef __WINDOWS__
  135. s = _sock;
  136. if (s == INVALID_HANDLE_VALUE)
  137. break;
  138. if (!ReadFile(s,tmp,sizeof(tmp),&n,NULL))
  139. break;
  140. if (n < 0)
  141. break;
  142. #else
  143. s = _sock;
  144. if (s <= 0)
  145. break;
  146. n = (int)::read(s,tmp,sizeof(tmp));
  147. if (n <= 0)
  148. break;
  149. #endif
  150. for(i=0;i<n;++i) {
  151. c = (linebuf[lineptr] = tmp[i]);
  152. if ((c == '\r')||(c == '\n')||(lineptr == (sizeof(linebuf) - 1))) {
  153. if (lineptr) {
  154. linebuf[lineptr] = (char)0;
  155. _handler(_arg,this,IPC_EVENT_COMMAND,linebuf);
  156. lineptr = 0;
  157. }
  158. } else ++lineptr;
  159. }
  160. }
  161. {
  162. _writeLock.lock();
  163. s = _sock;
  164. #ifdef __WINDOWS__
  165. _sock = INVALID_HANDLE_VALUE;
  166. if (s != INVALID_HANDLE_VALUE)
  167. CloseHandle(s);
  168. #else
  169. _sock = 0;
  170. if (s > 0)
  171. ::close(s);
  172. #endif
  173. _writeLock.unlock();
  174. }
  175. _handler(_arg,this,IPC_EVENT_CONNECTION_CLOSED,(const char *)0);
  176. }
  177. } // namespace ZeroTier