/* $KAME: inet_pton.c,v 1.5 2001/08/20 02:32:40 itojun Exp $ */12/* Copyright (c) 1996 by Internet Software Consortium.3*4* Permission to use, copy, modify, and distribute this software for any5* purpose with or without fee is hereby granted, provided that the above6* copyright notice and this permission notice appear in all copies.7*8* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS9* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES10* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE11* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL12* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR13* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS14* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS15* SOFTWARE.16*/1718#include <ldns/config.h>1920#include <string.h>21#include <stdio.h>22#include <errno.h>2324/*25* WARNING: Don't even consider trying to compile this on a system where26* sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.27*/2829static int inet_pton4 (const char *src, uint8_t *dst);30static int inet_pton6 (const char *src, uint8_t *dst);3132/*33*34* The definitions we might miss.35*36*/37#ifndef NS_INT16SZ38#define NS_INT16SZ 239#endif4041#ifndef NS_IN6ADDRSZ42#define NS_IN6ADDRSZ 1643#endif4445#ifndef NS_INADDRSZ46#define NS_INADDRSZ 447#endif4849/* int50* inet_pton(af, src, dst)51* convert from presentation format (which usually means ASCII printable)52* to network format (which is usually some kind of binary format).53* return:54* 1 if the address was valid for the specified address family55* 0 if the address wasn't valid (`dst' is untouched in this case)56* -1 if some other error occurred (`dst' is untouched in this case, too)57* author:58* Paul Vixie, 1996.59*/60int61inet_pton(af, src, dst)62int af;63const char *src;64void *dst;65{66switch (af) {67case AF_INET:68return (inet_pton4(src, dst));69case AF_INET6:70return (inet_pton6(src, dst));71default:72#ifdef EAFNOSUPPORT73errno = EAFNOSUPPORT;74#else75errno = ENOSYS;76#endif77return (-1);78}79/* NOTREACHED */80}8182/* int83* inet_pton4(src, dst)84* like inet_aton() but without all the hexadecimal and shorthand.85* return:86* 1 if `src' is a valid dotted quad, else 0.87* notice:88* does not touch `dst' unless it's returning 1.89* author:90* Paul Vixie, 1996.91*/92static int93inet_pton4(src, dst)94const char *src;95uint8_t *dst;96{97static const char digits[] = "0123456789";98int saw_digit, octets, ch;99uint8_t tmp[NS_INADDRSZ], *tp;100101saw_digit = 0;102octets = 0;103*(tp = tmp) = 0;104while ((ch = *src++) != '\0') {105const char *pch;106107if ((pch = strchr(digits, ch)) != NULL) {108uint32_t new = *tp * 10 + (pch - digits);109110if (new > 255)111return (0);112*tp = new;113if (! saw_digit) {114if (++octets > 4)115return (0);116saw_digit = 1;117}118} else if (ch == '.' && saw_digit) {119if (octets == 4)120return (0);121*++tp = 0;122saw_digit = 0;123} else124return (0);125}126if (octets < 4)127return (0);128129memcpy(dst, tmp, NS_INADDRSZ);130return (1);131}132133/* int134* inet_pton6(src, dst)135* convert presentation level address to network order binary form.136* return:137* 1 if `src' is a valid [RFC1884 2.2] address, else 0.138* notice:139* (1) does not touch `dst' unless it's returning 1.140* (2) :: in a full address is silently ignored.141* credit:142* inspired by Mark Andrews.143* author:144* Paul Vixie, 1996.145*/146static int147inet_pton6(src, dst)148const char *src;149uint8_t *dst;150{151static const char xdigits_l[] = "0123456789abcdef",152xdigits_u[] = "0123456789ABCDEF";153uint8_t tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;154const char *xdigits, *curtok;155int ch, saw_xdigit;156uint32_t val;157158memset((tp = tmp), '\0', NS_IN6ADDRSZ);159endp = tp + NS_IN6ADDRSZ;160colonp = NULL;161/* Leading :: requires some special handling. */162if (*src == ':')163if (*++src != ':')164return (0);165curtok = src;166saw_xdigit = 0;167val = 0;168while ((ch = *src++) != '\0') {169const char *pch;170171if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)172pch = strchr((xdigits = xdigits_u), ch);173if (pch != NULL) {174val <<= 4;175val |= (pch - xdigits);176if (val > 0xffff)177return (0);178saw_xdigit = 1;179continue;180}181if (ch == ':') {182curtok = src;183if (!saw_xdigit) {184if (colonp)185return (0);186colonp = tp;187continue;188}189if (tp + NS_INT16SZ > endp)190return (0);191*tp++ = (uint8_t) (val >> 8) & 0xff;192*tp++ = (uint8_t) val & 0xff;193saw_xdigit = 0;194val = 0;195continue;196}197if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&198inet_pton4(curtok, tp) > 0) {199tp += NS_INADDRSZ;200saw_xdigit = 0;201break; /* '\0' was seen by inet_pton4(). */202}203return (0);204}205if (saw_xdigit) {206if (tp + NS_INT16SZ > endp)207return (0);208*tp++ = (uint8_t) (val >> 8) & 0xff;209*tp++ = (uint8_t) val & 0xff;210}211if (colonp != NULL) {212/*213* Since some memmove()'s erroneously fail to handle214* overlapping regions, we'll do the shift by hand.215*/216const int n = tp - colonp;217int i;218219for (i = 1; i <= n; i++) {220endp[- i] = colonp[n - i];221colonp[n - i] = 0;222}223tp = endp;224}225if (tp != endp)226return (0);227memcpy(dst, tmp, NS_IN6ADDRSZ);228return (1);229}230231232