tcp_server.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <netdb.h>
  6. #include <sys/types.h>
  7. #include <netinet/in.h>
  8. #include <sys/socket.h>
  9. #include <unistd.h>
  10. #include <arpa/inet.h>
  11. #include <pthread.h>
  12. #include <signal.h>
  13. #define LOG_REQ_PORT 10001
  14. #define LOG_TFR_PORT 10002
  15. #define LOG_FRAME_HEADER_MAGIC 0xFF5AFFA5
  16. #define LOG_FRAME_TAIL_MAGIC 0xFFA5FF5A
  17. #define FILENAME_LEN 128
  18. #define MD5_LEN 32
  19. #define MAX_LOG_SIZE 0x100000
  20. #pragma pack(push, 1)
  21. typedef struct {
  22. uint32_t header;
  23. uint32_t type;
  24. uint32_t sub_type;
  25. uint32_t sub_type_1;
  26. uint32_t len;
  27. uint8_t checksum;
  28. } LogFrame;
  29. typedef struct {
  30. uint8_t request_type;
  31. uint32_t log_size;
  32. uint8_t log_md5[MD5_LEN];
  33. uint8_t log_filename[FILENAME_LEN];
  34. } LogTfrReq;
  35. #pragma pack(pop)
  36. static int log_req_socket = -1;
  37. static int log_tfr_socket = -1;
  38. static pthread_t req_thread_id = 0;
  39. static pthread_t tfr_thread_id = 0;
  40. #define FRAME_MAX_LEN 0x100000
  41. #define LOG_TFR_MAX_SIZE 512000
  42. #define OTA_LOG_FILENAME "log.zip"
  43. static int log_rev_state = 0;
  44. static int log_state_len = 0;
  45. static int log_rev_len = 0;
  46. static uint32_t log_frame_type = 0;
  47. static uint32_t log_frame_subtype = 0;
  48. static uint32_t log_frame_subtype_1 = 0;
  49. static uint32_t log_frame_len = 0;
  50. static uint8_t log_frame_checksum = 0;
  51. static uint8_t *log_frame_buf;
  52. static uint32_t log_file_size;
  53. static uint32_t log_file_offset;
  54. static uint32_t log_file_framenum = 0;
  55. static char log_file_md5[MD5_LEN];
  56. static char log_file_name[FILENAME_LEN];
  57. static uint8_t log_start_req = 1;
  58. #define LOG_CHECKSUM(x) ((((x) >> 24) & 0xff) + (((x) >> 16) & 0xff) + (((x) >> 8) & 0xff) + ((x) & 0xff))
  59. static uint8_t calc_log_checksum(uint32_t type, uint32_t subtype, uint32_t subtype_1, uint32_t len)
  60. {
  61. uint32_t checksum = 0;
  62. checksum = LOG_CHECKSUM(type) + LOG_CHECKSUM(subtype) + LOG_CHECKSUM(subtype_1) + LOG_CHECKSUM(len);
  63. return 0x100 - (uint8_t)checksum;
  64. }
  65. static void logSendFrame(int socket, uint32_t type, uint32_t subtype,
  66. uint32_t subtype_1, void *data, uint32_t len)
  67. {
  68. LogFrame *frame = NULL;
  69. uint8_t *tmp;
  70. int32_t leftsize;
  71. int ret;
  72. printf("send frame type=0x%x, subtype=0x%x, len=%d.\n", type, subtype, len);
  73. frame = (LogFrame*)malloc(sizeof(LogFrame) + len + 4);
  74. if (frame) {
  75. frame->header = LOG_FRAME_HEADER_MAGIC;
  76. frame->type = type;
  77. frame->sub_type = subtype;
  78. frame->sub_type_1 = subtype_1;
  79. frame->len = len;
  80. frame->checksum = calc_log_checksum(type, subtype, subtype_1, len);
  81. tmp = (uint8_t*)frame + sizeof(LogFrame);
  82. if (len) {
  83. memcpy(tmp, data, len);
  84. tmp += len;
  85. }
  86. *(uint32_t *)tmp = LOG_FRAME_TAIL_MAGIC;
  87. }
  88. leftsize = sizeof(LogFrame) + len + 4;
  89. tmp = (uint8_t *)frame;
  90. while (leftsize > 0) {
  91. ret = send(socket, tmp, leftsize, 0);
  92. if (ret > 0) {
  93. leftsize -= ret;
  94. tmp += leftsize;
  95. }
  96. }
  97. free(frame);
  98. }
  99. static int g_exit = 0;
  100. static void RevLogFrameHandler(uint32_t type, uint32_t subtype, uint32_t subtype_1,
  101. uint8_t *data, uint32_t len)
  102. {
  103. printf("rev frame type=0x%x, subtype=0x%x, len=%d.\n", type, subtype, len);
  104. switch (type) {
  105. case 0x1b:
  106. if (subtype == 0x06) {
  107. if (data[0] == 0x01) {
  108. uint8_t ack[2];
  109. log_file_size = (data[4] << 24) | (data[3] << 16) | (data[2] << 8) | data[1];
  110. memcpy(log_file_md5, data + 5, MD5_LEN);
  111. memcpy(log_file_name, data + 5 + MD5_LEN, FILENAME_LEN);
  112. ack[0] = 0x01; //positive ack
  113. ack[1] = 0x00; //no error
  114. logSendFrame(log_req_socket, 0x0b, 0x06, 0, ack, 2);
  115. }
  116. }
  117. break;
  118. case 0x1f:
  119. if (subtype == 0x01) {
  120. int datalen = *(uint32_t*)data;
  121. int flag = *(uint16_t*)(data + 4);
  122. if (datalen + 6 != len) {
  123. printf("invalid datalen %d.\n", datalen);
  124. break;
  125. }
  126. if (flag == 0) {
  127. uint8_t ack[130] = {0};
  128. printf("log transfer finish.\n");
  129. if (log_file_framenum == 0) {
  130. FILE *fp = fopen(log_file_name, "wb");
  131. if (fp) {
  132. printf("create log file %s.\n", log_file_name);
  133. fwrite(data + 6, 1, datalen, fp);
  134. fclose(fp);
  135. }
  136. } else {
  137. FILE *fp = fopen(log_file_name, "a+");
  138. if (fp) {
  139. fwrite(data + 6, 1, datalen, fp);
  140. fclose(fp);
  141. }
  142. }
  143. strcpy((char*)ack, log_file_name);
  144. ack[128] = 0; //receive successed
  145. ack[129] = 0; //no resend
  146. logSendFrame(log_req_socket, 0x0f, 0x03, 0, ack, 130);
  147. g_exit = 1;
  148. break;
  149. } else if (flag == 1) {
  150. uint8_t ack= 0;
  151. // first frame
  152. FILE *fp = fopen(log_file_name, "wb");
  153. if (!fp) {
  154. printf("create %s fail.\n", log_file_name);
  155. ack = 1; //no send next frame
  156. logSendFrame(log_req_socket, 0x0f, 0x02, 0, &ack, 1);
  157. break;
  158. }
  159. printf("create log file %s.\n", log_file_name);
  160. fwrite(data + 6, 1, datalen, fp);
  161. fclose(fp);
  162. logSendFrame(log_req_socket, 0x0f, 0x02, 0, &ack, 1);
  163. } else {
  164. uint8_t ack= 0;
  165. FILE *fp = fopen(log_file_name, "a+");
  166. if (!fp) {
  167. printf("open %s fail.\n", log_file_name);
  168. ack = 1; //no send next frame
  169. logSendFrame(log_req_socket, 0x0f, 0x02, 0, &ack, 1);
  170. break;
  171. }
  172. fwrite(data + 6, 1, datalen, fp);
  173. fclose(fp);
  174. logSendFrame(log_req_socket, 0x0f, 0x02, 0, &ack, 1);
  175. }
  176. log_file_framenum++;
  177. }
  178. break;
  179. }
  180. }
  181. static void RevLogDataHandler(uint8_t *buf, int32_t len)
  182. {
  183. int i;
  184. for (i = 0; i < len; i++) {
  185. switch (log_rev_state) {
  186. case 0: //head receive
  187. if (log_state_len == 0 && buf[i] == 0xa5) {
  188. log_state_len++;
  189. } else if (log_state_len == 1 && buf[i] == 0xff) {
  190. log_state_len++;
  191. } else if (log_state_len == 2 && buf[i] == 0x5a) {
  192. log_state_len++;
  193. } else if (log_state_len == 3 && buf[i] == 0xff) {
  194. log_rev_state++;
  195. log_state_len = 0;
  196. log_frame_type = 0;
  197. log_frame_subtype = 0;
  198. log_frame_subtype_1 = 0;
  199. log_frame_len = 0;
  200. } else if (buf[i] == 0xff) {
  201. log_state_len = 1;
  202. } else {
  203. log_state_len = 0;
  204. }
  205. break;
  206. case 1: //type receive
  207. log_frame_type |= buf[i] << (8 * log_state_len);
  208. if (++log_state_len == 4) {
  209. log_rev_state++;
  210. log_state_len = 0;
  211. }
  212. break;
  213. case 2: //sub_type receive
  214. log_frame_subtype |= buf[i] << (8 * log_state_len);
  215. if (++log_state_len == 4) {
  216. log_rev_state++;
  217. log_state_len = 0;
  218. }
  219. break;
  220. case 3: //sub_type_1 receive
  221. log_frame_subtype_1 |= buf[i] << (8 * log_state_len);
  222. if (++log_state_len == 4) {
  223. log_rev_state++;
  224. log_state_len = 0;
  225. }
  226. break;
  227. case 4: //data len receive
  228. log_frame_len |= buf[i] << (8 * log_state_len);
  229. if (++log_state_len == 4) {
  230. if (log_frame_len > FRAME_MAX_LEN) {
  231. printf("Invalid NCM frame len 0x%x.\n", log_frame_len);
  232. }
  233. if (log_frame_len == 0)
  234. log_rev_state += 2;
  235. else
  236. log_rev_state++;
  237. log_rev_len = 0;
  238. log_state_len = 0;
  239. }
  240. break;
  241. case 5: //checksum receive
  242. log_frame_checksum = buf[i];
  243. if (log_frame_checksum != calc_log_checksum(log_frame_type, log_frame_subtype,
  244. log_frame_subtype_1, log_frame_len)) {
  245. printf("log frame checksum fail.\n");
  246. log_rev_state = 0;
  247. } else {
  248. log_rev_state++;
  249. }
  250. break;
  251. case 6: //data receive
  252. if (log_rev_len < FRAME_MAX_LEN)
  253. log_frame_buf[log_rev_len] = buf[i];
  254. if (++log_rev_len == log_frame_len)
  255. log_rev_state++;
  256. break;
  257. case 7: //tail receive
  258. if (log_state_len == 0 && buf[i] == 0x5a) {
  259. log_state_len++;
  260. } else if (log_state_len == 1 && buf[i] == 0xff) {
  261. log_state_len++;
  262. } else if (log_state_len == 2 && buf[i] == 0xa5) {
  263. log_state_len++;
  264. } else if (log_state_len == 3 && buf[i] == 0xff) {
  265. RevLogFrameHandler(log_frame_type, log_frame_subtype, log_frame_subtype_1,
  266. log_frame_buf, log_frame_len);
  267. log_rev_state = 0;
  268. log_state_len = 0;
  269. } else {
  270. log_state_len = 0;
  271. }
  272. break;
  273. default:
  274. break;
  275. }
  276. }
  277. }
  278. void print_hex(unsigned char *data, int len, const char* tag)
  279. {
  280. unsigned long i, j, l;
  281. unsigned char tmp_str[140];
  282. unsigned char tmp_str1[10];
  283. for (i = 0; i < len; i += 16) {
  284. int n ;
  285. tmp_str[0] = '\0';
  286. n = i ;
  287. for (j = 0; j < 4; j++) {
  288. l = n % 16;
  289. if (l >= 10)
  290. tmp_str[3 - j] = (unsigned char)('A' + l - 10);
  291. else
  292. tmp_str[3 - j] = (unsigned char)(l + '0');
  293. n >>= 4 ;
  294. }
  295. tmp_str[4] = '\0';
  296. strcat((char *) tmp_str, ": ");
  297. /*
  298. Output the hex bytes
  299. */
  300. for (j = i; j < (i + 16); j ++) {
  301. int m ;
  302. if (j < len) {
  303. m = ((unsigned int)((unsigned char) * (data + j))) / 16 ;
  304. if (m >= 10)
  305. tmp_str1[0] = 'A' + (unsigned char) m - 10;
  306. else
  307. tmp_str1[0] = (unsigned char) m + '0';
  308. m = ((unsigned int)((unsigned char) * (data + j))) % 16 ;
  309. if (m >= 10)
  310. tmp_str1[1] = 'A' + (unsigned char) m - 10;
  311. else
  312. tmp_str1[1] = (unsigned char) m + '0';
  313. tmp_str1[2] = '\0';
  314. strcat((char *) tmp_str, (char *) tmp_str1);
  315. strcat((char *) tmp_str, " ");
  316. } else {
  317. strcat((char *) tmp_str, " ");
  318. }
  319. }
  320. strcat((char *) tmp_str, " ");
  321. l = strlen((char *) tmp_str);
  322. /* Output the ASCII bytes */
  323. for (j = i; j < (i + 16); j++) {
  324. if (j < len) {
  325. char c = * (data + j);
  326. if (c < ' ' || c > 'z') {
  327. c = '.';
  328. }
  329. tmp_str[l ++] = c;
  330. } else {
  331. tmp_str[l ++] = ' ';
  332. }
  333. }
  334. tmp_str[l ++] = '\r';
  335. tmp_str[l ++] = '\n';
  336. tmp_str[l ++] = '\0';
  337. printf("%s\r\n", (const char *) tmp_str);
  338. }
  339. }
  340. static void *tcp_server_handler(void *arg)
  341. {
  342. int domain = AF_INET;
  343. int type = SOCK_STREAM;
  344. int protocol = 0;
  345. int ret = -1;
  346. int nListenFd = -1;
  347. int port = *(int*)arg;
  348. struct sockaddr_in addr_in;
  349. int backlog = 5; // 默认是128
  350. int len = 0;
  351. char chBuffer[1024] = {0};
  352. int flags = 0;
  353. int nClientSocket = -1;
  354. int nMaxFd = -1;
  355. int i = 0;
  356. fd_set readfds;
  357. static const int kOn = 1;
  358. nListenFd = socket(domain, type, protocol);
  359. if (nListenFd < 0)
  360. {
  361. printf("socket failed ! errno[%d] err[%s]\n", errno, strerror(errno));
  362. return NULL;
  363. }
  364. setsockopt(nListenFd, SOL_SOCKET, SO_REUSEADDR, &kOn, sizeof(kOn));
  365. memset(&addr_in, 0, sizeof(struct sockaddr_in));
  366. addr_in.sin_family = AF_INET;
  367. addr_in.sin_port = htons(port); //htons的返回值是16位的网络字节序整型数 htons尾的字母s代表short
  368. addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
  369. ret = bind(nListenFd, (struct sockaddr *)(&addr_in), sizeof(struct sockaddr_in));
  370. if (ret < 0)
  371. {
  372. printf("bind failed ! errno[%d] err[%s]\n", errno, strerror(errno));
  373. close(nListenFd); //避免资源泄漏
  374. return NULL;
  375. }
  376. ret = listen(nListenFd, backlog);
  377. if (ret < 0)
  378. {
  379. printf("listen failed ! errno[%d] err[%s]\n", errno, strerror(errno));
  380. close(nListenFd); //避免资源泄漏
  381. return NULL;
  382. }
  383. nMaxFd = nListenFd;
  384. while (!g_exit)
  385. {
  386. struct timeval stuTime;
  387. int time_out_ms = 3000;
  388. int num = 0;
  389. FD_ZERO(&readfds);
  390. FD_SET(nListenFd, &readfds);
  391. if (nClientSocket != -1)
  392. FD_SET(nClientSocket, &readfds);
  393. memset(&stuTime, 0, sizeof(struct timeval));
  394. stuTime.tv_sec = time_out_ms / 1000;
  395. stuTime.tv_usec = 1000 * (time_out_ms % 1000);
  396. num = select(nMaxFd + 1, &readfds, NULL, NULL, &stuTime);
  397. if (num > 0)
  398. {
  399. if (nClientSocket != -1 && FD_ISSET(nClientSocket, &readfds))
  400. {
  401. len = recv(nClientSocket, chBuffer, sizeof(chBuffer), flags); //flags为0,阻塞模式
  402. if (len == 0)
  403. {
  404. printf("Recv ret:%d errno:%d\n", ret, errno);
  405. printf("recv error:%s\n", strerror(errno)); //server close or recv timeout
  406. printf("recv failed ! errno[%d] err[%s] len[%d]\n", errno, strerror(errno), len);
  407. close(nClientSocket);
  408. FD_CLR(nClientSocket, &readfds);
  409. nClientSocket = -1;
  410. }
  411. else if (len < 0)
  412. {
  413. if (!(errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
  414. {
  415. printf("recv error:%s\n", strerror(errno));
  416. printf("recv failed ! errno[%d] err[%s] len[%d]\n", errno, strerror(errno), len);
  417. close(nClientSocket);
  418. FD_CLR(nClientSocket, &readfds);
  419. nClientSocket = -1;
  420. }
  421. }
  422. else
  423. {
  424. //print_hex(chBuffer, sizeof(chBuffer), NULL);
  425. RevLogDataHandler(chBuffer, len);
  426. }
  427. }
  428. if (FD_ISSET(nListenFd, &readfds))
  429. {
  430. nClientSocket = accept(nListenFd, (struct sockaddr *)NULL, NULL); //阻塞模式
  431. if (nClientSocket < 0)
  432. {
  433. printf("accept failed ! errno[%d] err[%s]\n", errno, strerror(errno));
  434. //close(nListenFd); //避免资源泄漏
  435. break;
  436. }
  437. printf("new client nClientSocket[%d]\n", nClientSocket);
  438. if (port == LOG_REQ_PORT && log_start_req == 1) {
  439. logSendFrame(nClientSocket, 0x0a, 0x10, 0, &log_start_req, sizeof(log_start_req));
  440. log_start_req = 0;
  441. }
  442. if (nClientSocket > nMaxFd)
  443. nMaxFd = nClientSocket;
  444. if (port == LOG_REQ_PORT)
  445. log_req_socket = nClientSocket;
  446. else if (port == LOG_TFR_PORT)
  447. log_tfr_socket = nClientSocket;
  448. }
  449. }
  450. else if (num == 0)
  451. {
  452. //printf("\n time out \n");
  453. //return 0;
  454. }
  455. else
  456. {
  457. printf("error \n");
  458. //return -1;
  459. }
  460. }
  461. printf("tcp_server_handler port=%d exit.\n", port);
  462. if (nClientSocket != -1)
  463. close(nClientSocket);
  464. close(nListenFd);
  465. return 0;
  466. }
  467. void exit_sighandler(int signo)
  468. {
  469. printf("exit_handler\n");
  470. g_exit = 1;
  471. }
  472. int main(int argc ,char *argv[])
  473. {
  474. int reqport = LOG_REQ_PORT;
  475. int tfrport = LOG_TFR_PORT;
  476. log_frame_buf = malloc(FRAME_MAX_LEN);
  477. if (!log_frame_buf) {
  478. printf("log_frame_buf malloc fail.\n");
  479. return -1;
  480. }
  481. signal(SIGTERM, exit_sighandler);
  482. signal(SIGINT, exit_sighandler);
  483. pthread_create(&req_thread_id, NULL, tcp_server_handler, &reqport);
  484. pthread_create(&tfr_thread_id, NULL, tcp_server_handler, &tfrport);
  485. pthread_join(req_thread_id, NULL);
  486. pthread_join(tfr_thread_id, NULL);
  487. free(log_frame_buf);
  488. return 0;
  489. }