Socket.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "exception/Er.h"
  16. #include "memory/Allocator.h"
  17. #include "util/events/Socket.h"
  18. #include "util/log/Log.h"
  19. #include "util/Identity.h"
  20. #include "util/CString.h"
  21. #include "wire/Message.h"
  22. #include "wire/Error.h"
  23. #include "benc/String.h"
  24. #include "rust/cjdns_sys/Rffi.h"
  25. Er_DEFUN(Iface_t* Socket_forFd(int fd,
  26. int socketType,
  27. struct Allocator* userAlloc))
  28. {
  29. Iface_t* iface = NULL;
  30. RTypes_Error_t* err = Rffi_socketForFd(&iface, fd, socketType, userAlloc);
  31. if (err) {
  32. Er_raise(userAlloc, "Socket_forFd(%d): %s", fd,
  33. Rffi_printError(err, userAlloc));
  34. }
  35. Er_ret(iface);
  36. }
  37. Er_DEFUN(Iface_t* Socket_connect(const char* path, Allocator_t* userAlloc))
  38. {
  39. Iface_t* iface = NULL;
  40. RTypes_Error_t* err = Rffi_unixSocketConnect(&iface, path, userAlloc);
  41. if (err) {
  42. Er_raise(userAlloc, "Socket_connect(%s): %s", path,
  43. Rffi_printError(err, userAlloc));
  44. }
  45. Er_ret(iface);
  46. }
  47. void Socket_serverOnConnect(
  48. Socket_Server_t* server, Socket_ServerOnConnect callback, void* callbackContext)
  49. {
  50. Rffi_unixSocketServerOnConnect(server->rustServer, callback, callbackContext);
  51. }
  52. Er_DEFUN(Socket_Server_t* Socket_server(const char* path, Allocator_t* userAlloc))
  53. {
  54. Socket_Server_t* out = Allocator_calloc(userAlloc, sizeof(Socket_Server_t), 1);
  55. RTypes_Error_t* err = Rffi_unixSocketServer(
  56. &out->rustServer,
  57. &out->iface, path,
  58. userAlloc
  59. );
  60. if (err) {
  61. Er_raise(userAlloc, "Socket_server(%s): %s", path,
  62. Rffi_printError(err, userAlloc));
  63. }
  64. Er_ret(out);
  65. }
  66. Er_DEFUN(bool Socket_fileExists(const char* path, Allocator_t* errAlloc))
  67. {
  68. bool exists = false;
  69. RTypes_Error_t* err = Rffi_fileExists(&exists, path, errAlloc);
  70. if (err) {
  71. Er_raise(errAlloc, "Socket_fileExists(%s): %s", path,
  72. Rffi_printError(err, errAlloc));
  73. }
  74. Er_ret(exists);
  75. }