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