]>
Commit | Line | Data |
---|---|---|
f6e5cdd5 | 1 | /** @file\r |
2 | Test the getaddrinfo 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 | \r | |
22 | \r | |
23 | /**\r | |
24 | Test the getaddrinfo API\r | |
25 | \r | |
26 | @param [in] Argc The number of arguments\r | |
27 | @param [in] Argv The argument value array\r | |
28 | \r | |
29 | @retval 0 The application exited normally.\r | |
30 | @retval Other An error occurred.\r | |
31 | **/\r | |
32 | int\r | |
33 | main (\r | |
34 | IN int Argc,\r | |
35 | IN char **Argv\r | |
36 | )\r | |
37 | {\r | |
38 | int AppStatus;\r | |
39 | int Index;\r | |
40 | int MaxLen;\r | |
41 | struct addrinfo * pAddrInfo;\r | |
42 | char * pHostName;\r | |
43 | struct addrinfo * pInfo;\r | |
44 | char * pServerName;\r | |
45 | \r | |
46 | //\r | |
47 | // Determine if the host name is specified\r | |
48 | //\r | |
49 | AppStatus = 0;\r | |
50 | if ( 1 == Argc ) {\r | |
51 | printf ( "%s <host name> <server name>\r\n", Argv[0]);\r | |
52 | }\r | |
53 | else {\r | |
54 | //\r | |
55 | // Translate the host name\r | |
56 | //\r | |
57 | pHostName = Argv[1];\r | |
58 | pServerName = NULL;\r | |
59 | if ( 2 < Argc ) {\r | |
60 | pServerName = Argv[2];\r | |
61 | }\r | |
62 | AppStatus = getaddrinfo ( pHostName,\r | |
63 | pServerName,\r | |
64 | NULL,\r | |
65 | &pAddrInfo );\r | |
66 | if ( 0 != AppStatus ) {\r | |
67 | printf ( "ERROR - address info not found, errno: %d\r\n", AppStatus );\r | |
68 | }\r | |
69 | if ( NULL == pAddrInfo ) {\r | |
70 | printf ( "ERROR - No address info structure allocated\r\n" );\r | |
71 | }\r | |
72 | else {\r | |
73 | //\r | |
74 | // Walk the list of addresses\r | |
75 | //\r | |
76 | pInfo = pAddrInfo;\r | |
77 | while ( NULL != pInfo ) {\r | |
78 | //\r | |
79 | // Display this entry\r | |
80 | //\r | |
81 | printf ( "0x%08x: ai_flags\r\n", pInfo->ai_flags );\r | |
82 | printf ( "0x%08x: ai_family\r\n", pInfo->ai_family );\r | |
83 | printf ( "0x%08x: ai_socktype\r\n", pInfo->ai_socktype );\r | |
84 | printf ( "0x%08x: ai_protocol\r\n", pInfo->ai_protocol );\r | |
85 | printf ( "0x%08x: ai_addrlen\r\n", pInfo->ai_addrlen );\r | |
86 | printf ( "%s: ai_canonname\r\n", pInfo->ai_canonname );\r | |
87 | printf ( " 0x%02x: ai_addr->sa_len\r\n", (UINT8)pInfo->ai_addr->sa_len );\r | |
88 | printf ( " 0x%02x: ai_addr->sa_family\r\n", (UINT8)pInfo->ai_addr->sa_family );\r | |
89 | MaxLen = pInfo->ai_addr->sa_len;\r | |
90 | if ( sizeof ( struct sockaddr_in6 ) < MaxLen ) {\r | |
91 | MaxLen = sizeof ( struct sockaddr_in6 );\r | |
92 | }\r | |
93 | for ( Index = 0; ( MaxLen - 2 ) > Index; Index++ ) {\r | |
94 | printf ( " 0x%02x: ai_addr->sa_data[%02d]\r\n", (UINT8)pInfo->ai_addr->sa_data [ Index ], Index );\r | |
95 | }\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 |