BSocketPRFileDesc.c 7.0 KB

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