1
0

hsocket.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #include "hsocket.h"
  2. #include "hdef.h"
  3. #ifdef OS_WIN
  4. #include "hatomic.h"
  5. static hatomic_flag_t s_wsa_initialized = HATOMIC_FLAG_INIT;
  6. void WSAInit() {
  7. if (!hatomic_flag_test_and_set(&s_wsa_initialized)) {
  8. WSADATA wsadata;
  9. WSAStartup(MAKEWORD(2, 2), &wsadata);
  10. }
  11. }
  12. void WSADeinit() {
  13. if (hatomic_flag_test_and_set(&s_wsa_initialized)) {
  14. hatomic_flag_clear(&s_wsa_initialized);
  15. WSACleanup();
  16. }
  17. }
  18. #endif
  19. static inline int socket_errno_negative(int sockfd) {
  20. int err = socket_errno();
  21. if (sockfd >= 0) closesocket(sockfd);
  22. return err > 0 ? -err : -1;
  23. }
  24. const char* socket_strerror(int err) {
  25. #ifdef OS_WIN
  26. static char buffer[128];
  27. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
  28. FORMAT_MESSAGE_IGNORE_INSERTS |
  29. FORMAT_MESSAGE_MAX_WIDTH_MASK,
  30. 0, ABS(err), 0, buffer, sizeof(buffer), NULL);
  31. return buffer;
  32. #else
  33. return strerror(ABS(err));
  34. #endif
  35. }
  36. bool is_ipv4(const char* host) {
  37. struct sockaddr_in sin;
  38. return inet_pton(AF_INET, host, &sin) == 1;
  39. }
  40. bool is_ipv6(const char* host) {
  41. struct sockaddr_in6 sin6;
  42. return inet_pton(AF_INET6, host, &sin6) == 1;
  43. }
  44. int ResolveAddr(const char* host, sockaddr_u* addr) {
  45. #ifdef OS_WIN
  46. WSAInit();
  47. #endif
  48. if (inet_pton(AF_INET, host, &addr->sin.sin_addr) == 1) {
  49. addr->sa.sa_family = AF_INET; // host is ipv4, so easy ;)
  50. return 0;
  51. }
  52. if (inet_pton(AF_INET6, host, &addr->sin6.sin6_addr) == 1) {
  53. addr->sa.sa_family = AF_INET6; // host is ipv6
  54. }
  55. struct addrinfo* ais = NULL;
  56. int ret = getaddrinfo(host, NULL, NULL, &ais);
  57. if (ret != 0 || ais == NULL || ais->ai_addr == NULL || ais->ai_addrlen == 0) {
  58. printd("unknown host: %s err:%d:%s\n", host, ret, gai_strerror(ret));
  59. return ret;
  60. }
  61. struct addrinfo* pai = ais;
  62. while (pai != NULL) {
  63. if (pai->ai_family == AF_INET) break;
  64. pai = pai->ai_next;
  65. }
  66. if (pai == NULL) pai = ais;
  67. memcpy(addr, pai->ai_addr, pai->ai_addrlen);
  68. freeaddrinfo(ais);
  69. return 0;
  70. }
  71. const char* sockaddr_ip(sockaddr_u* addr, char *ip, int len) {
  72. if (addr->sa.sa_family == AF_INET) {
  73. return inet_ntop(AF_INET, &addr->sin.sin_addr, ip, len);
  74. }
  75. else if (addr->sa.sa_family == AF_INET6) {
  76. return inet_ntop(AF_INET6, &addr->sin6.sin6_addr, ip, len);
  77. }
  78. return ip;
  79. }
  80. uint16_t sockaddr_port(sockaddr_u* addr) {
  81. uint16_t port = 0;
  82. if (addr->sa.sa_family == AF_INET) {
  83. port = ntohs(addr->sin.sin_port);
  84. }
  85. else if (addr->sa.sa_family == AF_INET6) {
  86. port = ntohs(addr->sin6.sin6_port);
  87. }
  88. return port;
  89. }
  90. int sockaddr_set_ip(sockaddr_u* addr, const char* host) {
  91. if (!host || *host == '\0') {
  92. addr->sin.sin_family = AF_INET;
  93. addr->sin.sin_addr.s_addr = htonl(INADDR_ANY);
  94. return 0;
  95. }
  96. return ResolveAddr(host, addr);
  97. }
  98. void sockaddr_set_port(sockaddr_u* addr, int port) {
  99. if (addr->sa.sa_family == AF_INET) {
  100. addr->sin.sin_port = htons(port);
  101. }
  102. else if (addr->sa.sa_family == AF_INET6) {
  103. addr->sin6.sin6_port = htons(port);
  104. }
  105. }
  106. int sockaddr_set_ipport(sockaddr_u* addr, const char* host, int port) {
  107. #ifdef ENABLE_UDS
  108. if (port < 0) {
  109. sockaddr_set_path(addr, host);
  110. return 0;
  111. }
  112. #endif
  113. int ret = sockaddr_set_ip(addr, host);
  114. if (ret != 0) return ret;
  115. sockaddr_set_port(addr, port);
  116. // SOCKADDR_PRINT(addr);
  117. return 0;
  118. }
  119. socklen_t sockaddr_len(sockaddr_u* addr) {
  120. if (addr->sa.sa_family == AF_INET) {
  121. return sizeof(struct sockaddr_in);
  122. }
  123. else if (addr->sa.sa_family == AF_INET6) {
  124. return sizeof(struct sockaddr_in6);
  125. }
  126. #ifdef ENABLE_UDS
  127. else if (addr->sa.sa_family == AF_UNIX) {
  128. return sizeof(struct sockaddr_un);
  129. }
  130. #endif
  131. return sizeof(sockaddr_u);
  132. }
  133. const char* sockaddr_str(sockaddr_u* addr, char* buf, int len) {
  134. char ip[SOCKADDR_STRLEN] = {0};
  135. uint16_t port = 0;
  136. if (addr->sa.sa_family == AF_INET) {
  137. inet_ntop(AF_INET, &addr->sin.sin_addr, ip, len);
  138. port = ntohs(addr->sin.sin_port);
  139. snprintf(buf, len, "%s:%d", ip, port);
  140. }
  141. else if (addr->sa.sa_family == AF_INET6) {
  142. inet_ntop(AF_INET6, &addr->sin6.sin6_addr, ip, len);
  143. port = ntohs(addr->sin6.sin6_port);
  144. snprintf(buf, len, "[%s]:%d", ip, port);
  145. }
  146. #ifdef ENABLE_UDS
  147. else if (addr->sa.sa_family == AF_UNIX) {
  148. snprintf(buf, len, "%s", addr->sun.sun_path);
  149. }
  150. #endif
  151. return buf;
  152. }
  153. int sockaddr_compare(const sockaddr_u* addr1, const sockaddr_u* addr2) {
  154. if (addr1->sa.sa_family != addr2->sa.sa_family)
  155. return addr1->sa.sa_family - addr2->sa.sa_family;
  156. if (addr1->sa.sa_family == AF_INET) {
  157. if (addr1->sin.sin_family != addr2->sin.sin_family)
  158. return addr1->sin.sin_family - addr2->sin.sin_family;
  159. if (addr1->sin.sin_port != addr2->sin.sin_port)
  160. return addr1->sin.sin_port - addr2->sin.sin_port;
  161. return memcmp(&addr1->sin.sin_addr, &addr2->sin.sin_addr, sizeof(struct in_addr));
  162. }
  163. else if (addr1->sa.sa_family == AF_INET6) {
  164. if (addr1->sin6.sin6_family != addr2->sin6.sin6_family)
  165. return addr1->sin6.sin6_family - addr2->sin6.sin6_family;
  166. if (addr1->sin6.sin6_port != addr2->sin6.sin6_port)
  167. return addr1->sin6.sin6_port - addr2->sin6.sin6_port;
  168. return memcmp(&addr1->sin6.sin6_addr, &addr2->sin6.sin6_addr, sizeof(struct in_addr));
  169. }
  170. return memcmp(addr1, addr2, sizeof(sockaddr_u));
  171. }
  172. static int sockaddr_bind(sockaddr_u* localaddr, int type) {
  173. // socket -> setsockopt -> bind
  174. #ifdef SOCK_CLOEXEC
  175. type |= SOCK_CLOEXEC;
  176. #endif
  177. int sockfd = socket(localaddr->sa.sa_family, type, 0);
  178. if (sockfd < 0) {
  179. perror("socket");
  180. goto error;
  181. }
  182. #ifdef OS_UNIX
  183. so_reuseaddr(sockfd, 1);
  184. // so_reuseport(sockfd, 1);
  185. #endif
  186. if (localaddr->sa.sa_family == AF_INET6) {
  187. ip_v6only(sockfd, 0);
  188. }
  189. if (bind(sockfd, &localaddr->sa, sockaddr_len(localaddr)) < 0) {
  190. perror("bind");
  191. goto error;
  192. }
  193. return sockfd;
  194. error:
  195. return socket_errno_negative(sockfd);
  196. }
  197. static int sockaddr_connect(sockaddr_u* peeraddr, int nonblock) {
  198. // socket -> nonblocking -> connect
  199. int ret = 0;
  200. int connfd = socket(peeraddr->sa.sa_family, SOCK_STREAM, 0);
  201. if (connfd < 0) {
  202. perror("socket");
  203. goto error;
  204. }
  205. if (nonblock) {
  206. nonblocking(connfd);
  207. }
  208. ret = connect(connfd, &peeraddr->sa, sockaddr_len(peeraddr));
  209. #ifdef OS_WIN
  210. if (ret < 0 && socket_errno() != WSAEWOULDBLOCK) {
  211. #else
  212. if (ret < 0 && socket_errno() != EINPROGRESS) {
  213. #endif
  214. // perror("connect");
  215. goto error;
  216. }
  217. return connfd;
  218. error:
  219. return socket_errno_negative(connfd);
  220. }
  221. static int ListenFD(int sockfd) {
  222. if (sockfd < 0) return sockfd;
  223. if (listen(sockfd, SOMAXCONN) < 0) {
  224. perror("listen");
  225. return socket_errno_negative(sockfd);
  226. }
  227. return sockfd;
  228. }
  229. static int ConnectFDTimeout(int connfd, int ms) {
  230. int err = 0;
  231. socklen_t optlen = sizeof(err);
  232. struct timeval tv = { ms / 1000, (ms % 1000) * 1000 };
  233. fd_set writefds;
  234. FD_ZERO(&writefds);
  235. FD_SET(connfd, &writefds);
  236. int ret = select(connfd+1, 0, &writefds, 0, &tv);
  237. if (ret < 0) {
  238. perror("select");
  239. goto error;
  240. }
  241. if (ret == 0) {
  242. errno = ETIMEDOUT;
  243. goto error;
  244. }
  245. if (getsockopt(connfd, SOL_SOCKET, SO_ERROR, (char*)&err, &optlen) < 0 || err != 0) {
  246. if (err != 0) errno = err;
  247. goto error;
  248. }
  249. blocking(connfd);
  250. return connfd;
  251. error:
  252. return socket_errno_negative(connfd);
  253. }
  254. int Bind(int port, const char* host, int type) {
  255. #ifdef OS_WIN
  256. WSAInit();
  257. #endif
  258. sockaddr_u localaddr;
  259. memset(&localaddr, 0, sizeof(localaddr));
  260. int ret = sockaddr_set_ipport(&localaddr, host, port);
  261. if (ret != 0) {
  262. return NABS(ret);
  263. }
  264. return sockaddr_bind(&localaddr, type);
  265. }
  266. int Listen(int port, const char* host) {
  267. int sockfd = Bind(port, host, SOCK_STREAM);
  268. if (sockfd < 0) return sockfd;
  269. return ListenFD(sockfd);
  270. }
  271. int Connect(const char* host, int port, int nonblock) {
  272. #ifdef OS_WIN
  273. WSAInit();
  274. #endif
  275. sockaddr_u peeraddr;
  276. memset(&peeraddr, 0, sizeof(peeraddr));
  277. int ret = sockaddr_set_ipport(&peeraddr, host, port);
  278. if (ret != 0) {
  279. return NABS(ret);
  280. }
  281. return sockaddr_connect(&peeraddr, nonblock);
  282. }
  283. int ConnectNonblock(const char* host, int port) {
  284. return Connect(host, port, 1);
  285. }
  286. int ConnectTimeout(const char* host, int port, int ms) {
  287. int connfd = Connect(host, port, 1);
  288. if (connfd < 0) return connfd;
  289. return ConnectFDTimeout(connfd, ms);
  290. }
  291. #ifdef ENABLE_UDS
  292. int BindUnix(const char* path, int type) {
  293. sockaddr_u localaddr;
  294. memset(&localaddr, 0, sizeof(localaddr));
  295. sockaddr_set_path(&localaddr, path);
  296. return sockaddr_bind(&localaddr, type);
  297. }
  298. int ListenUnix(const char* path) {
  299. int sockfd = BindUnix(path, SOCK_STREAM);
  300. if (sockfd < 0) return sockfd;
  301. return ListenFD(sockfd);
  302. }
  303. int ConnectUnix(const char* path, int nonblock) {
  304. sockaddr_u peeraddr;
  305. memset(&peeraddr, 0, sizeof(peeraddr));
  306. sockaddr_set_path(&peeraddr, path);
  307. return sockaddr_connect(&peeraddr, nonblock);
  308. }
  309. int ConnectUnixNonblock(const char* path) {
  310. return ConnectUnix(path, 1);
  311. }
  312. int ConnectUnixTimeout(const char* path, int ms) {
  313. int connfd = ConnectUnix(path, 1);
  314. if (connfd < 0) return connfd;
  315. return ConnectFDTimeout(connfd, ms);
  316. }
  317. #endif
  318. int Socketpair(int family, int type, int protocol, int sv[2]) {
  319. #if defined(OS_UNIX) && HAVE_SOCKETPAIR
  320. return socketpair(AF_LOCAL, type, protocol, sv);
  321. #endif
  322. if (family != AF_INET || type != SOCK_STREAM) {
  323. return -1;
  324. }
  325. #ifdef OS_WIN
  326. WSAInit();
  327. #endif
  328. int listenfd, connfd, acceptfd;
  329. listenfd = connfd = acceptfd = -1;
  330. struct sockaddr_in localaddr;
  331. socklen_t addrlen = sizeof(localaddr);
  332. memset(&localaddr, 0, addrlen);
  333. localaddr.sin_family = AF_INET;
  334. localaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  335. localaddr.sin_port = 0;
  336. // listener
  337. listenfd = socket(AF_INET, SOCK_STREAM, 0);
  338. if (listenfd < 0) {
  339. perror("socket");
  340. goto error;
  341. }
  342. if (bind(listenfd, (struct sockaddr*)&localaddr, addrlen) < 0) {
  343. perror("bind");
  344. goto error;
  345. }
  346. if (listen(listenfd, 1) < 0) {
  347. perror("listen");
  348. goto error;
  349. }
  350. if (getsockname(listenfd, (struct sockaddr*)&localaddr, &addrlen) < 0) {
  351. perror("getsockname");
  352. goto error;
  353. }
  354. // connector
  355. connfd = socket(AF_INET, SOCK_STREAM, 0);
  356. if (connfd < 0) {
  357. perror("socket");
  358. goto error;
  359. }
  360. if (connect(connfd, (struct sockaddr*)&localaddr, addrlen) < 0) {
  361. perror("connect");
  362. goto error;
  363. }
  364. // acceptor
  365. acceptfd = accept(listenfd, (struct sockaddr*)&localaddr, &addrlen);
  366. if (acceptfd < 0) {
  367. perror("accept");
  368. goto error;
  369. }
  370. closesocket(listenfd);
  371. sv[0] = connfd;
  372. sv[1] = acceptfd;
  373. return 0;
  374. error:
  375. if (listenfd != -1) {
  376. closesocket(listenfd);
  377. }
  378. if (connfd != -1) {
  379. closesocket(connfd);
  380. }
  381. if (acceptfd != -1) {
  382. closesocket(acceptfd);
  383. }
  384. return -1;
  385. }