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