]> git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Sockets/GetNameInfo/GetNameInfo.c
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Sockets / GetNameInfo / GetNameInfo.c
1 /** @file
2 Test the getnameinfo API
3
4 Copyright (c) 2011-2012, Intel Corporation. All rights reserved.
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <errno.h>
10 #include <netdb.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <Uefi.h>
14 #include <unistd.h>
15
16 #include <netinet/in.h>
17
18 #include <sys/socket.h>
19
20 char mBuffer[65536];
21 char mHostName[256];
22 char mServiceName[256];
23
24 /**
25 Test the getnameinfo API
26
27 @param [in] Argc The number of arguments
28 @param [in] Argv The argument value array
29
30 @retval 0 The application exited normally.
31 @retval Other An error occurred.
32 **/
33 int
34 main (
35 IN int Argc,
36 IN char **Argv
37 )
38 {
39 int AppStatus;
40 struct addrinfo * pAddrInfo;
41 char * pHostName;
42 struct addrinfo * pInfo;
43 char * pServerName;
44
45 //
46 // Determine if the host name is specified
47 //
48 AppStatus = 0;
49 if ( 1 == Argc ) {
50 printf ( "%s <host address> <server name>\r\n", Argv[0]);
51 }
52 else {
53 //
54 // Translate the host name
55 //
56 pHostName = Argv[1];
57 pServerName = NULL;
58 if ( 2 < Argc ) {
59 pServerName = Argv[2];
60 }
61 AppStatus = getaddrinfo ( pHostName,
62 pServerName,
63 NULL,
64 &pAddrInfo );
65 if ( 0 != AppStatus ) {
66 printf ( "ERROR - address info not found, errno: %d\r\n", AppStatus );
67 }
68 if ( NULL == pAddrInfo ) {
69 printf ( "ERROR - No address info structure allocated\r\n" );
70 }
71 else {
72 //
73 // Walk the list of names
74 //
75 pInfo = pAddrInfo;
76 while ( NULL != pInfo ) {
77 //
78 // Get the name info
79 //
80 AppStatus = getnameinfo ((struct sockaddr *)pInfo->ai_addr,
81 pInfo->ai_addrlen,
82 &mHostName[0],
83 sizeof ( mHostName ),
84 &mServiceName[0],
85 sizeof ( mServiceName ),
86 0 );
87 if ( 0 != AppStatus ) {
88 break;
89 }
90
91 //
92 // Display this entry
93 //
94 printf ( "%s: HostName\r\n", &mHostName[0]);
95 printf ( "%s: Service Name\r\n", &mServiceName[0]);
96
97 //
98 // Set the next entry
99 //
100 pInfo = pInfo->ai_next;
101 }
102
103 //
104 // Done with this structures
105 //
106 freeaddrinfo ( pAddrInfo );
107 }
108 }
109
110 //
111 // All done
112 //
113 return AppStatus;
114 }