BConnection_win.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /**
  2. * @file BConnection_win.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdlib.h>
  23. #include <limits.h>
  24. #include <base/BLog.h>
  25. #include "BConnection.h"
  26. #include <generated/blog_channel_BConnection.h>
  27. #define LISTEN_BACKLOG 128
  28. struct sys_addr {
  29. int len;
  30. union {
  31. struct sockaddr generic;
  32. struct sockaddr_in ipv4;
  33. struct sockaddr_in6 ipv6;
  34. } addr;
  35. };
  36. static void addr_socket_to_sys (struct sys_addr *out, BAddr addr);
  37. static void addr_any_to_sys (struct sys_addr *out, int family);
  38. static void addr_sys_to_socket (BAddr *out, struct sys_addr addr);
  39. static void listener_next_job_handler (BListener *o);
  40. static void listener_olap_handler (BListener *o, int event, DWORD bytes);
  41. static void connector_olap_handler (BConnector *o, int event, DWORD bytes);
  42. static void connector_abort (BConnector *o);
  43. static void connection_report_error (BConnection *o);
  44. static void connection_abort (BConnection *o);
  45. static void connection_send_iface_handler_send (BConnection *o, uint8_t *data, int data_len);
  46. static void connection_recv_iface_handler_recv (BConnection *o, uint8_t *data, int data_len);
  47. static void connection_send_olap_handler (BConnection *o, int event, DWORD bytes);
  48. static void connection_recv_olap_handler (BConnection *o, int event, DWORD bytes);
  49. static void addr_socket_to_sys (struct sys_addr *out, BAddr addr)
  50. {
  51. switch (addr.type) {
  52. case BADDR_TYPE_IPV4: {
  53. out->len = sizeof(out->addr.ipv4);
  54. memset(&out->addr.ipv4, 0, sizeof(out->addr.ipv4));
  55. out->addr.ipv4.sin_family = AF_INET;
  56. out->addr.ipv4.sin_port = addr.ipv4.port;
  57. out->addr.ipv4.sin_addr.s_addr = addr.ipv4.ip;
  58. } break;
  59. case BADDR_TYPE_IPV6: {
  60. out->len = sizeof(out->addr.ipv6);
  61. memset(&out->addr.ipv6, 0, sizeof(out->addr.ipv6));
  62. out->addr.ipv6.sin6_family = AF_INET6;
  63. out->addr.ipv6.sin6_port = addr.ipv6.port;
  64. out->addr.ipv6.sin6_flowinfo = 0;
  65. memcpy(out->addr.ipv6.sin6_addr.s6_addr, addr.ipv6.ip, 16);
  66. out->addr.ipv6.sin6_scope_id = 0;
  67. } break;
  68. default: ASSERT(0);
  69. }
  70. }
  71. static void addr_any_to_sys (struct sys_addr *out, int family)
  72. {
  73. switch (family) {
  74. case BADDR_TYPE_IPV4: {
  75. out->len = sizeof(out->addr.ipv4);
  76. memset(&out->addr.ipv4, 0, sizeof(out->addr.ipv4));
  77. out->addr.ipv4.sin_family = AF_INET;
  78. out->addr.ipv4.sin_port = 0;
  79. out->addr.ipv4.sin_addr.s_addr = INADDR_ANY;
  80. } break;
  81. case BADDR_TYPE_IPV6: {
  82. out->len = sizeof(out->addr.ipv6);
  83. memset(&out->addr.ipv6, 0, sizeof(out->addr.ipv6));
  84. out->addr.ipv6.sin6_family = AF_INET6;
  85. out->addr.ipv6.sin6_port = 0;
  86. out->addr.ipv6.sin6_flowinfo = 0;
  87. out->addr.ipv6.sin6_addr = (struct in6_addr)IN6ADDR_ANY_INIT;
  88. out->addr.ipv6.sin6_scope_id = 0;
  89. } break;
  90. default: ASSERT(0);
  91. }
  92. }
  93. static void addr_sys_to_socket (BAddr *out, struct sys_addr addr)
  94. {
  95. switch (addr.addr.generic.sa_family) {
  96. case AF_INET: {
  97. ASSERT(addr.len == sizeof(struct sockaddr_in))
  98. BAddr_InitIPv4(out, addr.addr.ipv4.sin_addr.s_addr, addr.addr.ipv4.sin_port);
  99. } break;
  100. case AF_INET6: {
  101. ASSERT(addr.len == sizeof(struct sockaddr_in6))
  102. BAddr_InitIPv6(out, addr.addr.ipv6.sin6_addr.s6_addr, addr.addr.ipv6.sin6_port);
  103. } break;
  104. default: {
  105. BAddr_InitNone(out);
  106. } break;
  107. }
  108. }
  109. static void listener_next_job_handler (BListener *o)
  110. {
  111. DebugObject_Access(&o->d_obj);
  112. ASSERT(!o->busy)
  113. // free ready socket
  114. if (o->ready) {
  115. BLog(BLOG_ERROR, "discarding connection");
  116. // close new socket
  117. if (closesocket(o->newsock) == SOCKET_ERROR) {
  118. BLog(BLOG_ERROR, "closesocket failed");
  119. }
  120. // set not ready
  121. o->ready = 0;
  122. }
  123. // create new socket
  124. if ((o->newsock = WSASocket(o->sys_family, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET) {
  125. BLog(BLOG_ERROR, "WSASocket failed");
  126. goto fail0;
  127. }
  128. // start accept operation
  129. while (1) {
  130. memset(&o->olap.olap, 0, sizeof(o->olap.olap));
  131. DWORD bytes;
  132. BOOL res = o->fnAcceptEx(o->sock, o->newsock, o->addrbuf, 0, sizeof(struct BListener_addrbuf_stub), sizeof(struct BListener_addrbuf_stub), &bytes, &o->olap.olap);
  133. if (res == FALSE && WSAGetLastError() != ERROR_IO_PENDING) {
  134. BLog(BLOG_ERROR, "AcceptEx failed");
  135. continue;
  136. }
  137. break;
  138. }
  139. // set busy
  140. o->busy = 1;
  141. return;
  142. fail0:
  143. return;
  144. }
  145. static void listener_olap_handler (BListener *o, int event, DWORD bytes)
  146. {
  147. DebugObject_Access(&o->d_obj);
  148. ASSERT(o->busy)
  149. ASSERT(!o->ready)
  150. ASSERT(event == BREACTOR_IOCP_EVENT_SUCCEEDED || event == BREACTOR_IOCP_EVENT_FAILED)
  151. // set not busy
  152. o->busy = 0;
  153. // schedule next accept
  154. BPending_Set(&o->next_job);
  155. if (event == BREACTOR_IOCP_EVENT_FAILED) {
  156. BLog(BLOG_ERROR, "accepting failed");
  157. // close new socket
  158. if (closesocket(o->newsock) == SOCKET_ERROR) {
  159. BLog(BLOG_ERROR, "closesocket failed");
  160. }
  161. return;
  162. }
  163. BLog(BLOG_INFO, "connection accepted");
  164. // set ready
  165. o->ready = 1;
  166. // call handler
  167. o->handler(o->user);
  168. return;
  169. }
  170. static void connector_olap_handler (BConnector *o, int event, DWORD bytes)
  171. {
  172. DebugObject_Access(&o->d_obj);
  173. ASSERT(o->sock != INVALID_SOCKET)
  174. ASSERT(o->busy)
  175. ASSERT(!o->ready)
  176. ASSERT(event == BREACTOR_IOCP_EVENT_SUCCEEDED || event == BREACTOR_IOCP_EVENT_FAILED)
  177. // set not busy
  178. o->busy = 0;
  179. if (event == BREACTOR_IOCP_EVENT_FAILED) {
  180. BLog(BLOG_ERROR, "connection failed");
  181. } else {
  182. // set ready
  183. o->ready = 1;
  184. }
  185. // call handler
  186. o->handler(o->user, !o->ready);
  187. return;
  188. }
  189. static void connector_abort (BConnector *o)
  190. {
  191. if (o->sock != INVALID_SOCKET) {
  192. // cancel I/O
  193. if (o->busy) {
  194. if (!CancelIo((HANDLE)o->sock)) {
  195. BLog(BLOG_ERROR, "CancelIo failed");
  196. }
  197. }
  198. // close socket
  199. if (closesocket(o->sock) == SOCKET_ERROR) {
  200. BLog(BLOG_ERROR, "closesocket failed");
  201. }
  202. }
  203. // wait for connect operation to finish
  204. if (o->busy) {
  205. BReactorIOCPOverlapped_Wait(&o->olap, NULL, NULL);
  206. }
  207. // free olap
  208. BReactorIOCPOverlapped_Free(&o->olap);
  209. }
  210. static void connection_report_error (BConnection *o)
  211. {
  212. DebugError_AssertNoError(&o->d_err);
  213. ASSERT(o->handler)
  214. // report error
  215. DEBUGERROR(&o->d_err, o->handler(o->user, BCONNECTION_EVENT_ERROR));
  216. return;
  217. }
  218. static void connection_abort (BConnection *o)
  219. {
  220. ASSERT(!o->aborted)
  221. // cancel I/O
  222. if ((o->recv.inited && o->recv.busy) || (o->send.inited && o->send.busy)) {
  223. if (!CancelIo((HANDLE)o->sock)) {
  224. BLog(BLOG_ERROR, "CancelIo failed");
  225. }
  226. }
  227. // close socket
  228. if (closesocket(o->sock) == SOCKET_ERROR) {
  229. BLog(BLOG_ERROR, "closesocket failed");
  230. }
  231. // wait for receiving to complete
  232. if (o->recv.inited && o->recv.busy) {
  233. BReactorIOCPOverlapped_Wait(&o->recv.olap, NULL, NULL);
  234. }
  235. // wait for sending to complete
  236. if (o->send.inited && o->send.busy) {
  237. BReactorIOCPOverlapped_Wait(&o->send.olap, NULL, NULL);
  238. }
  239. // free recv olap
  240. BReactorIOCPOverlapped_Free(&o->recv.olap);
  241. // free send olap
  242. BReactorIOCPOverlapped_Free(&o->send.olap);
  243. // set aborted
  244. o->aborted = 1;
  245. }
  246. static void connection_send_iface_handler_send (BConnection *o, uint8_t *data, int data_len)
  247. {
  248. DebugObject_Access(&o->d_obj);
  249. DebugError_AssertNoError(&o->d_err);
  250. ASSERT(!o->aborted)
  251. ASSERT(o->send.inited)
  252. ASSERT(!o->send.busy)
  253. ASSERT(data_len > 0)
  254. if (data_len > ULONG_MAX) {
  255. data_len = ULONG_MAX;
  256. }
  257. WSABUF buf;
  258. buf.buf = data;
  259. buf.len = data_len;
  260. memset(&o->send.olap.olap, 0, sizeof(o->send.olap.olap));
  261. // send
  262. int res = WSASend(o->sock, &buf, 1, NULL, 0, &o->send.olap.olap, NULL);
  263. if (res == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING) {
  264. BLog(BLOG_ERROR, "WSASend failed (%d)", WSAGetLastError());
  265. connection_report_error(o);
  266. return;
  267. }
  268. // set busy
  269. o->send.busy = 1;
  270. o->send.busy_data_len = data_len;
  271. }
  272. static void connection_recv_iface_handler_recv (BConnection *o, uint8_t *data, int data_len)
  273. {
  274. DebugObject_Access(&o->d_obj);
  275. DebugError_AssertNoError(&o->d_err);
  276. ASSERT(!o->recv.closed)
  277. ASSERT(!o->aborted)
  278. ASSERT(o->recv.inited)
  279. ASSERT(!o->recv.busy)
  280. ASSERT(data_len > 0)
  281. if (data_len > ULONG_MAX) {
  282. data_len = ULONG_MAX;
  283. }
  284. WSABUF buf;
  285. buf.buf = data;
  286. buf.len = data_len;
  287. memset(&o->recv.olap.olap, 0, sizeof(o->recv.olap.olap));
  288. // recv
  289. DWORD flags = 0;
  290. int res = WSARecv(o->sock, &buf, 1, NULL, &flags, &o->recv.olap.olap, NULL);
  291. if (res == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING) {
  292. BLog(BLOG_ERROR, "WSARecv failed (%d)", WSAGetLastError());
  293. connection_report_error(o);
  294. return;
  295. }
  296. // set busy
  297. o->recv.busy = 1;
  298. o->recv.busy_data_len = data_len;
  299. }
  300. static void connection_send_olap_handler (BConnection *o, int event, DWORD bytes)
  301. {
  302. DebugObject_Access(&o->d_obj);
  303. DebugError_AssertNoError(&o->d_err);
  304. ASSERT(!o->aborted)
  305. ASSERT(o->send.inited)
  306. ASSERT(o->send.busy)
  307. ASSERT(event == BREACTOR_IOCP_EVENT_SUCCEEDED || event == BREACTOR_IOCP_EVENT_FAILED)
  308. // set not busy
  309. o->send.busy = 0;
  310. if (event == BREACTOR_IOCP_EVENT_FAILED) {
  311. BLog(BLOG_ERROR, "sending failed");
  312. connection_report_error(o);
  313. return;
  314. }
  315. ASSERT(bytes > 0)
  316. ASSERT(bytes <= o->send.busy_data_len)
  317. // done
  318. StreamPassInterface_Done(&o->send.iface, bytes);
  319. }
  320. static void connection_recv_olap_handler (BConnection *o, int event, DWORD bytes)
  321. {
  322. DebugObject_Access(&o->d_obj);
  323. DebugError_AssertNoError(&o->d_err);
  324. ASSERT(!o->recv.closed)
  325. ASSERT(!o->aborted)
  326. ASSERT(o->recv.inited)
  327. ASSERT(o->recv.busy)
  328. ASSERT(event == BREACTOR_IOCP_EVENT_SUCCEEDED || event == BREACTOR_IOCP_EVENT_FAILED)
  329. // set not busy
  330. o->recv.busy = 0;
  331. if (event == BREACTOR_IOCP_EVENT_FAILED) {
  332. BLog(BLOG_ERROR, "receiving failed");
  333. connection_report_error(o);
  334. return;
  335. }
  336. if (bytes == 0) {
  337. // set closed
  338. o->recv.closed = 1;
  339. // report recv closed
  340. o->handler(o->user, BCONNECTION_EVENT_RECVCLOSED);
  341. return;
  342. }
  343. ASSERT(bytes > 0)
  344. ASSERT(bytes <= o->recv.busy_data_len)
  345. // done
  346. StreamRecvInterface_Done(&o->recv.iface, bytes);
  347. }
  348. int BConnection_AddressSupported (BAddr addr)
  349. {
  350. BAddr_Assert(&addr);
  351. return (addr.type == BADDR_TYPE_IPV4 || addr.type == BADDR_TYPE_IPV6);
  352. }
  353. int BListener_Init (BListener *o, BAddr addr, BReactor *reactor, void *user,
  354. BListener_handler handler)
  355. {
  356. ASSERT(BConnection_AddressSupported(addr))
  357. ASSERT(handler)
  358. BNetwork_Assert();
  359. // init arguments
  360. o->reactor = reactor;
  361. o->user = user;
  362. o->handler = handler;
  363. // convert address
  364. struct sys_addr sysaddr;
  365. addr_socket_to_sys(&sysaddr, addr);
  366. // remember family
  367. o->sys_family = sysaddr.addr.generic.sa_family;
  368. // init socket
  369. if ((o->sock = WSASocket(sysaddr.addr.generic.sa_family, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET) {
  370. BLog(BLOG_ERROR, "WSASocket failed");
  371. goto fail0;
  372. }
  373. // associate with IOCP
  374. if (!CreateIoCompletionPort((HANDLE)o->sock, BReactor_GetIOCPHandle(o->reactor), 0, 0)) {
  375. BLog(BLOG_ERROR, "CreateIoCompletionPort failed");
  376. goto fail1;
  377. }
  378. // bind
  379. if (bind(o->sock, &sysaddr.addr.generic, sysaddr.len) < 0) {
  380. BLog(BLOG_ERROR, "bind failed");
  381. goto fail1;
  382. }
  383. // listen
  384. if (listen(o->sock, LISTEN_BACKLOG) < 0) {
  385. BLog(BLOG_ERROR, "listen failed");
  386. goto fail1;
  387. }
  388. GUID guid;
  389. DWORD out_bytes;
  390. // obtain AcceptEx
  391. guid = (GUID)WSAID_ACCEPTEX;
  392. if (WSAIoctl(o->sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), &o->fnAcceptEx, sizeof(o->fnAcceptEx), &out_bytes, NULL, NULL) != 0) {
  393. BLog(BLOG_ERROR, "faild to obtain AcceptEx");
  394. goto fail1;
  395. }
  396. // obtain GetAcceptExSockaddrs
  397. guid = (GUID)WSAID_GETACCEPTEXSOCKADDRS;
  398. if (WSAIoctl(o->sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), &o->fnGetAcceptExSockaddrs, sizeof(o->fnGetAcceptExSockaddrs), &out_bytes, NULL, NULL) != 0) {
  399. BLog(BLOG_ERROR, "faild to obtain GetAcceptExSockaddrs");
  400. goto fail1;
  401. }
  402. // init olap
  403. BReactorIOCPOverlapped_Init(&o->olap, o->reactor, o, (BReactorIOCPOverlapped_handler)listener_olap_handler);
  404. // init next job
  405. BPending_Init(&o->next_job, BReactor_PendingGroup(o->reactor), (BPending_handler)listener_next_job_handler, o);
  406. // set not busy
  407. o->busy = 0;
  408. // set not ready
  409. o->ready = 0;
  410. // set next job
  411. BPending_Set(&o->next_job);
  412. DebugObject_Init(&o->d_obj);
  413. return 1;
  414. fail1:
  415. if (closesocket(o->sock) == SOCKET_ERROR) {
  416. BLog(BLOG_ERROR, "closesocket failed");
  417. }
  418. fail0:
  419. return 0;
  420. }
  421. void BListener_Free (BListener *o)
  422. {
  423. DebugObject_Free(&o->d_obj);
  424. // cancel I/O
  425. if (o->busy) {
  426. if (!CancelIo((HANDLE)o->sock)) {
  427. BLog(BLOG_ERROR, "CancelIo failed");
  428. }
  429. }
  430. // close socket
  431. if (closesocket(o->sock) == SOCKET_ERROR) {
  432. BLog(BLOG_ERROR, "closesocket failed");
  433. }
  434. // wait for accept operation to finish
  435. if (o->busy) {
  436. BReactorIOCPOverlapped_Wait(&o->olap, NULL, NULL);
  437. }
  438. // close new socket
  439. if (o->busy || o->ready) {
  440. if (closesocket(o->newsock) == SOCKET_ERROR) {
  441. BLog(BLOG_ERROR, "closesocket failed");
  442. }
  443. }
  444. // free next job
  445. BPending_Free(&o->next_job);
  446. // free olap
  447. BReactorIOCPOverlapped_Free(&o->olap);
  448. }
  449. int BConnector_Init (BConnector *o, BAddr addr, BReactor *reactor, void *user,
  450. BConnector_handler handler)
  451. {
  452. ASSERT(BConnection_AddressSupported(addr))
  453. ASSERT(handler)
  454. BNetwork_Assert();
  455. // init arguments
  456. o->reactor = reactor;
  457. o->user = user;
  458. o->handler = handler;
  459. // convert address
  460. struct sys_addr sysaddr;
  461. addr_socket_to_sys(&sysaddr, addr);
  462. // create local any address
  463. struct sys_addr local_sysaddr;
  464. addr_any_to_sys(&local_sysaddr, addr.type);
  465. // init socket
  466. if ((o->sock = WSASocket(sysaddr.addr.generic.sa_family, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET) {
  467. BLog(BLOG_ERROR, "WSASocket failed");
  468. goto fail0;
  469. }
  470. // associate with IOCP
  471. if (!CreateIoCompletionPort((HANDLE)o->sock, BReactor_GetIOCPHandle(o->reactor), 0, 0)) {
  472. BLog(BLOG_ERROR, "CreateIoCompletionPort failed");
  473. goto fail1;
  474. }
  475. // bind socket
  476. if (bind(o->sock, &local_sysaddr.addr.generic, local_sysaddr.len) < 0) {
  477. BLog(BLOG_ERROR, "bind failed");
  478. goto fail1;
  479. }
  480. // obtain ConnectEx
  481. GUID guid = WSAID_CONNECTEX;
  482. DWORD out_bytes;
  483. if (WSAIoctl(o->sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), &o->fnConnectEx, sizeof(o->fnConnectEx), &out_bytes, NULL, NULL) != 0) {
  484. BLog(BLOG_ERROR, "faild to get ConnectEx");
  485. goto fail1;
  486. }
  487. // init olap
  488. BReactorIOCPOverlapped_Init(&o->olap, o->reactor, o, (BReactorIOCPOverlapped_handler)connector_olap_handler);
  489. // start connect operation
  490. BOOL res = o->fnConnectEx(o->sock, &sysaddr.addr.generic, sysaddr.len, NULL, 0, NULL, &o->olap.olap);
  491. if (res == FALSE && WSAGetLastError() != ERROR_IO_PENDING) {
  492. BLog(BLOG_ERROR, "ConnectEx failed (%d)", WSAGetLastError());
  493. goto fail2;
  494. }
  495. // set busy
  496. o->busy = 1;
  497. // set not ready
  498. o->ready = 0;
  499. DebugObject_Init(&o->d_obj);
  500. return 1;
  501. fail2:
  502. BReactorIOCPOverlapped_Free(&o->olap);
  503. fail1:
  504. if (closesocket(o->sock) == SOCKET_ERROR) {
  505. BLog(BLOG_ERROR, "closesocket failed");
  506. }
  507. fail0:
  508. return 0;
  509. }
  510. void BConnector_Free (BConnector *o)
  511. {
  512. DebugObject_Free(&o->d_obj);
  513. if (o->sock != INVALID_SOCKET) {
  514. connector_abort(o);
  515. }
  516. }
  517. int BConnection_Init (BConnection *o, struct BConnection_source source, BReactor *reactor, void *user,
  518. BConnection_handler handler)
  519. {
  520. switch (source.type) {
  521. case BCONNECTION_SOURCE_TYPE_LISTENER: {
  522. BListener *listener = source.u.listener.listener;
  523. DebugObject_Access(&listener->d_obj);
  524. ASSERT(BPending_IsSet(&listener->next_job))
  525. ASSERT(!listener->busy)
  526. ASSERT(listener->ready)
  527. } break;
  528. case BCONNECTION_SOURCE_TYPE_CONNECTOR: {
  529. BConnector *connector = source.u.connector.connector;
  530. DebugObject_Access(&connector->d_obj);
  531. ASSERT(connector->reactor == reactor)
  532. ASSERT(connector->sock != INVALID_SOCKET)
  533. ASSERT(!connector->busy)
  534. ASSERT(connector->ready)
  535. } break;
  536. default: ASSERT(0);
  537. }
  538. ASSERT(handler)
  539. BNetwork_Assert();
  540. // init arguments
  541. o->reactor = reactor;
  542. o->user = user;
  543. o->handler = handler;
  544. switch (source.type) {
  545. case BCONNECTION_SOURCE_TYPE_LISTENER: {
  546. BListener *listener = source.u.listener.listener;
  547. // grab new socket from listener
  548. o->sock = listener->newsock;
  549. listener->ready = 0;
  550. // associate with IOCP
  551. if (!CreateIoCompletionPort((HANDLE)o->sock, BReactor_GetIOCPHandle(o->reactor), 0, 0)) {
  552. BLog(BLOG_ERROR, "CreateIoCompletionPort failed");
  553. goto fail1;
  554. }
  555. // return address
  556. if (source.u.listener.out_addr) {
  557. struct sockaddr *addr_local;
  558. struct sockaddr *addr_remote;
  559. int len_local;
  560. int len_remote;
  561. listener->fnGetAcceptExSockaddrs(listener->addrbuf, 0, sizeof(struct BListener_addrbuf_stub), sizeof(struct BListener_addrbuf_stub),
  562. &addr_local, &len_local, &addr_remote, &len_remote);
  563. struct sys_addr sysaddr;
  564. ASSERT_FORCE(len_remote >= 0)
  565. ASSERT_FORCE(len_remote <= sizeof(sysaddr.addr))
  566. memcpy((uint8_t *)&sysaddr.addr, (uint8_t *)addr_remote, len_remote);
  567. sysaddr.len = len_remote;
  568. addr_sys_to_socket(source.u.listener.out_addr, sysaddr);
  569. }
  570. } break;
  571. case BCONNECTION_SOURCE_TYPE_CONNECTOR: {
  572. BConnector *connector = source.u.connector.connector;
  573. // grab fd from connector
  574. o->sock = connector->sock;
  575. connector->sock = INVALID_SOCKET;
  576. // release connector resources
  577. connector_abort(connector);
  578. } break;
  579. }
  580. // set not aborted
  581. o->aborted = 0;
  582. // init send olap
  583. BReactorIOCPOverlapped_Init(&o->send.olap, o->reactor, o, (BReactorIOCPOverlapped_handler)connection_send_olap_handler);
  584. // set send not inited
  585. o->send.inited = 0;
  586. // init recv olap
  587. BReactorIOCPOverlapped_Init(&o->recv.olap, o->reactor, o, (BReactorIOCPOverlapped_handler)connection_recv_olap_handler);
  588. // set recv not closed
  589. o->recv.closed = 0;
  590. // set recv not inited
  591. o->recv.inited = 0;
  592. DebugError_Init(&o->d_err, BReactor_PendingGroup(o->reactor));
  593. DebugObject_Init(&o->d_obj);
  594. return 1;
  595. fail1:
  596. if (closesocket(o->sock) == SOCKET_ERROR) {
  597. BLog(BLOG_ERROR, "closesocket failed");
  598. }
  599. return 0;
  600. }
  601. void BConnection_Free (BConnection *o)
  602. {
  603. DebugObject_Free(&o->d_obj);
  604. DebugError_Free(&o->d_err);
  605. ASSERT(!o->recv.inited)
  606. ASSERT(!o->send.inited)
  607. if (!o->aborted) {
  608. connection_abort(o);
  609. }
  610. }
  611. void BConnection_SetHandlers (BConnection *o, void *user, BConnection_handler handler)
  612. {
  613. DebugObject_Access(&o->d_obj);
  614. // set handlers
  615. o->user = user;
  616. o->handler = handler;
  617. }
  618. int BConnection_SetSendBuffer (BConnection *o, int buf_size)
  619. {
  620. DebugObject_Access(&o->d_obj);
  621. if (setsockopt(o->sock, SOL_SOCKET, SO_SNDBUF, (void *)&buf_size, sizeof(buf_size)) < 0) {
  622. BLog(BLOG_ERROR, "setsockopt failed");
  623. return 0;
  624. }
  625. return 1;
  626. }
  627. void BConnection_SendAsync_Init (BConnection *o)
  628. {
  629. DebugObject_Access(&o->d_obj);
  630. DebugError_AssertNoError(&o->d_err);
  631. ASSERT(!o->aborted)
  632. ASSERT(!o->send.inited)
  633. // init interface
  634. StreamPassInterface_Init(&o->send.iface, (StreamPassInterface_handler_send)connection_send_iface_handler_send, o, BReactor_PendingGroup(o->reactor));
  635. // set not busy
  636. o->send.busy = 0;
  637. // set inited
  638. o->send.inited = 1;
  639. }
  640. void BConnection_SendAsync_Free (BConnection *o)
  641. {
  642. DebugObject_Access(&o->d_obj);
  643. ASSERT(o->send.inited)
  644. // abort if busy
  645. if (o->send.busy && !o->aborted) {
  646. connection_abort(o);
  647. }
  648. // free interface
  649. StreamPassInterface_Free(&o->send.iface);
  650. // set not inited
  651. o->send.inited = 0;
  652. }
  653. StreamPassInterface * BConnection_SendAsync_GetIf (BConnection *o)
  654. {
  655. DebugObject_Access(&o->d_obj);
  656. ASSERT(o->send.inited)
  657. return &o->send.iface;
  658. }
  659. void BConnection_RecvAsync_Init (BConnection *o)
  660. {
  661. DebugObject_Access(&o->d_obj);
  662. DebugError_AssertNoError(&o->d_err);
  663. ASSERT(!o->recv.closed)
  664. ASSERT(!o->aborted)
  665. ASSERT(!o->recv.inited)
  666. // init interface
  667. StreamRecvInterface_Init(&o->recv.iface, (StreamRecvInterface_handler_recv)connection_recv_iface_handler_recv, o, BReactor_PendingGroup(o->reactor));
  668. // set not busy
  669. o->recv.busy = 0;
  670. // set inited
  671. o->recv.inited = 1;
  672. }
  673. void BConnection_RecvAsync_Free (BConnection *o)
  674. {
  675. DebugObject_Access(&o->d_obj);
  676. ASSERT(o->recv.inited)
  677. // abort if busy
  678. if (o->recv.busy && !o->aborted) {
  679. connection_abort(o);
  680. }
  681. // free interface
  682. StreamRecvInterface_Free(&o->recv.iface);
  683. // set not inited
  684. o->recv.inited = 0;
  685. }
  686. StreamRecvInterface * BConnection_RecvAsync_GetIf (BConnection *o)
  687. {
  688. DebugObject_Access(&o->d_obj);
  689. ASSERT(o->recv.inited)
  690. return &o->recv.iface;
  691. }