2 Raw IP4 receive application
4 Copyright (c) 2011, 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
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.
21 Run the raw IP4 receive application
23 @param [in] ArgC Argument count
24 @param [in] ArgV Argument value array
26 @retval 0 Successfully operation
34 ssize_t BytesReceived
;
35 struct sockaddr_in LocalPort
;
36 socklen_t LocalPortLength
;
37 struct sockaddr_in RemotePort
;
38 socklen_t RemotePortLength
;
41 UINT64 TotalBytesReceived
;
46 s
= socket ( AF_INET
, SOCK_RAW
, RAW_PROTOCOL
);
49 printf ( "ERROR - socket error, errno: %d\r\n", RetVal
);
53 // Use for/break; instead of goto
57 // Bind the socket to a known port
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;
65 (struct sockaddr
*)&LocalPort
,
66 sizeof ( LocalPort
));
69 printf ( "ERROR - bind error, errno: %d\r\n", RetVal
);
74 // Display the local address and protocol
76 LocalPortLength
= sizeof ( LocalPort
);
77 RetVal
= getsockname ( s
, (struct sockaddr
*)&LocalPort
, &LocalPortLength
);
80 printf ( "ERROR - getsockname error, errno: %d\r\n", RetVal
);
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 ),
91 // Use for/break instead of goto
93 TotalBytesReceived
= 0;
96 // Receive data from the remote system
100 // Attempt to receive a packet
102 RemotePortLength
= sizeof ( RemotePort
);
103 BytesReceived
= recvfrom ( s
,
107 (struct sockaddr
*)&RemotePort
,
109 RetVal
= (UINT32
)BytesReceived
;
110 if ( 0 < BytesReceived
) {
112 // Display the received data
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
),
143 TotalBytesReceived
+= BytesReceived
;
146 // All done when the correct packet is received
148 if ( mBuffer
[9] == RAW_PROTOCOL
) {
152 else if ( -1 == BytesReceived
) {
154 // Check for a timeout
157 printf ( "ERROR - recv error, errno: %d\r\n", RetVal
);
160 } while ( 0 != RetVal
);
163 // Display the bytes received
166 printf ( "Total Bytes Received: %Ld\r\n", TotalBytesReceived
);
181 printf ( "Socket closed\r\n" );
185 // Return the operation status