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