1 /** @file
2 Raw IP4 receive application
3
4 Copyright (c) 2011-2012, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "RawIp4Rx.h"
16
17 UINT8 mBuffer[65536];
18
19
20 /**
21 Run the raw IP4 receive application
22
23 @param [in] ArgC Argument count
24 @param [in] ArgV Argument value array
25
26 @retval 0 Successfully operation
27 **/
28 int
RawIp4Rx(IN int ArgC,IN char ** ArgV)29 RawIp4Rx (
30 IN int ArgC,
31 IN char **ArgV
32 )
33 {
34 ssize_t BytesReceived;
35 struct sockaddr_in LocalPort;
36 socklen_t LocalPortLength;
37 struct sockaddr_in RemotePort;
38 socklen_t RemotePortLength;
39 int RetVal;
40 SOCKET s;
41 UINT64 TotalBytesReceived;
42
43 //
44 // Create the socket
45 //
46 s = socket ( AF_INET, SOCK_RAW, RAW_PROTOCOL );
47 if ( -1 == s ) {
48 RetVal = GET_ERRNO;
49 printf ( "ERROR - socket error, errno: %d\r\n", RetVal );
50 }
51 else {
52 //
53 // Use for/break; instead of goto
54 //
55 for ( ; ; ) {
56 //
57 // Bind the socket to a known port
58 //
59 memset ( &LocalPort, 0, sizeof ( LocalPort ));
60 SIN_LEN ( LocalPort ) = sizeof ( LocalPort );
61 SIN_FAMILY ( LocalPort ) = AF_INET;
62 SIN_ADDR ( LocalPort ) = 0;
63 SIN_PORT ( LocalPort ) = 0;
64 RetVal = bind ( s,
65 (struct sockaddr *)&LocalPort,
66 sizeof ( LocalPort ));
67 if ( -1 == RetVal ) {
68 RetVal = GET_ERRNO;
69 printf ( "ERROR - bind error, errno: %d\r\n", RetVal );
70 break;
71 }
72
73 //
74 // Display the local address and protocol
75 //
76 LocalPortLength = sizeof ( LocalPort );
77 RetVal = getsockname ( s, (struct sockaddr *)&LocalPort, &LocalPortLength );
78 if ( 0 != RetVal ) {
79 RetVal = GET_ERRNO;
80 printf ( "ERROR - getsockname error, errno: %d\r\n", RetVal );
81 break;
82 }
83 printf ( "Local Address: %d.%d.%d.%d, Protocol: %d\r\n",
84 (UINT8)SIN_ADDR ( LocalPort ),
85 (UINT8)( SIN_ADDR ( LocalPort ) >> 8 ),
86 (UINT8)( SIN_ADDR ( LocalPort ) >> 16 ),
87 (UINT8)( SIN_ADDR ( LocalPort ) >> 24 ),
88 RAW_PROTOCOL );
89
90 //
91 // Use for/break instead of goto
92 //
93 TotalBytesReceived = 0;
94 for ( ; ; ) {
95 //
96 // Receive data from the remote system
97 //
98 do {
99 //
100 // Attempt to receive a packet
101 //
102 RemotePortLength = sizeof ( RemotePort );
103 BytesReceived = recvfrom ( s,
104 &mBuffer[0],
105 sizeof ( mBuffer ),
106 0,
107 (struct sockaddr *)&RemotePort,
108 &RemotePortLength );
109 RetVal = (UINT32)BytesReceived;
110 if ( 0 < BytesReceived ) {
111 //
112 // Display the received data
113 //
114 printf ( "%4d bytes received from %d.%d.%d.%d:%d\r\n"
115 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\r\n"
116 "%02x %02x %02x %02x\r\n",
117 (UINT32)BytesReceived,
118 (UINT8)SIN_ADDR ( RemotePort ),
119 (UINT8)( SIN_ADDR ( RemotePort ) >> 8 ),
120 (UINT8)( SIN_ADDR ( RemotePort ) >> 16 ),
121 (UINT8)( SIN_ADDR ( RemotePort ) >> 24 ),
122 SIN_PORT ( RemotePort ),
123 mBuffer[0],
124 mBuffer[1],
125 mBuffer[2],
126 mBuffer[3],
127 mBuffer[4],
128 mBuffer[5],
129 mBuffer[6],
130 mBuffer[7],
131 mBuffer[8],
132 mBuffer[9],
133 mBuffer[10],
134 mBuffer[11],
135 mBuffer[12],
136 mBuffer[13],
137 mBuffer[14],
138 mBuffer[15],
139 mBuffer[16],
140 mBuffer[17],
141 mBuffer[18],
142 mBuffer[19]);
143 TotalBytesReceived += BytesReceived;
144
145 //
146 // All done when the correct packet is received
147 //
148 if ( mBuffer[9] == RAW_PROTOCOL ) {
149 break;
150 }
151 }
152 else if ( -1 == BytesReceived ) {
153 //
154 // Check for a timeout
155 //
156 RetVal = GET_ERRNO;
157 printf ( "ERROR - recv error, errno: %d\r\n", RetVal );
158 break;
159 }
160 } while ( 0 != RetVal );
161
162 //
163 // Display the bytes received
164 //
165 if ( 0 == RetVal ) {
166 printf ( "Total Bytes Received: %Ld\r\n", TotalBytesReceived );
167 }
168
169 //
170 // Test complete
171 //
172 break;
173 }
174 break;
175 }
176
177 //
178 // Close the socket
179 //
180 CLOSE_SOCKET ( s );
181 printf ( "Socket closed\r\n" );
182 }
183
184 //
185 // Return the operation status
186 //
187 return RetVal;
188 }
189