Esempio n. 1
0
static int _netservice_get_first_addr( CFNetServiceRef netService, socklen_t * size, struct sockaddr ** addr ) {
  CFArrayRef addresses;
  CFDataRef data;
  CFNetServiceCancel( netService );
  CFNetServiceResolveWithTimeout( netService, 0.1, NULL );
  addresses = CFNetServiceGetAddressing( netService );
  if( addresses == NULL || CFArrayGetCount( addresses ) < 1 ) {
    return 1;
  }
  data  = CFArrayGetValueAtIndex( addresses, 0 );
  *size = CFDataGetLength( data );
  *addr = CFDataGetBytePtr( data );
  
  return 0;
}
Esempio n. 2
0
void ofxBonjourIp::NetServiceResolvedCallBack(CFNetServiceRef theService, CFStreamError* error, void* info) {
    
    //ofLog() << "netService resolved";
    bool serviceResolved = false;
    if(error->error != 0) ofLog() << "Error: " << error->error;
    
    CFArrayRef addresses = CFNetServiceGetAddressing(theService);
    struct sockaddr * socketAddress = NULL;
    
    for(int i=0; i < CFArrayGetCount(addresses); i++) {
        
        // error stopped here once!
        socketAddress = (struct sockaddr *) CFDataGetBytePtr((CFDataRef)CFArrayGetValueAtIndex(addresses, i));
        
        /* Only continue if this is an IPv4 address. */
        //|| socketAddress->sa_family == AF_INET6 ) == 0.0.0.0
        if (socketAddress && socketAddress->sa_family == AF_INET && info != NULL) {
            string addr = inet_ntoa(((struct sockaddr_in *)socketAddress)->sin_addr); // pdp_ip0
            int port = ntohs(((struct sockaddr_in *)socketAddress)->sin_port);
            
            // don't connect to self or 127.0.0.1 or 0.0.0.0
            ofxBonjourIp* bonjour = (ofxBonjourIp*)info;
            if(addr != "0.0.0.0" && addr != "127.0.0.1" && addr != bonjour->deviceIp) {
                
                serviceResolved = true;
                ofLog() << "* Successful connection: " << addr << ", " << port;
                // info has a reference to the class object
                
                // set the server ip
                bonjour->serverIp = addr;
                bonjour->serverHostName = CFStringGetCStringPtr(CFNetServiceGetTargetHost(theService), kCFStringEncodingMacRoman);
                bonjour->connectedToService = true;
                
                ofNotifyEvent(bonjour->discoveredServiceEvent,bonjour->serverIp,bonjour);
                
            } else {
                ofLog() << "Not connecting to self: " << addr << ", " << port;
            }
            
            
        }
    }
    
    // all the service details
    /*const char *host = CFStringGetCStringPtr(CFNetServiceGetTargetHost(theService), kCFStringEncodingMacRoman); //trents-MacBook-Pro.local
     ofLog() << "host: " << host;
     const char *type = CFStringGetCStringPtr(CFNetServiceGetType(theService), kCFStringEncodingMacRoman); //_ofxBonjourIp._tcp.
     ofLog() << "type: " << type;
     const char *domain = CFStringGetCStringPtr(CFNetServiceGetDomain(theService), kCFStringEncodingMacRoman); //local.
     ofLog() << "domain: " << domain;
     int port = CFNetServiceGetPortNumber(theService); //7777
     ofLog() << "port: " << port;
     const char *name = CFStringGetCStringPtr(CFNetServiceGetName(theService), kCFStringEncodingMacRoman);
     ofLog() << "name: " << name; // name is "" if not defined. should become device name once resolved?*/
    
    
    // release stuff (loop and callback)
    if(!serviceResolved) {
        CFNetServiceUnscheduleFromRunLoop(theService, CFRunLoopGetCurrent(),kCFRunLoopCommonModes);
        CFNetServiceSetClient(theService, NULL, NULL);
    }
    //CFNetServiceUnscheduleFromRunLoop(theService, CFRunLoopGetCurrent(),kCFRunLoopCommonModes);
    //CFNetServiceSetClient(theService, NULL, NULL);
    //CFRelease(theService); breaks stuff
    //CFRelease(addresses);
}