sockutil.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. #include "precompile.h"
  2. #include "sockutil.h"
  3. #include "strutil.h"
  4. #include "dbgutil.h"
  5. //define replaced by cmake synatx
  6. //
  7. #if defined(_MSC_VER)
  8. #include <WinSock2.h>
  9. #pragma comment(lib, "ws2_32.lib")
  10. #endif //_MSC_VER
  11. TOOLKIT_API int winsock_init()
  12. {
  13. WSADATA wsaData;
  14. return WSAStartup(0x0202, &wsaData);
  15. }
  16. TOOLKIT_API int winsock_term()
  17. {
  18. return WSACleanup();
  19. }
  20. /*
  21. 1:require
  22. 0:option
  23. -1:forbid
  24. */
  25. static int is_legal_ip(char* content)
  26. {
  27. int len = 0;
  28. if (content == NULL) {
  29. return -1;
  30. }
  31. len = strlen(content);
  32. if (len < strlen("0.0.0.0")) {
  33. return -1;
  34. }
  35. //99开头行内办公网,10开头行内业务网。规范出自《招商银行总行网络规范汇编(2017年版).pdf》
  36. if (strstr(content, "99.") == content || strstr(content, "10.") == content) {
  37. return 1;
  38. }
  39. if (strcmp(content, "127.0.0.1") == 0 /*sipphone*/
  40. || strcmp(content, "2.0.0.1") == 0 /*ConnecotrFSM*/
  41. || strstr(content, "198.168.") == content /*sipphone 双网卡*/
  42. ) {
  43. return -1;
  44. }
  45. return 0;
  46. }
  47. TOOLKIT_API int toolkit_getlocalip(char* buf, int len_of_buf)
  48. {
  49. char bak_buf[64];
  50. #if defined(_MSC_VER)
  51. memset(bak_buf, 0, 64);
  52. struct hostent* ent = gethostbyname(NULL);
  53. if (ent && ent->h_addr_list[0] != NULL)
  54. {
  55. int i = 0;
  56. for (; ent->h_addr_list[i] != NULL; ++i)
  57. {
  58. char tmp[64];
  59. int r;
  60. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  61. memset(tmp, 0, 64);
  62. sprintf_s(tmp, 64, "%d.%d.%d.%d", in->S_un.S_un_b.s_b1, in->S_un.S_un_b.s_b2, in->S_un.S_un_b.s_b3, in->S_un.S_un_b.s_b4);
  63. r = is_legal_ip(tmp);
  64. if (r == 1) {
  65. memset(buf, 0, len_of_buf * sizeof(char));
  66. strcpy(buf, tmp);
  67. return 0;
  68. }
  69. else if (r == 0 && strlen(bak_buf) == 0) {
  70. strcpy(bak_buf, tmp);
  71. }
  72. }
  73. #else
  74. // Same implements with:
  75. // * DeviceSimulator
  76. // * SipPhone
  77. //
  78. int sockfd = -1;
  79. struct ifconf ifconf;
  80. struct ifreq* ifreq = NULL;
  81. char strbuf[256] = { 0 };
  82. int i;
  83. memset(bak_buf, 0, 64);
  84. ifconf.ifc_len = 256;
  85. ifconf.ifc_buf = strbuf;
  86. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
  87. if (ioctl(sockfd, SIOCGIFCONF, &ifconf) >= 0) { //get all socket info
  88. for (i = (ifconf.ifc_len / sizeof(struct ifreq)); i > 0; i--) {
  89. ifreq = (struct ifreq*)&ifconf.ifc_req[i];
  90. if (ifreq->ifr_addr.sa_family == AF_INET) { //for ipv4
  91. struct sockaddr_in* addr = (struct sockaddr_in*)&ifreq->ifr_addr;
  92. char tmp[INET_ADDRSTRLEN];
  93. int r = 0;
  94. memset(tmp, 0, sizeof(tmp));
  95. inet_ntop(AF_INET, &addr->sin_addr, tmp, INET_ADDRSTRLEN);
  96. r = is_legal_ip(tmp);
  97. if (r == 1) {
  98. memset(buf, 0, len_of_buf * sizeof(char));
  99. strcpy(buf, tmp);
  100. close(sockfd);
  101. return 0;
  102. }
  103. else if (r == 0 && strlen(bak_buf) == 0) {
  104. strcpy(bak_buf, tmp);
  105. }
  106. }
  107. }
  108. }
  109. close(sockfd);
  110. #endif //_MSC_VER
  111. if (strlen(bak_buf) > 0) {
  112. memset(buf, 0, len_of_buf * sizeof(char));
  113. strcpy(buf, bak_buf);
  114. return 0;
  115. }
  116. }
  117. return -1;
  118. }
  119. // < 0 : error
  120. // < n : timeout
  121. // ==n : ok
  122. TOOLKIT_API int tsend_n(int fd, const char *buf, int n, long timeout/* = -1 */)
  123. {
  124. struct timeval _tm;
  125. struct timeval *tm;
  126. int offset = 0;
  127. int left = n;
  128. DWORD dwLast;
  129. tm = (timeout >= 0) ? &_tm : NULL;
  130. if (tm)
  131. dwLast = GetTickCount();
  132. while (left > 0 && (timeout == -1 || timeout > 0)) {
  133. int t = send(fd, buf+offset, left, 0);
  134. if (t == 0) {
  135. return offset;
  136. } else if (t < 0) {
  137. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  138. int rc;
  139. fd_set wr_fs;
  140. fd_set ex_fs;
  141. FD_ZERO(&wr_fs);
  142. FD_ZERO(&ex_fs);
  143. FD_SET(fd, &wr_fs);
  144. FD_SET(fd, &ex_fs);
  145. if (tm) {
  146. DWORD dwNow = GetTickCount();
  147. long diff = (long)(dwNow - dwLast);
  148. if (timeout >= diff) {
  149. timeout -= diff;
  150. } else {
  151. timeout = 0;
  152. }
  153. tm->tv_sec = timeout/1000;
  154. tm->tv_usec = timeout%1000;
  155. dwLast = dwNow;
  156. }
  157. rc = select(1, NULL, &wr_fs, &ex_fs, tm);
  158. if (rc < 0)
  159. return -1;
  160. if (rc == 0)
  161. return offset;
  162. if (FD_ISSET(fd, &ex_fs))
  163. return -1;
  164. TOOLKIT_ASSERT(FD_ISSET(fd, &wr_fs));
  165. } else {
  166. return -1;
  167. }
  168. } else { // (t > 0)
  169. offset += t;
  170. left -= t;
  171. }
  172. }
  173. return offset;
  174. }
  175. TOOLKIT_API int trecv_n(int fd, char *buf, size_t n, long timeout/* = -1*/)
  176. {
  177. struct timeval _tm;
  178. struct timeval *tm;
  179. int offset = 0;
  180. int left = n;
  181. DWORD dwLast;
  182. tm = (timeout >= 0) ? &_tm : NULL;
  183. if (tm)
  184. dwLast = GetTickCount();
  185. while (left > 0 && (timeout == -1 || timeout > 0)) {
  186. int t = recv(fd, buf+offset, left, 0);
  187. if (t == 0) {
  188. return offset;
  189. } else if (t < 0) {
  190. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  191. int rc;
  192. fd_set rd_fs;
  193. fd_set ex_fs;
  194. FD_ZERO(&rd_fs);
  195. FD_ZERO(&ex_fs);
  196. FD_SET(fd, &rd_fs);
  197. FD_SET(fd, &ex_fs);
  198. if (tm) {
  199. DWORD dwNow = GetTickCount();
  200. long diff = (long)(dwNow - dwLast);
  201. if (timeout >= diff) {
  202. timeout -= diff;
  203. } else {
  204. timeout = 0;
  205. }
  206. tm->tv_sec = timeout/1000;
  207. tm->tv_usec = timeout%1000;
  208. dwLast = dwNow;
  209. }
  210. rc = select(1, &rd_fs, NULL, &ex_fs, tm);
  211. if (rc < 0)
  212. return -1;
  213. if (rc == 0)
  214. return offset;
  215. if (FD_ISSET(fd, &ex_fs))
  216. return -1;
  217. TOOLKIT_ASSERT(FD_ISSET(fd, &rd_fs));
  218. } else {
  219. return -1;
  220. }
  221. } else {
  222. offset += t;
  223. left -= t;
  224. }
  225. }
  226. return offset;
  227. }
  228. TOOLKIT_API int trecv_until(int fd, char *buf, int n, const char *delimer, int *header_len, long timeout)
  229. {
  230. struct timeval _tm;
  231. struct timeval *tm;
  232. int offset = 0;
  233. int left = n;
  234. DWORD dwLast;
  235. TOOLKIT_ASSERT(delimer);
  236. *header_len = 0;
  237. tm = (timeout >= 0) ? &_tm : NULL;
  238. if (tm)
  239. dwLast = GetTickCount();
  240. while (left > 0 && (timeout == -1 || timeout > 0)) {
  241. int t = recv(fd, buf+offset, left, 0);
  242. if (t == 0) {
  243. *header_len = 0;
  244. return offset;
  245. } else if (t < 0) {
  246. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  247. int rc;
  248. fd_set rd_fs;
  249. fd_set ex_fs;
  250. FD_ZERO(&rd_fs);
  251. FD_ZERO(&ex_fs);
  252. FD_SET(fd, &rd_fs);
  253. FD_SET(fd, &ex_fs);
  254. if (tm) {
  255. DWORD dwNow = GetTickCount();
  256. long diff = (long)(dwNow - dwLast);
  257. if (timeout >= diff) {
  258. timeout -= diff;
  259. } else {
  260. timeout = 0;
  261. }
  262. tm->tv_sec = timeout/1000;
  263. tm->tv_usec = timeout%1000;
  264. dwLast = dwNow;
  265. }
  266. rc = select(1, &rd_fs, NULL, &ex_fs, tm);
  267. if (rc < 0)
  268. return -1;
  269. if (rc == 0)
  270. return offset;
  271. if (FD_ISSET(fd, &ex_fs))
  272. return -1;
  273. TOOLKIT_ASSERT(FD_ISSET(fd, &rd_fs));
  274. } else {
  275. return -1;
  276. }
  277. } else {
  278. const char *p;
  279. offset += t;
  280. left -= t;
  281. p = memstr(buf, offset, delimer);
  282. if (p) {
  283. p += strlen(delimer);
  284. *header_len = p - buf;
  285. return offset;
  286. }
  287. }
  288. }
  289. return offset;
  290. }
  291. TOOLKIT_API int tsend_until(int fd, const char *buf, int n, const char *delimer, long timeout)
  292. {
  293. const char *p;
  294. TOOLKIT_ASSERT(delimer);
  295. p = memstr(buf, n, delimer);
  296. if (!p)
  297. return -1;
  298. p += strlen(delimer);
  299. return tsend_n(fd, buf, p - buf, timeout);
  300. }
  301. #ifndef SIO_UDP_CONNRESET
  302. #define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
  303. #endif
  304. TOOLKIT_API int disable_udp_connreset(SOCKET so)
  305. {
  306. DWORD dwBytesReturned = 0;
  307. BOOL bNewBehavior = FALSE;
  308. return WSAIoctl(so, SIO_UDP_CONNRESET, &bNewBehavior, sizeof(bNewBehavior),
  309. NULL, 0, &dwBytesReturned, NULL, NULL);
  310. }
  311. TOOLKIT_API int tsend_n_v(int fd, LPWSABUF lpBuffers, DWORD dwBufferCount, long timeout)
  312. {
  313. struct timeval _tm;
  314. struct timeval *tm;
  315. int sendedbufs = 0;
  316. int leftbufs = (int)dwBufferCount;
  317. int offset = 0;
  318. int sendedbytes = 0;
  319. DWORD dwLast;
  320. tm = (timeout >= 0) ? &_tm : NULL;
  321. if (tm)
  322. dwLast = GetTickCount();
  323. while (leftbufs > 0 && (timeout == -1 || timeout > 0)) {
  324. DWORD dwSent = 0;
  325. int rc = WSASend(fd, lpBuffers+sendedbufs, leftbufs, &dwSent, 0, NULL, NULL);
  326. if (rc < 0) {
  327. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  328. int rc;
  329. fd_set wr_fs;
  330. fd_set ex_fs;
  331. FD_ZERO(&wr_fs);
  332. FD_ZERO(&ex_fs);
  333. FD_SET(fd, &wr_fs);
  334. FD_SET(fd, &ex_fs);
  335. if (tm) {
  336. DWORD dwNow = GetTickCount();
  337. long diff = (long)(dwNow - dwLast);
  338. if (timeout >= diff) {
  339. timeout -= diff;
  340. } else {
  341. timeout = 0;
  342. }
  343. tm->tv_sec = timeout/1000;
  344. tm->tv_usec = timeout%1000;
  345. dwLast = dwNow;
  346. }
  347. rc = select(1, NULL, &wr_fs, &ex_fs, tm);
  348. if (rc < 0)
  349. return -1;
  350. if (rc == 0)
  351. return sendedbytes;
  352. if (FD_ISSET(fd, &ex_fs))
  353. return -1;
  354. TOOLKIT_ASSERT(FD_ISSET(fd, &wr_fs));
  355. } else {
  356. return -1;
  357. }
  358. } else { // (t > 0)
  359. if (dwSent == 0)
  360. return sendedbytes;
  361. sendedbytes += dwSent;
  362. while (dwSent > 0) {
  363. if (dwSent >= (lpBuffers[sendedbufs].len-offset)) {
  364. dwSent -= lpBuffers[sendedbufs].len-offset;
  365. sendedbufs++;
  366. leftbufs--;
  367. offset = 0;
  368. } else {
  369. lpBuffers[sendedbufs].buf += dwSent;
  370. lpBuffers[sendedbufs].len -= dwSent;
  371. dwSent = 0;
  372. }
  373. }
  374. }
  375. }
  376. return sendedbytes;
  377. }
  378. TOOLKIT_API int trecv_n_v(int fd, LPWSABUF lpBuffers, DWORD dwBufferCount, long timeout)
  379. {
  380. struct timeval _tm;
  381. struct timeval *tm;
  382. int recvedbufs = 0;
  383. int leftbufs = (int)dwBufferCount;
  384. int offset = 0;
  385. int recvedbytes = 0;
  386. DWORD dwLast;
  387. tm = (timeout >= 0) ? &_tm : NULL;
  388. if (tm)
  389. dwLast = GetTickCount();
  390. while (leftbufs > 0 && (timeout == -1 || timeout > 0)) {
  391. DWORD dwRecv = 0;
  392. int rc = WSARecv(fd, lpBuffers+recvedbufs, leftbufs, &dwRecv, 0, NULL, NULL);
  393. if (rc < 0) {
  394. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  395. int rc;
  396. fd_set rd_fs;
  397. fd_set ex_fs;
  398. FD_ZERO(&rd_fs);
  399. FD_ZERO(&ex_fs);
  400. FD_SET(fd, &rd_fs);
  401. FD_SET(fd, &ex_fs);
  402. if (tm) {
  403. DWORD dwNow = GetTickCount();
  404. long diff = (long)(dwNow - dwLast);
  405. if (timeout >= diff) {
  406. timeout -= diff;
  407. } else {
  408. timeout = 0;
  409. }
  410. tm->tv_sec = timeout/1000;
  411. tm->tv_usec = timeout%1000;
  412. dwLast = dwNow;
  413. }
  414. rc = select(1, &rd_fs, NULL, &ex_fs, tm);
  415. if (rc < 0)
  416. return -1;
  417. if (rc == 0)
  418. return recvedbytes;
  419. if (FD_ISSET(fd, &ex_fs))
  420. return -1;
  421. TOOLKIT_ASSERT(FD_ISSET(fd, &rd_fs));
  422. } else {
  423. return -1;
  424. }
  425. } else { // (t > 0)
  426. if (dwRecv == 0)
  427. return recvedbytes;
  428. recvedbytes += dwRecv;
  429. while (dwRecv > 0) {
  430. if (dwRecv >= (lpBuffers[recvedbufs].len-offset)) {
  431. dwRecv -= lpBuffers[recvedbufs].len-offset;
  432. recvedbufs++;
  433. leftbufs--;
  434. offset = 0;
  435. } else {
  436. lpBuffers[recvedbufs].buf += dwRecv;
  437. lpBuffers[recvedbufs].len -= dwRecv;
  438. dwRecv = 0;
  439. }
  440. }
  441. }
  442. }
  443. return recvedbytes;
  444. }
  445. TOOLKIT_API int tsendto(int fd, const char *buf, int n, const struct sockaddr* to, int tolen, long timeout)
  446. {
  447. struct timeval _tm;
  448. struct timeval *tm;
  449. DWORD dwLast;
  450. tm = (timeout >= 0) ? &_tm : NULL;
  451. if (tm)
  452. dwLast = GetTickCount();
  453. do {
  454. int rc = sendto(fd, buf, n, 0, to, tolen);
  455. if (rc >= 0)
  456. return rc;
  457. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  458. int rc;
  459. fd_set wr_fs;
  460. fd_set ex_fs;
  461. FD_ZERO(&wr_fs);
  462. FD_ZERO(&ex_fs);
  463. FD_SET(fd, &wr_fs);
  464. FD_SET(fd, &ex_fs);
  465. if (tm) {
  466. DWORD dwNow = GetTickCount();
  467. long diff = (long)(dwNow - dwLast);
  468. if (timeout >= diff) {
  469. timeout -= diff;
  470. } else {
  471. timeout = 0;
  472. }
  473. tm->tv_sec = timeout/1000;
  474. tm->tv_usec = timeout%1000;
  475. dwLast = dwNow;
  476. }
  477. rc = select(1, NULL, &wr_fs, &ex_fs, tm);
  478. if (rc <= 0)
  479. break;
  480. if (FD_ISSET(fd, &ex_fs))
  481. break;
  482. TOOLKIT_ASSERT(FD_ISSET(fd, &wr_fs));
  483. } else {
  484. break;
  485. }
  486. } while (timeout == -1 || timeout > 0);
  487. return -1;
  488. }
  489. TOOLKIT_API int trecvfrom(int fd, char *buf, int n, struct sockaddr* fromaddr, int *fromlen, long timeout)
  490. {
  491. struct timeval _tm;
  492. struct timeval *tm;
  493. DWORD dwLast;
  494. tm = (timeout >= 0) ? &_tm : NULL;
  495. if (tm)
  496. dwLast = GetTickCount();
  497. do {
  498. int rc = recvfrom(fd, buf, n, 0, fromaddr, fromlen);
  499. if (rc >= 0)
  500. return rc;
  501. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  502. int rc;
  503. fd_set rd_fs;
  504. fd_set ex_fs;
  505. FD_ZERO(&rd_fs);
  506. FD_ZERO(&ex_fs);
  507. FD_SET(fd, &rd_fs);
  508. FD_SET(fd, &ex_fs);
  509. if (tm) {
  510. DWORD dwNow = GetTickCount();
  511. long diff = (long)(dwNow - dwLast);
  512. if (timeout >= diff) {
  513. timeout -= diff;
  514. } else {
  515. timeout = 0;
  516. }
  517. tm->tv_sec = timeout/1000;
  518. tm->tv_usec = timeout%1000;
  519. dwLast = dwNow;
  520. }
  521. rc = select(1, &rd_fs, NULL, &ex_fs, tm);
  522. if (rc <= 0)
  523. break;
  524. if (FD_ISSET(fd, &ex_fs))
  525. break;
  526. TOOLKIT_ASSERT(FD_ISSET(fd, &rd_fs));
  527. } else {
  528. break;
  529. }
  530. } while (timeout == -1 || timeout > 0);
  531. return -1;
  532. }