]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/socket.c
2 Implement the socket API.
4 Copyright (c) 2011, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
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.
15 #include <SocketInternals.h>
18 const struct fileops SocketOperations
= {
19 BslSocketClose
, // close
20 BslSocketRead
, // read
21 BslSocketWrite
, // write
26 fnullop_fcntl
, // fcntl
27 BslSocketPoll
, // poll
28 fnullop_flush
, // flush
31 fbadop_ioctl
, // ioctl
32 fbadop_delete
, // delete
33 fbadop_rmdir
, // rmdir
34 fbadop_mkdir
, // mkdir
35 fbadop_rename
, // rename
42 Translate from the socket file descriptor to the socket protocol.
44 @param [in] s Socket file descriptor returned from ::socket.
46 @param [in] ppDescriptor Address to receive the descriptor structure
48 @param [in] pErrno Address of the errno variable
50 @return A pointer to the socket protocol structure or NULL if
51 an invalid file descriptor was passed in.
55 BslFdToSocketProtocol (
57 struct __filedes
** ppDescriptor
,
61 struct __filedes
* pDescriptor
;
62 EFI_SOCKET_PROTOCOL
* pSocketProtocol
;
67 pSocketProtocol
= NULL
;
70 // Validate the file descriptor
72 if ( !ValidateFD ( s
, TRUE
)) {
74 // Bad file descriptor
80 // Get the descriptor for the file
82 pDescriptor
= &gMD
->fdarray
[ s
];
85 // Validate that the descriptor is associated with sockets
87 pSocketProtocol
= BslValidateSocketFd ( pDescriptor
, pErrno
);
88 if (( NULL
!= ppDescriptor
) && ( NULL
!= pSocketProtocol
)) {
89 *ppDescriptor
= pDescriptor
;
94 // Return the socket protocol
96 return pSocketProtocol
;
101 Build a file descriptor for a socket.
103 @param [in] pSocketProtocol Socket protocol structure address
105 @param [in] pErrno Address of the errno variable
107 @return The file descriptor for the socket or -1 if an error occurs.
111 BslSocketProtocolToFd (
112 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
117 struct __filedes
* pDescriptor
;
125 // Locate a file descriptor
127 FileDescriptor
= FindFreeFD ( VALID_CLOSED
);
128 if( FileDescriptor
< 0 ) {
130 // All available FDs are in use
136 // Initialize the file descriptor
138 pDescriptor
= &gMD
->fdarray
[ FileDescriptor
];
139 pDescriptor
->f_offset
= 0;
140 pDescriptor
->f_flag
= 0;
141 pDescriptor
->f_iflags
= DTYPE_SOCKET
;
142 pDescriptor
->MyFD
= (UINT16
)FileDescriptor
;
143 pDescriptor
->Oflags
= 0;
144 pDescriptor
->Omode
= S_ACC_READ
| S_ACC_WRITE
;
145 pDescriptor
->RefCount
= 1;
146 FILE_SET_MATURE ( pDescriptor
);
149 // Socket specific file descriptor initialization
151 pDescriptor
->devdata
= pSocketProtocol
;
152 pDescriptor
->f_ops
= &SocketOperations
;
156 // Return the socket's file descriptor
158 return FileDescriptor
;
163 Creates an endpoint for network communication.
165 The ::Socket routine initializes the communication endpoint by providing
166 the support for the socket library function ::socket. The
167 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html">POSIX</a>
168 documentation for the socket routine is available online for reference.
170 @param [in] domain Select the family of protocols for the client or server
173 @param [in] type Specifies how to make the network connection. The following values
177 SOCK_STREAM - Connect to TCP, provides a byte stream
178 that is manipluated by read, recv, send and write.
181 SOCK_SEQPACKET - Connect to TCP, provides sequenced packet stream
182 that is manipulated by read, recv, send and write.
185 SOCK_DGRAM - Connect to UDP, provides a datagram service that is
186 manipulated by recvfrom and sendto.
190 @param [in] protocol Specifies the lower layer protocol to use. The following
191 values are supported:
193 <li>IPPROTO_TCP</li> - This value must be combined with SOCK_STREAM.</li>
194 <li>IPPROTO_UDP</li> - This value must be combined with SOCK_DGRAM.</li>
197 @return This routine returns a file descriptor for the socket.
207 INT32 FileDescriptor
;
208 EFI_SOCKET_PROTOCOL
* pSocketProtocol
;
217 // Locate the socket protocol
219 errno
= EslServiceGetProtocol ( &pSocketProtocol
);
222 // Initialize the socket
224 Status
= pSocketProtocol
->pfnSocket ( pSocketProtocol
,
229 if ( !EFI_ERROR ( Status
))
232 // Build the file descriptor for the socket
234 FileDescriptor
= BslSocketProtocolToFd ( pSocketProtocol
,
240 // Return the socket's file descriptor
242 return FileDescriptor
;
247 Validate the socket's file descriptor
249 @param [in] pDescriptor Descriptor for the file
251 @param [in] pErrno Address of the errno variable
253 @return A pointer to the socket protocol structure or NULL if
254 an invalid file descriptor was passed in.
257 EFI_SOCKET_PROTOCOL
*
258 BslValidateSocketFd (
259 struct __filedes
* pDescriptor
,
263 EFI_SOCKET_PROTOCOL
* pSocketProtocol
;
269 pSocketProtocol
= NULL
;
272 // Validate that the descriptor is associated with sockets
274 if ( DTYPE_SOCKET
== ( pDescriptor
->f_iflags
& DTYPE_MASK
)) {
276 // Locate the socket protocol
278 pSocketProtocol
= pDescriptor
->devdata
;
283 // Return the socket protocol
285 return pSocketProtocol
;