apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Reid" <dr...@jetnet.co.uk>
Subject [PATCH] apr_get_formatted_address
Date Sat, 02 Dec 2000 11:12:37 GMT
Index: include/apr_network_io.h

===================================================================

RCS file: /home/cvs/apr/include/apr_network_io.h,v

retrieving revision 1.85

diff -u -r1.85 apr_network_io.h

--- include/apr_network_io.h 2000/12/01 18:47:28 1.85

+++ include/apr_network_io.h 2000/12/02 10:23:00

@@ -134,6 +134,16 @@

APR_REMOTE

} apr_interface_e;


+/* An enum that is used to tell us what format the user wants us to

+ * use when formatting an apr_sockaddr_t

+ */

+typedef enum {

+ APR_FORMAT_ADDR_ONLY, /* numeric address only */

+ APR_FORMAT_HOST_ONLY, /* hostname or numeric address only */

+ APR_FORMAT_PORT_ONLY, /* port number or service name only */

+ APR_FORMAT_ALL /* address/hostname and port */

+} apr_address_format_e;

+

/* I guess not everybody uses inet_addr. This defines apr_inet_addr

* appropriately.

*/

@@ -632,6 +642,15 @@


apr_status_t apr_getservbyname(apr_sockaddr_t *sockaddr, const char
*servname);


+/**

+ * Given an apr_sockaddr_t return a formatted string representation of the

+ * address in the format specified

+ * @param string The formatted string

+ * @param sa The apr_sockaddr_t to use

+ * @param how The format to use

+ */

+apr_status_t apr_get_formatted_address(char **string, apr_sockaddr_t *sa,

+ apr_address_format_e how);

#ifdef __cplusplus

}

#endif

Index: network_io/unix/sa_common.c

===================================================================

RCS file: /home/cvs/apr/network_io/unix/sa_common.c,v

retrieving revision 1.15

diff -u -r1.15 sa_common.c

--- network_io/unix/sa_common.c 2000/12/01 18:47:31 1.15

+++ network_io/unix/sa_common.c 2000/12/02 10:23:02

@@ -175,6 +175,7 @@

*sa = NULL;

return APR_EINVAL;

}

+ (*sa)->pool = sock->cntxt;

return APR_SUCCESS;

}


@@ -286,5 +287,52 @@

return APR_SUCCESS;

}

return errno;

+}

+

+apr_status_t apr_get_formatted_address(char **string, apr_sockaddr_t *sa,

+ apr_address_format_e how)

+{

+ if (sa->hostname == NULL){

+ sa->hostname = apr_palloc(sa->pool, sizeof(char) * sa->addr_str_len);

+ apr_inet_ntop(sa->sa.sin.sin_family, sa->ipaddr_ptr,

+ sa->hostname, sa->addr_str_len);

+ }

+

+ if (how == APR_FORMAT_ADDR_ONLY){

+ if (!isdigit(*sa->hostname) && *sa->hostname != ':'){

+ /* stored hostname is not a numeric representation */

+ (*string) = apr_palloc(sa->pool, sizeof(char) * sa->addr_str_len);

+ apr_inet_ntop(sa->sa.sin.sin_family, sa->ipaddr_ptr,

+ (*string), sa->addr_str_len);

+ return APR_SUCCESS;

+ }

+ }

+ if (how == APR_FORMAT_ADDR_ONLY || how == APR_FORMAT_HOST_ONLY){

+ (*string) = apr_pstrdup(sa->pool, sa->hostname);

+ }

+ if (how == APR_FORMAT_PORT_ONLY){

+ if (sa->servname == NULL)

+ (*string) = apr_psprintf(sa->pool, "%d", ntohs(sa->sa.sin.sin_port));

+ else

+ (*string) = apr_pstrdup(sa->pool, sa->servname);

+ return APR_SUCCESS;

+ }

+ if (how == APR_FORMAT_ALL){

+#if APR_HAVE_IPV6

+ if ((isdigit(*sa->hostname) || *sa->hostname == ':') &&

+ sa->sa.sin.sin_family == APR_INET6)

+ (*string) = apr_psprintf(sa->pool, "[%s]:",sa->hostname);

+ else

+#endif

+ (*string) = apr_psprintf(sa->pool, "%s:", sa->hostname);

+

+ if (sa->servname == NULL){

+ char *sp = apr_psprintf(sa->pool, "%d", ntohs(sa->sa.sin.sin_port));

+ (*string) = apr_pstrcat(sa->pool, (*string), sp, NULL);

+ } else

+ (*string) = apr_pstrcat(sa->pool, (*string), sa->servname, NULL);

+ return APR_SUCCESS;

+ }

+ return APR_EINVAL;

}


