# sendto _____________________________________________________
#
# long sys_sendto(int fd,
#		void __user * buff,
#		size_t len,
#		unsigned flags,
#		struct sockaddr __user *addr,
#		int addr_len)
#

@define _SYSCALL_SENDTO_NAME
%(
	name = "sendto"
%)

@define _SYSCALL_SENDTO_ARGSTR
%(
	argstr = sprintf("%d, %s, %u, %s, %s, %u", s, buf, len, flags_str, to_str, tolen)
%)

@define _SYSCALL_SENDTO_REGARGS
%(
	s = int_arg(1)
	buf_uaddr = pointer_arg(2)
	len = ulong_arg(3)
	flags = uint_arg(4)
	to_uaddr = pointer_arg(5)
	tolen = uint_arg(6)
%)

@define _SYSCALL_SENDTO_REGARGS_STORE
%(
        if (@probewrite(s))
          set_int_arg(1, s)

	if (@probewrite(buf_uaddr))
	  set_pointer_arg(2, buf_uaddr)

	if (@probewrite(len))
	  set_ulong_arg(3, len)

	if (@probewrite(flags))
	  set_uint_arg(4, flags)

	if (@probewrite(to_uaddr))
	  set_pointer_arg(5, to_uaddr)

	if (@probewrite(tolen))
	  set_uint_arg(6, tolen)
%)

@define _SYSCALL_SOCKETCALL_SENDTO_REGARGS
%(
	__args = &@ulong_cast(pointer_arg(2))
	s = __int32(user_ulong(&@ulong_cast(__args)[0]))
	buf_uaddr = user_ulong(&@ulong_cast(__args)[1])
	len = user_ulong(&@ulong_cast(__args)[2])
	flags = __uint32(user_ulong(&@ulong_cast(__args)[3]))
	to_uaddr = user_ulong(&@ulong_cast(__args)[4])
	tolen = __uint32(user_ulong(&@ulong_cast(__args)[5]))
%)

@define _SYSCALL_COMPAT_SOCKETCALL_SENDTO_REGARGS
%(
	__args = &@uint_cast(pointer_arg(2))
	s = user_int(&@uint_cast(__args)[0])
	buf_uaddr = user_uint32(&@uint_cast(__args)[1])
	len = user_uint32(&@uint_cast(__args)[2])
	flags = user_uint32(&@uint_cast(__args)[3])
	to_uaddr = user_uint32(&@uint_cast(__args)[4])
	tolen = user_uint32(&@uint_cast(__args)[5])
%)

probe syscall.sendto = dw_syscall.sendto !, nd_syscall.sendto ? {}
probe syscall.sendto.return = dw_syscall.sendto.return !,
                              nd_syscall.sendto.return ? {}

# dw_sendto _____________________________________________________

probe dw_syscall.sendto = __syscall.sendto ?, __syscall.socketcall.sendto ?,
	__syscall.compat_socketcall.sendto ?
{
	@_SYSCALL_SENDTO_NAME
	flags_str = _msg_flags_str(flags)
	buf = user_buffer_quoted(buf_uaddr, len, syscall_string_trunc)
	to_str = _struct_sockaddr_u(to_uaddr, tolen)
	@_SYSCALL_SENDTO_ARGSTR
}
probe __syscall.sendto = kernel.function("sys_sendto").call ?
{
	@__syscall_gate(@const("__NR_sendto"))
	s = __int32($fd)
	buf_uaddr = $buff
	len = __ulong($len)
	flags = __uint32($flags)
	to_uaddr = $addr
	tolen = __uint32($addr_len)
}
probe __syscall.socketcall.sendto = kernel.function("sys_socketcall").call ?
{
	if (__int32($call) != @const("SYS_SENDTO")) next;
	s = __int32(user_ulong(&@ulong_cast($args)[0]))
	buf_uaddr = user_ulong(&@ulong_cast($args)[1])
	len = user_ulong(&@ulong_cast($args)[2])
	flags = __uint32(user_ulong(&@ulong_cast($args)[3]))
	to_uaddr = user_ulong(&@ulong_cast($args)[4])
	tolen = __uint32(user_ulong(&@ulong_cast($args)[5]))
}
probe __syscall.compat_socketcall.sendto =
	kernel.function("compat_sys_socketcall").call ?
{
	if (__int32($call) != @const("SYS_SENDTO")) next;
	s = user_int(&@uint_cast($args)[0])
	buf_uaddr = user_uint32(&@uint_cast($args)[1])
	len = user_uint32(&@uint_cast($args)[2])
	flags = user_uint32(&@uint_cast($args)[3])
	to_uaddr = user_uint32(&@uint_cast($args)[4])
	tolen = user_uint32(&@uint_cast($args)[5])
}
probe dw_syscall.sendto.return = kernel.function("sys_sendto").return ?,
	kernel.function("sys_socketcall").return ?,
	kernel.function("compat_sys_socketcall").return ?
{
	if (@defined(@entry($call))) {
		if (__int32(@entry($call)) != @const("SYS_SENDTO")) next;
	}
	else {
		@__syscall_gate(@const("__NR_sendto"))
	}
	@_SYSCALL_SENDTO_NAME
	@SYSC_RETVALSTR($return)
}

# nd_sendto _____________________________________________________

probe nd_syscall.sendto = nd1_syscall.sendto!, nd2_syscall.sendto!, tp_syscall.sendto
  { }

