sockutil.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. #pragma comment(lib, "ws2_32.lib")
  9. #endif //_MSC_VER
  10. TOOLKIT_API int winsock_init()
  11. {
  12. WSADATA wsaData;
  13. return WSAStartup(0x0202, &wsaData);
  14. }
  15. TOOLKIT_API int winsock_term()
  16. {
  17. return WSACleanup();
  18. }
  19. TOOLKIT_API char* get_local_ip(char *buf, int n)
  20. {
  21. char name[64];
  22. struct hostent * he;
  23. gethostname(name, sizeof(name));
  24. he = gethostbyname(name);
  25. if (he) {
  26. if (he->h_addr_list[0]) {
  27. return strncpy(buf, inet_ntoa(*(struct in_addr*)he->h_addr_list[0]), n);
  28. }
  29. }
  30. return NULL;
  31. }
  32. // < 0 : error
  33. // < n : timeout
  34. // ==n : ok
  35. TOOLKIT_API int tsend_n(int fd, const char *buf, int n, long timeout/* = -1 */)
  36. {
  37. struct timeval _tm;
  38. struct timeval *tm;
  39. int offset = 0;
  40. int left = n;
  41. DWORD dwLast;
  42. tm = (timeout >= 0) ? &_tm : NULL;
  43. if (tm)
  44. dwLast = GetTickCount();
  45. while (left > 0 && (timeout == -1 || timeout > 0)) {
  46. int t = send(fd, buf+offset, left, 0);
  47. if (t == 0) {
  48. return offset;
  49. } else if (t < 0) {
  50. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  51. int rc;
  52. fd_set wr_fs;
  53. fd_set ex_fs;
  54. FD_ZERO(&wr_fs);
  55. FD_ZERO(&ex_fs);
  56. FD_SET(fd, &wr_fs);
  57. FD_SET(fd, &ex_fs);
  58. if (tm) {
  59. DWORD dwNow = GetTickCount();
  60. long diff = (long)(dwNow - dwLast);
  61. if (timeout >= diff) {
  62. timeout -= diff;
  63. } else {
  64. timeout = 0;
  65. }
  66. tm->tv_sec = timeout/1000;
  67. tm->tv_usec = timeout%1000;
  68. dwLast = dwNow;
  69. }
  70. rc = select(1, NULL, &wr_fs, &ex_fs, tm);
  71. if (rc < 0)
  72. return -1;
  73. if (rc == 0)
  74. return offset;
  75. if (FD_ISSET(fd, &ex_fs))
  76. return -1;
  77. TOOLKIT_ASSERT(FD_ISSET(fd, &wr_fs));
  78. } else {
  79. return -1;
  80. }
  81. } else { // (t > 0)
  82. offset += t;
  83. left -= t;
  84. }
  85. }
  86. return offset;
  87. }
  88. TOOLKIT_API int trecv_n(int fd, char *buf, size_t n, long timeout/* = -1*/)
  89. {
  90. struct timeval _tm;
  91. struct timeval *tm;
  92. int offset = 0;
  93. int left = n;
  94. DWORD dwLast;
  95. tm = (timeout >= 0) ? &_tm : NULL;
  96. if (tm)
  97. dwLast = GetTickCount();
  98. while (left > 0 && (timeout == -1 || timeout > 0)) {
  99. int t = recv(fd, buf+offset, left, 0);
  100. if (t == 0) {
  101. return offset;
  102. } else if (t < 0) {
  103. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  104. int rc;
  105. fd_set rd_fs;
  106. fd_set ex_fs;
  107. FD_ZERO(&rd_fs);
  108. FD_ZERO(&ex_fs);
  109. FD_SET(fd, &rd_fs);
  110. FD_SET(fd, &ex_fs);
  111. if (tm) {
  112. DWORD dwNow = GetTickCount();
  113. long diff = (long)(dwNow - dwLast);
  114. if (timeout >= diff) {
  115. timeout -= diff;
  116. } else {
  117. timeout = 0;
  118. }
  119. tm->tv_sec = timeout/1000;
  120. tm->tv_usec = timeout%1000;
  121. dwLast = dwNow;
  122. }
  123. rc = select(1, &rd_fs, NULL, &ex_fs, tm);
  124. if (rc < 0)
  125. return -1;
  126. if (rc == 0)
  127. return offset;
  128. if (FD_ISSET(fd, &ex_fs))
  129. return -1;
  130. TOOLKIT_ASSERT(FD_ISSET(fd, &rd_fs));
  131. } else {
  132. return -1;
  133. }
  134. } else {
  135. offset += t;
  136. left -= t;
  137. }
  138. }
  139. return offset;
  140. }
  141. TOOLKIT_API int trecv_until(int fd, char *buf, int n, const char *delimer, int *header_len, long timeout)
  142. {
  143. struct timeval _tm;
  144. struct timeval *tm;
  145. int offset = 0;
  146. int left = n;
  147. DWORD dwLast;
  148. TOOLKIT_ASSERT(delimer);
  149. *header_len = 0;
  150. tm = (timeout >= 0) ? &_tm : NULL;
  151. if (tm)
  152. dwLast = GetTickCount();
  153. while (left > 0 && (timeout == -1 || timeout > 0)) {
  154. int t = recv(fd, buf+offset, left, 0);
  155. if (t == 0) {
  156. *header_len = 0;
  157. return offset;
  158. } else if (t < 0) {
  159. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  160. int rc;
  161. fd_set rd_fs;
  162. fd_set ex_fs;
  163. FD_ZERO(&rd_fs);
  164. FD_ZERO(&ex_fs);
  165. FD_SET(fd, &rd_fs);
  166. FD_SET(fd, &ex_fs);
  167. if (tm) {
  168. DWORD dwNow = GetTickCount();
  169. long diff = (long)(dwNow - dwLast);
  170. if (timeout >= diff) {
  171. timeout -= diff;
  172. } else {
  173. timeout = 0;
  174. }
  175. tm->tv_sec = timeout/1000;
  176. tm->tv_usec = timeout%1000;
  177. dwLast = dwNow;
  178. }
  179. rc = select(1, &rd_fs, NULL, &ex_fs, tm);
  180. if (rc < 0)
  181. return -1;
  182. if (rc == 0)
  183. return offset;
  184. if (FD_ISSET(fd, &ex_fs))
  185. return -1;
  186. TOOLKIT_ASSERT(FD_ISSET(fd, &rd_fs));
  187. } else {
  188. return -1;
  189. }
  190. } else {
  191. const char *p;
  192. offset += t;
  193. left -= t;
  194. p = memstr(buf, offset, delimer);
  195. if (p) {
  196. p += strlen(delimer);
  197. *header_len = p - buf;
  198. return offset;
  199. }
  200. }
  201. }
  202. return offset;
  203. }
  204. TOOLKIT_API int tsend_until(int fd, const char *buf, int n, const char *delimer, long timeout)
  205. {
  206. const char *p;
  207. TOOLKIT_ASSERT(delimer);
  208. p = memstr(buf, n, delimer);
  209. if (!p)
  210. return -1;
  211. p += strlen(delimer);
  212. return tsend_n(fd, buf, p - buf, timeout);
  213. }
  214. #ifndef SIO_UDP_CONNRESET
  215. #define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
  216. #endif
  217. TOOLKIT_API int disable_udp_connreset(SOCKET so)
  218. {
  219. DWORD dwBytesReturned = 0;
  220. BOOL bNewBehavior = FALSE;
  221. return WSAIoctl(so, SIO_UDP_CONNRESET, &bNewBehavior, sizeof(bNewBehavior),
  222. NULL, 0, &dwBytesReturned, NULL, NULL);
  223. }
  224. TOOLKIT_API int tsend_n_v(int fd, LPWSABUF lpBuffers, DWORD dwBufferCount, long timeout)
  225. {
  226. struct timeval _tm;
  227. struct timeval *tm;
  228. int sendedbufs = 0;
  229. int leftbufs = (int)dwBufferCount;
  230. int offset = 0;
  231. int sendedbytes = 0;
  232. DWORD dwLast;
  233. tm = (timeout >= 0) ? &_tm : NULL;
  234. if (tm)
  235. dwLast = GetTickCount();
  236. while (leftbufs > 0 && (timeout == -1 || timeout > 0)) {
  237. DWORD dwSent = 0;
  238. int rc = WSASend(fd, lpBuffers+sendedbufs, leftbufs, &dwSent, 0, NULL, NULL);
  239. if (rc < 0) {
  240. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  241. int rc;
  242. fd_set wr_fs;
  243. fd_set ex_fs;
  244. FD_ZERO(&wr_fs);
  245. FD_ZERO(&ex_fs);
  246. FD_SET(fd, &wr_fs);
  247. FD_SET(fd, &ex_fs);
  248. if (tm) {
  249. DWORD dwNow = GetTickCount();
  250. long diff = (long)(dwNow - dwLast);
  251. if (timeout >= diff) {
  252. timeout -= diff;
  253. } else {
  254. timeout = 0;
  255. }
  256. tm->tv_sec = timeout/1000;
  257. tm->tv_usec = timeout%1000;
  258. dwLast = dwNow;
  259. }
  260. rc = select(1, NULL, &wr_fs, &ex_fs, tm);
  261. if (rc < 0)
  262. return -1;
  263. if (rc == 0)
  264. return sendedbytes;
  265. if (FD_ISSET(fd, &ex_fs))
  266. return -1;
  267. TOOLKIT_ASSERT(FD_ISSET(fd, &wr_fs));
  268. } else {
  269. return -1;
  270. }
  271. } else { // (t > 0)
  272. if (dwSent == 0)
  273. return sendedbytes;
  274. sendedbytes += dwSent;
  275. while (dwSent > 0) {
  276. if (dwSent >= (lpBuffers[sendedbufs].len-offset)) {
  277. dwSent -= lpBuffers[sendedbufs].len-offset;
  278. sendedbufs++;
  279. leftbufs--;
  280. offset = 0;
  281. } else {
  282. lpBuffers[sendedbufs].buf += dwSent;
  283. lpBuffers[sendedbufs].len -= dwSent;
  284. dwSent = 0;
  285. }
  286. }
  287. }
  288. }
  289. return sendedbytes;
  290. }
  291. TOOLKIT_API int trecv_n_v(int fd, LPWSABUF lpBuffers, DWORD dwBufferCount, long timeout)
  292. {
  293. struct timeval _tm;
  294. struct timeval *tm;
  295. int recvedbufs = 0;
  296. int leftbufs = (int)dwBufferCount;
  297. int offset = 0;
  298. int recvedbytes = 0;
  299. DWORD dwLast;
  300. tm = (timeout >= 0) ? &_tm : NULL;
  301. if (tm)
  302. dwLast = GetTickCount();
  303. while (leftbufs > 0 && (timeout == -1 || timeout > 0)) {
  304. DWORD dwRecv = 0;
  305. int rc = WSARecv(fd, lpBuffers+recvedbufs, leftbufs, &dwRecv, 0, NULL, NULL);
  306. if (rc < 0) {
  307. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  308. int rc;
  309. fd_set rd_fs;
  310. fd_set ex_fs;
  311. FD_ZERO(&rd_fs);
  312. FD_ZERO(&ex_fs);
  313. FD_SET(fd, &rd_fs);
  314. FD_SET(fd, &ex_fs);
  315. if (tm) {
  316. DWORD dwNow = GetTickCount();
  317. long diff = (long)(dwNow - dwLast);
  318. if (timeout >= diff) {
  319. timeout -= diff;
  320. } else {
  321. timeout = 0;
  322. }
  323. tm->tv_sec = timeout/1000;
  324. tm->tv_usec = timeout%1000;
  325. dwLast = dwNow;
  326. }
  327. rc = select(1, &rd_fs, NULL, &ex_fs, tm);
  328. if (rc < 0)
  329. return -1;
  330. if (rc == 0)
  331. return recvedbytes;
  332. if (FD_ISSET(fd, &ex_fs))
  333. return -1;
  334. TOOLKIT_ASSERT(FD_ISSET(fd, &rd_fs));
  335. } else {
  336. return -1;
  337. }
  338. } else { // (t > 0)
  339. if (dwRecv == 0)
  340. return recvedbytes;
  341. recvedbytes += dwRecv;
  342. while (dwRecv > 0) {
  343. if (dwRecv >= (lpBuffers[recvedbufs].len-offset)) {
  344. dwRecv -= lpBuffers[recvedbufs].len-offset;
  345. recvedbufs++;
  346. leftbufs--;
  347. offset = 0;
  348. } else {
  349. lpBuffers[recvedbufs].buf += dwRecv;
  350. lpBuffers[recvedbufs].len -= dwRecv;
  351. dwRecv = 0;
  352. }
  353. }
  354. }
  355. }
  356. return recvedbytes;
  357. }
  358. TOOLKIT_API int tsendto(int fd, const char *buf, int n, const struct sockaddr* to, int tolen, long timeout)
  359. {
  360. struct timeval _tm;
  361. struct timeval *tm;
  362. DWORD dwLast;
  363. tm = (timeout >= 0) ? &_tm : NULL;
  364. if (tm)
  365. dwLast = GetTickCount();
  366. do {
  367. int rc = sendto(fd, buf, n, 0, to, tolen);
  368. if (rc >= 0)
  369. return rc;
  370. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  371. int rc;
  372. fd_set wr_fs;
  373. fd_set ex_fs;
  374. FD_ZERO(&wr_fs);
  375. FD_ZERO(&ex_fs);
  376. FD_SET(fd, &wr_fs);
  377. FD_SET(fd, &ex_fs);
  378. if (tm) {
  379. DWORD dwNow = GetTickCount();
  380. long diff = (long)(dwNow - dwLast);
  381. if (timeout >= diff) {
  382. timeout -= diff;
  383. } else {
  384. timeout = 0;
  385. }
  386. tm->tv_sec = timeout/1000;
  387. tm->tv_usec = timeout%1000;
  388. dwLast = dwNow;
  389. }
  390. rc = select(1, NULL, &wr_fs, &ex_fs, tm);
  391. if (rc <= 0)
  392. break;
  393. if (FD_ISSET(fd, &ex_fs))
  394. break;
  395. TOOLKIT_ASSERT(FD_ISSET(fd, &wr_fs));
  396. } else {
  397. break;
  398. }
  399. } while (timeout == -1 || timeout > 0);
  400. return -1;
  401. }
  402. TOOLKIT_API int trecvfrom(int fd, char *buf, int n, struct sockaddr* fromaddr, int *fromlen, long timeout)
  403. {
  404. struct timeval _tm;
  405. struct timeval *tm;
  406. DWORD dwLast;
  407. tm = (timeout >= 0) ? &_tm : NULL;
  408. if (tm)
  409. dwLast = GetTickCount();
  410. do {
  411. int rc = recvfrom(fd, buf, n, 0, fromaddr, fromlen);
  412. if (rc >= 0)
  413. return rc;
  414. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  415. int rc;
  416. fd_set rd_fs;
  417. fd_set ex_fs;
  418. FD_ZERO(&rd_fs);
  419. FD_ZERO(&ex_fs);
  420. FD_SET(fd, &rd_fs);
  421. FD_SET(fd, &ex_fs);
  422. if (tm) {
  423. DWORD dwNow = GetTickCount();
  424. long diff = (long)(dwNow - dwLast);
  425. if (timeout >= diff) {
  426. timeout -= diff;
  427. } else {
  428. timeout = 0;
  429. }
  430. tm->tv_sec = timeout/1000;
  431. tm->tv_usec = timeout%1000;
  432. dwLast = dwNow;
  433. }
  434. rc = select(1, &rd_fs, NULL, &ex_fs, tm);
  435. if (rc <= 0)
  436. break;
  437. if (FD_ISSET(fd, &ex_fs))
  438. break;
  439. TOOLKIT_ASSERT(FD_ISSET(fd, &rd_fs));
  440. } else {
  441. break;
  442. }
  443. } while (timeout == -1 || timeout > 0);
  444. return -1;
  445. }