]>
Commit | Line | Data |
---|---|---|
4684b66f | 1 | /** @file\r |
2 | Receive a datagram\r | |
3 | \r | |
bcb96695 MK |
4 | Copyright (c) 2011-2012, Intel Corporation. All rights reserved.\r |
5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
4684b66f | 6 | \r |
7 | **/\r | |
8 | \r | |
9 | #include <errno.h>\r | |
10 | #include <string.h>\r | |
11 | #include <Uefi.h>\r | |
12 | #include <unistd.h>\r | |
13 | \r | |
14 | #include <Library/DebugLib.h>\r | |
15 | #include <Library/UefiLib.h>\r | |
16 | \r | |
17 | #include <netinet/in.h>\r | |
18 | \r | |
19 | #include <sys/socket.h>\r | |
20 | #include <sys/time.h>\r | |
21 | \r | |
59bc0593 | 22 | UINT8 mBuffer[ 65536 ];\r |
4684b66f | 23 | \r |
24 | /**\r | |
25 | Receive a datagram\r | |
26 | \r | |
27 | @param [in] Argc The number of arguments\r | |
28 | @param [in] Argv The argument value array\r | |
29 | \r | |
30 | @retval 0 The application exited normally.\r | |
31 | @retval Other An error occurred.\r | |
32 | **/\r | |
33 | int\r | |
34 | main (\r | |
35 | IN int Argc,\r | |
36 | IN char **Argv\r | |
37 | )\r | |
38 | {\r | |
39 | struct sockaddr_in Address;\r | |
40 | socklen_t AddressLength;\r | |
41 | ssize_t LengthInBytes;\r | |
42 | int s;\r | |
43 | int Status;\r | |
44 | struct timeval Timeout;\r | |
45 | \r | |
46 | \r | |
47 | DEBUG (( DEBUG_INFO,\r | |
48 | "%a starting\r\n",\r | |
49 | Argv[0]));\r | |
50 | \r | |
51 | //\r | |
52 | // Get the socket\r | |
53 | //\r | |
54 | s = socket ( AF_INET, SOCK_DGRAM, 0 );\r | |
55 | if ( -1 == s ) {\r | |
56 | Print ( L"ERROR - Unable to open the socket, errno: %d\r\n", errno );\r | |
57 | }\r | |
58 | else {\r | |
59 | Timeout.tv_sec = 5;\r | |
60 | Timeout.tv_usec = 0;\r | |
61 | Status = setsockopt ( s,\r | |
62 | SOL_SOCKET,\r | |
63 | SO_RCVTIMEO,\r | |
64 | &Timeout,\r | |
65 | sizeof ( Timeout ));\r | |
66 | if ( -1 == Status ) {\r | |
67 | Print ( L"ERROR - Unable to set the receive timeout, errno: %d\r\n", errno );\r | |
68 | }\r | |
69 | else {\r | |
70 | AddressLength = sizeof ( Address );\r | |
71 | LengthInBytes = recvfrom ( s,\r | |
72 | &mBuffer[0],\r | |
73 | sizeof ( mBuffer[0]),\r | |
74 | 0,\r | |
75 | (struct sockaddr *)&Address,\r | |
76 | &AddressLength );\r | |
77 | if ( -1 == LengthInBytes ) {\r | |
78 | if ( ETIMEDOUT == errno ) {\r | |
79 | Print ( L"No datagram received\r\n" );\r | |
80 | }\r | |
81 | else {\r | |
82 | Print ( L"ERROR - No datagram received, errno: %d\r\n", errno );\r | |
83 | }\r | |
84 | }\r | |
85 | else {\r | |
86 | Print ( L"Received %d bytes from %d.%d.%d.%d:%d\r\n",\r | |
87 | LengthInBytes,\r | |
88 | (UINT8)Address.sin_addr.s_addr,\r | |
89 | (UINT8)( Address.sin_addr.s_addr >> 8 ),\r | |
90 | (UINT8)( Address.sin_addr.s_addr >> 16 ),\r | |
91 | (UINT8)( Address.sin_addr.s_addr >> 24 ),\r | |
92 | htons ( Address.sin_port ));\r | |
93 | }\r | |
94 | }\r | |
95 | \r | |
96 | //\r | |
97 | // Done with the socket\r | |
98 | //\r | |
99 | close ( s );\r | |
100 | }\r | |
101 | \r | |
102 | //\r | |
103 | // All done\r | |
104 | //\r | |
105 | DEBUG (( DEBUG_INFO,\r | |
106 | "%a exiting, errno: %d\r\n",\r | |
107 | Argv[0],\r | |
108 | errno ));\r | |
109 | return errno;\r | |
110 | }\r |