ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/sockinfo.C
(Generate patch)

Comparing gvpe/src/sockinfo.C (file contents):
Revision 1.1 by pcg, Fri Mar 28 04:05:10 2003 UTC vs.
Revision 1.6 by pcg, Sat Apr 5 18:03:20 2003 UTC

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.
16 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/ 17*/
18 18
19#include "config.h" 19#include "config.h"
20 20
21#include "arpa/inet.h" 21#include <arpa/inet.h>
22#include <netdb.h>
22 23
23#include "gettext.h" 24#include "gettext.h"
24 25
25#include "sockinfo.h" 26#include "sockinfo.h"
26#include "slog.h" 27#include "slog.h"
31 port = prot_ == PROT_IPv4 ? 0 : sa->sin_port; 32 port = prot_ == PROT_IPv4 ? 0 : sa->sin_port;
32 prot = prot_; 33 prot = prot_;
33} 34}
34 35
35void 36void
36sockinfo::set (const conf_node *conf) 37sockinfo::set (const conf_node *conf, u8 prot_)
37{ 38{
39 prot = prot_;
38 host = 0; 40 host = 0;
39 port = htons (conf->udp_port); 41 port = prot == PROT_UDPv4 ? htons (conf->udp_port)
42 : prot == PROT_TCPv4 ? htons (conf->tcp_port)
43 : 0;
40 44
41 if (conf->hostname) 45 if (prot && conf->hostname)
42 { 46 {
43 struct hostent *he = gethostbyname (conf->hostname); 47 struct hostent *he = gethostbyname (conf->hostname);
44 48
45 if (he 49 if (he
46 && 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])
47 { 51 {
48 //sa->sin_family = he->h_addrtype; 52 //sa->sin_family = he->h_addrtype;
49 memcpy (&host, he->h_addr_list[0], 4); 53 memcpy (&host, he->h_addr_list[0], 4);
50 54
51 prot = PROT_UDPv4 | PROT_IPv4;
52 } 55 }
53 else 56 else
54 slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname); 57 slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname);
55 } 58 }
56} 59}
66 sa.sin_addr.s_addr = host; 69 sa.sin_addr.s_addr = host;
67 70
68 return (const sockaddr *)&sa; 71 return (const sockaddr *)&sa;
69} 72}
70 73
71static char hostport[15 + 1 + 5 + 1]; // IPv4 : port 74static char hostport[10 + 15 + 1 + 5 + 1]; // protype / IPv4 : port
72 75
73const char * 76const char *
74sockinfo::ntoa () const 77sockinfo::ntoa () const
75{ 78{
76 in_addr ia = { host }; 79 in_addr ia = { host };
82 85
83sockinfo::operator const char *() const 86sockinfo::operator const char *() const
84{ 87{
85 in_addr ia = { host }; 88 in_addr ia = { host };
86 89
87 sprintf (hostport, "%.15s:%d", inet_ntoa (ia), ntohs (port) & 0xffff); 90 sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff);
88 91
89 return hostport; 92 return hostport;
90} 93}
91 94
95u8
96sockinfo::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
118bool
119sockinfo::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 port = htons (conf->udp_port);
141 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 port = htons (conf->tcp_port);
151 return true;
152 }
153 }
154
155 return false;
156}
157
158bool
159operator == (const sockinfo &a, const sockinfo &b)
160{
161 return a.host == b.host && a.port == b.port && a.prot == b.prot;
162}
163
164bool
165operator < (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}
171

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines