sockutil.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef NETWORK_SOCKUTIL_H
  11. #define NETWORK_SOCKUTIL_H
  12. #if defined(_WIN32)
  13. #include <winsock2.h>
  14. #include <ws2tcpip.h>
  15. #include <iphlpapi.h>
  16. #pragma comment (lib, "Ws2_32.lib")
  17. #pragma comment(lib,"Iphlpapi.lib")
  18. #else
  19. #include <netdb.h>
  20. #include <arpa/inet.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/socket.h>
  23. #include <net/if.h>
  24. #include <netinet/in.h>
  25. #include <netinet/tcp.h>
  26. #endif // defined(_WIN32)
  27. #include <cstring>
  28. #include <cstdint>
  29. #include <map>
  30. #include <vector>
  31. #include <string>
  32. namespace toolkit {
  33. #if defined(_WIN32)
  34. #ifndef socklen_t
  35. #define socklen_t int
  36. #endif //!socklen_t
  37. #ifndef SHUT_RDWR
  38. #define SHUT_RDWR 2
  39. #endif //!SHUT_RDWR
  40. int ioctl(int fd, long cmd, u_long *ptr);
  41. int close(int fd);
  42. #endif // defined(_WIN32)
  43. #define SOCKET_DEFAULT_BUF_SIZE (256 * 1024)
  44. #define TCP_KEEPALIVE_INTERVAL 30
  45. #define TCP_KEEPALIVE_PROBE_TIMES 9
  46. #define TCP_KEEPALIVE_TIME 120
  47. //套接字工具类,封装了socket、网络的一些基本操作
  48. class SockUtil {
  49. public:
  50. /**
  51. * 创建tcp客户端套接字并连接服务器
  52. * @param host 服务器ip或域名
  53. * @param port 服务器端口号
  54. * @param async 是否异步连接
  55. * @param local_ip 绑定的本地网卡ip
  56. * @param local_port 绑定的本地端口号
  57. * @return -1代表失败,其他为socket fd号
  58. */
  59. static int connect(const char *host, uint16_t port, bool async = true, const char *local_ip = "::", uint16_t local_port = 0);
  60. /**
  61. * 创建tcp监听套接字
  62. * @param port 监听的本地端口
  63. * @param local_ip 绑定的本地网卡ip
  64. * @param back_log accept列队长度
  65. * @return -1代表失败,其他为socket fd号
  66. */
  67. static int listen(const uint16_t port, const char *local_ip = "::", int back_log = 1024);
  68. /**
  69. * 创建udp套接字
  70. * @param port 监听的本地端口
  71. * @param local_ip 绑定的本地网卡ip
  72. * @param enable_reuse 是否允许重复bind端口
  73. * @return -1代表失败,其他为socket fd号
  74. */
  75. static int bindUdpSock(const uint16_t port, const char *local_ip = "::", bool enable_reuse = true);
  76. /**
  77. * @brief 解除与 sock 相关的绑定关系
  78. * @param sock, socket fd 号
  79. * @return 0 成功, -1 失败
  80. */
  81. static int dissolveUdpSock(int sock);
  82. /**
  83. * 开启TCP_NODELAY,降低TCP交互延时
  84. * @param fd socket fd号
  85. * @param on 是否开启
  86. * @return 0代表成功,-1为失败
  87. */
  88. static int setNoDelay(int fd, bool on = true);
  89. /**
  90. * 写socket不触发SIG_PIPE信号(貌似只有mac有效)
  91. * @param fd socket fd号
  92. * @return 0代表成功,-1为失败
  93. */
  94. static int setNoSigpipe(int fd);
  95. /**
  96. * 设置读写socket是否阻塞
  97. * @param fd socket fd号
  98. * @param noblock 是否阻塞
  99. * @return 0代表成功,-1为失败
  100. */
  101. static int setNoBlocked(int fd, bool noblock = true);
  102. /**
  103. * 设置socket接收缓存,默认貌似8K左右,一般有设置上限
  104. * 可以通过配置内核配置文件调整
  105. * @param fd socket fd号
  106. * @param size 接收缓存大小
  107. * @return 0代表成功,-1为失败
  108. */
  109. static int setRecvBuf(int fd, int size = SOCKET_DEFAULT_BUF_SIZE);
  110. /**
  111. * 设置socket接收缓存,默认貌似8K左右,一般有设置上限
  112. * 可以通过配置内核配置文件调整
  113. * @param fd socket fd号
  114. * @param size 接收缓存大小
  115. * @return 0代表成功,-1为失败
  116. */
  117. static int setSendBuf(int fd, int size = SOCKET_DEFAULT_BUF_SIZE);
  118. /**
  119. * 设置后续可绑定复用端口(处于TIME_WAITE状态)
  120. * @param fd socket fd号
  121. * @param on 是否开启该特性
  122. * @return 0代表成功,-1为失败
  123. */
  124. static int setReuseable(int fd, bool on = true, bool reuse_port = true);
  125. /**
  126. * 运行发送或接收udp广播信息
  127. * @param fd socket fd号
  128. * @param on 是否开启该特性
  129. * @return 0代表成功,-1为失败
  130. */
  131. static int setBroadcast(int fd, bool on = true);
  132. /**
  133. * 是否开启TCP KeepAlive特性
  134. * @param fd socket fd号
  135. * @param on 是否开启该特性
  136. * @param idle keepalive空闲时间
  137. * @param interval keepalive探测时间间隔
  138. * @param times keepalive探测次数
  139. * @return 0代表成功,-1为失败
  140. */
  141. static int setKeepAlive(int fd, bool on = true, int interval = TCP_KEEPALIVE_INTERVAL, int idle = TCP_KEEPALIVE_TIME, int times = TCP_KEEPALIVE_PROBE_TIMES);
  142. /**
  143. * 是否开启FD_CLOEXEC特性(多进程相关)
  144. * @param fd fd号,不一定是socket
  145. * @param on 是否开启该特性
  146. * @return 0代表成功,-1为失败
  147. */
  148. static int setCloExec(int fd, bool on = true);
  149. /**
  150. * 开启SO_LINGER特性
  151. * @param sock socket fd号
  152. * @param second 内核等待关闭socket超时时间,单位秒
  153. * @return 0代表成功,-1为失败
  154. */
  155. static int setCloseWait(int sock, int second = 0);
  156. /**
  157. * dns解析
  158. * @param host 域名或ip
  159. * @param port 端口号
  160. * @param addr sockaddr结构体
  161. * @return 是否成功
  162. */
  163. static bool getDomainIP(const char *host, uint16_t port, struct sockaddr_storage &addr, int ai_family = AF_INET,
  164. int ai_socktype = SOCK_STREAM, int ai_protocol = IPPROTO_TCP, int expire_sec = 60);
  165. /**
  166. * 设置组播ttl
  167. * @param sock socket fd号
  168. * @param ttl ttl值
  169. * @return 0代表成功,-1为失败
  170. */
  171. static int setMultiTTL(int sock, uint8_t ttl = 64);
  172. /**
  173. * 设置组播发送网卡
  174. * @param sock socket fd号
  175. * @param local_ip 本机网卡ip
  176. * @return 0代表成功,-1为失败
  177. */
  178. static int setMultiIF(int sock, const char *local_ip);
  179. /**
  180. * 设置是否接收本机发出的组播包
  181. * @param fd socket fd号
  182. * @param acc 是否接收
  183. * @return 0代表成功,-1为失败
  184. */
  185. static int setMultiLOOP(int fd, bool acc = false);
  186. /**
  187. * 加入组播
  188. * @param fd socket fd号
  189. * @param addr 组播地址
  190. * @param local_ip 本机网卡ip
  191. * @return 0代表成功,-1为失败
  192. */
  193. static int joinMultiAddr(int fd, const char *addr, const char *local_ip = "0.0.0.0");
  194. /**
  195. * 退出组播
  196. * @param fd socket fd号
  197. * @param addr 组播地址
  198. * @param local_ip 本机网卡ip
  199. * @return 0代表成功,-1为失败
  200. */
  201. static int leaveMultiAddr(int fd, const char *addr, const char *local_ip = "0.0.0.0");
  202. /**
  203. * 加入组播并只接受该源端的组播数据
  204. * @param sock socket fd号
  205. * @param addr 组播地址
  206. * @param src_ip 数据源端地址
  207. * @param local_ip 本机网卡ip
  208. * @return 0代表成功,-1为失败
  209. */
  210. static int joinMultiAddrFilter(int sock, const char *addr, const char *src_ip, const char *local_ip = "0.0.0.0");
  211. /**
  212. * 退出组播
  213. * @param fd socket fd号
  214. * @param addr 组播地址
  215. * @param src_ip 数据源端地址
  216. * @param local_ip 本机网卡ip
  217. * @return 0代表成功,-1为失败
  218. */
  219. static int leaveMultiAddrFilter(int fd, const char *addr, const char *src_ip, const char *local_ip = "0.0.0.0");
  220. /**
  221. * 获取该socket当前发生的错误
  222. * @param fd socket fd号
  223. * @return 错误代码
  224. */
  225. static int getSockError(int fd);
  226. /**
  227. * 获取网卡列表
  228. * @return vector<map<ip:name> >
  229. */
  230. static std::vector<std::map<std::string, std::string>> getInterfaceList();
  231. /**
  232. * 获取本机默认网卡ip
  233. */
  234. static std::string get_local_ip();
  235. /**
  236. * 获取该socket绑定的本地ip
  237. * @param sock socket fd号
  238. */
  239. static std::string get_local_ip(int sock);
  240. /**
  241. * 获取该socket绑定的本地端口
  242. * @param sock socket fd号
  243. */
  244. static uint16_t get_local_port(int sock);
  245. /**
  246. * 获取该socket绑定的远端ip
  247. * @param sock socket fd号
  248. */
  249. static std::string get_peer_ip(int sock);
  250. /**
  251. * 获取该socket绑定的远端端口
  252. * @param sock socket fd号
  253. */
  254. static uint16_t get_peer_port(int sock);
  255. static bool support_ipv6();
  256. /**
  257. * 线程安全的in_addr转ip字符串
  258. */
  259. static std::string inet_ntoa(const struct in_addr &addr);
  260. static std::string inet_ntoa(const struct in6_addr &addr);
  261. static std::string inet_ntoa(const struct sockaddr *addr);
  262. static uint16_t inet_port(const struct sockaddr *addr);
  263. static struct sockaddr_storage make_sockaddr(const char *ip, uint16_t port);
  264. static socklen_t get_sock_len(const struct sockaddr *addr);
  265. static bool get_sock_local_addr(int fd, struct sockaddr_storage &addr);
  266. static bool get_sock_peer_addr(int fd, struct sockaddr_storage &addr);
  267. /**
  268. * 获取网卡ip
  269. * @param if_name 网卡名
  270. */
  271. static std::string get_ifr_ip(const char *if_name);
  272. /**
  273. * 获取网卡名
  274. * @param local_op 网卡ip
  275. */
  276. static std::string get_ifr_name(const char *local_op);
  277. /**
  278. * 根据网卡名获取子网掩码
  279. * @param if_name 网卡名
  280. */
  281. static std::string get_ifr_mask(const char *if_name);
  282. /**
  283. * 根据网卡名获取广播地址
  284. * @param if_name 网卡名
  285. */
  286. static std::string get_ifr_brdaddr(const char *if_name);
  287. /**
  288. * 判断两个ip是否为同一网段
  289. * @param src_ip 我的ip
  290. * @param dts_ip 对方ip
  291. */
  292. static bool in_same_lan(const char *src_ip, const char *dts_ip);
  293. /**
  294. * 判断是否为ipv4地址
  295. */
  296. static bool is_ipv4(const char *str);
  297. /**
  298. * 判断是否为ipv6地址
  299. */
  300. static bool is_ipv6(const char *str);
  301. };
  302. } // namespace toolkit
  303. #endif // !NETWORK_SOCKUTIL_H