1 | /* |
1 | /* |
2 | sockinfo.c -- socket address management |
2 | sockinfo.C -- socket address management |
3 | |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
7 | (at your option) any later version. |
… | |
… | |
36 | void |
36 | void |
37 | sockinfo::set (const conf_node *conf, u8 prot_) |
37 | sockinfo::set (const conf_node *conf, u8 prot_) |
38 | { |
38 | { |
39 | prot = prot_; |
39 | prot = prot_; |
40 | host = 0; |
40 | host = 0; |
41 | port = prot_ == PROT_UDPv4 ? htons (conf->udp_port) |
41 | port = prot == PROT_UDPv4 ? htons (conf->udp_port) |
42 | : prot_ == PROT_TCPv4 ? htons (conf->tcp_port) |
42 | : prot == PROT_TCPv4 ? htons (conf->tcp_port) |
43 | : 0; |
43 | : 0; |
44 | |
44 | |
45 | if (conf->hostname) |
45 | if (prot && conf->hostname) |
46 | { |
46 | { |
47 | struct hostent *he = gethostbyname (conf->hostname); |
47 | struct hostent *he = gethostbyname (conf->hostname); |
48 | |
48 | |
49 | if (he |
49 | if (he |
50 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
50 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
… | |
… | |
69 | sa.sin_addr.s_addr = host; |
69 | sa.sin_addr.s_addr = host; |
70 | |
70 | |
71 | return (const sockaddr *)&sa; |
71 | return (const sockaddr *)&sa; |
72 | } |
72 | } |
73 | |
73 | |
74 | static char hostport[15 + 1 + 5 + 1]; // IPv4 : port |
74 | static char hostport[10 + 15 + 1 + 5 + 1]; // protype / IPv4 : port |
75 | |
75 | |
76 | const char * |
76 | const char * |
77 | sockinfo::ntoa () const |
77 | sockinfo::ntoa () const |
78 | { |
78 | { |
79 | in_addr ia = { host }; |
79 | in_addr ia = { host }; |
… | |
… | |
85 | |
85 | |
86 | sockinfo::operator const char *() const |
86 | sockinfo::operator const char *() const |
87 | { |
87 | { |
88 | in_addr ia = { host }; |
88 | in_addr ia = { host }; |
89 | |
89 | |
90 | sprintf (hostport, "%.15s:%d", inet_ntoa (ia), ntohs (port) & 0xffff); |
90 | sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff); |
91 | |
91 | |
92 | return hostport; |
92 | return hostport; |
93 | } |
93 | } |
94 | |
94 | |
|
|
95 | bool |
|
|
96 | operator == (const sockinfo &a, const sockinfo &b) |
|
|
97 | { |
|
|
98 | return a.host == b.host && a.port == b.port && a.prot == b.prot; |
|
|
99 | } |
|
|
100 | |
|
|
101 | bool |
|
|
102 | operator < (const sockinfo &a, const sockinfo &b) |
|
|
103 | { |
|
|
104 | return a.host < b.host |
|
|
105 | || (a.host == b.host && (a.port < b.port |
|
|
106 | || (a.port == b.port && a.prot < b.prot))); |
|
|
107 | } |
|
|
108 | |