Пример #1
0
int main(int argc, char *argv[]) {
    {
        struct sockaddr_in address;
        address.sin_len = sizeof(address);
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = INADDR_ANY;
        address.sin_port = htons(787);

        CFDataRef data(CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&address), sizeof(address)));

        CFSocketSignature signature;
        signature.protocolFamily = AF_INET;
        signature.socketType = SOCK_STREAM;
        signature.protocol = IPPROTO_TCP;
        signature.address = data;

        CFSocketRef socket(CFSocketCreateWithSocketSignature(kCFAllocatorDefault, &signature, kCFSocketAcceptCallBack, &OnAccept, NULL));
        CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0), kCFRunLoopDefaultMode);
    }

    {
        CYServer *server(new CYServer());
        server->socket_ = _syscall(socket(PF_UNIX, SOCK_STREAM, 0));

        struct sockaddr_un address;
        memset(&address, 0, sizeof(address));
        address.sun_family = AF_UNIX;

        sprintf(address.sun_path, "/tmp/.s.cy");
    }
}
Пример #2
0
struct MacGSocketData* _GSocket_Get_Mac_Socket(GSocket *socket)
{
  /* If socket is already created, returns a pointer to the data */
  /* Otherwise, creates socket and returns the pointer */
  CFSocketContext cont;
  struct MacGSocketData* data = (struct MacGSocketData*)socket->m_gui_dependent;

  if (data && data->source) return data;

  /* CFSocket has not been created, create it: */
  if (socket->m_fd < 0 || !data) return NULL;
  cont.version = 0; cont.retain = NULL;
  cont.release = NULL; cont.copyDescription = NULL;
  cont.info = socket;

  CFSocketRef cf = CFSocketCreateWithNative(NULL, socket->m_fd,
  			ALL_CALLBACK_TYPES, Mac_Socket_Callback, &cont);
  CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, cf, 0);
  assert(source);
  socket->m_gui_dependent = (char*)data;

  /* Keep the source and the socket around. */
  data->source = source;
  data->socket = cf;

  return data;
}
Пример #3
0
/*
    Adds a loop source for the given socket to the current run loop.
*/
CFRunLoopSourceRef qt_mac_add_socket_to_runloop(const CFSocketRef socket)
{
    CFRunLoopSourceRef loopSource = CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0);
    if (!loopSource)
        return 0;

    CFRunLoopAddSource(CFRunLoopGetCurrent(), loopSource, kCFRunLoopCommonModes);
    return loopSource;
}
Пример #4
0
int
browse(void)
{
	DNSServiceErrorType err;
	CFSocketContext ctx = { 0, NULL, NULL, NULL, NULL };

	err = DNSServiceBrowse(&nfsinfo.sdref, 0, 0, "_nfs._tcp", NULL, browser_callback, NULL);
	if (err != kDNSServiceErr_NoError)
		return (1);
	ctx.info = (void*)&nfsinfo;
	nfsinfo.sockref = CFSocketCreateWithNative(kCFAllocatorDefault, DNSServiceRefSockFD(nfsinfo.sdref),
			kCFSocketReadCallBack, socket_callback, &ctx);
	if (!nfsinfo.sockref)
		return (1);
	nfsinfo.rls = CFSocketCreateRunLoopSource(kCFAllocatorDefault, nfsinfo.sockref, 1);
	CFRunLoopAddSource(CFRunLoopGetCurrent(), nfsinfo.rls, kCFRunLoopDefaultMode);

	/* For backwards compatibility, browse for "mountd" services too */
	err = DNSServiceBrowse(&mountdinfo.sdref, 0, 0, "_mountd._tcp", NULL, browser_callback, NULL);
	if (err != kDNSServiceErr_NoError)
		return (1);
	ctx.info = (void*)&mountdinfo;
	mountdinfo.sockref = CFSocketCreateWithNative(kCFAllocatorDefault, DNSServiceRefSockFD(mountdinfo.sdref),
			kCFSocketReadCallBack, socket_callback, &ctx);
	if (!mountdinfo.sockref)
		return (1);
	mountdinfo.rls = CFSocketCreateRunLoopSource(kCFAllocatorDefault, mountdinfo.sockref, 1);
	CFRunLoopAddSource(CFRunLoopGetCurrent(), mountdinfo.rls, kCFRunLoopDefaultMode);

	CFRunLoopRun();

	CFRelease(nfsinfo.rls);
	CFSocketInvalidate(nfsinfo.sockref);
	CFRelease(nfsinfo.sockref);
	DNSServiceRefDeallocate(nfsinfo.sdref);
	CFRelease(mountdinfo.rls);
	CFSocketInvalidate(mountdinfo.sockref);
	CFRelease(mountdinfo.sockref);
	DNSServiceRefDeallocate(mountdinfo.sdref);
	return (0);
}
Пример #5
0
struct CFMIDIRunLoopSource * CFMIDIRunLoopSourceCreate( struct MIDIRunloopSource * source ) {
  int i, create;
  CFRunLoopTimerContext timer_context;
  CFSocketContext       socket_context;
  CFOptionFlags         socket_cb_types;
  CFSocketRef           socket;
  struct CFMIDIRunLoopSource * cf = malloc( sizeof(struct CFMIDIRunLoopSource)
                                          + sizeof(CFRunLoopSourceRef)*(source->nfds-1) );
  MIDIPrecondReturn( cf != NULL, ENOMEM, NULL );
  
  timer_context.version = 0;
  timer_context.info = source;
  timer_context.release = NULL;
  timer_context.retain  = NULL;
  timer_context.copyDescription = NULL;

  socket_context.version = 0;
  socket_context.info = source;
  socket_context.release = NULL;
  socket_context.retain  = NULL;
  socket_context.copyDescription = NULL;
  
  cf->length = source->nfds;

  if( source->timeout.tv_sec > 0 || source->timeout.tv_nsec > 0 ) {
    cf->cfrlt = CFRunLoopTimerCreate( NULL, CFAbsoluteTimeGetCurrent(),
      (double) source->timeout.tv_sec + 0.000000001 * (double) source->timeout.tv_nsec,
      0, 1, &_cf_timer_callback, &timer_context );
  } else {
    cf->cfrlt = NULL;
  }

  for( i=0; i<cf->length; i++ ) {
    create = 0;
    socket_cb_types = kCFSocketNoCallBack;
    if( FD_ISSET(i, &(source->readfds)) ) {
      socket_cb_types |= kCFSocketReadCallBack;
      create = 1;
    }
    if( FD_ISSET(i, &(source->writefds)) ) {
      socket_cb_types |= kCFSocketWriteCallBack;
      create = 1;
    }
    if( create ) {
      socket       = CFSocketCreateWithNative( NULL, i, socket_cb_types, &_cf_socket_callback, &socket_context );
      cf->cfrls[i] = CFSocketCreateRunLoopSource( NULL, socket, 1 );
      CFRelease( socket );
    } else {
      cf->cfrls[i] = NULL;
    }
  }
  return cf;
}
Пример #6
0
/*
 * Handle newly-discovered services
 */
