Added lm_connection_get_client_host (forward-port from LM1.2).
authorSenko Rasic <senko.rasic@collabora.co.uk>
Tue, 30 Oct 2007 23:27:30 +0100
changeset 308 7e2050a6df75
parent 307 f169e9281745
child 309 329900413057
Added lm_connection_get_client_host (forward-port from LM1.2).
loudmouth/lm-connection.c
loudmouth/lm-connection.h
loudmouth/lm-internals.h
loudmouth/lm-sock.c
loudmouth/lm-socket.c
loudmouth/lm-socket.h
--- 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.
  * 
--- 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__ */
--- 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);
--- 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 <netinet/in.h>
 #include <netinet/ip.h>
 #include <netinet/tcp.h>
+#include <arpa/inet.h>
 #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);
+}
+
--- 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)
 {
--- 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__ */