-//
-// $Id: convert.cc,v 1.3 2000/06/20 09:43:01 voeckler Exp $
-//
-// Author: Jens-S. Vöckler <voeckler@rvs.uni-hannover.de>
+/*
+ * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+// Author: Jens-S. V?ckler <voeckler@rvs.uni-hannover.de>
//
// File: convert.cc
// Thu Oct 30 1997
//
// (c) 1997 Lehrgebiet Rechnernetze und Verteilte Systeme
-// Universität Hannover, Germany
+// Universit?t Hannover, Germany
//
// Permission to use, copy, modify, distribute, and sell this software
// and its documentation for any purpose is hereby granted without fee,
// ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
// SOFTWARE.
//
-// $Log: convert.cc,v $
// Revision 1.3 2000/06/20 09:43:01 voeckler
// added FreeBSD related fixes and support.
//
// Revision 1.1 1998/08/13 21:38:04 voeckler
// Initial revision
//
-//
-#if defined(__GNUC__) || defined(__GNUG__)
-#pragma implementation
-#endif
+#include "squid.h"
#include "convert.hh"
-#include <string.h>
+
+#include <cstdlib>
+#include <cstring>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
-#include <stdlib.h>
-#include <stdio.h>
#ifndef SA
#define SA struct sockaddr
#endif
-static const char* RCS_ID =
-"$Id: convert.cc,v 1.3 2000/06/20 09:43:01 voeckler Exp $";
-
const char*
my_inet_ntoa( const struct in_addr& a, HostAddress output )
- // purpose: thread-safely convert IPv4 address -> ASCII representation
- // paramtr: a (IN): networked representation of IPv4 address
- // buffer (OUT): storage area to store representation into.
- // returns: pointer to buffer
- // goodies: INADDR_ANY will be converted to "*"
+// purpose: thread-safely convert IPv4 address -> ASCII representation
+// paramtr: a (IN): networked representation of IPv4 address
+// buffer (OUT): storage area to store representation into.
+// returns: pointer to buffer
+// goodies: INADDR_ANY will be converted to "*"
{
- if ( a.s_addr == ntohl(INADDR_ANY) ) {
- // 'default' or '*' or ...
- output[0] = '*';
- output[1] = '\0';
- } else {
- // ANSI C++ forbids casting to an array type, nag, nag, nag...
- unsigned char s[sizeof(a.s_addr)];
- memcpy( s, &a.s_addr, sizeof(a.s_addr) );
+ if ( a.s_addr == ntohl(INADDR_ANY) ) {
+ // 'default' or '*' or ...
+ output[0] = '*';
+ output[1] = '\0';
+ } else {
+ // ANSI C++ forbids casting to an array type, nag, nag, nag...
+ unsigned char s[sizeof(a.s_addr)];
+ memcpy( s, &a.s_addr, sizeof(a.s_addr) );
- sprintf( output, "%d.%d.%d.%d", s[0], s[1], s[2], s[3] );
- }
- return output;
+ snprintf(output, sizeof(HostAddress), "%d.%d.%d.%d", s[0], s[1], s[2], s[3] );
+ }
+ return output;
}
const char*
my_sock_ntoa( const struct sockaddr_in& a, SockAddress buffer )
- // purpose: thread-safely convert IPv4 socket pair into ASCII rep.
- // paramtr: a (IN): sockaddr_in address
- // buffer (OUT): storage area to store representation into.
- // returns: pointer to buffer
+// purpose: thread-safely convert IPv4 socket pair into ASCII rep.
+// paramtr: a (IN): sockaddr_in address
+// buffer (OUT): storage area to store representation into.
+// returns: pointer to buffer
{
- HostAddress host;
- sprintf( buffer, "%s:%u",
- my_inet_ntoa(a.sin_addr,host), ntohs(a.sin_port) );
- return buffer;
+ HostAddress host;
+ snprintf( buffer, sizeof(SockAddress), "%s:%u",
+ my_inet_ntoa(a.sin_addr,host), ntohs(a.sin_port) );
+ return buffer;
}
const char*
my_sock_fd2a( int fd, SockAddress buffer, bool peer )
- // purpose: thread-safely convert IPv4 socket FD associated address
- // to ASCII representation
- // paramtr: fd (IN): open socket FD
- // buffer (OUT): storage area
- // peer (IN): true, use peer (remote) socket pair
- // false, use own (local) socket pair
- // returns: NULL in case of error, or pointer to buffer otherwise
- // Refer to errno in case of error (usually unconnected fd...)
+// purpose: thread-safely convert IPv4 socket FD associated address
+// to ASCII representation
+// paramtr: fd (IN): open socket FD
+// buffer (OUT): storage area
+// peer (IN): true, use peer (remote) socket pair
+// false, use own (local) socket pair
+// returns: NULL in case of error, or pointer to buffer otherwise
+// Refer to errno in case of error (usually unconnected fd...)
{
- struct sockaddr_in socket;
- SOCKLEN len = sizeof(socket);
+ struct sockaddr_in socket;
+ socklen_t len = sizeof(socket);
- if ( (peer ? getpeername( fd, (SA*) &socket, &len ) :
- getsockname( fd, (SA*) &socket, &len )) == -1 )
- return NULL;
- else
- return my_sock_ntoa( socket, buffer );
+ if ( (peer ? getpeername( fd, (SA*) &socket, &len ) :
+ getsockname( fd, (SA*) &socket, &len )) == -1 )
+ return NULL;
+ else
+ return my_sock_ntoa( socket, buffer );
}
int
convertHostname( const char* host, in_addr& dst )
- // purpose: convert a numeric or symbolic hostname
- // paramtr: host (IN): host description to convert
- // dst (OUT): the internet address in network byteorder.
- // returns: -1 in case of error, see h_errno; 0 otherwise.
+// purpose: convert a numeric or symbolic hostname
+// paramtr: host (IN): host description to convert
+// dst (OUT): the internet address in network byteorder.
+// returns: -1 in case of error, see h_errno; 0 otherwise.
{
- if ( host == 0 ) return -1;
- unsigned long int h = inet_addr(host);
- if ( h == 0xFFFFFFFF && strncmp(host,"255.255.255.255",15) != 0 ) {
- // symbolic host
- struct hostent* dns = gethostbyname(host);
- if ( dns == NULL ) return -1;
- else memcpy( &dst.s_addr, dns->h_addr, dns->h_length );
- } else {
- // numeric host
- dst.s_addr = h;
- }
- return 0;
+ if ( host == 0 ) return -1;
+ unsigned long int h = inet_addr(host);
+ if ( h == 0xFFFFFFFF && strncmp(host,"255.255.255.255",15) != 0 ) {
+ // symbolic host
+ struct hostent* dns = gethostbyname(host);
+ if ( dns == NULL ) return -1;
+ else memcpy( &dst.s_addr, dns->h_addr, dns->h_length );
+ } else {
+ // numeric host
+ dst.s_addr = h;
+ }
+ return 0;
}
int
convertPortname( const char* port, unsigned short& dst )
- // purpose: convert a numeric or symbolic port number
- // paramtr: port (IN): port description to convert
- // dst (OUT): port number in network byteorder.
- // returns: -1 in case of error, see errno; 0 otherwise.
+// purpose: convert a numeric or symbolic port number
+// paramtr: port (IN): port description to convert
+// dst (OUT): port number in network byteorder.
+// returns: -1 in case of error, see errno; 0 otherwise.
{
- int p = strtoul(port,0,0);
+ int p = strtoul(port,0,0);
- if ( p == 0 ) {
- // symbolic port
- struct servent* proto = getservbyname( port, "tcp" );
- if ( proto == NULL ) return -1;
- else dst = proto->s_port;
- } else {
- // numeric port
- dst = htons(p);
- }
- return 0;
+ if ( p == 0 ) {
+ // symbolic port
+ struct servent* proto = getservbyname( port, "tcp" );
+ if ( proto == NULL ) return -1;
+ else dst = proto->s_port;
+ } else {
+ // numeric port
+ dst = htons(p);
+ }
+ return 0;
}
+