static void
browser_callback(
	__unused DNSServiceRef sdRef,
	DNSServiceFlags servFlags,
	uint32_t interfaceIndex,
	DNSServiceErrorType errorCode,
	const char *serviceName,
	const char *regType,
	const char *replyDomain,
	__unused void *context)
{
	DNSServiceErrorType err;
	CFSocketContext ctx = { 0, NULL, NULL, NULL, NULL };
	struct cbinfo *info;

        if (errorCode != kDNSServiceErr_NoError) {
		printf("DNS service discovery error: %d\n", errorCode);
		return;
	}
#ifdef DEBUG
	printf("browse: %s: %s, %s, %s\n",
		(servFlags & kDNSServiceFlagsAdd) ? "new" : "gone",
		serviceName, regType, replyDomain);
#endif
	if (!(servFlags & kDNSServiceFlagsAdd))
		return;

	info = malloc(sizeof(*info));
	if (!info) {
		printf("browse: out of memeory\n");
		return;
	}

	err = DNSServiceResolve(&info->sdref, servFlags, interfaceIndex, serviceName, regType, replyDomain, resolve_callback, info);
	if (err != kDNSServiceErr_NoError) {
		printf("DNSServiceResolve failed: %d\n", err);
		free(info);
		return;
	}
	ctx.info = (void*)info;
	info->sockref = CFSocketCreateWithNative(kCFAllocatorDefault, DNSServiceRefSockFD(info->sdref),
				kCFSocketReadCallBack, socket_callback, &ctx);
	if (!info->sockref) {
		printf("CFSocketCreateWithNative failed\n");
		DNSServiceRefDeallocate(info->sdref);
		free(info);
		return;
	}
	info->rls = CFSocketCreateRunLoopSource(kCFAllocatorDefault, info->sockref, 1);
	CFRunLoopAddSource(CFRunLoopGetCurrent(), info->rls, kCFRunLoopDefaultMode);
}
Пример #7
0
void start_remote_debug_server(AMDeviceRef device) {
    assert(AMDeviceStartService(device, CFSTR("com.apple.debugserver"), &gdbfd, NULL) == 0);

    CFSocketRef fdvendor = CFSocketCreate(NULL, AF_UNIX, 0, 0, kCFSocketAcceptCallBack, &fdvendor_callback, NULL);

    int yes = 1;
    setsockopt(CFSocketGetNative(fdvendor), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));

    struct sockaddr_un address;
    memset(&address, 0, sizeof(address));
    address.sun_family = AF_UNIX;
    strcpy(address.sun_path, FDVENDOR_PATH);
    CFDataRef address_data = CFDataCreate(NULL, (const UInt8 *)&address, sizeof(address));

    unlink(FDVENDOR_PATH);

    CFSocketSetAddress(fdvendor, address_data);
    CFRelease(address_data);
    CFRunLoopAddSource(CFRunLoopGetMain(), CFSocketCreateRunLoopSource(NULL, fdvendor, 0), kCFRunLoopCommonModes);
}
Пример #8
0
static void OnAccept(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) {
    switch (type) {
        case kCFSocketAcceptCallBack:
            Client *client(new Client());

            client->message_ = NULL;

            CFSocketContext context;
            context.version = 0;
            context.info = client;
            context.retain = NULL;
            context.release = NULL;
            context.copyDescription = NULL;

            client->socket_ = CFSocketCreateWithNative(kCFAllocatorDefault, *reinterpret_cast<const CFSocketNativeHandle *>(value), kCFSocketDataCallBack, &OnData, &context);

            CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(kCFAllocatorDefault, client->socket_, 0), kCFRunLoopDefaultMode);
        break;
    }
}
Пример #9
0
unsigned int
xmmsc_mainloop_cf_init (xmmsc_connection_t *c, CFRunLoopSourceRef *source)
{

	CFRunLoopRef runLoopRef = CFRunLoopGetCurrent ();
	CFRunLoopSourceRef runLoopSourceRef;
	CFSocketContext context;
	CFSocketRef sockRef;
	CFOptionFlags flags;

	context.version = 0;
	context.info = c;
	context.retain = NULL;
	context.release = NULL;
	context.copyDescription = NULL;

	flags = kCFSocketReadCallBack;
	if (xmmsc_io_want_out (c))
		flags |= kCFSocketWriteCallBack;

	sockRef = CFSocketCreateWithNative (kCFAllocatorDefault,
	                                    xmmsc_io_fd_get (c),
	                                    flags,
	                                    &xmmsc_io_cf_event_callback,
	                                    &context);

	if (!sockRef)
		return 0;


	runLoopSourceRef = CFSocketCreateRunLoopSource (kCFAllocatorDefault,
	                                                sockRef, XMMSC_CF_SOURCE_PRIORITY);

	CFRunLoopAddSource (runLoopRef, runLoopSourceRef, kCFRunLoopDefaultMode);


	xmmsc_io_need_out_callback_set (c, xmmsc_io_cf_toggle_socket_flags, sockRef);
	*source = runLoopSourceRef;

	return 1;
}
Пример #10
0
/*!
    Implements the GUI-specific AddProcessCallback() for both wxMac and
    wxCocoa using the CFSocket/CFRunLoop API which is available to both.
    Takes advantage of the fact that sockets on UNIX are just regular
    file descriptors and thus even a non-socket file descriptor can
    apparently be used with CFSocket so long as you only tell CFSocket
    to do things with it that would be valid for a non-socket fd.
 */
