]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Sockets/OobTx/OobTx.c
BaseTools/BinToPcd: Fix Python 2.7.x compatibility issue
[mirror_edk2.git] / AppPkg / Applications / Sockets / OobTx / OobTx.c
CommitLineData
59bc0593 1/** @file\r
2 Windows version of the OOB Transmit application\r
3\r
9f7f5161 4 Copyright (c) 2011-2012, Intel Corporation\r
59bc0593 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 <OobTx.h>\r
16\r
17UINT8 mBuffer[8192];\r
18UINT8 mOob[512];\r
19\r
20/**\r
21 Transmit out-of-band messages to the remote system.\r
22\r
23 @param [in] ArgC Argument count\r
24 @param [in] ArgV Argument value array\r
25\r
26 @retval 0 Successfully operation\r
27 **/\r
28\r
29int\r
30OobTx (\r
31 IN int ArgC,\r
32 IN char **ArgV\r
33 )\r
34{\r
35 UINT32 BytesSent;\r
36 ssize_t BytesTransmitted;\r
37 UINT32 Index;\r
38 struct sockaddr_in LocalPort;\r
39 UINT32 OobInLine;\r
40 UINT16 PortNumber;\r
41 UINT32 RemoteAddress[4];\r
42 struct sockaddr_in RemotePort;\r
43 int RetVal;\r
44 UINT32 TransmittedAfter;\r
45 UINT32 TransmittedBefore;\r
46 UINT32 TransmittedOob;\r
47 SOCKET s;\r
48\r
49 //\r
50 // Create the socket\r
51 //\r
52 s = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );\r
53 if ( -1 == s ) {\r
54 RetVal = GET_ERRNO;\r
55 printf ( "ERROR - socket error, errno: %d\r\n", RetVal );\r
56 }\r
57 else {\r
58 //\r
59 // Use for/break; instead of goto\r
60 //\r
61 for ( ; ; ) {\r
62 //\r
63 // Validate the arguments\r
64 //\r
65 if (( 2 > ArgC )\r
66 || ( 4 != sscanf ( ArgV[1],\r
67 "%d.%d.%d.%d",\r
68 &RemoteAddress[0],\r
69 &RemoteAddress[1],\r
70 &RemoteAddress[2],\r
71 &RemoteAddress[3]))\r
72 || ( 224 < RemoteAddress[0])\r
73 || ( 255 < RemoteAddress[1])\r
74 || ( 255 < RemoteAddress[2])\r
75 || ( 255 < RemoteAddress[3])\r
76 || (( 0 == RemoteAddress[0])\r
77 && ( 0 == RemoteAddress[1])\r
78 && ( 0 == RemoteAddress[2])\r
79 && ( 0 == RemoteAddress[3]))) {\r
80 printf ( "%s <remote IP address> [optional: enables in-line OOB]\r\n", ArgV[0]);\r
81 RetVal = EINVAL;\r
82 break;\r
83 }\r
84\r
85 //\r
86 // Bind the socket to a local port\r
87 //\r
88 memset ( &LocalPort, 0, sizeof ( LocalPort ));\r
89 SIN_LEN ( LocalPort ) = sizeof ( LocalPort );\r
90 SIN_FAMILY ( LocalPort ) = AF_INET;\r
91 SIN_ADDR ( LocalPort ) = 0;\r
92 SIN_PORT ( LocalPort ) = 0;\r
93 RetVal = bind ( s,\r
94 (struct sockaddr *)&LocalPort,\r
95 sizeof ( LocalPort ));\r
96 if ( -1 == RetVal ) {\r
97 RetVal = GET_ERRNO;\r
98 printf ( "ERROR - bind error, errno: %d\r\n", RetVal );\r
99 break;\r
100 }\r
101\r
102 //\r
103 // Specify the remote port\r
104 //\r
105 PortNumber = OOB_RX_PORT;\r
106 memset ( &RemotePort, 0, sizeof ( RemotePort ));\r
107 SIN_LEN ( RemotePort ) = sizeof ( RemotePort );\r
108 SIN_FAMILY ( RemotePort ) = AF_INET;\r
109 SIN_ADDR ( RemotePort ) = ( RemoteAddress[3] << 24 )\r
110 | ( RemoteAddress[2] << 16 )\r
111 | ( RemoteAddress[1] << 8 )\r
112 | RemoteAddress[0];\r
113 SIN_PORT ( RemotePort ) = htons ( PortNumber );\r
114\r
115 //\r
116 // Connect to the remote server\r
117 //\r
118 RetVal = connect ( s, (struct sockaddr *)&RemotePort, sizeof ( RemotePort ));\r
119 if ( -1 == RetVal ) {\r
120 RetVal = GET_ERRNO;\r
121 printf ( "ERROR - connect error, errno: %d\r\n", RetVal );\r
122 break;\r
123 }\r
124\r
125 //\r
126 // Select the OOB processing\r
127 //\r
128 OobInLine = ( 2 < ArgC );\r
129 RetVal = setsockopt ( s,\r
130 SOL_SOCKET,\r
131 SO_OOBINLINE,\r
132 (char *)&OobInLine,\r
133 sizeof ( OobInLine ));\r
134 if ( -1 == RetVal ) {\r
135 RetVal = GET_ERRNO;\r
136 printf ( "ERROR - setsockopt OOBINLINE error, errno: %d\r\n", RetVal );\r
137 break;\r
138 }\r
139 printf ( "%s\r\n", ( 0 != OobInLine ) ? "OOB messages are in-line"\r
140 : "OOB messages move to the head of the queue" );\r
141\r
142 //\r
143 // Initialize the messages\r
144 //\r
145 memset ( &mBuffer[0], 0, sizeof ( mBuffer ));\r
146 memset ( &mOob[0], 0x11, sizeof ( mOob ));\r
147\r
148 //\r
149 // Send the data before the out-of-band message\r
150 //\r
151 TransmittedBefore = 0;\r
152 for ( Index = 0; TX_MSGS_BEFORE > Index; Index++ ) {\r
153 BytesSent = 0;\r
154 do {\r
155 BytesTransmitted = send ( s,\r
156 &mBuffer[BytesSent],\r
157 sizeof ( mBuffer ) - BytesSent,\r
158 0 );\r
159 if ( -1 == BytesTransmitted ) {\r
160 RetVal = GET_ERRNO;\r
161 printf ( "ERROR - send before error, errno: %d\r\n", RetVal );\r
162 break;\r
163 }\r
164 BytesSent += (UINT32)BytesTransmitted;\r
165 RetVal = 0;\r
166 } while ( sizeof ( mBuffer ) > BytesSent );\r
167 if ( 0 != RetVal ) {\r
168 break;\r
169 }\r
170 TransmittedBefore += BytesSent;\r
171 }\r
172 if ( 0 != RetVal ) {\r
173 break;\r
174 }\r
175\r
176 //\r
177 // Send the out-of-band message\r
178 //\r
179 BytesSent = 0;\r
180 do {\r
181 BytesTransmitted = send ( s,\r
182 &mOob[BytesSent],\r
183 sizeof ( mOob ) - BytesSent,\r
184 MSG_OOB );\r
185 if ( -1 == BytesTransmitted ) {\r
186 RetVal = GET_ERRNO;\r
187 printf ( "ERROR - send OOB error, errno: %d\r\n", RetVal );\r
188 break;\r
189 }\r
190 BytesSent += (UINT32)BytesTransmitted;\r
191 RetVal = 0;\r
192 } while ( sizeof ( mOob ) > BytesSent );\r
193 if ( 0 != RetVal ) {\r
194 break;\r
195 }\r
196 TransmittedOob = BytesSent;\r
197\r
198 //\r
199 // Send the data after the out-of-band message\r
200 //\r
201 TransmittedAfter = 0;\r
202 for ( Index = 0; TX_MSGS_AFTER > Index; Index++ ) {\r
203 BytesSent = 0;\r
204 do {\r
205 BytesTransmitted = send ( s,\r
206 &mBuffer[BytesSent],\r
207 sizeof ( mBuffer ) - BytesSent,\r
208 0 );\r
209 if ( -1 == BytesTransmitted ) {\r
210 RetVal = GET_ERRNO;\r
211 printf ( "ERROR - send after error, errno: %d\r\n", RetVal );\r
212 break;\r
213 }\r
214 BytesSent += (UINT32)BytesTransmitted;\r
215 RetVal = 0;\r
216 } while ( sizeof ( mBuffer ) > BytesSent );\r
217 if ( 0 != RetVal ) {\r
218 break;\r
219 }\r
220 TransmittedAfter += BytesSent;\r
221 }\r
222\r
223 //\r
224 // Test completed successfully\r
225 //\r
226 if ( 0 == RetVal ) {\r
227 printf ( "Bytes before OOB: %8d\r\n", TransmittedBefore );\r
228 printf ( "Out-of-band bytes: %8d\r\n", TransmittedOob );\r
229 printf ( "Bytes after OOB: %8d\r\n", TransmittedAfter );\r
230 printf ( " --------\r\n" );\r
231 printf ( "Total Bytes: %8d\r\n", TransmittedBefore\r
232 + TransmittedOob\r
233 + TransmittedAfter );\r
234 }\r
235 break;\r
236 }\r
237\r
238 //\r
239 // Close the socket\r
240 //\r
241 CLOSE_SOCKET ( s );\r
242 }\r
243\r
244 //\r
245 // Return the operation status\r
246 //\r
247 return RetVal;\r
248}\r