1 /** @file
2   Implement the bind API.
3 
4   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.
9 
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.
12 **/
13 #include <SocketInternals.h>
14 
15 
16 /** Bind a name to a socket.
17 
18   The bind routine connects a name (network address) to a socket on the local machine.
19 
20   The
21   <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html">POSIX</a>
22   documentation is available online.
23 
24   @param[in] s         Socket file descriptor returned from ::socket.
25 
26   @param[in] name      Address of a sockaddr structure that contains the
27                         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 
36   @param[in] namelen   Specifies the length in bytes of the sockaddr structure.
37 
38   @return     The bind routine returns zero (0) if successful and -1 upon failure.
39               In the case of an error, ::errno contains more information.
40  **/
41 int
bind(IN int s,IN const struct sockaddr * name,IN socklen_t namelen)42 bind (
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;
50 
51   //  Locate the context for this socket
52   pSocketProtocol = BslFdToSocketProtocol ( s, NULL, &errno );
53   if ( NULL != pSocketProtocol ) {
54 
55     //  Bind the socket
56     (void) pSocketProtocol->pfnBind ( pSocketProtocol,
57                                         name,
58                                         namelen,
59                                         &errno );
60   }
61 
62   //  Return the operation stauts
63   BindStatus = ( 0 == errno ) ? 0 : -1;
64   return BindStatus;
65 }
66