blob: 6f27af9f56a69d46c7416b791d849eb9fab26222 [file] [log] [blame]
Ratan Gupta8804feb2017-05-25 10:49:57 +05301#pragma once
2
3#include <unistd.h>
Ratan Gupta3681a502017-06-17 19:20:04 +05304#include "types.hpp"
Ratan Gupta8804feb2017-05-25 10:49:57 +05305
6namespace phosphor
7{
8namespace network
9{
10
11/* @brief converts the given subnet into prefix notation.
12 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
13 * @param[in] mask - Subnet Mask.
14 * @returns prefix.
15 */
16uint8_t toCidr(int addressFamily, const std::string& mask);
17
18/* @brief converts the prefix into subnetmask.
19 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
20 * @param[in] prefix - prefix length.
21 * @returns subnet mask.
22 */
23std::string toMask(int addressFamily, uint8_t prefix);
24
25/* @brief checks that the given ip address is link local or not.
26 * @param[in] address - IP address.
27 * @returns true if it is linklocal otherwise false.
28 */
29bool isLinkLocal(const std::string& address);
30
Ratan Gupta11cef802017-05-29 08:41:48 +053031/* @brief gets the network section of the ip adress.
32 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
33 * @param[in] ipaddress - IP address.
34 * @param[in] prefix - prefix length.
35 * @returns network section of the ipaddress.
36 */
37std::string getNetworkID(int addressFamily, const std::string& ipaddress,
38 uint8_t prefix);
39
Ratan Gupta3681a502017-06-17 19:20:04 +053040/** @brief Get all the interfaces from the system.
41 * @returns list of interface names.
42 */
43IntfAddrMap getInterfaceAddrs();
44
Ratan Gupta8804feb2017-05-25 10:49:57 +053045} //namespace network
46
47class Descriptor
48{
49 private:
50 /** default value */
51 int fd = -1;
52
53 public:
54 Descriptor() = delete;
55 Descriptor(const Descriptor&) = delete;
56 Descriptor& operator=(const Descriptor&) = delete;
57 Descriptor(Descriptor&&) = delete;
Ratan Gupta11cef802017-05-29 08:41:48 +053058 Descriptor& operator=(Descriptor &&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +053059
60 Descriptor(int fd) : fd(fd) {}
61
62 ~Descriptor()
63 {
64 if (fd >= 0)
65 {
66 close(fd);
67 }
68 }
69
70 int operator()() const
71 {
72 return fd;
73 }
74};
75
76} //namespace phosphor