Print this page
inet_pton

Split Close
Expand all
Collapse all
          --- old/usr/src/uts/common/inet/ip/inet_ntop.c
          +++ new/usr/src/uts/common/inet/ip/inet_ntop.c
↓ open down ↓ 15 lines elided ↑ open up ↑
  16   16   * If applicable, add the following below this CDDL HEADER, with the
  17   17   * fields enclosed by brackets "[]" replaced with your own identifying
  18   18   * information: Portions Copyright [yyyy] [name of copyright owner]
  19   19   *
  20   20   * CDDL HEADER END
  21   21   */
  22   22  /*
  23   23   * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
  24   24   * Use is subject to license terms.
  25   25   */
  26      -
  27      -#pragma ident   "%Z%%M% %I%     %E% SMI"
       26 +/*
       27 + * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
       28 + */
  28   29  
  29   30  #include <sys/types.h>
  30   31  #include <sys/cmn_err.h>
  31   32  #include <sys/systm.h>
  32   33  #include <sys/socket.h>
  33   34  #include <sys/sunddi.h>
  34   35  #include <netinet/in.h>
  35   36  #include <inet/led.h>
  36   37  
  37   38  static void     convert2ascii(char *, const in6_addr_t *);
↓ open down ↓ 188 lines elided ↑ open up ↑
 226  227          } while (*sp++);
 227  228          return (NULL);
 228  229  }
 229  230  
 230  231  static int
 231  232  str2inet_addr(char *cp, ipaddr_t *addrp)
 232  233  {
 233  234          char *end;
 234  235          long byte;
 235  236          int i;
 236      -        ipaddr_t addr = 0;
      237 +        uint8_t *addr = (uint8_t *)addrp;
      238 +
      239 +        *addrp = 0;
 237  240  
 238  241          for (i = 0; i < 4; i++) {
 239  242                  if (ddi_strtol(cp, &end, 10, &byte) != 0 || byte < 0 ||
 240  243                      byte > 255) {
 241  244                          return (0);
 242  245                  }
 243      -                addr = (addr << 8) | (uint8_t)byte;
      246 +                addr[i] = (uint8_t)byte;
 244  247                  if (i < 3) {
 245  248                          if (*end != '.') {
 246  249                                  return (0);
 247  250                          } else {
 248  251                                  cp = end + 1;
 249  252                          }
 250  253                  } else {
 251  254                          cp = end;
 252  255                  }
 253  256          }
 254      -        *addrp = addr;
      257 +
 255  258          return (1);
 256  259  }
 257  260  
 258  261  /*
 259  262   * inet_pton: This function takes string format IPv4 or IPv6 address and
 260  263   * converts it to binary form. The format of this function corresponds to
 261  264   * inet_pton() in the socket library.
 262  265   * It returns 0 for invalid IPv4 and IPv6 address
 263  266   *            1 when successfully converts ascii to binary
 264  267   *            -1 when af is not AF_INET or AF_INET6
 265  268   */
 266  269  int
 267      -inet_pton(int af, char *inp, void *outp)
      270 +m_inet_pton(int af, char *inp, void *outp, int revert)
 268  271  {
 269  272          int i;
 270  273          long byte;
 271  274          char *end;
 272  275  
 273  276          switch (af) {
 274  277          case AF_INET:
 275      -                return (str2inet_addr(inp, (ipaddr_t *)outp));
      278 +                if (str2inet_addr(inp, (ipaddr_t *)outp)) {
      279 +                        if (! revert)
      280 +                                *(uint32_t *)outp = ntohl(*(uint32_t *)outp);
      281 +                        return (1);
      282 +                } else {
      283 +                        return (0);
      284 +                }
 276  285          case AF_INET6: {
 277  286                  union v6buf_u {
 278  287                          uint16_t v6words_u[8];
 279  288                          in6_addr_t v6addr_u;
 280  289                  } v6buf, *v6outp;
 281  290                  uint16_t        *dbl_col = NULL;
 282  291                  char lastbyte = NULL;
 283  292  
 284  293                  v6outp = (union v6buf_u *)outp;
 285  294  
↓ open down ↓ 23 lines elided ↑ open up ↑
 309  318                           * 0 and continue.
 310  319                           */
 311  320                          if ((error = ddi_strtol(inp, &end, 16, &byte)) != 0) {
 312  321                                  if (error == ERANGE)
 313  322                                          return (0);
 314  323                                  byte = 0;
 315  324                          }
 316  325                          if (byte < 0 || byte > 0x0ffff) {
 317  326                                  return (0);
 318  327                          }
 319      -                        v6buf.v6words_u[i] = (uint16_t)byte;
      328 +                        if (revert) {
      329 +                                v6buf.v6words_u[i] = htons((uint16_t)byte);
      330 +                        } else {
      331 +                                v6buf.v6words_u[i] = (uint16_t)byte;
      332 +                        }
 320  333                          if (*end == NULL || i == 7) {
 321  334                                  inp = end;
 322  335                                  break;
 323  336                          }
 324  337                          if (inp == end) {       /* not a number must be */
 325  338                                  if (*inp == ':' &&
 326  339                                      ((i == 0 && *(inp + 1) == ':') ||
 327  340                                      lastbyte == ':')) {
 328  341                                          if (dbl_col) {
 329  342                                                  return (0);
↓ open down ↓ 51 lines elided ↑ open up ↑
 381  394                                  word++;
 382  395                                  rem--;
 383  396                                  next++;
 384  397                          }
 385  398                  }
 386  399                  return (1);     /* Success */
 387  400          }
 388  401          }       /* switch */
 389  402          return (-1);    /* return -1 for default case */
 390  403  }
      404 +
      405 +
      406 +int
      407 +_inet_pton(int af, char *inp, void *outp)
      408 +{
      409 +        return (m_inet_pton(af, inp, outp, 1));
      410 +}
      411 +
      412 +/*
      413 + * We need this inet_pton to preserve compatibility with old closed binaries.
      414 + * Earlier, inet_pton returned address in hardware native order,
      415 + * not in network one. (See http://www.illumos.org/issue/3105).
      416 + * Having fixed that, we still need to support binaries, that use bad inet_pton
      417 + * and reverse returned address manually. All new inet_pton calls will be
      418 + * redirected to _inet_pton with #define in the header file.
      419 + */
      420 +int
      421 +inet_pton(int af, char *inp, void *outp)
      422 +{
      423 +        return (m_inet_pton(af, inp, outp, 0));
      424 +}
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX