You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnixSocket.cxx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (c) 2012 University of Oslo. All Rights Reserved.
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <sys/stat.h>
  23. #include <sys/socket.h>
  24. #include <sys/un.h>
  25. #include <unistd.h>
  26. #include <errno.h>
  27. #include <stdlib.h>
  28. #include <stddef.h>
  29. #include <network/UnixSocket.h>
  30. #include <rfb/LogWriter.h>
  31. using namespace network;
  32. using namespace rdr;
  33. static rfb::LogWriter vlog("UnixSocket");
  34. // -=- UnixSocket
  35. UnixSocket::UnixSocket(int sock) : Socket(sock)
  36. {
  37. }
  38. UnixSocket::UnixSocket(const char *path)
  39. {
  40. int sock, err, result;
  41. sockaddr_un addr;
  42. socklen_t salen;
  43. if (strlen(path) >= sizeof(addr.sun_path))
  44. throw SocketException("socket path is too long", ENAMETOOLONG);
  45. // - Create a socket
  46. sock = socket(AF_UNIX, SOCK_STREAM, 0);
  47. if (sock == -1)
  48. throw SocketException("unable to create socket", errno);
  49. // - Attempt to connect
  50. memset(&addr, 0, sizeof(addr));
  51. addr.sun_family = AF_UNIX;
  52. strcpy(addr.sun_path, path);
  53. salen = sizeof(addr);
  54. while ((result = connect(sock, (sockaddr *)&addr, salen)) == -1) {
  55. err = errno;
  56. close(sock);
  57. break;
  58. }
  59. if (result == -1)
  60. throw SocketException("unable connect to socket", err);
  61. setFd(sock);
  62. }
  63. char* UnixSocket::getPeerAddress() {
  64. struct sockaddr_un addr;
  65. socklen_t salen;
  66. // AF_UNIX only has a single address (the server side).
  67. // Unfortunately we don't know which end we are, so we'll have to
  68. // test a bit.
  69. salen = sizeof(addr);
  70. if (getpeername(getFd(), (struct sockaddr *)&addr, &salen) != 0) {
  71. vlog.error("unable to get peer name for socket");
  72. return rfb::strDup("");
  73. }
  74. if (salen > offsetof(struct sockaddr_un, sun_path))
  75. return rfb::strDup(addr.sun_path);
  76. salen = sizeof(addr);
  77. if (getsockname(getFd(), (struct sockaddr *)&addr, &salen) != 0) {
  78. vlog.error("unable to get local name for socket");
  79. return rfb::strDup("");
  80. }
  81. if (salen > offsetof(struct sockaddr_un, sun_path))
  82. return rfb::strDup(addr.sun_path);
  83. // socketpair() will create unnamed sockets
  84. return rfb::strDup("(unnamed UNIX socket)");
  85. }
  86. char* UnixSocket::getPeerEndpoint() {
  87. return getPeerAddress();
  88. }
  89. bool UnixSocket::cork(bool enable)
  90. {
  91. return true;
  92. }
  93. UnixListener::UnixListener(const char *path, int mode)
  94. {
  95. struct sockaddr_un addr;
  96. mode_t saved_umask;
  97. int err, result;
  98. if (strlen(path) >= sizeof(addr.sun_path))
  99. throw SocketException("socket path is too long", ENAMETOOLONG);
  100. // - Create a socket
  101. if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
  102. throw SocketException("unable to create listening socket", errno);
  103. // - Delete existing socket (ignore result)
  104. unlink(path);
  105. // - Attempt to bind to the requested path
  106. memset(&addr, 0, sizeof(addr));
  107. addr.sun_family = AF_UNIX;
  108. strcpy(addr.sun_path, path);
  109. saved_umask = umask(0777);
  110. result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  111. err = errno;
  112. umask(saved_umask);
  113. if (result < 0) {
  114. close(fd);
  115. throw SocketException("unable to bind listening socket", err);
  116. }
  117. // - Set socket mode
  118. if (chmod(path, mode) < 0) {
  119. err = errno;
  120. close(fd);
  121. throw SocketException("unable to set socket mode", err);
  122. }
  123. listen(fd);
  124. }
  125. UnixListener::~UnixListener()
  126. {
  127. struct sockaddr_un addr;
  128. socklen_t salen = sizeof(addr);
  129. if (getsockname(getFd(), (struct sockaddr *)&addr, &salen) == 0)
  130. unlink(addr.sun_path);
  131. }
  132. Socket* UnixListener::createSocket(int fd) {
  133. return new UnixSocket(fd);
  134. }
  135. int UnixListener::getMyPort() {
  136. return 0;
  137. }