… | |
… | |
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" |
… | |
… | |
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) |
51 | #define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4 | PROT_DNSv4) |
46 | |
52 | |
|
|
53 | void |
47 | void sockinfo::set (const sockaddr_in *sa, u8 prot_) |
54 | sockinfo::set (const sockaddr_in *sa, u8 prot_) |
48 | { |
55 | { |
49 | host = sa->sin_addr.s_addr; |
56 | host = sa->sin_addr.s_addr; |
50 | port = prot_ & (PROT_IPv4 | PROT_ICMPv4) ? 0 : sa->sin_port; |
57 | port = prot_ & (PROT_IPv4 | PROT_ICMPv4) ? 0 : sa->sin_port; |
51 | prot = prot_; |
58 | prot = prot_; |
52 | } |
59 | } |
53 | |
60 | |
|
|
61 | void |
54 | void sockinfo::set (const char *hostname, u16 port_, u8 prot_) |
62 | sockinfo::set (const char *hostname, u16 port_, u8 prot_) |
55 | { |
63 | { |
56 | prot = prot_; |
64 | prot = prot_; |
57 | host = 0; |
65 | host = 0; |
58 | port = htons (port_); |
66 | port = htons (port_); |
59 | |
67 | |
60 | if (prot & PROTv4 |
68 | if (prot & PROTv4 |
61 | && hostname) |
69 | && hostname) |
62 | { |
70 | { |
|
|
71 | struct in_addr in; |
|
|
72 | struct hostent *he; |
|
|
73 | |
|
|
74 | if (inet_aton (hostname, &in)) |
|
|
75 | host = in.s_addr; |
63 | struct hostent *he = gethostbyname (hostname); |
76 | else if ((he = gethostbyname (hostname)) |
64 | |
77 | && he->h_addrtype == AF_INET |
65 | if (he |
78 | && he->h_length == 4 |
66 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
79 | && he->h_addr_list[0]) |
67 | { |
80 | memcpy (&host, he->h_addr_list[0], 4); //sa->sin_family = he->h_addrtype; |
68 | //sa->sin_family = he->h_addrtype; |
|
|
69 | memcpy (&host, he->h_addr_list[0], 4); |
|
|
70 | } |
|
|
71 | else |
81 | else |
72 | slog (L_NOTICE, _("unable to resolve host '%s'"), hostname); |
82 | slog (L_NOTICE, _("unable to resolve host '%s'"), hostname); |
73 | } |
83 | } |
74 | } |
84 | } |
75 | |
85 | |
… | |
… | |
111 | sprintf (hostport, "%.15s", inet_ntoa (ia)); |
121 | sprintf (hostport, "%.15s", inet_ntoa (ia)); |
112 | |
122 | |
113 | return hostport; |
123 | return hostport; |
114 | } |
124 | } |
115 | |
125 | |
|
|
126 | bool |
|
|
127 | sockinfo::valid () const |
|
|
128 | { |
|
|
129 | return (prot & THISNODE->protocols) && host; |
|
|
130 | } |
|
|
131 | |
116 | sockinfo::operator const char *() const |
132 | sockinfo::operator const char *() const |
117 | { |
133 | { |
118 | in_addr ia = { host }; |
134 | in_addr ia = { host }; |
119 | |
135 | |
120 | 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); |
… | |
… | |
125 | u8 |
141 | u8 |
126 | sockinfo::supported_protocols (conf_node *conf) |
142 | sockinfo::supported_protocols (conf_node *conf) |
127 | { |
143 | { |
128 | u8 protocols = prot; |
144 | u8 protocols = prot; |
129 | |
145 | |
130 | if (prot & (PROT_UDPv4 | PROT_TCPv4)) |
146 | if (prot & (PROT_IPv4 | PROT_ICMPv4 | PROT_UDPv4 | PROT_TCPv4)) |
131 | protocols |= PROT_IPv4 | PROT_ICMPv4; |
147 | protocols |= PROT_IPv4 | PROT_ICMPv4; |
132 | |
148 | |
133 | if (conf |
|
|
134 | && prot & PROTv4 |
149 | if (conf && prot & PROTv4) |
135 | && conf->protocols & PROT_UDPv4 |
150 | { |
136 | && conf->udp_port) |
151 | if (conf->protocols & PROT_UDPv4 && conf->udp_port) |
137 | protocols |= PROT_UDPv4; |
152 | protocols |= PROT_UDPv4; |
138 | |
153 | |
139 | if (conf |
154 | if (conf->protocols & PROT_TCPv4 && conf->tcp_port) |
140 | && prot & PROTv4 |
|
|
141 | && conf->protocols & PROT_TCPv4 |
|
|
142 | && conf->tcp_port) |
|
|
143 | protocols |= PROT_TCPv4; |
155 | protocols |= PROT_TCPv4; |
144 | |
156 | |
145 | if (conf |
157 | if (conf->protocols & PROT_DNSv4 && conf->dns_port) |
146 | && prot & PROTv4 |
|
|
147 | && conf->protocols & PROT_DNSv4 |
|
|
148 | && conf->dns_port) |
|
|
149 | protocols |= PROT_DNSv4; |
158 | protocols |= PROT_DNSv4; |
|
|
159 | } |
150 | |
160 | |
151 | return protocols; |
161 | return protocols; |
152 | } |
162 | } |
153 | |
163 | |
154 | bool |
164 | bool |