… | |
… | |
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 | { |
|
|
76 | host = in.s_addr; |
|
|
77 | } |
63 | struct hostent *he = gethostbyname (hostname); |
78 | else if ((he = gethostbyname (hostname)) |
64 | |
79 | && he->h_addrtype == AF_INET |
65 | if (he |
80 | && he->h_length == 4 |
66 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
81 | && he->h_addr_list[0]) |
67 | { |
82 | { |
68 | //sa->sin_family = he->h_addrtype; |
83 | //sa->sin_family = he->h_addrtype; |
69 | memcpy (&host, he->h_addr_list[0], 4); |
84 | memcpy (&host, he->h_addr_list[0], 4); |
70 | } |
85 | } |
71 | else |
86 | else |
… | |
… | |
111 | sprintf (hostport, "%.15s", inet_ntoa (ia)); |
126 | sprintf (hostport, "%.15s", inet_ntoa (ia)); |
112 | |
127 | |
113 | return hostport; |
128 | return hostport; |
114 | } |
129 | } |
115 | |
130 | |
|
|
131 | bool |
|
|
132 | sockinfo::valid () const |
|
|
133 | { |
|
|
134 | return (prot & THISNODE->protocols) && host; |
|
|
135 | } |
|
|
136 | |
116 | sockinfo::operator const char *() const |
137 | sockinfo::operator const char *() const |
117 | { |
138 | { |
118 | in_addr ia = { host }; |
139 | in_addr ia = { host }; |
119 | |
140 | |
120 | sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff); |
141 | sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff); |
… | |
… | |
125 | u8 |
146 | u8 |
126 | sockinfo::supported_protocols (conf_node *conf) |
147 | sockinfo::supported_protocols (conf_node *conf) |
127 | { |
148 | { |
128 | u8 protocols = prot; |
149 | u8 protocols = prot; |
129 | |
150 | |
130 | if (prot & (PROT_UDPv4 | PROT_TCPv4)) |
151 | if (prot & (PROT_IPv4 | PROT_ICMPv4 | PROT_UDPv4 | PROT_TCPv4)) |
131 | protocols |= PROT_IPv4 | PROT_ICMPv4; |
152 | protocols |= PROT_IPv4 | PROT_ICMPv4; |
132 | |
153 | |
133 | if (conf |
|
|
134 | && prot & PROTv4 |
154 | if (conf && prot & PROTv4) |
135 | && conf->protocols & PROT_UDPv4 |
155 | { |
136 | && conf->udp_port) |
156 | if (conf->protocols & PROT_UDPv4 && conf->udp_port) |
137 | protocols |= PROT_UDPv4; |
157 | protocols |= PROT_UDPv4; |
138 | |
158 | |
139 | if (conf |
159 | if (conf->protocols & PROT_TCPv4 && conf->tcp_port) |
140 | && prot & PROTv4 |
|
|
141 | && conf->protocols & PROT_TCPv4 |
|
|
142 | && conf->tcp_port) |
|
|
143 | protocols |= PROT_TCPv4; |
160 | protocols |= PROT_TCPv4; |
144 | |
161 | |
145 | if (conf |
162 | if (conf->protocols & PROT_DNSv4 && conf->dns_port) |
146 | && prot & PROTv4 |
|
|
147 | && conf->protocols & PROT_DNSv4 |
|
|
148 | && conf->dns_port) |
|
|
149 | protocols |= PROT_DNSv4; |
163 | protocols |= PROT_DNSv4; |
|
|
164 | } |
150 | |
165 | |
151 | return protocols; |
166 | return protocols; |
152 | } |
167 | } |
153 | |
168 | |
154 | bool |
169 | bool |