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