GuardianBase.cpp 8.6 KB

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