summaryrefslogtreecommitdiff
path: root/StdLib/BsdSocketLib/socket.c
blob: e78329291cdd21c5c6a0215b63786e299769ca0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/** @file
  Implement the socket API.

  Copyright (c) 2011, Intel Corporation
  All rights reserved. This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <SocketInternals.h>


const struct fileops SocketOperations = {
  BslSocketClose,     //  close
  BslSocketRead,      //  read
  BslSocketWrite,     //  write

  //
  //  Not supported
  //
  fnullop_fcntl,      //  fcntl
  BslSocketPoll,      //  poll
  fnullop_flush,      //  flush

  fbadop_stat,        //  stat
  fbadop_ioctl,       //  ioctl
  fbadop_delete,      //  delete
  fbadop_rmdir,       //  rmdir
  fbadop_mkdir,       //  mkdir
  fbadop_rename,      //  rename

  NULL                //  lseek
};


/**
  Translate from the socket file descriptor to the socket protocol.

  @param [in] s             Socket file descriptor returned from ::socket.

  @param [in] ppDescriptor  Address to receive the descriptor structure
                            address for the file
  @param [in] pErrno        Address of the errno variable

  @return   A pointer to the socket protocol structure or NULL if
            an invalid file descriptor was passed in.

 **/
EFI_SOCKET_PROTOCOL *
BslFdToSocketProtocol (
  int s,
  struct __filedes ** ppDescriptor,
  int * pErrno
  )
{
  struct __filedes * pDescriptor;
  EFI_SOCKET_PROTOCOL * pSocketProtocol;

  //
  //  Assume failure
  //
  pSocketProtocol = NULL;

  //
  //  Validate the file descriptor
  //
  if ( !ValidateFD ( s, TRUE )) {
    //
    //  Bad file descriptor
    //
    *pErrno = EBADF;
  }
  else {
    //
    //  Get the descriptor for the file
    //
    pDescriptor = &gMD->fdarray [ s ];

    //
    //  Validate that the descriptor is associated with sockets
    //
    pSocketProtocol = BslValidateSocketFd ( pDescriptor, pErrno );
    if (( NULL != ppDescriptor ) && ( NULL != pSocketProtocol )) {
      *ppDescriptor = pDescriptor;
    }
  }

  //
  //  Return the socket protocol
  //
  return pSocketProtocol;
}


/**
  Build a file descriptor for a socket.

  @param [in] pSocketProtocol   Socket protocol structure address

  @param [in] pErrno            Address of the errno variable

  @return  The file descriptor for the socket or -1 if an error occurs.

 **/
int
BslSocketProtocolToFd (
  IN EFI_SOCKET_PROTOCOL * pSocketProtocol,
  IN int * pErrno
  )
{
  int FileDescriptor;
  struct __filedes * pDescriptor;

  //
  //  Assume failure
  //
  FileDescriptor = -1;

  //
  //  Locate a file descriptor
  //
  FileDescriptor = FindFreeFD ( VALID_CLOSED );
  if( FileDescriptor < 0 ) {
    //
    // All available FDs are in use
    //
    errno = EMFILE;
  }
  else {
    //
    //  Initialize the file descriptor
    //
    pDescriptor = &gMD->fdarray [ FileDescriptor ];
    pDescriptor->f_offset = 0;
    pDescriptor->f_flag = 0;
    pDescriptor->f_iflags = DTYPE_SOCKET;
    pDescriptor->MyFD = (UINT16)FileDescriptor;
    pDescriptor->Oflags = 0;
    pDescriptor->Omode = S_ACC_READ | S_ACC_WRITE;
    pDescriptor->RefCount = 1;
    FILE_SET_MATURE ( pDescriptor );

    //
    //  Socket specific file descriptor initialization
    //
    pDescriptor->devdata = pSocketProtocol;
    pDescriptor->f_ops = &SocketOperations;
  }

  //
  //  Return the socket's file descriptor
  //
  return FileDescriptor;
}


/**
  Creates an endpoint for network communication.

  The ::Socket routine initializes the communication endpoint by providing
  the support for the socket library function ::socket.  The
  <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html">POSIX</a>
  documentation for the socket routine is available online for reference.

  @param [in] domain    Select the family of protocols for the client or server
                        application.

  @param [in] type      Specifies how to make the network connection.  The following values
                        are supported:
                        <ul>
                          <li>
                            SOCK_STREAM - Connect to TCP, provides a byte stream
                            that is manipluated by read, recv, send and write.
                          </li>
                          <li>
                            SOCK_SEQPACKET - Connect to TCP, provides sequenced packet stream
                            that is manipulated by read, recv, send and write.
                          </li>
                          <li>
                            SOCK_DGRAM - Connect to UDP, provides a datagram service that is
                            manipulated by recvfrom and sendto.
                          </li>
                        </ul>

  @param [in] protocol  Specifies the lower layer protocol to use.  The following
                        values are supported:
                        <ul>
                          <li>IPPROTO_TCP</li> - This value must be combined with SOCK_STREAM.</li>
                          <li>IPPROTO_UDP</li> - This value must be combined with SOCK_DGRAM.</li>
                        </ul>

  @return  This routine returns a file descriptor for the socket.

 **/
INT32
socket (
  IN INT32 domain,
  IN INT32 type,
  IN INT32 protocol
  )
{
  INT32 FileDescriptor;
  EFI_SOCKET_PROTOCOL * pSocketProtocol;
  EFI_STATUS Status;

  //
  //  Assume failure
  //
  FileDescriptor = -1;

  //
  //  Locate the socket protocol
  //
  errno = EslServiceGetProtocol ( &pSocketProtocol );
  if ( 0 == errno ) {
    //
    //  Initialize the socket
    //
    Status = pSocketProtocol->pfnSocket ( pSocketProtocol,
                                          domain,
                                          type,
                                          protocol,
                                          &errno );
    if ( !EFI_ERROR ( Status ))
    {
      //
      //  Build the file descriptor for the socket
      //
      FileDescriptor = BslSocketProtocolToFd ( pSocketProtocol,
                                               &errno );
    }
  }

  //
  //  Return the socket's file descriptor
  //
  return FileDescriptor;
}


/**
  Validate the socket's file descriptor

  @param [in] pDescriptor Descriptor for the file

  @param [in] pErrno      Address of the errno variable

  @return   A pointer to the socket protocol structure or NULL if
            an invalid file descriptor was passed in.

 **/
EFI_SOCKET_PROTOCOL *
BslValidateSocketFd (
  struct __filedes * pDescriptor,
  int * pErrno
  )
{
  EFI_SOCKET_PROTOCOL * pSocketProtocol;

  //
  //  Assume failure
  //
  *pErrno = ENOTSOCK;
  pSocketProtocol = NULL;

  //
  //  Validate that the descriptor is associated with sockets
  //
  if ( DTYPE_SOCKET == ( pDescriptor->f_iflags & DTYPE_MASK )) {
    //
    //  Locate the socket protocol
    //
    pSocketProtocol = pDescriptor->devdata;
    *pErrno = 0;
  }

  //
  //  Return the socket protocol
  //
  return pSocketProtocol;
}