(no commit message)
[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
39 /**
40 * printf() specifier to print a host.
41 * The specifier option '#' does include the port number, e.g.:
42 * printf("my host is %#H\n", my_host);
43 */
44 #define HOST_PRINTF_SPEC 'H'
45
46 typedef enum host_diff_t host_diff_t;
47
48 /**
49 * Differences between two hosts. They differ in
50 * address, port, or both.
51 */
52 enum host_diff_t {
53 HOST_DIFF_NONE = 0,
54 HOST_DIFF_ADDR = 1,
55 HOST_DIFF_PORT = 2,
56 };
57
58 typedef struct host_t host_t;
59
60 /**
61 * @brief Representates a Host
62 *
63 * Host object, identifies a address:port pair and defines some
64 * useful functions on it.
65 *
66 * @b Constructors:
67 * - host_create()
68 * - host_create_from_chunk()
69 * - host_create_from_sockaddr()
70 *
71 * @todo Add IPv6 support
72 *
73 * @ingroup network
74 */
75 struct host_t {
76
77 /**
78 * @brief Build a clone of this host object.
79 *
80 * @param this object to clone
81 * @return cloned host
82 */
83 host_t *(*clone) (host_t *this);
84
85 /**
86 * @brief Get a pointer to the internal sockaddr struct.
87 *
88 * This is used for sending and receiving via sockets.
89 *
90 * @param this object to clone
91 * @return pointer to the internal sockaddr structure
92 */
93 sockaddr_t *(*get_sockaddr) (host_t *this);
94
95 /**
96 * @brief Get the length of the sockaddr struct.
97 *
98 * Depending on the family, the length of the sockaddr struct
99 * is different. Use this function to get the length of the sockaddr
100 * struct returned by get_sock_addr.
101 *
102 * This is used for sending and receiving via sockets.
103 *
104 * @param this object to clone
105 * @return length of the sockaddr struct
106 */
107 socklen_t *(*get_sockaddr_len) (host_t *this);
108
109 /**
110 * @brief Gets the family of the address
111 *
112 * @param this calling object
113 * @return family
114 */
115 int (*get_family) (host_t *this);
116
117 /**
118 * @brief Checks if the ip address of host is set to default route.
119 *
120 * @param this calling object
121 * @return
122 * - TRUE if host has IP 0.0.0.0 for default route
123 * - FALSE otherwise
124 */
125 bool (*is_anyaddr) (host_t *this);
126
127 /**
128 * @brief get the address of this host as chunk_t
129 *
130 * Returned chunk points to internal data.
131 *
132 * @param this object
133 * @return address string,
134 */
135 chunk_t (*get_address) (host_t *this);
136
137 /**
138 * @brief get the port of this host
139 *
140 * @param this object to clone
141 * @return port number
142 */
143 u_int16_t (*get_port) (host_t *this);
144
145 /**
146 * @brief set the port of this host
147 *
148 * @param this object to clone
149 * @param port port numer
150 */
151 void (*set_port) (host_t *this, u_int16_t port);
152
153 /**
154 * @brief Compare the ips of two hosts hosts.
155 *
156 * @param this object to compare
157 * @param other the other to compare
158 * @return TRUE if addresses are equal.
159 */
160 bool (*ip_equals) (host_t *this, host_t *other);
161
162 /**
163 * @brief Compare two hosts, with port.
164 *
165 * @param this object to compare
166 * @param other the other to compare
167 * @return TRUE if addresses and ports are equal.
168 */
169 bool (*equals) (host_t *this, host_t *other);
170
171 /**
172 * @brief Compare two hosts and return the differences.
173 *
174 * @param this object to compare
175 * @param other the other to compare
176 * @return differences in a combination of host_diff_t's
177 */
178 host_diff_t (*get_differences) (host_t *this, host_t *other);
179
180 /**
181 * @brief Destroy this host object
182 *
183 * @param this calling
184 * @return SUCCESS in any case
185 */
186 void (*destroy) (host_t *this);
187 };
188
189 /**
190 * @brief Constructor to create a host_t object from an address string.
191 *
192 * @param string string of an address, such as "152.96.193.130"
193 * @param port port number
194 * @return
195 * - host_t object
196 * - NULL, if string not an address.
197 *
198 * @ingroup network
199 */
200 host_t *host_create_from_string(char *string, u_int16_t port);
201
202 /**
203 * @brief Constructor to create a host_t object from an address chunk
204 *
205 * @param family Address family to use for this object, such as AF_INET or AF_INET6
206 * @param address address as 4 byte chunk_t in networ order
207 * @param port port number
208 * @return
209 * - host_t object
210 * - NULL, if family not supported or chunk_t length not 4 bytes.
211 *
212 * @ingroup network
213 */
214 host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port);
215
216 /**
217 * @brief Constructor to create a host_t object from a sockaddr struct
218 *
219 * @param sockaddr sockaddr struct which contains family, address and port
220 * @return
221 * - host_t object
222 * - NULL, if family not supported.
223 *
224 * @ingroup network
225 */
226 host_t *host_create_from_sockaddr(sockaddr_t *sockaddr);
227
228 #endif /*HOST_H_*/