blob: a52272d7ce598949f28664946d831424b2011b4a [file] [log] [blame]
darylm503d7ce7002011-07-30 00:30:44 +00001/** @file
2 Implement the bind API.
3
Olivier Martinbeaaa3b2014-10-31 17:50:33 +00004 Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
darylm503d7ce7002011-07-30 00:30:44 +00009
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
darylm503d7ce7002011-07-30 00:30:44 +000012**/
darylm503d7ce7002011-07-30 00:30:44 +000013#include <SocketInternals.h>
14
15
Olivier Martinbeaaa3b2014-10-31 17:50:33 +000016/** Bind a name to a socket.
darylm503d7ce7002011-07-30 00:30:44 +000017
lpleahya88c3162011-09-30 23:02:35 +000018 The bind routine connects a name (network address) to a socket on the local machine.
19
20 The
darylm503d7ce7002011-07-30 00:30:44 +000021 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html">POSIX</a>
lpleahya88c3162011-09-30 23:02:35 +000022 documentation is available online.
darylm503d7ce7002011-07-30 00:30:44 +000023
darylm5037dc13292011-08-05 23:57:34 +000024 @param[in] s Socket file descriptor returned from ::socket.
darylm503d7ce7002011-07-30 00:30:44 +000025
darylm5037dc13292011-08-05 23:57:34 +000026 @param[in] name Address of a sockaddr structure that contains the
darylm503d7ce7002011-07-30 00:30:44 +000027 connection point on the local machine. An IPv4 address
28 of INADDR_ANY specifies that the connection is made to
29 all of the network stacks on the platform. Specifying a
30 specific IPv4 address restricts the connection to the
31 network stack supporting that address. Specifying zero
32 for the port causes the network layer to assign a port
33 number from the dynamic range. Specifying a specific
34 port number causes the network layer to use that port.
35
darylm5037dc13292011-08-05 23:57:34 +000036 @param[in] namelen Specifies the length in bytes of the sockaddr structure.
darylm503d7ce7002011-07-30 00:30:44 +000037
darylm5037dc13292011-08-05 23:57:34 +000038 @return The bind routine returns zero (0) if successful and -1 upon failure.
lpleahya88c3162011-09-30 23:02:35 +000039 In the case of an error, ::errno contains more information.
darylm503d7ce7002011-07-30 00:30:44 +000040 **/
41int
42bind (
43 IN int s,
44 IN const struct sockaddr * name,
45 IN socklen_t namelen
46 )
47{
48 int BindStatus;
49 EFI_SOCKET_PROTOCOL * pSocketProtocol;
darylm503d7ce7002011-07-30 00:30:44 +000050
darylm503d7ce7002011-07-30 00:30:44 +000051 // Locate the context for this socket
darylm503d7ce7002011-07-30 00:30:44 +000052 pSocketProtocol = BslFdToSocketProtocol ( s, NULL, &errno );
53 if ( NULL != pSocketProtocol ) {
Olivier Martinbeaaa3b2014-10-31 17:50:33 +000054
darylm503d7ce7002011-07-30 00:30:44 +000055 // Bind the socket
Olivier Martinbeaaa3b2014-10-31 17:50:33 +000056 (void) pSocketProtocol->pfnBind ( pSocketProtocol,
darylm503d7ce7002011-07-30 00:30:44 +000057 name,
58 namelen,
59 &errno );
60 }
61
darylm503d7ce7002011-07-30 00:30:44 +000062 // Return the operation stauts
darylm503d7ce7002011-07-30 00:30:44 +000063 BindStatus = ( 0 == errno ) ? 0 : -1;
64 return BindStatus;
65}