Created
August 5, 2018 15:52
-
-
Save nasacj/30c15dc7915f03814df100dc534a71b3 to your computer and use it in GitHub Desktop.
inet_pton4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* int | |
* inet_pton4(src, dst) | |
* like inet_aton() but without all the hexadecimal and shorthand. | |
* return: | |
* 1 if `src' is a valid dotted quad, else 0. | |
* notice: | |
* does not touch `dst' unless it's returning 1. | |
* author: | |
* Paul Vixie, 1996. | |
*/ | |
static int | |
inet_pton4(const char *src, unsigned char *dst) | |
{ | |
static const char digits[] = "0123456789"; | |
int saw_digit, octets, ch; | |
unsigned char tmp[INADDRSZ], *tp; | |
saw_digit = 0; | |
octets = 0; | |
*(tp = tmp) = 0; | |
while ((ch = *src++) != '\0') { | |
const char *pch; | |
if ((pch = strchr(digits, ch)) != NULL) { | |
unsigned int new = *tp * 10 + (unsigned int)(pch - digits); | |
if (new > 255) | |
return (0); | |
*tp = new; | |
if (! saw_digit) { | |
if (++octets > 4) | |
return (0); | |
saw_digit = 1; | |
} | |
} else if (ch == '.' && saw_digit) { | |
if (octets == 4) | |
return (0); | |
*++tp = 0; | |
saw_digit = 0; | |
} else | |
return (0); | |
} | |
if (octets < 4) | |
return (0); | |
memcpy(dst, tmp, INADDRSZ); | |
return (1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment