… | |
… | |
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 | { |
|
|
76 | host = in.s_addr; |
|
|
77 | } |
65 | struct hostent *he = gethostbyname (hostname); |
78 | else if ((he = gethostbyname (hostname)) |
66 | |
79 | && he->h_addrtype == AF_INET |
67 | if (he |
80 | && he->h_length == 4 |
68 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
81 | && he->h_addr_list[0]) |
69 | { |
82 | { |
70 | //sa->sin_family = he->h_addrtype; |
83 | //sa->sin_family = he->h_addrtype; |
71 | memcpy (&host, he->h_addr_list[0], 4); |
84 | memcpy (&host, he->h_addr_list[0], 4); |
72 | } |
85 | } |
73 | else |
86 | else |
… | |
… | |
133 | u8 |
146 | u8 |
134 | sockinfo::supported_protocols (conf_node *conf) |
147 | sockinfo::supported_protocols (conf_node *conf) |
135 | { |
148 | { |
136 | u8 protocols = prot; |
149 | u8 protocols = prot; |
137 | |
150 | |
138 | if (prot & (PROT_IPv4 | PROT_ICMPv4 | PROT_UDPv4 | PROT_TCPv4 | PROT_IPv42)) |
151 | if (prot & (PROT_IPv4 | PROT_ICMPv4 | PROT_UDPv4 | PROT_TCPv4)) |
139 | protocols |= PROT_IPv4 | PROT_ICMPv4 | PROT_IPv42; |
152 | protocols |= PROT_IPv4 | PROT_ICMPv4; |
140 | |
153 | |
141 | if (conf && prot & PROTv4) |
154 | if (conf && prot & PROTv4) |
142 | { |
155 | { |
143 | if (conf->protocols & PROT_UDPv4 && conf->udp_port) |
156 | if (conf->protocols & PROT_UDPv4 && conf->udp_port) |
144 | protocols |= PROT_UDPv4; |
157 | protocols |= PROT_UDPv4; |
… | |
… | |
160 | return true; |
173 | return true; |
161 | |
174 | |
162 | if (prot & PROTv4 |
175 | if (prot & PROTv4 |
163 | && prot_ & PROTv4) |
176 | && prot_ & PROTv4) |
164 | { |
177 | { |
165 | if (prot_ & (PROT_IPv4 | PROT_ICMPv4 | PROT_IPv42)) |
178 | if (prot_ & (PROT_IPv4 | PROT_ICMPv4)) |
166 | { |
179 | { |
167 | prot = prot_; |
180 | prot = prot_; |
168 | port = 0; |
181 | port = 0; |
169 | return true; |
182 | return true; |
170 | } |
183 | } |