int wxGUIAppTraits::AddProcessCallback(wxEndProcessData *proc_data, int fd)
{
    static int s_last_tag = 0;
    CFSocketContext context =
    {   0
    ,   static_cast<void*>(proc_data)
    ,   NULL
    ,   NULL
    ,   NULL
    };
    CFSocketRef cfSocket = CFSocketCreateWithNative(kCFAllocatorDefault,fd,kCFSocketReadCallBack,&WXCF_EndProcessDetector,&context);
    if(cfSocket == NULL)
    {
        wxLogError(wxT("Failed to create socket for end process detection"));
        return 0;
    }
    CFRunLoopSourceRef runLoopSource = CFSocketCreateRunLoopSource(kCFAllocatorDefault, cfSocket, /*highest priority:*/0);
    if(runLoopSource == NULL)
    {
        wxLogError(wxT("Failed to create CFRunLoopSource from CFSocket for end process detection"));
        // closes the fd.. we can't really stop it, nor do we necessarily want to.
        CFSocketInvalidate(cfSocket);
        CFRelease(cfSocket);
        return 0;
    }
    // Now that the run loop source has the socket retained and we no longer
    // need to refer to it within this method, we can release it.
    CFRelease(cfSocket);

    CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
    // Now that the run loop has the source retained we can release it.
    CFRelease(runLoopSource);

    /*
        Feed wx some bullshit.. we don't use it since CFSocket helpfully passes
        itself into our callback and that's enough to be able to
        CFSocketInvalidate it which is all we need to do to get everything we
        just created to be deallocated.
     */
    return ++s_last_tag;
}
Пример #11
0
FskErr KprZeroconfPlatformServiceNew(KprZeroconfPlatformService* it, KprZeroconfPlatformService owner, DNSServiceRef serviceRef, const char* name, UInt32 port)
{
	FskErr err = kFskErrNone;
	KprZeroconfPlatformService self = NULL;
	SInt32 fd = DNSServiceRefSockFD(serviceRef);
	bailIfError(FskMemPtrNewClear(sizeof(KprZeroconfPlatformServiceRecord), &self));
	FskInstrumentedItemNew(self, NULL, &KprZeroconfPlatformServiceInstrumentation);
	self->owner = owner;
	self->serviceRef = serviceRef;
	if (name) {
		self->name = FskStrDoCopy(name);
		bailIfNULL(self->name);
	}
	self->port = port;
#if KPR_ZEROCONF_EMBEDDED
	if (!gEmbeddedServiceCount) {
		FskTimeCallbackNew(&gEmbeddedServiceTimer);
		FskTimeCallbackScheduleFuture(gEmbeddedServiceTimer, 0, 0, KprZeroconfEmbeddedCallback, NULL);
	}
	gEmbeddedServiceCount++;
#elif TARGET_OS_MAC
	CFSocketContext context;
	FskMemSet(&context, 0, sizeof(context));
	context.info = (void*)serviceRef;
	self->socket = CFSocketCreateWithNative(kCFAllocatorDefault, fd, kCFSocketReadCallBack, KprZeroconfPlatformCallBack, &context);
	self->source = CFSocketCreateRunLoopSource(NULL, self->socket, 0);
	CFRunLoopAddSource(CFRunLoopGetCurrent(), self->source, kCFRunLoopCommonModes);
#else
	self->source = FskThreadCreateDataSource(fd);
	FskSocketActivate(self->source, true);
	FskThreadAddDataHandler(&self->handler, self->source, KprZeroconfPlatformCallBack, true, false, serviceRef);
#endif
	*it = self;
	return err;
bail:
	if (err) {
		KprZeroconfPlatformServiceDispose(self);
	}
	return err;
}
Пример #12
0
static void
MyDNSServiceAddToRunLoop(MyDNSServiceState *ref)
{
    CFSocketNativeHandle    sock;
    CFOptionFlags           sockFlags;
    CFRunLoopSourceRef      source;
    CFSocketContext         context = { 0, ref, NULL, NULL, NULL };  // Use MyDNSServiceState as context data.

    assert(ref);

    // Access the underlying Unix domain socket to communicate with the mDNSResponder daemon.
    sock = DNSServiceRefSockFD(ref->service);
    assert(sock >= 0);

    // Create a CFSocket using the Unix domain socket.
    ref->socket = CFSocketCreateWithNative(NULL, sock, kCFSocketReadCallBack, MyDNSServiceSocketCallBack, &context);
    assert(ref->socket);

    // Prevent CFSocketInvalidate from closing DNSServiceRef's socket.
    sockFlags = CFSocketGetSocketFlags(ref->socket);
    CFSocketSetSocketFlags(ref->socket, sockFlags & (~kCFSocketCloseOnInvalidate));

    // Create a CFRunLoopSource from the CFSocket.
    source = CFSocketCreateRunLoopSource(NULL, ref->socket, 0);
    assert(source);

    // Add the CFRunLoopSource to the current runloop.
    CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
    
    // We no longer need our reference to source.  The runloop continues to 
    // hold a reference to it, but we don't care about that.  When we invalidate 
    // the socket, the runloop will drop its reference and the source will get 
    // destroyed.
    
    CFRelease(source);
}
void
load(CFBundleRef bundle, Boolean bundleVerbose)
{
	int			so;
	int			status;
	struct kev_request	kev_req;
	CFSocketRef		es;
	CFSocketContext		context = { 0, NULL, NULL, NULL, NULL };
	CFRunLoopSourceRef	rls;

	if (bundleVerbose) {
		_verbose = TRUE;
	}

	SCLog(_verbose, LOG_DEBUG, CFSTR("load() called"));
	SCLog(_verbose, LOG_DEBUG, CFSTR("  bundle ID = %@"), CFBundleGetIdentifier(bundle));

	/* open a "configd" session to allow cache updates */
	store = SCDynamicStoreCreate(NULL,
				     CFSTR("Kernel Event Monitor plug-in"),
				     NULL,
				     NULL);
	if (!store) {
		SCLog(TRUE, LOG_ERR, CFSTR("SCDnamicStoreCreate() failed: %s"), SCErrorString(SCError()));
		SCLog(TRUE, LOG_ERR, CFSTR("kernel event monitor disabled."));
		return;
	}

	/* Open an event socket */
	so = socket(PF_SYSTEM, SOCK_RAW, SYSPROTO_EVENT);
	if (so != -1) {
		/* establish filter to return all events */
		kev_req.vendor_code  = 0;
		kev_req.kev_class    = 0;	/* Not used if vendor_code is 0 */
		kev_req.kev_subclass = 0;	/* Not used if either kev_class OR vendor_code are 0 */
		status = ioctl(so, SIOCSKEVFILT, &kev_req);
		if (status) {
			SCLog(TRUE, LOG_ERR, CFSTR("could not establish event filter, ioctl() failed: %s"), strerror(errno));
			(void) close(so);
			so = -1;
		}
	} else {
		SCLog(TRUE, LOG_ERR, CFSTR("could not open event socket, socket() failed: %s"), strerror(errno));
	}

	if (so != -1) {
		int	yes = 1;

		status = ioctl(so, FIONBIO, &yes);
		if (status) {
			SCLog(TRUE, LOG_ERR, CFSTR("could not set non-blocking io, ioctl() failed: %s"), strerror(errno));
			(void) close(so);
			so = -1;
		}
	}

	if (so == -1) {
		SCLog(TRUE, LOG_ERR, CFSTR("kernel event monitor disabled."));
		CFRelease(store);
		return;
	}

	/* Create a CFSocketRef for the PF_SYSTEM kernel event socket */
	es  = CFSocketCreateWithNative(NULL,
				       so,
				       kCFSocketReadCallBack,
				       eventCallback,
				       &context);

	/* Create and add a run loop source for the event socket */
	rls = CFSocketCreateRunLoopSource(NULL, es, 0);
	CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
	CFRelease(rls);
	CFRelease(es);

	return;
}
Пример #14
0
static int ConnectionRegisterListener(
    ConnectionRef               conn, 
    CFRunLoopRef                runLoop,
    CFStringRef                 runLoopMode, 
    ConnectionCallbackProcPtr   callback, 
    void *                      refCon
)
    // Register a listener to be called when packets arrive.  Once you've done 
    // this, you can no longer use conn for RPCs.
    //
    // conn must be a valid connection
    //
    // runLoop and runLoopMode specify the context in which the callback will 
    // be called; in most cases you specify CFRunLoopGetCurrent() and 
    // kCFRunLoopDefaultMode
    //
    // callback is the function you want to be called when packets arrive; it 
    // must not be NULL
    //
    // refCon is passed to callback
    //
    // Returns an errno-style error code
    // On success, the connection has been converted to a listener and your 
    // callback will be called from the context of the specific runloop when 
    // a packet arrives; on error, the connection is no longer useful (conn is 
    // still valid, but you can't use it to transmit any more data)
{
    int             err;

    assert(conn != NULL);
    assert(runLoop != NULL);
    assert(runLoopMode != NULL);
    assert(callback != NULL);

    assert(conn->fSockFD != -1);            // connection must not be shut down
    assert(conn->fSockCF == NULL);          // can't register twice
    
    // Create the packet buffer.
    
    err = 0;
    conn->fBufferedPackets = CFDataCreateMutable(NULL, 0);
    if (conn->fBufferedPackets == NULL) {
        err = ENOMEM;
    }
    
    // Add the source to the runloop.
    
    if (err == 0) {
        CFSocketContext context;

        memset(&context, 0, sizeof(context));
        context.info = conn;

        conn->fSockCF = CFSocketCreateWithNative(
            NULL, 
            (CFSocketNativeHandle) conn->fSockFD, 
            kCFSocketDataCallBack, 
            ConnectionGotData, 
            &context
        );
        if (conn->fSockCF == NULL) {
            err = EINVAL;
        }
    }
    if (err == 0) {
        conn->fRunLoopSource = CFSocketCreateRunLoopSource(NULL, conn->fSockCF, 0);
        if (conn->fRunLoopSource == NULL) {
            err = EINVAL;
        }
    }
    if (err == 0) {
        conn->fCallback = callback;
        conn->fCallbackRefCon = refCon;
        
        CFRunLoopAddSource( runLoop, conn->fRunLoopSource, runLoopMode);
    }

    // Any failure means the entire connection is dead; again, this is the 
    // draconian approach to error handling.  But hey, connections are 
    // (relatively) cheap.
    
    if (err != 0) {
        ConnectionShutdown(conn);
    }
    
    return err;
}
Пример #15
0
/*! iSCSI daemon entry point. */
int main(void)
{
    // Connect to the preferences .plist file associated with "iscsid" and
    // read configuration parameters for the initiator
    iSCSIPLSynchronize();

    CFStringRef initiatorIQN = iSCSIPLCopyInitiatorIQN();

    if(initiatorIQN) {
        iSCSISetInitiatiorName(initiatorIQN);
        CFRelease(initiatorIQN);
    }

    CFStringRef initiatorAlias = iSCSIPLCopyInitiatorAlias();

    if(initiatorAlias) {
        iSCSISetInitiatiorName(initiatorAlias);
        CFRelease(initiatorAlias);
    }

    // Register with launchd so it can manage this daemon
    launch_data_t reg_request = launch_data_new_string(LAUNCH_KEY_CHECKIN);

    // Quit if we are unable to checkin...
    if(!reg_request) {
        fprintf(stderr,"Failed to checkin with launchd.\n");
        goto ERROR_LAUNCH_DATA;
    }

    launch_data_t reg_response = launch_msg(reg_request);

    // Ensure registration was successful
    if((launch_data_get_type(reg_response) == LAUNCH_DATA_ERRNO)) {
        fprintf(stderr,"Failed to checkin with launchd.\n");
        goto ERROR_NO_SOCKETS;
    }

    // Grab label and socket dictionary from daemon's property list
    launch_data_t label = launch_data_dict_lookup(reg_response,LAUNCH_JOBKEY_LABEL);
    launch_data_t sockets = launch_data_dict_lookup(reg_response,LAUNCH_JOBKEY_SOCKETS);

    if(!label || !sockets) {
        fprintf(stderr,"Could not find socket ");
        goto ERROR_NO_SOCKETS;
    }

    launch_data_t listen_socket_array = launch_data_dict_lookup(sockets,"iscsid");

    if(!listen_socket_array || launch_data_array_get_count(listen_socket_array) == 0)
        goto ERROR_NO_SOCKETS;

    // Grab handle to socket we want to listen on...
    launch_data_t listen_socket = launch_data_array_get_index(listen_socket_array,0);

    if(!iSCSIDRegisterForPowerEvents())
        goto ERROR_PWR_MGMT_FAIL;

    // Create a socket that will
    CFSocketRef socket = CFSocketCreateWithNative(kCFAllocatorDefault,
                         launch_data_get_fd(listen_socket),
                         kCFSocketReadCallBack,
                         iSCSIDProcessIncomingRequest,0);

    // Runloop sources associated with socket events of connected clients
    CFRunLoopSourceRef clientSockSource = CFSocketCreateRunLoopSource(kCFAllocatorDefault,socket,0);
    CFRunLoopAddSource(CFRunLoopGetMain(),clientSockSource,kCFRunLoopDefaultMode);

    CFRunLoopRun();

    // Deregister for power
    iSCSIDDeregisterForPowerEvents();

    launch_data_free(reg_response);
    return 0;

    // TODO: verify that launch data is freed under all possible execution paths

ERROR_PWR_MGMT_FAIL:
ERROR_NO_SOCKETS:
    launch_data_free(reg_response);

ERROR_LAUNCH_DATA:

    return ENOTSUP;
}
Пример #16
0
WebSocketRef
WebSocketCreateWithHostAndPort (CFAllocatorRef allocator, CFStringRef host, UInt16 port, void *userInfo) {
    WebSocketRef self = CFAllocatorAllocate(allocator, sizeof(WebSocket), 0);
    if (self) {
        self->allocator = allocator ? CFRetain(allocator) : NULL;
        self->retainCount = 1;
        self->userInfo = userInfo;

        self->clientsLength = 1024;
        self->clientsUsedLength = 0;
        if (NULL == (self->clients = CFAllocatorAllocate(allocator, self->clientsLength, 0))) {
            WebSocketRelease(self), self = NULL;
            goto fin;
        }

        // Callbacks
        self->callbacks.didAddClientCallback     = NULL;
        self->callbacks.willRemoveClientCallback = NULL;
        self->callbacks.didClientReadCallback    = NULL;

        // Setup the context;
        CFSocketContext context = {
            .copyDescription = NULL,
            .retain = NULL,
            .release = NULL,
            .version = 0,
            .info = self
        };

        if (NULL == (self->socket = CFSocketCreate(self->allocator, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, __WebSocketAcceptCallBack, &context))) {
            WebSocketRelease(self), self = NULL;
            goto fin;
        }

        // Re-use local addresses, if they're still in TIME_WAIT
        int yes = 1;
        setsockopt(CFSocketGetNative(self->socket), SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));

        // Set the port and address we want to listen on
        memset(&self->addr, 0, sizeof(self->addr));
        self->addr.sin_len = sizeof(self->addr);
        self->addr.sin_family = AF_INET;

        if (CFEqual(kWebSocketHostAny, host)) {

            // Host is set to "0.0.0.0", set it to INADDR_ANY
            self->addr.sin_addr.s_addr = htonl(INADDR_ANY);
        } else {

            // Set the host based on provided string. TODO: hostname resolution?
            CFIndex hostCStringLength = CFStringGetMaximumSizeForEncoding(CFStringGetLength(host), kCFStringEncodingASCII) + 1;
            char *hostCString = CFAllocatorAllocate(self->allocator, hostCStringLength, 0);
            if (hostCString) {
                if (CFStringGetCString(host, hostCString, hostCStringLength, kCFStringEncodingASCII)) {
                    inet_aton(hostCString, &self->addr.sin_addr);
                } else {

                    // TODO: Couldn't get CString
                }
                CFAllocatorDeallocate(self->allocator, hostCString);
            } else {

                // TODO: Couldn't allocate buffer
            }
        }

        self->addr.sin_port = htons(port);

        CFDataRef address = CFDataCreate(self->allocator, (const void *) &self->addr, sizeof(self->addr));
        if (address) {
            if (CFSocketSetAddress(self->socket, (CFDataRef) address) != kCFSocketSuccess) {
                WebSocketRelease(self), self = NULL;
                CFRelease(address);
                goto fin;
            }
            CFRelease(address);
        }

        // Create run loop source and add it to the current run loop
        CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(self->allocator, self->socket, 0);
        CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
        CFRelease(source);
    }

