const char *pga_details(const PgAddr *a, char *dst, int dstlen) { char buf[PGADDR_BUF]; pga_ntop(a, buf, sizeof(buf)); if (pga_family(a) == AF_INET6) snprintf(dst, dstlen, "[%s]:%d", buf, pga_port(a)); else if (pga_family(a) == AF_UNIX && a->scred.pid) snprintf(dst, dstlen, "%s(%u@%s):%d", buf, a->scred.pid, cached_hostname(), pga_port(a)); else snprintf(dst, dstlen, "%s:%d", buf, pga_port(a)); return dst; }
const char *pga_str(const PgAddr *a, char *dst, int dstlen) { char buf[PGADDR_BUF]; pga_ntop(a, buf, sizeof(buf)); if (pga_family(a) == AF_INET6) snprintf(dst, dstlen, "[%s]:%d", buf, pga_port(a)); else if (pga_family(a) == AF_UNIX && a->scred.pid) snprintf(dst, dstlen, "%s:%d$%u", buf, pga_port(a), a->scred.pid); else snprintf(dst, dstlen, "%s:%d", buf, pga_port(a)); return dst; }
/* convert pgaddr to string */ const char *pga_ntop(const PgAddr *a, char *dst, int dstlen) { const char *res = NULL; char buf[PGADDR_BUF]; memset(buf, 0, sizeof(buf)); switch (pga_family(a)) { case AF_UNIX: res = "unix"; break; case AF_INET: res = inet_ntop(AF_INET, &a->sin.sin_addr, buf, sizeof(buf)); break; case AF_INET6: res = inet_ntop(AF_INET6, &a->sin6.sin6_addr, buf, sizeof(buf)); break; default: res = "(bad-af)"; } if (res == NULL) res = "(err-ntop)"; strlcpy(dst, res, dstlen); return dst; }
int pga_cmp_addr(const PgAddr *a, const PgAddr *b) { if (pga_family(a) != pga_family(b)) return pga_family(a) - pga_family(b); switch (pga_family(a)) { case AF_INET: return memcmp(&a->sin.sin_addr, &b->sin.sin_addr, sizeof(a->sin.sin_addr)); break; case AF_INET6: return memcmp(&a->sin6.sin6_addr, &b->sin6.sin6_addr, sizeof(a->sin6.sin6_addr)); break; default: log_error("pga_cmp_addr: unsupported family"); return 0; } }
int log_socket_prefix(enum LogLevel lev, void *ctx, char *dst, unsigned int dstlen) { const struct PgSocket *sock = ctx; const char *user, *db, *host; char host6[PGADDR_BUF]; int port; char stype; /* no prefix */ if (!sock) return 0; /* format prefix */ stype = is_server_socket(sock) ? 'S' : 'C'; port = pga_port(&sock->remote_addr); db = sock->pool ? sock->pool->db->name : "(nodb)"; user = sock->auth_user ? sock->auth_user->name : "(nouser)"; if (pga_is_unix(&sock->remote_addr)) { unsigned long pid = sock->remote_addr.scred.pid; if (pid) { snprintf(host6, sizeof(host6), "unix(%lu)", pid); host = host6; } else { host = "unix"; } } else { host = pga_ntop(&sock->remote_addr, host6, sizeof(host6)); } if (pga_family(&sock->remote_addr) == AF_INET6) { return snprintf(dst, dstlen, "%c-%p: %s/%s@[%s]:%d ", stype, sock, db, user, host, port); } else { return snprintf(dst, dstlen, "%c-%p: %s/%s@%s:%d ", stype, sock, db, user, host, port); } }