ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/sockinfo.C
Revision: 1.8
Committed: Mon Apr 7 01:12:56 2003 UTC (21 years, 1 month ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.7: +15 -9 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 sockinfo.C -- socket address management
3
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
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include "config.h"
20
21 #include <arpa/inet.h>
22 #include <netdb.h>
23
24 #include "gettext.h"
25
26 #include "sockinfo.h"
27 #include "slog.h"
28
29 void sockinfo::set (const sockaddr_in *sa, u8 prot_)
30 {
31 host = sa->sin_addr.s_addr;
32 port = prot_ == PROT_IPv4 ? 0 : sa->sin_port;
33 prot = prot_;
34 }
35
36 void sockinfo::set (const char *hostname, u16 port_, u8 prot_)
37 {
38 prot = prot_;
39 host = 0;
40 port = htons (port_);
41
42 if (prot & (PROT_UDPv4 | PROT_TCPv4 | PROT_IPv4)
43 && hostname)
44 {
45 struct hostent *he = gethostbyname (hostname);
46
47 if (he
48 && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0])
49 {
50 //sa->sin_family = he->h_addrtype;
51 memcpy (&host, he->h_addr_list[0], 4);
52 }
53 else
54 slog (L_NOTICE, _("unable to resolve host '%s'"), hostname);
55 }
56 }
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 }
67
68 const sockaddr *
69 sockinfo::sav4() const
70 {
71 static sockaddr_in sa;
72
73 sa.sin_family = AF_INET;
74 sa.sin_port = port;
75 sa.sin_addr.s_addr = host;
76
77 return (const sockaddr *)&sa;
78 }
79
80 static char hostport[10 + 15 + 1 + 5 + 1]; // proto / IPv4 : port
81
82 const char *
83 sockinfo::ntoa () const
84 {
85 in_addr ia = { host };
86
87 sprintf (hostport, "%.15s", inet_ntoa (ia));
88
89 return hostport;
90 }
91
92 sockinfo::operator const char *() const
93 {
94 in_addr ia = { host };
95
96 sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff);
97
98 return hostport;
99 }
100
101 u8
102 sockinfo::supported_protocols (conf_node *conf)
103 {
104 u8 protocols = prot;
105
106 if (prot & (PROT_UDPv4 | PROT_TCPv4))
107 protocols |= PROT_IPv4;
108
109 if (conf
110 && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4)
111 && conf->protocols & PROT_UDPv4
112 && conf->udp_port)
113 protocols |= PROT_UDPv4;
114
115 if (conf
116 && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4)
117 && conf->protocols & PROT_TCPv4
118 && conf->tcp_port)
119 protocols |= PROT_TCPv4;
120
121 return protocols;
122 }
123
124 bool
125 sockinfo::upgrade_protocol (u8 prot_, conf_node *conf)
126 {
127 if (prot_ == prot)
128 return true;
129
130 if (prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4)
131 && prot_ & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4))
132 {
133 if (prot_ & PROT_IPv4)
134 {
135 prot = prot_;
136 port = 0;
137 return true;
138 }
139
140 if (conf
141 && prot_ & PROT_UDPv4
142 && conf->protocols & PROT_UDPv4
143 && conf->udp_port)
144 {
145 prot = prot_;
146 port = htons (conf->udp_port);
147 return true;
148 }
149
150 if (conf
151 && prot_ & PROT_TCPv4
152 && conf->protocols & PROT_TCPv4
153 && conf->tcp_port)
154 {
155 prot = prot_;
156 port = htons (conf->tcp_port);
157 return true;
158 }
159 }
160
161 return false;
162 }
163
164 bool
165 operator == (const sockinfo &a, const sockinfo &b)
166 {
167 return a.host == b.host && a.port == b.port && a.prot == b.prot;
168 }
169
170 bool
171 operator < (const sockinfo &a, const sockinfo &b)
172 {
173 return a.host < b.host
174 || (a.host == b.host && (a.port < b.port
175 || (a.port == b.port && a.prot < b.prot)));
176 }
177