Index: test/client.c

===================================================================

RCS file: /home/cvs/apr/test/client.c,v

retrieving revision 1.21

diff -u -r1.21 client.c

--- test/client.c 2000/11/21 19:15:27 1.21

+++ test/client.c 2000/12/02 10:23:06

@@ -71,7 +71,6 @@

char msgbuf[80];

char *local_ipaddr, *remote_ipaddr;

char *dest = "127.0.0.1";

- apr_port_t local_port, remote_port;

apr_interval_time_t read_timeout = -1;

apr_sockaddr_t *local_sa, *remote_sa;


@@ -141,12 +140,10 @@

}


apr_get_sockaddr(&remote_sa, APR_REMOTE, sock);

- apr_get_ipaddr(&remote_ipaddr, remote_sa);

- apr_get_port(&remote_port, remote_sa);

+ apr_get_formatted_address(&remote_ipaddr, remote_sa, APR_FORMAT_ALL);

apr_get_sockaddr(&local_sa, APR_LOCAL, sock);

- apr_get_ipaddr(&local_ipaddr, local_sa);

- apr_get_port(&local_port, local_sa);

- fprintf(stdout, "\tClient socket: %s:%u -> %s:%u\n", local_ipaddr,
local_port, remote_ipaddr, remote_port);

+ apr_get_formatted_address(&local_ipaddr, local_sa, APR_FORMAT_ALL);

+ fprintf(stdout, "\tClient socket: %s -> %s\n", local_ipaddr,
remote_ipaddr);


fprintf(stdout, "\tClient: Trying to send data over socket.......");

length = STRLEN;

Index: test/server.c

===================================================================

RCS file: /home/cvs/apr/test/server.c,v

retrieving revision 1.21

diff -u -r1.21 server.c

--- test/server.c 2000/11/21 19:15:27 1.21

+++ test/server.c 2000/12/02 10:23:07

@@ -72,7 +72,6 @@

char datarecv[STRLEN] = "Recv data test";

const char *bind_to_ipaddr = NULL;

char *local_ipaddr, *remote_ipaddr;

- apr_port_t local_port, remote_port;

apr_sockaddr_t *localsa = NULL, *remotesa;

apr_status_t stat;

int family = APR_UNSPEC;

@@ -204,12 +203,10 @@

fprintf(stdout, "OK\n");


apr_get_sockaddr(&remotesa, APR_REMOTE, sock2);

- apr_get_ipaddr(&remote_ipaddr, remotesa);

- apr_get_port(&remote_port, remotesa);

+ apr_get_formatted_address(&remote_ipaddr, remotesa, APR_FORMAT_ALL);

apr_get_sockaddr(&localsa, APR_LOCAL, sock2);

- apr_get_ipaddr(&local_ipaddr, localsa);

- apr_get_port(&local_port, localsa);

- fprintf(stdout, "\tServer socket: %s:%u -> %s:%u\n", local_ipaddr,
local_port, remote_ipaddr, remote_port);

+ apr_get_formatted_address(&local_ipaddr, localsa, APR_FORMAT_ALL);

+ fprintf(stdout, "\tServer socket: %s -> %s\n", local_ipaddr,
remote_ipaddr);


length = STRLEN;

fprintf(stdout, "\tServer: Trying to recv data from socket.......");



Mime
View raw message