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