1 | /* |
1 | /* |
2 | sockinfo.C -- socket address management |
2 | sockinfo.C -- socket address management |
|
|
3 | Copyright (C) 2003 Marc Lehmann <gvpe@schmorp.de> |
3 | |
4 | |
|
|
5 | This file is part of GVPE. |
|
|
6 | |
4 | This program is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
8 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
9 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
10 | (at your option) any later version. |
8 | |
11 | |
9 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
15 | GNU General Public License for more details. |
13 | |
16 | |
14 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
16 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | */ |
20 | */ |
18 | |
21 | |
19 | #include "config.h" |
22 | #include "config.h" |
20 | |
23 | |
21 | #include <arpa/inet.h> |
|
|
22 | #include <netdb.h> |
24 | #include <netdb.h> |
23 | |
25 | |
24 | #include "gettext.h" |
26 | #include "gettext.h" |
25 | |
27 | |
26 | #include "sockinfo.h" |
28 | #include "sockinfo.h" |
27 | #include "slog.h" |
29 | #include "slog.h" |
28 | |
30 | |
|
|
31 | // all ipv4-based protocols |
|
|
32 | #define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4 | PROT_DNSv4) |
|
|
33 | |
29 | void sockinfo::set (const sockaddr_in *sa, u8 prot_) |
34 | void sockinfo::set (const sockaddr_in *sa, u8 prot_) |
30 | { |
35 | { |
31 | host = sa->sin_addr.s_addr; |
36 | host = sa->sin_addr.s_addr; |
32 | port = prot_ == PROT_IPv4 ? 0 : sa->sin_port; |
37 | port = prot_ & (PROT_IPv4 | PROT_ICMPv4) ? 0 : sa->sin_port; |
33 | prot = prot_; |
38 | prot = prot_; |
34 | } |
39 | } |
35 | |
40 | |
36 | void sockinfo::set (const char *hostname, u16 port_, u8 prot_) |
41 | void sockinfo::set (const char *hostname, u16 port_, u8 prot_) |
37 | { |
42 | { |
38 | prot = prot_; |
43 | prot = prot_; |
39 | host = 0; |
44 | host = 0; |
40 | port = htons (port_); |
45 | port = htons (port_); |
41 | |
46 | |
42 | if (prot & (PROT_UDPv4 | PROT_TCPv4 | PROT_IPv4) |
47 | if (prot & PROTv4 |
43 | && hostname) |
48 | && hostname) |
44 | { |
49 | { |
45 | struct hostent *he = gethostbyname (hostname); |
50 | struct hostent *he = gethostbyname (hostname); |
46 | |
51 | |
47 | if (he |
52 | if (he |
… | |
… | |
56 | } |
61 | } |
57 | |
62 | |
58 | void |
63 | void |
59 | sockinfo::set (const conf_node *conf, u8 prot_) |
64 | sockinfo::set (const conf_node *conf, u8 prot_) |
60 | { |
65 | { |
|
|
66 | if (prot_ == PROT_DNSv4) |
|
|
67 | set (conf->dns_hostname, conf->dns_hostname ? conf->dns_port : 0, prot_); |
|
|
68 | else |
61 | set (conf->hostname, |
69 | set (conf->hostname, |
62 | prot_ == PROT_UDPv4 ? conf->udp_port |
70 | prot_ == PROT_UDPv4 ? conf->udp_port |
63 | : prot_ == PROT_TCPv4 ? conf->tcp_port |
71 | : prot_ == PROT_TCPv4 ? conf->tcp_port |
|
|
72 | : prot_ == PROT_DNSv4 ? conf->dns_port |
64 | : 0, |
73 | : 0, |
65 | prot_); |
74 | prot_); |
66 | } |
75 | } |
67 | |
76 | |
68 | const sockaddr * |
77 | const sockaddr * |
69 | sockinfo::sav4() const |
78 | sockinfo::sav4() const |
70 | { |
79 | { |
… | |
… | |
102 | sockinfo::supported_protocols (conf_node *conf) |
111 | sockinfo::supported_protocols (conf_node *conf) |
103 | { |
112 | { |
104 | u8 protocols = prot; |
113 | u8 protocols = prot; |
105 | |
114 | |
106 | if (prot & (PROT_UDPv4 | PROT_TCPv4)) |
115 | if (prot & (PROT_UDPv4 | PROT_TCPv4)) |
107 | protocols |= PROT_IPv4; |
116 | protocols |= PROT_IPv4 | PROT_ICMPv4; |
108 | |
117 | |
109 | if (conf |
118 | if (conf |
110 | && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) |
119 | && prot & PROTv4 |
111 | && conf->protocols & PROT_UDPv4 |
120 | && conf->protocols & PROT_UDPv4 |
112 | && conf->udp_port) |
121 | && conf->udp_port) |
113 | protocols |= PROT_UDPv4; |
122 | protocols |= PROT_UDPv4; |
114 | |
123 | |
115 | if (conf |
124 | if (conf |
116 | && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) |
125 | && prot & PROTv4 |
117 | && conf->protocols & PROT_TCPv4 |
126 | && conf->protocols & PROT_TCPv4 |
118 | && conf->tcp_port) |
127 | && conf->tcp_port) |
119 | protocols |= PROT_TCPv4; |
128 | protocols |= PROT_TCPv4; |
120 | |
129 | |
|
|
130 | if (conf |
|
|
131 | && prot & PROTv4 |
|
|
132 | && conf->protocols & PROT_DNSv4 |
|
|
133 | && conf->dns_port) |
|
|
134 | protocols |= PROT_DNSv4; |
|
|
135 | |
121 | return protocols; |
136 | return protocols; |
122 | } |
137 | } |
123 | |
138 | |
124 | bool |
139 | bool |
125 | sockinfo::upgrade_protocol (u8 prot_, conf_node *conf) |
140 | sockinfo::upgrade_protocol (u8 prot_, conf_node *conf) |
126 | { |
141 | { |
127 | if (prot_ == prot) |
142 | if (prot_ == prot) |
128 | return true; |
143 | return true; |
129 | |
144 | |
130 | if (prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) |
145 | if (prot & PROTv4 |
131 | && prot_ & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4)) |
146 | && prot_ & PROTv4) |
132 | { |
147 | { |
133 | if (prot_ & PROT_IPv4) |
148 | if (prot_ & (PROT_IPv4 | PROT_ICMPv4)) |
134 | { |
149 | { |
135 | prot = prot_; |
150 | prot = prot_; |
136 | port = 0; |
151 | port = 0; |
137 | return true; |
152 | return true; |
138 | } |
153 | } |
… | |
… | |
154 | { |
169 | { |
155 | prot = prot_; |
170 | prot = prot_; |
156 | port = htons (conf->tcp_port); |
171 | port = htons (conf->tcp_port); |
157 | return true; |
172 | return true; |
158 | } |
173 | } |
|
|
174 | |
|
|
175 | #if ENABLE_DNS |
|
|
176 | if (conf |
|
|
177 | && prot_ & PROT_DNSv4 |
|
|
178 | && conf->protocols & PROT_DNSv4 |
|
|
179 | && conf->dns_port) |
|
|
180 | { |
|
|
181 | set (::conf.dns_forw_host, ::conf.dns_forw_port, prot_); |
|
|
182 | return true; |
|
|
183 | } |
|
|
184 | #endif |
159 | } |
185 | } |
160 | |
186 | |
161 | return false; |
187 | return false; |
162 | } |
188 | } |
163 | |
189 | |