apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From traw...@locus.apache.org
Subject cvs commit: apr/network_io/unix sa_common.c
Date Thu, 07 Dec 2000 18:10:28 GMT
trawick     00/12/07 10:10:28

  Modified:    .        CHANGES aprlib.def
               include  apr_network_io.h
               network_io/os2 Makefile.in
               network_io/unix sa_common.c
  Log:
  Add apr_parse_addr_port() for parsing the hostname:port portion of
  URLs and similar strings.
  
  Somebody with VC++ 6.0 needs to update the .dsp file(s) to include
  inet_pton.c.  The BeOS Makefile.in wasn't updated because I think
  BeOS uses the unix code now.
  
  Revision  Changes    Path
  1.18      +2 -0      apr/CHANGES
  
  Index: CHANGES
  ===================================================================
  RCS file: /home/cvs/apr/CHANGES,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- CHANGES	2000/12/07 06:58:57	1.17
  +++ CHANGES	2000/12/07 18:10:24	1.18
  @@ -1,4 +1,6 @@
   Changes with APR a9
  +  *) Add apr_parse_addr_port() for parsing the hostname:port portion
  +     of URLs and similar strings.  [Jeff Trawick]
   
     *) Add Win32 MMAP support [William Rowe]
   
  
  
  
  1.50      +1 -0      apr/aprlib.def
  
  Index: aprlib.def
  ===================================================================
  RCS file: /home/cvs/apr/aprlib.def,v
  retrieving revision 1.49
  retrieving revision 1.50
  diff -u -r1.49 -r1.50
  --- aprlib.def	2000/12/05 22:08:19	1.49
  +++ aprlib.def	2000/12/07 18:10:24	1.50
  @@ -88,6 +88,7 @@
           apr_get_port
   	apr_set_port
           apr_get_inaddr
  +        apr_parse_addr_port
   
   ;
   ;
  
  
  
  1.88      +33 -0     apr/include/apr_network_io.h
  
  Index: apr_network_io.h
  ===================================================================
  RCS file: /home/cvs/apr/include/apr_network_io.h,v
  retrieving revision 1.87
  retrieving revision 1.88
  diff -u -r1.87 -r1.88
  --- apr_network_io.h	2000/12/04 06:15:05	1.87
  +++ apr_network_io.h	2000/12/07 18:10:25	1.88
  @@ -306,6 +306,39 @@
                                apr_pool_t *p);
   
   /**
  + * Parse hostname/IP address with scope id and port.
  + *
  + * Any of the following strings are accepted:
  + *   8080                  (just the port number)
  + *   www.apache.org        (just the hostname)
  + *   www.apache.org:8080   (hostname and port number)
  + *   [fe80::1]:80          (IPv6 numeric address string only)
  + *   [fe80::1%eth0]        (IPv6 numeric address string and scope id)
  + *
  + * Invalid strings:
  + *                         (empty string)
  + *   [abc]                 (not valid IPv6 numeric address string)
  + *   abc:65536             (invalid port number)
  + *
  + * @param addr The new buffer containing just the hostname.  On output, *addr 
  + *             will be NULL if no hostname/IP address was specfied.
  + * @param scope_id The new buffer containing just the scope id.  On output, 
  + *                 *scope_id will be NULL if no scope id was specified.
  + * @param port The port number.  On output, *port will be 0 if no port was 
  + *             specified.
  + * @param str The input string to be parsed.
  + * @param p The pool from which *addr and *scope_id are allocated.
  + * @tip If scope id shouldn't be allowed, check for scope_id != NULL in addition 
  + *      to checking the return code.  If addr/hostname should be required, check 
  + *      for addr == NULL in addition to checking the return code.
  + */
  +apr_status_t apr_parse_addr_port(char **addr,
  +                                 char **scope_id,
  +                                 apr_port_t *port,
  +                                 const char *str,
  +                                 apr_pool_t *p);
  +
  +/**
    * Get name of the current machine
    * @param buf A buffer to store the hostname in.
    * @param len The maximum length of the hostname that can be stored in the
  
  
  
  1.14      +1 -0      apr/network_io/os2/Makefile.in
  
  Index: Makefile.in
  ===================================================================
  RCS file: /home/cvs/apr/network_io/os2/Makefile.in,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- Makefile.in	2000/11/25 03:36:01	1.13
  +++ Makefile.in	2000/12/07 18:10:25	1.14
  @@ -17,6 +17,7 @@
   	sockopt.o \
   	sockaddr.o \
   	inet_ntop.o \
  +	inet_pton.o \
   	os2calls.o
   
   .c.o:
  
  
  
  1.16      +102 -0    apr/network_io/unix/sa_common.c
  
  Index: sa_common.c
  ===================================================================
  RCS file: /home/cvs/apr/network_io/unix/sa_common.c,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- sa_common.c	2000/12/01 18:47:31	1.15
  +++ sa_common.c	2000/12/07 18:10:27	1.16
  @@ -65,6 +65,10 @@
   
   #include "apr.h"
   
  +#ifdef HAVE_STDLIB_H
  +#include <stdlib.h>
  +#endif
  +
   apr_status_t apr_set_port(apr_sockaddr_t *sockaddr, apr_port_t port)
   {
       /* XXX IPv6: assumes sin_port and sin6_port at same offset */
  @@ -174,6 +178,104 @@
       else {
           *sa = NULL;
           return APR_EINVAL;
  +    }
  +    return APR_SUCCESS;
  +}
  +
  +apr_status_t apr_parse_addr_port(char **addr,
  +                                 char **scope_id,
  +                                 apr_port_t *port,
  +                                 const char *str,
  +                                 apr_pool_t *p)
  +{
  +    const char *ch, *lastchar;
  +    int big_port;
  +    apr_size_t addrlen;
  +
  +    *addr = NULL;         /* assume not specified */
  +    *scope_id = NULL;     /* assume not specified */
  +    *port = 0;            /* assume not specified */
  +
  +    /* First handle the optional port number.  That may be all that
  +     * is specified in the string.
  +     */
  +    ch = lastchar = str + strlen(str) - 1;
  +    while (ch >= str && apr_isdigit(*ch)) {
  +        --ch;
  +    }
  +
  +    if (ch < str) {       /* Entire string is the port. */
  +        big_port = atoi(str);
  +        if (big_port < 1 || big_port > 65535) {
  +            return APR_EINVAL;
  +        }
  +        *port = big_port;
  +        return APR_SUCCESS;
  +    }
  +
  +    if (*ch == ':' && ch < lastchar) { /* host and port number specified */
  +        if (ch == str) {               /* string starts with ':' -- bad */
  +            return APR_EINVAL;
  +        }
  +        big_port = atoi(ch + 1);
  +        if (big_port < 1 || big_port > 65535) {
  +            return APR_EINVAL;
  +        }
  +        *port = big_port;
  +        lastchar = ch - 1;
  +    }
  +
  +    /* now handle the hostname */
  +    addrlen = lastchar - str + 1;
  +
  +#if APR_HAVE_IPV6 /* XXX don't require this; would need to pass char[] for ipaddr and always
define APR_INET6 */
  +    if (*str == '[') {
  +        const char *end_bracket = memchr(str, ']', addrlen);
  +        struct in6_addr ipaddr;
  +        const char *scope_delim;
  +
  +        if (!end_bracket || end_bracket != lastchar) {
  +            *port = 0;
  +            return APR_EINVAL;
  +        }
  +
  +        /* handle scope id; this is the only context where it is allowed */
  +        scope_delim = memchr(str, '%', addrlen);
  +        if (scope_delim) {
  +            if (scope_delim == end_bracket - 1) { /* '%' without scope identifier */
  +                *port = 0;
  +                return APR_EINVAL;
  +            }
  +            addrlen = scope_delim - str - 1;
  +            *scope_id = apr_palloc(p, end_bracket - scope_delim);
  +            memcpy(*scope_id, scope_delim + 1, end_bracket - scope_delim - 1);
  +            (*scope_id)[end_bracket - scope_delim - 1] = '\0';
  +        }
  +        else {
  +            addrlen = addrlen - 2; /* minus 2 for '[' and ']' */
  +        }
  +
  +        *addr = apr_palloc(p, addrlen + 1);
  +        memcpy(*addr,
  +               str + 1,
  +               addrlen);
  +        (*addr)[addrlen] = '\0';
  +        if (apr_inet_pton(AF_INET6, *addr, &ipaddr) != 1) {
  +            *addr = NULL;
  +            *scope_id = NULL;
  +            *port = 0;
  +            return APR_EINVAL;
  +        }
  +    }
  +    else 
  +#endif
  +    {
  +        /* XXX If '%' is not a valid char in a DNS name, we *could* check 
  +         *     for bogus scope ids first.
  +         */
  +        *addr = apr_palloc(p, addrlen + 1);
  +        memcpy(*addr, str, addrlen);
  +        (*addr)[addrlen] = '\0';
       }
       return APR_SUCCESS;
   }
  
  
  

Mime
View raw message