implement AI_NUMERICSERV (as defined in RFC3493).

Obtained from:	KAME
MFC after:	1 week
This commit is contained in:
Hajimu UMEMOTO 2005-01-27 14:45:11 +00:00
parent 3e7cb94c9c
commit c224435ed9
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=140908
2 changed files with 18 additions and 13 deletions

View file

@ -175,10 +175,12 @@ struct addrinfo {
*/ */
#define AI_PASSIVE 0x00000001 /* get address to use bind() */ #define AI_PASSIVE 0x00000001 /* get address to use bind() */
#define AI_CANONNAME 0x00000002 /* fill ai_canonname */ #define AI_CANONNAME 0x00000002 /* fill ai_canonname */
#define AI_NUMERICHOST 0x00000004 /* prevent name resolution */ #define AI_NUMERICHOST 0x00000004 /* prevent host name resolution */
/* valid flags for addrinfo */ #define AI_NUMERICSERV 0x00000008 /* prevent service name resolution */
/* valid flags for addrinfo (not a standard def, apps should not use it) */
#define AI_MASK \ #define AI_MASK \
(AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_ADDRCONFIG) (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV | \
AI_ADDRCONFIG)
#define AI_ALL 0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */ #define AI_ALL 0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */
#define AI_V4MAPPED_CFG 0x00000200 /* accept IPv4-mapped if kernel supports */ #define AI_V4MAPPED_CFG 0x00000200 /* accept IPv4-mapped if kernel supports */

View file

@ -233,7 +233,7 @@ typedef union {
u_char buf[MAXPACKET]; u_char buf[MAXPACKET];
} querybuf; } querybuf;
static int str_isnumber(const char *); static int str2number(const char *);
static int explore_null(const struct addrinfo *, static int explore_null(const struct addrinfo *,
const char *, struct addrinfo **); const char *, struct addrinfo **);
static int explore_numeric(const struct addrinfo *, const char *, static int explore_numeric(const struct addrinfo *, const char *,
@ -390,20 +390,21 @@ freeaddrinfo(ai)
} }
static int static int
str_isnumber(p) str2number(p)
const char *p; const char *p;
{ {
char *ep; char *ep;
unsigned long v;
if (*p == '\0') if (*p == '\0')
return NO; return -1;
ep = NULL; ep = NULL;
errno = 0; errno = 0;
(void)strtoul(p, &ep, 10); v = strtoul(p, &ep, 10);
if (errno == 0 && ep && *ep == '\0') if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
return YES; return v;
else else
return NO; return -1;
} }
int int
@ -1415,7 +1416,7 @@ get_portmatch(ai, servname)
const char *servname; const char *servname;
{ {
/* get_port does not touch first argument. when matchonly == 1. */ /* get_port does not touch first argument when matchonly == 1. */
/* LINTED const cast */ /* LINTED const cast */
return get_port((struct addrinfo *)ai, servname, 1); return get_port((struct addrinfo *)ai, servname, 1);
} }
@ -1457,14 +1458,16 @@ get_port(ai, servname, matchonly)
return EAI_SOCKTYPE; return EAI_SOCKTYPE;
} }
if (str_isnumber(servname)) { port = str2number(servname);
if (port >= 0) {
if (!allownumeric) if (!allownumeric)
return EAI_SERVICE; return EAI_SERVICE;
port = atoi(servname);
if (port < 0 || port > 65535) if (port < 0 || port > 65535)
return EAI_SERVICE; return EAI_SERVICE;
port = htons(port); port = htons(port);
} else { } else {
if (ai->ai_flags & AI_NUMERICSERV)
return EAI_NONAME;
switch (ai->ai_socktype) { switch (ai->ai_socktype) {
case SOCK_DGRAM: case SOCK_DGRAM:
proto = "udp"; proto = "udp";