- fixed doxyfile
[strongswan.git] / Source / charon / utils / host.c
1 /**
2 * @file host.c
3 *
4 * @brief host object, identifies a host and defines some useful functions on it.
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 #include "host.h"
24
25 #include <utils/allocator.h>
26
27 /**
28 * @brief The logger object.
29 */
30 typedef struct private_host_s private_host_t;
31 struct private_host_s {
32 /**
33 * Public data
34 */
35 host_t public;
36
37 /**
38 * Address family to use, such as AF_INET or AF_INET6
39 */
40 int family;
41
42 /**
43 * low-lewel structure, wich stores the address
44 */
45 sockaddr_t address;
46
47 /**
48 * length of address structure
49 */
50 socklen_t socklen;
51 };
52
53
54 /**
55 * implements host_t.get_sockaddr
56 */
57 static sockaddr_t *get_sockaddr(private_host_t *this)
58 {
59 return &(this->address);
60 }
61
62 /**
63 * implements host_t.get_sockaddr_len
64 */
65 static socklen_t *get_sockaddr_len(private_host_t *this)
66 {
67 return &(this->socklen);
68 }
69
70 /**
71 * implements host_t.get_address
72 */
73 static char *get_address(private_host_t *this)
74 {
75 switch (this->family)
76 {
77 case AF_INET:
78 {
79 struct sockaddr_in *sin = (struct sockaddr_in*)&(this->address);
80 return inet_ntoa(sin->sin_addr);
81 }
82 default:
83 {
84 return "(family not supported)";
85 }
86 }
87 }
88
89 /**
90 * implements host_t.get_port
91 */
92 static u_int16_t get_port(private_host_t *this)
93 {
94 switch (this->family)
95 {
96 case AF_INET:
97 {
98 struct sockaddr_in *sin = (struct sockaddr_in*)&(this->address);
99 return ntohs(sin->sin_port);
100 }
101 default:
102 {
103 return 0;
104 }
105 }
106 }
107
108 /**
109 * Implements host_t.destroy
110 */
111 static status_t destroy(private_host_t *this)
112 {
113 allocator_free(this);
114 return SUCCESS;
115 }
116
117 /**
118 * Implements host_t.clone.
119 */
120 static status_t clone(private_host_t *this, host_t **other)
121 {
122 private_host_t *new = allocator_alloc_thing(private_host_t);
123
124 if (new == NULL)
125 {
126 return OUT_OF_RES;
127 }
128
129 memcpy(new, this, sizeof(private_host_t));
130 *other = (host_t*)new;
131
132 return SUCCESS;
133 }
134
135
136 /*
137 * see header
138 */
139 host_t *host_create(int family, char *address, u_int16_t port)
140 {
141 private_host_t *this = allocator_alloc_thing(private_host_t);
142 if (this == NULL)
143 {
144 return NULL;
145 }
146
147 this->public.get_sockaddr = (sockaddr_t* (*) (host_t*))get_sockaddr;
148 this->public.get_sockaddr_len = (socklen_t*(*) (host_t*))get_sockaddr_len;
149 this->public.clone = (status_t (*) (host_t*, host_t**))clone;
150 this->public.get_address = (char* (*) (host_t *))get_address;
151 this->public.get_port = (u_int16_t (*) (host_t *))get_port;
152 this->public.destroy = (status_t (*) (host_t*))destroy;
153
154 this->family = family;
155
156 switch (family)
157 {
158 /* IPv4 */
159 case AF_INET:
160 {
161 struct sockaddr_in *sin = (struct sockaddr_in*)&(this->address);
162 sin->sin_family = AF_INET;
163 sin->sin_addr.s_addr = inet_addr(address);
164 sin->sin_port = htons(port);
165 this->socklen = sizeof(struct sockaddr_in);
166 return (host_t*)this;
167 }
168 }
169 allocator_free(this);
170 return NULL;
171 }