]>
Commit | Line | Data |
---|---|---|
050ce09b MW |
1 | /** |
2 | * @file host.h | |
3 | * | |
d048df5c | 4 | * @brief Interface of host_t. |
050ce09b MW |
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> | |
3ebebc5e | 32 | #include <linux/xfrm.h> |
050ce09b | 33 | |
021c2322 | 34 | #include <types.h> |
050ce09b | 35 | |
95c61cb9 JH |
36 | |
37 | typedef struct host_t host_t; | |
df3c59d0 | 38 | |
050ce09b | 39 | /** |
95c61cb9 | 40 | * @brief Representates a Host |
d048df5c | 41 | * |
2b547481 MW |
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() | |
0a373aec | 48 | * - host_create_from_sockaddr() |
2b547481 MW |
49 | * |
50 | * @todo Add IPv6 support | |
df3c59d0 MW |
51 | * |
52 | * @ingroup network | |
050ce09b | 53 | */ |
95c61cb9 | 54 | struct host_t { |
2b547481 | 55 | |
aa20c2ac MW |
56 | /** |
57 | * @brief Build a clone of this host object. | |
58 | * | |
59 | * @param this object to clone | |
d048df5c | 60 | * @return cloned host |
aa20c2ac | 61 | */ |
d048df5c MW |
62 | host_t *(*clone) (host_t *this); |
63 | ||
aa20c2ac MW |
64 | /** |
65 | * @brief Get a pointer to the internal sockaddr struct. | |
66 | * | |
67 | * This is used for sending and receiving via sockets. | |
68 | * | |
69 | * @param this object to clone | |
70 | * @return pointer to the internal sockaddr structure | |
71 | */ | |
050ce09b | 72 | sockaddr_t *(*get_sockaddr) (host_t *this); |
aa20c2ac MW |
73 | |
74 | /** | |
75 | * @brief Get the length of the sockaddr struct. | |
76 | * | |
77 | * Sepending on the family, the length of the sockaddr struct | |
78 | * is different. Use this function to get the length of the sockaddr | |
79 | * struct returned by get_sock_addr. | |
80 | * | |
81 | * This is used for sending and receiving via sockets. | |
82 | * | |
83 | * @param this object to clone | |
84 | * @return length of the sockaddr struct | |
85 | */ | |
050ce09b | 86 | socklen_t *(*get_sockaddr_len) (host_t *this); |
aa20c2ac | 87 | |
3ebebc5e MW |
88 | /** |
89 | * @brief Gets the address as xfrm_address_t. | |
2b547481 MW |
90 | * |
91 | * This function allows the conversion to an | |
92 | * xfrm_address_t, used for netlink communication | |
93 | * with the kernel. | |
94 | * | |
95 | * @see kernel_interface_t. | |
96 | * | |
97 | * @param this calling object | |
98 | * @return address in xfrm_address_t format | |
3ebebc5e MW |
99 | */ |
100 | xfrm_address_t (*get_xfrm_addr) (host_t *this); | |
101 | ||
102 | /** | |
2b547481 MW |
103 | * @brief Gets the family of the address |
104 | * | |
105 | * @param this calling object | |
106 | * @return family | |
3ebebc5e MW |
107 | */ |
108 | int (*get_family) (host_t *this); | |
109 | ||
aa20c2ac MW |
110 | /** |
111 | * @brief get the address of this host | |
112 | * | |
113 | * Mostly used for debugging purposes. | |
7da522ba | 114 | * @warning string must NOT be freed |
aa20c2ac | 115 | * |
a9428251 | 116 | * @param this object |
aa20c2ac MW |
117 | * @return address string, |
118 | */ | |
119 | char* (*get_address) (host_t *this); | |
7da522ba | 120 | |
a9428251 JH |
121 | /** |
122 | * @brief Checks if the ip address of host is set to default route. | |
123 | * | |
124 | * @param this calling object | |
125 | * @return | |
126 | * - TRUE if host has IP 0.0.0.0 for default route | |
127 | * - FALSE otherwise | |
128 | */ | |
129 | bool (*is_default_route) (host_t *this); | |
130 | ||
7da522ba JH |
131 | /** |
132 | * @brief get the address of this host as chunk_t | |
133 | * | |
134 | * @warning returned chunk has to get destroyed by caller. | |
135 | * | |
a9428251 | 136 | * @param this object |
7da522ba JH |
137 | * @return address string, |
138 | */ | |
139 | chunk_t (*get_address_as_chunk) (host_t *this); | |
aa20c2ac MW |
140 | |
141 | /** | |
142 | * @brief get the port of this host | |
143 | * | |
144 | * Mostly used for debugging purposes. | |
145 | * | |
146 | * @param this object to clone | |
147 | * @return port number | |
148 | */ | |
149 | u_int16_t (*get_port) (host_t *this); | |
d45ec1de MW |
150 | |
151 | /** | |
a9428251 | 152 | * @brief Compare the ips of two hosts hosts. |
d45ec1de MW |
153 | * |
154 | * @param this object to compare | |
155 | * @param other the other to compare | |
a9428251 | 156 | * @return TRUE if addresses are equal. |
d45ec1de | 157 | */ |
dec59822 MW |
158 | bool (*ip_equals) (host_t *this, host_t *other); |
159 | ||
160 | /** | |
161 | * @brief Compare two hosts, with port. | |
162 | * | |
163 | * @param this object to compare | |
164 | * @param other the other to compare | |
165 | * @return TRUE if addresses and ports are equal. | |
166 | */ | |
167 | bool (*equals) (host_t *this, host_t *other); | |
aa20c2ac MW |
168 | |
169 | /** | |
170 | * @brief Destroy this host object | |
171 | * | |
172 | * @param this calling | |
173 | * @return SUCCESS in any case | |
174 | */ | |
d048df5c | 175 | void (*destroy) (host_t *this); |
050ce09b MW |
176 | }; |
177 | ||
178 | /** | |
0a373aec | 179 | * @brief Constructor to create a host_t object from an address string |
aa20c2ac | 180 | * |
df3c59d0 | 181 | * Currently supports only IPv4! |
050ce09b | 182 | * |
aa20c2ac MW |
183 | * @param family Address family to use for this object, such as AF_INET or AF_INET6 |
184 | * @param address string of an address, such as "152.96.193.130" | |
185 | * @param port port number | |
df3c59d0 | 186 | * @return |
2b547481 MW |
187 | * - host_t object |
188 | * - NULL, if family not supported. | |
df3c59d0 MW |
189 | * |
190 | * @ingroup network | |
050ce09b MW |
191 | */ |
192 | host_t *host_create(int family, char *address, u_int16_t port); | |
050ce09b | 193 | |
7da522ba | 194 | /** |
0a373aec | 195 | * @brief Constructor to create a host_t object from an address chunk |
7da522ba JH |
196 | * |
197 | * Currently supports only IPv4! | |
198 | * | |
199 | * @param family Address family to use for this object, such as AF_INET or AF_INET6 | |
200 | * @param address address as 4 byte chunk_t in networ order | |
201 | * @param port port number | |
202 | * @return | |
2b547481 MW |
203 | * - host_t object |
204 | * - NULL, if family not supported or chunk_t length not 4 bytes. | |
7da522ba JH |
205 | * |
206 | * @ingroup network | |
207 | */ | |
208 | host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port); | |
2b547481 | 209 | |
0a373aec MW |
210 | /** |
211 | * @brief Constructor to create a host_t object from a sockaddr struct | |
212 | * | |
213 | * Currently supports only IPv4! | |
214 | * | |
215 | * @param sockaddr sockaddr struct which contains family, address and port | |
216 | * @return | |
217 | * - host_t object | |
218 | * - NULL, if family not supported. | |
219 | * | |
220 | * @ingroup network | |
221 | */ | |
222 | host_t *host_create_from_sockaddr(sockaddr_t *sockaddr); | |
223 | ||
2b547481 | 224 | |
050ce09b | 225 | #endif /*HOST_H_*/ |