IpcListener.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #ifdef __WINDOWS__
  33. #include <WinSock2.h>
  34. #include <Windows.h>
  35. #else
  36. #include <sys/socket.h>
  37. #include <sys/un.h>
  38. #include <sys/stat.h>
  39. #include <sys/types.h>
  40. #include <unistd.h>
  41. #endif
  42. namespace ZeroTier {
  43. IpcListener::IpcListener(const char *ep,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
  44. _endpoint(ep),
  45. _handler(commandHandler),
  46. _arg(arg),
  47. _sock(0)
  48. {
  49. #ifdef __WINDOWS__
  50. #else
  51. struct sockaddr_un unaddr;
  52. unaddr.sun_family = AF_UNIX;
  53. strncpy(unaddr.sun_path,_endpoint.c_str(),sizeof(unaddr.sun_path));
  54. unaddr.sun_path[sizeof(unaddr.sun_path) - 1] = (char)0;
  55. struct stat stattmp;
  56. if (stat(_endpoint.c_str(),&stattmp)) {
  57. int testSock = socket(AF_UNIX,SOCK_STREAM,0);
  58. if (testSock <= 0)
  59. throw std::runtime_error("unable to create socket of type AF_UNIX");
  60. if (connect(testSock,(struct sockaddr *)&unaddr,sizeof(unaddr))) {
  61. // error means nothing is listening, orphaned name
  62. ::close(testSock);
  63. } else {
  64. // success means endpoint is being actively listened to by a process
  65. ::close(testSock);
  66. throw std::runtime_error("IPC endpoint address in use");
  67. }
  68. }
  69. ::unlink(_endpoint.c_str());
  70. _sock = socket(AF_UNIX,SOCK_STREAM,0);
  71. if (_sock <= 0)
  72. throw std::runtime_error("unable to create socket of type AF_UNIX");
  73. if (bind(_sock,(struct sockaddr *)&unaddr,sizeof(unaddr))) {
  74. ::close(_sock);
  75. throw std::runtime_error("IPC endpoint could not be bound");
  76. }
  77. if (listen(_sock,8)) {
  78. ::close(_sock);
  79. throw std::runtime_error("listen() failed for bound AF_UNIX socket");
  80. }
  81. #endif
  82. _thread = Thread::start(this);
  83. }
  84. IpcListener::~IpcListener()
  85. {
  86. #ifdef __WINDOWS__
  87. #else
  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. unlink(_endpoint.c_str());
  96. #endif
  97. }
  98. void IpcListener::threadMain()
  99. throw()
  100. {
  101. #ifdef __WINDOWS__
  102. #else
  103. struct sockaddr_un unaddr;
  104. socklen_t socklen;
  105. int s;
  106. while (_sock > 0) {
  107. unaddr.sun_family = AF_UNIX;
  108. strncpy(unaddr.sun_path,_endpoint.c_str(),sizeof(unaddr.sun_path));
  109. unaddr.sun_path[sizeof(unaddr.sun_path) - 1] = (char)0;
  110. socklen = sizeof(unaddr);
  111. s = accept(_sock,(struct sockaddr *)&unaddr,&socklen);
  112. if (s <= 0)
  113. break;
  114. if (!_sock) {
  115. ::close(s);
  116. break;
  117. }
  118. try {
  119. _handler(_arg,new IpcConnection(s,_handler,_arg),IpcConnection::IPC_EVENT_NEW_CONNECTION,(const char *)0);
  120. } catch ( ... ) {} // handlers should not throw
  121. }
  122. #endif
  123. }
  124. } // namespace ZeroTier