[Mit-cadr-cvs] r409 - in branches/ggilley/emulator/usim: . CADR CADR/CADR CADR/CADR.xcodeproj CADR/EtherHelper

ggilley at common-lisp.net ggilley at common-lisp.net
Sun Jan 20 03:49:31 UTC 2013


Author: ggilley
Date: Sat Jan 19 19:49:30 2013
New Revision: 409

Log:
rough start for ethernet support

Added:
   branches/ggilley/emulator/usim/CADR/EtherHelper/
   branches/ggilley/emulator/usim/CADR/EtherHelper/EtherHelper-Info.plist
   branches/ggilley/emulator/usim/CADR/EtherHelper/EtherHelper-Launchd.plist
   branches/ggilley/emulator/usim/CADR/EtherHelper/main.c
Modified:
   branches/ggilley/emulator/usim/CADR/CADR.xcodeproj/project.pbxproj
   branches/ggilley/emulator/usim/CADR/CADR/CADR-Info.plist
   branches/ggilley/emulator/usim/CADR/CADR/CADR.entitlements
   branches/ggilley/emulator/usim/CADR/cadrAppDelegate.m
   branches/ggilley/emulator/usim/ether.c

Modified: branches/ggilley/emulator/usim/CADR/CADR.xcodeproj/project.pbxproj
==============================================================================
--- branches/ggilley/emulator/usim/CADR/CADR.xcodeproj/project.pbxproj	Wed Dec 12 19:43:47 2012	(r408)
+++ branches/ggilley/emulator/usim/CADR/CADR.xcodeproj/project.pbxproj	Sat Jan 19 19:49:30 2013	(r409)
@@ -7,6 +7,10 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		DA3EAAF5167EBA600025219F /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = DA3EAAF4167EBA600025219F /* main.c */; };
+		DA3EAB01167EBC320025219F /* com.cadr.EtherHelper in CopyFiles */ = {isa = PBXBuildFile; fileRef = DA3EAAF1167EBA600025219F /* com.cadr.EtherHelper */; };
+		DA3EAB03167ECA3C0025219F /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA3EAB02167ECA3C0025219F /* Security.framework */; };
+		DA3EAB05167ECA4C0025219F /* ServiceManagement.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA3EAB04167ECA4C0025219F /* ServiceManagement.framework */; };
 		DD308CD11665E6A000361F6D /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD308CD01665E6A000361F6D /* Cocoa.framework */; };
 		DD308CDB1665E6A000361F6D /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = DD308CD91665E6A000361F6D /* InfoPlist.strings */; };
 		DD308CDD1665E6A000361F6D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = DD308CDC1665E6A000361F6D /* main.m */; };
@@ -69,6 +73,13 @@
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
+		DA3EAAFE167EBC1E0025219F /* PBXContainerItemProxy */ = {
+			isa = PBXContainerItemProxy;
+			containerPortal = DD308CC31665E6A000361F6D /* Project object */;
+			proxyType = 1;
+			remoteGlobalIDString = DA3EAAF0167EBA600025219F;
+			remoteInfo = com.cadr.EtherHelper;
+		};
 		DD308CF11665E6A000361F6D /* PBXContainerItemProxy */ = {
 			isa = PBXContainerItemProxy;
 			containerPortal = DD308CC31665E6A000361F6D /* Project object */;
@@ -78,7 +89,35 @@
 		};
 /* End PBXContainerItemProxy section */
 
