fa06b14742aa9ba1d87c9d79efae17a7f691d9d2
[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
33 #include <types.h>
34
35
36 typedef struct host_t host_t;
37
38 /**
39 * @brief Representates a Host
40 *
41 * Host object, identifies a host and defines some useful functions on it.
42 *
43 * @ingroup network
44 */
45 struct host_t {
46 /**
47 * @brief Build a clone of this host object.
48 *
49 * @param this object to clone
50 * @return cloned host
51 */
52 host_t *(*clone) (host_t *this);
53
54 /**
55 * @brief Get a pointer to the internal sockaddr struct.
56 *
57 * This is used for sending and receiving via sockets.
58 *
59 * @param this object to clone
60 * @return pointer to the internal sockaddr structure
61 */
62 sockaddr_t *(*get_sockaddr) (host_t *this);
63
64 /**
65 * @brief Get the length of the sockaddr struct.
66 *
67 * Sepending on the family, the length of the sockaddr struct
68 * is different. Use this function to get the length of the sockaddr
69 * struct returned by get_sock_addr.
70 *
71 * This is used for sending and receiving via sockets.
72 *
73 * @param this object to clone
74 * @return length of the sockaddr struct
75 */
76 socklen_t *(*get_sockaddr_len) (host_t *this);
77
78 /**
79 * @brief get the address of this host
80 *
81 * Mostly used for debugging purposes.
82 * @warning string must NOT be freed
83 *
84 * @param this object to clone
85 * @return address string,
86 */
87 char* (*get_address) (host_t *this);
88
89 /**
90 * @brief get the address of this host as chunk_t
91 *
92 * @warning returned chunk has to get destroyed by caller.
93 *
94 * @param this object to clone
95 * @return address string,
96 */
97 chunk_t (*get_address_as_chunk) (host_t *this);
98
99 /**
100 * @brief get the port of this host
101 *
102 * Mostly used for debugging purposes.
103 *
104 * @param this object to clone
105 * @return port number
106 */
107 u_int16_t (*get_port) (host_t *this);
108
109 /**
110 * @brief Destroy this host object
111 *
112 * @param this calling
113 * @return SUCCESS in any case
114 */
115 void (*destroy) (host_t *this);
116 };
117
118 /**
119 * @brief Constructor to create a host_t object
120 *
121 * Currently supports only IPv4!
122 *
123 * @param family Address family to use for this object, such as AF_INET or AF_INET6
124 * @param address string of an address, such as "152.96.193.130"
125 * @param port port number
126 * @return
127 * - the host_t object, or
128 * - NULL, when family not supported.
129 *
130 * @ingroup network
131 */
132 host_t *host_create(int family, char *address, u_int16_t port);
133
134 /**
135 * @brief Constructor to create a host_t object
136 *
137 * Currently supports only IPv4!
138 *
139 * @param family Address family to use for this object, such as AF_INET or AF_INET6
140 * @param address address as 4 byte chunk_t in networ order
141 * @param port port number
142 * @return
143 * - the host_t object, or
144 * - NULL, when family not supported or chunk_t length not 4 bytes.
145 *
146 * @ingroup network
147 */
148 host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port);
149 #endif /*HOST_H_*/