]> git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Sockets/OobRx/OobRx.c
28e10d8fc6a3a2a9b6efc402eba557c781eabdd0
[mirror_edk2.git] / AppPkg / Applications / Sockets / OobRx / OobRx.c
1 /** @file
2 Windows version of the OOB Receive 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 <OobRx.h>
10
11 UINT8 mBuffer[65536];
12
13
14 /**
15 Run the OOB receive application
16
17 @param [in] ArgC Argument count
18 @param [in] ArgV Argument value array
19
20 @retval 0 Successfully operation
21 **/
22 int
23 OobRx (
24 IN int ArgC,
25 IN char **ArgV
26 )
27 {
28 SOCKET a;
29 ssize_t BytesReceived;
30 struct sockaddr_in LocalPort;
31 UINT32 OobInLine;
32 UINT16 PortNumber;
33 struct timeval ReceiveTimeout;
34 struct sockaddr_in RemotePort;
35 socklen_t RemotePortLength;
36 int RetVal;
37 SOCKET s;
38 UINT32 TransmittedBefore;
39 UINT32 TransmittedDuring;
40 UINT32 TransmittedOob;
41 UINT32 TransmittedAfter;
42 UINT32 * pTransmittedBytes;
43
44 //
45 // Create the socket
46 //
47 s = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );
48 if ( -1 == s ) {
49 RetVal = GET_ERRNO;
50 printf ( "ERROR - socket error, errno: %d\r\n", RetVal );
51 }
52 else {
53 //
54 // Use for/break; instead of goto
55 //
56 for ( ; ; ) {
57 //
58 // Bind the socket to a known port
59 //
60 PortNumber = OOB_RX_PORT;
61 memset ( &LocalPort, 0, sizeof ( LocalPort ));
62 SIN_LEN ( LocalPort ) = sizeof ( LocalPort );
63 SIN_FAMILY ( LocalPort ) = AF_INET;
64 SIN_ADDR ( LocalPort ) = 0;
65 SIN_PORT ( LocalPort ) = htons ( PortNumber );
66 RetVal = bind ( s,
67 (struct sockaddr *)&LocalPort,
68 sizeof ( LocalPort ));
69 if ( -1 == RetVal ) {
70 RetVal = GET_ERRNO;
71 printf ( "ERROR - bind error, errno: %d\r\n", RetVal );
72 break;
73 }
74
75 //
76 // Make the port available on the server
77 //
78 RetVal = listen ( s, 2 );
79 if ( -1 == RetVal ) {
80 RetVal = GET_ERRNO;
81 printf ( "ERROR - listen error, errno: %d\r\n", RetVal );
82 break;
83 }
84
85 //
86 // Wait for a connection to the known port
87 //
88 RemotePortLength = sizeof ( RemotePort );
89 a = accept ( s,
90 (struct sockaddr *)&RemotePort,
91 &RemotePortLength );
92 if ( -1 == a ) {
93 RetVal = GET_ERRNO;
94 printf ( "ERROR - accept error, errno: %d\r\n", RetVal );
95 break;
96 }
97
98 //
99 // Use for/break instead of goto
100 //
101 for ( ; ; ) {
102 //
103 // Set the receive timeout
104 //
105 ReceiveTimeout.tv_sec = 0;
106 ReceiveTimeout.tv_usec = 20 * 1000;
107 RetVal = setsockopt ( a,
108 SOL_SOCKET,
109 SO_RCVTIMEO,
110 (char *)&ReceiveTimeout,
111 sizeof ( ReceiveTimeout ));
112 if ( -1 == RetVal ) {
113 RetVal = GET_ERRNO;
114 printf ( "ERROR - setsockopt RCVTIMEO error, errno: %d\r\n", RetVal );
115 break;
116 }
117
118 //
119 // Select the OOB processing
120 //
121 OobInLine = ( 1 < ArgC );
122 RetVal = setsockopt ( s,
123 SOL_SOCKET,
124 SO_OOBINLINE,
125 (char *)&OobInLine,
126 sizeof ( OobInLine ));
127 if ( -1 == RetVal ) {
128 RetVal = GET_ERRNO;
129 printf ( "ERROR - setsockopt OOBINLINE error, errno: %d\r\n", RetVal );
130 break;
131 }
132 printf ( "%s\r\n", ( 0 != OobInLine ) ? "OOB messages are in-line"
133 : "OOB messages move to the head of the queue" );
134
135 //
136 // Receive data from the remote system
137 //
138 TransmittedBefore = 0;
139 TransmittedOob = 0;
140 TransmittedDuring = 0;
141 TransmittedAfter = 0;
142 pTransmittedBytes = &TransmittedBefore;
143 do {
144 //
145 // Attempt to receive OOB data
146 //
147 BytesReceived = recv ( a, &mBuffer[0], sizeof ( mBuffer ), MSG_OOB );
148 RetVal = (UINT32)BytesReceived;
149 if ( 0 < BytesReceived ) {
150 //
151 // Display the received OOB data
152 //
153 printf ( "%5Ld OOB bytes received\r\n", (UINT64)BytesReceived );
154
155 //
156 // Account for the bytes received
157 //
158 TransmittedOob += RetVal;
159 *pTransmittedBytes += TransmittedAfter;
160 TransmittedAfter = 0;
161 pTransmittedBytes = &TransmittedDuring;
162 }
163 else if ( -1 == BytesReceived ) {
164 //
165 // Check for connection timeout
166 //
167 RetVal = GET_ERRNO;
168 if ( RX_TIMEOUT_ERROR != RetVal ) {
169 //
170 // Receive error
171 //
172 printf ( "ERROR - recv OOB error, errno: %d\r\n", RetVal );
173 break;
174 }
175
176 //
177 // Ignore the timeout
178 // Try to receive normal data instead
179 //
180 BytesReceived = recv ( a, &mBuffer[0], sizeof ( mBuffer ), 0 );
181 RetVal = (UINT32)BytesReceived;
182 if ( 0 < BytesReceived ) {
183 //
184 // Display the received data
185 //
186 printf ( "%4Ld bytes received\r\n", (UINT64)BytesReceived );
187
188 //
189 // Account for the bytes received
190 //
191 TransmittedAfter += RetVal;
192 }
193 else if ( -1 == BytesReceived ) {
194 //
195 // Check for a timeout
196 //
197 RetVal = GET_ERRNO;
198 if ( RX_TIMEOUT_ERROR != RetVal ) {
199 printf ( "ERROR - recv error, errno: %d\r\n", RetVal );
200 break;
201 }
202 }
203 }
204 } while ( 0 != RetVal );
205
206 //
207 // Display the bytes received
208 //
209 if ( 0 == RetVal ) {
210 printf ( "Bytes before OOB: %8d\r\n", TransmittedBefore );
211 if ( 0 != TransmittedDuring ) {
212 printf ( "Bytes during OOB: %8d\r\n", TransmittedDuring );
213 }
214 printf ( "Out-of-band bytes: %8d\r\n", TransmittedOob );
215 printf ( "Bytes after OOB: %8d\r\n", TransmittedAfter );
216 printf ( " --------\r\n" );
217 printf ( "Total Bytes: %8d\r\n", TransmittedBefore
218 + TransmittedDuring
219 + TransmittedOob
220 + TransmittedAfter );
221 }
222
223 //
224 // Test complete
225 //
226 break;
227 }
228
229 //
230 // Close the test socket
231 //
232 CLOSE_SOCKET ( a );
233 break;
234 }
235
236 //
237 // Close the socket
238 //
239 CLOSE_SOCKET ( s );
240 printf ( "Socket closed\r\n" );
241 }
242
243 //
244 // Return the operation status
245 //
246 return RetVal;
247 }