… | |
… | |
29 | covered work. |
29 | covered work. |
30 | */ |
30 | */ |
31 | |
31 | |
32 | #include "config.h" |
32 | #include "config.h" |
33 | |
33 | |
|
|
34 | // for gethostbyname |
34 | #include <netdb.h> |
35 | #include <netdb.h> |
|
|
36 | |
|
|
37 | // for inet_aton |
|
|
38 | #include <sys/socket.h> |
|
|
39 | #include <netinet/in.h> |
|
|
40 | #include <arpa/inet.h> |
35 | |
41 | |
36 | #include "gettext.h" |
42 | #include "gettext.h" |
37 | |
43 | |
38 | #include "sockinfo.h" |
44 | #include "sockinfo.h" |
39 | #include "slog.h" |
45 | #include "slog.h" |
40 | |
46 | |
41 | #include <cstring> |
47 | #include <cstring> |
42 | #include <cstdio> |
48 | #include <cstdio> |
43 | |
49 | |
44 | // all ipv4-based protocols |
50 | // all ipv4-based protocols |
45 | #define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4 | PROT_DNSv4 | PROT_IPv42) |
51 | #define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4 | PROT_DNSv4) |
46 | |
52 | |
47 | void |
53 | void |
48 | sockinfo::set (const sockaddr_in *sa, u8 prot_) |
54 | sockinfo::set (const sockaddr_in *sa, u8 prot_) |
49 | { |
55 | { |
50 | host = sa->sin_addr.s_addr; |
56 | host = sa->sin_addr.s_addr; |
51 | port = prot_ & (PROT_IPv4 | PROT_ICMPv4 | PROT_IPv42) ? 0 : sa->sin_port; |
57 | port = prot_ & (PROT_IPv4 | PROT_ICMPv4) ? 0 : sa->sin_port; |
52 | prot = prot_; |
58 | prot = prot_; |
53 | } |
59 | } |
54 | |
60 | |
55 | void |
61 | void |
56 | sockinfo::set (const char *hostname, u16 port_, u8 prot_) |
62 | sockinfo::set (const char *hostname, u16 port_, u8 prot_) |
… | |
… | |
60 | port = htons (port_); |
66 | port = htons (port_); |
61 | |
67 | |
62 | if (prot & PROTv4 |
68 | if (prot & PROTv4 |
63 | && hostname) |
69 | && hostname) |
64 | { |
70 | { |
|
|
71 | struct in_addr in; |
|
|
72 | struct hostent *he; |
|
|
73 | |
|
|
74 | if (inet_aton (hostname, &in)) |
|
|
75 | host = in.s_addr; |
65 | struct hostent *he = gethostbyname (hostname); |
76 | else if ((he = gethostbyname (hostname)) |
66 | |
77 | && he->h_addrtype == AF_INET |
67 | if (he |
78 | && he->h_length == 4 |
68 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
79 | && he->h_addr_list[0]) |
69 | { |
80 | memcpy (&host, he->h_addr_list[0], 4); //sa->sin_family = he->h_addrtype; |
70 | //sa->sin_family = he->h_addrtype; |
|
|
71 | memcpy (&host, he->h_addr_list[0], 4); |
|
|
72 | } |
|
|
73 | else |
81 | else |
74 | slog (L_NOTICE, _("unable to resolve host '%s'"), hostname); |
82 | slog (L_NOTICE, _("unable to resolve host '%s'"), hostname); |
75 | } |
83 | } |
76 | } |
84 | } |
77 | |
85 | |
… | |
… | |
113 | sprintf (hostport, "%.15s", inet_ntoa (ia)); |
121 | sprintf (hostport, "%.15s", inet_ntoa (ia)); |
114 | |
122 | |
115 | return hostport; |
123 | return hostport; |
116 | } |
124 | } |
117 | |
125 | |
|
|
126 | bool |
|
|
127 | sockinfo::valid () const |
|
|
128 | { |
|
|
129 | return (prot & THISNODE->protocols) && host; |
|
|
130 | } |
|
|
131 | |
118 | sockinfo::operator const char *() const |
132 | sockinfo::operator const char *() const |
119 | { |
133 | { |
120 | in_addr ia = { host }; |
134 | in_addr ia = { host }; |
121 | |
135 | |
122 | sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff); |
136 | sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff); |
… | |
… | |
127 | u8 |
141 | u8 |
128 | sockinfo::supported_protocols (conf_node *conf) |
142 | sockinfo::supported_protocols (conf_node *conf) |
129 | { |
143 | { |
130 | u8 protocols = prot; |
144 | u8 protocols = prot; |
131 | |
145 | |
132 | if (prot & (PROT_IPv4 | PROT_ICMPv4 | PROT_UDPv4 | PROT_TCPv4 | PROT_IPv42)) |
146 | if (prot & (PROT_IPv4 | PROT_ICMPv4 | PROT_UDPv4 | PROT_TCPv4)) |
133 | protocols |= PROT_IPv4 | PROT_ICMPv4 | PROT_IPv42; |
147 | protocols |= PROT_IPv4 | PROT_ICMPv4; |
134 | |
148 | |
135 | if (conf && prot & PROTv4) |
149 | if (conf && prot & PROTv4) |
136 | { |
150 | { |
137 | if (conf->protocols & PROT_UDPv4 && conf->udp_port) |
151 | if (conf->protocols & PROT_UDPv4 && conf->udp_port) |
138 | protocols |= PROT_UDPv4; |
152 | protocols |= PROT_UDPv4; |
… | |
… | |
154 | return true; |
168 | return true; |
155 | |
169 | |
156 | if (prot & PROTv4 |
170 | if (prot & PROTv4 |
157 | && prot_ & PROTv4) |
171 | && prot_ & PROTv4) |
158 | { |
172 | { |
159 | if (prot_ & (PROT_IPv4 | PROT_ICMPv4 | PROT_IPv42)) |
173 | if (prot_ & (PROT_IPv4 | PROT_ICMPv4)) |
160 | { |
174 | { |
161 | prot = prot_; |
175 | prot = prot_; |
162 | port = 0; |
176 | port = 0; |
163 | return true; |
177 | return true; |
164 | } |
178 | } |