]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Sockets/OobTx/OobTx.c
AppPkg: Replace BSD License with BSD+Patent License
[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
bcb96695
MK
4 Copyright (c) 2011-2012, Intel Corporation. All rights reserved.\r
5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
59bc0593 6\r
7**/\r
8\r
9#include <OobTx.h>\r
10\r
11UINT8 mBuffer[8192];\r
12UINT8 mOob[512];\r
13\r
14/**\r
15 Transmit out-of-band messages to the remote system.\r
16\r
17 @param [in] ArgC Argument count\r
18 @param [in] ArgV Argument value array\r
19\r
20 @retval 0 Successfully operation\r
21 **/\r
22\r
23int\r
24OobTx (\r
25 IN int ArgC,\r
26 IN char **ArgV\r
27 )\r
28{\r
29 UINT32 BytesSent;\r
30 ssize_t BytesTransmitted;\r
31 UINT32 Index;\r
32 struct sockaddr_in LocalPort;\r
33 UINT32 OobInLine;\r
34 UINT16 PortNumber;\r
35 UINT32 RemoteAddress[4];\r
36 struct sockaddr_in RemotePort;\r
37 int RetVal;\r
38 UINT32 TransmittedAfter;\r
39 UINT32 TransmittedBefore;\r
40 UINT32 TransmittedOob;\r
41 SOCKET s;\r
42\r
43 //\r
44 // Create the socket\r
45 //\r
46 s = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );\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> [optional: enables in-line OOB]\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 PortNumber = OOB_RX_PORT;\r
100 memset ( &RemotePort, 0, sizeof ( RemotePort ));\r
101 SIN_LEN ( RemotePort ) = sizeof ( RemotePort );\r
102 SIN_FAMILY ( RemotePort ) = AF_INET;\r
103 SIN_ADDR ( RemotePort ) = ( RemoteAddress[3] << 24 )\r
104 | ( RemoteAddress[2] << 16 )\r
105 | ( RemoteAddress[1] << 8 )\r
106 | RemoteAddress[0];\r
107 SIN_PORT ( RemotePort ) = htons ( PortNumber );\r
108\r
109 //\r
110 // Connect to the remote server\r
111 //\r
112 RetVal = connect ( s, (struct sockaddr *)&RemotePort, sizeof ( RemotePort ));\r
113 if ( -1 == RetVal ) {\r
114 RetVal = GET_ERRNO;\r
115 printf ( "ERROR - connect error, errno: %d\r\n", RetVal );\r
116 break;\r
117 }\r
118\r
119 //\r
120 // Select the OOB processing\r
121 //\r
122 OobInLine = ( 2 < ArgC );\r
123 RetVal = setsockopt ( s,\r
124 SOL_SOCKET,\r
125 SO_OOBINLINE,\r
126 (char *)&OobInLine,\r
127 sizeof ( OobInLine ));\r
128 if ( -1 == RetVal ) {\r
129 RetVal = GET_ERRNO;\r
130 printf ( "ERROR - setsockopt OOBINLINE error, errno: %d\r\n", RetVal );\r
131 break;\r
132 }\r
133 printf ( "%s\r\n", ( 0 != OobInLine ) ? "OOB messages are in-line"\r
134 : "OOB messages move to the head of the queue" );\r
135\r
136 //\r
137 // Initialize the messages\r
138 //\r
139 memset ( &mBuffer[0], 0, sizeof ( mBuffer ));\r
140 memset ( &mOob[0], 0x11, sizeof ( mOob ));\r
141\r
142 //\r
143 // Send the data before the out-of-band message\r
144 //\r
145 TransmittedBefore = 0;\r
146 for ( Index = 0; TX_MSGS_BEFORE > Index; Index++ ) {\r
147 BytesSent = 0;\r
148 do {\r
149 BytesTransmitted = send ( s,\r
150 &mBuffer[BytesSent],\r
151 sizeof ( mBuffer ) - BytesSent,\r
152 0 );\r
153 if ( -1 == BytesTransmitted ) {\r
154 RetVal = GET_ERRNO;\r
155 printf ( "ERROR - send before error, errno: %d\r\n", RetVal );\r
156 break;\r
157 }\r
158 BytesSent += (UINT32)BytesTransmitted;\r
159 RetVal = 0;\r
160 } while ( sizeof ( mBuffer ) > BytesSent );\r
161 if ( 0 != RetVal ) {\r
162 break;\r
163 }\r
164 TransmittedBefore += BytesSent;\r
165 }\r
166 if ( 0 != RetVal ) {\r
167 break;\r
168 }\r
169\r
170 //\r
171 // Send the out-of-band message\r
172 //\r
173 BytesSent = 0;\r
174 do {\r
175 BytesTransmitted = send ( s,\r
176 &mOob[BytesSent],\r
177 sizeof ( mOob ) - BytesSent,\r
178 MSG_OOB );\r
179 if ( -1 == BytesTransmitted ) {\r
180 RetVal = GET_ERRNO;\r
181 printf ( "ERROR - send OOB error, errno: %d\r\n", RetVal );\r
182 break;\r
183 }\r
184 BytesSent += (UINT32)BytesTransmitted;\r
185 RetVal = 0;\r
186 } while ( sizeof ( mOob ) > BytesSent );\r
187 if ( 0 != RetVal ) {\r
188 break;\r
189 }\r
190 TransmittedOob = BytesSent;\r
191\r
192 //\r
193 // Send the data after the out-of-band message\r
194 //\r
195 TransmittedAfter = 0;\r
196 for ( Index = 0; TX_MSGS_AFTER > Index; Index++ ) {\r
197 BytesSent = 0;\r
198 do {\r
199 BytesTransmitted = send ( s,\r
200 &mBuffer[BytesSent],\r
201 sizeof ( mBuffer ) - BytesSent,\r
202 0 );\r
203 if ( -1 == BytesTransmitted ) {\r
204 RetVal = GET_ERRNO;\r
205 printf ( "ERROR - send after error, errno: %d\r\n", RetVal );\r
206 break;\r
207 }\r
208 BytesSent += (UINT32)BytesTransmitted;\r
209 RetVal = 0;\r
210 } while ( sizeof ( mBuffer ) > BytesSent );\r
211 if ( 0 != RetVal ) {\r
212 break;\r
213 }\r
214 TransmittedAfter += BytesSent;\r
215 }\r
216\r
217 //\r
218 // Test completed successfully\r
219 //\r
220 if ( 0 == RetVal ) {\r
221 printf ( "Bytes before OOB: %8d\r\n", TransmittedBefore );\r
222 printf ( "Out-of-band bytes: %8d\r\n", TransmittedOob );\r
223 printf ( "Bytes after OOB: %8d\r\n", TransmittedAfter );\r
224 printf ( " --------\r\n" );\r
225 printf ( "Total Bytes: %8d\r\n", TransmittedBefore\r
226 + TransmittedOob\r
227 + TransmittedAfter );\r
228 }\r
229 break;\r
230 }\r
231\r
232 //\r
233 // Close the socket\r
234 //\r
235 CLOSE_SOCKET ( s );\r
236 }\r
237\r
238 //\r
239 // Return the operation status\r
240 //\r
241 return RetVal;\r
242}\r