xpc: add support for logging over XPC channels
authorMartin Willi <martin@revosec.ch>
Fri, 3 May 2013 14:55:22 +0000 (16:55 +0200)
committerMartin Willi <martin@revosec.ch>
Thu, 18 Jul 2013 10:17:55 +0000 (12:17 +0200)
src/frontends/osx/charon-xpc/xpc_channels.c
src/frontends/osx/charon-xpc/xpc_logger.c [new file with mode: 0644]
src/frontends/osx/charon-xpc/xpc_logger.h [new file with mode: 0644]
src/frontends/osx/strongSwan.xcodeproj/project.pbxproj

index fc03ab3..494ce0b 100644 (file)
@@ -14,6 +14,7 @@
  */
 
 #include "xpc_channels.h"
+#include "xpc_logger.h"
 
 #include <credentials/sets/callback_cred.h>
 #include <collections/hashtable.h>
@@ -58,6 +59,8 @@ typedef struct {
        uintptr_t sa;
        /* did we already ask for a password? */
        bool passworded;
+       /* channel specific logger */
+       xpc_logger_t *logger;
 } entry_t;
 
 /**
@@ -65,6 +68,8 @@ typedef struct {
  */
 static void destroy_entry(entry_t *entry)
 {
+       charon->bus->remove_logger(charon->bus, &entry->logger->logger);
+       entry->logger->destroy(entry->logger);
        xpc_connection_suspend(entry->conn);
        xpc_connection_cancel(entry->conn);
        xpc_release(entry->conn);
@@ -110,6 +115,7 @@ METHOD(xpc_channels_t, add, void,
        INIT(entry,
                .conn = conn,
                .sa = ike_sa,
+               .logger = xpc_logger_create(conn),
        );
 
        xpc_connection_set_event_handler(entry->conn, ^(xpc_object_t event) {
@@ -117,7 +123,7 @@ METHOD(xpc_channels_t, add, void,
                if (event == XPC_ERROR_CONNECTION_INVALID ||
                        event == XPC_ERROR_CONNECTION_INTERRUPTED)
                {
-                       remove_conn(this, entry->conn);
+                       remove_conn(this, conn);
                }
                else
                {
@@ -125,6 +131,9 @@ METHOD(xpc_channels_t, add, void,
                }
        });
 
+       entry->logger->set_ike_sa(entry->logger, entry->sa);
+       charon->bus->add_logger(charon->bus, &entry->logger->logger);
+
        this->lock->write_lock(this->lock);
        this->channels->put(this->channels, (void*)entry->sa, entry);
        this->lock->unlock(this->lock);
@@ -144,6 +153,7 @@ METHOD(listener_t, ike_rekey, bool,
        if (entry)
        {
                entry->sa = new->get_unique_id(new);
+               entry->logger->set_ike_sa(entry->logger, entry->sa);
                this->channels->put(this->channels, (void*)entry->sa, entry);
        }
        this->lock->unlock(this->lock);
diff --git a/src/frontends/osx/charon-xpc/xpc_logger.c b/src/frontends/osx/charon-xpc/xpc_logger.c
new file mode 100644 (file)
index 0000000..38c34e4
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2013 Martin Willi
+ * Copyright (C) 2013 revosec AG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+#include "xpc_logger.h"
+
+typedef struct private_xpc_logger_t private_xpc_logger_t;
+
+/**
+ * Private data of an xpc_logger_t object.
+ */
+struct private_xpc_logger_t {
+
+       /**
+        * Public xpc_logger_t interface.
+        */
+       xpc_logger_t public;
+
+       /**
+        * XPC channel to send logging messages to
+        */
+       xpc_connection_t conn;
+
+       /**
+        * IKE_SA we log for
+        */
+       u_int32_t ike_sa;
+};
+
+METHOD(logger_t, log_, void,
+       private_xpc_logger_t *this, debug_t group, level_t level, int thread,
+       ike_sa_t* ike_sa, const char *message)
+{
+       if (ike_sa && ike_sa->get_unique_id(ike_sa) == this->ike_sa)
+       {
+               xpc_object_t msg;
+
+               msg = xpc_dictionary_create(NULL, NULL, 0);
+               xpc_dictionary_set_string(msg, "type", "event");
+               xpc_dictionary_set_string(msg, "event", "log");
+               xpc_dictionary_set_string(msg, "message", message);
+               xpc_connection_send_message(this->conn, msg);
+               xpc_release(msg);
+       }
+}
+
+METHOD(logger_t, get_level, level_t,
+       private_xpc_logger_t *this, debug_t group)
+{
+       return LEVEL_CTRL;
+}
+
+METHOD(xpc_logger_t, set_ike_sa, void,
+       private_xpc_logger_t *this, u_int32_t ike_sa)
+{
+       this->ike_sa = ike_sa;
+}
+
+METHOD(xpc_logger_t, destroy, void,
+       private_xpc_logger_t *this)
+{
+       free(this);
+}
+
+/**
+ * See header
+ */
+xpc_logger_t *xpc_logger_create(xpc_connection_t conn)
+{
+       private_xpc_logger_t *this;
+
+       INIT(this,
+               .public = {
+                       .logger = {
+                               .log = _log_,
+                               .get_level = _get_level,
+                       },
+                       .set_ike_sa = _set_ike_sa,
+                       .destroy = _destroy,
+               },
+               .conn = conn,
+       );
+
+       return &this->public;
+}
diff --git a/src/frontends/osx/charon-xpc/xpc_logger.h b/src/frontends/osx/charon-xpc/xpc_logger.h
new file mode 100644 (file)
index 0000000..fd5ad37
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2013 Martin Willi
+ * Copyright (C) 2013 revosec AG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+/**
+ * @defgroup xpc_logger xpc_logger
+ * @{ @ingroup xpc
+ */
+
+#ifndef XPC_LOGGER_H_
+#define XPC_LOGGER_H_
+
+#include <xpc/xpc.h>
+
+#include <daemon.h>
+
+typedef struct xpc_logger_t xpc_logger_t;
+
+/**
+ * Connection specific logger over XPC.
+ */
+struct xpc_logger_t {
+
+       /**
+        * Implements logger_t.
+        */
+       logger_t logger;
+
+       /**
+        * Set the IKE_SA unique identifier this logger logs for.
+        *
+        * @param ike_sa                IKE_SA unique identifier
+        */
+       void (*set_ike_sa)(xpc_logger_t *this, u_int32_t ike_sa);
+
+       /**
+        * Destroy a xpc_logger_t.
+        */
+       void (*destroy)(xpc_logger_t *this);
+};
+
+/**
+ * Create a xpc_logger instance.
+ *
+ * @param conn         XPC connection to send logging events to
+ * @return                     XPC logger
+ */
+xpc_logger_t *xpc_logger_create(xpc_connection_t conn);
+
+#endif /** XPC_LOGGER_H_ @}*/
index 29140b7..00781e9 100644 (file)
@@ -8,6 +8,7 @@
 
 /* Begin PBXBuildFile section */
                5B74989217311B200041971E /* xpc_channels.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B74989117311B200041971E /* xpc_channels.c */; };
+               5B7498B8173275D10041971E /* xpc_logger.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B7498B7173275D10041971E /* xpc_logger.c */; };
                5BD1CCD71726DB4000587077 /* charon-xpc.c in Sources */ = {isa = PBXBuildFile; fileRef = 5BD1CCD61726DB4000587077 /* charon-xpc.c */; };
                5BF60F31173405A000E5D608 /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5BD1CCD31726DB4000587077 /* CoreFoundation.framework */; };
                5BF60F33173405AC00E5D608 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5BD1CCF21727DE3E00587077 /* Security.framework */; };
@@ -21,6 +22,8 @@
                5B74989117311B200041971E /* xpc_channels.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = xpc_channels.c; sourceTree = "<group>"; };
                5BD1CCD11726DB4000587077 /* org.strongswan.charon-xpc */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.objfile"; includeInIndex = 0; path = "org.strongswan.charon-xpc"; sourceTree = BUILT_PRODUCTS_DIR; };
                5BD1CCD31726DB4000587077 /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; };
