IpcConnection.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 *,const SharedPtr<IpcConnection> &,const char *),void *arg) :
  44. _handler(commandHandler),
  45. _arg(arg),
  46. _sock(0)
  47. {
  48. struct sockaddr_un unaddr;
  49. unaddr.sun_family = AF_UNIX;
  50. strncpy(unaddr.sun_path,endpoint,sizeof(unaddr.sun_path));
  51. unaddr.sun_path[sizeof(unaddr.sun_path) - 1] = (char)0;
  52. _sock = socket(AF_UNIX,SOCK_STREAM,0);
  53. if (_sock <= 0)
  54. throw std::runtime_error("unable to create socket of type AF_UNIX");
  55. if (connect(_sock,(struct sockaddr *)&unaddr,sizeof(unaddr))) {
  56. ::close(_sock);
  57. throw std::runtime_error("IPC endpoint unreachable");
  58. }
  59. Thread::start(this);
  60. }
  61. IpcConnection::IpcConnection(int s,void (*commandHandler)(void *,const SharedPtr<IpcConnection> &,const char *),void *arg) :
  62. _handler(commandHandler),
  63. _arg(arg),
  64. _sock(s)
  65. {
  66. Thread::start(this);
  67. }
  68. IpcConnection::~IpcConnection()
  69. {
  70. this->close();
  71. }
  72. void IpcConnection::printf(const char *format,...)
  73. {
  74. va_list ap;
  75. int n;
  76. char tmp[65536];
  77. Mutex::Lock _l(_writeLock);
  78. if (_sock <= 0)
  79. return;
  80. va_start(ap,format);
  81. n = (int)::vsnprintf(tmp,sizeof(tmp),format,ap);
  82. va_end(ap);
  83. ::write(_sock,tmp,n);
  84. }
  85. void IpcConnection::close()
  86. {
  87. Mutex::Lock _l(_writeLock);
  88. int s = _sock;
  89. _sock = 0;
  90. if (s > 0) {
  91. ::shutdown(s,SHUT_RDWR);
  92. ::close(s);
  93. }
  94. Thread::join(_thread);
  95. }
  96. void IpcConnection::threadMain()
  97. throw()
  98. {
  99. char tmp[65536];
  100. char linebuf[65536];
  101. unsigned int lineptr = 0;
  102. while (_sock) {
  103. int n = (int)::read(_sock,tmp,sizeof(tmp));
  104. if (n <= 0)
  105. break;
  106. for(int i=0;i<n;++i) {
  107. char c = (linebuf[lineptr] = tmp[i]);
  108. if ((c == '\r')||(c == '\n')||(lineptr == (sizeof(linebuf) - 1))) {
  109. if (lineptr) {
  110. linebuf[lineptr] = (char)0;
  111. _handler(_arg,SharedPtr<IpcConnection>(this),linebuf);
  112. lineptr = 0;
  113. }
  114. } else ++lineptr;
  115. }
  116. }
  117. }
  118. } // namespace ZeroTier