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