updated Doxyfile
[strongswan.git] / src / libstrongswan / utils / 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 utils
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 <library.h>
38
39 /**
40 * Differences between two hosts. They differ in
41 * address, port, or both.
42 */
43 enum host_diff_t {
44 HOST_DIFF_NONE = 0,
45 HOST_DIFF_ADDR = 1,
46 HOST_DIFF_PORT = 2,
47 };
48
49 /**
50 * Representates a Host
51 *
52 * Host object, identifies a address:port pair and defines some
53 * useful functions on it.
54 */
55 struct host_t {
56
57 /**
58 * Build a clone of this host object.
59 *
60 * @return cloned host
61 */
62 host_t *(*clone) (host_t *this);
63
64 /**
65 * Get a pointer to the internal sockaddr struct.
66 *
67 * This is used for sending and receiving via sockets.
68 *
69 * @return pointer to the internal sockaddr structure
70 */
71 sockaddr_t *(*get_sockaddr) (host_t *this);
72
73 /**
74 * Get the length of the sockaddr struct.
75 *
76 * Depending on the family, the length of the sockaddr struct
77 * is different. Use this function to get the length of the sockaddr
78 * struct returned by get_sock_addr.
79 *
80 * This is used for sending and receiving via sockets.
81 *
82 * @return length of the sockaddr struct
83 */
84 socklen_t *(*get_sockaddr_len) (host_t *this);
85
86 /**
87 * Gets the family of the address
88 *
89 * @return family
90 */
91 int (*get_family) (host_t *this);
92
93 /**
94 * Checks if the ip address of host is set to default route.
95 *
96 * @return TRUE if host is 0.0.0.0 or 0::0, FALSE otherwise
97 */
98 bool (*is_anyaddr) (host_t *this);
99
100 /**
101 * Get the address of this host as chunk_t
102 *
103 * Returned chunk points to internal data.
104 *
105 * @return address string,
106 */
107 chunk_t (*get_address) (host_t *this);
108
109 /**
110 * Get the port of this host
111 *
112 * @return port number
113 */
114 u_int16_t (*get_port) (host_t *this);
115
116 /**
117 * Set the port of this host
118 *
119 * @param port port numer
120 */
121 void (*set_port) (host_t *this, u_int16_t port);
122
123 /**
124 * Compare the ips of two hosts hosts.
125 *
126 * @param other the other to compare
127 * @return TRUE if addresses are equal.
128 */
129 bool (*ip_equals) (host_t *this, host_t *other);
130
131 /**
132 * Compare two hosts, with port.
133 *
134 * @param other the other to compare
135 * @return TRUE if addresses and ports are equal.
136 */
137 bool (*equals) (host_t *this, host_t *other);
138
139 /**
140 * Compare two hosts and return the differences.
141 *
142 * @param other the other to compare
143 * @return differences in a combination of host_diff_t's
144 */
145 host_diff_t (*get_differences) (host_t *this, host_t *other);
146
147 /**
148 * Destroy this host object.
149 */
150 void (*destroy) (host_t *this);
151 };
152
153 /**
154 * Constructor to create a host_t object from an address string.
155 *
156 * @param string string of an address, such as "152.96.193.130"
157 * @param port port number
158 * @return host_t, NULL if string not an address.
159 */
160 host_t *host_create_from_string(char *string, u_int16_t port);
161
162 /**
163 * Constructor to create a host_t from a DNS name.
164 *
165 * @param string hostname to resolve
166 * @param family family to prefer, 0 for first match
167 * @param port port number
168 * @return host_t, NULL lookup failed
169 */
170 host_t *host_create_from_dns(char *string, int family, u_int16_t port);
171
172 /**
173 * Constructor to create a host_t object from an address chunk.
174 *
175 * If family is AF_UNSPEC, it is guessed using address.len.
176 *
177 * @param family Address family, such as AF_INET or AF_INET6
178 * @param address address as chunk_t in network order
179 * @param port port number
180 * @return host_t, NULL if family not supported/chunk invalid
181 */
182 host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port);
183
184 /**
185 * Constructor to create a host_t object from a sockaddr struct
186 *
187 * @param sockaddr sockaddr struct which contains family, address and port
188 * @return host_t, NULL if family not supported
189 */
190 host_t *host_create_from_sockaddr(sockaddr_t *sockaddr);
191
192 /**
193 * Create a host without an address, a "any" host.
194 *
195 * @param family family of the any host
196 * @return host_t, NULL if family not supported
197 */
198 host_t *host_create_any(int family);
199
200 /**
201 * printf hook function for host_t.
202 *
203 * Arguments are:
204 * host_t *host
205 * Use #-modifier to include port number
206 */
207 int host_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
208 const void *const *args);
209
210 #endif /** HOST_H_ @}*/