[go: up one dir, main page]

File: ipcalc.h

package info (click to toggle)
uhub 0.4.1-3.3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,508 kB
  • sloc: ansic: 18,133; xml: 575; perl: 568; sh: 368; makefile: 24
file content (114 lines) | stat: -rw-r--r-- 3,505 bytes parent folder | download | duplicates (6)
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
/*
 * uhub - A tiny ADC p2p connection hub
 * Copyright (C) 2007-2009, Jan Vidar Krey
 *
 * 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/>.
 *
 */

/*
 * This file is used for fiddling with IP-addresses, 
 * primarily used for IP-banning in uhub.
 */

#ifndef HAVE_UHUB_IPCALC_H
#define HAVE_UHUB_IPCALC_H

struct ip_addr_encap {
	int af;
	union {
		struct in_addr in;
		struct in6_addr in6;
	} internal_ip_data;
};

struct ip_range
{
	struct ip_addr_encap lo;
	struct ip_addr_encap hi;
};

extern int ip_convert_to_binary(const char* text_addr, struct ip_addr_encap* raw);

extern const char* ip_convert_to_string(struct ip_addr_encap* raw);

/**
 * Convert a string on the form:
 * ip-ip or ip/mask to an iprange.
 *
 * Note: both IPv4 and IPv6 addresses are valid, but if a range is given
 * both addresses must be of the same address family.
 *
 * Valid examples of address
 * IPv4:
 * 192.168.2.1
 * 192.168.0.0/16
 * 192.168.0.0-192.168.255.255
 *
 * IPv6:
 * 2001:4860:A005::68
 * 2001:4860:A005::0/80
 * 2001:4860:A005::0-2001:4860:A005:ffff:ffff:ffff:ffff:ffff
 *
 * @return 0 if invalid, 1 if OK
 */
extern int ip_convert_address_to_range(const char* address, struct ip_range* range);

/**
 * @return 1 if addr is inside range, 0 otherwise
 */
extern int ip_in_range(struct ip_addr_encap* addr, struct ip_range* range);

/**
 * @return 1 if address is a valid IPv4 address in text notation
 *         0 if invalid
 */
extern int ip_is_valid_ipv4(const char* address);

/**
 * @return 1 if address is a valid IPv6 address in text notation
 *         0 if invalid
 */
extern int ip_is_valid_ipv6(const char* address);


/**
 * This function converts an IP address in text_address to a binary
 * struct sockaddr.
 * This will auto-detect if the IP-address is IPv6 (and that is supported),
 * or if IPv4 should be used.
 * NOTE: Use sockaddr_storage to allocate enough memory for IPv6.
 *
 * @param text_addr is an ipaddress either ipv6 or ipv4.
 *                  Special magic addresses called "any" and "loopback" exist,
 *                  and will work accross IPv6/IPv4.
 * @param port      Fill the struct sockaddr* with the given port, can safely be ignored.
 */
extern int ip_convert_address(const char* text_address, int port, struct sockaddr* addr, socklen_t* addr_len);

extern int ip_mask_create_left(int af, int bits, struct ip_addr_encap* result);
extern int ip_mask_create_right(int af, int bits, struct ip_addr_encap* result);

extern void ip_mask_apply_AND(struct ip_addr_encap* address, struct ip_addr_encap* mask, struct ip_addr_encap* result);
extern void ip_mask_apply_OR(struct ip_addr_encap* address, struct ip_addr_encap* mask, struct ip_addr_encap* result);

/**
 * @return <0 if a is less than b
 * @return >0 if a is greater than b
 * @return  0 if they are equal
 */
extern int ip_compare(struct ip_addr_encap* a, struct ip_addr_encap* b);

#endif /* HAVE_UHUB_IPCALC_H */