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