… | |
… | |
24 | #include "gettext.h" |
24 | #include "gettext.h" |
25 | |
25 | |
26 | #include "sockinfo.h" |
26 | #include "sockinfo.h" |
27 | #include "slog.h" |
27 | #include "slog.h" |
28 | |
28 | |
|
|
29 | // all ipv4-based protocols |
|
|
30 | #define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4) |
|
|
31 | |
29 | void sockinfo::set (const sockaddr_in *sa, u8 prot_) |
32 | void sockinfo::set (const sockaddr_in *sa, u8 prot_) |
30 | { |
33 | { |
31 | host = sa->sin_addr.s_addr; |
34 | host = sa->sin_addr.s_addr; |
32 | port = prot_ == PROT_IPv4 ? 0 : sa->sin_port; |
35 | port = prot_ & (PROT_IPv4 | PROT_ICMPv4) ? 0 : sa->sin_port; |
33 | prot = prot_; |
36 | prot = prot_; |
34 | } |
37 | } |
35 | |
38 | |
36 | void |
39 | void sockinfo::set (const char *hostname, u16 port_, u8 prot_) |
37 | sockinfo::set (const conf_node *conf, u8 prot_) |
|
|
38 | { |
40 | { |
39 | prot = prot_; |
41 | prot = prot_; |
40 | host = 0; |
42 | host = 0; |
41 | port = prot == PROT_UDPv4 ? htons (conf->udp_port) |
43 | port = htons (port_); |
42 | : prot == PROT_TCPv4 ? htons (conf->tcp_port) |
|
|
43 | : 0; |
|
|
44 | |
44 | |
45 | if (prot && conf->hostname) |
45 | if (prot & PROTv4 |
|
|
46 | && hostname) |
46 | { |
47 | { |
47 | struct hostent *he = gethostbyname (conf->hostname); |
48 | struct hostent *he = gethostbyname (hostname); |
48 | |
49 | |
49 | if (he |
50 | if (he |
50 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
51 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
51 | { |
52 | { |
52 | //sa->sin_family = he->h_addrtype; |
53 | //sa->sin_family = he->h_addrtype; |
53 | memcpy (&host, he->h_addr_list[0], 4); |
54 | memcpy (&host, he->h_addr_list[0], 4); |
54 | |
|
|
55 | } |
55 | } |
56 | else |
56 | else |
57 | slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname); |
57 | slog (L_NOTICE, _("unable to resolve host '%s'"), hostname); |
58 | } |
58 | } |
59 | } |
59 | } |
60 | |
60 | |
|
|
61 | void |
|
|
62 | sockinfo::set (const conf_node *conf, u8 prot_) |
|
|
63 | { |
|
|
64 | set (conf->hostname, |
|
|
65 | prot_ == PROT_UDPv4 ? conf->udp_port |
|
|
66 | : prot_ == PROT_TCPv4 ? conf->tcp_port |
|
|
67 | : 0, |
|
|
68 | prot_); |
|
|
69 | } |
61 | |
70 | |
62 | const sockaddr * |
71 | const sockaddr * |
63 | sockinfo::sav4() const |
72 | sockinfo::sav4() const |
64 | { |
73 | { |
65 | static sockaddr_in sa; |
74 | static sockaddr_in sa; |
… | |
… | |
69 | sa.sin_addr.s_addr = host; |
78 | sa.sin_addr.s_addr = host; |
70 | |
79 | |
71 | return (const sockaddr *)&sa; |
80 | return (const sockaddr *)&sa; |
72 | } |
81 | } |
73 | |
82 | |
74 | static char hostport[10 + 15 + 1 + 5 + 1]; // protype / IPv4 : port |
83 | static char hostport[10 + 15 + 1 + 5 + 1]; // proto / IPv4 : port |
75 | |
84 | |
76 | const char * |
85 | const char * |
77 | sockinfo::ntoa () const |
86 | sockinfo::ntoa () const |
78 | { |
87 | { |
79 | in_addr ia = { host }; |
88 | in_addr ia = { host }; |
… | |
… | |
96 | sockinfo::supported_protocols (conf_node *conf) |
105 | sockinfo::supported_protocols (conf_node *conf) |
97 | { |
106 | { |
98 | u8 protocols = prot; |
107 | u8 protocols = prot; |
99 | |
108 | |
100 | if (prot & (PROT_UDPv4 | PROT_TCPv4)) |
109 | if (prot & (PROT_UDPv4 | PROT_TCPv4)) |
101 | protocols |= PROT_IPv4; |
110 | protocols |= PROT_IPv4 | PROT_ICMPv4; |
102 | |
111 | |
103 | if (conf |
112 | if (conf |
104 | && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) |
113 | && prot & PROTv4 |
105 | && conf->protocols & PROT_UDPv4 |
114 | && conf->protocols & PROT_UDPv4 |
106 | && conf->udp_port) |
115 | && conf->udp_port) |
107 | protocols |= PROT_UDPv4; |
116 | protocols |= PROT_UDPv4; |
108 | |
117 | |
109 | if (conf |
118 | if (conf |
110 | && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) |
119 | && prot & PROTv4 |
111 | && conf->protocols & PROT_TCPv4 |
120 | && conf->protocols & PROT_TCPv4 |
112 | && conf->tcp_port) |
121 | && conf->tcp_port) |
113 | protocols |= PROT_TCPv4; |
122 | protocols |= PROT_TCPv4; |
114 | |
123 | |
115 | return protocols; |
124 | return protocols; |
… | |
… | |
119 | sockinfo::upgrade_protocol (u8 prot_, conf_node *conf) |
128 | sockinfo::upgrade_protocol (u8 prot_, conf_node *conf) |
120 | { |
129 | { |
121 | if (prot_ == prot) |
130 | if (prot_ == prot) |
122 | return true; |
131 | return true; |
123 | |
132 | |
124 | if (prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) |
133 | if (prot & PROTv4 |
125 | && prot_ & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4)) |
134 | && prot_ & PROTv4) |
126 | { |
135 | { |
127 | if (prot_ & PROT_IPv4) |
136 | if (prot_ & (PROT_IPv4 | PROT_ICMPv4)) |
128 | { |
137 | { |
129 | prot = prot_; |
138 | prot = prot_; |
130 | port = 0; |
139 | port = 0; |
131 | return true; |
140 | return true; |
132 | } |
141 | } |