+
+
+char *pa_socket_server_get_address(struct pa_socket_server *s, char *c, size_t l) {
+ assert(s && c && l > 0);
+
+ switch (s->type) {
+ case SOCKET_SERVER_IPV6: {
+ struct sockaddr_in6 sa;
+ socklen_t l = sizeof(sa);
+
+ if (getsockname(s->fd, (struct sockaddr*) &sa, &l) < 0) {
+ pa_log(__FILE__": getsockname() failed: %s\n", strerror(errno));
+ return NULL;
+ }
+
+ if (memcmp(&in6addr_any, &sa.sin6_addr, sizeof(in6addr_any)) == 0) {
+ char fqdn[256];
+ if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
+ return NULL;
+
+ snprintf(c, l, "tcp6:%s:%u", fqdn, (unsigned) ntohs(sa.sin6_port));
+
+ } else if (memcmp(&in6addr_loopback, &sa.sin6_addr, sizeof(in6addr_loopback)) == 0) {
+ char hn[256];
+ if (!pa_get_host_name(hn, sizeof(hn)))
+ return NULL;
+
+ snprintf(c, l, "{%s}tcp6:localhost:%u", hn, (unsigned) ntohs(sa.sin6_port));
+ } else {
+ char ip[INET6_ADDRSTRLEN];
+
+ if (!inet_ntop(AF_INET6, &sa.sin6_addr, ip, sizeof(ip))) {
+ pa_log(__FILE__": inet_ntop() failed: %s\n", strerror(errno));
+ return NULL;
+ }
+
+ snprintf(c, l, "tcp6:[%s]:%u", ip, (unsigned) ntohs(sa.sin6_port));
+ }
+
+ return c;
+ }
+
+ case SOCKET_SERVER_IPV4: {
+ struct sockaddr_in sa;
+ socklen_t l = sizeof(sa);
+
+ if (getsockname(s->fd, &sa, &l) < 0) {
+ pa_log(__FILE__": getsockname() failed: %s\n", strerror(errno));
+ return NULL;
+ }
+
+ if (sa.sin_addr.s_addr == INADDR_ANY) {
+ char fqdn[256];
+ if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
+ return NULL;
+
+ snprintf(c, l, "tcp:%s:%u", fqdn, (unsigned) ntohs(sa.sin_port));
+ } else if (sa.sin_addr.s_addr == INADDR_LOOPBACK) {
+ char hn[256];
+ if (!pa_get_host_name(hn, sizeof(hn)))
+ return NULL;
+
+ snprintf(c, l, "{%s}tcp:localhost:%u", hn, (unsigned) ntohs(sa.sin_port));
+ } else {
+ char ip[INET_ADDRSTRLEN];
+
+ if (!inet_ntop(AF_INET, &sa.sin_addr, ip, sizeof(ip))) {
+ pa_log(__FILE__": inet_ntop() failed: %s\n", strerror(errno));
+ return NULL;
+ }
+
+ snprintf(c, l, "tcp:[%s]:%u", ip, (unsigned) ntohs(sa.sin_port));
+
+ }
+
+ return c;
+ }
+
+ case SOCKET_SERVER_UNIX: {
+ char hn[256];
+
+ if (!s->filename)
+ return NULL;
+
+ if (!pa_get_host_name(hn, sizeof(hn)))
+ return NULL;
+
+ snprintf(c, l, "{%s}unix:%s", hn, s->filename);
+ return c;
+ }
+
+ default:
+ return NULL;
+ }
+}