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