IpcListener.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <set>
  32. #include "IpcListener.hpp"
  33. #ifdef __WINDOWS__
  34. #include <WinSock2.h>
  35. #include <Windows.h>
  36. #else
  37. #include <sys/socket.h>
  38. #include <sys/ud.h>
  39. #include <unistd.h>
  40. #endif
  41. namespace ZeroTier {
  42. IpcListener::IpcListener(cosnt char *ep,void (*commandHandler)(void *,const SharedPtr<IpcConnection> &,const char *),void *arg) :
  43. _endpoint(ep),
  44. _handler(commandHandler),
  45. _arg(arg),
  46. _sock(0)
  47. {
  48. struct sockaddr_un unaddr;
  49. unaddr.sun_family = AF_UNIX;
  50. if ((long)_endpoint.length() > (long)(sizeof(unaddr.sun_path) - 1))
  51. throw std::runtime_error("IPC endpoint path too long");
  52. strncpy(unaddr.sun_path,_endpoint.c_str(),sizeof(unaddr.sun_path));
  53. for(int tries=0;tries<3;++tries) {
  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 (bind(_sock,(struct sockaddr *)&unaddr,sizeof(unaddr))) {
  58. ::close(_sock);
  59. if (errno == EADDRINUSE) {
  60. int testSock = socket(AF_UNIX,SOCK_STREAM,0);
  61. if (testSock <= 0)
  62. throw std::runtime_error("unable to create socket of type AF_UNIX");
  63. if (connect(testSock,(struct sockaddr *)&unaddr,sizeof(unaddr))) {
  64. // error indicates nothing is listening on other end, so unlink and try again
  65. ::close(testSock);
  66. unlink(_endpoint.c_str());
  67. } else {
  68. // success means endpoint is being actively listened to by a process
  69. ::close(testSock);
  70. throw std::runtime_error("IPC endpoint address in use");
  71. }
  72. } else throw std::runtime_error("IPC endpoint could not be bound");
  73. }
  74. }
  75. if (listen(_sock,8)) {
  76. ::close(_sock);
  77. throw std::runtime_error("listen() failed for bound AF_UNIX socket");
  78. }
  79. _thread = Thread::start(this);
  80. }
  81. IpcListener::~IpcListener()
  82. {
  83. int s = _sock;
  84. _sock = 0;
  85. if (s > 0) {
  86. ::shutdown(s,SHUT_RDWR);
  87. ::close(s);
  88. }
  89. Thread::join(_thread);
  90. unlink(_endpoint.c_str());
  91. }
  92. void IpcListener::threadMain()
  93. throw()
  94. {
  95. struct sockaddr_un unaddr;
  96. socklen_t socklen;
  97. int s;
  98. unaddr.sun_family = AF_UNIX;
  99. strncpy(unaddr.sun_path,_endpoint.c_str(),sizeof(unaddr.sun_path));
  100. while (_sock > 0) {
  101. socklen = sizeof(unaddr);
  102. s = accept(_sock,(struct sockaddr *)unaddr,&socklen);
  103. if (s <= 0)
  104. break;
  105. if (!_sock) {
  106. ::close(s);
  107. break;
  108. }
  109. try {
  110. _handler(_arg,SharedPtr<IpcConnection>(new IpcConnection(s)),(const char *)0);
  111. } catch ( ... ) {} // handlers should not throw
  112. }
  113. }
  114. } // namespace ZeroTier