1 /** @file
2   Socket header file.
3 
4 Copyright (c) 2005 - 2012, Intel Corporation. All rights reserved.<BR>
5 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<BR>
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 **/
14 
15 #ifndef _SOCKET_H_
16 #define _SOCKET_H_
17 
18 #include <Uefi.h>
19 
20 #include <Protocol/Ip4.h>
21 #include <Protocol/Tcp4.h>
22 #include <Protocol/Udp4.h>
23 
24 #include <Library/NetLib.h>
25 #include <Library/DebugLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/UefiRuntimeServicesTableLib.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/UefiDriverEntryPoint.h>
31 #include <Library/UefiLib.h>
32 #include <Library/DpcLib.h>
33 #include <Library/PrintLib.h>
34 
35 #define SOCK_SND_BUF        0
36 #define SOCK_RCV_BUF        1
37 
38 #define SOCK_BUFF_LOW_WATER (2 * 1024)
39 #define SOCK_RCV_BUFF_SIZE  (8 * 1024)
40 #define SOCK_SND_BUFF_SIZE  (8 * 1024)
41 #define SOCK_BACKLOG        5
42 
43 #define PROTO_RESERVED_LEN  20
44 
45 #define SO_NO_MORE_DATA     0x0001
46 
47 //
48 //
49 //
50 // When a socket is created it enters into SO_UNCONFIGURED,
51 // no actions can be taken on this socket, only after calling
52 // SockConfigure. The state transition diagram of socket is
53 // as following:
54 //
55 // SO_UNCONFIGURED --- SO_CONFIGURED --- SO_CONNECTING
56 //  ^      |                                    |
57 //  |      --->  SO_LISTENING                   |
58 //  |                                           |
59 //  |------------------SO_DISCONNECTING<-- SO_CONNECTED
60 //
61 // A passive socket can only go into SO_LISTENING and
62 // SO_UNCONFIGURED state. SO_XXXING state is a middle state
63 // when a socket is undergoing a protocol procedure such
64 // as requesting a TCP connection.
65 //
66 //
67 //
68 
69 ///
70 /// Socket state
71 ///
72 #define SO_CLOSED        0
73 #define SO_LISTENING     1
74 #define SO_CONNECTING    2
75 #define SO_CONNECTED     3
76 #define SO_DISCONNECTING 4
77 
78 ///
79 /// Socket configure state
80 ///
81 #define SO_UNCONFIGURED        0
82 #define SO_CONFIGURED_ACTIVE   1
83 #define SO_CONFIGURED_PASSIVE  2
84 #define SO_NO_MAPPING          3
85 
86 /**
87   Set socket SO_NO_MORE_DATA flag.
88 
89   @param Sock               Pointer to the socket
90 
91 **/
92 #define SOCK_NO_MORE_DATA(Sock)     ((Sock)->Flag |= SO_NO_MORE_DATA)
93 
94 /**
95   Check whether the socket is unconfigured.
96 
97   @param  Sock               Pointer to the socket
98 
99   @retval True               The socket is unconfigued
100   @retval False              The socket is not unconfigued
101 
102 **/
103 #define SOCK_IS_UNCONFIGURED(Sock)  ((Sock)->ConfigureState == SO_UNCONFIGURED)
104 
105 /**
106   Check whether the socket is configured.
107 
108   @param  Sock               Pointer to the socket
109 
110   @retval True               The socket is configued
111   @retval False              The socket is not configued
112 
113 **/
114 #define SOCK_IS_CONFIGURED(Sock) \
115     (((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE) || \
116     ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE))
117 
118 /**
119   Check whether the socket is configured to active mode.
120 
121   @param  Sock               Pointer to the socket
122 
123   @retval True               The socket is configued to active mode
124   @retval False              The socket is not configued to active mode
125 
126 **/
127 #define SOCK_IS_CONFIGURED_ACTIVE(Sock) \
128   ((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE)
129 
130 /**
131   Check whether the socket is configured to passive mode.
132 
133   @param  Sock               Pointer to the socket
134 
135   @retval True               The socket is configued to passive mode
136   @retval False              The socket is not configued to passive mode
137 
138 **/
139 #define SOCK_IS_CONNECTED_PASSIVE(Sock) \
140   ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE)
141 
142 /**
143   Check whether the socket is mapped.
144 
145   @param  Sock               Pointer to the socket
146 
147   @retval True               The socket is no mapping
148   @retval False              The socket is mapped
149 
150 **/
151 #define SOCK_IS_NO_MAPPING(Sock) \
152   ((Sock)->ConfigureState == SO_NO_MAPPING)
153 
154 /**
155   Check whether the socket is closed.
156 
157   @param  Sock               Pointer to the socket
158 
159   @retval True               The socket is closed
160   @retval False              The socket is not closed
161 
162 **/
163 #define SOCK_IS_CLOSED(Sock)          ((Sock)->State == SO_CLOSED)
164 
165 /**
166   Check whether the socket is listening.
167 
168   @param  Sock               Pointer to the socket
169 
170   @retval True               The socket is listening
171   @retval False              The socket is not listening
172 
173 **/
174 #define SOCK_IS_LISTENING(Sock)       ((Sock)->State == SO_LISTENING)
175 
176 /**
177   Check whether the socket is connecting.
178 
179   @param  Sock               Pointer to the socket
180 
181   @retval True               The socket is connecting
182   @retval False              The socket is not connecting
183 
184 **/
185 #define SOCK_IS_CONNECTING(Sock)      ((Sock)->State == SO_CONNECTING)
186 
187 /**
188   Check whether the socket has connected.
189 
190   @param  Sock               Pointer to the socket
191 
192   @retval True               The socket has connected
193   @retval False              The socket has not connected
194 
195 **/
196 #define SOCK_IS_CONNECTED(Sock)       ((Sock)->State == SO_CONNECTED)
197 
198 /**
199   Check whether the socket is disconnecting.
200 
201   @param  Sock               Pointer to the socket
202 
203   @retval True               The socket is disconnecting
204   @retval False              The socket is not disconnecting
205 
206 **/
207 #define SOCK_IS_DISCONNECTING(Sock)   ((Sock)->State == SO_DISCONNECTING)
208 
209 /**
210   Check whether the socket is no more data.
211 
212   @param  Sock               Pointer to the socket
213 
214   @retval True               The socket is no more data
215   @retval False              The socket still has data
216 
217 **/
218 #define SOCK_IS_NO_MORE_DATA(Sock)    (0 != ((Sock)->Flag & SO_NO_MORE_DATA))
219 
220 /**
221   Set the size of the receive buffer.
222 
223   @param  Sock               Pointer to the socket
224   @param  Size               The size to set
225 
226 **/
227 #define SET_RCV_BUFFSIZE(Sock, Size)  ((Sock)->RcvBuffer.HighWater = (Size))
228 
229 /**
230   Get the size of the receive buffer.
231 
232   @param  Sock               Pointer to the socket
233 
234   @return The receive buffer size
235 
236 **/
237 #define GET_RCV_BUFFSIZE(Sock)        ((Sock)->RcvBuffer.HighWater)
238 
239 /**
240   Get the size of the receive data.
241 
242   @param  Sock               Pointer to the socket
243 
244   @return The received data size
245 
246 **/
247 #define GET_RCV_DATASIZE(Sock)        (((Sock)->RcvBuffer.DataQueue)->BufSize)
248 
249 /**
250   Set the size of the send buffer.
251 
252   @param  Sock               Pointer to the socket
253   @param  Size               The size to set
254 
255 **/
256 #define SET_SND_BUFFSIZE(Sock, Size)  ((Sock)->SndBuffer.HighWater = (Size))
257 
258 /**
259   Get the size of the send buffer.
260 
261   @param  Sock               Pointer to the socket
262 
263   @return The send buffer size
264 
265 **/
266 #define GET_SND_BUFFSIZE(Sock)        ((Sock)->SndBuffer.HighWater)
267 
268 /**
269   Get the size of the send data.
270 
271   @param  Sock               Pointer to the socket
272 
273   @return The send data size
274 
275 **/
276 #define GET_SND_DATASIZE(Sock)        (((Sock)->SndBuffer.DataQueue)->BufSize)
277 
278 /**
279   Set the backlog value of the socket.
280 
281   @param  Sock               Pointer to the socket
282   @param  Value              The value to set
283 
284 **/
285 #define SET_BACKLOG(Sock, Value)      ((Sock)->BackLog = (Value))
286 
287 /**
288   Get the backlog value of the socket.
289 
290   @param  Sock               Pointer to the socket
291 
292   @return The backlog value
293 
294 **/
295 #define GET_BACKLOG(Sock)             ((Sock)->BackLog)
296 
297 /**
298   Set the socket with error state.
299 
300   @param  Sock               Pointer to the socket
301   @param  Error              The error state
302 
303 **/
304 #define SOCK_ERROR(Sock, Error)       ((Sock)->SockError = (Error))
305 
306 #define SND_BUF_HDR_LEN(Sock) \
307   ((SockBufFirst (&((Sock)->SndBuffer)))->TotalSize)
308 
309 #define RCV_BUF_HDR_LEN(Sock) \
310   ((SockBufFirst (&((Sock)->RcvBuffer)))->TotalSize)
311 
312 #define SOCK_SIGNATURE                SIGNATURE_32 ('S', 'O', 'C', 'K')
313 
314 #define SOCK_FROM_THIS(a)             CR ((a), SOCKET, NetProtocol, SOCK_SIGNATURE)
315 
316 #define SOCK_FROM_TOKEN(Token)        (((SOCK_TOKEN *) (Token))->Sock)
317 
318 #define PROTO_TOKEN_FORM_SOCK(SockToken, Type) \
319   ((Type *) (((SOCK_TOKEN *) (SockToken))->Token))
320 
321 typedef struct _SOCKET SOCKET;
322 
323 ///
324 /// Socket completion token
325 ///
326 typedef struct _SOCK_COMPLETION_TOKEN {
327   EFI_EVENT   Event;            ///< The event to be issued
328   EFI_STATUS  Status;           ///< The status to be issued
329 } SOCK_COMPLETION_TOKEN;
330 
331 typedef union {
332   VOID  *RxData;
333   VOID  *TxData;
334 } SOCK_IO_DATA;
335 
336 ///
337 /// The application token with data packet
338 ///
339 typedef struct _SOCK_IO_TOKEN {
340   SOCK_COMPLETION_TOKEN Token;
341   SOCK_IO_DATA          Packet;
342 } SOCK_IO_TOKEN;
343 
344 ///
345 ///  The request issued from socket layer to protocol layer.
346 ///
347 #define SOCK_ATTACH     0    ///< Attach current socket to a new PCB
348 #define SOCK_DETACH     1    ///< Detach current socket from the PCB
349 #define SOCK_CONFIGURE  2    ///< Configure attached PCB
350 #define SOCK_FLUSH      3    ///< Flush attached PCB
351 #define SOCK_SND        4    ///< Need protocol to send something
352 #define SOCK_SNDPUSH    5    ///< Need protocol to send pushed data
353 #define SOCK_SNDURG     6    ///< Need protocol to send urgent data
354 #define SOCK_CONSUMED   7    ///< Application has retrieved data from socket
355 #define SOCK_CONNECT    8    ///< Need to connect to a peer
356 #define SOCK_CLOSE      9    ///< Need to close the protocol process
357 #define SOCK_ABORT      10   ///< Need to reset the protocol process
358 #define SOCK_POLL       11   ///< Need to poll to the protocol layer
359 #define SOCK_ROUTE      12   ///< Need to add a route information
360 #define SOCK_MODE       13   ///< Need to get the mode data of the protocol
361 #define SOCK_GROUP      14   ///< Need to join a mcast group
362 
363 ///
364 ///  The socket type.
365 ///
366 typedef enum {
367   SockDgram, ///< This socket providing datagram service
368   SockStream ///< This socket providing stream service
369 } SOCK_TYPE;
370 
371 ///
372 ///  The buffer structure of rcvd data and send data used by socket.
373 ///
374 typedef struct _SOCK_BUFFER {
375   UINT32        HighWater;  ///< The buffersize upper limit of sock_buffer
376   UINT32        LowWater;   ///< The low warter mark of sock_buffer
377   NET_BUF_QUEUE *DataQueue; ///< The queue to buffer data
378 } SOCK_BUFFER;
379 
380 /**
381   The handler of protocol for request from socket.
382 
383   @param Socket              The socket issuing the request to protocol
384   @param Request             The request issued by socket
385   @param RequestData         The request related data
386 
387   @retval EFI_SUCCESS        The socket request is completed successfully.
388   @retval other              The error status returned by the corresponding TCP
389                              layer function.
390 
391 **/
392 typedef
393 EFI_STATUS
394 (*SOCK_PROTO_HANDLER) (
395   IN SOCKET       *Socket,
396   IN UINT8        Request,
397   IN VOID         *RequestData
398   );
399 
400 
401 //
402 // Socket provided oprerations for low layer protocol
403 //
404 
405 //
406 // Socket provided operations for user interface
407 //
408 
409 /**
410   Set the state of the socket.
411 
412   @param  Sock                  Pointer to the socket.
413   @param  State                 The new socket state to be set.
414 
415 **/
416 VOID
417 SockSetState (
418   IN OUT SOCKET     *Sock,
419   IN     UINT8      State
420   );
421 
422 /**
423   Called by the low layer protocol to indicate the socket a connection is
424   established.
425 
426   This function just changes the socket's state to SO_CONNECTED
427   and signals the token used for connection establishment.
428 
429   @param  Sock                  Pointer to the socket associated with the
430                                 established connection.
431 
432 **/
433 VOID
434 SockConnEstablished (
435   IN SOCKET *Sock
436   );
437 
438 /**
439   Called by the low layer protocol to indicate the connection is closed.
440 
441   This function flushes the socket, sets the state to SO_CLOSED and signals
442   the close token.
443 
444   @param  Sock                  Pointer to the socket associated with the closed
445                                 connection.
446 
447 **/
448 VOID
449 SockConnClosed (
450   IN OUT SOCKET *Sock
451   );
452 
453 /**
454   Called by low layer protocol to indicate that some data is sent or processed.
455 
456   This function trims the sent data in the socket send buffer, signals the data
457   token if proper.
458 
459   @param  Sock                  Pointer to the socket.
460   @param  Count                 The length of the data processed or sent, in bytes.
461 
462 **/
463 VOID
464 SockDataSent (
465   IN SOCKET     *Sock,
466   IN UINT32     Count
467   );
468 
469 /**
470   Called by the low layer protocol to copy some data in socket send
471   buffer starting from the specific offset to a buffer provided by
472   the caller.
473 
474   @param  Sock                  Pointer to the socket.
475   @param  Offset                The start point of the data to be copied.
476   @param  Len                   The length of the data to be copied.
477   @param  Dest                  Pointer to the destination to copy the data.
478 
479   @return The data size copied.
480 
481 **/
482 UINT32
483 SockGetDataToSend (
484   IN SOCKET      *Sock,
485   IN UINT32      Offset,
486   IN UINT32      Len,
487   IN UINT8       *Dest
488   );
489 
490 /**
491   Called by the low layer protocol to indicate that there
492   will be no more data from the communication peer.
493 
494   This function set the socket's state to SO_NO_MORE_DATA and
495   signal all queued IO tokens with the error status EFI_CONNECTION_FIN.
496 
497   @param  Sock                  Pointer to the socket.
498 
499 **/
500 VOID
501 SockNoMoreData (
502   IN OUT SOCKET *Sock
503   );
504 
505 /**
506   Called by the low layer protocol to deliver received data to socket layer.
507 
508   This function will append the data to the socket receive buffer, set ther
509   urgent data length and then check if any receive token can be signaled.
510 
511   @param  Sock                  Pointer to the socket.
512   @param  NetBuffer             Pointer to the buffer that contains the received
513                                 data.
514   @param  UrgLen                The length of the urgent data in the received data.
515 
516 **/
517 VOID
518 SockDataRcvd (
519   IN     SOCKET    *Sock,
520   IN OUT NET_BUF   *NetBuffer,
521   IN     UINT32    UrgLen
522   );
523 
524 /**
525   Get the length of the free space of the specific socket buffer.
526 
527   @param  Sock                  Pointer to the socket.
528   @param  Which                 Flag to indicate which socket buffer to check,
529                                 either send buffer or receive buffer.
530 
531   @return The length of the free space, in bytes.
532 
533 **/
534 UINT32
535 SockGetFreeSpace (
536   IN SOCKET  *Sock,
537   IN UINT32  Which
538   );
539 
540 /**
541   Clone a new socket including its associated protocol control block.
542 
543   @param  Sock                  Pointer to the socket to be cloned.
544 
545   @return Pointer to the newly cloned socket. If NULL, error condition occurred.
546 
547 **/
548 SOCKET *
549 SockClone (
550   IN SOCKET *Sock
551   );
552 
553 /**
554   Signal the receive token with the specific error or
555   set socket error code after error is received.
556 
557   @param  Sock                  Pointer to the socket.
558   @param  Error                 The error code received.
559 
560 **/
561 VOID
562 SockRcvdErr (
563   IN OUT SOCKET       *Sock,
564   IN     EFI_STATUS   Error
565   );
566 
567 ///
568 /// Proto type of the create callback
569 ///
570 typedef
571 EFI_STATUS
572 (*SOCK_CREATE_CALLBACK) (
573   IN SOCKET  *This,
574   IN VOID    *Context
575   );
576 
577 ///
578 /// Proto type of the destroy callback
579 ///
580 typedef
581 VOID
582 (*SOCK_DESTROY_CALLBACK) (
583   IN SOCKET  *This,
584   IN VOID    *Context
585   );
586 
587 ///
588 ///  The initialize data for create a new socket.
589 ///
590 typedef struct _SOCK_INIT_DATA {
591   SOCK_TYPE   Type;
592   UINT8       State;
593 
594   SOCKET      *Parent;        ///< The parent of this socket
595   UINT32      BackLog;        ///< The connection limit for listening socket
596   UINT32      SndBufferSize;  ///< The high warter mark of send buffer
597   UINT32      RcvBufferSize;  ///< The high warter mark of receive buffer
598   VOID        *Protocol;      ///< The pointer to protocol function template
599                               ///< wanted to install on socket
600 
601   //
602   // Callbacks after socket is created and before socket is to be destroyed.
603   //
604   SOCK_CREATE_CALLBACK   CreateCallback;  ///< Callback after created
605   SOCK_DESTROY_CALLBACK  DestroyCallback; ///< Callback before destroied
606   VOID                   *Context;        ///< The context of the callback
607 
608   //
609   // Opaque protocol data.
610   //
611   VOID                   *ProtoData;
612   UINT32                 DataSize;
613 
614   SOCK_PROTO_HANDLER     ProtoHandler;  ///< The handler of protocol for socket request
615 
616   EFI_HANDLE   DriverBinding;           ///< The driver binding handle
617 } SOCK_INIT_DATA;
618 
619 ///
620 ///  The union type of TCP and UDP protocol.
621 ///
622 typedef union _NET_PROTOCOL {
623   EFI_TCP4_PROTOCOL TcpProtocol;   ///< Tcp protocol
624   EFI_UDP4_PROTOCOL UdpProtocol;   ///< Udp protocol
625 } NET_PROTOCOL;
626 
627 ///
628 /// The socket structure representing a network service access point
629 ///
630 struct _SOCKET {
631 
632   //
633   // Socket description information
634   //
635   UINT32                Signature;      ///< Signature of the socket
636   EFI_HANDLE            SockHandle;     ///< The virtual handle of the socket
637   EFI_HANDLE            DriverBinding;  ///< Socket's driver binding protocol
638   EFI_DEVICE_PATH_PROTOCOL  *ParentDevicePath;
639   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
640   LIST_ENTRY                Link;
641   UINT8                 ConfigureState;
642   SOCK_TYPE             Type;
643   UINT8                 State;
644   UINT16                Flag;
645   EFI_LOCK              Lock;       ///< The lock of socket
646   SOCK_BUFFER           SndBuffer;  ///< Send buffer of application's data
647   SOCK_BUFFER           RcvBuffer;  ///< Receive buffer of received data
648   EFI_STATUS            SockError;  ///< The error returned by low layer protocol
649   BOOLEAN               InDestroy;
650 
651   //
652   // Fields used to manage the connection request
653   //
654   UINT32          BackLog;        ///< the limit of connection to this socket
655   UINT32          ConnCnt;        ///< the current count of connections to it
656   SOCKET          *Parent;        ///< listening parent that accept the connection
657   LIST_ENTRY      ConnectionList; ///< the connections maintained by this socket
658 
659   //
660   // The queue to buffer application's asynchronous token
661   //
662   LIST_ENTRY      ListenTokenList;
663   LIST_ENTRY      RcvTokenList;
664   LIST_ENTRY      SndTokenList;
665   LIST_ENTRY      ProcessingSndTokenList;
666 
667   SOCK_COMPLETION_TOKEN *ConnectionToken; ///< app's token to signal if connected
668   SOCK_COMPLETION_TOKEN *CloseToken;      ///< app's token to signal if closed
669 
670   //
671   // Interface for low level protocol
672   //
673   SOCK_PROTO_HANDLER    ProtoHandler;       ///< The request handler of protocol
674   UINT8 ProtoReserved[PROTO_RESERVED_LEN];  ///< Data fields reserved for protocol
675   NET_PROTOCOL          NetProtocol;        ///< TCP or UDP protocol socket used
676 
677   //
678   // Callbacks after socket is created and before socket is to be destroyed.
679   //
680   SOCK_CREATE_CALLBACK   CreateCallback;    ///< Callback after created
681   SOCK_DESTROY_CALLBACK  DestroyCallback;   ///< Callback before destroied
682   VOID                   *Context;          ///< The context of the callback
683 };
684 
685 ///
686 ///  The token structure buffered in socket layer.
687 ///
688 typedef struct _SOCK_TOKEN {
689   LIST_ENTRY            TokenList;      ///< The entry to add in the token list
690   SOCK_COMPLETION_TOKEN *Token;         ///< The application's token
691   UINT32                RemainDataLen;  ///< Unprocessed data length
692   SOCKET                *Sock;          ///< The poninter to the socket this token
693                                         ///< belongs to
694 } SOCK_TOKEN;
695 
696 ///
697 /// Reserved data to access the NET_BUF delivered by UDP driver.
698 ///
699 typedef struct _UDP_RSV_DATA {
700   EFI_TIME              TimeStamp;
701   EFI_UDP4_SESSION_DATA Session;
702 } UDP_RSV_DATA;
703 
704 ///
705 /// Reserved data to access the NET_BUF delivered by TCP driver.
706 ///
707 typedef struct _TCP_RSV_DATA {
708   UINT32 UrgLen;
709 } TCP_RSV_DATA;
710 
711 /**
712   Create a socket and its associated protocol control block
713   with the intial data SockInitData and protocol specific
714   data ProtoData.
715 
716   @param  SockInitData         Inital data to setting the socket.
717 
718   @return Pointer to the newly created socket. If NULL, error condition occured.
719 
720 **/
721 SOCKET *
722 SockCreateChild (
723   IN SOCK_INIT_DATA *SockInitData
724   );
725 
726 /**
727   Destroy the socket Sock and its associated protocol control block.
728 
729   @param  Sock                 The socket to be destroyed.
730 
731   @retval EFI_SUCCESS          The socket Sock is destroyed successfully.
732   @retval EFI_ACCESS_DENIED    Failed to get the lock to access the socket.
733 
734 **/
735 EFI_STATUS
736 SockDestroyChild (
737   IN   SOCKET *Sock
738   );
739 
740 /**
741   Configure the specific socket Sock using configuration data ConfigData.
742 
743   @param  Sock                 Pointer to the socket to be configured.
744   @param  ConfigData           Pointer to the configuration data.
745 
746   @retval EFI_SUCCESS          The socket is configured successfully.
747   @retval EFI_ACCESS_DENIED    Failed to get the lock to access the socket or the
748                                socket is already configured.
749 
750 **/
751 EFI_STATUS
752 SockConfigure (
753   IN SOCKET *Sock,
754   IN VOID   *ConfigData
755   );
756 
757 /**
758   Initiate a connection establishment process.
759 
760   @param  Sock                 Pointer to the socket to initiate the initate the
761                                connection.
762   @param  Token                Pointer to the token used for the connection
763                                operation.
764 
765   @retval EFI_SUCCESS          The connection is initialized successfully.
766   @retval EFI_ACCESS_DENIED    Failed to get the lock to access the socket, or the
767                                socket is closed, or the socket is not configured to
768                                be an active one, or the token is already in one of
769                                this socket's lists.
770   @retval EFI_NO_MAPPING       The IP address configuration operation is not
771                                finished.
772   @retval EFI_NOT_STARTED      The socket is not configured.
773 
774 **/
775 EFI_STATUS
776 SockConnect (
777   IN SOCKET *Sock,
778   IN VOID   *Token
779   );
780 
781 /**
782   Issue a listen token to get an existed connected network instance
783   or wait for a connection if there is none.
784 
785   @param  Sock                 Pointer to the socket to accept connections.
786   @param  Token                The token to accept a connection.
787 
788   @retval EFI_SUCCESS          Either a connection is accpeted or the Token is
789                                buffered for further acception.
790   @retval EFI_ACCESS_DENIED    Failed to get the lock to access the socket, or the
791                                socket is closed, or the socket is not configured to
792                                be a passive one, or the token is already in one of
793                                this socket's lists.
794   @retval EFI_NO_MAPPING       The IP address configuration operation is not
795                                finished.
796   @retval EFI_NOT_STARTED      The socket is not configured.
797   @retval EFI_OUT_OF_RESOURCE  Failed to buffer the Token due to memory limit.
798 
799 **/
800 EFI_STATUS
801 SockAccept (
802   IN SOCKET *Sock,
803   IN VOID   *Token
804   );
805 
806 /**
807   Issue a token with data to the socket to send out.
808 
809   @param  Sock                 Pointer to the socket to process the token with
810                                data.
811   @param  Token                The token with data that needs to send out.
812 
813   @retval EFI_SUCCESS          The token is processed successfully.
814   @retval EFI_ACCESS_DENIED    Failed to get the lock to access the socket, or the
815                                socket is closed, or the socket is not in a
816                                synchronized state , or the token is already in one
817                                of this socket's lists.
818   @retval EFI_NO_MAPPING       The IP address configuration operation is not
819                                finished.
820   @retval EFI_NOT_STARTED      The socket is not configured.
821   @retval EFI_OUT_OF_RESOURCE  Failed to buffer the token due to memory limit.
822 
823 **/
824 EFI_STATUS
825 SockSend (
826   IN SOCKET *Sock,
827   IN VOID   *Token
828   );
829 
830 /**
831   Issue a token to get data from the socket.
832 
833   @param  Sock                 Pointer to the socket to get data from.
834   @param  Token                The token to store the received data from the
835                                socket.
836 
837   @retval EFI_SUCCESS          The token is processed successfully.
838   @retval EFI_ACCESS_DENIED    Failed to get the lock to access the socket, or the
839                                socket is closed, or the socket is not in a
840                                synchronized state , or the token is already in one
841                                of this socket's lists.
842   @retval EFI_NO_MAPPING       The IP address configuration operation is not
843                                finished.
844   @retval EFI_NOT_STARTED      The socket is not configured.
845   @retval EFI_CONNECTION_FIN   The connection is closed and there is no more data.
846   @retval EFI_OUT_OF_RESOURCE  Failed to buffer the token due to memory limit.
847 
848 **/
849 EFI_STATUS
850 SockRcv (
851   IN SOCKET *Sock,
852   IN VOID   *Token
853   );
854 
855 /**
856   Reset the socket and its associated protocol control block.
857 
858   @param  Sock                 Pointer to the socket to be flushed.
859 
860   @retval EFI_SUCCESS          The socket is flushed successfully.
861   @retval EFI_ACCESS_DENIED    Failed to get the lock to access the socket.
862 
863 **/
864 EFI_STATUS
865 SockFlush (
866   IN SOCKET *Sock
867   );
868 
869 /**
870   Close or abort the socket associated connection.
871 
872   @param  Sock                 Pointer to the socket of the connection to close or
873                                abort.
874   @param  Token                The token for close operation.
875   @param  OnAbort              TRUE for aborting the connection, FALSE to close it.
876 
877   @retval EFI_SUCCESS          The close or abort operation is initialized
878                                successfully.
879   @retval EFI_ACCESS_DENIED    Failed to get the lock to access the socket, or the
880                                socket is closed, or the socket is not in a
881                                synchronized state , or the token is already in one
882                                of this socket's lists.
883   @retval EFI_NO_MAPPING       The IP address configuration operation is not
884                                finished.
885   @retval EFI_NOT_STARTED      The socket is not configured.
886 
887 **/
888 EFI_STATUS
889 SockClose (
890   IN SOCKET  *Sock,
891   IN VOID    *Token,
892   IN BOOLEAN OnAbort
893   );
894 
895 /**
896   Get the mode data of the low layer protocol.
897 
898   @param  Sock                 Pointer to the socket to get mode data from.
899   @param  Mode                 Pointer to the data to store the low layer mode
900                                information.
901 
902   @retval EFI_SUCCESS          The mode data is got successfully.
903   @retval EFI_NOT_STARTED      The socket is not configured.
904 
905 **/
906 EFI_STATUS
907 SockGetMode (
908   IN     SOCKET *Sock,
909   IN OUT VOID   *Mode
910   );
911 
912 /**
913   Configure the low level protocol to join a multicast group for
914   this socket's connection.
915 
916   @param  Sock                 Pointer to the socket of the connection to join the
917                                specific multicast group.
918   @param  GroupInfo            Pointer to the multicast group info.
919 
920   @retval EFI_SUCCESS          The configuration is done successfully.
921   @retval EFI_ACCESS_DENIED    Failed to get the lock to access the socket.
922   @retval EFI_NOT_STARTED      The socket is not configured.
923 
924 **/
925 EFI_STATUS
926 SockGroup (
927   IN SOCKET *Sock,
928   IN VOID   *GroupInfo
929   );
930 
931 /**
932   Add or remove route information in IP route table associated
933   with this socket.
934 
935   @param  Sock                 Pointer to the socket associated with the IP route
936                                table to operate on.
937   @param  RouteInfo            Pointer to the route information to be processed.
938 
939   @retval EFI_SUCCESS          The route table is updated successfully.
940   @retval EFI_ACCESS_DENIED    Failed to get the lock to access the socket.
941   @retval EFI_NO_MAPPING       The IP address configuration operation is  not
942                                finished.
943   @retval EFI_NOT_STARTED      The socket is not configured.
944 
945 **/
946 EFI_STATUS
947 SockRoute (
948   IN SOCKET    *Sock,
949   IN VOID      *RouteInfo
950   );
951 
952 //
953 // Supporting function to operate on socket buffer
954 //
955 
956 /**
957   Get the first buffer block in the specific socket buffer.
958 
959   @param  Sockbuf               Pointer to the socket buffer.
960 
961   @return Pointer to the first buffer in the queue. NULL if the queue is empty.
962 
963 **/
964 NET_BUF *
965 SockBufFirst (
966   IN SOCK_BUFFER *Sockbuf
967   );
968 
969 /**
970   Get the next buffer block in the specific socket buffer.
971 
972   @param  Sockbuf               Pointer to the socket buffer.
973   @param  SockEntry             Pointer to the buffer block prior to the required
974                                 one.
975 
976   @return Pointer to the buffer block next to SockEntry. NULL if SockEntry is
977           the tail or head entry.
978 
979 **/
980 NET_BUF *
981 SockBufNext (
982   IN SOCK_BUFFER *Sockbuf,
983   IN NET_BUF     *SockEntry
984   );
985 
986 #endif
987