initial commit
[glibc.git] / sysdeps / unix / bsd / bsd4.4 / kfreebsd / sendto.c
1 /* Copyright (C) 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Aurelien Jarno <aurelien@aurel32.net>, 2005.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #include <sys/socket.h>
21 #include <sysdep.h>
22 #include <sysdep-cancel.h>
23
24 /* According to POSIX.1-2004 the len argument specifies the length of
25 the sockaddr structure pointed to by the addrarg argument. However
26 the FreeBSD kernel waits the actual length of the address stored
27 there. The code below emulate this behaviour. */
28
29 extern int __libc_sa_len (sa_family_t __af);
30 libc_hidden_proto (__libc_sa_len)
31
32 extern ssize_t __syscall_sendto (int fd, __const void * buf,
33 size_t n, int flags,
34 __CONST_SOCKADDR_ARG addr,
35 socklen_t addrlen);
36 libc_hidden_proto (__syscall_sendto)
37
38 /* Send N bytes of BUF on socket FD to peer at address ADDR (which is
39 * ADDR_LEN bytes long). Returns the number sent, or -1 for errors. */
40
41
42 ssize_t
43 __libc_sendto (int fd, __const void * buf, size_t n, int flags,
44 __CONST_SOCKADDR_ARG addr, socklen_t addrlen)
45 {
46 socklen_t new_addrlen;
47
48 if (addr.__sockaddr__)
49 {
50 new_addrlen = __libc_sa_len ((addr.__sockaddr__)->sa_family);
51
52 /* Only allow a smaller size, otherwise it could lead to
53 stack corruption */
54 if ((new_addrlen != 0) && (new_addrlen < addrlen))
55 addrlen = new_addrlen;
56 }
57
58 /* We pass 6 arguments. */
59 if (SINGLE_THREAD_P)
60 return INLINE_SYSCALL (sendto, 6, fd, buf, n, flags, addr.__sockaddr__, addrlen);
61
62 int oldtype = LIBC_CANCEL_ASYNC ();
63 int result = INLINE_SYSCALL (sendto, 6, fd, buf, n, flags, addr.__sockaddr__, addrlen);
64 LIBC_CANCEL_RESET (oldtype);
65 return result;
66 }
67
68 weak_alias (__libc_sendto, __sendto)
69 weak_alias (__libc_sendto, sendto)