- Add support for AF_INET6 sockets for %S format character.
- Use inet_ntop(3) instead of reimplementing it. - Use %hhu for unsigned char instead of casting it to unsigned int and using %u. MFC after: 1 week
This commit is contained in:
parent
dbe66680b0
commit
0855e42386
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=222087
@ -31,8 +31,10 @@
|
|||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
__FBSDID("$FreeBSD$");
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@ -103,22 +105,39 @@ pjdlog_printf_render_sockaddr(struct __printf_io *io,
|
|||||||
switch (ss->ss_family) {
|
switch (ss->ss_family) {
|
||||||
case AF_INET:
|
case AF_INET:
|
||||||
{
|
{
|
||||||
|
char addr[INET_ADDRSTRLEN];
|
||||||
const struct sockaddr_in *sin;
|
const struct sockaddr_in *sin;
|
||||||
in_addr_t ip;
|
|
||||||
unsigned int port;
|
unsigned int port;
|
||||||
|
|
||||||
sin = (const struct sockaddr_in *)ss;
|
sin = (const struct sockaddr_in *)ss;
|
||||||
ip = ntohl(sin->sin_addr.s_addr);
|
|
||||||
port = ntohs(sin->sin_port);
|
port = ntohs(sin->sin_port);
|
||||||
|
if (inet_ntop(ss->ss_family, &sin->sin_addr, addr,
|
||||||
|
sizeof(addr)) == NULL) {
|
||||||
|
PJDLOG_ABORT("inet_ntop(AF_INET) failed: %s.",
|
||||||
|
strerror(errno));
|
||||||
|
}
|
||||||
|
snprintf(buf, sizeof(buf), "%s:%u", addr, port);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case AF_INET6:
|
||||||
|
{
|
||||||
|
char addr[INET6_ADDRSTRLEN];
|
||||||
|
const struct sockaddr_in6 *sin;
|
||||||
|
unsigned int port;
|
||||||
|
|
||||||
snprintf(buf, sizeof(buf), "%u.%u.%u.%u:%u",
|
sin = (const struct sockaddr_in6 *)ss;
|
||||||
((ip >> 24) & 0xff), ((ip >> 16) & 0xff),
|
port = ntohs(sin->sin6_port);
|
||||||
((ip >> 8) & 0xff), (ip & 0xff), port);
|
if (inet_ntop(ss->ss_family, &sin->sin6_addr, addr,
|
||||||
|
sizeof(addr)) == NULL) {
|
||||||
|
PJDLOG_ABORT("inet_ntop(AF_INET6) failed: %s.",
|
||||||
|
strerror(errno));
|
||||||
|
}
|
||||||
|
snprintf(buf, sizeof(buf), "[%s]:%u", addr, port);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
snprintf(buf, sizeof(buf), "[unsupported family %u]",
|
snprintf(buf, sizeof(buf), "[unsupported family %hhu]",
|
||||||
(unsigned int)ss->ss_family);
|
ss->ss_family);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ret = __printf_out(io, pi, buf, strlen(buf));
|
ret = __printf_out(io, pi, buf, strlen(buf));
|
||||||
|
Loading…
Reference in New Issue
Block a user