]> git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Sockets/GetAddrInfo/GetAddrInfo.c
3db71ba3dcf7c4b1a5185733a80051042d19993a
[mirror_edk2.git] / AppPkg / Applications / Sockets / GetAddrInfo / GetAddrInfo.c
1 /** @file
2 Test the getaddrinfo API
3
4 Copyright (c) 2011, 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 <netdb.h>
17 #include <string.h>
18 #include <stdio.h>
19 #include <Uefi.h>
20 #include <unistd.h>
21
22 #include <netinet/in.h>
23
24 #include <sys/socket.h>
25
26 char mBuffer[65536];
27
28
29 /**
30 Test the getaddrinfo API
31
32 @param [in] Argc The number of arguments
33 @param [in] Argv The argument value array
34
35 @retval 0 The application exited normally.
36 @retval Other An error occurred.
37 **/
38 int
39 main (
40 IN int Argc,
41 IN char **Argv
42 )
43 {
44 int AppStatus;
45 int Index;
46 int MaxLen;
47 struct addrinfo * pAddrInfo;
48 char * pHostName;
49 struct addrinfo * pInfo;
50 char * pServerName;
51
52 //
53 // Determine if the host name is specified
54 //
55 AppStatus = 0;
56 if ( 1 == Argc ) {
57 printf ( "%s <host name> <server name>\r\n", Argv[0]);
58 }
59 else {
60 //
61 // Translate the host name
62 //
63 pHostName = Argv[1];
64 pServerName = NULL;
65 if ( 2 < Argc ) {
66 pServerName = Argv[2];
67 }
68 AppStatus = getaddrinfo ( pHostName,
69 pServerName,
70 NULL,
71 &pAddrInfo );
72 if ( 0 != AppStatus ) {
73 printf ( "ERROR - address info not found, errno: %d\r\n", AppStatus );
74 }
75 if ( NULL == pAddrInfo ) {
76 printf ( "ERROR - No address info structure allocated\r\n" );
77 }
78 else {
79 //
80 // Walk the list of addresses
81 //
82 pInfo = pAddrInfo;
83 while ( NULL != pInfo ) {
84 //
85 // Display this entry
86 //
87 printf ( "0x%08x: ai_flags\r\n", pInfo->ai_flags );
88 printf ( "0x%08x: ai_family\r\n", pInfo->ai_family );
89 printf ( "0x%08x: ai_socktype\r\n", pInfo->ai_socktype );
90 printf ( "0x%08x: ai_protocol\r\n", pInfo->ai_protocol );
91 printf ( "0x%08x: ai_addrlen\r\n", pInfo->ai_addrlen );
92 printf ( "%s: ai_canonname\r\n", pInfo->ai_canonname );
93 printf ( " 0x%02x: ai_addr->sa_len\r\n", (UINT8)pInfo->ai_addr->sa_len );
94 printf ( " 0x%02x: ai_addr->sa_family\r\n", (UINT8)pInfo->ai_addr->sa_family );
95 MaxLen = pInfo->ai_addr->sa_len;
96 if ( sizeof ( struct sockaddr_in6 ) < MaxLen ) {
97 MaxLen = sizeof ( struct sockaddr_in6 );
98 }
99 for ( Index = 0; ( MaxLen - 2 ) > Index; Index++ ) {
100 printf ( " 0x%02x: ai_addr->sa_data[%02d]\r\n", (UINT8)pInfo->ai_addr->sa_data [ Index ], Index );
101 }
102
103 //
104 // Set the next entry
105 //
106 pInfo = pInfo->ai_next;
107 }
108
109 //
110 // Done with this structures
111 //
112 freeaddrinfo ( pAddrInfo );
113 }
114 }
115
116 //
117 // All done
118 //
119 return AppStatus;
120 }