LCOV - code coverage report
Current view: top level - src/core/support - host_port.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 34 34 100.0 %
Date: 2015-10-10 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #include <grpc/support/host_port.h>
      35             : 
      36             : #include <string.h>
      37             : 
      38             : #include "src/core/support/string.h"
      39             : #include <grpc/support/alloc.h>
      40             : #include <grpc/support/log.h>
      41             : #include <grpc/support/string_util.h>
      42             : 
      43        7133 : int gpr_join_host_port(char **out, const char *host, int port) {
      44        7133 :   if (host[0] != '[' && strchr(host, ':') != NULL) {
      45             :     /* IPv6 literals must be enclosed in brackets. */
      46         119 :     return gpr_asprintf(out, "[%s]:%d", host, port);
      47             :   } else {
      48             :     /* Ordinary non-bracketed host:port. */
      49        7014 :     return gpr_asprintf(out, "%s:%d", host, port);
      50             :   }
      51             : }
      52             : 
      53        6366 : int gpr_split_host_port(const char *name, char **host, char **port) {
      54             :   const char *host_start;
      55             :   size_t host_len;
      56             :   const char *port_start;
      57             : 
      58        6366 :   *host = NULL;
      59        6366 :   *port = NULL;
      60             : 
      61        6366 :   if (name[0] == '[') {
      62             :     /* Parse a bracketed host, typically an IPv6 literal. */
      63          76 :     const char *rbracket = strchr(name, ']');
      64          76 :     if (rbracket == NULL) {
      65             :       /* Unmatched [ */
      66           2 :       return 0;
      67             :     }
      68          74 :     if (rbracket[1] == '\0') {
      69             :       /* ]<end> */
      70           3 :       port_start = NULL;
      71          71 :     } else if (rbracket[1] == ':') {
      72             :       /* ]:<port?> */
      73          70 :       port_start = rbracket + 2;
      74             :     } else {
      75             :       /* ]<invalid> */
      76           1 :       return 0;
      77             :     }
      78          73 :     host_start = name + 1;
      79          73 :     host_len = (size_t)(rbracket - host_start);
      80          73 :     if (memchr(host_start, ':', host_len) == NULL) {
      81             :       /* Require all bracketed hosts to contain a colon, because a hostname or
      82             :          IPv4 address should never use brackets. */
      83           3 :       return 0;
      84             :     }
      85             :   } else {
      86        6290 :     const char *colon = strchr(name, ':');
      87        6290 :     if (colon != NULL && strchr(colon + 1, ':') == NULL) {
      88             :       /* Exactly 1 colon.  Split into host:port. */
      89        6284 :       host_start = name;
      90        6284 :       host_len = (size_t)(colon - name);
      91        6284 :       port_start = colon + 1;
      92             :     } else {
      93             :       /* 0 or 2+ colons.  Bare hostname or IPv6 litearal. */
      94           6 :       host_start = name;
      95           6 :       host_len = strlen(name);
      96           6 :       port_start = NULL;
      97             :     }
      98             :   }
      99             : 
     100             :   /* Allocate return values. */
     101        6360 :   *host = gpr_malloc(host_len + 1);
     102        6360 :   memcpy(*host, host_start, host_len);
     103        6360 :   (*host)[host_len] = '\0';
     104             : 
     105        6360 :   if (port_start != NULL) {
     106        6353 :     *port = gpr_strdup(port_start);
     107             :   }
     108             : 
     109        6360 :   return 1;
     110             : }

Generated by: LCOV version 1.10