2002-12-15 14:25:09 +08:00
|
|
|
|
|
|
|
#include <ngx_config.h>
|
2003-06-03 23:42:58 +08:00
|
|
|
#include <ngx_core.h>
|
2002-12-15 14:25:09 +08:00
|
|
|
|
|
|
|
|
2004-03-16 21:35:20 +08:00
|
|
|
ngx_inline static size_t ngx_sprint_uchar(u_char *text, u_char c, size_t len)
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
ngx_uint_t c1, c2;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
if (len == n) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
c1 = c / 100;
|
|
|
|
|
|
|
|
if (c1) {
|
|
|
|
*text++ = (u_char) (c1 + '0');
|
|
|
|
n++;
|
|
|
|
|
|
|
|
if (len == n) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c2 = (c % 100) / 10;
|
|
|
|
|
|
|
|
if (c1 || c2) {
|
|
|
|
*text++ = (u_char) (c2 + '0');
|
|
|
|
n++;
|
|
|
|
|
|
|
|
if (len == n) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c2 = c % 10;
|
|
|
|
|
|
|
|
*text++ = (u_char) (c2 + '0');
|
|
|
|
n++;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-30 15:28:09 +08:00
|
|
|
/* AF_INET only */
|
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
size_t ngx_sock_ntop(int family, struct sockaddr *addr, u_char *text,
|
|
|
|
size_t len)
|
2003-01-30 15:28:09 +08:00
|
|
|
{
|
2004-03-16 15:10:12 +08:00
|
|
|
u_char *p;
|
2004-03-16 21:35:20 +08:00
|
|
|
size_t n;
|
|
|
|
ngx_uint_t i;
|
2003-01-30 15:28:09 +08:00
|
|
|
struct sockaddr_in *addr_in;
|
|
|
|
|
2004-03-16 21:35:20 +08:00
|
|
|
if (len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-30 15:28:09 +08:00
|
|
|
if (family != AF_INET) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr_in = (struct sockaddr_in *) addr;
|
2004-03-16 15:10:12 +08:00
|
|
|
p = (u_char *) &addr_in->sin_addr;
|
|
|
|
|
2004-03-16 21:35:20 +08:00
|
|
|
if (len > INET_ADDRSTRLEN) {
|
|
|
|
len = INET_ADDRSTRLEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = ngx_sprint_uchar(text, p[0], len);
|
|
|
|
|
|
|
|
i = 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (len == n) {
|
|
|
|
text[n - 1] = '\0';
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
text[n++] = '.';
|
|
|
|
|
|
|
|
if (len == n) {
|
|
|
|
text[n - 1] = '\0';
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
n += ngx_sprint_uchar(&text[n], p[i++], len - n);
|
|
|
|
|
|
|
|
} while (i < 4);
|
|
|
|
|
|
|
|
if (len == n) {
|
|
|
|
text[n] = '\0';
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
text[n] = '\0';
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
|
|
|
#if 0
|
2004-03-16 15:10:12 +08:00
|
|
|
return ngx_snprintf((char *) text,
|
|
|
|
len > INET_ADDRSTRLEN ? INET_ADDRSTRLEN : len,
|
|
|
|
"%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
|
2004-03-16 21:35:20 +08:00
|
|
|
#endif
|
2003-01-30 15:28:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-16 21:35:20 +08:00
|
|
|
size_t ngx_inet_ntop(int family, void *addr, u_char *text, size_t len)
|
2002-12-15 14:25:09 +08:00
|
|
|
{
|
2004-03-16 21:35:20 +08:00
|
|
|
u_char *p;
|
|
|
|
size_t n;
|
|
|
|
ngx_uint_t i;
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-30 15:28:09 +08:00
|
|
|
if (family != AF_INET) {
|
2002-12-15 14:25:09 +08:00
|
|
|
return 0;
|
2003-01-30 15:28:09 +08:00
|
|
|
}
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2004-03-16 21:35:20 +08:00
|
|
|
p = (u_char *) addr;
|
|
|
|
|
|
|
|
if (len > INET_ADDRSTRLEN) {
|
|
|
|
len = INET_ADDRSTRLEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = ngx_sprint_uchar(text, p[0], len);
|
|
|
|
|
|
|
|
i = 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (len == n) {
|
|
|
|
text[n - 1] = '\0';
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
text[n++] = '.';
|
|
|
|
|
|
|
|
if (len == n) {
|
|
|
|
text[n - 1] = '\0';
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
n += ngx_sprint_uchar(&text[n], p[i++], len - n);
|
|
|
|
|
|
|
|
} while (i < 4);
|
|
|
|
|
|
|
|
if (len == n) {
|
|
|
|
text[n] = '\0';
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
text[n] = '\0';
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
|
|
|
#if 0
|
2004-03-16 15:10:12 +08:00
|
|
|
return ngx_snprintf((char *) text,
|
|
|
|
len > INET_ADDRSTRLEN ? INET_ADDRSTRLEN : len,
|
2004-03-16 21:35:20 +08:00
|
|
|
"%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
|
|
|
|
#endif
|
2002-12-15 14:25:09 +08:00
|
|
|
}
|