# HG changeset patch # User Senko Rasic # Date 1193783250 -3600 # Node ID 7e2050a6df75bfa9e5c8bf0f9832168f1d69c4d9 # Parent f169e928174577404b08d6db948f0bfb28ac6c12 Added lm_connection_get_client_host (forward-port from LM1.2). diff -r f169e9281745 -r 7e2050a6df75 loudmouth/lm-connection.c --- a/loudmouth/lm-connection.c Tue Oct 30 23:22:09 2007 +0100 +++ b/loudmouth/lm-connection.c Tue Oct 30 23:27:30 2007 +0100 @@ -2066,6 +2066,20 @@ } /** + * lm_connection_get_client_host: + * @connection: An #LmConnection + * + * Returns the local host name of the connection. + * + * Return value: A newly allocated string representing the local host name. + **/ +gchar * +lm_connection_get_local_host (LmConnection *connection) +{ + return lm_socket_get_local_host (connection->socket); +} + +/** * lm_connection_ref: * @connection: Connection to add a reference to. * diff -r f169e9281745 -r 7e2050a6df75 loudmouth/lm-connection.h --- a/loudmouth/lm-connection.h Tue Oct 30 23:22:09 2007 +0100 +++ b/loudmouth/lm-connection.h Tue Oct 30 23:27:30 2007 +0100 @@ -160,9 +160,11 @@ const gchar *str, GError **error); LmConnectionState lm_connection_get_state (LmConnection *connection); +gchar * lm_connection_get_local_host (LmConnection *connection); LmConnection* lm_connection_ref (LmConnection *connection); void lm_connection_unref (LmConnection *connection); + G_END_DECLS #endif /* __LM_CONNECTION_H__ */ diff -r f169e9281745 -r 7e2050a6df75 loudmouth/lm-internals.h --- a/loudmouth/lm-internals.h Tue Oct 30 23:22:09 2007 +0100 +++ b/loudmouth/lm-internals.h Tue Oct 30 23:27:30 2007 +0100 @@ -108,6 +108,7 @@ socklen_t *len); const gchar * _lm_sock_get_error_str (int err); const gchar * _lm_sock_addrinfo_get_error_str (int err); +gchar * _lm_sock_get_local_host (LmSocketT sock); #ifdef USE_TCP_KEEPALIVES gboolean _lm_sock_set_keepalive (LmSocketT sock, int delay); diff -r f169e9281745 -r 7e2050a6df75 loudmouth/lm-sock.c --- a/loudmouth/lm-sock.c Tue Oct 30 23:22:09 2007 +0100 +++ b/loudmouth/lm-sock.c Tue Oct 30 23:27:30 2007 +0100 @@ -32,6 +32,7 @@ #include #include #include +#include #define LM_SHUTDOWN SHUT_RDWR #else /* G_OS_WIN32 */ @@ -46,6 +47,8 @@ #include "lm-sock.h" #include "lm-debug.h" +#define IPV6_MAX_ADDRESS_LEN 46 /* 45 + '\0' */ + static gboolean initialised = FALSE; gboolean @@ -323,3 +326,37 @@ } #endif /* USE_TCP_KEEPALIVES */ +gchar * +_lm_sock_get_local_host (LmSocketT sock) +{ + struct sockaddr addr_info; + void *sock_addr; + socklen_t namelen; + char addrbuf[IPV6_MAX_ADDRESS_LEN]; + const char *host; + + namelen = sizeof (struct sockaddr); + if (getsockname (sock, &addr_info, &namelen)) { + return NULL; + } + + switch (addr_info.sa_family) { + case AF_INET: + + sock_addr = & (((struct sockaddr_in *) &addr_info)->sin_addr); + break; + case AF_INET6: + sock_addr = & (((struct sockaddr_in6 *) &addr_info)->sin6_addr); + break; + default: + return NULL; + } + /* inet_ntoa has been obsoleted in favour of inet_ntop */ + host = inet_ntop (addr_info.sa_family, + sock_addr, + addrbuf, + IPV6_MAX_ADDRESS_LEN); + + return g_strdup (host); +} + diff -r f169e9281745 -r 7e2050a6df75 loudmouth/lm-socket.c --- a/loudmouth/lm-socket.c Tue Oct 30 23:22:09 2007 +0100 +++ b/loudmouth/lm-socket.c Tue Oct 30 23:27:30 2007 +0100 @@ -1096,6 +1096,12 @@ } } +gchar * +lm_socket_get_local_host (LmSocket *socket) +{ + return _lm_sock_get_local_host (socket->fd); +} + LmSocket * lm_socket_ref (LmSocket *socket) { diff -r f169e9281745 -r 7e2050a6df75 loudmouth/lm-socket.h --- a/loudmouth/lm-socket.h Tue Oct 30 23:22:09 2007 +0100 +++ b/loudmouth/lm-socket.h Tue Oct 30 23:27:30 2007 +0100 @@ -71,6 +71,7 @@ #endif gboolean lm_socket_starttls (LmSocket *socket); gboolean lm_socket_set_keepalive (LmSocket *socket, int delay); +gchar * lm_socket_get_local_host (LmSocket *socket); #endif /* __LM_SOCKET_H__ */