return fd;
}
+/**
+ * Print numeric host and port number (beware - uses static char).
+ * \param sa The IPv4/IPv6 socket address to use.
+ * \param len The length of \p sa.
+ *
+ * \sa getnameinfo(3)
+ */
+char *host_and_port(struct sockaddr *sa, socklen_t len)
+{
+ static char output[NI_MAXHOST + NI_MAXSERV + 2];
+ char hbuf[NI_MAXHOST],
+ sbuf[NI_MAXSERV];
+ int ret;
+
+ ret = getnameinfo(sa, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
+ NI_NUMERICHOST | NI_NUMERICSERV);
+ if (ret) {
+ PARA_WARNING_LOG("hostname lookup error (%s).\n", gai_strerror(ret));
+ sprintf(output, "(unknown)");
+ } else {
+ sprintf(output, "%s#%s", hbuf, sbuf);
+ }
+ return output;
+}
+
+/**
+ * Look up the local or remote side of a connected socket structure.
+ * \param fd The socket descriptor of the connected socket.
+ * \param getname Either \fn getsockname() for local, or \fn getpeername() for remote side.
+ *
+ * \return A static character string identifying hostname and port of the chosen side
+ * \sa getsockname(2), getpeername(2)
+ */
+static char *__get_sock_name(int fd, int (*getname)(int, struct sockaddr*, socklen_t *))
+{
+ struct sockaddr_storage ss;
+ socklen_t sslen = sizeof(ss);
+
+ if (getname(fd, (struct sockaddr *)&ss, &sslen) < 0) {
+ static char *dont_know = "(don't know)";
+ PARA_ERROR_LOG("can not determine address from fd %d: %s\n", fd, strerror(errno));
+ return dont_know;
+ }
+
+ return host_and_port((struct sockaddr *)&ss, sslen);
+}
+
+char *local_name(int sockfd)
+{
+ return __get_sock_name(sockfd, getsockname);
+}
+
+char *remote_name(int sockfd)
+{
+ return __get_sock_name(sockfd, getpeername);
+}
+
/*
* Send out a buffer, resend on short writes.
*
#define BACKLOG 10
extern int para_listen(unsigned l3type, unsigned l4type, unsigned short port);
+/** Pretty-printing of IPv4/6 socket addresses */
+extern char *host_and_port(struct sockaddr *sa, socklen_t len);
+extern char *local_name(int sockfd);
+extern char *remote_name(int sockfd);
+
/** used to crypt the communication between para_server and para_client */
typedef void crypt_function(unsigned long len,
const unsigned char *indata, unsigned char *outdata, void *private_data);