UnixCommunicationSocket.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <IAERequest.h>
  38. #include <IAEResponse.h>
  39. #include <UnixCommunicationSocket.h>
  40. UnixCommunicationSocket::UnixCommunicationSocket(const char* socketbase)
  41. :mSocketBase(NULL), mWasTimeout(false), mTimeoutMseconds(0)
  42. {
  43. //doing all this manually to ensure <new> based memory allocation across the solution
  44. size_t size = strlen(socketbase) + 1;
  45. //ensure a fairly decend size for socketbase is not overflowed
  46. if (size > 255)
  47. return; //a socketbase of more than 255 chars may break the system on connect
  48. mSocketBase = new char[size];
  49. memset(mSocketBase, 0, size);
  50. strncpy(mSocketBase, socketbase, size);
  51. mSocket = -1;
  52. memset(&mStartTime, 0, sizeof(struct timeval));
  53. }
  54. UnixCommunicationSocket::UnixCommunicationSocket(int socket)
  55. :mSocketBase(NULL), mWasTimeout(false), mTimeoutMseconds(0)
  56. {
  57. mSocket = socket;
  58. memset(&mStartTime, 0, sizeof(struct timeval));
  59. }
  60. UnixCommunicationSocket::~UnixCommunicationSocket()
  61. {
  62. disconnect();
  63. if (mSocketBase != NULL)
  64. {
  65. delete [] mSocketBase;
  66. mSocketBase = NULL;
  67. }
  68. }
  69. void UnixCommunicationSocket::disconnect()
  70. {
  71. if (mSocket != -1){
  72. close(mSocket);
  73. mSocket = -1;
  74. }
  75. }
  76. bool UnixCommunicationSocket::setTimeout(uint32_t timeout_milliseconds)
  77. {
  78. struct timeval timeout;
  79. mTimeoutMseconds = timeout_milliseconds;
  80. timeout.tv_sec = timeout_milliseconds / 1000;
  81. uint32_t millisecondsRemainder = timeout_milliseconds % 1000;
  82. timeout.tv_usec = millisecondsRemainder * 1000;
  83. int rc = 0;
  84. rc = setsockopt (mSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
  85. if (rc < 0)
  86. return false;
  87. rc = setsockopt (mSocket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
  88. if (rc < 0)
  89. return false;
  90. return true;
  91. }
  92. void UnixCommunicationSocket::MarkStartTime()
  93. {
  94. gettimeofday(&mStartTime,NULL);
  95. }
  96. bool UnixCommunicationSocket::CheckForTimeout()
  97. {
  98. mWasTimeout = false;
  99. if (mTimeoutMseconds == 0)
  100. return false;
  101. struct timeval now;
  102. gettimeofday(&now, NULL);
  103. uint32_t sec = (uint32_t) (now.tv_sec - mStartTime.tv_sec);
  104. int32_t usec = (int32_t) (now.tv_usec - mStartTime.tv_usec);
  105. uint32_t delta_msec = (uint32_t) (sec * 1000 + usec / 1000);
  106. if (delta_msec >= mTimeoutMseconds)
  107. {
  108. mWasTimeout = true;
  109. return true;
  110. }
  111. return false;
  112. }
  113. ssize_t UnixCommunicationSocket::writeRaw(const char* data, ssize_t length)
  114. {
  115. MarkStartTime();
  116. if (mSocket == -1)
  117. return -1;
  118. ssize_t written = 0;
  119. do {
  120. ssize_t step = write(mSocket, data+written, length-written);
  121. if (step < 0 || CheckForTimeout())
  122. {
  123. //this connection is probably closed
  124. disconnect();
  125. break;
  126. }
  127. written+=step;
  128. } while (written < length);
  129. return written;
  130. }
  131. char* UnixCommunicationSocket::readRaw(ssize_t length)
  132. {
  133. if (mSocket == -1)
  134. return NULL;
  135. MarkStartTime();
  136. ssize_t total_read = 0;
  137. char * recBuf = NULL;
  138. recBuf = new char[length];
  139. memset(recBuf, 0, length);
  140. do {
  141. ssize_t step = read(mSocket, recBuf + total_read, length - total_read);
  142. //check connection closed by peer
  143. if (step <= 0 || CheckForTimeout())
  144. {
  145. //this connection is probably closed
  146. disconnect();
  147. delete[] recBuf;
  148. recBuf = NULL;
  149. break;
  150. }
  151. total_read += step;
  152. } while (total_read < length);
  153. return recBuf;
  154. }
  155. //this will connect to the AESM by opening an Unix Socket
  156. bool UnixCommunicationSocket::init()
  157. {
  158. //init will always return directly with success if object was created with pre-existent socket
  159. if (mSocket == -1)
  160. {
  161. struct sockaddr_un serv_addr;
  162. mSocket = socket(AF_UNIX, SOCK_STREAM, 0);
  163. if(mSocket < 0)
  164. {
  165. return false;
  166. }
  167. memset(&serv_addr, 0, sizeof(struct sockaddr_un));
  168. serv_addr.sun_family = AF_UNIX;
  169. memset(serv_addr.sun_path, 0, sizeof(serv_addr.sun_path));
  170. strncpy(serv_addr.sun_path, mSocketBase, sizeof(serv_addr.sun_path));
  171. if( connect(mSocket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0)
  172. {
  173. return false;
  174. }
  175. if (mSocket < 0)
  176. return false;
  177. }
  178. return true;
  179. }
  180. int UnixCommunicationSocket::getSockDescriptor() {
  181. return mSocket;
  182. }