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.3 by pcg, Wed Apr 2 05:15:00 2003 UTC vs.
Revision 1.8 by pcg, Mon Apr 7 01:12:56 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.
31 host = sa->sin_addr.s_addr; 31 host = sa->sin_addr.s_addr;
32 port = prot_ == PROT_IPv4 ? 0 : sa->sin_port; 32 port = prot_ == PROT_IPv4 ? 0 : sa->sin_port;
33 prot = prot_; 33 prot = prot_;
34} 34}
35 35
36void 36void sockinfo::set (const char *hostname, u16 port_, u8 prot_)
37sockinfo::set (const conf_node *conf, u8 prot_)
38{ 37{
39 prot = prot_; 38 prot = prot_;
40 host = 0; 39 host = 0;
41 port = prot_ == PROT_UDPv4 ? htons (conf->udp_port) 40 port = htons (port_);
42 : prot_ == PROT_TCPv4 ? htons (conf->tcp_port)
43 : 0;
44 41
45 if (conf->hostname) 42 if (prot & (PROT_UDPv4 | PROT_TCPv4 | PROT_IPv4)
43 && hostname)
46 { 44 {
47 struct hostent *he = gethostbyname (conf->hostname); 45 struct hostent *he = gethostbyname (hostname);
48 46
49 if (he 47 if (he
50 && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) 48 && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0])
51 { 49 {
52 //sa->sin_family = he->h_addrtype; 50 //sa->sin_family = he->h_addrtype;
53 memcpy (&host, he->h_addr_list[0], 4); 51 memcpy (&host, he->h_addr_list[0], 4);
54
55 } 52 }
56 else 53 else
57 slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname); 54 slog (L_NOTICE, _("unable to resolve host '%s'"), hostname);
58 } 55 }
59} 56}
60 57
58void
59sockinfo::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}
61 67
62const sockaddr * 68const sockaddr *
63sockinfo::sav4() const 69sockinfo::sav4() const
64{ 70{
65 static sockaddr_in sa; 71 static sockaddr_in sa;
69 sa.sin_addr.s_addr = host; 75 sa.sin_addr.s_addr = host;
70 76
71 return (const sockaddr *)&sa; 77 return (const sockaddr *)&sa;
72} 78}
73 79
74static char hostport[15 + 1 + 5 + 1]; // IPv4 : port 80static char hostport[10 + 15 + 1 + 5 + 1]; // proto / IPv4 : port
75 81
76const char * 82const char *
77sockinfo::ntoa () const 83sockinfo::ntoa () const
78{ 84{
79 in_addr ia = { host }; 85 in_addr ia = { host };
85 91
86sockinfo::operator const char *() const 92sockinfo::operator const char *() const
87{ 93{
88 in_addr ia = { host }; 94 in_addr ia = { host };
89 95
90 sprintf (hostport, "%.15s:%d", inet_ntoa (ia), ntohs (port) & 0xffff); 96 sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff);
91 97
92 return hostport; 98 return hostport;
93} 99}
94 100
101u8
102sockinfo::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
124bool
125sockinfo::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
164bool
165operator == (const sockinfo &a, const sockinfo &b)
166{
167 return a.host == b.host && a.port == b.port && a.prot == b.prot;
168}
169
170bool
171operator < (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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines