]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Sockets/OobTx/OobTx.c
Update the sockets applications
[mirror_edk2.git] / AppPkg / Applications / Sockets / OobTx / OobTx.c
diff --git a/AppPkg/Applications/Sockets/OobTx/OobTx.c b/AppPkg/Applications/Sockets/OobTx/OobTx.c
new file mode 100644 (file)
index 0000000..d52cbb7
--- /dev/null
@@ -0,0 +1,248 @@
+/** @file\r
+  Windows version of the OOB Transmit application\r
+\r
+  Copyright (c) 2011, Intel Corporation\r
+  All rights reserved. This program and the accompanying materials\r
+  are licensed and made available under the terms and conditions of the BSD License\r
+  which accompanies this distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <OobTx.h>\r
+\r
+UINT8 mBuffer[8192];\r
+UINT8 mOob[512];\r
+\r
+/**\r
+  Transmit out-of-band messages to the remote system.\r
+\r
+  @param [in] ArgC        Argument count\r
+  @param [in] ArgV        Argument value array\r
+\r
+  @retval 0               Successfully operation\r
+ **/\r
+\r
+int\r
+OobTx (\r
+  IN int ArgC,\r
+  IN char **ArgV\r
+  )\r
+{\r
+  UINT32 BytesSent;\r
+  ssize_t BytesTransmitted;\r
+  UINT32 Index;\r
+  struct sockaddr_in LocalPort;\r
+  UINT32 OobInLine;\r
+  UINT16 PortNumber;\r
+  UINT32 RemoteAddress[4];\r
+  struct sockaddr_in RemotePort;\r
+  int RetVal;\r
+  UINT32 TransmittedAfter;\r
+  UINT32 TransmittedBefore;\r
+  UINT32 TransmittedOob;\r
+  SOCKET s;\r
+\r
+  //\r
+  //  Create the socket\r
+  //\r
+  s = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );\r
+  if ( -1 == s ) {\r
+    RetVal = GET_ERRNO;\r
+    printf ( "ERROR - socket error, errno: %d\r\n", RetVal );\r
+  }\r
+  else {\r
+    //\r
+    //  Use for/break; instead of goto\r
+    //\r
+    for ( ; ; ) {\r
+      //\r
+      //  Validate the arguments\r
+      //\r
+      if (( 2 > ArgC )\r
+        || ( 4 != sscanf ( ArgV[1],\r
+                           "%d.%d.%d.%d",\r
+                           &RemoteAddress[0],\r
+                           &RemoteAddress[1],\r
+                           &RemoteAddress[2],\r
+                           &RemoteAddress[3]))\r
+          || ( 224 < RemoteAddress[0])\r
+          || ( 255 < RemoteAddress[1])\r
+          || ( 255 < RemoteAddress[2])\r
+          || ( 255 < RemoteAddress[3])\r
+          || (( 0 == RemoteAddress[0])\r
+              && ( 0 == RemoteAddress[1])\r
+              && ( 0 == RemoteAddress[2])\r
+              && ( 0 == RemoteAddress[3]))) {\r
+        printf ( "%s  <remote IP address>  [optional: enables in-line OOB]\r\n", ArgV[0]);\r
+        RetVal = EINVAL;\r
+        break;\r
+      }\r
+\r
+      //\r
+      //  Bind the socket to a local port\r
+      //\r
+      memset ( &LocalPort, 0, sizeof ( LocalPort ));\r
+      SIN_LEN ( LocalPort ) = sizeof ( LocalPort );\r
+      SIN_FAMILY ( LocalPort ) = AF_INET;\r
+      SIN_ADDR ( LocalPort ) = 0;\r
+      SIN_PORT ( LocalPort ) = 0;\r
+      RetVal = bind ( s,\r
+                      (struct sockaddr *)&LocalPort,\r
+                      sizeof ( LocalPort ));\r
+      if ( -1 == RetVal ) {\r
+        RetVal = GET_ERRNO;\r
+        printf ( "ERROR - bind error, errno: %d\r\n", RetVal );\r
+        break;\r
+      }\r
+\r
+      //\r
+      //  Specify the remote port\r
+      //\r
+      PortNumber = OOB_RX_PORT;\r
+      memset ( &RemotePort, 0, sizeof ( RemotePort ));\r
+      SIN_LEN ( RemotePort ) = sizeof ( RemotePort );\r
+      SIN_FAMILY ( RemotePort ) = AF_INET;\r
+      SIN_ADDR ( RemotePort ) = ( RemoteAddress[3] << 24 )\r
+                              | ( RemoteAddress[2] << 16 )\r
+                              | ( RemoteAddress[1] << 8 )\r
+                              | RemoteAddress[0];\r
+      SIN_PORT ( RemotePort ) = htons ( PortNumber );\r
+\r
+      //\r
+      //  Connect to the remote server\r
+      //\r
+      RetVal = connect ( s, (struct sockaddr *)&RemotePort, sizeof ( RemotePort ));\r
+      if ( -1 == RetVal ) {\r
+        RetVal = GET_ERRNO;\r
+        printf ( "ERROR - connect error, errno: %d\r\n", RetVal );\r
+        break;\r
+      }\r
+\r
+      //\r
+      //  Select the OOB processing\r
+      //\r
+      OobInLine = ( 2 < ArgC );\r
+      RetVal = setsockopt ( s,\r
+                            SOL_SOCKET,\r
+                            SO_OOBINLINE,\r
+                            (char *)&OobInLine,\r
+                            sizeof ( OobInLine ));\r
+      if ( -1 == RetVal ) {\r
+        RetVal = GET_ERRNO;\r
+        printf ( "ERROR - setsockopt OOBINLINE error, errno: %d\r\n", RetVal );\r
+        break;\r
+      }\r
+      printf ( "%s\r\n", ( 0 != OobInLine ) ? "OOB messages are in-line"\r
+                                            : "OOB messages move to the head of the queue" );\r
+\r
+      //\r
+      //  Initialize the messages\r
+      //\r
+      memset ( &mBuffer[0], 0, sizeof ( mBuffer ));\r
+      memset ( &mOob[0], 0x11, sizeof ( mOob ));\r
+\r
+      //\r
+      //  Send the data before the out-of-band message\r
+      //\r
+      TransmittedBefore = 0;\r
+      for ( Index = 0; TX_MSGS_BEFORE > Index; Index++ ) {\r
+        BytesSent = 0;\r
+        do {\r
+          BytesTransmitted = send ( s,\r
+                                    &mBuffer[BytesSent],\r
+                                    sizeof ( mBuffer ) - BytesSent,\r
+                                    0 );\r
+          if ( -1 == BytesTransmitted ) {\r
+            RetVal = GET_ERRNO;\r
+            printf ( "ERROR - send before error, errno: %d\r\n", RetVal );\r
+            break;\r
+          }\r
+          BytesSent += (UINT32)BytesTransmitted;\r
+          RetVal = 0;\r
+        } while ( sizeof ( mBuffer ) > BytesSent );\r
+        if ( 0 != RetVal ) {\r
+          break;\r
+        }\r
+        TransmittedBefore += BytesSent;\r
+      }\r
+      if ( 0 != RetVal ) {\r
+        break;\r
+      }\r
+\r
+      //\r
+      //  Send the out-of-band message\r
+      //\r
+      BytesSent = 0;\r
+      do {\r
+        BytesTransmitted = send ( s,\r
+                                  &mOob[BytesSent],\r
+                                  sizeof ( mOob ) - BytesSent,\r
+                                  MSG_OOB );\r
+        if ( -1 == BytesTransmitted ) {\r
+          RetVal = GET_ERRNO;\r
+          printf ( "ERROR - send OOB error, errno: %d\r\n", RetVal );\r
+          break;\r
+        }\r
+        BytesSent += (UINT32)BytesTransmitted;\r
+        RetVal = 0;\r
+      } while ( sizeof ( mOob ) > BytesSent );\r
+      if ( 0 != RetVal ) {\r
+        break;\r
+      }\r
+      TransmittedOob = BytesSent;\r
+\r
+      //\r
+      //  Send the data after the out-of-band message\r
+      //\r
+      TransmittedAfter = 0;\r
+      for ( Index = 0; TX_MSGS_AFTER > Index; Index++ ) {\r
+        BytesSent = 0;\r
+        do {\r
+          BytesTransmitted = send ( s,\r
+                                    &mBuffer[BytesSent],\r
+                                    sizeof ( mBuffer ) - BytesSent,\r
+                                    0 );\r
+          if ( -1 == BytesTransmitted ) {\r
+            RetVal = GET_ERRNO;\r
+            printf ( "ERROR - send after error, errno: %d\r\n", RetVal );\r
+            break;\r
+          }\r
+          BytesSent += (UINT32)BytesTransmitted;\r
+          RetVal = 0;\r
+        } while ( sizeof ( mBuffer ) > BytesSent );\r
+        if ( 0 != RetVal ) {\r
+          break;\r
+        }\r
+        TransmittedAfter += BytesSent;\r
+      }\r
+\r
+      //\r
+      //  Test completed successfully\r
+      //\r
+      if ( 0 == RetVal ) {\r
+        printf ( "Bytes before OOB:  %8d\r\n", TransmittedBefore );\r
+        printf ( "Out-of-band bytes: %8d\r\n", TransmittedOob );\r
+        printf ( "Bytes after OOB:   %8d\r\n", TransmittedAfter );\r
+        printf ( "                   --------\r\n" );\r
+        printf ( "Total Bytes:       %8d\r\n", TransmittedBefore\r
+                                               + TransmittedOob\r
+                                               + TransmittedAfter );\r
+      }\r
+      break;\r
+    }\r
+\r
+    //\r
+    //  Close the socket\r
+    //\r
+    CLOSE_SOCKET ( s );\r
+  }\r
+\r
+  //\r
+  //  Return the operation status\r
+  //\r
+  return RetVal;\r
+}\r