]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Sockets/RawIp4Rx/RawIp4Rx.c
Update the sockets applications
[mirror_edk2.git] / AppPkg / Applications / Sockets / RawIp4Rx / RawIp4Rx.c
CommitLineData
59bc0593 1/** @file\r
2 Raw IP4 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 "RawIp4Rx.h"\r
16\r
17UINT8 mBuffer[65536];\r
18\r
19\r
20/**\r
21 Run the raw IP4 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
29RawIp4Rx (\r
30 IN int ArgC,\r
31 IN char **ArgV\r
32 )\r
33{\r
34 ssize_t BytesReceived;\r
35 struct sockaddr_in LocalPort;\r
36 socklen_t LocalPortLength;\r
37 struct sockaddr_in RemotePort;\r
38 socklen_t RemotePortLength;\r
39 int RetVal;\r
40 SOCKET s;\r
41 UINT64 TotalBytesReceived;\r
42\r
43 //\r
44 // Create the socket\r
45 //\r
46 s = socket ( AF_INET, SOCK_RAW, RAW_PROTOCOL );\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 // Bind the socket to a known port\r
58 //\r
59 memset ( &LocalPort, 0, sizeof ( LocalPort ));\r
60 SIN_LEN ( LocalPort ) = sizeof ( LocalPort );\r
61 SIN_FAMILY ( LocalPort ) = AF_INET;\r
62 SIN_ADDR ( LocalPort ) = 0;\r
63 SIN_PORT ( LocalPort ) = 0;\r
64 RetVal = bind ( s,\r
65 (struct sockaddr *)&LocalPort,\r
66 sizeof ( LocalPort ));\r
67 if ( -1 == RetVal ) {\r
68 RetVal = GET_ERRNO;\r
69 printf ( "ERROR - bind error, errno: %d\r\n", RetVal );\r
70 break;\r
71 }\r
72\r
73 //\r
74 // Display the local address and protocol\r
75 //\r
76 LocalPortLength = sizeof ( LocalPort );\r
77 RetVal = getsockname ( s, (struct sockaddr *)&LocalPort, &LocalPortLength );\r
78 if ( 0 != RetVal ) {\r
79 RetVal = GET_ERRNO;\r
80 printf ( "ERROR - getsockname error, errno: %d\r\n", RetVal );\r
81 break;\r
82 }\r
83 printf ( "Local Address: %d.%d.%d.%d, Protocol: %d\r\n",\r
84 (UINT8)SIN_ADDR ( LocalPort ),\r
85 (UINT8)( SIN_ADDR ( LocalPort ) >> 8 ),\r
86 (UINT8)( SIN_ADDR ( LocalPort ) >> 16 ),\r
87 (UINT8)( SIN_ADDR ( LocalPort ) >> 24 ),\r
88 RAW_PROTOCOL );\r
89\r
90 //\r
91 // Use for/break instead of goto\r
92 //\r
93 TotalBytesReceived = 0;\r
94 for ( ; ; ) {\r
95 //\r
96 // Receive data from the remote system\r
97 //\r
98 do {\r
99 //\r
100 // Attempt to receive a packet\r
101 //\r
102 RemotePortLength = sizeof ( RemotePort );\r
103 BytesReceived = recvfrom ( s,\r
104 &mBuffer[0],\r
105 sizeof ( mBuffer ),\r
106 0,\r
107 (struct sockaddr *)&RemotePort,\r
108 &RemotePortLength );\r
109 RetVal = (UINT32)BytesReceived;\r
110 if ( 0 < BytesReceived ) {\r
111 //\r
112 // Display the received data\r
113 //\r
114 printf ( "%4d bytes received from %d.%d.%d.%d:%d\r\n"\r
115 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\r\n"\r
116 "%02x %02x %02x %02x\r\n",\r
117 (UINT32)BytesReceived,\r
118 (UINT8)SIN_ADDR ( RemotePort ),\r
119 (UINT8)( SIN_ADDR ( RemotePort ) >> 8 ),\r
120 (UINT8)( SIN_ADDR ( RemotePort ) >> 16 ),\r
121 (UINT8)( SIN_ADDR ( RemotePort ) >> 24 ),\r
122 SIN_PORT ( RemotePort ),\r
123 mBuffer[0],\r
124 mBuffer[1],\r
125 mBuffer[2],\r
126 mBuffer[3],\r
127 mBuffer[4],\r
128 mBuffer[5],\r
129 mBuffer[6],\r
130 mBuffer[7],\r
131 mBuffer[8],\r
132 mBuffer[9],\r
133 mBuffer[10],\r
134 mBuffer[11],\r
135 mBuffer[12],\r
136 mBuffer[13],\r
137 mBuffer[14],\r
138 mBuffer[15],\r
139 mBuffer[16],\r
140 mBuffer[17],\r
141 mBuffer[18],\r
142 mBuffer[19]);\r
143 TotalBytesReceived += BytesReceived;\r
144\r
145 //\r
146 // All done when the correct packet is received\r
147 //\r
148 if ( mBuffer[9] == RAW_PROTOCOL ) {\r
149 break;\r
150 }\r
151 }\r
152 else if ( -1 == BytesReceived ) {\r
153 //\r
154 // Check for a timeout\r
155 //\r
156 RetVal = GET_ERRNO;\r
157 printf ( "ERROR - recv error, errno: %d\r\n", RetVal );\r
158 break;\r
159 }\r
160 } while ( 0 != RetVal );\r
161\r
162 //\r
163 // Display the bytes received\r
164 //\r
165 if ( 0 == RetVal ) {\r
166 printf ( "Total Bytes Received: %Ld\r\n", TotalBytesReceived );\r
167 }\r
168\r
169 //\r
170 // Test complete\r
171 //\r
172 break;\r
173 }\r
174 break;\r
175 }\r
176\r
177 //\r
178 // Close the socket\r
179 //\r
180 CLOSE_SOCKET ( s );\r
181 printf ( "Socket closed\r\n" );\r
182 }\r
183\r
184 //\r
185 // Return the operation status\r
186 //\r
187 return RetVal;\r
188}\r