BSocketPRFileDesc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * @file BSocketPRFileDesc.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 <string.h>
  24. #include <limits.h>
  25. #include <prerror.h>
  26. #include <prmem.h>
  27. #include <misc/debug.h>
  28. #include <misc/offset.h>
  29. #include <base/BLog.h>
  30. #include <nspr_support/BSocketPRFileDesc.h>
  31. #include <generated/blog_channel_BSocketPRFileDesc.h>
  32. #ifndef NDEBUG
  33. int bsocketprfiledesc_initialized = 0;
  34. #endif
  35. PRDescIdentity bsocketprfiledesc_identity;
  36. static int baddr_to_prnetaddr (PRNetAddr *out, BAddr addr)
  37. {
  38. memset(out, 0, sizeof(PRNetAddr));
  39. switch (addr.type) {
  40. case BADDR_TYPE_IPV4:
  41. out->inet.family = PR_AF_INET;
  42. out->inet.port = addr.ipv4.port;
  43. out->inet.ip = addr.ipv4.ip;
  44. break;
  45. case BADDR_TYPE_IPV6:
  46. out->ipv6.family = PR_AF_INET6;
  47. out->ipv6.port = addr.ipv6.port;
  48. out->ipv6.flowinfo = 0;
  49. memcpy(&out->ipv6.ip, addr.ipv6.ip, 16);
  50. break;
  51. default:
  52. return 0;
  53. }
  54. return 1;
  55. }
  56. static PRStatus method_close (PRFileDesc *fd)
  57. {
  58. return PR_SUCCESS;
  59. }
  60. static PRInt32 method_read (PRFileDesc *fd, void *buf, PRInt32 amount)
  61. {
  62. ASSERT(amount >= 0)
  63. BSocket *bsock = (BSocket *)fd->secret;
  64. if (amount > INT_MAX) {
  65. amount = INT_MAX;
  66. }
  67. int res = BSocket_Recv(bsock, buf, amount);
  68. if (res < 0) {
  69. switch (BSocket_GetError(bsock)) {
  70. case BSOCKET_ERROR_LATER:
  71. PR_SetError(PR_WOULD_BLOCK_ERROR, 0);
  72. return -1;
  73. default:
  74. BLog(BLOG_NOTICE, "BSocket_Recv failed (%d)", BSocket_GetError(bsock));
  75. PR_SetError(PR_UNKNOWN_ERROR, 0);
  76. return -1;
  77. }
  78. }
  79. return res;
  80. }
  81. static PRInt32 method_write (PRFileDesc *fd, const void *buf, PRInt32 amount)
  82. {
  83. ASSERT(amount >= 0)
  84. BSocket *bsock = (BSocket *)fd->secret;
  85. if (amount > INT_MAX) {
  86. amount = INT_MAX;
  87. }
  88. int res = BSocket_Send(bsock, (uint8_t *)buf, amount);
  89. ASSERT(res != 0)
  90. if (res < 0) {
  91. switch (BSocket_GetError(bsock)) {
  92. case BSOCKET_ERROR_LATER:
  93. PR_SetError(PR_WOULD_BLOCK_ERROR, 0);
  94. return -1;
  95. default:
  96. BLog(BLOG_NOTICE, "BSocket_Send failed (%d)", BSocket_GetError(bsock));
  97. PR_SetError(PR_UNKNOWN_ERROR, 0);
  98. return -1;
  99. }
  100. }
  101. return res;
  102. }
  103. static PRStatus method_shutdown (PRFileDesc *fd, PRIntn how)
  104. {
  105. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  106. return PR_FAILURE;
  107. }
  108. static PRInt32 method_recv (PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags, PRIntervalTime timeout)
  109. {
  110. ASSERT(flags == 0)
  111. return method_read(fd, buf, amount);
  112. }
  113. static PRInt32 method_send (PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags, PRIntervalTime timeout)
  114. {
  115. ASSERT(flags == 0)
  116. return method_write(fd, buf, amount);
  117. }
  118. static PRInt16 method_poll (PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags)
  119. {
  120. *out_flags = 0;
  121. return in_flags;
  122. }
  123. static PRStatus method_getpeername (PRFileDesc *fd, PRNetAddr *addr)
  124. {
  125. BSocket *bsock = (BSocket *)fd->secret;
  126. BAddr baddr;
  127. if (BSocket_GetPeerName(bsock, &baddr) < 0) {
  128. PR_SetError(PR_UNKNOWN_ERROR, 0);
  129. return PR_FAILURE;
  130. }
  131. if (!baddr_to_prnetaddr(addr, baddr)) {
  132. PR_SetError(PR_UNKNOWN_ERROR, 0);
  133. return PR_FAILURE;
  134. }
  135. return PR_SUCCESS;
  136. }
  137. static PRStatus method_getsocketoption (PRFileDesc *fd, PRSocketOptionData *data)
  138. {
  139. switch (data->option) {
  140. case PR_SockOpt_Nonblocking:
  141. data->value.non_blocking = PR_TRUE;
  142. return PR_SUCCESS;
  143. }
  144. PR_SetError(PR_UNKNOWN_ERROR, 0);
  145. return PR_FAILURE;
  146. }
  147. static PRStatus method_setsocketoption (PRFileDesc *fd, const PRSocketOptionData *data)
  148. {
  149. PR_SetError(PR_UNKNOWN_ERROR, 0);
  150. return PR_FAILURE;
  151. }
  152. static PRIntn _PR_InvalidIntn (void)
  153. {
  154. ASSERT(0)
  155. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  156. return -1;
  157. }
  158. static PRInt32 _PR_InvalidInt32 (void)
  159. {
  160. ASSERT(0)
  161. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  162. return -1;
  163. }
  164. static PRInt64 _PR_InvalidInt64 (void)
  165. {
  166. ASSERT(0)
  167. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  168. return -1;
  169. }
  170. static PROffset32 _PR_InvalidOffset32 (void)
  171. {
  172. ASSERT(0)
  173. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  174. return -1;
  175. }
  176. static PROffset64 _PR_InvalidOffset64 (void)
  177. {
  178. ASSERT(0)
  179. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  180. return -1;
  181. }
  182. static PRStatus _PR_InvalidStatus (void)
  183. {
  184. ASSERT(0)
  185. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  186. return PR_FAILURE;
  187. }
  188. static PRFileDesc *_PR_InvalidDesc (void)
  189. {
  190. ASSERT(0)
  191. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  192. return NULL;
  193. }
  194. static PRIOMethods methods = {
  195. (PRDescType)0,
  196. method_close,
  197. method_read,
  198. method_write,
  199. (PRAvailableFN)_PR_InvalidInt32,
  200. (PRAvailable64FN)_PR_InvalidInt64,
  201. (PRFsyncFN)_PR_InvalidStatus,
  202. (PRSeekFN)_PR_InvalidOffset32,
  203. (PRSeek64FN)_PR_InvalidOffset64,
  204. (PRFileInfoFN)_PR_InvalidStatus,
  205. (PRFileInfo64FN)_PR_InvalidStatus,
  206. (PRWritevFN)_PR_InvalidInt32,
  207. (PRConnectFN)_PR_InvalidStatus,
  208. (PRAcceptFN)_PR_InvalidDesc,
  209. (PRBindFN)_PR_InvalidStatus,
  210. (PRListenFN)_PR_InvalidStatus,
  211. method_shutdown,
  212. method_recv,
  213. method_send,
  214. (PRRecvfromFN)_PR_InvalidInt32,
  215. (PRSendtoFN)_PR_InvalidInt32,
  216. method_poll,
  217. (PRAcceptreadFN)_PR_InvalidInt32,
  218. (PRTransmitfileFN)_PR_InvalidInt32,
  219. (PRGetsocknameFN)_PR_InvalidStatus,
  220. method_getpeername,
  221. (PRReservedFN)_PR_InvalidIntn,
  222. (PRReservedFN)_PR_InvalidIntn,
  223. method_getsocketoption,
  224. method_setsocketoption,
  225. (PRSendfileFN)_PR_InvalidInt32,
  226. (PRConnectcontinueFN)_PR_InvalidStatus,
  227. (PRReservedFN)_PR_InvalidIntn,
  228. (PRReservedFN)_PR_InvalidIntn,
  229. (PRReservedFN)_PR_InvalidIntn,
  230. (PRReservedFN)_PR_InvalidIntn
  231. };
  232. int BSocketPRFileDesc_GlobalInit (void)
  233. {
  234. ASSERT(!bsocketprfiledesc_initialized)
  235. if ((bsocketprfiledesc_identity = PR_GetUniqueIdentity("BSocketPRFileDesc")) == PR_INVALID_IO_LAYER) {
  236. return 0;
  237. }
  238. #ifndef NDEBUG
  239. bsocketprfiledesc_initialized = 1;
  240. #endif
  241. return 1;
  242. }
  243. void BSocketPRFileDesc_Create (PRFileDesc *prfd, BSocket *bsock)
  244. {
  245. ASSERT(bsocketprfiledesc_initialized)
  246. memset(prfd, 0, sizeof(prfd));
  247. prfd->methods = &methods;
  248. prfd->secret = (PRFilePrivate *)bsock;
  249. prfd->identity = bsocketprfiledesc_identity;
  250. }