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_; |
|
|
39 | } |
|
|
40 | |
|
|
41 | void sockinfo::set (const char *hostname, u16 port_, u8 prot_) |
|
|
42 | { |
|
|
43 | prot = prot_; |
|
|
44 | host = 0; |
|
|
45 | port = htons (port_); |
|
|
46 | |
|
|
47 | if (prot & PROTv4 |
|
|
48 | && hostname) |
|
|
49 | { |
|
|
50 | struct hostent *he = gethostbyname (hostname); |
|
|
51 | |
|
|
52 | if (he |
|
|
53 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
|
|
54 | { |
|
|
55 | //sa->sin_family = he->h_addrtype; |
|
|
56 | memcpy (&host, he->h_addr_list[0], 4); |
|
|
57 | } |
|
|
58 | else |
|
|
59 | slog (L_NOTICE, _("unable to resolve host '%s'"), hostname); |
|
|
60 | } |
34 | } |
61 | } |
35 | |
62 | |
36 | void |
63 | void |
37 | sockinfo::set (const conf_node *conf, u8 prot_) |
64 | sockinfo::set (const conf_node *conf, u8 prot_) |
38 | { |
65 | { |
39 | prot = prot_; |
66 | if (prot_ == PROT_DNSv4) |
40 | host = 0; |
67 | set (conf->dns_hostname, conf->dns_hostname ? conf->dns_port : 0, prot_); |
|
|
68 | else |
|
|
69 | set (conf->hostname, |
41 | port = prot == PROT_UDPv4 ? htons (conf->udp_port) |
70 | prot_ == PROT_UDPv4 ? conf->udp_port |
42 | : prot == PROT_TCPv4 ? htons (conf->tcp_port) |
71 | : prot_ == PROT_TCPv4 ? conf->tcp_port |
|
|
72 | : prot_ == PROT_DNSv4 ? conf->dns_port |
43 | : 0; |
73 | : 0, |
44 | |
74 | prot_); |
45 | if (prot && conf->hostname) |
|
|
46 | { |
|
|
47 | struct hostent *he = gethostbyname (conf->hostname); |
|
|
48 | |
|
|
49 | if (he |
|
|
50 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
|
|
51 | { |
|
|
52 | //sa->sin_family = he->h_addrtype; |
|
|
53 | memcpy (&host, he->h_addr_list[0], 4); |
|
|
54 | |
|
|
55 | } |
|
|
56 | else |
|
|
57 | slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname); |
|
|
58 | } |
|
|
59 | } |
75 | } |
60 | |
|
|
61 | |
76 | |
62 | const sockaddr * |
77 | const sockaddr * |
63 | sockinfo::sav4() const |
78 | sockinfo::sav4() const |
64 | { |
79 | { |
65 | static sockaddr_in sa; |
80 | static sockaddr_in sa; |
… | |
… | |
69 | sa.sin_addr.s_addr = host; |
84 | sa.sin_addr.s_addr = host; |
70 | |
85 | |
71 | return (const sockaddr *)&sa; |
86 | return (const sockaddr *)&sa; |
72 | } |
87 | } |
73 | |
88 | |
74 | static char hostport[10 + 15 + 1 + 5 + 1]; // protype / IPv4 : port |
89 | static char hostport[10 + 15 + 1 + 5 + 1]; // proto / IPv4 : port |
75 | |
90 | |
76 | const char * |
91 | const char * |
77 | sockinfo::ntoa () const |
92 | sockinfo::ntoa () const |
78 | { |
93 | { |
79 | in_addr ia = { host }; |
94 | in_addr ia = { host }; |
… | |
… | |
96 | sockinfo::supported_protocols (conf_node *conf) |
111 | sockinfo::supported_protocols (conf_node *conf) |
97 | { |
112 | { |
98 | u8 protocols = prot; |
113 | u8 protocols = prot; |
99 | |
114 | |
100 | if (prot & (PROT_UDPv4 | PROT_TCPv4)) |
115 | if (prot & (PROT_UDPv4 | PROT_TCPv4)) |
101 | protocols |= PROT_IPv4; |
116 | protocols |= PROT_IPv4 | PROT_ICMPv4; |
102 | |
117 | |
103 | if (conf |
118 | if (conf |
104 | && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) |
119 | && prot & PROTv4 |
105 | && conf->protocols & PROT_UDPv4 |
120 | && conf->protocols & PROT_UDPv4 |
106 | && conf->udp_port) |
121 | && conf->udp_port) |
107 | protocols |= PROT_UDPv4; |
122 | protocols |= PROT_UDPv4; |
108 | |
123 | |
109 | if (conf |
124 | if (conf |
110 | && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) |
125 | && prot & PROTv4 |
111 | && conf->protocols & PROT_TCPv4 |
126 | && conf->protocols & PROT_TCPv4 |
112 | && conf->tcp_port) |
127 | && conf->tcp_port) |
113 | protocols |= PROT_TCPv4; |
128 | protocols |= PROT_TCPv4; |
114 | |
129 | |
|
|
130 | if (conf |
|
|
131 | && prot & PROTv4 |
|
|
132 | && conf->protocols & PROT_DNSv4 |
|
|
133 | && conf->dns_port) |
|
|
134 | protocols |= PROT_DNSv4; |
|
|
135 | |
115 | return protocols; |
136 | return protocols; |
116 | } |
137 | } |
117 | |
138 | |
118 | bool |
139 | bool |
119 | sockinfo::upgrade_protocol (u8 prot_, conf_node *conf) |
140 | sockinfo::upgrade_protocol (u8 prot_, conf_node *conf) |
120 | { |
141 | { |
121 | if (prot_ == prot) |
142 | if (prot_ == prot) |
122 | return true; |
143 | return true; |
123 | |
144 | |
124 | if (prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) |
145 | if (prot & PROTv4 |
125 | && prot_ & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4)) |
146 | && prot_ & PROTv4) |
126 | { |
147 | { |
127 | if (prot_ & PROT_IPv4) |
148 | if (prot_ & (PROT_IPv4 | PROT_ICMPv4)) |
128 | { |
149 | { |
129 | prot = prot_; |
150 | prot = prot_; |
130 | port = 0; |
151 | port = 0; |
131 | return true; |
152 | return true; |
132 | } |
153 | } |
… | |
… | |
148 | { |
169 | { |
149 | prot = prot_; |
170 | prot = prot_; |
150 | port = htons (conf->tcp_port); |
171 | port = htons (conf->tcp_port); |
151 | return true; |
172 | return true; |
152 | } |
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 |
153 | } |
185 | } |
154 | |
186 | |
155 | return false; |
187 | return false; |
156 | } |
188 | } |
157 | |
189 | |