1 | /* |
1 | /* |
2 | sockinfo.c -- socket address management |
2 | sockinfo.C -- socket address management |
3 | |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
4 | 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 |
5 | 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 |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
7 | (at your option) any later version. |
… | |
… | |
31 | host = sa->sin_addr.s_addr; |
31 | host = sa->sin_addr.s_addr; |
32 | port = prot_ == PROT_IPv4 ? 0 : sa->sin_port; |
32 | port = prot_ == PROT_IPv4 ? 0 : sa->sin_port; |
33 | prot = prot_; |
33 | prot = prot_; |
34 | } |
34 | } |
35 | |
35 | |
36 | void |
36 | void sockinfo::set (const char *hostname, u16 port_, u8 prot_) |
37 | sockinfo::set (const conf_node *conf) |
|
|
38 | { |
37 | { |
|
|
38 | prot = prot_; |
39 | host = 0; |
39 | host = 0; |
40 | port = htons (conf->udp_port); |
40 | port = htons (port_); |
41 | |
41 | |
42 | if (conf->hostname) |
42 | if (prot & (PROT_UDPv4 | PROT_TCPv4 | PROT_IPv4) |
|
|
43 | && hostname) |
43 | { |
44 | { |
44 | struct hostent *he = gethostbyname (conf->hostname); |
45 | struct hostent *he = gethostbyname (hostname); |
45 | |
46 | |
46 | if (he |
47 | if (he |
47 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
48 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
48 | { |
49 | { |
49 | //sa->sin_family = he->h_addrtype; |
50 | //sa->sin_family = he->h_addrtype; |
50 | memcpy (&host, he->h_addr_list[0], 4); |
51 | memcpy (&host, he->h_addr_list[0], 4); |
51 | |
|
|
52 | prot = PROT_UDPv4 | PROT_IPv4; |
|
|
53 | } |
52 | } |
54 | else |
53 | else |
55 | slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname); |
54 | slog (L_NOTICE, _("unable to resolve host '%s'"), hostname); |
56 | } |
55 | } |
57 | } |
56 | } |
58 | |
57 | |
|
|
58 | void |
|
|
59 | sockinfo::set (const conf_node *conf, u8 prot_) |
|
|
60 | { |
|
|
61 | set (conf->hostname, |
|
|
62 | prot_ == PROT_UDPv4 ? conf->udp_port |
|
|
63 | : prot_ == PROT_TCPv4 ? conf->tcp_port |
|
|
64 | : 0, |
|
|
65 | prot_); |
|
|
66 | } |
59 | |
67 | |
60 | const sockaddr * |
68 | const sockaddr * |
61 | sockinfo::sav4() const |
69 | sockinfo::sav4() const |
62 | { |
70 | { |
63 | static sockaddr_in sa; |
71 | static sockaddr_in sa; |
… | |
… | |
67 | sa.sin_addr.s_addr = host; |
75 | sa.sin_addr.s_addr = host; |
68 | |
76 | |
69 | return (const sockaddr *)&sa; |
77 | return (const sockaddr *)&sa; |
70 | } |
78 | } |
71 | |
79 | |
72 | static char hostport[15 + 1 + 5 + 1]; // IPv4 : port |
80 | static char hostport[10 + 15 + 1 + 5 + 1]; // proto / IPv4 : port |
73 | |
81 | |
74 | const char * |
82 | const char * |
75 | sockinfo::ntoa () const |
83 | sockinfo::ntoa () const |
76 | { |
84 | { |
77 | in_addr ia = { host }; |
85 | in_addr ia = { host }; |
… | |
… | |
83 | |
91 | |
84 | sockinfo::operator const char *() const |
92 | sockinfo::operator const char *() const |
85 | { |
93 | { |
86 | in_addr ia = { host }; |
94 | in_addr ia = { host }; |
87 | |
95 | |
88 | sprintf (hostport, "%.15s:%d", inet_ntoa (ia), ntohs (port) & 0xffff); |
96 | sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff); |
89 | |
97 | |
90 | return hostport; |
98 | return hostport; |
91 | } |
99 | } |
92 | |
100 | |
|
|
101 | u8 |
|
|
102 | sockinfo::supported_protocols (conf_node *conf) |
|
|
103 | { |
|
|
104 | u8 protocols = prot; |
|
|
105 | |
|
|
106 | if (prot & (PROT_UDPv4 | PROT_TCPv4)) |
|
|
107 | protocols |= PROT_IPv4; |
|
|
108 | |
|
|
109 | if (conf |
|
|
110 | && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) |
|
|
111 | && conf->protocols & PROT_UDPv4 |
|
|
112 | && conf->udp_port) |
|
|
113 | protocols |= PROT_UDPv4; |
|
|
114 | |
|
|
115 | if (conf |
|
|
116 | && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) |
|
|
117 | && conf->protocols & PROT_TCPv4 |
|
|
118 | && conf->tcp_port) |
|
|
119 | protocols |= PROT_TCPv4; |
|
|
120 | |
|
|
121 | return protocols; |
|
|
122 | } |
|
|
123 | |
|
|
124 | bool |
|
|
125 | sockinfo::upgrade_protocol (u8 prot_, conf_node *conf) |
|
|
126 | { |
|
|
127 | if (prot_ == prot) |
|
|
128 | return true; |
|
|
129 | |
|
|
130 | if (prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) |
|
|
131 | && prot_ & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4)) |
|
|
132 | { |
|
|
133 | if (prot_ & PROT_IPv4) |
|
|
134 | { |
|
|
135 | prot = prot_; |
|
|
136 | port = 0; |
|
|
137 | return true; |
|
|
138 | } |
|
|
139 | |
|
|
140 | if (conf |
|
|
141 | && prot_ & PROT_UDPv4 |
|
|
142 | && conf->protocols & PROT_UDPv4 |
|
|
143 | && conf->udp_port) |
|
|
144 | { |
|
|
145 | prot = prot_; |
|
|
146 | port = htons (conf->udp_port); |
|
|
147 | return true; |
|
|
148 | } |
|
|
149 | |
|
|
150 | if (conf |
|
|
151 | && prot_ & PROT_TCPv4 |
|
|
152 | && conf->protocols & PROT_TCPv4 |
|
|
153 | && conf->tcp_port) |
|
|
154 | { |
|
|
155 | prot = prot_; |
|
|
156 | port = htons (conf->tcp_port); |
|
|
157 | return true; |
|
|
158 | } |
|
|
159 | } |
|
|
160 | |
|
|
161 | return false; |
|
|
162 | } |
|
|
163 | |
|
|
164 | bool |
|
|
165 | operator == (const sockinfo &a, const sockinfo &b) |
|
|
166 | { |
|
|
167 | return a.host == b.host && a.port == b.port && a.prot == b.prot; |
|
|
168 | } |
|
|
169 | |
|
|
170 | bool |
|
|
171 | operator < (const sockinfo &a, const sockinfo &b) |
|
|
172 | { |
|
|
173 | return a.host < b.host |
|
|
174 | || (a.host == b.host && (a.port < b.port |
|
|
175 | || (a.port == b.port && a.prot < b.prot))); |
|
|
176 | } |
|
|
177 | |