initial commit
[glibc.git] / sysdeps / unix / bsd / bsd4.4 / kfreebsd / bind.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
23 /* According to POSIX.1-2004 the len argument specifies the length of
24 the sockaddr structure pointed to by the addrarg argument. However
25 the FreeBSD kernel waits the actual length of the address stored
26 there. The code below emulate this behaviour. */
27
28 extern int __libc_sa_len (sa_family_t __af);
29 libc_hidden_proto (__libc_sa_len)
30
31 extern int __syscall_bind (int fd, __CONST_SOCKADDR_ARG addr,
32 socklen_t addrlen) __THROW;
33 libc_hidden_proto (__syscall_bind)
34
35 /* Open a connection on socket FD to peer at ADDR (which LEN bytes long).
36 For connectionless socket types, just set the default address to send to
37 and the only address from which to accept transmissions.
38 Return 0 on success, -1 for errors. */
39
40 int
41 __bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t addrlen)
42 {
43 socklen_t new_addrlen;
44
45 new_addrlen = __libc_sa_len ((addr.__sockaddr__)->sa_family);
46
47 /* Only allow a smaller size, otherwise it could lead to
48 stack corruption */
49 if ((new_addrlen != 0) && (new_addrlen < addrlen))
50 addrlen = new_addrlen;
51
52 /* We pass 3 arguments. */
53 return INLINE_SYSCALL (bind, 3, fd, addr.__sockaddr__, addrlen);
54 }
55
56 weak_alias (__bind, bind)