2 Raw IP4 receive application
4 Copyright (c) 2011-2012, Intel Corporation. All rights reserved.
5 SPDX-License-Identifier: BSD-2-Clause-Patent
15 Run the raw IP4 receive application
17 @param [in] ArgC Argument count
18 @param [in] ArgV Argument value array
20 @retval 0 Successfully operation
28 ssize_t BytesReceived
;
29 struct sockaddr_in LocalPort
;
30 socklen_t LocalPortLength
;
31 struct sockaddr_in RemotePort
;
32 socklen_t RemotePortLength
;
35 UINT64 TotalBytesReceived
;
40 s
= socket ( AF_INET
, SOCK_RAW
, RAW_PROTOCOL
);
43 printf ( "ERROR - socket error, errno: %d\r\n", RetVal
);
47 // Use for/break; instead of goto
51 // Bind the socket to a known port
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;
59 (struct sockaddr
*)&LocalPort
,
60 sizeof ( LocalPort
));
63 printf ( "ERROR - bind error, errno: %d\r\n", RetVal
);
68 // Display the local address and protocol
70 LocalPortLength
= sizeof ( LocalPort
);
71 RetVal
= getsockname ( s
, (struct sockaddr
*)&LocalPort
, &LocalPortLength
);
74 printf ( "ERROR - getsockname error, errno: %d\r\n", RetVal
);
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 ),
85 // Use for/break instead of goto
87 TotalBytesReceived
= 0;
90 // Receive data from the remote system
94 // Attempt to receive a packet
96 RemotePortLength
= sizeof ( RemotePort
);
97 BytesReceived
= recvfrom ( s
,
101 (struct sockaddr
*)&RemotePort
,
103 RetVal
= (UINT32
)BytesReceived
;
104 if ( 0 < BytesReceived
) {
106 // Display the received data
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
),
137 TotalBytesReceived
+= BytesReceived
;
140 // All done when the correct packet is received
142 if ( mBuffer
[9] == RAW_PROTOCOL
) {
146 else if ( -1 == BytesReceived
) {
148 // Check for a timeout
151 printf ( "ERROR - recv error, errno: %d\r\n", RetVal
);
154 } while ( 0 != RetVal
);
157 // Display the bytes received
160 printf ( "Total Bytes Received: %Ld\r\n", TotalBytesReceived
);
175 printf ( "Socket closed\r\n" );
179 // Return the operation status