ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/sockinfo.C
Revision: 1.7
Committed: Sun Apr 6 04:31:51 2003 UTC (21 years, 1 month ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.6: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2 pcg 1.4 sockinfo.C -- socket address management
3 pcg 1.1
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 pcg 1.2 #include <arpa/inet.h>
22     #include <netdb.h>
23 pcg 1.1
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
37 pcg 1.3 sockinfo::set (const conf_node *conf, u8 prot_)
38 pcg 1.1 {
39 pcg 1.3 prot = prot_;
40 pcg 1.1 host = 0;
41 pcg 1.4 port = prot == PROT_UDPv4 ? htons (conf->udp_port)
42     : prot == PROT_TCPv4 ? htons (conf->tcp_port)
43 pcg 1.3 : 0;
44 pcg 1.1
45 pcg 1.7 if (prot & (PROT_UDPv4 | PROT_TCPv4 | PROT_IPv4)
46     && conf->hostname)
47 pcg 1.1 {
48     struct hostent *he = gethostbyname (conf->hostname);
49    
50     if (he
51     && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0])
52     {
53     //sa->sin_family = he->h_addrtype;
54     memcpy (&host, he->h_addr_list[0], 4);
55     }
56     else
57     slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname);
58     }
59     }
60    
61    
62     const sockaddr *
63     sockinfo::sav4() const
64     {
65     static sockaddr_in sa;
66    
67     sa.sin_family = AF_INET;
68     sa.sin_port = port;
69     sa.sin_addr.s_addr = host;
70    
71     return (const sockaddr *)&sa;
72     }
73    
74 pcg 1.4 static char hostport[10 + 15 + 1 + 5 + 1]; // protype / IPv4 : port
75 pcg 1.1
76     const char *
77     sockinfo::ntoa () const
78     {
79     in_addr ia = { host };
80    
81     sprintf (hostport, "%.15s", inet_ntoa (ia));
82    
83     return hostport;
84     }
85    
86     sockinfo::operator const char *() const
87     {
88     in_addr ia = { host };
89    
90 pcg 1.4 sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff);
91 pcg 1.1
92     return hostport;
93 pcg 1.4 }
94    
95 pcg 1.5 u8
96     sockinfo::supported_protocols (conf_node *conf)
97     {
98     u8 protocols = prot;
99    
100     if (prot & (PROT_UDPv4 | PROT_TCPv4))
101     protocols |= PROT_IPv4;
102    
103     if (conf
104     && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4)
105     && conf->protocols & PROT_UDPv4
106     && conf->udp_port)
107     protocols |= PROT_UDPv4;
108    
109     if (conf
110     && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4)
111     && conf->protocols & PROT_TCPv4
112     && conf->tcp_port)
113     protocols |= PROT_TCPv4;
114    
115     return protocols;
116     }
117    
118     bool
119     sockinfo::upgrade_protocol (u8 prot_, conf_node *conf)
120     {
121     if (prot_ == prot)
122     return true;
123    
124     if (prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4)
125     && prot_ & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4))
126     {
127     if (prot_ & PROT_IPv4)
128     {
129     prot = prot_;
130     port = 0;
131     return true;
132     }
133    
134     if (conf
135     && prot_ & PROT_UDPv4
136     && conf->protocols & PROT_UDPv4
137     && conf->udp_port)
138     {
139     prot = prot_;
140 pcg 1.6 port = htons (conf->udp_port);
141 pcg 1.5 return true;
142     }
143    
144     if (conf
145     && prot_ & PROT_TCPv4
146     && conf->protocols & PROT_TCPv4
147     && conf->tcp_port)
148     {
149     prot = prot_;
150 pcg 1.6 port = htons (conf->tcp_port);
151 pcg 1.5 return true;
152     }
153     }
154    
155     return false;
156     }
157    
158 pcg 1.4 bool
159     operator == (const sockinfo &a, const sockinfo &b)
160     {
161     return a.host == b.host && a.port == b.port && a.prot == b.prot;
162     }
163    
164     bool
165     operator < (const sockinfo &a, const sockinfo &b)
166     {
167     return a.host < b.host
168     || (a.host == b.host && (a.port < b.port
169     || (a.port == b.port && a.prot < b.prot)));
170 pcg 1.1 }
171