IpcConnection.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #ifdef __WINDOWS__
  35. #include <WinSock2.h>
  36. #include <Windows.h>
  37. #else
  38. #include <sys/socket.h>
  39. #include <sys/un.h>
  40. #include <unistd.h>
  41. #endif
  42. namespace ZeroTier {
  43. IpcConnection::IpcConnection(const char *endpoint,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
  44. _handler(commandHandler),
  45. _arg(arg),
  46. _sock(0)
  47. {
  48. #ifdef __WINDOWS__
  49. #else
  50. struct sockaddr_un unaddr;
  51. unaddr.sun_family = AF_UNIX;
  52. strncpy(unaddr.sun_path,endpoint,sizeof(unaddr.sun_path));
  53. unaddr.sun_path[sizeof(unaddr.sun_path) - 1] = (char)0;
  54. _sock = socket(AF_UNIX,SOCK_STREAM,0);
  55. if (_sock <= 0)
  56. throw std::runtime_error("unable to create socket of type AF_UNIX");
  57. if (connect(_sock,(struct sockaddr *)&unaddr,sizeof(unaddr))) {
  58. ::close(_sock);
  59. throw std::runtime_error("IPC endpoint unreachable");
  60. }
  61. #endif
  62. Thread::start(this);
  63. }
  64. IpcConnection::IpcConnection(int s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
  65. _handler(commandHandler),
  66. _arg(arg),
  67. _sock(s)
  68. {
  69. Thread::start(this);
  70. }
  71. IpcConnection::~IpcConnection()
  72. {
  73. #ifdef __WINDOWS__
  74. #else
  75. _writeLock.lock();
  76. int s = _sock;
  77. _sock = 0;
  78. if (s > 0) {
  79. ::shutdown(s,SHUT_RDWR);
  80. ::close(s);
  81. }
  82. _writeLock.unlock();
  83. #endif
  84. }
  85. void IpcConnection::printf(const char *format,...)
  86. {
  87. va_list ap;
  88. int n;
  89. char tmp[65536];
  90. Mutex::Lock _l(_writeLock);
  91. if (_sock <= 0)
  92. return;
  93. va_start(ap,format);
  94. n = (int)::vsnprintf(tmp,sizeof(tmp),format,ap);
  95. va_end(ap);
  96. if (n <= 0)
  97. return;
  98. #ifdef __WINDOWS__
  99. #else
  100. ::write(_sock,tmp,n);
  101. #endif
  102. }
  103. void IpcConnection::threadMain()
  104. throw()
  105. {
  106. #ifdef __WINDOWS__
  107. #else
  108. char tmp[65536];
  109. char linebuf[65536];
  110. unsigned int lineptr = 0;
  111. int s,n,i;
  112. char c;
  113. for(;;) {
  114. s = _sock;
  115. if (s <= 0)
  116. break;
  117. n = (int)::read(s,tmp,sizeof(tmp));
  118. if (n <= 0)
  119. break;
  120. for(i=0;i<n;++i) {
  121. c = (linebuf[lineptr] = tmp[i]);
  122. if ((c == '\r')||(c == '\n')||(lineptr == (sizeof(linebuf) - 1))) {
  123. if (lineptr) {
  124. linebuf[lineptr] = (char)0;
  125. _handler(_arg,this,IPC_EVENT_COMMAND,linebuf);
  126. lineptr = 0;
  127. }
  128. } else ++lineptr;
  129. }
  130. }
  131. {
  132. _writeLock.lock();
  133. int s = _sock;
  134. _sock = 0;
  135. if (s > 0)
  136. ::close(s);
  137. _writeLock.unlock();
  138. }
  139. _handler(_arg,this,IPC_EVENT_CONNECTION_CLOSED,(const char *)0);
  140. #endif
  141. }
  142. } // namespace ZeroTier