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.5 by pcg, Sat Apr 5 17:54:22 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.
24#include "gettext.h" 25#include "gettext.h"
25 26
26#include "sockinfo.h" 27#include "sockinfo.h"
27#include "slog.h" 28#include "slog.h"
28 29
30// all ipv4-based protocols
31#define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4)
32
29void sockinfo::set (const sockaddr_in *sa, u8 prot_) 33void sockinfo::set (const sockaddr_in *sa, u8 prot_)
30{ 34{
31 host = sa->sin_addr.s_addr; 35 host = sa->sin_addr.s_addr;
32 port = prot_ == PROT_IPv4 ? 0 : sa->sin_port; 36 port = prot_ & (PROT_IPv4 | PROT_ICMPv4) ? 0 : sa->sin_port;
33 prot = prot_; 37 prot = prot_;
34} 38}
35 39
36void 40void sockinfo::set (const char *hostname, u16 port_, u8 prot_)
37sockinfo::set (const conf_node *conf, u8 prot_)
38{ 41{
39 prot = prot_; 42 prot = prot_;
40 host = 0; 43 host = 0;
41 port = prot == PROT_UDPv4 ? htons (conf->udp_port) 44 port = htons (port_);
42 : prot == PROT_TCPv4 ? htons (conf->tcp_port)
43 : 0;
44 45
45 if (prot && conf->hostname) 46 if (prot & PROTv4
47 && hostname)
46 { 48 {
47 struct hostent *he = gethostbyname (conf->hostname); 49 struct hostent *he = gethostbyname (hostname);
48 50
49 if (he 51 if (he
50 && 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])
51 { 53 {
52 //sa->sin_family = he->h_addrtype; 54 //sa->sin_family = he->h_addrtype;
53 memcpy (&host, he->h_addr_list[0], 4); 55 memcpy (&host, he->h_addr_list[0], 4);
54
55 } 56 }
56 else 57 else
57 slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname); 58 slog (L_NOTICE, _("unable to resolve host '%s'"), hostname);
58 } 59 }
59} 60}
60 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}
61 71
62const sockaddr * 72const sockaddr *
63sockinfo::sav4() const 73sockinfo::sav4() const
64{ 74{
65 static sockaddr_in sa; 75 static sockaddr_in sa;
69 sa.sin_addr.s_addr = host; 79 sa.sin_addr.s_addr = host;
70 80
71 return (const sockaddr *)&sa; 81 return (const sockaddr *)&sa;
72} 82}
73 83
74static char hostport[10 + 15 + 1 + 5 + 1]; // protype / IPv4 : port 84static char hostport[10 + 15 + 1 + 5 + 1]; // proto / IPv4 : port
75 85
76const char * 86const char *
77sockinfo::ntoa () const 87sockinfo::ntoa () const
78{ 88{
79 in_addr ia = { host }; 89 in_addr ia = { host };
96sockinfo::supported_protocols (conf_node *conf) 106sockinfo::supported_protocols (conf_node *conf)
97{ 107{
98 u8 protocols = prot; 108 u8 protocols = prot;
99 109
100 if (prot & (PROT_UDPv4 | PROT_TCPv4)) 110 if (prot & (PROT_UDPv4 | PROT_TCPv4))
101 protocols |= PROT_IPv4; 111 protocols |= PROT_IPv4 | PROT_ICMPv4;
102 112
103 if (conf 113 if (conf
104 && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) 114 && prot & PROTv4
105 && conf->protocols & PROT_UDPv4 115 && conf->protocols & PROT_UDPv4
106 && conf->udp_port) 116 && conf->udp_port)
107 protocols |= PROT_UDPv4; 117 protocols |= PROT_UDPv4;
108 118
109 if (conf 119 if (conf
110 && prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) 120 && prot & PROTv4
111 && conf->protocols & PROT_TCPv4 121 && conf->protocols & PROT_TCPv4
112 && conf->tcp_port) 122 && conf->tcp_port)
113 protocols |= PROT_TCPv4; 123 protocols |= PROT_TCPv4;
114 124
115 return protocols; 125 return protocols;
119sockinfo::upgrade_protocol (u8 prot_, conf_node *conf) 129sockinfo::upgrade_protocol (u8 prot_, conf_node *conf)
120{ 130{
121 if (prot_ == prot) 131 if (prot_ == prot)
122 return true; 132 return true;
123 133
124 if (prot & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4) 134 if (prot & PROTv4
125 && prot_ & (PROT_IPv4 | PROT_UDPv4 | PROT_TCPv4)) 135 && prot_ & PROTv4)
126 { 136 {
127 if (prot_ & PROT_IPv4) 137 if (prot_ & (PROT_IPv4 | PROT_ICMPv4))
128 { 138 {
129 prot = prot_; 139 prot = prot_;
130 port = 0; 140 port = 0;
131 return true; 141 return true;
132 } 142 }
135 && prot_ & PROT_UDPv4 145 && prot_ & PROT_UDPv4
136 && conf->protocols & PROT_UDPv4 146 && conf->protocols & PROT_UDPv4
137 && conf->udp_port) 147 && conf->udp_port)
138 { 148 {
139 prot = prot_; 149 prot = prot_;
140 port = conf->udp_port; 150 port = htons (conf->udp_port);
141 return true; 151 return true;
142 } 152 }
143 153
144 if (conf 154 if (conf
145 && prot_ & PROT_TCPv4 155 && prot_ & PROT_TCPv4
146 && conf->protocols & PROT_TCPv4 156 && conf->protocols & PROT_TCPv4
147 && conf->tcp_port) 157 && conf->tcp_port)
148 { 158 {
149 prot = prot_; 159 prot = prot_;
150 port = conf->tcp_port; 160 port = htons (conf->tcp_port);
151 return true; 161 return true;
152 } 162 }
153 } 163 }
154 164
155 return false; 165 return false;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines