UnixCommunicationSocket.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <unistd.h>
  32. #include <string.h>
  33. #include <sys/types.h>
  34. #include <sys/socket.h>
  35. #include <sys/un.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <IAERequest.h>
  39. #include <IAEResponse.h>
  40. #include <se_trace.h>
  41. #include <UnixCommunicationSocket.h>
  42. UnixCommunicationSocket::UnixCommunicationSocket(const char* socketbase)
  43. :mSocketBase(NULL), mWasTimeout(false), mTimeoutMseconds(0)
  44. {
  45. //doing all this manually to ensure <new> based memory allocation across the solution
  46. size_t size = strlen(socketbase) + 1;
  47. //ensure a fairly decend size for socketbase is not overflowed
  48. if (size > 255)
  49. return; //a socketbase of more than 255 chars may break the system on connect
  50. mSocketBase = new char[size];
  51. memset(mSocketBase, 0, size);
  52. strncpy(mSocketBase, socketbase, size);
  53. mSocket = -1;
  54. memset(&mStartTime, 0, sizeof(struct timeval));
  55. }
  56. UnixCommunicationSocket::UnixCommunicationSocket(int socket)
  57. :mSocketBase(NULL), mWasTimeout(false), mTimeoutMseconds(0)
  58. {
  59. mSocket = socket;
  60. memset(&mStartTime, 0, sizeof(struct timeval));
  61. }
  62. UnixCommunicationSocket::~UnixCommunicationSocket()
  63. {
  64. disconnect();
  65. if (mSocketBase != NULL)
  66. {
  67. delete [] mSocketBase;
  68. mSocketBase = NULL;
  69. }
  70. }
  71. void UnixCommunicationSocket::disconnect()
  72. {
  73. if (mSocket != -1){
  74. close(mSocket);
  75. mSocket = -1;
  76. }
  77. }
  78. bool UnixCommunicationSocket::setTimeout(uint32_t timeout_milliseconds)
  79. {
  80. struct timeval timeout;
  81. mTimeoutMseconds = timeout_milliseconds;
  82. timeout.tv_sec = timeout_milliseconds / 1000;
  83. uint32_t millisecondsRemainder = timeout_milliseconds % 1000;
  84. timeout.tv_usec = millisecondsRemainder * 1000;
  85. int rc = 0;
  86. rc = setsockopt (mSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
  87. if (rc < 0)
  88. return false;
  89. rc = setsockopt (mSocket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
  90. if (rc < 0)
  91. return false;
  92. return true;
  93. }
  94. void UnixCommunicationSocket::MarkStartTime()
  95. {
  96. gettimeofday(&mStartTime,NULL);
  97. }
  98. bool UnixCommunicationSocket::CheckForTimeout()
  99. {
  100. mWasTimeout = false;
  101. if (mTimeoutMseconds == 0)
  102. return false;
  103. struct timeval now;
  104. gettimeofday(&now, NULL);
  105. uint32_t sec = (uint32_t) (now.tv_sec - mStartTime.tv_sec);
  106. int32_t usec = (int32_t) (now.tv_usec - mStartTime.tv_usec);
  107. uint32_t delta_msec = (uint32_t) (sec * 1000 + usec / 1000);
  108. if (delta_msec >= mTimeoutMseconds)
  109. {
  110. mWasTimeout = true;
  111. return true;
  112. }
  113. return false;
  114. }
  115. ssize_t UnixCommunicationSocket::writeRaw(const char* data, ssize_t length)
  116. {
  117. MarkStartTime();
  118. if (mSocket == -1)
  119. return -1;
  120. ssize_t written = 0;
  121. do {
  122. ssize_t step = write(mSocket, data+written, length-written);
  123. if(step == -1 && errno == EINTR && CheckForTimeout() == false){
  124. SE_TRACE_WARNING("write was interrupted by signal\n");
  125. continue;
  126. }
  127. if (step < 0 || CheckForTimeout())
  128. {
  129. //this connection is probably closed
  130. disconnect();
  131. break;
  132. }
  133. written+=step;
  134. } while (written < length);
  135. return written;
  136. }
  137. char* UnixCommunicationSocket::readRaw(ssize_t length)
  138. {
  139. if (mSocket == -1)
  140. return NULL;
  141. MarkStartTime();
  142. ssize_t total_read = 0;
  143. char * recBuf = NULL;
  144. recBuf = new char[length];
  145. memset(recBuf, 0, length);
  146. do {
  147. ssize_t step = read(mSocket, recBuf + total_read, length - total_read);
  148. if(step == -1 && errno == EINTR && CheckForTimeout() == false){
  149. SE_TRACE_WARNING("read was interrupted by signal\n");
  150. continue;
  151. }
  152. //check connection closed by peer
  153. if (step <= 0 || CheckForTimeout())
  154. {
  155. //this connection is probably closed
  156. disconnect();
  157. delete[] recBuf;
  158. recBuf = NULL;
  159. break;
  160. }
  161. total_read += step;
  162. } while (total_read < length);
  163. return recBuf;
  164. }
  165. //this will connect to the AESM by opening an Unix Socket
  166. bool UnixCommunicationSocket::init()
  167. {
  168. //init will always return directly with success if object was created with pre-existent socket
  169. if (mSocket == -1)
  170. {
  171. struct sockaddr_un serv_addr;
  172. mSocket = socket(AF_UNIX, SOCK_STREAM, 0);
  173. if(mSocket < 0)
  174. {
  175. return false;
  176. }
  177. memset(&serv_addr, 0, sizeof(struct sockaddr_un));
  178. serv_addr.sun_family = AF_UNIX;
  179. memset(serv_addr.sun_path, 0, sizeof(serv_addr.sun_path));
  180. strncpy(serv_addr.sun_path, mSocketBase, sizeof(serv_addr.sun_path));
  181. if( connect(mSocket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0)
  182. {
  183. return false;
  184. }
  185. if (mSocket < 0)
  186. return false;
  187. }
  188. return true;
  189. }
  190. int UnixCommunicationSocket::getSockDescriptor() {
  191. return mSocket;
  192. }