+               5B7498B7173275D10041971E /* xpc_logger.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = xpc_logger.c; sourceTree = "<group>"; };
+               5B7498B9173275DD0041971E /* xpc_logger.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = xpc_logger.h; sourceTree = "<group>"; };
                5BD1CCD61726DB4000587077 /* charon-xpc.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = "charon-xpc.c"; sourceTree = "<group>"; };
                5BD1CCE01726DCD000587077 /* charon-xpc-Launchd.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "charon-xpc-Launchd.plist"; sourceTree = "<group>"; };
                5BD1CCE11726DD9900587077 /* charon-xpc-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "charon-xpc-Info.plist"; sourceTree = "<group>"; };
@@ -80,6 +83,8 @@
                                5B74984E172AA3670041971E /* xpc_dispatch.h */,
                                5B74989017311AFC0041971E /* xpc_channels.h */,
                                5B74989117311B200041971E /* xpc_channels.c */,
+                               5B7498B7173275D10041971E /* xpc_logger.c */,
+                               5B7498B9173275DD0041971E /* xpc_logger.h */,
                        );
                        path = "charon-xpc";
                        sourceTree = "<group>";
                                5BD1CCD71726DB4000587077 /* charon-xpc.c in Sources */,
                                5B74989217311B200041971E /* xpc_channels.c in Sources */,
                                5BF60F3E1734070A00E5D608 /* xpc_dispatch.c in Sources */,
+                               5B7498B8173275D10041971E /* xpc_logger.c in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };