GuardianBase.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // GuardianBase.cpp : Defines the exported functions for the DLL application.
  2. //
  3. #include "stdafx.h"
  4. #ifdef linux
  5. #include <netinet/in.h> // for sockaddr_in
  6. #include <sys/types.h> // for socket
  7. #include <sys/socket.h> // for socket
  8. #include <arpa/inet.h>
  9. #include <string.h> // for bzero
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <syslog.h>
  13. #include "GuardianBase.h"
  14. #define HELLO_WORLD_SERVER_PORT 6666
  15. #define BUFFER_SIZE 1024
  16. #define FILE_NAME_MAX_SIZE 512
  17. #else
  18. #ifndef WIN32_LEAN_AND_MEAN
  19. #define WIN32_LEAN_AND_MEAN
  20. #endif
  21. #include <windows.h>
  22. #include <winsock2.h>
  23. #include <ws2tcpip.h>
  24. #include "GuardianBase.h"
  25. // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
  26. #pragma comment (lib, "Ws2_32.lib")
  27. #pragma comment (lib, "Mswsock.lib")
  28. #pragma comment (lib, "AdvApi32.lib")
  29. #endif //linux
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #define DEFAULT_PORT "30005"
  33. const int default_port = 30005;
  34. int GetSocket()
  35. {
  36. #ifdef linux
  37. openlog("gdbase",LOG_CONS|LOG_PID,LOG_LOCAL0);
  38. syslog(LOG_ERR,"enter GetSocket");
  39. struct sockaddr_in client_addr;
  40. bzero(&client_addr, sizeof(client_addr));
  41. client_addr.sin_family = AF_INET;
  42. client_addr.sin_addr.s_addr = htons(INADDR_ANY);
  43. client_addr.sin_port = htons(0);
  44. int client_socket = socket(AF_INET, SOCK_STREAM, 0);
  45. if (client_socket < 0)
  46. {
  47. printf("Create Socket Failed!\n");
  48. return -1;
  49. }
  50. if (bind(client_socket, (struct sockaddr*) & client_addr, sizeof(client_addr)))
  51. {
  52. printf("Client Bind Port Failed!\n");
  53. return -1;
  54. }
  55. struct sockaddr_in server_addr;
  56. bzero(&server_addr, sizeof(server_addr));
  57. server_addr.sin_family = AF_INET;
  58. char* local_addr = "127.0.0.1";
  59. inet_aton(local_addr, &(server_addr.sin_addr));
  60. server_addr.sin_port = htons(default_port);
  61. socklen_t server_addr_length = sizeof(server_addr);
  62. //向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接
  63. if (connect(client_socket, (struct sockaddr*) & server_addr, server_addr_length) < 0)
  64. {
  65. printf("Can Not Connect To server!\n");
  66. return -1;
  67. }
  68. syslog(LOG_ERR,"after getsocket");
  69. return client_socket;
  70. #else
  71. WSADATA wsaData;
  72. SOCKET ConnectSocket = INVALID_SOCKET;
  73. struct addrinfo *result = NULL, *ptr = NULL,
  74. hints;
  75. int iResult;
  76. // Initialize Winsock
  77. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  78. if (iResult != 0) {
  79. printf("WSAStartup failed with error: %d\n", iResult);
  80. return ConnectSocket;
  81. }
  82. ZeroMemory(&hints, sizeof(hints));
  83. hints.ai_family = AF_INET;
  84. hints.ai_socktype = SOCK_STREAM;
  85. hints.ai_protocol = IPPROTO_TCP;
  86. // Resolve the server address and port
  87. iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
  88. if (iResult != 0) {
  89. printf("getaddrinfo failed with error: %d\n", iResult);
  90. WSACleanup();
  91. return ConnectSocket;
  92. }
  93. // 超时时间
  94. struct timeval tm;
  95. tm.tv_sec = 5;
  96. tm.tv_usec = 0;
  97. // Attempt to connect to an address until one succeeds
  98. for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
  99. // Create a SOCKET for connecting to server
  100. ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
  101. ptr->ai_protocol);
  102. if (ConnectSocket == INVALID_SOCKET) {
  103. printf("socket failed with error: %ld\n", WSAGetLastError());
  104. WSACleanup();
  105. return ConnectSocket;
  106. }
  107. // 设置为非阻塞的socket
  108. int iMode = 1;
  109. ioctlsocket(ConnectSocket, FIONBIO, (u_long FAR*)&iMode);
  110. bool isConnected = false;
  111. // Connect to server.
  112. iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  113. if (iResult != SOCKET_ERROR)
  114. {
  115. isConnected = true;
  116. }
  117. else
  118. {
  119. fd_set set;
  120. FD_ZERO(&set);
  121. FD_SET(ConnectSocket, &set);
  122. //oiltmp need or not? FD_ISSET
  123. if (select(-1, NULL, &set, NULL, &tm) <= 0)
  124. {
  125. isConnected = false;
  126. }
  127. else
  128. {
  129. int error = -1;
  130. int optLen = sizeof(int);
  131. getsockopt(ConnectSocket, SOL_SOCKET, SO_ERROR, (char*)&error, &optLen);
  132. if (0 == error)
  133. isConnected = true;
  134. }
  135. }
  136. if (!isConnected) {
  137. closesocket(ConnectSocket);
  138. ConnectSocket = INVALID_SOCKET;
  139. continue;
  140. }
  141. break;
  142. }
  143. freeaddrinfo(result);
  144. return ConnectSocket;
  145. #endif //linux
  146. }
  147. int SendData(GuardianInfo& info,int &sendSocket)
  148. {
  149. #ifdef linux
  150. openlog("gdbase",LOG_CONS|LOG_PID,LOG_LOCAL0);
  151. syslog(LOG_ERR,"to SendData");
  152. int ConnectSocket = -1;
  153. ConnectSocket = GetSocket();
  154. if (ConnectSocket == -1) {
  155. printf("Unable to connect to server!\n");
  156. return -1;
  157. }
  158. sendSocket = ConnectSocket;
  159. char sendbuf[DATA_BUFSIZE];
  160. memset(sendbuf, 0, sizeof(sendbuf));
  161. memcpy(sendbuf, &info, sizeof(GuardianInfo));
  162. send(sendSocket, sendbuf, sizeof(GuardianInfo), 0);//oiltmp size need to re calc
  163. syslog(LOG_ERR,"after send");
  164. return 0;
  165. #else
  166. SOCKET ConnectSocket = INVALID_SOCKET;
  167. ConnectSocket = GetSocket();
  168. if (ConnectSocket == INVALID_SOCKET) {
  169. printf("Unable to connect to server!\n");
  170. WSACleanup();
  171. return -1;
  172. }
  173. sendSocket = ConnectSocket;
  174. char sendbuf[DATA_BUFSIZE];
  175. int iResult;
  176. // Send an initial buffer
  177. memset(sendbuf, 0, sizeof(sendbuf));
  178. memcpy(sendbuf, &info, sizeof(GuardianInfo));
  179. iResult = send(ConnectSocket, sendbuf, (int)sizeof(sendbuf), 0);
  180. if (iResult == SOCKET_ERROR) {
  181. printf("send failed with error: %d\n", WSAGetLastError());
  182. closesocket(ConnectSocket);
  183. WSACleanup();
  184. return -1;
  185. }
  186. printf("Bytes Sent: %ld\n", iResult);
  187. // shutdown the connection since no more data will be sent
  188. iResult = shutdown(ConnectSocket, SD_SEND);
  189. if (iResult == SOCKET_ERROR) {
  190. printf("shutdown failed with error: %d\n", WSAGetLastError());
  191. closesocket(ConnectSocket);
  192. WSACleanup();
  193. return -1;
  194. }
  195. return 0;
  196. #endif //linux
  197. }
  198. void ReceiveDataOfOneWay(int socket)
  199. {
  200. #ifdef linux
  201. char recvbuf[DATA_BUFSIZE];
  202. int recvbuflen = DATA_BUFSIZE;
  203. bzero(recvbuf, DATA_BUFSIZE);
  204. int length = 0;
  205. while (length = recv(socket,recvbuf, DATA_BUFSIZE, 0))
  206. {
  207. if (length < 0)
  208. {
  209. printf("Recieve Data From Server Failed!\n");
  210. break;
  211. }
  212. bzero(recvbuf, DATA_BUFSIZE);
  213. }
  214. printf("Recieve From Server Finished\n");
  215. close(socket);
  216. #else
  217. char recvbuf[DATA_BUFSIZE];
  218. int recvbuflen = DATA_BUFSIZE;
  219. int iResult;
  220. // Receive until the peer closes the connection
  221. do {
  222. iResult = recv(socket, recvbuf, recvbuflen, 0);
  223. if (iResult > 0)
  224. {
  225. //printf("Bytes received: %d\n%s\n", iResult,recvbuf);
  226. switch (recvbuf[0])
  227. {
  228. default:
  229. break;
  230. }
  231. }
  232. else if (iResult == 0)
  233. printf("Connection closed\n");
  234. else
  235. printf("recv failed with error: %d\n", WSAGetLastError());
  236. } while (iResult > 0);
  237. // cleanup
  238. closesocket(socket);
  239. WSACleanup();
  240. #endif //linux
  241. }
  242. int ShakeHands(WorkStateEnum &eState)
  243. {
  244. GuardianInfo* pGdInfo = new GuardianInfo;
  245. memset(pGdInfo, 0, sizeof(GuardianInfo));
  246. pGdInfo->eType = GdOpShakeHand;
  247. int socket = -1;
  248. int eErr = SendData(*pGdInfo, socket);
  249. if (eErr != 0)
  250. return eErr;
  251. char recvbuf[DATA_BUFSIZE];
  252. int recvbuflen = DATA_BUFSIZE;
  253. int iResult;
  254. // Receive until the peer closes the connection
  255. do {
  256. iResult = recv(socket, recvbuf, recvbuflen, 0);
  257. if ( iResult > 0 )
  258. {
  259. //printf("Bytes received: %d\n%s\n", iResult,recvbuf);
  260. switch(recvbuf[0])
  261. {
  262. case 'u':
  263. eState = WorkStateUpgrading;
  264. break;
  265. case 'g':
  266. eState = WorkStateGuardian;
  267. break;
  268. case 'r':
  269. eState = WorkStateReboot;
  270. break;
  271. case 'b':
  272. eState = WorkStateRollback;
  273. break;
  274. default:
  275. break;
  276. }
  277. }
  278. else if ( iResult == 0 )
  279. printf("Connection closed\n");
  280. else
  281. printf("recv failed with error\n");
  282. } while( iResult > 0 );
  283. // cleanup
  284. #ifdef linux
  285. close(socket);
  286. #else
  287. closesocket(socket);
  288. WSACleanup();
  289. #endif
  290. return 0;
  291. }
  292. int PushUpdateTask(const char *pszPackName)
  293. {
  294. return 0;
  295. }
  296. bool IsInstalling()
  297. {
  298. return false;
  299. }
  300. int UpgradeRestart(const DWORD dwParam1,const DWORD dwParam2)
  301. {
  302. #ifdef linux
  303. openlog("gdbase",LOG_CONS|LOG_PID,LOG_LOCAL0);
  304. syslog(LOG_ERR,"UpgradeRestart");
  305. #endif
  306. GuardianInfo* pGdInfo = new GuardianInfo;
  307. memset(pGdInfo, 0, sizeof(GuardianInfo));
  308. pGdInfo->eType = GdOpUpgradeRestart;
  309. pGdInfo->dwParam1 = dwParam1;
  310. pGdInfo->dwParam2 = dwParam2;
  311. int socket = -1;
  312. int eErr = SendData(*pGdInfo, socket);
  313. if (eErr == 0)
  314. ReceiveDataOfOneWay(socket);
  315. if (pGdInfo != NULL)
  316. delete pGdInfo;
  317. return eErr;
  318. }
  319. int FrameworkQuit(int eReason)
  320. {
  321. #ifdef linux
  322. openlog("gdbase",LOG_CONS|LOG_PID,LOG_LOCAL0);
  323. syslog(LOG_ERR,"FrameworkQuit");
  324. #endif
  325. GuardianInfo* pGdInfo = new GuardianInfo;
  326. memset(pGdInfo, 0, sizeof(GuardianInfo));
  327. pGdInfo->eType = GdOpFrameQuit;
  328. int socket = -1;
  329. int eErr = SendData(*pGdInfo, socket);
  330. if (eErr == 0)
  331. ReceiveDataOfOneWay(socket);
  332. if (pGdInfo != NULL)
  333. delete pGdInfo;
  334. return eErr;
  335. }