b9a97b1cbb7eeadca45002b26ef6db8ce3709dc8
[strongswan.git] / src / libstrongswan / utils / host.h
1 /**
2 * @file host.h
3 *
4 * @brief Interface of host_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #ifndef HOST_H_
26 #define HOST_H_
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <linux/xfrm.h>
35
36 #include <types.h>
37
38 typedef enum host_diff_t host_diff_t;
39
40 /**
41 * Differences between two hosts. They differ in
42 * address, port, or both.
43 */
44 enum host_diff_t {
45 HOST_DIFF_NONE = 0,
46 HOST_DIFF_ADDR = 1,
47 HOST_DIFF_PORT = 2,
48 };
49
50 typedef struct host_t host_t;
51
52 /**
53 * @brief Representates a Host
54 *
55 * Host object, identifies a address:port pair and defines some
56 * useful functions on it.
57 *
58 * @b Constructors:
59 * - host_create()
60 * - host_create_from_chunk()
61 * - host_create_from_sockaddr()
62 *
63 * @todo Add IPv6 support
64 *
65 * @ingroup network
66 */
67 struct host_t {
68
69 /**
70 * @brief Build a clone of this host object.
71 *
72 * @param this object to clone
73 * @return cloned host
74 */
75 host_t *(*clone) (host_t *this);
76
77 /**
78 * @brief Get a pointer to the internal sockaddr struct.
79 *
80 * This is used for sending and receiving via sockets.
81 *
82 * @param this object to clone
83 * @return pointer to the internal sockaddr structure
84 */
85 sockaddr_t *(*get_sockaddr) (host_t *this);
86
87 /**
88 * @brief Get the length of the sockaddr struct.
89 *
90 * Depending on the family, the length of the sockaddr struct
91 * is different. Use this function to get the length of the sockaddr
92 * struct returned by get_sock_addr.
93 *
94 * This is used for sending and receiving via sockets.
95 *
96 * @param this object to clone
97 * @return length of the sockaddr struct
98 */
99 socklen_t *(*get_sockaddr_len) (host_t *this);
100
101 /**
102 * @brief Gets the family of the address
103 *
104 * @param this calling object
105 * @return family
106 */
107 int (*get_family) (host_t *this);
108
109 /**
110 * @brief Get the address of this host as a string
111 *
112 * Mostly used for debugging purposes. String
113 * points to internal data.
114 *
115 * @param this object
116 * @return address string,
117 */
118 char* (*get_string) (host_t *this);
119
120 /**
121 * @brief Checks if the ip address of host is set to default route.
122 *
123 * @param this calling object
124 * @return
125 * - TRUE if host has IP 0.0.0.0 for default route
126 * - FALSE otherwise
127 */
128 bool (*is_anyaddr) (host_t *this);
129
130 /**
131 * @brief get the address of this host as chunk_t
132 *
133 * Returned chunk points to internal data.
134 *
135 * @param this object
136 * @return address string,
137 */
138 chunk_t (*get_address) (host_t *this);
139
140 /**
141 * @brief get the port of this host
142 *
143 * @param this object to clone
144 * @return port number
145 */
146 u_int16_t (*get_port) (host_t *this);
147
148 /**
149 * @brief set the port of this host
150 *
151 * @param this object to clone
152 * @param port port numer
153 */
154 void (*set_port) (host_t *this, u_int16_t port);
155
156 /**
157 * @brief Compare the ips of two hosts hosts.
158 *
159 * @param this object to compare
160 * @param other the other to compare
161 * @return TRUE if addresses are equal.
162 */
163 bool (*ip_equals) (host_t *this, host_t *other);
164
165 /**
166 * @brief Compare two hosts, with port.
167 *
168 * @param this object to compare
169 * @param other the other to compare
170 * @return TRUE if addresses and ports are equal.
171 */
172 bool (*equals) (host_t *this, host_t *other);
173
174 /**
175 * @brief Compare two hosts and return the differences.
176 *
177 * @param this object to compare
178 * @param other the other to compare
179 * @return differences in a combination of host_diff_t's
180 */
181 host_diff_t (*get_differences) (host_t *this, host_t *other);
182
183 /**
184 * @brief Destroy this host object
185 *
186 * @param this calling
187 * @return SUCCESS in any case
188 */
189 void (*destroy) (host_t *this);
190 };
191
192 /**
193 * @brief Constructor to create a host_t object from an address string
194 *
195 * @param family Address family to use for this object, such as AF_INET or AF_INET6
196 * @param address string of an address, such as "152.96.193.130"
197 * @param port port number
198 * @return
199 * - host_t object
200 * - NULL, if family not supported/invalid string.
201 *
202 * @ingroup network
203 */
204 host_t *host_create(int family, char *address, u_int16_t port);
205
206 /**
207 * @brief Same as host_create(), but guesses the family.
208 *
209 * @param string string of an address, such as "152.96.193.130"
210 * @param port port number
211 * @return
212 * - host_t object
213 * - NULL, if string not an address.
214 *
215 * @ingroup network
216 */
217 host_t *host_create_from_string(char *string, u_int16_t port);
218
219 /**
220 * @brief Constructor to create a host_t object from an address chunk
221 *
222 * @param family Address family to use for this object, such as AF_INET or AF_INET6
223 * @param address address as 4 byte chunk_t in networ order
224 * @param port port number
225 * @return
226 * - host_t object
227 * - NULL, if family not supported or chunk_t length not 4 bytes.
228 *
229 * @ingroup network
230 */
231 host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port);
232
233 /**
234 * @brief Constructor to create a host_t object from a sockaddr struct
235 *
236 * @param sockaddr sockaddr struct which contains family, address and port
237 * @return
238 * - host_t object
239 * - NULL, if family not supported.
240 *
241 * @ingroup network
242 */
243 host_t *host_create_from_sockaddr(sockaddr_t *sockaddr);
244
245
246 #endif /*HOST_H_*/