I'm trying to pass a string (though my real goal is to pass an array) from one macOS app to the other. The receiver app is sandboxed while the sender app is not sandboxed.
I've added an App Group (DEV_TEAM_ID.com.if.APP_NAME.UNIQUE_NAME) to both apps.
In the receiver app, I have implemented creating the port and runloop:
CFMessagePortRef localPort =
CFMessagePortCreateLocal(nil,
CFSTR("DEV_TEAM_ID.com.if.APP_NAME.UNIQUE_NAME"),
PortCallBack,
nil,
nil);
CFRunLoopSourceRef runLoopSource =
CFMessagePortCreateRunLoopSource(nil, localPort, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(),
runLoopSource,
kCFRunLoopCommonModes);
and the callback, of course:
CFDataRef PortCallBack(CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info) {
char *message = "Thanks for saying hello!";
CFDataRef returnData = CFDataCreate(NULL, (const UInt8 *)message, strlen(message)+1);
printf("here is our received data: %s\n", CFDataGetBytePtr(data));
return returnData;
}
In the sender app, I have implemented:
CFMessagePortRef remotePort = CFMessagePortCreateRemote(nil, CFSTR("DEV_TEAM_ID.com.if.APP_NAME.UNIQUE_NAME"));
SInt32 messageIdentifier = 1;
CFDataRef messageData = (__bridge CFDataRef)[#"hello, friend." dataUsingEncoding:NSUTF8StringEncoding];
SInt32 status = CFMessagePortSendRequest(remotePort, messageIdentifier, messageData, 1000, 0, NULL, NULL);
if (status == kCFMessagePortSuccess)
{
NSLog(#"success");
}
The receiver app launches fine, but the sender app crashes on this line:
SInt32 status = CFMessagePortSendRequest(remotePort, messageIdentifier, messageData, 1000, 0, NULL, NULL);
EDIT: It seems that remotePort is null, and is causing the crash
with the following error:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)
CoreFoundation`CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER:
0x7fff20707fe0 <+0>: testq %rdi, %rdi
0x7fff20707fe3 <+3>: je 0x7fff20707fed ; <+13>
0x7fff20707fe5 <+5>: pushq %rbp
0x7fff20707fe6 <+6>: movq %rsp, %rbp
0x7fff20707fe9 <+9>: callq *0x10(%rdi)
-> 0x7fff20707fec <+12>: popq %rbp
0x7fff20707fed <+13>: retq
0x7fff20707fee <+14>: nop
0x7fff20707fef <+15>: nop
0x7fff20707ff0 <+16>: nop
0x7fff20707ff1 <+17>: nop
0x7fff20707ff2 <+18>: nop
0x7fff20707ff3 <+19>: nop
0x7fff20707ff4 <+20>: nop
0x7fff20707ff5 <+21>: nop
0x7fff20707ff6 <+22>: nop
I'm not sure why remotePort would have a null value. I'm using the same port name in both apps.
I've been trying to use code examples and other posts here on Stackoverflow to solve the issue, but I don't understand where I've messed it up. I would greatly appreciate any advice! Thanks!
Posts I've looked at:
https://nshipster.com/inter-process-communication/
CFMessagePort and sandboxing
Theos inter-app communication using mach ports
https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_application-groups
I was able to sort this out after reading Using App Groups for communication between macOS/iOS apps from the Same Vendor
The problem was that as of Big Sur (or possibly Catalina), macOS apps using App Groups require a group container to be created in the developer portal. after doing that, you need to reconfigure the code-signing your app to include a provisioning profile. I'm not really sure when this all became a requirement since setting up App Groups isn't really well-documented by Apple. Everything works great after following the steps in the above linked article.
I was able to get my array passed from one app to another:
NSData* myData = [NSKeyedArchiver archivedDataWithRootObject:self.runningProcs];
CFMessagePortRef remotePort = CFMessagePortCreateRemote(kCFAllocatorSystemDefault, CFSTR("group.DEV_TEAM_ID.com.if.APP_NAME.UNIQUE_NAME"));
SInt32 messageIdentifier = 1;
CFDataRef messageData = (__bridge CFDataRef)(myData);
SInt32 status = CFMessagePortSendRequest(remotePort, messageIdentifier, messageData, 1000, 0, NULL, NULL);
if (status == kCFMessagePortSuccess)
{
NSLog(#"success");
}
Related
This is a secondary question that arose out of a post I made earlier today. I have the method below, which works fine for what I need, but sometimes crashes when I call CFRelease on the hostRef variable. I think it may have to do with the resource being used elsewhere when I'm trying to release it, but as far as I can tell, I'm synchronously resolving the host and I'm not accessing it from another thread.
I tried calling CFHostCancelInfoResolution before CFRelease, but that didn't change the frequency of the crashes. I thought I would post this here to see if there are some assumptions or misconceptions I have that aren't true.
+ (NSArray *) addressesForHostname: (NSString *)hostname {
CFMutableArrayRef ipAddresses = nil;
DLog(#"Getting addresses for host name %#", hostname);
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFStringRef)(hostname));
CFStreamError error;
BOOL didResolve = CFHostStartInfoResolution(hostRef, kCFHostAddresses, &error); // synchronously get the host.
if (didResolve) {
CFArrayRef responseObjects = CFHostGetAddressing(hostRef, NULL);
long numberOfResponses = CFArrayGetCount(responseObjects);
ipAddresses = CFArrayCreateMutable(kCFAllocatorDefault, numberOfResponses, &kCFTypeArrayCallBacks);
for ( int i = 0 ; i < numberOfResponses; ++i ) {
char * ipAddress = NULL;
CFDataRef responseObject = CFArrayGetValueAtIndex(responseObjects, i);
struct sockaddr * currentAddress = (struct sockaddr *) CFDataGetBytePtr(responseObject); // Unwrap the CFData wrapper aound the sockaddr struct
switch (currentAddress->sa_family) {
case AF_INET: { // Internetworking AKA IPV4
DLog(#"Extracting IPV4 address");
struct sockaddr_in * socketAddress = (struct sockaddr_in *) currentAddress;
ipAddress = malloc(sizeof(INET_ADDRSTRLEN));
inet_ntop(AF_INET,
&(socketAddress->sin_addr),
ipAddress,
INET_ADDRSTRLEN);
CFStringRef ipAddressString = CFStringCreateWithCString(kCFAllocatorDefault, ipAddress, kCFStringEncodingASCII);
CFArrayInsertValueAtIndex(ipAddresses, i, ipAddressString);
break;
}
case AF_INET6: { // IPV6
DLog(#"Extracting IPV6 address");
struct sockaddr_in6 * socketAddress = (struct sockaddr_in6 *) currentAddress;
ipAddress = malloc(sizeof(INET6_ADDRSTRLEN));
inet_ntop(AF_INET6,
&(socketAddress->sin6_addr),
ipAddress,
INET6_ADDRSTRLEN);
CFStringRef ipAddressString = CFStringCreateWithCString(kCFAllocatorDefault, ipAddress, kCFStringEncodingASCII);
CFArrayInsertValueAtIndex(ipAddresses, i, ipAddressString);
break;
}
default:
DLog(#"Unsupported addressing protocol encountered. Gracefully ignoring and continuing.");
break;
}
if(ipAddress != NULL) {
free(ipAddress);
}
}
CFRelease(responseObjects);
}
CFRelease(hostRef);
return (__bridge_transfer NSArray *) ipAddresses;
}
removing CFRelease(responseObjects); shall fix it
You must follow the Create Rule for Core Foundation objects. If you received the object by calling a function with the words Create or Copy in their names (or if you call CFRetain explicitly), then you must release (CFRelease) the object when you're done with it. If you did not receive the object this way, then you must not release the object.
There are several mistakes in your code. First, the one you're finding, which is around responseObjects. You fetch this object using:
CFDataRef responseObject = CFArrayGetValueAtIndex(responseObjects, i);
The function does not have Create or Copy in its name. You must not call CFRelease on it.
However, you also call this:
ipAddresses = CFArrayCreateMutable(kCFAllocatorDefault, numberOfResponses, &kCFTypeArrayCallBacks);
and in a couple of places:
CFStringRef ipAddressString = CFStringCreateWithCString(kCFAllocatorDefault, ipAddress, kCFStringEncodingASCII);
You must call CFRelease on these objects before they go out of scope or you will leak them.
I think CFStreamError error; may cause this issue.
Try to declare error to null?
// synchronously get the host.
BOOL didResolve = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL);
I'm trying to send UDP packets in objective C. More specifically, building with xcode targeting the iphone 6.1 simulator.
I can't seem to actually receive the data I send. Weirdly, I do get a data event... the data's just been truncated to length 0.
I've cut it down as much as I can, to make a dead simple test I think should pass.
#import "UdpSocketTest.h"
#include <arpa/inet.h>
#import <CoreFoundation/CFSocket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#implementation UdpSocketTest
static int receivedByteCount = 0;
void onReceive(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info);
void onReceive(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
// this gets called once, but CFDataGetLength(data) == 0
receivedByteCount += CFDataGetLength(data);
}
-(void) testUdpSocket {
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(5000); // <-- doesn't really matter, not sending from receiver
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
CFSocketContext socketContext = { 0, (__bridge void*)self, CFRetain, CFRelease, NULL };
// prepare receiver
CFSocketRef receiver = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_DGRAM, IPPROTO_UDP ,kCFSocketDataCallBack, (CFSocketCallBack)onReceive, &socketContext);
CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(NULL, receiver, 0), kCFRunLoopCommonModes);
CFSocketConnectToAddress(receiver, CFDataCreate(NULL, (unsigned char *)&addr, sizeof(addr)), -1);
// point sender at receiver
CFSocketRef sender = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_DGRAM, IPPROTO_UDP, kCFSocketDataCallBack, (CFSocketCallBack)onReceive, &socketContext);
CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(NULL, sender, 0), kCFRunLoopCommonModes);
CFSocketConnectToAddress(sender, CFSocketCopyAddress(receiver), -1);
// send data of sixty zeroes, allow processing to occur
CFSocketSendData(sender, NULL, (__bridge CFDataRef)[NSMutableData dataWithLength:60], 2.0);
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
// did the data arrive?
STAssertTrue(receivedByteCount > 0, #"");
// nope
}
#end
What am I doing wrong? I know it's something obvious, but I can't see it.
I'm surprised you're getting any callbacks at all—you're never actually binding your receiver to your local port. You're creating two sockets and telling both of them "I went to send data to 127.0.0.1:5000", but nowhere are you saying "I want to receive data on 127.0.0.1:5000.
In order to do that, you should be calling CFSocketSetAddress on the receiver socket, not CFSocketConnectToAddress. This is equivalent to calling the bind(2) system call on the underlying native BSD socket.
I'm working on an iOS version of my Android application which constantly sends some binary data to server over regular TCP connection. And while Java code works fine, my code in plain C doesn't - the connection breaks after the first bunch of data is sent to server. I managed to reimplement Java solution using CFNetwork framework, however my C code is still a problem...
C code (error checking and uninteresting code is removed):
mSocket = socket(AF_INET, SOCK_STREAM, 0);
//...
connect(mSocket, (struct sockaddr *)&sin, sizeof(sin));
int keepalive = 1;
int res = setsockopt(mSocket, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive,
sizeof(int));
int set = 1;
setsockopt(mSocket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
unsigned char buffer* = //...;
int ret = send(mSocket, buffer, bufferLen, 0); //handshake with server, runs fine
while(/*there is new data*/) {
buffer = new_data(); //some fake method
ret = send(mSocket, buffer, bufferLen, 0); //On the second iteration
//write returns -1
}
However, this Objective-C code based on CFNetwork works just fine...
Objective-C code:
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
host,
port,
&readStream,
&writeStream);
CFWriteStreamOpen(mServerWriteStream);
while(/*there is new data*/) {
NSData* data = //....
int wroteBytes = CFWriteStreamWrite(mServerWriteStream, [data bytes], [data length]);
}
So I'm puzzled with what am I doing wrong in C code and will appreciate any hints
Thank you
If you need a regular TCP connection you need to create the socket with such protocol. Instead of:
mSocket = socket(AF_INET, SOCK_STREAM, 0);
You should create it as:
#include <netinet/in.h>
mSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
On my system IPPROTO_TCP is defined as 6 and not 0. I suspect you have something similar.
The setsockopt() has to execute before connect() or it is ignored.
The goal is to get read/write streams after a service is successfully resolved by with the dns_sd API. I started with Apple's DNSSDObjects sample project and I'm editing the DNSSDService.m file to get read and write streams after the service is resolved.
Here's what I've got so far. It seems like it should work, but it does't :(
I got this far by following the code on this thread, though I'm not entirely sure that it's how this should be done.
EDIT: Apple's documentation here confirms that this is how it should be done..."So, once you've resolved the service using DNSServiceResolve, you should pass the service's DNS name (the hosttarget parameter to your DNSServiceResolveReply callback) to a connect-by-name API (like CFStreamCreatePairWithSocketToHost)."
// Called by DNS-SD when something happens with the resolve operation.
static void ResolveReplyCallback(
DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char * fullname,
const char * hosttarget,
uint16_t port,
uint16_t txtLen,
const unsigned char * txtRecord,
void * context
)
{
CFStringRef host = CFStringCreateWithCString(kCFAllocatorDefault,
hosttarget,
kCFStringEncodingUTF8);
DNSSDService * obj;
#pragma unused(interfaceIndex)
assert([NSThread isMainThread]); // b/c sdRef dispatches to the main queue
obj = (__bridge DNSSDService *) context;
assert([obj isKindOfClass:[DNSSDService class]]);
assert(sdRef == obj->sdRef_);
#pragma unused(sdRef)
#pragma unused(flags)
#pragma unused(fullname)
#pragma unused(txtLen)
#pragma unused(txtRecord)
if (errorCode == kDNSServiceErr_NoError) {
[obj resolveReplyWithTarget:[NSString stringWithUTF8String:hosttarget]
port:ntohs(port)];
} else {
[obj stopWithError:[NSError errorWithDomain:NSNetServicesErrorDomain
code:errorCode
userInfo:nil]
notify:YES];
}
//now let's get read&write streams?
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
host,
port,
&readStream,
&writeStream
);
if (readStream && writeStream) {
CFReadStreamSetProperty(readStream,
kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream,
kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue);
obj.inputStream = (__bridge_transfer NSInputStream *) readStream;
obj.outputStream = (__bridge_transfer NSOutputStream *) writeStream;
}
CFRelease(host);
}
I'm working in a project for the iPad, it is a small program and I need it to communicate with another software that runs on windows and act like a server; so the application that I'm creating for the iPad will be the client.
I'm using CFNetwork to do sockets communication, this is the way I'm establishing the connection:
char ip[] = "192.168.0.244";
NSString *ipAddress = [[NSString alloc] initWithCString:ip];
/* Build our socket context; this ties an instance of self to the socket */
CFSocketContext CTX = { 0, self, NULL, NULL, NULL };
/* Create the server socket as a TCP IPv4 socket and set a callback */
/* for calls to the socket's lower-level connect() function */
TCPClient = CFSocketCreate(NULL, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketDataCallBack, (CFSocketCallBack)ConnectCallBack, &CTX);
if (TCPClient == NULL)
return;
/* Set the port and address we want to listen on */
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = inet_addr([ipAddress UTF8String]);
CFDataRef connectAddr = CFDataCreate(NULL, (unsigned char *)&addr, sizeof(addr));
CFSocketConnectToAddress(TCPClient, connectAddr, -1);
CFRunLoopSourceRef sourceRef = CFSocketCreateRunLoopSource(kCFAllocatorDefault, TCPClient, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), sourceRef, kCFRunLoopCommonModes);
CFRelease(sourceRef);
CFRunLoopRun();
And this is the way I sent the data, which basically is a byte array
/* The native socket, used for various operations */
// TCPClient is a CFSocketRef member variable
CFSocketNativeHandle sock = CFSocketGetNative(TCPClient);
Byte byteData[3];
byteData[0] = 0;
byteData[1] = 4;
byteData[2] = 0;
send(sock, byteData, strlen(byteData)+1, 0);
Finally, as you may have noticed, when I create the server socket, I registered a callback for the kCFSocketDataCallBack type, this is the code.
void ConnectCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
// SocketsViewController is the class that contains all the methods
SocketsViewController *obj = (SocketsViewController*)info;
UInt8 *unsignedData = (UInt8 *) CFDataGetBytePtr(data);
char *value = (char*)unsignedData;
NSString *text = [[NSString alloc]initWithCString:value length:strlen(value)];
[obj writeToTextView:text];
[text release];
}
Actually, this callback is being invoked in my code, the problem is that I don't know how can I get the data that the windows client sent me, I'm expecting to receive an array of bytes, but I don't know how can I get those bytes from the data param.
If anyone can help me to find a way to do this, or maybe me point me to another way to get the data from the server in my client application I would really appreciate it.
Thanks.
In your callback, the data parameter is indeed a CFDataRef value for the kCFSocketDataCallBack callback type.
CFDataRef dataRef = (CFDataRef) data;
Byte *array = new Byte[CFDataGetLength(dataRef)]; // Or use a fixed length
CFDataGetBytes(dataRef, CFRangeMake(0, CFDataGetLength(theData)), array);
The array will then contains the array of byte.