]> git.ipfire.org Git - thirdparty/squid.git/blob - tools/purge/convert.cc
Merge from trunk
[thirdparty/squid.git] / tools / purge / convert.cc
1 //
2 // $Id: convert.cc,v 1.3 2000/06/20 09:43:01 voeckler Exp $
3 //
4 // Author: Jens-S. V?ckler <voeckler@rvs.uni-hannover.de>
5 //
6 // File: convert.cc
7 // Thu Oct 30 1997
8 //
9 // (c) 1997 Lehrgebiet Rechnernetze und Verteilte Systeme
10 // Universit?t Hannover, Germany
11 //
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.
21 //
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.
25 //
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
32 // SOFTWARE.
33 //
34 // Revision 1.3 2000/06/20 09:43:01 voeckler
35 // added FreeBSD related fixes and support.
36 //
37 // Revision 1.2 1999/01/19 11:00:50 voeckler
38 // Linux glibc2 fixes for sockets.
39 //
40 // Revision 1.1 1998/08/13 21:38:04 voeckler
41 // Initial revision
42 //
43 //
44 #if defined(__GNUC__) || defined(__GNUG__)
45 #pragma implementation
46 #endif
47
48 #include "convert.hh"
49 #include <string.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <netdb.h>
53 #include <stdlib.h>
54 #include <stdio.h>
55
56 #ifndef SA
57 #define SA struct sockaddr
58 #endif
59
60 static const char* RCS_ID =
61 "$Id: convert.cc,v 1.3 2000/06/20 09:43:01 voeckler Exp $";
62
63 const char*
64 my_inet_ntoa( const struct in_addr& a, HostAddress output )
65 // purpose: thread-safely convert IPv4 address -> ASCII representation
66 // paramtr: a (IN): networked representation of IPv4 address
67 // buffer (OUT): storage area to store representation into.
68 // returns: pointer to buffer
69 // goodies: INADDR_ANY will be converted to "*"
70 {
71 if ( a.s_addr == ntohl(INADDR_ANY) ) {
72 // 'default' or '*' or ...
73 output[0] = '*';
74 output[1] = '\0';
75 } else {
76 // ANSI C++ forbids casting to an array type, nag, nag, nag...
77 unsigned char s[sizeof(a.s_addr)];
78 memcpy( s, &a.s_addr, sizeof(a.s_addr) );
79
80 sprintf( output, "%d.%d.%d.%d", s[0], s[1], s[2], s[3] );
81 }
82 return output;
83 }
84
85 const char*
86 my_sock_ntoa( const struct sockaddr_in& a, SockAddress buffer )
87 // purpose: thread-safely convert IPv4 socket pair into ASCII rep.
88 // paramtr: a (IN): sockaddr_in address
89 // buffer (OUT): storage area to store representation into.
90 // returns: pointer to buffer
91 {
92 HostAddress host;
93 sprintf( buffer, "%s:%u",
94 my_inet_ntoa(a.sin_addr,host), ntohs(a.sin_port) );
95 return buffer;
96 }
97
98 const char*
99 my_sock_fd2a( int fd, SockAddress buffer, bool peer )
100 // purpose: thread-safely convert IPv4 socket FD associated address
101 // to ASCII representation
102 // paramtr: fd (IN): open socket FD
103 // buffer (OUT): storage area
104 // peer (IN): true, use peer (remote) socket pair
105 // false, use own (local) socket pair
106 // returns: NULL in case of error, or pointer to buffer otherwise
107 // Refer to errno in case of error (usually unconnected fd...)
108 {
109 struct sockaddr_in socket;
110 SOCKLEN len = sizeof(socket);
111
112 if ( (peer ? getpeername( fd, (SA*) &socket, &len ) :
113 getsockname( fd, (SA*) &socket, &len )) == -1 )
114 return NULL;
115 else
116 return my_sock_ntoa( socket, buffer );
117 }
118
119 int
120 convertHostname( const char* host, in_addr& dst )
121 // purpose: convert a numeric or symbolic hostname
122 // paramtr: host (IN): host description to convert
123 // dst (OUT): the internet address in network byteorder.
124 // returns: -1 in case of error, see h_errno; 0 otherwise.
125 {
126 if ( host == 0 ) return -1;
127 unsigned long int h = inet_addr(host);
128 if ( h == 0xFFFFFFFF && strncmp(host,"255.255.255.255",15) != 0 ) {
129 // symbolic host
130 struct hostent* dns = gethostbyname(host);
131 if ( dns == NULL ) return -1;
132 else memcpy( &dst.s_addr, dns->h_addr, dns->h_length );
133 } else {
134 // numeric host
135 dst.s_addr = h;
136 }
137 return 0;
138 }
139
140 int
141 convertPortname( const char* port, unsigned short& dst )
142 // purpose: convert a numeric or symbolic port number
143 // paramtr: port (IN): port description to convert
144 // dst (OUT): port number in network byteorder.
145 // returns: -1 in case of error, see errno; 0 otherwise.
146 {
147 int p = strtoul(port,0,0);
148
149 if ( p == 0 ) {
150 // symbolic port
151 struct servent* proto = getservbyname( port, "tcp" );
152 if ( proto == NULL ) return -1;
153 else dst = proto->s_port;
154 } else {
155 // numeric port
156 dst = htons(p);
157 }
158 return 0;
159 }