fin:

    return self;
}

WebSocketRef
WebSocketCreate (CFAllocatorRef allocator, void *userInfo) {
    return WebSocketCreateWithHostAndPort(allocator, kWebSocketHostLoopBack, kWebSocketPortAny, userInfo);
}

void
WebSocketRetain (WebSocketRef self) {
    if_self {
        ++self->retainCount;
    }
}
Пример #17
0
/* extern */ void
_CFTypeScheduleOnRunLoop(CFTypeRef obj, CFRunLoopRef runLoop, CFStringRef runLoopMode) {

	CFTypeID t = CFGetTypeID(obj);
	CFTypeRef src = NULL;
	void(*fn)(CFTypeRef, CFRunLoopRef, CFStringRef);
	void(*fn2)(CFRunLoopRef, CFTypeRef, CFStringRef);

	fn = NULL;
	fn2 = (void(*)(CFRunLoopRef, CFTypeRef, CFStringRef))CFRunLoopAddSource;

	/* Get the correct source or function used for adding the object to the run loop. */
	if (t == CFRunLoopSourceGetTypeID()) {
		src = CFRetain(obj);
	}

	else if (t == CFMachPortGetTypeID()) {
		src = CFMachPortCreateRunLoopSource(CFGetAllocator(obj), (CFMachPortRef)obj, 0);
	}

	else if (t == CFSocketGetTypeID()) {
		src = CFSocketCreateRunLoopSource(CFGetAllocator(obj), (CFSocketRef)obj, 0);
	}

	else if (t == CFReadStreamGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFReadStreamScheduleWithRunLoop;
	}

	else if (t == CFWriteStreamGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFWriteStreamScheduleWithRunLoop;
	}

	else if (t == CFHostGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFHostScheduleWithRunLoop;
	}

	else if (t == SCNetworkReachabilityGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))SCNetworkReachabilityScheduleWithRunLoop;
	}

	else if (t == CFRunLoopTimerGetTypeID()) {
		src = CFRetain(obj);
		fn2 = (void(*)(CFRunLoopRef, CFTypeRef, CFStringRef))CFRunLoopAddTimer;
	}

	else if (t == CFNetServiceGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFNetServiceScheduleWithRunLoop;
	}

	else if (t == CFNetServiceBrowserGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFNetServiceBrowserScheduleWithRunLoop;
	}

	else if (t == CFNetServiceMonitorGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFNetServiceMonitorScheduleWithRunLoop;
	}

	else if (t == SCNetworkConnectionGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))SCNetworkConnectionScheduleWithRunLoop;
	}


	/* If a source was retrieved, need to add the source */
	if (src) {
		fn2(runLoop, src, runLoopMode);
		CFRelease(src);
	}

	/* If a schedule function was retrieved, call it. */
	else if (fn) {
		fn(obj, runLoop, runLoopMode);
	}
}
Пример #18
0
/* extern */ void
_CFTypeUnscheduleFromMultipleRunLoops(CFTypeRef obj, CFArrayRef schedules) {

	CFTypeID t = CFGetTypeID(obj);
	CFTypeRef src = NULL;
	void(*fn)(CFTypeRef, CFRunLoopRef, CFStringRef);
	void(*fn2)(CFRunLoopRef, CFTypeRef, CFStringRef);

	fn = NULL;
	fn2 = (void(*)(CFRunLoopRef, CFTypeRef, CFStringRef))CFRunLoopRemoveSource;

	/* Get the proper source or function for removing the object from the run loop. */
	if (t == CFRunLoopSourceGetTypeID()) {
		src = CFRetain(obj);
	}

	else if (t == CFMachPortGetTypeID()) {
		src = CFMachPortCreateRunLoopSource(CFGetAllocator(obj), (CFMachPortRef)obj, 0);
	}

	else if (t == CFSocketGetTypeID()) {
		src = CFSocketCreateRunLoopSource(CFGetAllocator(obj), (CFSocketRef)obj, 0);
	}

	else if (t == CFReadStreamGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFReadStreamUnscheduleFromRunLoop;
	}

	else if (t == CFWriteStreamGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFWriteStreamUnscheduleFromRunLoop;
	}

	else if (t == CFHostGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFHostUnscheduleFromRunLoop;
	}

	else if (t == SCNetworkReachabilityGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))SCNetworkReachabilityUnscheduleFromRunLoop;
	}

	else if (t == CFRunLoopTimerGetTypeID()) {
		src = CFRetain(obj);
		fn2 = (void(*)(CFRunLoopRef, CFTypeRef, CFStringRef))CFRunLoopRemoveTimer;
	}

	else if (t == CFNetServiceGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFNetServiceUnscheduleFromRunLoop;
	}

	else if (t == CFNetServiceBrowserGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFNetServiceBrowserUnscheduleFromRunLoop;
	}

	else if (t == CFNetServiceMonitorGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFNetServiceMonitorUnscheduleFromRunLoop;
	}

	else if (t == SCNetworkConnectionGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))SCNetworkConnectionUnscheduleFromRunLoop;
	}

	/* If a source was retrieved, need to remove it from the list of run loops*/
	if (src) {

		CFIndex i, length = CFArrayGetCount(schedules);

		for (i = 0; i < length; i += 2) {
			fn2((CFRunLoopRef)CFArrayGetValueAtIndex(schedules, i),
				src,
				(CFStringRef)CFArrayGetValueAtIndex(schedules, i + 1));
		}

		CFRelease(src);
	}

	/* If an unschedule function was retrieved, need to call it for each schedule in the list. */
	else if (fn) {

		CFIndex i, length = CFArrayGetCount(schedules);

		for (i = 0; i < length; i += 2) {
			fn(obj,
			   (CFRunLoopRef)CFArrayGetValueAtIndex(schedules, i),
			   (CFStringRef)CFArrayGetValueAtIndex(schedules, i + 1));
		}
	}
}
Пример #19
0
/* extern */ void
_CFTypeUnscheduleFromRunLoop(CFTypeRef obj, CFRunLoopRef runLoop, CFStringRef runLoopMode) {

	CFTypeID t = CFGetTypeID(obj);
	CFTypeRef src = NULL;
	void(*fn)(CFTypeRef, CFRunLoopRef, CFStringRef);
	void(*fn2)(CFRunLoopRef, CFTypeRef, CFStringRef);

	fn = NULL;
	fn2 = (void(*)(CFRunLoopRef, CFTypeRef, CFStringRef))CFRunLoopRemoveSource;

	/* Get the proper source or function for removing the object from the run loop. */
	if (t == CFRunLoopSourceGetTypeID()) {
		src = CFRetain(obj);
	}

	else if (t == CFMachPortGetTypeID()) {
		src = CFMachPortCreateRunLoopSource(CFGetAllocator(obj), (CFMachPortRef)obj, 0);
	}

	else if (t == CFSocketGetTypeID()) {
		src = CFSocketCreateRunLoopSource(CFGetAllocator(obj), (CFSocketRef)obj, 0);
	}

	else if (t == CFReadStreamGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFReadStreamUnscheduleFromRunLoop;
	}

	else if (t == CFWriteStreamGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFWriteStreamUnscheduleFromRunLoop;
	}

	else if (t == CFHostGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFHostUnscheduleFromRunLoop;
	}

	else if (t == SCNetworkReachabilityGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))SCNetworkReachabilityUnscheduleFromRunLoop;
	}

	else if (t == CFRunLoopTimerGetTypeID()) {
		src = CFRetain(obj);
		fn2 = (void(*)(CFRunLoopRef, CFTypeRef, CFStringRef))CFRunLoopRemoveTimer;
	}

	else if (t == CFNetServiceGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFNetServiceUnscheduleFromRunLoop;
	}

	else if (t == CFNetServiceBrowserGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFNetServiceBrowserUnscheduleFromRunLoop;
	}

	else if (t == CFNetServiceMonitorGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))CFNetServiceMonitorUnscheduleFromRunLoop;
	}

	else if (t == SCNetworkConnectionGetTypeID()) {
		fn = (void(*)(CFTypeRef, CFRunLoopRef, CFStringRef))SCNetworkConnectionUnscheduleFromRunLoop;
	}

	/* If a source was retrieved, need to remove it */
	if (src) {
		fn2(runLoop, src, runLoopMode);
		CFRelease(src);
	}

	/* If an unschedule function was retrieved, need to call it. */
	else if (fn) {
		fn(obj, runLoop, runLoopMode);
	}
}
int tnet_transport_wrap(tnet_transport_t *transport, int index) {
    transport_context_t *context = transport->context;
    transport_socket_t *sock = context->sockets[index];
    
    // If the socket is already wrapped in a CFSocket then return.
    if (sock->cf_socket || sock->cf_read_stream) {
        return 1;
    }
    
    // Put a reference to the transport context 
    const CFSocketContext socket_context = { 0, transport, NULL, NULL, NULL };
    
    if (TNET_SOCKET_TYPE_IS_DGRAM(sock->type)) {
        
        // Create a CFSocket from the native socket and register for Read events
        sock->cf_socket = CFSocketCreateWithNative(kCFAllocatorDefault, 
                                                   sock->fd,
                                                   kCFSocketReadCallBack, 
                                                   &__CFSocketCallBack, 
                                                   &socket_context);
        
        // Don't close the socket if the CFSocket is invalidated
        CFOptionFlags flags = CFSocketGetSocketFlags(sock->cf_socket);
        flags = flags & ~kCFSocketCloseOnInvalidate;
        CFSocketSetSocketFlags(sock->cf_socket, flags);
		
        
        // Create a new RunLoopSource and register it with the main thread RunLoop
        sock->cf_run_loop_source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, sock->cf_socket, 0);
        CFRunLoopAddSource(context->cf_run_loop, sock->cf_run_loop_source, kCFRunLoopDefaultMode);
        CFRelease(sock->cf_run_loop_source);
        
    } else if (TNET_SOCKET_TYPE_IS_STREAM(sock->type)) {
        
        // Create a pair of streams (read/write) from the socket
        CFStreamCreatePairWithSocket(kCFAllocatorDefault, sock->fd, &sock->cf_read_stream, &sock->cf_write_stream);
        
        // Don't close underlying socket
        CFReadStreamSetProperty(sock->cf_read_stream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse);
        CFWriteStreamSetProperty(sock->cf_write_stream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse);
        
        if (TNET_SOCKET_TYPE_IS_SECURE(sock->type)) {
            CFMutableDictionaryRef settings = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
            CFDictionaryAddValue(settings, kCFStreamSSLAllowsExpiredCertificates, kCFBooleanTrue);
            CFDictionaryAddValue(settings, kCFStreamSSLAllowsAnyRoot, kCFBooleanTrue);
            CFDictionaryAddValue(settings, kCFStreamSSLValidatesCertificateChain, kCFBooleanFalse);
            CFDictionaryAddValue(settings, kCFStreamSSLPeerName, kCFNull);
            
            // Set the SSL settings
            CFReadStreamSetProperty(sock->cf_read_stream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelNegotiatedSSL);
            CFReadStreamSetProperty(sock->cf_read_stream, kCFStreamPropertySSLSettings, settings);
            CFWriteStreamSetProperty(sock->cf_write_stream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelNegotiatedSSL);
            CFWriteStreamSetProperty(sock->cf_write_stream, kCFStreamPropertySSLSettings, settings);
            
            CFRelease(settings);
        }
        
#if __IPHONE_4_0
        // Mark the stream for VoIP usage
        CFReadStreamSetProperty(sock->cf_read_stream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
        CFWriteStreamSetProperty(sock->cf_write_stream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
#endif
        
        // Setup a context for the streams
        CFStreamClientContext streamContext = { 0, transport, NULL, NULL, NULL };
        
        // Set the client callback for the stream
        CFReadStreamSetClient(sock->cf_read_stream, 
                              kCFStreamEventOpenCompleted | kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered,
                              &__CFReadStreamClientCallBack, 
                              &streamContext);
        CFWriteStreamSetClient(sock->cf_write_stream, 
                               kCFStreamEventOpenCompleted | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered, 
                               &__CFWriteStreamClientCallBack, 
                               &streamContext);
        
        // Enroll streams in the run-loop
        CFReadStreamScheduleWithRunLoop(sock->cf_read_stream, context->cf_run_loop, kCFRunLoopDefaultMode);
        CFWriteStreamScheduleWithRunLoop(sock->cf_write_stream, context->cf_run_loop, kCFRunLoopDefaultMode);
        
        // Release references
        CFRelease(sock->cf_read_stream);
        CFRelease(sock->cf_write_stream);
        
        CFReadStreamOpen(sock->cf_read_stream);
        CFWriteStreamOpen(sock->cf_write_stream);
    }
    
    return 0;
}
Пример #21
0
/* extern */ Boolean
_CFServerStart(_CFServerRef server, CFStringRef name, CFStringRef type, UInt32 port) {
	
	Server* s = (Server*)server;

	CFDataRef address = NULL;
	
	do {
        unsigned i;
		CFRunLoopRef rl = CFRunLoopGetCurrent();
        CFAllocatorRef alloc = CFGetAllocator(server);
		
        struct sockaddr_in addr4;
        struct sockaddr_in6 addr6;
				
		// Make sure the port is valid (0 - 65535).
		if ((port & 0xFFFF0000U) != 0)
			break;
		
		// NULL means to use the machine name.
		if (name == NULL)
			name = _kCFServerEmptyString;
		
		for (i = 0; i < (sizeof(s->_sockets) / sizeof(s->_sockets[0])); i++) {
		
			// Create the run loop source for putting on the run loop.
			CFRunLoopSourceRef src = CFSocketCreateRunLoopSource(alloc, s->_sockets[i], 0);
			if (src == NULL)
				break;
				
			// Add the run loop source to the current run loop and default mode.
			CFRunLoopAddSource(rl, src, kCFRunLoopCommonModes);
			CFRelease(src);
		}

		memset(&addr4, 0, sizeof(addr4));
		
		// Put the local port and address into the native address.
#if !defined(__WIN32__)
        addr4.sin_len = sizeof(addr4);
#endif
		addr4.sin_family = AF_INET;
		addr4.sin_port = htons((UInt16)port);
		addr4.sin_addr.s_addr = htonl(INADDR_ANY);
		
		// Wrap the native address structure for CFSocketCreate.
		address = CFDataCreateWithBytesNoCopy(alloc, (const UInt8*)&addr4, sizeof(addr4), kCFAllocatorNull);
		
		// If it failed to create the address data, bail.
		if (address == NULL)
			break;
			
		// Set the local binding which causes the socket to start listening.
		if (CFSocketSetAddress(s->_sockets[0], address) != kCFSocketSuccess)
			break;
		
		CFRelease(address);
		
		address = CFSocketCopyAddress(s->_sockets[0]);
		memcpy(&addr4, CFDataGetBytePtr(address), CFDataGetLength(address));
            
		port = ntohs(addr4.sin_port);

		CFRelease(address);

		memset(&addr6, 0, sizeof(addr6));

        // Put the local port and address into the native address.
        addr6.sin6_family = AF_INET6;
#ifndef __WIN32__
        addr6.sin6_port = htons((UInt16)port);
        addr6.sin6_len = sizeof(addr6);
        memcpy(&(addr6.sin6_addr), &in6addr_any, sizeof(addr6.sin6_addr));
#else
#ifndef __MINGW32__
        // real MS headers have this
        IN6ADDR_SETANY(addr6);
        addr6.sin6_port = htons((UInt16)port);
#else
        addr6.sin6_port = htons((UInt16)port);
        // mingw's w32 headers have this INIT macro instead, for some odd reason
        struct sockaddr_in6 in6addr_any = IN6ADDR_ANY_INIT;
        memcpy(&(addr6.sin6_addr), &in6addr_any, sizeof(addr6.sin6_addr));
#endif
#endif
        
		// Wrap the native address structure for CFSocketCreate.
		address = CFDataCreateWithBytesNoCopy(alloc, (const UInt8*)&addr6, sizeof(addr6), kCFAllocatorNull);
			
		// Set the local binding which causes the socket to start listening.
		if (CFSocketSetAddress(s->_sockets[1], address) != kCFSocketSuccess)
			break;
		
		// Save the name, service type and port.
        s->_name = CFRetain(name);
		s->_type = type ? CFRetain(type) : NULL;
		s->_port = port;

#if defined(__MACH__)
        // Attempt to register the service on the network. 
		if (type && !_ServerCreateAndRegisterNetService(s))
            break;
#endif

        // Release this since it's not needed any longer. 
		CFRelease(address);
	
		return TRUE;
        
	} while (0);
	
	// Handle the error cleanup.
	
	// Release the address data if it was created.
	if (address)
		CFRelease(address);

	// Kill the socket if it was created.
	_ServerReleaseSocket(s);

	return FALSE;
}
Пример #22
0
static void DeviceNotificationCallback(am_device_notification_callback_info *info, void *unknown)
{
    struct am_device *device = info->dev;
    switch (info->msg) {
        case ADNCI_MSG_CONNECTED: {
            if (debug) {
                CFStringRef deviceId = AMDeviceCopyDeviceIdentifier(device);
                CFStringRef str = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("deviceconsole connected: %@"), deviceId);
                CFRelease(deviceId);
                CFShow(str);
                CFRelease(str);
            }
            if (requiredDeviceId) {
                CFStringRef deviceId = AMDeviceCopyDeviceIdentifier(device);
                Boolean isRequiredDevice = CFEqual(deviceId, requiredDeviceId);
                CFRelease(deviceId);
                if (!isRequiredDevice)
                    break;
            }
            if (AMDeviceConnect(device) == MDERR_OK) {
                if (AMDeviceIsPaired(device) && (AMDeviceValidatePairing(device) == MDERR_OK)) {
                    if (AMDeviceStartSession(device) == MDERR_OK) {
                        service_conn_t connection;
                        if (AMDeviceStartService(device, AMSVC_SYSLOG_RELAY, &connection, NULL) == MDERR_OK) {
                            CFSocketRef socket = CFSocketCreateWithNative(kCFAllocatorDefault, connection, kCFSocketDataCallBack, SocketCallback, NULL);
                            if (socket) {
                                CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0);
                                if (source) {
                                    CFRunLoopAddSource(CFRunLoopGetMain(), source, kCFRunLoopCommonModes);
                                    AMDeviceRetain(device);
                                    DeviceConsoleConnection *data = malloc(sizeof *data);
                                    data->connection = connection;
                                    data->socket = socket;
                                    data->source = source;
                                    CFDictionarySetValue(liveConnections, device, data);
                                    return;
                                }
                                CFRelease(source);
                            }
                        }
                        AMDeviceStopSession(device);
                    }
                }
            }
            AMDeviceDisconnect(device);
            break;
        }
        case ADNCI_MSG_DISCONNECTED: {
            if (debug) {
                CFStringRef deviceId = AMDeviceCopyDeviceIdentifier(device);
                CFStringRef str = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("deviceconsole disconnected: %@"), deviceId);
                CFRelease(deviceId);
                CFShow(str);
                CFRelease(str);
            }
            DeviceConsoleConnection *data = (DeviceConsoleConnection *)CFDictionaryGetValue(liveConnections, device);
            if (data) {
                CFDictionaryRemoveValue(liveConnections, device);
                AMDeviceRelease(device);
                CFRunLoopRemoveSource(CFRunLoopGetMain(), data->source, kCFRunLoopCommonModes);
                CFRelease(data->source);
                CFRelease(data->socket);
                free(data);
                AMDeviceStopSession(device);
                AMDeviceDisconnect(device);
            }
            break;
        }
        default:
            break;
    }
}
Пример #23
0
void StartDebuggingAndDetach(char *udid, char *app_path) {
	SDMMD_AMDeviceRef device = FindDeviceFromUDID(udid);
	if (device) {

		CFStringRef bundleId = CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8 *)app_path, strlen(app_path), kCFStringEncodingUTF8, false);
		CFURLRef relative_url = CFURLCreateWithFileSystemPath(NULL, bundleId, kCFURLPOSIXPathStyle, false);
		CFURLRef disk_app_url = CFURLCopyAbsoluteURL(relative_url);
		CFStringRef bundle_identifier = copy_disk_app_identifier(disk_app_url);

		
		SDMMD_AMDebugConnectionRef debug = SDMMD_AMDebugConnectionCreateForDevice(device);
		SDMMD_AMDebugConnectionStart(debug);
		
		uintptr_t socket = SDMMD_AMDServiceConnectionGetSocket(debug->connection);
		CFSocketContext context = { 0, (void*)socket, NULL, NULL, NULL };
		CFSocketRef fdvendor = CFSocketCreate(NULL, AF_UNIX, 0, 0, kCFSocketAcceptCallBack, &socket_callback, &context);
		
		int yes = 1;
		setsockopt(CFSocketGetNative(fdvendor), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
		
		struct sockaddr_un address;
		memset(&address, 0, sizeof(address));
		address.sun_family = AF_UNIX;
		strcpy(address.sun_path, SDM_LLDB_SOCKET);
		address.sun_len = SUN_LEN(&address);
		CFDataRef address_data = CFDataCreate(NULL, (const UInt8 *)&address, sizeof(address));
		
		unlink(SDM_LLDB_SOCKET);
		
		CFSocketSetAddress(fdvendor, address_data);
		CFRelease(address_data);
		CFRunLoopAddSource(CFRunLoopGetMain(), CFSocketCreateRunLoopSource(NULL, fdvendor, 0), kCFRunLoopCommonModes);
		
		SDMMD_AMDeviceRef device = SDMMD_AMDServiceConnectionGetDevice(debug->connection);
		CFMutableStringRef cmds = CFStringCreateMutableCopy(NULL, 0, LLDB_PREP_CMDS);
		CFRange range = { 0, CFStringGetLength(cmds) };
		
		CFURLRef device_app_url = copy_device_app_url(device, bundle_identifier);
		CFStringRef device_app_path = CFURLCopyFileSystemPath(device_app_url, kCFURLPOSIXPathStyle);
		CFStringFindAndReplace(cmds, CFSTR("{DEVICE_PATH}"), device_app_path, range, 0);
		range.length = CFStringGetLength(cmds);
		
		CFStringRef disk_app_path = CFURLCopyFileSystemPath(disk_app_url, kCFURLPOSIXPathStyle);
		CFStringFindAndReplace(cmds, CFSTR("{APP_PATH}"), disk_app_path, range, 0);
		range.length = CFStringGetLength(cmds);
		
		CFURLRef device_container_url = CFURLCreateCopyDeletingLastPathComponent(NULL, device_app_url);
		CFStringRef device_container_path = CFURLCopyFileSystemPath(device_container_url, kCFURLPOSIXPathStyle);
		CFMutableStringRef dcp_noprivate = CFStringCreateMutableCopy(NULL, 0, device_container_path);
		range.length = CFStringGetLength(dcp_noprivate);
		CFStringFindAndReplace(dcp_noprivate, CFSTR("/private/var/"), CFSTR("/var/"), range, 0);
		range.length = CFStringGetLength(cmds);
		CFStringFindAndReplace(cmds, CFSTR("{device_container}"), dcp_noprivate, range, 0);
		range.length = CFStringGetLength(cmds);
		
		CFURLRef disk_container_url = CFURLCreateCopyDeletingLastPathComponent(NULL, disk_app_url);
		CFStringRef disk_container_path = CFURLCopyFileSystemPath(disk_container_url, kCFURLPOSIXPathStyle);
		CFStringFindAndReplace(cmds, CFSTR("{disk_container}"), disk_container_path, range, 0);
		
		CFDataRef cmds_data = CFStringCreateExternalRepresentation(NULL, cmds, kCFStringEncodingASCII, 0);
		FILE *out = fopen(PREP_CMDS_PATH, "w");
		fwrite(CFDataGetBytePtr(cmds_data), CFDataGetLength(cmds_data), 1, out);
		fclose(out);
		
		CFSafeRelease(cmds);
		CFSafeRelease(bundle_identifier);
		CFSafeRelease(device_app_url);
		CFSafeRelease(device_app_path);
		CFSafeRelease(disk_app_path);
		CFSafeRelease(device_container_url);
		CFSafeRelease(device_container_path);
		CFSafeRelease(dcp_noprivate);
		CFSafeRelease(disk_container_url);
		CFSafeRelease(disk_container_path);
		CFSafeRelease(cmds_data);
		
		signal(SIGHUP, exit);
		
		pid_t parent = getpid();
		int pid = fork();
		if (pid == 0) {
			system("xcrun -sdk iphoneos lldb /tmp/sdmmd-lldb-prep");
			kill(parent, SIGHUP);
			_exit(0);
		}
		CFRunLoopRun();
	}
}
Пример #24
0
/*------------------------------------ udpPortSetPort ---*/
bool
udpPortSetPort(UdpObjectData * xx,
               const bool      bangOnError)
{
    bool okSoFar = true;

    if (xx)
    {
        if (xx->fSocket)
        {
            CFSocketInvalidate(xx->fSocket);
            CFRelease(xx->fSocket);
            xx->fSocket = NULL;
        }
        int err;
        int sock = socket(AF_INET, SOCK_DGRAM, 0);

        if (0 > sock)
        {
            err = errno;
            okSoFar = false;
        }
        else
        {
            err = 0;
        }
        if (okSoFar)
        {
            sockaddr_in addr;

            memset(&addr, 0, sizeof(addr));
            addr.sin_len = sizeof(addr);
            addr.sin_family = AF_INET;
            addr.sin_port = htons(xx->fSelfPort);
            addr.sin_addr.s_addr = INADDR_ANY;
            err = bind(sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr));
            if (0 > err)
            {
                err = errno;
                okSoFar = false;
            }
        }
        if (okSoFar)
        {
            int flags = fcntl(sock, F_GETFL);

            err = fcntl(sock, F_SETFL, flags | O_NONBLOCK);
            if (0 > err)
            {
                err = errno;
                okSoFar = false;
            }
        }
        if (okSoFar)
        {
            const CFSocketContext context = { 0, xx, NULL, NULL, NULL };

            xx->fSocket = CFSocketCreateWithNative(kCFAllocatorDefault, sock, kCFSocketReadCallBack,
                                                   socketReadCallback, &context);
            sock = -1;
            CFRunLoopSourceRef rls = CFSocketCreateRunLoopSource(kCFAllocatorDefault, xx->fSocket,
                                                                 0);

            CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
            CFRelease(rls);
            setObjectState(xx, kUdpStateBound);
        }
        if (! okSoFar)
        {
            close(sock);
            LOG_ERROR_2(xx, OUTPUT_PREFIX "set port failed (%ld)", static_cast<long>(err))
            if (bangOnError)
            {
                signalError(xx);
            }
        }
    }
