]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Sockets/RecvDgram/RecvDgram.c
Add Socket Library applications.
[mirror_edk2.git] / AppPkg / Applications / Sockets / RecvDgram / RecvDgram.c
CommitLineData
4684b66f 1/** @file\r
2 Receive a datagram\r
3\r
4 Copyright (c) 2011, Intel Corporation\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <errno.h>\r
16#include <string.h>\r
17#include <Uefi.h>\r
18#include <unistd.h>\r
19\r
20#include <Library/DebugLib.h>\r
21#include <Library/UefiLib.h>\r
22\r
23#include <netinet/in.h>\r
24\r
25#include <sys/socket.h>\r
26#include <sys/time.h>\r
27\r
28UINT8 mBuffer [ 65536 ];\r
29\r
30/**\r
31 Receive a datagram\r
32\r
33 @param [in] Argc The number of arguments\r
34 @param [in] Argv The argument value array\r
35\r
36 @retval 0 The application exited normally.\r
37 @retval Other An error occurred.\r
38**/\r
39int\r
40main (\r
41 IN int Argc,\r
42 IN char **Argv\r
43 )\r
44{\r
45 struct sockaddr_in Address;\r
46 socklen_t AddressLength;\r
47 ssize_t LengthInBytes;\r
48 int s;\r
49 int Status;\r
50 struct timeval Timeout;\r
51 \r
52\r
53 DEBUG (( DEBUG_INFO,\r
54 "%a starting\r\n",\r
55 Argv[0]));\r
56\r
57 //\r
58 // Get the socket\r
59 //\r
60 s = socket ( AF_INET, SOCK_DGRAM, 0 );\r
61 if ( -1 == s ) {\r
62 Print ( L"ERROR - Unable to open the socket, errno: %d\r\n", errno );\r
63 }\r
64 else {\r
65 Timeout.tv_sec = 5;\r
66 Timeout.tv_usec = 0;\r
67 Status = setsockopt ( s,\r
68 SOL_SOCKET,\r
69 SO_RCVTIMEO,\r
70 &Timeout,\r
71 sizeof ( Timeout ));\r
72 if ( -1 == Status ) {\r
73 Print ( L"ERROR - Unable to set the receive timeout, errno: %d\r\n", errno );\r
74 }\r
75 else {\r
76 AddressLength = sizeof ( Address );\r
77 LengthInBytes = recvfrom ( s,\r
78 &mBuffer[0],\r
79 sizeof ( mBuffer[0]),\r
80 0,\r
81 (struct sockaddr *)&Address,\r
82 &AddressLength );\r
83 if ( -1 == LengthInBytes ) {\r
84 if ( ETIMEDOUT == errno ) {\r
85 Print ( L"No datagram received\r\n" );\r
86 }\r
87 else {\r
88 Print ( L"ERROR - No datagram received, errno: %d\r\n", errno );\r
89 }\r
90 }\r
91 else {\r
92 Print ( L"Received %d bytes from %d.%d.%d.%d:%d\r\n",\r
93 LengthInBytes,\r
94 (UINT8)Address.sin_addr.s_addr,\r
95 (UINT8)( Address.sin_addr.s_addr >> 8 ),\r
96 (UINT8)( Address.sin_addr.s_addr >> 16 ),\r
97 (UINT8)( Address.sin_addr.s_addr >> 24 ),\r
98 htons ( Address.sin_port ));\r
99 }\r
100 }\r
101\r
102 //\r
103 // Done with the socket\r
104 //\r
105 close ( s );\r
106 }\r
107\r
108 //\r
109 // All done\r
110 //\r
111 DEBUG (( DEBUG_INFO,\r
112 "%a exiting, errno: %d\r\n",\r
113 Argv[0],\r
114 errno ));\r
115 return errno;\r
116}\r