]>
git.ipfire.org Git - thirdparty/squid.git/blob - tools/purge/convert.cc
2 // $Id: convert.cc,v 1.3 2000/06/20 09:43:01 voeckler Exp $
4 // Author: Jens-S. Vöckler <voeckler@rvs.uni-hannover.de>
9 // (c) 1997 Lehrgebiet Rechnernetze und Verteilte Systeme
10 // Universität Hannover, Germany
12 // Permission to use, copy, modify, distribute, and sell this software
13 // and its documentation for any purpose is hereby granted without fee,
14 // provided that (i) the above copyright notices and this permission
15 // notice appear in all copies of the software and related documentation,
16 // and (ii) the names of the Lehrgebiet Rechnernetze und Verteilte
17 // Systeme and the University of Hannover may not be used in any
18 // advertising or publicity relating to the software without the
19 // specific, prior written permission of Lehrgebiet Rechnernetze und
20 // Verteilte Systeme and the University of Hannover.
22 // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
24 // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
26 // IN NO EVENT SHALL THE LEHRGEBIET RECHNERNETZE UND VERTEILTE SYSTEME OR
27 // THE UNIVERSITY OF HANNOVER BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
28 // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
29 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT
30 // ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY,
31 // ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
34 // $Log: convert.cc,v $
35 // Revision 1.3 2000/06/20 09:43:01 voeckler
36 // added FreeBSD related fixes and support.
38 // Revision 1.2 1999/01/19 11:00:50 voeckler
39 // Linux glibc2 fixes for sockets.
41 // Revision 1.1 1998/08/13 21:38:04 voeckler
45 #if defined(__GNUC__) || defined(__GNUG__)
46 #pragma implementation
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
58 #define SA struct sockaddr
61 static const char* RCS_ID
=
62 "$Id: convert.cc,v 1.3 2000/06/20 09:43:01 voeckler Exp $";
65 my_inet_ntoa( const struct in_addr
& a
, HostAddress output
)
66 // purpose: thread-safely convert IPv4 address -> ASCII representation
67 // paramtr: a (IN): networked representation of IPv4 address
68 // buffer (OUT): storage area to store representation into.
69 // returns: pointer to buffer
70 // goodies: INADDR_ANY will be converted to "*"
72 if ( a
.s_addr
== ntohl(INADDR_ANY
) ) {
73 // 'default' or '*' or ...
77 // ANSI C++ forbids casting to an array type, nag, nag, nag...
78 unsigned char s
[sizeof(a
.s_addr
)];
79 memcpy( s
, &a
.s_addr
, sizeof(a
.s_addr
) );
81 sprintf( output
, "%d.%d.%d.%d", s
[0], s
[1], s
[2], s
[3] );
87 my_sock_ntoa( const struct sockaddr_in
& a
, SockAddress buffer
)
88 // purpose: thread-safely convert IPv4 socket pair into ASCII rep.
89 // paramtr: a (IN): sockaddr_in address
90 // buffer (OUT): storage area to store representation into.
91 // returns: pointer to buffer
94 sprintf( buffer
, "%s:%u",
95 my_inet_ntoa(a
.sin_addr
,host
), ntohs(a
.sin_port
) );
100 my_sock_fd2a( int fd
, SockAddress buffer
, bool peer
)
101 // purpose: thread-safely convert IPv4 socket FD associated address
102 // to ASCII representation
103 // paramtr: fd (IN): open socket FD
104 // buffer (OUT): storage area
105 // peer (IN): true, use peer (remote) socket pair
106 // false, use own (local) socket pair
107 // returns: NULL in case of error, or pointer to buffer otherwise
108 // Refer to errno in case of error (usually unconnected fd...)
110 struct sockaddr_in socket
;
111 SOCKLEN len
= sizeof(socket
);
113 if ( (peer
? getpeername( fd
, (SA
*) &socket
, &len
) :
114 getsockname( fd
, (SA
*) &socket
, &len
)) == -1 )
117 return my_sock_ntoa( socket
, buffer
);
121 convertHostname( const char* host
, in_addr
& dst
)
122 // purpose: convert a numeric or symbolic hostname
123 // paramtr: host (IN): host description to convert
124 // dst (OUT): the internet address in network byteorder.
125 // returns: -1 in case of error, see h_errno; 0 otherwise.
127 if ( host
== 0 ) return -1;
128 unsigned long int h
= inet_addr(host
);
129 if ( h
== 0xFFFFFFFF && strncmp(host
,"255.255.255.255",15) != 0 ) {
131 struct hostent
* dns
= gethostbyname(host
);
132 if ( dns
== NULL
) return -1;
133 else memcpy( &dst
.s_addr
, dns
->h_addr
, dns
->h_length
);
142 convertPortname( const char* port
, unsigned short& dst
)
143 // purpose: convert a numeric or symbolic port number
144 // paramtr: port (IN): port description to convert
145 // dst (OUT): port number in network byteorder.
146 // returns: -1 in case of error, see errno; 0 otherwise.
148 int p
= strtoul(port
,0,0);
152 struct servent
* proto
= getservbyname( port
, "tcp" );
153 if ( proto
== NULL
) return -1;
154 else dst
= proto
->s_port
;