1 |
/* |
2 |
sockinfo.C -- socket address management |
3 |
Copyright (C) 2003 Marc Lehmann <gvpe@schmorp.de> |
4 |
|
5 |
This file is part of GVPE. |
6 |
|
7 |
GVPE is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License |
18 |
along with gvpe; if not, write to the Free Software |
19 |
Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 |
*/ |
21 |
|
22 |
#include "config.h" |
23 |
|
24 |
#include <netdb.h> |
25 |
|
26 |
#include "gettext.h" |
27 |
|
28 |
#include "sockinfo.h" |
29 |
#include "slog.h" |
30 |
|
31 |
#include <cstring> |
32 |
#include <cstdio> |
33 |
|
34 |
// all ipv4-based protocols |
35 |
#define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4 | PROT_DNSv4) |
36 |
|
37 |
void sockinfo::set (const sockaddr_in *sa, u8 prot_) |
38 |
{ |
39 |
host = sa->sin_addr.s_addr; |
40 |
port = prot_ & (PROT_IPv4 | PROT_ICMPv4) ? 0 : sa->sin_port; |
41 |
prot = prot_; |
42 |
} |
43 |
|
44 |
void sockinfo::set (const char *hostname, u16 port_, u8 prot_) |
45 |
{ |
46 |
prot = prot_; |
47 |
host = 0; |
48 |
port = htons (port_); |
49 |
|
50 |
if (prot & PROTv4 |
51 |
&& hostname) |
52 |
{ |
53 |
struct hostent *he = gethostbyname (hostname); |
54 |
|
55 |
if (he |
56 |
&& he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
57 |
{ |
58 |
//sa->sin_family = he->h_addrtype; |
59 |
memcpy (&host, he->h_addr_list[0], 4); |
60 |
} |
61 |
else |
62 |
slog (L_NOTICE, _("unable to resolve host '%s'"), hostname); |
63 |
} |
64 |
} |
65 |
|
66 |
void |
67 |
sockinfo::set (const conf_node *conf, u8 prot_) |
68 |
{ |
69 |
if (prot_ == PROT_DNSv4) |
70 |
{ |
71 |
host = htonl (conf->id); port = 0; prot = prot_; |
72 |
} |
73 |
else |
74 |
set (conf->hostname, |
75 |
prot_ == PROT_UDPv4 ? conf->udp_port |
76 |
: prot_ == PROT_TCPv4 ? conf->tcp_port |
77 |
: prot_ == PROT_DNSv4 ? conf->dns_port |
78 |
: 0, |
79 |
prot_); |
80 |
} |
81 |
|
82 |
const sockaddr * |
83 |
sockinfo::sav4() const |
84 |
{ |
85 |
static sockaddr_in sa; |
86 |
|
87 |
sa.sin_family = AF_INET; |
88 |
sa.sin_port = port; |
89 |
sa.sin_addr.s_addr = host; |
90 |
|
91 |
return (const sockaddr *)&sa; |
92 |
} |
93 |
|
94 |
static char hostport[10 + 15 + 1 + 5 + 1]; // proto / IPv4 : port |
95 |
|
96 |
const char * |
97 |
sockinfo::ntoa () const |
98 |
{ |
99 |
in_addr ia = { host }; |
100 |
|
101 |
sprintf (hostport, "%.15s", inet_ntoa (ia)); |
102 |
|
103 |
return hostport; |
104 |
} |
105 |
|
106 |
sockinfo::operator const char *() const |
107 |
{ |
108 |
in_addr ia = { host }; |
109 |
|
110 |
sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff); |
111 |
|
112 |
return hostport; |
113 |
} |
114 |
|
115 |
u8 |
116 |
sockinfo::supported_protocols (conf_node *conf) |
117 |
{ |
118 |
u8 protocols = prot; |
119 |
|
120 |
if (prot & (PROT_UDPv4 | PROT_TCPv4)) |
121 |
protocols |= PROT_IPv4 | PROT_ICMPv4; |
122 |
|
123 |
if (conf |
124 |
&& prot & PROTv4 |
125 |
&& conf->protocols & PROT_UDPv4 |
126 |
&& conf->udp_port) |
127 |
protocols |= PROT_UDPv4; |
128 |
|
129 |
if (conf |
130 |
&& prot & PROTv4 |
131 |
&& conf->protocols & PROT_TCPv4 |
132 |
&& conf->tcp_port) |
133 |
protocols |= PROT_TCPv4; |
134 |
|
135 |
if (conf |
136 |
&& prot & PROTv4 |
137 |
&& conf->protocols & PROT_DNSv4 |
138 |
&& conf->dns_port) |
139 |
protocols |= PROT_DNSv4; |
140 |
|
141 |
return protocols; |
142 |
} |
143 |
|
144 |
bool |
145 |
sockinfo::upgrade_protocol (u8 prot_, conf_node *conf) |
146 |
{ |
147 |
if (prot_ == prot) |
148 |
return true; |
149 |
|
150 |
if (prot & PROTv4 |
151 |
&& prot_ & PROTv4) |
152 |
{ |
153 |
if (prot_ & (PROT_IPv4 | PROT_ICMPv4)) |
154 |
{ |
155 |
prot = prot_; |
156 |
port = 0; |
157 |
return true; |
158 |
} |
159 |
|
160 |
if (conf |
161 |
&& prot_ & PROT_UDPv4 |
162 |
&& conf->protocols & PROT_UDPv4 |
163 |
&& conf->udp_port) |
164 |
{ |
165 |
prot = prot_; |
166 |
port = htons (conf->udp_port); |
167 |
return true; |
168 |
} |
169 |
|
170 |
if (conf |
171 |
&& prot_ & PROT_TCPv4 |
172 |
&& conf->protocols & PROT_TCPv4 |
173 |
&& conf->tcp_port) |
174 |
{ |
175 |
prot = prot_; |
176 |
port = htons (conf->tcp_port); |
177 |
return true; |
178 |
} |
179 |
} |
180 |
|
181 |
return false; |
182 |
} |
183 |
|
184 |
bool |
185 |
operator == (const sockinfo &a, const sockinfo &b) |
186 |
{ |
187 |
return a.host == b.host && a.port == b.port && a.prot == b.prot; |
188 |
} |
189 |
|
190 |
bool |
191 |
operator < (const sockinfo &a, const sockinfo &b) |
192 |
{ |
193 |
return a.host < b.host |
194 |
|| (a.host == b.host && (a.port < b.port |
195 |
|| (a.port == b.port && a.prot < b.prot))); |
196 |
} |
197 |
|