-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathiprange.c
137 lines (124 loc) · 3.54 KB
/
iprange.c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (C) 2018-2024 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/>.
*/
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#ifdef _MSC_VER
#pragma comment(lib, "ws2_32.lib")
#endif // _MSC_VER
#else
#include <arpa/inet.h> /* ntohl */
#endif
#include <errno.h> /* errno */
#include <stdio.h>
#include <stdlib.h> /* exit */
#include <string.h> /* strchr */
#include "iprange.h"
/**
* @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)
{
struct in_addr in;
#ifdef _WIN32
if (inet_pton(AF_INET, ipstr, &in) != 1) {
#else
if (!inet_aton(ipstr, &in)) {
#endif
fprintf(stderr, "Invalid address %s!\n", ipstr);
exit(1);
}
return (in_addr_t)ntohl(in.s_addr);
}
/**
* @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)
{
if (prefix == 0) {
return (~((in_addr_t)-1));
} else {
return (~(((in_addr_t)1 << (32 - prefix)) - 1));
}
}
/**
* @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)
{
return (addr & netmask(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)
{
return (addr | ~netmask(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)
{
long prefix = 32;
char *prefixstr;
network_addr_t netaddr;
char *ipstr_copy = strdup(ipstr); // Make a copy of ipstr to modify
if ((prefixstr = strchr(ipstr_copy, '/'))) {
*prefixstr = '\0';
prefixstr++;
errno = 0;
prefix = strtol(prefixstr, (char **)NULL, 10);
if (errno || (*prefixstr == '\0') || (prefix < 0) ||
(prefix > 32)) {
fprintf(stderr, "Invalid prefix /%s...!\n", prefixstr);
free(ipstr_copy);
exit(1);
}
}
netaddr.pfx = (int)prefix;
netaddr.addr = network(a_to_hl(ipstr_copy), (int)prefix);
free(ipstr_copy);
return (netaddr);
}