2007-07-24 10:06:37 +02:00
|
|
|
/** @file
|
2009-01-16 07:56:18 +01:00
|
|
|
Implementation of the Socket.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
2012-10-17 10:23:41 +02:00
|
|
|
Copyright (c) 2005 - 2012, Intel Corporation. All rights reserved.<BR>
|
2010-04-24 11:33:45 +02:00
|
|
|
This program and the accompanying materials
|
2007-07-24 10:06:37 +02:00
|
|
|
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
|
2008-12-16 03:57:53 +01:00
|
|
|
http://opensource.org/licenses/bsd-license.php<BR>
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
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 "SockImpl.h"
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
/**
|
|
|
|
Get the length of the data that can be retrieved from the socket
|
|
|
|
receive buffer.
|
|
|
|
|
|
|
|
@param SockBuffer Pointer to the socket receive buffer.
|
|
|
|
@param IsUrg Pointer to a BOOLEAN variable. If TRUE the data is
|
|
|
|
OOB.
|
|
|
|
@param BufLen The maximum length of the data buffer to store the
|
|
|
|
received data in socket layer.
|
|
|
|
|
|
|
|
@return The length of the data can be retreived.
|
|
|
|
|
|
|
|
**/
|
2007-07-24 10:06:37 +02:00
|
|
|
UINT32
|
|
|
|
SockTcpDataToRcv (
|
2008-12-16 03:57:53 +01:00
|
|
|
IN SOCK_BUFFER *SockBuffer,
|
|
|
|
OUT BOOLEAN *IsUrg,
|
|
|
|
IN UINT32 BufLen
|
2007-07-24 10:06:37 +02:00
|
|
|
);
|
2008-12-06 00:47:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
Process the send token.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
|
|
|
|
**/
|
2007-07-24 10:06:37 +02:00
|
|
|
VOID
|
|
|
|
SockProcessSndToken (
|
2008-12-29 13:58:38 +01:00
|
|
|
IN OUT SOCKET *Sock
|
2007-07-24 10:06:37 +02:00
|
|
|
);
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
/**
|
2008-12-16 03:57:53 +01:00
|
|
|
Supporting function for both SockImpl and SockInterface.
|
2008-12-06 00:47:55 +01:00
|
|
|
|
2008-12-16 03:57:53 +01:00
|
|
|
@param Event The Event this notify function registered to, ignored.
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
**/
|
2007-07-24 10:06:37 +02:00
|
|
|
VOID
|
2010-04-17 01:24:45 +02:00
|
|
|
EFIAPI
|
2007-07-24 10:06:37 +02:00
|
|
|
SockFreeFoo (
|
|
|
|
IN EFI_EVENT Event
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Get the length of the data that can be retrieved from the socket
|
|
|
|
receive buffer.
|
|
|
|
|
|
|
|
@param SockBuffer Pointer to the socket receive buffer.
|
|
|
|
@param IsUrg Pointer to a BOOLEAN variable. If TRUE the data is
|
|
|
|
OOB.
|
|
|
|
@param BufLen The maximum length of the data buffer to store the
|
|
|
|
received data in socket layer.
|
|
|
|
|
|
|
|
@return The length of the data can be retreived.
|
|
|
|
|
|
|
|
**/
|
|
|
|
UINT32
|
|
|
|
SockTcpDataToRcv (
|
2008-12-06 00:47:55 +01:00
|
|
|
IN SOCK_BUFFER *SockBuffer,
|
|
|
|
OUT BOOLEAN *IsUrg,
|
|
|
|
IN UINT32 BufLen
|
2007-07-24 10:06:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
NET_BUF *RcvBufEntry;
|
|
|
|
UINT32 DataLen;
|
|
|
|
TCP_RSV_DATA *TcpRsvData;
|
|
|
|
BOOLEAN Urg;
|
2008-12-06 00:47:55 +01:00
|
|
|
ASSERT ((SockBuffer != NULL) && (IsUrg != NULL) && (BufLen > 0));
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
RcvBufEntry = SockBufFirst (SockBuffer);
|
2008-12-06 00:47:55 +01:00
|
|
|
ASSERT (RcvBufEntry != NULL);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
TcpRsvData = (TCP_RSV_DATA *) RcvBufEntry->ProtoData;
|
|
|
|
|
2007-07-25 07:32:10 +02:00
|
|
|
*IsUrg = (BOOLEAN) ((TcpRsvData->UrgLen > 0) ? TRUE : FALSE);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
if (*IsUrg && TcpRsvData->UrgLen < RcvBufEntry->TotalSize) {
|
|
|
|
|
2007-11-20 06:42:23 +01:00
|
|
|
DataLen = MIN (TcpRsvData->UrgLen, BufLen);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
if (DataLen < TcpRsvData->UrgLen) {
|
|
|
|
TcpRsvData->UrgLen = TcpRsvData->UrgLen - DataLen;
|
|
|
|
} else {
|
|
|
|
TcpRsvData->UrgLen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DataLen;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
DataLen = RcvBufEntry->TotalSize;
|
|
|
|
|
|
|
|
RcvBufEntry = SockBufNext (SockBuffer, RcvBufEntry);
|
|
|
|
|
|
|
|
while ((BufLen > DataLen) && (RcvBufEntry != NULL)) {
|
|
|
|
|
|
|
|
TcpRsvData = (TCP_RSV_DATA *) RcvBufEntry->ProtoData;
|
|
|
|
|
2007-07-25 07:32:10 +02:00
|
|
|
Urg = (BOOLEAN) ((TcpRsvData->UrgLen > 0) ? TRUE : FALSE);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
if (*IsUrg != Urg) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*IsUrg && TcpRsvData->UrgLen < RcvBufEntry->TotalSize) {
|
|
|
|
|
|
|
|
if (TcpRsvData->UrgLen + DataLen < BufLen) {
|
|
|
|
TcpRsvData->UrgLen = 0;
|
|
|
|
} else {
|
|
|
|
TcpRsvData->UrgLen = TcpRsvData->UrgLen - (BufLen - DataLen);
|
|
|
|
}
|
|
|
|
|
2007-11-20 06:42:23 +01:00
|
|
|
return MIN (TcpRsvData->UrgLen + DataLen, BufLen);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
DataLen += RcvBufEntry->TotalSize;
|
|
|
|
|
|
|
|
RcvBufEntry = SockBufNext (SockBuffer, RcvBufEntry);
|
|
|
|
}
|
|
|
|
|
2007-11-20 06:42:23 +01:00
|
|
|
DataLen = MIN (BufLen, DataLen);
|
2007-07-24 10:06:37 +02:00
|
|
|
return DataLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Copy data from socket buffer to application provided receive buffer.
|
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
@param TcpRxData Pointer to the application provided receive buffer.
|
|
|
|
@param RcvdBytes The maximum length of the data can be copied.
|
2008-12-29 13:58:38 +01:00
|
|
|
@param IsOOB If TURE the data is OOB, FALSE the data is normal.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockSetTcpRxData (
|
|
|
|
IN SOCKET *Sock,
|
|
|
|
IN VOID *TcpRxData,
|
|
|
|
IN UINT32 RcvdBytes,
|
|
|
|
IN BOOLEAN IsOOB
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT32 Index;
|
|
|
|
UINT32 CopyBytes;
|
|
|
|
UINT32 OffSet;
|
|
|
|
EFI_TCP4_RECEIVE_DATA *RxData;
|
|
|
|
EFI_TCP4_FRAGMENT_DATA *Fragment;
|
|
|
|
|
|
|
|
RxData = (EFI_TCP4_RECEIVE_DATA *) TcpRxData;
|
|
|
|
|
|
|
|
OffSet = 0;
|
|
|
|
|
|
|
|
ASSERT (RxData->DataLength >= RcvdBytes);
|
|
|
|
|
|
|
|
RxData->DataLength = RcvdBytes;
|
|
|
|
RxData->UrgentFlag = IsOOB;
|
|
|
|
|
|
|
|
for (Index = 0; (Index < RxData->FragmentCount) && (RcvdBytes > 0); Index++) {
|
|
|
|
|
|
|
|
Fragment = &RxData->FragmentTable[Index];
|
2007-11-20 06:42:23 +01:00
|
|
|
CopyBytes = MIN ((UINT32) (Fragment->FragmentLength), RcvdBytes);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
NetbufQueCopy (
|
|
|
|
Sock->RcvBuffer.DataQueue,
|
|
|
|
OffSet,
|
|
|
|
CopyBytes,
|
|
|
|
Fragment->FragmentBuffer
|
|
|
|
);
|
|
|
|
|
|
|
|
Fragment->FragmentLength = CopyBytes;
|
|
|
|
RcvdBytes -= CopyBytes;
|
|
|
|
OffSet += CopyBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Get received data from the socket layer to the receive token.
|
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
@param RcvToken Pointer to the application provided receive token.
|
|
|
|
|
|
|
|
@return The length of data received in this token.
|
|
|
|
|
|
|
|
**/
|
|
|
|
UINT32
|
|
|
|
SockProcessRcvToken (
|
2008-12-29 13:58:38 +01:00
|
|
|
IN SOCKET *Sock,
|
|
|
|
IN OUT SOCK_IO_TOKEN *RcvToken
|
2007-07-24 10:06:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT32 TokenRcvdBytes;
|
|
|
|
EFI_TCP4_RECEIVE_DATA *RxData;
|
|
|
|
BOOLEAN IsUrg;
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
ASSERT (Sock != NULL);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
2009-11-04 09:18:34 +01:00
|
|
|
ASSERT (SockStream == Sock->Type);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
RxData = RcvToken->Packet.RxData;
|
|
|
|
|
|
|
|
TokenRcvdBytes = SockTcpDataToRcv (
|
|
|
|
&Sock->RcvBuffer,
|
|
|
|
&IsUrg,
|
2007-07-25 07:32:10 +02:00
|
|
|
(UINT32) RxData->DataLength
|
2007-07-24 10:06:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Copy data from RcvBuffer of socket to user
|
|
|
|
// provided RxData and set the fields in TCP RxData
|
|
|
|
//
|
|
|
|
SockSetTcpRxData (Sock, RxData, TokenRcvdBytes, IsUrg);
|
|
|
|
|
2008-12-16 03:57:53 +01:00
|
|
|
NetbufQueTrim (Sock->RcvBuffer.DataQueue, TokenRcvdBytes);
|
2007-07-24 10:06:37 +02:00
|
|
|
SIGNAL_TOKEN (&(RcvToken->Token), EFI_SUCCESS);
|
|
|
|
|
|
|
|
return TokenRcvdBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Process the TCP send data, buffer the tcp txdata and append
|
|
|
|
the buffer to socket send buffer,then try to send it.
|
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
2008-12-29 13:58:38 +01:00
|
|
|
@param TcpTxData Pointer to the application provided send buffer.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
@retval EFI_SUCCESS The operation is completed successfully.
|
|
|
|
@retval EFI_OUT_OF_RESOURCES Failed due to resource limit.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
SockProcessTcpSndData (
|
|
|
|
IN SOCKET *Sock,
|
|
|
|
IN VOID *TcpTxData
|
|
|
|
)
|
|
|
|
{
|
|
|
|
NET_BUF *SndData;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
EFI_TCP4_TRANSMIT_DATA *TxData;
|
|
|
|
|
|
|
|
TxData = (EFI_TCP4_TRANSMIT_DATA *) TcpTxData;
|
|
|
|
|
|
|
|
//
|
|
|
|
// transform this TxData into a NET_BUFFER
|
|
|
|
// and insert it into Sock->SndBuffer
|
|
|
|
//
|
|
|
|
SndData = NetbufFromExt (
|
|
|
|
(NET_FRAGMENT *) TxData->FragmentTable,
|
2007-07-25 07:32:10 +02:00
|
|
|
(UINT32) TxData->FragmentCount,
|
2007-07-24 10:06:37 +02:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
SockFreeFoo,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
if (NULL == SndData) {
|
2008-02-14 10:40:22 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "SockKProcessSndData: Failed to"
|
2007-07-24 10:06:37 +02:00
|
|
|
" call NetBufferFromExt\n"));
|
|
|
|
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetbufQueAppend (Sock->SndBuffer.DataQueue, SndData);
|
|
|
|
|
|
|
|
//
|
|
|
|
// notify the low layer protocol to handle this send token
|
|
|
|
//
|
|
|
|
if (TxData->Urgent) {
|
|
|
|
Status = Sock->ProtoHandler (Sock, SOCK_SNDURG, NULL);
|
|
|
|
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TxData->Push) {
|
|
|
|
Status = Sock->ProtoHandler (Sock, SOCK_SNDPUSH, NULL);
|
|
|
|
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// low layer protocol should really handle the sending
|
|
|
|
// process when catching SOCK_SND request
|
|
|
|
//
|
|
|
|
Status = Sock->ProtoHandler (Sock, SOCK_SND, NULL);
|
|
|
|
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Flush the tokens in the specific token list.
|
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
@param PendingTokenList Pointer to the token list to be flushed.
|
2008-12-06 00:47:55 +01:00
|
|
|
|
2007-07-24 10:06:37 +02:00
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockFlushPendingToken (
|
|
|
|
IN SOCKET *Sock,
|
2008-02-14 10:40:22 +01:00
|
|
|
IN LIST_ENTRY *PendingTokenList
|
2007-07-24 10:06:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
SOCK_TOKEN *SockToken;
|
|
|
|
SOCK_COMPLETION_TOKEN *Token;
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
ASSERT ((Sock != NULL) && (PendingTokenList != NULL));
|
2007-07-24 10:06:37 +02:00
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
while (!IsListEmpty (PendingTokenList)) {
|
2007-07-24 10:06:37 +02:00
|
|
|
SockToken = NET_LIST_HEAD (
|
|
|
|
PendingTokenList,
|
|
|
|
SOCK_TOKEN,
|
|
|
|
TokenList
|
|
|
|
);
|
|
|
|
|
|
|
|
Token = SockToken->Token;
|
|
|
|
SIGNAL_TOKEN (Token, Sock->SockError);
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
RemoveEntryList (&(SockToken->TokenList));
|
2009-11-13 07:13:59 +01:00
|
|
|
FreePool (SockToken);
|
2007-07-24 10:06:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-12-06 00:47:55 +01:00
|
|
|
Wake up the connection token while the connection is successfully established,
|
|
|
|
then try to process any pending send token.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockWakeConnToken (
|
2008-12-29 13:58:38 +01:00
|
|
|
IN OUT SOCKET *Sock
|
2007-07-24 10:06:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
ASSERT (Sock->ConnectionToken != NULL);
|
|
|
|
|
|
|
|
SIGNAL_TOKEN (Sock->ConnectionToken, EFI_SUCCESS);
|
|
|
|
Sock->ConnectionToken = NULL;
|
|
|
|
|
|
|
|
//
|
|
|
|
// check to see if some pending send token existed?
|
|
|
|
//
|
|
|
|
SockProcessSndToken (Sock);
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-12-06 00:47:55 +01:00
|
|
|
Wake up the listen token while the connection is established successfully.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockWakeListenToken (
|
2008-12-29 13:58:38 +01:00
|
|
|
IN OUT SOCKET *Sock
|
2007-07-24 10:06:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
SOCKET *Parent;
|
|
|
|
SOCK_TOKEN *SockToken;
|
|
|
|
EFI_TCP4_LISTEN_TOKEN *ListenToken;
|
|
|
|
|
|
|
|
Parent = Sock->Parent;
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
ASSERT ((Parent != NULL) && SOCK_IS_LISTENING (Parent) && SOCK_IS_CONNECTED (Sock));
|
2007-07-24 10:06:37 +02:00
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
if (!IsListEmpty (&Parent->ListenTokenList)) {
|
2007-07-24 10:06:37 +02:00
|
|
|
SockToken = NET_LIST_HEAD (
|
|
|
|
&Parent->ListenTokenList,
|
|
|
|
SOCK_TOKEN,
|
|
|
|
TokenList
|
|
|
|
);
|
|
|
|
|
|
|
|
ListenToken = (EFI_TCP4_LISTEN_TOKEN *) SockToken->Token;
|
|
|
|
ListenToken->NewChildHandle = Sock->SockHandle;
|
|
|
|
|
|
|
|
SIGNAL_TOKEN (&(ListenToken->CompletionToken), EFI_SUCCESS);
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
RemoveEntryList (&SockToken->TokenList);
|
2009-11-13 07:13:59 +01:00
|
|
|
FreePool (SockToken);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
RemoveEntryList (&Sock->ConnectionList);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
Parent->ConnCnt--;
|
2008-12-16 03:57:53 +01:00
|
|
|
DEBUG ((EFI_D_INFO, "SockWakeListenToken: accept a socket, now conncnt is %d", Parent->ConnCnt));
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
Sock->Parent = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Wake up the receive token while some data is received.
|
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockWakeRcvToken (
|
|
|
|
IN SOCKET *Sock
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT32 RcvdBytes;
|
|
|
|
UINT32 TokenRcvdBytes;
|
|
|
|
SOCK_TOKEN *SockToken;
|
|
|
|
SOCK_IO_TOKEN *RcvToken;
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
ASSERT (Sock->RcvBuffer.DataQueue != NULL);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
RcvdBytes = (Sock->RcvBuffer.DataQueue)->BufSize;
|
|
|
|
|
|
|
|
ASSERT (RcvdBytes > 0);
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
while (RcvdBytes > 0 && !IsListEmpty (&Sock->RcvTokenList)) {
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
SockToken = NET_LIST_HEAD (
|
|
|
|
&Sock->RcvTokenList,
|
|
|
|
SOCK_TOKEN,
|
|
|
|
TokenList
|
|
|
|
);
|
|
|
|
|
|
|
|
RcvToken = (SOCK_IO_TOKEN *) SockToken->Token;
|
|
|
|
TokenRcvdBytes = SockProcessRcvToken (Sock, RcvToken);
|
|
|
|
|
|
|
|
if (0 == TokenRcvdBytes) {
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
RemoveEntryList (&(SockToken->TokenList));
|
2009-11-13 07:13:59 +01:00
|
|
|
FreePool (SockToken);
|
2007-07-24 10:06:37 +02:00
|
|
|
RcvdBytes -= TokenRcvdBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Process the send token.
|
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockProcessSndToken (
|
2008-12-29 13:58:38 +01:00
|
|
|
IN OUT SOCKET *Sock
|
2007-07-24 10:06:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT32 FreeSpace;
|
|
|
|
SOCK_TOKEN *SockToken;
|
|
|
|
UINT32 DataLen;
|
|
|
|
SOCK_IO_TOKEN *SndToken;
|
|
|
|
EFI_TCP4_TRANSMIT_DATA *TxData;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
|
2009-11-04 09:18:34 +01:00
|
|
|
ASSERT ((Sock != NULL) && (SockStream == Sock->Type));
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
FreeSpace = SockGetFreeSpace (Sock, SOCK_SND_BUF);
|
|
|
|
|
|
|
|
//
|
|
|
|
// to determine if process a send token using
|
|
|
|
// socket layer flow control policy
|
|
|
|
//
|
|
|
|
while ((FreeSpace >= Sock->SndBuffer.LowWater) &&
|
2008-02-14 10:40:22 +01:00
|
|
|
!IsListEmpty (&Sock->SndTokenList)) {
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
SockToken = NET_LIST_HEAD (
|
|
|
|
&(Sock->SndTokenList),
|
|
|
|
SOCK_TOKEN,
|
|
|
|
TokenList
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// process this token
|
|
|
|
//
|
2008-02-14 10:40:22 +01:00
|
|
|
RemoveEntryList (&(SockToken->TokenList));
|
|
|
|
InsertTailList (
|
2007-07-24 10:06:37 +02:00
|
|
|
&(Sock->ProcessingSndTokenList),
|
|
|
|
&(SockToken->TokenList)
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Proceess it in the light of SockType
|
|
|
|
//
|
|
|
|
SndToken = (SOCK_IO_TOKEN *) SockToken->Token;
|
|
|
|
TxData = SndToken->Packet.TxData;
|
|
|
|
|
2007-07-25 07:32:10 +02:00
|
|
|
DataLen = (UINT32) TxData->DataLength;
|
2007-07-24 10:06:37 +02:00
|
|
|
Status = SockProcessTcpSndData (Sock, TxData);
|
|
|
|
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
goto OnError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DataLen >= FreeSpace) {
|
|
|
|
FreeSpace = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
FreeSpace -= DataLen;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ;
|
|
|
|
|
|
|
|
OnError:
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
RemoveEntryList (&SockToken->TokenList);
|
2007-07-24 10:06:37 +02:00
|
|
|
SIGNAL_TOKEN (SockToken->Token, Status);
|
2009-11-13 07:13:59 +01:00
|
|
|
FreePool (SockToken);
|
2007-07-24 10:06:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Create a socket with initial data SockInitData.
|
|
|
|
|
|
|
|
@param SockInitData Pointer to the initial data of the socket.
|
|
|
|
|
2008-12-24 09:14:19 +01:00
|
|
|
@return Pointer to the newly created socket, return NULL when exception occured.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
SOCKET *
|
|
|
|
SockCreate (
|
|
|
|
IN SOCK_INIT_DATA *SockInitData
|
|
|
|
)
|
|
|
|
{
|
|
|
|
SOCKET *Sock;
|
|
|
|
SOCKET *Parent;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
ASSERT ((SockInitData != NULL) && (SockInitData->ProtoHandler != NULL));
|
2009-11-04 09:18:34 +01:00
|
|
|
ASSERT (SockInitData->Type == SockStream);
|
2008-12-06 00:47:55 +01:00
|
|
|
ASSERT ((SockInitData->ProtoData != NULL) && (SockInitData->DataSize <= PROTO_RESERVED_LEN));
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
Parent = SockInitData->Parent;
|
|
|
|
|
2008-11-28 09:32:24 +01:00
|
|
|
if ((Parent != NULL) && (Parent->ConnCnt == Parent->BackLog)) {
|
2008-02-14 10:40:22 +01:00
|
|
|
DEBUG (
|
|
|
|
(EFI_D_ERROR,
|
|
|
|
"SockCreate: Socket parent has "
|
2007-07-24 10:06:37 +02:00
|
|
|
"reached its connection limit with %d ConnCnt and %d BackLog\n",
|
|
|
|
Parent->ConnCnt,
|
|
|
|
Parent->BackLog)
|
|
|
|
);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
Sock = AllocateZeroPool (sizeof (SOCKET));
|
2007-07-24 10:06:37 +02:00
|
|
|
if (NULL == Sock) {
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "SockCreate: No resource to create a new socket\n"));
|
2007-07-24 10:06:37 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
InitializeListHead (&Sock->Link);
|
|
|
|
InitializeListHead (&Sock->ConnectionList);
|
|
|
|
InitializeListHead (&Sock->ListenTokenList);
|
|
|
|
InitializeListHead (&Sock->RcvTokenList);
|
|
|
|
InitializeListHead (&Sock->SndTokenList);
|
|
|
|
InitializeListHead (&Sock->ProcessingSndTokenList);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
EfiInitializeLock (&(Sock->Lock), TPL_CALLBACK);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
Sock->SndBuffer.DataQueue = NetbufQueAlloc ();
|
|
|
|
if (NULL == Sock->SndBuffer.DataQueue) {
|
2008-02-14 10:40:22 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "SockCreate: No resource to allocate"
|
2007-07-24 10:06:37 +02:00
|
|
|
" SndBuffer for new socket\n"));
|
|
|
|
|
|
|
|
goto OnError;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sock->RcvBuffer.DataQueue = NetbufQueAlloc ();
|
|
|
|
if (NULL == Sock->RcvBuffer.DataQueue) {
|
2008-02-14 10:40:22 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "SockCreate: No resource to allocate "
|
2007-07-24 10:06:37 +02:00
|
|
|
"RcvBuffer for new socket\n"));
|
|
|
|
|
|
|
|
goto OnError;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sock->Signature = SOCK_SIGNATURE;
|
|
|
|
|
|
|
|
Sock->Parent = Parent;
|
|
|
|
Sock->BackLog = SockInitData->BackLog;
|
|
|
|
Sock->ProtoHandler = SockInitData->ProtoHandler;
|
|
|
|
Sock->SndBuffer.HighWater = SockInitData->SndBufferSize;
|
|
|
|
Sock->RcvBuffer.HighWater = SockInitData->RcvBufferSize;
|
|
|
|
Sock->Type = SockInitData->Type;
|
|
|
|
Sock->DriverBinding = SockInitData->DriverBinding;
|
|
|
|
Sock->State = SockInitData->State;
|
2008-01-29 09:47:42 +01:00
|
|
|
Sock->CreateCallback = SockInitData->CreateCallback;
|
|
|
|
Sock->DestroyCallback = SockInitData->DestroyCallback;
|
|
|
|
Sock->Context = SockInitData->Context;
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
Sock->SockError = EFI_ABORTED;
|
|
|
|
Sock->SndBuffer.LowWater = SOCK_BUFF_LOW_WATER;
|
|
|
|
Sock->RcvBuffer.LowWater = SOCK_BUFF_LOW_WATER;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Install protocol on Sock->SockHandle
|
|
|
|
//
|
2008-02-14 10:40:22 +01:00
|
|
|
CopyMem (
|
2007-07-24 10:06:37 +02:00
|
|
|
&(Sock->NetProtocol.TcpProtocol),
|
|
|
|
SockInitData->Protocol,
|
|
|
|
sizeof (EFI_TCP4_PROTOCOL)
|
|
|
|
);
|
|
|
|
|
2008-01-29 09:47:42 +01:00
|
|
|
//
|
|
|
|
// copy the protodata into socket
|
|
|
|
//
|
2008-02-14 10:40:22 +01:00
|
|
|
CopyMem (Sock->ProtoReserved, SockInitData->ProtoData, SockInitData->DataSize);
|
2008-01-29 09:47:42 +01:00
|
|
|
|
2007-07-24 10:06:37 +02:00
|
|
|
Status = gBS->InstallMultipleProtocolInterfaces (
|
|
|
|
&Sock->SockHandle,
|
|
|
|
&gEfiTcp4ProtocolGuid,
|
|
|
|
&(Sock->NetProtocol.TcpProtocol),
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
if (EFI_ERROR (Status)) {
|
2008-02-14 10:40:22 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "SockCreate: Install TCP protocol in "
|
2007-07-24 10:06:37 +02:00
|
|
|
"socket failed with %r\n", Status));
|
|
|
|
|
|
|
|
goto OnError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Parent != NULL) {
|
|
|
|
ASSERT (Parent->BackLog > 0);
|
|
|
|
ASSERT (SOCK_IS_LISTENING (Parent));
|
|
|
|
|
|
|
|
//
|
|
|
|
// need to add it into Parent->ConnectionList
|
|
|
|
// if the Parent->ConnCnt < Parent->BackLog
|
|
|
|
//
|
|
|
|
Parent->ConnCnt++;
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
DEBUG (
|
2008-12-16 03:57:53 +01:00
|
|
|
(EFI_D_INFO,
|
2008-02-14 10:40:22 +01:00
|
|
|
"SockCreate: Create a new socket and add to parent, now conncnt is %d\n",
|
|
|
|
Parent->ConnCnt)
|
|
|
|
);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
InsertTailList (&Parent->ConnectionList, &Sock->ConnectionList);
|
2007-07-24 10:06:37 +02:00
|
|
|
}
|
|
|
|
|
2008-01-29 09:47:42 +01:00
|
|
|
if (Sock->CreateCallback != NULL) {
|
|
|
|
Status = Sock->CreateCallback (Sock, Sock->Context);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
goto OnError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-24 10:06:37 +02:00
|
|
|
return Sock;
|
|
|
|
|
|
|
|
OnError:
|
|
|
|
|
2008-01-29 09:47:42 +01:00
|
|
|
if (Sock->SockHandle != NULL) {
|
|
|
|
gBS->UninstallMultipleProtocolInterfaces (
|
|
|
|
Sock->SockHandle,
|
|
|
|
&gEfiTcp4ProtocolGuid,
|
|
|
|
&(Sock->NetProtocol.TcpProtocol),
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
}
|
2007-07-24 10:06:37 +02:00
|
|
|
|
2008-01-29 09:47:42 +01:00
|
|
|
if (NULL != Sock->SndBuffer.DataQueue) {
|
|
|
|
NetbufQueFree (Sock->SndBuffer.DataQueue);
|
|
|
|
}
|
2007-07-24 10:06:37 +02:00
|
|
|
|
2008-01-29 09:47:42 +01:00
|
|
|
if (NULL != Sock->RcvBuffer.DataQueue) {
|
|
|
|
NetbufQueFree (Sock->RcvBuffer.DataQueue);
|
2007-07-24 10:06:37 +02:00
|
|
|
}
|
|
|
|
|
2009-11-13 07:13:59 +01:00
|
|
|
FreePool (Sock);
|
2008-01-29 09:47:42 +01:00
|
|
|
|
2007-07-24 10:06:37 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Destroy a socket.
|
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockDestroy (
|
2008-12-29 13:58:38 +01:00
|
|
|
IN OUT SOCKET *Sock
|
2007-07-24 10:06:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
VOID *SockProtocol;
|
|
|
|
EFI_GUID *ProtocolGuid;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
|
2009-11-04 09:18:34 +01:00
|
|
|
ASSERT (SockStream == Sock->Type);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
2008-01-29 09:47:42 +01:00
|
|
|
if (Sock->DestroyCallback != NULL) {
|
|
|
|
Sock->DestroyCallback (Sock, Sock->Context);
|
|
|
|
}
|
|
|
|
|
2007-07-24 10:06:37 +02:00
|
|
|
//
|
|
|
|
// Flush the completion token buffered
|
|
|
|
// by sock and rcv, snd buffer
|
|
|
|
//
|
|
|
|
if (!SOCK_IS_UNCONFIGURED (Sock)) {
|
|
|
|
|
|
|
|
SockConnFlush (Sock);
|
|
|
|
SockSetState (Sock, SO_CLOSED);
|
|
|
|
Sock->ConfigureState = SO_UNCONFIGURED;
|
|
|
|
|
|
|
|
}
|
|
|
|
//
|
2012-10-17 10:23:41 +02:00
|
|
|
// Destroy the RcvBuffer Queue and SendBuffer Queue
|
2007-07-24 10:06:37 +02:00
|
|
|
//
|
|
|
|
NetbufQueFree (Sock->RcvBuffer.DataQueue);
|
|
|
|
NetbufQueFree (Sock->SndBuffer.DataQueue);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Remove it from parent connection list if needed
|
|
|
|
//
|
2008-11-28 09:32:24 +01:00
|
|
|
if (Sock->Parent != NULL) {
|
2007-07-24 10:06:37 +02:00
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
RemoveEntryList (&(Sock->ConnectionList));
|
2007-07-24 10:06:37 +02:00
|
|
|
(Sock->Parent->ConnCnt)--;
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
DEBUG (
|
2008-12-16 03:57:53 +01:00
|
|
|
(EFI_D_INFO,
|
2012-10-17 10:23:41 +02:00
|
|
|
"SockDestroy: Delete a unaccepted socket from parent"
|
2008-02-14 10:40:22 +01:00
|
|
|
"now conncnt is %d\n",
|
|
|
|
Sock->Parent->ConnCnt)
|
|
|
|
);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
Sock->Parent = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Set the protocol guid and driver binding handle
|
|
|
|
// in the light of Sock->SockType
|
|
|
|
//
|
|
|
|
ProtocolGuid = &gEfiTcp4ProtocolGuid;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Retrieve the protocol installed on this sock
|
|
|
|
//
|
|
|
|
Status = gBS->OpenProtocol (
|
|
|
|
Sock->SockHandle,
|
|
|
|
ProtocolGuid,
|
|
|
|
&SockProtocol,
|
|
|
|
Sock->DriverBinding,
|
|
|
|
Sock->SockHandle,
|
|
|
|
EFI_OPEN_PROTOCOL_GET_PROTOCOL
|
|
|
|
);
|
|
|
|
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "SockDestroy: Open protocol installed "
|
2007-07-24 10:06:37 +02:00
|
|
|
"on socket failed with %r\n", Status));
|
|
|
|
|
|
|
|
goto FreeSock;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Uninstall the protocol installed on this sock
|
|
|
|
// in the light of Sock->SockType
|
|
|
|
//
|
|
|
|
gBS->UninstallMultipleProtocolInterfaces (
|
|
|
|
Sock->SockHandle,
|
|
|
|
ProtocolGuid,
|
|
|
|
SockProtocol,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
FreeSock:
|
2009-11-13 07:13:59 +01:00
|
|
|
FreePool (Sock);
|
2007-07-24 10:06:37 +02:00
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-12-29 13:58:38 +01:00
|
|
|
Flush the sndBuffer and rcvBuffer of socket.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockConnFlush (
|
2008-12-29 13:58:38 +01:00
|
|
|
IN OUT SOCKET *Sock
|
2007-07-24 10:06:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
SOCKET *Child;
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
ASSERT (Sock != NULL);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Clear the flag in this socket
|
|
|
|
//
|
|
|
|
Sock->Flag = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Flush the SndBuffer and RcvBuffer of Sock
|
|
|
|
//
|
|
|
|
NetbufQueFlush (Sock->SndBuffer.DataQueue);
|
|
|
|
NetbufQueFlush (Sock->RcvBuffer.DataQueue);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Signal the pending token
|
|
|
|
//
|
|
|
|
if (Sock->ConnectionToken != NULL) {
|
|
|
|
SIGNAL_TOKEN (Sock->ConnectionToken, Sock->SockError);
|
|
|
|
Sock->ConnectionToken = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Sock->CloseToken != NULL) {
|
|
|
|
SIGNAL_TOKEN (Sock->CloseToken, Sock->SockError);
|
|
|
|
Sock->CloseToken = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SockFlushPendingToken (Sock, &(Sock->ListenTokenList));
|
|
|
|
SockFlushPendingToken (Sock, &(Sock->RcvTokenList));
|
|
|
|
SockFlushPendingToken (Sock, &(Sock->SndTokenList));
|
|
|
|
SockFlushPendingToken (Sock, &(Sock->ProcessingSndTokenList));
|
|
|
|
|
|
|
|
//
|
|
|
|
// Destroy the pending connection, if it is a listening socket
|
|
|
|
//
|
|
|
|
if (SOCK_IS_LISTENING (Sock)) {
|
2008-02-14 10:40:22 +01:00
|
|
|
while (!IsListEmpty (&Sock->ConnectionList)) {
|
2007-07-24 10:06:37 +02:00
|
|
|
Child = NET_LIST_HEAD (
|
|
|
|
&Sock->ConnectionList,
|
|
|
|
SOCKET,
|
|
|
|
ConnectionList
|
|
|
|
);
|
|
|
|
|
|
|
|
SockDestroyChild (Child);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sock->ConnCnt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Set the state of the socket.
|
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
2009-11-04 09:18:34 +01:00
|
|
|
@param State The new socket state to be set.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockSetState (
|
2008-12-29 13:58:38 +01:00
|
|
|
IN OUT SOCKET *Sock,
|
2009-11-04 09:18:34 +01:00
|
|
|
IN UINT8 State
|
2007-07-24 10:06:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
Sock->State = State;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Clone a new socket including its associated protocol control block.
|
|
|
|
|
|
|
|
@param Sock Pointer to the socket to be cloned.
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
@return Pointer to the newly cloned socket. If NULL, error condition occurred.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
SOCKET *
|
|
|
|
SockClone (
|
|
|
|
IN SOCKET *Sock
|
|
|
|
)
|
|
|
|
{
|
|
|
|
SOCKET *ClonedSock;
|
|
|
|
SOCK_INIT_DATA InitData;
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
InitData.BackLog = Sock->BackLog;
|
|
|
|
InitData.Parent = Sock;
|
|
|
|
InitData.State = Sock->State;
|
|
|
|
InitData.ProtoHandler = Sock->ProtoHandler;
|
|
|
|
InitData.Type = Sock->Type;
|
|
|
|
InitData.RcvBufferSize = Sock->RcvBuffer.HighWater;
|
|
|
|
InitData.SndBufferSize = Sock->SndBuffer.HighWater;
|
|
|
|
InitData.DriverBinding = Sock->DriverBinding;
|
|
|
|
InitData.Protocol = &(Sock->NetProtocol);
|
2008-01-29 09:47:42 +01:00
|
|
|
InitData.CreateCallback = Sock->CreateCallback;
|
|
|
|
InitData.DestroyCallback = Sock->DestroyCallback;
|
|
|
|
InitData.Context = Sock->Context;
|
|
|
|
InitData.ProtoData = Sock->ProtoReserved;
|
|
|
|
InitData.DataSize = sizeof (Sock->ProtoReserved);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
ClonedSock = SockCreate (&InitData);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
if (NULL == ClonedSock) {
|
2008-02-14 10:40:22 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "SockClone: no resource to create a cloned sock\n"));
|
2007-07-24 10:06:37 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SockSetState (ClonedSock, SO_CONNECTING);
|
|
|
|
ClonedSock->ConfigureState = Sock->ConfigureState;
|
|
|
|
|
|
|
|
return ClonedSock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-12-06 00:47:55 +01:00
|
|
|
Called by the low layer protocol to indicate the socket a connection is
|
2008-12-24 09:14:19 +01:00
|
|
|
established.
|
|
|
|
|
|
|
|
This function just changes the socket's state to SO_CONNECTED
|
2008-12-06 00:47:55 +01:00
|
|
|
and signals the token used for connection establishment.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
@param Sock Pointer to the socket associated with the
|
|
|
|
established connection.
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockConnEstablished (
|
|
|
|
IN SOCKET *Sock
|
|
|
|
)
|
|
|
|
{
|
|
|
|
|
|
|
|
ASSERT (SO_CONNECTING == Sock->State);
|
|
|
|
|
|
|
|
SockSetState (Sock, SO_CONNECTED);
|
|
|
|
|
|
|
|
if (NULL == Sock->Parent) {
|
|
|
|
SockWakeConnToken (Sock);
|
|
|
|
} else {
|
|
|
|
SockWakeListenToken (Sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-12-24 09:14:19 +01:00
|
|
|
Called by the low layer protocol to indicate the connection is closed.
|
|
|
|
|
|
|
|
This function flushes the socket, sets the state to SO_CLOSED and signals
|
|
|
|
the close token.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
@param Sock Pointer to the socket associated with the closed
|
|
|
|
connection.
|
2008-12-24 09:14:19 +01:00
|
|
|
|
2007-07-24 10:06:37 +02:00
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockConnClosed (
|
2008-12-29 13:58:38 +01:00
|
|
|
IN OUT SOCKET *Sock
|
2007-07-24 10:06:37 +02:00
|
|
|
)
|
|
|
|
{
|
2008-11-28 09:32:24 +01:00
|
|
|
if (Sock->CloseToken != NULL) {
|
2007-07-24 10:06:37 +02:00
|
|
|
SIGNAL_TOKEN (Sock->CloseToken, EFI_SUCCESS);
|
|
|
|
Sock->CloseToken = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SockConnFlush (Sock);
|
|
|
|
SockSetState (Sock, SO_CLOSED);
|
|
|
|
|
|
|
|
if (Sock->Parent != NULL) {
|
|
|
|
SockDestroyChild (Sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-12-24 09:14:19 +01:00
|
|
|
Called by low layer protocol to indicate that some data is sent or processed.
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
This function trims the sent data in the socket send buffer, signals the data
|
|
|
|
token if proper.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
@param Count The length of the data processed or sent, in bytes.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockDataSent (
|
|
|
|
IN SOCKET *Sock,
|
|
|
|
IN UINT32 Count
|
|
|
|
)
|
|
|
|
{
|
|
|
|
SOCK_TOKEN *SockToken;
|
|
|
|
SOCK_COMPLETION_TOKEN *SndToken;
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
ASSERT (!IsListEmpty (&Sock->ProcessingSndTokenList));
|
2007-07-24 10:06:37 +02:00
|
|
|
ASSERT (Count <= (Sock->SndBuffer.DataQueue)->BufSize);
|
|
|
|
|
|
|
|
NetbufQueTrim (Sock->SndBuffer.DataQueue, Count);
|
|
|
|
|
|
|
|
//
|
|
|
|
// To check if we can signal some snd token in this socket
|
|
|
|
//
|
|
|
|
while (Count > 0) {
|
|
|
|
SockToken = NET_LIST_HEAD (
|
|
|
|
&(Sock->ProcessingSndTokenList),
|
|
|
|
SOCK_TOKEN,
|
|
|
|
TokenList
|
|
|
|
);
|
|
|
|
|
|
|
|
SndToken = SockToken->Token;
|
|
|
|
|
|
|
|
if (SockToken->RemainDataLen <= Count) {
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
RemoveEntryList (&(SockToken->TokenList));
|
2007-07-24 10:06:37 +02:00
|
|
|
SIGNAL_TOKEN (SndToken, EFI_SUCCESS);
|
|
|
|
Count -= SockToken->RemainDataLen;
|
2009-11-13 07:13:59 +01:00
|
|
|
FreePool (SockToken);
|
2007-07-24 10:06:37 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
SockToken->RemainDataLen -= Count;
|
|
|
|
Count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// to judge if we can process some send token in
|
|
|
|
// Sock->SndTokenList, if so process those send token
|
|
|
|
//
|
|
|
|
SockProcessSndToken (Sock);
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Called by the low layer protocol to copy some data in socket send
|
|
|
|
buffer starting from the specific offset to a buffer provided by
|
|
|
|
the caller.
|
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
@param Offset The start point of the data to be copied.
|
|
|
|
@param Len The length of the data to be copied.
|
|
|
|
@param Dest Pointer to the destination to copy the data.
|
|
|
|
|
|
|
|
@return The data size copied.
|
|
|
|
|
|
|
|
**/
|
|
|
|
UINT32
|
|
|
|
SockGetDataToSend (
|
|
|
|
IN SOCKET *Sock,
|
|
|
|
IN UINT32 Offset,
|
|
|
|
IN UINT32 Len,
|
|
|
|
IN UINT8 *Dest
|
|
|
|
)
|
|
|
|
{
|
2009-11-04 09:18:34 +01:00
|
|
|
ASSERT ((Sock != NULL) && SockStream == Sock->Type);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
return NetbufQueCopy (
|
|
|
|
Sock->SndBuffer.DataQueue,
|
|
|
|
Offset,
|
|
|
|
Len,
|
|
|
|
Dest
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-12-24 09:14:19 +01:00
|
|
|
Called by the low layer protocol to deliver received data to socket layer.
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
This function will append the data to the socket receive buffer, set ther
|
|
|
|
urgent data length and then check if any receive token can be signaled.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
@param NetBuffer Pointer to the buffer that contains the received
|
|
|
|
data.
|
|
|
|
@param UrgLen The length of the urgent data in the received data.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockDataRcvd (
|
2008-12-29 13:58:38 +01:00
|
|
|
IN SOCKET *Sock,
|
|
|
|
IN OUT NET_BUF *NetBuffer,
|
|
|
|
IN UINT32 UrgLen
|
2007-07-24 10:06:37 +02:00
|
|
|
)
|
|
|
|
{
|
2008-12-06 00:47:55 +01:00
|
|
|
ASSERT ((Sock != NULL) && (Sock->RcvBuffer.DataQueue != NULL) &&
|
2007-07-24 10:06:37 +02:00
|
|
|
UrgLen <= NetBuffer->TotalSize);
|
|
|
|
|
|
|
|
NET_GET_REF (NetBuffer);
|
|
|
|
|
|
|
|
((TCP_RSV_DATA *) (NetBuffer->ProtoData))->UrgLen = UrgLen;
|
|
|
|
|
|
|
|
NetbufQueAppend (Sock->RcvBuffer.DataQueue, NetBuffer);
|
|
|
|
|
|
|
|
SockWakeRcvToken (Sock);
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Get the length of the free space of the specific socket buffer.
|
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
@param Which Flag to indicate which socket buffer to check,
|
|
|
|
either send buffer or receive buffer.
|
|
|
|
|
|
|
|
@return The length of the free space, in bytes.
|
|
|
|
|
|
|
|
**/
|
|
|
|
UINT32
|
|
|
|
SockGetFreeSpace (
|
|
|
|
IN SOCKET *Sock,
|
|
|
|
IN UINT32 Which
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT32 BufferCC;
|
|
|
|
SOCK_BUFFER *SockBuffer;
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
ASSERT ((Sock != NULL) && ((SOCK_SND_BUF == Which) || (SOCK_RCV_BUF == Which)));
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
if (SOCK_SND_BUF == Which) {
|
|
|
|
SockBuffer = &(Sock->SndBuffer);
|
|
|
|
} else {
|
|
|
|
SockBuffer = &(Sock->RcvBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferCC = (SockBuffer->DataQueue)->BufSize;
|
|
|
|
|
|
|
|
if (BufferCC >= SockBuffer->HighWater) {
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SockBuffer->HighWater - BufferCC;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Signal the receive token with the specific error or
|
|
|
|
set socket error code after error is received.
|
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
@param Error The error code received.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockRcvdErr (
|
2008-12-29 13:58:38 +01:00
|
|
|
IN OUT SOCKET *Sock,
|
|
|
|
IN EFI_STATUS Error
|
2007-07-24 10:06:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
SOCK_TOKEN *SockToken;
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
if (!IsListEmpty (&Sock->RcvTokenList)) {
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
SockToken = NET_LIST_HEAD (
|
|
|
|
&Sock->RcvTokenList,
|
|
|
|
SOCK_TOKEN,
|
|
|
|
TokenList
|
|
|
|
);
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
RemoveEntryList (&SockToken->TokenList);
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
SIGNAL_TOKEN (SockToken->Token, Error);
|
|
|
|
|
2009-11-13 07:13:59 +01:00
|
|
|
FreePool (SockToken);
|
2007-07-24 10:06:37 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
SOCK_ERROR (Sock, Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-12-24 09:14:19 +01:00
|
|
|
Called by the low layer protocol to indicate that there will be no more data
|
|
|
|
from the communication peer.
|
|
|
|
|
|
|
|
This function set the socket's state to SO_NO_MORE_DATA and signal all queued
|
|
|
|
IO tokens with the error status EFI_CONNECTION_FIN.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
@param Sock Pointer to the socket.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SockNoMoreData (
|
2008-12-29 13:58:38 +01:00
|
|
|
IN OUT SOCKET *Sock
|
2007-07-24 10:06:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Err;
|
|
|
|
|
|
|
|
SOCK_NO_MORE_DATA (Sock);
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
if (!IsListEmpty (&Sock->RcvTokenList)) {
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
ASSERT (0 == GET_RCV_DATASIZE (Sock));
|
|
|
|
|
|
|
|
Err = Sock->SockError;
|
|
|
|
|
|
|
|
SOCK_ERROR (Sock, EFI_CONNECTION_FIN);
|
|
|
|
|
|
|
|
SockFlushPendingToken (Sock, &Sock->RcvTokenList);
|
|
|
|
|
|
|
|
SOCK_ERROR (Sock, Err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Get the first buffer block in the specific socket buffer.
|
|
|
|
|
|
|
|
@param Sockbuf Pointer to the socket buffer.
|
|
|
|
|
|
|
|
@return Pointer to the first buffer in the queue. NULL if the queue is empty.
|
|
|
|
|
|
|
|
**/
|
|
|
|
NET_BUF *
|
|
|
|
SockBufFirst (
|
|
|
|
IN SOCK_BUFFER *Sockbuf
|
|
|
|
)
|
|
|
|
{
|
2008-02-14 10:40:22 +01:00
|
|
|
LIST_ENTRY *NetbufList;
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
NetbufList = &(Sockbuf->DataQueue->BufList);
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
if (IsListEmpty (NetbufList)) {
|
2007-07-24 10:06:37 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NET_LIST_HEAD (NetbufList, NET_BUF, List);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Get the next buffer block in the specific socket buffer.
|
|
|
|
|
|
|
|
@param Sockbuf Pointer to the socket buffer.
|
|
|
|
@param SockEntry Pointer to the buffer block prior to the required
|
|
|
|
one.
|
|
|
|
|
2008-12-06 00:47:55 +01:00
|
|
|
@return Pointer to the buffer block next to SockEntry. NULL if SockEntry is
|
|
|
|
the tail or head entry.
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
NET_BUF *
|
|
|
|
SockBufNext (
|
|
|
|
IN SOCK_BUFFER *Sockbuf,
|
|
|
|
IN NET_BUF *SockEntry
|
|
|
|
)
|
|
|
|
{
|
2008-02-14 10:40:22 +01:00
|
|
|
LIST_ENTRY *NetbufList;
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
NetbufList = &(Sockbuf->DataQueue->BufList);
|
|
|
|
|
|
|
|
if ((SockEntry->List.ForwardLink == NetbufList) ||
|
|
|
|
(SockEntry->List.BackLink == &SockEntry->List) ||
|
2008-12-06 00:47:55 +01:00
|
|
|
(SockEntry->List.ForwardLink == &SockEntry->List)) {
|
2007-07-24 10:06:37 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NET_LIST_USER_STRUCT (SockEntry->List.ForwardLink, NET_BUF, List);
|
|
|
|
}
|