]> git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Sockets/GetNetByAddr/GetNetByAddr.c
0d89a9afc4bac1ab3e59e7e0aa783d36bd935dd6
[mirror_edk2.git] / AppPkg / Applications / Sockets / GetNetByAddr / GetNetByAddr.c
1 /** @file
2 Translate the IPv4 address into a network name
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 <stdio.h>
12 #include <string.h>
13 #include <Uefi.h>
14 #include <unistd.h>
15
16 #include <Library/DebugLib.h>
17 #include <Library/UefiLib.h>
18
19 #include <sys/socket.h>
20
21 /**
22 Translate the IPv4 address into a network name
23
24 @param [in] Argc The number of arguments
25 @param [in] Argv The argument value array
26
27 @retval 0 The application exited normally.
28 @retval Other An error occurred.
29 **/
30 int
31 main (
32 IN int Argc,
33 IN char **Argv
34 )
35 {
36 UINT32 RemoteAddress[4];
37 UINT8 IpAddress[4];
38 struct netent * pNetwork;
39
40 //
41 // Determine if the IPv4 address is specified
42 //
43 if (( 2 != Argc )
44 || ( 4 != sscanf ( Argv[1],
45 "%d.%d.%d.%d",
46 &RemoteAddress[0],
47 &RemoteAddress[1],
48 &RemoteAddress[2],
49 &RemoteAddress[3]))
50 || ( 255 < RemoteAddress[0])
51 || ( 255 < RemoteAddress[1])
52 || ( 255 < RemoteAddress[2])
53 || ( 255 < RemoteAddress[3])) {
54 Print ( L"%a <IPv4 Address>\r\n", Argv[0]);
55 }
56 else {
57 //
58 // Translate the address into a network name
59 //
60 IpAddress[0] = (UINT8)RemoteAddress[0];
61 IpAddress[1] = (UINT8)RemoteAddress[1];
62 IpAddress[2] = (UINT8)RemoteAddress[2];
63 IpAddress[3] = (UINT8)RemoteAddress[3];
64 pNetwork = getnetbyaddr ( *(uint32_t *)&IpAddress[0], AF_INET );
65 if ( NULL == pNetwork ) {
66 Print ( L"ERROR - network not found, errno: %d\r\n", errno );
67 }
68 else {
69 Print ( L"%a: %d.%d.%d.%d, 0x%08x\r\n",
70 pNetwork->n_name,
71 IpAddress[0],
72 IpAddress[1],
73 IpAddress[2],
74 IpAddress[3],
75 pNetwork->n_net );
76 }
77 }
78
79 //
80 // All done
81 //
82 return errno;
83 }