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 | |
|
|
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 |
|
|
30 | #define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4 | PROT_DNSv4) |
|
|
31 | |
29 | void sockinfo::set (const sockaddr_in *sa, u8 prot_) |
32 | void sockinfo::set (const sockaddr_in *sa, u8 prot_) |
30 | { |
33 | { |
31 | host = sa->sin_addr.s_addr; |
34 | host = sa->sin_addr.s_addr; |
32 | port = prot_ == PROT_IPv4 ? 0 : sa->sin_port; |
35 | port = prot_ & (PROT_IPv4 | PROT_ICMPv4) ? 0 : sa->sin_port; |
33 | prot = prot_; |
36 | prot = prot_; |
34 | } |
37 | } |
35 | |
38 | |
36 | void |
39 | void sockinfo::set (const char *hostname, u16 port_, u8 prot_) |
37 | sockinfo::set (const conf_node *conf, u8 prot_) |
|
|
38 | { |
40 | { |
39 | prot = prot_; |
41 | prot = prot_; |
40 | host = 0; |
42 | host = 0; |
41 | port = prot_ == PROT_UDPv4 ? htons (conf->udp_port) |
43 | port = htons (port_); |
42 | : prot_ == PROT_TCPv4 ? htons (conf->tcp_port) |
|
|
43 | : 0; |
|
|
44 | |
44 | |
45 | if (conf->hostname) |
45 | if (prot & PROTv4 |
|
|
46 | && hostname) |
46 | { |
47 | { |
47 | struct hostent *he = gethostbyname (conf->hostname); |
48 | struct hostent *he = gethostbyname (hostname); |
48 | |
49 | |
49 | if (he |
50 | if (he |
50 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
51 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
51 | { |
52 | { |
52 | //sa->sin_family = he->h_addrtype; |
53 | //sa->sin_family = he->h_addrtype; |
53 | memcpy (&host, he->h_addr_list[0], 4); |
54 | memcpy (&host, he->h_addr_list[0], 4); |
54 | |
|
|
55 | } |
55 | } |
56 | else |
56 | else |
57 | slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname); |
57 | slog (L_NOTICE, _("unable to resolve host '%s'"), hostname); |
58 | } |
58 | } |
59 | } |
59 | } |
60 | |
60 | |
|
|
61 | void |
|
|
62 | sockinfo::set (const conf_node *conf, u8 prot_) |
|
|
63 | { |
|
|
64 | if (prot_ == PROT_DNSv4) |
|
|
65 | { |
|
|
66 | host = htonl (conf->id); port = 0; prot = prot_; |
|
|
67 | } |
|
|
68 | else |
|
|
69 | set (conf->hostname, |
|
|
70 | prot_ == PROT_UDPv4 ? conf->udp_port |
|
|
71 | : prot_ == PROT_TCPv4 ? conf->tcp_port |
|
|
72 | : prot_ == PROT_DNSv4 ? conf->dns_port |
|
|
73 | : 0, |
|
|
74 | prot_); |
|
|
75 | } |
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[15 + 1 + 5 + 1]; // 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 }; |
… | |
… | |
85 | |
100 | |
86 | sockinfo::operator const char *() const |
101 | sockinfo::operator const char *() const |
87 | { |
102 | { |
88 | in_addr ia = { host }; |
103 | in_addr ia = { host }; |
89 | |
104 | |
90 | sprintf (hostport, "%.15s:%d", inet_ntoa (ia), ntohs (port) & 0xffff); |
105 | sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff); |
91 | |
106 | |
92 | return hostport; |
107 | return hostport; |
93 | } |
108 | } |
94 | |
109 | |
|
|
110 | u8 |
|
|
111 | sockinfo::supported_protocols (conf_node *conf) |
|
|
112 | { |
|
|
113 | u8 protocols = prot; |
|
|
114 | |
|
|
115 | if (prot & (PROT_UDPv4 | PROT_TCPv4)) |
|
|
116 | protocols |= PROT_IPv4 | PROT_ICMPv4; |
|
|
117 | |
|
|
118 | if (conf |
|
|
119 | && prot & PROTv4 |
|
|
120 | && conf->protocols & PROT_UDPv4 |
|
|
121 | && conf->udp_port) |
|
|
122 | protocols |= PROT_UDPv4; |
|
|
123 | |
|
|
124 | if (conf |
|
|
125 | && prot & PROTv4 |
|
|
126 | && conf->protocols & PROT_TCPv4 |
|
|
127 | && conf->tcp_port) |
|
|
128 | protocols |= PROT_TCPv4; |
|
|
129 | |
|
|
130 | if (conf |
|
|
131 | && prot & PROTv4 |
|
|
132 | && conf->protocols & PROT_DNSv4 |
|
|
133 | && conf->dns_port) |
|
|
134 | protocols |= PROT_DNSv4; |
|
|
135 | |
|
|
136 | return protocols; |
|
|
137 | } |
|
|
138 | |
|
|
139 | bool |
|
|
140 | sockinfo::upgrade_protocol (u8 prot_, conf_node *conf) |
|
|
141 | { |
|
|
142 | if (prot_ == prot) |
|
|
143 | return true; |
|
|
144 | |
|
|
145 | if (prot & PROTv4 |
|
|
146 | && prot_ & PROTv4) |
|
|
147 | { |
|
|
148 | if (prot_ & (PROT_IPv4 | PROT_ICMPv4)) |
|
|
149 | { |
|
|
150 | prot = prot_; |
|
|
151 | port = 0; |
|
|
152 | return true; |
|
|
153 | } |
|
|
154 | |
|
|
155 | if (conf |
|
|
156 | && prot_ & PROT_UDPv4 |
|
|
157 | && conf->protocols & PROT_UDPv4 |
|
|
158 | && conf->udp_port) |
|
|
159 | { |
|
|
160 | prot = prot_; |
|
|
161 | port = htons (conf->udp_port); |
|
|
162 | return true; |
|
|
163 | } |
|
|
164 | |
|
|
165 | if (conf |
|
|
166 | && prot_ & PROT_TCPv4 |
|
|
167 | && conf->protocols & PROT_TCPv4 |
|
|
168 | && conf->tcp_port) |
|
|
169 | { |
|
|
170 | prot = prot_; |
|
|
171 | port = htons (conf->tcp_port); |
|
|
172 | return true; |
|
|
173 | } |
|
|
174 | } |
|
|
175 | |
|
|
176 | return false; |
|
|
177 | } |
|
|
178 | |
|
|
179 | bool |
|
|
180 | operator == (const sockinfo &a, const sockinfo &b) |
|
|
181 | { |
|
|
182 | return a.host == b.host && a.port == b.port && a.prot == b.prot; |
|
|
183 | } |
|
|
184 | |
|
|
185 | bool |
|
|
186 | operator < (const sockinfo &a, const sockinfo &b) |
|
|
187 | { |
|
|
188 | return a.host < b.host |
|
|
189 | || (a.host == b.host && (a.port < b.port |
|
|
190 | || (a.port == b.port && a.prot < b.prot))); |
|
|
191 | } |
|
|
192 | |