From: lpleahy Date: Thu, 9 Feb 2012 19:18:06 +0000 (+0000) Subject: Merged socket development branch: X-Git-Tag: edk2-stable201903~13648 X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=commitdiff_plain;h=f6e5cdd5cfa100e777db38e5f7b26b7ffcf41330 Merged socket development branch: * Add TCPv6 support to DataSink * Add TCPv6 support to DataSource * Add GetAddrInfo test application * Add GetNameInfo test application * Fixed copyright date * Completed TFTP server - now downloads files from local directory * Added ports and exit pages to web server * Made PCD values package specific Signed-off-by: lpleahy git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13003 6f19259b-4bc3-4df7-8a09-765794883524 --- diff --git a/AppPkg/AppPkg.dec b/AppPkg/AppPkg.dec new file mode 100644 index 0000000000..8aff19fc6b --- /dev/null +++ b/AppPkg/AppPkg.dec @@ -0,0 +1,36 @@ +## @file +# Declarations for the UDK Standard Libraries. +# +# Copyright (c) 2010 - 2012, 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. +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +## + + +[Defines] + DEC_SPECIFICATION = 0x00010005 + PACKAGE_NAME = AppPkg + PACKAGE_GUID = B3E3D3D5-D62B-4497-A175-264F489D127E + PACKAGE_VERSION = 0.01 + + +[Guids] + gAppPkgTokenSpaceGuid = { 0xe7e1efa6, 0x7607, 0x4a78, { 0xa7, 0xdd, 0x43, 0xe4, 0xbd, 0x72, 0xc0, 0x99 }} + + +[PcdsFixedAtBuild] + gAppPkgTokenSpaceGuid.DataSource_Port|1234|UINT16|0 + gAppPkgTokenSpaceGuid.Tftp_AckLogBase|4|UINT32|1 + gAppPkgTokenSpaceGuid.Tftp_AckMultiplier|4|UINT32|2 + gAppPkgTokenSpaceGuid.Tftp_Bandwidth|0|BOOLEAN|3 + gAppPkgTokenSpaceGuid.Tftp_HighSpeed|0|BOOLEAN|4 + gAppPkgTokenSpaceGuid.Tftp_MaxRetry|10|UINT32|5 + gAppPkgTokenSpaceGuid.Tftp_MaxTimeoutInSec|3|UINT32|6 + gAppPkgTokenSpaceGuid.WebServer_HttpPort|80|UINT16|7 + diff --git a/AppPkg/Applications/Sockets/DataSink/DataSink.c b/AppPkg/Applications/Sockets/DataSink/DataSink.c index 8223ead118..05added9c2 100644 --- a/AppPkg/Applications/Sockets/DataSink/DataSink.c +++ b/AppPkg/Applications/Sockets/DataSink/DataSink.c @@ -28,10 +28,12 @@ #include -#define MAX_CONNECTIONS ( 1 + 16 ) ///< Maximum number of client connections -#define RANGE_SWITCH 2048 ///< Switch display ranges -#define DATA_RATE_UPDATE_SHIFT 2 ///< 2n seconds between updates +#define DATA_SAMPLE_SHIFT 5 ///< Shift for number of samples +#define MAX_CONNECTIONS ( 1 + 16 ) ///< Maximum number of client connections +#define RANGE_SWITCH ( 1024 * 1024 ) ///< Switch display ranges +#define DATA_RATE_UPDATE_SHIFT 2 ///< 2n seconds between updates #define AVERAGE_SHIFT_COUNT ( 6 - DATA_RATE_UPDATE_SHIFT ) ///< 2n samples in average +#define DATA_SAMPLES ( 1 << DATA_SAMPLE_SHIFT ) ///< Number of samples #define TPL_DATASINK TPL_CALLBACK ///< Synchronization TPL @@ -39,16 +41,16 @@ #define DATA_BUFFER_SIZE (( 65536 / PACKET_SIZE ) * PACKET_SIZE ) ///< Buffer size in bytes typedef struct _DT_PORT { - UINT64 BytesAverage; - UINT64 BytesPrevious; UINT64 BytesTotal; - struct sockaddr_in RemoteAddress; - UINT64 Samples; + struct sockaddr_in6 IpAddress; + UINT32 In; + UINT32 Samples; + UINT64 BytesReceived[ DATA_SAMPLES ]; } DT_PORT; volatile BOOLEAN bTick; BOOLEAN bTimerRunning; -struct sockaddr_in LocalAddress; +struct sockaddr_in6 LocalAddress; EFI_EVENT pTimer; int ListenSocket; UINT8 Buffer[ DATA_BUFFER_SIZE ]; @@ -120,7 +122,7 @@ SocketAccept ( // SocketStatus = bind ( ListenSocket, (struct sockaddr *) &LocalAddress, - LocalAddress.sin_len ); + LocalAddress.sin6_len ); if ( 0 == SocketStatus ) { // // Start listening on the local socket @@ -139,14 +141,7 @@ SocketAccept ( PollFd[ Index ].fd = ListenSocket; PollFd[ Index ].events = POLLRDNORM | POLLHUP; PollFd[ Index ].revents = 0; - Port[ Index ].BytesAverage = 0; - Port[ Index ].BytesPrevious = 0; - Port[ Index ].BytesTotal = 0; - Port[ Index ].Samples = 0; - Port[ Index ].RemoteAddress.sin_len = 0; - Port[ Index ].RemoteAddress.sin_family = 0; - Port[ Index ].RemoteAddress.sin_port = 0; - Port[ Index ].RemoteAddress.sin_addr.s_addr= 0; + ZeroMem ( &Port[ Index ], sizeof ( Port[ Index ])); } } @@ -203,11 +198,14 @@ SocketClose ( /** Create the socket + @param [in] Family Network family, AF_INET or AF_INET6 + @retval EFI_SUCCESS The application is running normally @retval Other The user stopped the application **/ EFI_STATUS SocketNew ( + sa_family_t Family ) { EFI_STATUS Status; @@ -216,9 +214,9 @@ SocketNew ( // Get the port number // ZeroMem ( &LocalAddress, sizeof ( LocalAddress )); - LocalAddress.sin_len = sizeof ( LocalAddress ); - LocalAddress.sin_family = AF_INET; - LocalAddress.sin_port = htons ( PcdGet16 ( DataSource_Port )); + LocalAddress.sin6_len = sizeof ( LocalAddress ); + LocalAddress.sin6_family = Family; + LocalAddress.sin6_port = htons ( PcdGet16 ( DataSource_Port )); // // Loop creating the socket @@ -234,7 +232,7 @@ SocketNew ( // // Attempt to create the socket // - ListenSocket = socket ( AF_INET, + ListenSocket = socket ( LocalAddress.sin6_family, SOCK_STREAM, IPPROTO_TCP ); if ( -1 != ListenSocket ) { @@ -274,7 +272,11 @@ SocketPoll ( int FdCount; nfds_t Index; socklen_t LengthInBytes; - struct sockaddr_in RemoteAddress; + struct sockaddr_in * pPortIpAddress4; + struct sockaddr_in6 * pPortIpAddress6; + struct sockaddr_in * pRemoteAddress4; + struct sockaddr_in6 * pRemoteAddress6; + struct sockaddr_in6 RemoteAddress; int Socket; EFI_STATUS Status; EFI_TPL TplPrevious; @@ -282,6 +284,8 @@ SocketPoll ( // // Check for control-C // + pRemoteAddress4 = (struct sockaddr_in *)&RemoteAddress; + pRemoteAddress6 = (struct sockaddr_in6 *)&RemoteAddress; bListenError = FALSE; Status = ControlCCheck ( ); if ( !EFI_ERROR ( Status )) { @@ -311,6 +315,8 @@ SocketPoll ( // // Account for this descriptor // + pPortIpAddress4 = (struct sockaddr_in *)&Port[ Index ].IpAddress; + pPortIpAddress6 = (struct sockaddr_in6 *)&Port[ Index ].IpAddress; if ( 0 != PollFd[ Index ].revents ) { FdCount -= 1; } @@ -327,14 +333,38 @@ SocketPoll ( errno )); } else { - DEBUG (( DEBUG_ERROR, - "ERROR - Network closed on socket %d.%d.%d.%d:%d, errno: %d\r\n", - Port[ Index ].RemoteAddress.sin_addr.s_addr & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 8 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 16 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 24 ) & 0xff, - htons ( Port[ Index ].RemoteAddress.sin_port ), - errno )); + if ( AF_INET == pPortIpAddress4->sin_family ) { + DEBUG (( DEBUG_ERROR, + "ERROR - Network closed on socket %d.%d.%d.%d:%d, errno: %d\r\n", + pPortIpAddress4->sin_addr.s_addr & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 8 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 16 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 24 ) & 0xff, + htons ( pPortIpAddress4->sin_port ), + errno )); + } + else { + DEBUG (( DEBUG_ERROR, + "ERROR - Network closed on socket [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d, errno: %d\r\n", + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pPortIpAddress6->sin6_port ), + errno )); + } // // Close the socket @@ -342,25 +372,74 @@ SocketPoll ( CloseStatus = close ( PollFd[ Index ].fd ); if ( 0 == CloseStatus ) { bRemoveSocket = TRUE; - DEBUG (( DEBUG_INFO, - "0x%08x: Socket closed for %d.%d.%d.%d:%d\r\n", - PollFd[ Index ].fd, - Port[ Index ].RemoteAddress.sin_addr.s_addr & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 8 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 16 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 24 ) & 0xff, - htons ( Port[ Index ].RemoteAddress.sin_port ))); + if ( AF_INET == pPortIpAddress4->sin_family ) { + DEBUG (( DEBUG_INFO, + "0x%08x: Socket closed for %d.%d.%d.%d:%d\r\n", + PollFd[ Index ].fd, + pPortIpAddress4->sin_addr.s_addr & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 8 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 16 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 24 ) & 0xff, + htons ( pPortIpAddress4->sin_port ))); + } + else { + DEBUG (( DEBUG_INFO, + "0x%08x: Socket closed for [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", + PollFd[ Index ].fd, + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pPortIpAddress6->sin6_port ))); + } } else { - DEBUG (( DEBUG_ERROR, - "ERROR - Failed to close socket 0x%08x for %d.%d.%d.%d:%d, errno: %d\r\n", - PollFd[ Index ].fd, - Port[ Index ].RemoteAddress.sin_addr.s_addr & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 8 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 16 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 24 ) & 0xff, - htons ( Port[ Index ].RemoteAddress.sin_port ), - errno )); + if ( AF_INET == pPortIpAddress4->sin_family ) { + DEBUG (( DEBUG_ERROR, + "ERROR - Failed to close socket 0x%08x for %d.%d.%d.%d:%d, errno: %d\r\n", + PollFd[ Index ].fd, + pPortIpAddress4->sin_addr.s_addr & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 8 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 16 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 24 ) & 0xff, + htons ( pPortIpAddress4->sin_port ), + errno )); + } + else { + DEBUG (( DEBUG_ERROR, + "ERROR - Failed to close socket 0x%08x for [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d, errno: %d\r\n", + PollFd[ Index ].fd, + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pPortIpAddress6->sin6_port ), + errno )); + } } } } @@ -399,12 +478,34 @@ SocketPoll ( // // Display the connection // - Print ( L"Rejecting connection to remote system %d.%d.%d.%d:%d\r\n", - RemoteAddress.sin_addr.s_addr & 0xff, - ( RemoteAddress.sin_addr.s_addr >> 8 ) & 0xff, - ( RemoteAddress.sin_addr.s_addr >> 16 ) & 0xff, - ( RemoteAddress.sin_addr.s_addr >> 24 ) & 0xff, - htons ( RemoteAddress.sin_port )); + if ( AF_INET == pRemoteAddress4->sin_family ) { + Print ( L"Rejecting connection to remote system %d.%d.%d.%d:%d\r\n", + pRemoteAddress4->sin_addr.s_addr & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 8 ) & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 16 ) & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 24 ) & 0xff, + htons ( pRemoteAddress4->sin_port )); + } + else { + Print ( L"Rejecting connection to remote system [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pRemoteAddress6->sin6_port )); + } // // No room for this connection @@ -413,14 +514,38 @@ SocketPoll ( CloseStatus = close ( Socket ); if ( 0 == CloseStatus ) { bRemoveSocket = TRUE; - DEBUG (( DEBUG_INFO, - "0x%08x: Socket closed for %d.%d.%d.%d:%d\r\n", - PollFd[ Index ].fd, - RemoteAddress.sin_addr.s_addr & 0xff, - ( RemoteAddress.sin_addr.s_addr >> 8 ) & 0xff, - ( RemoteAddress.sin_addr.s_addr >> 16 ) & 0xff, - ( RemoteAddress.sin_addr.s_addr >> 24 ) & 0xff, - htons ( RemoteAddress.sin_port ))); + if ( AF_INET == pRemoteAddress4->sin_family ) { + DEBUG (( DEBUG_INFO, + "0x%08x: Socket closed for %d.%d.%d.%d:%d\r\n", + PollFd[ Index ].fd, + pRemoteAddress4->sin_addr.s_addr & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 8 ) & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 16 ) & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 24 ) & 0xff, + htons ( pRemoteAddress4->sin_port ))); + } + else { + DEBUG (( DEBUG_INFO, + "0x%08x: Socket closed for [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", + PollFd[ Index ].fd, + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pRemoteAddress6->sin6_port ))); + } } else { DEBUG (( DEBUG_ERROR, @@ -439,25 +564,41 @@ SocketPoll ( // // Display the connection // - Print ( L"Connected to remote system %d.%d.%d.%d:%d\r\n", - RemoteAddress.sin_addr.s_addr & 0xff, - ( RemoteAddress.sin_addr.s_addr >> 8 ) & 0xff, - ( RemoteAddress.sin_addr.s_addr >> 16 ) & 0xff, - ( RemoteAddress.sin_addr.s_addr >> 24 ) & 0xff, - htons ( RemoteAddress.sin_port )); + if ( AF_INET == pRemoteAddress4->sin_family ) { + Print ( L"Connected to remote system %d.%d.%d.%d:%d\r\n", + pRemoteAddress4->sin_addr.s_addr & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 8 ) & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 16 ) & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 24 ) & 0xff, + htons ( pRemoteAddress4->sin_port )); + } + else { + Print ( L"Connected to remote system [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pRemoteAddress6->sin6_port )); + } // // Allocate the client connection // Index = MaxPort++; - Port[ Index ].BytesAverage = 0; - Port[ Index ].BytesPrevious = 0; - Port[ Index ].BytesTotal = 0; - Port[ Index ].Samples = 0; - Port[ Index ].RemoteAddress.sin_len = RemoteAddress.sin_len; - Port[ Index ].RemoteAddress.sin_family = RemoteAddress.sin_family; - Port[ Index ].RemoteAddress.sin_port = RemoteAddress.sin_port; - Port[ Index ].RemoteAddress.sin_addr = RemoteAddress.sin_addr; + ZeroMem ( &Port[ Index ], sizeof ( Port[ Index ])); + CopyMem ( pPortIpAddress6, pRemoteAddress6, sizeof ( *pRemoteAddress6 )); PollFd[ Index ].fd = Socket; PollFd[ Index ].events = POLLRDNORM | POLLHUP; PollFd[ Index ].revents = 0; @@ -475,15 +616,40 @@ SocketPoll ( // // Display the amount of data received // - DEBUG (( DEBUG_INFO, - "0x%08x: Socket received 0x%08x bytes from %d.%d.%d.%d:%d\r\n", - PollFd[ Index ].fd, - BytesReceived, - Port[ Index ].RemoteAddress.sin_addr.s_addr & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 8 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 16 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 24 ) & 0xff, - htons ( Port[ Index ].RemoteAddress.sin_port ))); + if ( AF_INET == pPortIpAddress4->sin_family ) { + DEBUG (( DEBUG_INFO, + "0x%08x: Socket received 0x%08x bytes from %d.%d.%d.%d:%d\r\n", + PollFd[ Index ].fd, + BytesReceived, + pPortIpAddress4->sin_addr.s_addr & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 8 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 16 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 24 ) & 0xff, + htons ( pPortIpAddress4->sin_port ))); + } + else { + DEBUG (( DEBUG_INFO, + "0x%08x: Socket received 0x%08x bytes from [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", + PollFd[ Index ].fd, + BytesReceived, + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pPortIpAddress6->sin6_port ))); + } // // Synchronize with the TimerCallback routine @@ -504,36 +670,109 @@ SocketPoll ( // // Close the socket // - DEBUG (( DEBUG_INFO, - "ERROR - Receive failure for %d.%d.%d.%d:%d, errno: %d\r\n", - Port[ Index ].RemoteAddress.sin_addr.s_addr & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 8 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 16 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 24 ) & 0xff, - htons ( Port[ Index ].RemoteAddress.sin_port ), - errno )); + if ( AF_INET == pPortIpAddress4->sin_family ) { + DEBUG (( DEBUG_INFO, + "ERROR - Receive failure for %d.%d.%d.%d:%d, errno: %d\r\n", + pPortIpAddress4->sin_addr.s_addr & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 8 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 16 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 24 ) & 0xff, + htons ( pPortIpAddress4->sin_port ), + errno )); + } + else { + DEBUG (( DEBUG_INFO, + "ERROR - Receive failure for [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d, errno: %d\r\n", + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pPortIpAddress6->sin6_port ), + errno )); + } CloseStatus = close ( PollFd[ Index ].fd ); if ( 0 == CloseStatus ) { bRemoveSocket = TRUE; - DEBUG (( DEBUG_INFO, - "0x%08x: Socket closed for %d.%d.%d.%d:%d\r\n", - PollFd[ Index ].fd, - Port[ Index ].RemoteAddress.sin_addr.s_addr & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 8 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 16 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 24 ) & 0xff, - htons ( Port[ Index ].RemoteAddress.sin_port ))); + if ( AF_INET == pPortIpAddress4->sin_family ) { + DEBUG (( DEBUG_INFO, + "0x%08x: Socket closed for %d.%d.%d.%d:%d\r\n", + PollFd[ Index ].fd, + pPortIpAddress4->sin_addr.s_addr & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 8 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 16 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 24 ) & 0xff, + htons ( pPortIpAddress4->sin_port ))); + } + else { + DEBUG (( DEBUG_INFO, + "0x%08x: Socket closed for [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", + PollFd[ Index ].fd, + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pPortIpAddress6->sin6_port ))); + } } else { - DEBUG (( DEBUG_ERROR, - "ERROR - Failed to close socket 0x%08x for %d.%d.%d.%d:%d, errno: %d\r\n", - PollFd[ Index ].fd, - Port[ Index ].RemoteAddress.sin_addr.s_addr & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 8 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 16 ) & 0xff, - ( Port[ Index ].RemoteAddress.sin_addr.s_addr >> 24 ) & 0xff, - htons ( Port[ Index ].RemoteAddress.sin_port ), - errno )); + if ( AF_INET == pPortIpAddress4->sin_family ) { + DEBUG (( DEBUG_ERROR, + "ERROR - Failed to close socket 0x%08x for %d.%d.%d.%d:%d, errno: %d\r\n", + PollFd[ Index ].fd, + pPortIpAddress4->sin_addr.s_addr & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 8 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 16 ) & 0xff, + ( pPortIpAddress4->sin_addr.s_addr >> 24 ) & 0xff, + htons ( pPortIpAddress4->sin_port ), + errno )); + } + else { + DEBUG (( DEBUG_ERROR, + "ERROR - Failed to close socket 0x%08x for [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d, errno: %d\r\n", + PollFd[ Index ].fd, + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pPortIpAddress6->sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pPortIpAddress6->sin6_port ), + errno )); + } } } @@ -555,14 +794,9 @@ SocketPoll ( MaxPort -= 1; for ( Entry = Index + 1; MaxPort >= Entry; Entry++ ) { EntryPrevious = Entry; - Port[ EntryPrevious ].BytesAverage = Port[ Entry ].BytesAverage; - Port[ EntryPrevious ].BytesPrevious = Port[ Entry ].BytesPrevious; - Port[ EntryPrevious ].BytesTotal = Port[ Entry ].BytesTotal; - Port[ EntryPrevious ].RemoteAddress.sin_len = Port[ Entry ].RemoteAddress.sin_len; - Port[ EntryPrevious ].RemoteAddress.sin_family = Port[ Entry ].RemoteAddress.sin_family; - Port[ EntryPrevious ].RemoteAddress.sin_port = Port[ Entry ].RemoteAddress.sin_port; - Port[ EntryPrevious ].RemoteAddress.sin_addr.s_addr = Port[ Entry ].RemoteAddress.sin_addr.s_addr; - Port[ EntryPrevious ].Samples = Port[ Entry ].Samples; + CopyMem ( &Port[ EntryPrevious ], + &Port[ Entry ], + sizeof ( Port[ Entry ])); PollFd[ EntryPrevious ].events = PollFd[ Entry ].events; PollFd[ EntryPrevious ].fd = PollFd[ Entry ].fd; PollFd[ EntryPrevious ].revents = PollFd[ Entry ].revents; @@ -605,11 +839,12 @@ TimerCallback ( IN VOID * pContext ) { - UINT64 Average; + UINT32 Average; + UINT64 BitsPerSecond; UINT64 BytesReceived; - UINT32 Delta; - UINT64 DeltaBytes; + UINT32 Count; nfds_t Index; + UINT64 TotalBytes; // // Notify the other code of the timer tick @@ -627,65 +862,84 @@ TimerCallback ( if (( ListenSocket != PollFd[ Index ].fd ) && ( 0 != BytesReceived )) { // - // Update the average bytes per second + // Update the received data samples // - DeltaBytes = Port[ Index ].BytesAverage >> AVERAGE_SHIFT_COUNT; - Port[ Index ].BytesAverage -= DeltaBytes; - DeltaBytes = BytesReceived - Port[ Index ].BytesPrevious; - Port[ Index ].BytesPrevious = BytesReceived; - Port[ Index ].BytesAverage += DeltaBytes; - + Port[ Index ].BytesTotal = 0; + Port[ Index ].BytesReceived [ Port[ Index ].In ] = BytesReceived; + Port[ Index ].In += 1; + if ( DATA_SAMPLES <= Port[ Index ].In ) { + Port[ Index ].In = 0; + } + // // Separate the samples // - if (( 2 << AVERAGE_SHIFT_COUNT ) == Port[ Index ].Samples ) { + if ( DATA_SAMPLES == Port[ Index ].Samples ) { Print ( L"---------- Stable average ----------\r\n" ); } Port[ Index ].Samples += 1; + // + // Compute the data rate + // + TotalBytes = 0; + for ( Count = 0; DATA_SAMPLES > Count; Count++ ) + { + TotalBytes += Port[ Index ].BytesReceived[ Count ]; + } + Average = (UINT32)RShiftU64 ( TotalBytes, DATA_SAMPLE_SHIFT ); + BitsPerSecond = Average * 8; + // // Display the data rate // - Delta = (UINT32)( DeltaBytes >> DATA_RATE_UPDATE_SHIFT ); - Average = Port[ Index ].BytesAverage >> ( AVERAGE_SHIFT_COUNT + DATA_RATE_UPDATE_SHIFT ); - if ( Average < RANGE_SWITCH ) { - Print ( L"%d Bytes/sec, Ave: %d Bytes/Sec\r\n", - Delta, - (UINT32) Average ); + if (( RANGE_SWITCH >> 10 ) > Average ) { + Print ( L"Ave: %d Bytes/Sec, %Ld Bits/sec\r\n", + Average, + BitsPerSecond ); } else { - Average >>= 10; - if ( Average < RANGE_SWITCH ) { - Print ( L"%d Bytes/sec, Ave: %d KiBytes/Sec\r\n", - Delta, - (UINT32) Average ); + BitsPerSecond /= 1000; + if ( RANGE_SWITCH > Average ) { + Print ( L"Ave: %d.%03d KiBytes/Sec, %Ld KBits/sec\r\n", + Average >> 10, + (( Average & 0x3ff ) * 1000 ) >> 10, + BitsPerSecond ); } else { + BitsPerSecond /= 1000; Average >>= 10; - if ( Average < RANGE_SWITCH ) { - Print ( L"%d Bytes/sec, Ave: %d MiBytes/Sec\r\n", - Delta, - (UINT32) Average ); + if ( RANGE_SWITCH > Average ) { + Print ( L"Ave: %d.%03d MiBytes/Sec, %Ld MBits/sec\r\n", + Average >> 10, + (( Average & 0x3ff ) * 1000 ) >> 10, + BitsPerSecond ); } else { + BitsPerSecond /= 1000; Average >>= 10; - if ( Average < RANGE_SWITCH ) { - Print ( L"%d Bytes/sec, Ave: %d GiBytes/Sec\r\n", - Delta, - (UINT32) Average ); + if ( RANGE_SWITCH > Average ) { + Print ( L"Ave: %d.%03d GiBytes/Sec, %Ld GBits/sec\r\n", + Average >> 10, + (( Average & 0x3ff ) * 1000 ) >> 10, + BitsPerSecond ); } else { + BitsPerSecond /= 1000; Average >>= 10; - if ( Average < RANGE_SWITCH ) { - Print ( L"%d Bytes/sec, Ave: %d TiBytes/Sec\r\n", - Delta, - Average ); + if ( RANGE_SWITCH > Average ) { + Print ( L"Ave: %d.%03d TiBytes/Sec, %Ld TBits/sec\r\n", + Average >> 10, + (( Average & 0x3ff ) * 1000 ) >> 10, + BitsPerSecond ); } else { + BitsPerSecond /= 1000; Average >>= 10; - Print ( L"%d Bytes/sec, Ave: %d PiBytes/Sec\r\n", - Delta, - (UINT32) Average ); + Print ( L"Ave: %d.%03d PiBytes/Sec, %Ld PBits/sec\r\n", + Average >> 10, + (( Average & 0x3ff ) * 1000 ) >> 10, + BitsPerSecond ); } } } @@ -909,11 +1163,17 @@ main ( IN char **Argv ) { + sa_family_t Family; EFI_STATUS Status; DEBUG (( DEBUG_INFO, "DataSink starting\r\n" )); + // + // Determine the family to use + // + Family = ( 1 < Argc ) ? AF_INET6 : AF_INET; + // // Use for/break instead of goto // @@ -966,7 +1226,7 @@ main ( // // Wait for the network layer to initialize // - Status = SocketNew ( ); + Status = SocketNew ( Family ); if ( EFI_ERROR ( Status )) { continue; } @@ -987,7 +1247,7 @@ main ( } // - // Send data until the connection breaks + // Receive data until the connection breaks // do { Status = SocketPoll ( ); diff --git a/AppPkg/Applications/Sockets/DataSink/DataSink.inf b/AppPkg/Applications/Sockets/DataSink/DataSink.inf index 7c79ab28f8..43a8e962f3 100644 --- a/AppPkg/Applications/Sockets/DataSink/DataSink.inf +++ b/AppPkg/Applications/Sockets/DataSink/DataSink.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such @@ -39,10 +39,11 @@ [Pcd] - gStdLibTokenSpaceGuid.DataSource_Port + gAppPkgTokenSpaceGuid.DataSource_Port [Packages] + AppPkg/AppPkg.dec MdePkg/MdePkg.dec ShellPkg/ShellPkg.dec StdLib/StdLib.dec diff --git a/AppPkg/Applications/Sockets/DataSource/DataSource.c b/AppPkg/Applications/Sockets/DataSource/DataSource.c index 98a0c9f120..31c38950a6 100644 --- a/AppPkg/Applications/Sockets/DataSource/DataSource.c +++ b/AppPkg/Applications/Sockets/DataSource/DataSource.c @@ -30,10 +30,14 @@ #include #include +#include -#define RANGE_SWITCH 2048 ///< Switch display ranges -#define DATA_RATE_UPDATE_SHIFT 2 ///< 2n seconds between updates + +#define DATA_SAMPLE_SHIFT 5 ///< Shift for number of samples +#define RANGE_SWITCH ( 1024 * 1024 ) ///< Switch display ranges +#define DATA_RATE_UPDATE_SHIFT 2 ///< 2n seconds between updates #define AVERAGE_SHIFT_COUNT ( 6 - DATA_RATE_UPDATE_SHIFT ) ///< 2n samples in average +#define DATA_SAMPLES ( 1 << DATA_SAMPLE_SHIFT ) ///< Number of samples #define TPL_DATASOURCE TPL_CALLBACK ///< Synchronization TPL @@ -74,16 +78,16 @@ EFI_EVENT pTimer; // // Remote IP Address Data // -struct sockaddr_in RemoteHostAddress; +struct sockaddr_in6 RemoteHostAddress; CHAR8 * pRemoteHost; // // Traffic Data // UINT64 TotalBytesSent; -UINT64 PreviousBytes; -UINT64 AverageBytes; -UINT64 Samples; +UINT32 In; +UINT32 Samples; +UINT64 BytesSent[ DATA_SAMPLES ]; UINT8 Buffer[ DATA_BUFFER_SIZE ]; @@ -185,48 +189,124 @@ EFI_STATUS IpAddress ( ) { - CHAR8 * pSeparator; - INT32 RemoteAddress; + struct sockaddr_in * pRemoteAddress4; + struct sockaddr_in6 * pRemoteAddress6; + UINT32 RemoteAddress; EFI_STATUS Status; UINT32 Value1; UINT32 Value2; UINT32 Value3; UINT32 Value4; + UINT32 Value5; + UINT32 Value6; + UINT32 Value7; + UINT32 Value8; // // Assume failure // Status = EFI_INVALID_PARAMETER; + // + // Get the port number + // + ZeroMem ( &RemoteHostAddress, sizeof ( RemoteHostAddress )); + RemoteHostAddress.sin6_port = htons ( PcdGet16 ( DataSource_Port )); + pRemoteAddress4 = (struct sockaddr_in *)&RemoteHostAddress; + pRemoteAddress6 = &RemoteHostAddress; + // // Convert the IP address from a string to a numeric value // - pSeparator = GetDigit ( pRemoteHost, &Value1 ); - if (( 255 >= Value1 ) && ( '.' == *pSeparator )) { - pSeparator = GetDigit ( ++pSeparator, &Value2 ); - if (( 255 >= Value2 ) && ( '.' == *pSeparator )) { - pSeparator = GetDigit ( ++pSeparator, &Value3 ); - if (( 255 >= Value3 ) && ( '.' == *pSeparator )) { - pSeparator = GetDigit ( ++pSeparator, &Value4 ); - if (( 255 >= Value4 ) && ( 0 == *pSeparator )) { - RemoteAddress = Value1 - | ( Value2 << 8 ) - | ( Value3 << 16 ) - | ( Value4 << 24 ); - RemoteHostAddress.sin_addr.s_addr = (UINT32) RemoteAddress; - Status = EFI_SUCCESS; - DEBUG (( DEBUG_INFO, - "%d.%d.%d.%d: Remote host IP address\r\n", - Value1, - Value2, - Value3, - Value4 )); - } - } - } + if (( 4 == sscanf ( pRemoteHost, + "%d.%d.%d.%d", + &Value1, + &Value2, + &Value3, + &Value4 )) + && ( 255 >= Value1 ) + && ( 255 >= Value2 ) + && ( 255 >= Value3 ) + && ( 255 >= Value4 )) { + // + // Build the IPv4 address + // + pRemoteAddress4->sin_len = sizeof ( *pRemoteAddress4 ); + pRemoteAddress4->sin_family = AF_INET; + RemoteAddress = Value1 + | ( Value2 << 8 ) + | ( Value3 << 16 ) + | ( Value4 << 24 ); + pRemoteAddress4->sin_addr.s_addr = RemoteAddress; + Status = EFI_SUCCESS; + + // + // Display the IP address + // + DEBUG (( DEBUG_INFO, + "%d.%d.%d.%d: Remote host IP address\r\n", + Value1, + Value2, + Value3, + Value4 )); } - if ( EFI_ERROR ( Status )) { - Print ( L"Invalid digit detected: %d\r\n", *pSeparator ); + else if (( 8 == sscanf ( pRemoteHost, + "%x:%x:%x:%x:%x:%x:%x:%x", + &Value1, + &Value2, + &Value3, + &Value4, + &Value5, + &Value6, + &Value7, + &Value8 )) + && ( 0xffff >= Value1 ) + && ( 0xffff >= Value2 ) + && ( 0xffff >= Value3 ) + && ( 0xffff >= Value4 ) + && ( 0xffff >= Value5 ) + && ( 0xffff >= Value6 ) + && ( 0xffff >= Value7 ) + && ( 0xffff >= Value8 )) { + // + // Build the IPv6 address + // + pRemoteAddress6->sin6_len = sizeof ( *pRemoteAddress6 ); + pRemoteAddress6->sin6_family = AF_INET6; + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 0 ] = (UINT8)( Value1 >> 8 ); + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 1 ] = (UINT8)Value1; + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 2 ] = (UINT8)( Value2 >> 8 ); + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 3 ] = (UINT8)Value2; + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 4 ] = (UINT8)( Value3 >> 8 ); + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 5 ] = (UINT8)Value3; + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 6 ] = (UINT8)( Value4 >> 8 ); + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 7 ] = (UINT8)Value4; + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 8 ] = (UINT8)( Value5 >> 8 ); + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 9 ] = (UINT8)Value5; + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 10 ] = (UINT8)( Value6 >> 8 ); + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 11 ] = (UINT8)Value6; + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 12 ] = (UINT8)( Value7 >> 8 ); + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 13 ] = (UINT8)Value7; + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 14 ] = (UINT8)( Value8 >> 8 ); + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 15 ] = (UINT8)Value8; + Status = EFI_SUCCESS; + + // + // Display the IP address + // + DEBUG (( DEBUG_INFO, + "[%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x]: Remote host IP address\r\n", + Value1, + Value2, + Value3, + Value4, + Value5, + Value6, + Value7, + Value8 )); + } + else { + Print ( L"ERROR - Invalid IP address!\r\n" ); } // @@ -290,19 +370,43 @@ SocketConnect ( ) { int ConnectStatus; - UINT32 RemoteAddress; + struct sockaddr_in * pRemoteAddress4; + struct sockaddr_in6 * pRemoteAddress6; EFI_STATUS Status; // // Display the connecting message // - RemoteAddress = RemoteHostAddress.sin_addr.s_addr; - Print ( L"Connecting to remote system %d.%d.%d.%d:%d\r\n", - RemoteAddress & 0xff, - ( RemoteAddress >> 8 ) & 0xff, - ( RemoteAddress >> 16 ) & 0xff, - ( RemoteAddress >> 24 ) & 0xff, - htons ( RemoteHostAddress.sin_port )); + pRemoteAddress4 = (struct sockaddr_in *)&RemoteHostAddress; + pRemoteAddress6 = &RemoteHostAddress; + if ( AF_INET == pRemoteAddress6->sin6_family ) { + Print ( L"Connecting to remote system %d.%d.%d.%d:%d\r\n", + pRemoteAddress4->sin_addr.s_addr & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 8 ) & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 16 ) & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 24 ) & 0xff, + htons ( pRemoteAddress4->sin_port )); + } + else { + Print ( L"Connecting to remote system [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pRemoteAddress6->sin6_port )); + } // // Connect to the remote system @@ -327,15 +431,37 @@ SocketConnect ( // Connect to the remote system // ConnectStatus = connect ( Socket, - (struct sockaddr *) &RemoteHostAddress, - RemoteHostAddress.sin_len ); + (struct sockaddr *)pRemoteAddress6, + pRemoteAddress6->sin6_len ); if ( -1 != ConnectStatus ) { - Print ( L"Connected to remote system %d.%d.%d.%d:%d\r\n", - RemoteAddress & 0xff, - ( RemoteAddress >> 8 ) & 0xff, - ( RemoteAddress >> 16 ) & 0xff, - ( RemoteAddress >> 24 ) & 0xff, - htons ( RemoteHostAddress.sin_port )); + if ( AF_INET == pRemoteAddress6->sin6_family ) { + Print ( L"Connected to remote system %d.%d.%d.%d:%d\r\n", + pRemoteAddress4->sin_addr.s_addr & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 8 ) & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 16 ) & 0xff, + ( pRemoteAddress4->sin_addr.s_addr >> 24 ) & 0xff, + htons ( pRemoteAddress4->sin_port )); + } + else { + Print ( L"Connected to remote system [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pRemoteAddress6->sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pRemoteAddress6->sin6_port )); + } } else { // @@ -358,11 +484,14 @@ SocketConnect ( /** Create the socket + @param [in] Family Network family, AF_INET or AF_INET6 + @retval EFI_SUCCESS The application is running normally @retval Other The user stopped the application **/ EFI_STATUS SocketNew ( + sa_family_t Family ) { EFI_STATUS Status; @@ -384,7 +513,7 @@ SocketNew ( // // Attempt to create the socket // - Socket = socket ( AF_INET, + Socket = socket ( Family, SOCK_STREAM, IPPROTO_TCP ); if ( -1 != Socket ) { @@ -419,7 +548,7 @@ SocketSend ( // // Restart the timer // - TimerStart ( 1000 << DATA_RATE_UPDATE_SHIFT ); + TimerStart ( 1 * 1000 ); // // Loop until the connection breaks or the user stops @@ -497,7 +626,7 @@ SocketOpen ( // // Wait for the network layer to initialize // - Status = SocketNew ( ); + Status = SocketNew ( RemoteHostAddress.sin6_family ); if ( EFI_ERROR ( Status )) { break; } @@ -584,13 +713,13 @@ Tcp4Close ( // // Display the port closed message // - pIpAddress = (UINT8 *)&RemoteHostAddress.sin_addr.s_addr; + pIpAddress = (UINT8 *)&((struct sockaddr_in *)&RemoteHostAddress)->sin_addr.s_addr; Print ( L"Closed connection to %d.%d.%d.%d:%d\r\n", pIpAddress[0], pIpAddress[1], pIpAddress[2], pIpAddress[3], - htons ( RemoteHostAddress.sin_port )); + htons ( ((struct sockaddr_in *)&RemoteHostAddress)->sin_port )); } } } @@ -786,13 +915,13 @@ Tcp4Locate ( // Display the connecting message // if ( bTcp4Connecting ) { - pIpAddress = (UINT8 *)&RemoteHostAddress.sin_addr.s_addr; + pIpAddress = (UINT8 *)&((struct sockaddr_in *)&RemoteHostAddress)->sin_addr.s_addr; Print ( L"Connecting to %d.%d.%d.%d:%d\r\n", pIpAddress[0], pIpAddress[1], pIpAddress[2], pIpAddress[3], - htons ( RemoteHostAddress.sin_port )); + htons ( ((struct sockaddr_in *)&RemoteHostAddress)->sin_port )); bTcp4Connecting = FALSE; } @@ -885,7 +1014,7 @@ Tcp4Send ( // // Restart the timer // - TimerStart ( 1000 << DATA_RATE_UPDATE_SHIFT ); + TimerStart ( 1 * 1000 ); // // Initialize the packet @@ -1095,11 +1224,11 @@ Tcp4Open ( Tcp4ConfigData.AccessPoint.StationAddress.Addr[2] = 0; Tcp4ConfigData.AccessPoint.StationAddress.Addr[3] = 0; Tcp4ConfigData.AccessPoint.StationPort = 0; - Tcp4ConfigData.AccessPoint.RemoteAddress.Addr[0] = (UINT8) RemoteHostAddress.sin_addr.s_addr; - Tcp4ConfigData.AccessPoint.RemoteAddress.Addr[1] = (UINT8)( RemoteHostAddress.sin_addr.s_addr >> 8 ); - Tcp4ConfigData.AccessPoint.RemoteAddress.Addr[2] = (UINT8)( RemoteHostAddress.sin_addr.s_addr >> 16 ); - Tcp4ConfigData.AccessPoint.RemoteAddress.Addr[3] = (UINT8)( RemoteHostAddress.sin_addr.s_addr >> 24 ); - Tcp4ConfigData.AccessPoint.RemotePort = RemoteHostAddress.sin_port; + Tcp4ConfigData.AccessPoint.RemoteAddress.Addr[0] = (UINT8) ((struct sockaddr_in *)&RemoteHostAddress)->sin_addr.s_addr; + Tcp4ConfigData.AccessPoint.RemoteAddress.Addr[1] = (UINT8)( ((struct sockaddr_in *)&RemoteHostAddress)->sin_addr.s_addr >> 8 ); + Tcp4ConfigData.AccessPoint.RemoteAddress.Addr[2] = (UINT8)( ((struct sockaddr_in *)&RemoteHostAddress)->sin_addr.s_addr >> 16 ); + Tcp4ConfigData.AccessPoint.RemoteAddress.Addr[3] = (UINT8)( ((struct sockaddr_in *)&RemoteHostAddress)->sin_addr.s_addr >> 24 ); + Tcp4ConfigData.AccessPoint.RemotePort = ((struct sockaddr_in *)&RemoteHostAddress)->sin_port; Tcp4ConfigData.AccessPoint.UseDefaultAddress = TRUE; Tcp4ConfigData.AccessPoint.SubnetMask.Addr[0] = 0; Tcp4ConfigData.AccessPoint.SubnetMask.Addr[1] = 0; @@ -1152,13 +1281,13 @@ Tcp4Open ( // // Display the connection // - pIpAddress = (UINT8 *)&RemoteHostAddress.sin_addr.s_addr; + pIpAddress = (UINT8 *)&((struct sockaddr_in *)&RemoteHostAddress)->sin_addr.s_addr; Print ( L"Connected to %d.%d.%d.%d:%d\r\n", pIpAddress[0], pIpAddress[1], pIpAddress[2], pIpAddress[3], - htons ( RemoteHostAddress.sin_port )); + htons ( ((struct sockaddr_in *)&RemoteHostAddress)->sin_port )); } while ( 0 ); if ( EFI_ERROR ( Status )) { @@ -1193,10 +1322,10 @@ TimerCallback ( IN VOID * pContext ) { - UINT64 BytesSent; - UINT64 DeltaBytes; - UINT32 Delta; - UINT64 Average; + UINT32 Average; + UINT64 BitsPerSecond; + UINT32 Index; + UINT64 TotalBytes; // // Notify the other code of the timer tick @@ -1206,65 +1335,82 @@ TimerCallback ( // // Update the average bytes per second // - BytesSent = TotalBytesSent; - if ( 0 != BytesSent ) { - DeltaBytes = AverageBytes >> AVERAGE_SHIFT_COUNT; - AverageBytes -= DeltaBytes; - DeltaBytes = BytesSent - PreviousBytes; - PreviousBytes = BytesSent; - AverageBytes += DeltaBytes; + if ( 0 != TotalBytesSent ) { + BytesSent[ In ] = TotalBytesSent; + TotalBytesSent = 0; + In += 1; + if ( DATA_SAMPLES <= In ) { + In = 0; + } // // Separate the samples // - if (( 2 << AVERAGE_SHIFT_COUNT ) == Samples ) { + if ( DATA_SAMPLES == Samples ) { Print ( L"---------- Stable average ----------\r\n" ); } Samples += 1; + // + // Compute the data rate + // + TotalBytes = 0; + for ( Index = 0; DATA_SAMPLES > Index; Index++ ) { + TotalBytes += BytesSent[ Index ]; + } + Average = (UINT32)RShiftU64 ( TotalBytes, DATA_SAMPLE_SHIFT ); + BitsPerSecond = Average * 8; + // // Display the data rate // - Delta = (UINT32)( DeltaBytes >> DATA_RATE_UPDATE_SHIFT ); - Average = AverageBytes >> ( AVERAGE_SHIFT_COUNT + DATA_RATE_UPDATE_SHIFT ); - if ( Average < RANGE_SWITCH ) { - Print ( L"%d Bytes/sec, Ave: %d Bytes/Sec\r\n", - Delta, - (UINT32) Average ); + if (( RANGE_SWITCH >> 10 ) > Average ) { + Print ( L"Ave: %d Bytes/Sec, %Ld Bits/sec\r\n", + Average, + BitsPerSecond ); } else { - Average >>= 10; - if ( Average < RANGE_SWITCH ) { - Print ( L"%d Bytes/sec, Ave: %d KiBytes/Sec\r\n", - Delta, - (UINT32) Average ); + BitsPerSecond /= 1000; + if ( RANGE_SWITCH > Average ) { + Print ( L"Ave: %d.%03d KiBytes/Sec, %Ld KBits/sec\r\n", + Average >> 10, + (( Average & 0x3ff ) * 1000 ) >> 10, + BitsPerSecond ); } else { + BitsPerSecond /= 1000; Average >>= 10; - if ( Average < RANGE_SWITCH ) { - Print ( L"%d Bytes/sec, Ave: %d MiBytes/Sec\r\n", - Delta, - (UINT32) Average ); + if ( RANGE_SWITCH > Average ) { + Print ( L"Ave: %d.%03d MiBytes/Sec, %Ld MBits/sec\r\n", + Average >> 10, + (( Average & 0x3ff ) * 1000 ) >> 10, + BitsPerSecond ); } else { + BitsPerSecond /= 1000; Average >>= 10; - if ( Average < RANGE_SWITCH ) { - Print ( L"%d Bytes/sec, Ave: %d GiBytes/Sec\r\n", - Delta, - (UINT32) Average ); + if ( RANGE_SWITCH > Average ) { + Print ( L"Ave: %d.%03d GiBytes/Sec, %Ld GBits/sec\r\n", + Average >> 10, + (( Average & 0x3ff ) * 1000 ) >> 10, + BitsPerSecond ); } else { + BitsPerSecond /= 1000; Average >>= 10; - if ( Average < RANGE_SWITCH ) { - Print ( L"%d Bytes/sec, Ave: %d TiBytes/Sec\r\n", - Delta, - Average ); + if ( RANGE_SWITCH > Average ) { + Print ( L"Ave: %d.%03d TiBytes/Sec, %Ld TBits/sec\r\n", + Average >> 10, + (( Average & 0x3ff ) * 1000 ) >> 10, + BitsPerSecond ); } else { + BitsPerSecond /= 1000; Average >>= 10; - Print ( L"%d Bytes/sec, Ave: %d PiBytes/Sec\r\n", - Delta, - (UINT32) Average ); + Print ( L"Ave: %d.%03d PiBytes/Sec, %Ld PBits/sec\r\n", + Average >> 10, + (( Average & 0x3ff ) * 1000 ) >> 10, + BitsPerSecond ); } } } @@ -1528,17 +1674,8 @@ main ( // No bytes sent so far // TotalBytesSent = 0; - AverageBytes = 0; - PreviousBytes = 0; Samples = 0; - - // - // Get the port number - // - ZeroMem ( &RemoteHostAddress, sizeof ( RemoteHostAddress )); - RemoteHostAddress.sin_len = sizeof ( RemoteHostAddress ); - RemoteHostAddress.sin_family = AF_INET; - RemoteHostAddress.sin_port = htons ( PcdGet16 ( DataSource_Port )); + memset ( &BytesSent, 0, sizeof ( BytesSent )); // // Get the IP address diff --git a/AppPkg/Applications/Sockets/DataSource/DataSource.inf b/AppPkg/Applications/Sockets/DataSource/DataSource.inf index ec9cf6c185..8353041ddf 100644 --- a/AppPkg/Applications/Sockets/DataSource/DataSource.inf +++ b/AppPkg/Applications/Sockets/DataSource/DataSource.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such @@ -39,10 +39,11 @@ [Pcd] - gStdLibTokenSpaceGuid.DataSource_Port + gAppPkgTokenSpaceGuid.DataSource_Port [Packages] + AppPkg/AppPkg.dec MdePkg/MdePkg.dec ShellPkg/ShellPkg.dec StdLib/StdLib.dec diff --git a/AppPkg/Applications/Sockets/GetAddrInfo/GetAddrInfo.c b/AppPkg/Applications/Sockets/GetAddrInfo/GetAddrInfo.c new file mode 100644 index 0000000000..3db71ba3dc --- /dev/null +++ b/AppPkg/Applications/Sockets/GetAddrInfo/GetAddrInfo.c @@ -0,0 +1,120 @@ +/** @file + Test the getaddrinfo 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 +#include +#include +#include +#include +#include + +#include + +#include + +char mBuffer[65536]; + + +/** + Test the getaddrinfo API + + @param [in] Argc The number of arguments + @param [in] Argv The argument value array + + @retval 0 The application exited normally. + @retval Other An error occurred. +**/ +int +main ( + IN int Argc, + IN char **Argv + ) +{ + int AppStatus; + int Index; + int MaxLen; + struct addrinfo * pAddrInfo; + char * pHostName; + struct addrinfo * pInfo; + char * pServerName; + + // + // Determine if the host name is specified + // + AppStatus = 0; + if ( 1 == Argc ) { + printf ( "%s \r\n", Argv[0]); + } + else { + // + // Translate the host name + // + pHostName = Argv[1]; + pServerName = NULL; + if ( 2 < Argc ) { + pServerName = Argv[2]; + } + AppStatus = getaddrinfo ( pHostName, + pServerName, + NULL, + &pAddrInfo ); + if ( 0 != AppStatus ) { + printf ( "ERROR - address info not found, errno: %d\r\n", AppStatus ); + } + if ( NULL == pAddrInfo ) { + printf ( "ERROR - No address info structure allocated\r\n" ); + } + else { + // + // Walk the list of addresses + // + pInfo = pAddrInfo; + while ( NULL != pInfo ) { + // + // Display this entry + // + printf ( "0x%08x: ai_flags\r\n", pInfo->ai_flags ); + printf ( "0x%08x: ai_family\r\n", pInfo->ai_family ); + printf ( "0x%08x: ai_socktype\r\n", pInfo->ai_socktype ); + printf ( "0x%08x: ai_protocol\r\n", pInfo->ai_protocol ); + printf ( "0x%08x: ai_addrlen\r\n", pInfo->ai_addrlen ); + printf ( "%s: ai_canonname\r\n", pInfo->ai_canonname ); + printf ( " 0x%02x: ai_addr->sa_len\r\n", (UINT8)pInfo->ai_addr->sa_len ); + printf ( " 0x%02x: ai_addr->sa_family\r\n", (UINT8)pInfo->ai_addr->sa_family ); + MaxLen = pInfo->ai_addr->sa_len; + if ( sizeof ( struct sockaddr_in6 ) < MaxLen ) { + MaxLen = sizeof ( struct sockaddr_in6 ); + } + for ( Index = 0; ( MaxLen - 2 ) > Index; Index++ ) { + printf ( " 0x%02x: ai_addr->sa_data[%02d]\r\n", (UINT8)pInfo->ai_addr->sa_data [ Index ], Index ); + } + + // + // Set the next entry + // + pInfo = pInfo->ai_next; + } + + // + // Done with this structures + // + freeaddrinfo ( pAddrInfo ); + } + } + + // + // All done + // + return AppStatus; +} diff --git a/AppPkg/Applications/Sockets/GetAddrInfo/GetAddrInfo.inf b/AppPkg/Applications/Sockets/GetAddrInfo/GetAddrInfo.inf new file mode 100644 index 0000000000..c0d5c29573 --- /dev/null +++ b/AppPkg/Applications/Sockets/GetAddrInfo/GetAddrInfo.inf @@ -0,0 +1,63 @@ +#/** @file +# GetAddrInfo Application +# +# This file contains an 'Intel Peripheral Driver' and is +# licensed for Intel CPUs and chipsets under the terms of your +# license agreement with Intel or your vendor. This file may +# be modified by the user, subject to additional terms of the +# license agreement +# +# +# Copyright (c) 2011 Intel Corporation. All rights reserved +# This software and associated documentation (if any) is furnished +# under a license and may only be used or copied in accordance +# with the terms of the license. Except as permitted by such +# license, no part of this software or documentation may be +# reproduced, stored in a retrieval system, or transmitted in any +# form or by any means without the express written consent of +# Intel Corporation. +# +## + + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = GetAddrInfo + FILE_GUID = 4C26DF71-EBE7-4dea-B5E2-0B5980433908 + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 1.0 + ENTRY_POINT = ShellCEntryLib + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 IPF EBC +# + +[Sources] + GetAddrInfo.c + + +[Packages] + MdePkg/MdePkg.dec + ShellPkg/ShellPkg.dec + StdLib/StdLib.dec + + +[LibraryClasses] + BaseMemoryLib + BsdSocketLib + DevShell + EfiSocketLib + LibC + LibMath + LibNetUtil + ShellCEntryLib + UefiBootServicesTableLib +# UseSocketDxe + +[BuildOptions] + INTEL:*_*_*_CC_FLAGS = /Qdiag-disable:181,186 + MSFT:*_*_*_CC_FLAGS = /Od + GCC:*_*_*_CC_FLAGS = -O0 -Wno-unused-variable + diff --git a/AppPkg/Applications/Sockets/GetHostByAddr/GetHostByAddr.inf b/AppPkg/Applications/Sockets/GetHostByAddr/GetHostByAddr.inf index f31b8278dd..e2aee9e489 100644 --- a/AppPkg/Applications/Sockets/GetHostByAddr/GetHostByAddr.inf +++ b/AppPkg/Applications/Sockets/GetHostByAddr/GetHostByAddr.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/GetHostByDns/GetHostByDns.inf b/AppPkg/Applications/Sockets/GetHostByDns/GetHostByDns.inf index 417eb5e228..a69e1ffcd9 100644 --- a/AppPkg/Applications/Sockets/GetHostByDns/GetHostByDns.inf +++ b/AppPkg/Applications/Sockets/GetHostByDns/GetHostByDns.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/GetHostByName/GetHostByName.inf b/AppPkg/Applications/Sockets/GetHostByName/GetHostByName.inf index 48beb2a26f..84dc8507b7 100644 --- a/AppPkg/Applications/Sockets/GetHostByName/GetHostByName.inf +++ b/AppPkg/Applications/Sockets/GetHostByName/GetHostByName.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/GetNameInfo/GetNameInfo.c b/AppPkg/Applications/Sockets/GetNameInfo/GetNameInfo.c new file mode 100644 index 0000000000..c9292e1fa7 --- /dev/null +++ b/AppPkg/Applications/Sockets/GetNameInfo/GetNameInfo.c @@ -0,0 +1,120 @@ +/** @file + Test the getnameinfo 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 +#include +#include +#include +#include +#include + +#include + +#include + +char mBuffer[65536]; +char mHostName[256]; +char mServiceName[256]; + +/** + Test the getnameinfo API + + @param [in] Argc The number of arguments + @param [in] Argv The argument value array + + @retval 0 The application exited normally. + @retval Other An error occurred. +**/ +int +main ( + IN int Argc, + IN char **Argv + ) +{ + int AppStatus; + struct addrinfo * pAddrInfo; + char * pHostName; + struct addrinfo * pInfo; + char * pServerName; + + // + // Determine if the host name is specified + // + AppStatus = 0; + if ( 1 == Argc ) { + printf ( "%s \r\n", Argv[0]); + } + else { + // + // Translate the host name + // + pHostName = Argv[1]; + pServerName = NULL; + if ( 2 < Argc ) { + pServerName = Argv[2]; + } + AppStatus = getaddrinfo ( pHostName, + pServerName, + NULL, + &pAddrInfo ); + if ( 0 != AppStatus ) { + printf ( "ERROR - address info not found, errno: %d\r\n", AppStatus ); + } + if ( NULL == pAddrInfo ) { + printf ( "ERROR - No address info structure allocated\r\n" ); + } + else { + // + // Walk the list of names + // + pInfo = pAddrInfo; + while ( NULL != pInfo ) { + // + // Get the name info + // + AppStatus = getnameinfo ((struct sockaddr *)pInfo->ai_addr, + pInfo->ai_addrlen, + &mHostName[0], + sizeof ( mHostName ), + &mServiceName[0], + sizeof ( mServiceName ), + 0 ); + if ( 0 != AppStatus ) { + break; + } + + // + // Display this entry + // + printf ( "%s: HostName\r\n", mHostName[0]); + printf ( "%s: Service Name\r\n", &mServiceName[0]); + + // + // Set the next entry + // + pInfo = pInfo->ai_next; + } + + // + // Done with this structures + // + freeaddrinfo ( pAddrInfo ); + } + } + + // + // All done + // + return AppStatus; +} diff --git a/AppPkg/Applications/Sockets/GetNameInfo/GetNameInfo.inf b/AppPkg/Applications/Sockets/GetNameInfo/GetNameInfo.inf new file mode 100644 index 0000000000..0b9ceb59ac --- /dev/null +++ b/AppPkg/Applications/Sockets/GetNameInfo/GetNameInfo.inf @@ -0,0 +1,63 @@ +#/** @file +# GetNameInfo Application +# +# This file contains an 'Intel Peripheral Driver' and is +# licensed for Intel CPUs and chipsets under the terms of your +# license agreement with Intel or your vendor. This file may +# be modified by the user, subject to additional terms of the +# license agreement +# +# +# Copyright (c) 2011 Intel Corporation. All rights reserved +# This software and associated documentation (if any) is furnished +# under a license and may only be used or copied in accordance +# with the terms of the license. Except as permitted by such +# license, no part of this software or documentation may be +# reproduced, stored in a retrieval system, or transmitted in any +# form or by any means without the express written consent of +# Intel Corporation. +# +## + + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = GetNameInfo + FILE_GUID = 553087F6-BAAC-4d7f-97B4-31D8179AAE15 + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 1.0 + ENTRY_POINT = ShellCEntryLib + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 IPF EBC +# + +[Sources] + GetNameInfo.c + + +[Packages] + MdePkg/MdePkg.dec + ShellPkg/ShellPkg.dec + StdLib/StdLib.dec + + +[LibraryClasses] + BaseMemoryLib + BsdSocketLib + DevShell + EfiSocketLib + LibC + LibMath + LibNetUtil + ShellCEntryLib + UefiBootServicesTableLib +# UseSocketDxe + +[BuildOptions] + INTEL:*_*_*_CC_FLAGS = /Qdiag-disable:181,186 + MSFT:*_*_*_CC_FLAGS = /Od + GCC:*_*_*_CC_FLAGS = -O0 -Wno-unused-variable + diff --git a/AppPkg/Applications/Sockets/GetNetByAddr/GetNetByAddr.inf b/AppPkg/Applications/Sockets/GetNetByAddr/GetNetByAddr.inf index c280d186f1..2bc1836f48 100644 --- a/AppPkg/Applications/Sockets/GetNetByAddr/GetNetByAddr.inf +++ b/AppPkg/Applications/Sockets/GetNetByAddr/GetNetByAddr.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/GetNetByName/GetNetByName.inf b/AppPkg/Applications/Sockets/GetNetByName/GetNetByName.inf index 1b9764e54b..8c706ba947 100644 --- a/AppPkg/Applications/Sockets/GetNetByName/GetNetByName.inf +++ b/AppPkg/Applications/Sockets/GetNetByName/GetNetByName.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/GetServByName/GetServByName.inf b/AppPkg/Applications/Sockets/GetServByName/GetServByName.inf index 290d15e938..7af2f6c532 100644 --- a/AppPkg/Applications/Sockets/GetServByName/GetServByName.inf +++ b/AppPkg/Applications/Sockets/GetServByName/GetServByName.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/GetServByPort/GetServByPort.inf b/AppPkg/Applications/Sockets/GetServByPort/GetServByPort.inf index 1a232c0aad..ef902c4a3b 100644 --- a/AppPkg/Applications/Sockets/GetServByPort/GetServByPort.inf +++ b/AppPkg/Applications/Sockets/GetServByPort/GetServByPort.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/OobRx/OobRx.inf b/AppPkg/Applications/Sockets/OobRx/OobRx.inf index 516f7cc215..fd137dd232 100644 --- a/AppPkg/Applications/Sockets/OobRx/OobRx.inf +++ b/AppPkg/Applications/Sockets/OobRx/OobRx.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/OobTx/OobTx.inf b/AppPkg/Applications/Sockets/OobTx/OobTx.inf index cc9c2b583e..7f7c091b09 100644 --- a/AppPkg/Applications/Sockets/OobTx/OobTx.inf +++ b/AppPkg/Applications/Sockets/OobTx/OobTx.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/RawIp4Rx/RawIp4Rx.inf b/AppPkg/Applications/Sockets/RawIp4Rx/RawIp4Rx.inf index 4eba247f34..b543f294ef 100644 --- a/AppPkg/Applications/Sockets/RawIp4Rx/RawIp4Rx.inf +++ b/AppPkg/Applications/Sockets/RawIp4Rx/RawIp4Rx.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/RawIp4Tx/RawIp4Tx.inf b/AppPkg/Applications/Sockets/RawIp4Tx/RawIp4Tx.inf index 5452df08f6..8852f148c8 100644 --- a/AppPkg/Applications/Sockets/RawIp4Tx/RawIp4Tx.inf +++ b/AppPkg/Applications/Sockets/RawIp4Tx/RawIp4Tx.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/RecvDgram/RecvDgram.inf b/AppPkg/Applications/Sockets/RecvDgram/RecvDgram.inf index bef842ad54..6e51317a7a 100644 --- a/AppPkg/Applications/Sockets/RecvDgram/RecvDgram.inf +++ b/AppPkg/Applications/Sockets/RecvDgram/RecvDgram.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/SetHostName/SetHostName.inf b/AppPkg/Applications/Sockets/SetHostName/SetHostName.inf index 2b0bdc7914..539a315e48 100644 --- a/AppPkg/Applications/Sockets/SetHostName/SetHostName.inf +++ b/AppPkg/Applications/Sockets/SetHostName/SetHostName.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/SetSockOpt/SetSockOpt.inf b/AppPkg/Applications/Sockets/SetSockOpt/SetSockOpt.inf index 835e1ebb61..4de5133801 100644 --- a/AppPkg/Applications/Sockets/SetSockOpt/SetSockOpt.inf +++ b/AppPkg/Applications/Sockets/SetSockOpt/SetSockOpt.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such diff --git a/AppPkg/Applications/Sockets/Sockets.inc b/AppPkg/Applications/Sockets/Sockets.inc index c1bd8bcc4e..8b6bf8ed65 100644 --- a/AppPkg/Applications/Sockets/Sockets.inc +++ b/AppPkg/Applications/Sockets/Sockets.inc @@ -6,9 +6,11 @@ [Components] AppPkg/Applications/Sockets/DataSink/DataSink.inf AppPkg/Applications/Sockets/DataSource/DataSource.inf + AppPkg/Applications/Sockets/GetAddrInfo/GetAddrInfo.inf AppPkg/Applications/Sockets/GetHostByAddr/GetHostByAddr.inf AppPkg/Applications/Sockets/GetHostByDns/GetHostByDns.inf AppPkg/Applications/Sockets/GetHostByName/GetHostByName.inf + AppPkg/Applications/Sockets/GetNameInfo/GetNameInfo.inf AppPkg/Applications/Sockets/GetNetByAddr/GetNetByAddr.inf AppPkg/Applications/Sockets/GetNetByName/GetNetByName.inf AppPkg/Applications/Sockets/GetServByName/GetServByName.inf diff --git a/AppPkg/Applications/Sockets/TftpServer/TftpServer.c b/AppPkg/Applications/Sockets/TftpServer/TftpServer.c index 1da3c250d5..e9ab0dee5f 100644 --- a/AppPkg/Applications/Sockets/TftpServer/TftpServer.c +++ b/AppPkg/Applications/Sockets/TftpServer/TftpServer.c @@ -1,106 +1,211 @@ -/*++ - This file contains an 'Intel UEFI Application' and is - licensed for Intel CPUs and chipsets under the terms of your - license agreement with Intel or your vendor. This file may - be modified by the user, subject to additional terms of the - license agreement ---*/ -/*++ - -Copyright (c) 2011 Intel Corporation. All rights reserved -This software and associated documentation (if any) is furnished -under a license and may only be used or copied in accordance -with the terms of the license. Except as permitted by such -license, no part of this software or documentation may be -reproduced, stored in a retrieval system, or transmitted in any -form or by any means without the express written consent of -Intel Corporation. - ---*/ - /** @file This is a simple TFTP server application + Copyright (c) 2011, 2012, 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 -TSDT_TFTP_SERVER mTftpServer; ///< TFTP server's control structure +TSDT_TFTP_SERVER mTftpServer; ///< TFTP server's control structure +volatile BOOLEAN mbTftpServerExit; ///< Set TRUE to cause TFTP server to exit /** - Add a connection context to the list of connection contexts. + Read file data into a buffer - @param [in] pTftpServer The TFTP server control structure address. + @param [in] pContext Connection context structure address - @retval Context structure address, NULL if allocation fails + @retval TRUE if a read error occurred **/ -TSDT_CONNECTION_CONTEXT * -ContextAdd ( - IN TSDT_TFTP_SERVER * pTftpServer +BOOLEAN +BufferFill ( + IN TSDT_CONNECTION_CONTEXT * pContext ) { - size_t LengthInBytes; - TSDT_CONNECTION_CONTEXT * pContext; - EFI_STATUS Status; + BOOLEAN bReadError; + size_t BytesRead; + UINT64 LengthInBytes; DBG_ENTER ( ); // - // Use for/break instead of goto + // Use break instead of goto // + bReadError = FALSE; for ( ; ; ) { // - // Allocate a new context - // - LengthInBytes = sizeof ( *pContext ); - Status = gBS->AllocatePool ( EfiRuntimeServicesData, - LengthInBytes, - (VOID **)&pContext ); - if ( EFI_ERROR ( Status )) { - DEBUG (( DEBUG_ERROR | DEBUG_POOL, - "ERROR - Failed to allocate the context, Status: %r\r\n", - Status )); - pContext = NULL; + // Determine if there is any work to do + // + LengthInBytes = DIM ( pContext->FileData ) >> 1; + if (( pContext->ValidBytes > LengthInBytes ) + || ( 0 == pContext->BytesRemaining )) { + break; + } + + // + // Determine the number of bytes to read + // + if ( LengthInBytes > pContext->BytesRemaining ) { + LengthInBytes = pContext->BytesRemaining; + } + + // + // Read in the next portion of the file + // + BytesRead = fread ( pContext->pFill, + 1, + (size_t)LengthInBytes, + pContext->File ); + if ( -1 == BytesRead ) { + bReadError = TRUE; break; } + // + // Account for the file data read + // + pContext->BytesRemaining -= BytesRead; + pContext->ValidBytes += BytesRead; + DEBUG (( DEBUG_FILE_BUFFER, + "0x%08x: Buffer filled with %Ld bytes, %Ld bytes ramaining\r\n", + pContext->pFill, + BytesRead, + pContext->BytesRemaining )); + + // + // Set the next buffer location + // + pContext->pFill += BytesRead; + if ( pContext->pEnd <= pContext->pFill ) { + pContext->pFill = &pContext->FileData[ 0 ]; + } + + // + // Verify that the end of the buffer is reached + // + ASSERT ( 0 == ( DIM ( pContext->FileData ) & 1 )); + break; + } + + // + // Return the read status + // + DBG_EXIT ( ); + return bReadError; +} + + +/** + Add a connection context to the list of connection contexts. + + @param [in] pTftpServer Address of the ::TSDT_TFTP_SERVER structure + @param [in] SocketFd Socket file descriptor + + @retval Context structure address, NULL if allocation fails + +**/ +TSDT_CONNECTION_CONTEXT * +ContextAdd ( + IN TSDT_TFTP_SERVER * pTftpServer, + IN int SocketFd + ) +{ + TSDT_CONNECTION_CONTEXT * pContext; + TFTP_PACKET * pEnd; + TFTP_PACKET * pPacket; + + DBG_ENTER ( ); + + // + // Allocate a new context + // + pContext = (TSDT_CONNECTION_CONTEXT *)AllocateZeroPool ( sizeof ( *pContext )); + if ( NULL != pContext ) { // // Initialize the context // - ZeroMem ( pContext, LengthInBytes ); + pContext->SocketFd = SocketFd; CopyMem ( &pContext->RemoteAddress, &pTftpServer->RemoteAddress, sizeof ( pContext->RemoteAddress )); - pContext->BlockSize = TFTP_MAX_BLOCK_SIZE; - pContext->pBuffer = &pContext->FileData[0]; - pContext->pEnd = &pContext->pBuffer[sizeof ( pContext->pBuffer )]; - pContext->MaxTransferSize = 0; - pContext->MaxTransferSize -= 1; + pContext->BlockSize = 512; + + // + // Buffer management + // + pContext->pFill = &pContext->FileData[ 0 ]; + pContext->pEnd = &pContext->FileData[ sizeof ( pContext->FileData )]; + pContext->pBuffer = pContext->pFill; + + // + // Window management + // + pContext->MaxTimeout = MultU64x32 ( PcdGet32 ( Tftp_MaxTimeoutInSec ), + 2 * 1000 * 1000 * 1000 ); + pContext->Rtt2x = pContext->MaxTimeout; + pContext->WindowSize = MAX_PACKETS; + WindowTimeout ( pContext ); + + // + // Place the packets on the free list + // + pPacket = &pContext->Tx[ 0 ]; + pEnd = &pPacket[ DIM ( pContext->Tx )]; + while ( pEnd > pPacket ) { + PacketFree ( pContext, pPacket ); + pPacket += 1; + } // // Display the new context // - DEBUG (( DEBUG_PORT_WORK | DEBUG_INFO, - "0x%08x: Context for %d.%d.%d.%d:%d\r\n", - pContext, - (UINT8)pContext->RemoteAddress.sin_addr.s_addr, - (UINT8)( pContext->RemoteAddress.sin_addr.s_addr >> 8 ), - (UINT8)( pContext->RemoteAddress.sin_addr.s_addr >> 16 ), - (UINT8)( pContext->RemoteAddress.sin_addr.s_addr >> 24 ), - htons ( pContext->RemoteAddress.sin_port ))); + if ( AF_INET == pTftpServer->RemoteAddress.v4.sin_family ) { + DEBUG (( DEBUG_PORT_WORK, + "0x%08x: Context for %d.%d.%d.%d:%d\r\n", + pContext, + (UINT8)pTftpServer->RemoteAddress.v4.sin_addr.s_addr, + (UINT8)( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 8 ), + (UINT8)( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 16 ), + (UINT8)( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 24 ), + htons ( pTftpServer->RemoteAddress.v4.sin_port ))); + } + else { + DEBUG (( DEBUG_PORT_WORK, + "0x%08x: Context for [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", + pContext, + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pTftpServer->RemoteAddress.v6.sin6_port ))); + } // // Add the context to the context list // pContext->pNext = pTftpServer->pContextList; pTftpServer->pContextList = pContext; - - // - // All done - // - break; } // @@ -114,10 +219,8 @@ ContextAdd ( /** Locate a remote connection context. - @param [in] pTftpServer The TFTP server control structure address. - + @param [in] pTftpServer Address of the ::TSDT_TFTP_SERVER structure @param [in] pIpAddress The start of the remote IP address in network order - @param [in] Port The remote port number @retval Context structure address, NULL if not found @@ -140,8 +243,9 @@ ContextFind ( // // Attempt to locate the remote network connection // - if (( pTftpServer->RemoteAddress.sin_addr.s_addr == pContext->RemoteAddress.sin_addr.s_addr ) - && ( pTftpServer->RemoteAddress.sin_port == pContext->RemoteAddress.sin_port )) { + if ( 0 == memcmp ( &pTftpServer->RemoteAddress, + &pContext->RemoteAddress, + pTftpServer->RemoteAddress.v6.sin6_len )) { // // The connection was found // @@ -168,9 +272,8 @@ ContextFind ( /** Remove a context from the list. - @param [in] pTftpServer The TFTP server control structure address. - - @param [in] pContext The context structure address. + @param [in] pTftpServer Address of the ::TSDT_TFTP_SERVER structure + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure **/ VOID @@ -228,297 +331,900 @@ ContextRemove ( /** - Process the work for the sockets. + Queue data packets for transmission + + @param [in] pContext Connection context structure address - @param [in] pTftpServer The TFTP server control structure address. + @retval TRUE if a read error occurred **/ -VOID -PortWork ( - IN TSDT_TFTP_SERVER * pTftpServer +BOOLEAN +PacketFill ( + IN TSDT_CONNECTION_CONTEXT * pContext ) { - TSDT_CONNECTION_CONTEXT * pContext; - socklen_t RemoteAddressLength; + BOOLEAN bReadError; + UINT64 LengthInBytes; + UINT8 * pBuffer; + TFTP_PACKET * pPacket; DBG_ENTER ( ); // - // Handle input events + // Use break instead of goto // - if ( 0 != ( pTftpServer->TftpPort.revents & POLLRDNORM )) { + bReadError = FALSE; + for ( ; ; ) { // - // Receive the message from the remote system + // Fill the buffer if necessary // - RemoteAddressLength = sizeof ( pTftpServer->RemoteAddress ); - pTftpServer->RxBytes = recvfrom ( pTftpServer->TftpPort.fd, - &pTftpServer->RxBuffer[0], - sizeof ( pTftpServer->RxBuffer ), - 0, - (struct sockaddr *) &pTftpServer->RemoteAddress, - &RemoteAddressLength ); - if ( -1 != pTftpServer->RxBytes ) { - pTftpServer->RemoteAddress.sin_len = (UINT8) RemoteAddressLength; - DEBUG (( DEBUG_TFTP_PORT, - "Received %d bytes from %d.%d.%d.%d:%d\r\n", - pTftpServer->RxBytes, - pTftpServer->RemoteAddress.sin_addr.s_addr & 0xff, - ( pTftpServer->RemoteAddress.sin_addr.s_addr >> 8 ) & 0xff, - ( pTftpServer->RemoteAddress.sin_addr.s_addr >> 16 ) & 0xff, - ( pTftpServer->RemoteAddress.sin_addr.s_addr >> 24 ) & 0xff, - htons ( pTftpServer->RemoteAddress.sin_port ))); - + bReadError = BufferFill ( pContext ); + if ( bReadError ) { // - // Lookup connection context using the remote system address and port - // to determine if an existing connection to this remote - // system exists + // File access mode not supported // - pContext = ContextFind ( pTftpServer ); + DEBUG (( DEBUG_ERROR | DEBUG_TFTP_REQUEST, + "ERROR - File read failure!\r\n" )); // - // Process the received message + // Tell the client of the error // - TftpProcessRequest ( pTftpServer, pContext ); + SendError ( pContext, + TFTP_ERROR_SEE_MSG, + (UINT8 *)"Read failure" ); + break; } - else { + + // + // Determine if any packets can be filled + // + if ( pContext->bEofSent + || ( NULL == pContext->pFreeList )) { // - // Receive error on the TFTP server port - // Close the server socket + // All of the packets are filled // - DEBUG (( DEBUG_ERROR, - "ERROR - Failed receive on TFTP server port, errno: 0x%08x\r\n", - errno )); - pTftpServer->TftpPort.revents |= POLLHUP; + break; } - } - // - // Handle the close event - // - if ( 0 != ( pTftpServer->TftpPort.revents & POLLHUP )) { // - // Close the port + // Set the TFTP opcode and block number + // + pPacket = PacketGet ( pContext ); + pBuffer = &pPacket->TxBuffer[ 0 ]; + *pBuffer++ = 0; + *pBuffer++ = TFTP_OP_DATA; + *pBuffer++ = (UINT8)( pContext->BlockNumber >> 8 ); + *pBuffer++ = (UINT8)pContext->BlockNumber; + + // + // Determine how much data needs to be sent + // + LengthInBytes = pContext->BlockSize; + if (( pContext->BytesToSend < TFTP_MAX_BLOCK_SIZE ) + && ( LengthInBytes > pContext->BytesToSend )) { + LengthInBytes = pContext->BytesToSend; + pContext->bEofSent = TRUE; + } + DEBUG (( DEBUG_TX_PACKET, + "0x%08x: Packet, Block %d filled with %d bytes\r\n", + pPacket, + pContext->BlockNumber, + (UINT32)LengthInBytes )); + + // + // Copy the file data into the packet + // + pPacket->TxBytes = (ssize_t)( 2 + 2 + LengthInBytes ); + if ( 0 < LengthInBytes ) { + CopyMem ( pBuffer, + pContext->pBuffer, + (UINTN)LengthInBytes ); + DEBUG (( DEBUG_FILE_BUFFER, + "0x%08x: Buffer consumed %d bytes of file data\r\n", + pContext->pBuffer, + LengthInBytes )); + + // + // Account for the file data consumed + // + pContext->ValidBytes -= LengthInBytes; + pContext->BytesToSend -= LengthInBytes; + pContext->pBuffer += LengthInBytes; + if ( pContext->pEnd <= pContext->pBuffer ) { + pContext->pBuffer = &pContext->FileData[ 0 ]; + } + } + + // + // Queue the packet for transmission // - close ( pTftpServer->TftpPort.fd ); - pTftpServer->TftpPort.fd = -1; + PacketQueue ( pContext, pPacket ); } + // + // Return the read status + // DBG_EXIT ( ); + return bReadError; } /** - Scan the list of sockets and process any pending work + Free the packet - @param [in] pTftpServer The TFTP server control structure address. + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure + @param [in] pPacket Address of a ::TFTP_PACKET structure **/ VOID -SocketPoll ( - IN TSDT_TFTP_SERVER * pTftpServer +PacketFree( + IN TSDT_CONNECTION_CONTEXT * pContext, + IN TFTP_PACKET * pPacket ) { - int FDCount; - - DEBUG (( DEBUG_SOCKET_POLL, "Entering SocketPoll\r\n" )); + DBG_ENTER ( ); // - // Determine if any ports are active + // Don't free the error packet // - FDCount = poll ( &pTftpServer->TftpPort, - 1, - CLIENT_POLL_DELAY ); - if ( -1 == FDCount ) { - DEBUG (( DEBUG_ERROR | DEBUG_SOCKET_POLL, - "ERROR - errno: %d\r\n", - errno )); - } - - if ( 0 < FDCount ) { + if ( pPacket != &pContext->ErrorPacket ) { // - // Process this port + // Place the packet on the free list // - PortWork ( pTftpServer ); - pTftpServer->TftpPort.revents = 0; + pPacket->pNext = pContext->pFreeList; + pContext->pFreeList = pPacket; + DEBUG (( DEBUG_TX_PACKET, + "0x%08x: Packet queued to free list\r\n", + pPacket )); } - DEBUG (( DEBUG_SOCKET_POLL, "Exiting SocketPoll\r\n" )); + DBG_EXIT ( ); } /** - Convert a character to lower case + Get a packet from the free list for transmission - @param [in] Character The character to convert + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure - @return The lower case equivalent of the character + @retval Address of a ::TFTP_PACKET structure **/ -int -tolower ( - int Character +TFTP_PACKET * +PacketGet ( + IN TSDT_CONNECTION_CONTEXT * pContext ) { + TFTP_PACKET * pPacket; + + DBG_ENTER ( ); + // - // Determine if the character is upper case + // Get the next packet from the free list // - if (( 'A' <= Character ) && ( 'Z' >= Character )) { - // - // Convert the character to lower caes - // - Character += 'a' - 'A'; + pPacket = pContext->pFreeList; + if ( NULL != pPacket ) { + pContext->pFreeList = pPacket->pNext; + pPacket->RetryCount = 0; + DEBUG (( DEBUG_TX_PACKET, + "0x%08x: Packet removed from free list\r\n", + pPacket )); } // - // Return the converted character + // Return the packet // - return Character; + DBG_EXIT_HEX ( pPacket ); + return pPacket; } /** - Case independent string comparison + Queue the packet for transmission - @param [in] pString1 Zero terminated string address - @param [in] pString2 Zero terminated string address + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure + @param [in] pPacket Address of a ::TFTP_PACKET structure - @return Returns the first character difference between string 1 - and string 2. + @retval TRUE if a transmission error has occurred **/ -int -stricmp ( - char * pString1, - char * pString2 +BOOLEAN +PacketQueue ( + IN TSDT_CONNECTION_CONTEXT * pContext, + IN TFTP_PACKET * pPacket ) { - int Char1; - int Char2; - int Difference; + BOOLEAN bTransmitError; + TFTP_PACKET * pTail; + EFI_STATUS Status; + + DBG_ENTER ( ); // - // Walk the length of the strings + // Account for this data block // - do { - // - // Get the next characters - // - Char1 = (UINT8)*pString1++; - Char2 = (UINT8)*pString2++; + pPacket->BlockNumber = pContext->BlockNumber; + pContext->BlockNumber += 1; - // - // Convert them to lower case - // - Char1 = tolower ( Char1 ); - Char2 = tolower ( Char2 ); + // + // Queue the packet for transmission + // + pTail = pContext->pTxTail; + if ( NULL == pTail ) { + pContext->pTxHead = pPacket; + } + else { + pTail->pNext = pPacket; + } + pContext->pTxTail = pPacket; + pPacket->pNext = NULL; + DEBUG (( DEBUG_TX_PACKET, + "0x%08x: Packet queued to TX list\r\n", + pPacket )); - // - // Done when the characters differ - // - Difference = Char1 - Char2; - if ( 0 != Difference ) { - break; + // + // Start the transmission if necessary + // + bTransmitError = FALSE; + if ( pContext->PacketsInWindow < pContext->WindowSize ) { + Status = PacketTx ( pContext, pPacket ); + bTransmitError = (BOOLEAN)( EFI_ERROR ( Status )); + } + + // + // Return the transmit status + // + DBG_EXIT_TF ( bTransmitError ); + return bTransmitError; +} + + +/** + Remove a packet from the transmit queue + + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure + +**/ +TFTP_PACKET * +PacketRemove( + IN TSDT_CONNECTION_CONTEXT * pContext + ) +{ + TFTP_PACKET * pNext; + TFTP_PACKET * pPacket; + + DBG_ENTER ( ); + + // + // Remove a packet from the transmit queue + // + // + pPacket = pContext->pTxHead; + if ( NULL != pPacket ) { + pNext = pPacket->pNext; + pContext->pTxHead = pNext; + if ( NULL == pNext ) { + pContext->pTxTail = NULL; } + DEBUG (( DEBUG_TX_PACKET, + "0x%08x: Packet removed from TX list\r\n", + pPacket )); // - // Done at the end of the string + // Remove this packet from the window // - } while ( 0 != Char1 ); + pContext->PacketsInWindow -= 1; + } // - // Return the difference + // Return the packet // - return Difference; + DBG_EXIT_HEX ( pPacket ); + return pPacket; } /** - Get the next TFTP option + Transmit the packet - @param [in] pOption Address of a zero terminated option string - @param [in] pEnd End of buffer address - @param [in] ppNextOption Address to receive the address of the next - zero terminated option string + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure + @param [in] pPacket Address of a ::TFTP_PACKET structure @retval EFI_SUCCESS Message processed successfully **/ EFI_STATUS -TftpOptionGet ( - IN UINT8 * pOption, - IN UINT8 * pEnd, - IN UINT8 ** ppNextOption +PacketTx ( + IN TSDT_CONNECTION_CONTEXT * pContext, + IN TFTP_PACKET * pPacket ) { - UINT8 * pNextOption; + ssize_t LengthInBytes; EFI_STATUS Status; + DBG_ENTER ( ); + // - // Locate the end of the option + // Assume success // - pNextOption = pOption; - while (( pEnd > pNextOption ) && ( 0 != *pNextOption )) { - pNextOption += 1; - } - if ( pEnd <= pNextOption ) { + Status = EFI_SUCCESS; + + // + // Determine if this packet should be transmitted + // + if ( PcdGet32 ( Tftp_MaxRetry ) >= pPacket->RetryCount ) { + pPacket->RetryCount += 1; + // - // Error - end of buffer reached + // Display the operation // - DEBUG (( DEBUG_ERROR | DEBUG_TFTP_REQUEST, - "ERROR - Option without zero termination received!\r\n" )); - Status = EFI_INVALID_PARAMETER; - } - else { + DEBUG (( DEBUG_TX_PACKET, + "0x%08x: Packet transmiting\r\n", + pPacket )); + DEBUG (( DEBUG_TX, + "0x%08x: pContext sending 0x%08x bytes\r\n", + pContext, + pPacket->TxBytes )); + // - // Zero terminated option found + // Keep track of when the packet was transmitted // - pNextOption += 1; + if ( PcdGetBool ( Tftp_HighSpeed )) { + pPacket->TxTime = GetPerformanceCounter ( ); + } // - // Display the zero terminated ASCII option string + // Send the TFTP packet // - DEBUG (( DEBUG_TFTP_REQUEST, - "Option: %a\r\n", - pOption )); - Status = EFI_SUCCESS; + pContext->PacketsInWindow += 1; + LengthInBytes = sendto ( pContext->SocketFd, + &pPacket->TxBuffer[ 0 ], + pPacket->TxBytes, + 0, + (struct sockaddr *)&pContext->RemoteAddress, + pContext->RemoteAddress.sin6_len ); + if ( -1 == LengthInBytes ) { + DEBUG (( DEBUG_ERROR | DEBUG_TX, + "ERROR - Transmit failure, errno: 0x%08x\r\n", + errno )); + pContext->PacketsInWindow -= 1; + Status = EFI_DEVICE_ERROR; + } + } + else { + // + // Too many retries + // + Status = EFI_NO_RESPONSE; + DEBUG (( DEBUG_WARN | DEBUG_WINDOW, + "WARNING - No response from TFTP client\r\n" )); } - - // - // Return the next option address - // - *ppNextOption = pNextOption; // // Return the operation status // + DBG_EXIT_STATUS ( Status ); return Status; } /** - Place an option value into the option acknowledgement - - @param [in] pOack Option acknowledgement address - @param [in] Value Value to translate into ASCII decimal + Process the work for the sockets. - @return Option acknowledgement address + @param [in] pTftpServer Address of the ::TSDT_TFTP_SERVER structure + @param [in] pIndex Address of an index into the pollfd array **/ -UINT8 * -TftpOptionSet ( - IN UINT8 * pOack, - IN UINT64 Value +VOID +PortWork ( + IN TSDT_TFTP_SERVER * pTftpServer, + IN int * pIndex ) { - UINT64 NextValue; + int Index; + TSDT_CONNECTION_CONTEXT * pContext; + struct pollfd * pTftpPort; + socklen_t RemoteAddressLength; + int revents; - // - // Determine the next value - // - NextValue = Value / 10; + DBG_ENTER ( ); // - // Supress leading zeros + // Locate the port + // + Index = *pIndex; + if ( -1 != Index ) { + pTftpPort = &pTftpServer->TftpPort[ *pIndex ]; + + // + // Handle input events + // + revents = pTftpPort->revents; + pTftpPort->revents = 0; + if ( 0 != ( revents & POLLRDNORM )) { + // + // Receive the message from the remote system + // + RemoteAddressLength = sizeof ( pTftpServer->RemoteAddress ); + pTftpServer->RxBytes = recvfrom ( pTftpPort->fd, + &pTftpServer->RxBuffer[ 0 ], + sizeof ( pTftpServer->RxBuffer ), + 0, + (struct sockaddr *) &pTftpServer->RemoteAddress, + &RemoteAddressLength ); + if ( -1 != pTftpServer->RxBytes ) { + if ( PcdGetBool ( Tftp_HighSpeed )) { + pTftpServer->RxTime = GetPerformanceCounter ( ); + } + if ( AF_INET == pTftpServer->RemoteAddress.v4.sin_family ) { + DEBUG (( DEBUG_TFTP_PORT, + "Received %d bytes from %d.%d.%d.%d:%d\r\n", + pTftpServer->RxBytes, + pTftpServer->RemoteAddress.v4.sin_addr.s_addr & 0xff, + ( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 8 ) & 0xff, + ( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 16 ) & 0xff, + ( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 24 ) & 0xff, + htons ( pTftpServer->RemoteAddress.v4.sin_port ))); + } + else { + DEBUG (( DEBUG_TFTP_PORT, + "Received %d bytes from [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", + pTftpServer->RxBytes, + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pTftpServer->RemoteAddress.v6.sin6_port ))); + } + + // + // Lookup connection context using the remote system address and port + // to determine if an existing connection to this remote + // system exists + // + pContext = ContextFind ( pTftpServer ); + + // + // Process the received message + // + TftpProcessRequest ( pTftpServer, pContext, pTftpPort->fd ); + } + else { + // + // Receive error on the TFTP server port + // Close the server socket + // + DEBUG (( DEBUG_ERROR, + "ERROR - Failed receive on TFTP server port, errno: 0x%08x\r\n", + errno )); + revents |= POLLHUP; + } + } + + // + // Handle the close event + // + if ( 0 != ( revents & POLLHUP )) { + // + // Close the port + // + close ( pTftpPort->fd ); + pTftpPort->fd = -1; + *pIndex = -1; + pTftpServer->Entries -= 1; + ASSERT ( 0 <= pTftpServer->Entries ); + } + } + + DBG_EXIT ( ); +} + + +/** + Build and send an error packet + + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure + @param [in] Error Error number for the packet + @param [in] pError Zero terminated error string address + + @retval EFI_SUCCESS Message processed successfully + +**/ +EFI_STATUS +SendError ( + IN TSDT_CONNECTION_CONTEXT * pContext, + IN UINT16 Error, + IN UINT8 * pError + ) +{ + UINT8 Character; + UINT8 * pBuffer; + TFTP_PACKET * pPacket; + EFI_STATUS Status; + + DBG_ENTER ( ); + + // + // Build the error packet + // + pPacket = &pContext->ErrorPacket; + pBuffer = &pPacket->TxBuffer[ 0 ]; + pBuffer[ 0 ] = 0; + pBuffer[ 1 ] = TFTP_OP_ERROR; + pBuffer[ 2 ] = (UINT8)( Error >> 8 ); + pBuffer[ 3 ] = (UINT8)Error; + + // + // Copy the zero terminated string into the buffer + // + pBuffer += 4; + do { + Character = *pError++; + *pBuffer++ = Character; + } while ( 0 != Character ); + + // + // Send the error message + // + pPacket->TxBytes = pBuffer - &pPacket->TxBuffer[ 0 ]; + Status = PacketTx ( pContext, pPacket ); + + // + // Return the operation status + // + DBG_EXIT_STATUS ( Status ); + return Status; +} + + +/** + Scan the list of sockets and process any pending work + + @param [in] pTftpServer Address of the ::TSDT_TFTP_SERVER structure + +**/ +VOID +SocketPoll ( + IN TSDT_TFTP_SERVER * pTftpServer + ) +{ + int FDCount; + + DEBUG (( DEBUG_SOCKET_POLL, "Entering SocketPoll\r\n" )); + + // + // Determine if any ports are active + // + if ( 0 != pTftpServer->Entries ) { + FDCount = poll ( &pTftpServer->TftpPort[ 0 ], + pTftpServer->Entries, + CLIENT_POLL_DELAY ); + if ( 0 < FDCount ) { + // + // Process this port + // + PortWork ( pTftpServer, &pTftpServer->Udpv4Index ); + PortWork ( pTftpServer, &pTftpServer->Udpv6Index ); + } + } + + DEBUG (( DEBUG_SOCKET_POLL, "Exiting SocketPoll\r\n" )); +} + + +/** + Process the ACK + + @param [in] pTftpServer Address of the ::TSDT_TFTP_SERVER structure + @param [in] pContext Connection context structure address + + @retval TRUE if the context should be closed + +**/ +BOOLEAN +TftpAck ( + IN TSDT_TFTP_SERVER * pTftpServer, + IN TSDT_CONNECTION_CONTEXT * pContext + ) +{ + INTN AckNumber; + BOOLEAN bCloseContext; + UINT16 BlockNumber; + UINT8 * pBuffer; + TFTP_PACKET * pPacket; + EFI_STATUS Status; + + DBG_ENTER ( ); + + // + // Use break instead of goto + // + bCloseContext = FALSE; + for ( ; ; ) { + // + // Validate the parameters + // + if ( NULL == pContext ) { + if ( AF_INET == pTftpServer->RemoteAddress.v4.sin_family ) { + DEBUG (( DEBUG_ERROR, + "ERROR - File not open for %d.%d.%d.%d:%d\r\n", + (UINT8)pTftpServer->RemoteAddress.v4.sin_addr.s_addr, + (UINT8)( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 8 ), + (UINT8)( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 16 ), + (UINT8)( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 24 ), + htons ( pTftpServer->RemoteAddress.v4.sin_port ))); + } + else { + DEBUG (( DEBUG_ERROR, + "ERROR - File not open for [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pTftpServer->RemoteAddress.v6.sin6_port ))); + } + break; + } + + // + // Verify that the ACK was expected + // + pPacket = pContext->pTxHead; + if ( NULL == pPacket ) { + // + // ACK not expected! + // + DEBUG (( DEBUG_ERROR, + "ERROR - Expecting data not ACKs for pContext 0x%08x\r\n", + pContext )); + break; + } + + // + // Get the ACKed block number + // + pBuffer = &pTftpServer->RxBuffer[ 0 ]; + BlockNumber = HTONS ( *(UINT16 *)&pBuffer[ 2 ]); + + // + // Determine if this is the correct ACK + // + DEBUG (( DEBUG_TFTP_ACK, + "ACK for block 0x%04x received\r\n", + BlockNumber )); + AckNumber = BlockNumber - pPacket->BlockNumber; + if (( 0 > AckNumber ) || ( AckNumber >= (INTN)pContext->PacketsInWindow )){ + DEBUG (( DEBUG_WARN | DEBUG_TFTP_ACK, + "WARNING - Expecting ACK 0x%0x4 not received ACK 0x%08x\r\n", + pPacket->BlockNumber, + BlockNumber )); + break; + } + + // + // Release the ACKed packets + // + do { + // + // Remove the packet from the transmit list and window + // + pPacket = PacketRemove ( pContext ); + + // + // Get the block number of this packet + // + AckNumber = pPacket->BlockNumber; + + // + // Increase the size of the transmit window + // + if ( PcdGetBool ( Tftp_HighSpeed ) + && ( AckNumber == BlockNumber )) { + WindowAck ( pTftpServer, pContext, pPacket ); + } + + // + // Free this packet + // + PacketFree ( pContext, pPacket ); + } while (( NULL != pContext->pTxHead ) && ( AckNumber != BlockNumber )); + + // + // Fill the window with packets + // + pPacket = pContext->pTxHead; + while (( NULL != pPacket ) + && ( pContext->PacketsInWindow < pContext->WindowSize ) + && ( !bCloseContext )) { + Status = PacketTx ( pContext, pPacket ); + bCloseContext = (BOOLEAN)( EFI_ERROR ( Status )); + pPacket = pPacket->pNext; + } + + // + // Get more packets ready for transmission + // + PacketFill ( pContext ); + + // + // Close the context when the last packet is ACKed + // + if ( 0 == pContext->PacketsInWindow ) { + bCloseContext = TRUE; + + // + // Display the bandwidth + // + if ( PcdGetBool ( Tftp_Bandwidth )) { + UINT64 Bandwidth; + UINT64 DeltaTime; + UINT64 NanoSeconds; + UINT32 Value; + + // + // Compute the download time + // + DeltaTime = GetPerformanceCounter ( ); + if ( pTftpServer->Time2 > pTftpServer->Time1 ) { + DeltaTime = DeltaTime - pContext->TimeStart; + } + else { + DeltaTime = pContext->TimeStart - DeltaTime; + } + NanoSeconds = GetTimeInNanoSecond ( DeltaTime ); + Bandwidth = pContext->LengthInBytes; + DEBUG (( DEBUG_WINDOW, + "File Length %Ld, Transfer Time: %d.%03d Sec\r\n", + Bandwidth, + DivU64x32 ( NanoSeconds, 1000 * 1000 * 1000 ), + ((UINT32)DivU64x32 ( NanoSeconds, 1000 * 1000 )) % 1000 )); + + // + // Display the round trip time + // + Bandwidth = MultU64x32 ( Bandwidth, 8 * 1000 * 1000 ); + Bandwidth /= NanoSeconds; + if ( 1000 > Bandwidth ) { + Value = (UINT32)Bandwidth; + Print ( L"Bandwidth: %d Kbits/Sec\r\n", + Value ); + } + else if (( 1000 * 1000 ) > Bandwidth ) { + Value = (UINT32)Bandwidth; + Print ( L"Bandwidth: %d.%03d Mbits/Sec\r\n", + Value / 1000, + Value % 1000 ); + } + else { + Value = (UINT32)DivU64x32 ( Bandwidth, 1000 ); + Print ( L"Bandwidth: %d.%03d Gbits/Sec\r\n", + Value / 1000, + Value % 1000 ); + } + } + } + break; + } + + // + // Return the operation status + // + DBG_EXIT ( ); + return bCloseContext; +} + + +/** + Get the next TFTP option + + @param [in] pOption Address of a zero terminated option string + @param [in] pEnd End of buffer address + @param [in] ppNextOption Address to receive the address of the next + zero terminated option string + + @retval EFI_SUCCESS Message processed successfully + +**/ +EFI_STATUS +TftpOptionGet ( + IN UINT8 * pOption, + IN UINT8 * pEnd, + IN UINT8 ** ppNextOption + ) +{ + UINT8 * pNextOption; + EFI_STATUS Status; + + // + // Locate the end of the option + // + pNextOption = pOption; + while (( pEnd > pNextOption ) && ( 0 != *pNextOption )) { + pNextOption += 1; + } + if ( pEnd <= pNextOption ) { + // + // Error - end of buffer reached + // + DEBUG (( DEBUG_ERROR | DEBUG_TFTP_REQUEST, + "ERROR - Option without zero termination received!\r\n" )); + Status = EFI_INVALID_PARAMETER; + } + else { + // + // Zero terminated option found + // + pNextOption += 1; + + // + // Display the zero terminated ASCII option string + // + DEBUG (( DEBUG_TFTP_REQUEST, + "Option: %a\r\n", + pOption )); + Status = EFI_SUCCESS; + } + + // + // Return the next option address + // + *ppNextOption = pNextOption; + + // + // Return the operation status + // + return Status; +} + + +/** + Place an option value into the option acknowledgement + + @param [in] pOack Option acknowledgement address + @param [in] Value Value to translate into ASCII decimal + + @return Option acknowledgement address + +**/ +UINT8 * +TftpOptionSet ( + IN UINT8 * pOack, + IN UINT64 Value + ) +{ + UINT64 NextValue; + + // + // Determine the next value + // + NextValue = Value / 10; + + // + // Supress leading zeros // if ( 0 != NextValue ) { pOack = TftpOptionSet ( pOack, NextValue ); @@ -539,7 +1245,7 @@ TftpOptionSet ( /** Process the TFTP request - @param [in] pContext The context structure address. + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure @param [in] pOption Address of the first zero terminated option string @param [in] pEnd End of buffer address @@ -553,20 +1259,27 @@ TftpOptions ( { UINT8 * pNextOption; UINT8 * pOack; + TFTP_PACKET * pPacket; UINT8 * pTemp; UINT8 * pValue; EFI_STATUS Status; INT32 Value; + // + // Get a packet + // + pPacket = PacketGet ( pContext ); + // // Start the OACK packet // Let the OACK handle the parsing errors // See http://tools.ietf.org/html/rfc2347 // - pOack = &pContext->TxBuffer[0]; + pOack = &pPacket->TxBuffer[ 0 ]; *pOack++ = 0; *pOack++ = TFTP_OP_OACK; - pContext->TxBytes = 2; + pPacket->TxBytes = 2; + pPacket->BlockNumber = 0; // // Walk the list of options @@ -585,7 +1298,7 @@ TftpOptions ( // blksize - See http://tools.ietf.org/html/rfc2348 // pValue = pNextOption; - if ( 0 == stricmp ((char *)pOption, "blksize" )) { + if ( 0 == strcasecmp ((char *)pOption, "blksize" )) { // // Get the value // @@ -625,7 +1338,7 @@ TftpOptions ( *pOack++ = 0; pOack = TftpOptionSet ( pOack, pContext->BlockSize ); *pOack++ = 0; - pContext->TxBytes += pOack - pTemp; + pPacket->TxBytes += pOack - pTemp; } } } @@ -633,7 +1346,7 @@ TftpOptions ( // // timeout - See http://tools.ietf.org/html/rfc2349 // - else if ( 0 == stricmp ((char *)pOption, "timeout" )) { + else if ( 0 == strcasecmp ((char *)pOption, "timeout" )) { // // Get the value // @@ -644,10 +1357,10 @@ TftpOptions ( // // Set the timeout value // - pContext->Timeout = Value; + pContext->MaxTimeout = Value; DEBUG (( DEBUG_TFTP_REQUEST, "Using timeout of %d seconds\r\n", - pContext->Timeout )); + pContext->MaxTimeout )); // // Update the OACK @@ -661,9 +1374,9 @@ TftpOptions ( *pOack++ = 'u'; *pOack++ = 't'; *pOack++ = 0; - pOack = TftpOptionSet ( pOack, pContext->Timeout ); + pOack = TftpOptionSet ( pOack, pContext->MaxTimeout ); *pOack++ = 0; - pContext->TxBytes += pOack - pTemp; + pPacket->TxBytes += pOack - pTemp; } } } @@ -671,7 +1384,7 @@ TftpOptions ( // // tsize - See http://tools.ietf.org/html/rfc2349 // - else if ( 0 == stricmp ((char *)pOption, "tsize" )) { + else if ( 0 == strcasecmp ((char *)pOption, "tsize" )) { // // Get the value // @@ -698,7 +1411,7 @@ TftpOptions ( *pOack++ = 0; pOack = TftpOptionSet ( pOack, pContext->LengthInBytes ); *pOack++ = 0; - pContext->TxBytes += pOack - pTemp; + pPacket->TxBytes += pOack - pTemp; } } } @@ -717,6 +1430,16 @@ TftpOptions ( // pOption = pNextOption; } while ( pEnd > pOption ); + + // + // Transmit the OACK if necessary + // + if ( 2 < pPacket->TxBytes ) { + PacketQueue ( pContext, pPacket ); + } + else { + PacketFree ( pContext, pPacket ); + } } @@ -781,69 +1504,91 @@ TftpOptionValue ( /** Process the TFTP request - @param [in] pTftpServer The TFTP server control structure address. - @param [in] pContext Connection context structure address + @param [in] pTftpServer Address of the ::TSDT_TFTP_SERVER structure + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure + @param [in] SocketFd Socket file descriptor **/ VOID TftpProcessRequest ( IN TSDT_TFTP_SERVER * pTftpServer, - IN TSDT_CONNECTION_CONTEXT * pContext + IN TSDT_CONNECTION_CONTEXT * pContext, + IN int SocketFd ) { BOOLEAN bCloseContext; - BOOLEAN bIgnorePacket; - UINT16 BlockNumber; UINT16 Opcode; - UINT8 * pBuffer; - UINT8 * pEnd; - UINT8 * pFileName; - UINT8 * pMode; - UINT8 * pOption; - EFI_STATUS Status; DBG_ENTER ( ); // // Get the opcode // - pBuffer = &pTftpServer->RxBuffer[0]; - Opcode = HTONS ( *(UINT16 *)&pBuffer[0]); -Print ( L"TFTP Opcode: 0x%08x\r\n", Opcode ); + Opcode = HTONS ( *(UINT16 *)&pTftpServer->RxBuffer[ 0 ]); + DEBUG (( DEBUG_TFTP_REQUEST, + "TFTP Opcode: 0x%08x\r\n", + Opcode )); // // Validate the parameters // bCloseContext = FALSE; - bIgnorePacket = FALSE; switch ( Opcode ) { default: DEBUG (( DEBUG_TFTP_REQUEST, "ERROR - Unknown TFTP opcode: %d\r\n", Opcode )); - bIgnorePacket = TRUE; + break; + + case TFTP_OP_ACK: + bCloseContext = TftpAck ( pTftpServer, pContext ); break; case TFTP_OP_READ_REQUEST: + bCloseContext = TftpRead ( pTftpServer, pContext, SocketFd ); break; + + + case TFTP_OP_DATA: if ( NULL == pContext ) { - DEBUG (( DEBUG_ERROR, - "ERROR - File not open for %d.%d.%d.%d:%d\r\n", - (UINT8)pTftpServer->RemoteAddress.sin_addr.s_addr, - (UINT8)( pTftpServer->RemoteAddress.sin_addr.s_addr >> 8 ), - (UINT8)( pTftpServer->RemoteAddress.sin_addr.s_addr >> 16 ), - (UINT8)( pTftpServer->RemoteAddress.sin_addr.s_addr >> 24 ), - htons ( pTftpServer->RemoteAddress.sin_port ))); - bIgnorePacket = TRUE; + if ( AF_INET == pTftpServer->RemoteAddress.v4.sin_family ) { + DEBUG (( DEBUG_ERROR, + "ERROR - File not open for %d.%d.%d.%d:%d\r\n", + (UINT8)pTftpServer->RemoteAddress.v4.sin_addr.s_addr, + (UINT8)( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 8 ), + (UINT8)( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 16 ), + (UINT8)( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 24 ), + htons ( pTftpServer->RemoteAddress.v4.sin_port ))); + } + else { + DEBUG (( DEBUG_ERROR, + "ERROR - File not open for [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pTftpServer->RemoteAddress.v6.sin6_port ))); + } break; } - if ( pContext->bExpectAck ) { + if ( 0 != pContext->PacketsInWindow ) { DEBUG (( DEBUG_ERROR, "ERROR - Expecting ACKs not data for pContext 0x%08x\r\n", pContext )); - bIgnorePacket = TRUE; break; } if ( pTftpServer->RxBytes > (ssize_t)( pContext->BlockSize + 2 + 2 )) { @@ -852,229 +1597,44 @@ Print ( L"TFTP Opcode: 0x%08x\r\n", Opcode ); pTftpServer->RxBytes - 2 - 2, pContext->BlockSize, pContext )); - bIgnorePacket = TRUE; - break; - } - break; - - case TFTP_OP_ACK: - if ( NULL == pContext ) { - DEBUG (( DEBUG_ERROR, - "ERROR - File not open for %d.%d.%d.%d:%d\r\n", - (UINT8)pTftpServer->RemoteAddress.sin_addr.s_addr, - (UINT8)( pTftpServer->RemoteAddress.sin_addr.s_addr >> 8 ), - (UINT8)( pTftpServer->RemoteAddress.sin_addr.s_addr >> 16 ), - (UINT8)( pTftpServer->RemoteAddress.sin_addr.s_addr >> 24 ), - htons ( pTftpServer->RemoteAddress.sin_port ))); - bIgnorePacket = TRUE; - } - if ( !pContext->bExpectAck ) { - DEBUG (( DEBUG_ERROR, - "ERROR - Expecting data not ACKs for pContext 0x%08x\r\n", - pContext )); - bIgnorePacket = TRUE; break; } break; case TFTP_OP_ERROR: if ( NULL == pContext ) { - DEBUG (( DEBUG_ERROR, - "ERROR - File not open for %d.%d.%d.%d:%d\r\n", - (UINT8)pTftpServer->RemoteAddress.sin_addr.s_addr, - (UINT8)( pTftpServer->RemoteAddress.sin_addr.s_addr >> 8 ), - (UINT8)( pTftpServer->RemoteAddress.sin_addr.s_addr >> 16 ), - (UINT8)( pTftpServer->RemoteAddress.sin_addr.s_addr >> 24 ), - htons ( pTftpServer->RemoteAddress.sin_port ))); - bIgnorePacket = TRUE; - } - break; - } - if ( !bIgnorePacket ) { - // - // Process the request - // - switch ( Opcode ) { - default: - DEBUG (( DEBUG_TFTP_REQUEST, - "ERROR - Unable to process TFTP opcode: %d\r\n", - Opcode )); - break; - - case TFTP_OP_READ_REQUEST: - - // - // Close the context if necessary - // - if ( NULL != pContext ) { - ContextRemove ( pTftpServer, pContext ); - } - - // - // Create the connection context - // - pContext = ContextAdd ( pTftpServer ); - if ( NULL == pContext ) { - break; - } - - // - // Locate the mode - // - pFileName = &pBuffer[2]; - pEnd = &pBuffer[pTftpServer->RxBytes]; - pMode = pFileName; - while (( pEnd > pMode ) && ( 0 != *pMode )) { - pMode += 1; - } - if ( pEnd <= pMode ) { - // - // Mode not found - // - DEBUG (( DEBUG_ERROR | DEBUG_RX, - "ERROR - File mode not found\r\n" )); - // - // Tell the client of the error - // - TftpSendError ( pTftpServer, - pContext, - 0, - (UINT8 *)"File open mode not found" ); - break; - } - pMode += 1; - DEBUG (( DEBUG_TFTP_REQUEST, - "TFTP - FileName: %a\n", - pFileName )); - - // - // Locate the options - // - pOption = pMode; - while (( pEnd > pOption ) && ( 0 != *pOption )) { - pOption += 1; - } - if ( pEnd <= pOption ) { - // - // End of mode not found - // - DEBUG (( DEBUG_ERROR | DEBUG_RX, - "ERROR - File mode not valid\r\n" )); - // - // Tell the client of the error - // - TftpSendError ( pTftpServer, - pContext, - 0, - (UINT8 *)"File open mode not valid" ); - break; - } - pOption += 1; - DEBUG (( DEBUG_TFTP_REQUEST, - "TFTP - Mode: %a\r\n", - pMode )); - - // - // Verify the mode is supported - // - if ( 0 != stricmp ((char *)pMode, "octet" )) { - // - // File access mode not supported - // - DEBUG (( DEBUG_ERROR | DEBUG_TFTP_REQUEST, - "ERROR - File mode %a not supported\r\n", - pMode )); - - // - // Tell the client of the error - // - TftpSendError ( pTftpServer, - pContext, - 0, - (UINT8 *)"File open mode not supported" ); - break; - } - - // - // Open the file, close the context on error - // -// TODO: Remove the following line -pContext->File = (EFI_HANDLE)1; - - // - // Determine the file length - // -//fstat - - // - // Process the options - // - TftpOptions ( pContext, pOption, pEnd ); - - // - // Read in the first portion of the file - // - - // - // Send the first block - // - pContext->bExpectAck = TRUE; - if ( 2 < pContext->TxBytes ) { - // - // Send the OACK - // - Status = TftpTxPacket ( pTftpServer, pContext ); + if ( AF_INET == pTftpServer->RemoteAddress.v4.sin_family ) { + DEBUG (( DEBUG_ERROR, + "ERROR - File not open for %d.%d.%d.%d:%d\r\n", + (UINT8)pTftpServer->RemoteAddress.v4.sin_addr.s_addr, + (UINT8)( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 8 ), + (UINT8)( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 16 ), + (UINT8)( pTftpServer->RemoteAddress.v4.sin_addr.s_addr >> 24 ), + htons ( pTftpServer->RemoteAddress.v4.sin_port ))); } else { - // - // Send the first block of data - // - Status = TftpSendNextBlock ( pTftpServer, pContext ); - } - break; - - case TFTP_OP_ACK: - // - // Get the block number that is being ACKed - // - BlockNumber = pTftpServer->RxBuffer[2]; - BlockNumber <<= 8; - BlockNumber |= pTftpServer->RxBuffer[3]; - - // - // Determine if this is the correct ACK - // - DEBUG (( DEBUG_TFTP_ACK, - "ACK for block 0x%04x received\r\n", - BlockNumber )); - if (( !pContext->bExpectAck ) - || ( BlockNumber != pContext->AckNext )) { - DEBUG (( DEBUG_WARN | DEBUG_TFTP_ACK, - "WARNING - Expecting ACK 0x%0x4 not received ACK 0x%08x\r\n", - pContext->AckNext, - BlockNumber )); + DEBUG (( DEBUG_ERROR, + "ERROR - File not open for [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 0 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 1 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 2 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 3 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 4 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 5 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 6 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 7 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 8 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 9 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 10 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 11 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 12 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 13 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 14 ], + pTftpServer->RemoteAddress.v6.sin6_addr.__u6_addr.__u6_addr8[ 15 ], + htons ( pTftpServer->RemoteAddress.v6.sin6_port ))); } - else { - // - // Process the expected ACK - // - if ( pContext->bEofSent ) { - bCloseContext = TRUE; - } - else { - // - // Set the next expected ACK - // - pContext->AckNext += 1; - - // - // Send the next packet of data - // - Status = TftpSendNextBlock ( pTftpServer, pContext ); - } - } - break; } + break; } // @@ -1089,119 +1649,229 @@ pContext->File = (EFI_HANDLE)1; /** - Build and send an error packet + Process the read request - @param [in] pTftpServer The TFTP server control structure address. - @param [in] pContext The context structure address. - @param [in] Error Error number for the packet - @param [in] pError Zero terminated error string address + @param [in] pTftpServer Address of the ::TSDT_TFTP_SERVER structure + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure + @param [in] SocketFd Socket file descriptor - @retval EFI_SUCCESS Message processed successfully + @retval TRUE if the context should be closed **/ -EFI_STATUS -TftpSendError ( +BOOLEAN +TftpRead ( IN TSDT_TFTP_SERVER * pTftpServer, IN TSDT_CONNECTION_CONTEXT * pContext, - IN UINT16 Error, - IN UINT8 * pError + IN int SocketFd ) { - UINT8 Character; + BOOLEAN bCloseContext; + struct stat FileStatus; UINT8 * pBuffer; - EFI_STATUS Status; + UINT8 * pEnd; + UINT8 * pFileName; + UINT8 * pMode; + UINT8 * pOption; + CHAR8 * pReadMode; + UINT64 TimeStart; DBG_ENTER ( ); // - // Build the error packet + // Log the receive time // - pBuffer = &pContext->TxBuffer[0]; - pBuffer[0] = 0; - pBuffer[1] = TFTP_OP_ERROR; - pBuffer[2] = (UINT8)( Error >> 8 ); - pBuffer[3] = (UINT8)Error; + TimeStart = 0; + if ( PcdGetBool ( Tftp_Bandwidth )) { + TimeStart = GetPerformanceCounter ( ); + } // - // Copy the zero terminated string into the buffer + // Close the context if necessary // - pBuffer += 4; - do { - Character = *pError++; - *pBuffer++ = Character; - } while ( 0 != Character ); + bCloseContext = FALSE; + if ( NULL != pContext ) { + ContextRemove ( pTftpServer, pContext ); + } // - // Send the error message + // Use break instead of goto // - pContext->TxBytes = pBuffer - &pContext->TxBuffer[0]; - Status = TftpTxPacket ( pTftpServer, pContext ); + for ( ; ; ) { + // + // Create the connection context + // + pContext = ContextAdd ( pTftpServer, SocketFd ); + if ( NULL == pContext ) { + break; + } - // - // Return the operation status - // - DBG_EXIT_STATUS ( Status ); - return Status; -} + // + // Set the start time + // + if ( PcdGetBool ( Tftp_Bandwidth )) { + pContext->TimeStart = TimeStart; + } + // + // Locate the mode + // + pBuffer = &pTftpServer->RxBuffer[ 0 ]; + pEnd = &pBuffer[ pTftpServer->RxBytes ]; + pFileName = &pBuffer[ 2 ]; + pMode = pFileName; + while (( pEnd > pMode ) && ( 0 != *pMode )) { + pMode += 1; + } + if ( pEnd <= pMode ) { + // + // Mode not found + // + DEBUG (( DEBUG_ERROR | DEBUG_RX, + "ERROR - File mode not found\r\n" )); + // + // Tell the client of the error + // + SendError ( pContext, + TFTP_ERROR_SEE_MSG, + (UINT8 *)"File open mode not found" ); + break; + } + pMode += 1; + DEBUG (( DEBUG_TFTP_REQUEST, + "TFTP - FileName: %a\r\n", + pFileName )); -/** - Send the next block of file system data + // + // Locate the options + // + pOption = pMode; + while (( pEnd > pOption ) && ( 0 != *pOption )) { + pOption += 1; + } + if ( pEnd <= pOption ) { + // + // End of mode not found + // + DEBUG (( DEBUG_ERROR | DEBUG_RX, + "ERROR - File mode not valid\r\n" )); + // + // Tell the client of the error + // + SendError ( pContext, + TFTP_ERROR_SEE_MSG, + (UINT8 *)"File open mode not valid" ); + break; + } + pOption += 1; + DEBUG (( DEBUG_TFTP_REQUEST, + "TFTP - Mode: %a\r\n", + pMode )); - @param [in] pTftpServer The TFTP server control structure address. - @param [in] pContext The context structure address. + // + // Verify the mode is supported + // + pReadMode = "r"; + if ( 0 == strcasecmp ((char *)pMode, "octet" )) { + // + // Read the file as binary input + // + pReadMode = "rb"; + } - @retval EFI_SUCCESS Message processed successfully + // + // Determine the file length + // + pContext->File = fopen ( pFileName, pReadMode ); + if (( NULL == pContext->File ) + || ( -1 == stat ( pFileName, &FileStatus ))) { + // + // File not found + // + DEBUG (( DEBUG_ERROR | DEBUG_TFTP_REQUEST, + ( NULL == pContext->File ) + ? "ERROR - File not found!\r\n" + : "ERROR - Unable to determine file %a size!\r\n", + pFileName )); -**/ -EFI_STATUS -TftpSendNextBlock ( - IN TSDT_TFTP_SERVER * pTftpServer, - IN TSDT_CONNECTION_CONTEXT * pContext - ) -{ - ssize_t LengthInBytes; - UINT8 * pBuffer; - EFI_STATUS Status; + // + // Tell the client of the error + // + SendError ( pContext, + TFTP_ERROR_NOT_FOUND, + (UINT8 *)"File not found" ); + break; + } + pContext->LengthInBytes = FileStatus.st_size; + pContext->BytesRemaining = pContext->LengthInBytes; + pContext->BytesToSend = pContext->LengthInBytes; - // - // Determine how much data needs to be sent - // - LengthInBytes = pContext->BlockSize; - if (( pContext->LengthInBytes < TFTP_MAX_BLOCK_SIZE ) - || ( LengthInBytes > (ssize_t)pContext->LengthInBytes )) { - LengthInBytes = (ssize_t)pContext->LengthInBytes; - pContext->bEofSent = TRUE; - } + // + // Display the file size + // + DEBUG_CODE_BEGIN ( ); + UINT32 Value; + + if ( 1024 > pContext->LengthInBytes ) { + Value = (UINT32)pContext->LengthInBytes; + DEBUG (( DEBUG_FILE_BUFFER, + "%a size: %d Bytes\r\n", + pFileName, + Value )); + } + else if (( 1024 * 1024 ) > pContext->LengthInBytes ) { + Value = (UINT32)pContext->LengthInBytes; + DEBUG (( DEBUG_FILE_BUFFER, + "%a size: %d.%03d KiBytes (%Ld Bytes)\r\n", + pFileName, + Value / 1024, + (( Value % 1024 ) * 1000 ) / 1024, + pContext->LengthInBytes )); + } + else if (( 1024 * 1024 * 1024 ) > pContext->LengthInBytes ) { + Value = (UINT32)DivU64x32 ( pContext->LengthInBytes, 1024 ); + DEBUG (( DEBUG_FILE_BUFFER, + "%a size: %d.%03d MiBytes (%Ld Bytes)\r\n", + pFileName, + Value / 1024, + (( Value % 1024 ) * 1000 ) / 1024, + pContext->LengthInBytes )); + } + else { + Value = (UINT32)DivU64x32 ( pContext->LengthInBytes, 1024 * 1024 ); + DEBUG (( DEBUG_FILE_BUFFER, + "%a size: %d.%03d GiBytes (%Ld Bytes)\r\n", + pFileName, + Value / 1024, + (( Value % 1024 ) * 1000 ) / 1024, + pContext->LengthInBytes )); + } + DEBUG_CODE_END ( ); - // - // Set the TFTP opcode and block number - // - pBuffer = &pContext->TxBuffer[0]; - *pBuffer++ = 0; - *pBuffer++ = TFTP_OP_DATA; - *pBuffer++ = (UINT8)( pContext->AckNext >> 8 ); - *pBuffer++ = (UINT8)pContext->AckNext; + // + // Process the options + // + if ( pEnd > pOption ) { + TftpOptions ( pContext, pOption, pEnd ); + } + else { + // + // Skip the open ACK + // + pContext->BlockNumber = 1; + } - // - // Copy the file data into the transmit buffer - // - pContext->TxBytes = 2 + 2 + LengthInBytes; - if ( 0 < LengthInBytes ) { - CopyMem ( &pBuffer, - pContext->pBuffer, - LengthInBytes ); + // + // Send the first packet (OACK or data block) + // + bCloseContext = PacketFill ( pContext ); + break; } // - // Send the next block - // - Status = TftpTxPacket ( pTftpServer, pContext ); - - // - // Return the operation status + // Return the close status // - return Status; + DBG_EXIT ( ); + return bCloseContext; } @@ -1213,241 +1883,314 @@ TftpSendNextBlock ( some time to get the IP address and initialize the upper layers of the network stack. - @param [in] pTftpServer The TFTP server control structure address. + @param [in] pTftpServer Address of the ::TSDT_TFTP_SERVER structure + @param [in] AddressFamily The address family to use for the conection. + @param [in] pIndex Address of the index into the port array **/ VOID -TftpServerTimer ( - IN TSDT_TFTP_SERVER * pTftpServer +TftpServerSocket ( + IN TSDT_TFTP_SERVER * pTftpServer, + IN sa_family_t AddressFamily, + IN int * pIndex ) { - UINT16 TftpPort; int SocketStatus; - EFI_STATUS Status; + struct pollfd * pTftpPort; + UINT16 TftpPort; + union { + struct sockaddr_in v4; + struct sockaddr_in6 v6; + } TftpServerAddress; - DEBUG (( DEBUG_SERVER_TIMER, "Entering TftpServerTimer\r\n" )); + DEBUG (( DEBUG_SERVER_TIMER, "Entering TftpServerListen\r\n" )); // - // Open the TFTP port on the server + // Determine if the socket is already initialized // - do { - do { - // - // Wait for a while - // - Status = gBS->CheckEvent ( pTftpServer->TimerEvent ); - } while ( EFI_SUCCESS != Status ); - + if ( -1 == *pIndex ) { // // Attempt to create the socket for the TFTP server // - pTftpServer->TftpPort.events = POLLRDNORM | POLLHUP; - pTftpServer->TftpPort.revents = 0; - pTftpServer->TftpPort.fd = socket ( AF_INET, - SOCK_DGRAM, - IPPROTO_UDP ); - if ( -1 != pTftpServer->TftpPort.fd ) { + pTftpPort = &pTftpServer->TftpPort[ pTftpServer->Entries ]; + pTftpPort->fd = socket ( AddressFamily, + SOCK_DGRAM, + IPPROTO_UDP ); + if ( -1 != pTftpPort->fd ) { + // + // Initialize the poll structure + // + pTftpPort->events = POLLRDNORM | POLLHUP; + pTftpPort->revents = 0; + // // Set the socket address // - ZeroMem ( &pTftpServer->TftpServerAddress, - sizeof ( pTftpServer->TftpServerAddress )); TftpPort = 69; - DEBUG (( DEBUG_TFTP_PORT, - "TFTP Port: %d\r\n", - TftpPort )); - pTftpServer->TftpServerAddress.sin_len = sizeof ( pTftpServer->TftpServerAddress ); - pTftpServer->TftpServerAddress.sin_family = AF_INET; - pTftpServer->TftpServerAddress.sin_addr.s_addr = INADDR_ANY; - pTftpServer->TftpServerAddress.sin_port = htons ( TftpPort ); + ZeroMem ( &TftpServerAddress, sizeof ( TftpServerAddress )); + TftpServerAddress.v4.sin_port = htons ( TftpPort ); + if ( AF_INET == AddressFamily ) { + TftpServerAddress.v4.sin_len = sizeof ( TftpServerAddress.v4 ); + TftpServerAddress.v4.sin_family = AF_INET; + } + else { + TftpServerAddress.v6.sin6_len = sizeof ( TftpServerAddress.v6 ); + TftpServerAddress.v6.sin6_family = AF_INET6; + } // // Bind the socket to the TFTP port // - SocketStatus = bind ( pTftpServer->TftpPort.fd, - (struct sockaddr *) &pTftpServer->TftpServerAddress, - pTftpServer->TftpServerAddress.sin_len ); + SocketStatus = bind ( pTftpPort->fd, + (struct sockaddr *) &TftpServerAddress, + TftpServerAddress.v6.sin6_len ); if ( -1 != SocketStatus ) { DEBUG (( DEBUG_TFTP_PORT, "0x%08x: Socket bound to port %d\r\n", - pTftpServer->TftpPort.fd, + pTftpPort->fd, TftpPort )); + + // + // Account for this connection + // + *pIndex = pTftpServer->Entries; + pTftpServer->Entries += 1; + ASSERT ( DIM ( pTftpServer->TftpPort ) >= pTftpServer->Entries ); } // // Release the socket if necessary // if ( -1 == SocketStatus ) { - close ( pTftpServer->TftpPort.fd ); - pTftpServer->TftpPort.fd = -1; + close ( pTftpPort->fd ); + pTftpPort->fd = -1; } } + } - // - // Wait until the socket is open - // - }while ( -1 == pTftpServer->TftpPort.fd ); - - DEBUG (( DEBUG_SERVER_TIMER, "Exiting TftpServerTimer\r\n" )); + DEBUG (( DEBUG_SERVER_TIMER, "Exiting TftpServerListen\r\n" )); } /** - Start the TFTP server port creation timer + Update the window due to the ACK - @param [in] pTftpServer The TFTP server control structure address. - - @retval EFI_SUCCESS The timer was successfully started. - @retval EFI_ALREADY_STARTED The timer is already running. - @retval Other The timer failed to start. + @param [in] pTftpServer Address of the ::TSDT_TFTP_SERVER structure + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure + @param [in] pPacket Address of a ::TFTP_PACKET structure **/ -EFI_STATUS -TftpServerTimerStart ( - IN TSDT_TFTP_SERVER * pTftpServer +VOID +WindowAck ( + IN TSDT_TFTP_SERVER * pTftpServer, + IN TSDT_CONNECTION_CONTEXT * pContext, + IN TFTP_PACKET * pPacket ) { - EFI_STATUS Status; - UINT64 TriggerTime; + if ( PcdGetBool ( Tftp_HighSpeed )) { + UINT64 DeltaTime; + UINT64 NanoSeconds; - DBG_ENTER ( ); + DBG_ENTER ( ); - // - // Assume the timer is already running - // - Status = EFI_ALREADY_STARTED; - if ( !pTftpServer->bTimerRunning ) { // - // Compute the poll interval + // Compute the round trip time // - TriggerTime = TFTP_PORT_POLL_DELAY * ( 1000 * 10 ); - Status = gBS->SetTimer ( pTftpServer->TimerEvent, - TimerPeriodic, - TriggerTime ); - if ( !EFI_ERROR ( Status )) { - DEBUG (( DEBUG_TFTP_PORT, "TFTP port timer started\r\n" )); - - // - // Mark the timer running - // - pTftpServer->bTimerRunning = TRUE; + if ( pTftpServer->Time2 > pTftpServer->Time1 ) { + DeltaTime = pTftpServer->RxTime - pPacket->TxTime; } else { - DEBUG (( DEBUG_ERROR | DEBUG_TFTP_PORT, - "ERROR - Failed to start TFTP port timer, Status: %r\r\n", - Status )); + DeltaTime = pPacket->TxTime - pTftpServer->RxTime; } - } - // - // Return the operation status - // - DBG_EXIT_STATUS ( Status ); - return Status; + // + // Adjust the round trip time + // + NanoSeconds = GetTimeInNanoSecond ( DeltaTime ); + DeltaTime = RShiftU64 ( pContext->Rtt2x, ACK_SHIFT ); + pContext->Rtt2x += NanoSeconds + NanoSeconds - DeltaTime; + if ( pContext->Rtt2x > pContext->MaxTimeout ) { + pContext->Rtt2x = pContext->MaxTimeout; + } + + // + // Account for the ACK + // + if ( pContext->WindowSize < MAX_PACKETS ) { + pContext->AckCount -= 1; + if ( 0 == pContext->AckCount ) { + // + // Increase the window + // + pContext->WindowSize += 1; + + // + // Set the ACK count + // + if ( pContext->WindowSize < pContext->Threshold ) { + pContext->AckCount = pContext->WindowSize * PcdGet32 ( Tftp_AckMultiplier ); + } + else { + pContext->AckCount = PcdGet32 ( Tftp_AckLogBase ) << pContext->WindowSize; + } + + // + // Display the round trip time + // + DEBUG_CODE_BEGIN ( ); + UINT32 Value; + + DeltaTime = RShiftU64 ( pContext->Rtt2x, 1 ); + if ( 1000 > DeltaTime ) { + DEBUG (( DEBUG_WINDOW, + "WindowSize: %d, Threshold: %d, AckCount: %4d, RTT: %Ld nSec\r\n", + pContext->WindowSize, + pContext->Threshold, + pContext->AckCount, + DeltaTime )); + } + else if (( 1000 * 1000 ) > DeltaTime ) { + Value = (UINT32)DeltaTime; + DEBUG (( DEBUG_WINDOW, + "WindowSize: %d, Threshold: %d, AckCount: %4d, RTT: %d.%03d uSec\r\n", + pContext->WindowSize, + pContext->Threshold, + pContext->AckCount, + Value / 1000, + Value % 1000 )); + } + else if (( 1000 * 1000 * 1000 ) > DeltaTime ) { + Value = (UINT32)DivU64x32 ( DeltaTime, 1000 ); + DEBUG (( DEBUG_WINDOW, + "WindowSize: %d, Threshold: %d, AckCount: %4d, RTT: %d.%03d mSec\r\n", + pContext->WindowSize, + pContext->Threshold, + pContext->AckCount, + Value / 1000, + Value % 1000 )); + } + else { + Value = (UINT32)DivU64x32 ( DeltaTime, 1000 * 1000 ); + DEBUG (( DEBUG_WINDOW, + "WindowSize: %d, Threshold: %d, AckCount: %4d, RTT: %d.%03d Sec\r\n", + pContext->WindowSize, + pContext->Threshold, + pContext->AckCount, + Value / 1000, + Value % 1000 )); + } + DEBUG_CODE_END ( ); + } + } + + DBG_EXIT ( ); + } } /** - Stop the TFTP server port creation timer - - @param [in] pTftpServer The TFTP server control structure address. + A timeout has occurred, close the window - @retval EFI_SUCCESS The TFTP port timer is stopped - @retval Other Failed to stop the TFTP port timer + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure **/ -EFI_STATUS -TftpServerTimerStop ( - IN TSDT_TFTP_SERVER * pTftpServer +VOID +WindowTimeout ( + IN TSDT_CONNECTION_CONTEXT * pContext ) { - EFI_STATUS Status; + if ( PcdGetBool ( Tftp_HighSpeed )) { + TFTP_PACKET * pPacket; - DBG_ENTER ( ); + DBG_ENTER ( ); - // - // Assume the timer is stopped - // - Status = EFI_SUCCESS; - if ( pTftpServer->bTimerRunning ) { // - // Stop the port creation polling + // Set the threshold at half the previous window size // - Status = gBS->SetTimer ( pTftpServer->TimerEvent, - TimerCancel, - 0 ); - if ( !EFI_ERROR ( Status )) { - DEBUG (( DEBUG_TFTP_PORT, "TFT[ port timer stopped\r\n" )); + pContext->Threshold = ( pContext->WindowSize + 1 ) >> 1; - // - // Mark the timer stopped - // - pTftpServer->bTimerRunning = FALSE; + // + // Close the transmit window + // + pContext->WindowSize = 1; + pContext->PacketsInWindow = 0; + + // + // Double the round trip time + // + pContext->Rtt2x = LShiftU64 ( pContext->Rtt2x, 1 ); + if ( pContext->Rtt2x > pContext->MaxTimeout ) { + pContext->Rtt2x = pContext->MaxTimeout; + } + + // + // Set the ACK count + // + if ( pContext->WindowSize < pContext->Threshold ) { + pContext->AckCount = pContext->WindowSize * PcdGet32 ( Tftp_AckMultiplier ); } else { - DEBUG (( DEBUG_ERROR | DEBUG_TFTP_PORT, - "ERROR - Failed to stop TFT[ port timer, Status: %r\r\n", - Status )); + pContext->AckCount = PcdGet32 ( Tftp_AckLogBase ) << pContext->WindowSize; } - } - - // - // Return the operation status - // - DBG_EXIT_STATUS ( Status ); - return Status; -} - -/** - Send the next TFTP packet - - @param [in] pTftpServer The TFTP server control structure address. - @param [in] pContext The context structure address. - - @retval EFI_SUCCESS Message processed successfully - -**/ -EFI_STATUS -TftpTxPacket ( - IN TSDT_TFTP_SERVER * pTftpServer, - IN TSDT_CONNECTION_CONTEXT * pContext - ) -{ - ssize_t LengthInBytes; - EFI_STATUS Status; - - DBG_ENTER ( ); - // - // Assume success - // - Status = EFI_SUCCESS; + // + // Display the round trip time + // + DEBUG_CODE_BEGIN ( ); + UINT64 DeltaTime; + UINT32 Value; + + DeltaTime = RShiftU64 ( pContext->Rtt2x, 1 ); + if ( 1000 > DeltaTime ) { + DEBUG (( DEBUG_WINDOW, + "WindowSize: %d, Threshold: %d, AckCount: %4d, RTT: %Ld nSec\r\n", + pContext->WindowSize, + pContext->Threshold, + pContext->AckCount, + DeltaTime )); + } + else if (( 1000 * 1000 ) > DeltaTime ) { + Value = (UINT32)DeltaTime; + DEBUG (( DEBUG_WINDOW, + "WindowSize: %d, Threshold: %d, AckCount: %4d, RTT: %d.%03d uSec\r\n", + pContext->WindowSize, + pContext->Threshold, + pContext->AckCount, + Value / 1000, + Value % 1000 )); + } + else if (( 1000 * 1000 * 1000 ) > DeltaTime ) { + Value = (UINT32)DivU64x32 ( DeltaTime, 1000 ); + DEBUG (( DEBUG_WINDOW, + "WindowSize: %d, Threshold: %d, AckCount: %4d, RTT: %d.%03d mSec\r\n", + pContext->WindowSize, + pContext->Threshold, + pContext->AckCount, + Value / 1000, + Value % 1000 )); + } + else { + Value = (UINT32)DivU64x32 ( DeltaTime, 1000 * 1000 ); + DEBUG (( DEBUG_WINDOW, + "WindowSize: %d, Threshold: %d, AckCount: %4d, RTT: %d.%03d Sec\r\n", + pContext->WindowSize, + pContext->Threshold, + pContext->AckCount, + Value / 1000, + Value % 1000 )); + } + DEBUG_CODE_END ( ); - // - // Send the TFTP packet - // - DEBUG (( DEBUG_TX, - "0x%08x: pContext sending 0x%08x bytes\r\n", - pContext, - pContext->TxBytes )); - LengthInBytes = sendto ( pTftpServer->TftpPort.fd, - &pContext->TxBuffer[0], - pContext->TxBytes, - 0, - (struct sockaddr *)&pContext->RemoteAddress, - pContext->RemoteAddress.sin_len ); - if ( -1 == LengthInBytes ) { - DEBUG (( DEBUG_ERROR | DEBUG_TX, - "ERROR - Transmit failure, errno: 0x%08x\r\n", - errno )); - Status = EFI_DEVICE_ERROR; + // + // Retransmit the first packet in the window + // + pPacket = pContext->pTxHead; + if ( NULL != pPacket ) { + PacketTx ( pContext, pPacket ); + } + + DBG_EXIT ( ); } - - // - // Return the operation status - // - DBG_EXIT_STATUS ( Status ); - return Status; } @@ -1466,25 +2209,46 @@ main ( IN char **Argv ) { + UINTN Index; TSDT_TFTP_SERVER * pTftpServer; EFI_STATUS Status; + UINT64 TriggerTime; // - // Create a timer event to start TFTP port + // Get the performance counter characteristics // pTftpServer = &mTftpServer; + if ( PcdGetBool ( Tftp_HighSpeed ) + || PcdGetBool ( Tftp_Bandwidth )) { + pTftpServer->ClockFrequency = GetPerformanceCounterProperties ( &pTftpServer->Time1, + &pTftpServer->Time2 ); + } + + // + // Create a timer event to start TFTP port + // Status = gBS->CreateEvent ( EVT_TIMER, TPL_TFTP_SERVER, NULL, NULL, &pTftpServer->TimerEvent ); if ( !EFI_ERROR ( Status )) { - Status = TftpServerTimerStart ( pTftpServer ); + // + // Compute the poll interval + // + TriggerTime = TFTP_PORT_POLL_DELAY * ( 1000 * 10 ); + Status = gBS->SetTimer ( pTftpServer->TimerEvent, + TimerPeriodic, + TriggerTime ); if ( !EFI_ERROR ( Status )) { + DEBUG (( DEBUG_TFTP_PORT, "TFTP port timer started\r\n" )); + // // Run the TFTP server forever // - for ( ; ; ) { + pTftpServer->Udpv4Index = -1; + pTftpServer->Udpv6Index = -1; + do { // // Poll the network layer to create the TFTP port // for the tftp server. More than one attempt may @@ -1492,28 +2256,107 @@ main ( // the IP address and initialize the upper layers // of the network stack. // - TftpServerTimer ( pTftpServer ); + if ( DIM ( pTftpServer->TftpPort ) != pTftpServer->Entries ) { + do { + // + // Wait a while before polling for a connection + // + if ( EFI_SUCCESS != gBS->CheckEvent ( pTftpServer->TimerEvent )) { + if ( 0 == pTftpServer->Entries ) { + break; + } + gBS->WaitForEvent ( 1, &pTftpServer->TimerEvent, &Index ); + } + + // + // Poll for a network connection + // + TftpServerSocket ( pTftpServer, + AF_INET, + &pTftpServer->Udpv4Index ); + TftpServerSocket ( pTftpServer, + AF_INET6, + &pTftpServer->Udpv6Index ); + } while ( 0 == pTftpServer->Entries ); + } // // Poll the socket for activity // do { SocketPoll ( pTftpServer ); - } while ( -1 != pTftpServer->TftpPort.fd ); -// -// TODO: Remove the following test code -// Exit when the network connection is broken -// -break; - } + // + // Normal TFTP lets the client request the retransmit by + // sending another ACK for the previous packet + // + if ( PcdGetBool ( Tftp_HighSpeed )) { + UINT64 CurrentTime; + UINT64 ElapsedTime; + TSDT_CONNECTION_CONTEXT * pContext; + TFTP_PACKET * pPacket; + + // + // High speed TFTP uses an agressive retransmit to + // get the TFTP client moving again when the ACK or + // previous data packet was lost. + // + // Get the current time + // + CurrentTime = GetPerformanceCounter ( ); + + // + // Walk the list of contexts + // + pContext = pTftpServer->pContextList; + while ( NULL != pContext ) + { + // + // Check for a transmit timeout + // + pPacket = pContext->pTxHead; + if ( NULL != pPacket ) { + // + // Compute the elapsed time + // + if ( pTftpServer->Time2 > pTftpServer->Time1 ) { + ElapsedTime = CurrentTime - pPacket->TxTime; + } + else { + ElapsedTime = pPacket->TxTime - CurrentTime; + } + ElapsedTime = GetTimeInNanoSecond ( ElapsedTime ); + + // + // Determine if a retransmission is necessary + // + if ( ElapsedTime >= pContext->Rtt2x ) { + DEBUG (( DEBUG_WINDOW, + "0x%08x: Context TX timeout for packet 0x%08x, Window: %d\r\n", + pContext, + pPacket, + pContext->WindowSize )); + WindowTimeout ( pContext ); + } + } + + // + // Set the next context + // + pContext = pContext->pNext; + } + } + } while ( DIM ( pTftpServer->TftpPort ) == pTftpServer->Entries ); + } while ( !mbTftpServerExit ); // // Done with the timer event // - TftpServerTimerStop ( pTftpServer ); - Status = gBS->CloseEvent ( pTftpServer->TimerEvent ); + gBS->SetTimer ( pTftpServer->TimerEvent, + TimerCancel, + 0 ); } + gBS->CloseEvent ( pTftpServer->TimerEvent ); } // diff --git a/AppPkg/Applications/Sockets/TftpServer/TftpServer.h b/AppPkg/Applications/Sockets/TftpServer/TftpServer.h index b1e9f83ce5..79b64c2820 100644 --- a/AppPkg/Applications/Sockets/TftpServer/TftpServer.h +++ b/AppPkg/Applications/Sockets/TftpServer/TftpServer.h @@ -1,7 +1,7 @@ /** @file Definitions for the TFTP server. - Copyright (c) 2011, Intel Corporation + Copyright (c) 2011, 2012, 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 @@ -16,6 +16,7 @@ #define _TFTP_SERVER_H_ #include +#include #include #include #include @@ -24,29 +25,33 @@ #include #include +#include #include +#include #include #include #include #include #include +#include #include #include #include +#include //------------------------------------------------------------------------------ // Macros //------------------------------------------------------------------------------ #if defined(_MSC_VER) /* Handle Microsoft VC++ compiler specifics. */ -#define DBG_ENTER() DEBUG (( DEBUG_INFO, "Entering " __FUNCTION__ "\n" )) ///< Display routine entry -#define DBG_EXIT() DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ "\n" )) ///< Display routine exit -#define DBG_EXIT_DEC(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: %d\n", Status )) ///< Display routine exit with decimal value -#define DBG_EXIT_HEX(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: 0x%08x\n", Status )) ///< Display routine exit with hex value -#define DBG_EXIT_STATUS(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: %r\n", Status )) ///< Display routine exit with status value -#define DBG_EXIT_TF(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", returning %s\n", (FALSE == Status) ? L"FALSE" : L"TRUE" )) ///< Display routine with TRUE/FALSE value +#define DBG_ENTER() DEBUG (( DEBUG_ENTER_EXIT, "Entering " __FUNCTION__ "\n" )) ///< Display routine entry +#define DBG_EXIT() DEBUG (( DEBUG_ENTER_EXIT, "Exiting " __FUNCTION__ "\n" )) ///< Display routine exit +#define DBG_EXIT_DEC(Status) DEBUG (( DEBUG_ENTER_EXIT, "Exiting " __FUNCTION__ ", Status: %d\n", Status )) ///< Display routine exit with decimal value +#define DBG_EXIT_HEX(Status) DEBUG (( DEBUG_ENTER_EXIT, "Exiting " __FUNCTION__ ", Status: 0x%08x\n", Status )) ///< Display routine exit with hex value +#define DBG_EXIT_STATUS(Status) DEBUG (( DEBUG_ENTER_EXIT, "Exiting " __FUNCTION__ ", Status: %r\n", Status )) ///< Display routine exit with status value +#define DBG_EXIT_TF(Status) DEBUG (( DEBUG_ENTER_EXIT, "Exiting " __FUNCTION__ ", returning %s\n", (FALSE == Status) ? L"FALSE" : L"TRUE" )) ///< Display routine with TRUE/FALSE value #else // _MSC_VER #define DBG_ENTER() #define DBG_EXIT() @@ -62,14 +67,22 @@ // Constants //------------------------------------------------------------------------------ -#define DEBUG_PORT_WORK 0x40000000 ///< Display the port work messages -#define DEBUG_SERVER_TIMER 0x20000000 ///< Display the socket poll messages -#define DEBUG_TFTP_PORT 0x10000000 ///< Display the TFTP port messages -#define DEBUG_TFTP_REQUEST 0x08000000 ///< Display the TFTP request messages -#define DEBUG_TX 0x04000000 ///< Display transmit messages -#define DEBUG_SOCKET_POLL 0x02000000 ///< Display the socket poll messages -#define DEBUG_RX 0x01000000 ///< Display receive messages -#define DEBUG_TFTP_ACK 0x00800000 ///< Display the TFTP ACK messages +#define ACK_SHIFT 4 ///< Number of samples in ACK average + +#define DEBUG_WINDOW 0x00000001 ///< Display the window messages +#define DEBUG_TX_PACKET 0x00000002 ///< Display the transmit packet messages +#define DEBUG_FILE_BUFFER 0x00000004 ///< Display the file buffer messages +#define DEBUG_SERVER_TIMER 0x00000008 ///< Display the socket poll messages +#define DEBUG_TFTP_REQUEST 0x00000010 ///< Display the TFTP request messages +#define DEBUG_PORT_WORK 0x00000020 ///< Display the port work messages +#define DEBUG_SOCKET_POLL 0x00000040 ///< Display the socket poll messages +#define DEBUG_TFTP_PORT 0x00000080 ///< Display the TFTP port messages +#define DEBUG_TX 0x00000100 ///< Display transmit messages +#define DEBUG_RX 0x00000200 ///< Display receive messages +#define DEBUG_TFTP_ACK 0x00000400 ///< Display the TFTP ACK messages +#define DEBUG_ENTER_EXIT 0x00000800 ///< Display entry and exit messages + +#define MAX_PACKETS 8 ///< Maximum number of packets in the window #define TFTP_PORT_POLL_DELAY ( 2 * 1000 ) ///< Delay in milliseconds for attempts to open the TFTP port #define CLIENT_POLL_DELAY 50 ///< Delay in milliseconds between client polls @@ -123,10 +136,32 @@ #define TFTP_MAX_BLOCK_SIZE 4096 ///< Maximum block size +#define TFTP_ERROR_SEE_MSG 0 ///< See the error message +#define TFTP_ERROR_NOT_FOUND 1 ///< File not found +#define TFTP_ERROR_ACCESS_VIOLATION 2 ///< Access violation +#define TFTP_ERROR_DISK_FULL 3 ///< Disk full +#define TFTP_ERROR_ILLEGAL_OP 4 ///< Illegal operation +#define TFTP_ERROR_UNKNOWN_XFER_ID 5 ///< Unknown transfer ID +#define TFTP_ERROR_FILE_EXISTS 6 ///< File already exists +#define TFTP_ERROR_NO_SUCH_USER 7 ///< No such user + //------------------------------------------------------------------------------ // Data Types //------------------------------------------------------------------------------ +/** + Packet structure +**/ +typedef struct _TFTP_PACKET TFTP_PACKET; +typedef struct _TFTP_PACKET { + TFTP_PACKET * pNext; ///< Next packet in list + UINT64 TxTime; ///< Time the transmit was performed + ssize_t TxBytes; ///< Bytes in the TX buffer + UINT32 RetryCount; ///< Number of transmissions + UINT16 BlockNumber; ///< Block number of this packet + UINT8 TxBuffer[ 2 + 2 + TFTP_MAX_BLOCK_SIZE ]; ///< Transmit buffer +} GCC_TFTP_PACKET; + /** Port control structure **/ @@ -135,33 +170,49 @@ typedef struct _TSDT_CONNECTION_CONTEXT { // // Remote connection management // - TSDT_CONNECTION_CONTEXT * pNext; ///< Next context in the connection list - struct sockaddr_in RemoteAddress; ///< Remote address - - // - // TFTP management parameters - // - UINT16 AckNext; ///< Next block to be received - BOOLEAN bExpectAck; ///< TRUE for read, FALSE for write - UINT32 BlockSize; ///< Negotiated block size - UINT32 Timeout; ///< Number of seconds to wait before retransmission + TSDT_CONNECTION_CONTEXT * pNext; ///< Next context in the connection list + struct sockaddr_in6 RemoteAddress; ///< Remote address + int SocketFd; ///< Socket file descriptor // // File management parameters // - EFI_HANDLE File; ///< NULL while file is closed + FILE * File; ///< NULL while file is closed UINT64 LengthInBytes; ///< Size of the file - UINT64 MaxTransferSize; ///< Maximum transfer size + UINT64 BytesRemaining; ///< Number of bytes remaining to be sent + UINT64 BytesToSend; ///< Number of bytes to send + UINT64 ValidBytes; ///< Number of valid bytes in the buffer BOOLEAN bEofSent; ///< End of file sent + UINT8 * pFill; ///< Next portion of the buffer to fill UINT8 * pBuffer; ///< Pointer into the file data UINT8 * pEnd; ///< End of the file data - UINT8 FileData[ 64 * TFTP_MAX_BLOCK_SIZE ]; ///< File data to send + UINT8 FileData[ 2 * MAX_PACKETS * TFTP_MAX_BLOCK_SIZE ]; ///< File data to send + UINT64 TimeStart; ///< Start of file transfer + + // + // TFTP management parameters + // + UINT16 BlockNumber; ///< Next block to be transmitted + UINT32 BlockSize; ///< Negotiated block size + + // + // Window management + // + UINT32 AckCount; ///< Number of ACKs to receive before increasing the window + UINT32 PacketsInWindow; ///< Number of packets in the window + UINT32 Threshold; ///< Size of window when ACK count becomes logrithmic + UINT32 WindowSize; ///< Size of the transmit window + UINT64 MaxTimeout; ///< Maximum number of seconds to wait before retransmission + UINT64 Rtt2x; ///< Twice the average round trip time in nanoseconds // // Buffer management // - ssize_t TxBytes; ///< Bytes in the TX buffer - UINT8 TxBuffer[ 2 + 2 + TFTP_MAX_BLOCK_SIZE ]; ///< Transmit buffer + TFTP_PACKET * pFreeList; ///< List of free packets + TFTP_PACKET * pTxHead; ///< First packet in the list of packets for transmission + TFTP_PACKET * pTxTail; ///< Last packet in the list of packets for transmission + TFTP_PACKET ErrorPacket; ///< Error packet + TFTP_PACKET Tx[ MAX_PACKETS ];///< Transmit packets }GCC_TSDT_CONNECTION_CONTEXT; /** @@ -175,20 +226,32 @@ typedef struct { // EFI_HANDLE ImageHandle; ///< Image handle + // + // Performance management + // + UINT64 ClockFrequency; ///< Frequency of the clock + UINT64 Time1; ///< Clock value after rollover + UINT64 Time2; ///< Clock value before rollover + UINT64 RxTime; ///< Time when the packet was recevied + // // TFTP port management // - BOOLEAN bTimerRunning; ///< Port creation timer status EFI_EVENT TimerEvent; ///< Timer to open TFTP port - struct pollfd TftpPort; ///< Poll descriptor for the TFTP port - struct sockaddr_in TftpServerAddress; ///< Address of the local TFTP server + int Udpv4Index; ///< Entry for UDPv4 + int Udpv6Index; ///< Entry for UDPv6 + int Entries; ///< Number of TFTP ports + struct pollfd TftpPort [ 2 ]; ///< Poll descriptor for the TFTP ports (UDP4, UDP6) // // Request management // - struct sockaddr_in RemoteAddress; ///< Remote address - ssize_t RxBytes; ///< Receive data length in bytes - UINT8 RxBuffer[ 2 + 2 + TFTP_MAX_BLOCK_SIZE ]; ///< Receive buffer + union { + struct sockaddr_in v4; ///< UDP4 address + struct sockaddr_in6 v6; ///< UDP6 address + } RemoteAddress; ///< Remote address + ssize_t RxBytes; ///< Receive data length in bytes + UINT8 RxBuffer[ 2 + 2 + TFTP_MAX_BLOCK_SIZE ]; ///< Receive buffer // // Client port management @@ -205,37 +268,77 @@ extern TSDT_TFTP_SERVER mTftpServer; //------------------------------------------------------------------------------ /** - Process the TFTP request + Queue data packets for transmission - @param [in] pOption Address of the first zero terminated option string - @param [in] pValue Address to receive the value + @param [in] pContext Connection context structure address - @retval EFI_SUCCESS Option translated into a value + @retval TRUE if a read error occurred **/ -EFI_STATUS -TftpOptionValue ( - IN UINT8 * pOption, - IN INT32 * pValue +BOOLEAN +PacketFill ( + IN TSDT_CONNECTION_CONTEXT * pContext ); /** - Process the TFTP request + Free the packet - @param [in] pTftpServer The TFTP server control structure address. - @param [in] pContext Connection context structure address + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure + @param [in] pPacket Address of a ::TFTP_PACKET structure **/ VOID -TftpProcessRequest ( - IN TSDT_TFTP_SERVER * pTftpServer, +PacketFree( + IN TSDT_CONNECTION_CONTEXT * pContext, + IN TFTP_PACKET * pPacket + ); + +/** + Get a packet for transmission + + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure + + @retval Address of a ::TFTP_PACKET structure + +**/ +TFTP_PACKET * +PacketGet ( IN TSDT_CONNECTION_CONTEXT * pContext ); +/** + Queue the packet for transmission + + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure + @param [in] pPacket Address of a ::TFTP_PACKET structure + + @retval TRUE if a transmission error has occurred + +**/ +BOOLEAN +PacketQueue ( + IN TSDT_CONNECTION_CONTEXT * pContext, + IN TFTP_PACKET * pPacket + ); + +/** + Transmit the packet + + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure + @param [in] pPacket Address of a ::TFTP_PACKET structure + + @retval EFI_SUCCESS Message processed successfully + +**/ +EFI_STATUS +PacketTx ( + IN TSDT_CONNECTION_CONTEXT * pContext, + IN TFTP_PACKET * pPacket + ); + /** Build and send an error packet - @param [in] pTftpServer The TFTP server control structure address. @param [in] pContext The context structure address. @param [in] Error Error number for the packet @param [in] pError Zero terminated error string address @@ -244,85 +347,82 @@ TftpProcessRequest ( **/ EFI_STATUS -TftpSendError ( - IN TSDT_TFTP_SERVER * pTftpServer, +SendError ( IN TSDT_CONNECTION_CONTEXT * pContext, IN UINT16 Error, IN UINT8 * pError ); /** - Send the next block of file system data + Process the TFTP request - @param [in] pTftpServer The TFTP server control structure address. - @param [in] pContext The context structure address. + @param [in] pOption Address of the first zero terminated option string + @param [in] pValue Address to receive the value - @retval EFI_SUCCESS Message processed successfully + @retval EFI_SUCCESS Option translated into a value **/ EFI_STATUS -TftpSendNextBlock ( - IN TSDT_TFTP_SERVER * pTftpServer, - IN TSDT_CONNECTION_CONTEXT * pContext +TftpOptionValue ( + IN UINT8 * pOption, + IN INT32 * pValue ); /** - TFTP port creation timer routine - - This routine polls the socket layer waiting for the initial network connection - which will enable the creation of the TFTP port. The socket layer will manage - the coming and going of the network connections after that until the last network - connection is broken. + Process the TFTP request - @param [in] pTftpServer The TFTP server control structure address. + @param [in] pTftpServer The TFTP server control structure address. + @param [in] pContext Connection context structure address + @param [in] SocketFd Socket file descriptor **/ VOID -TftpServerTimer ( - IN TSDT_TFTP_SERVER * pTftpServer +TftpProcessRequest ( + IN TSDT_TFTP_SERVER * pTftpServer, + IN TSDT_CONNECTION_CONTEXT * pContext, + IN int SocketFd ); /** - Start the TFTP server port creation timer + Process the read request - @param [in] pTftpServer The TFTP server control structure address. + @param [in] pTftpServer Address of the ::TSDT_TFTP_SERVER structure + @param [in] pContext Connection context structure address + @param [in] SocketFd Socket file descriptor - @retval EFI_SUCCESS The timer was successfully started. - @retval EFI_ALREADY_STARTED The timer is already running. - @retval Other The timer failed to start. + @retval TRUE if the context should be closed **/ -EFI_STATUS -TftpServerTimerStart ( - IN TSDT_TFTP_SERVER * pTftpServer +BOOLEAN +TftpRead ( + IN TSDT_TFTP_SERVER * pTftpServer, + IN TSDT_CONNECTION_CONTEXT * pContext, + IN int SocketFd ); /** - Stop the TFTP server port creation timer - - @param [in] pTftpServer The TFTP server control structure address. + Update the window due to the ACK - @retval EFI_SUCCESS The TFTP port timer is stopped - @retval Other Failed to stop the TFTP port timer + @param [in] pTftpServer Address of the ::TSDT_TFTP_SERVER structure + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure + @param [in] pPacket Address of a ::TFTP_PACKET structure **/ -EFI_STATUS -TftpServerTimerStop ( - IN TSDT_TFTP_SERVER * pTftpServer +VOID +WindowAck ( + IN TSDT_TFTP_SERVER * pTftpServer, + IN TSDT_CONNECTION_CONTEXT * pContext, + IN TFTP_PACKET * pPacket ); /** - Send the next TFTP packet - - @param [in] pTftpServer The TFTP server control structure address. - @param [in] pContext The context structure address. + A timeout has occurred, close the window - @retval EFI_SUCCESS Message processed successfully + @param [in] pContext Address of a ::TSDT_CONNECTION_CONTEXT structure **/ -EFI_STATUS -TftpTxPacket ( - IN TSDT_TFTP_SERVER * pTftpServer, +VOID +WindowTimeout ( IN TSDT_CONNECTION_CONTEXT * pContext ); diff --git a/AppPkg/Applications/Sockets/TftpServer/TftpServer.inf b/AppPkg/Applications/Sockets/TftpServer/TftpServer.inf index f48724289f..d7cbfaa303 100644 --- a/AppPkg/Applications/Sockets/TftpServer/TftpServer.inf +++ b/AppPkg/Applications/Sockets/TftpServer/TftpServer.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such @@ -39,7 +39,17 @@ TftpServer.c +[Pcd] + gAppPkgTokenSpaceGuid.Tftp_AckLogBase + gAppPkgTokenSpaceGuid.Tftp_AckMultiplier + gAppPkgTokenSpaceGuid.Tftp_Bandwidth + gAppPkgTokenSpaceGuid.Tftp_HighSpeed + gAppPkgTokenSpaceGuid.Tftp_MaxRetry + gAppPkgTokenSpaceGuid.Tftp_MaxTimeoutInSec + + [Packages] + AppPkg/AppPkg.dec MdePkg/MdePkg.dec ShellPkg/ShellPkg.dec StdLib/StdLib.dec @@ -54,6 +64,7 @@ LibC ShellLib ShellCEntryLib + TimerLib UefiBootServicesTableLib UefiLib UefiRuntimeServicesTableLib diff --git a/AppPkg/Applications/Sockets/WebServer/Exit.c b/AppPkg/Applications/Sockets/WebServer/Exit.c new file mode 100644 index 0000000000..cad4b006b2 --- /dev/null +++ b/AppPkg/Applications/Sockets/WebServer/Exit.c @@ -0,0 +1,92 @@ +/*++ + This file contains an 'Intel UEFI Application' and is + licensed for Intel CPUs and chipsets under the terms of your + license agreement with Intel or your vendor. This file may + be modified by the user, subject to additional terms of the + license agreement +--*/ +/*++ + +Copyright (c) 2011 Intel Corporation. All rights reserved +This software and associated documentation (if any) is furnished +under a license and may only be used or copied in accordance +with the terms of the license. Except as permitted by such +license, no part of this software or documentation may be +reproduced, stored in a retrieval system, or transmitted in any +form or by any means without the express written consent of +Intel Corporation. + +--*/ + +/** @file + Exit response page + +**/ + +#include + + +/** + Respond with the Exit page + + @param [in] SocketFD The socket's file descriptor to add to the list. + @param [in] pPort The WSDT_PORT structure address + @param [out] pbDone Address to receive the request completion status + + @retval EFI_SUCCESS The request was successfully processed + +**/ +EFI_STATUS +ExitPage ( + IN int SocketFD, + IN WSDT_PORT * pPort, + OUT BOOLEAN * pbDone + ) +{ + EFI_STATUS Status; + + DBG_ENTER ( ); + + // + // Send the Hello World page + // + for ( ; ; ) { + // + // Tell the web-server to exit + // + mWebServer.bRunning = FALSE; + + // + // Send the page header + // + Status = HttpPageHeader ( SocketFD, pPort, L"Exit" ); + if ( EFI_ERROR ( Status )) { + break; + } + + // + // Send the page body + // + Status = HttpSendAnsiString ( SocketFD, + pPort, + "