+/* Begin PBXCopyFilesBuildPhase section */
+		DA3EAAEF167EBA600025219F /* CopyFiles */ = {
+			isa = PBXCopyFilesBuildPhase;
+			buildActionMask = 2147483647;
+			dstPath = /usr/share/man/man1/;
+			dstSubfolderSpec = 0;
+			files = (
+			);
+			runOnlyForDeploymentPostprocessing = 1;
+		};
+		DA3EAB00167EBC240025219F /* CopyFiles */ = {
+			isa = PBXCopyFilesBuildPhase;
+			buildActionMask = 2147483647;
+			dstPath = Contents/Library/LaunchServices;
+			dstSubfolderSpec = 1;
+			files = (
+				DA3EAB01167EBC320025219F /* com.cadr.EtherHelper in CopyFiles */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXCopyFilesBuildPhase section */
+
 /* Begin PBXFileReference section */
+		DA3EAAF1167EBA600025219F /* com.cadr.EtherHelper */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = com.cadr.EtherHelper; sourceTree = BUILT_PRODUCTS_DIR; };
+		DA3EAAF4167EBA600025219F /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
+		DA3EAAFC167EBB550025219F /* EtherHelper-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "EtherHelper-Info.plist"; sourceTree = "<group>"; };
+		DA3EAAFD167EBB550025219F /* EtherHelper-Launchd.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "EtherHelper-Launchd.plist"; sourceTree = "<group>"; };
+		DA3EAB02167ECA3C0025219F /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
+		DA3EAB04167ECA4C0025219F /* ServiceManagement.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ServiceManagement.framework; path = System/Library/Frameworks/ServiceManagement.framework; sourceTree = SDKROOT; };
 		DD308CCC1665E6A000361F6D /* CADR.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CADR.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		DD308CD01665E6A000361F6D /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
 		DD308CD31665E6A000361F6D /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
@@ -172,11 +211,20 @@
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
+		DA3EAAEE167EBA600025219F /* Frameworks */ = {
+			isa = PBXFrameworksBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
 		DD308CC91665E6A000361F6D /* Frameworks */ = {
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				DA3EAB05167ECA4C0025219F /* ServiceManagement.framework in Frameworks */,
 				DD308CD11665E6A000361F6D /* Cocoa.framework in Frameworks */,
+				DA3EAB03167ECA3C0025219F /* Security.framework in Frameworks */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -192,12 +240,23 @@
 /* End PBXFrameworksBuildPhase section */
 
 /* Begin PBXGroup section */
+		DA3EAAF3167EBA600025219F /* EtherHelper */ = {
+			isa = PBXGroup;
+			children = (
+				DA3EAAF4167EBA600025219F /* main.c */,
+				DA3EAAFC167EBB550025219F /* EtherHelper-Info.plist */,
+				DA3EAAFD167EBB550025219F /* EtherHelper-Launchd.plist */,
+			);
+			path = EtherHelper;
+			sourceTree = "<group>";
+		};
 		DD308CC11665E6A000361F6D = {
 			isa = PBXGroup;
 			children = (
 				DD3658611665EED400755138 /* proms */,
 				DD308CD61665E6A000361F6D /* CADR */,
 				DD308CF31665E6A000361F6D /* CADRTests */,
+				DA3EAAF3167EBA600025219F /* EtherHelper */,
 				DD308CCF1665E6A000361F6D /* Frameworks */,
 				DD308CCD1665E6A000361F6D /* Products */,
 			);
@@ -208,6 +267,7 @@
 			children = (
 				DD308CCC1665E6A000361F6D /* CADR.app */,
 				DD308CED1665E6A000361F6D /* CADRTests.octest */,
+				DA3EAAF1167EBA600025219F /* com.cadr.EtherHelper */,
 			);
 			name = Products;
 			sourceTree = "<group>";
@@ -217,6 +277,8 @@
 			children = (
 				DD308CD01665E6A000361F6D /* Cocoa.framework */,
 				DD308CEE1665E6A000361F6D /* SenTestingKit.framework */,
+				DA3EAB02167ECA3C0025219F /* Security.framework */,
+				DA3EAB04167ECA4C0025219F /* ServiceManagement.framework */,
 				DD308CD21665E6A000361F6D /* Other Frameworks */,
 			);
 			name = Frameworks;
@@ -355,6 +417,23 @@
 /* End PBXGroup section */
 
 /* Begin PBXNativeTarget section */
+		DA3EAAF0167EBA600025219F /* com.cadr.EtherHelper */ = {
+			isa = PBXNativeTarget;
+			buildConfigurationList = DA3EAAFA167EBA600025219F /* Build configuration list for PBXNativeTarget "com.cadr.EtherHelper" */;
+			buildPhases = (
+				DA3EAAED167EBA600025219F /* Sources */,
+				DA3EAAEE167EBA600025219F /* Frameworks */,
+				DA3EAAEF167EBA600025219F /* CopyFiles */,
+			);
+			buildRules = (
+			);
+			dependencies = (
+			);
+			name = com.cadr.EtherHelper;
+			productName = EtherHelper;
+			productReference = DA3EAAF1167EBA600025219F /* com.cadr.EtherHelper */;
+			productType = "com.apple.product-type.tool";
+		};
 		DD308CCB1665E6A000361F6D /* CADR */ = {
 			isa = PBXNativeTarget;
 			buildConfigurationList = DD308CFE1665E6A000361F6D /* Build configuration list for PBXNativeTarget "CADR" */;
@@ -362,10 +441,12 @@
 				DD308CC81665E6A000361F6D /* Sources */,
 				DD308CC91665E6A000361F6D /* Frameworks */,
 				DD308CCA1665E6A000361F6D /* Resources */,
+				DA3EAB00167EBC240025219F /* CopyFiles */,
 			);
 			buildRules = (
 			);
 			dependencies = (
+				DA3EAAFF167EBC1E0025219F /* PBXTargetDependency */,
 			);
 			name = CADR;
 			productName = CADR;
@@ -414,6 +495,7 @@
 			targets = (
 				DD308CCB1665E6A000361F6D /* CADR */,
 				DD308CEC1665E6A000361F6D /* CADRTests */,
+				DA3EAAF0167EBA600025219F /* com.cadr.EtherHelper */,
 			);
 		};
 /* End PBXProject section */
@@ -482,6 +564,14 @@
 /* End PBXShellScriptBuildPhase section */
 
 /* Begin PBXSourcesBuildPhase section */
+		DA3EAAED167EBA600025219F /* Sources */ = {
+			isa = PBXSourcesBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				DA3EAAF5167EBA600025219F /* main.c in Sources */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
 		DD308CC81665E6A000361F6D /* Sources */ = {
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
@@ -525,6 +615,11 @@
 /* End PBXSourcesBuildPhase section */
 
 /* Begin PBXTargetDependency section */
+		DA3EAAFF167EBC1E0025219F /* PBXTargetDependency */ = {
+			isa = PBXTargetDependency;
+			target = DA3EAAF0167EBA600025219F /* com.cadr.EtherHelper */;
+			targetProxy = DA3EAAFE167EBC1E0025219F /* PBXContainerItemProxy */;
+		};
 		DD308CF21665E6A000361F6D /* PBXTargetDependency */ = {
 			isa = PBXTargetDependency;
 			target = DD308CCB1665E6A000361F6D /* CADR */;
@@ -568,6 +663,50 @@
 /* End PBXVariantGroup section */
 
 /* Begin XCBuildConfiguration section */
+		DA3EAAF8167EBA600025219F /* Debug */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				CODE_SIGN_IDENTITY = "Greg Gilley";
+				GCC_PREPROCESSOR_DEFINITIONS = (
+					"DEBUG=1",
+					"$(inherited)",
+				);
+				INFOPLIST_FILE = "EtherHelper/EtherHelper-Info.plist";
+				OTHER_LDFLAGS = (
+					"-sectcreate",
+					__TEXT,
+					__info_plist,
+					"EtherHelper/EtherHelper-Info.plist",
+					"-sectcreate",
+					__TEXT,
+					__launchd_plist,
+					"EtherHelper/EtherHelper-Launchd.plist",
+				);
+				PRODUCT_NAME = com.cadr.EtherHelper;
+				PROVISIONING_PROFILE = "";
+			};
+			name = Debug;
+		};
+		DA3EAAF9167EBA600025219F /* Release */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				CODE_SIGN_IDENTITY = "Greg Gilley";
+				INFOPLIST_FILE = "EtherHelper/EtherHelper-Info.plist";
+				OTHER_LDFLAGS = (
+					"-sectcreate",
+					__TEXT,
+					__info_plist,
+					"EtherHelper/EtherHelper-Info.plist",
+					"-sectcreate",
+					__TEXT,
+					__launchd_plist,
+					"EtherHelper/EtherHelper-Launchd.plist",
+				);
+				PRODUCT_NAME = com.cadr.EtherHelper;
+				PROVISIONING_PROFILE = "";
+			};
+			name = Release;
+		};
 		DD308CFC1665E6A000361F6D /* Debug */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
@@ -585,6 +724,7 @@
 				GCC_PREPROCESSOR_DEFINITIONS = (
 					"DEBUG=1",
 					"COCOA=1",
+					"CADR2=1",
 				);
 				GCC_SYMBOLS_PRIVATE_EXTERN = NO;
 				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
@@ -610,7 +750,10 @@
 				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
 				GCC_C_LANGUAGE_STANDARD = gnu99;
 				GCC_ENABLE_OBJC_EXCEPTIONS = YES;
-				GCC_PREPROCESSOR_DEFINITIONS = "COCOA=1";
+				GCC_PREPROCESSOR_DEFINITIONS = (
+					"COCOA=1",
+					"CADR2=1",
+				);
 				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
 				GCC_WARN_ABOUT_RETURN_TYPE = YES;
 				GCC_WARN_UNINITIALIZED_AUTOS = YES;
@@ -632,6 +775,7 @@
 					"DEBUG=1",
 					"DISPLAY_OSX=1",
 					"MAP_SITE_TREE_DIRECTORY=1",
+					"CADR2=1",
 				);
 				INFOPLIST_FILE = "CADR/CADR-Info.plist";
 				PRODUCT_NAME = "$(TARGET_NAME)";
@@ -651,6 +795,7 @@
 				GCC_PREPROCESSOR_DEFINITIONS = (
 					"DISPLAY_OSX=1",
 					"MAP_SITE_TREE_DIRECTORY=1",
+					"CADR2=1",
 				);
 				INFOPLIST_FILE = "CADR/CADR-Info.plist";
 				PRODUCT_NAME = "$(TARGET_NAME)";
@@ -692,6 +837,15 @@
 /* End XCBuildConfiguration section */
 
 /* Begin XCConfigurationList section */
+		DA3EAAFA167EBA600025219F /* Build configuration list for PBXNativeTarget "com.cadr.EtherHelper" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				DA3EAAF8167EBA600025219F /* Debug */,
+				DA3EAAF9167EBA600025219F /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
+		};
 		DD308CC61665E6A000361F6D /* Build configuration list for PBXProject "CADR" */ = {
 			isa = XCConfigurationList;
 			buildConfigurations = (

Modified: branches/ggilley/emulator/usim/CADR/CADR/CADR-Info.plist
==============================================================================
--- branches/ggilley/emulator/usim/CADR/CADR/CADR-Info.plist	Wed Dec 12 19:43:47 2012	(r408)
+++ branches/ggilley/emulator/usim/CADR/CADR/CADR-Info.plist	Sat Jan 19 19:49:30 2013	(r409)
@@ -32,5 +32,10 @@
 	<string>MainMenu</string>
 	<key>NSPrincipalClass</key>
 	<string>NSApplication</string>
+	<key>SMPrivilegedExecutables</key>
+	<dict>
+		<key>com.cadr.EtherHelper</key>
+		<string>identifier com.cadr.EtherHelper and certificate leaf[subject.CN] = "Greg Gilley"</string>
+	</dict>
 </dict>
 </plist>

Modified: branches/ggilley/emulator/usim/CADR/CADR/CADR.entitlements
==============================================================================
--- branches/ggilley/emulator/usim/CADR/CADR/CADR.entitlements	Wed Dec 12 19:43:47 2012	(r408)
+++ branches/ggilley/emulator/usim/CADR/CADR/CADR.entitlements	Sat Jan 19 19:49:30 2013	(r409)
@@ -8,5 +8,11 @@
 	<true/>
 	<key>com.apple.security.files.user-selected.read-write</key>
 	<true/>
+    <key>com.apple.security.temporary-exception.sbpl</key>
+    <string>(begin
+        (allow mach-lookup (global-name "com.cadr.EtherHelper"))
+        (allow mach-lookup (global-name "com.apple.bsd.launchdadd"))
+        (allow authorization-right-obtain (right-name "com.apple.ServiceManagement.blesshelper")))
+    </string>
 </dict>
 </plist>

Added: branches/ggilley/emulator/usim/CADR/EtherHelper/EtherHelper-Info.plist
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ branches/ggilley/emulator/usim/CADR/EtherHelper/EtherHelper-Info.plist	Sat Jan 19 19:49:30 2013	(r409)
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+	<key>CFBundleIdentifier</key>
+	<string>com.cadr.EtherHelper</string>
+	<key>CFBundleInfoDictionaryVersion</key>
+	<string>6.0</string>
+	<key>CFBundleName</key>
+	<string>EtherHelper</string>
+	<key>CFBundleVersion</key>
+	<string>1.0</string>
+	<key>SMAuthorizedClients</key>
+	<array>
+		<string>identifier com.cadr.CADR and certificate leaf[subject.CN] = "Mac Developer: Greg Gilley (HW2UG2Q7MX)"</string>
+	</array>
+</dict>
+</plist>

Added: branches/ggilley/emulator/usim/CADR/EtherHelper/EtherHelper-Launchd.plist
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ branches/ggilley/emulator/usim/CADR/EtherHelper/EtherHelper-Launchd.plist	Sat Jan 19 19:49:30 2013	(r409)
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+	<key>Label</key>
+	<string>com.cadr.EtherHelper</string>
+	<key>MachServices</key>
+	<dict>
+		<key>com.cadr.EtherHelper</key>
+		<true/>
+	</dict>
+</dict>
+</plist>

Added: branches/ggilley/emulator/usim/CADR/EtherHelper/main.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ branches/ggilley/emulator/usim/CADR/EtherHelper/main.c	Sat Jan 19 19:49:30 2013	(r409)
@@ -0,0 +1,166 @@
+//
+//  main.c
+//  EtherHelper
+//
+//  Created by Greg Gilley on 12/16/12.
+//  Copyright (c) 2012 Greg Gilley. All rights reserved.
+//
+
+#include <stdio.h>
+#include <errno.h>
+#include <syslog.h>
+#include <xpc/xpc.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <net/bpf.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+
+int tap_fd = -1;
+const char* interface = "en0";
+struct ifreq bound_if;
+int buf_len = 1;
+
+static void __XPC_Peer_Event_Handler(xpc_connection_t connection, xpc_object_t event) {
+    syslog(LOG_NOTICE, "Received event in helper.");
+    
+	xpc_type_t type = xpc_get_type(event);
+    
+	if (type == XPC_TYPE_ERROR) {
+        syslog(LOG_NOTICE, "XPC_Peer_Event_Handler error\n");
+
+		if (event == XPC_ERROR_CONNECTION_INVALID) {
+			// The client process on the other end of the connection has either
+			// crashed or cancelled the connection. After receiving this error,
+			// the connection is in an invalid state, and you do not need to
+			// call xpc_connection_cancel(). Just tear down any associated state
+			// here.
+            
+		} else if (event == XPC_ERROR_TERMINATION_IMMINENT) {
+			// Handle per-connection termination cleanup.
+		}
+        
+	}
+    else if (type == XPC_TYPE_DICTIONARY)
+    {
+
+        const void *data;
+        size_t len;
+        const char *op;
+
+        xpc_connection_t remote = xpc_dictionary_get_remote_connection(event);
+        
+        op = xpc_dictionary_get_string(event, "op");
+        
+        if (op && strcmp(op, "write") == 0)
+        {
+            syslog(LOG_NOTICE, "write\n");
+            data = xpc_dictionary_get_data(event, "packet", &len);
+            if (data)
+            {
+                if (write(tap_fd, data, len) != len)
+                {
+                    syslog(LOG_NOTICE, "error on write\n");
+                }
+            }
+            else
+                syslog(LOG_NOTICE, "no packet\n");
+
+            xpc_object_t reply = xpc_dictionary_create_reply(event);
+            xpc_dictionary_set_string(reply, "reply", "write!");
+            xpc_connection_send_message(remote, reply);
+            xpc_release(reply);
+        }
+        else if (op && strcmp(op, "read") == 0)
+        {
+            size_t len;
+            uint32_t packet[375];
+
+            
+            len = read(tap_fd, packet, sizeof(packet));
+            
+            syslog(LOG_NOTICE, "read: %zd\n", len);
+
+            xpc_object_t reply = xpc_dictionary_create_reply(event);
+            xpc_dictionary_set_string(reply, "reply", "read!");
+            if (len > 0)
+                xpc_dictionary_set_data(event, "packet", packet, len);
+            xpc_connection_send_message(remote, reply);
+            xpc_release(reply);
+        }
+        else
+        {
+            syslog(LOG_NOTICE, "unknown op='%s'\n", op ? op : "(NULL)");
+        }
+    }
+    else
+    {
+        syslog(LOG_NOTICE, "unknown type = %p\n", type);
+    }
+}
+
+static void __XPC_Connection_Handler(xpc_connection_t connection)  {
+    syslog(LOG_NOTICE, "Configuring message event handler for helper.");
+    
+	xpc_connection_set_event_handler(connection, ^(xpc_object_t event) {
+		__XPC_Peer_Event_Handler(connection, event);
+	});
+	
+	xpc_connection_resume(connection);
+}
+
+int main(int argc, const char *argv[]) {
+    syslog(LOG_NOTICE, "Before creating mach service");
+    xpc_connection_t service = xpc_connection_create_mach_service("com.cadr.EtherHelper",
+                                                                  dispatch_get_main_queue(),
+                                                                  XPC_CONNECTION_MACH_SERVICE_LISTENER);
+    
+    if (!service) {
+        syslog(LOG_NOTICE, "Failed to create service.");
+        exit(EXIT_FAILURE);
+    }
+    
+    if (tap_fd == -1)
+    {
+        syslog(LOG_NOTICE, "before open bpf\n");
+
+        if ((tap_fd = open("/dev/bpf3", O_RDWR)) < 0) {
+            syslog(LOG_NOTICE, "Couldn't open /dev/bfp3: %s\n", strerror(errno));
+            exit(EXIT_FAILURE);
+        }
+        
+        strcpy(bound_if.ifr_name, interface);
+        if (ioctl(tap_fd, BIOCSETIF, &bound_if) > 0)
+        {
+            syslog(LOG_NOTICE, "Can't bind to /dev/bfp3\n");
+            exit(EXIT_FAILURE);
+        }
+        
+        // activate immediate mode (therefore, buf_len is initially set to "1")
+        if (ioctl(tap_fd, BIOCIMMEDIATE, &buf_len) == -1) {
+            syslog(LOG_NOTICE, "Can't activate immediate mode\n");
+            exit(EXIT_FAILURE);
+        }
+    }
+    
+    // request buffer length
+    if (ioctl(tap_fd, BIOCGBLEN, &buf_len) == -1) {
+        syslog(LOG_NOTICE, "Can't get buffer length\n");
+        //  exit(EXIT_FAILURE);
+    }
+    syslog(LOG_NOTICE, "buffer length=%d\n", buf_len);
+
+    syslog(LOG_NOTICE, "Configuring connection event handler for helper");
+    xpc_connection_set_event_handler(service, ^(xpc_object_t connection) {
+        __XPC_Connection_Handler(connection);
+    });
+    
+    xpc_connection_resume(service);
+    
+    dispatch_main();
+    
+    xpc_release(service);
+    
+    return EXIT_SUCCESS;
+}
+

Modified: branches/ggilley/emulator/usim/CADR/cadrAppDelegate.m
==============================================================================
--- branches/ggilley/emulator/usim/CADR/cadrAppDelegate.m	Wed Dec 12 19:43:47 2012	(r408)
+++ branches/ggilley/emulator/usim/CADR/cadrAppDelegate.m	Sat Jan 19 19:49:30 2013	(r409)
@@ -10,6 +10,8 @@
 #import "DebuggerController.h"
 #import <Cocoa/Cocoa.h>
 #import <mach/mach_time.h> // for mach_absolute_time
+#import <ServiceManagement/ServiceManagement.h>
+#import <Security/Authorization.h>
 
 #include <sys/time.h>
 
@@ -353,6 +355,36 @@
 	return 0;
 }
 
+- (BOOL)blessHelperWithLabel:(NSString *)label
+                       error:(NSError **)error {
+    
+	BOOL result = NO;
+    
+	AuthorizationItem authItem		= { kSMRightBlessPrivilegedHelper, 0, NULL, 0 };
+	AuthorizationRights authRights	= { 1, &authItem };
+	AuthorizationFlags flags		=	kAuthorizationFlagDefaults				|
+    kAuthorizationFlagInteractionAllowed	|
+    kAuthorizationFlagPreAuthorize			|
+    kAuthorizationFlagExtendRights;
+    
+	AuthorizationRef authRef = NULL;
+	
+	/* Obtain the right to install privileged helper tools (kSMRightBlessPrivilegedHelper). */
+	OSStatus status = AuthorizationCreate(&authRights, kAuthorizationEmptyEnvironment, flags, &authRef);
+	if (status != errAuthorizationSuccess) {
+        printf("Failed to create AuthorizationRef. Error code: %d\n", status);
+	} else {
+		/* This does all the work of verifying the helper tool against the application
+		 * and vice-versa. Once verification has passed, the embedded launchd.plist
+		 * is extracted and placed in /Library/LaunchDaemons and then loaded. The
+		 * executable is placed in /Library/PrivilegedHelperTools.
+		 */
+		result = SMJobBless(kSMDomainSystemLaunchd, (CFStringRef)label, authRef, (CFErrorRef *)error);
+	}
+	
+	return result;
+}
+
 - (NSURL*)applicationDataDirectory {
     NSFileManager* sharedFM = [NSFileManager defaultManager];
     NSArray* possibleURLs = [sharedFM URLsForDirectory:NSApplicationSupportDirectory
@@ -382,6 +414,14 @@
     int ether_init(void);
     void iob_warm_boot_key(void);
     
+    NSError *error = nil;
+    if (![self blessHelperWithLabel:@"com.cadr.EtherHelper" error:&error]) {
+        NSLog(@"%@", [NSString stringWithFormat:@"Failed to bless helper. Error: %@", error]);
+        return;
+    }
+    
+    printf("Helper available.\n");
+
     // Insert code here to initialize your application
     Black = 0x00000000;
     White = 0xFFFFFFFF;

Modified: branches/ggilley/emulator/usim/ether.c
==============================================================================
--- branches/ggilley/emulator/usim/ether.c	Wed Dec 12 19:43:47 2012	(r408)
+++ branches/ggilley/emulator/usim/ether.c	Sat Jan 19 19:49:30 2013	(r409)
@@ -29,6 +29,7 @@
 #include <sys/socket.h>
 #include <net/if.h>
 #include <net/bpf.h>
+#include <xpc/xpc.h>
 #endif
 
 #if defined(BSD) && !defined(OSX)
@@ -101,6 +102,11 @@
 uint32_t packet[375];
 int enabled;
 
+#if defined(OSX)
+xpc_connection_t connection;
+
+#endif
+
 extern void assert_xbus_interrupt(void);
 extern int read_phy_mem(int paddr, unsigned int *pv);
 extern int write_phy_mem(int paddr, unsigned int v);
@@ -161,6 +167,53 @@
 
 	close(s);
 #endif
+#if defined(OSX)
+        connection = xpc_connection_create_mach_service("com.cadr.EtherHelper", NULL, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);
+        if (!connection) {
+            printf("Failed to create XPC connection.\n");
+            return -1;
+        }
+        
+        xpc_connection_set_event_handler(connection, ^(xpc_object_t event) {
+            xpc_type_t type = xpc_get_type(event);
+            
+            if (type == XPC_TYPE_ERROR) {
+                
+                if (event == XPC_ERROR_CONNECTION_INTERRUPTED) {
+                    printf("XPC connection interupted.\n");
+                    
+                } else if (event == XPC_ERROR_CONNECTION_INVALID) {
+                    printf("XPC connection invalid, releasing.\n");
+                    xpc_release(connection);
+                    
+                } else {
+                    printf("Unexpected XPC connection error.\n");
+                }
+                
+            } else {
+                printf("Unexpected XPC connection event.\n");
+            }
+        });
+        
+        xpc_connection_resume(connection);
+        enabled = 1;
+
+#if 0
+        xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
+        const char* request = "Hi there, helper service.";
+        xpc_dictionary_set_string(message, "request", request);
+        
+        printf("Sending request: %s\n", request);
+        
+        xpc_connection_send_message_with_reply(connection, message, dispatch_get_main_queue(), ^(xpc_object_t event) {
+            const char* response = xpc_dictionary_get_string(event, "reply");
+            printf("Received response: %s.\n", response);
+
+        });
+#endif
+
+        //    xpc_data_create(dataPtr, length)
+#endif // OSX
 	return 0;
 }
 
@@ -174,67 +227,104 @@
 #if defined(BSD) && !defined(OSX)
 	ssize_t ret;
 #endif
-    
+
 	if (enabled) {
 
 	    if (moder & ETHER_MODE_TXEN) {
-		for (i = 0; i < tx_bd_num; i++) {
-		    status = descs.desc_structs[i].status;
-		    if (status & ETHER_DESC_TX_READY) {
-			len = (size_t) descs.desc_structs[i].len;
-			ptr = descs.desc_structs[i].ptr;
-			words = (len + 3) >> 2;
-			for (j = 0; j < words; j++) {
-			    read_phy_mem(ptr + j, &packet[j]);
-			}
-			if (status & ETHER_DESC_TX_PAD)
-			    len = MAX(len, 60);
-
-#if defined(BSD) && !defined(OSX)
-			ret = write(tap_fd, packet, len);
-			if (ret != len) {
-			    perror("write"); 
-			}
-#endif
-
-			status &= ~ETHER_DESC_TX_READY;
-			descs.desc_structs[i].status = status;
-
-			if (status & ETHER_DESC_TX_IRQ)
-			    int_source |= ETHER_INT_TXB;    
-		    }
+            for (i = 0; i < tx_bd_num; i++) {
+                status = descs.desc_structs[i].status;
+                if (status & ETHER_DESC_TX_READY) {
+                len = (size_t) descs.desc_structs[i].len;
+                ptr = descs.desc_structs[i].ptr;
+                words = (len + 3) >> 2;
+                for (j = 0; j < words; j++) {
+                    read_phy_mem(ptr + j, &packet[j]);
+                }
+                if (status & ETHER_DESC_TX_PAD)
+                    len = MAX(len, 60);
 
-		    if (status & ETHER_DESC_TX_WRAP) break;
-		}
+#if defined(BSD) && !defined(OSX)
+                ret = write(tap_fd, packet, len);
+                if (ret != len) {
+                    perror("write"); 
+                }
+#endif
+#if defined(OSX)
+                xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
+                const char* request = "Hi there, helper service.";
+                xpc_dictionary_set_string(message, "request", request);
+                xpc_dictionary_set_string(message, "op", "write");
+                xpc_dictionary_set_data(message, "packet", packet, len);
+                
+                printf("Sending request: %s\n", request);
+                
+                xpc_connection_send_message_with_reply(connection, message, dispatch_get_main_queue(), ^(xpc_object_t event) {
+                    const char* response = xpc_dictionary_get_string(event, "reply");
+                    printf("Received response: %s.\n", response);
+                    
+                });
+#endif
+
+                status &= ~ETHER_DESC_TX_READY;
+                descs.desc_structs[i].status = status;
+
+                if (status & ETHER_DESC_TX_IRQ)
+                    int_source |= ETHER_INT_TXB;    
+                }
+
+                if (status & ETHER_DESC_TX_WRAP) break;
+            }
 	    }
 
 	    if (moder & ETHER_MODE_RXEN) {
-		for (i = tx_bd_num; i < 0x80; i++) {
-		    status = descs.desc_structs[i].status;
-		    if (status & ETHER_DESC_RX_EMPTY) {
+            for (i = tx_bd_num; i < 0x80; i++) {
+                status = descs.desc_structs[i].status;
+                if (status & ETHER_DESC_RX_EMPTY) {
 #if defined(BSD) && !defined(OSX)
-			len = read(tap_fd, packet, sizeof(packet));
+                len = read(tap_fd, packet, sizeof(packet));
 #else
-			len = -1;
+                len = -1;
+#if defined(OSX)
+                xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
+                const char* request = "Hi there, helper service.";
+                xpc_dictionary_set_string(message, "request", request);
+                xpc_dictionary_set_string(message, "op", "read");
+                
+                printf("Sending request: %s\n", request);
+                
+                xpc_connection_send_message_with_reply(connection, message, dispatch_get_main_queue(), ^(xpc_object_t event) {
+                    const char* response = xpc_dictionary_get_string(event, "reply");
+                    size_t len;
+                    const void *packet;
+
+                    printf("Received response: %s.\n", response);
+                    packet = xpc_dictionary_get_data(event, "packet", &len);
+                    if (packet)
+                    {
+                        printf("got packet: len=%zd\n", len);
+                    }
+                    
+                });
 #endif
-			if (len == -1) break;
-
-			ptr = descs.desc_structs[i].ptr;
-			descs.desc_structs[i].len = (uint16_t) len;
-			words = (len + 3) >> 2;
-			for (j = 0; j < words; j++) {
-			    write_phy_mem(ptr + j, packet[j]);
-			}
-
-			status &= ~ETHER_DESC_RX_EMPTY;
-			descs.desc_structs[i].status = status;
+#endif
+                if (len == -1) break;
 
-			if (status & ETHER_DESC_RX_IRQ)
-			    int_source |= ETHER_INT_RXB;    
-		    }
+                ptr = descs.desc_structs[i].ptr;
+                descs.desc_structs[i].len = (uint16_t) len;
+                words = (len + 3) >> 2;
+                for (j = 0; j < words; j++) {
+                    write_phy_mem(ptr + j, packet[j]);
+                }
+
+                status &= ~ETHER_DESC_RX_EMPTY;
+                descs.desc_structs[i].status = status;
+
+                if (status & ETHER_DESC_RX_IRQ)
+                    int_source |= ETHER_INT_RXB;    
+                }
 
-		    if (status & ETHER_DESC_RX_WRAP) break;
-		}
+                if (status & ETHER_DESC_RX_WRAP) break;
+            }
 	    }
 #if 0
 	    if (int_source & int_mask)




More information about the mit-cadr-cvs mailing list