ngx_inet_addr()

This commit is contained in:
Igor Sysoev 2007-11-23 16:59:24 +00:00
parent 48bf997e10
commit 36b634cdbc
2 changed files with 46 additions and 0 deletions

View File

@ -8,6 +8,51 @@
#include <ngx_core.h>
/* AF_INET only */
in_addr_t
ngx_inet_addr(u_char *text, size_t len)
{
u_char *p, c;
in_addr_t addr;
ngx_uint_t octet, n;
addr = 0;
octet = 0;
n = 0;
for (p = text; p < text + len; p++) {
c = *p;
if (c >= '0' && c <= '9') {
octet = octet * 10 + (c - '0');
continue;
}
if (c == '.' && octet < 256) {
addr = (addr << 8) + octet;
octet = 0;
n++;
continue;
}
return INADDR_NONE;
}
if (n != 3) {
return INADDR_NONE;
}
if (octet < 256) {
addr = (addr << 8) + octet;
return htonl(addr);
}
return INADDR_NONE;
}
/*
* ngx_sock_ntop() and ngx_inet_ntop() may be implemented as
* "ngx_sprintf(text, "%ud.%ud.%ud.%ud", p[0], p[1], p[2], p[3])", however,

View File

@ -58,6 +58,7 @@ typedef struct {
} ngx_url_t;
in_addr_t ngx_inet_addr(u_char *text, size_t len);
size_t ngx_sock_ntop(int family, struct sockaddr *sa, u_char *text, size_t len);
size_t ngx_inet_ntop(int family, void *addr, u_char *text, size_t len);
ngx_int_t ngx_ptocidr(ngx_str_t *text, void *cidr);