ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/sockinfo.C
Revision: 1.22
Committed: Tue Dec 4 15:01:12 2007 UTC (16 years, 5 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.21: +3 -0 lines
Log Message:
switch to new cb system

File Contents

# User Rev Content
1 pcg 1.1 /*
2 pcg 1.4 sockinfo.C -- socket address management
3 pcg 1.15 Copyright (C) 2003 Marc Lehmann <gvpe@schmorp.de>
4 pcg 1.1
5 pcg 1.15 This file is part of GVPE.
6    
7     GVPE is free software; you can redistribute it and/or modify
8 pcg 1.1 it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11    
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16    
17     You should have received a copy of the GNU General Public License
18 pcg 1.15 along with gvpe; if not, write to the Free Software
19 pcg 1.21 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 pcg 1.1 */
21    
22     #include "config.h"
23    
24 pcg 1.2 #include <netdb.h>
25 pcg 1.1
26 pcg 1.20 #include "gettext.h"
27    
28 pcg 1.1 #include "sockinfo.h"
29     #include "slog.h"
30    
31 pcg 1.22 #include <cstring>
32     #include <cstdio>
33    
34 pcg 1.9 // all ipv4-based protocols
35 pcg 1.13 #define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4 | PROT_DNSv4)
36 pcg 1.9
37 pcg 1.1 void sockinfo::set (const sockaddr_in *sa, u8 prot_)
38     {
39     host = sa->sin_addr.s_addr;
40 pcg 1.9 port = prot_ & (PROT_IPv4 | PROT_ICMPv4) ? 0 : sa->sin_port;
41 pcg 1.1 prot = prot_;
42     }
43    
44 pcg 1.8 void sockinfo::set (const char *hostname, u16 port_, u8 prot_)
45 pcg 1.1 {
46 pcg 1.3 prot = prot_;
47 pcg 1.1 host = 0;
48 pcg 1.8 port = htons (port_);
49 pcg 1.1
50 pcg 1.9 if (prot & PROTv4
51 pcg 1.8 && hostname)
52 pcg 1.1 {
53 pcg 1.8 struct hostent *he = gethostbyname (hostname);
54 pcg 1.1
55     if (he
56     && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0])
57     {
58     //sa->sin_family = he->h_addrtype;
59     memcpy (&host, he->h_addr_list[0], 4);
60     }
61     else
62 pcg 1.8 slog (L_NOTICE, _("unable to resolve host '%s'"), hostname);
63 pcg 1.1 }
64     }
65    
66 pcg 1.8 void
67     sockinfo::set (const conf_node *conf, u8 prot_)
68     {
69 pcg 1.14 if (prot_ == PROT_DNSv4)
70 pcg 1.16 {
71 pcg 1.18 host = htonl (conf->id); port = 0; prot = prot_;
72 pcg 1.16 }
73 pcg 1.14 else
74     set (conf->hostname,
75     prot_ == PROT_UDPv4 ? conf->udp_port
76     : prot_ == PROT_TCPv4 ? conf->tcp_port
77     : prot_ == PROT_DNSv4 ? conf->dns_port
78     : 0,
79     prot_);
80 pcg 1.8 }
81 pcg 1.1
82     const sockaddr *
83     sockinfo::sav4() const
84     {
85     static sockaddr_in sa;
86    
87     sa.sin_family = AF_INET;
88     sa.sin_port = port;
89     sa.sin_addr.s_addr = host;
90    
91     return (const sockaddr *)&sa;
92     }
93    
94 pcg 1.8 static char hostport[10 + 15 + 1 + 5 + 1]; // proto / IPv4 : port
95 pcg 1.1
96     const char *
97     sockinfo::ntoa () const
98     {
99     in_addr ia = { host };
100    
101     sprintf (hostport, "%.15s", inet_ntoa (ia));
102    
103     return hostport;
104     }
105    
106     sockinfo::operator const char *() const
107     {
108     in_addr ia = { host };
109    
110 pcg 1.4 sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff);
111 pcg 1.1
112     return hostport;
113 pcg 1.4 }
114    
115 pcg 1.5 u8
116     sockinfo::supported_protocols (conf_node *conf)
117     {
118     u8 protocols = prot;
119    
120     if (prot & (PROT_UDPv4 | PROT_TCPv4))
121 pcg 1.9 protocols |= PROT_IPv4 | PROT_ICMPv4;
122 pcg 1.5
123     if (conf
124 pcg 1.9 && prot & PROTv4
125 pcg 1.5 && conf->protocols & PROT_UDPv4
126     && conf->udp_port)
127     protocols |= PROT_UDPv4;
128    
129     if (conf
130 pcg 1.9 && prot & PROTv4
131 pcg 1.5 && conf->protocols & PROT_TCPv4
132     && conf->tcp_port)
133     protocols |= PROT_TCPv4;
134 pcg 1.12
135     if (conf
136     && prot & PROTv4
137 pcg 1.13 && conf->protocols & PROT_DNSv4
138     && conf->dns_port)
139 pcg 1.12 protocols |= PROT_DNSv4;
140 pcg 1.5
141     return protocols;
142     }
143    
144     bool
145     sockinfo::upgrade_protocol (u8 prot_, conf_node *conf)
146     {
147     if (prot_ == prot)
148     return true;
149    
150 pcg 1.9 if (prot & PROTv4
151     && prot_ & PROTv4)
152 pcg 1.5 {
153 pcg 1.9 if (prot_ & (PROT_IPv4 | PROT_ICMPv4))
154 pcg 1.5 {
155     prot = prot_;
156     port = 0;
157     return true;
158     }
159    
160     if (conf
161     && prot_ & PROT_UDPv4
162     && conf->protocols & PROT_UDPv4
163     && conf->udp_port)
164     {
165     prot = prot_;
166 pcg 1.6 port = htons (conf->udp_port);
167 pcg 1.5 return true;
168     }
169    
170     if (conf
171     && prot_ & PROT_TCPv4
172     && conf->protocols & PROT_TCPv4
173     && conf->tcp_port)
174     {
175     prot = prot_;
176 pcg 1.6 port = htons (conf->tcp_port);
177 pcg 1.5 return true;
178     }
179     }
180    
181     return false;
182     }
183    
184 pcg 1.4 bool
185     operator == (const sockinfo &a, const sockinfo &b)
186     {
187     return a.host == b.host && a.port == b.port && a.prot == b.prot;
188     }
189    
190     bool
191     operator < (const sockinfo &a, const sockinfo &b)
192     {
193     return a.host < b.host
194     || (a.host == b.host && (a.port < b.port
195     || (a.port == b.port && a.prot < b.prot)));
196 pcg 1.1 }
197