Update the sockets applications
[mirror_edk2.git] / AppPkg / Applications / Sockets / RawIp4Tx / RawIp4Tx.c
CommitLineData
59bc0593 1/** @file\r
2 Raw IP4 transmit application\r
3\r
4 Copyright (c) 2011, Intel Corporation\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "RawIp4Tx.h"\r
16\r
17UINT8 mBuffer[1024];\r
18\r
19/**\r
20 Transmit raw IP4 packets to the remote system.\r
21\r
22 @param [in] ArgC Argument count\r
23 @param [in] ArgV Argument value array\r
24\r
25 @retval 0 Successfully operation\r
26 **/\r
27\r
28int\r
29RawIp4Tx (\r
30 IN int ArgC,\r
31 IN char **ArgV\r
32 )\r
33{\r
34 UINT32 BytesSent;\r
35 ssize_t BytesTransmitted;\r
36 struct sockaddr_in LocalPort;\r
37 UINT32 RemoteAddress[4];\r
38 struct sockaddr_in RemotePort;\r
39 int RetVal;\r
40 UINT32 TotalSent;\r
41 SOCKET s;\r
42\r
43 //\r
44 // Create the socket\r
45 //\r
46 s = socket ( AF_INET, SOCK_RAW, RAW_PROTOCOL );\r
47 if ( -1 == s ) {\r
48 RetVal = GET_ERRNO;\r
49 printf ( "ERROR - socket error, errno: %d\r\n", RetVal );\r
50 }\r
51 else {\r
52 //\r
53 // Use for/break; instead of goto\r
54 //\r
55 for ( ; ; ) {\r
56 //\r
57 // Validate the arguments\r
58 //\r
59 if (( 2 > ArgC )\r
60 || ( 4 != sscanf ( ArgV[1],\r
61 "%d.%d.%d.%d",\r
62 &RemoteAddress[0],\r
63 &RemoteAddress[1],\r
64 &RemoteAddress[2],\r
65 &RemoteAddress[3]))\r
66 || ( 224 < RemoteAddress[0])\r
67 || ( 255 < RemoteAddress[1])\r
68 || ( 255 < RemoteAddress[2])\r
69 || ( 255 < RemoteAddress[3])\r
70 || (( 0 == RemoteAddress[0])\r
71 && ( 0 == RemoteAddress[1])\r
72 && ( 0 == RemoteAddress[2])\r
73 && ( 0 == RemoteAddress[3]))) {\r
74 printf ( "%s <remote IP address>\r\n", ArgV[0]);\r
75 RetVal = EINVAL;\r
76 break;\r
77 }\r
78\r
79 //\r
80 // Bind the socket to a local port\r
81 //\r
82 memset ( &LocalPort, 0, sizeof ( LocalPort ));\r
83 SIN_LEN ( LocalPort ) = sizeof ( LocalPort );\r
84 SIN_FAMILY ( LocalPort ) = AF_INET;\r
85 SIN_ADDR ( LocalPort ) = 0;\r
86 SIN_PORT ( LocalPort ) = 0;\r
87 RetVal = bind ( s,\r
88 (struct sockaddr *)&LocalPort,\r
89 sizeof ( LocalPort ));\r
90 if ( -1 == RetVal ) {\r
91 RetVal = GET_ERRNO;\r
92 printf ( "ERROR - bind error, errno: %d\r\n", RetVal );\r
93 break;\r
94 }\r
95\r
96 //\r
97 // Specify the remote port\r
98 //\r
99 memset ( &RemotePort, 0, sizeof ( RemotePort ));\r
100 SIN_LEN ( RemotePort ) = sizeof ( RemotePort );\r
101 SIN_FAMILY ( RemotePort ) = AF_INET;\r
102 SIN_ADDR ( RemotePort ) = ( RemoteAddress[3] << 24 )\r
103 | ( RemoteAddress[2] << 16 )\r
104 | ( RemoteAddress[1] << 8 )\r
105 | RemoteAddress[0];\r
106 SIN_PORT ( RemotePort ) = 0;\r
107\r
108 //\r
109 // Initialize the messages\r
110 //\r
111 memset ( &mBuffer[0], 0, sizeof ( mBuffer ));\r
112\r
113 //\r
114 // Send the data before the out-of-band message\r
115 //\r
116 TotalSent = 0;\r
117 BytesSent = 0;\r
118 do {\r
119 BytesTransmitted = sendto ( s,\r
120 &mBuffer[BytesSent],\r
121 sizeof ( mBuffer ) - BytesSent,\r
122 0,\r
123 (struct sockaddr *)&RemotePort,\r
124 sizeof ( RemotePort ));\r
125 if ( -1 == BytesTransmitted ) {\r
126 RetVal = GET_ERRNO;\r
127 printf ( "ERROR - send before error, errno: %d\r\n", RetVal );\r
128 break;\r
129 }\r
130 BytesSent += (UINT32)BytesTransmitted;\r
131 RetVal = 0;\r
132 } while ( sizeof ( mBuffer ) > BytesSent );\r
133 if ( 0 != RetVal ) {\r
134 break;\r
135 }\r
136 TotalSent += BytesSent;\r
137\r
138 //\r
139 // Test completed successfully\r
140 //\r
141 if ( 0 == RetVal ) {\r
142 printf ( "Bytes sent: %8d\r\n", TotalSent );\r
143 }\r
144 break;\r
145 }\r
146\r
147 //\r
148 // Close the socket\r
149 //\r
150 CLOSE_SOCKET ( s );\r
151 }\r
152\r
153 //\r
154 // Return the operation status\r
155 //\r
156 return RetVal;\r
157}\r