-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathiprange.h
89 lines (80 loc) · 2.58 KB
/
iprange.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright (C) 2018 Jorge Matricali <jorgematricali@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef IPRANGE_H
#define IPRANGE_H
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
typedef int in_addr_t;
#ifdef _MSC_VER
#pragma comment(lib, "ws2_32.lib")
#endif // _MSC_VER
#else
#include <arpa/inet.h> /* in_addr_t */
#endif
typedef struct network_addr {
in_addr_t addr;
int pfx;
} network_addr_t;
/**
* @brief Convert an IPv4 address in string format to a 32-bit host-order value.
*
* @param ipstr The IPv4 address in string format (e.g., "192.168.1.1").
*
* @return The 32-bit host-order value of the IPv4 address.
*
* @note Exits the program with an error message if the address is invalid.
*/
in_addr_t a_to_hl(const char *ipstr);
/**
* @brief Compute the netmask address given a prefix length.
*
* @param prefix The prefix length (0 to 32).
*
* @return The netmask address as a 32-bit integer.
*/
in_addr_t netmask(int prefix);
/**
* @brief Compute the network address given an IP address and a prefix length.
*
* @param addr The IP address as a 32-bit integer.
* @param prefix The prefix length (0 to 32).
*
* @return The network address as a 32-bit integer.
*/
in_addr_t network(in_addr_t addr, int prefix);
/**
* @brief Compute the broadcast address given an IP address and a prefix length.
*
* @param addr The IP address as a 32-bit integer.
* @param prefix The prefix length (0 to 32).
*
* @return The broadcast address as a 32-bit integer.
*/
in_addr_t broadcast(in_addr_t addr, int prefix);
/**
* @brief Convert a network address in string format into a host-order network address
* and an integer prefix value.
*
* @param ipstr The network address in string format (e.g., "192.168.1.0/24").
*
* @return A network_addr_t structure containing the network address and prefix.
*
* @note Exits the program with an error message if the address or prefix is invalid.
*/
network_addr_t str_to_netaddr(const char *ipstr);
#endif /* IPRANGE_H */