Exit

\r\n" + "

\r\n" + " Exiting the web-server application.\r\n" + "

\r\n" ); + if ( EFI_ERROR ( Status )) { + break; + } + + // + // Send the page trailer + // + Status = HttpPageTrailer ( SocketFD, pPort, pbDone ); + break; + } + + // + // Return the operation status + // + DBG_EXIT_STATUS ( Status ); + return Status; +} diff --git a/AppPkg/Applications/Sockets/WebServer/HTTP.c b/AppPkg/Applications/Sockets/WebServer/HTTP.c index 8487de751a..9a8bd70a9d 100644 --- a/AppPkg/Applications/Sockets/WebServer/HTTP.c +++ b/AppPkg/Applications/Sockets/WebServer/HTTP.c @@ -401,8 +401,8 @@ HttpPageTrailer ( int RetVal; EFI_STATUS Status; socklen_t LengthInBytes; - struct sockaddr_in LocalAddress; - struct sockaddr_in RemoteAddress; + struct sockaddr_in6 LocalAddress; + struct sockaddr_in6 RemoteAddress; DBG_ENTER ( ); @@ -413,12 +413,13 @@ HttpPageTrailer ( LengthInBytes = sizeof ( LocalAddress ); RetVal = getsockname ( SocketFD, (struct sockaddr *)&LocalAddress, &LengthInBytes ); if ( 0 == RetVal ) { + LengthInBytes = sizeof ( LocalAddress ); RetVal = getpeername ( SocketFD, (struct sockaddr *)&RemoteAddress, &LengthInBytes ); if ( 0 == RetVal ) { // // Seperate the body from the trailer // - Status = HttpSendAnsiString ( SocketFD, pPort, "
\r\n" ); + Status = HttpSendAnsiString ( SocketFD, pPort, "
\r\n" ); if ( EFI_ERROR ( Status )) { break; } @@ -438,7 +439,7 @@ HttpPageTrailer ( if ( EFI_ERROR ( Status )) { break; } - Status = HttpSendAnsiString ( SocketFD, pPort, "\r\n" ); + Status = HttpSendAnsiString ( SocketFD, pPort, "\r\n" ); if ( EFI_ERROR ( Status )) { break; } @@ -1213,7 +1214,7 @@ HttpSendHexBits ( // Digit = (UINT32)(( Value >> Shift ) & 0xf ); if ( 10 <= Digit ) { - Digit += 'A' - '0' - 10; + Digit += 'a' - '0' - 10; } // @@ -1274,7 +1275,7 @@ HttpSendHexValue ( // Digit = (UINT32)(( Value >> Shift ) & 0xf ); if ( 10 <= Digit ) { - Digit += 'A' - '0' - 10; + Digit += 'a' - '0' - 10; } // @@ -1305,6 +1306,112 @@ HttpSendHexValue ( } +/** + Output an IP6 address value to the HTML page + + @param [in] SocketFD Socket file descriptor + @param [in] pPort The WSDT_PORT structure address + @param [in] Value Value to display + @param [in] bFirstValue TRUE if first value + @param [in] bLastValue TRUE if last value + @param [in] bZeroSuppression TRUE while zeros are being suppressed + @param [in] pbZeroSuppression Address to receive TRUE when zero suppression + has started, use NULL if next colon value not + needed. + + @retval EFI_SUCCESS Successfully displayed the address +**/ +EFI_STATUS +HttpSendIp6Value ( + IN int SocketFD, + IN WSDT_PORT * pPort, + IN UINT16 Value, + IN BOOLEAN bFirstValue, + IN BOOLEAN bLastValue, + IN BOOLEAN bZeroSuppression, + IN BOOLEAN * pbZeroSuppression + ) +{ + BOOLEAN bZeroSuppressionStarting; + UINT32 Digit; + EFI_STATUS Status; + + // + // Use break instead of goto + // + bZeroSuppressionStarting = FALSE; + Status = EFI_SUCCESS; + for ( ; ; ) { + // + // Display the leading colon if necessary + // + if ( bZeroSuppression && ( bLastValue || ( 0 != Value ))) { + Status = HttpSendByte ( SocketFD, pPort, ':' ); + if ( EFI_ERROR ( Status )) { + break; + } + } + + // + // Skip over a series of zero values + // + bZeroSuppressionStarting = (BOOLEAN)( 0 == Value ); + if ( !bZeroSuppressionStarting ) { + // + // Display the value + // + Digit = ( Value >> 4 ) & 0xf; + Status = HttpSendHexValue ( SocketFD, + pPort, + Digit ); + if ( EFI_ERROR ( Status )) { + break; + } + Digit = Value & 0xf; + Status = HttpSendHexValue ( SocketFD, + pPort, + Digit ); + if ( EFI_ERROR ( Status )) { + break; + } + Digit = ( Value >> 12 ) & 0xf; + Status = HttpSendHexValue ( SocketFD, + pPort, + Digit ); + if ( EFI_ERROR ( Status )) { + break; + } + Digit = ( Value >> 8 ) & 0xf; + Status = HttpSendHexValue ( SocketFD, + pPort, + Digit ); + if ( EFI_ERROR ( Status )) { + break; + } + } + + // + // Display the trailing colon if necessary + // + if (( !bLastValue ) && ( bFirstValue || ( 0 != Value ))) { + Status = HttpSendByte ( SocketFD, pPort, ':' ); + } + break; + } + + // + // Return the next colon display + if ( NULL != pbZeroSuppression ) { + *pbZeroSuppression = bZeroSuppressionStarting; + } + + // + // Return the operation status + // + return Status; +} + + /** Output an IP address to the HTML page @@ -1318,41 +1425,109 @@ EFI_STATUS HttpSendIpAddress ( IN int SocketFD, IN WSDT_PORT * pPort, - IN struct sockaddr_in * pAddress + IN struct sockaddr_in6 * pAddress ) { + BOOLEAN bZeroSuppression; + UINT32 Index; + struct sockaddr_in * pIpv4; + struct sockaddr_in6 * pIpv6; + UINT16 PortNumber; EFI_STATUS Status; // - // Output the IPv4 address + // Use break instead of goto // - Status = HttpSendValue ( SocketFD, pPort, (UINT8)pAddress->sin_addr.s_addr ); - if ( !EFI_ERROR ( Status )) { - Status = HttpSendByte ( SocketFD, pPort, '.' ); - if ( !EFI_ERROR ( Status )) { - Status = HttpSendValue ( SocketFD, pPort, (UINT8)( pAddress->sin_addr.s_addr >> 8 )); - if ( !EFI_ERROR ( Status )) { - Status = HttpSendByte ( SocketFD, pPort, '.' ); - if ( !EFI_ERROR ( Status )) { - Status = HttpSendValue ( SocketFD, pPort, (UINT8)( pAddress->sin_addr.s_addr >> 16 )); - if ( !EFI_ERROR ( Status )) { - Status = HttpSendByte ( SocketFD, pPort, '.' ); - if ( !EFI_ERROR ( Status )) { - Status = HttpSendValue ( SocketFD, pPort, (UINT8)( pAddress->sin_addr.s_addr >> 24 )); - if ( !EFI_ERROR ( Status )) { - // - // Output the port number - // - Status = HttpSendByte ( SocketFD, pPort, ':' ); - if ( !EFI_ERROR ( Status )) { - Status = HttpSendValue ( SocketFD, pPort, htons ( pAddress->sin_port )); - } - } - } - } + for ( ; ; ) { + // + // Determine the type of address + // + if ( AF_INET6 == pAddress->sin6_family ) { + pIpv6 = pAddress; + + // + // Display the address in RFC2732 format + // + bZeroSuppression = FALSE; + Status = HttpSendByte ( SocketFD, pPort, '[' ); + if ( EFI_ERROR ( Status )) { + break; + } + for ( Index = 0; 8 > Index; Index++ ) { + Status = HttpSendIp6Value ( SocketFD, + pPort, + pIpv6->sin6_addr.__u6_addr.__u6_addr16[ Index ], + (BOOLEAN)( 0 == Index ), + (BOOLEAN)( 7 == Index ), + bZeroSuppression, + &bZeroSuppression ); + if ( EFI_ERROR ( Status )) { + break; } } + if ( EFI_ERROR ( Status )) { + break; + } + + // + // Separate the port number + // + Status = HttpSendByte ( SocketFD, pPort, ']' ); + + // + // Get the port number + // + PortNumber = pIpv6->sin6_port; } + else { + // + // Output the IPv4 address + // + pIpv4 = (struct sockaddr_in *)pAddress; + Status = HttpSendValue ( SocketFD, pPort, (UINT8)pIpv4->sin_addr.s_addr ); + if ( EFI_ERROR ( Status )) { + break; + } + Status = HttpSendByte ( SocketFD, pPort, '.' ); + if ( EFI_ERROR ( Status )) { + break; + } + Status = HttpSendValue ( SocketFD, pPort, (UINT8)( pIpv4->sin_addr.s_addr >> 8 )); + if ( EFI_ERROR ( Status )) { + break; + } + Status = HttpSendByte ( SocketFD, pPort, '.' ); + if ( EFI_ERROR ( Status )) { + break; + } + Status = HttpSendValue ( SocketFD, pPort, (UINT8)( pIpv4->sin_addr.s_addr >> 16 )); + if ( EFI_ERROR ( Status )) { + break; + } + Status = HttpSendByte ( SocketFD, pPort, '.' ); + if ( EFI_ERROR ( Status )) { + break; + } + Status = HttpSendValue ( SocketFD, pPort, (UINT8)( pIpv4->sin_addr.s_addr >> 24 )); + + // + // Get the port number + // + PortNumber = pIpv4->sin_port; + } + if ( EFI_ERROR ( Status )) { + break; + } + + // + // Display the port number + // + Status = HttpSendByte ( SocketFD, pPort, ':' ); + if ( EFI_ERROR ( Status )) { + break; + } + Status = HttpSendValue ( SocketFD, pPort, htons ( PortNumber )); + break; } // diff --git a/AppPkg/Applications/Sockets/WebServer/PageList.c b/AppPkg/Applications/Sockets/WebServer/PageList.c index f26ab0fdbb..98927e8e1d 100644 --- a/AppPkg/Applications/Sockets/WebServer/PageList.c +++ b/AppPkg/Applications/Sockets/WebServer/PageList.c @@ -44,10 +44,12 @@ CONST DT_PAGE mPageList[] = { { L"/DhcpOptions", DhcpOptionsPage, L"DHCP Options" }, ///< Display the DHCP options { PAGE_ACPI_DSDT, AcpiDsdtPage, L"DSDT - Differentiated System Description Table" }, ///< Format DSDT { PAGE_DXE_SERVICES_TABLE, DxeServicesTablePage, L"DXE Services Table" }, ///< Format DXE services table + { L"/Exit", ExitPage, L"Exit the web server" }, ///< Exit the web server application { PAGE_ACPI_FADT, AcpiFadtPage, L"FADT - Fixed ACPI Description Table" }, ///< Format FADT { L"/Firmware", FirmwarePage, L"Firmware" }, ///< Firmware status { L"/Handles", HandlePage, L"Display handles and associated protocol GUIDs" }, ///< Handle database page { L"/Hello", HelloPage, L"Hello World" }, ///< Hello world page + { L"/Ports", PortsPage, L"Display web-server ports" },///< Web-server ports page { L"/Reboot", RebootPage, L"Reboot the sytem" }, ///< Reboot page { PAGE_ACPI_RSDP_10B, AcpiRsdp10Page, L"RSDP 1.0b - ACPI Root System Description Pointer" }, ///< Format RSDP 1.0b table { PAGE_ACPI_RSDP_30, AcpiRsdp30Page, L"RSDP 3.0 - ACPI Root System Description Pointer" }, ///< Format RSDP 3.0 table diff --git a/AppPkg/Applications/Sockets/WebServer/Ports.c b/AppPkg/Applications/Sockets/WebServer/Ports.c new file mode 100644 index 0000000000..e9190bf68d --- /dev/null +++ b/AppPkg/Applications/Sockets/WebServer/Ports.c @@ -0,0 +1,146 @@ +/*++ + This file contains an 'Intel UEFI Application' and is + licensed for Intel CPUs and chipsets under the terms of your + license agreement with Intel or your vendor. This file may + be modified by the user, subject to additional terms of the + license agreement +--*/ +/*++ + +Copyright (c) 2011 Intel Corporation. All rights reserved +This software and associated documentation (if any) is furnished +under a license and may only be used or copied in accordance +with the terms of the license. Except as permitted by such +license, no part of this software or documentation may be +reproduced, stored in a retrieval system, or transmitted in any +form or by any means without the express written consent of +Intel Corporation. + +--*/ + +/** @file + Ports response page + +**/ + +#include + + +/** + Respond with the Ports page + + @param [in] SocketFD The socket's file descriptor to add to the list. + @param [in] pPort The WSDT_PORT structure address + @param [out] pbDone Address to receive the request completion status + + @retval EFI_SUCCESS The request was successfully processed + +**/ +EFI_STATUS +PortsPage ( + IN int SocketFD, + IN WSDT_PORT * pPort, + OUT BOOLEAN * pbDone + ) +{ + socklen_t AddressLength; + struct sockaddr_in6 LocalAddress; + DT_WEB_SERVER * pWebServer; + EFI_STATUS Status; + + DBG_ENTER ( ); + + // + // Send the Hello World page + // + pWebServer = &mWebServer; + for ( ; ; ) { + // + // Send the page header + // + Status = HttpPageHeader ( SocketFD, pPort, L"Ports" ); + if ( EFI_ERROR ( Status )) { + break; + } + + // + // Send the page body + // + Status = HttpSendAnsiString ( SocketFD, + pPort, + "

