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