sock_ops.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. use core::ffi::c_void;
  2. use aya_ebpf_bindings::helpers::bpf_sock_ops_cb_flags_set;
  3. use crate::{bindings::bpf_sock_ops, EbpfContext};
  4. pub struct SockOpsContext {
  5. pub ops: *mut bpf_sock_ops,
  6. }
  7. impl SockOpsContext {
  8. pub fn new(ops: *mut bpf_sock_ops) -> SockOpsContext {
  9. SockOpsContext { ops }
  10. }
  11. pub fn op(&self) -> u32 {
  12. unsafe { (*self.ops).op }
  13. }
  14. pub fn family(&self) -> u32 {
  15. unsafe { (*self.ops).family }
  16. }
  17. pub fn cb_flags(&self) -> u32 {
  18. unsafe { (*self.ops).bpf_sock_ops_cb_flags }
  19. }
  20. pub fn set_cb_flags(&self, flags: i32) -> Result<(), i64> {
  21. let ret = unsafe { bpf_sock_ops_cb_flags_set(self.ops, flags) };
  22. if ret == 0 {
  23. Ok(())
  24. } else {
  25. Err(ret)
  26. }
  27. }
  28. pub fn remote_ip4(&self) -> u32 {
  29. unsafe { (*self.ops).remote_ip4 }
  30. }
  31. pub fn local_ip4(&self) -> u32 {
  32. unsafe { (*self.ops).local_ip4 }
  33. }
  34. pub fn remote_ip6(&self) -> [u32; 4] {
  35. unsafe { (*self.ops).remote_ip6 }
  36. }
  37. pub fn local_ip6(&self) -> [u32; 4] {
  38. unsafe { (*self.ops).local_ip6 }
  39. }
  40. pub fn local_port(&self) -> u32 {
  41. unsafe { (*self.ops).local_port }
  42. }
  43. pub fn remote_port(&self) -> u32 {
  44. unsafe { (*self.ops).remote_port }
  45. }
  46. pub fn arg(&self, n: usize) -> u32 {
  47. unsafe { (*self.ops).__bindgen_anon_1.args[n] }
  48. }
  49. pub fn set_reply(&mut self, reply: u32) {
  50. unsafe { (*self.ops).__bindgen_anon_1.reply = reply }
  51. }
  52. }
  53. impl EbpfContext for SockOpsContext {
  54. fn as_ptr(&self) -> *mut c_void {
  55. self.ops as *mut _
  56. }
  57. }