Web-Server Ports

\r\n" ); + if ( EFI_ERROR ( Status )) { + break; + } + + // + // Check for TCP v4 + // + if ( -1 != pWebServer->HttpListenPort ) { + AddressLength = sizeof ( LocalAddress ); + if ( 0 == getsockname ( pWebServer->HttpListenPort, + (struct sockaddr *)&LocalAddress, + &AddressLength )) { + Status = HttpSendAnsiString ( SocketFD, + pPort, + "Tcp4
\r\n" ); + if ( EFI_ERROR ( Status )) { + break; + } + } + } + + // + // Check for TCP v6 + // + if ( -1 != pWebServer->HttpListenPort6 ) { + AddressLength = sizeof ( LocalAddress ); + if ( 0 == getsockname ( pWebServer->HttpListenPort6, + (struct sockaddr *)&LocalAddress, + &AddressLength )) { + Status = HttpSendAnsiString ( SocketFD, + pPort, + "Tcp6
\r\n" ); + if ( EFI_ERROR ( Status )) { + break; + } + } + } + + // + // Send the page trailer + // + Status = HttpPageTrailer ( SocketFD, pPort, pbDone ); + break; + } + + // + // Return the operation status + // + DBG_EXIT_STATUS ( Status ); + return Status; +} diff --git a/AppPkg/Applications/Sockets/WebServer/WebServer.c b/AppPkg/Applications/Sockets/WebServer/WebServer.c index 5c73bfe8f7..00f8633efb 100644 --- a/AppPkg/Applications/Sockets/WebServer/WebServer.c +++ b/AppPkg/Applications/Sockets/WebServer/WebServer.c @@ -337,7 +337,7 @@ PortWork ( size_t LengthInBytes; int NewSocket; EFI_STATUS OpStatus; - struct sockaddr RemoteAddress; + struct sockaddr_in6 RemoteAddress; socklen_t RemoteAddressLength; EFI_STATUS Status; @@ -355,14 +355,15 @@ PortWork ( // // Determine if this is a connection attempt // - if ( SocketFD == pWebServer->HttpListenPort ) { + if (( SocketFD == pWebServer->HttpListenPort ) + || ( SocketFD == pWebServer->HttpListenPort6 )) { // // Handle connection attempts // Accepts arrive as read events // RemoteAddressLength = sizeof ( RemoteAddress ); NewSocket = accept ( SocketFD, - &RemoteAddress, + (struct sockaddr *)&RemoteAddress, &RemoteAddressLength ); if ( -1 != NewSocket ) { if ( 0 != NewSocket ) { @@ -561,9 +562,9 @@ SocketPoll ( /** - Create the HTTP port for the web server + Create an HTTP port for the web server - This routine polls the network layer to create the HTTP port for the + This routine polls the network layer to create an HTTP port for the web server. More than one attempt may be necessary since it may take some time to get the IP address and initialize the upper layers of the network stack. @@ -572,195 +573,93 @@ SocketPoll ( coming and going of the network connections until the last network connection is broken. - @param [in] pWebServer The web server control structure address. + @param [in] pWebServer The web server control structure address. + @param [in] AddressFamily Address family for the network connection + @param [in] Protocol Protocol to use for the network connection + @param [in] HttpPort Port number for the HTTP connection + @param [out] pPort Address of the port **/ VOID -WebServerTimer ( - IN DT_WEB_SERVER * pWebServer +WebServerListen ( + IN DT_WEB_SERVER * pWebServer, + IN sa_family_t AddressFamily, + IN int Protocol, + IN UINT16 HttpPort, + OUT int * pPort ) { - UINT16 HttpPort; - struct sockaddr_in WebServerAddress; + union { + struct sockaddr_in v4; + struct sockaddr_in6 v6; + } WebServerAddress; int SocketStatus; EFI_STATUS Status; - DEBUG (( DEBUG_SERVER_TIMER, "Entering WebServerTimer\r\n" )); + DEBUG (( DEBUG_SERVER_LISTEN, "Entering WebServerListen\r\n" )); // - // Open the HTTP port on the server + // Attempt to create the socket for the web server // - do { - do { - // - // Complete the client operations - // - SocketPoll ( pWebServer ); - - // - // Wait for a while - // - Status = gBS->CheckEvent ( pWebServer->TimerEvent ); - } while ( EFI_SUCCESS != Status ); - + * pPort = socket ( AddressFamily, SOCK_STREAM, Protocol ); + if ( -1 != *pPort ) { // - // Attempt to create the socket for the web server + // Build the socket address // - pWebServer->HttpListenPort = socket ( AF_INET, - SOCK_STREAM, - IPPROTO_TCP ); - if ( -1 != pWebServer->HttpListenPort ) { - // - // Set the socket address - // - ZeroMem ( &WebServerAddress, sizeof ( WebServerAddress )); - HttpPort = PcdGet16 ( WebServer_HttpPort ); - DEBUG (( DEBUG_HTTP_PORT, - "HTTP Port: %d\r\n", - HttpPort )); - WebServerAddress.sin_len = sizeof ( WebServerAddress ); - WebServerAddress.sin_family = AF_INET; - WebServerAddress.sin_addr.s_addr = INADDR_ANY; - WebServerAddress.sin_port = htons ( HttpPort ); + ZeroMem ( &WebServerAddress, sizeof ( WebServerAddress )); + if ( AF_INET == AddressFamily ) { + WebServerAddress.v4.sin_len = sizeof ( WebServerAddress.v4 ); + WebServerAddress.v4.sin_family = AddressFamily; + WebServerAddress.v4.sin_port = htons ( HttpPort ); + } + else { + WebServerAddress.v6.sin6_len = sizeof ( WebServerAddress.v6 ); + WebServerAddress.v6.sin6_family = AddressFamily; + WebServerAddress.v6.sin6_port = htons ( HttpPort ); + WebServerAddress.v6.sin6_scope_id = __IPV6_ADDR_SCOPE_GLOBAL; + } + // + // Bind the socket to the HTTP port + // + SocketStatus = bind ( *pPort, + (struct sockaddr *) &WebServerAddress, + WebServerAddress.v4.sin_len ); + if ( -1 != SocketStatus ) { // - // Bind the socket to the HTTP port + // Enable connections to the HTTP port // - SocketStatus = bind ( pWebServer->HttpListenPort, - (struct sockaddr *) &WebServerAddress, - WebServerAddress.sin_len ); + SocketStatus = listen ( *pPort, SOMAXCONN ); if ( -1 != SocketStatus ) { // - // Enable connections to the HTTP port + // Add the HTTP port to the list of ports to poll // - SocketStatus = listen ( pWebServer->HttpListenPort, - SOMAXCONN ); - } - - // - // Release the socket if necessary - // - if ( -1 == SocketStatus ) { - close ( pWebServer->HttpListenPort ); - pWebServer->HttpListenPort = -1; + Status = PortAdd ( pWebServer, *pPort ); + if ( EFI_ERROR ( Status )) { + SocketStatus = -1; + } + else { + DEBUG (( DEBUG_PORT_WORK, + "Listening on Tcp%d:%d\r\n", + ( AF_INET == AddressFamily ) ? 4 : 6, + HttpPort )); + } } } // - // Wait until the socket is open - // - }while ( -1 == pWebServer->HttpListenPort ); - - DEBUG (( DEBUG_SERVER_TIMER, "Exiting WebServerTimer\r\n" )); -} - - -/** - Start the web server port creation timer - - @param [in] pWebServer The web server control structure address. - - @retval EFI_SUCCESS The timer was successfully started. - @retval EFI_ALREADY_STARTED The timer is already running. - @retval Other The timer failed to start. - -**/ -EFI_STATUS -WebServerTimerStart ( - IN DT_WEB_SERVER * pWebServer - ) -{ - EFI_STATUS Status; - UINT64 TriggerTime; - - DBG_ENTER ( ); - - // - // Assume the timer is already running - // - Status = EFI_ALREADY_STARTED; - if ( !pWebServer->bTimerRunning ) { - // - // Compute the poll interval + // Release the socket if necessary // - TriggerTime = HTTP_PORT_POLL_DELAY * ( 1000 * 10 ); - Status = gBS->SetTimer ( pWebServer->TimerEvent, - TimerPeriodic, - TriggerTime ); - if ( !EFI_ERROR ( Status )) { - DEBUG (( DEBUG_HTTP_PORT, "HTTP port timer started\r\n" )); - - // - // Mark the timer running - // - pWebServer->bTimerRunning = TRUE; - } - else { - DEBUG (( DEBUG_ERROR | DEBUG_HTTP_PORT, - "ERROR - Failed to start HTTP port timer, Status: %r\r\n", - Status )); + if ( -1 == SocketStatus ) { + close ( *pPort ); + *pPort = -1; } } - // - // Return the operation status - // - DBG_EXIT_STATUS ( Status ); - return Status; + DEBUG (( DEBUG_SERVER_LISTEN, "Exiting WebServerListen\r\n" )); } -/** - Stop the web server port creation timer - - @param [in] pWebServer The web server control structure address. - - @retval EFI_SUCCESS The HTTP port timer is stopped - @retval Other Failed to stop the HTTP port timer - -**/ -EFI_STATUS -WebServerTimerStop ( - IN DT_WEB_SERVER * pWebServer - ) -{ - EFI_STATUS Status; - - DBG_ENTER ( ); - - // - // Assume the timer is stopped - // - Status = EFI_SUCCESS; - if ( pWebServer->bTimerRunning ) { - // - // Stop the port creation polling - // - Status = gBS->SetTimer ( pWebServer->TimerEvent, - TimerCancel, - 0 ); - if ( !EFI_ERROR ( Status )) { - DEBUG (( DEBUG_HTTP_PORT, "HTTP port timer stopped\r\n" )); - - // - // Mark the timer stopped - // - pWebServer->bTimerRunning = FALSE; - } - else { - DEBUG (( DEBUG_ERROR | DEBUG_HTTP_PORT, - "ERROR - Failed to stop HTTP port timer, Status: %r\r\n", - Status )); - } - } - - // - // Return the operation status - // - DBG_EXIT_STATUS ( Status ); - return Status; -} - /** Entry point for the web server application. @@ -776,8 +675,19 @@ main ( IN char **Argv ) { + UINT16 HttpPort; + UINTN Index; DT_WEB_SERVER * pWebServer; EFI_STATUS Status; + UINT64 TriggerTime; + + // + // Get the HTTP port + // + HttpPort = PcdGet16 ( WebServer_HttpPort ); + DEBUG (( DEBUG_HTTP_PORT, + "HTTP Port: %d\r\n", + HttpPort )); // // Create a timer event to start HTTP port @@ -789,12 +699,18 @@ main ( NULL, &pWebServer->TimerEvent ); if ( !EFI_ERROR ( Status )) { - Status = WebServerTimerStart ( pWebServer ); + TriggerTime = HTTP_PORT_POLL_DELAY * ( 1000 * 10 ); + Status = gBS->SetTimer ( pWebServer->TimerEvent, + TimerPeriodic, + TriggerTime ); if ( !EFI_ERROR ( Status )) { // // Run the web server forever // - for ( ; ; ) { + pWebServer->HttpListenPort = -1; + pWebServer->HttpListenPort6 = -1; + pWebServer->bRunning = TRUE; + do { // // Poll the network layer to create the HTTP port // for the web server. More than one attempt may @@ -802,45 +718,72 @@ main ( // the IP address and initialize the upper layers // of the network stack. // - WebServerTimer ( pWebServer ); - - // - // Add the HTTP port to the list of ports - // - Status = PortAdd ( pWebServer, pWebServer->HttpListenPort ); - if ( !EFI_ERROR ( Status )) { - // - // Poll the sockets for activity - // + if (( -1 == pWebServer->HttpListenPort ) + || ( -1 == pWebServer->HttpListenPort6 )) { do { - SocketPoll ( pWebServer ); - } while ( -1 != pWebServer->HttpListenPort ); + // + // Wait a while before polling for a connection + // + if ( EFI_SUCCESS != gBS->CheckEvent ( pWebServer->TimerEvent )) { + if ( 0 != pWebServer->Entries ) { + break; + } + gBS->WaitForEvent ( 1, &pWebServer->TimerEvent, &Index ); + } - // - // The HTTP port failed the accept and was closed - // + // + // Poll for a network connection + // + if ( -1 == pWebServer->HttpListenPort ) { + WebServerListen ( pWebServer, + AF_INET, + IPPROTO_TCP, + HttpPort, + &pWebServer->HttpListenPort ); + } + if ( -1 == pWebServer->HttpListenPort6 ) { + WebServerListen ( pWebServer, + AF_INET6, + IPPROTO_TCP, + HttpPort, + &pWebServer->HttpListenPort6 ); + } + + // + // Continue polling while both network connections are + // not present + // + } while ( 0 == pWebServer->Entries ); } // - // Close the HTTP port if necessary + // Poll the sockets for activity while both network + // connections are connected // - if ( -1 != pWebServer->HttpListenPort ) { - close ( pWebServer->HttpListenPort ); - pWebServer->HttpListenPort = -1; - } -// -// TODO: Remove the following test code -// Exit when the network connection is broken -// -break; - } + do { + SocketPoll ( pWebServer ); + } while ( pWebServer->bRunning + && ( -1 != pWebServer->HttpListenPort ) + && ( -1 != pWebServer->HttpListenPort6 )); + + // + // Continue polling the network connections until both + // TCP4 and TCP6 are connected + // + } while ( pWebServer->bRunning ); // - // Done with the timer event + // Stop the timer // - WebServerTimerStop ( pWebServer ); - Status = gBS->CloseEvent ( pWebServer->TimerEvent ); + gBS->SetTimer ( pWebServer->TimerEvent, + TimerCancel, + 0 ); } + + // + // Done with the timer event + // + gBS->CloseEvent ( pWebServer->TimerEvent ); } // diff --git a/AppPkg/Applications/Sockets/WebServer/WebServer.h b/AppPkg/Applications/Sockets/WebServer/WebServer.h index 0eee114832..a5dcea4969 100644 --- a/AppPkg/Applications/Sockets/WebServer/WebServer.h +++ b/AppPkg/Applications/Sockets/WebServer/WebServer.h @@ -86,7 +86,7 @@ #define DEBUG_SOCKET_POLL 0x00080000 ///< Display the socket poll messages #define DEBUG_PORT_WORK 0x00040000 ///< Display the port work messages -#define DEBUG_SERVER_TIMER 0x00020000 ///< Display the socket poll messages +#define DEBUG_SERVER_LISTEN 0x00020000 ///< Display the socket poll messages #define DEBUG_HTTP_PORT 0x00010000 ///< Display HTTP port related messages #define DEBUG_REQUEST 0x00008000 ///< Display the HTTP request messages @@ -173,9 +173,10 @@ typedef struct { // // HTTP port management // - BOOLEAN bTimerRunning; ///< Port creation timer status + BOOLEAN bRunning; ///< Web server running EFI_EVENT TimerEvent; ///< Timer to open HTTP port - int HttpListenPort; ///< File descriptor for the HTTP listen port + int HttpListenPort; ///< File descriptor for the HTTP listen port over TCP4 + int HttpListenPort6; ///< File descriptor for the HTTP listen port over TCP6 // // Client port management @@ -377,6 +378,23 @@ DxeServicesTablePage ( OUT BOOLEAN * pbDone ); +/** + Respond with the Exit page + + @param [in] SocketFD The socket's file descriptor to add to the list. + @param [in] pPort The WSDT_PORT structure address + @param [out] pbDone Address to receive the request completion status + + @retval EFI_SUCCESS The request was successfully processed + +**/ +EFI_STATUS +ExitPage ( + IN int SocketFD, + IN WSDT_PORT * pPort, + OUT BOOLEAN * pbDone + ); + /** Respond with the firmware status @@ -445,6 +463,23 @@ IndexPage ( OUT BOOLEAN * pbDone ); +/** + Respond with the Ports page + + @param [in] SocketFD The socket's file descriptor to add to the list. + @param [in] pPort The WSDT_PORT structure address + @param [out] pbDone Address to receive the request completion status + + @retval EFI_SUCCESS The request was successfully processed + +**/ +EFI_STATUS +PortsPage ( + IN int SocketFD, + IN WSDT_PORT * pPort, + OUT BOOLEAN * pbDone + ); + /** Page to reboot the system @@ -723,7 +758,7 @@ EFI_STATUS HttpSendIpAddress ( IN int SocketFD, IN WSDT_PORT * pPort, - IN struct sockaddr_in * pAddress + IN struct sockaddr_in6 * pAddress ); /** diff --git a/AppPkg/Applications/Sockets/WebServer/WebServer.inf b/AppPkg/Applications/Sockets/WebServer/WebServer.inf index 308e4bfff2..c250007295 100644 --- a/AppPkg/Applications/Sockets/WebServer/WebServer.inf +++ b/AppPkg/Applications/Sockets/WebServer/WebServer.inf @@ -8,7 +8,7 @@ # license agreement # # -# Copyright (c) 20011 Intel Corporation. All rights reserved +# Copyright (c) 2011 Intel Corporation. All rights reserved # This software and associated documentation (if any) is furnished # under a license and may only be used or copied in accordance # with the terms of the license. Except as permitted by such @@ -40,12 +40,14 @@ ConfigurationTable.c DhcpOptions.c DxeServicesTable.c + Exit.c Firmware.c Handles.c Hello.c HTTP.c Index.c PageList.c + Ports.c Reboot.c RuntimeServicesTable.c SystemTable.c @@ -53,10 +55,10 @@ [Pcd] - gStdLibTokenSpaceGuid.WebServer_HttpPort - + gAppPkgTokenSpaceGuid.WebServer_HttpPort [Packages] + AppPkg/AppPkg.dec MdePkg/MdePkg.dec MdeModulePkg/MdeModulePkg.dec ShellPkg/ShellPkg.dec @@ -68,14 +70,14 @@ BsdSocketLib DebugLib DevShell -# EfiSocketLib + EfiSocketLib LibC ShellLib ShellCEntryLib UefiBootServicesTableLib UefiLib UefiRuntimeServicesTableLib - UseSocketDxe +# UseSocketDxe [Guids] gEfiAcpi10TableGuid