bool wxServiceDiscoveryTaskBase::Start( void )
{
	wxASSERT( m_rServiceRef == NULL );

	bool output = DoStart();
	
	if ( output )
	{
		wxASSERT( m_rServiceRef != NULL );
		
		if ( m_rServiceRef != NULL )
		{
			if ( m_bUseThreads )
			{
				wxASSERT( m_pThread == NULL );
				
				if ( m_pThread != NULL )
				{
					m_pThread->Delete();
					delete m_pThread;
					m_pThread = NULL;
				}
				
				m_pThread = new wxServiceDiscoveryServiceHelperThread( m_rServiceRef );
				
				if ( m_pThread != NULL )
				{
					m_pThread->Create();
					m_pThread->Run();
				}
			}
			else
			{
				dns_sd_fd = DNSServiceRefSockFD( m_rServiceRef );

				#ifdef __DARWIN__
				
					CFSocketContext context = { 0, this, NULL, NULL, NULL }; 
				
					cfsocket = CFSocketCreateWithNative(NULL, 
														dns_sd_fd, 
														kCFSocketReadCallBack, 
														wxServiceDiscoveryTaskBase::Mac_Socket_Callback, 
														&context);
					
					// Prevent CFSocketInvalidate from closing DNSServiceRef's socket. 
					CFOptionFlags f = CFSocketGetSocketFlags( cfsocket ); 
					CFSocketSetSocketFlags( cfsocket, f & ~kCFSocketCloseOnInvalidate); 
					
					source = CFSocketCreateRunLoopSource( NULL, cfsocket, 0 ); 
					CFRunLoopAddSource( CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes );
					
				#elif defined( _WIN32 )
					
				    // Create the window. This window won't actually be shown, 
					// but it demonstrates how to use DNS-SD with Windows GUI 
					// applications by having DNS-SD events processed as messages 
					// to a Window. 
					wind = CreateWindow(GetWndClass().lpszClassName, 
										GetWndClass().lpszClassName,
										0, 
										CW_USEDEFAULT,
										0,
										CW_USEDEFAULT,
										0,
										HWND_MESSAGE, // requires 2000/XP http://msdn2.microsoft.com/en-us/library/ms632599.aspx#message_only
										NULL,
										instance,
										NULL ); 

					BOOL status = SetProp( wind, PROPNAME, this );
					wxASSERT( status != 0 );
					
					// Associate the DNS-SD browser with our window 
					// using the WSAAsyncSelect mechanism. Whenever something 
					// related to the DNS-SD browser occurs, our private Windows message 
					// will be sent to our window so we can give DNS-SD a 
					// chance to process it. This allows DNS-SD to avoid using a 
					// secondary thread (and all the issues with synchronization that 
					// would introduce), but still process everything asynchronously. 
					
					// This also simplifies app code because DNS-SD will only run when we 
					// explicitly call it. 
					
					err = WSAAsyncSelect( dns_sd_fd, 
										  wind, 
										  DNSSD_EVENT, 
										  FD_READ | FD_CLOSE); 
					
					assert(err == kDNSServiceErr_NoError); 

				#else
					
					nfds = dns_sd_fd + 1;
					
					FD_ZERO(&readfds);
					FD_SET(dns_sd_fd,&readfds);
					
				#endif
			}
		}
	}
	
	return output;	
}