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.10 by pcg, Thu Oct 16 02:41:21 2003 UTC

1/* 1/*
2 sockinfo.c -- socket address management 2 sockinfo.C -- socket address management
3 Copyright (C) 2003 Marc Lehmann <pcg@goof.com>
3 4
4 This program is free software; you can redistribute it and/or modify 5 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 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 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version. 8 (at your option) any later version.
16 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/ 18*/
18 19
19#include "config.h" 20#include "config.h"
20 21
21#include "arpa/inet.h" 22#include <arpa/inet.h>
23#include <netdb.h>
22 24
23#include "gettext.h" 25#include "gettext.h"
24 26
25#include "sockinfo.h" 27#include "sockinfo.h"
26#include "slog.h" 28#include "slog.h"
27 29
30// all ipv4-based protocols
31#define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4)
32
28void sockinfo::set (const sockaddr_in *sa, u8 prot_) 33void sockinfo::set (const sockaddr_in *sa, u8 prot_)
29{ 34{
30 host = sa->sin_addr.s_addr; 35 host = sa->sin_addr.s_addr;
31 port = prot_ == PROT_IPv4 ? 0 : sa->sin_port; 36 port = prot_ & (PROT_IPv4 | PROT_ICMPv4) ? 0 : sa->sin_port;
32 prot = prot_; 37 prot = prot_;
33} 38}
34 39
35void 40void sockinfo::set (const char *hostname, u16 port_, u8 prot_)
36sockinfo::set (const conf_node *conf)
37{ 41{
42 prot = prot_;
38 host = 0; 43 host = 0;
39 port = htons (conf->udp_port); 44 port = htons (port_);
40 45
41 if (conf->hostname) 46 if (prot & PROTv4
47 && hostname)
42 { 48 {
43 struct hostent *he = gethostbyname (conf->hostname); 49 struct hostent *he = gethostbyname (hostname);
44 50
45 if (he 51 if (he
46 && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) 52 && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0])
47 { 53 {
48 //sa->sin_family = he->h_addrtype; 54 //sa->sin_family = he->h_addrtype;
49 memcpy (&host, he->h_addr_list[0], 4); 55 memcpy (&host, he->h_addr_list[0], 4);
50
51 prot = PROT_UDPv4 | PROT_IPv4;
52 } 56 }
53 else 57 else
54 slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname); 58 slog (L_NOTICE, _("unable to resolve host '%s'"), hostname);
55 } 59 }
56} 60}
57 61
62void
63sockinfo::set (const conf_node *conf, u8 prot_)
64{
65 set (conf->hostname,
66 prot_ == PROT_UDPv4 ? conf->udp_port
67 : prot_ == PROT_TCPv4 ? conf->tcp_port
68 : 0,
69 prot_);
70}
58 71
59const sockaddr * 72const sockaddr *
60sockinfo::sav4() const 73sockinfo::sav4() const
61{ 74{
62 static sockaddr_in sa; 75 static sockaddr_in sa;
66 sa.sin_addr.s_addr = host; 79 sa.sin_addr.s_addr = host;
67 80
68 return (const sockaddr *)&sa; 81 return (const sockaddr *)&sa;
69} 82}
70 83
71static char hostport[15 + 1 + 5 + 1]; // IPv4 : port 84static char hostport[10 + 15 + 1 + 5 + 1]; // proto / IPv4 : port
72 85
73const char * 86const char *
74sockinfo::ntoa () const 87sockinfo::ntoa () const
75{ 88{
76 in_addr ia = { host }; 89 in_addr ia = { host };
82 95
83sockinfo::operator const char *() const 96sockinfo::operator const char *() const
84{ 97{
85 in_addr ia = { host }; 98 in_addr ia = { host };
86 99
87 sprintf (hostport, "%.15s:%d", inet_ntoa (ia), ntohs (port) & 0xffff); 100 sprintf (hostport, "%s/%.15s:%d", strprotocol (prot), inet_ntoa (ia), ntohs (port) & 0xffff);
88 101
89 return hostport; 102 return hostport;
90} 103}
91 104
105u8
106sockinfo::supported_protocols (conf_node *conf)
107{
108 u8 protocols = prot;
109
110 if (prot & (PROT_UDPv4 | PROT_TCPv4))
111 protocols |= PROT_IPv4 | PROT_ICMPv4;
112
113 if (conf
114 && prot & PROTv4
115 && conf->protocols & PROT_UDPv4
116 && conf->udp_port)
117 protocols |= PROT_UDPv4;
118
119 if (conf
120 && prot & PROTv4
121 && conf->protocols & PROT_TCPv4
122 && conf->tcp_port)
123 protocols |= PROT_TCPv4;
124
125 return protocols;
126}
127
128bool
129sockinfo::upgrade_protocol (u8 prot_, conf_node *conf)
130{
131 if (prot_ == prot)
132 return true;
133
134 if (prot & PROTv4
135 && prot_ & PROTv4)
136 {
137 if (prot_ & (PROT_IPv4 | PROT_ICMPv4))
138 {
139 prot = prot_;
140 port = 0;
141 return true;
142 }
143
144 if (conf
145 && prot_ & PROT_UDPv4
146 && conf->protocols & PROT_UDPv4
147 && conf->udp_port)
148 {
149 prot = prot_;
150 port = htons (conf->udp_port);
151 return true;
152 }
153
154 if (conf
155 && prot_ & PROT_TCPv4
156 && conf->protocols & PROT_TCPv4
157 && conf->tcp_port)
158 {
159 prot = prot_;
160 port = htons (conf->tcp_port);
161 return true;
162 }
163 }
164
165 return false;
166}
167
168bool
169operator == (const sockinfo &a, const sockinfo &b)
170{
171 return a.host == b.host && a.port == b.port && a.prot == b.prot;
172}
173
174bool
175operator < (const sockinfo &a, const sockinfo &b)
176{
177 return a.host < b.host
178 || (a.host == b.host && (a.port < b.port
179 || (a.port == b.port && a.prot < b.prot)));
180}
181

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines