3cf0c6397a866f84872f1a0ca705336fcf912516
[strongswan.git] / Source / charon / network / host.h
1 /**
2 * @file host.h
3 *
4 * @brief Interface of host_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #ifndef HOST_H_
24 #define HOST_H_
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <linux/xfrm.h>
33
34 #include <types.h>
35
36
37 typedef struct host_t host_t;
38
39 /**
40 * @brief Representates a Host
41 *
42 * Host object, identifies a address:port pair and defines some
43 * useful functions on it.
44 *
45 * @b Constructors:
46 * - host_create()
47 * - host_create_from_chunk()
48 *
49 * @todo Add IPv6 support
50 *
51 * @ingroup network
52 */
53 struct host_t {
54
55 /**
56 * @brief Build a clone of this host object.
57 *
58 * @param this object to clone
59 * @return cloned host
60 */
61 host_t *(*clone) (host_t *this);
62
63 /**
64 * @brief Get a pointer to the internal sockaddr struct.
65 *
66 * This is used for sending and receiving via sockets.
67 *
68 * @param this object to clone
69 * @return pointer to the internal sockaddr structure
70 */
71 sockaddr_t *(*get_sockaddr) (host_t *this);
72
73 /**
74 * @brief Get the length of the sockaddr struct.
75 *
76 * Sepending 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 * @param this object to clone
83 * @return length of the sockaddr struct
84 */
85 socklen_t *(*get_sockaddr_len) (host_t *this);
86
87 /**
88 * @brief Gets the address as xfrm_address_t.
89 *
90 * This function allows the conversion to an
91 * xfrm_address_t, used for netlink communication
92 * with the kernel.
93 *
94 * @see kernel_interface_t.
95 *
96 * @param this calling object
97 * @return address in xfrm_address_t format
98 */
99 xfrm_address_t (*get_xfrm_addr) (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
111 *
112 * Mostly used for debugging purposes.
113 * @warning string must NOT be freed
114 *
115 * @param this object
116 * @return address string,
117 */
118 char* (*get_address) (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_default_route) (host_t *this);
129
130 /**
131 * @brief get the address of this host as chunk_t
132 *
133 * @warning returned chunk has to get destroyed by caller.
134 *
135 * @param this object
136 * @return address string,
137 */
138 chunk_t (*get_address_as_chunk) (host_t *this);
139
140 /**
141 * @brief get the port of this host
142 *
143 * Mostly used for debugging purposes.
144 *
145 * @param this object to clone
146 * @return port number
147 */
148 u_int16_t (*get_port) (host_t *this);
149
150 /**
151 * @brief Compare the ips of two hosts hosts.
152 *
153 * @param this object to compare
154 * @param other the other to compare
155 * @return TRUE if addresses are equal.
156 */
157 bool (*ip_is_equal) (host_t *this, host_t *other);
158
159 /**
160 * @brief Destroy this host object
161 *
162 * @param this calling
163 * @return SUCCESS in any case
164 */
165 void (*destroy) (host_t *this);
166 };
167
168 /**
169 * @brief Constructor to create a host_t object
170 *
171 * Currently supports only IPv4!
172 *
173 * @param family Address family to use for this object, such as AF_INET or AF_INET6
174 * @param address string of an address, such as "152.96.193.130"
175 * @param port port number
176 * @return
177 * - host_t object
178 * - NULL, if family not supported.
179 *
180 * @ingroup network
181 */
182 host_t *host_create(int family, char *address, u_int16_t port);
183
184 /**
185 * @brief Constructor to create a host_t object
186 *
187 * Currently supports only IPv4!
188 *
189 * @param family Address family to use for this object, such as AF_INET or AF_INET6
190 * @param address address as 4 byte chunk_t in networ order
191 * @param port port number
192 * @return
193 * - host_t object
194 * - NULL, if family not supported or chunk_t length not 4 bytes.
195 *
196 * @ingroup network
197 */
198 host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port);
199
200
201 #endif /*HOST_H_*/