host: add a netmask constructor taking the number of network bits
[strongswan.git] / src / libstrongswan / networking / host.h
1 /*
2 * Copyright (C) 2006-2009 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2008 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 /**
20 * @defgroup host host
21 * @{ @ingroup networking
22 */
23
24 #ifndef HOST_H_
25 #define HOST_H_
26
27 typedef enum host_diff_t host_diff_t;
28 typedef struct host_t host_t;
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36
37 #include <utils/chunk.h>
38
39 /**
40 * Representates a Host
41 *
42 * Host object, identifies a address:port pair and defines some
43 * useful functions on it.
44 */
45 struct host_t {
46
47 /**
48 * Build a clone of this host object.
49 *
50 * @return cloned host
51 */
52 host_t *(*clone) (host_t *this);
53
54 /**
55 * Get a pointer to the internal sockaddr struct.
56 *
57 * This is used for sending and receiving via sockets.
58 *
59 * @return pointer to the internal sockaddr structure
60 */
61 sockaddr_t *(*get_sockaddr) (host_t *this);
62
63 /**
64 * Get the length of the sockaddr struct.
65 *
66 * Depending on the family, the length of the sockaddr struct
67 * is different. Use this function to get the length of the sockaddr
68 * struct returned by get_sock_addr.
69 *
70 * This is used for sending and receiving via sockets.
71 *
72 * @return length of the sockaddr struct
73 */
74 socklen_t *(*get_sockaddr_len) (host_t *this);
75
76 /**
77 * Gets the family of the address
78 *
79 * @return family
80 */
81 int (*get_family) (host_t *this);
82
83 /**
84 * Checks if the ip address of host is set to default route.
85 *
86 * @return TRUE if host is 0.0.0.0 or 0::0, FALSE otherwise
87 */
88 bool (*is_anyaddr) (host_t *this);
89
90 /**
91 * Get the address of this host as chunk_t
92 *
93 * Returned chunk points to internal data.
94 *
95 * @return address string,
96 */
97 chunk_t (*get_address) (host_t *this);
98
99 /**
100 * Get the port of this host
101 *
102 * @return port number
103 */
104 u_int16_t (*get_port) (host_t *this);
105
106 /**
107 * Set the port of this host
108 *
109 * @param port port numer
110 */
111 void (*set_port) (host_t *this, u_int16_t port);
112
113 /**
114 * Compare the ips of two hosts hosts.
115 *
116 * @param other the other to compare
117 * @return TRUE if addresses are equal.
118 */
119 bool (*ip_equals) (host_t *this, host_t *other);
120
121 /**
122 * Compare two hosts, with port.
123 *
124 * @param other the other to compare
125 * @return TRUE if addresses and ports are equal.
126 */
127 bool (*equals) (host_t *this, host_t *other);
128
129 /**
130 * Destroy this host object.
131 */
132 void (*destroy) (host_t *this);
133 };
134
135 /**
136 * Constructor to create a host_t object from an address string.
137 *
138 * @param string string of an address, such as "152.96.193.130"
139 * @param port port number
140 * @return host_t, NULL if string not an address.
141 */
142 host_t *host_create_from_string(char *string, u_int16_t port);
143
144 /**
145 * Same as host_create_from_string(), but with the option to enforce a family.
146 *
147 * @param string string of an address
148 * @param family address family, or AF_UNSPEC
149 * @param port port number
150 * @return host_t, NULL if string not an address.
151 */
152 host_t *host_create_from_string_and_family(char *string, int family,
153 u_int16_t port);
154
155 /**
156 * Constructor to create a host_t from a DNS name.
157 *
158 * @param string hostname to resolve
159 * @param family family to prefer, 0 for first match
160 * @param port port number
161 * @return host_t, NULL lookup failed
162 */
163 host_t *host_create_from_dns(char *string, int family, u_int16_t port);
164
165 /**
166 * Constructor to create a host_t object from an address chunk.
167 *
168 * If family is AF_UNSPEC, it is guessed using address.len.
169 *
170 * @param family Address family, such as AF_INET or AF_INET6
171 * @param address address as chunk_t in network order
172 * @param port port number
173 * @return host_t, NULL if family not supported/chunk invalid
174 */
175 host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port);
176
177 /**
178 * Constructor to create a host_t object from a sockaddr struct
179 *
180 * @param sockaddr sockaddr struct which contains family, address and port
181 * @return host_t, NULL if family not supported
182 */
183 host_t *host_create_from_sockaddr(sockaddr_t *sockaddr);
184
185 /**
186 * Create a host from a CIDR subnet definition (1.2.3.0/24), return bits.
187 *
188 * @param string string to parse
189 * @param bits gets the number of network bits in CIDR notation
190 * @return network start address, NULL on error
191 */
192 host_t *host_create_from_subnet(char *string, int *bits);
193
194 /**
195 * Create a netmask host having the first netbits bits set.
196 *
197 * @param netbits number of leading bits set in the host
198 * @return netmask host
199 */
200 host_t *host_create_netmask(int family, int netbits);
201
202 /**
203 * Create a host without an address, a "any" host.
204 *
205 * @param family family of the any host
206 * @return host_t, NULL if family not supported
207 */
208 host_t *host_create_any(int family);
209
210 /**
211 * printf hook function for host_t.
212 *
213 * Arguments are:
214 * host_t *host
215 * Use #-modifier to include port number
216 * Use +-modifier to force numeric representation (instead of e.g. %any)
217 */
218 int host_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
219 const void *const *args);
220
221 #endif /** HOST_H_ @}*/