… | |
… | |
31 | host = sa->sin_addr.s_addr; |
31 | host = sa->sin_addr.s_addr; |
32 | port = prot_ == PROT_IPv4 ? 0 : sa->sin_port; |
32 | port = prot_ == PROT_IPv4 ? 0 : sa->sin_port; |
33 | prot = prot_; |
33 | prot = prot_; |
34 | } |
34 | } |
35 | |
35 | |
36 | void |
36 | void sockinfo::set (const char *hostname, u16 port_, u8 prot_) |
37 | sockinfo::set (const conf_node *conf, u8 prot_) |
|
|
38 | { |
37 | { |
39 | prot = prot_; |
38 | prot = prot_; |
40 | host = 0; |
39 | host = 0; |
41 | port = prot == PROT_UDPv4 ? htons (conf->udp_port) |
40 | port = htons (port_); |
42 | : prot == PROT_TCPv4 ? htons (conf->tcp_port) |
|
|
43 | : 0; |
|
|
44 | |
41 | |
45 | if (prot && conf->hostname) |
42 | if (prot & (PROT_UDPv4 | PROT_TCPv4 | PROT_IPv4) |
|
|
43 | && hostname) |
46 | { |
44 | { |
47 | struct hostent *he = gethostbyname (conf->hostname); |
45 | struct hostent *he = gethostbyname (hostname); |
48 | |
46 | |
49 | if (he |
47 | if (he |
50 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
48 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
51 | { |
49 | { |
52 | //sa->sin_family = he->h_addrtype; |
50 | //sa->sin_family = he->h_addrtype; |
53 | memcpy (&host, he->h_addr_list[0], 4); |
51 | memcpy (&host, he->h_addr_list[0], 4); |
54 | |
|
|
55 | } |
52 | } |
56 | else |
53 | else |
57 | slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname); |
54 | slog (L_NOTICE, _("unable to resolve host '%s'"), hostname); |
58 | } |
55 | } |
59 | } |
56 | } |
60 | |
57 | |
|
|
58 | void |
|
|
59 | sockinfo::set (const conf_node *conf, u8 prot_) |
|
|
60 | { |
|
|
61 | set (conf->hostname, |
|
|
62 | prot_ == PROT_UDPv4 ? conf->udp_port |
|
|
63 | : prot_ == PROT_TCPv4 ? conf->tcp_port |
|
|
64 | : 0, |
|
|
65 | prot_); |
|
|
66 | } |
61 | |
67 | |
62 | const sockaddr * |
68 | const sockaddr * |
63 | sockinfo::sav4() const |
69 | sockinfo::sav4() const |
64 | { |
70 | { |
65 | static sockaddr_in sa; |
71 | static sockaddr_in sa; |
… | |
… | |
69 | sa.sin_addr.s_addr = host; |
75 | sa.sin_addr.s_addr = host; |
70 | |
76 | |
71 | return (const sockaddr *)&sa; |
77 | return (const sockaddr *)&sa; |
72 | } |
78 | } |
73 | |
79 | |
74 | static char hostport[10 + 15 + 1 + 5 + 1]; // protype / IPv4 : port |
80 | static char hostport[10 + 15 + 1 + 5 + 1]; // proto / IPv4 : port |
75 | |
81 | |
76 | const char * |
82 | const char * |
77 | sockinfo::ntoa () const |
83 | sockinfo::ntoa () const |
78 | { |
84 | { |
79 | in_addr ia = { host }; |
85 | in_addr ia = { host }; |
… | |
… | |
135 | && prot_ & PROT_UDPv4 |
141 | && prot_ & PROT_UDPv4 |
136 | && conf->protocols & PROT_UDPv4 |
142 | && conf->protocols & PROT_UDPv4 |
137 | && conf->udp_port) |
143 | && conf->udp_port) |
138 | { |
144 | { |
139 | prot = prot_; |
145 | prot = prot_; |
140 | port = conf->udp_port; |
146 | port = htons (conf->udp_port); |
141 | return true; |
147 | return true; |
142 | } |
148 | } |
143 | |
149 | |
144 | if (conf |
150 | if (conf |
145 | && prot_ & PROT_TCPv4 |
151 | && prot_ & PROT_TCPv4 |
146 | && conf->protocols & PROT_TCPv4 |
152 | && conf->protocols & PROT_TCPv4 |
147 | && conf->tcp_port) |
153 | && conf->tcp_port) |
148 | { |
154 | { |
149 | prot = prot_; |
155 | prot = prot_; |
150 | port = conf->tcp_port; |
156 | port = htons (conf->tcp_port); |
151 | return true; |
157 | return true; |
152 | } |
158 | } |
153 | } |
159 | } |
154 | |
160 | |
155 | return false; |
161 | return false; |