android: Add measurement collector for Installed Packages
authorTobias Brunner <tobias@strongswan.org>
Fri, 26 Apr 2013 13:36:03 +0000 (15:36 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 8 Jul 2013 16:49:27 +0000 (18:49 +0200)
src/frontends/android/src/org/strongswan/android/logic/imc/AndroidImc.java
src/frontends/android/src/org/strongswan/android/logic/imc/attributes/InstalledPackagesAttribute.java [new file with mode: 0644]
src/frontends/android/src/org/strongswan/android/logic/imc/collectors/InstalledPackagesCollector.java [new file with mode: 0644]

index 828b99d..27f21cd 100644 (file)
@@ -18,6 +18,7 @@ package org.strongswan.android.logic.imc;
 import org.strongswan.android.logic.imc.attributes.Attribute;
 import org.strongswan.android.logic.imc.attributes.AttributeType;
 import org.strongswan.android.logic.imc.collectors.Collector;
+import org.strongswan.android.logic.imc.collectors.InstalledPackagesCollector;
 import org.strongswan.android.logic.imc.collectors.ProductInformationCollector;
 import org.strongswan.android.logic.imc.collectors.StringVersionCollector;
 
@@ -53,6 +54,9 @@ public class AndroidImc
                        case IETF_STRING_VERSION:
                                collector = new StringVersionCollector();
                                break;
+                       case IETF_INSTALLED_PACKAGES:
+                               collector = new InstalledPackagesCollector(mContext);
+                               break;
                        default:
                                break;
                }
diff --git a/src/frontends/android/src/org/strongswan/android/logic/imc/attributes/InstalledPackagesAttribute.java b/src/frontends/android/src/org/strongswan/android/logic/imc/attributes/InstalledPackagesAttribute.java
new file mode 100644 (file)
index 0000000..dd1ad72
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2013 Tobias Brunner
+ * Copyright (C) 2012 Christoph Buehler
+ * Copyright (C) 2012 Patrick Loetscher
+ * Hochschule fuer Technik Rapperswil
+ *
+ * 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.
+ */
+
+package org.strongswan.android.logic.imc.attributes;
+
+import java.util.LinkedList;
+
+import org.strongswan.android.utils.BufferedByteWriter;
+
+import android.util.Pair;
+
+/**
+ * PA-TNC Installed Packages attribute (see section 4.2.7 of RFC 5792)
+ *
+ *                       1                   2                   3
+ *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ *  |          Reserved             |         Package Count         |
+ *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ *  | Pkg Name Len  |        Package Name (Variable Length)         |
+ *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ *  |  Version Len  |    Package Version Number (Variable Length)   |
+ *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ */
+public class InstalledPackagesAttribute implements Attribute
+{
+       private final short RESERVED = 0;
+       private final LinkedList<Pair<String, String>> mPackages = new LinkedList<Pair<String, String>>();
+
+       /**
+        * Add an installed package to this attribute.
+        * @param name name of the package
+        * @param version version number of the package
+        */
+       public void addPackage(String name, String version)
+       {
+               mPackages.add(new Pair<String, String>(name, version));
+       }
+
+       @Override
+       public byte[] getEncoding()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put16(RESERVED);
+               writer.put16((short)mPackages.size());
+               for (Pair<String, String> pair : mPackages)
+               {
+                       writer.putLen8(pair.first.getBytes());
+                       writer.putLen8(pair.second.getBytes());
+               }
+               return writer.toByteArray();
+       }
+}
diff --git a/src/frontends/android/src/org/strongswan/android/logic/imc/collectors/InstalledPackagesCollector.java b/src/frontends/android/src/org/strongswan/android/logic/imc/collectors/InstalledPackagesCollector.java
new file mode 100644 (file)
index 0000000..caa5170
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2013 Tobias Brunner
+ * Copyright (C) 2012 Christoph Buehler
+ * Copyright (C) 2012 Patrick Loetscher
+ * Hochschule fuer Technik Rapperswil
+ *
+ * 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.
+ */
+
+package org.strongswan.android.logic.imc.collectors;
+
+import java.util.List;
+
+import org.strongswan.android.logic.imc.attributes.Attribute;
+import org.strongswan.android.logic.imc.attributes.InstalledPackagesAttribute;
+
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+
+public class InstalledPackagesCollector implements Collector
+{
+       private final PackageManager mPackageManager;
+
+       public InstalledPackagesCollector(Context context)
+       {
+               mPackageManager = context.getPackageManager();
+       }
+
+       @Override
+       public Attribute getMeasurement()
+       {
+               InstalledPackagesAttribute attribute = new InstalledPackagesAttribute();
+               List<PackageInfo> packages = mPackageManager.getInstalledPackages(0);
+               for (PackageInfo info : packages)
+               {
+                       if ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 ||
+                               info.packageName == null || info.versionName == null)
+                       {       /* ignore packages installed in the system image */
+                               continue;
+                       }
+                       attribute.addPackage(info.packageName, info.versionName);
+               }
+               return attribute;
+       }
+}