socket.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <Kernel/Syscall.h>
  28. #include <errno.h>
  29. #include <stdio.h>
  30. #include <sys/socket.h>
  31. extern "C" {
  32. int socket(int domain, int type, int protocol)
  33. {
  34. int rc = syscall(SC_socket, domain, type, protocol);
  35. __RETURN_WITH_ERRNO(rc, rc, -1);
  36. }
  37. int bind(int sockfd, const sockaddr* addr, socklen_t addrlen)
  38. {
  39. int rc = syscall(SC_bind, sockfd, addr, addrlen);
  40. __RETURN_WITH_ERRNO(rc, rc, -1);
  41. }
  42. int listen(int sockfd, int backlog)
  43. {
  44. int rc = syscall(SC_listen, sockfd, backlog);
  45. __RETURN_WITH_ERRNO(rc, rc, -1);
  46. }
  47. int accept(int sockfd, sockaddr* addr, socklen_t* addrlen)
  48. {
  49. int rc = syscall(SC_accept, sockfd, addr, addrlen);
  50. __RETURN_WITH_ERRNO(rc, rc, -1);
  51. }
  52. int connect(int sockfd, const sockaddr* addr, socklen_t addrlen)
  53. {
  54. int rc = syscall(SC_connect, sockfd, addr, addrlen);
  55. __RETURN_WITH_ERRNO(rc, rc, -1);
  56. }
  57. int shutdown(int sockfd, int how)
  58. {
  59. int rc = syscall(SC_shutdown, sockfd, how);
  60. __RETURN_WITH_ERRNO(rc, rc, -1);
  61. }
  62. ssize_t sendto(int sockfd, const void* data, size_t data_length, int flags, const struct sockaddr* addr, socklen_t addr_length)
  63. {
  64. Syscall::SC_sendto_params params { sockfd, { data, data_length }, flags, addr, addr_length };
  65. int rc = syscall(SC_sendto, &params);
  66. __RETURN_WITH_ERRNO(rc, rc, -1);
  67. }
  68. ssize_t send(int sockfd, const void* data, size_t data_length, int flags)
  69. {
  70. return sendto(sockfd, data, data_length, flags, nullptr, 0);
  71. }
  72. ssize_t recvfrom(int sockfd, void* buffer, size_t buffer_length, int flags, struct sockaddr* addr, socklen_t* addr_length)
  73. {
  74. Syscall::SC_recvfrom_params params { sockfd, { buffer, buffer_length }, flags, addr, addr_length };
  75. int rc = syscall(SC_recvfrom, &params);
  76. __RETURN_WITH_ERRNO(rc, rc, -1);
  77. }
  78. ssize_t recv(int sockfd, void* buffer, size_t buffer_length, int flags)
  79. {
  80. return recvfrom(sockfd, buffer, buffer_length, flags, nullptr, nullptr);
  81. }
  82. int getsockopt(int sockfd, int level, int option, void* value, socklen_t* value_size)
  83. {
  84. Syscall::SC_getsockopt_params params { sockfd, level, option, value, value_size };
  85. int rc = syscall(SC_getsockopt, &params);
  86. __RETURN_WITH_ERRNO(rc, rc, -1);
  87. }
  88. int setsockopt(int sockfd, int level, int option, const void* value, socklen_t value_size)
  89. {
  90. Syscall::SC_setsockopt_params params { sockfd, level, option, value, value_size };
  91. int rc = syscall(SC_setsockopt, &params);
  92. __RETURN_WITH_ERRNO(rc, rc, -1);
  93. }
  94. int getsockname(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
  95. {
  96. Syscall::SC_getsockname_params params { sockfd, addr, addrlen };
  97. int rc = syscall(SC_getsockname, &params);
  98. __RETURN_WITH_ERRNO(rc, rc, -1);
  99. }
  100. int getpeername(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
  101. {
  102. Syscall::SC_getpeername_params params { sockfd, addr, addrlen };
  103. int rc = syscall(SC_getpeername, &params);
  104. __RETURN_WITH_ERRNO(rc, rc, -1);
  105. }
  106. }