probe nd1_syscall.sendto = __nd1_syscall.sendto ?,
	__nd1_syscall.socketcall.sendto ?,
	__nd1_syscall.compat_socketcall.sendto ?
{
	@_SYSCALL_SENDTO_NAME
	flags_str = _msg_flags_str(flags)
	buf = user_buffer_quoted(buf_uaddr, len, syscall_string_trunc)
	to_str = _struct_sockaddr_u(to_uaddr, tolen)
	@_SYSCALL_SENDTO_ARGSTR
}
probe __nd1_syscall.sendto = kprobe.function("sys_sendto").call ?
{
	asmlinkage()
	@__syscall_gate(@const("__NR_sendto"))
	@_SYSCALL_SENDTO_REGARGS
}
probe __nd1_syscall.socketcall.sendto = kprobe.function("sys_socketcall").call ?
{
	asmlinkage()
	if (int_arg(1) != @const("SYS_SENDTO")) next;
	@_SYSCALL_SOCKETCALL_SENDTO_REGARGS
}
probe __nd1_syscall.compat_socketcall.sendto =
	kprobe.function("compat_sys_socketcall").call ?
{
	asmlinkage()
	if (int_arg(1) != @const("SYS_SENDTO")) next;
	@_SYSCALL_COMPAT_SOCKETCALL_SENDTO_REGARGS
}

/* kernel 4.17+ */
probe nd2_syscall.sendto = __nd2_syscall.sendto ?,
	__nd2_syscall.socketcall.sendto ?,
	__nd2_syscall.compat_socketcall.sendto ?
{
	@_SYSCALL_SENDTO_NAME
	flags_str = _msg_flags_str(flags)
	buf = user_buffer_quoted(buf_uaddr, len, syscall_string_trunc)
	to_str = _struct_sockaddr_u(to_uaddr, tolen)
	@_SYSCALL_SENDTO_ARGSTR
}
probe __nd2_syscall.sendto = kprobe.function(@arch_syscall_prefix "sys_sendto") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SENDTO_REGARGS
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SENDTO_REGARGS_STORE %)
}
probe __nd2_syscall.socketcall.sendto = kprobe.function(@arch_syscall_prefix "sys_socketcall").call ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	if (int_arg(1) != @const("SYS_SENDTO")) next;
	@_SYSCALL_SOCKETCALL_SENDTO_REGARGS
}
probe __nd2_syscall.compat_socketcall.sendto =
	kprobe.function(@arch_syscall_prefix "compat_sys_socketcall").call ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	if (int_arg(1) != @const("SYS_SENDTO")) next;
	@_SYSCALL_COMPAT_SOCKETCALL_SENDTO_REGARGS
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.sendto = __tp_syscall.sendto ?,
	__tp_syscall.socketcall.sendto ?
{
	@_SYSCALL_SENDTO_NAME
	flags_str = _msg_flags_str(flags)
	buf = user_buffer_quoted(buf_uaddr, len, syscall_string_trunc)
	to_str = _struct_sockaddr_u(to_uaddr, tolen)
	@_SYSCALL_SENDTO_ARGSTR
}
probe __tp_syscall.sendto = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_sendto"))
	@_SYSCALL_SENDTO_REGARGS
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_SENDTO_REGARGS_STORE %)
}
probe __tp_syscall.socketcall.sendto = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_socketcall"), @const("__NR_compat_socketcall"))
	if (int_arg(1) != @const("SYS_SENDTO")) next;
	if (@__compat_task) {
		@_SYSCALL_COMPAT_SOCKETCALL_SENDTO_REGARGS
	}
	else {
		@_SYSCALL_SOCKETCALL_SENDTO_REGARGS
	}
}

probe nd_syscall.sendto.return = nd1_syscall.sendto.return!, nd2_syscall.sendto.return!, tp_syscall.sendto.return
  { }

probe nd1_syscall.sendto.return = __nd1_syscall.sendto.return ?,
	__nd1_syscall.socketcall.sendto.return ?
{
	@_SYSCALL_SENDTO_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.sendto.return = kprobe.function("sys_sendto").return ?
{
	@__syscall_gate(@const("__NR_sendto"))
}
probe __nd1_syscall.socketcall.recv.return =
	kprobe.function("sys_socketcall").return ?,
	kprobe.function("compat_sys_socketcall").return ?
{
	if (@entry(__asmlinkage_int_arg(1)) != @const("SYS_SENDTO")) next;
}

/* kernel 4.17+ */
probe nd2_syscall.sendto.return =
	kprobe.function(@arch_syscall_prefix "sys_sendto").return ?,
	__nd2_syscall.socketcall.sendto.return ?
{
	@_SYSCALL_SENDTO_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd2_syscall.socketcall.sendto.return =
	kprobe.function(@arch_syscall_prefix "sys_socketcall").return ?,
	kprobe.function(@arch_syscall_prefix "compat_sys_socketcall").return ?
{
	__set_syscall_pt_regs(@entry(pointer_arg(1)))
	if (int_arg(1) != @const("SYS_SENDTO")) next;
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.sendto.return = __tp_syscall.sendto.return ?,
	__tp_syscall.socketcall.sendto.return ?
{
	@_SYSCALL_SENDTO_NAME
	@SYSC_RETVALSTR($ret)
}
probe __tp_syscall.sendto.return = kernel.trace("sys_exit")
{
	@__syscall_gate(@const("__NR_sendto"))
}
probe __tp_syscall.socketcall.sendto.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_socketcall"), @const("__NR_compat_socketcall"))
	if (int_arg(1) != @const("SYS_SENDTO")) next;
}
