1 | /* |
1 | /* |
2 | sockinfo.C -- socket address management |
2 | sockinfo.C -- socket address management |
|
|
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | |
4 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | 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 |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
16 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | */ |
18 | */ |
18 | |
19 | |
19 | #include "config.h" |
20 | #include "config.h" |
20 | |
21 | |
21 | #include <arpa/inet.h> |
|
|
22 | #include <netdb.h> |
22 | #include <netdb.h> |
23 | |
23 | |
24 | #include "gettext.h" |
24 | #include "gettext.h" |
25 | |
25 | |
26 | #include "sockinfo.h" |
26 | #include "sockinfo.h" |
27 | #include "slog.h" |
27 | #include "slog.h" |
28 | |
28 | |
29 | // all ipv4-based protocols |
29 | // all ipv4-based protocols |
30 | #define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4) |
30 | #define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4 | PROT_DNSv4) |
31 | |
31 | |
32 | void sockinfo::set (const sockaddr_in *sa, u8 prot_) |
32 | void sockinfo::set (const sockaddr_in *sa, u8 prot_) |
33 | { |
33 | { |
34 | host = sa->sin_addr.s_addr; |
34 | host = sa->sin_addr.s_addr; |
35 | port = prot_ & (PROT_IPv4 | PROT_ICMPv4) ? 0 : sa->sin_port; |
35 | port = prot_ & (PROT_IPv4 | PROT_ICMPv4) ? 0 : sa->sin_port; |
… | |
… | |
59 | } |
59 | } |
60 | |
60 | |
61 | void |
61 | void |
62 | sockinfo::set (const conf_node *conf, u8 prot_) |
62 | sockinfo::set (const conf_node *conf, u8 prot_) |
63 | { |
63 | { |
64 | set (conf->hostname, |
64 | set (prot_ == PROT_DNSv4 ? ::conf.dns_forw_host |
|
|
65 | : conf->hostname, |
65 | prot_ == PROT_UDPv4 ? conf->udp_port |
66 | prot_ == PROT_UDPv4 ? conf->udp_port |
66 | : prot_ == PROT_TCPv4 ? conf->tcp_port |
67 | : prot_ == PROT_TCPv4 ? conf->tcp_port |
|
|
68 | : prot_ == PROT_DNSv4 ? conf->dns_port |
67 | : 0, |
69 | : 0, |
68 | prot_); |
70 | prot_); |
69 | } |
71 | } |
70 | |
72 | |
71 | const sockaddr * |
73 | const sockaddr * |
72 | sockinfo::sav4() const |
74 | sockinfo::sav4() const |
73 | { |
75 | { |
… | |
… | |
119 | && prot & PROTv4 |
121 | && prot & PROTv4 |
120 | && conf->protocols & PROT_TCPv4 |
122 | && conf->protocols & PROT_TCPv4 |
121 | && conf->tcp_port) |
123 | && conf->tcp_port) |
122 | protocols |= PROT_TCPv4; |
124 | protocols |= PROT_TCPv4; |
123 | |
125 | |
|
|
126 | if (conf |
|
|
127 | && prot & PROTv4 |
|
|
128 | && conf->protocols & PROT_DNSv4 |
|
|
129 | && conf->dns_port) |
|
|
130 | protocols |= PROT_DNSv4; |
|
|
131 | |
124 | return protocols; |
132 | return protocols; |
125 | } |
133 | } |
126 | |
134 | |
127 | bool |
135 | bool |
128 | sockinfo::upgrade_protocol (u8 prot_, conf_node *conf) |
136 | sockinfo::upgrade_protocol (u8 prot_, conf_node *conf) |
… | |
… | |
157 | { |
165 | { |
158 | prot = prot_; |
166 | prot = prot_; |
159 | port = htons (conf->tcp_port); |
167 | port = htons (conf->tcp_port); |
160 | return true; |
168 | return true; |
161 | } |
169 | } |
|
|
170 | |
|
|
171 | if (conf |
|
|
172 | && prot_ & PROT_DNSv4 |
|
|
173 | && conf->protocols & PROT_DNSv4 |
|
|
174 | && conf->dns_port) |
|
|
175 | { |
|
|
176 | set (::conf.dns_forw_host, ::conf.dns_forw_port, prot_); |
|
|
177 | return true; |
|
|
178 | } |
162 | } |
179 | } |
163 | |
180 | |
164 | return false; |
181 | return false; |
165 | } |
182 | } |
166 | |
183 | |