mod.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //! socket implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xns/syssocket.h.html
  2. use core::ptr;
  3. use platform::types::*;
  4. use platform::{PalSocket, Sys};
  5. pub mod constants;
  6. pub type sa_family_t = u16;
  7. pub type socklen_t = u32;
  8. #[repr(C)]
  9. #[derive(Default)]
  10. pub struct sockaddr {
  11. pub sa_family: sa_family_t,
  12. pub data: [c_char; 14],
  13. }
  14. #[no_mangle]
  15. pub unsafe extern "C" fn accept(
  16. socket: c_int,
  17. address: *mut sockaddr,
  18. address_len: *mut socklen_t,
  19. ) -> c_int {
  20. Sys::accept(
  21. socket,
  22. address,
  23. address_len,
  24. )
  25. }
  26. #[no_mangle]
  27. pub unsafe extern "C" fn bind(
  28. socket: c_int,
  29. address: *const sockaddr,
  30. address_len: socklen_t,
  31. ) -> c_int {
  32. Sys::bind(
  33. socket,
  34. address,
  35. address_len,
  36. )
  37. }
  38. #[no_mangle]
  39. pub unsafe extern "C" fn connect(
  40. socket: c_int,
  41. address: *const sockaddr,
  42. address_len: socklen_t,
  43. ) -> c_int {
  44. Sys::connect(
  45. socket,
  46. address,
  47. address_len,
  48. )
  49. }
  50. #[no_mangle]
  51. pub unsafe extern "C" fn getpeername(
  52. socket: c_int,
  53. address: *mut sockaddr,
  54. address_len: *mut socklen_t,
  55. ) -> c_int {
  56. Sys::getpeername(
  57. socket,
  58. address,
  59. address_len,
  60. )
  61. }
  62. #[no_mangle]
  63. pub unsafe extern "C" fn getsockname(
  64. socket: c_int,
  65. address: *mut sockaddr,
  66. address_len: *mut socklen_t,
  67. ) -> c_int {
  68. Sys::getsockname(
  69. socket,
  70. address,
  71. address_len,
  72. )
  73. }
  74. #[no_mangle]
  75. pub unsafe extern "C" fn getsockopt(
  76. socket: c_int,
  77. level: c_int,
  78. option_name: c_int,
  79. option_value: *mut c_void,
  80. option_len: *mut socklen_t,
  81. ) -> c_int {
  82. Sys::getsockopt(socket, level, option_name, option_value, option_len)
  83. }
  84. #[no_mangle]
  85. pub unsafe extern "C" fn listen(socket: c_int, backlog: c_int) -> c_int {
  86. Sys::listen(socket, backlog)
  87. }
  88. #[no_mangle]
  89. pub unsafe extern "C" fn recv(
  90. socket: c_int,
  91. buffer: *mut c_void,
  92. length: size_t,
  93. flags: c_int,
  94. ) -> ssize_t {
  95. recvfrom(
  96. socket,
  97. buffer,
  98. length,
  99. flags,
  100. ptr::null_mut(),
  101. ptr::null_mut(),
  102. )
  103. }
  104. #[no_mangle]
  105. pub unsafe extern "C" fn recvfrom(
  106. socket: c_int,
  107. buffer: *mut c_void,
  108. length: size_t,
  109. flags: c_int,
  110. address: *mut sockaddr,
  111. address_len: *mut socklen_t,
  112. ) -> ssize_t {
  113. Sys::recvfrom(
  114. socket,
  115. buffer,
  116. length,
  117. flags,
  118. address,
  119. address_len,
  120. )
  121. }
  122. #[no_mangle]
  123. pub unsafe extern "C" fn send(
  124. socket: c_int,
  125. message: *const c_void,
  126. length: size_t,
  127. flags: c_int,
  128. ) -> ssize_t {
  129. sendto(socket, message, length, flags, ptr::null(), 0)
  130. }
  131. #[no_mangle]
  132. pub unsafe extern "C" fn sendto(
  133. socket: c_int,
  134. message: *const c_void,
  135. length: size_t,
  136. flags: c_int,
  137. dest_addr: *const sockaddr,
  138. dest_len: socklen_t,
  139. ) -> ssize_t {
  140. Sys::sendto(
  141. socket,
  142. message,
  143. length,
  144. flags,
  145. dest_addr,
  146. dest_len,
  147. )
  148. }
  149. #[no_mangle]
  150. pub unsafe extern "C" fn setsockopt(
  151. socket: c_int,
  152. level: c_int,
  153. option_name: c_int,
  154. option_value: *const c_void,
  155. option_len: socklen_t,
  156. ) -> c_int {
  157. Sys::setsockopt(socket, level, option_name, option_value, option_len)
  158. }
  159. #[no_mangle]
  160. pub unsafe extern "C" fn shutdown(socket: c_int, how: c_int) -> c_int {
  161. Sys::shutdown(socket, how)
  162. }
  163. #[no_mangle]
  164. pub unsafe extern "C" fn socket(domain: c_int, kind: c_int, protocol: c_int) -> c_int {
  165. Sys::socket(domain, kind, protocol)
  166. }
  167. #[no_mangle]
  168. pub unsafe extern "C" fn socketpair(
  169. domain: c_int,
  170. kind: c_int,
  171. protocol: c_int,
  172. socket_vector: *mut c_int,
  173. ) -> c_int {
  174. Sys::socketpair(domain, kind, protocol, socket_vector)
  175. }