sockutil.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef __SOCKUTIL_H__
  2. #define __SOCKUTIL_H__
  3. #pragma once
  4. #include "config.h"
  5. #include <winpr/wtypes.h>
  6. #include <winpr/winsock.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. TOOLKIT_API int winsock_init();
  11. TOOLKIT_API int winsock_term();
  12. TOOLKIT_API char* get_local_ip(char *buf, int n);
  13. // < 0 : error
  14. // < n : timeout
  15. // ==n : ok
  16. TOOLKIT_API int tsend_n(int fd, const char *buf, int n, long timeout/* = -1 */);
  17. TOOLKIT_API int trecv_n(int fd, char *buf, size_t n, long timeout/* = -1*/);
  18. // > 0 && header_len == 0, recv bytes, but not encounter delimer
  19. // > 0 && header_len > 0, ok
  20. // < 0 : error
  21. TOOLKIT_API int trecv_until(int fd, char *buf, int n, const char *delimer, int *header_len, long timeout);
  22. TOOLKIT_API int tsend_until(int fd, const char *buf, int n, const char *delimer, long timeout);
  23. TOOLKIT_API int tsend_n_v(int fd, LPWSABUF lpBuffers, DWORD dwBufferCount, long timeout);
  24. TOOLKIT_API int trecv_n_v(int fd, LPWSABUF lpBuffers, DWORD dwBufferCount, long timeout);
  25. static int tsend_n_v2(int fd,
  26. const char *buf1, int n1,
  27. const char *buf2, int n2,
  28. long timeout)
  29. {
  30. WSABUF wbuf[2] =
  31. {
  32. {(ULONG)n1, (char*)buf1},
  33. {(ULONG)n2, (char*)buf2},
  34. };
  35. return tsend_n_v(fd, &wbuf[0], 2, timeout);
  36. }
  37. static int tsend_n_v3(int fd,
  38. const char *buf1, int n1,
  39. const char *buf2, int n2,
  40. const char *buf3, int n3,
  41. long timeout)
  42. {
  43. WSABUF wbuf[3] =
  44. {
  45. {(ULONG)n1, (char*)buf1},
  46. {(ULONG)n2, (char*)buf2},
  47. {(ULONG)n3, (char*)buf3},
  48. };
  49. return tsend_n_v(fd, &wbuf[0], 3, timeout);
  50. }
  51. static int trecv_n_v2(int fd,
  52. char *buf1, int n1,
  53. char *buf2, int n2,
  54. long timeout)
  55. {
  56. WSABUF wbuf[2] =
  57. {
  58. {(ULONG)n1, (char*)buf1},
  59. {(ULONG)n2, (char*)buf2},
  60. };
  61. return trecv_n_v(fd, &wbuf[0], 2, timeout);
  62. }
  63. static int trecv_n_v3(int fd,
  64. char *buf1, int n1,
  65. char *buf2, int n2,
  66. char *buf3, int n3,
  67. long timeout)
  68. {
  69. WSABUF wbuf[3] =
  70. {
  71. {(ULONG)n1, (char*)buf1},
  72. {(ULONG)n2, (char*)buf2},
  73. {(ULONG)n3, (char*)buf3},
  74. };
  75. return trecv_n_v(fd, &wbuf[0], 3, timeout);
  76. }
  77. static int nonblock_sock(SOCKET sock)
  78. {
  79. unsigned long v = 1;
  80. return _ioctlsocket(sock, FIONBIO, &v);
  81. }
  82. static int reuse_addr(SOCKET sock)
  83. {
  84. BOOL reuseaddr = 1;
  85. return setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&reuseaddr, sizeof(reuseaddr));
  86. }
  87. TOOLKIT_API int disable_udp_connreset(SOCKET sock);
  88. TOOLKIT_API int tsendto(int fd, const char *buf, int n, const struct sockaddr* to, int tolen, long timeout);
  89. TOOLKIT_API int trecvfrom(int fd, char *buf, int n, struct sockaddr* fromaddr, int *fromlen, long timeout);
  90. #ifdef __cplusplus
  91. } // extern "C" {
  92. #endif
  93. #endif //__SOCKUTIL_H__