]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Sockets/RawIp4Tx/RawIp4Tx.c
Update the sockets applications
[mirror_edk2.git] / AppPkg / Applications / Sockets / RawIp4Tx / RawIp4Tx.c
diff --git a/AppPkg/Applications/Sockets/RawIp4Tx/RawIp4Tx.c b/AppPkg/Applications/Sockets/RawIp4Tx/RawIp4Tx.c
new file mode 100644 (file)
index 0000000..aea946c
--- /dev/null
@@ -0,0 +1,157 @@
+/** @file\r
+  Raw IP4 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 "RawIp4Tx.h"\r
+\r
+UINT8 mBuffer[1024];\r
+\r
+/**\r
+  Transmit raw IP4 packets 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
+RawIp4Tx (\r
+  IN int ArgC,\r
+  IN char **ArgV\r
+  )\r
+{\r
+  UINT32 BytesSent;\r
+  ssize_t BytesTransmitted;\r
+  struct sockaddr_in LocalPort;\r
+  UINT32 RemoteAddress[4];\r
+  struct sockaddr_in RemotePort;\r
+  int RetVal;\r
+  UINT32 TotalSent;\r
+  SOCKET s;\r
+\r
+  //\r
+  //  Create the socket\r
+  //\r
+  s = socket ( AF_INET, SOCK_RAW, RAW_PROTOCOL );\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>\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
+      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 ) = 0;\r
+\r
+      //\r
+      //  Initialize the messages\r
+      //\r
+      memset ( &mBuffer[0], 0, sizeof ( mBuffer ));\r
+\r
+      //\r
+      //  Send the data before the out-of-band message\r
+      //\r
+      TotalSent = 0;\r
+      BytesSent = 0;\r
+      do {\r
+        BytesTransmitted = sendto ( s,\r
+                                    &mBuffer[BytesSent],\r
+                                    sizeof ( mBuffer ) - BytesSent,\r
+                                    0,\r
+                                    (struct sockaddr *)&RemotePort,\r
+                                    sizeof ( RemotePort ));\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
+      TotalSent += BytesSent;\r
+\r
+      //\r
+      //  Test completed successfully\r
+      //\r
+      if ( 0 == RetVal ) {\r
+        printf ( "Bytes sent:  %8d\r\n", TotalSent );\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