--- gvpe/src/sockinfo.C 2004/04/02 14:42:45 1.12 +++ gvpe/src/sockinfo.C 2012/12/04 13:49:16 1.29 @@ -1,42 +1,65 @@ /* sockinfo.C -- socket address management - Copyright (C) 2003 Marc Lehmann + Copyright (C) 2003-2008 Marc Lehmann - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + This file is part of GVPE. + + GVPE is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3 of the License, or (at your + option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, see . + + Additional permission under GNU GPL version 3 section 7 + + If you modify this Program, or any covered work, by linking or + combining it with the OpenSSL project's OpenSSL library (or a modified + version of that library), containing parts covered by the terms of the + OpenSSL or SSLeay licenses, the licensors of this Program grant you + additional permission to convey the resulting work. Corresponding + Source for a non-source form of such a combination shall include the + source code for the parts of OpenSSL used as well as that of the + covered work. */ #include "config.h" +// for gethostbyname #include +// for inet_aton +#include +#include +#include + #include "gettext.h" #include "sockinfo.h" #include "slog.h" +#include +#include + // all ipv4-based protocols -#define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4) +#define PROTv4 (PROT_UDPv4 | PROT_TCPv4 | PROT_ICMPv4 | PROT_IPv4 | PROT_DNSv4) -void sockinfo::set (const sockaddr_in *sa, u8 prot_) +void +sockinfo::set (const sockaddr_in *sa, u8 prot_) { host = sa->sin_addr.s_addr; port = prot_ & (PROT_IPv4 | PROT_ICMPv4) ? 0 : sa->sin_port; prot = prot_; } -void sockinfo::set (const char *hostname, u16 port_, u8 prot_) +void +sockinfo::set (const char *hostname, u16 port_, u8 prot_) { prot = prot_; host = 0; @@ -45,10 +68,17 @@ if (prot & PROTv4 && hostname) { - struct hostent *he = gethostbyname (hostname); + struct in_addr in; + struct hostent *he; - if (he - && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) + if (inet_aton (hostname, &in)) + { + host = in.s_addr; + } + else if ((he = gethostbyname (hostname)) + && he->h_addrtype == AF_INET + && he->h_length == 4 + && he->h_addr_list[0]) { //sa->sin_family = he->h_addrtype; memcpy (&host, he->h_addr_list[0], 4); @@ -61,12 +91,17 @@ void sockinfo::set (const conf_node *conf, u8 prot_) { - set (conf->hostname, - prot_ == PROT_UDPv4 ? conf->udp_port - : prot_ == PROT_TCPv4 ? conf->tcp_port - : prot_ == PROT_DNSv4 ? conf->dns_port - : 0, - prot_); + if (prot_ == PROT_DNSv4) + { + host = htonl (conf->id); port = 0; prot = prot_; + } + else + set (conf->hostname, + prot_ == PROT_UDPv4 ? conf->udp_port + : prot_ == PROT_TCPv4 ? conf->tcp_port + : prot_ == PROT_DNSv4 ? conf->dns_port + : 0, + prot_); } const sockaddr * @@ -93,6 +128,12 @@ return hostport; } +bool +sockinfo::valid () const +{ + return (prot & THISNODE->protocols) && host; +} + sockinfo::operator const char *() const { in_addr ia = { host }; @@ -107,25 +148,20 @@ { u8 protocols = prot; - if (prot & (PROT_UDPv4 | PROT_TCPv4)) + if (prot & (PROT_IPv4 | PROT_ICMPv4 | PROT_UDPv4 | PROT_TCPv4)) protocols |= PROT_IPv4 | PROT_ICMPv4; - if (conf - && prot & PROTv4 - && conf->protocols & PROT_UDPv4 - && conf->udp_port) - protocols |= PROT_UDPv4; - - if (conf - && prot & PROTv4 - && conf->protocols & PROT_TCPv4 - && conf->tcp_port) - protocols |= PROT_TCPv4; - - if (conf - && prot & PROTv4 - && conf->protocols & PROT_DNSv4) - protocols |= PROT_DNSv4; + if (conf && prot & PROTv4) + { + if (conf->protocols & PROT_UDPv4 && conf->udp_port) + protocols |= PROT_UDPv4; + + if (conf->protocols & PROT_TCPv4 && conf->tcp_port) + protocols |= PROT_TCPv4; + + if (conf->protocols & PROT_DNSv4 && conf->dns_port) + protocols |= PROT_DNSv4; + } return protocols; }