]>
Commit | Line | Data |
---|---|---|
f6e5cdd5 | 1 | /** @file\r |
2 | Test the getnameinfo API\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 | |
f6e5cdd5 | 6 | \r |
7 | **/\r | |
8 | \r | |
9 | #include <errno.h>\r | |
10 | #include <netdb.h>\r | |
11 | #include <string.h>\r | |
12 | #include <stdio.h>\r | |
13 | #include <Uefi.h>\r | |
14 | #include <unistd.h>\r | |
15 | \r | |
16 | #include <netinet/in.h>\r | |
17 | \r | |
18 | #include <sys/socket.h>\r | |
19 | \r | |
20 | char mBuffer[65536];\r | |
21 | char mHostName[256];\r | |
22 | char mServiceName[256];\r | |
23 | \r | |
24 | /**\r | |
25 | Test the getnameinfo API\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 | int AppStatus;\r | |
40 | struct addrinfo * pAddrInfo;\r | |
41 | char * pHostName;\r | |
42 | struct addrinfo * pInfo;\r | |
43 | char * pServerName;\r | |
44 | \r | |
45 | //\r | |
46 | // Determine if the host name is specified\r | |
47 | //\r | |
48 | AppStatus = 0;\r | |
49 | if ( 1 == Argc ) {\r | |
50 | printf ( "%s <host address> <server name>\r\n", Argv[0]);\r | |
51 | }\r | |
52 | else {\r | |
53 | //\r | |
54 | // Translate the host name\r | |
55 | //\r | |
56 | pHostName = Argv[1];\r | |
57 | pServerName = NULL;\r | |
58 | if ( 2 < Argc ) {\r | |
59 | pServerName = Argv[2];\r | |
60 | }\r | |
61 | AppStatus = getaddrinfo ( pHostName,\r | |
62 | pServerName,\r | |
63 | NULL,\r | |
64 | &pAddrInfo );\r | |
65 | if ( 0 != AppStatus ) {\r | |
66 | printf ( "ERROR - address info not found, errno: %d\r\n", AppStatus );\r | |
67 | }\r | |
68 | if ( NULL == pAddrInfo ) {\r | |
69 | printf ( "ERROR - No address info structure allocated\r\n" );\r | |
70 | }\r | |
71 | else {\r | |
72 | //\r | |
73 | // Walk the list of names\r | |
74 | //\r | |
75 | pInfo = pAddrInfo;\r | |
76 | while ( NULL != pInfo ) {\r | |
77 | //\r | |
78 | // Get the name info\r | |
79 | //\r | |
80 | AppStatus = getnameinfo ((struct sockaddr *)pInfo->ai_addr,\r | |
81 | pInfo->ai_addrlen,\r | |
82 | &mHostName[0],\r | |
83 | sizeof ( mHostName ),\r | |
84 | &mServiceName[0],\r | |
85 | sizeof ( mServiceName ),\r | |
86 | 0 );\r | |
87 | if ( 0 != AppStatus ) {\r | |
88 | break;\r | |
89 | }\r | |
90 | \r | |
91 | //\r | |
92 | // Display this entry\r | |
93 | //\r | |
d3a595ce | 94 | printf ( "%s: HostName\r\n", &mHostName[0]);\r |
f6e5cdd5 | 95 | printf ( "%s: Service Name\r\n", &mServiceName[0]);\r |
96 | \r | |
97 | //\r | |
98 | // Set the next entry\r | |
99 | //\r | |
100 | pInfo = pInfo->ai_next;\r | |
101 | }\r | |
102 | \r | |
103 | //\r | |
104 | // Done with this structures\r | |
105 | //\r | |
106 | freeaddrinfo ( pAddrInfo );\r | |
107 | }\r | |
108 | }\r | |
109 | \r | |
110 | //\r | |
111 | // All done\r | |
112 | //\r | |
113 | return AppStatus;\r | |
114 | }\r |